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