Client/MainForm.cs
author StephaneLenclud
Thu, 24 Sep 2015 14:35:50 +0200
changeset 158 e22bf44c4300
parent 123 0df386e37e29
child 171 151e11cac3b2
permissions -rw-r--r--
Fixing issues where layout change would not be detected beccause of partial
field similarity between new and older layout.
Therefore we now clear our fields whenever our layout is changed.
Now resetting our create bitmap flag, hoping it will fix our rather large memory
usage when minimized.
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);
StephaneLenclud@158
   223
StephaneLenclud@158
   224
            //Set texts
StephaneLenclud@158
   225
            iClient.CreateFields(new DataField[]
StephaneLenclud@158
   226
            {                
StephaneLenclud@158
   227
                new DataField(0, textBoxTop.Text, Alignment),
StephaneLenclud@158
   228
                new DataField(1, textBoxBottom.Text, Alignment),
StephaneLenclud@158
   229
                new DataField(2, "Third text field", Alignment),
StephaneLenclud@158
   230
                new DataField(3, "Forth text field", Alignment)
StephaneLenclud@158
   231
            });
StephaneLenclud@158
   232
sl@62
   233
        }
sl@67
   234
sl@68
   235
        private void buttonSetBitmap_Click(object sender, EventArgs e)
sl@67
   236
        {
sl@68
   237
            int x1 = 0;
sl@68
   238
            int y1 = 0;
sl@68
   239
            int x2 = 256;
sl@68
   240
            int y2 = 32;
sl@68
   241
sl@68
   242
            Bitmap bitmap = new Bitmap(x2,y2);
sl@67
   243
            Pen blackPen = new Pen(Color.Black, 3);
sl@67
   244
sl@67
   245
            // Draw line to screen.
sl@67
   246
            using (var graphics = Graphics.FromImage(bitmap))
sl@67
   247
            {
sl@67
   248
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@68
   249
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@67
   250
            }
sl@67
   251
sl@72
   252
            DataField field = new DataField(0, bitmap);
sl@80
   253
            //field.ColumnSpan = 2;
sl@74
   254
            iClient.SetField(field);
sl@70
   255
        }
sl@70
   256
sl@71
   257
        private void buttonBitmapLayout_Click(object sender, EventArgs e)
sl@71
   258
        {
sl@71
   259
            SetLayoutWithBitmap();
sl@71
   260
        }
sl@71
   261
sl@71
   262
        /// <summary>
sl@71
   263
        /// Define a layout with a bitmap field on the left and two lines of text on the right.
sl@71
   264
        /// </summary>
sl@71
   265
        private void SetLayoutWithBitmap()
sl@70
   266
        {
sl@70
   267
            //Define a 2 by 2 layout
sl@70
   268
            TableLayout layout = new TableLayout(2, 2);
sl@71
   269
            //First column only takes 25%
sl@70
   270
            layout.Columns[0].Width = 25F;
sl@73
   271
            //Second column takes up 75%
sl@70
   272
            layout.Columns[1].Width = 75F;
sl@70
   273
            //Send layout to server
sl@70
   274
            iClient.SetLayout(layout);
sl@70
   275
sl@70
   276
            //Set a bitmap for our first field
sl@70
   277
            int x1 = 0;
sl@70
   278
            int y1 = 0;
sl@70
   279
            int x2 = 64;
sl@70
   280
            int y2 = 64;
sl@70
   281
sl@70
   282
            Bitmap bitmap = new Bitmap(x2, y2);
sl@70
   283
            Pen blackPen = new Pen(Color.Black, 3);
sl@70
   284
sl@70
   285
            // Draw line to screen.
sl@70
   286
            using (var graphics = Graphics.FromImage(bitmap))
sl@70
   287
            {
sl@70
   288
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@70
   289
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@70
   290
            }
sl@70
   291
sl@71
   292
            //Create a bitmap field from the bitmap we just created
sl@72
   293
            DataField field = new DataField(0, bitmap);
sl@71
   294
            //We want our bitmap field to span across two rows
sl@70
   295
            field.RowSpan = 2;
sl@70
   296
sl@70
   297
            //Set texts
sl@80
   298
            iClient.CreateFields(new DataField[]
sl@70
   299
            {
sl@75
   300
                field,
sl@72
   301
                new DataField(1, textBoxTop.Text, Alignment),
sl@72
   302
                new DataField(2, textBoxBottom.Text, Alignment)
sl@70
   303
            });
sl@70
   304
sl@67
   305
        }
sl@79
   306
sl@79
   307
        private void buttonIndicatorsLayout_Click(object sender, EventArgs e)
sl@79
   308
        {
sl@79
   309
            //Define a 2 by 4 layout
sl@79
   310
            TableLayout layout = new TableLayout(2, 4);
sl@79
   311
            //First column
sl@79
   312
            layout.Columns[0].Width = 87.5F;
sl@79
   313
            //Second column
sl@79
   314
            layout.Columns[1].Width = 12.5F;
sl@79
   315
            //Send layout to server
sl@79
   316
            iClient.SetLayout(layout);
sl@79
   317
sl@79
   318
            //Create a bitmap for our indicators field
sl@79
   319
            int x1 = 0;
sl@79
   320
            int y1 = 0;
sl@79
   321
            int x2 = 32;
sl@79
   322
            int y2 = 16;
sl@79
   323
sl@79
   324
            Bitmap bitmap = new Bitmap(x2, y2);
sl@79
   325
            Pen blackPen = new Pen(Color.Black, 3);
sl@79
   326
sl@79
   327
            // Draw line to screen.
sl@79
   328
            using (var graphics = Graphics.FromImage(bitmap))
sl@79
   329
            {
sl@79
   330
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@79
   331
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@79
   332
            }
sl@79
   333
sl@79
   334
            //Create a bitmap field from the bitmap we just created
sl@79
   335
            DataField indicator1 = new DataField(2, bitmap);
sl@79
   336
            //Create a bitmap field from the bitmap we just created
sl@79
   337
            DataField indicator2 = new DataField(3, bitmap);
sl@79
   338
            //Create a bitmap field from the bitmap we just created
sl@79
   339
            DataField indicator3 = new DataField(4, bitmap);
sl@79
   340
            //Create a bitmap field from the bitmap we just created
sl@79
   341
            DataField indicator4 = new DataField(5, bitmap);
sl@79
   342
sl@79
   343
            //
sl@79
   344
            DataField textFieldTop = new DataField(0, textBoxTop.Text, Alignment);
sl@79
   345
            textFieldTop.RowSpan = 2;
sl@79
   346
sl@79
   347
            DataField textFieldBottom = new DataField(1, textBoxBottom.Text, Alignment);
sl@79
   348
            textFieldBottom.RowSpan = 2;
sl@79
   349
sl@79
   350
sl@79
   351
            //Set texts
sl@80
   352
            iClient.CreateFields(new DataField[]
sl@79
   353
            {
sl@79
   354
                textFieldTop,
sl@79
   355
                indicator1,
sl@79
   356
                indicator2,
sl@79
   357
                textFieldBottom,
sl@79
   358
                indicator3,
sl@79
   359
                indicator4
sl@79
   360
            });
sl@79
   361
sl@79
   362
        }
sl@80
   363
sl@80
   364
        private void buttonUpdateTexts_Click(object sender, EventArgs e)
sl@80
   365
        {
sl@80
   366
sl@80
   367
            bool res = iClient.SetFields(new DataField[]
sl@80
   368
            {
sl@80
   369
                new DataField(0, textBoxTop.Text, Alignment),
sl@80
   370
                new DataField(1, textBoxBottom.Text, Alignment)
sl@80
   371
            });
sl@80
   372
sl@80
   373
            if (!res)
sl@80
   374
            {
sl@80
   375
                MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@80
   376
            }
sl@80
   377
sl@80
   378
        }
StephaneLenclud@108
   379
StephaneLenclud@108
   380
		private void buttonLayoutOneTextField_Click(object sender, EventArgs e)
StephaneLenclud@108
   381
		{
StephaneLenclud@108
   382
			//Set one column two lines layout
StephaneLenclud@108
   383
			TableLayout layout = new TableLayout(1, 1);
StephaneLenclud@108
   384
			iClient.SetLayout(layout);
StephaneLenclud@108
   385
StephaneLenclud@108
   386
			//Set our fields
StephaneLenclud@108
   387
			iClient.CreateFields(new DataField[]
StephaneLenclud@108
   388
            {
StephaneLenclud@108
   389
                new DataField(0, textBoxTop.Text, Alignment)
StephaneLenclud@108
   390
            });
StephaneLenclud@108
   391
		}
sl@18
   392
    }
sl@18
   393
}