CellEditor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.Drawing;
  32. using System.Windows.Forms;
  33. using System.Windows.Forms.Design;
  34. using XPTable.Events;
  35. using XPTable.Models;
  36. using XPTable.Win32;
  37. namespace XPTable.Editors
  38. {
  39. /// <summary>
  40. /// Base class for Cell editors
  41. /// </summary>
  42. public abstract class CellEditor : ICellEditor, IMouseMessageFilterClient, IKeyMessageFilterClient
  43. {
  44. #region Event Handlers
  45. /// <summary>
  46. /// Occurs when the CellEditor begins editing a Cell
  47. /// </summary>
  48. public event CellEditEventHandler BeginEdit;
  49. /// <summary>
  50. /// Occurs when the CellEditor stops editing a Cell
  51. /// </summary>
  52. public event CellEditEventHandler EndEdit;
  53. /// <summary>
  54. /// Occurs when the editing of a Cell is cancelled
  55. /// </summary>
  56. public event CellEditEventHandler CancelEdit;
  57. #endregion
  58. #region Class Data
  59. /// <summary>
  60. /// The Control that is performing the editing
  61. /// </summary>
  62. private Control control;
  63. /// <summary>
  64. /// The Cell that is being edited
  65. /// </summary>
  66. internal Cell cell;
  67. /// <summary>
  68. /// The Table that contains the Cell being edited
  69. /// </summary>
  70. private Table table;
  71. /// <summary>
  72. /// A CellPos that represents the position of the Cell being edited
  73. /// </summary>
  74. private CellPos cellPos;
  75. /// <summary>
  76. /// The Rectangle that represents the Cells location and size
  77. /// </summary>
  78. private Rectangle cellRect;
  79. /// <summary>
  80. /// A MouseMessageFilter that receives mouse messages before they
  81. /// are dispatched to their destination
  82. /// </summary>
  83. private MouseMessageFilter mouseMessageFilter;
  84. /// <summary>
  85. /// A KeyMessageFilter that receives key messages before they
  86. /// are dispatched to their destination
  87. /// </summary>
  88. private KeyMessageFilter keyMessageFilter;
  89. #endregion
  90. #region Constructor
  91. /// <summary>
  92. /// Initializes a new instance of the CellEditor class with default settings
  93. /// </summary>
  94. protected CellEditor()
  95. {
  96. this.control = null;
  97. this.cell = null;
  98. this.table = null;
  99. this.cellPos = CellPos.Empty;
  100. this.cellRect = Rectangle.Empty;
  101. this.mouseMessageFilter = new MouseMessageFilter(this);
  102. this.keyMessageFilter = new KeyMessageFilter(this);
  103. }
  104. #endregion
  105. #region Methods
  106. /// <summary>
  107. /// Prepares the CellEditor to edit the specified Cell
  108. /// </summary>
  109. /// <param name="cell">The Cell to be edited</param>
  110. /// <param name="table">The Table that contains the Cell</param>
  111. /// <param name="cellPos">A CellPos representing the position of the Cell</param>
  112. /// <param name="cellRect">The Rectangle that represents the Cells location and size</param>
  113. /// <param name="userSetEditorValues">Specifies whether the ICellEditors
  114. /// starting value has already been set by the user</param>
  115. /// <returns>true if the ICellEditor can continue editing the Cell, false otherwise</returns>
  116. public virtual bool PrepareForEditing(Cell cell, Table table, CellPos cellPos, Rectangle cellRect, bool userSetEditorValues)
  117. {
  118. //
  119. this.cell = cell;
  120. this.table = table;
  121. this.cellPos = cellPos;
  122. this.cellRect = cellRect;
  123. // check if the user has already set the editors value for us
  124. if (!userSetEditorValues)
  125. {
  126. this.SetEditValue();
  127. }
  128. this.SetEditLocation(cellRect);
  129. // raise the BeginEdit event
  130. CellEditEventArgs e = new CellEditEventArgs(cell, this, table, cellPos.Row, cellPos.Column, cellRect);
  131. e.Handled = userSetEditorValues;
  132. this.OnBeginEdit(e);
  133. // if the edit has been canceled, remove the editor and return false
  134. if (e.Cancel)
  135. {
  136. this.RemoveEditControl();
  137. return false;
  138. }
  139. return true;
  140. }
  141. /// <summary>
  142. /// Sets the location and size of the CellEditor
  143. /// </summary>
  144. /// <param name="cellRect">A Rectangle that represents the size and location
  145. /// of the Cell being edited</param>
  146. protected abstract void SetEditLocation(Rectangle cellRect);
  147. /// <summary>
  148. /// Sets the initial value of the editor based on the contents of
  149. /// the Cell being edited
  150. /// </summary>
  151. protected abstract void SetEditValue();
  152. /// <summary>
  153. /// Sets the contents of the Cell being edited based on the value
  154. /// in the editor
  155. /// </summary>
  156. protected abstract void SetCellValue();
  157. /// <summary>
  158. /// Displays the editor to the user and adds it to the Table's Control
  159. /// collection
  160. /// </summary>
  161. protected virtual void ShowEditControl()
  162. {
  163. this.control.Parent = this.table;
  164. this.control.Visible = true;
  165. }
  166. /// <summary>
  167. /// Conceals the editor from the user, but does not remove it from the
  168. /// Table's Control collection
  169. /// </summary>
  170. protected virtual void HideEditControl()
  171. {
  172. this.control.Visible = false;
  173. }
  174. /// <summary>
  175. /// Conceals the editor from the user and removes it from the Table's
  176. /// Control collection
  177. /// </summary>
  178. protected virtual void RemoveEditControl()
  179. {
  180. this.control.Visible = false;
  181. this.control.Parent = null;
  182. this.table.Focus();
  183. this.cell = null;
  184. this.table = null;
  185. this.cellPos = CellPos.Empty;
  186. this.cellRect = Rectangle.Empty;
  187. }
  188. /// <summary>
  189. /// Starts editing the Cell
  190. /// </summary>
  191. public virtual void StartEditing()
  192. {
  193. this.ShowEditControl();
  194. Application.AddMessageFilter(this.keyMessageFilter);
  195. Application.AddMessageFilter(this.mouseMessageFilter);
  196. }
  197. /// <summary>
  198. /// Stops editing the Cell and commits any changes
  199. /// </summary>
  200. public virtual void StopEditing()
  201. {
  202. Application.RemoveMessageFilter(this.keyMessageFilter);
  203. Application.RemoveMessageFilter(this.mouseMessageFilter);
  204. //
  205. CellEditEventArgs e = new CellEditEventArgs(this.cell, this, this.table, this.cellPos.Row, this.cellPos.Column, this.cellRect);
  206. this.table.OnEditingStopped(e);
  207. this.OnEndEdit(e);
  208. if (!e.Cancel && !e.Handled)
  209. {
  210. this.SetCellValue();
  211. }
  212. this.RemoveEditControl();
  213. }
  214. /// <summary>
  215. /// Stops editing the Cell and ignores any changes
  216. /// </summary>
  217. public virtual void CancelEditing()
  218. {
  219. Application.RemoveMessageFilter(this.keyMessageFilter);
  220. Application.RemoveMessageFilter(this.mouseMessageFilter);
  221. //
  222. CellEditEventArgs e = new CellEditEventArgs(this.cell, this, this.table, this.cellPos.Row, this.cellPos.Column, this.cellRect);
  223. this.table.OnEditingCancelled(e);
  224. this.OnCancelEdit(e);
  225. this.RemoveEditControl();
  226. }
  227. /// <summary>
  228. /// Filters out a mouse message before it is dispatched
  229. /// </summary>
  230. /// <param name="target">The Control that will receive the message</param>
  231. /// <param name="msg">A WindowMessage that represents the message to process</param>
  232. /// <param name="wParam">Specifies the WParam field of the message</param>
  233. /// <param name="lParam">Specifies the LParam field of the message</param>
  234. /// <returns>true to filter the message and prevent it from being dispatched;
  235. /// false to allow the message to continue to the next filter or control</returns>
  236. public virtual bool ProcessMouseMessage(Control target, WindowMessage msg, int wParam, int lParam)
  237. {
  238. if (msg == WindowMessage.WM_LBUTTONDOWN || msg == WindowMessage.WM_RBUTTONDOWN ||
  239. msg == WindowMessage.WM_MBUTTONDOWN || msg == WindowMessage.WM_XBUTTONDOWN ||
  240. msg == WindowMessage.WM_NCLBUTTONDOWN || msg == WindowMessage.WM_NCRBUTTONDOWN ||
  241. msg == WindowMessage.WM_NCMBUTTONDOWN || msg == WindowMessage.WM_NCXBUTTONDOWN)
  242. {
  243. Point cursorPos = Cursor.Position;
  244. if (target != this.EditingTable && target != this.Control)
  245. {
  246. this.EditingTable.StopEditing();
  247. }
  248. }
  249. return false;
  250. }
  251. /// <summary>
  252. /// Filters out a key message before it is dispatched
  253. /// </summary>
  254. /// <param name="target">The Control that will receive the message</param>
  255. /// <param name="msg">A WindowMessage that represents the message to process</param>
  256. /// <param name="wParam">Specifies the WParam field of the message</param>
  257. /// <param name="lParam">Specifies the LParam field of the message</param>
  258. /// <returns>true to filter the message and prevent it from being dispatched;
  259. /// false to allow the message to continue to the next filter or control</returns>
  260. public virtual bool ProcessKeyMessage(Control target, WindowMessage msg, int wParam, int lParam)
  261. {
  262. return false;
  263. }
  264. #endregion
  265. #region Properties
  266. /// <summary>
  267. /// Gets or sets the Control that is being used to edit the Cell
  268. /// </summary>
  269. protected Control Control
  270. {
  271. get
  272. {
  273. return this.control;
  274. }
  275. set
  276. {
  277. this.control = value;
  278. }
  279. }
  280. /// <summary>
  281. /// Gets the Cell that is being edited
  282. /// </summary>
  283. public Cell EditingCell
  284. {
  285. get
  286. {
  287. return this.cell;
  288. }
  289. }
  290. /// <summary>
  291. /// Gets the Table that contains the Cell being edited
  292. /// </summary>
  293. public Table EditingTable
  294. {
  295. get
  296. {
  297. return this.table;
  298. }
  299. }
  300. /// <summary>
  301. /// Gets a CellPos that represents the position of the Cell being edited
  302. /// </summary>
  303. public CellPos EditingCellPos
  304. {
  305. get
  306. {
  307. return this.cellPos;
  308. }
  309. }
  310. /// <summary>
  311. /// Gets whether the CellEditor is currently editing a Cell
  312. /// </summary>
  313. public bool IsEditing
  314. {
  315. get
  316. {
  317. return this.cell != null;
  318. }
  319. }
  320. #endregion
  321. #region Events
  322. /// <summary>
  323. /// Raises the BeginEdit event
  324. /// </summary>
  325. /// <param name="e">A CellEditEventArgs that contains the event data</param>
  326. protected virtual void OnBeginEdit(CellEditEventArgs e)
  327. {
  328. if (this.BeginEdit != null)
  329. {
  330. this.BeginEdit(this, e);
  331. }
  332. }
  333. /// <summary>
  334. /// Raises the EndEdit event
  335. /// </summary>
  336. /// <param name="e">A CellEditEventArgs that contains the event data</param>
  337. protected virtual void OnEndEdit(CellEditEventArgs e)
  338. {
  339. if (this.EndEdit != null)
  340. {
  341. this.EndEdit(this, e);
  342. }
  343. }
  344. /// <summary>
  345. /// Raises the CancelEdit event
  346. /// </summary>
  347. /// <param name="e">A CellEditEventArgs that contains the event data</param>
  348. protected virtual void OnCancelEdit(CellEditEventArgs e)
  349. {
  350. if (this.CancelEdit != null)
  351. {
  352. this.CancelEdit(this, e);
  353. }
  354. }
  355. #endregion
  356. }
  357. }