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