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