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

namespace Counter
{
	/// <summary>
	/// Comp ̊Tv̐łB
	/// </summary>
	public class Comp : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.GroupBox GroupBox2;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.Label Label11;
		internal System.Windows.Forms.Label Label7;
		internal System.Windows.Forms.Label Label10;
		internal System.Windows.Forms.Label Label9;
		internal System.Windows.Forms.Label Label8;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Button btnCancel;
		internal System.Windows.Forms.Button btnOK;
		internal System.Windows.Forms.TextBox txtPulse;
		internal System.Windows.Forms.TextBox txtComp7;
		internal System.Windows.Forms.TextBox txtComp6;
		internal System.Windows.Forms.TextBox txtComp5;
		internal System.Windows.Forms.TextBox txtComp4;
		internal System.Windows.Forms.TextBox txtComp3;
		internal System.Windows.Forms.TextBox txtComp2;
		internal System.Windows.Forms.TextBox txtComp1;
		internal System.Windows.Forms.TextBox txtComp0;
		internal System.Windows.Forms.RadioButton rdoComp1;
		internal System.Windows.Forms.RadioButton rdoComp0;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;
		//---------------------------------------------------------
		// O[oϐ
		//---------------------------------------------------------
		Int32				Ret;									// ߂l
		StringBuilder	szError = new StringBuilder(256);		// G[
		string			szReturnCode = new string(' ', 256);	// \G[
		string			szText = new string(' ', 100);			// e|
		static	Int32[]	CompValue = new Int32[8];					// rl
		static	Int32		PulseWidth = 0;							// pX
		// ǉf[^
		TextBox[]		m_Comp = new TextBox[8];				// rlRg[

		public Comp()
		{
			//
			// Windows tH[ fUCi T|[gɕKvłB
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent ĂяǒɁARXgN^ R[hǉĂB
			//
		}

		/// <summary>
		/// gpĂ郊\[XɌ㏈s܂B
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// fUCi T|[gɕKvȃ\bhłB̃\bh̓e
		/// R[h GfB^ŕύXȂłB
		/// </summary>
		private void InitializeComponent()
		{
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.GroupBox2 = new System.Windows.Forms.GroupBox();
			this.Label2 = new System.Windows.Forms.Label();
			this.txtPulse = new System.Windows.Forms.TextBox();
			this.Label1 = new System.Windows.Forms.Label();
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.txtComp7 = new System.Windows.Forms.TextBox();
			this.txtComp6 = new System.Windows.Forms.TextBox();
			this.txtComp5 = new System.Windows.Forms.TextBox();
			this.txtComp4 = new System.Windows.Forms.TextBox();
			this.txtComp3 = new System.Windows.Forms.TextBox();
			this.txtComp2 = new System.Windows.Forms.TextBox();
			this.txtComp1 = new System.Windows.Forms.TextBox();
			this.txtComp0 = new System.Windows.Forms.TextBox();
			this.Label11 = new System.Windows.Forms.Label();
			this.Label7 = new System.Windows.Forms.Label();
			this.Label10 = new System.Windows.Forms.Label();
			this.Label9 = new System.Windows.Forms.Label();
			this.Label8 = new System.Windows.Forms.Label();
			this.Label6 = new System.Windows.Forms.Label();
			this.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.rdoComp1 = new System.Windows.Forms.RadioButton();
			this.rdoComp0 = new System.Windows.Forms.RadioButton();
			this.GroupBox2.SuspendLayout();
			this.GroupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(137, 346);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(96, 24);
			this.btnCancel.TabIndex = 7;
			this.btnCancel.Text = "ݾ";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(23, 346);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(96, 24);
			this.btnOK.TabIndex = 6;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// GroupBox2
			// 
			this.GroupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Label2,
																					this.txtPulse,
																					this.Label1});
			this.GroupBox2.Location = new System.Drawing.Point(23, 284);
			this.GroupBox2.Name = "GroupBox2";
			this.GroupBox2.Size = new System.Drawing.Size(210, 52);
			this.GroupBox2.TabIndex = 5;
			this.GroupBox2.TabStop = false;
			this.GroupBox2.Text = "JEgvpXݒ";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(173, 24);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(24, 16);
			this.Label2.TabIndex = 2;
			this.Label2.Text = "Hex";
			// 
			// txtPulse
			// 
			this.txtPulse.Location = new System.Drawing.Point(83, 20);
			this.txtPulse.Name = "txtPulse";
			this.txtPulse.Size = new System.Drawing.Size(85, 19);
			this.txtPulse.TabIndex = 1;
			this.txtPulse.Text = "";
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(24, 24);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(64, 16);
			this.Label1.TabIndex = 0;
			this.Label1.Text = "pXF";
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtComp7,
																					this.txtComp6,
																					this.txtComp5,
																					this.txtComp4,
																					this.txtComp3,
																					this.txtComp2,
																					this.txtComp1,
																					this.txtComp0,
																					this.Label11,
																					this.Label7,
																					this.Label10,
																					this.Label9,
																					this.Label8,
																					this.Label6,
																					this.Label5,
																					this.Label4,
																					this.rdoComp1,
																					this.rdoComp0});
			this.GroupBox1.Location = new System.Drawing.Point(23, 10);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(210, 268);
			this.GroupBox1.TabIndex = 4;
			this.GroupBox1.TabStop = false;
			this.GroupBox1.Text = "rWX^ݒ";
			// 
			// txtComp7
			// 
			this.txtComp7.Location = new System.Drawing.Point(84, 232);
			this.txtComp7.Name = "txtComp7";
			this.txtComp7.TabIndex = 61;
			this.txtComp7.Text = "";
			// 
			// txtComp6
			// 
			this.txtComp6.Location = new System.Drawing.Point(84, 208);
			this.txtComp6.Name = "txtComp6";
			this.txtComp6.TabIndex = 60;
			this.txtComp6.Text = "";
			// 
			// txtComp5
			// 
			this.txtComp5.Location = new System.Drawing.Point(84, 184);
			this.txtComp5.Name = "txtComp5";
			this.txtComp5.TabIndex = 59;
			this.txtComp5.Text = "";
			// 
			// txtComp4
			// 
			this.txtComp4.Location = new System.Drawing.Point(84, 160);
			this.txtComp4.Name = "txtComp4";
			this.txtComp4.TabIndex = 58;
			this.txtComp4.Text = "";
			// 
			// txtComp3
			// 
			this.txtComp3.Location = new System.Drawing.Point(84, 136);
			this.txtComp3.Name = "txtComp3";
			this.txtComp3.TabIndex = 57;
			this.txtComp3.Text = "";
			// 
			// txtComp2
			// 
			this.txtComp2.Location = new System.Drawing.Point(84, 112);
			this.txtComp2.Name = "txtComp2";
			this.txtComp2.TabIndex = 56;
			this.txtComp2.Text = "";
			// 
			// txtComp1
			// 
			this.txtComp1.Location = new System.Drawing.Point(84, 88);
			this.txtComp1.Name = "txtComp1";
			this.txtComp1.TabIndex = 55;
			this.txtComp1.Text = "";
			// 
			// txtComp0
			// 
			this.txtComp0.Location = new System.Drawing.Point(84, 64);
			this.txtComp0.Name = "txtComp0";
			this.txtComp0.TabIndex = 54;
			this.txtComp0.Text = "";
			// 
			// Label11
			// 
			this.Label11.Location = new System.Drawing.Point(24, 240);
			this.Label11.Name = "Label11";
			this.Label11.Size = new System.Drawing.Size(48, 16);
			this.Label11.TabIndex = 53;
			this.Label11.Text = "7";
			// 
			// Label7
			// 
			this.Label7.Location = new System.Drawing.Point(24, 144);
			this.Label7.Name = "Label7";
			this.Label7.Size = new System.Drawing.Size(48, 16);
			this.Label7.TabIndex = 49;
			this.Label7.Text = "3";
			// 
			// Label10
			// 
			this.Label10.Location = new System.Drawing.Point(24, 216);
			this.Label10.Name = "Label10";
			this.Label10.Size = new System.Drawing.Size(48, 16);
			this.Label10.TabIndex = 52;
			this.Label10.Text = "6";
			// 
			// Label9
			// 
			this.Label9.Location = new System.Drawing.Point(24, 192);
			this.Label9.Name = "Label9";
			this.Label9.Size = new System.Drawing.Size(48, 16);
			this.Label9.TabIndex = 51;
			this.Label9.Text = "5";
			// 
			// Label8
			// 
			this.Label8.Location = new System.Drawing.Point(24, 168);
			this.Label8.Name = "Label8";
			this.Label8.Size = new System.Drawing.Size(48, 16);
			this.Label8.TabIndex = 50;
			this.Label8.Text = "4";
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(24, 120);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(48, 16);
			this.Label6.TabIndex = 48;
			this.Label6.Text = "2";
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(24, 96);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(48, 16);
			this.Label5.TabIndex = 47;
			this.Label5.Text = "1";
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(24, 72);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(48, 16);
			this.Label4.TabIndex = 46;
			this.Label4.Text = "0";
			// 
			// rdoComp1
			// 
			this.rdoComp1.Location = new System.Drawing.Point(84, 44);
			this.rdoComp1.Name = "rdoComp1";
			this.rdoComp1.Size = new System.Drawing.Size(64, 16);
			this.rdoComp1.TabIndex = 1;
			this.rdoComp1.Text = "16i";
			this.rdoComp1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.rdoComp1_MouseDown);
			// 
			// rdoComp0
			// 
			this.rdoComp0.Checked = true;
			this.rdoComp0.Location = new System.Drawing.Point(84, 24);
			this.rdoComp0.Name = "rdoComp0";
			this.rdoComp0.Size = new System.Drawing.Size(64, 16);
			this.rdoComp0.TabIndex = 0;
			this.rdoComp0.TabStop = true;
			this.rdoComp0.Text = "10i";
			this.rdoComp0.MouseDown += new System.Windows.Forms.MouseEventHandler(this.rdoComp0_MouseDown);
			// 
			// Comp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(256, 381);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnOK,
																		  this.GroupBox2,
																		  this.GroupBox1});
			this.Font = new System.Drawing.Font("lr oSVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Comp";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "rݒ";
			this.Load += new System.EventHandler(this.Comp_Load);
			this.GroupBox2.ResumeLayout(false);
			this.GroupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//---------------------------------------------------------
		//	_CAO
		//---------------------------------------------------------
		private void Comp_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// ʃACȅ
			//-----------------------------------
			short	i;
			rdoComp0.Checked = true;
			m_Comp[0] = txtComp0;
			m_Comp[1] = txtComp1;
			m_Comp[2] = txtComp2;
			m_Comp[3] = txtComp3;
			m_Comp[4] = txtComp4;
			m_Comp[5] = txtComp5;
			m_Comp[6] = txtComp6;
			m_Comp[7] = txtComp7;
			for (i=0; i<8; i++)
			{
				if (i >= Common.ChannelNum)
				{
					m_Comp[i].Enabled = false;
				}
			}
			//-----------------------------------
			// JEgvpX̎擾ƕ\
			//-----------------------------------
			int	PlsWidth;
			PlsWidth = 0;
			Ret = CNT.Ccapcnt.CntGetPulseWidth(Common.Id, 0, ref PlsWidth);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
			{
				PlsWidth = 0;
			}
			szText = string.Format("{0:X}", PlsWidth);
			txtPulse.Text = szText;
			//-----------------------------------
			// JEgrl̎擾ƕ\
			//-----------------------------------
			for (i=0; i<Common.ChannelNum; i++)
			{
				CompValue[i] = 0;
				Ret = CNT.Ccapcnt.CntGetCountMatchReg(Common.Id, i, 0, ref CompValue[i]);
				szText = string.Format("{0:d}", CompValue[i]);
				m_Comp[i].Text = szText;
			}
		}

		//---------------------------------------------------------
		//	OK{^̏
		//---------------------------------------------------------
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			short	i;
			//-----------------------------------
			// ʂݒl擾
			//-----------------------------------
			for (i=0; i<8; i++)
			{
				szText = m_Comp[i].Text;
				try 
				{
					if (rdoComp0.Checked == true)
					{
                        CompValue[i] = Int32.Parse(szText);
					} 
					else 
					{
						CompValue[i] = Int32.Parse(szText, NumberStyles.HexNumber); 
					}
				}
				catch (System.FormatException)
				{
					CompValue[i] = 0;
				}
			}
			szText = txtPulse.Text;
			try
			{
				PulseWidth = Int32.Parse(szText, NumberStyles.HexNumber);;
			}
			catch (System.FormatException)
			{
				PulseWidth = 0;
			}
			//-----------------------------------
			// e`l
			//-----------------------------------
			for (i=0; i<Common.ChannelNum; i++)
			{
				//-----------------------------------
				// rlݒ
				//-----------------------------------
				Ret = CNT.Ccapcnt.CntSetCountMatchEvent(Common.Id, i, 0, CompValue[i], 1);
				//-----------------------------
				// G[
				//-----------------------------
				if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
				{
					CNT.Ccapcnt.CntGetErrorString(Ret, szError);
					szReturnCode = "CntSetCountMatchEvent : " + szError;
					MessageBox.Show(szReturnCode, "G[", MessageBoxButtons.OK);
					break;
				}
				//-----------------------------------
				// pXݒ
				//-----------------------------------
				Ret = CNT.Ccapcnt.CntSetPulseWidth(Common.Id, i, PulseWidth);
				//-----------------------------
				// G[
				//-----------------------------
				if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
				{
					CNT.Ccapcnt.CntGetErrorString(Ret, szError);
					szReturnCode = "CntSetPulseWidth : " + szError;
					MessageBox.Show(szReturnCode, "G[", MessageBoxButtons.OK);
					break;
				}
			}
			//-----------------------------------
			// pXݒ
			//-----------------------------------
			this.Close();
		}

		//---------------------------------------------------------
		//	LZ{^̏
		//---------------------------------------------------------
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//---------------------------------------------------------
		//	rl̕\ύX
		//---------------------------------------------------------
		//	10i
		private void rdoComp0_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			//-----------------------------------
			// ʏXV
			//-----------------------------------
			short	i;
			for (i=0; i<8; i++)
			{
				szText = string.Format("{0:d}", CompValue[i]);
				m_Comp[i].Text = szText;
			}
		}

		//	16i
		private void rdoComp1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			//-----------------------------------
			// ʏXV
			//-----------------------------------
			short	i;
			for (i=0; i<8; i++)
			{
				szText = string.Format("{0:X8}", CompValue[i]);
				m_Comp[i].Text = szText;
			}
		}
	}
}