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