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

namespace Counter
{
	/// <summary>
	/// Mode ̊Tv̐łB
	/// </summary>
	public class Mode : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.Label Label10;
		internal System.Windows.Forms.Label Label9;
		internal System.Windows.Forms.Label Label8;
		internal System.Windows.Forms.Label Label7;
		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;
		internal System.Windows.Forms.Button btnCancel;
		internal System.Windows.Forms.Button btnSet;
		internal System.Windows.Forms.ComboBox cboClr;
		internal System.Windows.Forms.ComboBox cboMul;
		internal System.Windows.Forms.TextBox txtFilter;
		internal System.Windows.Forms.ComboBox cboPhase;
		internal System.Windows.Forms.ComboBox cboDir;
		internal System.Windows.Forms.ComboBox cboSource;
		internal System.Windows.Forms.ComboBox cboZLogic;
		internal System.Windows.Forms.ComboBox cboZMode;
		internal System.Windows.Forms.ComboBox cboChNo;
		/// <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|
		// ǉf[^
		short	ChNo;
		short	ZMode;
		short	ZLogic;
		short	SigType;
		short	Dir;
		short	Phase;
		short	Mul;
		short	SyncClr;
		short	FilterValue;

		//---------------------------------------------------------
		// [h`
		//---------------------------------------------------------
		public struct StrNum
		{
			public string Str;
			public short	Num;
			public StrNum(string Str, short Num)
			{
				this.Str = Str;
				this.Num = Num;
			}
		};

		static StrNum[] Combo_ZModeL = 
		{
			new StrNum(	"gp",			CNT.Ccapcnt.CNT_ZPHASE_NOT_USE		),
			new StrNum(	"̂P",			CNT.Ccapcnt.CNT_ZPHASE_NEXT_ONE		),
			new StrNum(	"",				CNT.Ccapcnt.CNT_ZPHASE_EVERY_TIME	),
			new StrNum(	"End",				0								)
		};

		static StrNum[] Combo_ZLogicL = 
		{
			new StrNum(	"_",			CNT.Ccapcnt.CNT_ZLOGIC_POSITIVE		),
			new StrNum(	"_",			CNT.Ccapcnt.CNT_ZLOGIC_NEGATIVE		),
			new StrNum(	"End",				0								)
		};

		static StrNum[] Combo_ChannelSignalL = 
		{
			new StrNum(	"tHgJv≏",	CNT.Ccapcnt.CNT_SIGTYPE_ISOLATE		),
			new StrNum(	"TTL",				CNT.Ccapcnt.CNT_SIGTYPE_TTL			),
			new StrNum(	"",				CNT.Ccapcnt.CNT_SIGTYPE_LINERECEIVER),
			new StrNum(	"End",				0								)
		};

		static StrNum[] Combo_CntDirL = 
		{
			new StrNum(	"_EJEg",	CNT.Ccapcnt.CNT_DIR_DOWN	),
			new StrNum(	"AbvJEg",	CNT.Ccapcnt.CNT_DIR_UP		),
			new StrNum(	"End",				0						)
		};

		static StrNum[] Combo_PhaseL = 
		{
			new StrNum(	"P",				CNT.Ccapcnt.CNT_MODE_1PHASE		),
			new StrNum(	"Q",				CNT.Ccapcnt.CNT_MODE_2PHASE		),
			new StrNum(	"Q[gRg[",	CNT.Ccapcnt.CNT_MODE_GATECONTROL),
			new StrNum(	"End",				0							)
		};

		static StrNum[] Combo_MulL=
		{
			new StrNum(	"P{",			CNT.Ccapcnt.CNT_MUL_X1		),
			new StrNum(	"Q{",			CNT.Ccapcnt.CNT_MUL_X2		),
			new StrNum(	"S{",			CNT.Ccapcnt.CNT_MUL_X4		),
			new StrNum(	"End",				0						)
		};
				
		static StrNum[] Combo_SyncClrL=
		{
			new StrNum(	"񓯊NA",		CNT.Ccapcnt.CNT_CLR_ASYNC	),
			new StrNum(	"NA",		CNT.Ccapcnt.CNT_CLR_SYNC	),
			new StrNum(	"End",				0						)
		};

		public Mode()
		{
			//
			// 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.btnSet = new System.Windows.Forms.Button();
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.Label10 = new System.Windows.Forms.Label();
			this.Label9 = new System.Windows.Forms.Label();
			this.Label8 = new System.Windows.Forms.Label();
			this.Label7 = 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.Label3 = new System.Windows.Forms.Label();
			this.cboClr = new System.Windows.Forms.ComboBox();
			this.cboMul = new System.Windows.Forms.ComboBox();
			this.txtFilter = new System.Windows.Forms.TextBox();
			this.cboPhase = new System.Windows.Forms.ComboBox();
			this.cboDir = new System.Windows.Forms.ComboBox();
			this.cboSource = new System.Windows.Forms.ComboBox();
			this.cboZLogic = new System.Windows.Forms.ComboBox();
			this.cboZMode = new System.Windows.Forms.ComboBox();
			this.cboChNo = new System.Windows.Forms.ComboBox();
			this.Label2 = new System.Windows.Forms.Label();
			this.Label1 = new System.Windows.Forms.Label();
			this.GroupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(239, 322);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(96, 24);
			this.btnCancel.TabIndex = 5;
			this.btnCancel.Text = "";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnSet
			// 
			this.btnSet.Location = new System.Drawing.Point(127, 322);
			this.btnSet.Name = "btnSet";
			this.btnSet.Size = new System.Drawing.Size(96, 24);
			this.btnSet.TabIndex = 4;
			this.btnSet.Text = "ݒ";
			this.btnSet.Click += new System.EventHandler(this.btnSet_Click);
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Label10,
																					this.Label9,
																					this.Label8,
																					this.Label7,
																					this.Label6,
																					this.Label5,
																					this.Label4,
																					this.Label3,
																					this.cboClr,
																					this.cboMul,
																					this.txtFilter,
																					this.cboPhase,
																					this.cboDir,
																					this.cboSource,
																					this.cboZLogic,
																					this.cboZMode,
																					this.cboChNo,
																					this.Label2,
																					this.Label1});
			this.GroupBox1.Location = new System.Drawing.Point(16, 10);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(320, 288);
			this.GroupBox1.TabIndex = 3;
			this.GroupBox1.TabStop = false;
			this.GroupBox1.Text = "JE^[h";
			// 
			// Label10
			// 
			this.Label10.Location = new System.Drawing.Point(287, 168);
			this.Label10.Name = "Label10";
			this.Label10.Size = new System.Drawing.Size(26, 15);
			this.Label10.TabIndex = 18;
			this.Label10.Text = "Hex";
			// 
			// Label9
			// 
			this.Label9.Location = new System.Drawing.Point(16, 253);
			this.Label9.Name = "Label9";
			this.Label9.Size = new System.Drawing.Size(85, 16);
			this.Label9.TabIndex = 17;
			this.Label9.Text = "NAF";
			// 
			// Label8
			// 
			this.Label8.Location = new System.Drawing.Point(16, 226);
			this.Label8.Name = "Label8";
			this.Label8.Size = new System.Drawing.Size(85, 16);
			this.Label8.TabIndex = 16;
			this.Label8.Text = "{F";
			// 
			// Label7
			// 
			this.Label7.Location = new System.Drawing.Point(16, 197);
			this.Label7.Name = "Label7";
			this.Label7.Size = new System.Drawing.Size(85, 16);
			this.Label7.TabIndex = 15;
			this.Label7.Text = "F";
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(16, 168);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(96, 16);
			this.Label6.TabIndex = 14;
			this.Label6.Text = "fW^tB^F";
			// 
			// Label5
			// 
			this.Label5.Location = new System.Drawing.Point(16, 140);
			this.Label5.Name = "Label5";
			this.Label5.Size = new System.Drawing.Size(85, 16);
			this.Label5.TabIndex = 13;
			this.Label5.Text = "JEgF";
			// 
			// Label4
			// 
			this.Label4.Location = new System.Drawing.Point(16, 112);
			this.Label4.Name = "Label4";
			this.Label4.Size = new System.Drawing.Size(85, 16);
			this.Label4.TabIndex = 12;
			this.Label4.Text = "MF";
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(16, 85);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(85, 16);
			this.Label3.TabIndex = 11;
			this.Label3.Text = "Z_F";
			// 
			// cboClr
			// 
			this.cboClr.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboClr.Location = new System.Drawing.Point(115, 248);
			this.cboClr.Name = "cboClr";
			this.cboClr.Size = new System.Drawing.Size(165, 20);
			this.cboClr.TabIndex = 10;
			// 
			// cboMul
			// 
			this.cboMul.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboMul.Location = new System.Drawing.Point(115, 220);
			this.cboMul.Name = "cboMul";
			this.cboMul.Size = new System.Drawing.Size(165, 20);
			this.cboMul.TabIndex = 9;
			// 
			// txtFilter
			// 
			this.txtFilter.Location = new System.Drawing.Point(115, 164);
			this.txtFilter.Name = "txtFilter";
			this.txtFilter.Size = new System.Drawing.Size(165, 19);
			this.txtFilter.TabIndex = 8;
			this.txtFilter.Text = "";
			// 
			// cboPhase
			// 
			this.cboPhase.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboPhase.Location = new System.Drawing.Point(115, 192);
			this.cboPhase.Name = "cboPhase";
			this.cboPhase.Size = new System.Drawing.Size(165, 20);
			this.cboPhase.TabIndex = 7;
			// 
			// cboDir
			// 
			this.cboDir.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDir.Location = new System.Drawing.Point(115, 136);
			this.cboDir.Name = "cboDir";
			this.cboDir.Size = new System.Drawing.Size(165, 20);
			this.cboDir.TabIndex = 6;
			// 
			// cboSource
			// 
			this.cboSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboSource.Location = new System.Drawing.Point(115, 108);
			this.cboSource.Name = "cboSource";
			this.cboSource.Size = new System.Drawing.Size(165, 20);
			this.cboSource.TabIndex = 5;
			// 
			// cboZLogic
			// 
			this.cboZLogic.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboZLogic.Location = new System.Drawing.Point(115, 80);
			this.cboZLogic.Name = "cboZLogic";
			this.cboZLogic.Size = new System.Drawing.Size(165, 20);
			this.cboZLogic.TabIndex = 4;
			// 
			// cboZMode
			// 
			this.cboZMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboZMode.Location = new System.Drawing.Point(115, 52);
			this.cboZMode.Name = "cboZMode";
			this.cboZMode.Size = new System.Drawing.Size(165, 20);
			this.cboZMode.TabIndex = 3;
			// 
			// cboChNo
			// 
			this.cboChNo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboChNo.Location = new System.Drawing.Point(115, 24);
			this.cboChNo.Name = "cboChNo";
			this.cboChNo.Size = new System.Drawing.Size(165, 20);
			this.cboChNo.TabIndex = 2;
			this.cboChNo.SelectedIndexChanged += new System.EventHandler(this.Combo_ChNo_SelectedIndexChanged);
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(16, 56);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(85, 16);
			this.Label2.TabIndex = 1;
			this.Label2.Text = "Z[hF";
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(16, 30);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(85, 16);
			this.Label1.TabIndex = 0;
			this.Label1.Text = "`lԍF";
			// 
			// Mode
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(346, 357);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnSet,
																		  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 = "Mode";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "[hݒ";
			this.Load += new System.EventHandler(this.Mode_Load);
			this.GroupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//---------------------------------------------------------
		//	Tu[`Fԍϊ
		//---------------------------------------------------------
		private string NumToStr(StrNum[] p, short Num)
		{
			short	i;
			string None = new string(' ', 2);
			for (i=0; string.Compare(p[i].Str, "End") != 0; i++)
			{
				if (p[i].Num == Num)
				{
					return p[i].Str;
				}
			}
			return None;
		}

		private short StrToNum(StrNum[] p, string Str)
		{
			short	i;
			for (i=0; string.Compare(p[i].Str, "End") != 0; i++)
			{
				if (string.Compare(p[i].Str, Str) == 0)
				{
					return p[i].Num;
				}
			}
			return 0;
		}

		//---------------------------------------------------------
		//	_CAO
		//---------------------------------------------------------
		private void Mode_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// R{{bNX̏
			//-----------------------------------
			short	i;
			for (i=0; i<Common.ChannelNum; i++)
			{
				szText = string.Format("{0:d}", i);
				cboChNo.Items.Add(szText);
			}
            cboChNo.SelectedIndex = 0;
			for (i=0; string.Compare(Combo_ZModeL[i].Str, "End") != 0; i++)
			{
				cboZMode.Items.Add(Combo_ZModeL[i].Str);
			}
			for (i=0; string.Compare(Combo_ZLogicL[i].Str, "End") != 0; i++)
			{
				cboZLogic.Items.Add(Combo_ZLogicL[i].Str);
			}
			for (i=0; string.Compare(Combo_ChannelSignalL[i].Str, "End") != 0; i++)
			{
				cboSource.Items.Add(Combo_ChannelSignalL[i].Str);
			}
			for (i=0; string.Compare(Combo_CntDirL[i].Str, "End") != 0; i++)
			{
				cboDir.Items.Add(Combo_CntDirL[i].Str);
			}
			for (i=0; string.Compare(Combo_PhaseL[i].Str, "End") != 0; i++)
			{
				cboPhase.Items.Add(Combo_PhaseL[i].Str);
			}
			for (i=0; string.Compare(Combo_MulL[i].Str, "End") != 0; i++)
			{
				cboMul.Items.Add(Combo_MulL[i].Str);
			}
			for (i=0; string.Compare(Combo_SyncClrL[i].Str, "End") != 0; i++)
			{
				cboClr.Items.Add(Combo_SyncClrL[i].Str);
			}
			//-----------------------------------
			// `l0I
			//-----------------------------------
			Combo_ChNo_SelectedIndexChanged(cboChNo, new System.EventArgs());
		}

		//---------------------------------------------------------
		//	`lR{{bNXI
		//---------------------------------------------------------
		private void Combo_ChNo_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// [h̎擾
			//-----------------------------------
			szText = cboChNo.Text;
			ChNo = short.Parse(szText, NumberStyles.Integer);

			CNT.Ccapcnt.CntGetZMode(Common.Id, ChNo, ref ZMode);
			CNT.Ccapcnt.CntGetZLogic(Common.Id, ChNo, ref ZLogic);
			CNT.Ccapcnt.CntGetChannelSignal(Common.Id, ChNo, ref SigType);
			CNT.Ccapcnt.CntGetCountDirection(Common.Id, ChNo, ref Dir);
			CNT.Ccapcnt.CntGetOperationMode(Common.Id, ChNo, ref Phase, ref Mul, ref SyncClr);
			CNT.Ccapcnt.CntGetDigitalFilter(Common.Id, ChNo, ref FilterValue);
			//-----------------------------------
			// [h̐ݒ
			//-----------------------------------
			cboZMode.Text = NumToStr(Combo_ZModeL, ZMode);
			cboZLogic.Text = NumToStr(Combo_ZLogicL, ZLogic);
			cboSource.Text = NumToStr(Combo_ChannelSignalL, SigType);
			cboDir.Text = NumToStr(Combo_CntDirL, Dir);
			cboPhase.Text = NumToStr(Combo_PhaseL, Phase);
			cboMul.Text = NumToStr(Combo_MulL, Mul);
			cboClr.Text = NumToStr(Combo_SyncClrL, SyncClr);
			szText = string.Format("{0:X}", FilterValue);
			txtFilter.Text = szText;
		}

		//---------------------------------------------------------
		//	ݒ{^̏
		//---------------------------------------------------------
		private void btnSet_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// ʂ烂[h擾
			//-----------------------------------
			szText = cboZMode.Text;
			ZMode = StrToNum(Combo_ZModeL, szText);
			szText = cboZLogic.Text;
			ZLogic = StrToNum(Combo_ZLogicL,szText);
			szText = cboSource.Text;
			SigType = StrToNum(Combo_ChannelSignalL, szText);
			szText = cboDir.Text;
			Dir = StrToNum(Combo_CntDirL, szText);
			szText = cboPhase.Text;
			Phase = StrToNum(Combo_PhaseL, szText);
			szText = cboMul.Text;
			Mul = StrToNum(Combo_MulL, szText);
			szText = cboClr.Text;
			SyncClr = StrToNum(Combo_SyncClrL, szText);
			szText = txtFilter.Text;
			FilterValue = short.Parse(szText, NumberStyles.HexNumber);
			//-----------------------------------
			// [h̐ݒ
			//-----------------------------------
			Ret = CNT.Ccapcnt.CntSetZMode(Common.Id, ChNo, ZMode);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetZMode : " + szError;
				MessageBox.Show(szReturnCode, "G[", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSetZLogic(Common.Id, ChNo, ZLogic);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetZLogic : " + szError;
				MessageBox.Show(szReturnCode, "G[", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSelectChannelSignal(Common.Id, ChNo, SigType);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSelectChannelSignal : " + szError;
				MessageBox.Show(szReturnCode, "G[", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSetCountDirection(Common.Id, ChNo, Dir);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetCountDirection : " + szError;
				MessageBox.Show(szReturnCode, "G[", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSetOperationMode(Common.Id, ChNo, Phase, Mul, SyncClr);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetOperationMode : " + szError;
				MessageBox.Show(szReturnCode, "G[", MessageBoxButtons.OK);
			}
			Ret = CNT.Ccapcnt.CntSetDigitalFilter(Common.Id, ChNo, FilterValue);
			if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
			{
				CNT.Ccapcnt.CntGetErrorString(Ret, szError);
				szReturnCode = "CntSetDigitalFilter : " + szError;
				MessageBox.Show(szReturnCode, "G[", MessageBoxButtons.OK);
			}
			this.Close();
		}

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