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