Server/MainForm.cs
author Stephane Lenclud
Mon, 25 Jan 2016 23:16:35 +0100
changeset 186 e10744f29ef3
parent 185 729e91c7d5b2
child 187 549e7d887271
permissions -rw-r--r--
Published v0.8.5.0
Now properly work out current client based on priorities when a client connects,
disconnects or change its priority.
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@122
   240
			//Make sure we resume frame rendering
StephaneLenclud@122
   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@104
   275
			//Our display was just closed, update our UI consequently
StephaneLenclud@104
   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@103
  1151
				//We have a display connection
StephaneLenclud@103
  1152
				//Reflect that in our UI
StephaneLenclud@103
  1153
StephaneLenclud@175
  1154
				iTableLayoutPanel.Enabled = true;
StephaneLenclud@105
  1155
				panelDisplay.Enabled = true;
StephaneLenclud@103
  1156
sl@48
  1157
                //Only setup brightness if display is open
sl@48
  1158
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@48
  1159
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
StephaneLenclud@105
  1160
				if (cds.Brightness < iDisplay.MinBrightness() || cds.Brightness > iDisplay.MaxBrightness())
StephaneLenclud@105
  1161
				{
StephaneLenclud@105
  1162
					//Brightness out of range, this can occur when using auto-detect
StephaneLenclud@105
  1163
					//Use max brightness instead
StephaneLenclud@105
  1164
					trackBarBrightness.Value = iDisplay.MaxBrightness();
StephaneLenclud@105
  1165
					iDisplay.SetBrightness(iDisplay.MaxBrightness());
StephaneLenclud@105
  1166
				}
StephaneLenclud@105
  1167
				else
StephaneLenclud@105
  1168
				{
StephaneLenclud@105
  1169
					trackBarBrightness.Value = cds.Brightness;
StephaneLenclud@105
  1170
					iDisplay.SetBrightness(cds.Brightness);
StephaneLenclud@105
  1171
				}
StephaneLenclud@105
  1172
StephaneLenclud@105
  1173
				//Try compute the steps to something that makes sense
sl@48
  1174
                trackBarBrightness.LargeChange = Math.Max(1, (iDisplay.MaxBrightness() - iDisplay.MinBrightness()) / 5);
sl@48
  1175
                trackBarBrightness.SmallChange = 1;
StephaneLenclud@105
  1176
                
sl@48
  1177
                //
sl@7
  1178
                buttonFill.Enabled = true;
sl@7
  1179
                buttonClear.Enabled = true;
sl@7
  1180
                buttonOpen.Enabled = false;
sl@7
  1181
                buttonClose.Enabled = true;
sl@7
  1182
                trackBarBrightness.Enabled = true;
sl@10
  1183
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
  1184
                //+ " - " + iDisplay.SerialNumber();
sl@52
  1185
sl@52
  1186
                if (iDisplay.SupportPowerOnOff())
sl@52
  1187
                {
sl@52
  1188
                    buttonPowerOn.Enabled = true;
sl@52
  1189
                    buttonPowerOff.Enabled = true;
sl@52
  1190
                }
sl@52
  1191
                else
sl@52
  1192
                {
sl@52
  1193
                    buttonPowerOn.Enabled = false;
sl@52
  1194
                    buttonPowerOff.Enabled = false;
sl@52
  1195
                }
sl@53
  1196
sl@53
  1197
                if (iDisplay.SupportClock())
sl@53
  1198
                {
sl@53
  1199
                    buttonShowClock.Enabled = true;
sl@53
  1200
                    buttonHideClock.Enabled = true;
sl@53
  1201
                }
sl@53
  1202
                else
sl@53
  1203
                {
sl@53
  1204
                    buttonShowClock.Enabled = false;
sl@53
  1205
                    buttonHideClock.Enabled = false;
sl@53
  1206
                }
StephaneLenclud@115
  1207
StephaneLenclud@115
  1208
				
StephaneLenclud@115
  1209
				//Check if Volume Label is supported. To date only MDM166AA supports that crap :)
StephaneLenclud@135
  1210
				checkBoxShowVolumeLabel.Enabled = iDisplay.IconCount(MiniDisplay.IconType.VolumeLabel)>0;
StephaneLenclud@115
  1211
StephaneLenclud@115
  1212
				if (cds.ShowVolumeLabel)
StephaneLenclud@115
  1213
				{
StephaneLenclud@135
  1214
                    iDisplay.SetIconOn(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@115
  1215
				}
StephaneLenclud@115
  1216
				else
StephaneLenclud@115
  1217
				{
StephaneLenclud@135
  1218
                    iDisplay.SetIconOff(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@115
  1219
				}
sl@7
  1220
            }
sl@7
  1221
            else
sl@7
  1222
            {
StephaneLenclud@103
  1223
				//Display is connection not available
StephaneLenclud@103
  1224
				//Reflect that in our UI
StephaneLenclud@115
  1225
				checkBoxShowVolumeLabel.Enabled = false;
StephaneLenclud@175
  1226
				iTableLayoutPanel.Enabled = false;
StephaneLenclud@105
  1227
				panelDisplay.Enabled = false;
sl@7
  1228
                buttonFill.Enabled = false;
sl@7
  1229
                buttonClear.Enabled = false;
sl@7
  1230
                buttonOpen.Enabled = true;
sl@7
  1231
                buttonClose.Enabled = false;
sl@7
  1232
                trackBarBrightness.Enabled = false;
sl@52
  1233
                buttonPowerOn.Enabled = false;
sl@52
  1234
                buttonPowerOff.Enabled = false;
sl@53
  1235
                buttonShowClock.Enabled = false;
sl@53
  1236
                buttonHideClock.Enabled = false;
sl@9
  1237
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@48
  1238
                toolStripStatusLabelPower.Text = "N/A";
sl@7
  1239
            }
StephaneLenclud@106
  1240
sl@7
  1241
        }
sl@9
  1242
sl@13
  1243
StephaneLenclud@115
  1244
		/// <summary>
StephaneLenclud@115
  1245
		/// 
StephaneLenclud@115
  1246
		/// </summary>
StephaneLenclud@115
  1247
		/// <param name="sender"></param>
StephaneLenclud@115
  1248
		/// <param name="e"></param>
StephaneLenclud@115
  1249
		private void checkBoxShowVolumeLabel_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@115
  1250
		{
StephaneLenclud@115
  1251
			cds.ShowVolumeLabel = checkBoxShowVolumeLabel.Checked;
StephaneLenclud@115
  1252
			Properties.Settings.Default.Save();
StephaneLenclud@115
  1253
			UpdateStatus();
StephaneLenclud@115
  1254
		}
sl@13
  1255
sl@9
  1256
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
  1257
        {
sl@16
  1258
            //Save our show borders setting
StephaneLenclud@175
  1259
            iTableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@48
  1260
            cds.ShowBorders = checkBoxShowBorders.Checked;
sl@9
  1261
            Properties.Settings.Default.Save();
sl@57
  1262
            CheckFontHeight();
sl@9
  1263
        }
sl@13
  1264
sl@13
  1265
        private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
sl@13
  1266
        {
sl@16
  1267
            //Save our connect on startup setting
sl@13
  1268
            Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
sl@13
  1269
            Properties.Settings.Default.Save();
sl@13
  1270
        }
sl@13
  1271
sl@94
  1272
		private void checkBoxMinimizeToTray_CheckedChanged(object sender, EventArgs e)
sl@94
  1273
		{
sl@94
  1274
			//Save our "Minimize to tray" setting
sl@94
  1275
			Properties.Settings.Default.MinimizeToTray = checkBoxMinimizeToTray.Checked;
sl@94
  1276
			Properties.Settings.Default.Save();
sl@94
  1277
sl@94
  1278
		}
sl@94
  1279
sl@94
  1280
		private void checkBoxStartMinimized_CheckedChanged(object sender, EventArgs e)
sl@94
  1281
		{
sl@94
  1282
			//Save our "Start minimized" setting
sl@94
  1283
			Properties.Settings.Default.StartMinimized = checkBoxStartMinimized.Checked;
sl@94
  1284
			Properties.Settings.Default.Save();
sl@94
  1285
		}
sl@94
  1286
sl@94
  1287
		private void checkBoxAutoStart_CheckedChanged(object sender, EventArgs e)
sl@94
  1288
		{
sl@94
  1289
			iStartupManager.Startup = checkBoxAutoStart.Checked;
sl@94
  1290
		}
sl@94
  1291
sl@94
  1292
sl@16
  1293
        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
sl@16
  1294
        {
sl@16
  1295
            //Save our reverse screen setting
sl@48
  1296
            cds.ReverseScreen = checkBoxReverseScreen.Checked;
sl@16
  1297
            Properties.Settings.Default.Save();
sl@58
  1298
            SetupPixelDelegates();
sl@16
  1299
        }
sl@16
  1300
sl@57
  1301
        private void checkBoxInverseColors_CheckedChanged(object sender, EventArgs e)
sl@57
  1302
        {
sl@57
  1303
            //Save our inverse colors setting
sl@57
  1304
            cds.InverseColors = checkBoxInverseColors.Checked;
sl@57
  1305
            Properties.Settings.Default.Save();
sl@58
  1306
            SetupPixelDelegates();
sl@57
  1307
        }
sl@57
  1308
sl@100
  1309
        private void checkBoxScaleToFit_CheckedChanged(object sender, EventArgs e)
sl@100
  1310
        {
sl@100
  1311
            //Save our scale to fit setting
sl@100
  1312
            cds.ScaleToFit = checkBoxScaleToFit.Checked;
sl@100
  1313
            Properties.Settings.Default.Save();
sl@100
  1314
            //
sl@100
  1315
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1316
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1317
        }
sl@100
  1318
sl@14
  1319
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
  1320
        {
sl@14
  1321
            if (WindowState == FormWindowState.Minimized)
sl@14
  1322
            {
StephaneLenclud@158
  1323
                // To workaround our empty bitmap bug on Windows 7 we need to recreate our bitmap when the application is minimized
StephaneLenclud@158
  1324
                // That's apparently not needed on Windows 10 but we better leave it in place.
sl@14
  1325
                iCreateBitmap = true;
sl@14
  1326
            }
sl@14
  1327
        }
sl@14
  1328
sl@17
  1329
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@17
  1330
        {
StephaneLenclud@167
  1331
            iCecManager.Stop();
StephaneLenclud@117
  1332
			iNetworkManager.Dispose();
StephaneLenclud@105
  1333
			CloseDisplayConnection();
sl@17
  1334
            StopServer();
sl@32
  1335
            e.Cancel = iClosing;
sl@17
  1336
        }
sl@17
  1337
sl@17
  1338
        public void StartServer()
sl@17
  1339
        {
sl@17
  1340
            iServiceHost = new ServiceHost
sl@17
  1341
                (
sl@55
  1342
                    typeof(Session),
sl@20
  1343
                    new Uri[] { new Uri("net.tcp://localhost:8001/") }
sl@17
  1344
                );
sl@17
  1345
sl@55
  1346
            iServiceHost.AddServiceEndpoint(typeof(IService), new NetTcpBinding(SecurityMode.None, true), "DisplayService");
sl@17
  1347
            iServiceHost.Open();
sl@17
  1348
        }
sl@17
  1349
sl@17
  1350
        public void StopServer()
sl@17
  1351
        {
sl@32
  1352
            if (iClients.Count > 0 && !iClosing)
sl@29
  1353
            {
sl@29
  1354
                //Tell our clients
sl@32
  1355
                iClosing = true;
sl@29
  1356
                BroadcastCloseEvent();
sl@29
  1357
            }
sl@32
  1358
            else if (iClosing)
sl@32
  1359
            {
sl@32
  1360
                if (MessageBox.Show("Force exit?", "Waiting for clients...", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
sl@32
  1361
                {
sl@32
  1362
                    iClosing = false; //We make sure we force close if asked twice
sl@32
  1363
                }
sl@32
  1364
            }
sl@32
  1365
            else
sl@36
  1366
            {
sl@32
  1367
                //We removed that as it often lags for some reason
sl@32
  1368
                //iServiceHost.Close();
sl@32
  1369
            }
sl@17
  1370
        }
sl@17
  1371
sl@21
  1372
        public void BroadcastCloseEvent()
sl@21
  1373
        {
sl@31
  1374
            Trace.TraceInformation("BroadcastCloseEvent - start");
sl@31
  1375
sl@21
  1376
            var inactiveClients = new List<string>();
sl@21
  1377
            foreach (var client in iClients)
sl@21
  1378
            {
sl@21
  1379
                //if (client.Key != eventData.ClientName)
sl@21
  1380
                {
sl@21
  1381
                    try
sl@21
  1382
                    {
sl@31
  1383
                        Trace.TraceInformation("BroadcastCloseEvent - " + client.Key);
sl@33
  1384
                        client.Value.Callback.OnCloseOrder(/*eventData*/);
sl@21
  1385
                    }
sl@21
  1386
                    catch (Exception ex)
sl@21
  1387
                    {
sl@21
  1388
                        inactiveClients.Add(client.Key);
sl@21
  1389
                    }
sl@21
  1390
                }
sl@21
  1391
            }
sl@21
  1392
sl@21
  1393
            if (inactiveClients.Count > 0)
sl@21
  1394
            {
sl@21
  1395
                foreach (var client in inactiveClients)
sl@21
  1396
                {
sl@21
  1397
                    iClients.Remove(client);
sl@30
  1398
                    Program.iMainForm.treeViewClients.Nodes.Remove(Program.iMainForm.treeViewClients.Nodes.Find(client, false)[0]);
sl@21
  1399
                }
sl@21
  1400
            }
sl@97
  1401
sl@97
  1402
			if (iClients.Count==0)
sl@97
  1403
			{
sl@97
  1404
				ClearLayout();
sl@97
  1405
			}
sl@21
  1406
        }
sl@21
  1407
sl@97
  1408
		/// <summary>
sl@97
  1409
		/// Just remove all our fields.
sl@97
  1410
		/// </summary>
sl@97
  1411
		private void ClearLayout()
sl@97
  1412
		{
StephaneLenclud@175
  1413
			iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  1414
			iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  1415
			iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@122
  1416
			iCurrentClientData = null;
sl@97
  1417
		}
sl@97
  1418
StephaneLenclud@106
  1419
		/// <summary>
StephaneLenclud@106
  1420
		/// Just launch a demo client.
StephaneLenclud@106
  1421
		/// </summary>
StephaneLenclud@106
  1422
		private void StartNewClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@106
  1423
		{
StephaneLenclud@106
  1424
			Thread clientThread = new Thread(SharpDisplayClient.Program.MainWithParams);
StephaneLenclud@106
  1425
			SharpDisplayClient.StartParams myParams = new SharpDisplayClient.StartParams(new Point(this.Right, this.Top),aTopText,aBottomText);
StephaneLenclud@106
  1426
			clientThread.Start(myParams);
StephaneLenclud@106
  1427
			BringToFront();
StephaneLenclud@106
  1428
		}
StephaneLenclud@106
  1429
sl@25
  1430
        private void buttonStartClient_Click(object sender, EventArgs e)
sl@25
  1431
        {
StephaneLenclud@106
  1432
			StartNewClient();
sl@25
  1433
        }
sl@25
  1434
sl@27
  1435
        private void buttonSuspend_Click(object sender, EventArgs e)
sl@27
  1436
        {
sl@52
  1437
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
sl@27
  1438
            timer.Enabled = !timer.Enabled;
sl@27
  1439
            if (!timer.Enabled)
sl@27
  1440
            {
sl@52
  1441
                buttonSuspend.Text = "Run";
sl@27
  1442
            }
sl@27
  1443
            else
sl@27
  1444
            {
sl@27
  1445
                buttonSuspend.Text = "Pause";
sl@27
  1446
            }
sl@27
  1447
        }
sl@27
  1448
sl@29
  1449
        private void buttonCloseClients_Click(object sender, EventArgs e)
sl@29
  1450
        {
sl@29
  1451
            BroadcastCloseEvent();
sl@29
  1452
        }
sl@29
  1453
sl@30
  1454
        private void treeViewClients_AfterSelect(object sender, TreeViewEventArgs e)
sl@30
  1455
        {
StephaneLenclud@141
  1456
            //Root node must have at least one child
StephaneLenclud@141
  1457
            if (e.Node.Nodes.Count == 0)
StephaneLenclud@141
  1458
            {
StephaneLenclud@141
  1459
                return;
StephaneLenclud@141
  1460
            }
sl@21
  1461
StephaneLenclud@141
  1462
            //If the selected node is the root node of a client then switch to it
StephaneLenclud@141
  1463
            string sessionId=e.Node.Nodes[0].Text; //First child of a root node is the sessionId
StephaneLenclud@141
  1464
            if (iClients.ContainsKey(sessionId)) //Check that's actually what we are looking at
StephaneLenclud@141
  1465
            {
StephaneLenclud@141
  1466
                //We have a valid session just switch to that client
StephaneLenclud@141
  1467
                SetCurrentClient(sessionId,true);
StephaneLenclud@141
  1468
            }
StephaneLenclud@141
  1469
            
sl@30
  1470
        }
sl@30
  1471
sl@36
  1472
sl@30
  1473
        /// <summary>
sl@36
  1474
        ///
sl@30
  1475
        /// </summary>
sl@30
  1476
        /// <param name="aSessionId"></param>
sl@30
  1477
        /// <param name="aCallback"></param>
sl@55
  1478
        public void AddClientThreadSafe(string aSessionId, ICallback aCallback)
sl@30
  1479
        {
sl@33
  1480
            if (this.InvokeRequired)
sl@30
  1481
            {
sl@30
  1482
                //Not in the proper thread, invoke ourselves
sl@30
  1483
                AddClientDelegate d = new AddClientDelegate(AddClientThreadSafe);
sl@30
  1484
                this.Invoke(d, new object[] { aSessionId, aCallback });
sl@30
  1485
            }
sl@30
  1486
            else
sl@30
  1487
            {
sl@30
  1488
                //We are in the proper thread
sl@30
  1489
                //Add this session to our collection of clients
sl@33
  1490
                ClientData newClient = new ClientData(aSessionId, aCallback);
sl@33
  1491
                Program.iMainForm.iClients.Add(aSessionId, newClient);
sl@30
  1492
                //Add this session to our UI
sl@33
  1493
                UpdateClientTreeViewNode(newClient);
sl@30
  1494
            }
sl@30
  1495
        }
sl@30
  1496
Stephane@186
  1497
Stephane@186
  1498
        /// <summary>
Stephane@186
  1499
        /// Find the client with the highest priority if any.
Stephane@186
  1500
        /// </summary>
Stephane@186
  1501
        /// <returns>Our highest priority client or null if not a single client is connected.</returns>
Stephane@186
  1502
        public ClientData FindHighestPriorityClient()
Stephane@186
  1503
        {
Stephane@186
  1504
            ClientData highestPriorityClient = null;
Stephane@186
  1505
            foreach (var client in iClients)
Stephane@186
  1506
            {
Stephane@186
  1507
                if (highestPriorityClient == null || client.Value.Priority > highestPriorityClient.Priority)
Stephane@186
  1508
                {
Stephane@186
  1509
                    highestPriorityClient = client.Value;
Stephane@186
  1510
                }
Stephane@186
  1511
            }
Stephane@186
  1512
Stephane@186
  1513
            return highestPriorityClient;
Stephane@186
  1514
        }
Stephane@186
  1515
sl@30
  1516
        /// <summary>
sl@36
  1517
        ///
sl@30
  1518
        /// </summary>
sl@30
  1519
        /// <param name="aSessionId"></param>
sl@30
  1520
        public void RemoveClientThreadSafe(string aSessionId)
sl@30
  1521
        {
sl@33
  1522
            if (this.InvokeRequired)
sl@30
  1523
            {
sl@30
  1524
                //Not in the proper thread, invoke ourselves
sl@30
  1525
                RemoveClientDelegate d = new RemoveClientDelegate(RemoveClientThreadSafe);
sl@30
  1526
                this.Invoke(d, new object[] { aSessionId });
sl@30
  1527
            }
sl@30
  1528
            else
sl@30
  1529
            {
sl@30
  1530
                //We are in the proper thread
sl@33
  1531
                //Remove this session from both client collection and UI tree view
sl@30
  1532
                if (Program.iMainForm.iClients.Keys.Contains(aSessionId))
sl@30
  1533
                {
sl@30
  1534
                    Program.iMainForm.iClients.Remove(aSessionId);
sl@30
  1535
                    Program.iMainForm.treeViewClients.Nodes.Remove(Program.iMainForm.treeViewClients.Nodes.Find(aSessionId, false)[0]);
sl@32
  1536
                }
sl@32
  1537
Stephane@186
  1538
                if (iCurrentClientSessionId == aSessionId)
Stephane@186
  1539
                {
Stephane@186
  1540
                    //The current client is closing
Stephane@186
  1541
                    iCurrentClientData = null;
Stephane@186
  1542
                    //Find the client with the highest priority and set it as current
Stephane@186
  1543
                    ClientData newCurrentClient = FindHighestPriorityClient();
Stephane@186
  1544
                    if (newCurrentClient!=null)
Stephane@186
  1545
                    {
Stephane@186
  1546
                        SetCurrentClient(newCurrentClient.SessionId, true);
Stephane@186
  1547
                    }                    
Stephane@186
  1548
                }
Stephane@186
  1549
Stephane@186
  1550
                if (iClients.Count == 0)
sl@97
  1551
				{
sl@97
  1552
					//Clear our screen when last client disconnects
sl@97
  1553
					ClearLayout();
sl@97
  1554
sl@97
  1555
					if (iClosing)
sl@97
  1556
					{
sl@97
  1557
						//We were closing our form
sl@97
  1558
						//All clients are now closed
sl@97
  1559
						//Just resume our close operation
sl@97
  1560
						iClosing = false;
sl@97
  1561
						Close();
sl@97
  1562
					}
sl@97
  1563
				}
sl@30
  1564
            }
sl@30
  1565
        }
sl@30
  1566
sl@30
  1567
        /// <summary>
sl@36
  1568
        ///
sl@30
  1569
        /// </summary>
sl@62
  1570
        /// <param name="aSessionId"></param>
sl@72
  1571
        /// <param name="aLayout"></param>
sl@62
  1572
        public void SetClientLayoutThreadSafe(string aSessionId, TableLayout aLayout)
sl@62
  1573
        {
sl@62
  1574
            if (this.InvokeRequired)
sl@62
  1575
            {
sl@62
  1576
                //Not in the proper thread, invoke ourselves
sl@62
  1577
                SetLayoutDelegate d = new SetLayoutDelegate(SetClientLayoutThreadSafe);
sl@62
  1578
                this.Invoke(d, new object[] { aSessionId, aLayout });
sl@62
  1579
            }
sl@62
  1580
            else
sl@62
  1581
            {
sl@62
  1582
                ClientData client = iClients[aSessionId];
sl@62
  1583
                if (client != null)
sl@62
  1584
                {
StephaneLenclud@148
  1585
                    //Don't change a thing if the layout is the same
StephaneLenclud@148
  1586
                    if (!client.Layout.IsSameAs(aLayout))
StephaneLenclud@148
  1587
                    {
StephaneLenclud@158
  1588
                        Debug.Print("SetClientLayoutThreadSafe: Layout updated.");
StephaneLenclud@148
  1589
                        //Set our client layout then
StephaneLenclud@148
  1590
                        client.Layout = aLayout;
StephaneLenclud@175
  1591
                        //So that next time we update all our fields at ones
StephaneLenclud@175
  1592
                        client.HasNewLayout = true;
StephaneLenclud@158
  1593
                        //Layout has changed clear our fields then
StephaneLenclud@158
  1594
                        client.Fields.Clear();
StephaneLenclud@148
  1595
                        //
StephaneLenclud@148
  1596
                        UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1597
                    }
StephaneLenclud@158
  1598
                    else
StephaneLenclud@158
  1599
                    {
StephaneLenclud@158
  1600
                        Debug.Print("SetClientLayoutThreadSafe: Layout has not changed.");
StephaneLenclud@158
  1601
                    }
sl@62
  1602
                }
sl@62
  1603
            }
sl@62
  1604
        }
sl@62
  1605
sl@62
  1606
        /// <summary>
sl@62
  1607
        ///
sl@62
  1608
        /// </summary>
sl@67
  1609
        /// <param name="aSessionId"></param>
sl@72
  1610
        /// <param name="aField"></param>
sl@75
  1611
        public void SetClientFieldThreadSafe(string aSessionId, DataField aField)
sl@30
  1612
        {
sl@33
  1613
            if (this.InvokeRequired)
sl@30
  1614
            {
sl@30
  1615
                //Not in the proper thread, invoke ourselves
sl@79
  1616
                SetFieldDelegate d = new SetFieldDelegate(SetClientFieldThreadSafe);
sl@72
  1617
                this.Invoke(d, new object[] { aSessionId, aField });
sl@30
  1618
            }
sl@30
  1619
            else
sl@30
  1620
            {
sl@75
  1621
                //We are in the proper thread
sl@75
  1622
                //Call the non-thread-safe variant
sl@75
  1623
                SetClientField(aSessionId, aField);
sl@75
  1624
            }
sl@75
  1625
        }
sl@75
  1626
StephaneLenclud@176
  1627
StephaneLenclud@176
  1628
StephaneLenclud@176
  1629
sl@75
  1630
        /// <summary>
StephaneLenclud@175
  1631
        /// Set a data field in the given client.
sl@75
  1632
        /// </summary>
sl@75
  1633
        /// <param name="aSessionId"></param>
sl@75
  1634
        /// <param name="aField"></param>
sl@75
  1635
        private void SetClientField(string aSessionId, DataField aField)
StephaneLenclud@141
  1636
        {   
StephaneLenclud@141
  1637
            //TODO: should check if the field actually changed?
StephaneLenclud@141
  1638
sl@75
  1639
            ClientData client = iClients[aSessionId];
StephaneLenclud@141
  1640
            bool layoutChanged = false;
StephaneLenclud@148
  1641
            bool contentChanged = true;
StephaneLenclud@141
  1642
StephaneLenclud@176
  1643
            //Fetch our field index
StephaneLenclud@176
  1644
            int fieldIndex = client.FindSameFieldIndex(aField);
StephaneLenclud@176
  1645
StephaneLenclud@176
  1646
            if (fieldIndex < 0)
sl@75
  1647
            {
StephaneLenclud@176
  1648
                //No corresponding field, just bail out
StephaneLenclud@176
  1649
                return;
StephaneLenclud@141
  1650
            }
sl@76
  1651
StephaneLenclud@175
  1652
            //Keep our previous field in there
StephaneLenclud@176
  1653
            DataField previousField = client.Fields[fieldIndex];
StephaneLenclud@176
  1654
            //Just update that field then 
StephaneLenclud@176
  1655
            client.Fields[fieldIndex] = aField;
StephaneLenclud@148
  1656
StephaneLenclud@176
  1657
            if (!aField.IsTableField)
StephaneLenclud@176
  1658
            {
StephaneLenclud@176
  1659
                //We are done then if that field is not in our table layout
StephaneLenclud@176
  1660
                return;
StephaneLenclud@176
  1661
            }
StephaneLenclud@176
  1662
StephaneLenclud@176
  1663
            TableField tableField = (TableField) aField;
StephaneLenclud@172
  1664
StephaneLenclud@175
  1665
            if (previousField.IsSameLayout(aField))
StephaneLenclud@141
  1666
            {
StephaneLenclud@141
  1667
                //If we are updating a field in our current client we need to update it in our panel
StephaneLenclud@141
  1668
                if (aSessionId == iCurrentClientSessionId)
sl@30
  1669
                {
StephaneLenclud@176
  1670
                    Control ctrl=iTableLayoutPanel.GetControlFromPosition(tableField.Column, tableField.Row);
StephaneLenclud@176
  1671
                    if (aField.IsTextField && ctrl is MarqueeLabel)
sl@79
  1672
                    {
StephaneLenclud@172
  1673
                        TextField textField=(TextField)aField;
sl@75
  1674
                        //Text field control already in place, just change the text
StephaneLenclud@176
  1675
                        MarqueeLabel label = (MarqueeLabel)ctrl;
StephaneLenclud@172
  1676
                        contentChanged = (label.Text != textField.Text || label.TextAlign != textField.Alignment);
StephaneLenclud@172
  1677
                        label.Text = textField.Text;
StephaneLenclud@172
  1678
                        label.TextAlign = textField.Alignment;
sl@68
  1679
                    }
StephaneLenclud@176
  1680
                    else if (aField.IsBitmapField && ctrl is PictureBox)
sl@75
  1681
                    {
StephaneLenclud@172
  1682
                        BitmapField bitmapField = (BitmapField)aField;
StephaneLenclud@148
  1683
                        contentChanged = true; //TODO: Bitmap comp or should we leave that to clients?
sl@75
  1684
                        //Bitmap field control already in place just change the bitmap
StephaneLenclud@176
  1685
                        PictureBox pictureBox = (PictureBox)ctrl;
StephaneLenclud@172
  1686
                        pictureBox.Image = bitmapField.Bitmap;
sl@75
  1687
                    }
sl@68
  1688
                    else
sl@68
  1689
                    {
StephaneLenclud@141
  1690
                        layoutChanged = true;
sl@68
  1691
                    }
sl@30
  1692
                }
StephaneLenclud@141
  1693
            }
StephaneLenclud@141
  1694
            else
StephaneLenclud@148
  1695
            {                
StephaneLenclud@141
  1696
                layoutChanged = true;
StephaneLenclud@141
  1697
            }
StephaneLenclud@141
  1698
StephaneLenclud@148
  1699
            //If either content or layout changed we need to update our tree view to reflect the changes
StephaneLenclud@148
  1700
            if (contentChanged || layoutChanged)
StephaneLenclud@141
  1701
            {
StephaneLenclud@141
  1702
                UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1703
                //
StephaneLenclud@148
  1704
                if (layoutChanged)
sl@75
  1705
                {
StephaneLenclud@148
  1706
                    Debug.Print("Layout changed");
StephaneLenclud@148
  1707
                    //Our layout has changed, if we are already the current client we need to update our panel
StephaneLenclud@148
  1708
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@148
  1709
                    {
StephaneLenclud@148
  1710
                        //Apply layout and set data fields.
StephaneLenclud@148
  1711
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@148
  1712
                    }
sl@75
  1713
                }
StephaneLenclud@158
  1714
                else
StephaneLenclud@158
  1715
                {
StephaneLenclud@158
  1716
                    Debug.Print("Layout has not changed.");
StephaneLenclud@158
  1717
                }
StephaneLenclud@158
  1718
            }
StephaneLenclud@158
  1719
            else
StephaneLenclud@158
  1720
            {
StephaneLenclud@158
  1721
                Debug.Print("WARNING: content and layout have not changed!");
StephaneLenclud@141
  1722
            }
sl@75
  1723
StephaneLenclud@141
  1724
            //When a client field is set we try switching to this client to present the new information to our user
StephaneLenclud@141
  1725
            SetCurrentClient(aSessionId);
sl@30
  1726
        }
sl@30
  1727
sl@30
  1728
        /// <summary>
sl@36
  1729
        ///
sl@30
  1730
        /// </summary>
sl@30
  1731
        /// <param name="aTexts"></param>
sl@75
  1732
        public void SetClientFieldsThreadSafe(string aSessionId, System.Collections.Generic.IList<DataField> aFields)
sl@30
  1733
        {
sl@33
  1734
            if (this.InvokeRequired)
sl@30
  1735
            {
sl@30
  1736
                //Not in the proper thread, invoke ourselves
sl@75
  1737
                SetFieldsDelegate d = new SetFieldsDelegate(SetClientFieldsThreadSafe);
sl@72
  1738
                this.Invoke(d, new object[] { aSessionId, aFields });
sl@30
  1739
            }
sl@30
  1740
            else
sl@30
  1741
            {
StephaneLenclud@175
  1742
                ClientData client = iClients[aSessionId];
StephaneLenclud@175
  1743
StephaneLenclud@175
  1744
                if (client.HasNewLayout)
sl@30
  1745
                {
StephaneLenclud@175
  1746
                    //TODO: Assert client.Count == 0
StephaneLenclud@175
  1747
                    //Our layout was just changed
StephaneLenclud@175
  1748
                    //Do some special handling to avoid re-creating our panel N times, once for each fields
StephaneLenclud@175
  1749
                    client.HasNewLayout = false;
StephaneLenclud@175
  1750
                    //Just set all our fields then
StephaneLenclud@175
  1751
                    client.Fields.AddRange(aFields);
StephaneLenclud@175
  1752
                    //Try switch to that client
StephaneLenclud@175
  1753
                    SetCurrentClient(aSessionId);
StephaneLenclud@175
  1754
StephaneLenclud@175
  1755
                    //If we are updating the current client update our panel
StephaneLenclud@175
  1756
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@175
  1757
                    {
StephaneLenclud@175
  1758
                        //Apply layout and set data fields.
StephaneLenclud@175
  1759
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@175
  1760
                    }
StephaneLenclud@176
  1761
StephaneLenclud@176
  1762
                    UpdateClientTreeViewNode(client);
StephaneLenclud@175
  1763
                }
StephaneLenclud@175
  1764
                else
StephaneLenclud@175
  1765
                {
StephaneLenclud@175
  1766
                    //Put each our text fields in a label control
StephaneLenclud@175
  1767
                    foreach (DataField field in aFields)
StephaneLenclud@175
  1768
                    {
StephaneLenclud@175
  1769
                        SetClientField(aSessionId, field);
StephaneLenclud@175
  1770
                    }
sl@30
  1771
                }
sl@30
  1772
            }
sl@32
  1773
        }
sl@30
  1774
sl@67
  1775
        /// <summary>
sl@67
  1776
        ///
sl@67
  1777
        /// </summary>
sl@67
  1778
        /// <param name="aSessionId"></param>
sl@32
  1779
        /// <param name="aName"></param>
sl@32
  1780
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
  1781
        {
sl@32
  1782
            if (this.InvokeRequired)
sl@32
  1783
            {
sl@32
  1784
                //Not in the proper thread, invoke ourselves
sl@32
  1785
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
sl@32
  1786
                this.Invoke(d, new object[] { aSessionId, aName });
sl@32
  1787
            }
sl@32
  1788
            else
sl@32
  1789
            {
sl@32
  1790
                //We are in the proper thread
sl@33
  1791
                //Get our client
sl@33
  1792
                ClientData client = iClients[aSessionId];
sl@33
  1793
                if (client != null)
sl@32
  1794
                {
sl@33
  1795
                    //Set its name
sl@33
  1796
                    client.Name = aName;
sl@33
  1797
                    //Update our tree-view
sl@33
  1798
                    UpdateClientTreeViewNode(client);
sl@33
  1799
                }
sl@33
  1800
            }
sl@33
  1801
        }
sl@33
  1802
StephaneLenclud@184
  1803
        ///
StephaneLenclud@184
  1804
        public void SetClientPriorityThreadSafe(string aSessionId, uint aPriority)
StephaneLenclud@184
  1805
        {
StephaneLenclud@184
  1806
            if (this.InvokeRequired)
StephaneLenclud@184
  1807
            {
StephaneLenclud@184
  1808
                //Not in the proper thread, invoke ourselves
StephaneLenclud@184
  1809
                SetClientPriorityDelegate d = new SetClientPriorityDelegate(SetClientPriorityThreadSafe);
StephaneLenclud@184
  1810
                this.Invoke(d, new object[] { aSessionId, aPriority });
StephaneLenclud@184
  1811
            }
StephaneLenclud@184
  1812
            else
StephaneLenclud@184
  1813
            {
StephaneLenclud@184
  1814
                //We are in the proper thread
StephaneLenclud@184
  1815
                //Get our client
StephaneLenclud@184
  1816
                ClientData client = iClients[aSessionId];
StephaneLenclud@184
  1817
                if (client != null)
StephaneLenclud@184
  1818
                {
StephaneLenclud@184
  1819
                    //Set its name
StephaneLenclud@184
  1820
                    client.Priority = aPriority;
StephaneLenclud@184
  1821
                    //Update our tree-view
StephaneLenclud@184
  1822
                    UpdateClientTreeViewNode(client);
Stephane@186
  1823
                    //Change our current client as per new priority
Stephane@186
  1824
                    ClientData newCurrentClient = FindHighestPriorityClient();
Stephane@186
  1825
                    if (newCurrentClient!=null)
Stephane@186
  1826
                    {
Stephane@186
  1827
                        SetCurrentClient(newCurrentClient.SessionId);
Stephane@186
  1828
                    }
StephaneLenclud@184
  1829
                }
StephaneLenclud@184
  1830
            }
StephaneLenclud@184
  1831
        }
StephaneLenclud@184
  1832
sl@33
  1833
        /// <summary>
StephaneLenclud@183
  1834
        /// 
StephaneLenclud@183
  1835
        /// </summary>
StephaneLenclud@183
  1836
        /// <param name="value"></param>
StephaneLenclud@183
  1837
        /// <param name="maxChars"></param>
StephaneLenclud@183
  1838
        /// <returns></returns>
StephaneLenclud@183
  1839
        public static string Truncate(string value, int maxChars)
StephaneLenclud@183
  1840
        {
StephaneLenclud@183
  1841
            return value.Length <= maxChars ? value : value.Substring(0, maxChars-3) + "...";
StephaneLenclud@183
  1842
        }
StephaneLenclud@183
  1843
StephaneLenclud@183
  1844
        /// <summary>
StephaneLenclud@180
  1845
        /// Update our recording notification.
StephaneLenclud@180
  1846
        /// </summary>
StephaneLenclud@180
  1847
        private void UpdateRecordingNotification()
StephaneLenclud@180
  1848
        {
StephaneLenclud@180
  1849
            //Go through each 
StephaneLenclud@180
  1850
            bool activeRecording = false;
StephaneLenclud@180
  1851
            string text="";
StephaneLenclud@180
  1852
            RecordingField recField=new RecordingField();
StephaneLenclud@180
  1853
            foreach (var client in iClients)
StephaneLenclud@180
  1854
            {
StephaneLenclud@180
  1855
                RecordingField rec=(RecordingField)client.Value.FindSameFieldAs(recField);
StephaneLenclud@180
  1856
                if (rec!=null && rec.IsActive)
StephaneLenclud@180
  1857
                {
StephaneLenclud@180
  1858
                    activeRecording = true;
StephaneLenclud@183
  1859
                    //Don't break cause we are collecting the names/texts.
StephaneLenclud@183
  1860
                    if (!String.IsNullOrEmpty(rec.Text))
StephaneLenclud@183
  1861
                    {
StephaneLenclud@183
  1862
                        text += (rec.Text + "\n");
StephaneLenclud@183
  1863
                    }
StephaneLenclud@183
  1864
                    else
StephaneLenclud@183
  1865
                    {
StephaneLenclud@183
  1866
                        //Not text for that recording, use client name instead
StephaneLenclud@183
  1867
                        text += client.Value.Name + " recording\n";
StephaneLenclud@183
  1868
                    }
StephaneLenclud@183
  1869
                    
StephaneLenclud@180
  1870
                }
StephaneLenclud@180
  1871
            }
StephaneLenclud@180
  1872
StephaneLenclud@183
  1873
            //Update our text no matter what, can't have more than 63 characters otherwise it throws an exception.
StephaneLenclud@183
  1874
            iRecordingNotification.Text = Truncate(text,63);
StephaneLenclud@183
  1875
StephaneLenclud@180
  1876
            //Change visibility of notification if needed
StephaneLenclud@180
  1877
            if (iRecordingNotification.Visible != activeRecording)
StephaneLenclud@183
  1878
            {                
StephaneLenclud@180
  1879
                iRecordingNotification.Visible = activeRecording;
Stephane@182
  1880
                //Assuming the notification icon is in sync with our display icon
Stephane@182
  1881
                //Take care of our REC icon
StephaneLenclud@183
  1882
                if (iDisplay.IsOpen())
StephaneLenclud@183
  1883
                {
StephaneLenclud@183
  1884
                    iDisplay.SetIconOnOff(MiniDisplay.IconType.Recording, activeRecording);
StephaneLenclud@183
  1885
                }                
StephaneLenclud@180
  1886
            }
StephaneLenclud@180
  1887
        }
StephaneLenclud@180
  1888
StephaneLenclud@180
  1889
        /// <summary>
sl@36
  1890
        ///
sl@33
  1891
        /// </summary>
sl@33
  1892
        /// <param name="aClient"></param>
sl@33
  1893
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
  1894
        {
StephaneLenclud@148
  1895
            Debug.Print("UpdateClientTreeViewNode");
StephaneLenclud@148
  1896
sl@33
  1897
            if (aClient == null)
sl@33
  1898
            {
sl@33
  1899
                return;
sl@33
  1900
            }
sl@33
  1901
StephaneLenclud@180
  1902
            //Hook in record icon update too
StephaneLenclud@180
  1903
            UpdateRecordingNotification();
StephaneLenclud@180
  1904
sl@33
  1905
            TreeNode node = null;
sl@33
  1906
            //Check that our client node already exists
sl@33
  1907
            //Get our client root node using its key which is our session ID
sl@33
  1908
            TreeNode[] nodes = treeViewClients.Nodes.Find(aClient.SessionId, false);
sl@33
  1909
            if (nodes.Count()>0)
sl@33
  1910
            {
sl@33
  1911
                //We already have a node for that client
sl@33
  1912
                node = nodes[0];
sl@33
  1913
                //Clear children as we are going to recreate them below
sl@33
  1914
                node.Nodes.Clear();
sl@33
  1915
            }
sl@33
  1916
            else
sl@33
  1917
            {
sl@33
  1918
                //Client node does not exists create a new one
sl@33
  1919
                treeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
sl@33
  1920
                node = treeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
  1921
            }
sl@33
  1922
sl@33
  1923
            if (node != null)
sl@33
  1924
            {
sl@33
  1925
                //Change its name
StephaneLenclud@184
  1926
                if (!String.IsNullOrEmpty(aClient.Name))
sl@33
  1927
                {
StephaneLenclud@184
  1928
                    //We have a name, use it as text for our root node
sl@33
  1929
                    node.Text = aClient.Name;
sl@32
  1930
                    //Add a child with SessionId
sl@33
  1931
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
  1932
                }
sl@33
  1933
                else
sl@33
  1934
                {
sl@33
  1935
                    //No name, use session ID instead
sl@33
  1936
                    node.Text = aClient.SessionId;
sl@33
  1937
                }
sl@36
  1938
StephaneLenclud@184
  1939
                //Display client priority
StephaneLenclud@184
  1940
                node.Nodes.Add(new TreeNode("Priority: " + aClient.Priority));
StephaneLenclud@184
  1941
sl@67
  1942
                if (aClient.Fields.Count > 0)
sl@33
  1943
                {
sl@33
  1944
                    //Create root node for our texts
sl@70
  1945
                    TreeNode textsRoot = new TreeNode("Fields");
sl@33
  1946
                    node.Nodes.Add(textsRoot);
sl@33
  1947
                    //For each text add a new entry
sl@67
  1948
                    foreach (DataField field in aClient.Fields)
sl@33
  1949
                    {
StephaneLenclud@172
  1950
                        if (field.IsTextField)
sl@67
  1951
                        {
StephaneLenclud@172
  1952
                            TextField textField = (TextField)field;
sl@70
  1953
                            textsRoot.Nodes.Add(new TreeNode("[Text]" + textField.Text));
sl@67
  1954
                        }
StephaneLenclud@172
  1955
                        else if (field.IsBitmapField)
sl@67
  1956
                        {
sl@72
  1957
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap]"));
sl@70
  1958
                        }
StephaneLenclud@172
  1959
                        else if (field.IsRecordingField)
StephaneLenclud@172
  1960
                        {
StephaneLenclud@177
  1961
                            RecordingField recordingField = (RecordingField)field;
StephaneLenclud@177
  1962
                            textsRoot.Nodes.Add(new TreeNode("[Recording]" + recordingField.IsActive));
StephaneLenclud@172
  1963
                        }
sl@33
  1964
                    }
sl@32
  1965
                }
sl@34
  1966
sl@34
  1967
                node.ExpandAll();
sl@32
  1968
            }
sl@30
  1969
        }
sl@17
  1970
sl@60
  1971
        /// <summary>
sl@60
  1972
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  1973
        /// </summary>
sl@60
  1974
        private void UpdateTableLayoutRowStyles()
sl@60
  1975
        {
StephaneLenclud@175
  1976
            foreach (RowStyle rowStyle in iTableLayoutPanel.RowStyles)
sl@60
  1977
            {
sl@60
  1978
                rowStyle.SizeType = SizeType.Percent;
StephaneLenclud@175
  1979
                rowStyle.Height = 100 / iTableLayoutPanel.RowCount;
sl@60
  1980
            }
sl@60
  1981
        }
sl@60
  1982
sl@63
  1983
        /// <summary>
sl@63
  1984
        /// Update our display table layout.
StephaneLenclud@175
  1985
        /// Will instanciated every field control as defined by our client.
StephaneLenclud@175
  1986
        /// Fields must be specified by rows from the left.
sl@63
  1987
        /// </summary>
sl@63
  1988
        /// <param name="aLayout"></param>
sl@65
  1989
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  1990
        {
StephaneLenclud@175
  1991
            Debug.Print("UpdateTableLayoutPanel");
StephaneLenclud@148
  1992
StephaneLenclud@106
  1993
			if (aClient == null)
StephaneLenclud@106
  1994
			{
StephaneLenclud@106
  1995
				//Just drop it
StephaneLenclud@106
  1996
				return;
StephaneLenclud@106
  1997
			}
StephaneLenclud@106
  1998
StephaneLenclud@106
  1999
sl@65
  2000
            TableLayout layout = aClient.Layout;
sl@70
  2001
StephaneLenclud@141
  2002
            //First clean our current panel
StephaneLenclud@175
  2003
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  2004
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  2005
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@175
  2006
            iTableLayoutPanel.RowCount = 0;
StephaneLenclud@175
  2007
            iTableLayoutPanel.ColumnCount = 0;
sl@63
  2008
StephaneLenclud@175
  2009
            //Then recreate our rows...
StephaneLenclud@175
  2010
            while (iTableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  2011
            {
StephaneLenclud@175
  2012
                iTableLayoutPanel.RowCount++;
sl@63
  2013
            }
sl@63
  2014
StephaneLenclud@175
  2015
            // ...and columns 
StephaneLenclud@175
  2016
            while (iTableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  2017
            {
StephaneLenclud@175
  2018
                iTableLayoutPanel.ColumnCount++;
sl@63
  2019
            }
sl@63
  2020
StephaneLenclud@175
  2021
            //For each column
StephaneLenclud@175
  2022
            for (int i = 0; i < iTableLayoutPanel.ColumnCount; i++)
sl@63
  2023
            {
sl@63
  2024
                //Create our column styles
StephaneLenclud@175
  2025
                this.iTableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  2026
StephaneLenclud@175
  2027
                //For each rows
StephaneLenclud@175
  2028
                for (int j = 0; j < iTableLayoutPanel.RowCount; j++)
sl@63
  2029
                {
sl@63
  2030
                    if (i == 0)
sl@63
  2031
                    {
sl@63
  2032
                        //Create our row styles
StephaneLenclud@175
  2033
                        this.iTableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  2034
                    }
StephaneLenclud@176
  2035
                    else
sl@70
  2036
                    {
sl@70
  2037
                        continue;
sl@70
  2038
                    }
sl@63
  2039
                }
sl@63
  2040
            }
sl@63
  2041
StephaneLenclud@176
  2042
            //For each field
StephaneLenclud@176
  2043
            foreach (DataField field in aClient.Fields)
sl@70
  2044
            {
StephaneLenclud@176
  2045
                if (!field.IsTableField)
StephaneLenclud@176
  2046
                {
StephaneLenclud@176
  2047
                    //That field is not taking part in our table layout skip it
StephaneLenclud@176
  2048
                    continue;
StephaneLenclud@176
  2049
                }
StephaneLenclud@176
  2050
StephaneLenclud@176
  2051
                TableField tableField = (TableField)field;
StephaneLenclud@176
  2052
StephaneLenclud@176
  2053
                //Create a control corresponding to the field specified for that cell
StephaneLenclud@176
  2054
                Control control = CreateControlForDataField(tableField);
StephaneLenclud@176
  2055
StephaneLenclud@176
  2056
                //Add newly created control to our table layout at the specified row and column
StephaneLenclud@176
  2057
                iTableLayoutPanel.Controls.Add(control, tableField.Column, tableField.Row);
StephaneLenclud@176
  2058
                //Make sure we specify column and row span for that new control
StephaneLenclud@176
  2059
                iTableLayoutPanel.SetColumnSpan(control, tableField.ColumnSpan);
StephaneLenclud@176
  2060
                iTableLayoutPanel.SetRowSpan(control, tableField.RowSpan);
sl@70
  2061
            }
sl@70
  2062
StephaneLenclud@176
  2063
sl@63
  2064
            CheckFontHeight();
sl@63
  2065
        }
sl@63
  2066
sl@68
  2067
        /// <summary>
sl@70
  2068
        /// Check our type of data field and create corresponding control
sl@68
  2069
        /// </summary>
sl@68
  2070
        /// <param name="aField"></param>
sl@69
  2071
        private Control CreateControlForDataField(DataField aField)
sl@68
  2072
        {
sl@68
  2073
            Control control=null;
StephaneLenclud@172
  2074
            if (aField.IsTextField)
sl@68
  2075
            {
sl@68
  2076
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  2077
                label.AutoEllipsis = true;
sl@68
  2078
                label.AutoSize = true;
sl@68
  2079
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  2080
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2081
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  2082
                label.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@176
  2083
                label.Name = "marqueeLabel" + aField;
sl@68
  2084
                label.OwnTimer = false;
StephaneLenclud@106
  2085
                label.PixelsPerSecond = cds.ScrollingSpeedInPixelsPerSecond;
sl@100
  2086
                label.Separator = cds.Separator;
sl@100
  2087
                label.MinFontSize = cds.MinFontSize;
sl@100
  2088
                label.ScaleToFit = cds.ScaleToFit;
sl@68
  2089
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  2090
                //control.TabIndex = 2;
sl@68
  2091
                label.Font = cds.Font;
sl@68
  2092
StephaneLenclud@172
  2093
                TextField field = (TextField)aField;
StephaneLenclud@172
  2094
                label.TextAlign = field.Alignment;
sl@68
  2095
                label.UseCompatibleTextRendering = true;
StephaneLenclud@172
  2096
                label.Text = field.Text;
sl@68
  2097
                //
sl@68
  2098
                control = label;
sl@68
  2099
            }
StephaneLenclud@172
  2100
            else if (aField.IsBitmapField)
sl@68
  2101
            {
sl@68
  2102
                //Create picture box
sl@68
  2103
                PictureBox picture = new PictureBox();
sl@68
  2104
                picture.AutoSize = true;
sl@68
  2105
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  2106
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2107
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  2108
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  2109
                picture.Name = "pictureBox" + aField;
sl@68
  2110
                //Set our image
StephaneLenclud@172
  2111
                BitmapField field = (BitmapField)aField;
StephaneLenclud@172
  2112
                picture.Image = field.Bitmap;
sl@68
  2113
                //
sl@68
  2114
                control = picture;
sl@68
  2115
            }
StephaneLenclud@172
  2116
            //TODO: Handle recording field?
sl@68
  2117
sl@69
  2118
            return control;
sl@68
  2119
        }
sl@68
  2120
StephaneLenclud@103
  2121
		/// <summary>
StephaneLenclud@103
  2122
		/// Called when the user selected a new display type.
StephaneLenclud@103
  2123
		/// </summary>
StephaneLenclud@103
  2124
		/// <param name="sender"></param>
StephaneLenclud@103
  2125
		/// <param name="e"></param>
sl@46
  2126
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  2127
        {
StephaneLenclud@103
  2128
			//Store the selected display type in our settings
sl@48
  2129
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  2130
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  2131
            Properties.Settings.Default.Save();
StephaneLenclud@103
  2132
StephaneLenclud@103
  2133
			//Try re-opening the display connection if we were already connected.
StephaneLenclud@103
  2134
			//Otherwise just update our status to reflect display type change.
sl@51
  2135
            if (iDisplay.IsOpen())
sl@51
  2136
            {
sl@51
  2137
                OpenDisplayConnection();
sl@51
  2138
            }
sl@51
  2139
            else
sl@51
  2140
            {
sl@51
  2141
                UpdateStatus();
sl@51
  2142
            }
sl@46
  2143
        }
sl@46
  2144
sl@47
  2145
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  2146
        {
sl@47
  2147
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  2148
            {
sl@51
  2149
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  2150
sl@51
  2151
                if (interval > 0)
sl@51
  2152
                {
sl@51
  2153
                    timer.Interval = interval;
sl@51
  2154
                    cds.TimerInterval = timer.Interval;
sl@51
  2155
                    Properties.Settings.Default.Save();
sl@51
  2156
                }
sl@47
  2157
            }
sl@47
  2158
        }
sl@47
  2159
sl@100
  2160
        private void maskedTextBoxMinFontSize_TextChanged(object sender, EventArgs e)
sl@100
  2161
        {
sl@100
  2162
            if (maskedTextBoxMinFontSize.Text != "")
sl@100
  2163
            {
sl@100
  2164
                int minFontSize = Convert.ToInt32(maskedTextBoxMinFontSize.Text);
sl@100
  2165
sl@100
  2166
                if (minFontSize > 0)
sl@100
  2167
                {
sl@100
  2168
                    cds.MinFontSize = minFontSize;
sl@100
  2169
                    Properties.Settings.Default.Save();
StephaneLenclud@106
  2170
					//We need to recreate our layout for that change to take effect
StephaneLenclud@106
  2171
					UpdateTableLayoutPanel(iCurrentClientData);
sl@100
  2172
                }
sl@100
  2173
            }
sl@100
  2174
        }
sl@100
  2175
StephaneLenclud@106
  2176
StephaneLenclud@106
  2177
		private void maskedTextBoxScrollingSpeed_TextChanged(object sender, EventArgs e)
StephaneLenclud@106
  2178
		{
StephaneLenclud@106
  2179
			if (maskedTextBoxScrollingSpeed.Text != "")
StephaneLenclud@106
  2180
			{
StephaneLenclud@106
  2181
				int scrollingSpeed = Convert.ToInt32(maskedTextBoxScrollingSpeed.Text);
StephaneLenclud@106
  2182
StephaneLenclud@106
  2183
				if (scrollingSpeed > 0)
StephaneLenclud@106
  2184
				{
StephaneLenclud@106
  2185
					cds.ScrollingSpeedInPixelsPerSecond = scrollingSpeed;
StephaneLenclud@106
  2186
					Properties.Settings.Default.Save();
StephaneLenclud@106
  2187
					//We need to recreate our layout for that change to take effect
StephaneLenclud@106
  2188
					UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@106
  2189
				}
StephaneLenclud@106
  2190
			}
StephaneLenclud@106
  2191
		}
StephaneLenclud@106
  2192
sl@100
  2193
        private void textBoxScrollLoopSeparator_TextChanged(object sender, EventArgs e)
sl@100
  2194
        {
sl@100
  2195
            cds.Separator = textBoxScrollLoopSeparator.Text;
sl@100
  2196
            Properties.Settings.Default.Save();
StephaneLenclud@110
  2197
StephaneLenclud@110
  2198
			//Update our text fields
StephaneLenclud@175
  2199
			foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
StephaneLenclud@110
  2200
			{
StephaneLenclud@110
  2201
				ctrl.Separator = cds.Separator;
StephaneLenclud@110
  2202
			}
StephaneLenclud@110
  2203
sl@100
  2204
        }
sl@100
  2205
sl@52
  2206
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  2207
        {
sl@52
  2208
            iDisplay.PowerOn();
sl@52
  2209
        }
sl@52
  2210
sl@52
  2211
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  2212
        {
sl@52
  2213
            iDisplay.PowerOff();
sl@52
  2214
        }
sl@52
  2215
sl@53
  2216
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  2217
        {
StephaneLenclud@122
  2218
			ShowClock();
sl@53
  2219
        }
sl@53
  2220
sl@53
  2221
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  2222
        {
StephaneLenclud@122
  2223
			HideClock();
sl@53
  2224
        }
sl@88
  2225
sl@88
  2226
        private void buttonUpdate_Click(object sender, EventArgs e)
sl@88
  2227
        {
sl@88
  2228
            InstallUpdateSyncWithInfo();
sl@88
  2229
        }
sl@88
  2230
StephaneLenclud@122
  2231
		/// <summary>
StephaneLenclud@122
  2232
		/// 
StephaneLenclud@122
  2233
		/// </summary>
StephaneLenclud@122
  2234
		void ShowClock()
StephaneLenclud@122
  2235
		{
StephaneLenclud@122
  2236
			if (!iDisplay.IsOpen())
StephaneLenclud@122
  2237
			{
StephaneLenclud@122
  2238
				return;
StephaneLenclud@122
  2239
			}
StephaneLenclud@122
  2240
StephaneLenclud@122
  2241
			//Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@122
  2242
			iSkipFrameRendering = true;
StephaneLenclud@122
  2243
			//Clear our screen 
StephaneLenclud@122
  2244
			iDisplay.Clear();
StephaneLenclud@122
  2245
			iDisplay.SwapBuffers();
StephaneLenclud@122
  2246
			//Then show our clock
StephaneLenclud@122
  2247
			iDisplay.ShowClock();
StephaneLenclud@122
  2248
		}
StephaneLenclud@122
  2249
StephaneLenclud@122
  2250
		/// <summary>
StephaneLenclud@122
  2251
		/// 
StephaneLenclud@122
  2252
		/// </summary>
StephaneLenclud@122
  2253
		void HideClock()
StephaneLenclud@122
  2254
		{
StephaneLenclud@122
  2255
			if (!iDisplay.IsOpen())
StephaneLenclud@122
  2256
			{
StephaneLenclud@122
  2257
				return;
StephaneLenclud@122
  2258
			}
StephaneLenclud@122
  2259
StephaneLenclud@122
  2260
			//Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@122
  2261
			iSkipFrameRendering = false;
StephaneLenclud@122
  2262
			iDisplay.HideClock();
StephaneLenclud@122
  2263
		}
sl@88
  2264
sl@88
  2265
        private void InstallUpdateSyncWithInfo()
sl@88
  2266
        {
sl@88
  2267
            UpdateCheckInfo info = null;
sl@88
  2268
sl@88
  2269
            if (ApplicationDeployment.IsNetworkDeployed)
sl@88
  2270
            {
sl@88
  2271
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
sl@88
  2272
sl@88
  2273
                try
sl@88
  2274
                {
sl@88
  2275
                    info = ad.CheckForDetailedUpdate();
sl@88
  2276
sl@88
  2277
                }
sl@88
  2278
                catch (DeploymentDownloadException dde)
sl@88
  2279
                {
sl@88
  2280
                    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
  2281
                    return;
sl@88
  2282
                }
sl@88
  2283
                catch (InvalidDeploymentException ide)
sl@88
  2284
                {
sl@88
  2285
                    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
  2286
                    return;
sl@88
  2287
                }
sl@88
  2288
                catch (InvalidOperationException ioe)
sl@88
  2289
                {
sl@88
  2290
                    MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message);
sl@88
  2291
                    return;
sl@88
  2292
                }
sl@88
  2293
sl@90
  2294
				if (info.UpdateAvailable)
sl@90
  2295
				{
sl@90
  2296
					Boolean doUpdate = true;
sl@88
  2297
sl@90
  2298
					if (!info.IsUpdateRequired)
sl@90
  2299
					{
sl@90
  2300
						DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel);
sl@90
  2301
						if (!(DialogResult.OK == dr))
sl@90
  2302
						{
sl@90
  2303
							doUpdate = false;
sl@90
  2304
						}
sl@90
  2305
					}
sl@90
  2306
					else
sl@90
  2307
					{
sl@90
  2308
						// Display a message that the app MUST reboot. Display the minimum required version.
sl@90
  2309
						MessageBox.Show("This application has detected a mandatory update from your current " +
sl@90
  2310
							"version to version " + info.MinimumRequiredVersion.ToString() +
sl@90
  2311
							". The application will now install the update and restart.",
sl@90
  2312
							"Update Available", MessageBoxButtons.OK,
sl@90
  2313
							MessageBoxIcon.Information);
sl@90
  2314
					}
sl@88
  2315
sl@90
  2316
					if (doUpdate)
sl@90
  2317
					{
sl@90
  2318
						try
sl@90
  2319
						{
sl@90
  2320
							ad.Update();
sl@90
  2321
							MessageBox.Show("The application has been upgraded, and will now restart.");
sl@90
  2322
							Application.Restart();
sl@90
  2323
						}
sl@90
  2324
						catch (DeploymentDownloadException dde)
sl@90
  2325
						{
sl@90
  2326
							MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
sl@90
  2327
							return;
sl@90
  2328
						}
sl@90
  2329
					}
sl@90
  2330
				}
sl@90
  2331
				else
sl@90
  2332
				{
sl@90
  2333
					MessageBox.Show("You are already running the latest version.", "Application up-to-date");
sl@90
  2334
				}
sl@88
  2335
            }
sl@88
  2336
        }
sl@92
  2337
sl@94
  2338
sl@94
  2339
		/// <summary>
sl@99
  2340
		/// Used to
sl@94
  2341
		/// </summary>
sl@94
  2342
		private void SysTrayHideShow()
sl@92
  2343
		{
sl@94
  2344
			Visible = !Visible;
sl@94
  2345
			if (Visible)
sl@94
  2346
			{
sl@94
  2347
				Activate();
sl@94
  2348
				WindowState = FormWindowState.Normal;
sl@94
  2349
			}
sl@92
  2350
		}
sl@94
  2351
sl@94
  2352
		/// <summary>
sl@94
  2353
		/// Use to handle minimize events.
sl@94
  2354
		/// </summary>
sl@94
  2355
		/// <param name="sender"></param>
sl@94
  2356
		/// <param name="e"></param>
sl@94
  2357
		private void MainForm_SizeChanged(object sender, EventArgs e)
sl@94
  2358
		{
sl@94
  2359
			if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
sl@94
  2360
			{
sl@94
  2361
				if (Visible)
sl@94
  2362
				{
sl@94
  2363
					SysTrayHideShow();
sl@94
  2364
				}
sl@94
  2365
			}
sl@94
  2366
		}
sl@94
  2367
StephaneLenclud@105
  2368
		/// <summary>
StephaneLenclud@105
  2369
		/// 
StephaneLenclud@105
  2370
		/// </summary>
StephaneLenclud@105
  2371
		/// <param name="sender"></param>
StephaneLenclud@105
  2372
		/// <param name="e"></param>
StephaneLenclud@105
  2373
		private void tableLayoutPanel_SizeChanged(object sender, EventArgs e)
StephaneLenclud@105
  2374
		{
StephaneLenclud@105
  2375
			//Our table layout size has changed which means our display size has changed.
StephaneLenclud@105
  2376
			//We need to re-create our bitmap.
StephaneLenclud@105
  2377
			iCreateBitmap = true;
StephaneLenclud@105
  2378
		}
StephaneLenclud@126
  2379
StephaneLenclud@126
  2380
		/// <summary>
StephaneLenclud@126
  2381
		/// 
StephaneLenclud@126
  2382
		/// </summary>
StephaneLenclud@126
  2383
		/// <param name="sender"></param>
StephaneLenclud@126
  2384
		/// <param name="e"></param>
StephaneLenclud@126
  2385
		private void buttonSelectFile_Click(object sender, EventArgs e)
StephaneLenclud@126
  2386
		{
StephaneLenclud@126
  2387
			//openFileDialog1.InitialDirectory = "c:\\";
StephaneLenclud@126
  2388
			//openFileDialog.Filter = "EXE files (*.exe)|*.exe|All files (*.*)|*.*";
StephaneLenclud@126
  2389
			//openFileDialog.FilterIndex = 1;
StephaneLenclud@126
  2390
			openFileDialog.RestoreDirectory = true;
StephaneLenclud@126
  2391
StephaneLenclud@126
  2392
			if (DlgBox.ShowDialog(openFileDialog) == DialogResult.OK)
StephaneLenclud@126
  2393
			{
StephaneLenclud@126
  2394
				labelStartFileName.Text = openFileDialog.FileName;
StephaneLenclud@126
  2395
				Properties.Settings.Default.StartFileName = openFileDialog.FileName;
StephaneLenclud@126
  2396
				Properties.Settings.Default.Save();
StephaneLenclud@126
  2397
			}
StephaneLenclud@126
  2398
		}
StephaneLenclud@153
  2399
StephaneLenclud@153
  2400
        /// <summary>
StephaneLenclud@153
  2401
        /// 
StephaneLenclud@153
  2402
        /// </summary>
StephaneLenclud@153
  2403
        /// <param name="sender"></param>
StephaneLenclud@153
  2404
        /// <param name="e"></param>
StephaneLenclud@153
  2405
        private void comboBoxOpticalDrives_SelectedIndexChanged(object sender, EventArgs e)
StephaneLenclud@153
  2406
        {
StephaneLenclud@153
  2407
            //Save the optical drive the user selected for ejection
StephaneLenclud@153
  2408
            Properties.Settings.Default.OpticalDriveToEject = comboBoxOpticalDrives.SelectedItem.ToString();
StephaneLenclud@153
  2409
            Properties.Settings.Default.Save();
StephaneLenclud@153
  2410
        }
StephaneLenclud@167
  2411
StephaneLenclud@167
  2412
        /// <summary>
StephaneLenclud@167
  2413
        /// Broadcast messages to subscribers.
StephaneLenclud@167
  2414
        /// </summary>
StephaneLenclud@167
  2415
        /// <param name="message"></param>
StephaneLenclud@167
  2416
        protected override void WndProc(ref Message aMessage)
StephaneLenclud@167
  2417
        {
StephaneLenclud@167
  2418
            if (OnWndProc!=null)
StephaneLenclud@167
  2419
            {
StephaneLenclud@167
  2420
                OnWndProc(ref aMessage);
StephaneLenclud@167
  2421
            }
StephaneLenclud@167
  2422
            
StephaneLenclud@167
  2423
            base.WndProc(ref aMessage);
StephaneLenclud@167
  2424
        }
StephaneLenclud@168
  2425
StephaneLenclud@168
  2426
        private void checkBoxCecEnabled_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2427
        {
StephaneLenclud@168
  2428
            //Save CEC enabled status
StephaneLenclud@168
  2429
            Properties.Settings.Default.CecEnabled = checkBoxCecEnabled.Checked;
StephaneLenclud@168
  2430
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2431
            //
StephaneLenclud@168
  2432
            ResetCec();
StephaneLenclud@168
  2433
        }
StephaneLenclud@168
  2434
StephaneLenclud@168
  2435
        private void comboBoxHdmiPort_SelectedIndexChanged(object sender, EventArgs e)
StephaneLenclud@168
  2436
        {
StephaneLenclud@168
  2437
            //Save CEC HDMI port
StephaneLenclud@168
  2438
            Properties.Settings.Default.CecHdmiPort = Convert.ToByte(comboBoxHdmiPort.SelectedIndex);
StephaneLenclud@168
  2439
            Properties.Settings.Default.CecHdmiPort++;
StephaneLenclud@168
  2440
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2441
            //
StephaneLenclud@168
  2442
            ResetCec();
StephaneLenclud@168
  2443
        }
StephaneLenclud@168
  2444
StephaneLenclud@168
  2445
        private void checkBoxCecMonitorOff_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2446
        {
StephaneLenclud@168
  2447
            Properties.Settings.Default.CecMonitorOff = checkBoxCecMonitorOff.Checked;
StephaneLenclud@168
  2448
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2449
            //
StephaneLenclud@168
  2450
            ResetCec();
StephaneLenclud@168
  2451
        }
StephaneLenclud@168
  2452
StephaneLenclud@168
  2453
        private void checkBoxCecMonitorOn_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2454
        {
StephaneLenclud@168
  2455
            Properties.Settings.Default.CecMonitorOn = checkBoxCecMonitorOn.Checked;
StephaneLenclud@168
  2456
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2457
            //
StephaneLenclud@168
  2458
            ResetCec();
StephaneLenclud@168
  2459
        }
StephaneLenclud@168
  2460
StephaneLenclud@168
  2461
        /// <summary>
StephaneLenclud@168
  2462
        /// 
StephaneLenclud@168
  2463
        /// </summary>
StephaneLenclud@168
  2464
        private void ResetCec()
StephaneLenclud@168
  2465
        {
StephaneLenclud@168
  2466
            if (iCecManager==null)
StephaneLenclud@168
  2467
            {
StephaneLenclud@168
  2468
                //Thus skipping initial UI setup
StephaneLenclud@168
  2469
                return;
StephaneLenclud@168
  2470
            }
StephaneLenclud@168
  2471
StephaneLenclud@168
  2472
            iCecManager.Stop();
StephaneLenclud@168
  2473
            //
StephaneLenclud@168
  2474
            if (Properties.Settings.Default.CecEnabled)
StephaneLenclud@168
  2475
            {
StephaneLenclud@168
  2476
                iCecManager.Start(Handle, "CEC",
StephaneLenclud@168
  2477
                Properties.Settings.Default.CecHdmiPort,
StephaneLenclud@168
  2478
                Properties.Settings.Default.CecMonitorOn,
StephaneLenclud@168
  2479
                Properties.Settings.Default.CecMonitorOff);
StephaneLenclud@168
  2480
            }
StephaneLenclud@168
  2481
        }
sl@0
  2482
    }
sl@0
  2483
}