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