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