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