Server/FormMain.cs
author StephaneLenclud
Mon, 05 Sep 2016 18:11:21 +0200
changeset 268 31d4905ad28a
parent 266 b11d7ebbdc7f
child 269 8e32e51a2edd
permissions -rw-r--r--
Now storing encrypted version of Logitech password.
StephaneLenclud@123
     1
´╗┐//
StephaneLenclud@123
     2
// Copyright (C) 2014-2015 St├ęphane Lenclud.
StephaneLenclud@123
     3
//
StephaneLenclud@123
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@123
     5
//
StephaneLenclud@123
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@123
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123
     9
// (at your option) any later version.
StephaneLenclud@123
    10
//
StephaneLenclud@123
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@123
    14
// GNU General Public License for more details.
StephaneLenclud@123
    15
//
StephaneLenclud@123
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@123
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@123
    18
//
StephaneLenclud@123
    19
StephaneLenclud@123
    20
using System;
sl@0
    21
using System.Collections.Generic;
sl@0
    22
using System.ComponentModel;
sl@0
    23
using System.Data;
sl@0
    24
using System.Drawing;
sl@0
    25
using System.Linq;
sl@0
    26
using System.Text;
sl@0
    27
using System.Threading.Tasks;
sl@0
    28
using System.Windows.Forms;
sl@14
    29
using System.IO;
sl@0
    30
using CodeProject.Dialog;
sl@14
    31
using System.Drawing.Imaging;
sl@17
    32
using System.ServiceModel;
sl@25
    33
using System.Threading;
sl@31
    34
using System.Diagnostics;
sl@88
    35
using System.Deployment.Application;
sl@94
    36
using System.Reflection;
StephaneLenclud@112
    37
//NAudio
StephaneLenclud@112
    38
using NAudio.CoreAudioApi;
StephaneLenclud@112
    39
using NAudio.CoreAudioApi.Interfaces;
StephaneLenclud@112
    40
using System.Runtime.InteropServices;
StephaneLenclud@268
    41
using System.Security;
StephaneLenclud@206
    42
using CecSharp;
StephaneLenclud@117
    43
//Network
StephaneLenclud@117
    44
using NETWORKLIST;
sl@25
    45
//
sl@25
    46
using SharpDisplayClient;
sl@55
    47
using SharpDisplay;
StephaneLenclud@135
    48
using MiniDisplayInterop;
StephaneLenclud@171
    49
using SharpLib.Display;
StephaneLenclud@234
    50
using Ear = SharpLib.Ear;
StephaneLenclud@124
    51
sl@0
    52
namespace SharpDisplayManager
sl@0
    53
{
sl@58
    54
    //Types declarations
sl@58
    55
    public delegate uint ColorProcessingDelegate(int aX, int aY, uint aPixel);
StephaneLenclud@223
    56
sl@58
    57
    public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
StephaneLenclud@223
    58
sl@62
    59
    //Delegates are used for our thread safe method
sl@62
    60
    public delegate void AddClientDelegate(string aSessionId, ICallback aCallback);
StephaneLenclud@223
    61
sl@62
    62
    public delegate void RemoveClientDelegate(string aSessionId);
StephaneLenclud@223
    63
sl@79
    64
    public delegate void SetFieldDelegate(string SessionId, DataField aField);
StephaneLenclud@223
    65
sl@79
    66
    public delegate void SetFieldsDelegate(string SessionId, System.Collections.Generic.IList<DataField> aFields);
StephaneLenclud@223
    67
sl@62
    68
    public delegate void SetLayoutDelegate(string SessionId, TableLayout aLayout);
StephaneLenclud@223
    69
sl@62
    70
    public delegate void SetClientNameDelegate(string aSessionId, string aName);
StephaneLenclud@223
    71
StephaneLenclud@184
    72
    public delegate void SetClientPriorityDelegate(string aSessionId, uint aPriority);
StephaneLenclud@223
    73
StephaneLenclud@184
    74
    public delegate void PlainUpdateDelegate();
StephaneLenclud@223
    75
StephaneLenclud@167
    76
    public delegate void WndProcDelegate(ref Message aMessage);
sl@58
    77
sl@58
    78
    /// <summary>
sl@58
    79
    /// Our Display manager main form
sl@58
    80
    /// </summary>
StephaneLenclud@223
    81
    [System.ComponentModel.DesignerCategory("Form")]
StephaneLenclud@226
    82
    public partial class FormMain : FormMainHid, IMMNotificationClient
sl@0
    83
    {
StephaneLenclud@234
    84
        //public Manager iManager = new Manager();        
sl@2
    85
        DateTime LastTickTime;
sl@3
    86
        Display iDisplay;
sl@14
    87
        System.Drawing.Bitmap iBmp;
sl@14
    88
        bool iCreateBitmap; //Workaround render to bitmap issues when minimized
sl@17
    89
        ServiceHost iServiceHost;
sl@65
    90
        // Our collection of clients sorted by session id.
sl@33
    91
        public Dictionary<string, ClientData> iClients;
sl@65
    92
        // The name of the client which informations are currently displayed.
sl@65
    93
        public string iCurrentClientSessionId;
sl@65
    94
        ClientData iCurrentClientData;
sl@65
    95
        //
sl@29
    96
        public bool iClosing;
StephaneLenclud@223
    97
        //
StephaneLenclud@223
    98
        public bool iSkipFrameRendering;
sl@65
    99
        //Function pointer for pixel color filtering
sl@58
   100
        ColorProcessingDelegate iColorFx;
sl@65
   101
        //Function pointer for pixel X coordinate intercept
sl@58
   102
        CoordinateTranslationDelegate iScreenX;
sl@65
   103
        //Function pointer for pixel Y coordinate intercept
sl@58
   104
        CoordinateTranslationDelegate iScreenY;
StephaneLenclud@223
   105
        //NAudio
StephaneLenclud@223
   106
        private MMDeviceEnumerator iMultiMediaDeviceEnumerator;
StephaneLenclud@223
   107
        private MMDevice iMultiMediaDevice;
StephaneLenclud@223
   108
        //Network
StephaneLenclud@223
   109
        private NetworkManager iNetworkManager;
StephaneLenclud@167
   110
StephaneLenclud@167
   111
        /// <summary>
StephaneLenclud@167
   112
        /// CEC - Consumer Electronic Control.
StephaneLenclud@167
   113
        /// Notably used to turn TV on and off as Windows broadcast monitor on and off notifications.
StephaneLenclud@167
   114
        /// </summary>
StephaneLenclud@167
   115
        private ConsumerElectronicControl iCecManager;
StephaneLenclud@223
   116
StephaneLenclud@223
   117
        /// <summary>
StephaneLenclud@223
   118
        /// Manage run when Windows startup option
StephaneLenclud@223
   119
        /// </summary>
StephaneLenclud@223
   120
        private StartupManager iStartupManager;
StephaneLenclud@223
   121
StephaneLenclud@223
   122
        /// <summary>
StephaneLenclud@223
   123
        /// System notification icon used to hide our application from the task bar.
StephaneLenclud@223
   124
        /// </summary>
StephaneLenclud@223
   125
        private SharpLib.Notification.Control iNotifyIcon;
sl@94
   126
StephaneLenclud@167
   127
        /// <summary>
StephaneLenclud@194
   128
        /// System recording notification icon.
StephaneLenclud@179
   129
        /// </summary>
StephaneLenclud@179
   130
        private SharpLib.Notification.Control iRecordingNotification;
StephaneLenclud@179
   131
Stephane@202
   132
        /// <summary>
Stephane@202
   133
        /// 
Stephane@202
   134
        /// </summary>
StephaneLenclud@253
   135
        RichTextBoxTraceListener iWriter;
Stephane@202
   136
StephaneLenclud@179
   137
StephaneLenclud@179
   138
        /// <summary>
StephaneLenclud@167
   139
        /// Allow user to receive window messages;
StephaneLenclud@167
   140
        /// </summary>
StephaneLenclud@167
   141
        public event WndProcDelegate OnWndProc;
StephaneLenclud@167
   142
StephaneLenclud@226
   143
        public FormMain()
sl@0
   144
        {
StephaneLenclud@235
   145
            if (Properties.Settings.Default.EarManager == null)
Stephane@212
   146
            {
Stephane@212
   147
                //No actions in our settings yet
StephaneLenclud@235
   148
                Properties.Settings.Default.EarManager = new EarManager();
Stephane@212
   149
            }
Stephane@212
   150
            else
Stephane@212
   151
            {
StephaneLenclud@235
   152
                // We loaded events and actions from our settings
StephaneLenclud@235
   153
                // Internalizer apparently skips constructor so we need to initialize it here
StephaneLenclud@235
   154
                // Though I reckon that should only be needed when loading an empty EAR manager I guess.
Stephane@243
   155
                Properties.Settings.Default.EarManager.Construct();
Stephane@212
   156
            }
StephaneLenclud@210
   157
            iSkipFrameRendering = false;
StephaneLenclud@223
   158
            iClosing = false;
sl@65
   159
            iCurrentClientSessionId = "";
sl@65
   160
            iCurrentClientData = null;
sl@2
   161
            LastTickTime = DateTime.Now;
StephaneLenclud@223
   162
            //Instantiate our display and register for events notifications
sl@3
   163
            iDisplay = new Display();
StephaneLenclud@223
   164
            iDisplay.OnOpened += OnDisplayOpened;
StephaneLenclud@223
   165
            iDisplay.OnClosed += OnDisplayClosed;
StephaneLenclud@223
   166
            //
StephaneLenclud@223
   167
            iClients = new Dictionary<string, ClientData>();
StephaneLenclud@223
   168
            iStartupManager = new StartupManager();
StephaneLenclud@223
   169
            iNotifyIcon = new SharpLib.Notification.Control();
StephaneLenclud@179
   170
            iRecordingNotification = new SharpLib.Notification.Control();
sl@2
   171
StephaneLenclud@179
   172
            //Have our designer initialize its controls
sl@0
   173
            InitializeComponent();
StephaneLenclud@104
   174
StephaneLenclud@201
   175
            //Redirect console output
StephaneLenclud@253
   176
            iWriter = new RichTextBoxTraceListener(richTextBoxLogs);
StephaneLenclud@253
   177
            Trace.Listeners.Add(iWriter);
StephaneLenclud@201
   178
StephaneLenclud@201
   179
            //Populate device types
StephaneLenclud@201
   180
            PopulateDeviceTypes();
StephaneLenclud@104
   181
StephaneLenclud@223
   182
            //Initial status update 
sl@7
   183
            UpdateStatus();
StephaneLenclud@104
   184
sl@14
   185
            //We have a bug when drawing minimized and reusing our bitmap
StephaneLenclud@158
   186
            //Though I could not reproduce it on Windows 10
StephaneLenclud@223
   187
            iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height,
StephaneLenclud@223
   188
                PixelFormat.Format32bppArgb);
sl@14
   189
            iCreateBitmap = false;
sl@94
   190
StephaneLenclud@223
   191
            //Minimize our window if desired
StephaneLenclud@223
   192
            if (Properties.Settings.Default.StartMinimized)
StephaneLenclud@223
   193
            {
StephaneLenclud@223
   194
                WindowState = FormWindowState.Minimized;
StephaneLenclud@223
   195
            }
sl@94
   196
sl@0
   197
        }
sl@0
   198
StephaneLenclud@223
   199
        /// <summary>
StephaneLenclud@223
   200
        ///
StephaneLenclud@223
   201
        /// </summary>
StephaneLenclud@223
   202
        /// <param name="sender"></param>
StephaneLenclud@223
   203
        /// <param name="e"></param>
StephaneLenclud@106
   204
        private void MainForm_Load(object sender, EventArgs e)
StephaneLenclud@106
   205
        {
StephaneLenclud@223
   206
            //Check if we are running a Click Once deployed application
StephaneLenclud@223
   207
            if (ApplicationDeployment.IsNetworkDeployed)
StephaneLenclud@223
   208
            {
StephaneLenclud@223
   209
                //This is a proper Click Once installation, fetch and show our version number
StephaneLenclud@223
   210
                this.Text += " - v" + ApplicationDeployment.CurrentDeployment.CurrentVersion;
StephaneLenclud@223
   211
            }
StephaneLenclud@223
   212
            else
StephaneLenclud@223
   213
            {
StephaneLenclud@223
   214
                //Not a proper Click Once installation, assuming development build then
StephaneLenclud@223
   215
                this.Text += " - development";
StephaneLenclud@223
   216
            }
StephaneLenclud@223
   217
StephaneLenclud@223
   218
            //NAudio
StephaneLenclud@223
   219
            iMultiMediaDeviceEnumerator = new MMDeviceEnumerator();
StephaneLenclud@223
   220
            iMultiMediaDeviceEnumerator.RegisterEndpointNotificationCallback(this);
StephaneLenclud@223
   221
            UpdateAudioDeviceAndMasterVolumeThreadSafe();
StephaneLenclud@223
   222
StephaneLenclud@223
   223
            //Network
StephaneLenclud@223
   224
            iNetworkManager = new NetworkManager();
StephaneLenclud@223
   225
            iNetworkManager.OnConnectivityChanged += OnConnectivityChanged;
StephaneLenclud@223
   226
            UpdateNetworkStatus();
StephaneLenclud@117
   227
StephaneLenclud@167
   228
            //CEC
StephaneLenclud@167
   229
            iCecManager = new ConsumerElectronicControl();
StephaneLenclud@167
   230
            OnWndProc += iCecManager.OnWndProc;
StephaneLenclud@168
   231
            ResetCec();
StephaneLenclud@168
   232
StephaneLenclud@233
   233
            //Harmony
StephaneLenclud@255
   234
            ResetHarmonyAsync();
StephaneLenclud@233
   235
StephaneLenclud@211
   236
            //Setup Events
StephaneLenclud@260
   237
            PopulateTreeViewEvents();
StephaneLenclud@167
   238
StephaneLenclud@167
   239
            //Setup notification icon
StephaneLenclud@167
   240
            SetupTrayIcon();
StephaneLenclud@106
   241
StephaneLenclud@179
   242
            //Setup recording notification
StephaneLenclud@179
   243
            SetupRecordingNotification();
StephaneLenclud@179
   244
StephaneLenclud@179
   245
            // To make sure start up with minimize to tray works
StephaneLenclud@179
   246
            if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
StephaneLenclud@223
   247
            {
StephaneLenclud@223
   248
                Visible = false;
StephaneLenclud@223
   249
            }
StephaneLenclud@106
   250
StephaneLenclud@106
   251
#if !DEBUG
StephaneLenclud@223
   252
    //When not debugging we want the screen to be empty until a client takes over
StephaneLenclud@106
   253
			ClearLayout();
StephaneLenclud@106
   254
#else
StephaneLenclud@223
   255
            //When developing we want at least one client for testing
StephaneLenclud@223
   256
            StartNewClient("abcdefghijklmnopqrst-0123456789", "ABCDEFGHIJKLMNOPQRST-0123456789");
StephaneLenclud@106
   257
#endif
StephaneLenclud@106
   258
StephaneLenclud@223
   259
            //Open display connection on start-up if needed
StephaneLenclud@223
   260
            if (Properties.Settings.Default.DisplayConnectOnStartup)
StephaneLenclud@223
   261
            {
StephaneLenclud@223
   262
                OpenDisplayConnection();
StephaneLenclud@223
   263
            }
StephaneLenclud@223
   264
StephaneLenclud@223
   265
            //Start our server so that we can get client requests
StephaneLenclud@223
   266
            StartServer();
StephaneLenclud@223
   267
StephaneLenclud@223
   268
            //Register for HID events
StephaneLenclud@223
   269
            RegisterHidDevices();
StephaneLenclud@194
   270
StephaneLenclud@194
   271
            //Start Idle client if needed
StephaneLenclud@194
   272
            if (Properties.Settings.Default.StartIdleClient)
StephaneLenclud@194
   273
            {
StephaneLenclud@194
   274
                StartIdleClient();
StephaneLenclud@194
   275
            }
StephaneLenclud@106
   276
        }
StephaneLenclud@106
   277
StephaneLenclud@223
   278
        /// <summary>
StephaneLenclud@223
   279
        /// Called when our display is opened.
StephaneLenclud@223
   280
        /// </summary>
StephaneLenclud@223
   281
        /// <param name="aDisplay"></param>
StephaneLenclud@223
   282
        private void OnDisplayOpened(Display aDisplay)
StephaneLenclud@223
   283
        {
StephaneLenclud@187
   284
            //Make sure we resume frame rendering
StephaneLenclud@187
   285
            iSkipFrameRendering = false;
StephaneLenclud@122
   286
StephaneLenclud@223
   287
            //Set our screen size now that our display is connected
StephaneLenclud@223
   288
            //Our panelDisplay is the container of our tableLayoutPanel
StephaneLenclud@223
   289
            //tableLayoutPanel will resize itself to fit the client size of our panelDisplay
StephaneLenclud@223
   290
            //panelDisplay needs an extra 2 pixels for borders on each sides
StephaneLenclud@223
   291
            //tableLayoutPanel will eventually be the exact size of our display
StephaneLenclud@223
   292
            Size size = new Size(iDisplay.WidthInPixels() + 2, iDisplay.HeightInPixels() + 2);
StephaneLenclud@223
   293
            panelDisplay.Size = size;
StephaneLenclud@223
   294
StephaneLenclud@223
   295
            //Our display was just opened, update our UI
StephaneLenclud@223
   296
            UpdateStatus();
StephaneLenclud@223
   297
            //Initiate asynchronous request
StephaneLenclud@223
   298
            iDisplay.RequestFirmwareRevision();
StephaneLenclud@223
   299
StephaneLenclud@223
   300
            //Audio
StephaneLenclud@223
   301
            UpdateMasterVolumeThreadSafe();
StephaneLenclud@223
   302
            //Network
StephaneLenclud@223
   303
            UpdateNetworkStatus();
StephaneLenclud@112
   304
StephaneLenclud@108
   305
#if DEBUG
StephaneLenclud@223
   306
            //Testing icon in debug, no arm done if icon not supported
StephaneLenclud@223
   307
            //iDisplay.SetIconStatus(Display.TMiniDisplayIconType.EMiniDisplayIconRecording, 0, 1);
StephaneLenclud@223
   308
            //iDisplay.SetAllIconsStatus(2);
StephaneLenclud@108
   309
#endif
StephaneLenclud@108
   310
StephaneLenclud@223
   311
        }
StephaneLenclud@104
   312
StephaneLenclud@265
   313
StephaneLenclud@265
   314
        private static void AddActionsToTreeNode(TreeNode aParentNode, Ear.Object aObject)
StephaneLenclud@265
   315
        {
StephaneLenclud@265
   316
            foreach (Ear.Action a in aObject.Objects.OfType<Ear.Action>())
StephaneLenclud@265
   317
            {
StephaneLenclud@265
   318
                //Create action node
StephaneLenclud@265
   319
                TreeNode actionNode = aParentNode.Nodes.Add(a.Brief());
StephaneLenclud@265
   320
                actionNode.Tag = a;
StephaneLenclud@265
   321
                //Use color from parent unless our action itself is disabled
StephaneLenclud@265
   322
                actionNode.ForeColor = a.Enabled ? aParentNode.ForeColor : Color.DimGray;
StephaneLenclud@265
   323
                //Go recursive
StephaneLenclud@265
   324
                AddActionsToTreeNode(actionNode,a);
StephaneLenclud@265
   325
            }
StephaneLenclud@265
   326
        }
StephaneLenclud@265
   327
StephaneLenclud@265
   328
StephaneLenclud@265
   329
        /// <summary>
StephaneLenclud@265
   330
        /// 
StephaneLenclud@265
   331
        /// </summary>
StephaneLenclud@265
   332
        /// <param name="aObject"></param>
StephaneLenclud@265
   333
        /// <param name="aNode"></param>
StephaneLenclud@265
   334
        private static TreeNode FindTreeNodeForEarObject(Ear.Object aObject, TreeNode aNode)
StephaneLenclud@265
   335
        {
StephaneLenclud@265
   336
            if (aNode.Tag == aObject)
StephaneLenclud@265
   337
            {
StephaneLenclud@265
   338
                return aNode;
StephaneLenclud@265
   339
            }
StephaneLenclud@265
   340
StephaneLenclud@265
   341
            foreach (TreeNode n in aNode.Nodes)
StephaneLenclud@265
   342
            {
StephaneLenclud@265
   343
                TreeNode found = FindTreeNodeForEarObject(aObject,n);
StephaneLenclud@265
   344
                if (found != null)
StephaneLenclud@265
   345
                {
StephaneLenclud@265
   346
                    return found;
StephaneLenclud@265
   347
                }
StephaneLenclud@265
   348
            }
StephaneLenclud@265
   349
StephaneLenclud@265
   350
            return null;
StephaneLenclud@265
   351
        }
StephaneLenclud@265
   352
StephaneLenclud@265
   353
StephaneLenclud@265
   354
        /// <summary>
StephaneLenclud@265
   355
        /// 
StephaneLenclud@265
   356
        /// </summary>
StephaneLenclud@265
   357
        /// <param name="aObject"></param>
StephaneLenclud@265
   358
        private void SelectEarObject(Ear.Object aObject)
StephaneLenclud@265
   359
        {
StephaneLenclud@265
   360
            foreach (TreeNode n in iTreeViewEvents.Nodes)
StephaneLenclud@265
   361
            {
StephaneLenclud@265
   362
                TreeNode found = FindTreeNodeForEarObject(aObject, n);
StephaneLenclud@265
   363
                if (found != null)
StephaneLenclud@265
   364
                {
StephaneLenclud@265
   365
                    iTreeViewEvents.SelectedNode=found;
StephaneLenclud@265
   366
                    iTreeViewEvents.Focus();
StephaneLenclud@265
   367
                    return;
StephaneLenclud@265
   368
                }
StephaneLenclud@265
   369
            }
StephaneLenclud@265
   370
        }
StephaneLenclud@265
   371
StephaneLenclud@211
   372
        /// <summary>
StephaneLenclud@218
   373
        /// Populate tree view with events and actions
StephaneLenclud@211
   374
        /// </summary>
StephaneLenclud@266
   375
        private void PopulateTreeViewEvents(Ear.Object aSelectedObject=null)
StephaneLenclud@211
   376
        {
StephaneLenclud@211
   377
            //Reset our tree
StephaneLenclud@211
   378
            iTreeViewEvents.Nodes.Clear();
StephaneLenclud@211
   379
            //Populate registered events
StephaneLenclud@235
   380
            foreach (Ear.Event e in Properties.Settings.Default.EarManager.Events)
StephaneLenclud@211
   381
            {
StephaneLenclud@231
   382
                //Create our event node
StephaneLenclud@260
   383
                //Work out the name of our node
StephaneLenclud@260
   384
                string eventNodeName = "";
StephaneLenclud@260
   385
                if (!string.IsNullOrEmpty(e.Name))
StephaneLenclud@260
   386
                {
StephaneLenclud@260
   387
                    //That event has a proper name, use it then
StephaneLenclud@260
   388
                    eventNodeName = $"{e.Name} - {e.Brief()}";
StephaneLenclud@260
   389
                }
StephaneLenclud@260
   390
                else
StephaneLenclud@260
   391
                {
StephaneLenclud@260
   392
                    //Unnamed events just use brief
StephaneLenclud@260
   393
                    eventNodeName = e.Brief();
StephaneLenclud@260
   394
                }
StephaneLenclud@260
   395
                
StephaneLenclud@260
   396
                TreeNode eventNode = iTreeViewEvents.Nodes.Add(eventNodeName);
StephaneLenclud@231
   397
                eventNode.Tag = e; //For easy access to our event
StephaneLenclud@231
   398
                if (!e.Enabled)
StephaneLenclud@231
   399
                {
StephaneLenclud@231
   400
                    //Dim our nodes if disabled
StephaneLenclud@231
   401
                    eventNode.ForeColor = Color.DimGray;
StephaneLenclud@231
   402
                }
StephaneLenclud@231
   403
StephaneLenclud@231
   404
                //Add event description as child node
StephaneLenclud@260
   405
                eventNode.Nodes.Add(e.AttributeDescription).ForeColor = eventNode.ForeColor; 
StephaneLenclud@231
   406
                //Create child node for actions root
StephaneLenclud@265
   407
                TreeNode actionsNode = eventNode.Nodes.Add("Actions");
StephaneLenclud@265
   408
                actionsNode.ForeColor = eventNode.ForeColor;
StephaneLenclud@265
   409
StephaneLenclud@265
   410
                // Recursively add our actions for that event
StephaneLenclud@265
   411
                AddActionsToTreeNode(actionsNode,e);
StephaneLenclud@211
   412
            }
StephaneLenclud@211
   413
StephaneLenclud@214
   414
            iTreeViewEvents.ExpandAll();
StephaneLenclud@231
   415
StephaneLenclud@266
   416
            if (aSelectedObject != null)
StephaneLenclud@266
   417
            {
StephaneLenclud@266
   418
                SelectEarObject(aSelectedObject);
StephaneLenclud@266
   419
            }            
StephaneLenclud@266
   420
StephaneLenclud@266
   421
            // Just to be safe in case the selection did not work
StephaneLenclud@231
   422
            UpdateEventView();
StephaneLenclud@211
   423
        }
StephaneLenclud@211
   424
StephaneLenclud@223
   425
        /// <summary>
StephaneLenclud@223
   426
        /// Called when our display is closed.
StephaneLenclud@223
   427
        /// </summary>
StephaneLenclud@223
   428
        /// <param name="aDisplay"></param>
StephaneLenclud@223
   429
        private void OnDisplayClosed(Display aDisplay)
StephaneLenclud@223
   430
        {
StephaneLenclud@187
   431
            //Our display was just closed, update our UI consequently
StephaneLenclud@187
   432
            UpdateStatus();
StephaneLenclud@223
   433
        }
StephaneLenclud@223
   434
StephaneLenclud@223
   435
        public void OnConnectivityChanged(NetworkManager aNetwork, NLM_CONNECTIVITY newConnectivity)
StephaneLenclud@223
   436
        {
StephaneLenclud@223
   437
            //Update network status
StephaneLenclud@223
   438
            UpdateNetworkStatus();
StephaneLenclud@223
   439
        }
StephaneLenclud@223
   440
StephaneLenclud@223
   441
        /// <summary>
StephaneLenclud@223
   442
        /// Update our Network Status
StephaneLenclud@223
   443
        /// </summary>
StephaneLenclud@223
   444
        private void UpdateNetworkStatus()
StephaneLenclud@223
   445
        {
StephaneLenclud@223
   446
            if (iDisplay.IsOpen())
StephaneLenclud@223
   447
            {
StephaneLenclud@223
   448
                iDisplay.SetIconOnOff(MiniDisplay.IconType.Internet,
StephaneLenclud@223
   449
                    iNetworkManager.NetworkListManager.IsConnectedToInternet);
StephaneLenclud@135
   450
                iDisplay.SetIconOnOff(MiniDisplay.IconType.NetworkSignal, iNetworkManager.NetworkListManager.IsConnected);
StephaneLenclud@223
   451
            }
StephaneLenclud@223
   452
        }
StephaneLenclud@223
   453
StephaneLenclud@223
   454
StephaneLenclud@223
   455
        int iLastNetworkIconIndex = 0;
StephaneLenclud@223
   456
        int iUpdateCountSinceLastNetworkAnimation = 0;
StephaneLenclud@223
   457
StephaneLenclud@223
   458
        /// <summary>
StephaneLenclud@223
   459
        /// 
StephaneLenclud@223
   460
        /// </summary>
StephaneLenclud@223
   461
        private void UpdateNetworkSignal(DateTime aLastTickTime, DateTime aNewTickTime)
StephaneLenclud@223
   462
        {
StephaneLenclud@223
   463
            iUpdateCountSinceLastNetworkAnimation++;
StephaneLenclud@223
   464
            iUpdateCountSinceLastNetworkAnimation = iUpdateCountSinceLastNetworkAnimation%4;
StephaneLenclud@223
   465
StephaneLenclud@223
   466
            if (iDisplay.IsOpen() && iNetworkManager.NetworkListManager.IsConnected &&
StephaneLenclud@223
   467
                iUpdateCountSinceLastNetworkAnimation == 0)
StephaneLenclud@223
   468
            {
StephaneLenclud@135
   469
                int iconCount = iDisplay.IconCount(MiniDisplay.IconType.NetworkSignal);
StephaneLenclud@223
   470
                if (iconCount <= 0)
StephaneLenclud@223
   471
                {
StephaneLenclud@223
   472
                    //Prevents div by zero and other undefined behavior
StephaneLenclud@223
   473
                    return;
StephaneLenclud@223
   474
                }
StephaneLenclud@223
   475
                iLastNetworkIconIndex++;
StephaneLenclud@223
   476
                iLastNetworkIconIndex = iLastNetworkIconIndex%(iconCount*2);
StephaneLenclud@223
   477
                for (int i = 0; i < iconCount; i++)
StephaneLenclud@223
   478
                {
StephaneLenclud@223
   479
                    if (i < iLastNetworkIconIndex && !(i == 0 && iLastNetworkIconIndex > 3) &&
StephaneLenclud@223
   480
                        !(i == 1 && iLastNetworkIconIndex > 4))
StephaneLenclud@223
   481
                    {
StephaneLenclud@135
   482
                        iDisplay.SetIconOn(MiniDisplay.IconType.NetworkSignal, i);
StephaneLenclud@223
   483
                    }
StephaneLenclud@223
   484
                    else
StephaneLenclud@223
   485
                    {
StephaneLenclud@135
   486
                        iDisplay.SetIconOff(MiniDisplay.IconType.NetworkSignal, i);
StephaneLenclud@223
   487
                    }
StephaneLenclud@223
   488
                }
StephaneLenclud@223
   489
            }
StephaneLenclud@223
   490
        }
StephaneLenclud@118
   491
StephaneLenclud@118
   492
StephaneLenclud@118
   493
StephaneLenclud@112
   494
        /// <summary>
StephaneLenclud@112
   495
        /// Receive volume change notification and reflect changes on our slider.
StephaneLenclud@112
   496
        /// </summary>
StephaneLenclud@112
   497
        /// <param name="data"></param>
StephaneLenclud@112
   498
        public void OnVolumeNotificationThreadSafe(AudioVolumeNotificationData data)
StephaneLenclud@112
   499
        {
StephaneLenclud@223
   500
            UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   501
        }
StephaneLenclud@112
   502
StephaneLenclud@112
   503
        /// <summary>
StephaneLenclud@112
   504
        /// Update master volume when user moves our slider.
StephaneLenclud@112
   505
        /// </summary>
StephaneLenclud@112
   506
        /// <param name="sender"></param>
StephaneLenclud@112
   507
        /// <param name="e"></param>
StephaneLenclud@112
   508
        private void trackBarMasterVolume_Scroll(object sender, EventArgs e)
StephaneLenclud@112
   509
        {
StephaneLenclud@223
   510
            //Just like Windows Volume Mixer we unmute if the volume is adjusted
StephaneLenclud@223
   511
            iMultiMediaDevice.AudioEndpointVolume.Mute = false;
StephaneLenclud@223
   512
            //Set volume level according to our volume slider new position
StephaneLenclud@223
   513
            iMultiMediaDevice.AudioEndpointVolume.MasterVolumeLevelScalar = trackBarMasterVolume.Value/100.0f;
StephaneLenclud@112
   514
        }
StephaneLenclud@112
   515
StephaneLenclud@113
   516
StephaneLenclud@223
   517
        /// <summary>
StephaneLenclud@223
   518
        /// Mute check box changed.
StephaneLenclud@223
   519
        /// </summary>
StephaneLenclud@223
   520
        /// <param name="sender"></param>
StephaneLenclud@223
   521
        /// <param name="e"></param>
StephaneLenclud@223
   522
        private void checkBoxMute_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
   523
        {
StephaneLenclud@223
   524
            iMultiMediaDevice.AudioEndpointVolume.Mute = checkBoxMute.Checked;
StephaneLenclud@223
   525
        }
StephaneLenclud@113
   526
StephaneLenclud@112
   527
        /// <summary>
StephaneLenclud@112
   528
        /// Device State Changed
StephaneLenclud@112
   529
        /// </summary>
StephaneLenclud@223
   530
        public void OnDeviceStateChanged([MarshalAs(UnmanagedType.LPWStr)] string deviceId,
StephaneLenclud@223
   531
            [MarshalAs(UnmanagedType.I4)] DeviceState newState)
StephaneLenclud@223
   532
        {
StephaneLenclud@223
   533
        }
StephaneLenclud@112
   534
StephaneLenclud@112
   535
        /// <summary>
StephaneLenclud@112
   536
        /// Device Added
StephaneLenclud@112
   537
        /// </summary>
StephaneLenclud@223
   538
        public void OnDeviceAdded([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId)
StephaneLenclud@223
   539
        {
StephaneLenclud@223
   540
        }
StephaneLenclud@112
   541
StephaneLenclud@112
   542
        /// <summary>
StephaneLenclud@112
   543
        /// Device Removed
StephaneLenclud@112
   544
        /// </summary>
StephaneLenclud@223
   545
        public void OnDeviceRemoved([MarshalAs(UnmanagedType.LPWStr)] string deviceId)
StephaneLenclud@223
   546
        {
StephaneLenclud@223
   547
        }
StephaneLenclud@112
   548
StephaneLenclud@112
   549
        /// <summary>
StephaneLenclud@112
   550
        /// Default Device Changed
StephaneLenclud@112
   551
        /// </summary>
StephaneLenclud@223
   552
        public void OnDefaultDeviceChanged(DataFlow flow, Role role,
StephaneLenclud@223
   553
            [MarshalAs(UnmanagedType.LPWStr)] string defaultDeviceId)
StephaneLenclud@112
   554
        {
StephaneLenclud@112
   555
            if (role == Role.Multimedia && flow == DataFlow.Render)
StephaneLenclud@112
   556
            {
StephaneLenclud@112
   557
                UpdateAudioDeviceAndMasterVolumeThreadSafe();
StephaneLenclud@112
   558
            }
StephaneLenclud@112
   559
        }
StephaneLenclud@112
   560
StephaneLenclud@112
   561
        /// <summary>
StephaneLenclud@112
   562
        /// Property Value Changed
StephaneLenclud@112
   563
        /// </summary>
StephaneLenclud@112
   564
        /// <param name="pwstrDeviceId"></param>
StephaneLenclud@112
   565
        /// <param name="key"></param>
StephaneLenclud@223
   566
        public void OnPropertyValueChanged([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId, PropertyKey key)
StephaneLenclud@223
   567
        {
StephaneLenclud@223
   568
        }
StephaneLenclud@223
   569
StephaneLenclud@223
   570
StephaneLenclud@223
   571
StephaneLenclud@223
   572
StephaneLenclud@223
   573
        /// <summary>
StephaneLenclud@223
   574
        /// Update master volume indicators based our current system states.
StephaneLenclud@223
   575
        /// This typically includes volume levels and mute status.
StephaneLenclud@223
   576
        /// </summary>
StephaneLenclud@223
   577
        private void UpdateMasterVolumeThreadSafe()
StephaneLenclud@223
   578
        {
StephaneLenclud@223
   579
            if (this.InvokeRequired)
StephaneLenclud@223
   580
            {
StephaneLenclud@223
   581
                //Not in the proper thread, invoke ourselves
StephaneLenclud@223
   582
                PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateMasterVolumeThreadSafe);
StephaneLenclud@223
   583
                this.Invoke(d, new object[] {});
StephaneLenclud@223
   584
                return;
StephaneLenclud@223
   585
            }
StephaneLenclud@223
   586
StephaneLenclud@223
   587
            //Update volume slider
StephaneLenclud@223
   588
            float volumeLevelScalar = iMultiMediaDevice.AudioEndpointVolume.MasterVolumeLevelScalar;
StephaneLenclud@223
   589
            trackBarMasterVolume.Value = Convert.ToInt32(volumeLevelScalar*100);
StephaneLenclud@223
   590
            //Update mute checkbox
StephaneLenclud@223
   591
            checkBoxMute.Checked = iMultiMediaDevice.AudioEndpointVolume.Mute;
StephaneLenclud@223
   592
StephaneLenclud@223
   593
            //If our display connection is open we need to update its icons
StephaneLenclud@223
   594
            if (iDisplay.IsOpen())
StephaneLenclud@223
   595
            {
StephaneLenclud@223
   596
                //First take care our our volume level icons
StephaneLenclud@135
   597
                int volumeIconCount = iDisplay.IconCount(MiniDisplay.IconType.Volume);
StephaneLenclud@223
   598
                if (volumeIconCount > 0)
StephaneLenclud@223
   599
                {
StephaneLenclud@223
   600
                    //Compute current volume level from system level and the number of segments in our display volume bar.
StephaneLenclud@223
   601
                    //That tells us how many segments in our volume bar needs to be turned on.
StephaneLenclud@223
   602
                    float currentVolume = volumeLevelScalar*volumeIconCount;
StephaneLenclud@223
   603
                    int segmentOnCount = Convert.ToInt32(currentVolume);
StephaneLenclud@223
   604
                    //Check if our segment count was rounded up, this will later be used for half brightness segment
StephaneLenclud@223
   605
                    bool roundedUp = segmentOnCount > currentVolume;
StephaneLenclud@223
   606
StephaneLenclud@223
   607
                    for (int i = 0; i < volumeIconCount; i++)
StephaneLenclud@223
   608
                    {
StephaneLenclud@223
   609
                        if (i < segmentOnCount)
StephaneLenclud@223
   610
                        {
StephaneLenclud@223
   611
                            //If we are dealing with our last segment and our segment count was rounded up then we will use half brightness.
StephaneLenclud@223
   612
                            if (i == segmentOnCount - 1 && roundedUp)
StephaneLenclud@223
   613
                            {
StephaneLenclud@223
   614
                                //Half brightness
StephaneLenclud@223
   615
                                iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i,
StephaneLenclud@223
   616
                                    (iDisplay.IconStatusCount(MiniDisplay.IconType.Volume) - 1)/2);
StephaneLenclud@223
   617
                            }
StephaneLenclud@223
   618
                            else
StephaneLenclud@223
   619
                            {
StephaneLenclud@223
   620
                                //Full brightness
StephaneLenclud@223
   621
                                iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i,
StephaneLenclud@223
   622
                                    iDisplay.IconStatusCount(MiniDisplay.IconType.Volume) - 1);
StephaneLenclud@223
   623
                            }
StephaneLenclud@223
   624
                        }
StephaneLenclud@223
   625
                        else
StephaneLenclud@223
   626
                        {
StephaneLenclud@135
   627
                            iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i, 0);
StephaneLenclud@223
   628
                        }
StephaneLenclud@223
   629
                    }
StephaneLenclud@223
   630
                }
StephaneLenclud@223
   631
StephaneLenclud@223
   632
                //Take care of our mute icon
StephaneLenclud@135
   633
                iDisplay.SetIconOnOff(MiniDisplay.IconType.Mute, iMultiMediaDevice.AudioEndpointVolume.Mute);
StephaneLenclud@223
   634
            }
StephaneLenclud@223
   635
StephaneLenclud@223
   636
        }
StephaneLenclud@112
   637
StephaneLenclud@112
   638
        /// <summary>
StephaneLenclud@112
   639
        /// 
StephaneLenclud@112
   640
        /// </summary>
StephaneLenclud@112
   641
        private void UpdateAudioDeviceAndMasterVolumeThreadSafe()
StephaneLenclud@112
   642
        {
StephaneLenclud@112
   643
            if (this.InvokeRequired)
StephaneLenclud@112
   644
            {
StephaneLenclud@112
   645
                //Not in the proper thread, invoke ourselves
StephaneLenclud@223
   646
                PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateAudioDeviceAndMasterVolumeThreadSafe);
StephaneLenclud@223
   647
                this.Invoke(d, new object[] {});
StephaneLenclud@112
   648
                return;
StephaneLenclud@112
   649
            }
StephaneLenclud@223
   650
StephaneLenclud@112
   651
            //We are in the correct thread just go ahead.
StephaneLenclud@112
   652
            try
StephaneLenclud@223
   653
            {
StephaneLenclud@112
   654
                //Get our master volume            
StephaneLenclud@223
   655
                iMultiMediaDevice = iMultiMediaDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
StephaneLenclud@223
   656
                //Update our label
StephaneLenclud@223
   657
                labelDefaultAudioDevice.Text = iMultiMediaDevice.FriendlyName;
StephaneLenclud@116
   658
StephaneLenclud@112
   659
                //Show our volume in our track bar
StephaneLenclud@223
   660
                UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   661
StephaneLenclud@112
   662
                //Register to get volume modifications
StephaneLenclud@223
   663
                iMultiMediaDevice.AudioEndpointVolume.OnVolumeNotification += OnVolumeNotificationThreadSafe;
StephaneLenclud@112
   664
                //
StephaneLenclud@223
   665
                trackBarMasterVolume.Enabled = true;
StephaneLenclud@112
   666
            }
StephaneLenclud@112
   667
            catch (Exception ex)
StephaneLenclud@112
   668
            {
StephaneLenclud@112
   669
                Debug.WriteLine("Exception thrown in UpdateAudioDeviceAndMasterVolume");
StephaneLenclud@112
   670
                Debug.WriteLine(ex.ToString());
StephaneLenclud@112
   671
                //Something went wrong S/PDIF device ca throw exception I guess
StephaneLenclud@223
   672
                trackBarMasterVolume.Enabled = false;
StephaneLenclud@112
   673
            }
StephaneLenclud@112
   674
        }
StephaneLenclud@104
   675
StephaneLenclud@223
   676
        /// <summary>
StephaneLenclud@223
   677
        /// 
StephaneLenclud@223
   678
        /// </summary>
StephaneLenclud@223
   679
        private void PopulateDeviceTypes()
StephaneLenclud@223
   680
        {
StephaneLenclud@223
   681
            int count = Display.TypeCount();
StephaneLenclud@223
   682
StephaneLenclud@223
   683
            for (int i = 0; i < count; i++)
StephaneLenclud@223
   684
            {
StephaneLenclud@223
   685
                comboBoxDisplayType.Items.Add(Display.TypeName((MiniDisplay.Type) i));
StephaneLenclud@223
   686
            }
StephaneLenclud@223
   687
        }
StephaneLenclud@104
   688
StephaneLenclud@152
   689
        /// <summary>
StephaneLenclud@223
   690
        ///
StephaneLenclud@223
   691
        /// </summary>
StephaneLenclud@223
   692
        private void SetupTrayIcon()
StephaneLenclud@223
   693
        {
StephaneLenclud@223
   694
            iNotifyIcon.Icon = GetIcon("vfd.ico");
StephaneLenclud@223
   695
            iNotifyIcon.Text = "Sharp Display Manager";
StephaneLenclud@223
   696
            iNotifyIcon.Visible = true;
StephaneLenclud@223
   697
StephaneLenclud@223
   698
            //Double click toggles visibility - typically brings up the application
StephaneLenclud@223
   699
            iNotifyIcon.DoubleClick += delegate(object obj, EventArgs args)
StephaneLenclud@223
   700
            {
StephaneLenclud@223
   701
                SysTrayHideShow();
StephaneLenclud@223
   702
            };
StephaneLenclud@223
   703
StephaneLenclud@223
   704
            //Adding a context menu, useful to be able to exit the application
StephaneLenclud@223
   705
            ContextMenu contextMenu = new ContextMenu();
StephaneLenclud@223
   706
            //Context menu item to toggle visibility
StephaneLenclud@223
   707
            MenuItem hideShowItem = new MenuItem("Hide/Show");
StephaneLenclud@223
   708
            hideShowItem.Click += delegate(object obj, EventArgs args)
StephaneLenclud@223
   709
            {
StephaneLenclud@223
   710
                SysTrayHideShow();
StephaneLenclud@223
   711
            };
StephaneLenclud@223
   712
            contextMenu.MenuItems.Add(hideShowItem);
StephaneLenclud@223
   713
StephaneLenclud@223
   714
            //Context menu item separator
StephaneLenclud@223
   715
            contextMenu.MenuItems.Add(new MenuItem("-"));
StephaneLenclud@223
   716
StephaneLenclud@223
   717
            //Context menu exit item
StephaneLenclud@223
   718
            MenuItem exitItem = new MenuItem("Exit");
StephaneLenclud@223
   719
            exitItem.Click += delegate(object obj, EventArgs args)
StephaneLenclud@223
   720
            {
StephaneLenclud@223
   721
                Application.Exit();
StephaneLenclud@223
   722
            };
StephaneLenclud@223
   723
            contextMenu.MenuItems.Add(exitItem);
StephaneLenclud@223
   724
StephaneLenclud@223
   725
            iNotifyIcon.ContextMenu = contextMenu;
StephaneLenclud@223
   726
        }
sl@95
   727
StephaneLenclud@178
   728
        /// <summary>
StephaneLenclud@179
   729
        ///
StephaneLenclud@179
   730
        /// </summary>
StephaneLenclud@179
   731
        private void SetupRecordingNotification()
StephaneLenclud@179
   732
        {
StephaneLenclud@179
   733
            iRecordingNotification.Icon = GetIcon("record.ico");
StephaneLenclud@179
   734
            iRecordingNotification.Text = "No recording";
StephaneLenclud@180
   735
            iRecordingNotification.Visible = false;
StephaneLenclud@179
   736
        }
StephaneLenclud@179
   737
StephaneLenclud@179
   738
        /// <summary>
StephaneLenclud@178
   739
        /// Access icons from embedded resources.
StephaneLenclud@178
   740
        /// </summary>
StephaneLenclud@178
   741
        /// <param name="aName"></param>
StephaneLenclud@178
   742
        /// <returns></returns>
StephaneLenclud@178
   743
        public static Icon GetIcon(string aName)
StephaneLenclud@223
   744
        {
StephaneLenclud@223
   745
            string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
StephaneLenclud@223
   746
            foreach (string name in names)
StephaneLenclud@223
   747
            {
StephaneLenclud@178
   748
                //Find a resource name that ends with the given name
StephaneLenclud@223
   749
                if (name.EndsWith(aName))
StephaneLenclud@223
   750
                {
StephaneLenclud@223
   751
                    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
StephaneLenclud@223
   752
                    {
StephaneLenclud@223
   753
                        return new Icon(stream);
StephaneLenclud@223
   754
                    }
StephaneLenclud@223
   755
                }
StephaneLenclud@223
   756
            }
StephaneLenclud@223
   757
StephaneLenclud@223
   758
            return null;
StephaneLenclud@223
   759
        }
sl@94
   760
sl@94
   761
sl@65
   762
        /// <summary>
sl@65
   763
        /// Set our current client.
sl@65
   764
        /// This will take care of applying our client layout and set data fields.
sl@65
   765
        /// </summary>
sl@65
   766
        /// <param name="aSessionId"></param>
StephaneLenclud@223
   767
        void SetCurrentClient(string aSessionId, bool aForce = false)
sl@57
   768
        {
sl@65
   769
            if (aSessionId == iCurrentClientSessionId)
sl@57
   770
            {
sl@65
   771
                //Given client is already the current one.
sl@65
   772
                //Don't bother changing anything then.
sl@65
   773
                return;
sl@65
   774
            }
sl@57
   775
StephaneLenclud@185
   776
            ClientData requestedClientData = iClients[aSessionId];
StephaneLenclud@141
   777
StephaneLenclud@141
   778
            //Check when was the last time we switched to that client
StephaneLenclud@142
   779
            if (iCurrentClientData != null)
StephaneLenclud@141
   780
            {
StephaneLenclud@185
   781
                //Do not switch client if priority of current client is higher 
StephaneLenclud@185
   782
                if (!aForce && requestedClientData.Priority < iCurrentClientData.Priority)
StephaneLenclud@185
   783
                {
StephaneLenclud@185
   784
                    return;
StephaneLenclud@185
   785
                }
StephaneLenclud@185
   786
StephaneLenclud@185
   787
StephaneLenclud@142
   788
                double lastSwitchToClientSecondsAgo = (DateTime.Now - iCurrentClientData.LastSwitchTime).TotalSeconds;
StephaneLenclud@142
   789
                //TODO: put that hard coded value as a client property
StephaneLenclud@142
   790
                //Clients should be able to define how often they can be interrupted
StephaneLenclud@142
   791
                //Thus a background client can set this to zero allowing any other client to interrupt at any time
StephaneLenclud@142
   792
                //We could also compute this delay by looking at the requests frequencies?
StephaneLenclud@185
   793
                if (!aForce &&
StephaneLenclud@223
   794
                    requestedClientData.Priority == iCurrentClientData.Priority &&
StephaneLenclud@223
   795
                    //Time sharing is only if clients have the same priority
StephaneLenclud@185
   796
                    (lastSwitchToClientSecondsAgo < 30)) //Make sure a client is on for at least 30 seconds
StephaneLenclud@142
   797
                {
StephaneLenclud@142
   798
                    //Don't switch clients too often
StephaneLenclud@142
   799
                    return;
StephaneLenclud@142
   800
                }
StephaneLenclud@141
   801
            }
StephaneLenclud@141
   802
sl@65
   803
            //Set current client ID.
sl@65
   804
            iCurrentClientSessionId = aSessionId;
StephaneLenclud@141
   805
            //Set the time we last switched to that client
StephaneLenclud@141
   806
            iClients[aSessionId].LastSwitchTime = DateTime.Now;
sl@65
   807
            //Fetch and set current client data.
StephaneLenclud@185
   808
            iCurrentClientData = requestedClientData;
sl@65
   809
            //Apply layout and set data fields.
sl@65
   810
            UpdateTableLayoutPanel(iCurrentClientData);
sl@57
   811
        }
sl@57
   812
sl@0
   813
        private void buttonFont_Click(object sender, EventArgs e)
sl@0
   814
        {
sl@0
   815
            //fontDialog.ShowColor = true;
sl@0
   816
            //fontDialog.ShowApply = true;
sl@0
   817
            fontDialog.ShowEffects = true;
sl@99
   818
            fontDialog.Font = cds.Font;
sl@28
   819
sl@28
   820
            fontDialog.FixedPitchOnly = checkBoxFixedPitchFontOnly.Checked;
sl@28
   821
sl@0
   822
            //fontDialog.ShowHelp = true;
sl@0
   823
sl@0
   824
            //fontDlg.MaxSize = 40;
sl@0
   825
            //fontDlg.MinSize = 22;
sl@0
   826
sl@0
   827
            //fontDialog.Parent = this;
sl@0
   828
            //fontDialog.StartPosition = FormStartPosition.CenterParent;
sl@0
   829
sl@0
   830
            //DlgBox.ShowDialog(fontDialog);
sl@0
   831
sl@0
   832
            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
sl@0
   833
            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
sl@0
   834
            {
sl@99
   835
                //Set the fonts to all our labels in our layout
StephaneLenclud@175
   836
                foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@99
   837
                {
sl@99
   838
                    if (ctrl is MarqueeLabel)
sl@99
   839
                    {
StephaneLenclud@223
   840
                        ((MarqueeLabel) ctrl).Font = fontDialog.Font;
sl@99
   841
                    }
sl@99
   842
                }
sl@0
   843
sl@99
   844
                //Save font settings
sl@48
   845
                cds.Font = fontDialog.Font;
sl@8
   846
                Properties.Settings.Default.Save();
sl@36
   847
                //
sl@37
   848
                CheckFontHeight();
sl@37
   849
            }
sl@37
   850
        }
sl@36
   851
sl@37
   852
        /// <summary>
sl@38
   853
        ///
sl@37
   854
        /// </summary>
sl@37
   855
        void CheckFontHeight()
sl@37
   856
        {
sl@54
   857
            //Show font height and width
sl@54
   858
            labelFontHeight.Text = "Font height: " + cds.Font.Height;
sl@54
   859
            float charWidth = IsFixedWidth(cds.Font);
sl@54
   860
            if (charWidth == 0.0f)
sl@54
   861
            {
sl@54
   862
                labelFontWidth.Visible = false;
sl@54
   863
            }
sl@54
   864
            else
sl@54
   865
            {
sl@54
   866
                labelFontWidth.Visible = true;
sl@54
   867
                labelFontWidth.Text = "Font width: " + charWidth;
sl@54
   868
            }
sl@54
   869
sl@70
   870
            MarqueeLabel label = null;
sl@68
   871
            //Get the first label control we can find
StephaneLenclud@175
   872
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@68
   873
            {
sl@68
   874
                if (ctrl is MarqueeLabel)
sl@68
   875
                {
StephaneLenclud@223
   876
                    label = (MarqueeLabel) ctrl;
sl@68
   877
                    break;
sl@68
   878
                }
sl@68
   879
            }
sl@68
   880
sl@54
   881
            //Now check font height and show a warning if needed.
sl@68
   882
            if (label != null && label.Font.Height > label.Height)
sl@37
   883
            {
StephaneLenclud@223
   884
                labelWarning.Text = "WARNING: Selected font is too height by " + (label.Font.Height - label.Height) +
StephaneLenclud@223
   885
                                    " pixels!";
sl@37
   886
                labelWarning.Visible = true;
sl@0
   887
            }
sl@37
   888
            else
sl@37
   889
            {
sl@37
   890
                labelWarning.Visible = false;
sl@37
   891
            }
sl@37
   892
sl@0
   893
        }
sl@0
   894
sl@0
   895
        private void buttonCapture_Click(object sender, EventArgs e)
sl@0
   896
        {
StephaneLenclud@175
   897
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height);
StephaneLenclud@175
   898
            iTableLayoutPanel.DrawToBitmap(bmp, iTableLayoutPanel.ClientRectangle);
sl@14
   899
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
   900
            bmp.Save("D:\\capture.png");
sl@14
   901
StephaneLenclud@223
   902
            ((MarqueeLabel) iTableLayoutPanel.Controls[0]).Text = "Captured";
sl@17
   903
sl@14
   904
            /*
sl@14
   905
            string outputFileName = "d:\\capture.png";
sl@14
   906
            using (MemoryStream memory = new MemoryStream())
sl@14
   907
            {
sl@14
   908
                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
sl@14
   909
                {
sl@14
   910
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
sl@14
   911
                    byte[] bytes = memory.ToArray();
sl@14
   912
                    fs.Write(bytes, 0, bytes.Length);
sl@14
   913
                }
sl@14
   914
            }
sl@14
   915
             */
sl@14
   916
sl@0
   917
        }
sl@2
   918
sl@12
   919
        private void CheckForRequestResults()
sl@12
   920
        {
sl@12
   921
            if (iDisplay.IsRequestPending())
sl@12
   922
            {
sl@12
   923
                switch (iDisplay.AttemptRequestCompletion())
sl@12
   924
                {
StephaneLenclud@135
   925
                    case MiniDisplay.Request.FirmwareRevision:
sl@51
   926
                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
sl@51
   927
                        //Issue next request then
sl@51
   928
                        iDisplay.RequestPowerSupplyStatus();
sl@51
   929
                        break;
sl@51
   930
StephaneLenclud@135
   931
                    case MiniDisplay.Request.PowerSupplyStatus:
sl@12
   932
                        if (iDisplay.PowerSupplyStatus())
sl@12
   933
                        {
sl@12
   934
                            toolStripStatusLabelPower.Text = "ON";
sl@12
   935
                        }
sl@12
   936
                        else
sl@12
   937
                        {
sl@12
   938
                            toolStripStatusLabelPower.Text = "OFF";
sl@12
   939
                        }
sl@12
   940
                        //Issue next request then
sl@12
   941
                        iDisplay.RequestDeviceId();
sl@12
   942
                        break;
sl@12
   943
StephaneLenclud@135
   944
                    case MiniDisplay.Request.DeviceId:
sl@12
   945
                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
sl@12
   946
                        //No more request to issue
sl@12
   947
                        break;
sl@12
   948
                }
sl@12
   949
            }
sl@12
   950
        }
sl@12
   951
sl@58
   952
        public static uint ColorWhiteIsOn(int aX, int aY, uint aPixel)
sl@58
   953
        {
sl@58
   954
            if ((aPixel & 0x00FFFFFF) == 0x00FFFFFF)
sl@58
   955
            {
sl@58
   956
                return 0xFFFFFFFF;
sl@58
   957
            }
sl@58
   958
            return 0x00000000;
sl@58
   959
        }
sl@16
   960
sl@58
   961
        public static uint ColorUntouched(int aX, int aY, uint aPixel)
sl@57
   962
        {
sl@57
   963
            return aPixel;
sl@57
   964
        }
sl@57
   965
sl@58
   966
        public static uint ColorInversed(int aX, int aY, uint aPixel)
sl@57
   967
        {
sl@57
   968
            return ~aPixel;
sl@57
   969
        }
sl@57
   970
sl@58
   971
        public static uint ColorChessboard(int aX, int aY, uint aPixel)
sl@58
   972
        {
StephaneLenclud@223
   973
            if ((aX%2 == 0) && (aY%2 == 0))
sl@58
   974
            {
sl@58
   975
                return ~aPixel;
sl@58
   976
            }
StephaneLenclud@223
   977
            else if ((aX%2 != 0) && (aY%2 != 0))
sl@58
   978
            {
sl@58
   979
                return ~aPixel;
sl@58
   980
            }
sl@58
   981
            return 0x00000000;
sl@58
   982
        }
sl@58
   983
sl@16
   984
sl@16
   985
        public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
sl@16
   986
        {
sl@16
   987
            return aBmp.Width - aX - 1;
sl@16
   988
        }
sl@16
   989
sl@16
   990
        public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
sl@16
   991
        {
sl@16
   992
            return iBmp.Height - aY - 1;
sl@16
   993
        }
sl@16
   994
sl@16
   995
        public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
sl@16
   996
        {
sl@16
   997
            return aX;
sl@16
   998
        }
sl@16
   999
sl@16
  1000
        public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
sl@16
  1001
        {
sl@16
  1002
            return aY;
sl@16
  1003
        }
sl@16
  1004
sl@58
  1005
        /// <summary>
sl@58
  1006
        /// Select proper pixel delegates according to our current settings.
sl@58
  1007
        /// </summary>
sl@58
  1008
        private void SetupPixelDelegates()
sl@58
  1009
        {
sl@59
  1010
            //Select our pixel processing routine
sl@58
  1011
            if (cds.InverseColors)
sl@58
  1012
            {
sl@58
  1013
                //iColorFx = ColorChessboard;
sl@58
  1014
                iColorFx = ColorInversed;
sl@58
  1015
            }
sl@58
  1016
            else
sl@58
  1017
            {
sl@58
  1018
                iColorFx = ColorWhiteIsOn;
sl@58
  1019
            }
sl@58
  1020
sl@58
  1021
            //Select proper coordinate translation functions
sl@58
  1022
            //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
sl@58
  1023
            if (cds.ReverseScreen)
sl@58
  1024
            {
sl@58
  1025
                iScreenX = ScreenReversedX;
sl@58
  1026
                iScreenY = ScreenReversedY;
sl@58
  1027
            }
sl@58
  1028
            else
sl@58
  1029
            {
sl@58
  1030
                iScreenX = ScreenX;
sl@58
  1031
                iScreenY = ScreenY;
sl@58
  1032
            }
sl@58
  1033
sl@58
  1034
        }
sl@16
  1035
sl@16
  1036
        //This is our timer tick responsible to perform our render
sl@2
  1037
        private void timer_Tick(object sender, EventArgs e)
sl@14
  1038
        {
sl@2
  1039
            //Update our animations
sl@2
  1040
            DateTime NewTickTime = DateTime.Now;
sl@2
  1041
StephaneLenclud@223
  1042
            UpdateNetworkSignal(LastTickTime, NewTickTime);
StephaneLenclud@118
  1043
sl@60
  1044
            //Update animation for all our marquees
StephaneLenclud@175
  1045
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@60
  1046
            {
sl@68
  1047
                if (ctrl is MarqueeLabel)
sl@68
  1048
                {
StephaneLenclud@223
  1049
                    ((MarqueeLabel) ctrl).UpdateAnimation(LastTickTime, NewTickTime);
sl@68
  1050
                }
sl@60
  1051
            }
sl@60
  1052
sl@4
  1053
            //Update our display
sl@4
  1054
            if (iDisplay.IsOpen())
sl@4
  1055
            {
sl@12
  1056
                CheckForRequestResults();
sl@12
  1057
StephaneLenclud@223
  1058
                //Check if frame rendering is needed
StephaneLenclud@223
  1059
                //Typically used when showing clock
StephaneLenclud@223
  1060
                if (!iSkipFrameRendering)
StephaneLenclud@223
  1061
                {
StephaneLenclud@223
  1062
                    //Draw to bitmap
StephaneLenclud@223
  1063
                    if (iCreateBitmap)
StephaneLenclud@223
  1064
                    {
StephaneLenclud@223
  1065
                        iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height,
StephaneLenclud@223
  1066
                            PixelFormat.Format32bppArgb);
StephaneLenclud@158
  1067
                        iCreateBitmap = false;
StephaneLenclud@158
  1068
                    }
StephaneLenclud@223
  1069
                    iTableLayoutPanel.DrawToBitmap(iBmp, iTableLayoutPanel.ClientRectangle);
StephaneLenclud@223
  1070
                    //iBmp.Save("D:\\capture.png");
StephaneLenclud@223
  1071
StephaneLenclud@223
  1072
                    //Send it to our display
StephaneLenclud@223
  1073
                    for (int i = 0; i < iBmp.Width; i++)
StephaneLenclud@223
  1074
                    {
StephaneLenclud@223
  1075
                        for (int j = 0; j < iBmp.Height; j++)
StephaneLenclud@223
  1076
                        {
StephaneLenclud@223
  1077
                            unchecked
StephaneLenclud@223
  1078
                            {
StephaneLenclud@223
  1079
                                //Get our processed pixel coordinates
StephaneLenclud@223
  1080
                                int x = iScreenX(iBmp, i);
StephaneLenclud@223
  1081
                                int y = iScreenY(iBmp, j);
StephaneLenclud@223
  1082
                                //Get pixel color
StephaneLenclud@223
  1083
                                uint color = (uint) iBmp.GetPixel(i, j).ToArgb();
StephaneLenclud@223
  1084
                                //Apply color effects
StephaneLenclud@223
  1085
                                color = iColorFx(x, y, color);
StephaneLenclud@223
  1086
                                //Now set our pixel
StephaneLenclud@223
  1087
                                iDisplay.SetPixel(x, y, color);
StephaneLenclud@223
  1088
                            }
StephaneLenclud@223
  1089
                        }
StephaneLenclud@223
  1090
                    }
StephaneLenclud@223
  1091
StephaneLenclud@223
  1092
                    iDisplay.SwapBuffers();
StephaneLenclud@223
  1093
                }
sl@4
  1094
            }
sl@8
  1095
sl@8
  1096
            //Compute instant FPS
StephaneLenclud@223
  1097
            toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " / " +
StephaneLenclud@223
  1098
                                           (1000/timer.Interval).ToString() + " FPS";
sl@8
  1099
sl@8
  1100
            LastTickTime = NewTickTime;
sl@8
  1101
sl@2
  1102
        }
sl@3
  1103
StephaneLenclud@223
  1104
        /// <summary>
StephaneLenclud@223
  1105
        /// Attempt to establish connection with our display hardware.
StephaneLenclud@223
  1106
        /// </summary>
sl@46
  1107
        private void OpenDisplayConnection()
sl@3
  1108
        {
sl@46
  1109
            CloseDisplayConnection();
sl@46
  1110
StephaneLenclud@223
  1111
            if (!iDisplay.Open((MiniDisplay.Type) cds.DisplayType))
StephaneLenclud@223
  1112
            {
StephaneLenclud@223
  1113
                UpdateStatus();
StephaneLenclud@223
  1114
                toolStripStatusLabelConnect.Text = "Connection error";
sl@7
  1115
            }
sl@46
  1116
        }
sl@7
  1117
sl@46
  1118
        private void CloseDisplayConnection()
sl@46
  1119
        {
StephaneLenclud@223
  1120
            //Status will be updated upon receiving the closed event
StephaneLenclud@223
  1121
StephaneLenclud@223
  1122
            if (iDisplay == null || !iDisplay.IsOpen())
StephaneLenclud@223
  1123
            {
StephaneLenclud@223
  1124
                return;
StephaneLenclud@223
  1125
            }
StephaneLenclud@223
  1126
StephaneLenclud@223
  1127
            //Do not clear if we gave up on rendering already.
StephaneLenclud@223
  1128
            //This means we will keep on displaying clock on MDM166AA for instance.
StephaneLenclud@223
  1129
            if (!iSkipFrameRendering)
StephaneLenclud@223
  1130
            {
StephaneLenclud@223
  1131
                iDisplay.Clear();
StephaneLenclud@223
  1132
                iDisplay.SwapBuffers();
StephaneLenclud@223
  1133
            }
StephaneLenclud@223
  1134
StephaneLenclud@223
  1135
            iDisplay.SetAllIconsStatus(0); //Turn off all icons
sl@46
  1136
            iDisplay.Close();
sl@46
  1137
        }
sl@46
  1138
sl@46
  1139
        private void buttonOpen_Click(object sender, EventArgs e)
sl@46
  1140
        {
sl@46
  1141
            OpenDisplayConnection();
sl@3
  1142
        }
sl@3
  1143
sl@3
  1144
        private void buttonClose_Click(object sender, EventArgs e)
sl@3
  1145
        {
sl@46
  1146
            CloseDisplayConnection();
sl@3
  1147
        }
sl@3
  1148
sl@3
  1149
        private void buttonClear_Click(object sender, EventArgs e)
sl@3
  1150
        {
sl@3
  1151
            iDisplay.Clear();
sl@3
  1152
            iDisplay.SwapBuffers();
sl@3
  1153
        }
sl@3
  1154
sl@3
  1155
        private void buttonFill_Click(object sender, EventArgs e)
sl@3
  1156
        {
sl@3
  1157
            iDisplay.Fill();
sl@3
  1158
            iDisplay.SwapBuffers();
sl@3
  1159
        }
sl@3
  1160
sl@3
  1161
        private void trackBarBrightness_Scroll(object sender, EventArgs e)
sl@3
  1162
        {
sl@48
  1163
            cds.Brightness = trackBarBrightness.Value;
sl@9
  1164
            Properties.Settings.Default.Save();
sl@3
  1165
            iDisplay.SetBrightness(trackBarBrightness.Value);
sl@9
  1166
sl@3
  1167
        }
sl@7
  1168
sl@48
  1169
sl@48
  1170
        /// <summary>
sl@48
  1171
        /// CDS stands for Current Display Settings
sl@48
  1172
        /// </summary>
sl@50
  1173
        private DisplaySettings cds
sl@48
  1174
        {
sl@48
  1175
            get
sl@48
  1176
            {
sl@65
  1177
                DisplaysSettings settings = Properties.Settings.Default.DisplaysSettings;
sl@51
  1178
                if (settings == null)
sl@51
  1179
                {
sl@51
  1180
                    settings = new DisplaysSettings();
sl@51
  1181
                    settings.Init();
sl@65
  1182
                    Properties.Settings.Default.DisplaysSettings = settings;
sl@51
  1183
                }
sl@48
  1184
sl@48
  1185
                //Make sure all our settings have been created
sl@48
  1186
                while (settings.Displays.Count <= Properties.Settings.Default.CurrentDisplayIndex)
sl@48
  1187
                {
sl@50
  1188
                    settings.Displays.Add(new DisplaySettings());
sl@48
  1189
                }
sl@48
  1190
sl@50
  1191
                DisplaySettings displaySettings = settings.Displays[Properties.Settings.Default.CurrentDisplayIndex];
sl@48
  1192
                return displaySettings;
sl@48
  1193
            }
sl@48
  1194
        }
sl@48
  1195
sl@54
  1196
        /// <summary>
sl@54
  1197
        /// Check if the given font has a fixed character pitch.
sl@54
  1198
        /// </summary>
sl@54
  1199
        /// <param name="ft"></param>
sl@54
  1200
        /// <returns>0.0f if this is not a monospace font, otherwise returns the character width.</returns>
sl@54
  1201
        public float IsFixedWidth(Font ft)
sl@54
  1202
        {
sl@54
  1203
            Graphics g = CreateGraphics();
StephaneLenclud@223
  1204
            char[] charSizes = new char[] {'i', 'a', 'Z', '%', '#', 'a', 'B', 'l', 'm', ',', '.'};
sl@54
  1205
            float charWidth = g.MeasureString("I", ft, Int32.MaxValue, StringFormat.GenericTypographic).Width;
sl@54
  1206
sl@54
  1207
            bool fixedWidth = true;
sl@54
  1208
sl@54
  1209
            foreach (char c in charSizes)
StephaneLenclud@223
  1210
                if (g.MeasureString(c.ToString(), ft, Int32.MaxValue, StringFormat.GenericTypographic).Width !=
StephaneLenclud@223
  1211
                    charWidth)
sl@54
  1212
                    fixedWidth = false;
sl@54
  1213
sl@54
  1214
            if (fixedWidth)
sl@54
  1215
            {
sl@54
  1216
                return charWidth;
sl@54
  1217
            }
sl@54
  1218
sl@54
  1219
            return 0.0f;
sl@54
  1220
        }
sl@54
  1221
StephaneLenclud@223
  1222
        /// <summary>
StephaneLenclud@223
  1223
        /// Synchronize UI with settings
StephaneLenclud@223
  1224
        /// </summary>
sl@7
  1225
        private void UpdateStatus()
StephaneLenclud@223
  1226
        {
sl@48
  1227
            //Load settings
sl@54
  1228
            checkBoxShowBorders.Checked = cds.ShowBorders;
StephaneLenclud@223
  1229
            iTableLayoutPanel.CellBorderStyle = (cds.ShowBorders
StephaneLenclud@223
  1230
                ? TableLayoutPanelCellBorderStyle.Single
StephaneLenclud@223
  1231
                : TableLayoutPanelCellBorderStyle.None);
sl@60
  1232
sl@60
  1233
            //Set the proper font to each of our labels
StephaneLenclud@175
  1234
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
sl@60
  1235
            {
sl@60
  1236
                ctrl.Font = cds.Font;
sl@60
  1237
            }
sl@60
  1238
sl@54
  1239
            CheckFontHeight();
StephaneLenclud@223
  1240
            //Check if "run on Windows startup" is enabled
StephaneLenclud@223
  1241
            checkBoxAutoStart.Checked = iStartupManager.Startup;
StephaneLenclud@153
  1242
StephaneLenclud@233
  1243
            //Harmony settings
StephaneLenclud@268
  1244
            //Decrypt our password first
StephaneLenclud@268
  1245
            iTextBoxLogitechPassword.Text = Secure.ToInsecureString(Secure.DecryptString(Properties.Settings.Default.LogitechPassword));
StephaneLenclud@268
  1246
            
StephaneLenclud@168
  1247
            //CEC settings
StephaneLenclud@168
  1248
            comboBoxHdmiPort.SelectedIndex = Properties.Settings.Default.CecHdmiPort - 1;
StephaneLenclud@153
  1249
StephaneLenclud@168
  1250
            //Mini Display settings
sl@48
  1251
            checkBoxReverseScreen.Checked = cds.ReverseScreen;
sl@57
  1252
            checkBoxInverseColors.Checked = cds.InverseColors;
StephaneLenclud@223
  1253
            checkBoxShowVolumeLabel.Checked = cds.ShowVolumeLabel;
sl@100
  1254
            checkBoxScaleToFit.Checked = cds.ScaleToFit;
sl@100
  1255
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1256
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1257
            maskedTextBoxMinFontSize.Text = cds.MinFontSize.ToString();
StephaneLenclud@223
  1258
            maskedTextBoxScrollingSpeed.Text = cds.ScrollingSpeedInPixelsPerSecond.ToString();
sl@48
  1259
            comboBoxDisplayType.SelectedIndex = cds.DisplayType;
sl@48
  1260
            timer.Interval = cds.TimerInterval;
sl@48
  1261
            maskedTextBoxTimerInterval.Text = cds.TimerInterval.ToString();
sl@100
  1262
            textBoxScrollLoopSeparator.Text = cds.Separator;
sl@58
  1263
            //
sl@58
  1264
            SetupPixelDelegates();
sl@48
  1265
sl@7
  1266
            if (iDisplay.IsOpen())
sl@7
  1267
            {
StephaneLenclud@187
  1268
                //We have a display connection
StephaneLenclud@187
  1269
                //Reflect that in our UI
StephaneLenclud@187
  1270
                StartTimer();
StephaneLenclud@103
  1271
StephaneLenclud@223
  1272
                iTableLayoutPanel.Enabled = true;
StephaneLenclud@223
  1273
                panelDisplay.Enabled = true;
StephaneLenclud@103
  1274
sl@48
  1275
                //Only setup brightness if display is open
sl@48
  1276
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@48
  1277
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
StephaneLenclud@223
  1278
                if (cds.Brightness < iDisplay.MinBrightness() || cds.Brightness > iDisplay.MaxBrightness())
StephaneLenclud@223
  1279
                {
StephaneLenclud@223
  1280
                    //Brightness out of range, this can occur when using auto-detect
StephaneLenclud@223
  1281
                    //Use max brightness instead
StephaneLenclud@223
  1282
                    trackBarBrightness.Value = iDisplay.MaxBrightness();
StephaneLenclud@223
  1283
                    iDisplay.SetBrightness(iDisplay.MaxBrightness());
StephaneLenclud@223
  1284
                }
StephaneLenclud@223
  1285
                else
StephaneLenclud@223
  1286
                {
StephaneLenclud@223
  1287
                    trackBarBrightness.Value = cds.Brightness;
StephaneLenclud@223
  1288
                    iDisplay.SetBrightness(cds.Brightness);
StephaneLenclud@223
  1289
                }
StephaneLenclud@223
  1290
StephaneLenclud@223
  1291
                //Try compute the steps to something that makes sense
StephaneLenclud@223
  1292
                trackBarBrightness.LargeChange = Math.Max(1, (iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
sl@48
  1293
                trackBarBrightness.SmallChange = 1;
StephaneLenclud@223
  1294
sl@48
  1295
                //
sl@7
  1296
                buttonFill.Enabled = true;
sl@7
  1297
                buttonClear.Enabled = true;
sl@7
  1298
                buttonOpen.Enabled = false;
sl@7
  1299
                buttonClose.Enabled = true;
sl@7
  1300
                trackBarBrightness.Enabled = true;
sl@10
  1301
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
  1302
                //+ " - " + iDisplay.SerialNumber();
sl@52
  1303
sl@52
  1304
                if (iDisplay.SupportPowerOnOff())
sl@52
  1305
                {
sl@52
  1306
                    buttonPowerOn.Enabled = true;
sl@52
  1307
                    buttonPowerOff.Enabled = true;
sl@52
  1308
                }
sl@52
  1309
                else
sl@52
  1310
                {
sl@52
  1311
                    buttonPowerOn.Enabled = false;
sl@52
  1312
                    buttonPowerOff.Enabled = false;
sl@52
  1313
                }
sl@53
  1314
sl@53
  1315
                if (iDisplay.SupportClock())
sl@53
  1316
                {
sl@53
  1317
                    buttonShowClock.Enabled = true;
sl@53
  1318
                    buttonHideClock.Enabled = true;
sl@53
  1319
                }
sl@53
  1320
                else
sl@53
  1321
                {
sl@53
  1322
                    buttonShowClock.Enabled = false;
sl@53
  1323
                    buttonHideClock.Enabled = false;
sl@53
  1324
                }
StephaneLenclud@115
  1325
StephaneLenclud@223
  1326
StephaneLenclud@223
  1327
                //Check if Volume Label is supported. To date only MDM166AA supports that crap :)
StephaneLenclud@223
  1328
                checkBoxShowVolumeLabel.Enabled = iDisplay.IconCount(MiniDisplay.IconType.VolumeLabel) > 0;
StephaneLenclud@223
  1329
StephaneLenclud@223
  1330
                if (cds.ShowVolumeLabel)
StephaneLenclud@223
  1331
                {
StephaneLenclud@135
  1332
                    iDisplay.SetIconOn(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@223
  1333
                }
StephaneLenclud@223
  1334
                else
StephaneLenclud@223
  1335
                {
StephaneLenclud@135
  1336
                    iDisplay.SetIconOff(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@223
  1337
                }
sl@7
  1338
            }
sl@7
  1339
            else
sl@7
  1340
            {
StephaneLenclud@187
  1341
                //Display connection not available
StephaneLenclud@187
  1342
                //Reflect that in our UI
StephaneLenclud@187
  1343
#if DEBUG
StephaneLenclud@187
  1344
                //In debug start our timer even if we don't have a display connection
StephaneLenclud@187
  1345
                StartTimer();
StephaneLenclud@187
  1346
#else
StephaneLenclud@223
  1347
    //In production environment we don't need our timer if no display connection
StephaneLenclud@187
  1348
                StopTimer();
StephaneLenclud@187
  1349
#endif
StephaneLenclud@187
  1350
                checkBoxShowVolumeLabel.Enabled = false;
StephaneLenclud@223
  1351
                iTableLayoutPanel.Enabled = false;
StephaneLenclud@223
  1352
                panelDisplay.Enabled = false;
sl@7
  1353
                buttonFill.Enabled = false;
sl@7
  1354
                buttonClear.Enabled = false;
sl@7
  1355
                buttonOpen.Enabled = true;
sl@7
  1356
                buttonClose.Enabled = false;
sl@7
  1357
                trackBarBrightness.Enabled = false;
sl@52
  1358
                buttonPowerOn.Enabled = false;
sl@52
  1359
                buttonPowerOff.Enabled = false;
sl@53
  1360
                buttonShowClock.Enabled = false;
sl@53
  1361
                buttonHideClock.Enabled = false;
sl@9
  1362
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@48
  1363
                toolStripStatusLabelPower.Text = "N/A";
sl@7
  1364
            }
StephaneLenclud@106
  1365
sl@7
  1366
        }
sl@9
  1367
sl@13
  1368
StephaneLenclud@223
  1369
        /// <summary>
StephaneLenclud@223
  1370
        /// 
StephaneLenclud@223
  1371
        /// </summary>
StephaneLenclud@223
  1372
        /// <param name="sender"></param>
StephaneLenclud@223
  1373
        /// <param name="e"></param>
StephaneLenclud@223
  1374
        private void checkBoxShowVolumeLabel_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
  1375
        {
StephaneLenclud@223
  1376
            cds.ShowVolumeLabel = checkBoxShowVolumeLabel.Checked;
StephaneLenclud@223
  1377
            Properties.Settings.Default.Save();
StephaneLenclud@223
  1378
            UpdateStatus();
StephaneLenclud@223
  1379
        }
sl@13
  1380
sl@9
  1381
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
  1382
        {
sl@16
  1383
            //Save our show borders setting
StephaneLenclud@223
  1384
            iTableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked
StephaneLenclud@223
  1385
                ? TableLayoutPanelCellBorderStyle.Single
StephaneLenclud@223
  1386
                : TableLayoutPanelCellBorderStyle.None);
sl@48
  1387
            cds.ShowBorders = checkBoxShowBorders.Checked;
sl@9
  1388
            Properties.Settings.Default.Save();
sl@57
  1389
            CheckFontHeight();
sl@9
  1390
        }
sl@13
  1391
StephaneLenclud@194
  1392
        private void checkBoxAutoStart_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
  1393
        {
StephaneLenclud@223
  1394
            iStartupManager.Startup = checkBoxAutoStart.Checked;
StephaneLenclud@223
  1395
        }
sl@94
  1396
sl@94
  1397
sl@16
  1398
        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
sl@16
  1399
        {
sl@16
  1400
            //Save our reverse screen setting
sl@48
  1401
            cds.ReverseScreen = checkBoxReverseScreen.Checked;
sl@16
  1402
            Properties.Settings.Default.Save();
sl@58
  1403
            SetupPixelDelegates();
sl@16
  1404
        }
sl@16
  1405
sl@57
  1406
        private void checkBoxInverseColors_CheckedChanged(object sender, EventArgs e)
sl@57
  1407
        {
sl@57
  1408
            //Save our inverse colors setting
sl@57
  1409
            cds.InverseColors = checkBoxInverseColors.Checked;
sl@57
  1410
            Properties.Settings.Default.Save();
sl@58
  1411
            SetupPixelDelegates();
sl@57
  1412
        }
sl@57
  1413
sl@100
  1414
        private void checkBoxScaleToFit_CheckedChanged(object sender, EventArgs e)
sl@100
  1415
        {
sl@100
  1416
            //Save our scale to fit setting
sl@100
  1417
            cds.ScaleToFit = checkBoxScaleToFit.Checked;
sl@100
  1418
            Properties.Settings.Default.Save();
sl@100
  1419
            //
sl@100
  1420
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1421
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1422
        }
sl@100
  1423
sl@14
  1424
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
  1425
        {
sl@14
  1426
            if (WindowState == FormWindowState.Minimized)
sl@14
  1427
            {
StephaneLenclud@158
  1428
                // To workaround our empty bitmap bug on Windows 7 we need to recreate our bitmap when the application is minimized
StephaneLenclud@158
  1429
                // That's apparently not needed on Windows 10 but we better leave it in place.
sl@14
  1430
                iCreateBitmap = true;
sl@14
  1431
            }
sl@14
  1432
        }
sl@14
  1433
sl@17
  1434
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@17
  1435
        {
StephaneLenclud@167
  1436
            iCecManager.Stop();
StephaneLenclud@223
  1437
            iNetworkManager.Dispose();
StephaneLenclud@223
  1438
            CloseDisplayConnection();
sl@17
  1439
            StopServer();
sl@32
  1440
            e.Cancel = iClosing;
sl@17
  1441
        }
sl@17
  1442
sl@17
  1443
        public void StartServer()
sl@17
  1444
        {
sl@17
  1445
            iServiceHost = new ServiceHost
sl@17
  1446
                (
StephaneLenclud@223
  1447
                typeof(Session),
StephaneLenclud@223
  1448
                new Uri[] {new Uri("net.tcp://localhost:8001/")}
sl@17
  1449
                );
sl@17
  1450
StephaneLenclud@223
  1451
            iServiceHost.AddServiceEndpoint(typeof(IService), new NetTcpBinding(SecurityMode.None, true),
StephaneLenclud@223
  1452
                "DisplayService");
sl@17
  1453
            iServiceHost.Open();
sl@17
  1454
        }
sl@17
  1455
sl@17
  1456
        public void StopServer()
sl@17
  1457
        {
sl@32
  1458
            if (iClients.Count > 0 && !iClosing)
sl@29
  1459
            {
sl@29
  1460
                //Tell our clients
sl@32
  1461
                iClosing = true;
sl@29
  1462
                BroadcastCloseEvent();
sl@29
  1463
            }
sl@32
  1464
            else if (iClosing)
sl@32
  1465
            {
StephaneLenclud@223
  1466
                if (
StephaneLenclud@223
  1467
                    MessageBox.Show("Force exit?", "Waiting for clients...", MessageBoxButtons.YesNo,
StephaneLenclud@223
  1468
                        MessageBoxIcon.Warning) == DialogResult.Yes)
sl@32
  1469
                {
sl@32
  1470
                    iClosing = false; //We make sure we force close if asked twice
sl@32
  1471
                }
sl@32
  1472
            }
sl@32
  1473
            else
sl@36
  1474
            {
sl@32
  1475
                //We removed that as it often lags for some reason
sl@32
  1476
                //iServiceHost.Close();
sl@32
  1477
            }
sl@17
  1478
        }
sl@17
  1479
sl@21
  1480
        public void BroadcastCloseEvent()
sl@21
  1481
        {
sl@31
  1482
            Trace.TraceInformation("BroadcastCloseEvent - start");
sl@31
  1483
sl@21
  1484
            var inactiveClients = new List<string>();
sl@21
  1485
            foreach (var client in iClients)
sl@21
  1486
            {
sl@21
  1487
                //if (client.Key != eventData.ClientName)
sl@21
  1488
                {
sl@21
  1489
                    try
sl@21
  1490
                    {
sl@31
  1491
                        Trace.TraceInformation("BroadcastCloseEvent - " + client.Key);
StephaneLenclud@223
  1492
                        client.Value.Callback.OnCloseOrder( /*eventData*/);
sl@21
  1493
                    }
sl@21
  1494
                    catch (Exception ex)
sl@21
  1495
                    {
sl@21
  1496
                        inactiveClients.Add(client.Key);
sl@21
  1497
                    }
sl@21
  1498
                }
sl@21
  1499
            }
sl@21
  1500
sl@21
  1501
            if (inactiveClients.Count > 0)
sl@21
  1502
            {
sl@21
  1503
                foreach (var client in inactiveClients)
sl@21
  1504
                {
sl@21
  1505
                    iClients.Remove(client);
StephaneLenclud@226
  1506
                    Program.iFormMain.iTreeViewClients.Nodes.Remove(
StephaneLenclud@226
  1507
                        Program.iFormMain.iTreeViewClients.Nodes.Find(client, false)[0]);
sl@21
  1508
                }
sl@21
  1509
            }
sl@97
  1510
StephaneLenclud@223
  1511
            if (iClients.Count == 0)
StephaneLenclud@223
  1512
            {
StephaneLenclud@223
  1513
                ClearLayout();
StephaneLenclud@223
  1514
            }
sl@21
  1515
        }
sl@21
  1516
StephaneLenclud@223
  1517
        /// <summary>
StephaneLenclud@223
  1518
        /// Just remove all our fields.
StephaneLenclud@223
  1519
        /// </summary>
StephaneLenclud@223
  1520
        private void ClearLayout()
StephaneLenclud@223
  1521
        {
StephaneLenclud@223
  1522
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@223
  1523
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@223
  1524
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@223
  1525
            iCurrentClientData = null;
StephaneLenclud@223
  1526
        }
StephaneLenclud@223
  1527
StephaneLenclud@223
  1528
        /// <summary>
StephaneLenclud@223
  1529
        /// Just launch a demo client.
StephaneLenclud@223
  1530
        /// </summary>
StephaneLenclud@223
  1531
        private void StartNewClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@223
  1532
        {
StephaneLenclud@223
  1533
            Thread clientThread = new Thread(SharpDisplayClient.Program.MainWithParams);
StephaneLenclud@223
  1534
            SharpDisplayClient.StartParams myParams = new SharpDisplayClient.StartParams(
StephaneLenclud@223
  1535
                new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@223
  1536
            clientThread.Start(myParams);
StephaneLenclud@223
  1537
            BringToFront();
StephaneLenclud@223
  1538
        }
StephaneLenclud@106
  1539
StephaneLenclud@189
  1540
        /// <summary>
StephaneLenclud@189
  1541
        /// Just launch our idle client.
StephaneLenclud@189
  1542
        /// </summary>
StephaneLenclud@189
  1543
        private void StartIdleClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@189
  1544
        {
StephaneLenclud@225
  1545
            Thread clientThread = new Thread(SharpDisplayClientIdle.Program.MainWithParams);
StephaneLenclud@225
  1546
            SharpDisplayClientIdle.StartParams myParams =
StephaneLenclud@225
  1547
                new SharpDisplayClientIdle.StartParams(new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@189
  1548
            clientThread.Start(myParams);
StephaneLenclud@189
  1549
            BringToFront();
StephaneLenclud@189
  1550
        }
StephaneLenclud@189
  1551
StephaneLenclud@189
  1552
sl@25
  1553
        private void buttonStartClient_Click(object sender, EventArgs e)
sl@25
  1554
        {
StephaneLenclud@223
  1555
            StartNewClient();
sl@25
  1556
        }
sl@25
  1557
sl@27
  1558
        private void buttonSuspend_Click(object sender, EventArgs e)
sl@27
  1559
        {
StephaneLenclud@187
  1560
            ToggleTimer();
StephaneLenclud@187
  1561
        }
StephaneLenclud@187
  1562
StephaneLenclud@187
  1563
        private void StartTimer()
StephaneLenclud@187
  1564
        {
StephaneLenclud@187
  1565
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@187
  1566
            timer.Enabled = true;
StephaneLenclud@187
  1567
            UpdateSuspendButton();
StephaneLenclud@187
  1568
        }
StephaneLenclud@187
  1569
StephaneLenclud@187
  1570
        private void StopTimer()
StephaneLenclud@187
  1571
        {
StephaneLenclud@187
  1572
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@187
  1573
            timer.Enabled = false;
StephaneLenclud@187
  1574
            UpdateSuspendButton();
StephaneLenclud@187
  1575
        }
StephaneLenclud@187
  1576
StephaneLenclud@187
  1577
        private void ToggleTimer()
StephaneLenclud@187
  1578
        {
sl@52
  1579
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
sl@27
  1580
            timer.Enabled = !timer.Enabled;
StephaneLenclud@187
  1581
            UpdateSuspendButton();
StephaneLenclud@187
  1582
        }
StephaneLenclud@187
  1583
StephaneLenclud@187
  1584
        private void UpdateSuspendButton()
StephaneLenclud@187
  1585
        {
sl@27
  1586
            if (!timer.Enabled)
sl@27
  1587
            {
sl@52
  1588
                buttonSuspend.Text = "Run";
sl@27
  1589
            }
sl@27
  1590
            else
sl@27
  1591
            {
sl@27
  1592
                buttonSuspend.Text = "Pause";
sl@27
  1593
            }
sl@27
  1594
        }
sl@27
  1595
StephaneLenclud@187
  1596
sl@29
  1597
        private void buttonCloseClients_Click(object sender, EventArgs e)
sl@29
  1598
        {
sl@29
  1599
            BroadcastCloseEvent();
sl@29
  1600
        }
sl@29
  1601
sl@30
  1602
        private void treeViewClients_AfterSelect(object sender, TreeViewEventArgs e)
sl@30
  1603
        {
StephaneLenclud@141
  1604
            //Root node must have at least one child
StephaneLenclud@141
  1605
            if (e.Node.Nodes.Count == 0)
StephaneLenclud@141
  1606
            {
StephaneLenclud@141
  1607
                return;
StephaneLenclud@141
  1608
            }
sl@21
  1609
StephaneLenclud@141
  1610
            //If the selected node is the root node of a client then switch to it
StephaneLenclud@223
  1611
            string sessionId = e.Node.Nodes[0].Text; //First child of a root node is the sessionId
StephaneLenclud@141
  1612
            if (iClients.ContainsKey(sessionId)) //Check that's actually what we are looking at
StephaneLenclud@141
  1613
            {
StephaneLenclud@141
  1614
                //We have a valid session just switch to that client
StephaneLenclud@223
  1615
                SetCurrentClient(sessionId, true);
StephaneLenclud@141
  1616
            }
StephaneLenclud@223
  1617
sl@30
  1618
        }
sl@30
  1619
sl@36
  1620
sl@30
  1621
        /// <summary>
sl@36
  1622
        ///
sl@30
  1623
        /// </summary>
sl@30
  1624
        /// <param name="aSessionId"></param>
sl@30
  1625
        /// <param name="aCallback"></param>
sl@55
  1626
        public void AddClientThreadSafe(string aSessionId, ICallback aCallback)
sl@30
  1627
        {
sl@33
  1628
            if (this.InvokeRequired)
sl@30
  1629
            {
sl@30
  1630
                //Not in the proper thread, invoke ourselves
sl@30
  1631
                AddClientDelegate d = new AddClientDelegate(AddClientThreadSafe);
StephaneLenclud@223
  1632
                this.Invoke(d, new object[] {aSessionId, aCallback});
sl@30
  1633
            }
sl@30
  1634
            else
sl@30
  1635
            {
sl@30
  1636
                //We are in the proper thread
sl@30
  1637
                //Add this session to our collection of clients
sl@33
  1638
                ClientData newClient = new ClientData(aSessionId, aCallback);
StephaneLenclud@226
  1639
                Program.iFormMain.iClients.Add(aSessionId, newClient);
sl@30
  1640
                //Add this session to our UI
sl@33
  1641
                UpdateClientTreeViewNode(newClient);
sl@30
  1642
            }
sl@30
  1643
        }
sl@30
  1644
Stephane@186
  1645
Stephane@186
  1646
        /// <summary>
Stephane@186
  1647
        /// Find the client with the highest priority if any.
Stephane@186
  1648
        /// </summary>
Stephane@186
  1649
        /// <returns>Our highest priority client or null if not a single client is connected.</returns>
Stephane@186
  1650
        public ClientData FindHighestPriorityClient()
Stephane@186
  1651
        {
Stephane@186
  1652
            ClientData highestPriorityClient = null;
Stephane@186
  1653
            foreach (var client in iClients)
Stephane@186
  1654
            {
Stephane@186
  1655
                if (highestPriorityClient == null || client.Value.Priority > highestPriorityClient.Priority)
Stephane@186
  1656
                {
Stephane@186
  1657
                    highestPriorityClient = client.Value;
Stephane@186
  1658
                }
Stephane@186
  1659
            }
Stephane@186
  1660
Stephane@186
  1661
            return highestPriorityClient;
Stephane@186
  1662
        }
Stephane@186
  1663
sl@30
  1664
        /// <summary>
sl@36
  1665
        ///
sl@30
  1666
        /// </summary>
sl@30
  1667
        /// <param name="aSessionId"></param>
sl@30
  1668
        public void RemoveClientThreadSafe(string aSessionId)
sl@30
  1669
        {
sl@33
  1670
            if (this.InvokeRequired)
sl@30
  1671
            {
sl@30
  1672
                //Not in the proper thread, invoke ourselves
sl@30
  1673
                RemoveClientDelegate d = new RemoveClientDelegate(RemoveClientThreadSafe);
StephaneLenclud@223
  1674
                this.Invoke(d, new object[] {aSessionId});
sl@30
  1675
            }
sl@30
  1676
            else
sl@30
  1677
            {
sl@30
  1678
                //We are in the proper thread
sl@33
  1679
                //Remove this session from both client collection and UI tree view
StephaneLenclud@226
  1680
                if (Program.iFormMain.iClients.Keys.Contains(aSessionId))
sl@30
  1681
                {
StephaneLenclud@226
  1682
                    Program.iFormMain.iClients.Remove(aSessionId);
StephaneLenclud@226
  1683
                    Program.iFormMain.iTreeViewClients.Nodes.Remove(
StephaneLenclud@226
  1684
                        Program.iFormMain.iTreeViewClients.Nodes.Find(aSessionId, false)[0]);
StephaneLenclud@188
  1685
                    //Update recording status too whenever a client is removed
StephaneLenclud@188
  1686
                    UpdateRecordingNotification();
sl@32
  1687
                }
sl@32
  1688
Stephane@186
  1689
                if (iCurrentClientSessionId == aSessionId)
Stephane@186
  1690
                {
Stephane@186
  1691
                    //The current client is closing
Stephane@186
  1692
                    iCurrentClientData = null;
Stephane@186
  1693
                    //Find the client with the highest priority and set it as current
Stephane@186
  1694
                    ClientData newCurrentClient = FindHighestPriorityClient();
StephaneLenclud@223
  1695
                    if (newCurrentClient != null)
Stephane@186
  1696
                    {
Stephane@186
  1697
                        SetCurrentClient(newCurrentClient.SessionId, true);
StephaneLenclud@223
  1698
                    }
Stephane@186
  1699
                }
Stephane@186
  1700
Stephane@186
  1701
                if (iClients.Count == 0)
StephaneLenclud@223
  1702
                {
StephaneLenclud@223
  1703
                    //Clear our screen when last client disconnects
StephaneLenclud@223
  1704
                    ClearLayout();
StephaneLenclud@223
  1705
StephaneLenclud@223
  1706
                    if (iClosing)
StephaneLenclud@223
  1707
                    {
StephaneLenclud@223
  1708
                        //We were closing our form
StephaneLenclud@223
  1709
                        //All clients are now closed
StephaneLenclud@223
  1710
                        //Just resume our close operation
StephaneLenclud@223
  1711
                        iClosing = false;
StephaneLenclud@223
  1712
                        Close();
StephaneLenclud@223
  1713
                    }
StephaneLenclud@223
  1714
                }
sl@30
  1715
            }
sl@30
  1716
        }
sl@30
  1717
sl@30
  1718
        /// <summary>
sl@36
  1719
        ///
sl@30
  1720
        /// </summary>
sl@62
  1721
        /// <param name="aSessionId"></param>
sl@72
  1722
        /// <param name="aLayout"></param>
sl@62
  1723
        public void SetClientLayoutThreadSafe(string aSessionId, TableLayout aLayout)
sl@62
  1724
        {
sl@62
  1725
            if (this.InvokeRequired)
sl@62
  1726
            {
sl@62
  1727
                //Not in the proper thread, invoke ourselves
sl@62
  1728
                SetLayoutDelegate d = new SetLayoutDelegate(SetClientLayoutThreadSafe);
StephaneLenclud@223
  1729
                this.Invoke(d, new object[] {aSessionId, aLayout});
sl@62
  1730
            }
sl@62
  1731
            else
sl@62
  1732
            {
sl@62
  1733
                ClientData client = iClients[aSessionId];
sl@62
  1734
                if (client != null)
sl@62
  1735
                {
StephaneLenclud@148
  1736
                    //Don't change a thing if the layout is the same
StephaneLenclud@148
  1737
                    if (!client.Layout.IsSameAs(aLayout))
StephaneLenclud@148
  1738
                    {
StephaneLenclud@158
  1739
                        Debug.Print("SetClientLayoutThreadSafe: Layout updated.");
StephaneLenclud@148
  1740
                        //Set our client layout then
StephaneLenclud@148
  1741
                        client.Layout = aLayout;
StephaneLenclud@175
  1742
                        //So that next time we update all our fields at ones
StephaneLenclud@175
  1743
                        client.HasNewLayout = true;
StephaneLenclud@158
  1744
                        //Layout has changed clear our fields then
StephaneLenclud@158
  1745
                        client.Fields.Clear();
StephaneLenclud@148
  1746
                        //
StephaneLenclud@148
  1747
                        UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1748
                    }
StephaneLenclud@158
  1749
                    else
StephaneLenclud@158
  1750
                    {
StephaneLenclud@158
  1751
                        Debug.Print("SetClientLayoutThreadSafe: Layout has not changed.");
StephaneLenclud@158
  1752
                    }
sl@62
  1753
                }
sl@62
  1754
            }
sl@62
  1755
        }
sl@62
  1756
sl@62
  1757
        /// <summary>
sl@62
  1758
        ///
sl@62
  1759
        /// </summary>
sl@67
  1760
        /// <param name="aSessionId"></param>
sl@72
  1761
        /// <param name="aField"></param>
sl@75
  1762
        public void SetClientFieldThreadSafe(string aSessionId, DataField aField)
sl@30
  1763
        {
sl@33
  1764
            if (this.InvokeRequired)
sl@30
  1765
            {
sl@30
  1766
                //Not in the proper thread, invoke ourselves
sl@79
  1767
                SetFieldDelegate d = new SetFieldDelegate(SetClientFieldThreadSafe);
StephaneLenclud@223
  1768
                this.Invoke(d, new object[] {aSessionId, aField});
sl@30
  1769
            }
sl@30
  1770
            else
sl@30
  1771
            {
sl@75
  1772
                //We are in the proper thread
sl@75
  1773
                //Call the non-thread-safe variant
sl@75
  1774
                SetClientField(aSessionId, aField);
sl@75
  1775
            }
sl@75
  1776
        }
sl@75
  1777
StephaneLenclud@176
  1778
StephaneLenclud@176
  1779
StephaneLenclud@176
  1780
sl@75
  1781
        /// <summary>
StephaneLenclud@175
  1782
        /// Set a data field in the given client.
sl@75
  1783
        /// </summary>
sl@75
  1784
        /// <param name="aSessionId"></param>
sl@75
  1785
        /// <param name="aField"></param>
sl@75
  1786
        private void SetClientField(string aSessionId, DataField aField)
StephaneLenclud@223
  1787
        {
StephaneLenclud@141
  1788
            //TODO: should check if the field actually changed?
StephaneLenclud@141
  1789
sl@75
  1790
            ClientData client = iClients[aSessionId];
StephaneLenclud@141
  1791
            bool layoutChanged = false;
StephaneLenclud@148
  1792
            bool contentChanged = true;
StephaneLenclud@141
  1793
StephaneLenclud@176
  1794
            //Fetch our field index
StephaneLenclud@176
  1795
            int fieldIndex = client.FindSameFieldIndex(aField);
StephaneLenclud@176
  1796
StephaneLenclud@176
  1797
            if (fieldIndex < 0)
sl@75
  1798
            {
StephaneLenclud@176
  1799
                //No corresponding field, just bail out
StephaneLenclud@176
  1800
                return;
StephaneLenclud@141
  1801
            }
sl@76
  1802
StephaneLenclud@175
  1803
            //Keep our previous field in there
StephaneLenclud@176
  1804
            DataField previousField = client.Fields[fieldIndex];
StephaneLenclud@176
  1805
            //Just update that field then 
StephaneLenclud@176
  1806
            client.Fields[fieldIndex] = aField;
StephaneLenclud@148
  1807
StephaneLenclud@176
  1808
            if (!aField.IsTableField)
StephaneLenclud@176
  1809
            {
StephaneLenclud@176
  1810
                //We are done then if that field is not in our table layout
StephaneLenclud@176
  1811
                return;
StephaneLenclud@176
  1812
            }
StephaneLenclud@176
  1813
StephaneLenclud@176
  1814
            TableField tableField = (TableField) aField;
StephaneLenclud@172
  1815
StephaneLenclud@175
  1816
            if (previousField.IsSameLayout(aField))
StephaneLenclud@141
  1817
            {
StephaneLenclud@141
  1818
                //If we are updating a field in our current client we need to update it in our panel
StephaneLenclud@141
  1819
                if (aSessionId == iCurrentClientSessionId)
sl@30
  1820
                {
StephaneLenclud@223
  1821
                    Control ctrl = iTableLayoutPanel.GetControlFromPosition(tableField.Column, tableField.Row);
StephaneLenclud@176
  1822
                    if (aField.IsTextField && ctrl is MarqueeLabel)
sl@79
  1823
                    {
StephaneLenclud@223
  1824
                        TextField textField = (TextField) aField;
sl@75
  1825
                        //Text field control already in place, just change the text
StephaneLenclud@223
  1826
                        MarqueeLabel label = (MarqueeLabel) ctrl;
StephaneLenclud@172
  1827
                        contentChanged = (label.Text != textField.Text || label.TextAlign != textField.Alignment);
StephaneLenclud@172
  1828
                        label.Text = textField.Text;
StephaneLenclud@172
  1829
                        label.TextAlign = textField.Alignment;
sl@68
  1830
                    }
StephaneLenclud@176
  1831
                    else if (aField.IsBitmapField && ctrl is PictureBox)
sl@75
  1832
                    {
StephaneLenclud@223
  1833
                        BitmapField bitmapField = (BitmapField) aField;
StephaneLenclud@148
  1834
                        contentChanged = true; //TODO: Bitmap comp or should we leave that to clients?
sl@75
  1835
                        //Bitmap field control already in place just change the bitmap
StephaneLenclud@223
  1836
                        PictureBox pictureBox = (PictureBox) ctrl;
StephaneLenclud@172
  1837
                        pictureBox.Image = bitmapField.Bitmap;
sl@75
  1838
                    }
sl@68
  1839
                    else
sl@68
  1840
                    {
StephaneLenclud@141
  1841
                        layoutChanged = true;
sl@68
  1842
                    }
sl@30
  1843
                }
StephaneLenclud@141
  1844
            }
StephaneLenclud@141
  1845
            else
StephaneLenclud@223
  1846
            {
StephaneLenclud@141
  1847
                layoutChanged = true;
StephaneLenclud@141
  1848
            }
StephaneLenclud@141
  1849
StephaneLenclud@148
  1850
            //If either content or layout changed we need to update our tree view to reflect the changes
StephaneLenclud@148
  1851
            if (contentChanged || layoutChanged)
StephaneLenclud@141
  1852
            {
StephaneLenclud@141
  1853
                UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1854
                //
StephaneLenclud@148
  1855
                if (layoutChanged)
sl@75
  1856
                {
StephaneLenclud@148
  1857
                    Debug.Print("Layout changed");
StephaneLenclud@148
  1858
                    //Our layout has changed, if we are already the current client we need to update our panel
StephaneLenclud@148
  1859
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@148
  1860
                    {
StephaneLenclud@148
  1861
                        //Apply layout and set data fields.
StephaneLenclud@148
  1862
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@148
  1863
                    }
sl@75
  1864
                }
StephaneLenclud@158
  1865
                else
StephaneLenclud@158
  1866
                {
StephaneLenclud@158
  1867
                    Debug.Print("Layout has not changed.");
StephaneLenclud@158
  1868
                }
StephaneLenclud@158
  1869
            }
StephaneLenclud@158
  1870
            else
StephaneLenclud@158
  1871
            {
StephaneLenclud@158
  1872
                Debug.Print("WARNING: content and layout have not changed!");
StephaneLenclud@141
  1873
            }
sl@75
  1874
StephaneLenclud@141
  1875
            //When a client field is set we try switching to this client to present the new information to our user
StephaneLenclud@141
  1876
            SetCurrentClient(aSessionId);
sl@30
  1877
        }
sl@30
  1878
sl@30
  1879
        /// <summary>
sl@36
  1880
        ///
sl@30
  1881
        /// </summary>
sl@30
  1882
        /// <param name="aTexts"></param>
sl@75
  1883
        public void SetClientFieldsThreadSafe(string aSessionId, System.Collections.Generic.IList<DataField> aFields)
sl@30
  1884
        {
sl@33
  1885
            if (this.InvokeRequired)
sl@30
  1886
            {
sl@30
  1887
                //Not in the proper thread, invoke ourselves
sl@75
  1888
                SetFieldsDelegate d = new SetFieldsDelegate(SetClientFieldsThreadSafe);
StephaneLenclud@223
  1889
                this.Invoke(d, new object[] {aSessionId, aFields});
sl@30
  1890
            }
sl@30
  1891
            else
sl@30
  1892
            {
StephaneLenclud@175
  1893
                ClientData client = iClients[aSessionId];
StephaneLenclud@175
  1894
StephaneLenclud@175
  1895
                if (client.HasNewLayout)
sl@30
  1896
                {
StephaneLenclud@175
  1897
                    //TODO: Assert client.Count == 0
StephaneLenclud@175
  1898
                    //Our layout was just changed
StephaneLenclud@175
  1899
                    //Do some special handling to avoid re-creating our panel N times, once for each fields
StephaneLenclud@175
  1900
                    client.HasNewLayout = false;
StephaneLenclud@175
  1901
                    //Just set all our fields then
StephaneLenclud@175
  1902
                    client.Fields.AddRange(aFields);
StephaneLenclud@175
  1903
                    //Try switch to that client
StephaneLenclud@175
  1904
                    SetCurrentClient(aSessionId);
StephaneLenclud@175
  1905
StephaneLenclud@175
  1906
                    //If we are updating the current client update our panel
StephaneLenclud@175
  1907
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@175
  1908
                    {
StephaneLenclud@175
  1909
                        //Apply layout and set data fields.
StephaneLenclud@175
  1910
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@175
  1911
                    }
StephaneLenclud@176
  1912
StephaneLenclud@176
  1913
                    UpdateClientTreeViewNode(client);
StephaneLenclud@175
  1914
                }
StephaneLenclud@175
  1915
                else
StephaneLenclud@175
  1916
                {
StephaneLenclud@175
  1917
                    //Put each our text fields in a label control
StephaneLenclud@175
  1918
                    foreach (DataField field in aFields)
StephaneLenclud@175
  1919
                    {
StephaneLenclud@175
  1920
                        SetClientField(aSessionId, field);
StephaneLenclud@175
  1921
                    }
sl@30
  1922
                }
sl@30
  1923
            }
sl@32
  1924
        }
sl@30
  1925
sl@67
  1926
        /// <summary>
sl@67
  1927
        ///
sl@67
  1928
        /// </summary>
sl@67
  1929
        /// <param name="aSessionId"></param>
sl@32
  1930
        /// <param name="aName"></param>
sl@32
  1931
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
  1932
        {
sl@32
  1933
            if (this.InvokeRequired)
sl@32
  1934
            {
sl@32
  1935
                //Not in the proper thread, invoke ourselves
sl@32
  1936
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
StephaneLenclud@223
  1937
                this.Invoke(d, new object[] {aSessionId, aName});
sl@32
  1938
            }
sl@32
  1939
            else
sl@32
  1940
            {
sl@32
  1941
                //We are in the proper thread
sl@33
  1942
                //Get our client
sl@33
  1943
                ClientData client = iClients[aSessionId];
sl@33
  1944
                if (client != null)
sl@32
  1945
                {
sl@33
  1946
                    //Set its name
sl@33
  1947
                    client.Name = aName;
sl@33
  1948
                    //Update our tree-view
sl@33
  1949
                    UpdateClientTreeViewNode(client);
sl@33
  1950
                }
sl@33
  1951
            }
sl@33
  1952
        }
sl@33
  1953
StephaneLenclud@184
  1954
        ///
StephaneLenclud@184
  1955
        public void SetClientPriorityThreadSafe(string aSessionId, uint aPriority)
StephaneLenclud@184
  1956
        {
StephaneLenclud@184
  1957
            if (this.InvokeRequired)
StephaneLenclud@184
  1958
            {
StephaneLenclud@184
  1959
                //Not in the proper thread, invoke ourselves
StephaneLenclud@184
  1960
                SetClientPriorityDelegate d = new SetClientPriorityDelegate(SetClientPriorityThreadSafe);
StephaneLenclud@223
  1961
                this.Invoke(d, new object[] {aSessionId, aPriority});
StephaneLenclud@184
  1962
            }
StephaneLenclud@184
  1963
            else
StephaneLenclud@184
  1964
            {
StephaneLenclud@184
  1965
                //We are in the proper thread
StephaneLenclud@184
  1966
                //Get our client
StephaneLenclud@184
  1967
                ClientData client = iClients[aSessionId];
StephaneLenclud@184
  1968
                if (client != null)
StephaneLenclud@184
  1969
                {
StephaneLenclud@184
  1970
                    //Set its name
StephaneLenclud@184
  1971
                    client.Priority = aPriority;
StephaneLenclud@184
  1972
                    //Update our tree-view
StephaneLenclud@184
  1973
                    UpdateClientTreeViewNode(client);
Stephane@186
  1974
                    //Change our current client as per new priority
Stephane@186
  1975
                    ClientData newCurrentClient = FindHighestPriorityClient();
StephaneLenclud@223
  1976
                    if (newCurrentClient != null)
Stephane@186
  1977
                    {
Stephane@186
  1978
                        SetCurrentClient(newCurrentClient.SessionId);
Stephane@186
  1979
                    }
StephaneLenclud@184
  1980
                }
StephaneLenclud@184
  1981
            }
StephaneLenclud@184
  1982
        }
StephaneLenclud@184
  1983
sl@33
  1984
        /// <summary>
StephaneLenclud@183
  1985
        /// 
StephaneLenclud@183
  1986
        /// </summary>
StephaneLenclud@183
  1987
        /// <param name="value"></param>
StephaneLenclud@183
  1988
        /// <param name="maxChars"></param>
StephaneLenclud@183
  1989
        /// <returns></returns>
StephaneLenclud@183
  1990
        public static string Truncate(string value, int maxChars)
StephaneLenclud@183
  1991
        {
StephaneLenclud@223
  1992
            return value.Length <= maxChars ? value : value.Substring(0, maxChars - 3) + "...";
StephaneLenclud@183
  1993
        }
StephaneLenclud@183
  1994
StephaneLenclud@183
  1995
        /// <summary>
StephaneLenclud@180
  1996
        /// Update our recording notification.
StephaneLenclud@180
  1997
        /// </summary>
StephaneLenclud@180
  1998
        private void UpdateRecordingNotification()
StephaneLenclud@180
  1999
        {
StephaneLenclud@180
  2000
            //Go through each 
StephaneLenclud@180
  2001
            bool activeRecording = false;
StephaneLenclud@223
  2002
            string text = "";
StephaneLenclud@223
  2003
            RecordingField recField = new RecordingField();
StephaneLenclud@180
  2004
            foreach (var client in iClients)
StephaneLenclud@180
  2005
            {
StephaneLenclud@223
  2006
                RecordingField rec = (RecordingField) client.Value.FindSameFieldAs(recField);
StephaneLenclud@223
  2007
                if (rec != null && rec.IsActive)
StephaneLenclud@180
  2008
                {
StephaneLenclud@180
  2009
                    activeRecording = true;
StephaneLenclud@183
  2010
                    //Don't break cause we are collecting the names/texts.
StephaneLenclud@183
  2011
                    if (!String.IsNullOrEmpty(rec.Text))
StephaneLenclud@183
  2012
                    {
StephaneLenclud@183
  2013
                        text += (rec.Text + "\n");
StephaneLenclud@183
  2014
                    }
StephaneLenclud@183
  2015
                    else
StephaneLenclud@183
  2016
                    {
StephaneLenclud@183
  2017
                        //Not text for that recording, use client name instead
StephaneLenclud@183
  2018
                        text += client.Value.Name + " recording\n";
StephaneLenclud@183
  2019
                    }
StephaneLenclud@223
  2020
StephaneLenclud@180
  2021
                }
StephaneLenclud@180
  2022
            }
StephaneLenclud@180
  2023
StephaneLenclud@183
  2024
            //Update our text no matter what, can't have more than 63 characters otherwise it throws an exception.
StephaneLenclud@223
  2025
            iRecordingNotification.Text = Truncate(text, 63);
StephaneLenclud@183
  2026
StephaneLenclud@180
  2027
            //Change visibility of notification if needed
StephaneLenclud@180
  2028
            if (iRecordingNotification.Visible != activeRecording)
StephaneLenclud@223
  2029
            {
StephaneLenclud@180
  2030
                iRecordingNotification.Visible = activeRecording;
Stephane@182
  2031
                //Assuming the notification icon is in sync with our display icon
Stephane@182
  2032
                //Take care of our REC icon
StephaneLenclud@183
  2033
                if (iDisplay.IsOpen())
StephaneLenclud@183
  2034
                {
StephaneLenclud@183
  2035
                    iDisplay.SetIconOnOff(MiniDisplay.IconType.Recording, activeRecording);
StephaneLenclud@223
  2036
                }
StephaneLenclud@180
  2037
            }
StephaneLenclud@180
  2038
        }
StephaneLenclud@180
  2039
StephaneLenclud@180
  2040
        /// <summary>
sl@36
  2041
        ///
sl@33
  2042
        /// </summary>
sl@33
  2043
        /// <param name="aClient"></param>
sl@33
  2044
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
  2045
        {
StephaneLenclud@148
  2046
            Debug.Print("UpdateClientTreeViewNode");
StephaneLenclud@148
  2047
sl@33
  2048
            if (aClient == null)
sl@33
  2049
            {
sl@33
  2050
                return;
sl@33
  2051
            }
sl@33
  2052
StephaneLenclud@180
  2053
            //Hook in record icon update too
StephaneLenclud@180
  2054
            UpdateRecordingNotification();
StephaneLenclud@180
  2055
sl@33
  2056
            TreeNode node = null;
sl@33
  2057
            //Check that our client node already exists
sl@33
  2058
            //Get our client root node using its key which is our session ID
StephaneLenclud@188
  2059
            TreeNode[] nodes = iTreeViewClients.Nodes.Find(aClient.SessionId, false);
StephaneLenclud@223
  2060
            if (nodes.Count() > 0)
sl@33
  2061
            {
sl@33
  2062
                //We already have a node for that client
sl@33
  2063
                node = nodes[0];
sl@33
  2064
                //Clear children as we are going to recreate them below
sl@33
  2065
                node.Nodes.Clear();
sl@33
  2066
            }
sl@33
  2067
            else
sl@33
  2068
            {
sl@33
  2069
                //Client node does not exists create a new one
StephaneLenclud@188
  2070
                iTreeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
StephaneLenclud@188
  2071
                node = iTreeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
  2072
            }
sl@33
  2073
sl@33
  2074
            if (node != null)
sl@33
  2075
            {
sl@33
  2076
                //Change its name
StephaneLenclud@184
  2077
                if (!String.IsNullOrEmpty(aClient.Name))
sl@33
  2078
                {
StephaneLenclud@184
  2079
                    //We have a name, use it as text for our root node
sl@33
  2080
                    node.Text = aClient.Name;
sl@32
  2081
                    //Add a child with SessionId
sl@33
  2082
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
  2083
                }
sl@33
  2084
                else
sl@33
  2085
                {
sl@33
  2086
                    //No name, use session ID instead
sl@33
  2087
                    node.Text = aClient.SessionId;
sl@33
  2088
                }
sl@36
  2089
StephaneLenclud@184
  2090
                //Display client priority
StephaneLenclud@184
  2091
                node.Nodes.Add(new TreeNode("Priority: " + aClient.Priority));
StephaneLenclud@184
  2092
sl@67
  2093
                if (aClient.Fields.Count > 0)
sl@33
  2094
                {
sl@33
  2095
                    //Create root node for our texts
sl@70
  2096
                    TreeNode textsRoot = new TreeNode("Fields");
sl@33
  2097
                    node.Nodes.Add(textsRoot);
sl@33
  2098
                    //For each text add a new entry
sl@67
  2099
                    foreach (DataField field in aClient.Fields)
sl@33
  2100
                    {
StephaneLenclud@172
  2101
                        if (field.IsTextField)
sl@67
  2102
                        {
StephaneLenclud@223
  2103
                            TextField textField = (TextField) field;
sl@70
  2104
                            textsRoot.Nodes.Add(new TreeNode("[Text]" + textField.Text));
sl@67
  2105
                        }
StephaneLenclud@172
  2106
                        else if (field.IsBitmapField)
sl@67
  2107
                        {
sl@72
  2108
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap]"));
sl@70
  2109
                        }
StephaneLenclud@172
  2110
                        else if (field.IsRecordingField)
StephaneLenclud@172
  2111
                        {
StephaneLenclud@223
  2112
                            RecordingField recordingField = (RecordingField) field;
StephaneLenclud@177
  2113
                            textsRoot.Nodes.Add(new TreeNode("[Recording]" + recordingField.IsActive));
StephaneLenclud@172
  2114
                        }
sl@33
  2115
                    }
sl@32
  2116
                }
sl@34
  2117
sl@34
  2118
                node.ExpandAll();
sl@32
  2119
            }
sl@30
  2120
        }
sl@17
  2121
sl@60
  2122
        /// <summary>
sl@60
  2123
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  2124
        /// </summary>
sl@60
  2125
        private void UpdateTableLayoutRowStyles()
sl@60
  2126
        {
StephaneLenclud@175
  2127
            foreach (RowStyle rowStyle in iTableLayoutPanel.RowStyles)
sl@60
  2128
            {
sl@60
  2129
                rowStyle.SizeType = SizeType.Percent;
StephaneLenclud@223
  2130
                rowStyle.Height = 100/iTableLayoutPanel.RowCount;
sl@60
  2131
            }
sl@60
  2132
        }
sl@60
  2133
sl@63
  2134
        /// <summary>
sl@63
  2135
        /// Update our display table layout.
StephaneLenclud@175
  2136
        /// Will instanciated every field control as defined by our client.
StephaneLenclud@175
  2137
        /// Fields must be specified by rows from the left.
sl@63
  2138
        /// </summary>
sl@63
  2139
        /// <param name="aLayout"></param>
sl@65
  2140
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  2141
        {
StephaneLenclud@175
  2142
            Debug.Print("UpdateTableLayoutPanel");
StephaneLenclud@148
  2143
StephaneLenclud@223
  2144
            if (aClient == null)
StephaneLenclud@223
  2145
            {
StephaneLenclud@223
  2146
                //Just drop it
StephaneLenclud@223
  2147
                return;
StephaneLenclud@223
  2148
            }
StephaneLenclud@106
  2149
StephaneLenclud@106
  2150
sl@65
  2151
            TableLayout layout = aClient.Layout;
sl@70
  2152
StephaneLenclud@141
  2153
            //First clean our current panel
StephaneLenclud@175
  2154
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  2155
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  2156
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@175
  2157
            iTableLayoutPanel.RowCount = 0;
StephaneLenclud@175
  2158
            iTableLayoutPanel.ColumnCount = 0;
sl@63
  2159
StephaneLenclud@175
  2160
            //Then recreate our rows...
StephaneLenclud@175
  2161
            while (iTableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  2162
            {
StephaneLenclud@175
  2163
                iTableLayoutPanel.RowCount++;
sl@63
  2164
            }
sl@63
  2165
StephaneLenclud@175
  2166
            // ...and columns 
StephaneLenclud@175
  2167
            while (iTableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  2168
            {
StephaneLenclud@175
  2169
                iTableLayoutPanel.ColumnCount++;
sl@63
  2170
            }
sl@63
  2171
StephaneLenclud@175
  2172
            //For each column
StephaneLenclud@175
  2173
            for (int i = 0; i < iTableLayoutPanel.ColumnCount; i++)
sl@63
  2174
            {
sl@63
  2175
                //Create our column styles
StephaneLenclud@175
  2176
                this.iTableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  2177
StephaneLenclud@175
  2178
                //For each rows
StephaneLenclud@175
  2179
                for (int j = 0; j < iTableLayoutPanel.RowCount; j++)
sl@63
  2180
                {
sl@63
  2181
                    if (i == 0)
sl@63
  2182
                    {
sl@63
  2183
                        //Create our row styles
StephaneLenclud@175
  2184
                        this.iTableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  2185
                    }
StephaneLenclud@176
  2186
                    else
sl@70
  2187
                    {
sl@70
  2188
                        continue;
sl@70
  2189
                    }
sl@63
  2190
                }
sl@63
  2191
            }
sl@63
  2192
StephaneLenclud@176
  2193
            //For each field
StephaneLenclud@176
  2194
            foreach (DataField field in aClient.Fields)
sl@70
  2195
            {
StephaneLenclud@176
  2196
                if (!field.IsTableField)
StephaneLenclud@176
  2197
                {
StephaneLenclud@176
  2198
                    //That field is not taking part in our table layout skip it
StephaneLenclud@176
  2199
                    continue;
StephaneLenclud@176
  2200
                }
StephaneLenclud@176
  2201
StephaneLenclud@223
  2202
                TableField tableField = (TableField) field;
StephaneLenclud@176
  2203
StephaneLenclud@176
  2204
                //Create a control corresponding to the field specified for that cell
StephaneLenclud@176
  2205
                Control control = CreateControlForDataField(tableField);
StephaneLenclud@176
  2206
StephaneLenclud@176
  2207
                //Add newly created control to our table layout at the specified row and column
StephaneLenclud@176
  2208
                iTableLayoutPanel.Controls.Add(control, tableField.Column, tableField.Row);
StephaneLenclud@176
  2209
                //Make sure we specify column and row span for that new control
StephaneLenclud@176
  2210
                iTableLayoutPanel.SetColumnSpan(control, tableField.ColumnSpan);
StephaneLenclud@176
  2211
                iTableLayoutPanel.SetRowSpan(control, tableField.RowSpan);
sl@70
  2212
            }
sl@70
  2213
StephaneLenclud@176
  2214
sl@63
  2215
            CheckFontHeight();
sl@63
  2216
        }
sl@63
  2217
sl@68
  2218
        /// <summary>
sl@70
  2219
        /// Check our type of data field and create corresponding control
sl@68
  2220
        /// </summary>
sl@68
  2221
        /// <param name="aField"></param>
sl@69
  2222
        private Control CreateControlForDataField(DataField aField)
sl@68
  2223
        {
StephaneLenclud@223
  2224
            Control control = null;
StephaneLenclud@172
  2225
            if (aField.IsTextField)
sl@68
  2226
            {
sl@68
  2227
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  2228
                label.AutoEllipsis = true;
sl@68
  2229
                label.AutoSize = true;
sl@68
  2230
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  2231
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2232
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  2233
                label.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@176
  2234
                label.Name = "marqueeLabel" + aField;
sl@68
  2235
                label.OwnTimer = false;
StephaneLenclud@106
  2236
                label.PixelsPerSecond = cds.ScrollingSpeedInPixelsPerSecond;
sl@100
  2237
                label.Separator = cds.Separator;
sl@100
  2238
                label.MinFontSize = cds.MinFontSize;
sl@100
  2239
                label.ScaleToFit = cds.ScaleToFit;
sl@68
  2240
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  2241
                //control.TabIndex = 2;
sl@68
  2242
                label.Font = cds.Font;
sl@68
  2243
StephaneLenclud@223
  2244
                TextField field = (TextField) aField;
StephaneLenclud@172
  2245
                label.TextAlign = field.Alignment;
sl@68
  2246
                label.UseCompatibleTextRendering = true;
StephaneLenclud@172
  2247
                label.Text = field.Text;
sl@68
  2248
                //
sl@68
  2249
                control = label;
sl@68
  2250
            }
StephaneLenclud@172
  2251
            else if (aField.IsBitmapField)
sl@68
  2252
            {
sl@68
  2253
                //Create picture box
sl@68
  2254
                PictureBox picture = new PictureBox();
sl@68
  2255
                picture.AutoSize = true;
sl@68
  2256
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  2257
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2258
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  2259
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  2260
                picture.Name = "pictureBox" + aField;
sl@68
  2261
                //Set our image
StephaneLenclud@223
  2262
                BitmapField field = (BitmapField) aField;
StephaneLenclud@172
  2263
                picture.Image = field.Bitmap;
sl@68
  2264
                //
sl@68
  2265
                control = picture;
sl@68
  2266
            }
StephaneLenclud@172
  2267
            //TODO: Handle recording field?
sl@68
  2268
sl@69
  2269
            return control;
sl@68
  2270
        }
sl@68
  2271
StephaneLenclud@223
  2272
        /// <summary>
StephaneLenclud@223
  2273
        /// Called when the user selected a new display type.
StephaneLenclud@223
  2274
        /// </summary>
StephaneLenclud@223
  2275
        /// <param name="sender"></param>
StephaneLenclud@223
  2276
        /// <param name="e"></param>
sl@46
  2277
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  2278
        {
StephaneLenclud@223
  2279
            //Store the selected display type in our settings
sl@48
  2280
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  2281
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  2282
            Properties.Settings.Default.Save();
StephaneLenclud@103
  2283
StephaneLenclud@223
  2284
            //Try re-opening the display connection if we were already connected.
StephaneLenclud@223
  2285
            //Otherwise just update our status to reflect display type change.
sl@51
  2286
            if (iDisplay.IsOpen())
sl@51
  2287
            {
sl@51
  2288
                OpenDisplayConnection();
sl@51
  2289
            }
sl@51
  2290
            else
sl@51
  2291
            {
sl@51
  2292
                UpdateStatus();
sl@51
  2293
            }
sl@46
  2294
        }
sl@46
  2295
sl@47
  2296
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  2297
        {
sl@47
  2298
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  2299
            {
sl@51
  2300
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  2301
sl@51
  2302
                if (interval > 0)
sl@51
  2303
                {
sl@51
  2304
                    timer.Interval = interval;
sl@51
  2305
                    cds.TimerInterval = timer.Interval;
sl@51
  2306
                    Properties.Settings.Default.Save();
sl@51
  2307
                }
sl@47
  2308
            }
sl@47
  2309
        }
sl@47
  2310
sl@100
  2311
        private void maskedTextBoxMinFontSize_TextChanged(object sender, EventArgs e)
sl@100
  2312
        {
sl@100
  2313
            if (maskedTextBoxMinFontSize.Text != "")
sl@100
  2314
            {
sl@100
  2315
                int minFontSize = Convert.ToInt32(maskedTextBoxMinFontSize.Text);
sl@100
  2316
sl@100
  2317
                if (minFontSize > 0)
sl@100
  2318
                {
sl@100
  2319
                    cds.MinFontSize = minFontSize;
sl@100
  2320
                    Properties.Settings.Default.Save();
StephaneLenclud@223
  2321
                    //We need to recreate our layout for that change to take effect
StephaneLenclud@223
  2322
                    UpdateTableLayoutPanel(iCurrentClientData);
sl@100
  2323
                }
sl@100
  2324
            }
sl@100
  2325
        }
sl@100
  2326
StephaneLenclud@106
  2327
StephaneLenclud@223
  2328
        private void maskedTextBoxScrollingSpeed_TextChanged(object sender, EventArgs e)
StephaneLenclud@223
  2329
        {
StephaneLenclud@223
  2330
            if (maskedTextBoxScrollingSpeed.Text != "")
StephaneLenclud@223
  2331
            {
StephaneLenclud@223
  2332
                int scrollingSpeed = Convert.ToInt32(maskedTextBoxScrollingSpeed.Text);
StephaneLenclud@223
  2333
StephaneLenclud@223
  2334
                if (scrollingSpeed > 0)
StephaneLenclud@223
  2335
                {
StephaneLenclud@223
  2336
                    cds.ScrollingSpeedInPixelsPerSecond = scrollingSpeed;
StephaneLenclud@223
  2337
                    Properties.Settings.Default.Save();
StephaneLenclud@223
  2338
                    //We need to recreate our layout for that change to take effect
StephaneLenclud@223
  2339
                    UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@223
  2340
                }
StephaneLenclud@223
  2341
            }
StephaneLenclud@223
  2342
        }
StephaneLenclud@106
  2343
sl@100
  2344
        private void textBoxScrollLoopSeparator_TextChanged(object sender, EventArgs e)
sl@100
  2345
        {
sl@100
  2346
            cds.Separator = textBoxScrollLoopSeparator.Text;
sl@100
  2347
            Properties.Settings.Default.Save();
StephaneLenclud@110
  2348
StephaneLenclud@223
  2349
            //Update our text fields
StephaneLenclud@223
  2350
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
StephaneLenclud@223
  2351
            {
StephaneLenclud@223
  2352
                ctrl.Separator = cds.Separator;
StephaneLenclud@223
  2353
            }
StephaneLenclud@110
  2354
sl@100
  2355
        }
sl@100
  2356
sl@52
  2357
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  2358
        {
sl@52
  2359
            iDisplay.PowerOn();
sl@52
  2360
        }
sl@52
  2361
sl@52
  2362
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  2363
        {
sl@52
  2364
            iDisplay.PowerOff();
sl@52
  2365
        }
sl@52
  2366
sl@53
  2367
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  2368
        {
StephaneLenclud@223
  2369
            ShowClock();
sl@53
  2370
        }
sl@53
  2371
sl@53
  2372
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  2373
        {
StephaneLenclud@223
  2374
            HideClock();
sl@53
  2375
        }
sl@88
  2376
sl@88
  2377
        private void buttonUpdate_Click(object sender, EventArgs e)
sl@88
  2378
        {
sl@88
  2379
            InstallUpdateSyncWithInfo();
sl@88
  2380
        }
sl@88
  2381
StephaneLenclud@223
  2382
        /// <summary>
StephaneLenclud@223
  2383
        /// 
StephaneLenclud@223
  2384
        /// </summary>
StephaneLenclud@223
  2385
        void ShowClock()
StephaneLenclud@223
  2386
        {
StephaneLenclud@223
  2387
            if (!iDisplay.IsOpen())
StephaneLenclud@223
  2388
            {
StephaneLenclud@223
  2389
                return;
StephaneLenclud@223
  2390
            }
StephaneLenclud@223
  2391
StephaneLenclud@223
  2392
            //Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@223
  2393
            iSkipFrameRendering = true;
StephaneLenclud@223
  2394
            //Clear our screen 
StephaneLenclud@223
  2395
            iDisplay.Clear();
StephaneLenclud@223
  2396
            iDisplay.SwapBuffers();
StephaneLenclud@223
  2397
            //Then show our clock
StephaneLenclud@223
  2398
            iDisplay.ShowClock();
StephaneLenclud@223
  2399
        }
StephaneLenclud@223
  2400
StephaneLenclud@223
  2401
        /// <summary>
StephaneLenclud@223
  2402
        /// 
StephaneLenclud@223
  2403
        /// </summary>
StephaneLenclud@223
  2404
        void HideClock()
StephaneLenclud@223
  2405
        {
StephaneLenclud@223
  2406
            if (!iDisplay.IsOpen())
StephaneLenclud@223
  2407
            {
StephaneLenclud@223
  2408
                return;
StephaneLenclud@223
  2409
            }
StephaneLenclud@223
  2410
StephaneLenclud@223
  2411
            //Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@223
  2412
            iSkipFrameRendering = false;
StephaneLenclud@223
  2413
            iDisplay.HideClock();
StephaneLenclud@223
  2414
        }
sl@88
  2415
sl@88
  2416
        private void InstallUpdateSyncWithInfo()
sl@88
  2417
        {
sl@88
  2418
            UpdateCheckInfo info = null;
sl@88
  2419
sl@88
  2420
            if (ApplicationDeployment.IsNetworkDeployed)
sl@88
  2421
            {
sl@88
  2422
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
sl@88
  2423
sl@88
  2424
                try
sl@88
  2425
                {
sl@88
  2426
                    info = ad.CheckForDetailedUpdate();
sl@88
  2427
sl@88
  2428
                }
sl@88
  2429
                catch (DeploymentDownloadException dde)
sl@88
  2430
                {
StephaneLenclud@223
  2431
                    MessageBox.Show(
StephaneLenclud@223
  2432
                        "The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " +
StephaneLenclud@223
  2433
                        dde.Message);
sl@88
  2434
                    return;
sl@88
  2435
                }
sl@88
  2436
                catch (InvalidDeploymentException ide)
sl@88
  2437
                {
StephaneLenclud@223
  2438
                    MessageBox.Show(
StephaneLenclud@223
  2439
                        "Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " +
StephaneLenclud@223
  2440
                        ide.Message);
sl@88
  2441
                    return;
sl@88
  2442
                }
sl@88
  2443
                catch (InvalidOperationException ioe)
sl@88
  2444
                {
StephaneLenclud@223
  2445
                    MessageBox.Show(
StephaneLenclud@223
  2446
                        "This application cannot be updated. It is likely not a ClickOnce application. Error: " +
StephaneLenclud@223
  2447
                        ioe.Message);
sl@88
  2448
                    return;
sl@88
  2449
                }
sl@88
  2450
StephaneLenclud@223
  2451
                if (info.UpdateAvailable)
StephaneLenclud@223
  2452
                {
StephaneLenclud@223
  2453
                    Boolean doUpdate = true;
StephaneLenclud@223
  2454
StephaneLenclud@223
  2455
                    if (!info.IsUpdateRequired)
StephaneLenclud@223
  2456
                    {
StephaneLenclud@223
  2457
                        DialogResult dr =
StephaneLenclud@223
  2458
                            MessageBox.Show("An update is available. Would you like to update the application now?",
StephaneLenclud@223
  2459
                                "Update Available", MessageBoxButtons.OKCancel);
StephaneLenclud@223
  2460
                        if (!(DialogResult.OK == dr))
StephaneLenclud@223
  2461
                        {
StephaneLenclud@223
  2462
                            doUpdate = false;
StephaneLenclud@223
  2463
                        }
StephaneLenclud@223
  2464
                    }
StephaneLenclud@223
  2465
                    else
StephaneLenclud@223
  2466
                    {
StephaneLenclud@223
  2467
                        // Display a message that the application MUST reboot. Display the minimum required version.
StephaneLenclud@223
  2468
                        MessageBox.Show("This application has detected a mandatory update from your current " +
StephaneLenclud@223
  2469
                                        "version to version " + info.MinimumRequiredVersion.ToString() +
StephaneLenclud@223
  2470
                                        ". The application will now install the update and restart.",
StephaneLenclud@223
  2471
                            "Update Available", MessageBoxButtons.OK,
StephaneLenclud@223
  2472
                            MessageBoxIcon.Information);
StephaneLenclud@223
  2473
                    }
StephaneLenclud@223
  2474
StephaneLenclud@223
  2475
                    if (doUpdate)
StephaneLenclud@223
  2476
                    {
StephaneLenclud@223
  2477
                        try
StephaneLenclud@223
  2478
                        {
StephaneLenclud@223
  2479
                            ad.Update();
StephaneLenclud@223
  2480
                            MessageBox.Show("The application has been upgraded, and will now restart.");
StephaneLenclud@223
  2481
                            Application.Restart();
StephaneLenclud@223
  2482
                        }
StephaneLenclud@223
  2483
                        catch (DeploymentDownloadException dde)
StephaneLenclud@223
  2484
                        {
StephaneLenclud@223
  2485
                            MessageBox.Show(
StephaneLenclud@223
  2486
                                "Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " +
StephaneLenclud@223
  2487
                                dde);
StephaneLenclud@223
  2488
                            return;
StephaneLenclud@223
  2489
                        }
StephaneLenclud@223
  2490
                    }
StephaneLenclud@223
  2491
                }
StephaneLenclud@223
  2492
                else
StephaneLenclud@223
  2493
                {
StephaneLenclud@223
  2494
                    MessageBox.Show("You are already running the latest version.", "Application up-to-date");
StephaneLenclud@223
  2495
                }
sl@88
  2496
            }
sl@88
  2497
        }
sl@92
  2498
sl@94
  2499
StephaneLenclud@223
  2500
        /// <summary>
StephaneLenclud@223
  2501
        /// Used to
StephaneLenclud@223
  2502
        /// </summary>
StephaneLenclud@223
  2503
        private void SysTrayHideShow()
StephaneLenclud@223
  2504
        {
StephaneLenclud@223
  2505
            Visible = !Visible;
StephaneLenclud@223
  2506
            if (Visible)
StephaneLenclud@223
  2507
            {
StephaneLenclud@223
  2508
                Activate();
StephaneLenclud@223
  2509
                WindowState = FormWindowState.Normal;
StephaneLenclud@223
  2510
            }
StephaneLenclud@223
  2511
        }
StephaneLenclud@223
  2512
StephaneLenclud@223
  2513
        /// <summary>
StephaneLenclud@223
  2514
        /// Use to handle minimize events.
StephaneLenclud@223
  2515
        /// </summary>
StephaneLenclud@223
  2516
        /// <param name="sender"></param>
StephaneLenclud@223
  2517
        /// <param name="e"></param>
StephaneLenclud@223
  2518
        private void MainForm_SizeChanged(object sender, EventArgs e)
StephaneLenclud@223
  2519
        {
StephaneLenclud@223
  2520
            if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
StephaneLenclud@223
  2521
            {
StephaneLenclud@223
  2522
                if (Visible)
StephaneLenclud@223
  2523
                {
StephaneLenclud@223
  2524
                    SysTrayHideShow();
StephaneLenclud@223
  2525
                }
StephaneLenclud@223
  2526
            }
StephaneLenclud@223
  2527
        }
StephaneLenclud@223
  2528
StephaneLenclud@223
  2529
        /// <summary>
StephaneLenclud@223
  2530
        /// 
StephaneLenclud@223
  2531
        /// </summary>
StephaneLenclud@223
  2532
        /// <param name="sender"></param>
StephaneLenclud@223
  2533
        /// <param name="e"></param>
StephaneLenclud@223
  2534
        private void tableLayoutPanel_SizeChanged(object sender, EventArgs e)
StephaneLenclud@223
  2535
        {
StephaneLenclud@223
  2536
            //Our table layout size has changed which means our display size has changed.
StephaneLenclud@223
  2537
            //We need to re-create our bitmap.
StephaneLenclud@223
  2538
            iCreateBitmap = true;
StephaneLenclud@223
  2539
        }
StephaneLenclud@223
  2540
StephaneLenclud@223
  2541
        /// <summary>
StephaneLenclud@167
  2542
        /// Broadcast messages to subscribers.
StephaneLenclud@167
  2543
        /// </summary>
StephaneLenclud@167
  2544
        /// <param name="message"></param>
StephaneLenclud@167
  2545
        protected override void WndProc(ref Message aMessage)
StephaneLenclud@167
  2546
        {
StephaneLenclud@223
  2547
            if (OnWndProc != null)
StephaneLenclud@167
  2548
            {
StephaneLenclud@167