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