StephaneLenclud@123: //
StephaneLenclud@123: // Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@123: //
StephaneLenclud@123: // This file is part of SharpDisplayManager.
StephaneLenclud@123: //
StephaneLenclud@123: // SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123: // it under the terms of the GNU General Public License as published by
StephaneLenclud@123: // the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123: // (at your option) any later version.
StephaneLenclud@123: //
StephaneLenclud@123: // SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123: // but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
StephaneLenclud@123: // GNU General Public License for more details.
StephaneLenclud@123: //
StephaneLenclud@123: // You should have received a copy of the GNU General Public License
StephaneLenclud@123: // along with SharpDisplayManager. If not, see .
StephaneLenclud@123: //
StephaneLenclud@123:
StephaneLenclud@123: using System;
sl@18: using System.Collections.Generic;
sl@18: using System.ComponentModel;
sl@18: using System.Data;
sl@18: using System.Drawing;
sl@18: using System.Linq;
sl@18: using System.Text;
sl@18: using System.Threading.Tasks;
sl@18: using System.Windows.Forms;
sl@18: using System.ServiceModel;
sl@18: using System.ServiceModel.Channels;
sl@31: using System.Diagnostics;
StephaneLenclud@171: using SharpLib.Display;
sl@20:
sl@18:
sl@18: namespace SharpDisplayClient
sl@18: {
sl@18: public partial class MainForm : Form
sl@18: {
StephaneLenclud@106: public StartParams Params { get; set; }
StephaneLenclud@106:
StephaneLenclud@106: //
StephaneLenclud@171: Client iClient;
StephaneLenclud@106: //
sl@43: ContentAlignment Alignment;
StephaneLenclud@172: TextField iTextFieldTop;
sl@18:
StephaneLenclud@106:
StephaneLenclud@106: ///
StephaneLenclud@106: /// Constructor
StephaneLenclud@106: ///
sl@18: public MainForm()
sl@18: {
sl@18: InitializeComponent();
sl@43: Alignment = ContentAlignment.MiddleLeft;
StephaneLenclud@176: iTextFieldTop = new TextField();
sl@18: }
sl@18:
StephaneLenclud@171: public void OnCloseOrder()
StephaneLenclud@171: {
StephaneLenclud@171: CloseThreadSafe();
StephaneLenclud@171: }
StephaneLenclud@171:
StephaneLenclud@171: ///
StephaneLenclud@171: ///
StephaneLenclud@171: ///
StephaneLenclud@171: ///
StephaneLenclud@171: ///
sl@18: private void MainForm_Load(object sender, EventArgs e)
sl@18: {
StephaneLenclud@171: iClient = new Client();
StephaneLenclud@171: iClient.CloseOrderEvent += OnCloseOrder;
sl@73: iClient.Open();
sl@18:
sl@29: //Connect using unique name
sl@32: //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
sl@32: string name = "Client-" + (iClient.ClientCount() - 1);
sl@32: iClient.SetName(name);
sl@30: //Text = Text + ": " + name;
sl@32: Text = "[[" + name + "]] " + iClient.SessionId;
sl@18:
sl@33: //
sl@33: textBoxTop.Text = iClient.Name;
sl@33: textBoxBottom.Text = iClient.SessionId;
sl@33:
StephaneLenclud@106: if (Params != null)
StephaneLenclud@106: {
StephaneLenclud@106: //Parameters where specified use them
StephaneLenclud@106: if (Params.TopText != "")
StephaneLenclud@106: {
StephaneLenclud@106: textBoxTop.Text = Params.TopText;
StephaneLenclud@106: }
StephaneLenclud@106:
StephaneLenclud@106: if (Params.BottomText != "")
StephaneLenclud@106: {
StephaneLenclud@106: textBoxBottom.Text = Params.BottomText;
StephaneLenclud@106: }
StephaneLenclud@106:
StephaneLenclud@106: Location = Params.Location;
StephaneLenclud@106: //
StephaneLenclud@106: SetBasicLayoutAndText();
StephaneLenclud@106: }
StephaneLenclud@106:
sl@18: }
sl@21:
sl@31:
sl@60:
sl@31: public delegate void CloseConnectionDelegate();
sl@31: public delegate void CloseDelegate();
sl@31:
sl@31: ///
sl@60: ///
sl@31: ///
sl@31: public void CloseConnectionThreadSafe()
sl@21: {
sl@31: if (this.InvokeRequired)
sl@29: {
sl@31: //Not in the proper thread, invoke ourselves
sl@31: CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
sl@31: this.Invoke(d, new object[] { });
sl@29: }
sl@31: else
sl@31: {
sl@31: //We are in the proper thread
sl@31: if (IsClientReady())
sl@31: {
sl@74: string sessionId = iClient.SessionId;
sl@74: Trace.TraceInformation("Closing client: " + sessionId);
sl@31: iClient.Close();
sl@74: Trace.TraceInformation("Closed client: " + sessionId);
sl@31: }
sl@29:
sl@31: iClient = null;
sl@31: }
sl@26: }
sl@26:
sl@31: ///
sl@60: ///
sl@31: ///
sl@31: public void CloseThreadSafe()
sl@31: {
sl@31: if (this.InvokeRequired)
sl@31: {
sl@31: //Not in the proper thread, invoke ourselves
sl@31: CloseDelegate d = new CloseDelegate(CloseThreadSafe);
sl@31: this.Invoke(d, new object[] { });
sl@31: }
sl@31: else
sl@31: {
sl@31: //We are in the proper thread
sl@31: Close();
sl@31: }
sl@31: }
sl@31:
sl@31:
sl@26: private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@26: {
sl@31: CloseConnectionThreadSafe();
sl@29: }
sl@29:
sl@29: public bool IsClientReady()
sl@29: {
sl@73: return (iClient != null && iClient.IsReady());
sl@21: }
sl@43:
sl@43: private void buttonAlignLeft_Click(object sender, EventArgs e)
sl@43: {
sl@43: Alignment = ContentAlignment.MiddleLeft;
sl@43: textBoxTop.TextAlign = HorizontalAlignment.Left;
sl@43: textBoxBottom.TextAlign = HorizontalAlignment.Left;
sl@43: }
sl@43:
sl@43: private void buttonAlignCenter_Click(object sender, EventArgs e)
sl@43: {
sl@43: Alignment = ContentAlignment.MiddleCenter;
sl@43: textBoxTop.TextAlign = HorizontalAlignment.Center;
sl@43: textBoxBottom.TextAlign = HorizontalAlignment.Center;
sl@43: }
sl@43:
sl@43: private void buttonAlignRight_Click(object sender, EventArgs e)
sl@43: {
sl@43: Alignment = ContentAlignment.MiddleRight;
sl@43: textBoxTop.TextAlign = HorizontalAlignment.Right;
sl@43: textBoxBottom.TextAlign = HorizontalAlignment.Right;
sl@43: }
sl@43:
sl@43: private void buttonSetTopText_Click(object sender, EventArgs e)
sl@43: {
sl@43: //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43: iTextFieldTop.Text = textBoxTop.Text;
sl@60: iTextFieldTop.Alignment = Alignment;
sl@81: bool res = iClient.SetField(iTextFieldTop);
sl@81:
sl@81: if (!res)
sl@81: {
sl@81: MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@81: }
sl@81:
sl@81:
sl@43: }
sl@43:
sl@43: private void buttonSetText_Click(object sender, EventArgs e)
sl@43: {
StephaneLenclud@106: SetBasicLayoutAndText();
StephaneLenclud@106: }
sl@78:
StephaneLenclud@106: void SetBasicLayoutAndText()
StephaneLenclud@106: {
StephaneLenclud@106: //Set one column two lines layout
StephaneLenclud@106: TableLayout layout = new TableLayout(1, 2);
StephaneLenclud@106: iClient.SetLayout(layout);
StephaneLenclud@106:
StephaneLenclud@106: //Set our fields
StephaneLenclud@106: iClient.CreateFields(new DataField[]
sl@60: {
StephaneLenclud@176: new TextField(textBoxTop.Text, Alignment, 0, 0),
StephaneLenclud@176: new TextField(textBoxBottom.Text, Alignment, 0, 1)
sl@43: });
StephaneLenclud@106:
StephaneLenclud@106: }
sl@62:
sl@62: private void buttonLayoutUpdate_Click(object sender, EventArgs e)
sl@62: {
sl@65: //Define a 2 by 2 layout
sl@62: TableLayout layout = new TableLayout(2,2);
sl@65: //Second column only takes up 25%
sl@63: layout.Columns[1].Width = 25F;
sl@65: //Send layout to server
sl@62: iClient.SetLayout(layout);
StephaneLenclud@158:
StephaneLenclud@174: //
StephaneLenclud@174: RecordingField recording = new RecordingField();
StephaneLenclud@174: recording.IsActive = true;
StephaneLenclud@183: recording.Text = "Recording Tame of Gone until 22:05";
StephaneLenclud@158: //Set texts
StephaneLenclud@158: iClient.CreateFields(new DataField[]
StephaneLenclud@158: {
StephaneLenclud@176: new TextField(textBoxTop.Text, Alignment, 0, 0),
StephaneLenclud@176: new TextField(textBoxBottom.Text, Alignment, 0, 1),
StephaneLenclud@176: new TextField("Third text field", Alignment, 1, 0),
StephaneLenclud@176: new TextField("Forth text field", Alignment, 1, 1),
StephaneLenclud@175: recording
StephaneLenclud@158: });
StephaneLenclud@158:
sl@62: }
sl@67:
sl@68: private void buttonSetBitmap_Click(object sender, EventArgs e)
sl@67: {
sl@68: int x1 = 0;
sl@68: int y1 = 0;
sl@68: int x2 = 256;
sl@68: int y2 = 32;
sl@68:
sl@68: Bitmap bitmap = new Bitmap(x2,y2);
sl@67: Pen blackPen = new Pen(Color.Black, 3);
sl@67:
sl@67: // Draw line to screen.
sl@67: using (var graphics = Graphics.FromImage(bitmap))
sl@67: {
sl@67: graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@68: graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@67: }
sl@67:
StephaneLenclud@176: DataField field = new BitmapField(bitmap);
sl@80: //field.ColumnSpan = 2;
sl@74: iClient.SetField(field);
sl@70: }
sl@70:
sl@71: private void buttonBitmapLayout_Click(object sender, EventArgs e)
sl@71: {
sl@71: SetLayoutWithBitmap();
sl@71: }
sl@71:
sl@71: ///
sl@71: /// Define a layout with a bitmap field on the left and two lines of text on the right.
sl@71: ///
sl@71: private void SetLayoutWithBitmap()
sl@70: {
sl@70: //Define a 2 by 2 layout
sl@70: TableLayout layout = new TableLayout(2, 2);
sl@71: //First column only takes 25%
sl@70: layout.Columns[0].Width = 25F;
sl@73: //Second column takes up 75%
sl@70: layout.Columns[1].Width = 75F;
sl@70: //Send layout to server
sl@70: iClient.SetLayout(layout);
sl@70:
sl@70: //Set a bitmap for our first field
sl@70: int x1 = 0;
sl@70: int y1 = 0;
sl@70: int x2 = 64;
sl@70: int y2 = 64;
sl@70:
sl@70: Bitmap bitmap = new Bitmap(x2, y2);
sl@70: Pen blackPen = new Pen(Color.Black, 3);
sl@70:
sl@70: // Draw line to screen.
sl@70: using (var graphics = Graphics.FromImage(bitmap))
sl@70: {
sl@70: graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@70: graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@70: }
sl@70:
sl@71: //Create a bitmap field from the bitmap we just created
sl@71: //We want our bitmap field to span across two rows
StephaneLenclud@176: BitmapField bitmapField = new BitmapField(bitmap, 0, 0, 1, 2);
StephaneLenclud@176:
sl@70: //Set texts
sl@80: iClient.CreateFields(new DataField[]
sl@70: {
StephaneLenclud@176: bitmapField,
StephaneLenclud@176: new TextField(textBoxTop.Text, Alignment, 1, 0),
StephaneLenclud@176: new TextField(textBoxBottom.Text, Alignment, 1, 1)
sl@70: });
sl@70:
sl@67: }
sl@79:
sl@79: private void buttonIndicatorsLayout_Click(object sender, EventArgs e)
sl@79: {
sl@79: //Define a 2 by 4 layout
sl@79: TableLayout layout = new TableLayout(2, 4);
sl@79: //First column
sl@79: layout.Columns[0].Width = 87.5F;
sl@79: //Second column
sl@79: layout.Columns[1].Width = 12.5F;
sl@79: //Send layout to server
sl@79: iClient.SetLayout(layout);
sl@79:
sl@79: //Create a bitmap for our indicators field
sl@79: int x1 = 0;
sl@79: int y1 = 0;
sl@79: int x2 = 32;
sl@79: int y2 = 16;
sl@79:
sl@79: Bitmap bitmap = new Bitmap(x2, y2);
sl@79: Pen blackPen = new Pen(Color.Black, 3);
sl@79:
sl@79: // Draw line to screen.
sl@79: using (var graphics = Graphics.FromImage(bitmap))
sl@79: {
sl@79: graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@79: graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@79: }
sl@79:
sl@79: //Create a bitmap field from the bitmap we just created
StephaneLenclud@176: DataField indicator1 = new BitmapField(bitmap, 1, 0);
sl@79: //Create a bitmap field from the bitmap we just created
StephaneLenclud@176: DataField indicator2 = new BitmapField(bitmap, 1, 1);
sl@79: //Create a bitmap field from the bitmap we just created
StephaneLenclud@176: DataField indicator3 = new BitmapField(bitmap, 1, 2);
sl@79: //Create a bitmap field from the bitmap we just created
StephaneLenclud@176: DataField indicator4 = new BitmapField(bitmap, 1, 3);
sl@79:
sl@79: //
StephaneLenclud@176: TextField textFieldTop = new TextField(textBoxTop.Text, Alignment, 0, 0, 1, 2);
StephaneLenclud@176: TextField textFieldBottom = new TextField(textBoxBottom.Text, Alignment, 0, 2, 1, 2);
sl@79:
sl@79: //Set texts
sl@80: iClient.CreateFields(new DataField[]
sl@79: {
sl@79: textFieldTop,
StephaneLenclud@175: textFieldBottom,
sl@79: indicator1,
StephaneLenclud@175: indicator2,
sl@79: indicator3,
sl@79: indicator4
sl@79: });
sl@79:
sl@79: }
sl@80:
sl@80: private void buttonUpdateTexts_Click(object sender, EventArgs e)
sl@80: {
sl@80:
sl@80: bool res = iClient.SetFields(new DataField[]
sl@80: {
StephaneLenclud@176: new TextField(textBoxTop.Text, Alignment,0,0),
StephaneLenclud@176: new TextField(textBoxBottom.Text, Alignment,0,1)
sl@80: });
sl@80:
sl@80: if (!res)
sl@80: {
sl@80: MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@80: }
sl@80:
sl@80: }
StephaneLenclud@108:
StephaneLenclud@108: private void buttonLayoutOneTextField_Click(object sender, EventArgs e)
StephaneLenclud@108: {
StephaneLenclud@172: //Set one column one line layout
StephaneLenclud@108: TableLayout layout = new TableLayout(1, 1);
StephaneLenclud@108: iClient.SetLayout(layout);
StephaneLenclud@108:
StephaneLenclud@108: //Set our fields
StephaneLenclud@108: iClient.CreateFields(new DataField[]
StephaneLenclud@108: {
StephaneLenclud@176: new TextField(textBoxTop.Text, Alignment)
StephaneLenclud@108: });
StephaneLenclud@108: }
StephaneLenclud@184:
StephaneLenclud@184: private void numericUpDownPriority_ValueChanged(object sender, EventArgs e)
StephaneLenclud@184: {
StephaneLenclud@184: iClient.SetPriority((uint)numericUpDownPriority.Value);
StephaneLenclud@184: }
sl@18: }
sl@18: }