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

namespace JovianTools.Support
{
	/// <summary>
	/// Summary description for rtfReplace.
	/// </summary>
	public class rtfReplace : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.ListBox lstSearchResults;
		private System.Windows.Forms.Label clkCancel;
		private System.Windows.Forms.ImageList imgList;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.ComboBox cmoBox;
		private System.Windows.Forms.Button btnFilter;
		private System.Windows.Forms.Button btnRemove;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Button btnReplace;
		private System.Windows.Forms.TextBox txtRepTxt;

		private JovianEdit Owner;
		public rtfReplace(JovianEdit own)
		{
			Owner = own;
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the 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 Component 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.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(rtfReplace));
			this.lstSearchResults = new System.Windows.Forms.ListBox();
			this.clkCancel = new System.Windows.Forms.Label();
			this.imgList = new System.Windows.Forms.ImageList(this.components);
			this.cmoBox = new System.Windows.Forms.ComboBox();
			this.btnFilter = new System.Windows.Forms.Button();
			this.btnRemove = new System.Windows.Forms.Button();
			this.btnDelete = new System.Windows.Forms.Button();
			this.btnReplace = new System.Windows.Forms.Button();
			this.txtRepTxt = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// lstSearchResults
			// 
			this.lstSearchResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lstSearchResults.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lstSearchResults.Cursor = System.Windows.Forms.Cursors.Arrow;
			this.lstSearchResults.Location = new System.Drawing.Point(4, 46);
			this.lstSearchResults.Name = "lstSearchResults";
			this.lstSearchResults.Size = new System.Drawing.Size(80, 145);
			this.lstSearchResults.TabIndex = 0;
			this.lstSearchResults.SelectedIndexChanged += new System.EventHandler(this.lstSearchResults_SelectedIndexChanged);
			// 
			// clkCancel
			// 
			this.clkCancel.Cursor = System.Windows.Forms.Cursors.Hand;
			this.clkCancel.ImageIndex = 0;
			this.clkCancel.ImageList = this.imgList;
			this.clkCancel.Location = new System.Drawing.Point(68, 2);
			this.clkCancel.Name = "clkCancel";
			this.clkCancel.Size = new System.Drawing.Size(18, 16);
			this.clkCancel.TabIndex = 8;
			this.clkCancel.Click += new System.EventHandler(this.clkCancel_Click);
			this.clkCancel.MouseEnter += new System.EventHandler(this.clkCancel_MouseEnter);
			this.clkCancel.MouseLeave += new System.EventHandler(this.clkCancel_MouseLeave);
			// 
			// imgList
			// 
			this.imgList.ImageSize = new System.Drawing.Size(16, 16);
			this.imgList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgList.ImageStream")));
			this.imgList.TransparentColor = System.Drawing.Color.Olive;
			// 
			// cmoBox
			// 
			this.cmoBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.cmoBox.Cursor = System.Windows.Forms.Cursors.Arrow;
			this.cmoBox.Items.AddRange(new object[] {
														"0",
														"1",
														"2",
														"3",
														"4"});
			this.cmoBox.Location = new System.Drawing.Point(4, 194);
			this.cmoBox.Name = "cmoBox";
			this.cmoBox.Size = new System.Drawing.Size(80, 21);
			this.cmoBox.TabIndex = 9;
			this.cmoBox.Text = "0";
			// 
			// btnFilter
			// 
			this.btnFilter.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.btnFilter.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnFilter.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnFilter.Location = new System.Drawing.Point(4, 242);
			this.btnFilter.Name = "btnFilter";
			this.btnFilter.Size = new System.Drawing.Size(78, 20);
			this.btnFilter.TabIndex = 10;
			this.btnFilter.Text = "Filter";
			this.btnFilter.Click += new System.EventHandler(this.btnFilter_Click);
			// 
			// btnRemove
			// 
			this.btnRemove.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.btnRemove.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnRemove.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnRemove.Location = new System.Drawing.Point(4, 218);
			this.btnRemove.Name = "btnRemove";
			this.btnRemove.Size = new System.Drawing.Size(78, 20);
			this.btnRemove.TabIndex = 11;
			this.btnRemove.Text = "Remove";
			this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
			// 
			// btnDelete
			// 
			this.btnDelete.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.btnDelete.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnDelete.Location = new System.Drawing.Point(4, 22);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(78, 20);
			this.btnDelete.TabIndex = 12;
			this.btnDelete.Text = "Delete";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnReplace
			// 
			this.btnReplace.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.btnReplace.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnReplace.Location = new System.Drawing.Point(4, 326);
			this.btnReplace.Name = "btnReplace";
			this.btnReplace.Size = new System.Drawing.Size(78, 20);
			this.btnReplace.TabIndex = 13;
			this.btnReplace.Text = "Replace";
			this.btnReplace.Click += new System.EventHandler(this.btnReplace_Click);
			// 
			// txtRepTxt
			// 
			this.txtRepTxt.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtRepTxt.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtRepTxt.Location = new System.Drawing.Point(4, 280);
			this.txtRepTxt.Multiline = true;
			this.txtRepTxt.Name = "txtRepTxt";
			this.txtRepTxt.Size = new System.Drawing.Size(78, 40);
			this.txtRepTxt.TabIndex = 14;
			this.txtRepTxt.Text = "";
			// 
			// rtfReplace
			// 
			this.Controls.Add(this.txtRepTxt);
			this.Controls.Add(this.btnReplace);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnRemove);
			this.Controls.Add(this.btnFilter);
			this.Controls.Add(this.cmoBox);
			this.Controls.Add(this.clkCancel);
			this.Controls.Add(this.lstSearchResults);
			this.Cursor = System.Windows.Forms.Cursors.Arrow;
			this.Name = "rtfReplace";
			this.Size = new System.Drawing.Size(90, 354);
			this.Load += new System.EventHandler(this.rtfReplace_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void clkCancel_MouseEnter(object sender, System.EventArgs e)
		{
			clkCancel.ImageIndex = 1;
		}

		private void clkCancel_MouseLeave(object sender, System.EventArgs e)
		{
			clkCancel.ImageIndex = 0;
		}

		private void clkCancel_Click(object sender, System.EventArgs e)
		{
			Owner.KillReplaceBar();
		}

		public void ResetResults(ArrayList R)
		{
			lstSearchResults.Items.Clear();
			lstSearchResults.Items.AddRange(R.ToArray());
		}

		private void lstSearchResults_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			btnDelete.Enabled = false;
			if(lstSearchResults.SelectedIndex >= 0)
			{
				rtfSearchRange key = lstSearchResults.Items[lstSearchResults.SelectedIndex] as rtfSearchRange;
				if(key != null)
				{
					btnDelete.Enabled = true;
					if(key.Idx >= 0) Owner.ChangeCursor(key.Idx);
				}
			}
		}

		private void btnRemove_Click(object sender, System.EventArgs e)
		{
			Owner.AlterSearchResults( Int32.Parse(cmoBox.Text), true );
		}

		private void btnFilter_Click(object sender, System.EventArgs e)
		{
			Owner.AlterSearchResults( Int32.Parse(cmoBox.Text), false );
		}

		private void rtfReplace_Load(object sender, System.EventArgs e)
		{
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			if(lstSearchResults.SelectedIndex >= 0)
			{
				int sIdx = lstSearchResults.SelectedIndex;
				rtfSearchRange key = lstSearchResults.Items[lstSearchResults.SelectedIndex] as rtfSearchRange;
				if(key != null)
				{
					Owner.DeleteSearchResults(key);
				}
				if(lstSearchResults.Items.Count > 0)
				lstSearchResults.SelectedIndex = Math.Min(sIdx,lstSearchResults.Items.Count - 1);
			}
		}

		private void btnReplace_Click(object sender, System.EventArgs e)
		{
			Owner.ExecuteReplace(txtRepTxt.Text);
		}
	}
}
