/******************************************************************************
 * AnnoForm.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.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace GODS
{
	using MarkData;
	using DBHandler;

	/// <summary>
	/// Summary description for AnnoForm.
	/// </summary>
	public class AnnoForm : System.Windows.Forms.Form
	{
		private ImageData imageData;
		private GODSMain mainApp;
		private DiagnosticPanel diagnosticPanel;

		private bool modified = false;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.Label lblMRN;
		private System.Windows.Forms.Label lblDate;
		private System.Windows.Forms.TextBox txtBxMRN;
		private System.Windows.Forms.Button btnOriginal;
		private System.Windows.Forms.TextBox txtBxDate;
		private System.Windows.Forms.GroupBox dimCtrlGrp;
		private System.Windows.Forms.Label lbX;
		private System.Windows.Forms.ComboBox heightDropBox;
		private System.Windows.Forms.ComboBox widthDropBox;
		private System.Windows.Forms.Button btnComment;
		private System.Windows.Forms.Button btnZoomOut;
		private System.Windows.Forms.Button btnZoomIn;
		private System.Windows.Forms.ListBox summaryLstBox;
		private System.Windows.Forms.Panel imgPanel;
		private System.Windows.Forms.Button btnDeleteMark;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnFinalize;

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

		public AnnoForm(GODSMain mainApp, ImageData imgData, DiagnosticImage dImage)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.mainApp = mainApp;
			this.imageData = imgData;
			this.setupDiagnosticPanel(dImage);
			this.txtBxMRN.Text = "" + imgData.Mrn;
			this.txtBxDate.Text = imgData.AppDate.ToString();
			this.fillMarkDimension();
			this.doDisable();
			this.Closing +=new CancelEventHandler(AnnoForm_Closing);
		}

		/// <summary>
		/// Disable the buttons on this form once the image is finalized.
		/// </summary>
		public void doDisable()
		{
			if(imageData.Final)
			{
				this.btnComment.Enabled = false;
				this.btnDeleteMark.Enabled = false;
				this.btnFinalize.Enabled = false;
				this.btnSave.Enabled = false;
				this.btnZoomIn.Enabled = false;
				this.btnZoomOut.Enabled = false;
				//this.heightDropBox.Enabled = false;
				//this.widthDropBox.Enabled = false;
			}
		}

		/// <summary>
		/// Check whether this annotation is modified since it's created
		/// or retrieved from the database.
		/// </summary>
		public bool IsModified
		{
			get 
			{
				return this.modified;
			}
			set 
			{
				this.modified = value;
			}
		}

		/// <summary>
		/// Get the ID of the markset this annotation used.
		/// </summary>
		public int MarksetID
		{
			get 
			{
				return this.imageData.MarkSetID;
			}
			set
			{
				this.imageData.MarkSetID = value;
			}
		}

		/// <summary>
		/// Get the current active mark on the annotation form.
		/// </summary>
		public Mark CurrentMark
		{
			get 
			{
				return diagnosticPanel.CurrentMark;
			}
		}

		/// <summary>
		/// Check whether this annotation is finalized.
		/// </summary>
		public bool IsFinalized
		{
			get
			{
				return this.imageData.Final;
			}
		}

		/// <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(AnnoForm));
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.lblMRN = new System.Windows.Forms.Label();
			this.lblDate = new System.Windows.Forms.Label();
			this.txtBxMRN = new System.Windows.Forms.TextBox();
			this.txtBxDate = new System.Windows.Forms.TextBox();
			this.btnOriginal = new System.Windows.Forms.Button();
			this.dimCtrlGrp = new System.Windows.Forms.GroupBox();
			this.lbX = new System.Windows.Forms.Label();
			this.heightDropBox = new System.Windows.Forms.ComboBox();
			this.widthDropBox = new System.Windows.Forms.ComboBox();
			this.btnComment = new System.Windows.Forms.Button();
			this.btnZoomOut = new System.Windows.Forms.Button();
			this.btnZoomIn = new System.Windows.Forms.Button();
			this.summaryLstBox = new System.Windows.Forms.ListBox();
			this.imgPanel = new System.Windows.Forms.Panel();
			this.btnDeleteMark = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnFinalize = new System.Windows.Forms.Button();
			this.dimCtrlGrp.SuspendLayout();
			this.SuspendLayout();
			// 
			// toolBar1
			// 
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(576, 42);
			this.toolBar1.TabIndex = 0;
			// 
			// lblMRN
			// 
			this.lblMRN.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblMRN.Location = new System.Drawing.Point(8, 16);
			this.lblMRN.Name = "lblMRN";
			this.lblMRN.Size = new System.Drawing.Size(40, 16);
			this.lblMRN.TabIndex = 1;
			this.lblMRN.Text = "MRN";
			// 
			// lblDate
			// 
			this.lblDate.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblDate.Location = new System.Drawing.Point(152, 16);
			this.lblDate.Name = "lblDate";
			this.lblDate.Size = new System.Drawing.Size(32, 16);
			this.lblDate.TabIndex = 2;
			this.lblDate.Text = "Date";
			// 
			// txtBxMRN
			// 
			this.txtBxMRN.Enabled = false;
			this.txtBxMRN.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtBxMRN.Location = new System.Drawing.Point(48, 13);
			this.txtBxMRN.Name = "txtBxMRN";
			this.txtBxMRN.Size = new System.Drawing.Size(90, 20);
			this.txtBxMRN.TabIndex = 3;
			this.txtBxMRN.Text = "";
			// 
			// txtBxDate
			// 
			this.txtBxDate.Enabled = false;
			this.txtBxDate.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtBxDate.Location = new System.Drawing.Point(200, 13);
			this.txtBxDate.Name = "txtBxDate";
			this.txtBxDate.Size = new System.Drawing.Size(144, 20);
			this.txtBxDate.TabIndex = 4;
			this.txtBxDate.Text = "";
			// 
			// btnOriginal
			// 
			this.btnOriginal.Location = new System.Drawing.Point(360, 11);
			this.btnOriginal.Name = "btnOriginal";
			this.btnOriginal.Size = new System.Drawing.Size(120, 24);
			this.btnOriginal.TabIndex = 5;
			this.btnOriginal.Text = "Original Image";
			this.btnOriginal.Click += new System.EventHandler(this.btnOriginal_Click);
			// 
			// dimCtrlGrp
			// 
			this.dimCtrlGrp.BackColor = System.Drawing.SystemColors.Control;
			this.dimCtrlGrp.Controls.Add(this.lbX);
			this.dimCtrlGrp.Controls.Add(this.heightDropBox);
			this.dimCtrlGrp.Controls.Add(this.widthDropBox);
			this.dimCtrlGrp.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dimCtrlGrp.Location = new System.Drawing.Point(8, 304);
			this.dimCtrlGrp.Name = "dimCtrlGrp";
			this.dimCtrlGrp.Size = new System.Drawing.Size(136, 40);
			this.dimCtrlGrp.TabIndex = 48;
			this.dimCtrlGrp.TabStop = false;
			this.dimCtrlGrp.Text = "Mark Dimension";
			// 
			// lbX
			// 
			this.lbX.Location = new System.Drawing.Point(57, 17);
			this.lbX.Name = "lbX";
			this.lbX.Size = new System.Drawing.Size(18, 18);
			this.lbX.TabIndex = 2;
			this.lbX.Text = "X";
			this.lbX.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// heightDropBox
			// 
			this.heightDropBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.heightDropBox.Location = new System.Drawing.Point(80, 14);
			this.heightDropBox.Name = "heightDropBox";
			this.heightDropBox.Size = new System.Drawing.Size(48, 21);
			this.heightDropBox.TabIndex = 1;
			this.heightDropBox.SelectedIndexChanged += new System.EventHandler(this.heightDropBox_SelectedIndexChanged);
			// 
			// widthDropBox
			// 
			this.widthDropBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.widthDropBox.Location = new System.Drawing.Point(5, 14);
			this.widthDropBox.Name = "widthDropBox";
			this.widthDropBox.Size = new System.Drawing.Size(48, 21);
			this.widthDropBox.TabIndex = 0;
			this.widthDropBox.SelectedIndexChanged += new System.EventHandler(this.widthDropBox_SelectedIndexChanged);
			// 
			// btnComment
			// 
			this.btnComment.Location = new System.Drawing.Point(264, 320);
			this.btnComment.Name = "btnComment";
			this.btnComment.Size = new System.Drawing.Size(104, 24);
			this.btnComment.TabIndex = 67;
			this.btnComment.Text = "Add Comment";
			this.btnComment.Click += new System.EventHandler(this.btnComment_Click);
			// 
			// btnZoomOut
			// 
			this.btnZoomOut.Image = ((System.Drawing.Image)(resources.GetObject("btnZoomOut.Image")));
			this.btnZoomOut.Location = new System.Drawing.Point(208, 320);
			this.btnZoomOut.Name = "btnZoomOut";
			this.btnZoomOut.Size = new System.Drawing.Size(48, 24);
			this.btnZoomOut.TabIndex = 78;
			this.btnZoomOut.Click += new System.EventHandler(this.btnZoomOut_Click);
			// 
			// btnZoomIn
			// 
			this.btnZoomIn.Image = ((System.Drawing.Image)(resources.GetObject("btnZoomIn.Image")));
			this.btnZoomIn.Location = new System.Drawing.Point(152, 320);
			this.btnZoomIn.Name = "btnZoomIn";
			this.btnZoomIn.Size = new System.Drawing.Size(48, 24);
			this.btnZoomIn.TabIndex = 77;
			this.btnZoomIn.Click += new System.EventHandler(this.btnZoomIn_Click);
			// 
			// summaryLstBox
			// 
			this.summaryLstBox.Cursor = System.Windows.Forms.Cursors.Default;
			this.summaryLstBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.summaryLstBox.HorizontalScrollbar = true;
			this.summaryLstBox.ItemHeight = 20;
			this.summaryLstBox.Location = new System.Drawing.Point(8, 352);
			this.summaryLstBox.Name = "summaryLstBox";
			this.summaryLstBox.Size = new System.Drawing.Size(560, 184);
			this.summaryLstBox.TabIndex = 79;
			this.summaryLstBox.SelectedIndexChanged += new System.EventHandler(this.summaryLstBox_SelectedIndexChanged);
			// 
			// imgPanel
			// 
			this.imgPanel.AutoScroll = true;
			this.imgPanel.Location = new System.Drawing.Point(8, 40);
			this.imgPanel.Name = "imgPanel";
			this.imgPanel.Size = new System.Drawing.Size(464, 256);
			this.imgPanel.TabIndex = 80;
			// 
			// btnDeleteMark
			// 
			this.btnDeleteMark.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(0)));
			this.btnDeleteMark.Location = new System.Drawing.Point(376, 320);
			this.btnDeleteMark.Name = "btnDeleteMark";
			this.btnDeleteMark.Size = new System.Drawing.Size(112, 24);
			this.btnDeleteMark.TabIndex = 81;
			this.btnDeleteMark.Text = "Delete Mark";
			this.btnDeleteMark.Click += new System.EventHandler(this.btnDeleteMark_Click);
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(488, 11);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(72, 24);
			this.btnSave.TabIndex = 82;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnFinalize
			// 
			this.btnFinalize.Location = new System.Drawing.Point(496, 320);
			this.btnFinalize.Name = "btnFinalize";
			this.btnFinalize.Size = new System.Drawing.Size(72, 24);
			this.btnFinalize.TabIndex = 83;
			this.btnFinalize.Text = "Finalize";
			this.btnFinalize.Click += new System.EventHandler(this.btnFinalize_Click);
			// 
			// AnnoForm
			// 
			this.AutoScale = false;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(576, 566);
			this.Controls.Add(this.btnFinalize);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.btnDeleteMark);
			this.Controls.Add(this.imgPanel);
			this.Controls.Add(this.summaryLstBox);
			this.Controls.Add(this.btnZoomOut);
			this.Controls.Add(this.btnZoomIn);
			this.Controls.Add(this.btnComment);
			this.Controls.Add(this.dimCtrlGrp);
			this.Controls.Add(this.btnOriginal);
			this.Controls.Add(this.txtBxDate);
			this.Controls.Add(this.txtBxMRN);
			this.Controls.Add(this.lblDate);
			this.Controls.Add(this.lblMRN);
			this.Controls.Add(this.toolBar1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimumSize = new System.Drawing.Size(385, 390);
			this.Name = "AnnoForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "Annotation";
			this.TopMost = true;
			this.Activated += new System.EventHandler(this.AnnoForm_Activated);
			this.dimCtrlGrp.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Add a mark without image.
		/// </summary>
		/// <param name="mark"></param>
		public void addNoImageMark(Mark mark)
		{
			this.diagnosticPanel.AddNoImageMark(mark);
		}

		/// <summary>
		/// Set itself to be the active form for the whole app when
		/// there are action performed on this form.
		/// </summary>
		public void setAsActiveForm()
		{
			this.mainApp.setActiveAnnoForm(this);
		}

		public void saveNoCheck()
		{
			if (this.modified) 
			{
				string path = this.imageData.AnnotImgPath;
				this.diagnosticPanel.saveAnnoImage(path);
				this.imageData.Summary = this.diagnosticPanel.LatestSummary;
				
				ArrayList marks = new ArrayList();
				foreach (MarkData.Mark mark in this.diagnosticPanel.Marks) 
				{
					marks.Add(					
						new ImageMarkData(
						-1, 
						mark.MarkID, 
						mark.Location.X, 
						mark.Location.Y, 
						mark.MarkWidth, 
						mark.MarkHeight, 
						mark.MarkComment, 
						mark.CurrentValueIndex, 
						mark.Ratio));
				}

				//construct DBhandler's MarkData array
				GODS.GODSMain.dbManager.saveAnnotatedImage(this.imageData, marks);
				if (GODS.GODSMain.showSaveConfirmDlg)
				{
					(new ConfirmSaveDlg()).ShowDialog(this);
				}
				this.modified = false;
			}
			this.clearAllMarks();
			this.ReloadMarks();
		}

		/// <summary>
		/// Save the ImageData to the database.
		/// </summary>
		public void saveRecord()
		{
			if (this.checkIsFinal())
				return;
			this.saveNoCheck();			
		}

		/// <summary>
		/// Refresh the summary area once a mark is modified.
		/// </summary>
		public void refreshSummary()
		{
			diagnosticPanel.updateSummary();
			highlightSummary(diagnosticPanel.GetCurrentMarkIndex());
		}

		/// <summary>
		/// Highlight the current mark.
		/// </summary>
		/// <param name="markIndex"></param>
		public void highlightSummary(int markIndex) 
		{
			if (markIndex > -1 && markIndex < this.summaryLstBox.Items.Count)
				this.summaryLstBox.SelectedIndex = markIndex;
		}

		/// <summary>
		/// Change the mark set ID for the annotated image. Will not allow change
		/// if there are already marks in the diagnostic panel.
		/// </summary>
		/// <param name="markSetID"></param>
		public void setAnnoMarkset(int markSetID)
		{
			if ((this.imageData.MarkSetID != markSetID) &&
				(this.diagnosticPanel.Marks.Count == 0))
			{
				this.imageData.MarkSetID = markSetID;
			}
		}

		public void clearAllMarks()
		{
			this.diagnosticPanel.CurrentMark = null;
			this.diagnosticPanel.clearAllMarks();
		}

		public void ReloadMarks()
		{
			this.mainApp.markForm.loadMarksetWithID(imageData.MarkSetID);
			ArrayList allmarks = GODSMain.currMarkSet.AllMarks;
			//loading the marks
			ImageMarkData[] iMarkDataArr = GODS.GODSMain.dbManager.getImageMarks(imageData.ImageId);
			if (iMarkDataArr != null && iMarkDataArr.Length != 0)
			{
				Mark mark = null;
				foreach (ImageMarkData iMarkData in iMarkDataArr)
				{
					Mark markInSet = null;
					for (int i=0;i<allmarks.Count;i++)
					{
						if(((Mark) allmarks[i]).MarkID==iMarkData.MarkId)
						{
							markInSet = (Mark)allmarks[i];
							break;
						}
					}
					if (markInSet != null)
					{
						mark = (Mark)markInSet.Clone();
						mark.Location= new Point(iMarkData.Position_X,iMarkData.Position_Y); 
						mark.MarkWidth=iMarkData.EstimateWidth; 
						mark.MarkHeight=iMarkData.EstimateHeight; 
						mark.MarkComment=iMarkData.Comment; 
						mark.CurrentValueIndex=iMarkData.ValueIndex; 
						mark.Ratio=(int)iMarkData.Ratio;
					}
					this.addMarkFromDB(mark);
				}
			}
			this.diagnosticPanel.adjustAllSize();
		}

		/// <summary>
		/// fill in values for two dropdown box
		/// </summary>
		protected void fillMarkDimension() 
		{
			double i = 0.5;
			while (i < 3.1) 
			{
				widthDropBox.Items.Add(i);
				heightDropBox.Items.Add(i);
				i += 0.1;
			}
			i = 4.0;
			while (i < 11.0)
			{
				widthDropBox.Items.Add(i);
				heightDropBox.Items.Add(i);
				i++;
			}
			
			this.widthDropBox.SelectedIndex = 5;
			this.heightDropBox.SelectedIndex = 5;
		}

		/// <summary>
		/// Build the summary for list, loop through the mark on panel
		/// and two discharged fields
		/// </summary>
		public void BuildSummary(ArrayList summaries)
		{
			this.summaryLstBox.Items.Clear();
			this.summaryLstBox.Items.AddRange((summaries.ToArray()));
		}

		public void loadMarksFromDB(Point position, Mark mark)
		{
			this.diagnosticPanel.AddCurrentMark(position, mark);
		}

		public void addMarkFromDB(Mark mark)
		{
			this.diagnosticPanel.addMarkFromDB(mark);
		}

		// listener for the zoom out button
		private void btnZoomOut_Click(object sender, System.EventArgs e)
		{
			Mark curMark = this.diagnosticPanel.CurrentMark;
			if (!this.checkIsFinal() && curMark != null) 
			{
				curMark.ZoomOut();
				this.IsModified = true;
			}
		}

		// listener for the zoom in button
		private void btnZoomIn_Click(object sender, System.EventArgs e)
		{
			Mark curMark = this.diagnosticPanel.CurrentMark;
			if (!this.checkIsFinal() && curMark != null)
			{ 
				curMark.ZoomIn();
				this.IsModified = true;
			}
		}

		// initialization work for the diagnostic panel
		private void setupDiagnosticPanel(DiagnosticImage dImage)
		{
			this.diagnosticPanel = new GODS.DiagnosticPanel(this, dImage);
			this.diagnosticPanel.Location = new System.Drawing.Point(0, 0);
			this.diagnosticPanel.Size = new Size(this.imgPanel.Width, this.imgPanel.Height);
			this.imgPanel.Controls.Add(this.diagnosticPanel);
		}

		// listener for the original button to bring up the original image
		private void btnOriginal_Click(object sender, System.EventArgs e)
		{
			Image image = Image.FromFile(this.imageData.OrigImgPath);
			Form form = new Form();
			PictureBox picBox = new PictureBox();
			picBox.Image = image;
			form.Size = new Size(image.Width+10, image.Height+10);
			picBox.Size = image.Size;
			form.Text = "Original image for patient " + 
				this.imageData.Mrn + " on " + this.imageData.AppDate;
			form.Controls.Add(picBox);
			form.StartPosition = FormStartPosition.CenterScreen;
			form.Show();
		}

		// house-keeping work before the form is closed
		private void AnnoForm_Closing(object sender, CancelEventArgs e)
		{
			if(this.imageData!=null && imageData.Final) return;

			e.Cancel = false;
			if (modified)
			{
				DialogResult result = MessageBox.Show(
					"Modifications has not been saved for patient " +
					this.Text + ", do you want to save it before exit?",
					"Warning", MessageBoxButtons.YesNoCancel,
					MessageBoxIcon.Warning,MessageBoxDefaultButton.Button3);
				if (result == DialogResult.Yes)
				{
					this.saveRecord();
				}
				else if (result == DialogResult.Cancel)
				{
					e.Cancel = true;
				}
			}
		}

		// listener for the delete button
		private void btnDeleteMark_Click(object sender, System.EventArgs e)
		{
			this.diagnosticPanel.deleteCurrentMark();
		}

		// listener for the summary box
		private void summaryLstBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.diagnosticPanel.updateCurrentMark(this.summaryLstBox.SelectedIndex);
		}

		// listener for the save button
		private void btnSave_Click(object sender, System.EventArgs e)
		{
			this.saveRecord();
		}

		// house-keeping work when this form is activated
		private void AnnoForm_Activated(object sender, System.EventArgs e)
		{
			GODS.GODSMain.ActiveAnnoForm = (AnnoForm) this;
			this.mainApp.updateMarkForm(this.imageData.MarkSetID, this.diagnosticPanel.Marks);
		}

		// listener for the width drop-down list
		private void widthDropBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.diagnosticPanel.changeMarkWidth(double.Parse(this.widthDropBox.Text));
		}

		// listener for the height drop-down list
		private void heightDropBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.diagnosticPanel.changeMarkHeight(double.Parse(this.heightDropBox.Text));
		}

		// listener for the comment button
		private void btnComment_Click(object sender, System.EventArgs e)
		{
			if (this.checkIsFinal())
				return;

			if (this.CurrentMark == null) 
			{
				MessageBox.Show("No mark selected!");
				return;
			}

			string comment = getUserInput("Comment on the mark", this.CurrentMark.MarkComment);
		
			if (comment != null)
			{
				this.CurrentMark.MarkComment = comment;
			}
			this.refreshSummary();
		}

		private string getUserInput(string title, string origValue)
		{
			GODS.MarkConfig.InputDialog inputDialog = new GODS.MarkConfig.InputDialog();
			inputDialog.ShowDialog(title, origValue);
			return inputDialog.Input;
		}

		// called when browsing/searching ends up with opening the record
		internal int getImageId()
		{
			if (imageData == null)
				return -1;
			else
				return imageData.ImageId;
		}

		// listener for the finalize button
		private void btnFinalize_Click(object sender, System.EventArgs e)
		{
			
			if (this.imageData!= null && !this.imageData.final && MessageBox.Show(
				this, 
				"After you finalize this record, you won't be able to modify it again."
				+ System.Environment.NewLine + "Are you sure you want to proceed?",
				"Finalizing Record", 
				MessageBoxButtons.OKCancel, 
				MessageBoxIcon.Warning,
				MessageBoxDefaultButton.Button2) == DialogResult.OK)
			{
				this.imageData.Final = true;
				this.saveNoCheck();
				this.doDisable();
				
			}
		}

		/// <summary>
		/// Show error message when the user try to edit a finalized annotation
		/// </summary>
		/// <returns></returns>
		public bool checkIsFinal()
		{
			if (this.IsFinalized) 
			{
				MessageBox.Show(this, "This image is finalized and cannot be modified!", "Warning", 
					MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return true;
			}
			return false;
		}
	}
}
