Client/MainForm.cs
author StephaneLenclud
Mon, 23 Nov 2015 18:35:24 +0100
changeset 173 d33776198be5
parent 171 151e11cac3b2
child 174 f5a6cec31e81
permissions -rw-r--r--
Migrating to SharpLibNotification.
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@172
    55
            iTextFieldTop = new TextField(0);
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@172
   215
                new TextField(0, textBoxTop.Text, Alignment),
StephaneLenclud@172
   216
                new TextField(1, textBoxBottom.Text, Alignment)
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@158
   230
            //Set texts
StephaneLenclud@158
   231
            iClient.CreateFields(new DataField[]
StephaneLenclud@158
   232
            {                
StephaneLenclud@172
   233
                new TextField(0, textBoxTop.Text, Alignment),
StephaneLenclud@172
   234
                new TextField(1, textBoxBottom.Text, Alignment),
StephaneLenclud@172
   235
                new TextField(2, "Third text field", Alignment),
StephaneLenclud@172
   236
                new TextField(3, "Forth text field", Alignment)
StephaneLenclud@158
   237
            });
StephaneLenclud@158
   238
sl@62
   239
        }
sl@67
   240
sl@68
   241
        private void buttonSetBitmap_Click(object sender, EventArgs e)
sl@67
   242
        {
sl@68
   243
            int x1 = 0;
sl@68
   244
            int y1 = 0;
sl@68
   245
            int x2 = 256;
sl@68
   246
            int y2 = 32;
sl@68
   247
sl@68
   248
            Bitmap bitmap = new Bitmap(x2,y2);
sl@67
   249
            Pen blackPen = new Pen(Color.Black, 3);
sl@67
   250
sl@67
   251
            // Draw line to screen.
sl@67
   252
            using (var graphics = Graphics.FromImage(bitmap))
sl@67
   253
            {
sl@67
   254
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@68
   255
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@67
   256
            }
sl@67
   257
StephaneLenclud@172
   258
            DataField field = new BitmapField(0, bitmap);
sl@80
   259
            //field.ColumnSpan = 2;
sl@74
   260
            iClient.SetField(field);
sl@70
   261
        }
sl@70
   262
sl@71
   263
        private void buttonBitmapLayout_Click(object sender, EventArgs e)
sl@71
   264
        {
sl@71
   265
            SetLayoutWithBitmap();
sl@71
   266
        }
sl@71
   267
sl@71
   268
        /// <summary>
sl@71
   269
        /// Define a layout with a bitmap field on the left and two lines of text on the right.
sl@71
   270
        /// </summary>
sl@71
   271
        private void SetLayoutWithBitmap()
sl@70
   272
        {
sl@70
   273
            //Define a 2 by 2 layout
sl@70
   274
            TableLayout layout = new TableLayout(2, 2);
sl@71
   275
            //First column only takes 25%
sl@70
   276
            layout.Columns[0].Width = 25F;
sl@73
   277
            //Second column takes up 75%
sl@70
   278
            layout.Columns[1].Width = 75F;
sl@70
   279
            //Send layout to server
sl@70
   280
            iClient.SetLayout(layout);
sl@70
   281
sl@70
   282
            //Set a bitmap for our first field
sl@70
   283
            int x1 = 0;
sl@70
   284
            int y1 = 0;
sl@70
   285
            int x2 = 64;
sl@70
   286
            int y2 = 64;
sl@70
   287
sl@70
   288
            Bitmap bitmap = new Bitmap(x2, y2);
sl@70
   289
            Pen blackPen = new Pen(Color.Black, 3);
sl@70
   290
sl@70
   291
            // Draw line to screen.
sl@70
   292
            using (var graphics = Graphics.FromImage(bitmap))
sl@70
   293
            {
sl@70
   294
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@70
   295
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@70
   296
            }
sl@70
   297
sl@71
   298
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@172
   299
            BitmapField field = new BitmapField(0, bitmap);
sl@71
   300
            //We want our bitmap field to span across two rows
sl@70
   301
            field.RowSpan = 2;
sl@70
   302
sl@70
   303
            //Set texts
sl@80
   304
            iClient.CreateFields(new DataField[]
sl@70
   305
            {
sl@75
   306
                field,
StephaneLenclud@172
   307
                new TextField(1, textBoxTop.Text, Alignment),
StephaneLenclud@172
   308
                new TextField(2, textBoxBottom.Text, Alignment)
sl@70
   309
            });
sl@70
   310
sl@67
   311
        }
sl@79
   312
sl@79
   313
        private void buttonIndicatorsLayout_Click(object sender, EventArgs e)
sl@79
   314
        {
sl@79
   315
            //Define a 2 by 4 layout
sl@79
   316
            TableLayout layout = new TableLayout(2, 4);
sl@79
   317
            //First column
sl@79
   318
            layout.Columns[0].Width = 87.5F;
sl@79
   319
            //Second column
sl@79
   320
            layout.Columns[1].Width = 12.5F;
sl@79
   321
            //Send layout to server
sl@79
   322
            iClient.SetLayout(layout);
sl@79
   323
sl@79
   324
            //Create a bitmap for our indicators field
sl@79
   325
            int x1 = 0;
sl@79
   326
            int y1 = 0;
sl@79
   327
            int x2 = 32;
sl@79
   328
            int y2 = 16;
sl@79
   329
sl@79
   330
            Bitmap bitmap = new Bitmap(x2, y2);
sl@79
   331
            Pen blackPen = new Pen(Color.Black, 3);
sl@79
   332
sl@79
   333
            // Draw line to screen.
sl@79
   334
            using (var graphics = Graphics.FromImage(bitmap))
sl@79
   335
            {
sl@79
   336
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@79
   337
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@79
   338
            }
sl@79
   339
sl@79
   340
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@172
   341
            DataField indicator1 = new BitmapField(2, bitmap);
sl@79
   342
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@172
   343
            DataField indicator2 = new BitmapField(3, bitmap);
sl@79
   344
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@172
   345
            DataField indicator3 = new BitmapField(4, bitmap);
sl@79
   346
            //Create a bitmap field from the bitmap we just created
StephaneLenclud@172
   347
            DataField indicator4 = new BitmapField(5, bitmap);
sl@79
   348
sl@79
   349
            //
StephaneLenclud@172
   350
            TextField textFieldTop = new TextField(0, textBoxTop.Text, Alignment);
sl@79
   351
            textFieldTop.RowSpan = 2;
sl@79
   352
StephaneLenclud@172
   353
            TextField textFieldBottom = new TextField(1, textBoxBottom.Text, Alignment);
sl@79
   354
            textFieldBottom.RowSpan = 2;
sl@79
   355
sl@79
   356
sl@79
   357
            //Set texts
sl@80
   358
            iClient.CreateFields(new DataField[]
sl@79
   359
            {
sl@79
   360
                textFieldTop,
sl@79
   361
                indicator1,
sl@79
   362
                indicator2,
sl@79
   363
                textFieldBottom,
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@172
   375
                new TextField(0, textBoxTop.Text, Alignment),
StephaneLenclud@172
   376
                new TextField(1, textBoxBottom.Text, Alignment)
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@172
   395
                new TextField(0, textBoxTop.Text, Alignment)
StephaneLenclud@108
   396
            });
StephaneLenclud@108
   397
		}
sl@18
   398
    }
sl@18
   399
}