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