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