Clients/Test/MainForm.cs
changeset 190 60977f2b3ca7
parent 184 7b6aa551eb6c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Clients/Test/MainForm.cs	Wed Feb 03 13:02:48 2016 +0100
     1.3 @@ -0,0 +1,404 @@
     1.4 +//
     1.5 +// Copyright (C) 2014-2015 Stéphane Lenclud.
     1.6 +//
     1.7 +// This file is part of SharpDisplayManager.
     1.8 +//
     1.9 +// SharpDisplayManager is free software: you can redistribute it and/or modify
    1.10 +// it under the terms of the GNU General Public License as published by
    1.11 +// the Free Software Foundation, either version 3 of the License, or
    1.12 +// (at your option) any later version.
    1.13 +//
    1.14 +// SharpDisplayManager is distributed in the hope that it will be useful,
    1.15 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +// GNU General Public License for more details.
    1.18 +//
    1.19 +// You should have received a copy of the GNU General Public License
    1.20 +// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    1.21 +//
    1.22 +
    1.23 +using System;
    1.24 +using System.Collections.Generic;
    1.25 +using System.ComponentModel;
    1.26 +using System.Data;
    1.27 +using System.Drawing;
    1.28 +using System.Linq;
    1.29 +using System.Text;
    1.30 +using System.Threading.Tasks;
    1.31 +using System.Windows.Forms;
    1.32 +using System.ServiceModel;
    1.33 +using System.ServiceModel.Channels;
    1.34 +using System.Diagnostics;
    1.35 +using SharpLib.Display;
    1.36 +
    1.37 +
    1.38 +namespace SharpDisplayClient
    1.39 +{
    1.40 +    public partial class MainForm : Form
    1.41 +    {
    1.42 +		public StartParams Params { get; set; }
    1.43 +
    1.44 +		//
    1.45 +        Client iClient;
    1.46 +		//
    1.47 +        ContentAlignment Alignment;
    1.48 +        TextField iTextFieldTop;
    1.49 +
    1.50 +		
    1.51 +		/// <summary>
    1.52 +		/// Constructor
    1.53 +		/// </summary>
    1.54 +        public MainForm()
    1.55 +        {
    1.56 +            InitializeComponent();
    1.57 +            Alignment = ContentAlignment.MiddleLeft;
    1.58 +            iTextFieldTop = new TextField();
    1.59 +        }
    1.60 +
    1.61 +        public void OnCloseOrder()
    1.62 +        {
    1.63 +            CloseThreadSafe();
    1.64 +        }
    1.65 +
    1.66 +        /// <summary>
    1.67 +        /// 
    1.68 +        /// </summary>
    1.69 +        /// <param name="sender"></param>
    1.70 +        /// <param name="e"></param>
    1.71 +        private void MainForm_Load(object sender, EventArgs e)
    1.72 +        {
    1.73 +            iClient = new Client();
    1.74 +            iClient.CloseOrderEvent += OnCloseOrder;
    1.75 +            iClient.Open();
    1.76 +
    1.77 +            //Connect using unique name
    1.78 +            //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
    1.79 +            string name = "Client-" + (iClient.ClientCount() - 1);
    1.80 +            iClient.SetName(name);
    1.81 +            //Text = Text + ": " + name;
    1.82 +            Text = "[[" + name + "]]  " + iClient.SessionId;
    1.83 +
    1.84 +            //
    1.85 +            textBoxTop.Text = iClient.Name;
    1.86 +            textBoxBottom.Text = iClient.SessionId;
    1.87 +
    1.88 +			if (Params != null)
    1.89 +			{
    1.90 +				//Parameters where specified use them
    1.91 +				if (Params.TopText != "")
    1.92 +				{
    1.93 +					textBoxTop.Text = Params.TopText;
    1.94 +				}
    1.95 +
    1.96 +				if (Params.BottomText != "")
    1.97 +				{
    1.98 +					textBoxBottom.Text = Params.BottomText;
    1.99 +				}
   1.100 +
   1.101 +				Location = Params.Location;
   1.102 +				//
   1.103 +				SetBasicLayoutAndText();
   1.104 +			}
   1.105 +
   1.106 +        }
   1.107 +
   1.108 +
   1.109 +
   1.110 +        public delegate void CloseConnectionDelegate();
   1.111 +        public delegate void CloseDelegate();
   1.112 +
   1.113 +        /// <summary>
   1.114 +        ///
   1.115 +        /// </summary>
   1.116 +        public void CloseConnectionThreadSafe()
   1.117 +        {
   1.118 +            if (this.InvokeRequired)
   1.119 +            {
   1.120 +                //Not in the proper thread, invoke ourselves
   1.121 +                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
   1.122 +                this.Invoke(d, new object[] { });
   1.123 +            }
   1.124 +            else
   1.125 +            {
   1.126 +                //We are in the proper thread
   1.127 +                if (IsClientReady())
   1.128 +                {
   1.129 +                    string sessionId = iClient.SessionId;
   1.130 +                    Trace.TraceInformation("Closing client: " + sessionId);
   1.131 +                    iClient.Close();
   1.132 +                    Trace.TraceInformation("Closed client: " + sessionId);
   1.133 +                }
   1.134 +
   1.135 +                iClient = null;
   1.136 +            }
   1.137 +        }
   1.138 +
   1.139 +        /// <summary>
   1.140 +        ///
   1.141 +        /// </summary>
   1.142 +        public void CloseThreadSafe()
   1.143 +        {
   1.144 +            if (this.InvokeRequired)
   1.145 +            {
   1.146 +                //Not in the proper thread, invoke ourselves
   1.147 +                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
   1.148 +                this.Invoke(d, new object[] { });
   1.149 +            }
   1.150 +            else
   1.151 +            {
   1.152 +                //We are in the proper thread
   1.153 +                Close();
   1.154 +            }
   1.155 +        }
   1.156 +
   1.157 +
   1.158 +        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
   1.159 +        {
   1.160 +            CloseConnectionThreadSafe();
   1.161 +        }
   1.162 +
   1.163 +        public bool IsClientReady()
   1.164 +        {
   1.165 +            return (iClient != null && iClient.IsReady());
   1.166 +        }
   1.167 +
   1.168 +        private void buttonAlignLeft_Click(object sender, EventArgs e)
   1.169 +        {
   1.170 +            Alignment = ContentAlignment.MiddleLeft;
   1.171 +            textBoxTop.TextAlign = HorizontalAlignment.Left;
   1.172 +            textBoxBottom.TextAlign = HorizontalAlignment.Left;
   1.173 +        }
   1.174 +
   1.175 +        private void buttonAlignCenter_Click(object sender, EventArgs e)
   1.176 +        {
   1.177 +            Alignment = ContentAlignment.MiddleCenter;
   1.178 +            textBoxTop.TextAlign = HorizontalAlignment.Center;
   1.179 +            textBoxBottom.TextAlign = HorizontalAlignment.Center;
   1.180 +        }
   1.181 +
   1.182 +        private void buttonAlignRight_Click(object sender, EventArgs e)
   1.183 +        {
   1.184 +            Alignment = ContentAlignment.MiddleRight;
   1.185 +            textBoxTop.TextAlign = HorizontalAlignment.Right;
   1.186 +            textBoxBottom.TextAlign = HorizontalAlignment.Right;
   1.187 +        }
   1.188 +
   1.189 +        private void buttonSetTopText_Click(object sender, EventArgs e)
   1.190 +        {
   1.191 +            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
   1.192 +            iTextFieldTop.Text = textBoxTop.Text;
   1.193 +            iTextFieldTop.Alignment = Alignment;
   1.194 +            bool res = iClient.SetField(iTextFieldTop);
   1.195 +
   1.196 +            if (!res)
   1.197 +            {
   1.198 +                MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   1.199 +            }
   1.200 +
   1.201 +
   1.202 +        }
   1.203 +
   1.204 +        private void buttonSetText_Click(object sender, EventArgs e)
   1.205 +        {
   1.206 +			SetBasicLayoutAndText();
   1.207 +        }
   1.208 +
   1.209 +		void SetBasicLayoutAndText()
   1.210 +		{
   1.211 +			//Set one column two lines layout
   1.212 +			TableLayout layout = new TableLayout(1, 2);
   1.213 +			iClient.SetLayout(layout);
   1.214 +
   1.215 +			//Set our fields
   1.216 +			iClient.CreateFields(new DataField[]
   1.217 +            {
   1.218 +                new TextField(textBoxTop.Text, Alignment, 0, 0),
   1.219 +                new TextField(textBoxBottom.Text, Alignment, 0, 1)
   1.220 +            });
   1.221 +
   1.222 +		}
   1.223 +
   1.224 +        private void buttonLayoutUpdate_Click(object sender, EventArgs e)
   1.225 +        {
   1.226 +            //Define a 2 by 2 layout
   1.227 +            TableLayout layout = new TableLayout(2,2);
   1.228 +            //Second column only takes up 25%
   1.229 +            layout.Columns[1].Width = 25F;
   1.230 +            //Send layout to server
   1.231 +            iClient.SetLayout(layout);
   1.232 +
   1.233 +            //
   1.234 +            RecordingField recording = new RecordingField();
   1.235 +            recording.IsActive = true;
   1.236 +            recording.Text = "Recording Tame of Gone until 22:05";
   1.237 +            //Set texts
   1.238 +            iClient.CreateFields(new DataField[]
   1.239 +            {                
   1.240 +                new TextField(textBoxTop.Text, Alignment, 0, 0),
   1.241 +                new TextField(textBoxBottom.Text, Alignment, 0, 1),
   1.242 +                new TextField("Third text field", Alignment, 1, 0),
   1.243 +                new TextField("Forth text field", Alignment, 1, 1),
   1.244 +                recording
   1.245 +            });
   1.246 +
   1.247 +        }
   1.248 +
   1.249 +        private void buttonSetBitmap_Click(object sender, EventArgs e)
   1.250 +        {
   1.251 +            int x1 = 0;
   1.252 +            int y1 = 0;
   1.253 +            int x2 = 256;
   1.254 +            int y2 = 32;
   1.255 +
   1.256 +            Bitmap bitmap = new Bitmap(x2,y2);
   1.257 +            Pen blackPen = new Pen(Color.Black, 3);
   1.258 +
   1.259 +            // Draw line to screen.
   1.260 +            using (var graphics = Graphics.FromImage(bitmap))
   1.261 +            {
   1.262 +                graphics.DrawLine(blackPen, x1, y1, x2, y2);
   1.263 +                graphics.DrawLine(blackPen, x1, y2, x2, y1);
   1.264 +            }
   1.265 +
   1.266 +            DataField field = new BitmapField(bitmap);
   1.267 +            //field.ColumnSpan = 2;
   1.268 +            iClient.SetField(field);
   1.269 +        }
   1.270 +
   1.271 +        private void buttonBitmapLayout_Click(object sender, EventArgs e)
   1.272 +        {
   1.273 +            SetLayoutWithBitmap();
   1.274 +        }
   1.275 +
   1.276 +        /// <summary>
   1.277 +        /// Define a layout with a bitmap field on the left and two lines of text on the right.
   1.278 +        /// </summary>
   1.279 +        private void SetLayoutWithBitmap()
   1.280 +        {
   1.281 +            //Define a 2 by 2 layout
   1.282 +            TableLayout layout = new TableLayout(2, 2);
   1.283 +            //First column only takes 25%
   1.284 +            layout.Columns[0].Width = 25F;
   1.285 +            //Second column takes up 75%
   1.286 +            layout.Columns[1].Width = 75F;
   1.287 +            //Send layout to server
   1.288 +            iClient.SetLayout(layout);
   1.289 +
   1.290 +            //Set a bitmap for our first field
   1.291 +            int x1 = 0;
   1.292 +            int y1 = 0;
   1.293 +            int x2 = 64;
   1.294 +            int y2 = 64;
   1.295 +
   1.296 +            Bitmap bitmap = new Bitmap(x2, y2);
   1.297 +            Pen blackPen = new Pen(Color.Black, 3);
   1.298 +
   1.299 +            // Draw line to screen.
   1.300 +            using (var graphics = Graphics.FromImage(bitmap))
   1.301 +            {
   1.302 +                graphics.DrawLine(blackPen, x1, y1, x2, y2);
   1.303 +                graphics.DrawLine(blackPen, x1, y2, x2, y1);
   1.304 +            }
   1.305 +
   1.306 +            //Create a bitmap field from the bitmap we just created
   1.307 +            //We want our bitmap field to span across two rows
   1.308 +            BitmapField bitmapField = new BitmapField(bitmap, 0, 0, 1, 2);
   1.309 +            
   1.310 +            //Set texts
   1.311 +            iClient.CreateFields(new DataField[]
   1.312 +            {
   1.313 +                bitmapField,
   1.314 +                new TextField(textBoxTop.Text, Alignment, 1, 0),
   1.315 +                new TextField(textBoxBottom.Text, Alignment, 1, 1)
   1.316 +            });
   1.317 +
   1.318 +        }
   1.319 +
   1.320 +        private void buttonIndicatorsLayout_Click(object sender, EventArgs e)
   1.321 +        {
   1.322 +            //Define a 2 by 4 layout
   1.323 +            TableLayout layout = new TableLayout(2, 4);
   1.324 +            //First column
   1.325 +            layout.Columns[0].Width = 87.5F;
   1.326 +            //Second column
   1.327 +            layout.Columns[1].Width = 12.5F;
   1.328 +            //Send layout to server
   1.329 +            iClient.SetLayout(layout);
   1.330 +
   1.331 +            //Create a bitmap for our indicators field
   1.332 +            int x1 = 0;
   1.333 +            int y1 = 0;
   1.334 +            int x2 = 32;
   1.335 +            int y2 = 16;
   1.336 +
   1.337 +            Bitmap bitmap = new Bitmap(x2, y2);
   1.338 +            Pen blackPen = new Pen(Color.Black, 3);
   1.339 +
   1.340 +            // Draw line to screen.
   1.341 +            using (var graphics = Graphics.FromImage(bitmap))
   1.342 +            {
   1.343 +                graphics.DrawLine(blackPen, x1, y1, x2, y2);
   1.344 +                graphics.DrawLine(blackPen, x1, y2, x2, y1);
   1.345 +            }
   1.346 +
   1.347 +            //Create a bitmap field from the bitmap we just created
   1.348 +            DataField indicator1 = new BitmapField(bitmap, 1, 0);
   1.349 +            //Create a bitmap field from the bitmap we just created
   1.350 +            DataField indicator2 = new BitmapField(bitmap, 1, 1);
   1.351 +            //Create a bitmap field from the bitmap we just created
   1.352 +            DataField indicator3 = new BitmapField(bitmap, 1, 2);
   1.353 +            //Create a bitmap field from the bitmap we just created
   1.354 +            DataField indicator4 = new BitmapField(bitmap, 1, 3);
   1.355 +
   1.356 +            //
   1.357 +            TextField textFieldTop = new TextField(textBoxTop.Text, Alignment, 0, 0, 1, 2);
   1.358 +            TextField textFieldBottom = new TextField(textBoxBottom.Text, Alignment, 0, 2, 1, 2);
   1.359 +
   1.360 +            //Set texts
   1.361 +            iClient.CreateFields(new DataField[]
   1.362 +            {
   1.363 +                textFieldTop,
   1.364 +                textFieldBottom,
   1.365 +                indicator1,
   1.366 +                indicator2,                
   1.367 +                indicator3,
   1.368 +                indicator4
   1.369 +            });
   1.370 +
   1.371 +        }
   1.372 +
   1.373 +        private void buttonUpdateTexts_Click(object sender, EventArgs e)
   1.374 +        {
   1.375 +
   1.376 +            bool res = iClient.SetFields(new DataField[]
   1.377 +            {
   1.378 +                new TextField(textBoxTop.Text, Alignment,0,0),
   1.379 +                new TextField(textBoxBottom.Text, Alignment,0,1)
   1.380 +            });
   1.381 +
   1.382 +            if (!res)
   1.383 +            {
   1.384 +                MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   1.385 +            }
   1.386 +
   1.387 +        }
   1.388 +
   1.389 +		private void buttonLayoutOneTextField_Click(object sender, EventArgs e)
   1.390 +		{
   1.391 +			//Set one column one line layout
   1.392 +			TableLayout layout = new TableLayout(1, 1);
   1.393 +			iClient.SetLayout(layout);
   1.394 +
   1.395 +			//Set our fields
   1.396 +			iClient.CreateFields(new DataField[]
   1.397 +            {
   1.398 +                new TextField(textBoxTop.Text, Alignment)
   1.399 +            });
   1.400 +		}
   1.401 +
   1.402 +        private void numericUpDownPriority_ValueChanged(object sender, EventArgs e)
   1.403 +        {
   1.404 +            iClient.SetPriority((uint)numericUpDownPriority.Value);
   1.405 +        }
   1.406 +    }
   1.407 +}