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