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