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