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