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