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