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