123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- //#########################################################################################
- //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
- //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
- //#########################################################################################
- /*
- * Copyright ?2005, Mathew Hall
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- * OF SUCH DAMAGE.
- */
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Windows.Forms.Design;
- using XPTable.Events;
- using XPTable.Models;
- using XPTable.Win32;
- namespace XPTable.Editors
- {
- /// <summary>
- /// Base class for Cell editors
- /// </summary>
- public abstract class CellEditor : ICellEditor, IMouseMessageFilterClient, IKeyMessageFilterClient
- {
- #region Event Handlers
- /// <summary>
- /// Occurs when the CellEditor begins editing a Cell
- /// </summary>
- public event CellEditEventHandler BeginEdit;
- /// <summary>
- /// Occurs when the CellEditor stops editing a Cell
- /// </summary>
- public event CellEditEventHandler EndEdit;
- /// <summary>
- /// Occurs when the editing of a Cell is cancelled
- /// </summary>
- public event CellEditEventHandler CancelEdit;
- #endregion
- #region Class Data
- /// <summary>
- /// The Control that is performing the editing
- /// </summary>
- private Control control;
- /// <summary>
- /// The Cell that is being edited
- /// </summary>
- internal Cell cell;
- /// <summary>
- /// The Table that contains the Cell being edited
- /// </summary>
- private Table table;
- /// <summary>
- /// A CellPos that represents the position of the Cell being edited
- /// </summary>
- private CellPos cellPos;
- /// <summary>
- /// The Rectangle that represents the Cells location and size
- /// </summary>
- private Rectangle cellRect;
- /// <summary>
- /// A MouseMessageFilter that receives mouse messages before they
- /// are dispatched to their destination
- /// </summary>
- private MouseMessageFilter mouseMessageFilter;
- /// <summary>
- /// A KeyMessageFilter that receives key messages before they
- /// are dispatched to their destination
- /// </summary>
- private KeyMessageFilter keyMessageFilter;
- #endregion
-
-
- #region Constructor
- /// <summary>
- /// Initializes a new instance of the CellEditor class with default settings
- /// </summary>
- protected CellEditor()
- {
- this.control = null;
- this.cell = null;
- this.table = null;
- this.cellPos = CellPos.Empty;
- this.cellRect = Rectangle.Empty;
- this.mouseMessageFilter = new MouseMessageFilter(this);
- this.keyMessageFilter = new KeyMessageFilter(this);
- }
- #endregion
- #region Methods
- /// <summary>
- /// Prepares the CellEditor to edit the specified Cell
- /// </summary>
- /// <param name="cell">The Cell to be edited</param>
- /// <param name="table">The Table that contains the Cell</param>
- /// <param name="cellPos">A CellPos representing the position of the Cell</param>
- /// <param name="cellRect">The Rectangle that represents the Cells location and size</param>
- /// <param name="userSetEditorValues">Specifies whether the ICellEditors
- /// starting value has already been set by the user</param>
- /// <returns>true if the ICellEditor can continue editing the Cell, false otherwise</returns>
- public virtual bool PrepareForEditing(Cell cell, Table table, CellPos cellPos, Rectangle cellRect, bool userSetEditorValues)
- {
- //
- this.cell = cell;
- this.table = table;
- this.cellPos = cellPos;
- this.cellRect = cellRect;
- // check if the user has already set the editors value for us
- if (!userSetEditorValues)
- {
- this.SetEditValue();
- }
- this.SetEditLocation(cellRect);
- // raise the BeginEdit event
- CellEditEventArgs e = new CellEditEventArgs(cell, this, table, cellPos.Row, cellPos.Column, cellRect);
- e.Handled = userSetEditorValues;
- this.OnBeginEdit(e);
-
- // if the edit has been canceled, remove the editor and return false
- if (e.Cancel)
- {
- this.RemoveEditControl();
- return false;
- }
- return true;
- }
- /// <summary>
- /// Sets the location and size of the CellEditor
- /// </summary>
- /// <param name="cellRect">A Rectangle that represents the size and location
- /// of the Cell being edited</param>
- protected abstract void SetEditLocation(Rectangle cellRect);
- /// <summary>
- /// Sets the initial value of the editor based on the contents of
- /// the Cell being edited
- /// </summary>
- protected abstract void SetEditValue();
- /// <summary>
- /// Sets the contents of the Cell being edited based on the value
- /// in the editor
- /// </summary>
- protected abstract void SetCellValue();
- /// <summary>
- /// Displays the editor to the user and adds it to the Table's Control
- /// collection
- /// </summary>
- protected virtual void ShowEditControl()
- {
- this.control.Parent = this.table;
- this.control.Visible = true;
- }
- /// <summary>
- /// Conceals the editor from the user, but does not remove it from the
- /// Table's Control collection
- /// </summary>
- protected virtual void HideEditControl()
- {
- this.control.Visible = false;
- }
- /// <summary>
- /// Conceals the editor from the user and removes it from the Table's
- /// Control collection
- /// </summary>
- protected virtual void RemoveEditControl()
- {
- this.control.Visible = false;
- this.control.Parent = null;
- this.table.Focus();
- this.cell = null;
- this.table = null;
- this.cellPos = CellPos.Empty;
- this.cellRect = Rectangle.Empty;
- }
- /// <summary>
- /// Starts editing the Cell
- /// </summary>
- public virtual void StartEditing()
- {
- this.ShowEditControl();
- Application.AddMessageFilter(this.keyMessageFilter);
- Application.AddMessageFilter(this.mouseMessageFilter);
- }
- /// <summary>
- /// Stops editing the Cell and commits any changes
- /// </summary>
- public virtual void StopEditing()
- {
- Application.RemoveMessageFilter(this.keyMessageFilter);
- Application.RemoveMessageFilter(this.mouseMessageFilter);
-
- //
- CellEditEventArgs e = new CellEditEventArgs(this.cell, this, this.table, this.cellPos.Row, this.cellPos.Column, this.cellRect);
- this.table.OnEditingStopped(e);
- this.OnEndEdit(e);
-
- if (!e.Cancel && !e.Handled)
- {
- this.SetCellValue();
- }
- this.RemoveEditControl();
- }
- /// <summary>
- /// Stops editing the Cell and ignores any changes
- /// </summary>
- public virtual void CancelEditing()
- {
- Application.RemoveMessageFilter(this.keyMessageFilter);
- Application.RemoveMessageFilter(this.mouseMessageFilter);
-
- //
- CellEditEventArgs e = new CellEditEventArgs(this.cell, this, this.table, this.cellPos.Row, this.cellPos.Column, this.cellRect);
- this.table.OnEditingCancelled(e);
- this.OnCancelEdit(e);
-
- this.RemoveEditControl();
- }
- /// <summary>
- /// Filters out a mouse message before it is dispatched
- /// </summary>
- /// <param name="target">The Control that will receive the message</param>
- /// <param name="msg">A WindowMessage that represents the message to process</param>
- /// <param name="wParam">Specifies the WParam field of the message</param>
- /// <param name="lParam">Specifies the LParam field of the message</param>
- /// <returns>true to filter the message and prevent it from being dispatched;
- /// false to allow the message to continue to the next filter or control</returns>
- public virtual bool ProcessMouseMessage(Control target, WindowMessage msg, int wParam, int lParam)
- {
- if (msg == WindowMessage.WM_LBUTTONDOWN || msg == WindowMessage.WM_RBUTTONDOWN ||
- msg == WindowMessage.WM_MBUTTONDOWN || msg == WindowMessage.WM_XBUTTONDOWN ||
- msg == WindowMessage.WM_NCLBUTTONDOWN || msg == WindowMessage.WM_NCRBUTTONDOWN ||
- msg == WindowMessage.WM_NCMBUTTONDOWN || msg == WindowMessage.WM_NCXBUTTONDOWN)
- {
- Point cursorPos = Cursor.Position;
-
- if (target != this.EditingTable && target != this.Control)
- {
- this.EditingTable.StopEditing();
- }
- }
-
- return false;
- }
- /// <summary>
- /// Filters out a key message before it is dispatched
- /// </summary>
- /// <param name="target">The Control that will receive the message</param>
- /// <param name="msg">A WindowMessage that represents the message to process</param>
- /// <param name="wParam">Specifies the WParam field of the message</param>
- /// <param name="lParam">Specifies the LParam field of the message</param>
- /// <returns>true to filter the message and prevent it from being dispatched;
- /// false to allow the message to continue to the next filter or control</returns>
- public virtual bool ProcessKeyMessage(Control target, WindowMessage msg, int wParam, int lParam)
- {
- return false;
- }
- #endregion
- #region Properties
- /// <summary>
- /// Gets or sets the Control that is being used to edit the Cell
- /// </summary>
- protected Control Control
- {
- get
- {
- return this.control;
- }
- set
- {
- this.control = value;
- }
- }
- /// <summary>
- /// Gets the Cell that is being edited
- /// </summary>
- public Cell EditingCell
- {
- get
- {
- return this.cell;
- }
- }
- /// <summary>
- /// Gets the Table that contains the Cell being edited
- /// </summary>
- public Table EditingTable
- {
- get
- {
- return this.table;
- }
- }
- /// <summary>
- /// Gets a CellPos that represents the position of the Cell being edited
- /// </summary>
- public CellPos EditingCellPos
- {
- get
- {
- return this.cellPos;
- }
- }
- /// <summary>
- /// Gets whether the CellEditor is currently editing a Cell
- /// </summary>
- public bool IsEditing
- {
- get
- {
- return this.cell != null;
- }
- }
- #endregion
- #region Events
- /// <summary>
- /// Raises the BeginEdit event
- /// </summary>
- /// <param name="e">A CellEditEventArgs that contains the event data</param>
- protected virtual void OnBeginEdit(CellEditEventArgs e)
- {
- if (this.BeginEdit != null)
- {
- this.BeginEdit(this, e);
- }
- }
- /// <summary>
- /// Raises the EndEdit event
- /// </summary>
- /// <param name="e">A CellEditEventArgs that contains the event data</param>
- protected virtual void OnEndEdit(CellEditEventArgs e)
- {
- if (this.EndEdit != null)
- {
- this.EndEdit(this, e);
- }
- }
- /// <summary>
- /// Raises the CancelEdit event
- /// </summary>
- /// <param name="e">A CellEditEventArgs that contains the event data</param>
- protected virtual void OnCancelEdit(CellEditEventArgs e)
- {
- if (this.CancelEdit != null)
- {
- this.CancelEdit(this, e);
- }
- }
- #endregion
- }
- }
|