Clients/Message/FormClientMessage.cs
author StephaneLenclud
Wed, 17 Aug 2016 16:39:36 +0200
changeset 237 1a1c2ae3a29c
permissions -rw-r--r--
Adding HID consumer control event.
StephaneLenclud@225
     1
//
StephaneLenclud@225
     2
// Copyright (C) 2014-2016 Stéphane Lenclud.
StephaneLenclud@225
     3
//
StephaneLenclud@225
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@225
     5
//
StephaneLenclud@225
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@225
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@225
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@225
     9
// (at your option) any later version.
StephaneLenclud@225
    10
//
StephaneLenclud@225
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@225
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@225
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@225
    14
// GNU General Public License for more details.
StephaneLenclud@225
    15
//
StephaneLenclud@225
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@225
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@225
    18
//
StephaneLenclud@225
    19
StephaneLenclud@225
    20
using System;
StephaneLenclud@225
    21
using System.Collections.Generic;
StephaneLenclud@225
    22
using System.ComponentModel;
StephaneLenclud@225
    23
using System.Data;
StephaneLenclud@225
    24
using System.Drawing;
StephaneLenclud@225
    25
using System.Linq;
StephaneLenclud@225
    26
using System.Text;
StephaneLenclud@225
    27
using System.Threading.Tasks;
StephaneLenclud@225
    28
using System.Windows.Forms;
StephaneLenclud@225
    29
using System.Diagnostics;
StephaneLenclud@225
    30
using SharpLib.Display;
StephaneLenclud@225
    31
StephaneLenclud@225
    32
StephaneLenclud@225
    33
namespace SharpDisplayClientMessage
StephaneLenclud@225
    34
{
StephaneLenclud@225
    35
StephaneLenclud@225
    36
    /// <summary>
StephaneLenclud@225
    37
    /// Sharp Display Client designed to act as an idle client.
StephaneLenclud@225
    38
    /// It should take care of screen saving and other such concerns.
StephaneLenclud@225
    39
    /// </summary>
StephaneLenclud@225
    40
    public partial class FormClientMessage : Form
StephaneLenclud@225
    41
    {
StephaneLenclud@225
    42
        public StartParams Params { get; set; }
StephaneLenclud@225
    43
StephaneLenclud@225
    44
        Client iClient;
StephaneLenclud@225
    45
        ContentAlignment iAlignment;
StephaneLenclud@225
    46
        TextField iPrimaryTextField;
StephaneLenclud@225
    47
        TextField iSecondaryTextField;
StephaneLenclud@225
    48
StephaneLenclud@225
    49
StephaneLenclud@225
    50
        public delegate void CloseDelegate();
StephaneLenclud@225
    51
        public delegate void CloseConnectionDelegate();
StephaneLenclud@225
    52
StephaneLenclud@225
    53
StephaneLenclud@225
    54
        public FormClientMessage()
StephaneLenclud@225
    55
        {
StephaneLenclud@225
    56
            InitializeComponent();
StephaneLenclud@225
    57
        }
StephaneLenclud@225
    58
StephaneLenclud@225
    59
        /// <summary>
StephaneLenclud@225
    60
        /// 
StephaneLenclud@225
    61
        /// </summary>
StephaneLenclud@225
    62
        /// <param name="sender"></param>
StephaneLenclud@225
    63
        /// <param name="e"></param>
StephaneLenclud@225
    64
        private void FormClientMessage_Load(object sender, EventArgs e)
StephaneLenclud@225
    65
        {
StephaneLenclud@225
    66
            //Prevents showing in the Open Task view (Windows Key + Tab)
StephaneLenclud@225
    67
            Visible = false;
StephaneLenclud@225
    68
StephaneLenclud@225
    69
            //Display client
StephaneLenclud@225
    70
            iClient = new Client();
StephaneLenclud@225
    71
            iClient.CloseOrderEvent += OnCloseOrder;
StephaneLenclud@225
    72
            iClient.Open();
StephaneLenclud@225
    73
            iClient.SetName("Message");
StephaneLenclud@225
    74
            iClient.SetPriority(Params.Priority);
StephaneLenclud@225
    75
            SetupDisplayClient();
StephaneLenclud@225
    76
StephaneLenclud@225
    77
            //Timer
StephaneLenclud@225
    78
            iTimer.Interval = Params.DurationInMs;
StephaneLenclud@225
    79
            iTimer.Start();
StephaneLenclud@225
    80
        }
StephaneLenclud@225
    81
StephaneLenclud@225
    82
StephaneLenclud@225
    83
        /// <summary>
StephaneLenclud@225
    84
        /// 
StephaneLenclud@225
    85
        /// </summary>
StephaneLenclud@225
    86
        /// <returns></returns>
StephaneLenclud@225
    87
        public bool IsClientReady()
StephaneLenclud@225
    88
        {
StephaneLenclud@225
    89
            return (iClient != null && iClient.IsReady());
StephaneLenclud@225
    90
        }
StephaneLenclud@225
    91
StephaneLenclud@225
    92
        /// <summary>
StephaneLenclud@225
    93
        /// 
StephaneLenclud@225
    94
        /// </summary>
StephaneLenclud@225
    95
        public void SetupDisplayClient()
StephaneLenclud@225
    96
        {
StephaneLenclud@225
    97
            //Setup our layout
StephaneLenclud@225
    98
StephaneLenclud@225
    99
            //Set one column one line layout
StephaneLenclud@225
   100
StephaneLenclud@225
   101
            //Setup our fields
StephaneLenclud@225
   102
            iAlignment = ContentAlignment.MiddleCenter;
StephaneLenclud@225
   103
            iPrimaryTextField = new TextField(Params.PrimaryText, iAlignment, 0, 0);
StephaneLenclud@225
   104
            iSecondaryTextField = new TextField(Params.SecondaryText, iAlignment, 0, 1);
StephaneLenclud@225
   105
StephaneLenclud@225
   106
            //Set our fields
StephaneLenclud@225
   107
            if (string.IsNullOrEmpty(Params.SecondaryText))
StephaneLenclud@225
   108
            {
StephaneLenclud@225
   109
                //One field layout
StephaneLenclud@225
   110
                TableLayout layout = new TableLayout(1, 1);
StephaneLenclud@225
   111
                iClient.SetLayout(layout);
StephaneLenclud@225
   112
StephaneLenclud@225
   113
                iClient.CreateFields(new DataField[]
StephaneLenclud@225
   114
                {
StephaneLenclud@225
   115
                iPrimaryTextField
StephaneLenclud@225
   116
                });
StephaneLenclud@225
   117
            }
StephaneLenclud@225
   118
            else
StephaneLenclud@225
   119
            {
StephaneLenclud@225
   120
                //Two fields layout
StephaneLenclud@225
   121
                TableLayout layout = new TableLayout(1, 2);
StephaneLenclud@225
   122
                iClient.SetLayout(layout);
StephaneLenclud@225
   123
StephaneLenclud@225
   124
                iClient.CreateFields(new DataField[]
StephaneLenclud@225
   125
                {
StephaneLenclud@225
   126
                iPrimaryTextField,
StephaneLenclud@225
   127
                iSecondaryTextField
StephaneLenclud@225
   128
                });
StephaneLenclud@225
   129
            }
StephaneLenclud@225
   130
        }
StephaneLenclud@225
   131
StephaneLenclud@225
   132
        public void OnCloseOrder()
StephaneLenclud@225
   133
        {
StephaneLenclud@225
   134
            CloseThreadSafe();
StephaneLenclud@225
   135
        }
StephaneLenclud@225
   136
StephaneLenclud@225
   137
        /// <summary>
StephaneLenclud@225
   138
        ///
StephaneLenclud@225
   139
        /// </summary>
StephaneLenclud@225
   140
        public void CloseThreadSafe()
StephaneLenclud@225
   141
        {
StephaneLenclud@225
   142
            if (this.InvokeRequired)
StephaneLenclud@225
   143
            {
StephaneLenclud@225
   144
                //Not in the proper thread, invoke ourselves
StephaneLenclud@225
   145
                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
StephaneLenclud@225
   146
                this.Invoke(d, new object[] { });
StephaneLenclud@225
   147
            }
StephaneLenclud@225
   148
            else
StephaneLenclud@225
   149
            {
StephaneLenclud@225
   150
                //We are in the proper thread
StephaneLenclud@225
   151
                Close();
StephaneLenclud@225
   152
            }
StephaneLenclud@225
   153
        }
StephaneLenclud@225
   154
StephaneLenclud@225
   155
        /// <summary>
StephaneLenclud@225
   156
        ///
StephaneLenclud@225
   157
        /// </summary>
StephaneLenclud@225
   158
        public void CloseConnectionThreadSafe()
StephaneLenclud@225
   159
        {
StephaneLenclud@225
   160
            if (this.InvokeRequired)
StephaneLenclud@225
   161
            {
StephaneLenclud@225
   162
                //Not in the proper thread, invoke ourselves
StephaneLenclud@225
   163
                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
StephaneLenclud@225
   164
                this.Invoke(d, new object[] { });
StephaneLenclud@225
   165
            }
StephaneLenclud@225
   166
            else
StephaneLenclud@225
   167
            {
StephaneLenclud@225
   168
                //We are in the proper thread
StephaneLenclud@225
   169
                if (IsClientReady())
StephaneLenclud@225
   170
                {
StephaneLenclud@225
   171
                    string sessionId = iClient.SessionId;
StephaneLenclud@225
   172
                    Trace.TraceInformation("Closing client: " + sessionId);
StephaneLenclud@225
   173
                    iClient.Close();
StephaneLenclud@225
   174
                    Trace.TraceInformation("Closed client: " + sessionId);
StephaneLenclud@225
   175
                }
StephaneLenclud@225
   176
StephaneLenclud@225
   177
                iClient = null;
StephaneLenclud@225
   178
            }
StephaneLenclud@225
   179
        }
StephaneLenclud@225
   180
StephaneLenclud@225
   181
        /// <summary>
StephaneLenclud@225
   182
        /// 
StephaneLenclud@225
   183
        /// </summary>
StephaneLenclud@225
   184
        /// <param name="sender"></param>
StephaneLenclud@225
   185
        /// <param name="e"></param>
StephaneLenclud@225
   186
        private void FormClientMessage_FormClosing(object sender, FormClosingEventArgs e)
StephaneLenclud@225
   187
        {
StephaneLenclud@225
   188
            CloseConnectionThreadSafe();
StephaneLenclud@225
   189
        }
StephaneLenclud@225
   190
StephaneLenclud@225
   191
StephaneLenclud@225
   192
        /// <summary>
StephaneLenclud@225
   193
        /// 
StephaneLenclud@225
   194
        /// </summary>
StephaneLenclud@225
   195
        /// <param name="sender"></param>
StephaneLenclud@225
   196
        /// <param name="e"></param>
StephaneLenclud@225
   197
        private void iTimer_Tick(object sender, EventArgs e)
StephaneLenclud@225
   198
        {
StephaneLenclud@225
   199
            Close();
StephaneLenclud@225
   200
        }
StephaneLenclud@225
   201
StephaneLenclud@225
   202
StephaneLenclud@225
   203
        /// <summary>
StephaneLenclud@225
   204
        /// 
StephaneLenclud@225
   205
        /// </summary>
StephaneLenclud@225
   206
        /// <param name="sender"></param>
StephaneLenclud@225
   207
        /// <param name="e"></param>
StephaneLenclud@225
   208
        private void FormClientMessage_Shown(object sender, EventArgs e)
StephaneLenclud@225
   209
        {
StephaneLenclud@225
   210
            //Visible = false;
StephaneLenclud@225
   211
        }
StephaneLenclud@225
   212
StephaneLenclud@225
   213
    }
StephaneLenclud@225
   214
StephaneLenclud@225
   215
}