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