CellRenderer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 Renderers that draw Cells
  41. /// </summary>
  42. public abstract class CellRenderer : Renderer, ICellRenderer
  43. {
  44. #region Class Data
  45. /// <summary>
  46. /// A string that specifies how a Cells contents are formatted
  47. /// </summary>
  48. private string format;
  49. /// <summary>
  50. /// The Brush used to draw disabled text
  51. /// </summary>
  52. private SolidBrush grayTextBrush;
  53. /// <summary>
  54. /// The amount of padding for the cell being rendered
  55. /// </summary>
  56. private CellPadding padding;
  57. #endregion
  58. #region Constructor
  59. /// <summary>
  60. /// Initializes a new instance of the CellRenderer class with default settings
  61. /// </summary>
  62. protected CellRenderer() : base()
  63. {
  64. this.format = "";
  65. this.grayTextBrush = new SolidBrush(SystemColors.GrayText);
  66. this.padding = CellPadding.Empty;
  67. }
  68. #endregion
  69. #region Methods
  70. /// <summary>
  71. /// Releases the unmanaged resources used by the Renderer and
  72. /// optionally releases the managed resources
  73. /// </summary>
  74. public override void Dispose()
  75. {
  76. base.Dispose();
  77. if (this.grayTextBrush != null)
  78. {
  79. this.grayTextBrush.Dispose();
  80. this.grayTextBrush = null;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the renderer specific data used by the Renderer from
  85. /// the specified Cell
  86. /// </summary>
  87. /// <param name="cell">The Cell to get the renderer data for</param>
  88. /// <returns>The renderer data for the specified Cell</returns>
  89. protected object GetRendererData(Cell cell)
  90. {
  91. return cell.RendererData;
  92. }
  93. /// <summary>
  94. /// Sets the specified renderer specific data used by the Renderer for
  95. /// the specified Cell
  96. /// </summary>
  97. /// <param name="cell">The Cell for which the data is to be stored</param>
  98. /// <param name="value">The renderer specific data to be stored</param>
  99. protected void SetRendererData(Cell cell, object value)
  100. {
  101. cell.RendererData = value;
  102. }
  103. #endregion
  104. #region Properties
  105. /// <summary>
  106. /// Overrides Renderer.ClientRectangle
  107. /// </summary>
  108. public override Rectangle ClientRectangle
  109. {
  110. get
  111. {
  112. Rectangle client = new Rectangle(this.Bounds.Location, this.Bounds.Size);
  113. // take borders into account
  114. client.Width -= Renderer.BorderWidth;
  115. client.Height -= Renderer.BorderWidth;
  116. // take cell padding into account
  117. client.X += this.Padding.Left + 1;
  118. client.Y += this.Padding.Top;
  119. client.Width -= this.Padding.Left + this.Padding.Right + 1;
  120. client.Height -= this.Padding.Top + this.Padding.Bottom;
  121. return client;
  122. }
  123. }
  124. /// <summary>
  125. /// Gets or sets the string that specifies how a Cells contents are formatted
  126. /// </summary>
  127. protected string Format
  128. {
  129. get
  130. {
  131. return this.format;
  132. }
  133. set
  134. {
  135. this.format = value;
  136. }
  137. }
  138. /// <summary>
  139. /// Gets the Brush used to draw disabled text
  140. /// </summary>
  141. protected Brush GrayTextBrush
  142. {
  143. get
  144. {
  145. return this.grayTextBrush;
  146. }
  147. }
  148. /// <summary>
  149. /// Gets or sets the amount of padding around the Cell being rendered
  150. /// </summary>
  151. protected CellPadding Padding
  152. {
  153. get
  154. {
  155. return this.padding;
  156. }
  157. set
  158. {
  159. this.padding = value;
  160. }
  161. }
  162. #endregion
  163. #region Events
  164. #region Focus
  165. /// <summary>
  166. /// Raises the GotFocus event
  167. /// </summary>
  168. /// <param name="e">A CellFocusEventArgs that contains the event data</param>
  169. public virtual void OnGotFocus(CellFocusEventArgs e)
  170. {
  171. this.Bounds = e.CellRect;
  172. if (e.Cell == null)
  173. {
  174. this.Padding = CellPadding.Empty;
  175. }
  176. else
  177. {
  178. this.Padding = e.Cell.Padding;
  179. }
  180. e.Table.Invalidate(e.CellRect);
  181. }
  182. /// <summary>
  183. /// Raises the LostFocus event
  184. /// </summary>
  185. /// <param name="e">A CellFocusEventArgs that contains the event data</param>
  186. public virtual void OnLostFocus(CellFocusEventArgs e)
  187. {
  188. this.Bounds = e.CellRect;
  189. if (e.Cell == null)
  190. {
  191. this.Padding = CellPadding.Empty;
  192. }
  193. else
  194. {
  195. this.Padding = e.Cell.Padding;
  196. }
  197. e.Table.Invalidate(e.CellRect);
  198. }
  199. #endregion
  200. #region Keys
  201. /// <summary>
  202. /// Raises the KeyDown event
  203. /// </summary>
  204. /// <param name="e">A CellKeyEventArgs that contains the event data</param>
  205. public virtual void OnKeyDown(CellKeyEventArgs e)
  206. {
  207. }
  208. /// <summary>
  209. /// Raises the KeyUp event
  210. /// </summary>
  211. /// <param name="e">A CellKeyEventArgs that contains the event data</param>
  212. public virtual void OnKeyUp(CellKeyEventArgs e)
  213. {
  214. }
  215. #endregion
  216. #region Mouse
  217. #region MouseEnter
  218. /// <summary>
  219. /// Raises the MouseEnter event
  220. /// </summary>
  221. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  222. public virtual void OnMouseEnter(CellMouseEventArgs e)
  223. {
  224. this.Bounds = e.CellRect;
  225. if (e.Cell == null)
  226. {
  227. this.Padding = CellPadding.Empty;
  228. }
  229. else
  230. {
  231. this.Padding = e.Cell.Padding;
  232. }
  233. bool tooltipActive = e.Table.ToolTip.Active;
  234. if (tooltipActive)
  235. {
  236. e.Table.ToolTip.Active = false;
  237. }
  238. e.Table.ResetMouseEventArgs();
  239. e.Table.ToolTip.SetToolTip(e.Table, e.Cell.ToolTipText);
  240. if (tooltipActive)
  241. {
  242. e.Table.ToolTip.Active = true;
  243. }
  244. }
  245. #endregion
  246. #region MouseLeave
  247. /// <summary>
  248. /// Raises the MouseLeave event
  249. /// </summary>
  250. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  251. public virtual void OnMouseLeave(CellMouseEventArgs e)
  252. {
  253. this.Bounds = e.CellRect;
  254. if (e.Cell == null)
  255. {
  256. this.Padding = CellPadding.Empty;
  257. }
  258. else
  259. {
  260. this.Padding = e.Cell.Padding;
  261. }
  262. }
  263. #endregion
  264. #region MouseUp
  265. /// <summary>
  266. /// Raises the MouseUp event
  267. /// </summary>
  268. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  269. public virtual void OnMouseUp(CellMouseEventArgs e)
  270. {
  271. this.Bounds = e.CellRect;
  272. if (e.Cell == null)
  273. {
  274. this.Padding = CellPadding.Empty;
  275. }
  276. else
  277. {
  278. this.Padding = e.Cell.Padding;
  279. }
  280. }
  281. #endregion
  282. #region MouseDown
  283. /// <summary>
  284. /// Raises the MouseDown event
  285. /// </summary>
  286. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  287. public virtual void OnMouseDown(CellMouseEventArgs e)
  288. {
  289. if (!e.Table.Focused)
  290. {
  291. if (!(e.Table.IsEditing && e.Table.EditingCell == e.CellPos && e.Table.EditingCellEditor is IEditorUsesRendererButtons))
  292. {
  293. e.Table.Focus();
  294. }
  295. }
  296. this.Bounds = e.CellRect;
  297. if (e.Cell == null)
  298. {
  299. this.Padding = CellPadding.Empty;
  300. }
  301. else
  302. {
  303. this.Padding = e.Cell.Padding;
  304. }
  305. }
  306. #endregion
  307. #region MouseMove
  308. /// <summary>
  309. /// Raises the MouseMove event
  310. /// </summary>
  311. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  312. public virtual void OnMouseMove(CellMouseEventArgs e)
  313. {
  314. this.Bounds = e.CellRect;
  315. if (e.Cell == null)
  316. {
  317. this.Padding = CellPadding.Empty;
  318. }
  319. else
  320. {
  321. this.Padding = e.Cell.Padding;
  322. }
  323. }
  324. #endregion
  325. #region Click
  326. /// <summary>
  327. /// Raises the Click event
  328. /// </summary>
  329. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  330. public virtual void OnClick(CellMouseEventArgs e)
  331. {
  332. this.Bounds = e.CellRect;
  333. if (e.Cell == null)
  334. {
  335. this.Padding = CellPadding.Empty;
  336. }
  337. else
  338. {
  339. this.Padding = e.Cell.Padding;
  340. }
  341. if (e.Table.EditStartAction == EditStartAction.SingleClick && e.Table.IsCellEditable(e.CellPos))
  342. {
  343. e.Table.EditCell(e.CellPos);
  344. }
  345. }
  346. /// <summary>
  347. /// Raises the DoubleClick event
  348. /// </summary>
  349. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  350. public virtual void OnDoubleClick(CellMouseEventArgs e)
  351. {
  352. this.Bounds = e.CellRect;
  353. if (e.Cell == null)
  354. {
  355. this.Padding = CellPadding.Empty;
  356. }
  357. else
  358. {
  359. this.Padding = e.Cell.Padding;
  360. }
  361. if (e.Table.EditStartAction == EditStartAction.DoubleClick && e.Table.IsCellEditable(e.CellPos))
  362. {
  363. e.Table.EditCell(e.CellPos);
  364. }
  365. }
  366. #endregion
  367. #endregion
  368. #region Paint
  369. /// <summary>
  370. /// Raises the PaintCell event
  371. /// </summary>
  372. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  373. public virtual void OnPaintCell(PaintCellEventArgs e)
  374. {
  375. this.Bounds = e.CellRect;
  376. if (e.Cell != null)
  377. {
  378. this.Padding = e.Cell.Padding;
  379. this.Alignment = e.Table.ColumnModel.Columns[e.Column].Alignment;
  380. this.LineAlignment = e.Table.TableModel.Rows[e.Row].Alignment;
  381. this.Format = e.Table.ColumnModel.Columns[e.Column].Format;
  382. this.Font = e.Cell.Font;
  383. }
  384. else
  385. {
  386. this.Padding = CellPadding.Empty;
  387. this.Alignment = ColumnAlignment.Left;
  388. this.LineAlignment = RowAlignment.Center;
  389. this.Format = "";
  390. this.Font = null;
  391. }
  392. // if the font is null, use the default font
  393. if (this.Font == null)
  394. {
  395. this.Font = Control.DefaultFont;
  396. }
  397. // paint the Cells background
  398. this.OnPaintBackground(e);
  399. // paint the Cells foreground
  400. this.OnPaint(e);
  401. }
  402. /// <summary>
  403. /// Raises the PaintBackground event
  404. /// </summary>
  405. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  406. protected virtual void OnPaintBackground(PaintCellEventArgs e)
  407. {
  408. if (e.Selected && (!e.Table.HideSelection || (e.Table.HideSelection && (e.Table.Focused || e.Table.IsEditing))))
  409. {
  410. if (e.Table.Focused || e.Table.IsEditing)
  411. {
  412. this.ForeColor = e.Table.SelectionForeColor;
  413. this.BackColor = e.Table.SelectionBackColor;
  414. }
  415. else
  416. {
  417. this.BackColor = e.Table.UnfocusedSelectionBackColor;
  418. this.ForeColor = e.Table.UnfocusedSelectionForeColor;
  419. }
  420. if (this.BackColor.A != 0)
  421. {
  422. e.Graphics.FillRectangle(this.BackBrush, e.CellRect);
  423. }
  424. }
  425. else
  426. {
  427. this.ForeColor = e.Cell != null ? e.Cell.ForeColor : Color.Black;
  428. if (!e.Sorted || (e.Sorted && e.Table.SortedColumnBackColor.A < 255))
  429. {
  430. if (e.Cell != null)
  431. {
  432. if (e.Cell.BackColor.A < 255)
  433. {
  434. if (e.Row % 2 == 1)
  435. {
  436. if (e.Table.AlternatingRowColor.A != 0)
  437. {
  438. this.BackColor = e.Table.AlternatingRowColor;
  439. e.Graphics.FillRectangle(this.BackBrush, e.CellRect);
  440. }
  441. }
  442. this.BackColor = e.Cell.BackColor;
  443. if (e.Cell.BackColor.A != 0)
  444. {
  445. e.Graphics.FillRectangle(this.BackBrush, e.CellRect);
  446. }
  447. }
  448. else
  449. {
  450. this.BackColor = e.Cell.BackColor;
  451. if (e.Cell.BackColor.A != 0)
  452. {
  453. e.Graphics.FillRectangle(this.BackBrush, e.CellRect);
  454. }
  455. }
  456. }
  457. else
  458. {
  459. if (e.Row % 2 == 1)
  460. {
  461. if (e.Table.AlternatingRowColor.A != 0)
  462. {
  463. this.BackColor = e.Table.AlternatingRowColor;
  464. e.Graphics.FillRectangle(this.BackBrush, e.CellRect);
  465. }
  466. }
  467. }
  468. if (e.Sorted)
  469. {
  470. this.BackColor = e.Table.SortedColumnBackColor;
  471. if (e.Table.SortedColumnBackColor.A != 0)
  472. {
  473. e.Graphics.FillRectangle(this.BackBrush, e.CellRect);
  474. }
  475. }
  476. }
  477. else
  478. {
  479. this.BackColor = e.Table.SortedColumnBackColor;
  480. e.Graphics.FillRectangle(this.BackBrush, e.CellRect);
  481. }
  482. }
  483. }
  484. /// <summary>
  485. /// Raises the Paint event
  486. /// </summary>
  487. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  488. protected virtual void OnPaint(PaintCellEventArgs e)
  489. {
  490. }
  491. /// <summary>
  492. /// Raises the PaintBorder event
  493. /// </summary>
  494. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  495. /// <param name="pen">The pen used to draw the border</param>
  496. protected virtual void OnPaintBorder(PaintCellEventArgs e, Pen pen)
  497. {
  498. // bottom
  499. e.Graphics.DrawLine(pen, e.CellRect.Left, e.CellRect.Bottom, e.CellRect.Right, e.CellRect.Bottom);
  500. // right
  501. e.Graphics.DrawLine(pen, e.CellRect.Right, e.CellRect.Top, e.CellRect.Right, e.CellRect.Bottom);
  502. }
  503. #endregion
  504. #endregion
  505. }
  506. }