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