Server/MainForm.cs
author sl
Sun, 26 Oct 2014 16:16:57 +0100
changeset 75 2549a8055bd1
parent 72 fd0bb39a7818
child 76 906d88eb53fb
permissions -rw-r--r--
Removing specific functions for setting text and bitmap.
Now having a generic functions for setting any kind of fields.
Thus bitmaps can now be set together with text fields in a single server call.
sl@0
     1
using System;
sl@0
     2
using System.Collections.Generic;
sl@0
     3
using System.ComponentModel;
sl@0
     4
using System.Data;
sl@0
     5
using System.Drawing;
sl@0
     6
using System.Linq;
sl@0
     7
using System.Text;
sl@0
     8
using System.Threading.Tasks;
sl@0
     9
using System.Windows.Forms;
sl@14
    10
using System.IO;
sl@0
    11
using CodeProject.Dialog;
sl@14
    12
using System.Drawing.Imaging;
sl@17
    13
using System.ServiceModel;
sl@25
    14
using System.Threading;
sl@31
    15
using System.Diagnostics;
sl@25
    16
//
sl@25
    17
using SharpDisplayClient;
sl@55
    18
using SharpDisplay;
sl@0
    19
sl@0
    20
namespace SharpDisplayManager
sl@0
    21
{
sl@58
    22
    //Types declarations
sl@58
    23
    public delegate uint ColorProcessingDelegate(int aX, int aY, uint aPixel);
sl@58
    24
    public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
sl@62
    25
    //Delegates are used for our thread safe method
sl@62
    26
    public delegate void AddClientDelegate(string aSessionId, ICallback aCallback);
sl@62
    27
    public delegate void RemoveClientDelegate(string aSessionId);
sl@72
    28
    public delegate void SetTextDelegate(string SessionId, DataField aField);
sl@62
    29
    public delegate void SetLayoutDelegate(string SessionId, TableLayout aLayout);
sl@75
    30
    public delegate void SetFieldsDelegate(string SessionId, System.Collections.Generic.IList<DataField> aFields);
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@75
   810
                SetTextDelegate d = new SetTextDelegate(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@75
   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@75
   827
        {            
sl@75
   828
            SetCurrentClient(aSessionId);
sl@75
   829
            ClientData client = iClients[aSessionId];
sl@75
   830
            if (client != null)
sl@75
   831
            {
sl@75
   832
                //Make sure all our fields are in place
sl@75
   833
                while (client.Fields.Count < (aField.Index + 1))
sl@30
   834
                {
sl@75
   835
                    //Add a text field with proper index
sl@75
   836
                    client.Fields.Add(new DataField(client.Fields.Count));
sl@75
   837
                }
sl@75
   838
sl@75
   839
                if (client.Fields[aField.Index].IsSameLayout(aField))
sl@75
   840
                {
sl@75
   841
                    //Same layout just update our field
sl@75
   842
                    client.Fields[aField.Index] = aField;
sl@75
   843
                    //
sl@75
   844
                    if (aField.IsText && tableLayoutPanel.Controls[aField.Index] is MarqueeLabel)
sl@34
   845
                    {
sl@75
   846
                        //Text field control already in place, just change the text
sl@72
   847
                        MarqueeLabel label = (MarqueeLabel)tableLayoutPanel.Controls[aField.Index];
sl@72
   848
                        label.Text = aField.Text;
sl@72
   849
                        label.TextAlign = aField.Alignment;
sl@68
   850
                    }
sl@75
   851
                    else if (aField.IsBitmap && tableLayoutPanel.Controls[aField.Index] is PictureBox)
sl@75
   852
                    {
sl@75
   853
                        //Bitmap field control already in place just change the bitmap
sl@75
   854
                        PictureBox pictureBox = (PictureBox)tableLayoutPanel.Controls[aField.Index];
sl@75
   855
                        pictureBox.Image = aField.Bitmap;
sl@75
   856
                    }
sl@68
   857
                    else
sl@68
   858
                    {
sl@75
   859
                        //The requested control in our layout it not of the correct type
sl@68
   860
                        //Wrong control type, re-create them all
sl@68
   861
                        UpdateTableLayoutPanel(iCurrentClientData);
sl@68
   862
                    }
sl@30
   863
                }
sl@75
   864
                else
sl@75
   865
                {
sl@75
   866
                    //Different layout, need to rebuild it
sl@75
   867
                    client.Fields[aField.Index] = aField;
sl@75
   868
                    UpdateTableLayoutPanel(iCurrentClientData);
sl@75
   869
                }
sl@75
   870
sl@75
   871
                //
sl@75
   872
                UpdateClientTreeViewNode(client);
sl@30
   873
            }
sl@30
   874
        }
sl@30
   875
sl@30
   876
        /// <summary>
sl@36
   877
        ///
sl@30
   878
        /// </summary>
sl@30
   879
        /// <param name="aTexts"></param>
sl@75
   880
        public void SetClientFieldsThreadSafe(string aSessionId, System.Collections.Generic.IList<DataField> aFields)
sl@30
   881
        {
sl@33
   882
            if (this.InvokeRequired)
sl@30
   883
            {
sl@30
   884
                //Not in the proper thread, invoke ourselves
sl@75
   885
                SetFieldsDelegate d = new SetFieldsDelegate(SetClientFieldsThreadSafe);
sl@72
   886
                this.Invoke(d, new object[] { aSessionId, aFields });
sl@30
   887
            }
sl@30
   888
            else
sl@30
   889
            {
sl@75
   890
                //Put each our text fields in a label control
sl@75
   891
                foreach (DataField field in aFields)
sl@30
   892
                {
sl@75
   893
                    SetClientField(aSessionId, field);
sl@30
   894
                }
sl@30
   895
            }
sl@32
   896
        }
sl@30
   897
sl@67
   898
        /// <summary>
sl@67
   899
        ///
sl@67
   900
        /// </summary>
sl@67
   901
        /// <param name="aSessionId"></param>
sl@32
   902
        /// <param name="aName"></param>
sl@32
   903
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
   904
        {
sl@32
   905
            if (this.InvokeRequired)
sl@32
   906
            {
sl@32
   907
                //Not in the proper thread, invoke ourselves
sl@32
   908
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
sl@32
   909
                this.Invoke(d, new object[] { aSessionId, aName });
sl@32
   910
            }
sl@32
   911
            else
sl@32
   912
            {
sl@32
   913
                //We are in the proper thread
sl@33
   914
                //Get our client
sl@33
   915
                ClientData client = iClients[aSessionId];
sl@33
   916
                if (client != null)
sl@32
   917
                {
sl@33
   918
                    //Set its name
sl@33
   919
                    client.Name = aName;
sl@33
   920
                    //Update our tree-view
sl@33
   921
                    UpdateClientTreeViewNode(client);
sl@33
   922
                }
sl@33
   923
            }
sl@33
   924
        }
sl@33
   925
sl@33
   926
        /// <summary>
sl@36
   927
        ///
sl@33
   928
        /// </summary>
sl@33
   929
        /// <param name="aClient"></param>
sl@33
   930
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
   931
        {
sl@33
   932
            if (aClient == null)
sl@33
   933
            {
sl@33
   934
                return;
sl@33
   935
            }
sl@33
   936
sl@33
   937
            TreeNode node = null;
sl@33
   938
            //Check that our client node already exists
sl@33
   939
            //Get our client root node using its key which is our session ID
sl@33
   940
            TreeNode[] nodes = treeViewClients.Nodes.Find(aClient.SessionId, false);
sl@33
   941
            if (nodes.Count()>0)
sl@33
   942
            {
sl@33
   943
                //We already have a node for that client
sl@33
   944
                node = nodes[0];
sl@33
   945
                //Clear children as we are going to recreate them below
sl@33
   946
                node.Nodes.Clear();
sl@33
   947
            }
sl@33
   948
            else
sl@33
   949
            {
sl@33
   950
                //Client node does not exists create a new one
sl@33
   951
                treeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
sl@33
   952
                node = treeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
   953
            }
sl@33
   954
sl@33
   955
            if (node != null)
sl@33
   956
            {
sl@33
   957
                //Change its name
sl@33
   958
                if (aClient.Name != "")
sl@33
   959
                {
sl@33
   960
                    //We have a name, us it as text for our root node
sl@33
   961
                    node.Text = aClient.Name;
sl@32
   962
                    //Add a child with SessionId
sl@33
   963
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
   964
                }
sl@33
   965
                else
sl@33
   966
                {
sl@33
   967
                    //No name, use session ID instead
sl@33
   968
                    node.Text = aClient.SessionId;
sl@33
   969
                }
sl@36
   970
sl@67
   971
                if (aClient.Fields.Count > 0)
sl@33
   972
                {
sl@33
   973
                    //Create root node for our texts
sl@70
   974
                    TreeNode textsRoot = new TreeNode("Fields");
sl@33
   975
                    node.Nodes.Add(textsRoot);
sl@33
   976
                    //For each text add a new entry
sl@67
   977
                    foreach (DataField field in aClient.Fields)
sl@33
   978
                    {
sl@75
   979
                        if (!field.IsBitmap)
sl@67
   980
                        {
sl@72
   981
                            DataField textField = (DataField)field;
sl@70
   982
                            textsRoot.Nodes.Add(new TreeNode("[Text]" + textField.Text));
sl@67
   983
                        }
sl@67
   984
                        else
sl@67
   985
                        {
sl@72
   986
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap]"));
sl@70
   987
                        }
sl@33
   988
                    }
sl@32
   989
                }
sl@34
   990
sl@34
   991
                node.ExpandAll();
sl@32
   992
            }
sl@30
   993
        }
sl@17
   994
sl@38
   995
        private void buttonAddRow_Click(object sender, EventArgs e)
sl@38
   996
        {
sl@38
   997
            if (tableLayoutPanel.RowCount < 6)
sl@38
   998
            {
sl@62
   999
                UpdateTableLayoutPanel(tableLayoutPanel.ColumnCount, tableLayoutPanel.RowCount + 1);
sl@38
  1000
            }
sl@38
  1001
        }
sl@38
  1002
sl@38
  1003
        private void buttonRemoveRow_Click(object sender, EventArgs e)
sl@38
  1004
        {
sl@38
  1005
            if (tableLayoutPanel.RowCount > 1)
sl@38
  1006
            {
sl@62
  1007
                UpdateTableLayoutPanel(tableLayoutPanel.ColumnCount, tableLayoutPanel.RowCount - 1);
sl@38
  1008
            }
sl@60
  1009
sl@60
  1010
            UpdateTableLayoutRowStyles();
sl@60
  1011
        }
sl@60
  1012
sl@62
  1013
        private void buttonAddColumn_Click(object sender, EventArgs e)
sl@62
  1014
        {
sl@62
  1015
            if (tableLayoutPanel.ColumnCount < 8)
sl@62
  1016
            {
sl@62
  1017
                UpdateTableLayoutPanel(tableLayoutPanel.ColumnCount + 1, tableLayoutPanel.RowCount);
sl@62
  1018
            }
sl@62
  1019
        }
sl@62
  1020
sl@62
  1021
        private void buttonRemoveColumn_Click(object sender, EventArgs e)
sl@62
  1022
        {
sl@62
  1023
            if (tableLayoutPanel.ColumnCount > 1)
sl@62
  1024
            {
sl@62
  1025
                UpdateTableLayoutPanel(tableLayoutPanel.ColumnCount - 1, tableLayoutPanel.RowCount);
sl@62
  1026
            }
sl@62
  1027
        }
sl@62
  1028
sl@62
  1029
sl@60
  1030
        /// <summary>
sl@60
  1031
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  1032
        /// </summary>
sl@60
  1033
        private void UpdateTableLayoutRowStyles()
sl@60
  1034
        {
sl@60
  1035
            foreach (RowStyle rowStyle in tableLayoutPanel.RowStyles)
sl@60
  1036
            {
sl@60
  1037
                rowStyle.SizeType = SizeType.Percent;
sl@60
  1038
                rowStyle.Height = 100 / tableLayoutPanel.RowCount;
sl@60
  1039
            }
sl@60
  1040
        }
sl@60
  1041
sl@70
  1042
        /// DEPRECATED
sl@60
  1043
        /// <summary>
sl@61
  1044
        /// Empty and recreate our table layout with the given number of columns and rows.
sl@61
  1045
        /// Sizes of rows and columns are uniform.
sl@60
  1046
        /// </summary>
sl@60
  1047
        /// <param name="aColumn"></param>
sl@60
  1048
        /// <param name="aRow"></param>
sl@62
  1049
        private void UpdateTableLayoutPanel(int aColumn, int aRow)
sl@60
  1050
        {
sl@61
  1051
            tableLayoutPanel.Controls.Clear();
sl@61
  1052
            tableLayoutPanel.RowStyles.Clear();
sl@61
  1053
            tableLayoutPanel.ColumnStyles.Clear();
sl@61
  1054
            tableLayoutPanel.RowCount = 0;
sl@61
  1055
            tableLayoutPanel.ColumnCount = 0;
sl@60
  1056
sl@61
  1057
            while (tableLayoutPanel.RowCount < aRow)
sl@61
  1058
            {
sl@61
  1059
                tableLayoutPanel.RowCount++;
sl@61
  1060
            }
sl@60
  1061
sl@61
  1062
            while (tableLayoutPanel.ColumnCount < aColumn)
sl@61
  1063
            {
sl@61
  1064
                tableLayoutPanel.ColumnCount++;
sl@61
  1065
            }
sl@61
  1066
sl@61
  1067
            for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
sl@61
  1068
            {
sl@61
  1069
                //Create our column styles
sl@61
  1070
                this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100 / tableLayoutPanel.ColumnCount));
sl@61
  1071
sl@61
  1072
                for (int j = 0; j < tableLayoutPanel.RowCount; j++)
sl@61
  1073
                {
sl@61
  1074
                    if (i == 0)
sl@61
  1075
                    {
sl@61
  1076
                        //Create our row styles
sl@61
  1077
                        this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100 / tableLayoutPanel.RowCount));
sl@61
  1078
                    }
sl@61
  1079
sl@61
  1080
                    MarqueeLabel control = new SharpDisplayManager.MarqueeLabel();
sl@61
  1081
                    control.AutoEllipsis = true;
sl@61
  1082
                    control.AutoSize = true;
sl@61
  1083
                    control.BackColor = System.Drawing.Color.Transparent;
sl@61
  1084
                    control.Dock = System.Windows.Forms.DockStyle.Fill;
sl@61
  1085
                    control.Location = new System.Drawing.Point(1, 1);
sl@61
  1086
                    control.Margin = new System.Windows.Forms.Padding(0);
sl@61
  1087
                    control.Name = "marqueeLabelCol" + aColumn + "Row" + aRow;
sl@61
  1088
                    control.OwnTimer = false;
sl@61
  1089
                    control.PixelsPerSecond = 64;
sl@61
  1090
                    control.Separator = "|";
sl@61
  1091
                    //control.Size = new System.Drawing.Size(254, 30);
sl@61
  1092
                    //control.TabIndex = 2;
sl@61
  1093
                    control.Font = cds.Font;
sl@61
  1094
                    control.Text = "ABCDEFGHIJKLMNOPQRST-0123456789";
sl@61
  1095
                    control.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
sl@61
  1096
                    control.UseCompatibleTextRendering = true;
sl@61
  1097
                    //
sl@61
  1098
                    tableLayoutPanel.Controls.Add(control, i, j);
sl@61
  1099
                }
sl@61
  1100
            }
sl@38
  1101
sl@62
  1102
            CheckFontHeight();
sl@38
  1103
        }
sl@38
  1104
sl@63
  1105
sl@63
  1106
        /// <summary>
sl@63
  1107
        /// Update our display table layout.
sl@63
  1108
        /// </summary>
sl@63
  1109
        /// <param name="aLayout"></param>
sl@65
  1110
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  1111
        {
sl@65
  1112
            TableLayout layout = aClient.Layout;
sl@70
  1113
            int fieldCount = 0;
sl@70
  1114
sl@63
  1115
            tableLayoutPanel.Controls.Clear();
sl@63
  1116
            tableLayoutPanel.RowStyles.Clear();
sl@63
  1117
            tableLayoutPanel.ColumnStyles.Clear();
sl@63
  1118
            tableLayoutPanel.RowCount = 0;
sl@63
  1119
            tableLayoutPanel.ColumnCount = 0;
sl@63
  1120
sl@65
  1121
            while (tableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  1122
            {
sl@63
  1123
                tableLayoutPanel.RowCount++;
sl@63
  1124
            }
sl@63
  1125
sl@65
  1126
            while (tableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  1127
            {
sl@63
  1128
                tableLayoutPanel.ColumnCount++;
sl@63
  1129
            }
sl@63
  1130
sl@63
  1131
            for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
sl@63
  1132
            {
sl@63
  1133
                //Create our column styles
sl@65
  1134
                this.tableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  1135
sl@63
  1136
                for (int j = 0; j < tableLayoutPanel.RowCount; j++)
sl@63
  1137
                {
sl@63
  1138
                    if (i == 0)
sl@63
  1139
                    {
sl@63
  1140
                        //Create our row styles
sl@65
  1141
                        this.tableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  1142
                    }
sl@63
  1143
sl@70
  1144
                    //Check if we already have a control
sl@70
  1145
                    Control existingControl = tableLayoutPanel.GetControlFromPosition(i,j);
sl@70
  1146
                    if (existingControl!=null)
sl@70
  1147
                    {
sl@70
  1148
                        //We already have a control in that cell as a results of row/col spanning
sl@70
  1149
                        //Move on to next cell then
sl@70
  1150
                        continue;
sl@70
  1151
                    }
sl@70
  1152
sl@70
  1153
                    fieldCount++;
sl@70
  1154
sl@69
  1155
                    //Check if a client field already exists for that cell
sl@69
  1156
                    if (aClient.Fields.Count <= tableLayoutPanel.Controls.Count)
sl@65
  1157
                    {
sl@69
  1158
                        //No client field specified, create a text field by default
sl@72
  1159
                        aClient.Fields.Add(new DataField(aClient.Fields.Count));
sl@65
  1160
                    }
sl@68
  1161
sl@69
  1162
                    //Create a control corresponding to the field specified for that cell
sl@70
  1163
                    DataField field = aClient.Fields[tableLayoutPanel.Controls.Count];
sl@70
  1164
                    Control control = CreateControlForDataField(field);
sl@70
  1165
sl@69
  1166
                    //Add newly created control to our table layout at the specified row and column
sl@63
  1167
                    tableLayoutPanel.Controls.Add(control, i, j);
sl@70
  1168
                    //Make sure we specify row and column span for that new control
sl@70
  1169
                    tableLayoutPanel.SetRowSpan(control,field.RowSpan);
sl@70
  1170
                    tableLayoutPanel.SetColumnSpan(control, field.ColumnSpan);
sl@63
  1171
                }
sl@63
  1172
            }
sl@63
  1173
sl@70
  1174
            //
sl@70
  1175
            while (aClient.Fields.Count > fieldCount)
sl@70
  1176
            {
sl@70
  1177
                //We have too much fields for this layout
sl@70
  1178
                //Just discard them until we get there
sl@70
  1179
                aClient.Fields.RemoveAt(aClient.Fields.Count-1);
sl@70
  1180
            }
sl@70
  1181
sl@63
  1182
            CheckFontHeight();
sl@63
  1183
        }
sl@63
  1184
sl@68
  1185
        /// <summary>
sl@70
  1186
        /// Check our type of data field and create corresponding control
sl@68
  1187
        /// </summary>
sl@68
  1188
        /// <param name="aField"></param>
sl@69
  1189
        private Control CreateControlForDataField(DataField aField)
sl@68
  1190
        {
sl@68
  1191
            Control control=null;
sl@75
  1192
            if (!aField.IsBitmap)
sl@68
  1193
            {
sl@68
  1194
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  1195
                label.AutoEllipsis = true;
sl@68
  1196
                label.AutoSize = true;
sl@68
  1197
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  1198
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  1199
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  1200
                label.Margin = new System.Windows.Forms.Padding(0);
sl@68
  1201
                label.Name = "marqueeLabel" + aField.Index;
sl@68
  1202
                label.OwnTimer = false;
sl@68
  1203
                label.PixelsPerSecond = 64;
sl@68
  1204
                label.Separator = "|";
sl@68
  1205
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  1206
                //control.TabIndex = 2;
sl@68
  1207
                label.Font = cds.Font;
sl@68
  1208
sl@68
  1209
                label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
sl@68
  1210
                label.UseCompatibleTextRendering = true;
sl@72
  1211
                label.Text = aField.Text;
sl@68
  1212
                //
sl@68
  1213
                control = label;
sl@68
  1214
            }
sl@72
  1215
            else
sl@68
  1216
            {
sl@68
  1217
                //Create picture box
sl@68
  1218
                PictureBox picture = new PictureBox();
sl@68
  1219
                picture.AutoSize = true;
sl@68
  1220
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  1221
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  1222
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  1223
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  1224
                picture.Name = "pictureBox" + aField;
sl@68
  1225
                //Set our image
sl@72
  1226
                picture.Image = aField.Bitmap;
sl@68
  1227
                //
sl@68
  1228
                control = picture;
sl@68
  1229
            }
sl@68
  1230
sl@69
  1231
            return control;
sl@68
  1232
        }
sl@68
  1233
sl@63
  1234
sl@41
  1235
        private void buttonAlignLeft_Click(object sender, EventArgs e)
sl@41
  1236
        {
sl@60
  1237
            foreach (MarqueeLabel ctrl in tableLayoutPanel.Controls)
sl@60
  1238
            {
sl@60
  1239
                ctrl.TextAlign = ContentAlignment.MiddleLeft;
sl@60
  1240
            }
sl@41
  1241
        }
sl@41
  1242
sl@41
  1243
        private void buttonAlignCenter_Click(object sender, EventArgs e)
sl@41
  1244
        {
sl@60
  1245
            foreach (MarqueeLabel ctrl in tableLayoutPanel.Controls)
sl@60
  1246
            {
sl@60
  1247
                ctrl.TextAlign = ContentAlignment.MiddleCenter;
sl@60
  1248
            }
sl@41
  1249
        }
sl@41
  1250
sl@41
  1251
        private void buttonAlignRight_Click(object sender, EventArgs e)
sl@41
  1252
        {
sl@60
  1253
            foreach (MarqueeLabel ctrl in tableLayoutPanel.Controls)
sl@60
  1254
            {
sl@60
  1255
                ctrl.TextAlign = ContentAlignment.MiddleRight;
sl@60
  1256
            }
sl@41
  1257
        }
sl@36
  1258
sl@46
  1259
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  1260
        {
sl@48
  1261
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  1262
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  1263
            Properties.Settings.Default.Save();
sl@51
  1264
            if (iDisplay.IsOpen())
sl@51
  1265
            {
sl@51
  1266
                OpenDisplayConnection();
sl@51
  1267
            }
sl@51
  1268
            else
sl@51
  1269
            {
sl@51
  1270
                UpdateStatus();
sl@51
  1271
            }
sl@46
  1272
        }
sl@46
  1273
sl@47
  1274
sl@47
  1275
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  1276
        {
sl@47
  1277
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  1278
            {
sl@51
  1279
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  1280
sl@51
  1281
                if (interval > 0)
sl@51
  1282
                {
sl@51
  1283
                    timer.Interval = interval;
sl@51
  1284
                    cds.TimerInterval = timer.Interval;
sl@51
  1285
                    Properties.Settings.Default.Save();
sl@51
  1286
                }
sl@47
  1287
            }
sl@47
  1288
        }
sl@47
  1289
sl@52
  1290
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  1291
        {
sl@52
  1292
            iDisplay.PowerOn();
sl@52
  1293
        }
sl@52
  1294
sl@52
  1295
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  1296
        {
sl@52
  1297
            iDisplay.PowerOff();
sl@52
  1298
        }
sl@52
  1299
sl@53
  1300
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  1301
        {
sl@53
  1302
            iDisplay.ShowClock();
sl@53
  1303
        }
sl@53
  1304
sl@53
  1305
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  1306
        {
sl@53
  1307
            iDisplay.HideClock();
sl@53
  1308
        }
sl@0
  1309
    }
sl@34
  1310
sl@34
  1311
    /// <summary>
sl@34
  1312
    /// A UI thread copy of a client relevant data.
sl@34
  1313
    /// Keeping this copy in the UI thread helps us deal with threading issues.
sl@34
  1314
    /// </summary>
sl@34
  1315
    public class ClientData
sl@34
  1316
    {
sl@55
  1317
        public ClientData(string aSessionId, ICallback aCallback)
sl@34
  1318
        {
sl@34
  1319
            SessionId = aSessionId;
sl@34
  1320
            Name = "";
sl@67
  1321
            Fields = new List<DataField>();
sl@62
  1322
            Layout = new TableLayout(1, 2); //Default to one column and two rows
sl@34
  1323
            Callback = aCallback;
sl@34
  1324
        }
sl@34
  1325
sl@34
  1326
        public string SessionId { get; set; }
sl@34
  1327
        public string Name { get; set; }
sl@67
  1328
        public List<DataField> Fields { get; set; }
sl@62
  1329
        public TableLayout Layout { get; set; }
sl@55
  1330
        public ICallback Callback { get; set; }
sl@34
  1331
    }
sl@0
  1332
}