Server/MainForm.cs
author StephaneLenclud
Wed, 03 Feb 2016 16:04:37 +0100
changeset 193 317fdfa4fbb6
parent 188 12372d6d7a47
child 194 b0c0aa5aea05
permissions -rw-r--r--
Basic idle client functionality.
StephaneLenclud@123
     1
//
StephaneLenclud@123
     2
// Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@123
     3
//
StephaneLenclud@123
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@123
     5
//
StephaneLenclud@123
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@123
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123
     9
// (at your option) any later version.
StephaneLenclud@123
    10
//
StephaneLenclud@123
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@123
    14
// GNU General Public License for more details.
StephaneLenclud@123
    15
//
StephaneLenclud@123
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@123
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@123
    18
//
StephaneLenclud@123
    19
StephaneLenclud@123
    20
using System;
sl@0
    21
using System.Collections.Generic;
sl@0
    22
using System.ComponentModel;
sl@0
    23
using System.Data;
sl@0
    24
using System.Drawing;
sl@0
    25
using System.Linq;
sl@0
    26
using System.Text;
sl@0
    27
using System.Threading.Tasks;
sl@0
    28
using System.Windows.Forms;
sl@14
    29
using System.IO;
sl@0
    30
using CodeProject.Dialog;
sl@14
    31
using System.Drawing.Imaging;
sl@17
    32
using System.ServiceModel;
sl@25
    33
using System.Threading;
sl@31
    34
using System.Diagnostics;
sl@88
    35
using System.Deployment.Application;
sl@94
    36
using System.Reflection;
StephaneLenclud@112
    37
//NAudio
StephaneLenclud@112
    38
using NAudio.CoreAudioApi;
StephaneLenclud@112
    39
using NAudio.CoreAudioApi.Interfaces;
StephaneLenclud@112
    40
using System.Runtime.InteropServices;
StephaneLenclud@117
    41
//Network
StephaneLenclud@117
    42
using NETWORKLIST;
sl@25
    43
//
sl@25
    44
using SharpDisplayClient;
sl@55
    45
using SharpDisplay;
StephaneLenclud@135
    46
using MiniDisplayInterop;
StephaneLenclud@171
    47
using SharpLib.Display;
StephaneLenclud@124
    48
sl@0
    49
namespace SharpDisplayManager
sl@0
    50
{
sl@58
    51
    //Types declarations
sl@58
    52
    public delegate uint ColorProcessingDelegate(int aX, int aY, uint aPixel);
sl@58
    53
    public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
sl@62
    54
    //Delegates are used for our thread safe method
sl@62
    55
    public delegate void AddClientDelegate(string aSessionId, ICallback aCallback);
sl@62
    56
    public delegate void RemoveClientDelegate(string aSessionId);
sl@79
    57
    public delegate void SetFieldDelegate(string SessionId, DataField aField);
sl@79
    58
    public delegate void SetFieldsDelegate(string SessionId, System.Collections.Generic.IList<DataField> aFields);
sl@62
    59
    public delegate void SetLayoutDelegate(string SessionId, TableLayout aLayout);
sl@62
    60
    public delegate void SetClientNameDelegate(string aSessionId, string aName);
StephaneLenclud@184
    61
    public delegate void SetClientPriorityDelegate(string aSessionId, uint aPriority);
StephaneLenclud@184
    62
    public delegate void PlainUpdateDelegate();
StephaneLenclud@167
    63
    public delegate void WndProcDelegate(ref Message aMessage);
sl@58
    64
sl@58
    65
    /// <summary>
sl@58
    66
    /// Our Display manager main form
sl@58
    67
    /// </summary>
StephaneLenclud@126
    68
	[System.ComponentModel.DesignerCategory("Form")]
StephaneLenclud@126
    69
	public partial class MainForm : MainFormHid, IMMNotificationClient
sl@0
    70
    {
sl@2
    71
        DateTime LastTickTime;
sl@3
    72
        Display iDisplay;
sl@14
    73
        System.Drawing.Bitmap iBmp;
sl@14
    74
        bool iCreateBitmap; //Workaround render to bitmap issues when minimized
sl@17
    75
        ServiceHost iServiceHost;
sl@65
    76
        // Our collection of clients sorted by session id.
sl@33
    77
        public Dictionary<string, ClientData> iClients;
sl@65
    78
        // The name of the client which informations are currently displayed.
sl@65
    79
        public string iCurrentClientSessionId;
sl@65
    80
        ClientData iCurrentClientData;
sl@65
    81
        //
sl@29
    82
        public bool iClosing;
StephaneLenclud@122
    83
		//
StephaneLenclud@122
    84
		public bool iSkipFrameRendering;
sl@65
    85
        //Function pointer for pixel color filtering
sl@58
    86
        ColorProcessingDelegate iColorFx;
sl@65
    87
        //Function pointer for pixel X coordinate intercept
sl@58
    88
        CoordinateTranslationDelegate iScreenX;
sl@65
    89
        //Function pointer for pixel Y coordinate intercept
sl@58
    90
        CoordinateTranslationDelegate iScreenY;
StephaneLenclud@112
    91
		//NAudio
StephaneLenclud@112
    92
		private MMDeviceEnumerator iMultiMediaDeviceEnumerator;
StephaneLenclud@112
    93
		private MMDevice iMultiMediaDevice;
StephaneLenclud@117
    94
		//Network
StephaneLenclud@117
    95
		private NetworkManager iNetworkManager;
StephaneLenclud@167
    96
StephaneLenclud@167
    97
        /// <summary>
StephaneLenclud@167
    98
        /// CEC - Consumer Electronic Control.
StephaneLenclud@167
    99
        /// Notably used to turn TV on and off as Windows broadcast monitor on and off notifications.
StephaneLenclud@167
   100
        /// </summary>
StephaneLenclud@167
   101
        private ConsumerElectronicControl iCecManager;
StephaneLenclud@112
   102
		
sl@94
   103
		/// <summary>
sl@94
   104
		/// Manage run when Windows startup option
sl@94
   105
		/// </summary>
sl@92
   106
		private StartupManager iStartupManager;
sl@92
   107
sl@94
   108
		/// <summary>
StephaneLenclud@179
   109
		/// System notification icon used to hide our application from the task bar.
sl@94
   110
		/// </summary>
StephaneLenclud@173
   111
		private SharpLib.Notification.Control iNotifyIcon;
sl@94
   112
StephaneLenclud@167
   113
        /// <summary>
StephaneLenclud@179
   114
        /// System recording notifcation icon.
StephaneLenclud@179
   115
        /// </summary>
StephaneLenclud@179
   116
        private SharpLib.Notification.Control iRecordingNotification;
StephaneLenclud@179
   117
StephaneLenclud@179
   118
StephaneLenclud@179
   119
        /// <summary>
StephaneLenclud@167
   120
        /// Allow user to receive window messages;
StephaneLenclud@167
   121
        /// </summary>
StephaneLenclud@167
   122
        public event WndProcDelegate OnWndProc;
StephaneLenclud@167
   123
sl@0
   124
        public MainForm()
sl@0
   125
        {
StephaneLenclud@122
   126
			iSkipFrameRendering = false;
StephaneLenclud@122
   127
			iClosing = false;
sl@65
   128
            iCurrentClientSessionId = "";
sl@65
   129
            iCurrentClientData = null;
sl@2
   130
            LastTickTime = DateTime.Now;
StephaneLenclud@104
   131
			//Instantiate our display and register for events notifications
sl@3
   132
            iDisplay = new Display();
StephaneLenclud@104
   133
			iDisplay.OnOpened += OnDisplayOpened;
StephaneLenclud@104
   134
			iDisplay.OnClosed += OnDisplayClosed;
StephaneLenclud@104
   135
			//
StephaneLenclud@104
   136
			iClients = new Dictionary<string, ClientData>();
sl@92
   137
			iStartupManager = new StartupManager();
StephaneLenclud@173
   138
			iNotifyIcon = new SharpLib.Notification.Control();
StephaneLenclud@179
   139
            iRecordingNotification = new SharpLib.Notification.Control();
sl@2
   140
StephaneLenclud@179
   141
            //Have our designer initialize its controls
sl@0
   142
            InitializeComponent();
StephaneLenclud@104
   143
StephaneLenclud@104
   144
			//Populate device types
StephaneLenclud@104
   145
			PopulateDeviceTypes();
StephaneLenclud@104
   146
StephaneLenclud@152
   147
            //Populate optical drives
StephaneLenclud@152
   148
            PopulateOpticalDrives();
StephaneLenclud@152
   149
StephaneLenclud@104
   150
			//Initial status update 
sl@7
   151
            UpdateStatus();
StephaneLenclud@104
   152
sl@14
   153
            //We have a bug when drawing minimized and reusing our bitmap
StephaneLenclud@158
   154
            //Though I could not reproduce it on Windows 10
StephaneLenclud@175
   155
            iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
   156
            iCreateBitmap = false;
sl@94
   157
StephaneLenclud@104
   158
			//Minimize our window if desired
sl@94
   159
			if (Properties.Settings.Default.StartMinimized)
sl@94
   160
			{
sl@94
   161
				WindowState = FormWindowState.Minimized;
sl@94
   162
			}
sl@94
   163
sl@0
   164
        }
sl@0
   165
sl@94
   166
		/// <summary>
StephaneLenclud@106
   167
		///
StephaneLenclud@106
   168
		/// </summary>
StephaneLenclud@106
   169
		/// <param name="sender"></param>
StephaneLenclud@106
   170
		/// <param name="e"></param>
StephaneLenclud@106
   171
        private void MainForm_Load(object sender, EventArgs e)
StephaneLenclud@106
   172
        {
StephaneLenclud@106
   173
			//Check if we are running a Click Once deployed application
StephaneLenclud@106
   174
			if (ApplicationDeployment.IsNetworkDeployed)
StephaneLenclud@106
   175
			{
StephaneLenclud@106
   176
				//This is a proper Click Once installation, fetch and show our version number
StephaneLenclud@106
   177
				this.Text += " - v" + ApplicationDeployment.CurrentDeployment.CurrentVersion;
StephaneLenclud@106
   178
			}
StephaneLenclud@106
   179
			else
StephaneLenclud@106
   180
			{
StephaneLenclud@106
   181
				//Not a proper Click Once installation, assuming development build then
StephaneLenclud@106
   182
				this.Text += " - development";
StephaneLenclud@106
   183
			}
StephaneLenclud@106
   184
StephaneLenclud@112
   185
			//NAudio
StephaneLenclud@112
   186
			iMultiMediaDeviceEnumerator = new MMDeviceEnumerator();
StephaneLenclud@117
   187
			iMultiMediaDeviceEnumerator.RegisterEndpointNotificationCallback(this);			
StephaneLenclud@112
   188
			UpdateAudioDeviceAndMasterVolumeThreadSafe();
StephaneLenclud@112
   189
StephaneLenclud@117
   190
			//Network
StephaneLenclud@117
   191
			iNetworkManager = new NetworkManager();
StephaneLenclud@117
   192
			iNetworkManager.OnConnectivityChanged += OnConnectivityChanged;
StephaneLenclud@117
   193
			UpdateNetworkStatus();
StephaneLenclud@117
   194
StephaneLenclud@167
   195
            //CEC
StephaneLenclud@167
   196
            iCecManager = new ConsumerElectronicControl();
StephaneLenclud@167
   197
            OnWndProc += iCecManager.OnWndProc;
StephaneLenclud@168
   198
            ResetCec();
StephaneLenclud@168
   199
StephaneLenclud@167
   200
StephaneLenclud@167
   201
            //Setup notification icon
StephaneLenclud@167
   202
            SetupTrayIcon();
StephaneLenclud@106
   203
StephaneLenclud@179
   204
            //Setup recording notification
StephaneLenclud@179
   205
            SetupRecordingNotification();
StephaneLenclud@179
   206
StephaneLenclud@179
   207
            // To make sure start up with minimize to tray works
StephaneLenclud@179
   208
            if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
StephaneLenclud@106
   209
			{
StephaneLenclud@106
   210
				Visible = false;
StephaneLenclud@106
   211
			}
StephaneLenclud@106
   212
StephaneLenclud@106
   213
#if !DEBUG
StephaneLenclud@106
   214
			//When not debugging we want the screen to be empty until a client takes over
StephaneLenclud@106
   215
			ClearLayout();
StephaneLenclud@106
   216
#else
StephaneLenclud@106
   217
			//When developing we want at least one client for testing
StephaneLenclud@106
   218
			StartNewClient("abcdefghijklmnopqrst-0123456789","ABCDEFGHIJKLMNOPQRST-0123456789");
StephaneLenclud@106
   219
#endif
StephaneLenclud@106
   220
StephaneLenclud@106
   221
			//Open display connection on start-up if needed
StephaneLenclud@106
   222
			if (Properties.Settings.Default.DisplayConnectOnStartup)
StephaneLenclud@106
   223
			{
StephaneLenclud@106
   224
				OpenDisplayConnection();
StephaneLenclud@106
   225
			}
StephaneLenclud@106
   226
StephaneLenclud@106
   227
			//Start our server so that we can get client requests
StephaneLenclud@106
   228
			StartServer();
StephaneLenclud@124
   229
StephaneLenclud@124
   230
			//Register for HID events
StephaneLenclud@124
   231
			RegisterHidDevices();
StephaneLenclud@106
   232
        }
StephaneLenclud@106
   233
StephaneLenclud@106
   234
		/// <summary>
StephaneLenclud@104
   235
		/// Called when our display is opened.
StephaneLenclud@104
   236
		/// </summary>
StephaneLenclud@104
   237
		/// <param name="aDisplay"></param>
StephaneLenclud@104
   238
		private void OnDisplayOpened(Display aDisplay)
StephaneLenclud@104
   239
		{
StephaneLenclud@187
   240
            //Make sure we resume frame rendering
StephaneLenclud@187
   241
            iSkipFrameRendering = false;
StephaneLenclud@122
   242
StephaneLenclud@105
   243
			//Set our screen size now that our display is connected
StephaneLenclud@105
   244
			//Our panelDisplay is the container of our tableLayoutPanel
StephaneLenclud@105
   245
			//tableLayoutPanel will resize itself to fit the client size of our panelDisplay
StephaneLenclud@105
   246
			//panelDisplay needs an extra 2 pixels for borders on each sides
StephaneLenclud@105
   247
			//tableLayoutPanel will eventually be the exact size of our display
StephaneLenclud@105
   248
			Size size = new Size(iDisplay.WidthInPixels() + 2, iDisplay.HeightInPixels() + 2);
StephaneLenclud@105
   249
			panelDisplay.Size = size;
StephaneLenclud@105
   250
StephaneLenclud@104
   251
			//Our display was just opened, update our UI
StephaneLenclud@104
   252
			UpdateStatus();
StephaneLenclud@104
   253
			//Initiate asynchronous request
StephaneLenclud@104
   254
			iDisplay.RequestFirmwareRevision();
StephaneLenclud@108
   255
StephaneLenclud@117
   256
			//Audio
StephaneLenclud@112
   257
			UpdateMasterVolumeThreadSafe();
StephaneLenclud@117
   258
			//Network
StephaneLenclud@117
   259
			UpdateNetworkStatus();
StephaneLenclud@112
   260
StephaneLenclud@108
   261
#if DEBUG
StephaneLenclud@108
   262
			//Testing icon in debug, no arm done if icon not supported
StephaneLenclud@109
   263
			//iDisplay.SetIconStatus(Display.TMiniDisplayIconType.EMiniDisplayIconRecording, 0, 1);
StephaneLenclud@112
   264
			//iDisplay.SetAllIconsStatus(2);
StephaneLenclud@108
   265
#endif
StephaneLenclud@108
   266
StephaneLenclud@104
   267
		}
StephaneLenclud@104
   268
StephaneLenclud@104
   269
		/// <summary>
StephaneLenclud@104
   270
		/// Called when our display is closed.
StephaneLenclud@104
   271
		/// </summary>
StephaneLenclud@104
   272
		/// <param name="aDisplay"></param>
StephaneLenclud@104
   273
		private void OnDisplayClosed(Display aDisplay)
StephaneLenclud@104
   274
		{
StephaneLenclud@187
   275
            //Our display was just closed, update our UI consequently
StephaneLenclud@187
   276
            UpdateStatus();
StephaneLenclud@104
   277
		}
StephaneLenclud@117
   278
StephaneLenclud@117
   279
		public void OnConnectivityChanged(NetworkManager aNetwork, NLM_CONNECTIVITY newConnectivity)
StephaneLenclud@117
   280
		{
StephaneLenclud@117
   281
			//Update network status
StephaneLenclud@117
   282
			UpdateNetworkStatus();			
StephaneLenclud@117
   283
		}
StephaneLenclud@117
   284
StephaneLenclud@117
   285
		/// <summary>
StephaneLenclud@117
   286
		/// Update our Network Status
StephaneLenclud@117
   287
		/// </summary>
StephaneLenclud@117
   288
		private void UpdateNetworkStatus()
StephaneLenclud@117
   289
		{
StephaneLenclud@117
   290
			if (iDisplay.IsOpen())
StephaneLenclud@117
   291
			{
StephaneLenclud@135
   292
                iDisplay.SetIconOnOff(MiniDisplay.IconType.Internet, iNetworkManager.NetworkListManager.IsConnectedToInternet);
StephaneLenclud@135
   293
                iDisplay.SetIconOnOff(MiniDisplay.IconType.NetworkSignal, iNetworkManager.NetworkListManager.IsConnected);
StephaneLenclud@117
   294
			}
StephaneLenclud@117
   295
		}
StephaneLenclud@117
   296
StephaneLenclud@117
   297
StephaneLenclud@118
   298
		int iLastNetworkIconIndex = 0;
StephaneLenclud@118
   299
		int iUpdateCountSinceLastNetworkAnimation = 0;
StephaneLenclud@118
   300
StephaneLenclud@118
   301
		/// <summary>
StephaneLenclud@118
   302
		/// 
StephaneLenclud@118
   303
		/// </summary>
StephaneLenclud@118
   304
		private void UpdateNetworkSignal(DateTime aLastTickTime, DateTime aNewTickTime)
StephaneLenclud@118
   305
		{
StephaneLenclud@118
   306
			iUpdateCountSinceLastNetworkAnimation++;
StephaneLenclud@118
   307
			iUpdateCountSinceLastNetworkAnimation = iUpdateCountSinceLastNetworkAnimation % 4;
StephaneLenclud@118
   308
StephaneLenclud@118
   309
			if (iDisplay.IsOpen() && iNetworkManager.NetworkListManager.IsConnected && iUpdateCountSinceLastNetworkAnimation==0)
StephaneLenclud@135
   310
			{
StephaneLenclud@135
   311
                int iconCount = iDisplay.IconCount(MiniDisplay.IconType.NetworkSignal);
StephaneLenclud@120
   312
				if (iconCount <= 0)
StephaneLenclud@120
   313
				{
StephaneLenclud@120
   314
					//Prevents div by zero and other undefined behavior
StephaneLenclud@120
   315
					return;
StephaneLenclud@120
   316
				}
StephaneLenclud@118
   317
				iLastNetworkIconIndex++;
StephaneLenclud@119
   318
				iLastNetworkIconIndex = iLastNetworkIconIndex % (iconCount*2);
StephaneLenclud@118
   319
				for (int i=0;i<iconCount;i++)
StephaneLenclud@118
   320
				{
StephaneLenclud@119
   321
					if (i < iLastNetworkIconIndex && !(i == 0 && iLastNetworkIconIndex > 3) && !(i == 1 && iLastNetworkIconIndex > 4))
StephaneLenclud@118
   322
					{
StephaneLenclud@135
   323
                        iDisplay.SetIconOn(MiniDisplay.IconType.NetworkSignal, i);
StephaneLenclud@118
   324
					}
StephaneLenclud@118
   325
					else
StephaneLenclud@118
   326
					{
StephaneLenclud@135
   327
                        iDisplay.SetIconOff(MiniDisplay.IconType.NetworkSignal, i);
StephaneLenclud@118
   328
					}
StephaneLenclud@118
   329
				}				
StephaneLenclud@118
   330
			}
StephaneLenclud@118
   331
		}
StephaneLenclud@118
   332
StephaneLenclud@118
   333
StephaneLenclud@118
   334
StephaneLenclud@112
   335
        /// <summary>
StephaneLenclud@112
   336
        /// Receive volume change notification and reflect changes on our slider.
StephaneLenclud@112
   337
        /// </summary>
StephaneLenclud@112
   338
        /// <param name="data"></param>
StephaneLenclud@112
   339
        public void OnVolumeNotificationThreadSafe(AudioVolumeNotificationData data)
StephaneLenclud@112
   340
        {
StephaneLenclud@112
   341
			UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   342
        }
StephaneLenclud@112
   343
StephaneLenclud@112
   344
        /// <summary>
StephaneLenclud@112
   345
        /// Update master volume when user moves our slider.
StephaneLenclud@112
   346
        /// </summary>
StephaneLenclud@112
   347
        /// <param name="sender"></param>
StephaneLenclud@112
   348
        /// <param name="e"></param>
StephaneLenclud@112
   349
        private void trackBarMasterVolume_Scroll(object sender, EventArgs e)
StephaneLenclud@112
   350
        {
StephaneLenclud@116
   351
			//Just like Windows Volume Mixer we unmute if the volume is adjusted
StephaneLenclud@116
   352
			iMultiMediaDevice.AudioEndpointVolume.Mute = false;
StephaneLenclud@116
   353
			//Set volume level according to our volume slider new position
StephaneLenclud@112
   354
			iMultiMediaDevice.AudioEndpointVolume.MasterVolumeLevelScalar = trackBarMasterVolume.Value / 100.0f;
StephaneLenclud@112
   355
        }
StephaneLenclud@112
   356
StephaneLenclud@113
   357
StephaneLenclud@113
   358
		/// <summary>
StephaneLenclud@113
   359
		/// Mute check box changed.
StephaneLenclud@113
   360
		/// </summary>
StephaneLenclud@113
   361
		/// <param name="sender"></param>
StephaneLenclud@113
   362
		/// <param name="e"></param>
StephaneLenclud@113
   363
		private void checkBoxMute_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@113
   364
		{
StephaneLenclud@113
   365
			iMultiMediaDevice.AudioEndpointVolume.Mute = checkBoxMute.Checked;
StephaneLenclud@113
   366
		}
StephaneLenclud@113
   367
StephaneLenclud@112
   368
        /// <summary>
StephaneLenclud@112
   369
        /// Device State Changed
StephaneLenclud@112
   370
        /// </summary>
StephaneLenclud@112
   371
        public void OnDeviceStateChanged([MarshalAs(UnmanagedType.LPWStr)] string deviceId, [MarshalAs(UnmanagedType.I4)] DeviceState newState){}
StephaneLenclud@112
   372
StephaneLenclud@112
   373
        /// <summary>
StephaneLenclud@112
   374
        /// Device Added
StephaneLenclud@112
   375
        /// </summary>
StephaneLenclud@112
   376
        public void OnDeviceAdded([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId) { }
StephaneLenclud@112
   377
StephaneLenclud@112
   378
        /// <summary>
StephaneLenclud@112
   379
        /// Device Removed
StephaneLenclud@112
   380
        /// </summary>
StephaneLenclud@112
   381
        public void OnDeviceRemoved([MarshalAs(UnmanagedType.LPWStr)] string deviceId) { }
StephaneLenclud@112
   382
StephaneLenclud@112
   383
        /// <summary>
StephaneLenclud@112
   384
        /// Default Device Changed
StephaneLenclud@112
   385
        /// </summary>
StephaneLenclud@112
   386
        public void OnDefaultDeviceChanged(DataFlow flow, Role role, [MarshalAs(UnmanagedType.LPWStr)] string defaultDeviceId)
StephaneLenclud@112
   387
        {
StephaneLenclud@112
   388
            if (role == Role.Multimedia && flow == DataFlow.Render)
StephaneLenclud@112
   389
            {
StephaneLenclud@112
   390
                UpdateAudioDeviceAndMasterVolumeThreadSafe();
StephaneLenclud@112
   391
            }
StephaneLenclud@112
   392
        }
StephaneLenclud@112
   393
StephaneLenclud@112
   394
        /// <summary>
StephaneLenclud@112
   395
        /// Property Value Changed
StephaneLenclud@112
   396
        /// </summary>
StephaneLenclud@112
   397
        /// <param name="pwstrDeviceId"></param>
StephaneLenclud@112
   398
        /// <param name="key"></param>
StephaneLenclud@112
   399
        public void OnPropertyValueChanged([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId, PropertyKey key){}
StephaneLenclud@112
   400
StephaneLenclud@112
   401
StephaneLenclud@112
   402
        
StephaneLenclud@112
   403
StephaneLenclud@112
   404
		/// <summary>
StephaneLenclud@116
   405
		/// Update master volume indicators based our current system states.
StephaneLenclud@116
   406
		/// This typically includes volume levels and mute status.
StephaneLenclud@112
   407
		/// </summary>
StephaneLenclud@112
   408
		private void UpdateMasterVolumeThreadSafe()
StephaneLenclud@112
   409
		{
StephaneLenclud@112
   410
			if (this.InvokeRequired)
StephaneLenclud@112
   411
			{
StephaneLenclud@112
   412
				//Not in the proper thread, invoke ourselves
StephaneLenclud@112
   413
				PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateMasterVolumeThreadSafe);
StephaneLenclud@112
   414
				this.Invoke(d, new object[] { });
StephaneLenclud@112
   415
				return;
StephaneLenclud@112
   416
			}
StephaneLenclud@112
   417
StephaneLenclud@113
   418
			//Update volume slider
StephaneLenclud@112
   419
			float volumeLevelScalar = iMultiMediaDevice.AudioEndpointVolume.MasterVolumeLevelScalar;
StephaneLenclud@112
   420
			trackBarMasterVolume.Value = Convert.ToInt32(volumeLevelScalar * 100);
StephaneLenclud@113
   421
			//Update mute checkbox
StephaneLenclud@113
   422
			checkBoxMute.Checked = iMultiMediaDevice.AudioEndpointVolume.Mute;
StephaneLenclud@112
   423
StephaneLenclud@116
   424
			//If our display connection is open we need to update its icons
StephaneLenclud@112
   425
			if (iDisplay.IsOpen())
StephaneLenclud@112
   426
			{
StephaneLenclud@116
   427
				//First take care our our volume level icons
StephaneLenclud@135
   428
                int volumeIconCount = iDisplay.IconCount(MiniDisplay.IconType.Volume);
StephaneLenclud@112
   429
				if (volumeIconCount > 0)
StephaneLenclud@114
   430
				{					
StephaneLenclud@116
   431
					//Compute current volume level from system level and the number of segments in our display volume bar.
StephaneLenclud@116
   432
					//That tells us how many segments in our volume bar needs to be turned on.
StephaneLenclud@116
   433
					float currentVolume = volumeLevelScalar * volumeIconCount;
StephaneLenclud@116
   434
					int segmentOnCount = Convert.ToInt32(currentVolume);
StephaneLenclud@116
   435
					//Check if our segment count was rounded up, this will later be used for half brightness segment
StephaneLenclud@116
   436
					bool roundedUp = segmentOnCount > currentVolume;
StephaneLenclud@114
   437
StephaneLenclud@112
   438
					for (int i = 0; i < volumeIconCount; i++)
StephaneLenclud@112
   439
					{
StephaneLenclud@116
   440
						if (i < segmentOnCount)
StephaneLenclud@112
   441
						{
StephaneLenclud@116
   442
							//If we are dealing with our last segment and our segment count was rounded up then we will use half brightness.
StephaneLenclud@116
   443
							if (i == segmentOnCount - 1 && roundedUp)
StephaneLenclud@114
   444
							{
StephaneLenclud@114
   445
								//Half brightness
StephaneLenclud@135
   446
                                iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i, (iDisplay.IconStatusCount(MiniDisplay.IconType.Volume) - 1) / 2);
StephaneLenclud@114
   447
							}
StephaneLenclud@114
   448
							else
StephaneLenclud@114
   449
							{
StephaneLenclud@114
   450
								//Full brightness
StephaneLenclud@135
   451
                                iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i, iDisplay.IconStatusCount(MiniDisplay.IconType.Volume) - 1);
StephaneLenclud@114
   452
							}
StephaneLenclud@112
   453
						}
StephaneLenclud@112
   454
						else
StephaneLenclud@112
   455
						{
StephaneLenclud@135
   456
                            iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i, 0);
StephaneLenclud@112
   457
						}
StephaneLenclud@112
   458
					}
StephaneLenclud@112
   459
				}
StephaneLenclud@113
   460
Stephane@182
   461
				//Take care of our mute icon
StephaneLenclud@135
   462
                iDisplay.SetIconOnOff(MiniDisplay.IconType.Mute, iMultiMediaDevice.AudioEndpointVolume.Mute);
StephaneLenclud@112
   463
			}
StephaneLenclud@112
   464
StephaneLenclud@112
   465
		}
StephaneLenclud@112
   466
StephaneLenclud@112
   467
        /// <summary>
StephaneLenclud@112
   468
        /// 
StephaneLenclud@112
   469
        /// </summary>
StephaneLenclud@112
   470
        private void UpdateAudioDeviceAndMasterVolumeThreadSafe()
StephaneLenclud@112
   471
        {
StephaneLenclud@112
   472
            if (this.InvokeRequired)
StephaneLenclud@112
   473
            {
StephaneLenclud@112
   474
                //Not in the proper thread, invoke ourselves
StephaneLenclud@112
   475
				PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateAudioDeviceAndMasterVolumeThreadSafe);
StephaneLenclud@112
   476
                this.Invoke(d, new object[] { });
StephaneLenclud@112
   477
                return;
StephaneLenclud@112
   478
            }
StephaneLenclud@112
   479
            
StephaneLenclud@112
   480
            //We are in the correct thread just go ahead.
StephaneLenclud@112
   481
            try
StephaneLenclud@112
   482
            {                
StephaneLenclud@112
   483
                //Get our master volume            
StephaneLenclud@112
   484
				iMultiMediaDevice = iMultiMediaDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
StephaneLenclud@116
   485
				//Update our label
StephaneLenclud@116
   486
				labelDefaultAudioDevice.Text = iMultiMediaDevice.FriendlyName;
StephaneLenclud@116
   487
StephaneLenclud@112
   488
                //Show our volume in our track bar
StephaneLenclud@112
   489
				UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   490
StephaneLenclud@112
   491
                //Register to get volume modifications
StephaneLenclud@112
   492
				iMultiMediaDevice.AudioEndpointVolume.OnVolumeNotification += OnVolumeNotificationThreadSafe;
StephaneLenclud@112
   493
                //
StephaneLenclud@112
   494
				trackBarMasterVolume.Enabled = true;
StephaneLenclud@112
   495
            }
StephaneLenclud@112
   496
            catch (Exception ex)
StephaneLenclud@112
   497
            {
StephaneLenclud@112
   498
                Debug.WriteLine("Exception thrown in UpdateAudioDeviceAndMasterVolume");
StephaneLenclud@112
   499
                Debug.WriteLine(ex.ToString());
StephaneLenclud@112
   500
                //Something went wrong S/PDIF device ca throw exception I guess
StephaneLenclud@112
   501
				trackBarMasterVolume.Enabled = false;
StephaneLenclud@112
   502
            }
StephaneLenclud@112
   503
        }
StephaneLenclud@104
   504
StephaneLenclud@104
   505
		/// <summary>
StephaneLenclud@104
   506
		/// 
StephaneLenclud@104
   507
		/// </summary>
StephaneLenclud@104
   508
		private void PopulateDeviceTypes()
StephaneLenclud@104
   509
		{
StephaneLenclud@104
   510
			int count = Display.TypeCount();
StephaneLenclud@104
   511
StephaneLenclud@106
   512
			for (int i = 0; i < count; i++)
StephaneLenclud@104
   513
			{
StephaneLenclud@135
   514
				comboBoxDisplayType.Items.Add(Display.TypeName((MiniDisplay.Type)i));
StephaneLenclud@106
   515
			}
StephaneLenclud@104
   516
		}
StephaneLenclud@104
   517
StephaneLenclud@152
   518
        /// <summary>
StephaneLenclud@152
   519
        /// 
StephaneLenclud@152
   520
        /// </summary>
StephaneLenclud@152
   521
        private void PopulateOpticalDrives()
StephaneLenclud@152
   522
        {
StephaneLenclud@152
   523
            //Reset our list of drives
StephaneLenclud@152
   524
            comboBoxOpticalDrives.Items.Clear();
StephaneLenclud@153
   525
            comboBoxOpticalDrives.Items.Add("None");
StephaneLenclud@152
   526
StephaneLenclud@152
   527
            //Go through each drives on our system and collected the optical ones in our list
StephaneLenclud@152
   528
            DriveInfo[] allDrives = DriveInfo.GetDrives();
StephaneLenclud@152
   529
            foreach (DriveInfo d in allDrives)
StephaneLenclud@152
   530
            {
StephaneLenclud@157
   531
                Debug.WriteLine("Drive " + d.Name);
StephaneLenclud@152
   532
                Debug.WriteLine("  Drive type: {0}", d.DriveType);
StephaneLenclud@152
   533
StephaneLenclud@152
   534
                if (d.DriveType==DriveType.CDRom)
StephaneLenclud@152
   535
                {
StephaneLenclud@152
   536
                    //This is an optical drive, add it now
StephaneLenclud@152
   537
                    comboBoxOpticalDrives.Items.Add(d.Name.Substring(0,2));
StephaneLenclud@152
   538
                }                
StephaneLenclud@153
   539
            }           
StephaneLenclud@152
   540
        }
StephaneLenclud@152
   541
StephaneLenclud@152
   542
        /// <summary>
StephaneLenclud@152
   543
        /// 
StephaneLenclud@152
   544
        /// </summary>
StephaneLenclud@152
   545
        /// <returns></returns>
StephaneLenclud@152
   546
        public string OpticalDriveToEject()
StephaneLenclud@152
   547
        {
StephaneLenclud@153
   548
            return comboBoxOpticalDrives.SelectedItem.ToString();
StephaneLenclud@152
   549
        }
StephaneLenclud@152
   550
StephaneLenclud@152
   551
StephaneLenclud@152
   552
StephaneLenclud@104
   553
		/// <summary>
sl@99
   554
		///
sl@94
   555
		/// </summary>
sl@95
   556
		private void SetupTrayIcon()
sl@95
   557
		{
sl@95
   558
			iNotifyIcon.Icon = GetIcon("vfd.ico");
sl@95
   559
			iNotifyIcon.Text = "Sharp Display Manager";
sl@95
   560
			iNotifyIcon.Visible = true;
sl@95
   561
sl@95
   562
			//Double click toggles visibility - typically brings up the application
sl@95
   563
			iNotifyIcon.DoubleClick += delegate(object obj, EventArgs args)
sl@95
   564
			{
sl@95
   565
				SysTrayHideShow();
sl@95
   566
			};
sl@95
   567
sl@95
   568
			//Adding a context menu, useful to be able to exit the application
sl@95
   569
			ContextMenu contextMenu = new ContextMenu();
sl@95
   570
			//Context menu item to toggle visibility
sl@95
   571
			MenuItem hideShowItem = new MenuItem("Hide/Show");
sl@95
   572
			hideShowItem.Click += delegate(object obj, EventArgs args)
sl@95
   573
			{
sl@95
   574
				SysTrayHideShow();
sl@95
   575
			};
sl@95
   576
			contextMenu.MenuItems.Add(hideShowItem);
sl@95
   577
sl@95
   578
			//Context menu item separator
sl@95
   579
			contextMenu.MenuItems.Add(new MenuItem("-"));
sl@95
   580
sl@95
   581
			//Context menu exit item
sl@95
   582
			MenuItem exitItem = new MenuItem("Exit");
sl@95
   583
			exitItem.Click += delegate(object obj, EventArgs args)
sl@95
   584
			{
sl@95
   585
				Application.Exit();
sl@95
   586
			};
sl@95
   587
			contextMenu.MenuItems.Add(exitItem);
sl@95
   588
sl@95
   589
			iNotifyIcon.ContextMenu = contextMenu;
sl@95
   590
		}
sl@95
   591
StephaneLenclud@178
   592
        /// <summary>
StephaneLenclud@179
   593
        ///
StephaneLenclud@179
   594
        /// </summary>
StephaneLenclud@179
   595
        private void SetupRecordingNotification()
StephaneLenclud@179
   596
        {
StephaneLenclud@179
   597
            iRecordingNotification.Icon = GetIcon("record.ico");
StephaneLenclud@179
   598
            iRecordingNotification.Text = "No recording";
StephaneLenclud@180
   599
            iRecordingNotification.Visible = false;
StephaneLenclud@179
   600
        }
StephaneLenclud@179
   601
StephaneLenclud@179
   602
        /// <summary>
StephaneLenclud@178
   603
        /// Access icons from embedded resources.
StephaneLenclud@178
   604
        /// </summary>
StephaneLenclud@178
   605
        /// <param name="aName"></param>
StephaneLenclud@178
   606
        /// <returns></returns>
StephaneLenclud@178
   607
        public static Icon GetIcon(string aName)
sl@94
   608
		{
StephaneLenclud@178
   609
			string[] names =  Assembly.GetExecutingAssembly().GetManifestResourceNames();
StephaneLenclud@178
   610
			foreach (string name in names)
sl@94
   611
			{
StephaneLenclud@178
   612
                //Find a resource name that ends with the given name
StephaneLenclud@178
   613
				if (name.EndsWith(aName))
sl@94
   614
				{
StephaneLenclud@178
   615
					using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
sl@94
   616
					{
sl@94
   617
						return new Icon(stream);
sl@94
   618
					}
sl@94
   619
				}
sl@94
   620
			}
sl@94
   621
sl@94
   622
			return null;
sl@94
   623
		}
sl@94
   624
sl@94
   625
sl@65
   626
        /// <summary>
sl@65
   627
        /// Set our current client.
sl@65
   628
        /// This will take care of applying our client layout and set data fields.
sl@65
   629
        /// </summary>
sl@65
   630
        /// <param name="aSessionId"></param>
StephaneLenclud@141
   631
        void SetCurrentClient(string aSessionId, bool aForce=false)
sl@57
   632
        {
sl@65
   633
            if (aSessionId == iCurrentClientSessionId)
sl@57
   634
            {
sl@65
   635
                //Given client is already the current one.
sl@65
   636
                //Don't bother changing anything then.
sl@65
   637
                return;
sl@65
   638
            }
sl@57
   639
StephaneLenclud@185
   640
            ClientData requestedClientData = iClients[aSessionId];
StephaneLenclud@141
   641
StephaneLenclud@141
   642
            //Check when was the last time we switched to that client
StephaneLenclud@142
   643
            if (iCurrentClientData != null)
StephaneLenclud@141
   644
            {
StephaneLenclud@185
   645
                //Do not switch client if priority of current client is higher 
StephaneLenclud@185
   646
                if (!aForce && requestedClientData.Priority < iCurrentClientData.Priority)
StephaneLenclud@185
   647
                {
StephaneLenclud@185
   648
                    return;
StephaneLenclud@185
   649
                }
StephaneLenclud@185
   650
StephaneLenclud@185
   651
StephaneLenclud@142
   652
                double lastSwitchToClientSecondsAgo = (DateTime.Now - iCurrentClientData.LastSwitchTime).TotalSeconds;
StephaneLenclud@142
   653
                //TODO: put that hard coded value as a client property
StephaneLenclud@142
   654
                //Clients should be able to define how often they can be interrupted
StephaneLenclud@142
   655
                //Thus a background client can set this to zero allowing any other client to interrupt at any time
StephaneLenclud@142
   656
                //We could also compute this delay by looking at the requests frequencies?
StephaneLenclud@185
   657
                if (!aForce &&
StephaneLenclud@185
   658
                    requestedClientData.Priority == iCurrentClientData.Priority && //Time sharing is only if clients have the same priority
StephaneLenclud@185
   659
                    (lastSwitchToClientSecondsAgo < 30)) //Make sure a client is on for at least 30 seconds
StephaneLenclud@142
   660
                {
StephaneLenclud@142
   661
                    //Don't switch clients too often
StephaneLenclud@142
   662
                    return;
StephaneLenclud@142
   663
                }
StephaneLenclud@141
   664
            }
StephaneLenclud@141
   665
sl@65
   666
            //Set current client ID.
sl@65
   667
            iCurrentClientSessionId = aSessionId;
StephaneLenclud@141
   668
            //Set the time we last switched to that client
StephaneLenclud@141
   669
            iClients[aSessionId].LastSwitchTime = DateTime.Now;
sl@65
   670
            //Fetch and set current client data.
StephaneLenclud@185
   671
            iCurrentClientData = requestedClientData;
sl@65
   672
            //Apply layout and set data fields.
sl@65
   673
            UpdateTableLayoutPanel(iCurrentClientData);
sl@57
   674
        }
sl@57
   675
sl@0
   676
        private void buttonFont_Click(object sender, EventArgs e)
sl@0
   677
        {
sl@0
   678
            //fontDialog.ShowColor = true;
sl@0
   679
            //fontDialog.ShowApply = true;
sl@0
   680
            fontDialog.ShowEffects = true;
sl@99
   681
            fontDialog.Font = cds.Font;
sl@28
   682
sl@28
   683
            fontDialog.FixedPitchOnly = checkBoxFixedPitchFontOnly.Checked;
sl@28
   684
sl@0
   685
            //fontDialog.ShowHelp = true;
sl@0
   686
sl@0
   687
            //fontDlg.MaxSize = 40;
sl@0
   688
            //fontDlg.MinSize = 22;
sl@0
   689
sl@0
   690
            //fontDialog.Parent = this;
sl@0
   691
            //fontDialog.StartPosition = FormStartPosition.CenterParent;
sl@0
   692
sl@0
   693
            //DlgBox.ShowDialog(fontDialog);
sl@0
   694
sl@0
   695
            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
sl@0
   696
            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
sl@0
   697
            {
sl@99
   698
                //Set the fonts to all our labels in our layout
StephaneLenclud@175
   699
                foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@99
   700
                {
sl@99
   701
                    if (ctrl is MarqueeLabel)
sl@99
   702
                    {
sl@99
   703
                        ((MarqueeLabel)ctrl).Font = fontDialog.Font;
sl@99
   704
                    }
sl@99
   705
                }
sl@0
   706
sl@99
   707
                //Save font settings
sl@48
   708
                cds.Font = fontDialog.Font;
sl@8
   709
                Properties.Settings.Default.Save();
sl@36
   710
                //
sl@37
   711
                CheckFontHeight();
sl@37
   712
            }
sl@37
   713
        }
sl@36
   714
sl@37
   715
        /// <summary>
sl@38
   716
        ///
sl@37
   717
        /// </summary>
sl@37
   718
        void CheckFontHeight()
sl@37
   719
        {
sl@54
   720
            //Show font height and width
sl@54
   721
            labelFontHeight.Text = "Font height: " + cds.Font.Height;
sl@54
   722
            float charWidth = IsFixedWidth(cds.Font);
sl@54
   723
            if (charWidth == 0.0f)
sl@54
   724
            {
sl@54
   725
                labelFontWidth.Visible = false;
sl@54
   726
            }
sl@54
   727
            else
sl@54
   728
            {
sl@54
   729
                labelFontWidth.Visible = true;
sl@54
   730
                labelFontWidth.Text = "Font width: " + charWidth;
sl@54
   731
            }
sl@54
   732
sl@70
   733
            MarqueeLabel label = null;
sl@68
   734
            //Get the first label control we can find
StephaneLenclud@175
   735
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@68
   736
            {
sl@68
   737
                if (ctrl is MarqueeLabel)
sl@68
   738
                {
sl@68
   739
                    label = (MarqueeLabel)ctrl;
sl@68
   740
                    break;
sl@68
   741
                }
sl@68
   742
            }
sl@68
   743
sl@54
   744
            //Now check font height and show a warning if needed.
sl@68
   745
            if (label != null && label.Font.Height > label.Height)
sl@37
   746
            {
sl@60
   747
                labelWarning.Text = "WARNING: Selected font is too height by " + (label.Font.Height - label.Height) + " pixels!";
sl@37
   748
                labelWarning.Visible = true;
sl@0
   749
            }
sl@37
   750
            else
sl@37
   751
            {
sl@37
   752
                labelWarning.Visible = false;
sl@37
   753
            }
sl@37
   754
sl@0
   755
        }
sl@0
   756
sl@0
   757
        private void buttonCapture_Click(object sender, EventArgs e)
sl@0
   758
        {
StephaneLenclud@175
   759
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height);
StephaneLenclud@175
   760
            iTableLayoutPanel.DrawToBitmap(bmp, iTableLayoutPanel.ClientRectangle);
sl@14
   761
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
   762
            bmp.Save("D:\\capture.png");
sl@14
   763
StephaneLenclud@175
   764
            ((MarqueeLabel)iTableLayoutPanel.Controls[0]).Text = "Captured";
sl@17
   765
sl@14
   766
            /*
sl@14
   767
            string outputFileName = "d:\\capture.png";
sl@14
   768
            using (MemoryStream memory = new MemoryStream())
sl@14
   769
            {
sl@14
   770
                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
sl@14
   771
                {
sl@14
   772
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
sl@14
   773
                    byte[] bytes = memory.ToArray();
sl@14
   774
                    fs.Write(bytes, 0, bytes.Length);
sl@14
   775
                }
sl@14
   776
            }
sl@14
   777
             */
sl@14
   778
sl@0
   779
        }
sl@2
   780
sl@12
   781
        private void CheckForRequestResults()
sl@12
   782
        {
sl@12
   783
            if (iDisplay.IsRequestPending())
sl@12
   784
            {
sl@12
   785
                switch (iDisplay.AttemptRequestCompletion())
sl@12
   786
                {
StephaneLenclud@135
   787
                    case MiniDisplay.Request.FirmwareRevision:
sl@51
   788
                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
sl@51
   789
                        //Issue next request then
sl@51
   790
                        iDisplay.RequestPowerSupplyStatus();
sl@51
   791
                        break;
sl@51
   792
StephaneLenclud@135
   793
                    case MiniDisplay.Request.PowerSupplyStatus:
sl@12
   794
                        if (iDisplay.PowerSupplyStatus())
sl@12
   795
                        {
sl@12
   796
                            toolStripStatusLabelPower.Text = "ON";
sl@12
   797
                        }
sl@12
   798
                        else
sl@12
   799
                        {
sl@12
   800
                            toolStripStatusLabelPower.Text = "OFF";
sl@12
   801
                        }
sl@12
   802
                        //Issue next request then
sl@12
   803
                        iDisplay.RequestDeviceId();
sl@12
   804
                        break;
sl@12
   805
StephaneLenclud@135
   806
                    case MiniDisplay.Request.DeviceId:
sl@12
   807
                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
sl@12
   808
                        //No more request to issue
sl@12
   809
                        break;
sl@12
   810
                }
sl@12
   811
            }
sl@12
   812
        }
sl@12
   813
sl@58
   814
        public static uint ColorWhiteIsOn(int aX, int aY, uint aPixel)
sl@58
   815
        {
sl@58
   816
            if ((aPixel & 0x00FFFFFF) == 0x00FFFFFF)
sl@58
   817
            {
sl@58
   818
                return 0xFFFFFFFF;
sl@58
   819
            }
sl@58
   820
            return 0x00000000;
sl@58
   821
        }
sl@16
   822
sl@58
   823
        public static uint ColorUntouched(int aX, int aY, uint aPixel)
sl@57
   824
        {
sl@57
   825
            return aPixel;
sl@57
   826
        }
sl@57
   827
sl@58
   828
        public static uint ColorInversed(int aX, int aY, uint aPixel)
sl@57
   829
        {
sl@57
   830
            return ~aPixel;
sl@57
   831
        }
sl@57
   832
sl@58
   833
        public static uint ColorChessboard(int aX, int aY, uint aPixel)
sl@58
   834
        {
sl@58
   835
            if ((aX % 2 == 0) && (aY % 2 == 0))
sl@58
   836
            {
sl@58
   837
                return ~aPixel;
sl@58
   838
            }
sl@58
   839
            else if ((aX % 2 != 0) && (aY % 2 != 0))
sl@58
   840
            {
sl@58
   841
                return ~aPixel;
sl@58
   842
            }
sl@58
   843
            return 0x00000000;
sl@58
   844
        }
sl@58
   845
sl@16
   846
sl@16
   847
        public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
sl@16
   848
        {
sl@16
   849
            return aBmp.Width - aX - 1;
sl@16
   850
        }
sl@16
   851
sl@16
   852
        public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
sl@16
   853
        {
sl@16
   854
            return iBmp.Height - aY - 1;
sl@16
   855
        }
sl@16
   856
sl@16
   857
        public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
sl@16
   858
        {
sl@16
   859
            return aX;
sl@16
   860
        }
sl@16
   861
sl@16
   862
        public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
sl@16
   863
        {
sl@16
   864
            return aY;
sl@16
   865
        }
sl@16
   866
sl@58
   867
        /// <summary>
sl@58
   868
        /// Select proper pixel delegates according to our current settings.
sl@58
   869
        /// </summary>
sl@58
   870
        private void SetupPixelDelegates()
sl@58
   871
        {
sl@59
   872
            //Select our pixel processing routine
sl@58
   873
            if (cds.InverseColors)
sl@58
   874
            {
sl@58
   875
                //iColorFx = ColorChessboard;
sl@58
   876
                iColorFx = ColorInversed;
sl@58
   877
            }
sl@58
   878
            else
sl@58
   879
            {
sl@58
   880
                iColorFx = ColorWhiteIsOn;
sl@58
   881
            }
sl@58
   882
sl@58
   883
            //Select proper coordinate translation functions
sl@58
   884
            //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
sl@58
   885
            if (cds.ReverseScreen)
sl@58
   886
            {
sl@58
   887
                iScreenX = ScreenReversedX;
sl@58
   888
                iScreenY = ScreenReversedY;
sl@58
   889
            }
sl@58
   890
            else
sl@58
   891
            {
sl@58
   892
                iScreenX = ScreenX;
sl@58
   893
                iScreenY = ScreenY;
sl@58
   894
            }
sl@58
   895
sl@58
   896
        }
sl@16
   897
sl@16
   898
        //This is our timer tick responsible to perform our render
sl@2
   899
        private void timer_Tick(object sender, EventArgs e)
sl@14
   900
        {
sl@2
   901
            //Update our animations
sl@2
   902
            DateTime NewTickTime = DateTime.Now;
sl@2
   903
StephaneLenclud@118
   904
			UpdateNetworkSignal(LastTickTime, NewTickTime);
StephaneLenclud@118
   905
sl@60
   906
            //Update animation for all our marquees
StephaneLenclud@175
   907
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@60
   908
            {
sl@68
   909
                if (ctrl is MarqueeLabel)
sl@68
   910
                {
sl@68
   911
                    ((MarqueeLabel)ctrl).UpdateAnimation(LastTickTime, NewTickTime);
sl@68
   912
                }
sl@60
   913
            }
sl@60
   914
sl@4
   915
            //Update our display
sl@4
   916
            if (iDisplay.IsOpen())
sl@4
   917
            {
sl@12
   918
                CheckForRequestResults();
sl@12
   919
StephaneLenclud@122
   920
				//Check if frame rendering is needed
StephaneLenclud@122
   921
				//Typically used when showing clock
StephaneLenclud@122
   922
				if (!iSkipFrameRendering)
StephaneLenclud@122
   923
				{
StephaneLenclud@122
   924
					//Draw to bitmap
StephaneLenclud@122
   925
					if (iCreateBitmap)
StephaneLenclud@122
   926
					{
StephaneLenclud@175
   927
                        iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height, PixelFormat.Format32bppArgb);
StephaneLenclud@158
   928
                        iCreateBitmap = false;
StephaneLenclud@158
   929
                    }
StephaneLenclud@175
   930
					iTableLayoutPanel.DrawToBitmap(iBmp, iTableLayoutPanel.ClientRectangle);
StephaneLenclud@122
   931
					//iBmp.Save("D:\\capture.png");
sl@16
   932
StephaneLenclud@122
   933
					//Send it to our display
StephaneLenclud@122
   934
					for (int i = 0; i < iBmp.Width; i++)
StephaneLenclud@122
   935
					{
StephaneLenclud@122
   936
						for (int j = 0; j < iBmp.Height; j++)
StephaneLenclud@122
   937
						{
StephaneLenclud@122
   938
							unchecked
StephaneLenclud@122
   939
							{
StephaneLenclud@122
   940
								//Get our processed pixel coordinates
StephaneLenclud@122
   941
								int x = iScreenX(iBmp, i);
StephaneLenclud@122
   942
								int y = iScreenY(iBmp, j);
StephaneLenclud@122
   943
								//Get pixel color
StephaneLenclud@122
   944
								uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
StephaneLenclud@122
   945
								//Apply color effects
StephaneLenclud@122
   946
								color = iColorFx(x, y, color);
StephaneLenclud@122
   947
								//Now set our pixel
StephaneLenclud@122
   948
								iDisplay.SetPixel(x, y, color);
StephaneLenclud@122
   949
							}
StephaneLenclud@122
   950
						}
StephaneLenclud@122
   951
					}
sl@4
   952
StephaneLenclud@122
   953
					iDisplay.SwapBuffers();
StephaneLenclud@122
   954
				}
sl@4
   955
            }
sl@8
   956
sl@8
   957
            //Compute instant FPS
sl@47
   958
            toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " / " + (1000/timer.Interval).ToString() + " FPS";
sl@8
   959
sl@8
   960
            LastTickTime = NewTickTime;
sl@8
   961
sl@2
   962
        }
sl@3
   963
StephaneLenclud@103
   964
		/// <summary>
StephaneLenclud@103
   965
		/// Attempt to establish connection with our display hardware.
StephaneLenclud@103
   966
		/// </summary>
sl@46
   967
        private void OpenDisplayConnection()
sl@3
   968
        {
sl@46
   969
            CloseDisplayConnection();
sl@46
   970
StephaneLenclud@135
   971
            if (!iDisplay.Open((MiniDisplay.Type)cds.DisplayType))
StephaneLenclud@104
   972
            {   
StephaneLenclud@104
   973
				UpdateStatus();               
StephaneLenclud@104
   974
				toolStripStatusLabelConnect.Text = "Connection error";
sl@7
   975
            }
sl@46
   976
        }
sl@7
   977
sl@46
   978
        private void CloseDisplayConnection()
sl@46
   979
        {
StephaneLenclud@104
   980
			//Status will be updated upon receiving the closed event
StephaneLenclud@122
   981
StephaneLenclud@122
   982
			if (iDisplay == null || !iDisplay.IsOpen())
StephaneLenclud@122
   983
			{
StephaneLenclud@122
   984
				return;
StephaneLenclud@122
   985
			}
StephaneLenclud@122
   986
StephaneLenclud@122
   987
			//Do not clear if we gave up on rendering already.
StephaneLenclud@122
   988
			//This means we will keep on displaying clock on MDM166AA for instance.
StephaneLenclud@122
   989
			if (!iSkipFrameRendering)
StephaneLenclud@122
   990
			{
StephaneLenclud@122
   991
				iDisplay.Clear();
StephaneLenclud@122
   992
				iDisplay.SwapBuffers();
StephaneLenclud@122
   993
			}
StephaneLenclud@122
   994
StephaneLenclud@122
   995
			iDisplay.SetAllIconsStatus(0); //Turn off all icons
sl@46
   996
            iDisplay.Close();
sl@46
   997
        }
sl@46
   998
sl@46
   999
        private void buttonOpen_Click(object sender, EventArgs e)
sl@46
  1000
        {
sl@46
  1001
            OpenDisplayConnection();
sl@3
  1002
        }
sl@3
  1003
sl@3
  1004
        private void buttonClose_Click(object sender, EventArgs e)
sl@3
  1005
        {
sl@46
  1006
            CloseDisplayConnection();
sl@3
  1007
        }
sl@3
  1008
sl@3
  1009
        private void buttonClear_Click(object sender, EventArgs e)
sl@3
  1010
        {
sl@3
  1011
            iDisplay.Clear();
sl@3
  1012
            iDisplay.SwapBuffers();
sl@3
  1013
        }
sl@3
  1014
sl@3
  1015
        private void buttonFill_Click(object sender, EventArgs e)
sl@3
  1016
        {
sl@3
  1017
            iDisplay.Fill();
sl@3
  1018
            iDisplay.SwapBuffers();
sl@3
  1019
        }
sl@3
  1020
sl@3
  1021
        private void trackBarBrightness_Scroll(object sender, EventArgs e)
sl@3
  1022
        {
sl@48
  1023
            cds.Brightness = trackBarBrightness.Value;
sl@9
  1024
            Properties.Settings.Default.Save();
sl@3
  1025
            iDisplay.SetBrightness(trackBarBrightness.Value);
sl@9
  1026
sl@3
  1027
        }
sl@7
  1028
sl@48
  1029
sl@48
  1030
        /// <summary>
sl@48
  1031
        /// CDS stands for Current Display Settings
sl@48
  1032
        /// </summary>
sl@50
  1033
        private DisplaySettings cds
sl@48
  1034
        {
sl@48
  1035
            get
sl@48
  1036
            {
sl@65
  1037
                DisplaysSettings settings = Properties.Settings.Default.DisplaysSettings;
sl@51
  1038
                if (settings == null)
sl@51
  1039
                {
sl@51
  1040
                    settings = new DisplaysSettings();
sl@51
  1041
                    settings.Init();
sl@65
  1042
                    Properties.Settings.Default.DisplaysSettings = settings;
sl@51
  1043
                }
sl@48
  1044
sl@48
  1045
                //Make sure all our settings have been created
sl@48
  1046
                while (settings.Displays.Count <= Properties.Settings.Default.CurrentDisplayIndex)
sl@48
  1047
                {
sl@50
  1048
                    settings.Displays.Add(new DisplaySettings());
sl@48
  1049
                }
sl@48
  1050
sl@50
  1051
                DisplaySettings displaySettings = settings.Displays[Properties.Settings.Default.CurrentDisplayIndex];
sl@48
  1052
                return displaySettings;
sl@48
  1053
            }
sl@48
  1054
        }
sl@48
  1055
sl@54
  1056
        /// <summary>
sl@54
  1057
        /// Check if the given font has a fixed character pitch.
sl@54
  1058
        /// </summary>
sl@54
  1059
        /// <param name="ft"></param>
sl@54
  1060
        /// <returns>0.0f if this is not a monospace font, otherwise returns the character width.</returns>
sl@54
  1061
        public float IsFixedWidth(Font ft)
sl@54
  1062
        {
sl@54
  1063
            Graphics g = CreateGraphics();
sl@54
  1064
            char[] charSizes = new char[] { 'i', 'a', 'Z', '%', '#', 'a', 'B', 'l', 'm', ',', '.' };
sl@54
  1065
            float charWidth = g.MeasureString("I", ft, Int32.MaxValue, StringFormat.GenericTypographic).Width;
sl@54
  1066
sl@54
  1067
            bool fixedWidth = true;
sl@54
  1068
sl@54
  1069
            foreach (char c in charSizes)
sl@54
  1070
                if (g.MeasureString(c.ToString(), ft, Int32.MaxValue, StringFormat.GenericTypographic).Width != charWidth)
sl@54
  1071
                    fixedWidth = false;
sl@54
  1072
sl@54
  1073
            if (fixedWidth)
sl@54
  1074
            {
sl@54
  1075
                return charWidth;
sl@54
  1076
            }
sl@54
  1077
sl@54
  1078
            return 0.0f;
sl@54
  1079
        }
sl@54
  1080
StephaneLenclud@103
  1081
		/// <summary>
StephaneLenclud@103
  1082
		/// Synchronize UI with settings
StephaneLenclud@103
  1083
		/// </summary>
sl@7
  1084
        private void UpdateStatus()
StephaneLenclud@103
  1085
        {            
sl@48
  1086
            //Load settings
sl@54
  1087
            checkBoxShowBorders.Checked = cds.ShowBorders;
StephaneLenclud@175
  1088
            iTableLayoutPanel.CellBorderStyle = (cds.ShowBorders ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@60
  1089
sl@60
  1090
            //Set the proper font to each of our labels
StephaneLenclud@175
  1091
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
sl@60
  1092
            {
sl@60
  1093
                ctrl.Font = cds.Font;
sl@60
  1094
            }
sl@60
  1095
sl@54
  1096
            CheckFontHeight();
sl@96
  1097
			//Check if "run on Windows startup" is enabled
sl@96
  1098
			checkBoxAutoStart.Checked = iStartupManager.Startup;
sl@96
  1099
			//
sl@48
  1100
            checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
sl@94
  1101
			checkBoxMinimizeToTray.Checked = Properties.Settings.Default.MinimizeToTray;
sl@94
  1102
			checkBoxStartMinimized.Checked = Properties.Settings.Default.StartMinimized;
StephaneLenclud@126
  1103
			labelStartFileName.Text = Properties.Settings.Default.StartFileName;
StephaneLenclud@153
  1104
StephaneLenclud@153
  1105
            //Try find our drive in our drive list
StephaneLenclud@153
  1106
            int opticalDriveItemIndex=0;
StephaneLenclud@153
  1107
            bool driveNotFound = true;
StephaneLenclud@153
  1108
            string opticalDriveToEject=Properties.Settings.Default.OpticalDriveToEject;
StephaneLenclud@153
  1109
            foreach (object item in comboBoxOpticalDrives.Items)
StephaneLenclud@153
  1110
            {
StephaneLenclud@154
  1111
                if (opticalDriveToEject == item.ToString())
StephaneLenclud@153
  1112
                {
StephaneLenclud@153
  1113
                    comboBoxOpticalDrives.SelectedIndex = opticalDriveItemIndex;
StephaneLenclud@153
  1114
                    driveNotFound = false;
StephaneLenclud@153
  1115
                    break;
StephaneLenclud@153
  1116
                }
StephaneLenclud@153
  1117
                opticalDriveItemIndex++;
StephaneLenclud@153
  1118
            }
StephaneLenclud@153
  1119
StephaneLenclud@153
  1120
            if (driveNotFound)
StephaneLenclud@153
  1121
            {
StephaneLenclud@153
  1122
                //We could not find the drive we had saved.
StephaneLenclud@153
  1123
                //Select "None" then.
StephaneLenclud@153
  1124
                comboBoxOpticalDrives.SelectedIndex = 0;
StephaneLenclud@153
  1125
            }
StephaneLenclud@153
  1126
StephaneLenclud@168
  1127
            //CEC settings
StephaneLenclud@168
  1128
            checkBoxCecEnabled.Checked = Properties.Settings.Default.CecEnabled;
StephaneLenclud@168
  1129
            checkBoxCecMonitorOn.Checked = Properties.Settings.Default.CecMonitorOn;
StephaneLenclud@168
  1130
            checkBoxCecMonitorOff.Checked = Properties.Settings.Default.CecMonitorOff;
StephaneLenclud@168
  1131
            comboBoxHdmiPort.SelectedIndex = Properties.Settings.Default.CecHdmiPort - 1;
StephaneLenclud@153
  1132
StephaneLenclud@168
  1133
            //Mini Display settings
sl@48
  1134
            checkBoxReverseScreen.Checked = cds.ReverseScreen;
sl@57
  1135
            checkBoxInverseColors.Checked = cds.InverseColors;
StephaneLenclud@115
  1136
			checkBoxShowVolumeLabel.Checked = cds.ShowVolumeLabel;
sl@100
  1137
            checkBoxScaleToFit.Checked = cds.ScaleToFit;
sl@100
  1138
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1139
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1140
            maskedTextBoxMinFontSize.Text = cds.MinFontSize.ToString();
StephaneLenclud@106
  1141
			maskedTextBoxScrollingSpeed.Text = cds.ScrollingSpeedInPixelsPerSecond.ToString();
sl@48
  1142
            comboBoxDisplayType.SelectedIndex = cds.DisplayType;
sl@48
  1143
            timer.Interval = cds.TimerInterval;
sl@48
  1144
            maskedTextBoxTimerInterval.Text = cds.TimerInterval.ToString();
sl@100
  1145
            textBoxScrollLoopSeparator.Text = cds.Separator;
sl@58
  1146
            //
sl@58
  1147
            SetupPixelDelegates();
sl@48
  1148
sl@7
  1149
            if (iDisplay.IsOpen())
sl@7
  1150
            {
StephaneLenclud@187
  1151
                //We have a display connection
StephaneLenclud@187
  1152
                //Reflect that in our UI
StephaneLenclud@187
  1153
                StartTimer();
StephaneLenclud@103
  1154
StephaneLenclud@175
  1155
				iTableLayoutPanel.Enabled = true;
StephaneLenclud@105
  1156
				panelDisplay.Enabled = true;
StephaneLenclud@103
  1157
sl@48
  1158
                //Only setup brightness if display is open
sl@48
  1159
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@48
  1160
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
StephaneLenclud@105
  1161
				if (cds.Brightness < iDisplay.MinBrightness() || cds.Brightness > iDisplay.MaxBrightness())
StephaneLenclud@105
  1162
				{
StephaneLenclud@105
  1163
					//Brightness out of range, this can occur when using auto-detect
StephaneLenclud@105
  1164
					//Use max brightness instead
StephaneLenclud@105
  1165
					trackBarBrightness.Value = iDisplay.MaxBrightness();
StephaneLenclud@105
  1166
					iDisplay.SetBrightness(iDisplay.MaxBrightness());
StephaneLenclud@105
  1167
				}
StephaneLenclud@105
  1168
				else
StephaneLenclud@105
  1169
				{
StephaneLenclud@105
  1170
					trackBarBrightness.Value = cds.Brightness;
StephaneLenclud@105
  1171
					iDisplay.SetBrightness(cds.Brightness);
StephaneLenclud@105
  1172
				}
StephaneLenclud@105
  1173
StephaneLenclud@105
  1174
				//Try compute the steps to something that makes sense
sl@48
  1175
                trackBarBrightness.LargeChange = Math.Max(1, (iDisplay.MaxBrightness() - iDisplay.MinBrightness()) / 5);
sl@48
  1176
                trackBarBrightness.SmallChange = 1;
StephaneLenclud@105
  1177
                
sl@48
  1178
                //
sl@7
  1179
                buttonFill.Enabled = true;
sl@7
  1180
                buttonClear.Enabled = true;
sl@7
  1181
                buttonOpen.Enabled = false;
sl@7
  1182
                buttonClose.Enabled = true;
sl@7
  1183
                trackBarBrightness.Enabled = true;
sl@10
  1184
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
  1185
                //+ " - " + iDisplay.SerialNumber();
sl@52
  1186
sl@52
  1187
                if (iDisplay.SupportPowerOnOff())
sl@52
  1188
                {
sl@52
  1189
                    buttonPowerOn.Enabled = true;
sl@52
  1190
                    buttonPowerOff.Enabled = true;
sl@52
  1191
                }
sl@52
  1192
                else
sl@52
  1193
                {
sl@52
  1194
                    buttonPowerOn.Enabled = false;
sl@52
  1195
                    buttonPowerOff.Enabled = false;
sl@52
  1196
                }
sl@53
  1197
sl@53
  1198
                if (iDisplay.SupportClock())
sl@53
  1199
                {
sl@53
  1200
                    buttonShowClock.Enabled = true;
sl@53
  1201
                    buttonHideClock.Enabled = true;
sl@53
  1202
                }
sl@53
  1203
                else
sl@53
  1204
                {
sl@53
  1205
                    buttonShowClock.Enabled = false;
sl@53
  1206
                    buttonHideClock.Enabled = false;
sl@53
  1207
                }
StephaneLenclud@115
  1208
StephaneLenclud@115
  1209
				
StephaneLenclud@115
  1210
				//Check if Volume Label is supported. To date only MDM166AA supports that crap :)
StephaneLenclud@135
  1211
				checkBoxShowVolumeLabel.Enabled = iDisplay.IconCount(MiniDisplay.IconType.VolumeLabel)>0;
StephaneLenclud@115
  1212
StephaneLenclud@115
  1213
				if (cds.ShowVolumeLabel)
StephaneLenclud@115
  1214
				{
StephaneLenclud@135
  1215
                    iDisplay.SetIconOn(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@115
  1216
				}
StephaneLenclud@115
  1217
				else
StephaneLenclud@115
  1218
				{
StephaneLenclud@135
  1219
                    iDisplay.SetIconOff(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@115
  1220
				}
sl@7
  1221
            }
sl@7
  1222
            else
sl@7
  1223
            {
StephaneLenclud@187
  1224
                //Display connection not available
StephaneLenclud@187
  1225
                //Reflect that in our UI
StephaneLenclud@187
  1226
#if DEBUG
StephaneLenclud@187
  1227
                //In debug start our timer even if we don't have a display connection
StephaneLenclud@187
  1228
                StartTimer();
StephaneLenclud@187
  1229
#else
StephaneLenclud@187
  1230
                //In production environment we don't need our timer if no display connection
StephaneLenclud@187
  1231
                StopTimer();
StephaneLenclud@187
  1232
#endif
StephaneLenclud@187
  1233
                checkBoxShowVolumeLabel.Enabled = false;
StephaneLenclud@175
  1234
				iTableLayoutPanel.Enabled = false;
StephaneLenclud@105
  1235
				panelDisplay.Enabled = false;
sl@7
  1236
                buttonFill.Enabled = false;
sl@7
  1237
                buttonClear.Enabled = false;
sl@7
  1238
                buttonOpen.Enabled = true;
sl@7
  1239
                buttonClose.Enabled = false;
sl@7
  1240
                trackBarBrightness.Enabled = false;
sl@52
  1241
                buttonPowerOn.Enabled = false;
sl@52
  1242
                buttonPowerOff.Enabled = false;
sl@53
  1243
                buttonShowClock.Enabled = false;
sl@53
  1244
                buttonHideClock.Enabled = false;
sl@9
  1245
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@48
  1246
                toolStripStatusLabelPower.Text = "N/A";
sl@7
  1247
            }
StephaneLenclud@106
  1248
sl@7
  1249
        }
sl@9
  1250
sl@13
  1251
StephaneLenclud@115
  1252
		/// <summary>
StephaneLenclud@115
  1253
		/// 
StephaneLenclud@115
  1254
		/// </summary>
StephaneLenclud@115
  1255
		/// <param name="sender"></param>
StephaneLenclud@115
  1256
		/// <param name="e"></param>
StephaneLenclud@115
  1257
		private void checkBoxShowVolumeLabel_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@115
  1258
		{
StephaneLenclud@115
  1259
			cds.ShowVolumeLabel = checkBoxShowVolumeLabel.Checked;
StephaneLenclud@115
  1260
			Properties.Settings.Default.Save();
StephaneLenclud@115
  1261
			UpdateStatus();
StephaneLenclud@115
  1262
		}
sl@13
  1263
sl@9
  1264
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
  1265
        {
sl@16
  1266
            //Save our show borders setting
StephaneLenclud@175
  1267
            iTableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@48
  1268
            cds.ShowBorders = checkBoxShowBorders.Checked;
sl@9
  1269
            Properties.Settings.Default.Save();
sl@57
  1270
            CheckFontHeight();
sl@9
  1271
        }
sl@13
  1272
sl@13
  1273
        private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
sl@13
  1274
        {
sl@16
  1275
            //Save our connect on startup setting
sl@13
  1276
            Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
sl@13
  1277
            Properties.Settings.Default.Save();
sl@13
  1278
        }
sl@13
  1279
sl@94
  1280
		private void checkBoxMinimizeToTray_CheckedChanged(object sender, EventArgs e)
sl@94
  1281
		{
sl@94
  1282
			//Save our "Minimize to tray" setting
sl@94
  1283
			Properties.Settings.Default.MinimizeToTray = checkBoxMinimizeToTray.Checked;
sl@94
  1284
			Properties.Settings.Default.Save();
sl@94
  1285
sl@94
  1286
		}
sl@94
  1287
sl@94
  1288
		private void checkBoxStartMinimized_CheckedChanged(object sender, EventArgs e)
sl@94
  1289
		{
sl@94
  1290
			//Save our "Start minimized" setting
sl@94
  1291
			Properties.Settings.Default.StartMinimized = checkBoxStartMinimized.Checked;
sl@94
  1292
			Properties.Settings.Default.Save();
sl@94
  1293
		}
sl@94
  1294
sl@94
  1295
		private void checkBoxAutoStart_CheckedChanged(object sender, EventArgs e)
sl@94
  1296
		{
sl@94
  1297
			iStartupManager.Startup = checkBoxAutoStart.Checked;
sl@94
  1298
		}
sl@94
  1299
sl@94
  1300
sl@16
  1301
        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
sl@16
  1302
        {
sl@16
  1303
            //Save our reverse screen setting
sl@48
  1304
            cds.ReverseScreen = checkBoxReverseScreen.Checked;
sl@16
  1305
            Properties.Settings.Default.Save();
sl@58
  1306
            SetupPixelDelegates();
sl@16
  1307
        }
sl@16
  1308
sl@57
  1309
        private void checkBoxInverseColors_CheckedChanged(object sender, EventArgs e)
sl@57
  1310
        {
sl@57
  1311
            //Save our inverse colors setting
sl@57
  1312
            cds.InverseColors = checkBoxInverseColors.Checked;
sl@57
  1313
            Properties.Settings.Default.Save();
sl@58
  1314
            SetupPixelDelegates();
sl@57
  1315
        }
sl@57
  1316
sl@100
  1317
        private void checkBoxScaleToFit_CheckedChanged(object sender, EventArgs e)
sl@100
  1318
        {
sl@100
  1319
            //Save our scale to fit setting
sl@100
  1320
            cds.ScaleToFit = checkBoxScaleToFit.Checked;
sl@100
  1321
            Properties.Settings.Default.Save();
sl@100
  1322
            //
sl@100
  1323
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1324
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1325
        }
sl@100
  1326
sl@14
  1327
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
  1328
        {
sl@14
  1329
            if (WindowState == FormWindowState.Minimized)
sl@14
  1330
            {
StephaneLenclud@158
  1331
                // To workaround our empty bitmap bug on Windows 7 we need to recreate our bitmap when the application is minimized
StephaneLenclud@158
  1332
                // That's apparently not needed on Windows 10 but we better leave it in place.
sl@14
  1333
                iCreateBitmap = true;
sl@14
  1334
            }
sl@14
  1335
        }
sl@14
  1336
sl@17
  1337
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@17
  1338
        {
StephaneLenclud@167
  1339
            iCecManager.Stop();
StephaneLenclud@117
  1340
			iNetworkManager.Dispose();
StephaneLenclud@105
  1341
			CloseDisplayConnection();
sl@17
  1342
            StopServer();
sl@32
  1343
            e.Cancel = iClosing;
sl@17
  1344
        }
sl@17
  1345
sl@17
  1346
        public void StartServer()
sl@17
  1347
        {
sl@17
  1348
            iServiceHost = new ServiceHost
sl@17
  1349
                (
sl@55
  1350
                    typeof(Session),
sl@20
  1351
                    new Uri[] { new Uri("net.tcp://localhost:8001/") }
sl@17
  1352
                );
sl@17
  1353
sl@55
  1354
            iServiceHost.AddServiceEndpoint(typeof(IService), new NetTcpBinding(SecurityMode.None, true), "DisplayService");
sl@17
  1355
            iServiceHost.Open();
sl@17
  1356
        }
sl@17
  1357
sl@17
  1358
        public void StopServer()
sl@17
  1359
        {
sl@32
  1360
            if (iClients.Count > 0 && !iClosing)
sl@29
  1361
            {
sl@29
  1362
                //Tell our clients
sl@32
  1363
                iClosing = true;
sl@29
  1364
                BroadcastCloseEvent();
sl@29
  1365
            }
sl@32
  1366
            else if (iClosing)
sl@32
  1367
            {
sl@32
  1368
                if (MessageBox.Show("Force exit?", "Waiting for clients...", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
sl@32
  1369
                {
sl@32
  1370
                    iClosing = false; //We make sure we force close if asked twice
sl@32
  1371
                }
sl@32
  1372
            }
sl@32
  1373
            else
sl@36
  1374
            {
sl@32
  1375
                //We removed that as it often lags for some reason
sl@32
  1376
                //iServiceHost.Close();
sl@32
  1377
            }
sl@17
  1378
        }
sl@17
  1379
sl@21
  1380
        public void BroadcastCloseEvent()
sl@21
  1381
        {
sl@31
  1382
            Trace.TraceInformation("BroadcastCloseEvent - start");
sl@31
  1383
sl@21
  1384
            var inactiveClients = new List<string>();
sl@21
  1385
            foreach (var client in iClients)
sl@21
  1386
            {
sl@21
  1387
                //if (client.Key != eventData.ClientName)
sl@21
  1388
                {
sl@21
  1389
                    try
sl@21
  1390
                    {
sl@31
  1391
                        Trace.TraceInformation("BroadcastCloseEvent - " + client.Key);
sl@33
  1392
                        client.Value.Callback.OnCloseOrder(/*eventData*/);
sl@21
  1393
                    }
sl@21
  1394
                    catch (Exception ex)
sl@21
  1395
                    {
sl@21
  1396
                        inactiveClients.Add(client.Key);
sl@21
  1397
                    }
sl@21
  1398
                }
sl@21
  1399
            }
sl@21
  1400
sl@21
  1401
            if (inactiveClients.Count > 0)
sl@21
  1402
            {
sl@21
  1403
                foreach (var client in inactiveClients)
sl@21
  1404
                {
sl@21
  1405
                    iClients.Remove(client);
StephaneLenclud@188
  1406
                    Program.iMainForm.iTreeViewClients.Nodes.Remove(Program.iMainForm.iTreeViewClients.Nodes.Find(client, false)[0]);
sl@21
  1407
                }
sl@21
  1408
            }
sl@97
  1409
sl@97
  1410
			if (iClients.Count==0)
sl@97
  1411
			{
sl@97
  1412
				ClearLayout();
sl@97
  1413
			}
sl@21
  1414
        }
sl@21
  1415
sl@97
  1416
		/// <summary>
sl@97
  1417
		/// Just remove all our fields.
sl@97
  1418
		/// </summary>
sl@97
  1419
		private void ClearLayout()
sl@97
  1420
		{
StephaneLenclud@175
  1421
			iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  1422
			iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  1423
			iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@122
  1424
			iCurrentClientData = null;
sl@97
  1425
		}
sl@97
  1426
StephaneLenclud@106
  1427
		/// <summary>
StephaneLenclud@106
  1428
		/// Just launch a demo client.
StephaneLenclud@106
  1429
		/// </summary>
StephaneLenclud@106
  1430
		private void StartNewClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@106
  1431
		{
StephaneLenclud@106
  1432
			Thread clientThread = new Thread(SharpDisplayClient.Program.MainWithParams);
StephaneLenclud@106
  1433
			SharpDisplayClient.StartParams myParams = new SharpDisplayClient.StartParams(new Point(this.Right, this.Top),aTopText,aBottomText);
StephaneLenclud@106
  1434
			clientThread.Start(myParams);
StephaneLenclud@106
  1435
			BringToFront();
StephaneLenclud@106
  1436
		}
StephaneLenclud@106
  1437
StephaneLenclud@189
  1438
        /// <summary>
StephaneLenclud@189
  1439
        /// Just launch our idle client.
StephaneLenclud@189
  1440
        /// </summary>
StephaneLenclud@189
  1441
        private void StartIdleClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@189
  1442
        {
StephaneLenclud@189
  1443
            Thread clientThread = new Thread(SharpDisplayIdleClient.Program.MainWithParams);
StephaneLenclud@189
  1444
            SharpDisplayIdleClient.StartParams myParams = new SharpDisplayIdleClient.StartParams(new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@189
  1445
            clientThread.Start(myParams);
StephaneLenclud@189
  1446
            BringToFront();
StephaneLenclud@189
  1447
        }
StephaneLenclud@189
  1448
StephaneLenclud@189
  1449
sl@25
  1450
        private void buttonStartClient_Click(object sender, EventArgs e)
sl@25
  1451
        {
StephaneLenclud@106
  1452
			StartNewClient();
sl@25
  1453
        }
sl@25
  1454
sl@27
  1455
        private void buttonSuspend_Click(object sender, EventArgs e)
sl@27
  1456
        {
StephaneLenclud@187
  1457
            ToggleTimer();
StephaneLenclud@187
  1458
        }
StephaneLenclud@187
  1459
StephaneLenclud@187
  1460
        private void StartTimer()
StephaneLenclud@187
  1461
        {
StephaneLenclud@187
  1462
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@187
  1463
            timer.Enabled = true;
StephaneLenclud@187
  1464
            UpdateSuspendButton();
StephaneLenclud@187
  1465
        }
StephaneLenclud@187
  1466
StephaneLenclud@187
  1467
        private void StopTimer()
StephaneLenclud@187
  1468
        {
StephaneLenclud@187
  1469
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@187
  1470
            timer.Enabled = false;
StephaneLenclud@187
  1471
            UpdateSuspendButton();
StephaneLenclud@187
  1472
        }
StephaneLenclud@187
  1473
StephaneLenclud@187
  1474
        private void ToggleTimer()
StephaneLenclud@187
  1475
        {
sl@52
  1476
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
sl@27
  1477
            timer.Enabled = !timer.Enabled;
StephaneLenclud@187
  1478
            UpdateSuspendButton();
StephaneLenclud@187
  1479
        }
StephaneLenclud@187
  1480
StephaneLenclud@187
  1481
        private void UpdateSuspendButton()
StephaneLenclud@187
  1482
        {
sl@27
  1483
            if (!timer.Enabled)
sl@27
  1484
            {
sl@52
  1485
                buttonSuspend.Text = "Run";
sl@27
  1486
            }
sl@27
  1487
            else
sl@27
  1488
            {
sl@27
  1489
                buttonSuspend.Text = "Pause";
sl@27
  1490
            }
sl@27
  1491
        }
sl@27
  1492
StephaneLenclud@187
  1493
sl@29
  1494
        private void buttonCloseClients_Click(object sender, EventArgs e)
sl@29
  1495
        {
sl@29
  1496
            BroadcastCloseEvent();
sl@29
  1497
        }
sl@29
  1498
sl@30
  1499
        private void treeViewClients_AfterSelect(object sender, TreeViewEventArgs e)
sl@30
  1500
        {
StephaneLenclud@141
  1501
            //Root node must have at least one child
StephaneLenclud@141
  1502
            if (e.Node.Nodes.Count == 0)
StephaneLenclud@141
  1503
            {
StephaneLenclud@141
  1504
                return;
StephaneLenclud@141
  1505
            }
sl@21
  1506
StephaneLenclud@141
  1507
            //If the selected node is the root node of a client then switch to it
StephaneLenclud@141
  1508
            string sessionId=e.Node.Nodes[0].Text; //First child of a root node is the sessionId
StephaneLenclud@141
  1509
            if (iClients.ContainsKey(sessionId)) //Check that's actually what we are looking at
StephaneLenclud@141
  1510
            {
StephaneLenclud@141
  1511
                //We have a valid session just switch to that client
StephaneLenclud@141
  1512
                SetCurrentClient(sessionId,true);
StephaneLenclud@141
  1513
            }
StephaneLenclud@141
  1514
            
sl@30
  1515
        }
sl@30
  1516
sl@36
  1517
sl@30
  1518
        /// <summary>
sl@36
  1519
        ///
sl@30
  1520
        /// </summary>
sl@30
  1521
        /// <param name="aSessionId"></param>
sl@30
  1522
        /// <param name="aCallback"></param>
sl@55
  1523
        public void AddClientThreadSafe(string aSessionId, ICallback aCallback)
sl@30
  1524
        {
sl@33
  1525
            if (this.InvokeRequired)
sl@30
  1526
            {
sl@30
  1527
                //Not in the proper thread, invoke ourselves
sl@30
  1528
                AddClientDelegate d = new AddClientDelegate(AddClientThreadSafe);
sl@30
  1529
                this.Invoke(d, new object[] { aSessionId, aCallback });
sl@30
  1530
            }
sl@30
  1531
            else
sl@30
  1532
            {
sl@30
  1533
                //We are in the proper thread
sl@30
  1534
                //Add this session to our collection of clients
sl@33
  1535
                ClientData newClient = new ClientData(aSessionId, aCallback);
sl@33
  1536
                Program.iMainForm.iClients.Add(aSessionId, newClient);
sl@30
  1537
                //Add this session to our UI
sl@33
  1538
                UpdateClientTreeViewNode(newClient);
sl@30
  1539
            }
sl@30
  1540
        }
sl@30
  1541
Stephane@186
  1542
Stephane@186
  1543
        /// <summary>
Stephane@186
  1544
        /// Find the client with the highest priority if any.
Stephane@186
  1545
        /// </summary>
Stephane@186
  1546
        /// <returns>Our highest priority client or null if not a single client is connected.</returns>
Stephane@186
  1547
        public ClientData FindHighestPriorityClient()
Stephane@186
  1548
        {
Stephane@186
  1549
            ClientData highestPriorityClient = null;
Stephane@186
  1550
            foreach (var client in iClients)
Stephane@186
  1551
            {
Stephane@186
  1552
                if (highestPriorityClient == null || client.Value.Priority > highestPriorityClient.Priority)
Stephane@186
  1553
                {
Stephane@186
  1554
                    highestPriorityClient = client.Value;
Stephane@186
  1555
                }
Stephane@186
  1556
            }
Stephane@186
  1557
Stephane@186
  1558
            return highestPriorityClient;
Stephane@186
  1559
        }
Stephane@186
  1560
sl@30
  1561
        /// <summary>
sl@36
  1562
        ///
sl@30
  1563
        /// </summary>
sl@30
  1564
        /// <param name="aSessionId"></param>
sl@30
  1565
        public void RemoveClientThreadSafe(string aSessionId)
sl@30
  1566
        {
sl@33
  1567
            if (this.InvokeRequired)
sl@30
  1568
            {
sl@30
  1569
                //Not in the proper thread, invoke ourselves
sl@30
  1570
                RemoveClientDelegate d = new RemoveClientDelegate(RemoveClientThreadSafe);
sl@30
  1571
                this.Invoke(d, new object[] { aSessionId });
sl@30
  1572
            }
sl@30
  1573
            else
sl@30
  1574
            {
sl@30
  1575
                //We are in the proper thread
sl@33
  1576
                //Remove this session from both client collection and UI tree view
sl@30
  1577
                if (Program.iMainForm.iClients.Keys.Contains(aSessionId))
sl@30
  1578
                {
sl@30
  1579
                    Program.iMainForm.iClients.Remove(aSessionId);
StephaneLenclud@188
  1580
                    Program.iMainForm.iTreeViewClients.Nodes.Remove(Program.iMainForm.iTreeViewClients.Nodes.Find(aSessionId, false)[0]);
StephaneLenclud@188
  1581
                    //Update recording status too whenever a client is removed
StephaneLenclud@188
  1582
                    UpdateRecordingNotification();
sl@32
  1583
                }
sl@32
  1584
Stephane@186
  1585
                if (iCurrentClientSessionId == aSessionId)
Stephane@186
  1586
                {
Stephane@186
  1587
                    //The current client is closing
Stephane@186
  1588
                    iCurrentClientData = null;
Stephane@186
  1589
                    //Find the client with the highest priority and set it as current
Stephane@186
  1590
                    ClientData newCurrentClient = FindHighestPriorityClient();
Stephane@186
  1591
                    if (newCurrentClient!=null)
Stephane@186
  1592
                    {
Stephane@186
  1593
                        SetCurrentClient(newCurrentClient.SessionId, true);
Stephane@186
  1594
                    }                    
Stephane@186
  1595
                }
Stephane@186
  1596
Stephane@186
  1597
                if (iClients.Count == 0)
sl@97
  1598
				{
sl@97
  1599
					//Clear our screen when last client disconnects
sl@97
  1600
					ClearLayout();
sl@97
  1601
sl@97
  1602
					if (iClosing)
sl@97
  1603
					{
sl@97
  1604
						//We were closing our form
sl@97
  1605
						//All clients are now closed
sl@97
  1606
						//Just resume our close operation
sl@97
  1607
						iClosing = false;
sl@97
  1608
						Close();
sl@97
  1609
					}
sl@97
  1610
				}
sl@30
  1611
            }
sl@30
  1612
        }
sl@30
  1613
sl@30
  1614
        /// <summary>
sl@36
  1615
        ///
sl@30
  1616
        /// </summary>
sl@62
  1617
        /// <param name="aSessionId"></param>
sl@72
  1618
        /// <param name="aLayout"></param>
sl@62
  1619
        public void SetClientLayoutThreadSafe(string aSessionId, TableLayout aLayout)
sl@62
  1620
        {
sl@62
  1621
            if (this.InvokeRequired)
sl@62
  1622
            {
sl@62
  1623
                //Not in the proper thread, invoke ourselves
sl@62
  1624
                SetLayoutDelegate d = new SetLayoutDelegate(SetClientLayoutThreadSafe);
sl@62
  1625
                this.Invoke(d, new object[] { aSessionId, aLayout });
sl@62
  1626
            }
sl@62
  1627
            else
sl@62
  1628
            {
sl@62
  1629
                ClientData client = iClients[aSessionId];
sl@62
  1630
                if (client != null)
sl@62
  1631
                {
StephaneLenclud@148
  1632
                    //Don't change a thing if the layout is the same
StephaneLenclud@148
  1633
                    if (!client.Layout.IsSameAs(aLayout))
StephaneLenclud@148
  1634
                    {
StephaneLenclud@158
  1635
                        Debug.Print("SetClientLayoutThreadSafe: Layout updated.");
StephaneLenclud@148
  1636
                        //Set our client layout then
StephaneLenclud@148
  1637
                        client.Layout = aLayout;
StephaneLenclud@175
  1638
                        //So that next time we update all our fields at ones
StephaneLenclud@175
  1639
                        client.HasNewLayout = true;
StephaneLenclud@158
  1640
                        //Layout has changed clear our fields then
StephaneLenclud@158
  1641
                        client.Fields.Clear();
StephaneLenclud@148
  1642
                        //
StephaneLenclud@148
  1643
                        UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1644
                    }
StephaneLenclud@158
  1645
                    else
StephaneLenclud@158
  1646
                    {
StephaneLenclud@158
  1647
                        Debug.Print("SetClientLayoutThreadSafe: Layout has not changed.");
StephaneLenclud@158
  1648
                    }
sl@62
  1649
                }
sl@62
  1650
            }
sl@62
  1651
        }
sl@62
  1652
sl@62
  1653
        /// <summary>
sl@62
  1654
        ///
sl@62
  1655
        /// </summary>
sl@67
  1656
        /// <param name="aSessionId"></param>
sl@72
  1657
        /// <param name="aField"></param>
sl@75
  1658
        public void SetClientFieldThreadSafe(string aSessionId, DataField aField)
sl@30
  1659
        {
sl@33
  1660
            if (this.InvokeRequired)
sl@30
  1661
            {
sl@30
  1662
                //Not in the proper thread, invoke ourselves
sl@79
  1663
                SetFieldDelegate d = new SetFieldDelegate(SetClientFieldThreadSafe);
sl@72
  1664
                this.Invoke(d, new object[] { aSessionId, aField });
sl@30
  1665
            }
sl@30
  1666
            else
sl@30
  1667
            {
sl@75
  1668
                //We are in the proper thread
sl@75
  1669
                //Call the non-thread-safe variant
sl@75
  1670
                SetClientField(aSessionId, aField);
sl@75
  1671
            }
sl@75
  1672
        }
sl@75
  1673
StephaneLenclud@176
  1674
StephaneLenclud@176
  1675
StephaneLenclud@176
  1676
sl@75
  1677
        /// <summary>
StephaneLenclud@175
  1678
        /// Set a data field in the given client.
sl@75
  1679
        /// </summary>
sl@75
  1680
        /// <param name="aSessionId"></param>
sl@75
  1681
        /// <param name="aField"></param>
sl@75
  1682
        private void SetClientField(string aSessionId, DataField aField)
StephaneLenclud@141
  1683
        {   
StephaneLenclud@141
  1684
            //TODO: should check if the field actually changed?
StephaneLenclud@141
  1685
sl@75
  1686
            ClientData client = iClients[aSessionId];
StephaneLenclud@141
  1687
            bool layoutChanged = false;
StephaneLenclud@148
  1688
            bool contentChanged = true;
StephaneLenclud@141
  1689
StephaneLenclud@176
  1690
            //Fetch our field index
StephaneLenclud@176
  1691
            int fieldIndex = client.FindSameFieldIndex(aField);
StephaneLenclud@176
  1692
StephaneLenclud@176
  1693
            if (fieldIndex < 0)
sl@75
  1694
            {
StephaneLenclud@176
  1695
                //No corresponding field, just bail out
StephaneLenclud@176
  1696
                return;
StephaneLenclud@141
  1697
            }
sl@76
  1698
StephaneLenclud@175
  1699
            //Keep our previous field in there
StephaneLenclud@176
  1700
            DataField previousField = client.Fields[fieldIndex];
StephaneLenclud@176
  1701
            //Just update that field then 
StephaneLenclud@176
  1702
            client.Fields[fieldIndex] = aField;
StephaneLenclud@148
  1703
StephaneLenclud@176
  1704
            if (!aField.IsTableField)
StephaneLenclud@176
  1705
            {
StephaneLenclud@176
  1706
                //We are done then if that field is not in our table layout
StephaneLenclud@176
  1707
                return;
StephaneLenclud@176
  1708
            }
StephaneLenclud@176
  1709
StephaneLenclud@176
  1710
            TableField tableField = (TableField) aField;
StephaneLenclud@172
  1711
StephaneLenclud@175
  1712
            if (previousField.IsSameLayout(aField))
StephaneLenclud@141
  1713
            {
StephaneLenclud@141
  1714
                //If we are updating a field in our current client we need to update it in our panel
StephaneLenclud@141
  1715
                if (aSessionId == iCurrentClientSessionId)
sl@30
  1716
                {
StephaneLenclud@176
  1717
                    Control ctrl=iTableLayoutPanel.GetControlFromPosition(tableField.Column, tableField.Row);
StephaneLenclud@176
  1718
                    if (aField.IsTextField && ctrl is MarqueeLabel)
sl@79
  1719
                    {
StephaneLenclud@172
  1720
                        TextField textField=(TextField)aField;
sl@75
  1721
                        //Text field control already in place, just change the text
StephaneLenclud@176
  1722
                        MarqueeLabel label = (MarqueeLabel)ctrl;
StephaneLenclud@172
  1723
                        contentChanged = (label.Text != textField.Text || label.TextAlign != textField.Alignment);
StephaneLenclud@172
  1724
                        label.Text = textField.Text;
StephaneLenclud@172
  1725
                        label.TextAlign = textField.Alignment;
sl@68
  1726
                    }
StephaneLenclud@176
  1727
                    else if (aField.IsBitmapField && ctrl is PictureBox)
sl@75
  1728
                    {
StephaneLenclud@172
  1729
                        BitmapField bitmapField = (BitmapField)aField;
StephaneLenclud@148
  1730
                        contentChanged = true; //TODO: Bitmap comp or should we leave that to clients?
sl@75
  1731
                        //Bitmap field control already in place just change the bitmap
StephaneLenclud@176
  1732
                        PictureBox pictureBox = (PictureBox)ctrl;
StephaneLenclud@172
  1733
                        pictureBox.Image = bitmapField.Bitmap;
sl@75
  1734
                    }
sl@68
  1735
                    else
sl@68
  1736
                    {
StephaneLenclud@141
  1737
                        layoutChanged = true;
sl@68
  1738
                    }
sl@30
  1739
                }
StephaneLenclud@141
  1740
            }
StephaneLenclud@141
  1741
            else
StephaneLenclud@148
  1742
            {                
StephaneLenclud@141
  1743
                layoutChanged = true;
StephaneLenclud@141
  1744
            }
StephaneLenclud@141
  1745
StephaneLenclud@148
  1746
            //If either content or layout changed we need to update our tree view to reflect the changes
StephaneLenclud@148
  1747
            if (contentChanged || layoutChanged)
StephaneLenclud@141
  1748
            {
StephaneLenclud@141
  1749
                UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1750
                //
StephaneLenclud@148
  1751
                if (layoutChanged)
sl@75
  1752
                {
StephaneLenclud@148
  1753
                    Debug.Print("Layout changed");
StephaneLenclud@148
  1754
                    //Our layout has changed, if we are already the current client we need to update our panel
StephaneLenclud@148
  1755
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@148
  1756
                    {
StephaneLenclud@148
  1757
                        //Apply layout and set data fields.
StephaneLenclud@148
  1758
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@148
  1759
                    }
sl@75
  1760
                }
StephaneLenclud@158
  1761
                else
StephaneLenclud@158
  1762
                {
StephaneLenclud@158
  1763
                    Debug.Print("Layout has not changed.");
StephaneLenclud@158
  1764
                }
StephaneLenclud@158
  1765
            }
StephaneLenclud@158
  1766
            else
StephaneLenclud@158
  1767
            {
StephaneLenclud@158
  1768
                Debug.Print("WARNING: content and layout have not changed!");
StephaneLenclud@141
  1769
            }
sl@75
  1770
StephaneLenclud@141
  1771
            //When a client field is set we try switching to this client to present the new information to our user
StephaneLenclud@141
  1772
            SetCurrentClient(aSessionId);
sl@30
  1773
        }
sl@30
  1774
sl@30
  1775
        /// <summary>
sl@36
  1776
        ///
sl@30
  1777
        /// </summary>
sl@30
  1778
        /// <param name="aTexts"></param>
sl@75
  1779
        public void SetClientFieldsThreadSafe(string aSessionId, System.Collections.Generic.IList<DataField> aFields)
sl@30
  1780
        {
sl@33
  1781
            if (this.InvokeRequired)
sl@30
  1782
            {
sl@30
  1783
                //Not in the proper thread, invoke ourselves
sl@75
  1784
                SetFieldsDelegate d = new SetFieldsDelegate(SetClientFieldsThreadSafe);
sl@72
  1785
                this.Invoke(d, new object[] { aSessionId, aFields });
sl@30
  1786
            }
sl@30
  1787
            else
sl@30
  1788
            {
StephaneLenclud@175
  1789
                ClientData client = iClients[aSessionId];
StephaneLenclud@175
  1790
StephaneLenclud@175
  1791
                if (client.HasNewLayout)
sl@30
  1792
                {
StephaneLenclud@175
  1793
                    //TODO: Assert client.Count == 0
StephaneLenclud@175
  1794
                    //Our layout was just changed
StephaneLenclud@175
  1795
                    //Do some special handling to avoid re-creating our panel N times, once for each fields
StephaneLenclud@175
  1796
                    client.HasNewLayout = false;
StephaneLenclud@175
  1797
                    //Just set all our fields then
StephaneLenclud@175
  1798
                    client.Fields.AddRange(aFields);
StephaneLenclud@175
  1799
                    //Try switch to that client
StephaneLenclud@175
  1800
                    SetCurrentClient(aSessionId);
StephaneLenclud@175
  1801
StephaneLenclud@175
  1802
                    //If we are updating the current client update our panel
StephaneLenclud@175
  1803
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@175
  1804
                    {
StephaneLenclud@175
  1805
                        //Apply layout and set data fields.
StephaneLenclud@175
  1806
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@175
  1807
                    }
StephaneLenclud@176
  1808
StephaneLenclud@176
  1809
                    UpdateClientTreeViewNode(client);
StephaneLenclud@175
  1810
                }
StephaneLenclud@175
  1811
                else
StephaneLenclud@175
  1812
                {
StephaneLenclud@175
  1813
                    //Put each our text fields in a label control
StephaneLenclud@175
  1814
                    foreach (DataField field in aFields)
StephaneLenclud@175
  1815
                    {
StephaneLenclud@175
  1816
                        SetClientField(aSessionId, field);
StephaneLenclud@175
  1817
                    }
sl@30
  1818
                }
sl@30
  1819
            }
sl@32
  1820
        }
sl@30
  1821
sl@67
  1822
        /// <summary>
sl@67
  1823
        ///
sl@67
  1824
        /// </summary>
sl@67
  1825
        /// <param name="aSessionId"></param>
sl@32
  1826
        /// <param name="aName"></param>
sl@32
  1827
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
  1828
        {
sl@32
  1829
            if (this.InvokeRequired)
sl@32
  1830
            {
sl@32
  1831
                //Not in the proper thread, invoke ourselves
sl@32
  1832
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
sl@32
  1833
                this.Invoke(d, new object[] { aSessionId, aName });
sl@32
  1834
            }
sl@32
  1835
            else
sl@32
  1836
            {
sl@32
  1837
                //We are in the proper thread
sl@33
  1838
                //Get our client
sl@33
  1839
                ClientData client = iClients[aSessionId];
sl@33
  1840
                if (client != null)
sl@32
  1841
                {
sl@33
  1842
                    //Set its name
sl@33
  1843
                    client.Name = aName;
sl@33
  1844
                    //Update our tree-view
sl@33
  1845
                    UpdateClientTreeViewNode(client);
sl@33
  1846
                }
sl@33
  1847
            }
sl@33
  1848
        }
sl@33
  1849
StephaneLenclud@184
  1850
        ///
StephaneLenclud@184
  1851
        public void SetClientPriorityThreadSafe(string aSessionId, uint aPriority)
StephaneLenclud@184
  1852
        {
StephaneLenclud@184
  1853
            if (this.InvokeRequired)
StephaneLenclud@184
  1854
            {
StephaneLenclud@184
  1855
                //Not in the proper thread, invoke ourselves
StephaneLenclud@184
  1856
                SetClientPriorityDelegate d = new SetClientPriorityDelegate(SetClientPriorityThreadSafe);
StephaneLenclud@184
  1857
                this.Invoke(d, new object[] { aSessionId, aPriority });
StephaneLenclud@184
  1858
            }
StephaneLenclud@184
  1859
            else
StephaneLenclud@184
  1860
            {
StephaneLenclud@184
  1861
                //We are in the proper thread
StephaneLenclud@184
  1862
                //Get our client
StephaneLenclud@184
  1863
                ClientData client = iClients[aSessionId];
StephaneLenclud@184
  1864
                if (client != null)
StephaneLenclud@184
  1865
                {
StephaneLenclud@184
  1866
                    //Set its name
StephaneLenclud@184
  1867
                    client.Priority = aPriority;
StephaneLenclud@184
  1868
                    //Update our tree-view
StephaneLenclud@184
  1869
                    UpdateClientTreeViewNode(client);
Stephane@186
  1870
                    //Change our current client as per new priority
Stephane@186
  1871
                    ClientData newCurrentClient = FindHighestPriorityClient();
Stephane@186
  1872
                    if (newCurrentClient!=null)
Stephane@186
  1873
                    {
Stephane@186
  1874
                        SetCurrentClient(newCurrentClient.SessionId);
Stephane@186
  1875
                    }
StephaneLenclud@184
  1876
                }
StephaneLenclud@184
  1877
            }
StephaneLenclud@184
  1878
        }
StephaneLenclud@184
  1879
sl@33
  1880
        /// <summary>
StephaneLenclud@183
  1881
        /// 
StephaneLenclud@183
  1882
        /// </summary>
StephaneLenclud@183
  1883
        /// <param name="value"></param>
StephaneLenclud@183
  1884
        /// <param name="maxChars"></param>
StephaneLenclud@183
  1885
        /// <returns></returns>
StephaneLenclud@183
  1886
        public static string Truncate(string value, int maxChars)
StephaneLenclud@183
  1887
        {
StephaneLenclud@183
  1888
            return value.Length <= maxChars ? value : value.Substring(0, maxChars-3) + "...";
StephaneLenclud@183
  1889
        }
StephaneLenclud@183
  1890
StephaneLenclud@183
  1891
        /// <summary>
StephaneLenclud@180
  1892
        /// Update our recording notification.
StephaneLenclud@180
  1893
        /// </summary>
StephaneLenclud@180
  1894
        private void UpdateRecordingNotification()
StephaneLenclud@180
  1895
        {
StephaneLenclud@180
  1896
            //Go through each 
StephaneLenclud@180
  1897
            bool activeRecording = false;
StephaneLenclud@180
  1898
            string text="";
StephaneLenclud@180
  1899
            RecordingField recField=new RecordingField();
StephaneLenclud@180
  1900
            foreach (var client in iClients)
StephaneLenclud@180
  1901
            {
StephaneLenclud@180
  1902
                RecordingField rec=(RecordingField)client.Value.FindSameFieldAs(recField);
StephaneLenclud@180
  1903
                if (rec!=null && rec.IsActive)
StephaneLenclud@180
  1904
                {
StephaneLenclud@180
  1905
                    activeRecording = true;
StephaneLenclud@183
  1906
                    //Don't break cause we are collecting the names/texts.
StephaneLenclud@183
  1907
                    if (!String.IsNullOrEmpty(rec.Text))
StephaneLenclud@183
  1908
                    {
StephaneLenclud@183
  1909
                        text += (rec.Text + "\n");
StephaneLenclud@183
  1910
                    }
StephaneLenclud@183
  1911
                    else
StephaneLenclud@183
  1912
                    {
StephaneLenclud@183
  1913
                        //Not text for that recording, use client name instead
StephaneLenclud@183
  1914
                        text += client.Value.Name + " recording\n";
StephaneLenclud@183
  1915
                    }
StephaneLenclud@183
  1916
                    
StephaneLenclud@180
  1917
                }
StephaneLenclud@180
  1918
            }
StephaneLenclud@180
  1919
StephaneLenclud@183
  1920
            //Update our text no matter what, can't have more than 63 characters otherwise it throws an exception.
StephaneLenclud@183
  1921
            iRecordingNotification.Text = Truncate(text,63);
StephaneLenclud@183
  1922
StephaneLenclud@180
  1923
            //Change visibility of notification if needed
StephaneLenclud@180
  1924
            if (iRecordingNotification.Visible != activeRecording)
StephaneLenclud@183
  1925
            {                
StephaneLenclud@180
  1926
                iRecordingNotification.Visible = activeRecording;
Stephane@182
  1927
                //Assuming the notification icon is in sync with our display icon
Stephane@182
  1928
                //Take care of our REC icon
StephaneLenclud@183
  1929
                if (iDisplay.IsOpen())
StephaneLenclud@183
  1930
                {
StephaneLenclud@183
  1931
                    iDisplay.SetIconOnOff(MiniDisplay.IconType.Recording, activeRecording);
StephaneLenclud@183
  1932
                }                
StephaneLenclud@180
  1933
            }
StephaneLenclud@180
  1934
        }
StephaneLenclud@180
  1935
StephaneLenclud@180
  1936
        /// <summary>
sl@36
  1937
        ///
sl@33
  1938
        /// </summary>
sl@33
  1939
        /// <param name="aClient"></param>
sl@33
  1940
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
  1941
        {
StephaneLenclud@148
  1942
            Debug.Print("UpdateClientTreeViewNode");
StephaneLenclud@148
  1943
sl@33
  1944
            if (aClient == null)
sl@33
  1945
            {
sl@33
  1946
                return;
sl@33
  1947
            }
sl@33
  1948
StephaneLenclud@180
  1949
            //Hook in record icon update too
StephaneLenclud@180
  1950
            UpdateRecordingNotification();
StephaneLenclud@180
  1951
sl@33
  1952
            TreeNode node = null;
sl@33
  1953
            //Check that our client node already exists
sl@33
  1954
            //Get our client root node using its key which is our session ID
StephaneLenclud@188
  1955
            TreeNode[] nodes = iTreeViewClients.Nodes.Find(aClient.SessionId, false);
sl@33
  1956
            if (nodes.Count()>0)
sl@33
  1957
            {
sl@33
  1958
                //We already have a node for that client
sl@33
  1959
                node = nodes[0];
sl@33
  1960
                //Clear children as we are going to recreate them below
sl@33
  1961
                node.Nodes.Clear();
sl@33
  1962
            }
sl@33
  1963
            else
sl@33
  1964
            {
sl@33
  1965
                //Client node does not exists create a new one
StephaneLenclud@188
  1966
                iTreeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
StephaneLenclud@188
  1967
                node = iTreeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
  1968
            }
sl@33
  1969
sl@33
  1970
            if (node != null)
sl@33
  1971
            {
sl@33
  1972
                //Change its name
StephaneLenclud@184
  1973
                if (!String.IsNullOrEmpty(aClient.Name))
sl@33
  1974
                {
StephaneLenclud@184
  1975
                    //We have a name, use it as text for our root node
sl@33
  1976
                    node.Text = aClient.Name;
sl@32
  1977
                    //Add a child with SessionId
sl@33
  1978
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
  1979
                }
sl@33
  1980
                else
sl@33
  1981
                {
sl@33
  1982
                    //No name, use session ID instead
sl@33
  1983
                    node.Text = aClient.SessionId;
sl@33
  1984
                }
sl@36
  1985
StephaneLenclud@184
  1986
                //Display client priority
StephaneLenclud@184
  1987
                node.Nodes.Add(new TreeNode("Priority: " + aClient.Priority));
StephaneLenclud@184
  1988
sl@67
  1989
                if (aClient.Fields.Count > 0)
sl@33
  1990
                {
sl@33
  1991
                    //Create root node for our texts
sl@70
  1992
                    TreeNode textsRoot = new TreeNode("Fields");
sl@33
  1993
                    node.Nodes.Add(textsRoot);
sl@33
  1994
                    //For each text add a new entry
sl@67
  1995
                    foreach (DataField field in aClient.Fields)
sl@33
  1996
                    {
StephaneLenclud@172
  1997
                        if (field.IsTextField)
sl@67
  1998
                        {
StephaneLenclud@172
  1999
                            TextField textField = (TextField)field;
sl@70
  2000
                            textsRoot.Nodes.Add(new TreeNode("[Text]" + textField.Text));
sl@67
  2001
                        }
StephaneLenclud@172
  2002
                        else if (field.IsBitmapField)
sl@67
  2003
                        {
sl@72
  2004
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap]"));
sl@70
  2005
                        }
StephaneLenclud@172
  2006
                        else if (field.IsRecordingField)
StephaneLenclud@172
  2007
                        {
StephaneLenclud@177
  2008
                            RecordingField recordingField = (RecordingField)field;
StephaneLenclud@177
  2009
                            textsRoot.Nodes.Add(new TreeNode("[Recording]" + recordingField.IsActive));
StephaneLenclud@172
  2010
                        }
sl@33
  2011
                    }
sl@32
  2012
                }
sl@34
  2013
sl@34
  2014
                node.ExpandAll();
sl@32
  2015
            }
sl@30
  2016
        }
sl@17
  2017
sl@60
  2018
        /// <summary>
sl@60
  2019
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  2020
        /// </summary>
sl@60
  2021
        private void UpdateTableLayoutRowStyles()
sl@60
  2022
        {
StephaneLenclud@175
  2023
            foreach (RowStyle rowStyle in iTableLayoutPanel.RowStyles)
sl@60
  2024
            {
sl@60
  2025
                rowStyle.SizeType = SizeType.Percent;
StephaneLenclud@175
  2026
                rowStyle.Height = 100 / iTableLayoutPanel.RowCount;
sl@60
  2027
            }
sl@60
  2028
        }
sl@60
  2029
sl@63
  2030
        /// <summary>
sl@63
  2031
        /// Update our display table layout.
StephaneLenclud@175
  2032
        /// Will instanciated every field control as defined by our client.
StephaneLenclud@175
  2033
        /// Fields must be specified by rows from the left.
sl@63
  2034
        /// </summary>
sl@63
  2035
        /// <param name="aLayout"></param>
sl@65
  2036
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  2037
        {
StephaneLenclud@175
  2038
            Debug.Print("UpdateTableLayoutPanel");
StephaneLenclud@148
  2039
StephaneLenclud@106
  2040
			if (aClient == null)
StephaneLenclud@106
  2041
			{
StephaneLenclud@106
  2042
				//Just drop it
StephaneLenclud@106
  2043
				return;
StephaneLenclud@106
  2044
			}
StephaneLenclud@106
  2045
StephaneLenclud@106
  2046
sl@65
  2047
            TableLayout layout = aClient.Layout;
sl@70
  2048
StephaneLenclud@141
  2049
            //First clean our current panel
StephaneLenclud@175
  2050
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  2051
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  2052
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@175
  2053
            iTableLayoutPanel.RowCount = 0;
StephaneLenclud@175
  2054
            iTableLayoutPanel.ColumnCount = 0;
sl@63
  2055
StephaneLenclud@175
  2056
            //Then recreate our rows...
StephaneLenclud@175
  2057
            while (iTableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  2058
            {
StephaneLenclud@175
  2059
                iTableLayoutPanel.RowCount++;
sl@63
  2060
            }
sl@63
  2061
StephaneLenclud@175
  2062
            // ...and columns 
StephaneLenclud@175
  2063
            while (iTableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  2064
            {
StephaneLenclud@175
  2065
                iTableLayoutPanel.ColumnCount++;
sl@63
  2066
            }
sl@63
  2067
StephaneLenclud@175
  2068
            //For each column
StephaneLenclud@175
  2069
            for (int i = 0; i < iTableLayoutPanel.ColumnCount; i++)
sl@63
  2070
            {
sl@63
  2071
                //Create our column styles
StephaneLenclud@175
  2072
                this.iTableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  2073
StephaneLenclud@175
  2074
                //For each rows
StephaneLenclud@175
  2075
                for (int j = 0; j < iTableLayoutPanel.RowCount; j++)
sl@63
  2076
                {
sl@63
  2077
                    if (i == 0)
sl@63
  2078
                    {
sl@63
  2079
                        //Create our row styles
StephaneLenclud@175
  2080
                        this.iTableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  2081
                    }
StephaneLenclud@176
  2082
                    else
sl@70
  2083
                    {
sl@70
  2084
                        continue;
sl@70
  2085
                    }
sl@63
  2086
                }
sl@63
  2087
            }
sl@63
  2088
StephaneLenclud@176
  2089
            //For each field
StephaneLenclud@176
  2090
            foreach (DataField field in aClient.Fields)
sl@70
  2091
            {
StephaneLenclud@176
  2092
                if (!field.IsTableField)
StephaneLenclud@176
  2093
                {
StephaneLenclud@176
  2094
                    //That field is not taking part in our table layout skip it
StephaneLenclud@176
  2095
                    continue;
StephaneLenclud@176
  2096
                }
StephaneLenclud@176
  2097
StephaneLenclud@176
  2098
                TableField tableField = (TableField)field;
StephaneLenclud@176
  2099
StephaneLenclud@176
  2100
                //Create a control corresponding to the field specified for that cell
StephaneLenclud@176
  2101
                Control control = CreateControlForDataField(tableField);
StephaneLenclud@176
  2102
StephaneLenclud@176
  2103
                //Add newly created control to our table layout at the specified row and column
StephaneLenclud@176
  2104
                iTableLayoutPanel.Controls.Add(control, tableField.Column, tableField.Row);
StephaneLenclud@176
  2105
                //Make sure we specify column and row span for that new control
StephaneLenclud@176
  2106
                iTableLayoutPanel.SetColumnSpan(control, tableField.ColumnSpan);
StephaneLenclud@176
  2107
                iTableLayoutPanel.SetRowSpan(control, tableField.RowSpan);
sl@70
  2108
            }
sl@70
  2109
StephaneLenclud@176
  2110
sl@63
  2111
            CheckFontHeight();
sl@63
  2112
        }
sl@63
  2113
sl@68
  2114
        /// <summary>
sl@70
  2115
        /// Check our type of data field and create corresponding control
sl@68
  2116
        /// </summary>
sl@68
  2117
        /// <param name="aField"></param>
sl@69
  2118
        private Control CreateControlForDataField(DataField aField)
sl@68
  2119
        {
sl@68
  2120
            Control control=null;
StephaneLenclud@172
  2121
            if (aField.IsTextField)
sl@68
  2122
            {
sl@68
  2123
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  2124
                label.AutoEllipsis = true;
sl@68
  2125
                label.AutoSize = true;
sl@68
  2126
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  2127
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2128
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  2129
                label.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@176
  2130
                label.Name = "marqueeLabel" + aField;
sl@68
  2131
                label.OwnTimer = false;
StephaneLenclud@106
  2132
                label.PixelsPerSecond = cds.ScrollingSpeedInPixelsPerSecond;
sl@100
  2133
                label.Separator = cds.Separator;
sl@100
  2134
                label.MinFontSize = cds.MinFontSize;
sl@100
  2135
                label.ScaleToFit = cds.ScaleToFit;
sl@68
  2136
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  2137
                //control.TabIndex = 2;
sl@68
  2138
                label.Font = cds.Font;
sl@68
  2139
StephaneLenclud@172
  2140
                TextField field = (TextField)aField;
StephaneLenclud@172
  2141
                label.TextAlign = field.Alignment;
sl@68
  2142
                label.UseCompatibleTextRendering = true;
StephaneLenclud@172
  2143
                label.Text = field.Text;
sl@68
  2144
                //
sl@68
  2145
                control = label;
sl@68
  2146
            }
StephaneLenclud@172
  2147
            else if (aField.IsBitmapField)
sl@68
  2148
            {
sl@68
  2149
                //Create picture box
sl@68
  2150
                PictureBox picture = new PictureBox();
sl@68
  2151
                picture.AutoSize = true;
sl@68
  2152
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  2153
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2154
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  2155
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  2156
                picture.Name = "pictureBox" + aField;
sl@68
  2157
                //Set our image
StephaneLenclud@172
  2158
                BitmapField field = (BitmapField)aField;
StephaneLenclud@172
  2159
                picture.Image = field.Bitmap;
sl@68
  2160
                //
sl@68
  2161
                control = picture;
sl@68
  2162
            }
StephaneLenclud@172
  2163
            //TODO: Handle recording field?
sl@68
  2164
sl@69
  2165
            return control;
sl@68
  2166
        }
sl@68
  2167
StephaneLenclud@103
  2168
		/// <summary>
StephaneLenclud@103
  2169
		/// Called when the user selected a new display type.
StephaneLenclud@103
  2170
		/// </summary>
StephaneLenclud@103
  2171
		/// <param name="sender"></param>
StephaneLenclud@103
  2172
		/// <param name="e"></param>
sl@46
  2173
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  2174
        {
StephaneLenclud@103
  2175
			//Store the selected display type in our settings
sl@48
  2176
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  2177
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  2178
            Properties.Settings.Default.Save();
StephaneLenclud@103
  2179
StephaneLenclud@103
  2180
			//Try re-opening the display connection if we were already connected.
StephaneLenclud@103
  2181
			//Otherwise just update our status to reflect display type change.
sl@51
  2182
            if (iDisplay.IsOpen())
sl@51
  2183
            {
sl@51
  2184
                OpenDisplayConnection();
sl@51
  2185
            }
sl@51
  2186
            else
sl@51
  2187
            {
sl@51
  2188
                UpdateStatus();
sl@51
  2189
            }
sl@46
  2190
        }
sl@46
  2191
sl@47
  2192
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  2193
        {
sl@47
  2194
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  2195
            {
sl@51
  2196
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  2197
sl@51
  2198
                if (interval > 0)
sl@51
  2199
                {
sl@51
  2200
                    timer.Interval = interval;
sl@51
  2201
                    cds.TimerInterval = timer.Interval;
sl@51
  2202
                    Properties.Settings.Default.Save();
sl@51
  2203
                }
sl@47
  2204
            }
sl@47
  2205
        }
sl@47
  2206
sl@100
  2207
        private void maskedTextBoxMinFontSize_TextChanged(object sender, EventArgs e)
sl@100
  2208
        {
sl@100
  2209
            if (maskedTextBoxMinFontSize.Text != "")
sl@100
  2210
            {
sl@100
  2211
                int minFontSize = Convert.ToInt32(maskedTextBoxMinFontSize.Text);
sl@100
  2212
sl@100
  2213
                if (minFontSize > 0)
sl@100
  2214
                {
sl@100
  2215
                    cds.MinFontSize = minFontSize;
sl@100
  2216
                    Properties.Settings.Default.Save();
StephaneLenclud@106
  2217
					//We need to recreate our layout for that change to take effect
StephaneLenclud@106
  2218
					UpdateTableLayoutPanel(iCurrentClientData);
sl@100
  2219
                }
sl@100
  2220
            }
sl@100
  2221
        }
sl@100
  2222
StephaneLenclud@106
  2223
StephaneLenclud@106
  2224
		private void maskedTextBoxScrollingSpeed_TextChanged(object sender, EventArgs e)
StephaneLenclud@106
  2225
		{
StephaneLenclud@106
  2226
			if (maskedTextBoxScrollingSpeed.Text != "")
StephaneLenclud@106
  2227
			{
StephaneLenclud@106
  2228
				int scrollingSpeed = Convert.ToInt32(maskedTextBoxScrollingSpeed.Text);
StephaneLenclud@106
  2229
StephaneLenclud@106
  2230
				if (scrollingSpeed > 0)
StephaneLenclud@106
  2231
				{
StephaneLenclud@106
  2232
					cds.ScrollingSpeedInPixelsPerSecond = scrollingSpeed;
StephaneLenclud@106
  2233
					Properties.Settings.Default.Save();
StephaneLenclud@106
  2234
					//We need to recreate our layout for that change to take effect
StephaneLenclud@106
  2235
					UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@106
  2236
				}
StephaneLenclud@106
  2237
			}
StephaneLenclud@106
  2238
		}
StephaneLenclud@106
  2239
sl@100
  2240
        private void textBoxScrollLoopSeparator_TextChanged(object sender, EventArgs e)
sl@100
  2241
        {
sl@100
  2242
            cds.Separator = textBoxScrollLoopSeparator.Text;
sl@100
  2243
            Properties.Settings.Default.Save();
StephaneLenclud@110
  2244
StephaneLenclud@110
  2245
			//Update our text fields
StephaneLenclud@175
  2246
			foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
StephaneLenclud@110
  2247
			{
StephaneLenclud@110
  2248
				ctrl.Separator = cds.Separator;
StephaneLenclud@110
  2249
			}
StephaneLenclud@110
  2250
sl@100
  2251
        }
sl@100
  2252
sl@52
  2253
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  2254
        {
sl@52
  2255
            iDisplay.PowerOn();
sl@52
  2256
        }
sl@52
  2257
sl@52
  2258
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  2259
        {
sl@52
  2260
            iDisplay.PowerOff();
sl@52
  2261
        }
sl@52
  2262
sl@53
  2263
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  2264
        {
StephaneLenclud@122
  2265
			ShowClock();
sl@53
  2266
        }
sl@53
  2267
sl@53
  2268
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  2269
        {
StephaneLenclud@122
  2270
			HideClock();
sl@53
  2271
        }
sl@88
  2272
sl@88
  2273
        private void buttonUpdate_Click(object sender, EventArgs e)
sl@88
  2274
        {
sl@88
  2275
            InstallUpdateSyncWithInfo();
sl@88
  2276
        }
sl@88
  2277
StephaneLenclud@122
  2278
		/// <summary>
StephaneLenclud@122
  2279
		/// 
StephaneLenclud@122
  2280
		/// </summary>
StephaneLenclud@122
  2281
		void ShowClock()
StephaneLenclud@122
  2282
		{
StephaneLenclud@122
  2283
			if (!iDisplay.IsOpen())
StephaneLenclud@122
  2284
			{
StephaneLenclud@122
  2285
				return;
StephaneLenclud@122
  2286
			}
StephaneLenclud@122
  2287
StephaneLenclud@122
  2288
			//Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@122
  2289
			iSkipFrameRendering = true;
StephaneLenclud@122
  2290
			//Clear our screen 
StephaneLenclud@122
  2291
			iDisplay.Clear();
StephaneLenclud@122
  2292
			iDisplay.SwapBuffers();
StephaneLenclud@122
  2293
			//Then show our clock
StephaneLenclud@122
  2294
			iDisplay.ShowClock();
StephaneLenclud@122
  2295
		}
StephaneLenclud@122
  2296
StephaneLenclud@122
  2297
		/// <summary>
StephaneLenclud@122
  2298
		/// 
StephaneLenclud@122
  2299
		/// </summary>
StephaneLenclud@122
  2300
		void HideClock()
StephaneLenclud@122
  2301
		{
StephaneLenclud@122
  2302
			if (!iDisplay.IsOpen())
StephaneLenclud@122
  2303
			{
StephaneLenclud@122
  2304
				return;
StephaneLenclud@122
  2305
			}
StephaneLenclud@122
  2306
StephaneLenclud@122
  2307
			//Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@122
  2308
			iSkipFrameRendering = false;
StephaneLenclud@122
  2309
			iDisplay.HideClock();
StephaneLenclud@122
  2310
		}
sl@88
  2311
sl@88
  2312
        private void InstallUpdateSyncWithInfo()
sl@88
  2313
        {
sl@88
  2314
            UpdateCheckInfo info = null;
sl@88
  2315
sl@88
  2316
            if (ApplicationDeployment.IsNetworkDeployed)
sl@88
  2317
            {
sl@88
  2318
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
sl@88
  2319
sl@88
  2320
                try
sl@88
  2321
                {
sl@88
  2322
                    info = ad.CheckForDetailedUpdate();
sl@88
  2323
sl@88
  2324
                }
sl@88
  2325
                catch (DeploymentDownloadException dde)
sl@88
  2326
                {
sl@88
  2327
                    MessageBox.Show("The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " + dde.Message);
sl@88
  2328
                    return;
sl@88
  2329
                }
sl@88
  2330
                catch (InvalidDeploymentException ide)
sl@88
  2331
                {
sl@88
  2332
                    MessageBox.Show("Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " + ide.Message);
sl@88
  2333
                    return;
sl@88
  2334
                }
sl@88
  2335
                catch (InvalidOperationException ioe)
sl@88
  2336
                {
sl@88
  2337
                    MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message);
sl@88
  2338
                    return;
sl@88
  2339
                }
sl@88
  2340
sl@90
  2341
				if (info.UpdateAvailable)
sl@90
  2342
				{
sl@90
  2343
					Boolean doUpdate = true;
sl@88
  2344
sl@90
  2345
					if (!info.IsUpdateRequired)
sl@90
  2346
					{
sl@90
  2347
						DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel);
sl@90
  2348
						if (!(DialogResult.OK == dr))
sl@90
  2349
						{
sl@90
  2350
							doUpdate = false;
sl@90
  2351
						}
sl@90
  2352
					}
sl@90
  2353
					else
sl@90
  2354
					{
StephaneLenclud@187
  2355
						// Display a message that the application MUST reboot. Display the minimum required version.
sl@90
  2356
						MessageBox.Show("This application has detected a mandatory update from your current " +
sl@90
  2357
							"version to version " + info.MinimumRequiredVersion.ToString() +
sl@90
  2358
							". The application will now install the update and restart.",
sl@90
  2359
							"Update Available", MessageBoxButtons.OK,
sl@90
  2360
							MessageBoxIcon.Information);
sl@90
  2361
					}
sl@88
  2362
sl@90
  2363
					if (doUpdate)
sl@90
  2364
					{
sl@90
  2365
						try
sl@90
  2366
						{
sl@90
  2367
							ad.Update();
sl@90
  2368
							MessageBox.Show("The application has been upgraded, and will now restart.");
sl@90
  2369
							Application.Restart();
sl@90
  2370
						}
sl@90
  2371
						catch (DeploymentDownloadException dde)
sl@90
  2372
						{
sl@90
  2373
							MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
sl@90
  2374
							return;
sl@90
  2375
						}
sl@90
  2376
					}
sl@90
  2377
				}
sl@90
  2378
				else
sl@90
  2379
				{
sl@90
  2380
					MessageBox.Show("You are already running the latest version.", "Application up-to-date");
sl@90
  2381
				}
sl@88
  2382
            }
sl@88
  2383
        }
sl@92
  2384
sl@94
  2385
sl@94
  2386
		/// <summary>
sl@99
  2387
		/// Used to
sl@94
  2388
		/// </summary>
sl@94
  2389
		private void SysTrayHideShow()
sl@92
  2390
		{
sl@94
  2391
			Visible = !Visible;
sl@94
  2392
			if (Visible)
sl@94
  2393
			{
sl@94
  2394
				Activate();
sl@94
  2395
				WindowState = FormWindowState.Normal;
sl@94
  2396
			}
sl@92
  2397
		}
sl@94
  2398
sl@94
  2399
		/// <summary>
sl@94
  2400
		/// Use to handle minimize events.
sl@94
  2401
		/// </summary>
sl@94
  2402
		/// <param name="sender"></param>
sl@94
  2403
		/// <param name="e"></param>
sl@94
  2404
		private void MainForm_SizeChanged(object sender, EventArgs e)
sl@94
  2405
		{
sl@94
  2406
			if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
sl@94
  2407
			{
sl@94
  2408
				if (Visible)
sl@94
  2409
				{
sl@94
  2410
					SysTrayHideShow();
sl@94
  2411
				}
sl@94
  2412
			}
sl@94
  2413
		}
sl@94
  2414
StephaneLenclud@105
  2415
		/// <summary>
StephaneLenclud@105
  2416
		/// 
StephaneLenclud@105
  2417
		/// </summary>
StephaneLenclud@105
  2418
		/// <param name="sender"></param>
StephaneLenclud@105
  2419
		/// <param name="e"></param>
StephaneLenclud@105
  2420
		private void tableLayoutPanel_SizeChanged(object sender, EventArgs e)
StephaneLenclud@105
  2421
		{
StephaneLenclud@105
  2422
			//Our table layout size has changed which means our display size has changed.
StephaneLenclud@105
  2423
			//We need to re-create our bitmap.
StephaneLenclud@105
  2424
			iCreateBitmap = true;
StephaneLenclud@105
  2425
		}
StephaneLenclud@126
  2426
StephaneLenclud@126
  2427
		/// <summary>
StephaneLenclud@126
  2428
		/// 
StephaneLenclud@126
  2429
		/// </summary>
StephaneLenclud@126
  2430
		/// <param name="sender"></param>
StephaneLenclud@126
  2431
		/// <param name="e"></param>
StephaneLenclud@126
  2432
		private void buttonSelectFile_Click(object sender, EventArgs e)
StephaneLenclud@126
  2433
		{
StephaneLenclud@126
  2434
			//openFileDialog1.InitialDirectory = "c:\\";
StephaneLenclud@126
  2435
			//openFileDialog.Filter = "EXE files (*.exe)|*.exe|All files (*.*)|*.*";
StephaneLenclud@126
  2436
			//openFileDialog.FilterIndex = 1;
StephaneLenclud@126
  2437
			openFileDialog.RestoreDirectory = true;
StephaneLenclud@126
  2438
StephaneLenclud@126
  2439
			if (DlgBox.ShowDialog(openFileDialog) == DialogResult.OK)
StephaneLenclud@126
  2440
			{
StephaneLenclud@126
  2441
				labelStartFileName.Text = openFileDialog.FileName;
StephaneLenclud@126
  2442
				Properties.Settings.Default.StartFileName = openFileDialog.FileName;
StephaneLenclud@126
  2443
				Properties.Settings.Default.Save();
StephaneLenclud@126
  2444
			}
StephaneLenclud@126
  2445
		}
StephaneLenclud@153
  2446
StephaneLenclud@153
  2447
        /// <summary>
StephaneLenclud@153
  2448
        /// 
StephaneLenclud@153
  2449
        /// </summary>
StephaneLenclud@153
  2450
        /// <param name="sender"></param>
StephaneLenclud@153
  2451
        /// <param name="e"></param>
StephaneLenclud@153
  2452
        private void comboBoxOpticalDrives_SelectedIndexChanged(object sender, EventArgs e)
StephaneLenclud@153
  2453
        {
StephaneLenclud@153
  2454
            //Save the optical drive the user selected for ejection
StephaneLenclud@153
  2455
            Properties.Settings.Default.OpticalDriveToEject = comboBoxOpticalDrives.SelectedItem.ToString();
StephaneLenclud@153
  2456
            Properties.Settings.Default.Save();
StephaneLenclud@153
  2457
        }
StephaneLenclud@167
  2458
StephaneLenclud@167
  2459
        /// <summary>
StephaneLenclud@167
  2460
        /// Broadcast messages to subscribers.
StephaneLenclud@167
  2461
        /// </summary>
StephaneLenclud@167
  2462
        /// <param name="message"></param>
StephaneLenclud@167
  2463
        protected override void WndProc(ref Message aMessage)
StephaneLenclud@167
  2464
        {
StephaneLenclud@167
  2465
            if (OnWndProc!=null)
StephaneLenclud@167
  2466
            {
StephaneLenclud@167
  2467
                OnWndProc(ref aMessage);
StephaneLenclud@167
  2468
            }
StephaneLenclud@167
  2469
            
StephaneLenclud@167
  2470
            base.WndProc(ref aMessage);
StephaneLenclud@167
  2471
        }
StephaneLenclud@168
  2472
StephaneLenclud@168
  2473
        private void checkBoxCecEnabled_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2474
        {
StephaneLenclud@168
  2475
            //Save CEC enabled status
StephaneLenclud@168
  2476
            Properties.Settings.Default.CecEnabled = checkBoxCecEnabled.Checked;
StephaneLenclud@168
  2477
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2478
            //
StephaneLenclud@168
  2479
            ResetCec();
StephaneLenclud@168
  2480
        }
StephaneLenclud@168
  2481
StephaneLenclud@168
  2482
        private void comboBoxHdmiPort_SelectedIndexChanged(object sender, EventArgs e)
StephaneLenclud@168
  2483
        {
StephaneLenclud@168
  2484
            //Save CEC HDMI port
StephaneLenclud@168
  2485
            Properties.Settings.Default.CecHdmiPort = Convert.ToByte(comboBoxHdmiPort.SelectedIndex);
StephaneLenclud@168
  2486
            Properties.Settings.Default.CecHdmiPort++;
StephaneLenclud@168
  2487
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2488
            //
StephaneLenclud@168
  2489
            ResetCec();
StephaneLenclud@168
  2490
        }
StephaneLenclud@168
  2491
StephaneLenclud@168
  2492
        private void checkBoxCecMonitorOff_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2493
        {
StephaneLenclud@168
  2494
            Properties.Settings.Default.CecMonitorOff = checkBoxCecMonitorOff.Checked;
StephaneLenclud@168
  2495
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2496
            //
StephaneLenclud@168
  2497
            ResetCec();
StephaneLenclud@168
  2498
        }
StephaneLenclud@168
  2499
StephaneLenclud@168
  2500
        private void checkBoxCecMonitorOn_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2501
        {
StephaneLenclud@168
  2502
            Properties.Settings.Default.CecMonitorOn = checkBoxCecMonitorOn.Checked;
StephaneLenclud@168
  2503
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2504
            //
StephaneLenclud@168
  2505
            ResetCec();
StephaneLenclud@168
  2506
        }
StephaneLenclud@168
  2507
StephaneLenclud@168
  2508
        /// <summary>
StephaneLenclud@168
  2509
        /// 
StephaneLenclud@168
  2510
        /// </summary>
StephaneLenclud@168
  2511
        private void ResetCec()
StephaneLenclud@168
  2512
        {
StephaneLenclud@168
  2513
            if (iCecManager==null)
StephaneLenclud@168
  2514
            {
StephaneLenclud@168
  2515
                //Thus skipping initial UI setup
StephaneLenclud@168
  2516
                return;
StephaneLenclud@168
  2517
            }
StephaneLenclud@168
  2518
StephaneLenclud@168
  2519
            iCecManager.Stop();
StephaneLenclud@168
  2520
            //
StephaneLenclud@168
  2521
            if (Properties.Settings.Default.CecEnabled)
StephaneLenclud@168
  2522
            {
StephaneLenclud@168
  2523
                iCecManager.Start(Handle, "CEC",
StephaneLenclud@168
  2524
                Properties.Settings.Default.CecHdmiPort,
StephaneLenclud@168
  2525
                Properties.Settings.Default.CecMonitorOn,
StephaneLenclud@168
  2526
                Properties.Settings.Default.CecMonitorOff);
StephaneLenclud@168
  2527
            }
StephaneLenclud@168
  2528
        }
StephaneLenclud@189
  2529
StephaneLenclud@189
  2530
        private void ButtonStartIdleClient_Click(object sender, EventArgs e)
StephaneLenclud@189
  2531
        {
StephaneLenclud@189
  2532
            StartIdleClient();
StephaneLenclud@189
  2533
        }
sl@0
  2534
    }
sl@0
  2535
}