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