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