Client/MainForm.cs
author StephaneLenclud
Thu, 21 Jan 2016 18:16:15 +0100
changeset 179 533eb8a80d56
parent 175 391bce3c8368
child 183 e9a5dc5092fd
permissions -rw-r--r--
Server: Adding recording icon.
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@18
    21
using System.Collections.Generic;
sl@18
    22
using System.ComponentModel;
sl@18
    23
using System.Data;
sl@18
    24
using System.Drawing;
sl@18
    25
using System.Linq;
sl@18
    26
using System.Text;
sl@18
    27
using System.Threading.Tasks;
sl@18
    28
using System.Windows.Forms;
sl@18
    29
using System.ServiceModel;
sl@18
    30
using System.ServiceModel.Channels;
sl@31
    31
using System.Diagnostics;
StephaneLenclud@171
    32
using SharpLib.Display;
sl@20
    33
sl@18
    34
sl@18
    35
namespace SharpDisplayClient
sl@18
    36
{
sl@18
    37
    public partial class MainForm : Form
sl@18
    38
    {
StephaneLenclud@106
    39
		public StartParams Params { get; set; }
StephaneLenclud@106
    40
StephaneLenclud@106
    41
		//
StephaneLenclud@171
    42
        Client iClient;
StephaneLenclud@106
    43
		//
sl@43
    44
        ContentAlignment Alignment;
StephaneLenclud@172
    45
        TextField iTextFieldTop;
sl@18
    46
StephaneLenclud@106
    47
		
StephaneLenclud@106
    48
		/// <summary>
StephaneLenclud@106
    49
		/// Constructor
StephaneLenclud@106
    50
		/// </summary>
sl@18
    51
        public MainForm()
sl@18
    52
        {
sl@18
    53
            InitializeComponent();
sl@43
    54
            Alignment = ContentAlignment.MiddleLeft;
StephaneLenclud@176
    55
            iTextFieldTop = new TextField();
sl@18
    56
        }
sl@18
    57
StephaneLenclud@171
    58
        public void OnCloseOrder()
StephaneLenclud@171
    59
        {
StephaneLenclud@171
    60
            CloseThreadSafe();
StephaneLenclud@171
    61
        }
StephaneLenclud@171
    62
StephaneLenclud@171
    63
        /// <summary>
StephaneLenclud@171
    64
        /// 
StephaneLenclud@171
    65
        /// </summary>
StephaneLenclud@171
    66
        /// <param name="sender"></param>
StephaneLenclud@171
    67
        /// <param name="e"></param>
sl@18
    68
        private void MainForm_Load(object sender, EventArgs e)
sl@18
    69
        {
StephaneLenclud@171
    70
            iClient = new Client();
StephaneLenclud@171
    71
            iClient.CloseOrderEvent += OnCloseOrder;
sl@73
    72
            iClient.Open();
sl@18
    73
sl@29
    74
            //Connect using unique name
sl@32
    75
            //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
sl@32
    76
            string name = "Client-" + (iClient.ClientCount() - 1);
sl@32
    77
            iClient.SetName(name);
sl@30
    78
            //Text = Text + ": " + name;
sl@32
    79
            Text = "[[" + name + "]]  " + iClient.SessionId;
sl@18
    80
sl@33
    81
            //
sl@33
    82
            textBoxTop.Text = iClient.Name;
sl@33
    83
            textBoxBottom.Text = iClient.SessionId;
sl@33
    84
StephaneLenclud@106
    85
			if (Params != null)
StephaneLenclud@106
    86
			{
StephaneLenclud@106
    87
				//Parameters where specified use them
StephaneLenclud@106
    88
				if (Params.TopText != "")
StephaneLenclud@106
    89
				{
StephaneLenclud@106
    90
					textBoxTop.Text = Params.TopText;
StephaneLenclud@106
    91
				}
StephaneLenclud@106
    92
StephaneLenclud@106
    93
				if (Params.BottomText != "")
StephaneLenclud@106
    94
				{
StephaneLenclud@106
    95
					textBoxBottom.Text = Params.BottomText;
StephaneLenclud@106
    96
				}
StephaneLenclud@106
    97
StephaneLenclud@106
    98
				Location = Params.Location;
StephaneLenclud@106
    99
				//
StephaneLenclud@106
   100
				SetBasicLayoutAndText();
StephaneLenclud@106
   101
			}
StephaneLenclud@106
   102
sl@18
   103
        }
sl@21
   104
sl@31
   105
sl@60
   106
sl@31
   107
        public delegate void CloseConnectionDelegate();
sl@31
   108
        public delegate void CloseDelegate();
sl@31
   109
sl@31
   110
        /// <summary>
sl@60
   111
        ///
sl@31
   112
        /// </summary>
sl@31
   113
        public void CloseConnectionThreadSafe()
sl@21
   114
        {
sl@31
   115
            if (this.InvokeRequired)
sl@29
   116
            {
sl@31
   117
                //Not in the proper thread, invoke ourselves
sl@31
   118
                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
sl@31
   119
                this.Invoke(d, new object[] { });
sl@29
   120
            }
sl@31
   121
            else
sl@31
   122
            {
sl@31
   123
                //We are in the proper thread
sl@31
   124
                if (IsClientReady())
sl@31
   125
                {
sl@74
   126
                    string sessionId = iClient.SessionId;
sl@74
   127
                    Trace.TraceInformation("Closing client: " + sessionId);
sl@31
   128
                    iClient.Close();
sl@74
   129
                    Trace.TraceInformation("Closed client: " + sessionId);
sl@31
   130
                }
sl@29
   131
sl@31
   132
                iClient = null;
sl@31
   133
            }
sl@26
   134
        }
sl@26
   135
sl@31
   136
        /// <summary>
sl@60
   137
        ///
sl@31
   138
        /// </summary>
sl@31
   139
        public void CloseThreadSafe()
sl@31
   140
        {
sl@31
   141
            if (this.InvokeRequired)
sl@31
   142
            {
sl@31
   143
                //Not in the proper thread, invoke ourselves
sl@31
   144
                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
sl@31
   145
                this.Invoke(d, new object[] { });
sl@31
   146
            }
sl@31
   147
            else
sl@31
   148
            {
sl@31
   149
                //We are in the proper thread
sl@31
   150
                Close();
sl@31
   151
            }
sl@31
   152
        }
sl@31
   153
sl@31
   154
sl@26
   155
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@26
   156
        {
sl@31
   157
            CloseConnectionThreadSafe();
sl@29
   158
        }
sl@29
   159
sl@29
   160
        public bool IsClientReady()
sl@29
   161
        {
sl@73
   162
            return (iClient != null && iClient.IsReady());
sl@21
   163
        }
sl@43
   164
sl@43
   165
        private void buttonAlignLeft_Click(object sender, EventArgs e)
sl@43
   166
        {
sl@43
   167
            Alignment = ContentAlignment.MiddleLeft;
sl@43
   168
            textBoxTop.TextAlign = HorizontalAlignment.Left;
sl@43
   169
            textBoxBottom.TextAlign = HorizontalAlignment.Left;
sl@43
   170
        }
sl@43
   171
sl@43
   172
        private void buttonAlignCenter_Click(object sender, EventArgs e)
sl@43
   173
        {
sl@43
   174
            Alignment = ContentAlignment.MiddleCenter;
sl@43
   175
            textBoxTop.TextAlign = HorizontalAlignment.Center;
sl@43
   176
            textBoxBottom.TextAlign = HorizontalAlignment.Center;
sl@43
   177
        }
sl@43
   178
sl@43
   179
        private void buttonAlignRight_Click(object sender, EventArgs e)
sl@43
   180
        {
sl@43
   181
            Alignment = ContentAlignment.MiddleRight;
sl@43
   182
            textBoxTop.TextAlign = HorizontalAlignment.Right;
sl@43
   183
            textBoxBottom.TextAlign = HorizontalAlignment.Right;
sl@43
   184
        }
sl@43
   185
sl@43
   186
        private void buttonSetTopText_Click(object sender, EventArgs e)
sl@43
   187
        {
sl@43
   188
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   189
            iTextFieldTop.Text = textBoxTop.Text;
sl@60
   190
            iTextFieldTop.Alignment = Alignment;
sl@81
   191
            bool res = iClient.SetField(iTextFieldTop);
sl@81
   192
sl@81
   193
            if (!res)
sl@81
   194
            {
sl@81
   195
                MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@81
   196
            }
sl@81
   197
sl@81
   198
sl@43
   199
        }
sl@43
   200
sl@43
   201
        private void buttonSetText_Click(object sender, EventArgs e)
sl@43
   202
        {
StephaneLenclud@106
   203
			SetBasicLayoutAndText();
StephaneLenclud@106
   204
        }
sl@78
   205
StephaneLenclud@106
   206
		void SetBasicLayoutAndText()
StephaneLenclud@106
   207
		{
StephaneLenclud@106
   208
			//Set one column two lines layout
StephaneLenclud@106
   209
			TableLayout layout = new TableLayout(1, 2);
StephaneLenclud@106
   210
			iClient.SetLayout(layout);
StephaneLenclud@106
   211
StephaneLenclud@106
   212
			//Set our fields
StephaneLenclud@106
   213
			iClient.CreateFields(new DataField[]
sl@60
   214
            {
StephaneLenclud@176
   215
                new TextField(textBoxTop.Text, Alignment, 0, 0),
StephaneLenclud@176
   216
                new TextField(textBoxBottom.Text, Alignment, 0, 1)
sl@43
   217
            });
StephaneLenclud@106
   218
StephaneLenclud@106
   219
		}
sl@62
   220
sl@62
   221
        private void buttonLayoutUpdate_Click(object sender, EventArgs e)
sl@62
   222
        {
sl@65
   223
            //Define a 2 by 2 layout
sl@62
   224
            TableLayout layout = new TableLayout(2,2);
sl@65
   225
            //Second column only takes up 25%
sl@63
   226
            layout.Columns[1].Width = 25F;
sl@65
   227
            //Send layout to server
sl@62
   228
            iClient.SetLayout(layout);
StephaneLenclud@158
   229
StephaneLenclud@174
   230
            //
StephaneLenclud@174
   231
            RecordingField recording = new RecordingField();
StephaneLenclud@174
   232
            recording.IsActive = true;
StephaneLenclud@174
   233
StephaneLenclud@158
   234
            //Set texts
StephaneLenclud@158
   235
            iClient.CreateFields(new DataField[]
StephaneLenclud@158
   236
            {                
StephaneLenclud@176
   237
                new TextField(textBoxTop.Text, Alignment, 0, 0),
StephaneLenclud@176
   238
                new TextField(textBoxBottom.Text, Alignment, 0, 1),
StephaneLenclud@176
   239
                new TextField("Third text field", Alignment, 1, 0),
StephaneLenclud@176
   240
                new TextField("Forth text field", Alignment, 1, 1),
StephaneLenclud@175
   241
                recording
StephaneLenclud@158
   242
            });
StephaneLenclud@158
   243
sl@62
   244
        }
sl@67
   245
sl@68
   246
        private void buttonSetBitmap_Click(object sender, EventArgs e)
sl@67
   247
        {
sl@68
   248
            int x1 = 0;
sl@68
   249
            int y1 = 0;
sl@68
   250
            int x2 = 256;
sl@68
   251
            int y2 = 32;
sl@68
   252
sl@68
   253
            Bitmap bitmap = new Bitmap(x2,y2);
sl@67
   254
            Pen blackPen = new Pen(Color.Black, 3);
sl@67
   255
sl@67
   256
            // Draw line to screen.
sl@67
   257
            using (var graphics = Graphics.FromImage(bitmap))
sl@67
   258
            {
sl@67
   259
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@68
   260
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@67
   261
            }
sl@67
   262
StephaneLenclud@176
   263
            DataField field = new BitmapField(bitmap);
sl@80
   264
            //field.ColumnSpan = 2;
sl@74
   265
            iClient.SetField(field);
sl@70
   266
        }
sl@70
   267
sl@71
   268
        private void buttonBitmapLayout_Click(object sender, EventArgs e)
sl@71
   269
        {
sl@71
   270
            SetLayoutWithBitmap();
sl@71
   271
        }
sl@71
   272
sl@71
   273
        /// <summary>
sl@71
   274
        /// Define a layout with a bitmap field on the left and two lines of text on the right.
sl@71
   275
        /// </summary>
sl@71
   276
        private void SetLayoutWithBitmap()
sl@70
   277
        {
sl@70
   278
            //Define a 2 by 2 layout
sl@70
   279
            TableLayout layout = new TableLayout(2, 2);
sl@71
   280
            //First column only takes 25%
sl@70
   281
            layout.Columns[0].Width = 25F;
sl@73
   282
            //Second column takes up 75%
sl@70
   283
            layout.Columns[1].Width = 75F;
sl@70
   284
            //Send layout to server
sl@70
   285
            iClient.SetLayout(layout);
sl@70
   286
sl@70
   287
            //Set a bitmap for our first field
sl@70
   288
            int x1 = 0;
sl@70
   289
            int y1 = 0;
sl@70
   290
            int x2 = 64;
sl@70
   291
            int y2 = 64;
sl@70
   292
sl@70
   293
            Bitmap bitmap = new Bitmap(x2, y2);
sl@70
   294
            Pen blackPen = new Pen(Color.Black, 3);
sl@70
   295
sl@70
   296
            // Draw line to screen.
sl@70
   297
            using (var graphics = Graphics.FromImage(bitmap))
sl@70
   298
            {
sl@70
   299
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@70
   300
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@70
   301
            }
sl@70
   302
sl@71
   303
            //Create a bitmap field from the bitmap we just created
sl@71
   304
            //We want our bitmap field to span across two rows
StephaneLenclud@176
   305
            BitmapField bitmapField = new BitmapField(bitmap, 0, 0, 1, 2);
StephaneLenclud@176
   306
            
sl@70
   307
            //Set texts
sl@80
   308
            iClient.CreateFields(new DataField[]
sl@70
   309
            {
StephaneLenclud@176
   310
                bitmapField,
StephaneLenclud@176
   311
                new TextField(textBoxTop.Text, Alignment, 1, 0),
StephaneLenclud@176
   312
                new TextField(textBoxBottom.Text, Alignment, 1, 1)
sl@70
   313
            });
sl@70
   314
sl@67
   315
        }
sl@79
   316
sl@79
   317
        private void buttonIndicatorsLayout_Click(object sender, EventArgs e)
sl@79
   318
        {
sl@79
   319
            //Define a 2 by 4 layout
sl@79
   320
            TableLayout layout = new TableLayout(2, 4);
sl@79
   321
            //First column
sl@79
   322
            layout.Columns[0].Width = 87.5F;
sl@79
   323
            //Second column
sl@79
   324
            layout.Columns[1].Width = 12.5F;
sl@79
   325
            //Send layout to server
sl@79
   326
            iClient.SetLayout(layout);
sl@79
   327
sl@79
   328
            //Create a bitmap for our indicators field
sl@79
   329
            int x1 = 0;
sl@79
   330
            int y1 = 0;
sl@79
   331
            int x2 = 32;
sl@79
   332
            int y2 = 16;
sl@79
   333
sl@79
   334
            Bitmap bitmap = new Bitmap(x2, y2);
sl@79
   335
            Pen blackPen = new Pen(Color.Black, 3);
sl@79
   336
sl@79
   337
            // Draw line to screen.
sl@79
   338
            using (var graphics = Graphics.FromImage(bitmap))
sl@79
   339
            {
sl@79
   340
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@79
   341
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@79
   342
            }
sl@79
   343
sl@79
   344
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@176
   345
            DataField indicator1 = new BitmapField(bitmap, 1, 0);
sl@79
   346
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@176
   347
            DataField indicator2 = new BitmapField(bitmap, 1, 1);
sl@79
   348
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@176
   349
            DataField indicator3 = new BitmapField(bitmap, 1, 2);
sl@79
   350
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@176
   351
            DataField indicator4 = new BitmapField(bitmap, 1, 3);
sl@79
   352
sl@79
   353
            //
StephaneLenclud@176
   354
            TextField textFieldTop = new TextField(textBoxTop.Text, Alignment, 0, 0, 1, 2);
StephaneLenclud@176
   355
            TextField textFieldBottom = new TextField(textBoxBottom.Text, Alignment, 0, 2, 1, 2);
sl@79
   356
sl@79
   357
            //Set texts
sl@80
   358
            iClient.CreateFields(new DataField[]
sl@79
   359
            {
sl@79
   360
                textFieldTop,
StephaneLenclud@175
   361
                textFieldBottom,
sl@79
   362
                indicator1,
StephaneLenclud@175
   363
                indicator2,                
sl@79
   364
                indicator3,
sl@79
   365
                indicator4
sl@79
   366
            });
sl@79
   367
sl@79
   368
        }
sl@80
   369
sl@80
   370
        private void buttonUpdateTexts_Click(object sender, EventArgs e)
sl@80
   371
        {
sl@80
   372
sl@80
   373
            bool res = iClient.SetFields(new DataField[]
sl@80
   374
            {
StephaneLenclud@176
   375
                new TextField(textBoxTop.Text, Alignment,0,0),
StephaneLenclud@176
   376
                new TextField(textBoxBottom.Text, Alignment,0,1)
sl@80
   377
            });
sl@80
   378
sl@80
   379
            if (!res)
sl@80
   380
            {
sl@80
   381
                MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@80
   382
            }
sl@80
   383
sl@80
   384
        }
StephaneLenclud@108
   385
StephaneLenclud@108
   386
		private void buttonLayoutOneTextField_Click(object sender, EventArgs e)
StephaneLenclud@108
   387
		{
StephaneLenclud@172
   388
			//Set one column one line layout
StephaneLenclud@108
   389
			TableLayout layout = new TableLayout(1, 1);
StephaneLenclud@108
   390
			iClient.SetLayout(layout);
StephaneLenclud@108
   391
StephaneLenclud@108
   392
			//Set our fields
StephaneLenclud@108
   393
			iClient.CreateFields(new DataField[]
StephaneLenclud@108
   394
            {
StephaneLenclud@176
   395
                new TextField(textBoxTop.Text, Alignment)
StephaneLenclud@108
   396
            });
StephaneLenclud@108
   397
		}
sl@18
   398
    }
sl@18
   399
}