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