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