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