/******************************************************************************
 * MarkView.cs 
 * 
 * <Copyright and License> 
 * 
 * This file is part of GODS, a package for annotating medical images. 
 * 
 * Copyright (C) 2004 
 * Xuedong Chen 
 * Heri Mariwa 
 * Kimmy (Hsiang-ling) Lin 
 * Rui Yu 
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 *  
 * For further questions about this file, please send email to 
 * gods@cs.umb.edu or visit us at http://gods.sourceforge.net.  
 ******************************************************************************/

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using GODS.MarkData;

namespace GODS.MarkGUI
{
	/// <summary>
	/// Summary description for MarkView.
	/// </summary>
	public class MarkView : System.Windows.Forms.UserControl
	{
		private Mark myMark;
		private System.Windows.Forms.Label lblMarkName;
		private System.Windows.Forms.ComboBox cBxVal;
		private bool firstLoad = true;
		//indicate wether this  markview  selected
		private bool selectedOn = false;
		//inidicate wether the mark view can be selected
		//no need to select during non configaration activity
		private bool selectionEnable = false; 


		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MarkView(Mark mark)
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
			this.myMark = mark;
			this.lblMarkName.Text = mark.MarkName;
			if (mark.HasImage) 
			{
				mark.Location = new System.Drawing.Point(8, 8);
				this.Controls.Add(mark);
				this.myMark.MouseDown += new MouseEventHandler(myMark_MouseDown);
			}
				
			if (mark.HasPresetValues) 
			{
				this.cBxVal.Items.AddRange(mark.PresetValues.ToArray());
				this.cBxVal.SelectedIndex = 0;
				mark.CurrentValueIndex = 0;
			}
			else 
			{
				this.Controls.Remove(this.cBxVal);
				this.Height = 50;
			}
		}

		public Mark ViewMark
		{
			get
			{
				return this.myMark;
			}
		}

		//Method will set the mark as selected
		//done by setting the border properties on
		public void selectOn()
		{
			this.BackColor = System.Drawing.SystemColors.ControlDark;
			this.selectedOn = true;
		}
		//Method will set the mark as selected
		//done by setting the border properties off
		public void selectOff()
		{
			this.BackColor = System.Drawing.SystemColors.ControlLight;
			this.selectedOn = false;
		} 

		public bool IsSelectOn
		{
			get 
			{
				return selectedOn;
			}
		}

		public Mark MyMark
		{
			get
			{
				return this.myMark;
			}
		}

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

		//method set click event handler --
		public void setClickEvent(System.EventHandler handler)
		{  
			this.Click += handler;
			this.selectionEnable = true;
		}

		#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.lblMarkName = new System.Windows.Forms.Label();
			this.cBxVal = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// lblMarkName
			// 
			this.lblMarkName.Location = new System.Drawing.Point(96, 8);
			this.lblMarkName.Name = "lblMarkName";
			this.lblMarkName.Size = new System.Drawing.Size(80, 35);
			this.lblMarkName.TabIndex = 1;
			this.lblMarkName.Text = "dummyTxt";
			this.lblMarkName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.lblMarkName.Click += new System.EventHandler(this.lblMarkName_Click);
			// 
			// cBxVal
			// 
			this.cBxVal.Location = new System.Drawing.Point(8, 56);
			this.cBxVal.Name = "cBxVal";
			this.cBxVal.Size = new System.Drawing.Size(168, 21);
			this.cBxVal.TabIndex = 2;
			this.cBxVal.Click += new System.EventHandler(this.cBxVal_Click);
			this.cBxVal.SelectedIndexChanged += new System.EventHandler(this.cBxVal_SelectedIndexChanged);
			// 
			// MarkView
			// 
			this.Controls.Add(this.cBxVal);
			this.Controls.Add(this.lblMarkName);
			this.Name = "MarkView";
			this.Size = new System.Drawing.Size(184, 88);
			this.ResumeLayout(false);

		}
		#endregion

		public void setCurrentValueIndex(int index)
		{
			this.cBxVal.SelectedIndex = index;			
		}

		private void cBxVal_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// when first loading the mark panel, this event will be called
			// but we don't need to add the mark even though there is an active anno form
			if (this.firstLoad) 
			{
				this.firstLoad = false;
			}
			else if (!this.myMark.HasImage && GODS.GODSMain.ActiveAnnoForm != null) // mark w/o image but with preset values
			{
				this.myMark.CurrentValueIndex = this.cBxVal.SelectedIndex;
				GODS.GODSMain.ActiveAnnoForm.addNoImageMark((Mark)this.myMark.Clone());
			}
			else if (this.myMark.HasPresetValues) // mark with image and preset values
			{
				//this.myMark.CurrentValueIndex = this.cBxVal.SelectedIndex;
				if (GODS.GODSMain.ActiveAnnoForm != null)
				{
					//To if Node is current mark change its description
					this.myMark.CurrentValueIndex = this.cBxVal.SelectedIndex;
					Mark curm = GODS.GODSMain.ActiveAnnoForm.CurrentMark;
					if (curm != null && curm.MarkID == this.myMark.MarkID)
						curm.CurrentValueIndex = this.cBxVal.SelectedIndex;
					GODS.GODSMain.ActiveAnnoForm.refreshSummary();
				}
			}
		}

		private void lblMarkName_Click(object sender, System.EventArgs e)
		{
			//pass the click event down
			if (this.selectionEnable)
				this.InvokeOnClick();
		}

		private void cBxVal_Click(object sender, System.EventArgs e)
		{
			//pass the click event down
			if (this.selectionEnable)
				this.InvokeOnClick();
		}

		public void InvokeOnClick()
		{
			this.InvokeOnClick(this, null);
		}

		private void myMark_MouseDown(object sender, MouseEventArgs e)
		{
			//pass the click event down
			if (this.selectionEnable)
				this.InvokeOnClick();
		}
	}
}
