Cell.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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.ComponentModel;
  32. using System.Drawing;
  33. using System.Windows.Forms;
  34. using XPTable.Events;
  35. using XPTable.Models.Design;
  36. using XPTable.Renderers;
  37. namespace XPTable.Models
  38. {
  39. /// <summary>
  40. /// Represents a Cell that is displayed in a Table
  41. /// </summary>
  42. [DesignTimeVisible(true),
  43. TypeConverter(typeof(CellConverter))]
  44. public class Cell : IDisposable
  45. {
  46. #region EventHandlers
  47. /// <summary>
  48. /// Occurs when the value of a Cells property changes
  49. /// </summary>
  50. public event CellEventHandler PropertyChanged;
  51. #endregion
  52. #region Class Data
  53. // Cell state flags
  54. private static readonly int STATE_EDITABLE = 1;
  55. private static readonly int STATE_ENABLED = 2;
  56. private static readonly int STATE_SELECTED = 4;
  57. /// <summary>
  58. /// The text displayed in the Cell
  59. /// </summary>
  60. private string text;
  61. /// <summary>
  62. /// An object that contains data to be displayed in the Cell
  63. /// </summary>
  64. private object data;
  65. /// <summary>
  66. /// An object that contains data about the Cell
  67. /// </summary>
  68. private object tag;
  69. /// <summary>
  70. /// Stores information used by CellRenderers to record the current
  71. /// state of the Cell
  72. /// </summary>
  73. private object rendererData;
  74. /// <summary>
  75. /// The Row that the Cell belongs to
  76. /// </summary>
  77. private Row row;
  78. /// <summary>
  79. /// The index of the Cell
  80. /// </summary>
  81. private int index;
  82. /// <summary>
  83. /// Contains the current state of the the Cell
  84. /// </summary>
  85. private byte state;
  86. /// <summary>
  87. /// The Cells CellStyle settings
  88. /// </summary>
  89. private CellStyle cellStyle;
  90. /// <summary>
  91. /// The Cells CellCheckStyle settings
  92. /// </summary>
  93. private CellCheckStyle checkStyle;
  94. /// <summary>
  95. /// The Cells CellImageStyle settings
  96. /// </summary>
  97. private CellImageStyle imageStyle;
  98. /// <summary>
  99. /// The text displayed in the Cells tooltip
  100. /// </summary>
  101. private string tooltipText;
  102. /// <summary>
  103. /// Specifies whether the Cell has been disposed
  104. /// </summary>
  105. private bool disposed = false;
  106. #endregion
  107. #region Constructor
  108. /// <summary>
  109. /// Initializes a new instance of the Cell class with default settings
  110. /// </summary>
  111. public Cell() : base()
  112. {
  113. this.Init();
  114. }
  115. /// <summary>
  116. /// Initializes a new instance of the Cell class with the specified text
  117. /// </summary>
  118. /// <param name="text">The text displayed in the Cell</param>
  119. public Cell(string text)
  120. {
  121. this.Init();
  122. this.text = text;
  123. }
  124. /// <summary>
  125. /// Initializes a new instance of the Cell class with the specified object
  126. /// </summary>
  127. /// <param name="value">The object displayed in the Cell</param>
  128. public Cell(object value)
  129. {
  130. this.Init();
  131. this.data = value;
  132. }
  133. /// <summary>
  134. /// Initializes a new instance of the Cell class with the specified text
  135. /// and object
  136. /// </summary>
  137. /// <param name="text">The text displayed in the Cell</param>
  138. /// <param name="value">The object displayed in the Cell</param>
  139. public Cell(string text, object value)
  140. {
  141. this.Init();
  142. this.text = text;
  143. this.data = value;
  144. }
  145. /// <summary>
  146. /// Initializes a new instance of the Cell class with the specified text
  147. /// and check value
  148. /// </summary>
  149. /// <param name="text">The text displayed in the Cell</param>
  150. /// <param name="check">Specifies whether the Cell is Checked</param>
  151. public Cell(string text, bool check)
  152. {
  153. this.Init();
  154. this.text = text;
  155. this.Checked = check;
  156. }
  157. /// <summary>
  158. /// Initializes a new instance of the Cell class with the specified text
  159. /// and Image value
  160. /// </summary>
  161. /// <param name="text">The text displayed in the Cell</param>
  162. /// <param name="image">The Image displayed in the Cell</param>
  163. public Cell(string text, Image image)
  164. {
  165. this.Init();
  166. this.text = text;
  167. this.Image = image;
  168. }
  169. /// <summary>
  170. /// Initializes a new instance of the Cell class with the specified text,
  171. /// fore Color, back Color and Font
  172. /// </summary>
  173. /// <param name="text">The text displayed in the Cell</param>
  174. /// <param name="foreColor">The foreground Color of the Cell</param>
  175. /// <param name="backColor">The background Color of the Cell</param>
  176. /// <param name="font">The Font used to draw the text in the Cell</param>
  177. public Cell(string text, Color foreColor, Color backColor, Font font)
  178. {
  179. this.Init();
  180. this.text = text;
  181. this.ForeColor = foreColor;
  182. this.BackColor = backColor;
  183. this.Font = font;
  184. }
  185. /// <summary>
  186. /// Initializes a new instance of the Cell class with the specified text
  187. /// and CellStyle
  188. /// </summary>
  189. /// <param name="text">The text displayed in the Cell</param>
  190. /// <param name="cellStyle">A CellStyle that specifies the visual appearance
  191. /// of the Cell</param>
  192. public Cell(string text, CellStyle cellStyle)
  193. {
  194. this.Init();
  195. this.text = text;
  196. this.cellStyle = cellStyle;
  197. }
  198. /// <summary>
  199. /// Initializes a new instance of the Cell class with the specified object,
  200. /// fore Color, back Color and Font
  201. /// </summary>
  202. /// <param name="value">The object displayed in the Cell</param>
  203. /// <param name="foreColor">The foreground Color of the Cell</param>
  204. /// <param name="backColor">The background Color of the Cell</param>
  205. /// <param name="font">The Font used to draw the text in the Cell</param>
  206. public Cell(object value, Color foreColor, Color backColor, Font font)
  207. {
  208. this.Init();
  209. this.data = value;
  210. this.ForeColor = foreColor;
  211. this.BackColor = backColor;
  212. this.Font = font;
  213. }
  214. /// <summary>
  215. /// Initializes a new instance of the Cell class with the specified text
  216. /// and CellStyle
  217. /// </summary>
  218. /// <param name="value">The object displayed in the Cell</param>
  219. /// <param name="cellStyle">A CellStyle that specifies the visual appearance
  220. /// of the Cell</param>
  221. public Cell(object value, CellStyle cellStyle)
  222. {
  223. this.Init();
  224. this.data = value;
  225. this.cellStyle = cellStyle;
  226. }
  227. /// <summary>
  228. /// Initializes a new instance of the Cell class with the specified text,
  229. /// object, fore Color, back Color and Font
  230. /// </summary>
  231. /// <param name="text">The text displayed in the Cell</param>
  232. /// <param name="value">The object displayed in the Cell</param>
  233. /// <param name="foreColor">The foreground Color of the Cell</param>
  234. /// <param name="backColor">The background Color of the Cell</param>
  235. /// <param name="font">The Font used to draw the text in the Cell</param>
  236. public Cell(string text, object value, Color foreColor, Color backColor, Font font)
  237. {
  238. this.Init();
  239. this.text = text;
  240. this.data = value;
  241. this.ForeColor = foreColor;
  242. this.BackColor = backColor;
  243. this.Font = font;
  244. }
  245. /// <summary>
  246. /// Initializes a new instance of the Cell class with the specified text,
  247. /// object and CellStyle
  248. /// </summary>
  249. /// <param name="text">The text displayed in the Cell</param>
  250. /// <param name="value">The object displayed in the Cell</param>
  251. /// <param name="cellStyle">A CellStyle that specifies the visual appearance
  252. /// of the Cell</param>
  253. public Cell(string text, object value, CellStyle cellStyle)
  254. {
  255. this.Init();
  256. this.text = text;
  257. this.data = value;
  258. this.cellStyle = cellStyle;
  259. }
  260. /// <summary>
  261. /// Initializes a new instance of the Cell class with the specified text,
  262. /// check value, fore Color, back Color and Font
  263. /// </summary>
  264. /// <param name="text">The text displayed in the Cell</param>
  265. /// <param name="check">Specifies whether the Cell is Checked</param>
  266. /// <param name="foreColor">The foreground Color of the Cell</param>
  267. /// <param name="backColor">The background Color of the Cell</param>
  268. /// <param name="font">The Font used to draw the text in the Cell</param>
  269. public Cell(string text, bool check, Color foreColor, Color backColor, Font font)
  270. {
  271. this.Init();
  272. this.text = text;
  273. this.Checked = check;
  274. this.ForeColor = foreColor;
  275. this.BackColor = backColor;
  276. this.Font = font;
  277. }
  278. /// <summary>
  279. /// Initializes a new instance of the Cell class with the specified text,
  280. /// check value and CellStyle
  281. /// </summary>
  282. /// <param name="text">The text displayed in the Cell</param>
  283. /// <param name="check">Specifies whether the Cell is Checked</param>
  284. /// <param name="cellStyle">A CellStyle that specifies the visual appearance
  285. /// of the Cell</param>
  286. public Cell(string text, bool check, CellStyle cellStyle)
  287. {
  288. this.Init();
  289. this.text = text;
  290. this.Checked = check;
  291. this.cellStyle = cellStyle;
  292. }
  293. /// <summary>
  294. /// Initializes a new instance of the Cell class with the specified text,
  295. /// Image, fore Color, back Color and Font
  296. /// </summary>
  297. /// <param name="text">The text displayed in the Cell</param>
  298. /// <param name="image">The Image displayed in the Cell</param>
  299. /// <param name="foreColor">The foreground Color of the Cell</param>
  300. /// <param name="backColor">The background Color of the Cell</param>
  301. /// <param name="font">The Font used to draw the text in the Cell</param>
  302. public Cell(string text, Image image, Color foreColor, Color backColor, Font font)
  303. {
  304. this.Init();
  305. this.text = text;
  306. this.Image = image;
  307. this.ForeColor = foreColor;
  308. this.BackColor = backColor;
  309. this.Font = font;
  310. }
  311. /// <summary>
  312. /// Initializes a new instance of the Cell class with the specified text,
  313. /// Image and CellStyle
  314. /// </summary>
  315. /// <param name="text">The text displayed in the Cell</param>
  316. /// <param name="image">The Image displayed in the Cell</param>
  317. /// <param name="cellStyle">A CellStyle that specifies the visual appearance
  318. /// of the Cell</param>
  319. public Cell(string text, Image image, CellStyle cellStyle)
  320. {
  321. this.Init();
  322. this.text = text;
  323. this.Image = image;
  324. this.cellStyle = cellStyle;
  325. }
  326. /// <summary>
  327. /// Initialise default values
  328. /// </summary>
  329. private void Init()
  330. {
  331. this.text = null;
  332. this.data = null;
  333. this.rendererData = null;
  334. this.tag = null;
  335. this.row = null;
  336. this.index = -1;
  337. this.cellStyle = null;
  338. this.checkStyle = null;
  339. this.imageStyle = null;
  340. this.tooltipText = null;
  341. this.state = (byte) (STATE_EDITABLE | STATE_ENABLED);
  342. }
  343. #endregion
  344. #region Methods
  345. /// <summary>
  346. /// Releases all resources used by the Cell
  347. /// </summary>
  348. public void Dispose()
  349. {
  350. if (!this.disposed)
  351. {
  352. this.text = null;
  353. this.data = null;
  354. this.tag = null;
  355. this.rendererData = null;
  356. if (this.row != null)
  357. {
  358. this.row.Cells.Remove(this);
  359. }
  360. this.row = null;
  361. this.index = -1;
  362. this.cellStyle = null;
  363. this.checkStyle = null;
  364. this.imageStyle = null;
  365. this.tooltipText = null;
  366. this.state = (byte) 0;
  367. this.disposed = true;
  368. }
  369. }
  370. /// <summary>
  371. /// Returns the state represented by the specified state flag
  372. /// </summary>
  373. /// <param name="flag">A flag that represents the state to return</param>
  374. /// <returns>The state represented by the specified state flag</returns>
  375. internal bool GetState(int flag)
  376. {
  377. return ((this.state & flag) != 0);
  378. }
  379. /// <summary>
  380. /// Sets the state represented by the specified state flag to the specified value
  381. /// </summary>
  382. /// <param name="flag">A flag that represents the state to be set</param>
  383. /// <param name="value">The new value of the state</param>
  384. internal void SetState(int flag, bool value)
  385. {
  386. this.state = (byte) (value ? (this.state | flag) : (this.state & ~flag));
  387. }
  388. #endregion
  389. #region Properties
  390. /// <summary>
  391. /// Gets or sets the text displayed by the Cell
  392. /// </summary>
  393. [Category("Appearance"),
  394. DefaultValue(null),
  395. Description("The text displayed by the cell")]
  396. public string Text
  397. {
  398. get
  399. {
  400. return this.text;
  401. }
  402. set
  403. {
  404. if (this.text == null || !this.text.Equals(value))
  405. {
  406. string oldText = this.Text;
  407. this.text = value;
  408. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.ValueChanged, oldText));
  409. }
  410. }
  411. }
  412. /// <summary>
  413. /// Gets or sets the Cells non-text data
  414. /// </summary>
  415. [Category("Appearance"),
  416. DefaultValue(null),
  417. Description("The non-text data displayed by the cell"),
  418. TypeConverter(typeof(StringConverter))]
  419. public object Data
  420. {
  421. get
  422. {
  423. return this.data;
  424. }
  425. set
  426. {
  427. if (this.data != value)
  428. {
  429. object oldData = this.Data;
  430. this.data = value;
  431. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.ValueChanged, oldData));
  432. }
  433. }
  434. }
  435. /// <summary>
  436. /// Gets or sets the object that contains data about the Cell
  437. /// </summary>
  438. [Category("Appearance"),
  439. DefaultValue(null),
  440. Description("User defined data associated with the cell"),
  441. TypeConverter(typeof(StringConverter))]
  442. public object Tag
  443. {
  444. get
  445. {
  446. return this.tag;
  447. }
  448. set
  449. {
  450. this.tag = value;
  451. }
  452. }
  453. /// <summary>
  454. /// Gets or sets the CellStyle used by the Cell
  455. /// </summary>
  456. [Browsable(false),
  457. DefaultValue(null)]
  458. public CellStyle CellStyle
  459. {
  460. get
  461. {
  462. return this.cellStyle;
  463. }
  464. set
  465. {
  466. if (this.cellStyle != value)
  467. {
  468. CellStyle oldStyle = this.CellStyle;
  469. this.cellStyle = value;
  470. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.StyleChanged, oldStyle));
  471. }
  472. }
  473. }
  474. /// <summary>
  475. /// Gets or sets whether the Cell is selected
  476. /// </summary>
  477. [Browsable(false)]
  478. public bool Selected
  479. {
  480. get
  481. {
  482. return this.GetState(STATE_SELECTED);
  483. }
  484. }
  485. /// <summary>
  486. /// Sets whether the Cell is selected
  487. /// </summary>
  488. /// <param name="selected">A boolean value that specifies whether the
  489. /// cell is selected</param>
  490. internal void SetSelected(bool selected)
  491. {
  492. this.SetState(STATE_SELECTED, selected);
  493. }
  494. /// <summary>
  495. /// Gets or sets the background Color for the Cell
  496. /// </summary>
  497. [Category("Appearance"),
  498. Description("The background color used to display text and graphics in the cell")]
  499. public Color BackColor
  500. {
  501. get
  502. {
  503. if (this.CellStyle == null)
  504. {
  505. if (this.Row != null)
  506. {
  507. return this.Row.BackColor;
  508. }
  509. return Color.Transparent;
  510. }
  511. return this.CellStyle.BackColor;
  512. }
  513. set
  514. {
  515. if (this.CellStyle == null)
  516. {
  517. this.CellStyle = new CellStyle();
  518. }
  519. if (this.CellStyle.BackColor != value)
  520. {
  521. Color oldBackColor = this.BackColor;
  522. this.CellStyle.BackColor = value;
  523. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.BackColorChanged, oldBackColor));
  524. }
  525. }
  526. }
  527. /// <summary>
  528. /// Specifies whether the BackColor property should be serialized at
  529. /// design time
  530. /// </summary>
  531. /// <returns>true if the BackColor property should be serialized,
  532. /// false otherwise</returns>
  533. private bool ShouldSerializeBackColor()
  534. {
  535. return (this.cellStyle != null && this.cellStyle.BackColor != Color.Empty);
  536. }
  537. /// <summary>
  538. /// Gets or sets the foreground Color for the Cell
  539. /// </summary>
  540. [Category("Appearance"),
  541. Description("The foreground color used to display text and graphics in the cell")]
  542. public Color ForeColor
  543. {
  544. get
  545. {
  546. if (this.CellStyle == null)
  547. {
  548. if (this.Row != null)
  549. {
  550. return this.Row.ForeColor;
  551. }
  552. return Color.Transparent;
  553. }
  554. else
  555. {
  556. if (this.CellStyle.ForeColor == Color.Empty || this.CellStyle.ForeColor == Color.Transparent)
  557. {
  558. if (this.Row != null)
  559. {
  560. return this.Row.ForeColor;
  561. }
  562. }
  563. return this.CellStyle.ForeColor;
  564. }
  565. }
  566. set
  567. {
  568. if (this.CellStyle == null)
  569. {
  570. this.CellStyle = new CellStyle();
  571. }
  572. if (this.CellStyle.ForeColor != value)
  573. {
  574. Color oldForeColor = this.ForeColor;
  575. this.CellStyle.ForeColor = value;
  576. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.ForeColorChanged, oldForeColor));
  577. }
  578. }
  579. }
  580. /// <summary>
  581. /// Specifies whether the ForeColor property should be serialized at
  582. /// design time
  583. /// </summary>
  584. /// <returns>true if the ForeColor property should be serialized,
  585. /// false otherwise</returns>
  586. private bool ShouldSerializeForeColor()
  587. {
  588. return (this.cellStyle != null && this.cellStyle.ForeColor != Color.Empty);
  589. }
  590. /// <summary>
  591. /// Gets or sets the Font used by the Cell
  592. /// </summary>
  593. [Category("Appearance"),
  594. Description("The font used to display text in the cell")]
  595. public Font Font
  596. {
  597. get
  598. {
  599. if (this.CellStyle == null)
  600. {
  601. if (this.Row != null)
  602. {
  603. return this.Row.Font;
  604. }
  605. return null;
  606. }
  607. else
  608. {
  609. if (this.CellStyle.Font == null)
  610. {
  611. if (this.Row != null)
  612. {
  613. return this.Row.Font;
  614. }
  615. }
  616. return this.CellStyle.Font;
  617. }
  618. }
  619. set
  620. {
  621. if (this.CellStyle == null)
  622. {
  623. this.CellStyle = new CellStyle();
  624. }
  625. if (this.CellStyle.Font != value)
  626. {
  627. Font oldFont = this.Font;
  628. this.CellStyle.Font = value;
  629. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.FontChanged, oldFont));
  630. }
  631. }
  632. }
  633. /// <summary>
  634. /// Specifies whether the Font property should be serialized at
  635. /// design time
  636. /// </summary>
  637. /// <returns>true if the Font property should be serialized,
  638. /// false otherwise</returns>
  639. private bool ShouldSerializeFont()
  640. {
  641. return (this.cellStyle != null && this.cellStyle.Font != null);
  642. }
  643. /// <summary>
  644. /// Gets or sets the amount of space between the Cells Border and its contents
  645. /// </summary>
  646. [Category("Appearance"),
  647. Description("The amount of space between the cells border and its contents")]
  648. public CellPadding Padding
  649. {
  650. get
  651. {
  652. if (this.CellStyle == null)
  653. {
  654. return CellPadding.Empty;
  655. }
  656. return this.CellStyle.Padding;
  657. }
  658. set
  659. {
  660. if (this.CellStyle == null)
  661. {
  662. this.CellStyle = new CellStyle();
  663. }
  664. if (this.CellStyle.Padding != value)
  665. {
  666. CellPadding oldPadding = this.Padding;
  667. this.CellStyle.Padding = value;
  668. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.PaddingChanged, oldPadding));
  669. }
  670. }
  671. }
  672. /// <summary>
  673. /// Specifies whether the Padding property should be serialized at
  674. /// design time
  675. /// </summary>
  676. /// <returns>true if the Padding property should be serialized,
  677. /// false otherwise</returns>
  678. private bool ShouldSerializePadding()
  679. {
  680. return this.Padding != CellPadding.Empty;
  681. }
  682. /// <summary>
  683. /// Gets or sets whether the Cell is in the checked state
  684. /// </summary>
  685. [Category("Appearance"),
  686. DefaultValue(false),
  687. Description("Indicates whether the cell is checked or unchecked"),
  688. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  689. RefreshProperties(RefreshProperties.Repaint)]
  690. public bool Checked
  691. {
  692. get
  693. {
  694. if (this.checkStyle == null)
  695. {
  696. return false;
  697. }
  698. return this.checkStyle.Checked;
  699. }
  700. set
  701. {
  702. if (this.checkStyle == null)
  703. {
  704. this.checkStyle = new CellCheckStyle();
  705. }
  706. if (this.checkStyle.Checked != value)
  707. {
  708. bool oldCheck = this.Checked;
  709. this.checkStyle.Checked = value;
  710. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.CheckStateChanged, oldCheck));
  711. }
  712. }
  713. }
  714. /// <summary>
  715. /// Gets or sets the state of the Cells check box
  716. /// </summary>
  717. [Category("Appearance"),
  718. DefaultValue(CheckState.Unchecked),
  719. Description("Indicates the state of the cells check box"),
  720. RefreshProperties(RefreshProperties.Repaint)]
  721. public CheckState CheckState
  722. {
  723. get
  724. {
  725. if (this.checkStyle == null)
  726. {
  727. return CheckState.Unchecked;
  728. }
  729. return this.checkStyle.CheckState;
  730. }
  731. set
  732. {
  733. if (!Enum.IsDefined(typeof(CheckState), value))
  734. {
  735. throw new InvalidEnumArgumentException("value", (int) value, typeof(CheckState));
  736. }
  737. if (this.checkStyle == null)
  738. {
  739. this.checkStyle = new CellCheckStyle();
  740. }
  741. if (this.checkStyle.CheckState != value)
  742. {
  743. CheckState oldCheckState = this.CheckState;
  744. this.checkStyle.CheckState = value;
  745. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.CheckStateChanged, oldCheckState));
  746. }
  747. }
  748. }
  749. /// <summary>
  750. /// Gets or sets a value indicating whether the Cells check box
  751. /// will allow three check states rather than two
  752. /// </summary>
  753. [Category("Appearance"),
  754. DefaultValue(false),
  755. Description("Controls whether or not the user can select the indeterminate state of the cells check box"),
  756. RefreshProperties(RefreshProperties.Repaint)]
  757. public bool ThreeState
  758. {
  759. get
  760. {
  761. if (this.checkStyle == null)
  762. {
  763. return false;
  764. }
  765. return this.checkStyle.ThreeState;
  766. }
  767. set
  768. {
  769. if (this.checkStyle == null)
  770. {
  771. this.checkStyle = new CellCheckStyle();
  772. }
  773. if (this.checkStyle.ThreeState != value)
  774. {
  775. bool oldThreeState = this.ThreeState;
  776. this.checkStyle.ThreeState = value;
  777. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.ThreeStateChanged, oldThreeState));
  778. }
  779. }
  780. }
  781. /// <summary>
  782. /// Gets or sets the image that is displayed in the Cell
  783. /// </summary>
  784. [Category("Appearance"),
  785. DefaultValue(null),
  786. Description("The image that will be displayed in the cell")]
  787. public Image Image
  788. {
  789. get
  790. {
  791. if (this.imageStyle == null)
  792. {
  793. return null;
  794. }
  795. return this.imageStyle.Image;
  796. }
  797. set
  798. {
  799. if (this.imageStyle == null)
  800. {
  801. this.imageStyle = new CellImageStyle();
  802. }
  803. if (this.imageStyle.Image != value)
  804. {
  805. Image oldImage = this.Image;
  806. this.imageStyle.Image = value;
  807. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.ImageChanged, oldImage));
  808. }
  809. }
  810. }
  811. /// <summary>
  812. /// Gets or sets how the Cells image is sized within the Cell
  813. /// </summary>
  814. [Category("Appearance"),
  815. DefaultValue(ImageSizeMode.Normal),
  816. Description("Controls how the image is sized within the cell")]
  817. public ImageSizeMode ImageSizeMode
  818. {
  819. get
  820. {
  821. if (this.imageStyle == null)
  822. {
  823. return ImageSizeMode.Normal;
  824. }
  825. return this.imageStyle.ImageSizeMode;
  826. }
  827. set
  828. {
  829. if (this.imageStyle == null)
  830. {
  831. this.imageStyle = new CellImageStyle();
  832. }
  833. if (this.imageStyle.ImageSizeMode != value)
  834. {
  835. ImageSizeMode oldSizeMode = this.ImageSizeMode;
  836. this.imageStyle.ImageSizeMode = value;
  837. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.ImageSizeModeChanged, oldSizeMode));
  838. }
  839. }
  840. }
  841. /// <summary>
  842. /// Gets or sets a value indicating whether the Cells contents are able
  843. /// to be edited
  844. /// </summary>
  845. [Category("Appearance"),
  846. Description("Controls whether the cells contents are able to be changed by the user")]
  847. public bool Editable
  848. {
  849. get
  850. {
  851. if (!this.GetState(STATE_EDITABLE))
  852. {
  853. return false;
  854. }
  855. if (this.Row == null)
  856. {
  857. return this.Enabled;
  858. }
  859. return this.Enabled && this.Row.Editable;
  860. }
  861. set
  862. {
  863. bool editable = this.Editable;
  864. this.SetState(STATE_EDITABLE, value);
  865. if (editable != value)
  866. {
  867. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.EditableChanged, editable));
  868. }
  869. }
  870. }
  871. /// <summary>
  872. /// Specifies whether the Editable property should be serialized at
  873. /// design time
  874. /// </summary>
  875. /// <returns>true if the Editable property should be serialized,
  876. /// false otherwise</returns>
  877. private bool ShouldSerializeEditable()
  878. {
  879. return !this.GetState(STATE_EDITABLE);
  880. }
  881. /// <summary>
  882. /// Gets or sets a value indicating whether the Cell
  883. /// can respond to user interaction
  884. /// </summary>
  885. [Category("Appearance"),
  886. Description("Indicates whether the cell is enabled")]
  887. public bool Enabled
  888. {
  889. get
  890. {
  891. if (!this.GetState(STATE_ENABLED))
  892. {
  893. return false;
  894. }
  895. if (this.Row == null)
  896. {
  897. return true;
  898. }
  899. return this.Row.Enabled;
  900. }
  901. set
  902. {
  903. bool enabled = this.Enabled;
  904. this.SetState(STATE_ENABLED, value);
  905. if (enabled != value)
  906. {
  907. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.EnabledChanged, enabled));
  908. }
  909. }
  910. }
  911. /// <summary>
  912. /// Specifies whether the Enabled property should be serialized at
  913. /// design time
  914. /// </summary>
  915. /// <returns>true if the Enabled property should be serialized,
  916. /// false otherwise</returns>
  917. private bool ShouldSerializeEnabled()
  918. {
  919. return !this.GetState(STATE_ENABLED);
  920. }
  921. /// <summary>
  922. /// Gets or sets the text displayed in the Cells tooltip
  923. /// </summary>
  924. [Category("Appearance"),
  925. DefaultValue(null),
  926. Description("The text displayed in the cells tooltip")]
  927. public string ToolTipText
  928. {
  929. get
  930. {
  931. return this.tooltipText;
  932. }
  933. set
  934. {
  935. if (this.tooltipText != value)
  936. {
  937. string oldToolTip = this.tooltipText;
  938. this.tooltipText = value;
  939. this.OnPropertyChanged(new CellEventArgs(this, CellEventType.ToolTipTextChanged, oldToolTip));
  940. }
  941. }
  942. }
  943. /// <summary>
  944. /// Gets or sets the information used by CellRenderers to record the current
  945. /// state of the Cell
  946. /// </summary>
  947. protected internal object RendererData
  948. {
  949. get
  950. {
  951. return this.rendererData;
  952. }
  953. set
  954. {
  955. this.rendererData = value;
  956. }
  957. }
  958. /// <summary>
  959. /// Gets the Row that the Cell belongs to
  960. /// </summary>
  961. [Browsable(false)]
  962. public Row Row
  963. {
  964. get
  965. {
  966. return this.row;
  967. }
  968. }
  969. /// <summary>
  970. /// Gets or sets the Row that the Cell belongs to
  971. /// </summary>
  972. internal Row InternalRow
  973. {
  974. get
  975. {
  976. return this.row;
  977. }
  978. set
  979. {
  980. this.row = value;
  981. }
  982. }
  983. /// <summary>
  984. /// Gets the index of the Cell within its Row
  985. /// </summary>
  986. [Browsable(false)]
  987. public int Index
  988. {
  989. get
  990. {
  991. return this.index;
  992. }
  993. }
  994. /// <summary>
  995. /// Gets or sets the index of the Cell within its Row
  996. /// </summary>
  997. internal int InternalIndex
  998. {
  999. get
  1000. {
  1001. return this.index;
  1002. }
  1003. set
  1004. {
  1005. this.index = value;
  1006. }
  1007. }
  1008. /// <summary>
  1009. /// Gets whether the Cell is able to raise events
  1010. /// </summary>
  1011. protected internal bool CanRaiseEvents
  1012. {
  1013. get
  1014. {
  1015. // check if the Row that the Cell belongs to is able to
  1016. // raise events (if it can't, the Cell shouldn't raise
  1017. // events either)
  1018. if (this.Row != null)
  1019. {
  1020. return this.Row.CanRaiseEvents;
  1021. }
  1022. return true;
  1023. }
  1024. }
  1025. #endregion
  1026. #region Events
  1027. /// <summary>
  1028. /// Raises the PropertyChanged event
  1029. /// </summary>
  1030. /// <param name="e">A CellEventArgs that contains the event data</param>
  1031. protected virtual void OnPropertyChanged(CellEventArgs e)
  1032. {
  1033. e.SetColumn(this.Index);
  1034. if (this.Row != null)
  1035. {
  1036. e.SetRow(this.Row.Index);
  1037. }
  1038. if (this.CanRaiseEvents)
  1039. {
  1040. if (this.Row != null)
  1041. {
  1042. this.Row.OnCellPropertyChanged(e);
  1043. }
  1044. if (PropertyChanged != null)
  1045. {
  1046. PropertyChanged(this, e);
  1047. }
  1048. }
  1049. }
  1050. #endregion
  1051. }
  1052. }