using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;

namespace Pti4
{
	/// <summary>
	/// Summary description for Alarm.
	/// </summary>
	public class Alarm : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Button btnOK;
		internal System.Windows.Forms.Button btnSet;
		internal System.Windows.Forms.Button btnGet;
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.TextBox txtLL;
		internal System.Windows.Forms.TextBox txtLU;
		internal System.Windows.Forms.TextBox txtUL;
		internal System.Windows.Forms.TextBox txtUU;
		internal System.Windows.Forms.ComboBox cboAlarm;
		internal System.Windows.Forms.TextBox txtChannel;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		short	m_Id;

		public Alarm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnOK = new System.Windows.Forms.Button();
			this.btnSet = new System.Windows.Forms.Button();
			this.btnGet = new System.Windows.Forms.Button();
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.txtLL = new System.Windows.Forms.TextBox();
			this.txtLU = new System.Windows.Forms.TextBox();
			this.txtUL = new System.Windows.Forms.TextBox();
			this.txtUU = new System.Windows.Forms.TextBox();
			this.cboAlarm = new System.Windows.Forms.ComboBox();
			this.txtChannel = new System.Windows.Forms.TextBox();
			this.Label6 = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.Label1 = new System.Windows.Forms.Label();
			this.GroupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(235, 245);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(96, 28);
			this.btnOK.TabIndex = 7;
			this.btnOK.Text = "Close";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnSet
			// 
			this.btnSet.Location = new System.Drawing.Point(127, 245);
			this.btnSet.Name = "btnSet";
			this.btnSet.Size = new System.Drawing.Size(96, 28);
			this.btnSet.TabIndex = 6;
			this.btnSet.Text = "Set";
			this.btnSet.Click += new System.EventHandler(this.btnSet_Click);
			// 
			// btnGet
			// 
			this.btnGet.Location = new System.Drawing.Point(19, 245);
			this.btnGet.Name = "btnGet";
			this.btnGet.Size = new System.Drawing.Size(96, 28);
			this.btnGet.TabIndex = 5;
			this.btnGet.Text = "Read";
			this.btnGet.Click += new System.EventHandler(this.btnGet_Click);
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtLL,
																					this.txtLU,
																					this.txtUL,
																					this.txtUU,
																					this.cboAlarm,
																					this.txtChannel,
																					this.Label6,
																					this.Label5,
																					this.Label4,
																					this.Label3,
																					this.Label2,
																					this.Label1});
			this.GroupBox1.Location = new System.Drawing.Point(19, 13);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(312, 208);
			this.GroupBox1.TabIndex = 4;
			this.GroupBox1.TabStop = false;
			this.GroupBox1.Text = "Alarm Setting";
			// 
			// txtLL
			// 
			this.txtLL.Location = new System.Drawing.Point(137, 168);
			this.txtLL.Name = "txtLL";
			this.txtLL.Size = new System.Drawing.Size(96, 20);
			this.txtLL.TabIndex = 11;
			this.txtLL.Text = "";
			// 
			// txtLU
			// 
			this.txtLU.Location = new System.Drawing.Point(137, 141);
			this.txtLU.Name = "txtLU";
			this.txtLU.Size = new System.Drawing.Size(96, 20);
			this.txtLU.TabIndex = 10;
			this.txtLU.Text = "";
			// 
			// txtUL
			// 
			this.txtUL.Location = new System.Drawing.Point(137, 114);
			this.txtUL.Name = "txtUL";
			this.txtUL.Size = new System.Drawing.Size(96, 20);
			this.txtUL.TabIndex = 9;
			this.txtUL.Text = "";
			// 
			// txtUU
			// 
			this.txtUU.Location = new System.Drawing.Point(137, 87);
			this.txtUU.Name = "txtUU";
			this.txtUU.Size = new System.Drawing.Size(96, 20);
			this.txtUU.TabIndex = 8;
			this.txtUU.Text = "";
			// 
			// cboAlarm
			// 
			this.cboAlarm.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboAlarm.Items.AddRange(new object[] {
														  "Disable",
														  "Enable"});
			this.cboAlarm.Location = new System.Drawing.Point(137, 59);
			this.cboAlarm.Name = "cboAlarm";
			this.cboAlarm.Size = new System.Drawing.Size(96, 21);
			this.cboAlarm.TabIndex = 7;
			// 
			// txtChannel
			// 
			this.txtChannel.Location = new System.Drawing.Point(137, 32);
			this.txtChannel.Name = "txtChannel";
			this.txtChannel.Size = new System.Drawing.Size(96, 20);
			this.txtChannel.TabIndex = 6;
			this.txtChannel.Text = "";
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(24, 170);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(112, 16);
			this.Label6.TabIndex = 5;
			this.Label6.Text = "Lower Low-limit :";
			this.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(24, 142);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(112, 16);
			this.Label5.TabIndex = 4;
			this.Label5.Text = "Higher Low-limit :";
			this.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(24, 116);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(112, 16);
			this.Label4.TabIndex = 3;
			this.Label4.Text = "Lower High-limit :";
			this.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(24, 89);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(112, 16);
			this.Label3.TabIndex = 2;
			this.Label3.Text = "Higher High-limit :";
			this.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(24, 60);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(112, 16);
			this.Label2.TabIndex = 1;
			this.Label2.Text = "Alarm Enable :";
			this.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(24, 34);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(112, 16);
			this.Label1.TabIndex = 0;
			this.Label1.Text = "Channel Mumber :";
			this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// Alarm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(350, 287);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnOK,
																		  this.btnSet,
																		  this.btnGet,
																		  this.GroupBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Alarm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Alarm Setting";
			this.Load += new System.EventHandler(this.Alarm_Load);
			this.GroupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//	Get the caller ID
		public void SetId(short id)
		{
			m_Id = id;
		}

		//	Initialize dialog
		private void Alarm_Load(object sender, System.EventArgs e)
		{
			txtChannel.Text = "0";
			txtUU.Text = "0";
			txtUL.Text = "0";
			txtLU.Text = "0";
			txtLL.Text = "0";

			// Set the channel number to 0
			cboAlarm.SelectedIndex = 0;
			btnGet_Click(btnGet, new System.EventArgs());
		}

		//	Click the [Read] button
		private void btnGet_Click(object sender, System.EventArgs e)
		{
			int	ret;
			short	enable;
			int	uu, ul, lu, ll;
			short	chno;

			// Get the alarm setting status
			enable = 0;
			uu = 0;
			ul = 0;
			lu = 0;
			ll = 0;
			try
			{
				chno = Convert.ToInt16(txtChannel.Text);
			}
			catch (System.FormatException)
			{
				chno = 0;
			}
			ret = Ccappt.PtGetAlarm(m_Id, chno, ref enable, ref uu, ref ul, ref lu, ref ll);
			if (ret != 0)
			{
				ErrorHandling("PtGetAlarm", ret);
				return;
			}

			// Reflect the setting status in dialog
			cboAlarm.SelectedIndex = enable;
			txtUU.Text = string.Format("{0:f}", uu / 100.0);
			txtUL.Text = string.Format("{0:f}", ul / 100.0);
			txtLU.Text = string.Format("{0:f}", lu / 100.0);
			txtLL.Text = string.Format("{0:f}", ll / 100.0);
		}

		//	Click the [Set] button
		private void btnSet_Click(object sender, System.EventArgs e)
		{
			int	ret;
			short	enable;
			int	uu, ul, lu, ll;
			double	uu1, ul1, lu1, ll1;
			short	chno;

			// Get the setting in the dialog
			enable = (short)cboAlarm.SelectedIndex;
			try
			{
				uu1 = Convert.ToDouble(txtUU.Text);
				ul1 = Convert.ToDouble(txtUL.Text);
				lu1 = Convert.ToDouble(txtLU.Text);
				ll1 = Convert.ToDouble(txtLL.Text);
			}
			catch (System.FormatException)
			{
				uu1 = 0;
				ul1 = 0;
				lu1 = 0;
				ll1 = 0;
			}
			uu = Convert.ToInt32(uu1 * 100);
			ul = Convert.ToInt32(ul1 * 100);
			lu = Convert.ToInt32(lu1 * 100);
			ll = Convert.ToInt32(ll1 * 100);
	
			// Set alarm
			try
			{
				chno = Convert.ToInt16(txtChannel.Text);
			}
			catch (System.FormatException)
			{
				chno = 0;
			}
			ret = Ccappt.PtSetAlarm(m_Id, chno, enable, uu, ul, lu, ll);
			if (ret != 0)
			{
				ErrorHandling("PtSetAlarm", ret);
				return;
			}
		}

		//	Click the [Close] button
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//	Process error
		private void ErrorHandling(string Dfunction, int ret)
		{
			StringBuilder	errstr = new StringBuilder(256);
			string	str = new string(' ', 256);			 
			Ccappt.PtGetErrorString(ret, errstr);
			str = string.Format("{0}", errstr);
			MessageBox.Show(str, Dfunction, MessageBoxButtons.OK);
		}
	}
}
