Server/MainForm.cs
author sl
Mon, 13 Oct 2014 21:21:53 +0200
changeset 68 1d0cd5e6e0a9
parent 67 6e50baf5a811
child 69 2add30edb508
permissions -rw-r--r--
Bitmap field now mostly working.
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@68
   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@62
   779
        /// <param name="aTextField"></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@67
   805
        /// <param name="aTextField"></param>
sl@65
   806
        public void SetClientTextThreadSafe(string aSessionId, TextField aTextField)
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@43
   812
                this.Invoke(d, new object[] { aSessionId, aTextField });
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@67
   821
                    while (client.Fields.Count < (aTextField.Index + 1))
sl@34
   822
                    {
sl@43
   823
                        //Add a text field with proper index
sl@67
   824
                        client.Fields.Add(new TextField(client.Fields.Count));
sl@34
   825
                    }
sl@67
   826
                    client.Fields[aTextField.Index] = aTextField;
sl@34
   827
sl@34
   828
                    //We are in the proper thread
sl@68
   829
                    if (tableLayoutPanel.Controls[aTextField.Index] is MarqueeLabel)
sl@68
   830
                    {
sl@68
   831
                        MarqueeLabel label = (MarqueeLabel)tableLayoutPanel.Controls[aTextField.Index];
sl@68
   832
                        label.Text = aTextField.Text;
sl@68
   833
                        label.TextAlign = aTextField.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@65
   850
        public void SetClientTextsThreadSafe(string aSessionId, System.Collections.Generic.IList<TextField> aTextFields)
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@43
   856
                this.Invoke(d, new object[] { aSessionId, aTextFields });
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@34
   866
                    int j = 0;
sl@43
   867
                    foreach (TextField textField in aTextFields)
sl@30
   868
                    {
sl@67
   869
                        if (client.Fields.Count < (j + 1))
sl@34
   870
                        {
sl@67
   871
                            client.Fields.Add(textField);
sl@34
   872
                        }
sl@34
   873
                        else
sl@34
   874
                        {
sl@67
   875
                            client.Fields[j] = textField;
sl@34
   876
                        }
sl@34
   877
                        j++;
sl@54
   878
                    }
sl@65
   879
                    //Put each our text fields in a label control
sl@68
   880
                    foreach (TextField field in aTextFields)
sl@30
   881
                    {
sl@68
   882
                        if (tableLayoutPanel.Controls[field.Index] is MarqueeLabel)
sl@68
   883
                        {
sl@68
   884
                            //Proper control type just update the text
sl@68
   885
                            MarqueeLabel label = (MarqueeLabel)tableLayoutPanel.Controls[field.Index];
sl@68
   886
                            label.Text = field.Text;
sl@68
   887
                            label.TextAlign = field.Alignment;
sl@68
   888
                        }
sl@68
   889
                        else
sl@68
   890
                        {
sl@68
   891
                            //Wrong control for the given field
sl@68
   892
                            //Update our layout thus re-creating our controls
sl@68
   893
                            UpdateTableLayoutPanel(iCurrentClientData);
sl@68
   894
                            break; //No need to keep on looping layout update will take care of everything
sl@68
   895
                        }
sl@30
   896
                    }
sl@34
   897
sl@34
   898
sl@34
   899
                    UpdateClientTreeViewNode(client);
sl@30
   900
                }
sl@30
   901
            }
sl@32
   902
        }
sl@30
   903
sl@67
   904
        /// <summary>
sl@67
   905
        ///
sl@67
   906
        /// </summary>
sl@67
   907
        /// <param name="aSessionId"></param>
sl@67
   908
        /// <param name="aTextField"></param>
sl@67
   909
        public void SetClientBitmapThreadSafe(string aSessionId, BitmapField aBitmapField)
sl@67
   910
        {
sl@67
   911
            if (this.InvokeRequired)
sl@67
   912
            {
sl@67
   913
                //Not in the proper thread, invoke ourselves
sl@67
   914
                SetBitmapDelegate d = new SetBitmapDelegate(SetClientBitmapThreadSafe);
sl@67
   915
                this.Invoke(d, new object[] { aSessionId, aBitmapField });
sl@67
   916
            }
sl@67
   917
            else
sl@67
   918
            {
sl@67
   919
                SetCurrentClient(aSessionId);
sl@67
   920
                ClientData client = iClients[aSessionId];
sl@67
   921
                if (client != null)
sl@67
   922
                {
sl@67
   923
                    //Make sure all our texts are in place
sl@67
   924
                    while (client.Fields.Count < (aBitmapField.Index + 1))
sl@67
   925
                    {
sl@67
   926
                        //Add a text field with proper index
sl@67
   927
                        client.Fields.Add(new TextField(client.Fields.Count));
sl@67
   928
                    }
sl@67
   929
sl@67
   930
                    client.Fields[aBitmapField.Index] = aBitmapField;
sl@67
   931
sl@67
   932
                    //We are in the proper thread
sl@68
   933
                    if (tableLayoutPanel.Controls[aBitmapField.Index] is PictureBox)
sl@68
   934
                    {
sl@68
   935
                        PictureBox pictureBox = (PictureBox)tableLayoutPanel.Controls[aBitmapField.Index];
sl@68
   936
                        pictureBox.Image = aBitmapField.Bitmap;
sl@68
   937
                    }
sl@68
   938
                    else
sl@68
   939
                    {
sl@68
   940
                        //Wrong control type re-create them all
sl@68
   941
                        UpdateTableLayoutPanel(iCurrentClientData);
sl@68
   942
                    }
sl@67
   943
                    //
sl@67
   944
                    UpdateClientTreeViewNode(client);
sl@67
   945
                }
sl@67
   946
            }
sl@67
   947
        }
sl@67
   948
sl@67
   949
sl@32
   950
sl@32
   951
        /// <summary>
sl@36
   952
        ///
sl@32
   953
        /// </summary>
sl@32
   954
        /// <param name="aSessionId"></param>
sl@32
   955
        /// <param name="aName"></param>
sl@32
   956
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
   957
        {
sl@32
   958
            if (this.InvokeRequired)
sl@32
   959
            {
sl@32
   960
                //Not in the proper thread, invoke ourselves
sl@32
   961
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
sl@32
   962
                this.Invoke(d, new object[] { aSessionId, aName });
sl@32
   963
            }
sl@32
   964
            else
sl@32
   965
            {
sl@32
   966
                //We are in the proper thread
sl@33
   967
                //Get our client
sl@33
   968
                ClientData client = iClients[aSessionId];
sl@33
   969
                if (client != null)
sl@32
   970
                {
sl@33
   971
                    //Set its name
sl@33
   972
                    client.Name = aName;
sl@33
   973
                    //Update our tree-view
sl@33
   974
                    UpdateClientTreeViewNode(client);
sl@33
   975
                }
sl@33
   976
            }
sl@33
   977
        }
sl@33
   978
sl@33
   979
        /// <summary>
sl@36
   980
        ///
sl@33
   981
        /// </summary>
sl@33
   982
        /// <param name="aClient"></param>
sl@33
   983
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
   984
        {
sl@33
   985
            if (aClient == null)
sl@33
   986
            {
sl@33
   987
                return;
sl@33
   988
            }
sl@33
   989
sl@33
   990
            TreeNode node = null;
sl@33
   991
            //Check that our client node already exists
sl@33
   992
            //Get our client root node using its key which is our session ID
sl@33
   993
            TreeNode[] nodes = treeViewClients.Nodes.Find(aClient.SessionId, false);
sl@33
   994
            if (nodes.Count()>0)
sl@33
   995
            {
sl@33
   996
                //We already have a node for that client
sl@33
   997
                node = nodes[0];
sl@33
   998
                //Clear children as we are going to recreate them below
sl@33
   999
                node.Nodes.Clear();
sl@33
  1000
            }
sl@33
  1001
            else
sl@33
  1002
            {
sl@33
  1003
                //Client node does not exists create a new one
sl@33
  1004
                treeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
sl@33
  1005
                node = treeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
  1006
            }
sl@33
  1007
sl@33
  1008
            if (node != null)
sl@33
  1009
            {
sl@33
  1010
                //Change its name
sl@33
  1011
                if (aClient.Name != "")
sl@33
  1012
                {
sl@33
  1013
                    //We have a name, us it as text for our root node
sl@33
  1014
                    node.Text = aClient.Name;
sl@32
  1015
                    //Add a child with SessionId
sl@33
  1016
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
  1017
                }
sl@33
  1018
                else
sl@33
  1019
                {
sl@33
  1020
                    //No name, use session ID instead
sl@33
  1021
                    node.Text = aClient.SessionId;
sl@33
  1022
                }
sl@36
  1023
sl@67
  1024
                if (aClient.Fields.Count > 0)
sl@33
  1025
                {
sl@33
  1026
                    //Create root node for our texts
sl@33
  1027
                    TreeNode textsRoot = new TreeNode("Text");
sl@33
  1028
                    node.Nodes.Add(textsRoot);
sl@33
  1029
                    //For each text add a new entry
sl@67
  1030
                    foreach (DataField field in aClient.Fields)
sl@33
  1031
                    {
sl@67
  1032
                        if (field is TextField)
sl@67
  1033
                        {
sl@67
  1034
                            TextField textField = (TextField)field;
sl@67
  1035
                            textsRoot.Nodes.Add(new TreeNode(textField.Text));
sl@67
  1036
                        }
sl@67
  1037
                        else if (field is BitmapField)
sl@67
  1038
                        {
sl@67
  1039
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap Field]"));
sl@67
  1040
                        }
sl@67
  1041
                        else
sl@67
  1042
                        {
sl@67
  1043
                            textsRoot.Nodes.Add(new TreeNode("[Unknown Field Type]"));
sl@67
  1044
                        }                        
sl@33
  1045
                    }
sl@32
  1046
                }
sl@34
  1047
sl@34
  1048
                node.ExpandAll();
sl@32
  1049
            }
sl@30
  1050
        }
sl@17
  1051
sl@38
  1052
        private void buttonAddRow_Click(object sender, EventArgs e)
sl@38
  1053
        {
sl@38
  1054
            if (tableLayoutPanel.RowCount < 6)
sl@38
  1055
            {
sl@62
  1056
                UpdateTableLayoutPanel(tableLayoutPanel.ColumnCount, tableLayoutPanel.RowCount + 1);
sl@38
  1057
            }
sl@38
  1058
        }
sl@38
  1059
sl@38
  1060
        private void buttonRemoveRow_Click(object sender, EventArgs e)
sl@38
  1061
        {
sl@38
  1062
            if (tableLayoutPanel.RowCount > 1)
sl@38
  1063
            {
sl@62
  1064
                UpdateTableLayoutPanel(tableLayoutPanel.ColumnCount, tableLayoutPanel.RowCount - 1);
sl@38
  1065
            }
sl@60
  1066
sl@60
  1067
            UpdateTableLayoutRowStyles();
sl@60
  1068
        }
sl@60
  1069
sl@62
  1070
        private void buttonAddColumn_Click(object sender, EventArgs e)
sl@62
  1071
        {
sl@62
  1072
            if (tableLayoutPanel.ColumnCount < 8)
sl@62
  1073
            {
sl@62
  1074
                UpdateTableLayoutPanel(tableLayoutPanel.ColumnCount + 1, tableLayoutPanel.RowCount);
sl@62
  1075
            }
sl@62
  1076
        }
sl@62
  1077
sl@62
  1078
        private void buttonRemoveColumn_Click(object sender, EventArgs e)
sl@62
  1079
        {
sl@62
  1080
            if (tableLayoutPanel.ColumnCount > 1)
sl@62
  1081
            {
sl@62
  1082
                UpdateTableLayoutPanel(tableLayoutPanel.ColumnCount - 1, tableLayoutPanel.RowCount);
sl@62
  1083
            }
sl@62
  1084
        }
sl@62
  1085
sl@62
  1086
sl@60
  1087
        /// <summary>
sl@60
  1088
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  1089
        /// </summary>
sl@60
  1090
        private void UpdateTableLayoutRowStyles()
sl@60
  1091
        {
sl@60
  1092
            foreach (RowStyle rowStyle in tableLayoutPanel.RowStyles)
sl@60
  1093
            {
sl@60
  1094
                rowStyle.SizeType = SizeType.Percent;
sl@60
  1095
                rowStyle.Height = 100 / tableLayoutPanel.RowCount;
sl@60
  1096
            }
sl@60
  1097
        }
sl@60
  1098
sl@60
  1099
        /// <summary>
sl@61
  1100
        /// Empty and recreate our table layout with the given number of columns and rows.
sl@61
  1101
        /// Sizes of rows and columns are uniform.
sl@60
  1102
        /// </summary>
sl@60
  1103
        /// <param name="aColumn"></param>
sl@60
  1104
        /// <param name="aRow"></param>
sl@62
  1105
        private void UpdateTableLayoutPanel(int aColumn, int aRow)
sl@60
  1106
        {
sl@61
  1107
            tableLayoutPanel.Controls.Clear();
sl@61
  1108
            tableLayoutPanel.RowStyles.Clear();
sl@61
  1109
            tableLayoutPanel.ColumnStyles.Clear();
sl@61
  1110
            tableLayoutPanel.RowCount = 0;
sl@61
  1111
            tableLayoutPanel.ColumnCount = 0;
sl@60
  1112
sl@61
  1113
            while (tableLayoutPanel.RowCount < aRow)
sl@61
  1114
            {
sl@61
  1115
                tableLayoutPanel.RowCount++;
sl@61
  1116
            }
sl@60
  1117
sl@61
  1118
            while (tableLayoutPanel.ColumnCount < aColumn)
sl@61
  1119
            {
sl@61
  1120
                tableLayoutPanel.ColumnCount++;
sl@61
  1121
            }
sl@61
  1122
sl@61
  1123
            for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
sl@61
  1124
            {
sl@61
  1125
                //Create our column styles
sl@61
  1126
                this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100 / tableLayoutPanel.ColumnCount));
sl@61
  1127
sl@61
  1128
                for (int j = 0; j < tableLayoutPanel.RowCount; j++)
sl@61
  1129
                {
sl@61
  1130
                    if (i == 0)
sl@61
  1131
                    {
sl@61
  1132
                        //Create our row styles
sl@61
  1133
                        this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100 / tableLayoutPanel.RowCount));
sl@61
  1134
                    }
sl@61
  1135
sl@61
  1136
                    MarqueeLabel control = new SharpDisplayManager.MarqueeLabel();
sl@61
  1137
                    control.AutoEllipsis = true;
sl@61
  1138
                    control.AutoSize = true;
sl@61
  1139
                    control.BackColor = System.Drawing.Color.Transparent;
sl@61
  1140
                    control.Dock = System.Windows.Forms.DockStyle.Fill;
sl@61
  1141
                    control.Location = new System.Drawing.Point(1, 1);
sl@61
  1142
                    control.Margin = new System.Windows.Forms.Padding(0);
sl@61
  1143
                    control.Name = "marqueeLabelCol" + aColumn + "Row" + aRow;
sl@61
  1144
                    control.OwnTimer = false;
sl@61
  1145
                    control.PixelsPerSecond = 64;
sl@61
  1146
                    control.Separator = "|";
sl@61
  1147
                    //control.Size = new System.Drawing.Size(254, 30);
sl@61
  1148
                    //control.TabIndex = 2;
sl@61
  1149
                    control.Font = cds.Font;
sl@61
  1150
                    control.Text = "ABCDEFGHIJKLMNOPQRST-0123456789";
sl@61
  1151
                    control.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
sl@61
  1152
                    control.UseCompatibleTextRendering = true;
sl@61
  1153
                    //
sl@61
  1154
                    tableLayoutPanel.Controls.Add(control, i, j);
sl@61
  1155
                }
sl@61
  1156
            }
sl@38
  1157
sl@62
  1158
            CheckFontHeight();
sl@38
  1159
        }
sl@38
  1160
sl@63
  1161
sl@63
  1162
        /// <summary>
sl@63
  1163
        /// Update our display table layout.
sl@63
  1164
        /// </summary>
sl@63
  1165
        /// <param name="aLayout"></param>
sl@65
  1166
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  1167
        {
sl@65
  1168
            TableLayout layout = aClient.Layout;
sl@65
  1169
            
sl@63
  1170
            tableLayoutPanel.Controls.Clear();
sl@63
  1171
            tableLayoutPanel.RowStyles.Clear();
sl@63
  1172
            tableLayoutPanel.ColumnStyles.Clear();
sl@63
  1173
            tableLayoutPanel.RowCount = 0;
sl@63
  1174
            tableLayoutPanel.ColumnCount = 0;
sl@63
  1175
sl@65
  1176
            while (tableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  1177
            {
sl@63
  1178
                tableLayoutPanel.RowCount++;
sl@63
  1179
            }
sl@63
  1180
sl@65
  1181
            while (tableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  1182
            {
sl@63
  1183
                tableLayoutPanel.ColumnCount++;
sl@63
  1184
            }
sl@63
  1185
sl@63
  1186
            for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
sl@63
  1187
            {
sl@63
  1188
                //Create our column styles
sl@65
  1189
                this.tableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  1190
sl@63
  1191
                for (int j = 0; j < tableLayoutPanel.RowCount; j++)
sl@63
  1192
                {
sl@63
  1193
                    if (i == 0)
sl@63
  1194
                    {
sl@63
  1195
                        //Create our row styles
sl@65
  1196
                        this.tableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  1197
                    }
sl@63
  1198
sl@68
  1199
sl@68
  1200
                    MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  1201
                    label.AutoEllipsis = true;
sl@68
  1202
                    label.AutoSize = true;
sl@68
  1203
                    label.BackColor = System.Drawing.Color.Transparent;
sl@68
  1204
                    label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  1205
                    label.Location = new System.Drawing.Point(1, 1);
sl@68
  1206
                    label.Margin = new System.Windows.Forms.Padding(0);
sl@68
  1207
                    label.Name = "marqueeLabelCol" + layout.Columns.Count + "Row" + layout.Rows.Count;
sl@68
  1208
                    label.OwnTimer = false;
sl@68
  1209
                    label.PixelsPerSecond = 64;
sl@68
  1210
                    label.Separator = "|";
sl@63
  1211
                    //control.Size = new System.Drawing.Size(254, 30);
sl@63
  1212
                    //control.TabIndex = 2;
sl@68
  1213
                    label.Font = cds.Font;
sl@68
  1214
                    label.Text = "";
sl@68
  1215
                    label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
sl@68
  1216
                    label.UseCompatibleTextRendering = true;
sl@68
  1217
sl@68
  1218
                    Control control = label;
sl@65
  1219
                    //If we already have a text for that field
sl@67
  1220
                    if (aClient.Fields.Count > tableLayoutPanel.Controls.Count)
sl@65
  1221
                    {
sl@67
  1222
                        DataField field = aClient.Fields[tableLayoutPanel.Controls.Count];
sl@67
  1223
                        if (field is TextField)
sl@67
  1224
                        {
sl@67
  1225
                            TextField textField = (TextField)field;
sl@67
  1226
                            control.Text = textField.Text;
sl@67
  1227
                        }
sl@68
  1228
                        else if (field is BitmapField)
sl@68
  1229
                        {
sl@68
  1230
                            //Create picture box
sl@68
  1231
                            PictureBox pictue = new PictureBox();
sl@68
  1232
                            pictue.AutoSize = true;
sl@68
  1233
                            pictue.BackColor = System.Drawing.Color.Transparent;
sl@68
  1234
                            pictue.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  1235
                            pictue.Location = new System.Drawing.Point(1, 1);
sl@68
  1236
                            pictue.Margin = new System.Windows.Forms.Padding(0);
sl@68
  1237
                            pictue.Name = "pictureBox" + layout.Columns.Count + "Row" + layout.Rows.Count;
sl@68
  1238
                            //Set our image
sl@68
  1239
                            BitmapField bitmapField = (BitmapField)field;
sl@68
  1240
                            pictue.Image = bitmapField.Bitmap;
sl@68
  1241
                            //
sl@68
  1242
                            control = pictue;
sl@68
  1243
                        }
sl@65
  1244
                    }
sl@68
  1245
sl@63
  1246
                    //
sl@63
  1247
                    tableLayoutPanel.Controls.Add(control, i, j);
sl@63
  1248
                }
sl@63
  1249
            }
sl@63
  1250
sl@63
  1251
            CheckFontHeight();
sl@63
  1252
        }
sl@63
  1253
sl@68
  1254
        /// <summary>
sl@68
  1255
        /// Not used yet.
sl@68
  1256
        /// </summary>
sl@68
  1257
        /// <param name="aField"></param>
sl@68
  1258
        private void CreateControlForDataField(DataField aField)
sl@68
  1259
        {
sl@68
  1260
            Control control=null;
sl@68
  1261
            if (aField is TextField)
sl@68
  1262
            {
sl@68
  1263
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  1264
                label.AutoEllipsis = true;
sl@68
  1265
                label.AutoSize = true;
sl@68
  1266
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  1267
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  1268
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  1269
                label.Margin = new System.Windows.Forms.Padding(0);
sl@68
  1270
                label.Name = "marqueeLabel" + aField.Index;
sl@68
  1271
                label.OwnTimer = false;
sl@68
  1272
                label.PixelsPerSecond = 64;
sl@68
  1273
                label.Separator = "|";
sl@68
  1274
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  1275
                //control.TabIndex = 2;
sl@68
  1276
                label.Font = cds.Font;
sl@68
  1277
sl@68
  1278
                label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
sl@68
  1279
                label.UseCompatibleTextRendering = true;
sl@68
  1280
                TextField textField = (TextField)aField;
sl@68
  1281
                label.Text = textField.Text;
sl@68
  1282
                //
sl@68
  1283
                control = label;
sl@68
  1284
            }
sl@68
  1285
            else if (aField is BitmapField)
sl@68
  1286
            {
sl@68
  1287
                //Create picture box
sl@68
  1288
                PictureBox picture = new PictureBox();
sl@68
  1289
                picture.AutoSize = true;
sl@68
  1290
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  1291
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  1292
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  1293
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  1294
                picture.Name = "pictureBox" + aField;
sl@68
  1295
                //Set our image
sl@68
  1296
                BitmapField bitmapField = (BitmapField)aField;
sl@68
  1297
                picture.Image = bitmapField.Bitmap;
sl@68
  1298
                //
sl@68
  1299
                control = picture;
sl@68
  1300
            }
sl@68
  1301
sl@68
  1302
        }
sl@68
  1303
sl@63
  1304
sl@41
  1305
        private void buttonAlignLeft_Click(object sender, EventArgs e)
sl@41
  1306
        {
sl@60
  1307
            foreach (MarqueeLabel ctrl in tableLayoutPanel.Controls)
sl@60
  1308
            {
sl@60
  1309
                ctrl.TextAlign = ContentAlignment.MiddleLeft;
sl@60
  1310
            }
sl@41
  1311
        }
sl@41
  1312
sl@41
  1313
        private void buttonAlignCenter_Click(object sender, EventArgs e)
sl@41
  1314
        {
sl@60
  1315
            foreach (MarqueeLabel ctrl in tableLayoutPanel.Controls)
sl@60
  1316
            {
sl@60
  1317
                ctrl.TextAlign = ContentAlignment.MiddleCenter;
sl@60
  1318
            }
sl@41
  1319
        }
sl@41
  1320
sl@41
  1321
        private void buttonAlignRight_Click(object sender, EventArgs e)
sl@41
  1322
        {
sl@60
  1323
            foreach (MarqueeLabel ctrl in tableLayoutPanel.Controls)
sl@60
  1324
            {
sl@60
  1325
                ctrl.TextAlign = ContentAlignment.MiddleRight;
sl@60
  1326
            }
sl@41
  1327
        }
sl@36
  1328
sl@46
  1329
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  1330
        {
sl@48
  1331
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  1332
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  1333
            Properties.Settings.Default.Save();
sl@51
  1334
            if (iDisplay.IsOpen())
sl@51
  1335
            {
sl@51
  1336
                OpenDisplayConnection();
sl@51
  1337
            }
sl@51
  1338
            else
sl@51
  1339
            {
sl@51
  1340
                UpdateStatus();
sl@51
  1341
            }
sl@46
  1342
        }
sl@46
  1343
sl@47
  1344
sl@47
  1345
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  1346
        {
sl@47
  1347
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  1348
            {
sl@51
  1349
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  1350
sl@51
  1351
                if (interval > 0)
sl@51
  1352
                {
sl@51
  1353
                    timer.Interval = interval;
sl@51
  1354
                    cds.TimerInterval = timer.Interval;
sl@51
  1355
                    Properties.Settings.Default.Save();
sl@51
  1356
                }
sl@47
  1357
            }
sl@47
  1358
        }
sl@47
  1359
sl@52
  1360
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  1361
        {
sl@52
  1362
            iDisplay.PowerOn();
sl@52
  1363
        }
sl@52
  1364
sl@52
  1365
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  1366
        {
sl@52
  1367
            iDisplay.PowerOff();
sl@52
  1368
        }
sl@52
  1369
sl@53
  1370
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  1371
        {
sl@53
  1372
            iDisplay.ShowClock();
sl@53
  1373
        }
sl@53
  1374
sl@53
  1375
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  1376
        {
sl@53
  1377
            iDisplay.HideClock();
sl@53
  1378
        }
sl@0
  1379
    }
sl@34
  1380
sl@34
  1381
    /// <summary>
sl@34
  1382
    /// A UI thread copy of a client relevant data.
sl@34
  1383
    /// Keeping this copy in the UI thread helps us deal with threading issues.
sl@34
  1384
    /// </summary>
sl@34
  1385
    public class ClientData
sl@34
  1386
    {
sl@55
  1387
        public ClientData(string aSessionId, ICallback aCallback)
sl@34
  1388
        {
sl@34
  1389
            SessionId = aSessionId;
sl@34
  1390
            Name = "";
sl@67
  1391
            Fields = new List<DataField>();
sl@62
  1392
            Layout = new TableLayout(1, 2); //Default to one column and two rows
sl@34
  1393
            Callback = aCallback;
sl@34
  1394
        }
sl@34
  1395
sl@34
  1396
        public string SessionId { get; set; }
sl@34
  1397
        public string Name { get; set; }
sl@67
  1398
        public List<DataField> Fields { get; set; }
sl@62
  1399
        public TableLayout Layout { get; set; }
sl@55
  1400
        public ICallback Callback { get; set; }
sl@34
  1401
    }
sl@0
  1402
}