DropDownCellRenderer.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 XPTable.Editors;
  34. using XPTable.Events;
  35. using XPTable.Models;
  36. using XPTable.Themes;
  37. namespace XPTable.Renderers
  38. {
  39. /// <summary>
  40. /// Base class for CellRenderers that Cell contents like ComboBoxes
  41. /// </summary>
  42. public abstract class DropDownCellRenderer : CellRenderer
  43. {
  44. #region Class Data
  45. /// <summary>
  46. /// The width of the DropDownCellRenderer's dropdown button
  47. /// </summary>
  48. private int buttonWidth;
  49. /// <summary>
  50. /// Specifies whether the DropDownCellRenderer dropdown button should be drawn
  51. /// </summary>
  52. private bool showButton;
  53. #endregion
  54. #region Constructor
  55. /// <summary>
  56. /// Initializes a new instance of the DropDownCellRenderer class with
  57. /// default settings
  58. /// </summary>
  59. protected DropDownCellRenderer() : base()
  60. {
  61. this.buttonWidth = 15;
  62. this.showButton = true;
  63. }
  64. #endregion
  65. #region Methods
  66. /// <summary>
  67. /// Gets the Rectangle that specifies the Size and Location of
  68. /// the current Cell's dropdown button
  69. /// </summary>
  70. /// <returns>A Rectangle that specifies the Size and Location of
  71. /// the current Cell's dropdown button</returns>
  72. protected internal Rectangle CalcDropDownButtonBounds()
  73. {
  74. Rectangle buttonRect = this.ClientRectangle;
  75. buttonRect.Width = this.ButtonWidth;
  76. buttonRect.X = this.ClientRectangle.Right - buttonRect.Width;
  77. if (buttonRect.Width > this.ClientRectangle.Width)
  78. {
  79. buttonRect = this.ClientRectangle;
  80. }
  81. return buttonRect;
  82. }
  83. /// <summary>
  84. /// Gets the DropDownRendererData specific data used by the Renderer from
  85. /// the specified Cell
  86. /// </summary>
  87. /// <param name="cell">The Cell to get the DropDownRendererData data for</param>
  88. /// <returns>The DropDownRendererData data for the specified Cell</returns>
  89. protected DropDownRendererData GetDropDownRendererData(Cell cell)
  90. {
  91. object rendererData = this.GetRendererData(cell);
  92. if (rendererData == null || !(rendererData is DropDownRendererData))
  93. {
  94. rendererData = new DropDownRendererData();
  95. this.SetRendererData(cell, rendererData);
  96. }
  97. return (DropDownRendererData) rendererData;
  98. }
  99. #endregion
  100. #region Properties
  101. /// <summary>
  102. /// Gets or sets the width of the dropdown button
  103. /// </summary>
  104. protected internal int ButtonWidth
  105. {
  106. get
  107. {
  108. return this.buttonWidth;
  109. }
  110. set
  111. {
  112. this.buttonWidth = value;
  113. }
  114. }
  115. /// <summary>
  116. /// Gets or sets whether the DropDownCellRenderer dropdown button should be drawn
  117. /// </summary>
  118. protected bool ShowDropDownButton
  119. {
  120. get
  121. {
  122. return this.showButton;
  123. }
  124. set
  125. {
  126. this.showButton = value;
  127. }
  128. }
  129. #endregion
  130. #region Events
  131. #region Mouse
  132. #region MouseLeave
  133. /// <summary>
  134. /// Raises the MouseLeave event
  135. /// </summary>
  136. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  137. public override void OnMouseLeave(CellMouseEventArgs e)
  138. {
  139. base.OnMouseLeave(e);
  140. if (this.ShowDropDownButton || (e.Table.IsEditing && e.CellPos == e.Table.EditingCell))
  141. {
  142. if (e.Table.IsCellEditable(e.CellPos))
  143. {
  144. // get the button renderer data
  145. DropDownRendererData rendererData = this.GetDropDownRendererData(e.Cell);
  146. if (rendererData.ButtonState != ComboBoxStates.Normal)
  147. {
  148. rendererData.ButtonState = ComboBoxStates.Normal;
  149. e.Table.Invalidate(e.CellRect);
  150. }
  151. }
  152. }
  153. }
  154. #endregion
  155. #region MouseUp
  156. /// <summary>
  157. /// Raises the MouseUp event
  158. /// </summary>
  159. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  160. public override void OnMouseUp(CellMouseEventArgs e)
  161. {
  162. base.OnMouseUp(e);
  163. if (this.ShowDropDownButton || (e.Table.IsEditing && e.CellPos == e.Table.EditingCell))
  164. {
  165. if (e.Table.IsCellEditable(e.CellPos))
  166. {
  167. // get the renderer data
  168. DropDownRendererData rendererData = this.GetDropDownRendererData(e.Cell);
  169. if (this.CalcDropDownButtonBounds().Contains(e.X, e.Y))
  170. {
  171. rendererData.ButtonState = ComboBoxStates.Hot;
  172. e.Table.Invalidate(e.CellRect);
  173. }
  174. }
  175. }
  176. }
  177. #endregion
  178. #region MouseDown
  179. /// <summary>
  180. /// Raises the MouseDown event
  181. /// </summary>
  182. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  183. public override void OnMouseDown(CellMouseEventArgs e)
  184. {
  185. base.OnMouseDown(e);
  186. if (this.ShowDropDownButton || (e.Table.IsEditing && e.CellPos == e.Table.EditingCell))
  187. {
  188. if (e.Table.IsCellEditable(e.CellPos))
  189. {
  190. // get the button renderer data
  191. DropDownRendererData rendererData = this.GetDropDownRendererData(e.Cell);
  192. if (this.CalcDropDownButtonBounds().Contains(e.X, e.Y))
  193. {
  194. if (!(e.Table.ColumnModel.GetCellEditor(e.CellPos.Column) is DropDownCellEditor))
  195. {
  196. throw new InvalidOperationException("Cannot edit Cell as DropDownCellRenderer requires a DropDownColumn that uses a DropDownCellEditor");
  197. }
  198. rendererData.ButtonState = ComboBoxStates.Pressed;
  199. if (!e.Table.IsEditing)
  200. {
  201. e.Table.EditCell(e.CellPos);
  202. }
  203. ((IEditorUsesRendererButtons) e.Table.EditingCellEditor).OnEditorButtonMouseDown(this, e);
  204. e.Table.Invalidate(e.CellRect);
  205. }
  206. }
  207. }
  208. }
  209. #endregion
  210. #region MouseMove
  211. /// <summary>
  212. /// Raises the MouseMove event
  213. /// </summary>
  214. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  215. public override void OnMouseMove(XPTable.Events.CellMouseEventArgs e)
  216. {
  217. base.OnMouseMove(e);
  218. if (this.ShowDropDownButton || (e.Table.IsEditing && e.CellPos == e.Table.EditingCell))
  219. {
  220. if (e.Table.IsCellEditable(e.CellPos))
  221. {
  222. // get the button renderer data
  223. DropDownRendererData rendererData = this.GetDropDownRendererData(e.Cell);
  224. if (this.CalcDropDownButtonBounds().Contains(e.X, e.Y))
  225. {
  226. if (rendererData.ButtonState == ComboBoxStates.Normal)
  227. {
  228. if (e.Button == MouseButtons.Left && e.Row == e.Table.LastMouseDownCell.Row && e.Column == e.Table.LastMouseDownCell.Column)
  229. {
  230. rendererData.ButtonState = ComboBoxStates.Pressed;
  231. }
  232. else
  233. {
  234. rendererData.ButtonState = ComboBoxStates.Hot;
  235. }
  236. e.Table.Invalidate(e.CellRect);
  237. }
  238. }
  239. else
  240. {
  241. if (rendererData.ButtonState != ComboBoxStates.Normal)
  242. {
  243. rendererData.ButtonState = ComboBoxStates.Normal;
  244. e.Table.Invalidate(e.CellRect);
  245. }
  246. }
  247. }
  248. }
  249. }
  250. #endregion
  251. #endregion
  252. #region Paint
  253. /// <summary>
  254. /// Raises the PaintCell event
  255. /// </summary>
  256. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  257. public override void OnPaintCell(PaintCellEventArgs e)
  258. {
  259. if (e.Table.ColumnModel.Columns[e.Column] is DropDownColumn)
  260. {
  261. this.showButton = ((DropDownColumn) e.Table.ColumnModel.Columns[e.Column]).ShowDropDownButton;
  262. }
  263. else
  264. {
  265. this.showButton = true;
  266. }
  267. base.OnPaintCell(e);
  268. }
  269. /// <summary>
  270. /// Paints the Cells background
  271. /// </summary>
  272. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  273. protected override void OnPaintBackground(PaintCellEventArgs e)
  274. {
  275. base.OnPaintBackground(e);
  276. // don't bother going any further if the Cell is null
  277. if (e.Cell == null)
  278. {
  279. return;
  280. }
  281. if (this.ShowDropDownButton || (e.Table.IsEditing && e.CellPos == e.Table.EditingCell))
  282. {
  283. ComboBoxStates state = this.GetDropDownRendererData(e.Cell).ButtonState;
  284. if (!e.Enabled)
  285. {
  286. state = ComboBoxStates.Disabled;
  287. }
  288. ThemeManager.DrawComboBoxButton(e.Graphics, this.CalcDropDownButtonBounds(), state);
  289. }
  290. }
  291. #endregion
  292. #endregion
  293. }
  294. }