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