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