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