/*********************************************************************************************
 * Copyright 2004 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: SymbDlg.cs $     $Revision: 1 $
 * $Author: Kathy $   $Date: 7/27/04 8:34a $
 *
 * $History: SymbDlg.cs $
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 7/27/04    Time: 8:34a
 * Created in $/LibSource/Utils
 *********************************************************************************************/

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

namespace Utils
{
	/// <summary>
	/// Summary description for SymbDlg.
	/// </summary>
	public class SymbDlg : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tbctlSymbol;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private ListBox symlistbox;
		public SymbolListB SymList1;
		public string ReturnText;
		private int NumItems;
		private System.Windows.Forms.TextBox tbValue;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		public GridItem gridItem;
		private string[] SymbolFonts = {"VESymb","Courier New","VolianDraw"};

		public SymbDlg(SymbolListB symb, GridItem grditm)
		{
			//
			// Required for Windows Form Designer support
			//
			ReturnText=null;
			gridItem = grditm;
			
			SymList1 = symb;
			NumItems = symb.SymbolListStrings.Length;
			InitializeComponent();
			
			DoListTabPage();
			DoGridTabPage();
			
			if (gridItem!=null)
			{
				tbValue.Text = (string) gridItem.Value;
				tbValue.Visible = true;
			}
			else
				tbValue.Visible = false;
		}

		private void DoGridTabPage()
		{
			//TabPage gridTabPage = new TabPage("Grid");
			//tbctlSymbol.TabPages.Add(gridTabPage);
		}

		private void symlistbox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
		{
			// Set the DrawMode property to draw fixed sized items.
			symlistbox.DrawMode = DrawMode.OwnerDrawFixed;

			// Draw the background of the ListBox control for each item.
			e.DrawBackground();
			// Create a new Brush and initialize to a Black colored brush by default.
			Brush myBrush = Brushes.Black;
			Font fontVESymb = new Font(SymbolFonts[SymList1.SymbolList[e.Index].CharacterSet],e.Font.SizeInPoints);
			Font fontregular = new Font("Courier New",e.Font.SizeInPoints);
			float x;
			// Draw the current item text based on the current Font and the custom brush settings.
			e.Graphics.DrawString(SymList1.SymbolList[e.Index].CharacterCode,fontVESymb,myBrush,e.Bounds.Left,e.Bounds.Top);
			x=e.Graphics.MeasureString(SymList1.SymbolList[e.Index].CharacterCode,fontVESymb).Width;
			e.Graphics.DrawString(SymList1.SymbolListStrings[e.Index],fontregular,myBrush,e.Bounds.Left+x,e.Bounds.Top);
			e.DrawFocusRectangle();
		}

		private void DoListTabPage()
		{
			TabPage listTabPage = new TabPage("List");
			tbctlSymbol.TabPages.Add(listTabPage);
			symlistbox = new ListBox();
			symlistbox.Height = 200;
			symlistbox.Width = 400;
			listTabPage.Controls.Add(symlistbox);
			symlistbox.DrawItem += new DrawItemEventHandler(symlistbox_DrawItem);
			symlistbox.DoubleClick += new EventHandler(symlistbox_DoubleClick);
			symlistbox.DrawMode = DrawMode.OwnerDrawFixed;
			// add dummy items, these will be resolved when displayed.
			for(int i=0; i<NumItems;i++)
				symlistbox.Items.Add(i.ToString());
		}

		private void symlistbox_DoubleClick(Object sender, EventArgs e)
		{
			// use encoding for the Microsoft Windows OEM Codepage 437 to get the old
			// 'dos' character codes. Also, needed to use the encoding & transfer to/from
			// bytes so that the unicode character wouldn't be entered thus causing a
			// character conversion. (strings are unicode by default in .net)
			int itm = symlistbox.SelectedIndex;
			Encoding oem = Encoding.GetEncoding(437);
			byte[] oemBytes = oem.GetBytes(tbValue.Text);
			byte[] tst = new byte[oemBytes.Length+1];
			int indx = tbValue.SelectionStart;
			for (int i=0; i<indx; i++) tst[i]=oemBytes[i];
			tst[indx]=SymList1.SymbolList[itm].Translation;
			for (int i=indx;i<tbValue.Text.Length;i++) tst[i+1]=oemBytes[i];
			tbValue.Text = oem.GetString(tst);
			tbValue.Refresh();
		}

		/// <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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SymbDlg));
			this.tbctlSymbol = new System.Windows.Forms.TabControl();
			this.tbValue = new System.Windows.Forms.TextBox();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// tbctlSymbol
			// 
			this.tbctlSymbol.Location = new System.Drawing.Point(8, 0);
			this.tbctlSymbol.Name = "tbctlSymbol";
			this.tbctlSymbol.SelectedIndex = 0;
			this.tbctlSymbol.Size = new System.Drawing.Size(464, 264);
			this.tbctlSymbol.TabIndex = 0;
			// 
			// tbValue
			// 
			this.tbValue.Location = new System.Drawing.Point(8, 280);
			this.tbValue.Name = "tbValue";
			this.tbValue.Size = new System.Drawing.Size(504, 20);
			this.tbValue.TabIndex = 1;
			this.tbValue.Text = "";
			// 
			// btnOK
			// 
			this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnOK.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnOK.Location = new System.Drawing.Point(72, 320);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(104, 24);
			this.btnOK.TabIndex = 2;
			this.btnOK.Text = "Save Text";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnCancel.Location = new System.Drawing.Point(208, 320);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(72, 23);
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "Cancel";
			// 
			// SymbDlg
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(672, 350);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.tbValue);
			this.Controls.Add(this.tbctlSymbol);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "SymbDlg";
			this.Text = "Symbol";
			this.ResumeLayout(false);

		}
		#endregion

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			ReturnText = tbValue.Text;
		}
	}
}
