StephaneLenclud@225: //
StephaneLenclud@225: // Copyright (C) 2014-2016 Stéphane Lenclud.
StephaneLenclud@225: //
StephaneLenclud@225: // This file is part of SharpDisplayManager.
StephaneLenclud@225: //
StephaneLenclud@225: // SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@225: // it under the terms of the GNU General Public License as published by
StephaneLenclud@225: // the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@225: // (at your option) any later version.
StephaneLenclud@225: //
StephaneLenclud@225: // SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@225: // but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@225: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
StephaneLenclud@225: // GNU General Public License for more details.
StephaneLenclud@225: //
StephaneLenclud@225: // You should have received a copy of the GNU General Public License
StephaneLenclud@225: // along with SharpDisplayManager. If not, see .
StephaneLenclud@225: //
StephaneLenclud@225:
StephaneLenclud@225: using System;
StephaneLenclud@225: using System.Collections.Generic;
StephaneLenclud@225: using System.ComponentModel;
StephaneLenclud@225: using System.Data;
StephaneLenclud@225: using System.Drawing;
StephaneLenclud@225: using System.Linq;
StephaneLenclud@225: using System.Text;
StephaneLenclud@225: using System.Threading.Tasks;
StephaneLenclud@225: using System.Windows.Forms;
StephaneLenclud@225: using System.Diagnostics;
StephaneLenclud@225: using SharpLib.Display;
StephaneLenclud@225:
StephaneLenclud@225:
StephaneLenclud@225: namespace SharpDisplayClientMessage
StephaneLenclud@225: {
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: /// Sharp Display Client designed to act as an idle client.
StephaneLenclud@225: /// It should take care of screen saving and other such concerns.
StephaneLenclud@225: ///
StephaneLenclud@225: public partial class FormClientMessage : Form
StephaneLenclud@225: {
StephaneLenclud@225: public StartParams Params { get; set; }
StephaneLenclud@225:
StephaneLenclud@225: Client iClient;
StephaneLenclud@225: ContentAlignment iAlignment;
StephaneLenclud@225: TextField iPrimaryTextField;
StephaneLenclud@225: TextField iSecondaryTextField;
StephaneLenclud@225:
StephaneLenclud@225:
StephaneLenclud@225: public delegate void CloseDelegate();
StephaneLenclud@225: public delegate void CloseConnectionDelegate();
StephaneLenclud@225:
StephaneLenclud@225:
StephaneLenclud@225: public FormClientMessage()
StephaneLenclud@225: {
StephaneLenclud@225: InitializeComponent();
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: private void FormClientMessage_Load(object sender, EventArgs e)
StephaneLenclud@225: {
StephaneLenclud@225: //Prevents showing in the Open Task view (Windows Key + Tab)
StephaneLenclud@225: Visible = false;
StephaneLenclud@225:
StephaneLenclud@225: //Display client
StephaneLenclud@225: iClient = new Client();
StephaneLenclud@225: iClient.CloseOrderEvent += OnCloseOrder;
StephaneLenclud@225: iClient.Open();
StephaneLenclud@225: iClient.SetName("Message");
StephaneLenclud@225: iClient.SetPriority(Params.Priority);
StephaneLenclud@225: SetupDisplayClient();
StephaneLenclud@225:
StephaneLenclud@225: //Timer
StephaneLenclud@225: iTimer.Interval = Params.DurationInMs;
StephaneLenclud@225: iTimer.Start();
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: public bool IsClientReady()
StephaneLenclud@225: {
StephaneLenclud@225: return (iClient != null && iClient.IsReady());
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: public void SetupDisplayClient()
StephaneLenclud@225: {
StephaneLenclud@225: //Setup our layout
StephaneLenclud@225:
StephaneLenclud@225: //Set one column one line layout
StephaneLenclud@225:
StephaneLenclud@225: //Setup our fields
StephaneLenclud@225: iAlignment = ContentAlignment.MiddleCenter;
StephaneLenclud@225: iPrimaryTextField = new TextField(Params.PrimaryText, iAlignment, 0, 0);
StephaneLenclud@225: iSecondaryTextField = new TextField(Params.SecondaryText, iAlignment, 0, 1);
StephaneLenclud@225:
StephaneLenclud@225: //Set our fields
StephaneLenclud@225: if (string.IsNullOrEmpty(Params.SecondaryText))
StephaneLenclud@225: {
StephaneLenclud@225: //One field layout
StephaneLenclud@225: TableLayout layout = new TableLayout(1, 1);
StephaneLenclud@225: iClient.SetLayout(layout);
StephaneLenclud@225:
StephaneLenclud@225: iClient.CreateFields(new DataField[]
StephaneLenclud@225: {
StephaneLenclud@225: iPrimaryTextField
StephaneLenclud@225: });
StephaneLenclud@225: }
StephaneLenclud@225: else
StephaneLenclud@225: {
StephaneLenclud@225: //Two fields layout
StephaneLenclud@225: TableLayout layout = new TableLayout(1, 2);
StephaneLenclud@225: iClient.SetLayout(layout);
StephaneLenclud@225:
StephaneLenclud@225: iClient.CreateFields(new DataField[]
StephaneLenclud@225: {
StephaneLenclud@225: iPrimaryTextField,
StephaneLenclud@225: iSecondaryTextField
StephaneLenclud@225: });
StephaneLenclud@225: }
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: public void OnCloseOrder()
StephaneLenclud@225: {
StephaneLenclud@225: CloseThreadSafe();
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: public void CloseThreadSafe()
StephaneLenclud@225: {
StephaneLenclud@225: if (this.InvokeRequired)
StephaneLenclud@225: {
StephaneLenclud@225: //Not in the proper thread, invoke ourselves
StephaneLenclud@225: CloseDelegate d = new CloseDelegate(CloseThreadSafe);
StephaneLenclud@225: this.Invoke(d, new object[] { });
StephaneLenclud@225: }
StephaneLenclud@225: else
StephaneLenclud@225: {
StephaneLenclud@225: //We are in the proper thread
StephaneLenclud@225: Close();
StephaneLenclud@225: }
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: public void CloseConnectionThreadSafe()
StephaneLenclud@225: {
StephaneLenclud@225: if (this.InvokeRequired)
StephaneLenclud@225: {
StephaneLenclud@225: //Not in the proper thread, invoke ourselves
StephaneLenclud@225: CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
StephaneLenclud@225: this.Invoke(d, new object[] { });
StephaneLenclud@225: }
StephaneLenclud@225: else
StephaneLenclud@225: {
StephaneLenclud@225: //We are in the proper thread
StephaneLenclud@225: if (IsClientReady())
StephaneLenclud@225: {
StephaneLenclud@225: string sessionId = iClient.SessionId;
StephaneLenclud@225: Trace.TraceInformation("Closing client: " + sessionId);
StephaneLenclud@225: iClient.Close();
StephaneLenclud@225: Trace.TraceInformation("Closed client: " + sessionId);
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: iClient = null;
StephaneLenclud@225: }
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: private void FormClientMessage_FormClosing(object sender, FormClosingEventArgs e)
StephaneLenclud@225: {
StephaneLenclud@225: CloseConnectionThreadSafe();
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: private void iTimer_Tick(object sender, EventArgs e)
StephaneLenclud@225: {
StephaneLenclud@225: Close();
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225:
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: ///
StephaneLenclud@225: private void FormClientMessage_Shown(object sender, EventArgs e)
StephaneLenclud@225: {
StephaneLenclud@225: //Visible = false;
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: }
StephaneLenclud@225:
StephaneLenclud@225: }