/*============================================================================= Project: SharpImage Module: siFormTool.cs Language: C# Author: Dan Mueller Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ Revision: $Revision: 2 $ Copyright (c) Queensland University of Technology (QUT) 2007. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using SharpImage.Main; using SharpImage.Rendering; using SharpImage.Properties; namespace SharpImage.Forms { #region siFormTool Class //========================================================================= public partial class siFormTool : Form { #region Enumerations //===================================================================== public enum ContinueTypeEnum { Next, OK } //===================================================================== #endregion #region Instance Variables //===================================================================== //===================================================================== #endregion #region Form Methods //===================================================================== /// /// Default constructor. The ContinueType defaults to 'OK'. /// public siFormTool() { InitializeComponent(); this.m_Renderer = null; this.ContinueType = ContinueTypeEnum.OK; this.StopButtonsFromTakingFocus(); } /// /// Public constructor. /// public siFormTool(ContinueTypeEnum continueType) { InitializeComponent(); this.m_Renderer = null; this.ContinueType = continueType; this.StopButtonsFromTakingFocus(); } /// /// Public constructor. The ContinueType defaults to 'OK'. /// /// The renderer associated with this tool. public siFormTool(siRenderer renderer) { InitializeComponent(); this.m_Renderer = renderer; this.Renderer.Closed += new siRenderer.siRendererHandler(Renderer_Closed); this.ContinueType = ContinueTypeEnum.OK; this.StopButtonsFromTakingFocus(); } /// /// Public constructor. /// /// The renderer associated with this tool. public siFormTool(siRenderer renderer, ContinueTypeEnum continueType) { InitializeComponent(); this.m_Renderer = renderer; this.Renderer.Closed += new siRenderer.siRendererHandler(Renderer_Closed); this.ContinueType = continueType; this.StopButtonsFromTakingFocus(); } /// /// Close this form if the renderer is closed /// /// /// private void Renderer_Closed(siRenderer renderer, EventArgs e) { // Cancel the form this.OnCancel(this, new EventArgs()); this.Close(); } /// /// Removes the handlers we added to watch the Renderer.Closed event. /// private void RemoveRendererClosedHandlers() { if (this.Renderer != null) this.Renderer.Closed -= new siRenderer.siRendererHandler(Renderer_Closed); } private void StopButtonsFromTakingFocus() { this.cmdCancel.GotFocus += new EventHandler(cmd_GotFocus); this.cmdContinue.GotFocus += new EventHandler(cmd_GotFocus); } private void cmd_GotFocus(object sender, EventArgs e) { this.panelBottom.Focus(); } //===================================================================== #endregion #region Properties //===================================================================== #region ParentApplication //===================================================================== /// /// Gets the Parent Application associated with this form. /// Returns null if no parent application exists. /// protected IApplication ParentApplication { get { if (this.ParentForm != null && this.ParentForm is IApplication) { return this.ParentForm as IApplication; } return null; } } //===================================================================== #endregion #region Renderer //===================================================================== private siRenderer m_Renderer; /// /// Gets the RendererBase associated with this form. /// protected siRenderer Renderer { get { return this.m_Renderer; } } //===================================================================== #endregion #region ContinueType //===================================================================== private ContinueTypeEnum m_ContinueType; /// /// Gets/sets the how this form should continue (currently either /// to the 'Next' form or 'OK'). /// The default is 'OK'. /// public ContinueTypeEnum ContinueType { get { return this.m_ContinueType; } set { this.m_ContinueType = value; switch (this.m_ContinueType) { case ContinueTypeEnum.Next: this.cmdContinue.Text = "Next "; this.cmdContinue.Image = Resources.GoLeft; break; case ContinueTypeEnum.OK: this.cmdContinue.Text = "OK"; this.cmdContinue.Image = null; break; } this.cmdContinue.Refresh(); } } //===================================================================== #endregion //===================================================================== #endregion #region Events //===================================================================== private ContinueEventHandler m_EventStorage_Continue; private EventHandler m_EventStorage_Cancel; /// /// The Continue event is raised when the 'Next' or 'OK' button is /// pressed. /// public event ContinueEventHandler Continue { add { this.m_EventStorage_Continue += value; } remove { this.m_EventStorage_Continue -= value; } } /// /// The Cancel event is raised when the 'Cancel' button is pressed. /// public event EventHandler Cancel { add { this.m_EventStorage_Cancel += value; } remove { this.m_EventStorage_Cancel -= value; } } /// /// Raises the Continue event with the given arguments. /// /// /// protected void RaiseContinueEvent(object sender, ContinueEventArgs e) { if (!e.SuppressContinue) { this.RemoveRendererClosedHandlers(); if (this.m_EventStorage_Continue != null) this.m_EventStorage_Continue(sender, e); } } /// /// Raises the Cancel event with the given arguments. /// /// /// protected void RaiseCancelEvent(object sender, EventArgs e) { this.RemoveRendererClosedHandlers(); if (this.m_EventStorage_Cancel != null) this.m_EventStorage_Cancel(sender, e); } //===================================================================== #endregion #region Protected Methods //===================================================================== /// /// This method is invoked when the Continue button ('Next' or 'OK') /// is pressed. If subclasses override this method they must either /// call this base method or made a call to RaiseContinueEvent() /// themselves. /// /// This form /// protected virtual void OnContinue(object sender, ContinueEventArgs e) { if (!e.SuppressContinue) { this.RaiseContinueEvent(sender, e); if (this.ContinueType == ContinueTypeEnum.OK) this.Close(); } } /// /// This method is invoked when the Cancel button is pressed. /// If subclasses override this method they must either call /// this base method or made a call to RaiseContinueEvent() /// themselves. /// /// This form. /// protected virtual void OnCancel(object sender, EventArgs e) { this.RaiseCancelEvent(sender, e); this.Close(); } //===================================================================== #endregion #region Private Methods //===================================================================== private void cmdContinue_Click(object sender, EventArgs e) { try { this.OnContinue(this, new ContinueEventArgs()); } catch (Exception ex) { // TODO: Format this exception for python if (this.ParentApplication != null) this.ParentApplication.ScriptConsole.WriteLine(ex.ToString()); else Trace.WriteLine(ex.ToString()); } } private void cmdCancel_Click(object sender, EventArgs e) { try { this.OnCancel(this, e); this.panelBottom.Focus(); } catch (Exception ex) { // TODO: Format this exception for python this.ParentApplication.ScriptConsole.WriteLine(ex.ToString()); } } //===================================================================== #endregion } //========================================================================= #endregion #region siFormTool EventArgs and Delegates //========================================================================= public class ContinueEventArgs : EventArgs { private bool m_SuppressContinue = false; /// /// Gets/sets if the continue event should be /// public bool SuppressContinue { get { return this.m_SuppressContinue; } set { this.m_SuppressContinue = value; } } } public delegate void ContinueEventHandler(object sender, ContinueEventArgs e); //========================================================================= #endregion }