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