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