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