TableModel.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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.Collections;
  32. using System.ComponentModel;
  33. using System.ComponentModel.Design;
  34. using System.Drawing;
  35. using System.Drawing.Design;
  36. using XPTable.Events;
  37. using XPTable.Models.Design;
  38. namespace XPTable.Models
  39. {
  40. /// <summary>
  41. /// Represents a collection of Rows and Cells displayed in a Table.
  42. /// </summary>
  43. [DesignTimeVisible(true),
  44. ToolboxItem(true),
  45. ToolboxBitmap(typeof(TableModel))]
  46. public class TableModel : Component
  47. {
  48. #region Event Handlers
  49. /// <summary>
  50. /// Occurs when a Row is added to the TableModel
  51. /// </summary>
  52. public event TableModelEventHandler RowAdded;
  53. /// <summary>
  54. /// Occurs when a Row is removed from the TableModel
  55. /// </summary>
  56. public event TableModelEventHandler RowRemoved;
  57. /// <summary>
  58. /// Occurs when the value of the TableModel Selection property changes
  59. /// </summary>
  60. public event SelectionEventHandler SelectionChanged;
  61. /// <summary>
  62. /// Occurs when the value of the RowHeight property changes
  63. /// </summary>
  64. public event EventHandler RowHeightChanged;
  65. #endregion
  66. #region Class Data
  67. /// <summary>
  68. /// The default height of a Row
  69. /// </summary>
  70. public static readonly int DefaultRowHeight = 15;
  71. /// <summary>
  72. /// The minimum height of a Row
  73. /// </summary>
  74. public static readonly int MinimumRowHeight = 14;
  75. /// <summary>
  76. /// The maximum height of a Row
  77. /// </summary>
  78. public static readonly int MaximumRowHeight = 1024;
  79. /// <summary>
  80. /// The collection of Rows's contained in the TableModel
  81. /// </summary>
  82. private RowCollection rows;
  83. /// <summary>
  84. /// The Table that the TableModel belongs to
  85. /// </summary>
  86. private Table table;
  87. /// <summary>
  88. /// The currently selected Rows and Cells
  89. /// </summary>
  90. private Selection selection;
  91. /// <summary>
  92. /// The height of each Row in the TableModel
  93. /// </summary>
  94. private int rowHeight;
  95. #endregion
  96. #region Constructor
  97. /// <summary>
  98. /// Initializes a new instance of the TableModel class with default settings
  99. /// </summary>
  100. public TableModel()
  101. {
  102. this.Init();
  103. }
  104. /// <summary>
  105. /// Initializes a new instance of the TableModel class with an array of Row objects
  106. /// </summary>
  107. /// <param name="rows">An array of Row objects that represent the Rows
  108. /// of the TableModel</param>
  109. public TableModel(Row[] rows)
  110. {
  111. if (rows == null)
  112. {
  113. throw new ArgumentNullException("rows", "Row[] cannot be null");
  114. }
  115. this.Init();
  116. if (rows.Length > 0)
  117. {
  118. this.Rows.AddRange(rows);
  119. }
  120. }
  121. /// <summary>
  122. /// Initialise default settings
  123. /// </summary>
  124. private void Init()
  125. {
  126. this.rows = null;
  127. this.selection = new Selection(this);
  128. this.table = null;
  129. this.rowHeight = TableModel.DefaultRowHeight;
  130. }
  131. #endregion
  132. #region Methods
  133. /// <summary>
  134. /// Releases the unmanaged resources used by the TableModel and optionally
  135. /// releases the managed resources
  136. /// </summary>
  137. protected override void Dispose(bool disposing)
  138. {
  139. if (disposing)
  140. {
  141. }
  142. base.Dispose(disposing);
  143. }
  144. /// <summary>
  145. /// Returns the index of the Row that lies on the specified position
  146. /// </summary>
  147. /// <param name="yPosition">The y-coordinate to check</param>
  148. /// <returns>The index of the Row at the specified position or -1 if
  149. /// no Row is found</returns>
  150. public int RowIndexAt(int yPosition)
  151. {
  152. int row = yPosition / this.RowHeight;
  153. if (row < 0 || row > this.Rows.Count - 1)
  154. {
  155. return -1;
  156. }
  157. return row;
  158. }
  159. #endregion
  160. #region Properties
  161. /// <summary>
  162. /// Gets the Cell located at the specified row index and column index
  163. /// </summary>
  164. /// <param name="row">The row index of the Cell</param>
  165. /// <param name="column">The column index of the Cell</param>
  166. [Browsable(false)]
  167. public Cell this[int row, int column]
  168. {
  169. get
  170. {
  171. if (row < 0 || row >= this.Rows.Count)
  172. {
  173. return null;
  174. }
  175. if (column < 0 || column >= this.Rows[row].Cells.Count)
  176. {
  177. return null;
  178. }
  179. return this.Rows[row].Cells[column];
  180. }
  181. }
  182. /// <summary>
  183. /// Gets the Cell located at the specified cell position
  184. /// </summary>
  185. /// <param name="cellPos">The position of the Cell</param>
  186. [Browsable(false)]
  187. public Cell this[CellPos cellPos]
  188. {
  189. get
  190. {
  191. return this[cellPos.Row, cellPos.Column];
  192. }
  193. }
  194. /// <summary>
  195. /// A TableModel.RowCollection representing the collection of
  196. /// Rows contained within the TableModel
  197. /// </summary>
  198. [Category("Behavior"),
  199. Description("Row Collection"),
  200. DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  201. Editor(typeof(RowCollectionEditor), typeof(UITypeEditor))]
  202. public RowCollection Rows
  203. {
  204. get
  205. {
  206. if (this.rows == null)
  207. {
  208. this.rows = new RowCollection(this);
  209. }
  210. return this.rows;
  211. }
  212. }
  213. /// <summary>
  214. /// A TableModel.Selection representing the collection of selected
  215. /// Rows and Cells contained within the TableModel
  216. /// </summary>
  217. [Browsable(false)]
  218. public Selection Selections
  219. {
  220. get
  221. {
  222. if (this.selection == null)
  223. {
  224. this.selection = new Selection(this);
  225. }
  226. return this.selection;
  227. }
  228. }
  229. /// <summary>
  230. /// Gets or sets the height of each Row in the TableModel
  231. /// </summary>
  232. [Category("Appearance"),
  233. Description("The height of each row")]
  234. public int RowHeight
  235. {
  236. get
  237. {
  238. return this.rowHeight;
  239. }
  240. set
  241. {
  242. if (value < TableModel.MinimumRowHeight)
  243. {
  244. value = TableModel.MinimumRowHeight;
  245. }
  246. else if (value > TableModel.MaximumRowHeight)
  247. {
  248. value = TableModel.MaximumRowHeight;
  249. }
  250. if (this.rowHeight != value)
  251. {
  252. this.rowHeight = value;
  253. this.OnRowHeightChanged(EventArgs.Empty);
  254. }
  255. }
  256. }
  257. /// <summary>
  258. /// Specifies whether the RowHeight property should be serialized at
  259. /// design time
  260. /// </summary>
  261. /// <returns>true if the RowHeight property should be serialized,
  262. /// false otherwise</returns>
  263. private bool ShouldSerializeRowHeight()
  264. {
  265. return this.rowHeight != TableModel.DefaultRowHeight;
  266. }
  267. /// <summary>
  268. /// Gets the total height of all the Rows in the TableModel
  269. /// </summary>
  270. [Browsable(false)]
  271. public int TotalRowHeight
  272. {
  273. get
  274. {
  275. return this.Rows.Count * this.RowHeight;
  276. }
  277. }
  278. /// <summary>
  279. /// Gets the Table the TableModel belongs to
  280. /// </summary>
  281. [Browsable(false)]
  282. public Table Table
  283. {
  284. get
  285. {
  286. return this.table;
  287. }
  288. }
  289. /// <summary>
  290. /// Gets or sets the Table the TableModel belongs to
  291. /// </summary>
  292. internal Table InternalTable
  293. {
  294. get
  295. {
  296. return this.table;
  297. }
  298. set
  299. {
  300. this.table = value;
  301. }
  302. }
  303. /// <summary>
  304. /// Gets whether the TableModel is able to raise events
  305. /// </summary>
  306. protected internal bool CanRaiseEvents
  307. {
  308. get
  309. {
  310. // check if the Table that the TableModel belongs to is able to
  311. // raise events (if it can't, the TableModel shouldn't raise
  312. // events either)
  313. if (this.Table != null)
  314. {
  315. return this.Table.CanRaiseEvents;
  316. }
  317. return true;
  318. }
  319. }
  320. /// <summary>
  321. /// Gets whether the TableModel is enabled
  322. /// </summary>
  323. internal bool Enabled
  324. {
  325. get
  326. {
  327. if (this.Table == null)
  328. {
  329. return true;
  330. }
  331. return this.Table.Enabled;
  332. }
  333. }
  334. /// <summary>
  335. /// Updates the Row's Index property so that it matches the Rows
  336. /// position in the RowCollection
  337. /// </summary>
  338. /// <param name="start">The index to start updating from</param>
  339. internal void UpdateRowIndicies(int start)
  340. {
  341. if (start == -1)
  342. {
  343. start = 0;
  344. }
  345. for (int i=start; i<this.Rows.Count; i++)
  346. {
  347. this.Rows[i].InternalIndex = i;
  348. }
  349. }
  350. #endregion
  351. #region Events
  352. /// <summary>
  353. /// Raises the RowAdded event
  354. /// </summary>
  355. /// <param name="e">A TableModelEventArgs that contains the event data</param>
  356. protected internal virtual void OnRowAdded(TableModelEventArgs e)
  357. {
  358. e.Row.InternalTableModel = this;
  359. e.Row.InternalIndex = e.RowFromIndex;
  360. e.Row.ClearSelection();
  361. this.UpdateRowIndicies(e.RowFromIndex);
  362. if (this.CanRaiseEvents)
  363. {
  364. if (this.Table != null)
  365. {
  366. this.Table.OnRowAdded(e);
  367. }
  368. if (RowAdded != null)
  369. {
  370. RowAdded(this, e);
  371. }
  372. }
  373. }
  374. /// <summary>
  375. /// Raises the RowRemoved event
  376. /// </summary>
  377. /// <param name="e">A TableModelEventArgs that contains the event data</param>
  378. protected internal virtual void OnRowRemoved(TableModelEventArgs e)
  379. {
  380. if (e.Row != null && e.Row.TableModel == this)
  381. {
  382. e.Row.InternalTableModel = null;
  383. e.Row.InternalIndex = -1;
  384. if (e.Row.AnyCellsSelected)
  385. {
  386. e.Row.ClearSelection();
  387. this.Selections.RemoveRow(e.Row);
  388. }
  389. }
  390. this.UpdateRowIndicies(e.RowFromIndex);
  391. if (this.CanRaiseEvents)
  392. {
  393. if (this.Table != null)
  394. {
  395. this.Table.OnRowRemoved(e);
  396. }
  397. if (RowRemoved != null)
  398. {
  399. RowRemoved(this, e);
  400. }
  401. }
  402. }
  403. /// <summary>
  404. /// Raises the SelectionChanged event
  405. /// </summary>
  406. /// <param name="e">A SelectionEventArgs that contains the event data</param>
  407. protected virtual void OnSelectionChanged(SelectionEventArgs e)
  408. {
  409. if (this.CanRaiseEvents)
  410. {
  411. if (this.Table != null)
  412. {
  413. this.Table.OnSelectionChanged(e);
  414. }
  415. if (SelectionChanged != null)
  416. {
  417. SelectionChanged(this, e);
  418. }
  419. }
  420. }
  421. /// <summary>
  422. /// Raises the RowHeightChanged event
  423. /// </summary>
  424. /// <param name="e">An EventArgs that contains the event data</param>
  425. protected virtual void OnRowHeightChanged(EventArgs e)
  426. {
  427. if (this.CanRaiseEvents)
  428. {
  429. if (this.Table != null)
  430. {
  431. this.Table.OnRowHeightChanged(e);
  432. }
  433. if (RowHeightChanged != null)
  434. {
  435. RowHeightChanged(this, e);
  436. }
  437. }
  438. }
  439. /// <summary>
  440. /// Raises the RowPropertyChanged event
  441. /// </summary>
  442. /// <param name="e">A RowEventArgs that contains the event data</param>
  443. internal void OnRowPropertyChanged(RowEventArgs e)
  444. {
  445. if (this.Table != null)
  446. {
  447. this.Table.OnRowPropertyChanged(e);
  448. }
  449. }
  450. /// <summary>
  451. /// Raises the CellAdded event
  452. /// </summary>
  453. /// <param name="e">A RowEventArgs that contains the event data</param>
  454. internal void OnCellAdded(RowEventArgs e)
  455. {
  456. if (this.Table != null)
  457. {
  458. this.Table.OnCellAdded(e);
  459. }
  460. }
  461. /// <summary>
  462. /// Raises the CellRemoved event
  463. /// </summary>
  464. /// <param name="e">A RowEventArgs that contains the event data</param>
  465. internal void OnCellRemoved(RowEventArgs e)
  466. {
  467. if (this.Table != null)
  468. {
  469. this.Table.OnCellRemoved(e);
  470. }
  471. }
  472. /// <summary>
  473. /// Raises the CellPropertyChanged event
  474. /// </summary>
  475. /// <param name="e">A CellEventArgs that contains the event data</param>
  476. internal void OnCellPropertyChanged(CellEventArgs e)
  477. {
  478. if (this.Table != null)
  479. {
  480. this.Table.OnCellPropertyChanged(e);
  481. }
  482. }
  483. #endregion
  484. #region Selection
  485. /// <summary>
  486. /// Represents the collection of selected Rows and Cells in a TableModel.
  487. /// </summary>
  488. public class Selection
  489. {
  490. #region Class Data
  491. /// <summary>
  492. /// The TableModel that owns the Selection
  493. /// </summary>
  494. private TableModel owner;
  495. /// <summary>
  496. /// The list of Rows that have selected Cells
  497. /// </summary>
  498. private ArrayList rows;
  499. /// <summary>
  500. /// The starting cell of a selection that uses the shift key
  501. /// </summary>
  502. private CellPos shiftSelectStart;
  503. /// <summary>
  504. /// The ending cell of a selection that uses the shift key
  505. /// </summary>
  506. private CellPos shiftSelectEnd;
  507. #endregion
  508. #region Constructor
  509. /// <summary>
  510. /// Initializes a new instance of the TableModel.Selection class
  511. /// that belongs to the specified TableModel
  512. /// </summary>
  513. /// <param name="owner">A TableModel representing the tableModel that owns
  514. /// the Selection</param>
  515. public Selection(TableModel owner)
  516. {
  517. if (owner == null)
  518. {
  519. throw new ArgumentNullException("owner", "owner cannot be null");
  520. }
  521. this.owner = owner;
  522. this.rows = new ArrayList();
  523. this.shiftSelectStart = CellPos.Empty;
  524. this.shiftSelectEnd = CellPos.Empty;
  525. }
  526. #endregion
  527. #region Methods
  528. #region Add
  529. /// <summary>
  530. /// Replaces the currently selected Cells with the Cell at the specified
  531. /// row and column indexes
  532. /// </summary>
  533. /// <param name="row">The row index of the Cell to be selected</param>
  534. /// <param name="column">The column index of the Cell to be selected</param>
  535. public void SelectCell(int row, int column)
  536. {
  537. // don't bother going any further if the cell
  538. // is already selected
  539. if (this.rows.Count == 1)
  540. {
  541. Row r = (Row) this.rows[0];
  542. if (r.InternalIndex == row && r.SelectedCellCount == 1)
  543. {
  544. if (column >= 0 && column < r.Cells.Count)
  545. {
  546. if (r.Cells[column].Selected)
  547. {
  548. return;
  549. }
  550. }
  551. }
  552. }
  553. this.SelectCells(row, column, row, column);
  554. }
  555. /// <summary>
  556. /// Replaces the currently selected Cells with the Cell at the specified CellPos
  557. /// </summary>
  558. /// <param name="cellPos">A CellPos thst specifies the row and column indicies of
  559. /// the Cell to be selected</param>
  560. public void SelectCell(CellPos cellPos)
  561. {
  562. this.SelectCell(cellPos.Row, cellPos.Column);
  563. }
  564. /// <summary>
  565. /// Replaces the currently selected Cells with the Cells located between the specified
  566. /// start and end row/column indicies
  567. /// </summary>
  568. /// <param name="startRow">The row index of the start Cell</param>
  569. /// <param name="startColumn">The column index of the start Cell</param>
  570. /// <param name="endRow">The row index of the end Cell</param>
  571. /// <param name="endColumn">The column index of the end Cell</param>
  572. public void SelectCells(int startRow, int startColumn, int endRow, int endColumn)
  573. {
  574. int[] oldSelectedIndicies = this.SelectedIndicies;
  575. this.InternalClear();
  576. if (this.InternalAddCells(startRow, startColumn, endRow, endColumn))
  577. {
  578. this.owner.OnSelectionChanged(new SelectionEventArgs(this.owner, oldSelectedIndicies, this.SelectedIndicies));
  579. }
  580. this.shiftSelectStart = new CellPos(startRow, startColumn);
  581. this.shiftSelectEnd = new CellPos(endRow, endColumn);
  582. }
  583. /// <summary>
  584. /// Replaces the currently selected Cells with the Cells located between the specified
  585. /// start and end CellPos
  586. /// </summary>
  587. /// <param name="start">A CellPos that specifies the start Cell</param>
  588. /// <param name="end">A CellPos that specifies the end Cell</param>
  589. public void SelectCells(CellPos start, CellPos end)
  590. {
  591. this.SelectCells(start.Row, start.Column, end.Row, end.Column);
  592. }
  593. /// <summary>
  594. /// Adds the Cell at the specified row and column indicies to the current selection
  595. /// </summary>
  596. /// <param name="row">The row index of the Cell to add to the selection</param>
  597. /// <param name="column">The column index of the Cell to add to the selection</param>
  598. public void AddCell(int row, int column)
  599. {
  600. this.AddCells(row, column, row, column);
  601. }
  602. /// <summary>
  603. /// Adds the Cell at the specified row and column indicies to the current selection
  604. /// </summary>
  605. /// <param name="cellPos">A CellPos that specifies the Cell to add to the selection</param>
  606. public void AddCell(CellPos cellPos)
  607. {
  608. this.AddCell(cellPos.Row, cellPos.Column);
  609. }
  610. /// <summary>
  611. /// Adds the Cells located between the specified start and end row/column indicies
  612. /// to the current selection
  613. /// </summary>
  614. /// <param name="startRow">The row index of the start Cell</param>
  615. /// <param name="startColumn">The column index of the start Cell</param>
  616. /// <param name="endRow">The row index of the end Cell</param>
  617. /// <param name="endColumn">The column index of the end Cell</param>
  618. public void AddCells(int startRow, int startColumn, int endRow, int endColumn)
  619. {
  620. int[] oldSelectedIndicies = this.SelectedIndicies;
  621. if (InternalAddCells(startRow, startColumn, endRow, endColumn))
  622. {
  623. this.owner.OnSelectionChanged(new SelectionEventArgs(this.owner, oldSelectedIndicies, this.SelectedIndicies));
  624. }
  625. this.shiftSelectStart = new CellPos(startRow, startColumn);
  626. this.shiftSelectEnd = new CellPos(endRow, endColumn);
  627. }
  628. /// <summary>
  629. /// Adds the Cells located between the specified start and end CellPos to the
  630. /// current selection
  631. /// </summary>
  632. /// <param name="start">A CellPos that specifies the start Cell</param>
  633. /// <param name="end">A CellPos that specifies the end Cell</param>
  634. public void AddCells(CellPos start, CellPos end)
  635. {
  636. this.AddCells(start.Row, start.Column, end.Row, end.Column);
  637. }
  638. /// <summary>
  639. /// Adds the Cells located between the specified start and end CellPos to the
  640. /// current selection without raising an event
  641. /// </summary>
  642. /// <param name="start">A CellPos that specifies the start Cell</param>
  643. /// <param name="end">A CellPos that specifies the end Cell</param>
  644. /// <returns>true if any Cells were added, false otherwise</returns>
  645. private bool InternalAddCells(CellPos start, CellPos end)
  646. {
  647. return this.InternalAddCells(start.Row, start.Column, end.Row, end.Column);
  648. }
  649. /// <summary>
  650. /// Adds the Cells located between the specified start and end row/column indicies
  651. /// to the current selection without raising an event
  652. /// </summary>
  653. /// <param name="startRow">The row index of the start Cell</param>
  654. /// <param name="startColumn">The column index of the start Cell</param>
  655. /// <param name="endRow">The row index of the end Cell</param>
  656. /// <param name="endColumn">The column index of the end Cell</param>
  657. /// <returns>true if any Cells were added, false otherwise</returns>
  658. private bool InternalAddCells(int startRow, int startColumn, int endRow, int endColumn)
  659. {
  660. this.Normalise(ref startRow, ref endRow);
  661. this.Normalise(ref startColumn, ref endColumn);
  662. bool anyAdded = false;
  663. bool anyAddedInRow = false;
  664. for (int i=startRow; i<=endRow; i++)
  665. {
  666. if (i >= this.owner.Rows.Count)
  667. {
  668. break;
  669. }
  670. Row r = this.owner.Rows[i];
  671. for (int j=startColumn; j<=endColumn; j++)
  672. {
  673. if (j >= r.Cells.Count)
  674. {
  675. break;
  676. }
  677. if (!r.Cells[j].Selected && r.Cells[j].Enabled)
  678. {
  679. if (this.owner.Table != null && !this.owner.Table.IsCellEnabled(i, j))
  680. {
  681. continue;
  682. }
  683. r.Cells[j].SetSelected(true);
  684. r.InternalSelectedCellCount++;
  685. anyAdded = true;
  686. anyAddedInRow = true;
  687. }
  688. }
  689. if (anyAddedInRow && !this.rows.Contains(r))
  690. {
  691. this.rows.Add(r);
  692. }
  693. anyAddedInRow = false;
  694. }
  695. return anyAdded;
  696. }
  697. /// <summary>
  698. /// Adds the Cells between the last selection start Cell and the Cell at the
  699. /// specified row/column indicies to the current selection. Any Cells that are
  700. /// between the last start and end Cells that are not in the new area are
  701. /// removed from the current selection
  702. /// </summary>
  703. /// <param name="row">The row index of the shift selected Cell</param>
  704. /// <param name="column">The column index of the shift selected Cell</param>
  705. public void AddShiftSelectedCell(int row, int column)
  706. {
  707. int[] oldSelectedIndicies = this.SelectedIndicies;
  708. if (this.shiftSelectStart == CellPos.Empty)
  709. {
  710. this.shiftSelectStart = new CellPos(0, 0);
  711. }
  712. bool changed = false;
  713. if (this.shiftSelectEnd != CellPos.Empty)
  714. {
  715. changed = this.InternalRemoveCells(this.shiftSelectStart, this.shiftSelectEnd);
  716. changed |= this.InternalAddCells(this.shiftSelectStart, new CellPos(row, column));
  717. }
  718. else
  719. {
  720. changed = this.InternalAddCells(0, 0, row, column);
  721. }
  722. if (changed)
  723. {
  724. this.owner.OnSelectionChanged(new SelectionEventArgs(this.owner, oldSelectedIndicies, this.SelectedIndicies));
  725. }
  726. this.shiftSelectEnd = new CellPos(row, column);
  727. }
  728. /// <summary>
  729. /// Adds the Cells between the last selection start Cell and the Cell at the
  730. /// specified CellPas to the current selection. Any Cells that are
  731. /// between the last start and end Cells that are not in the new area are
  732. /// removed from the current selection
  733. /// </summary>
  734. /// <param name="cellPos">A CellPos that specifies the shift selected Cell</param>
  735. public void AddShiftSelectedCell(CellPos cellPos)
  736. {
  737. this.AddShiftSelectedCell(cellPos.Row, cellPos.Column);
  738. }
  739. /// <summary>
  740. /// Ensures that the first index is smaller than the second index,
  741. /// performing a swap if necessary
  742. /// </summary>
  743. /// <param name="a">The first index</param>
  744. /// <param name="b">The second index</param>
  745. private void Normalise(ref int a, ref int b)
  746. {
  747. if (a < 0)
  748. {
  749. a = 0;
  750. }
  751. if (b < 0)
  752. {
  753. b = 0;
  754. }
  755. if (b < a)
  756. {
  757. int temp = a;
  758. a = b;
  759. b = temp;
  760. }
  761. }
  762. #endregion
  763. #region Clear
  764. /// <summary>
  765. /// Removes all selected Rows and Cells from the selection
  766. /// </summary>
  767. public void Clear()
  768. {
  769. if (this.rows.Count > 0)
  770. {
  771. int[] oldSelectedIndicies = this.SelectedIndicies;
  772. this.InternalClear();
  773. this.shiftSelectStart = CellPos.Empty;
  774. this.shiftSelectEnd = CellPos.Empty;
  775. this.owner.OnSelectionChanged(new SelectionEventArgs(this.owner, oldSelectedIndicies, this.SelectedIndicies));
  776. }
  777. }
  778. /// <summary>
  779. /// Removes all selected Rows and Cells from the selection without raising an event
  780. /// </summary>
  781. private void InternalClear()
  782. {
  783. if (this.rows.Count > 0)
  784. {
  785. for (int i=0; i<this.rows.Count; i++)
  786. {
  787. ((Row) this.rows[i]).ClearSelection();
  788. }
  789. this.rows.Clear();
  790. this.rows.Capacity = 0;
  791. }
  792. }
  793. #endregion
  794. #region Remove
  795. /// <summary>
  796. /// Removes the Cell at the specified row and column indicies from the current selection
  797. /// </summary>
  798. /// <param name="row">The row index of the Cell to remove from the selection</param>
  799. /// <param name="column">The column index of the Cell to remove from the selection</param>
  800. public void RemoveCell(int row, int column)
  801. {
  802. this.RemoveCells(row, column, row, column);
  803. }
  804. /// <summary>
  805. /// Removes the Cell at the specified row and column indicies from the current selection
  806. /// </summary>
  807. /// <param name="cellPos">A CellPos that specifies the Cell to remove from the selection</param>
  808. public void RemoveCell(CellPos cellPos)
  809. {
  810. this.RemoveCell(cellPos.Row, cellPos.Column);
  811. }
  812. /// <summary>
  813. /// Removes the Cells located between the specified start and end row/column indicies
  814. /// from the current selection
  815. /// </summary>
  816. /// <param name="startRow">The row index of the start Cell</param>
  817. /// <param name="startColumn">The column index of the start Cell</param>
  818. /// <param name="endRow">The row index of the end Cell</param>
  819. /// <param name="endColumn">The column index of the end Cell</param>
  820. public void RemoveCells(int startRow, int startColumn, int endRow, int endColumn)
  821. {
  822. if (this.rows.Count > 0)
  823. {
  824. int[] oldSelectedIndicies = this.SelectedIndicies;
  825. if (this.InternalRemoveCells(startRow, startColumn, endRow, endColumn))
  826. {
  827. this.owner.OnSelectionChanged(new SelectionEventArgs(this.owner, oldSelectedIndicies, this.SelectedIndicies));
  828. }
  829. this.shiftSelectStart = new CellPos(startRow, startColumn);
  830. this.shiftSelectEnd = new CellPos(endRow, endColumn);
  831. }
  832. }
  833. /// <summary>
  834. /// Removes the Cells located between the specified start and end CellPos from the
  835. /// current selection
  836. /// </summary>
  837. /// <param name="start">A CellPos that specifies the start Cell</param>
  838. /// <param name="end">A CellPos that specifies the end Cell</param>
  839. public void RemoveCells(CellPos start, CellPos end)
  840. {
  841. this.RemoveCells(start.Row, start.Column, end.Row, end.Column);
  842. }
  843. /// <summary>
  844. /// Removes the Cells located between the specified start and end CellPos from the
  845. /// current selection without raising an event
  846. /// </summary>
  847. /// <param name="start">A CellPos that specifies the start Cell</param>
  848. /// <param name="end">A CellPos that specifies the end Cell</param>
  849. /// <returns>true if any Cells were added, false otherwise</returns>
  850. private bool InternalRemoveCells(CellPos start, CellPos end)
  851. {
  852. return this.InternalRemoveCells(start.Row, start.Column, end.Row, end.Column);
  853. }
  854. /// <summary>
  855. /// Removes the Cells located between the specified start and end row/column indicies
  856. /// from the current selection without raising an event
  857. /// </summary>
  858. /// <param name="startRow">The row index of the start Cell</param>
  859. /// <param name="startColumn">The column index of the start Cell</param>
  860. /// <param name="endRow">The row index of the end Cell</param>
  861. /// <param name="endColumn">The column index of the end Cell</param>
  862. /// <returns>true if any Cells were added, false otherwise</returns>
  863. private bool InternalRemoveCells(int startRow, int startColumn, int endRow, int endColumn)
  864. {
  865. this.Normalise(ref startRow, ref endRow);
  866. this.Normalise(ref startColumn, ref endColumn);
  867. bool anyRemoved = false;
  868. for (int i=startRow; i<=endRow; i++)
  869. {
  870. if (i >= this.owner.Rows.Count)
  871. {
  872. break;
  873. }
  874. Row r = this.owner.Rows[i];
  875. for (int j=startColumn; j<=endColumn; j++)
  876. {
  877. if (j >= r.Cells.Count)
  878. {
  879. break;
  880. }
  881. if (r.Cells[j].Selected)
  882. {
  883. r.Cells[j].SetSelected(false);
  884. r.InternalSelectedCellCount--;
  885. anyRemoved = true;
  886. }
  887. }
  888. if (!r.AnyCellsSelected)
  889. {
  890. if (this.rows.Contains(r))
  891. {
  892. this.rows.Remove(r);
  893. }
  894. }
  895. }
  896. return anyRemoved;
  897. }
  898. /// <summary>
  899. /// Removes the specified Row from the selection
  900. /// </summary>
  901. /// <param name="row">The Row to be removed from the selection</param>
  902. internal void RemoveRow(Row row)
  903. {
  904. if (this.rows.Contains(row))
  905. {
  906. int[] oldSelectedIndicies = this.SelectedIndicies;
  907. this.rows.Remove(row);
  908. this.owner.OnSelectionChanged(new SelectionEventArgs(this.owner, oldSelectedIndicies, this.SelectedIndicies));
  909. }
  910. }
  911. #endregion
  912. #region Queries
  913. /// <summary>
  914. /// Returns whether the Cell at the specified row and column indicies is
  915. /// currently selected
  916. /// </summary>
  917. /// <param name="row">The row index of the specified Cell</param>
  918. /// <param name="column">The column index of the specified Cell</param>
  919. /// <returns>true if the Cell at the specified row and column indicies is
  920. /// selected, false otherwise</returns>
  921. public bool IsCellSelected(int row, int column)
  922. {
  923. if (row < 0 || row >= this.owner.Rows.Count)
  924. {
  925. return false;
  926. }
  927. return this.owner.Rows[row].IsCellSelected(column);
  928. }
  929. /// <summary>
  930. /// Returns whether the Cell at the specified CellPos is currently selected
  931. /// </summary>
  932. /// <param name="cellPos">A CellPos the represents the row and column indicies
  933. /// of the Cell to check</param>
  934. /// <returns>true if the Cell at the specified CellPos is currently selected,
  935. /// false otherwise</returns>
  936. public bool IsCellSelected(CellPos cellPos)
  937. {
  938. return this.IsCellSelected(cellPos.Row, cellPos.Column);
  939. }
  940. /// <summary>
  941. /// Returns whether the Row at the specified index in th TableModel is
  942. /// currently selected
  943. /// </summary>
  944. /// <param name="index">The index of the Row to check</param>
  945. /// <returns>true if the Row at the specified index is currently selected,
  946. /// false otherwise</returns>
  947. public bool IsRowSelected(int index)
  948. {
  949. if (index < 0 || index >= this.owner.Rows.Count)
  950. {
  951. return false;
  952. }
  953. return this.owner.Rows[index].AnyCellsSelected;
  954. }
  955. #endregion
  956. #endregion
  957. #region Properties
  958. /// <summary>
  959. /// Gets an array that contains the currently selected Rows
  960. /// </summary>
  961. public Row[] SelectedItems
  962. {
  963. get
  964. {
  965. if (this.rows.Count == 0)
  966. {
  967. return new Row[0];
  968. }
  969. this.rows.Sort(new RowComparer());
  970. return (Row[]) this.rows.ToArray(typeof(Row));
  971. }
  972. }
  973. /// <summary>
  974. /// Gets an array that contains the indexes of the currently selected Rows
  975. /// </summary>
  976. public int[] SelectedIndicies
  977. {
  978. get
  979. {
  980. if (this.rows.Count == 0)
  981. {
  982. return new int[0];
  983. }
  984. this.rows.Sort(new RowComparer());
  985. int[] indicies = new int[this.rows.Count];
  986. for (int i=0; i<this.rows.Count; i++)
  987. {
  988. indicies[i] = ((Row) this.rows[i]).InternalIndex;
  989. }
  990. return indicies;
  991. }
  992. }
  993. /// <summary>
  994. /// Returns a Rectange that bounds the currently selected Rows
  995. /// </summary>
  996. public Rectangle SelectionBounds
  997. {
  998. get
  999. {
  1000. if (this.rows.Count == 0)
  1001. {
  1002. return Rectangle.Empty;
  1003. }
  1004. int[] indicies = this.SelectedIndicies;
  1005. return this.CalcSelectionBounds(indicies[0], indicies[indicies.Length-1]);
  1006. }
  1007. }
  1008. /// <summary>
  1009. ///
  1010. /// </summary>
  1011. /// <param name="start"></param>
  1012. /// <param name="end"></param>
  1013. /// <returns></returns>
  1014. internal Rectangle CalcSelectionBounds(int start, int end)
  1015. {
  1016. this.Normalise(ref start, ref end);
  1017. Rectangle bounds = new Rectangle();
  1018. if (this.owner.Table != null && this.owner.Table.ColumnModel != null)
  1019. {
  1020. bounds.Width = this.owner.Table.ColumnModel.VisibleColumnsWidth;
  1021. }
  1022. bounds.Y = start * this.owner.RowHeight;
  1023. if (start == end)
  1024. {
  1025. bounds.Height = this.owner.RowHeight;
  1026. }
  1027. else
  1028. {
  1029. bounds.Height = ((end + 1) * this.owner.RowHeight) - bounds.Y;
  1030. }
  1031. return bounds;
  1032. }
  1033. #endregion
  1034. }
  1035. #endregion
  1036. }
  1037. }