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