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