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