Server/MainForm.cs
author StephaneLenclud
Sat, 26 Dec 2015 23:27:55 +0100
changeset 176 01c72c29cfaf
parent 175 391bce3c8368
child 177 c24dc0c6b64b
permissions -rw-r--r--
Upgrade to SharpLibDisplay 0.1.3.
Much improved layout construction.
Not relying on field and control index anymore.
StephaneLenclud@123
     1
´╗┐//
StephaneLenclud@123
     2
// Copyright (C) 2014-2015 St├ęphane Lenclud.
StephaneLenclud@123
     3
//
StephaneLenclud@123
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@123
     5
//
StephaneLenclud@123
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@123
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123
     9
// (at your option) any later version.
StephaneLenclud@123
    10
//
StephaneLenclud@123
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@123
    14
// GNU General Public License for more details.
StephaneLenclud@123
    15
//
StephaneLenclud@123
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@123
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@123
    18
//
StephaneLenclud@123
    19
StephaneLenclud@123
    20
using System;
sl@0
    21
using System.Collections.Generic;
sl@0
    22
using System.ComponentModel;
sl@0
    23
using System.Data;
sl@0
    24
using System.Drawing;
sl@0
    25
using System.Linq;
sl@0
    26
using System.Text;
sl@0
    27
using System.Threading.Tasks;
sl@0
    28
using System.Windows.Forms;
sl@14
    29
using System.IO;
sl@0
    30
using CodeProject.Dialog;
sl@14
    31
using System.Drawing.Imaging;
sl@17
    32
using System.ServiceModel;
sl@25
    33
using System.Threading;
sl@31
    34
using System.Diagnostics;
sl@88
    35
using System.Deployment.Application;
sl@94
    36
using System.Reflection;
StephaneLenclud@112
    37
//NAudio
StephaneLenclud@112
    38
using NAudio.CoreAudioApi;
StephaneLenclud@112
    39
using NAudio.CoreAudioApi.Interfaces;
StephaneLenclud@112
    40
using System.Runtime.InteropServices;
StephaneLenclud@117
    41
//Network
StephaneLenclud@117
    42
using NETWORKLIST;
sl@25
    43
//
sl@25
    44
using SharpDisplayClient;
sl@55
    45
using SharpDisplay;
StephaneLenclud@135
    46
using MiniDisplayInterop;
StephaneLenclud@171
    47
using SharpLib.Display;
StephaneLenclud@124
    48
sl@0
    49
namespace SharpDisplayManager
sl@0
    50
{
sl@58
    51
    //Types declarations
sl@58
    52
    public delegate uint ColorProcessingDelegate(int aX, int aY, uint aPixel);
sl@58
    53
    public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
sl@62
    54
    //Delegates are used for our thread safe method
sl@62
    55
    public delegate void AddClientDelegate(string aSessionId, ICallback aCallback);
sl@62
    56
    public delegate void RemoveClientDelegate(string aSessionId);
sl@79
    57
    public delegate void SetFieldDelegate(string SessionId, DataField aField);
sl@79
    58
    public delegate void SetFieldsDelegate(string SessionId, System.Collections.Generic.IList<DataField> aFields);
sl@62
    59
    public delegate void SetLayoutDelegate(string SessionId, TableLayout aLayout);
sl@62
    60
    public delegate void SetClientNameDelegate(string aSessionId, string aName);
StephaneLenclud@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>
StephaneLenclud@173
   110
		private SharpLib.Notification.Control 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();
StephaneLenclud@173
   131
			iNotifyIcon = new SharpLib.Notification.Control();
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
StephaneLenclud@175
   147
            iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.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
StephaneLenclud@175
   671
                foreach (Control ctrl in iTableLayoutPanel.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
StephaneLenclud@175
   707
            foreach (Control ctrl in iTableLayoutPanel.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
        {
StephaneLenclud@175
   731
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height);
StephaneLenclud@175
   732
            iTableLayoutPanel.DrawToBitmap(bmp, iTableLayoutPanel.ClientRectangle);
sl@14
   733
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
   734
            bmp.Save("D:\\capture.png");
sl@14
   735
StephaneLenclud@175
   736
            ((MarqueeLabel)iTableLayoutPanel.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
StephaneLenclud@175
   879
            foreach (Control ctrl in iTableLayoutPanel.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@175
   899
                        iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height, PixelFormat.Format32bppArgb);
StephaneLenclud@158
   900
                        iCreateBitmap = false;
StephaneLenclud@158
   901
                    }
StephaneLenclud@175
   902
					iTableLayoutPanel.DrawToBitmap(iBmp, iTableLayoutPanel.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;
StephaneLenclud@175
  1060
            iTableLayoutPanel.CellBorderStyle = (cds.ShowBorders ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@60
  1061
sl@60
  1062
            //Set the proper font to each of our labels
StephaneLenclud@175
  1063
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.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@175
  1126
				iTableLayoutPanel.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@175
  1198
				iTableLayoutPanel.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
StephaneLenclud@175
  1231
            iTableLayoutPanel.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
		{
StephaneLenclud@175
  1385
			iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  1386
			iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  1387
			iTableLayoutPanel.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@175
  1532
                        //So that next time we update all our fields at ones
StephaneLenclud@175
  1533
                        client.HasNewLayout = true;
StephaneLenclud@158
  1534
                        //Layout has changed clear our fields then
StephaneLenclud@158
  1535
                        client.Fields.Clear();
StephaneLenclud@148
  1536
                        //
StephaneLenclud@148
  1537
                        UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1538
                    }
StephaneLenclud@158
  1539
                    else
StephaneLenclud@158
  1540
                    {
StephaneLenclud@158
  1541
                        Debug.Print("SetClientLayoutThreadSafe: Layout has not changed.");
StephaneLenclud@158
  1542
                    }
sl@62
  1543
                }
sl@62
  1544
            }
sl@62
  1545
        }
sl@62
  1546
sl@62
  1547
        /// <summary>
sl@62
  1548
        ///
sl@62
  1549
        /// </summary>
sl@67
  1550
        /// <param name="aSessionId"></param>
sl@72
  1551
        /// <param name="aField"></param>
sl@75
  1552
        public void SetClientFieldThreadSafe(string aSessionId, DataField aField)
sl@30
  1553
        {
sl@33
  1554
            if (this.InvokeRequired)
sl@30
  1555
            {
sl@30
  1556
                //Not in the proper thread, invoke ourselves
sl@79
  1557
                SetFieldDelegate d = new SetFieldDelegate(SetClientFieldThreadSafe);
sl@72
  1558
                this.Invoke(d, new object[] { aSessionId, aField });
sl@30
  1559
            }
sl@30
  1560
            else
sl@30
  1561
            {
sl@75
  1562
                //We are in the proper thread
sl@75
  1563
                //Call the non-thread-safe variant
sl@75
  1564
                SetClientField(aSessionId, aField);
sl@75
  1565
            }
sl@75
  1566
        }
sl@75
  1567
StephaneLenclud@176
  1568
StephaneLenclud@176
  1569
StephaneLenclud@176
  1570
sl@75
  1571
        /// <summary>
StephaneLenclud@175
  1572
        /// Set a data field in the given client.
sl@75
  1573
        /// </summary>
sl@75
  1574
        /// <param name="aSessionId"></param>
sl@75
  1575
        /// <param name="aField"></param>
sl@75
  1576
        private void SetClientField(string aSessionId, DataField aField)
StephaneLenclud@141
  1577
        {   
StephaneLenclud@141
  1578
            //TODO: should check if the field actually changed?
StephaneLenclud@141
  1579
sl@75
  1580
            ClientData client = iClients[aSessionId];
StephaneLenclud@141
  1581
            bool layoutChanged = false;
StephaneLenclud@148
  1582
            bool contentChanged = true;
StephaneLenclud@141
  1583
StephaneLenclud@176
  1584
            //Fetch our field index
StephaneLenclud@176
  1585
            int fieldIndex = client.FindSameFieldIndex(aField);
StephaneLenclud@176
  1586
StephaneLenclud@176
  1587
            if (fieldIndex < 0)
sl@75
  1588
            {
StephaneLenclud@176
  1589
                //No corresponding field, just bail out
StephaneLenclud@176
  1590
                return;
StephaneLenclud@141
  1591
            }
sl@76
  1592
StephaneLenclud@175
  1593
            //Keep our previous field in there
StephaneLenclud@176
  1594
            DataField previousField = client.Fields[fieldIndex];
StephaneLenclud@176
  1595
            //Just update that field then 
StephaneLenclud@176
  1596
            client.Fields[fieldIndex] = aField;
StephaneLenclud@148
  1597
StephaneLenclud@176
  1598
            if (!aField.IsTableField)
StephaneLenclud@176
  1599
            {
StephaneLenclud@176
  1600
                //We are done then if that field is not in our table layout
StephaneLenclud@176
  1601
                return;
StephaneLenclud@176
  1602
            }
StephaneLenclud@176
  1603
StephaneLenclud@176
  1604
            TableField tableField = (TableField) aField;
StephaneLenclud@172
  1605
StephaneLenclud@175
  1606
            if (previousField.IsSameLayout(aField))
StephaneLenclud@141
  1607
            {
StephaneLenclud@141
  1608
                //If we are updating a field in our current client we need to update it in our panel
StephaneLenclud@141
  1609
                if (aSessionId == iCurrentClientSessionId)
sl@30
  1610
                {
StephaneLenclud@176
  1611
                    Control ctrl=iTableLayoutPanel.GetControlFromPosition(tableField.Column, tableField.Row);
StephaneLenclud@176
  1612
                    if (aField.IsTextField && ctrl is MarqueeLabel)
sl@79
  1613
                    {
StephaneLenclud@172
  1614
                        TextField textField=(TextField)aField;
sl@75
  1615
                        //Text field control already in place, just change the text
StephaneLenclud@176
  1616
                        MarqueeLabel label = (MarqueeLabel)ctrl;
StephaneLenclud@172
  1617
                        contentChanged = (label.Text != textField.Text || label.TextAlign != textField.Alignment);
StephaneLenclud@172
  1618
                        label.Text = textField.Text;
StephaneLenclud@172
  1619
                        label.TextAlign = textField.Alignment;
sl@68
  1620
                    }
StephaneLenclud@176
  1621
                    else if (aField.IsBitmapField && ctrl is PictureBox)
sl@75
  1622
                    {
StephaneLenclud@172
  1623
                        BitmapField bitmapField = (BitmapField)aField;
StephaneLenclud@148
  1624
                        contentChanged = true; //TODO: Bitmap comp or should we leave that to clients?
sl@75
  1625
                        //Bitmap field control already in place just change the bitmap
StephaneLenclud@176
  1626
                        PictureBox pictureBox = (PictureBox)ctrl;
StephaneLenclud@172
  1627
                        pictureBox.Image = bitmapField.Bitmap;
sl@75
  1628
                    }
sl@68
  1629
                    else
sl@68
  1630
                    {
StephaneLenclud@141
  1631
                        layoutChanged = true;
sl@68
  1632
                    }
sl@30
  1633
                }
StephaneLenclud@141
  1634
            }
StephaneLenclud@141
  1635
            else
StephaneLenclud@148
  1636
            {                
StephaneLenclud@141
  1637
                layoutChanged = true;
StephaneLenclud@141
  1638
            }
StephaneLenclud@141
  1639
StephaneLenclud@148
  1640
            //If either content or layout changed we need to update our tree view to reflect the changes
StephaneLenclud@148
  1641
            if (contentChanged || layoutChanged)
StephaneLenclud@141
  1642
            {
StephaneLenclud@141
  1643
                UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1644
                //
StephaneLenclud@148
  1645
                if (layoutChanged)
sl@75
  1646
                {
StephaneLenclud@148
  1647
                    Debug.Print("Layout changed");
StephaneLenclud@148
  1648
                    //Our layout has changed, if we are already the current client we need to update our panel
StephaneLenclud@148
  1649
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@148
  1650
                    {
StephaneLenclud@148
  1651
                        //Apply layout and set data fields.
StephaneLenclud@148
  1652
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@148
  1653
                    }
sl@75
  1654
                }
StephaneLenclud@158
  1655
                else
StephaneLenclud@158
  1656
                {
StephaneLenclud@158
  1657
                    Debug.Print("Layout has not changed.");
StephaneLenclud@158
  1658
                }
StephaneLenclud@158
  1659
            }
StephaneLenclud@158
  1660
            else
StephaneLenclud@158
  1661
            {
StephaneLenclud@158
  1662
                Debug.Print("WARNING: content and layout have not changed!");
StephaneLenclud@141
  1663
            }
sl@75
  1664
StephaneLenclud@141
  1665
            //When a client field is set we try switching to this client to present the new information to our user
StephaneLenclud@141
  1666
            SetCurrentClient(aSessionId);
sl@30
  1667
        }
sl@30
  1668
sl@30
  1669
        /// <summary>
sl@36
  1670
        ///
sl@30
  1671
        /// </summary>
sl@30
  1672
        /// <param name="aTexts"></param>
sl@75
  1673
        public void SetClientFieldsThreadSafe(string aSessionId, System.Collections.Generic.IList<DataField> aFields)
sl@30
  1674
        {
sl@33
  1675
            if (this.InvokeRequired)
sl@30
  1676
            {
sl@30
  1677
                //Not in the proper thread, invoke ourselves
sl@75
  1678
                SetFieldsDelegate d = new SetFieldsDelegate(SetClientFieldsThreadSafe);
sl@72
  1679
                this.Invoke(d, new object[] { aSessionId, aFields });
sl@30
  1680
            }
sl@30
  1681
            else
sl@30
  1682
            {
StephaneLenclud@175
  1683
                ClientData client = iClients[aSessionId];
StephaneLenclud@175
  1684
StephaneLenclud@175
  1685
                if (client.HasNewLayout)
sl@30
  1686
                {
StephaneLenclud@175
  1687
                    //TODO: Assert client.Count == 0
StephaneLenclud@175
  1688
                    //Our layout was just changed
StephaneLenclud@175
  1689
                    //Do some special handling to avoid re-creating our panel N times, once for each fields
StephaneLenclud@175
  1690
                    client.HasNewLayout = false;
StephaneLenclud@175
  1691
                    //Just set all our fields then
StephaneLenclud@175
  1692
                    client.Fields.AddRange(aFields);
StephaneLenclud@175
  1693
                    //Try switch to that client
StephaneLenclud@175
  1694
                    SetCurrentClient(aSessionId);
StephaneLenclud@175
  1695
StephaneLenclud@175
  1696
                    //If we are updating the current client update our panel
StephaneLenclud@175
  1697
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@175
  1698
                    {
StephaneLenclud@175
  1699
                        //Apply layout and set data fields.
StephaneLenclud@175
  1700
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@175
  1701
                    }
StephaneLenclud@176
  1702
StephaneLenclud@176
  1703
                    UpdateClientTreeViewNode(client);
StephaneLenclud@175
  1704
                }
StephaneLenclud@175
  1705
                else
StephaneLenclud@175
  1706
                {
StephaneLenclud@175
  1707
                    //Put each our text fields in a label control
StephaneLenclud@175
  1708
                    foreach (DataField field in aFields)
StephaneLenclud@175
  1709
                    {
StephaneLenclud@175
  1710
                        SetClientField(aSessionId, field);
StephaneLenclud@175
  1711
                    }
sl@30
  1712
                }
sl@30
  1713
            }
sl@32
  1714
        }
sl@30
  1715
sl@67
  1716
        /// <summary>
sl@67
  1717
        ///
sl@67
  1718
        /// </summary>
sl@67
  1719
        /// <param name="aSessionId"></param>
sl@32
  1720
        /// <param name="aName"></param>
sl@32
  1721
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
  1722
        {
sl@32
  1723
            if (this.InvokeRequired)
sl@32
  1724
            {
sl@32
  1725
                //Not in the proper thread, invoke ourselves
sl@32
  1726
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
sl@32
  1727
                this.Invoke(d, new object[] { aSessionId, aName });
sl@32
  1728
            }
sl@32
  1729
            else
sl@32
  1730
            {
sl@32
  1731
                //We are in the proper thread
sl@33
  1732
                //Get our client
sl@33
  1733
                ClientData client = iClients[aSessionId];
sl@33
  1734
                if (client != null)
sl@32
  1735
                {
sl@33
  1736
                    //Set its name
sl@33
  1737
                    client.Name = aName;
sl@33
  1738
                    //Update our tree-view
sl@33
  1739
                    UpdateClientTreeViewNode(client);
sl@33
  1740
                }
sl@33
  1741
            }
sl@33
  1742
        }
sl@33
  1743
sl@33
  1744
        /// <summary>
sl@36
  1745
        ///
sl@33
  1746
        /// </summary>
sl@33
  1747
        /// <param name="aClient"></param>
sl@33
  1748
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
  1749
        {
StephaneLenclud@148
  1750
            Debug.Print("UpdateClientTreeViewNode");
StephaneLenclud@148
  1751
sl@33
  1752
            if (aClient == null)
sl@33
  1753
            {
sl@33
  1754
                return;
sl@33
  1755
            }
sl@33
  1756
sl@33
  1757
            TreeNode node = null;
sl@33
  1758
            //Check that our client node already exists
sl@33
  1759
            //Get our client root node using its key which is our session ID
sl@33
  1760
            TreeNode[] nodes = treeViewClients.Nodes.Find(aClient.SessionId, false);
sl@33
  1761
            if (nodes.Count()>0)
sl@33
  1762
            {
sl@33
  1763
                //We already have a node for that client
sl@33
  1764
                node = nodes[0];
sl@33
  1765
                //Clear children as we are going to recreate them below
sl@33
  1766
                node.Nodes.Clear();
sl@33
  1767
            }
sl@33
  1768
            else
sl@33
  1769
            {
sl@33
  1770
                //Client node does not exists create a new one
sl@33
  1771
                treeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
sl@33
  1772
                node = treeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
  1773
            }
sl@33
  1774
sl@33
  1775
            if (node != null)
sl@33
  1776
            {
sl@33
  1777
                //Change its name
sl@33
  1778
                if (aClient.Name != "")
sl@33
  1779
                {
sl@33
  1780
                    //We have a name, us it as text for our root node
sl@33
  1781
                    node.Text = aClient.Name;
sl@32
  1782
                    //Add a child with SessionId
sl@33
  1783
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
  1784
                }
sl@33
  1785
                else
sl@33
  1786
                {
sl@33
  1787
                    //No name, use session ID instead
sl@33
  1788
                    node.Text = aClient.SessionId;
sl@33
  1789
                }
sl@36
  1790
sl@67
  1791
                if (aClient.Fields.Count > 0)
sl@33
  1792
                {
sl@33
  1793
                    //Create root node for our texts
sl@70
  1794
                    TreeNode textsRoot = new TreeNode("Fields");
sl@33
  1795
                    node.Nodes.Add(textsRoot);
sl@33
  1796
                    //For each text add a new entry
sl@67
  1797
                    foreach (DataField field in aClient.Fields)
sl@33
  1798
                    {
StephaneLenclud@172
  1799
                        if (field.IsTextField)
sl@67
  1800
                        {
StephaneLenclud@172
  1801
                            TextField textField = (TextField)field;
sl@70
  1802
                            textsRoot.Nodes.Add(new TreeNode("[Text]" + textField.Text));
sl@67
  1803
                        }
StephaneLenclud@172
  1804
                        else if (field.IsBitmapField)
sl@67
  1805
                        {
sl@72
  1806
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap]"));
sl@70
  1807
                        }
StephaneLenclud@172
  1808
                        else if (field.IsRecordingField)
StephaneLenclud@172
  1809
                        {
StephaneLenclud@172
  1810
                            textsRoot.Nodes.Add(new TreeNode("[Recording]"));
StephaneLenclud@172
  1811
                        }
sl@33
  1812
                    }
sl@32
  1813
                }
sl@34
  1814
sl@34
  1815
                node.ExpandAll();
sl@32
  1816
            }
sl@30
  1817
        }
sl@17
  1818
sl@60
  1819
        /// <summary>
sl@60
  1820
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  1821
        /// </summary>
sl@60
  1822
        private void UpdateTableLayoutRowStyles()
sl@60
  1823
        {
StephaneLenclud@175
  1824
            foreach (RowStyle rowStyle in iTableLayoutPanel.RowStyles)
sl@60
  1825
            {
sl@60
  1826
                rowStyle.SizeType = SizeType.Percent;
StephaneLenclud@175
  1827
                rowStyle.Height = 100 / iTableLayoutPanel.RowCount;
sl@60
  1828
            }
sl@60
  1829
        }
sl@60
  1830
sl@63
  1831
        /// <summary>
sl@63
  1832
        /// Update our display table layout.
StephaneLenclud@175
  1833
        /// Will instanciated every field control as defined by our client.
StephaneLenclud@175
  1834
        /// Fields must be specified by rows from the left.
sl@63
  1835
        /// </summary>
sl@63
  1836
        /// <param name="aLayout"></param>
sl@65
  1837
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  1838
        {
StephaneLenclud@175
  1839
            Debug.Print("UpdateTableLayoutPanel");
StephaneLenclud@148
  1840
StephaneLenclud@106
  1841
			if (aClient == null)
StephaneLenclud@106
  1842
			{
StephaneLenclud@106
  1843
				//Just drop it
StephaneLenclud@106
  1844
				return;
StephaneLenclud@106
  1845
			}
StephaneLenclud@106
  1846
StephaneLenclud@106
  1847
sl@65
  1848
            TableLayout layout = aClient.Layout;
sl@70
  1849
StephaneLenclud@141
  1850
            //First clean our current panel
StephaneLenclud@175
  1851
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  1852
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  1853
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@175
  1854
            iTableLayoutPanel.RowCount = 0;
StephaneLenclud@175
  1855
            iTableLayoutPanel.ColumnCount = 0;
sl@63
  1856
StephaneLenclud@175
  1857
            //Then recreate our rows...
StephaneLenclud@175
  1858
            while (iTableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  1859
            {
StephaneLenclud@175
  1860
                iTableLayoutPanel.RowCount++;
sl@63
  1861
            }
sl@63
  1862
StephaneLenclud@175
  1863
            // ...and columns 
StephaneLenclud@175
  1864
            while (iTableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  1865
            {
StephaneLenclud@175
  1866
                iTableLayoutPanel.ColumnCount++;
sl@63
  1867
            }
sl@63
  1868
StephaneLenclud@175
  1869
            //For each column
StephaneLenclud@175
  1870
            for (int i = 0; i < iTableLayoutPanel.ColumnCount; i++)
sl@63
  1871
            {
sl@63
  1872
                //Create our column styles
StephaneLenclud@175
  1873
                this.iTableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  1874
StephaneLenclud@175
  1875
                //For each rows
StephaneLenclud@175
  1876
                for (int j = 0; j < iTableLayoutPanel.RowCount; j++)
sl@63
  1877
                {
sl@63
  1878
                    if (i == 0)
sl@63
  1879
                    {
sl@63
  1880
                        //Create our row styles
StephaneLenclud@175
  1881
                        this.iTableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  1882
                    }
StephaneLenclud@176
  1883
                    else
sl@70
  1884
                    {
sl@70
  1885
                        continue;
sl@70
  1886
                    }
sl@63
  1887
                }
sl@63
  1888
            }
sl@63
  1889
StephaneLenclud@176
  1890
            //For each field
StephaneLenclud@176
  1891
            foreach (DataField field in aClient.Fields)
sl@70
  1892
            {
StephaneLenclud@176
  1893
                if (!field.IsTableField)
StephaneLenclud@176
  1894
                {
StephaneLenclud@176
  1895
                    //That field is not taking part in our table layout skip it
StephaneLenclud@176
  1896
                    continue;
StephaneLenclud@176
  1897
                }
StephaneLenclud@176
  1898
StephaneLenclud@176
  1899
                TableField tableField = (TableField)field;
StephaneLenclud@176
  1900
StephaneLenclud@176
  1901
                //Create a control corresponding to the field specified for that cell
StephaneLenclud@176
  1902
                Control control = CreateControlForDataField(tableField);
StephaneLenclud@176
  1903
StephaneLenclud@176
  1904
                //Add newly created control to our table layout at the specified row and column
StephaneLenclud@176
  1905
                iTableLayoutPanel.Controls.Add(control, tableField.Column, tableField.Row);
StephaneLenclud@176
  1906
                //Make sure we specify column and row span for that new control
StephaneLenclud@176
  1907
                iTableLayoutPanel.SetColumnSpan(control, tableField.ColumnSpan);
StephaneLenclud@176
  1908
                iTableLayoutPanel.SetRowSpan(control, tableField.RowSpan);
sl@70
  1909
            }
sl@70
  1910
StephaneLenclud@176
  1911
sl@63
  1912
            CheckFontHeight();
sl@63
  1913
        }
sl@63
  1914
sl@68
  1915
        /// <summary>
sl@70
  1916
        /// Check our type of data field and create corresponding control
sl@68
  1917
        /// </summary>
sl@68
  1918
        /// <param name="aField"></param>
sl@69
  1919
        private Control CreateControlForDataField(DataField aField)
sl@68
  1920
        {
sl@68
  1921
            Control control=null;
StephaneLenclud@172
  1922
            if (aField.IsTextField)
sl@68
  1923
            {
sl@68
  1924
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  1925
                label.AutoEllipsis = true;
sl@68
  1926
                label.AutoSize = true;
sl@68
  1927
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  1928
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  1929
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  1930
                label.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@176
  1931
                label.Name = "marqueeLabel" + aField;
sl@68
  1932
                label.OwnTimer = false;
StephaneLenclud@106
  1933
                label.PixelsPerSecond = cds.ScrollingSpeedInPixelsPerSecond;
sl@100
  1934
                label.Separator = cds.Separator;
sl@100
  1935
                label.MinFontSize = cds.MinFontSize;
sl@100
  1936
                label.ScaleToFit = cds.ScaleToFit;
sl@68
  1937
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  1938
                //control.TabIndex = 2;
sl@68
  1939
                label.Font = cds.Font;
sl@68
  1940
StephaneLenclud@172
  1941
                TextField field = (TextField)aField;
StephaneLenclud@172
  1942
                label.TextAlign = field.Alignment;
sl@68
  1943
                label.UseCompatibleTextRendering = true;
StephaneLenclud@172
  1944
                label.Text = field.Text;
sl@68
  1945
                //
sl@68
  1946
                control = label;
sl@68
  1947
            }
StephaneLenclud@172
  1948
            else if (aField.IsBitmapField)
sl@68
  1949
            {
sl@68
  1950
                //Create picture box
sl@68
  1951
                PictureBox picture = new PictureBox();
sl@68
  1952
                picture.AutoSize = true;
sl@68
  1953
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  1954
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  1955
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  1956
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  1957
                picture.Name = "pictureBox" + aField;
sl@68
  1958
                //Set our image
StephaneLenclud@172
  1959
                BitmapField field = (BitmapField)aField;
StephaneLenclud@172
  1960
                picture.Image = field.Bitmap;
sl@68
  1961
                //
sl@68
  1962
                control = picture;
sl@68
  1963
            }
StephaneLenclud@172
  1964
            //TODO: Handle recording field?
sl@68
  1965
sl@69
  1966
            return control;
sl@68
  1967
        }
sl@68
  1968
StephaneLenclud@103
  1969
		/// <summary>
StephaneLenclud@103
  1970
		/// Called when the user selected a new display type.
StephaneLenclud@103
  1971
		/// </summary>
StephaneLenclud@103
  1972
		/// <param name="sender"></param>
StephaneLenclud@103
  1973
		/// <param name="e"></param>
sl@46
  1974
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  1975
        {
StephaneLenclud@103
  1976
			//Store the selected display type in our settings
sl@48
  1977
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  1978
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  1979
            Properties.Settings.Default.Save();
StephaneLenclud@103
  1980
StephaneLenclud@103
  1981
			//Try re-opening the display connection if we were already connected.
StephaneLenclud@103
  1982
			//Otherwise just update our status to reflect display type change.
sl@51
  1983
            if (iDisplay.IsOpen())
sl@51
  1984
            {
sl@51
  1985
                OpenDisplayConnection();
sl@51
  1986
            }
sl@51
  1987
            else
sl@51
  1988
            {
sl@51
  1989
                UpdateStatus();
sl@51
  1990
            }
sl@46
  1991
        }
sl@46
  1992
sl@47
  1993
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  1994
        {
sl@47
  1995
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  1996
            {
sl@51
  1997
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  1998
sl@51
  1999
                if (interval > 0)
sl@51
  2000
                {
sl@51
  2001
                    timer.Interval = interval;
sl@51
  2002
                    cds.TimerInterval = timer.Interval;
sl@51
  2003
                    Properties.Settings.Default.Save();
sl@51
  2004
                }
sl@47
  2005
            }
sl@47
  2006
        }
sl@47
  2007
sl@100
  2008
        private void maskedTextBoxMinFontSize_TextChanged(object sender, EventArgs e)
sl@100
  2009
        {
sl@100
  2010
            if (maskedTextBoxMinFontSize.Text != "")
sl@100
  2011
            {
sl@100
  2012
                int minFontSize = Convert.ToInt32(maskedTextBoxMinFontSize.Text);
sl@100
  2013
sl@100
  2014
                if (minFontSize > 0)
sl@100
  2015
                {
sl@100
  2016
                    cds.MinFontSize = minFontSize;
sl@100
  2017
                    Properties.Settings.Default.Save();
StephaneLenclud@106
  2018
					//We need to recreate our layout for that change to take effect
StephaneLenclud@106
  2019
					UpdateTableLayoutPanel(iCurrentClientData);
sl@100
  2020
                }
sl@100
  2021
            }
sl@100
  2022
        }
sl@100
  2023
StephaneLenclud@106
  2024
StephaneLenclud@106
  2025
		private void maskedTextBoxScrollingSpeed_TextChanged(object sender, EventArgs e)
StephaneLenclud@106
  2026
		{
StephaneLenclud@106
  2027
			if (maskedTextBoxScrollingSpeed.Text != "")
StephaneLenclud@106
  2028
			{
StephaneLenclud@106
  2029
				int scrollingSpeed = Convert.ToInt32(maskedTextBoxScrollingSpeed.Text);
StephaneLenclud@106
  2030
StephaneLenclud@106
  2031
				if (scrollingSpeed > 0)
StephaneLenclud@106
  2032
				{
StephaneLenclud@106
  2033
					cds.ScrollingSpeedInPixelsPerSecond = scrollingSpeed;
StephaneLenclud@106
  2034
					Properties.Settings.Default.Save();
StephaneLenclud@106
  2035
					//We need to recreate our layout for that change to take effect
StephaneLenclud@106
  2036
					UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@106
  2037
				}
StephaneLenclud@106
  2038
			}
StephaneLenclud@106
  2039
		}
StephaneLenclud@106
  2040
sl@100
  2041
        private void textBoxScrollLoopSeparator_TextChanged(object sender, EventArgs e)
sl@100
  2042
        {
sl@100
  2043
            cds.Separator = textBoxScrollLoopSeparator.Text;
sl@100
  2044
            Properties.Settings.Default.Save();
StephaneLenclud@110
  2045
StephaneLenclud@110
  2046
			//Update our text fields
StephaneLenclud@175
  2047
			foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
StephaneLenclud@110
  2048
			{
StephaneLenclud@110
  2049
				ctrl.Separator = cds.Separator;
StephaneLenclud@110
  2050
			}
StephaneLenclud@110
  2051
sl@100
  2052
        }
sl@100
  2053
sl@52
  2054
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  2055
        {
sl@52
  2056
            iDisplay.PowerOn();
sl@52
  2057
        }
sl@52
  2058
sl@52
  2059
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  2060
        {
sl@52
  2061
            iDisplay.PowerOff();
sl@52
  2062
        }
sl@52
  2063
sl@53
  2064
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  2065
        {
StephaneLenclud@122
  2066
			ShowClock();
sl@53
  2067
        }
sl@53
  2068
sl@53
  2069
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  2070
        {
StephaneLenclud@122
  2071
			HideClock();
sl@53
  2072
        }
sl@88
  2073
sl@88
  2074
        private void buttonUpdate_Click(object sender, EventArgs e)
sl@88
  2075
        {
sl@88
  2076
            InstallUpdateSyncWithInfo();
sl@88
  2077
        }
sl@88
  2078
StephaneLenclud@122
  2079
		/// <summary>
StephaneLenclud@122
  2080
		/// 
StephaneLenclud@122
  2081
		/// </summary>
StephaneLenclud@122
  2082
		void ShowClock()
StephaneLenclud@122
  2083
		{
StephaneLenclud@122
  2084
			if (!iDisplay.IsOpen())
StephaneLenclud@122
  2085
			{
StephaneLenclud@122
  2086
				return;
StephaneLenclud@122
  2087
			}
StephaneLenclud@122
  2088
StephaneLenclud@122
  2089
			//Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@122
  2090
			iSkipFrameRendering = true;
StephaneLenclud@122
  2091
			//Clear our screen 
StephaneLenclud@122
  2092
			iDisplay.Clear();
StephaneLenclud@122
  2093
			iDisplay.SwapBuffers();
StephaneLenclud@122
  2094
			//Then show our clock
StephaneLenclud@122
  2095
			iDisplay.ShowClock();
StephaneLenclud@122
  2096
		}
StephaneLenclud@122
  2097
StephaneLenclud@122
  2098
		/// <summary>
StephaneLenclud@122
  2099
		/// 
StephaneLenclud@122
  2100
		/// </summary>
StephaneLenclud@122
  2101
		void HideClock()
StephaneLenclud@122
  2102
		{
StephaneLenclud@122
  2103
			if (!iDisplay.IsOpen())
StephaneLenclud@122
  2104
			{
StephaneLenclud@122
  2105
				return;
StephaneLenclud@122
  2106
			}
StephaneLenclud@122
  2107
StephaneLenclud@122
  2108
			//Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@122
  2109
			iSkipFrameRendering = false;
StephaneLenclud@122
  2110
			iDisplay.HideClock();
StephaneLenclud@122
  2111
		}
sl@88
  2112
sl@88
  2113
        private void InstallUpdateSyncWithInfo()
sl@88
  2114
        {
sl@88
  2115
            UpdateCheckInfo info = null;
sl@88
  2116
sl@88
  2117
            if (ApplicationDeployment.IsNetworkDeployed)
sl@88
  2118
            {
sl@88
  2119
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
sl@88
  2120
sl@88
  2121
                try
sl@88
  2122
                {
sl@88
  2123
                    info = ad.CheckForDetailedUpdate();
sl@88
  2124
sl@88
  2125
                }
sl@88
  2126
                catch (DeploymentDownloadException dde)
sl@88
  2127
                {
sl@88
  2128
                    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
  2129
                    return;
sl@88
  2130
                }
sl@88
  2131
                catch (InvalidDeploymentException ide)
sl@88
  2132
                {
sl@88
  2133
                    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
  2134
                    return;
sl@88
  2135
                }
sl@88
  2136
                catch (InvalidOperationException ioe)
sl@88
  2137
                {
sl@88
  2138
                    MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message);
sl@88
  2139
                    return;
sl@88
  2140
                }
sl@88
  2141
sl@90
  2142
				if (info.UpdateAvailable)
sl@90
  2143
				{
sl@90
  2144
					Boolean doUpdate = true;
sl@88
  2145
sl@90
  2146
					if (!info.IsUpdateRequired)
sl@90
  2147
					{
sl@90
  2148
						DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel);
sl@90
  2149
						if (!(DialogResult.OK == dr))
sl@90
  2150
						{
sl@90
  2151
							doUpdate = false;
sl@90
  2152
						}
sl@90
  2153
					}
sl@90
  2154
					else
sl@90
  2155
					{
sl@90
  2156
						// Display a message that the app MUST reboot. Display the minimum required version.
sl@90
  2157
						MessageBox.Show("This application has detected a mandatory update from your current " +
sl@90
  2158
							"version to version " + info.MinimumRequiredVersion.ToString() +
sl@90
  2159
							". The application will now install the update and restart.",
sl@90
  2160
							"Update Available", MessageBoxButtons.OK,
sl@90
  2161
							MessageBoxIcon.Information);
sl@90
  2162
					}
sl@88
  2163
sl@90
  2164
					if (doUpdate)
sl@90
  2165
					{
sl@90
  2166
						try
sl@90
  2167
						{
sl@90
  2168
							ad.Update();
sl@90
  2169
							MessageBox.Show("The application has been upgraded, and will now restart.");
sl@90
  2170
							Application.Restart();
sl@90
  2171
						}
sl@90
  2172
						catch (DeploymentDownloadException dde)
sl@90
  2173
						{
sl@90
  2174
							MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
sl@90
  2175
							return;
sl@90
  2176
						}
sl@90
  2177
					}
sl@90
  2178
				}
sl@90
  2179
				else
sl@90
  2180
				{
sl@90
  2181
					MessageBox.Show("You are already running the latest version.", "Application up-to-date");
sl@90
  2182
				}
sl@88
  2183
            }
sl@88
  2184
        }
sl@92
  2185
sl@94
  2186
sl@94
  2187
		/// <summary>
sl@99
  2188
		/// Used to
sl@94
  2189
		/// </summary>
sl@94
  2190
		private void SysTrayHideShow()
sl@92
  2191
		{
sl@94
  2192
			Visible = !Visible;
sl@94
  2193
			if (Visible)
sl@94
  2194
			{
sl@94
  2195
				Activate();
sl@94
  2196
				WindowState = FormWindowState.Normal;
sl@94
  2197
			}
sl@92
  2198
		}
sl@94
  2199
sl@94
  2200
		/// <summary>
sl@94
  2201
		/// Use to handle minimize events.
sl@94
  2202
		/// </summary>
sl@94
  2203
		/// <param name="sender"></param>
sl@94
  2204
		/// <param name="e"></param>
sl@94
  2205
		private void MainForm_SizeChanged(object sender, EventArgs e)
sl@94
  2206
		{
sl@94
  2207
			if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
sl@94
  2208
			{
sl@94
  2209
				if (Visible)
sl@94
  2210
				{
sl@94
  2211
					SysTrayHideShow();
sl@94
  2212
				}
sl@94
  2213
			}
sl@94
  2214
		}
sl@94
  2215
StephaneLenclud@105
  2216
		/// <summary>
StephaneLenclud@105
  2217
		/// 
StephaneLenclud@105
  2218
		/// </summary>
StephaneLenclud@105
  2219
		/// <param name="sender"></param>
StephaneLenclud@105
  2220
		/// <param name="e"></param>
StephaneLenclud@105
  2221
		private void tableLayoutPanel_SizeChanged(object sender, EventArgs e)
StephaneLenclud@105
  2222
		{
StephaneLenclud@105
  2223
			//Our table layout size has changed which means our display size has changed.
StephaneLenclud@105
  2224
			//We need to re-create our bitmap.
StephaneLenclud@105
  2225
			iCreateBitmap = true;
StephaneLenclud@105
  2226
		}
StephaneLenclud@126
  2227
StephaneLenclud@126
  2228
		/// <summary>
StephaneLenclud@126
  2229
		/// 
StephaneLenclud@126
  2230
		/// </summary>
StephaneLenclud@126
  2231
		/// <param name="sender"></param>
StephaneLenclud@126
  2232
		/// <param name="e"></param>
StephaneLenclud@126
  2233
		private void buttonSelectFile_Click(object sender, EventArgs e)
StephaneLenclud@126
  2234
		{
StephaneLenclud@126
  2235
			//openFileDialog1.InitialDirectory = "c:\\";
StephaneLenclud@126
  2236
			//openFileDialog.Filter = "EXE files (*.exe)|*.exe|All files (*.*)|*.*";
StephaneLenclud@126
  2237
			//openFileDialog.FilterIndex = 1;
StephaneLenclud@126
  2238
			openFileDialog.RestoreDirectory = true;
StephaneLenclud@126
  2239
StephaneLenclud@126
  2240
			if (DlgBox.ShowDialog(openFileDialog) == DialogResult.OK)
StephaneLenclud@126
  2241
			{
StephaneLenclud@126
  2242
				labelStartFileName.Text = openFileDialog.FileName;
StephaneLenclud@126
  2243
				Properties.Settings.Default.StartFileName = openFileDialog.FileName;
StephaneLenclud@126
  2244
				Properties.Settings.Default.Save();
StephaneLenclud@126
  2245
			}
StephaneLenclud@126
  2246
		}
StephaneLenclud@153
  2247
StephaneLenclud@153
  2248
        /// <summary>
StephaneLenclud@153
  2249
        /// 
StephaneLenclud@153
  2250
        /// </summary>
StephaneLenclud@153
  2251
        /// <param name="sender"></param>
StephaneLenclud@153
  2252
        /// <param name="e"></param>
StephaneLenclud@153
  2253
        private void comboBoxOpticalDrives_SelectedIndexChanged(object sender, EventArgs e)
StephaneLenclud@153
  2254
        {
StephaneLenclud@153
  2255
            //Save the optical drive the user selected for ejection
StephaneLenclud@153
  2256
            Properties.Settings.Default.OpticalDriveToEject = comboBoxOpticalDrives.SelectedItem.ToString();
StephaneLenclud@153
  2257
            Properties.Settings.Default.Save();
StephaneLenclud@153
  2258
        }
StephaneLenclud@167
  2259
StephaneLenclud@167
  2260
        /// <summary>
StephaneLenclud@167
  2261
        /// Broadcast messages to subscribers.
StephaneLenclud@167
  2262
        /// </summary>
StephaneLenclud@167
  2263
        /// <param name="message"></param>
StephaneLenclud@167
  2264
        protected override void WndProc(ref Message aMessage)
StephaneLenclud@167
  2265
        {
StephaneLenclud@167
  2266
            if (OnWndProc!=null)
StephaneLenclud@167
  2267
            {
StephaneLenclud@167
  2268
                OnWndProc(ref aMessage);
StephaneLenclud@167
  2269
            }
StephaneLenclud@167
  2270
            
StephaneLenclud@167
  2271
            base.WndProc(ref aMessage);
StephaneLenclud@167
  2272
        }
StephaneLenclud@168
  2273
StephaneLenclud@168
  2274
        private void checkBoxCecEnabled_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2275
        {
StephaneLenclud@168
  2276
            //Save CEC enabled status
StephaneLenclud@168
  2277
            Properties.Settings.Default.CecEnabled = checkBoxCecEnabled.Checked;
StephaneLenclud@168
  2278
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2279
            //
StephaneLenclud@168
  2280
            ResetCec();
StephaneLenclud@168
  2281
        }
StephaneLenclud@168
  2282
StephaneLenclud@168
  2283
        private void comboBoxHdmiPort_SelectedIndexChanged(object sender, EventArgs e)
StephaneLenclud@168
  2284
        {
StephaneLenclud@168
  2285
            //Save CEC HDMI port
StephaneLenclud@168
  2286
            Properties.Settings.Default.CecHdmiPort = Convert.ToByte(comboBoxHdmiPort.SelectedIndex);
StephaneLenclud@168
  2287
            Properties.Settings.Default.CecHdmiPort++;
StephaneLenclud@168
  2288
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2289
            //
StephaneLenclud@168
  2290
            ResetCec();
StephaneLenclud@168
  2291
        }
StephaneLenclud@168
  2292
StephaneLenclud@168
  2293
        private void checkBoxCecMonitorOff_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2294
        {
StephaneLenclud@168
  2295
            Properties.Settings.Default.CecMonitorOff = checkBoxCecMonitorOff.Checked;
StephaneLenclud@168
  2296
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2297
            //
StephaneLenclud@168
  2298
            ResetCec();
StephaneLenclud@168
  2299
        }
StephaneLenclud@168
  2300
StephaneLenclud@168
  2301
        private void checkBoxCecMonitorOn_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2302
        {
StephaneLenclud@168
  2303
            Properties.Settings.Default.CecMonitorOn = checkBoxCecMonitorOn.Checked;
StephaneLenclud@168
  2304
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2305
            //
StephaneLenclud@168
  2306
            ResetCec();
StephaneLenclud@168
  2307
        }
StephaneLenclud@168
  2308
StephaneLenclud@168
  2309
        /// <summary>
StephaneLenclud@168
  2310
        /// 
StephaneLenclud@168
  2311
        /// </summary>
StephaneLenclud@168
  2312
        private void ResetCec()
StephaneLenclud@168
  2313
        {
StephaneLenclud@168
  2314
            if (iCecManager==null)
StephaneLenclud@168
  2315
            {
StephaneLenclud@168
  2316
                //Thus skipping initial UI setup
StephaneLenclud@168
  2317
                return;
StephaneLenclud@168
  2318
            }
StephaneLenclud@168
  2319
StephaneLenclud@168
  2320
            iCecManager.Stop();
StephaneLenclud@168
  2321
            //
StephaneLenclud@168
  2322
            if (Properties.Settings.Default.CecEnabled)
StephaneLenclud@168
  2323
            {
StephaneLenclud@168
  2324
                iCecManager.Start(Handle, "CEC",
StephaneLenclud@168
  2325
                Properties.Settings.Default.CecHdmiPort,
StephaneLenclud@168
  2326
                Properties.Settings.Default.CecMonitorOn,
StephaneLenclud@168
  2327
                Properties.Settings.Default.CecMonitorOff);
StephaneLenclud@168
  2328
            }
StephaneLenclud@168
  2329
        }
sl@0
  2330
    }
sl@0
  2331
}