PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGrid.cs

https://bitbucket.org/danipen/mono
C# | 3462 lines | 2716 code | 617 blank | 129 comment | 564 complexity | 6adc470b264352468b991579f8b9903b MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005,2006 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez <jordi@ximian.com>
  24. // Chris Toshok <toshok@ximian.com>
  25. //
  26. //
  27. using System;
  28. using System.ComponentModel;
  29. using System.Data;
  30. using System.Drawing;
  31. using System.Runtime.InteropServices;
  32. using System.Collections;
  33. using System.Text;
  34. namespace System.Windows.Forms
  35. {
  36. internal class DataGridRelationshipRow {
  37. DataGrid owner;
  38. public DataGridRelationshipRow (DataGrid owner)
  39. {
  40. this.owner = owner;
  41. IsSelected = false;
  42. IsExpanded = false;
  43. height = 0;
  44. VerticalOffset = 0;
  45. RelationHeight = 0;
  46. relation_area = Rectangle.Empty;
  47. }
  48. public int height;
  49. /* this needs to be a property so that the Autosize
  50. * example from the Windows.Forms FAQ will work */
  51. public int Height {
  52. get { return height; }
  53. set {
  54. if (height != value) {
  55. height = value;
  56. owner.UpdateRowsFrom (this);
  57. }
  58. }
  59. }
  60. public bool IsSelected;
  61. public bool IsExpanded;
  62. public int VerticalOffset;
  63. public int RelationHeight;
  64. public Rectangle relation_area; /* the Y coordinate of this rectangle is updated as needed */
  65. }
  66. internal class DataGridDataSource
  67. {
  68. public DataGrid owner;
  69. public CurrencyManager list_manager;
  70. public object view;
  71. public string data_member;
  72. public object data_source;
  73. public DataGridCell current;
  74. public DataGridDataSource (DataGrid owner, CurrencyManager list_manager, object data_source, string data_member, object view_data, DataGridCell current)
  75. {
  76. this.owner = owner;
  77. this.list_manager = list_manager;
  78. this.view = view_data;
  79. this.data_source = data_source;
  80. this.data_member = data_member;
  81. this.current = current;
  82. }
  83. DataGridRelationshipRow[] rows;
  84. public DataGridRelationshipRow[] Rows {
  85. get { return rows; }
  86. set { rows = value; }
  87. }
  88. Hashtable selected_rows;
  89. public Hashtable SelectedRows {
  90. get { return selected_rows; }
  91. set { selected_rows = value; }
  92. }
  93. int selection_start;
  94. public int SelectionStart {
  95. get { return selection_start; }
  96. set { selection_start = value; }
  97. }
  98. }
  99. [DefaultEvent("Navigate")]
  100. [DefaultProperty("DataSource")]
  101. [Designer("System.Windows.Forms.Design.DataGridDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  102. [ComplexBindingProperties ("DataSource", "DataMember")]
  103. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  104. [ComVisible (true)]
  105. public class DataGrid : Control, ISupportInitialize, IDataGridEditingService
  106. {
  107. [Flags]
  108. public enum HitTestType
  109. {
  110. None = 0,
  111. Cell = 1,
  112. ColumnHeader = 2,
  113. RowHeader = 4,
  114. ColumnResize = 8,
  115. RowResize = 16,
  116. Caption = 32,
  117. ParentRows = 64
  118. }
  119. public sealed class HitTestInfo
  120. {
  121. public static readonly HitTestInfo Nowhere = null;
  122. int row;
  123. int column;
  124. DataGrid.HitTestType type;
  125. #region Private Constructors
  126. internal HitTestInfo () : this (-1, -1, HitTestType.None)
  127. {
  128. }
  129. internal HitTestInfo (int row, int column, DataGrid.HitTestType type)
  130. {
  131. this.row = row;
  132. this.column = column;
  133. this.type = type;
  134. }
  135. #endregion
  136. #region Public Instance Properties
  137. public int Column {
  138. get { return column; }
  139. }
  140. public int Row {
  141. get { return row; }
  142. }
  143. public DataGrid.HitTestType Type {
  144. get { return type; }
  145. }
  146. #endregion //Public Instance Properties
  147. public override bool Equals (object value)
  148. {
  149. if (!(value is HitTestInfo))
  150. return false;
  151. HitTestInfo obj = (HitTestInfo) value;
  152. return (obj.Column == column && obj.Row == row && obj.Type ==type);
  153. }
  154. public override int GetHashCode ()
  155. {
  156. return row ^ column;
  157. }
  158. public override string ToString ()
  159. {
  160. return "{ " + type + "," + row + "," + column + "}";
  161. }
  162. }
  163. #region Local Variables
  164. /* cached theme defaults */
  165. static readonly Color def_background_color = ThemeEngine.Current.DataGridBackgroundColor;
  166. static readonly Color def_caption_backcolor = ThemeEngine.Current.DataGridCaptionBackColor;
  167. static readonly Color def_caption_forecolor = ThemeEngine.Current.DataGridCaptionForeColor;
  168. static readonly Color def_parent_rows_backcolor = ThemeEngine.Current.DataGridParentRowsBackColor;
  169. static readonly Color def_parent_rows_forecolor = ThemeEngine.Current.DataGridParentRowsForeColor;
  170. /* colors */
  171. // XXX this needs addressing. Control.background_color should not be internal.
  172. new Color background_color;
  173. Color caption_backcolor;
  174. Color caption_forecolor;
  175. Color parent_rows_backcolor;
  176. Color parent_rows_forecolor;
  177. /* flags to determine which areas of the datagrid are shown */
  178. bool caption_visible;
  179. bool parent_rows_visible;
  180. GridTableStylesCollection styles_collection;
  181. DataGridParentRowsLabelStyle parent_rows_label_style;
  182. DataGridTableStyle default_style;
  183. DataGridTableStyle grid_style;
  184. DataGridTableStyle current_style;
  185. /* selection */
  186. DataGridCell current_cell;
  187. Hashtable selected_rows;
  188. int selection_start; // used for range selection
  189. /* layout/rendering */
  190. bool allow_navigation;
  191. int first_visible_row;
  192. int first_visible_column;
  193. int visible_row_count;
  194. int visible_column_count;
  195. Font caption_font;
  196. string caption_text;
  197. bool flatmode;
  198. HScrollBar horiz_scrollbar;
  199. VScrollBar vert_scrollbar;
  200. int horiz_pixeloffset;
  201. internal Bitmap back_button_image;
  202. internal Rectangle back_button_rect;
  203. internal bool back_button_mouseover;
  204. internal bool back_button_active;
  205. internal Bitmap parent_rows_button_image;
  206. internal Rectangle parent_rows_button_rect;
  207. internal bool parent_rows_button_mouseover;
  208. internal bool parent_rows_button_active;
  209. /* databinding */
  210. object datasource;
  211. string datamember;
  212. CurrencyManager list_manager;
  213. bool refetch_list_manager = true;
  214. bool _readonly;
  215. DataGridRelationshipRow[] rows;
  216. /* column resize fields */
  217. bool column_resize_active;
  218. int resize_column_x;
  219. int resize_column_width_delta;
  220. int resize_column;
  221. /* row resize fields */
  222. bool row_resize_active;
  223. int resize_row_y;
  224. int resize_row_height_delta;
  225. int resize_row;
  226. /* used to make sure we don't endlessly recurse calling set_CurrentCell and OnListManagerPositionChanged */
  227. bool from_positionchanged_handler;
  228. /* editing state */
  229. bool cursor_in_add_row;
  230. bool add_row_changed;
  231. internal bool is_editing; // Current cell is edit mode
  232. bool is_changing;
  233. bool commit_row_changes = true; // Whether to commit current edit or cancel it
  234. bool adding_new_row; // Used to temporary ignore the new row added by CurrencyManager.AddNew in CurrentCell
  235. internal Stack data_source_stack;
  236. internal Stack data_grid_table_style_stack;
  237. internal Stack grid_style_stack;
  238. #endregion // Local Variables
  239. #region Public Constructors
  240. public DataGrid ()
  241. {
  242. allow_navigation = true;
  243. background_color = def_background_color;
  244. border_style = BorderStyle.Fixed3D;
  245. caption_backcolor = def_caption_backcolor;
  246. caption_forecolor = def_caption_forecolor;
  247. caption_text = string.Empty;
  248. caption_visible = true;
  249. datamember = string.Empty;
  250. parent_rows_backcolor = def_parent_rows_backcolor;
  251. parent_rows_forecolor = def_parent_rows_forecolor;
  252. parent_rows_visible = true;
  253. current_cell = new DataGridCell ();
  254. parent_rows_label_style = DataGridParentRowsLabelStyle.Both;
  255. selected_rows = new Hashtable ();
  256. selection_start = -1;
  257. rows = new DataGridRelationshipRow [0];
  258. grid_style_stack = new Stack ();
  259. data_grid_table_style_stack = new Stack ();
  260. default_style = new DataGridTableStyle (true);
  261. grid_style = new DataGridTableStyle ();
  262. styles_collection = new GridTableStylesCollection (this);
  263. styles_collection.CollectionChanged += new CollectionChangeEventHandler (OnTableStylesCollectionChanged);
  264. CurrentTableStyle = grid_style;
  265. horiz_scrollbar = new ImplicitHScrollBar ();
  266. horiz_scrollbar.Scroll += new ScrollEventHandler (GridHScrolled);
  267. vert_scrollbar = new ImplicitVScrollBar ();
  268. vert_scrollbar.Scroll += new ScrollEventHandler (GridVScrolled);
  269. SetStyle (ControlStyles.UserMouse, true);
  270. data_source_stack = new Stack ();
  271. back_button_image = ResourceImageLoader.Get ("go-previous.png");
  272. back_button_image.MakeTransparent (Color.Transparent);
  273. parent_rows_button_image = ResourceImageLoader.Get ("go-top.png");
  274. parent_rows_button_image.MakeTransparent (Color.Transparent);
  275. }
  276. #endregion // Public Constructor
  277. #region Public Instance Properties
  278. [DefaultValue(true)]
  279. public bool AllowNavigation {
  280. get { return allow_navigation; }
  281. set {
  282. if (allow_navigation != value) {
  283. allow_navigation = value;
  284. OnAllowNavigationChanged (EventArgs.Empty);
  285. }
  286. }
  287. }
  288. [DefaultValue(true)]
  289. public bool AllowSorting {
  290. get { return grid_style.AllowSorting; }
  291. set { grid_style.AllowSorting = value; }
  292. }
  293. public Color AlternatingBackColor {
  294. get { return grid_style.AlternatingBackColor; }
  295. set { grid_style.AlternatingBackColor = value; }
  296. }
  297. public override Color BackColor {
  298. get { return grid_style.BackColor; }
  299. set { grid_style.BackColor = value; }
  300. }
  301. public Color BackgroundColor {
  302. get { return background_color; }
  303. set {
  304. if (background_color != value) {
  305. background_color = value;
  306. OnBackgroundColorChanged (EventArgs.Empty);
  307. Invalidate ();
  308. }
  309. }
  310. }
  311. [Browsable(false)]
  312. [EditorBrowsable(EditorBrowsableState.Never)]
  313. public override Image BackgroundImage {
  314. get { return base.BackgroundImage; }
  315. set {
  316. if (base.BackgroundImage == value)
  317. return;
  318. base.BackgroundImage = value;
  319. Invalidate ();
  320. }
  321. }
  322. [Browsable (false)]
  323. [EditorBrowsable (EditorBrowsableState.Never)]
  324. public override ImageLayout BackgroundImageLayout {
  325. get { return base.BackgroundImageLayout; }
  326. set { base.BackgroundImageLayout = value; }
  327. }
  328. [DispId(-504)]
  329. [DefaultValue(BorderStyle.Fixed3D)]
  330. public BorderStyle BorderStyle {
  331. get { return InternalBorderStyle; }
  332. set {
  333. InternalBorderStyle = value;
  334. CalcAreasAndInvalidate ();
  335. OnBorderStyleChanged (EventArgs.Empty);
  336. }
  337. }
  338. public Color CaptionBackColor {
  339. get { return caption_backcolor; }
  340. set {
  341. if (caption_backcolor != value) {
  342. caption_backcolor = value;
  343. InvalidateCaption ();
  344. }
  345. }
  346. }
  347. [Localizable(true)]
  348. [AmbientValue(null)]
  349. public Font CaptionFont {
  350. get {
  351. if (caption_font == null)
  352. return new Font (Font, FontStyle.Bold);
  353. return caption_font;
  354. }
  355. set {
  356. if (caption_font != null && caption_font.Equals (value))
  357. return;
  358. caption_font = value;
  359. CalcAreasAndInvalidate ();
  360. }
  361. }
  362. public Color CaptionForeColor {
  363. get { return caption_forecolor; }
  364. set {
  365. if (caption_forecolor != value) {
  366. caption_forecolor = value;
  367. InvalidateCaption ();
  368. }
  369. }
  370. }
  371. [Localizable(true)]
  372. [DefaultValue("")]
  373. public string CaptionText {
  374. get { return caption_text; }
  375. set {
  376. if (caption_text != value) {
  377. caption_text = value;
  378. InvalidateCaption ();
  379. }
  380. }
  381. }
  382. [DefaultValue(true)]
  383. public bool CaptionVisible {
  384. get { return caption_visible; }
  385. set {
  386. if (caption_visible != value) {
  387. EndEdit ();
  388. caption_visible = value;
  389. CalcAreasAndInvalidate ();
  390. OnCaptionVisibleChanged (EventArgs.Empty);
  391. }
  392. }
  393. }
  394. [DefaultValue(true)]
  395. public bool ColumnHeadersVisible {
  396. get { return grid_style.ColumnHeadersVisible; }
  397. set {
  398. if (grid_style.ColumnHeadersVisible != value) {
  399. grid_style.ColumnHeadersVisible = value;
  400. // UIA Framework: To keep track of header
  401. OnUIAColumnHeadersVisibleChanged ();
  402. }
  403. }
  404. }
  405. bool setting_current_cell;
  406. [Browsable(false)]
  407. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  408. public DataGridCell CurrentCell {
  409. get { return current_cell; }
  410. set {
  411. if (setting_current_cell)
  412. return;
  413. setting_current_cell = true;
  414. if (!IsHandleCreated) {
  415. setting_current_cell = false;
  416. throw new Exception ("CurrentCell cannot be set at this time.");
  417. }
  418. /* Even if we are on the same cell, we could need to actually start edition */
  419. if (current_cell.Equals (value) && is_editing) {
  420. setting_current_cell = false;
  421. return;
  422. }
  423. /* make sure the new cell fits in the correct bounds for [row,column] */
  424. if (ReadOnly && value.RowNumber > RowsCount - 1)
  425. value.RowNumber = RowsCount - 1;
  426. else if (value.RowNumber > RowsCount)
  427. value.RowNumber = RowsCount;
  428. if (value.ColumnNumber >= CurrentTableStyle.GridColumnStyles.Count)
  429. value.ColumnNumber = CurrentTableStyle.GridColumnStyles.Count == 0 ? 0 : CurrentTableStyle.GridColumnStyles.Count - 1;
  430. /* now make sure we don't go negative */
  431. if (value.RowNumber < 0) value.RowNumber = 0;
  432. if (value.ColumnNumber < 0) value.ColumnNumber = 0;
  433. bool was_changing = is_changing;
  434. add_row_changed = add_row_changed || was_changing;
  435. EndEdit ();
  436. if (value.RowNumber != current_cell.RowNumber) {
  437. if (!from_positionchanged_handler) {
  438. try {
  439. if (commit_row_changes)
  440. ListManager.EndCurrentEdit ();
  441. else
  442. ListManager.CancelCurrentEdit ();
  443. }
  444. catch (Exception e) {
  445. DialogResult r = MessageBox.Show (String.Format ("{0} Do you wish to correct the value?", e.Message),
  446. "Error when committing the row to the original data source",
  447. MessageBoxButtons.YesNo);
  448. if (r == DialogResult.Yes) {
  449. InvalidateRowHeader (value.RowNumber);
  450. InvalidateRowHeader (current_cell.RowNumber);
  451. setting_current_cell = false;
  452. Edit ();
  453. return;
  454. }
  455. else
  456. ListManager.CancelCurrentEdit ();
  457. }
  458. }
  459. if (value.RowNumber == RowsCount && !ListManager.AllowNew)
  460. value.RowNumber --;
  461. }
  462. int old_row = current_cell.RowNumber;
  463. current_cell = value;
  464. EnsureCellVisibility (value);
  465. // by default, edition in existing rows is commited, and for new ones is discarded, unless
  466. // we receive actual input data from the user
  467. if (CurrentRow == RowsCount && ListManager.AllowNew) {
  468. commit_row_changes = false;
  469. cursor_in_add_row = true;
  470. add_row_changed = false;
  471. adding_new_row = true;
  472. AddNewRow ();
  473. adding_new_row = false;
  474. }
  475. else {
  476. cursor_in_add_row = false;
  477. commit_row_changes = true;
  478. }
  479. InvalidateRowHeader (old_row);
  480. InvalidateRowHeader (current_cell.RowNumber);
  481. list_manager.Position = current_cell.RowNumber;
  482. OnCurrentCellChanged (EventArgs.Empty);
  483. if (!from_positionchanged_handler)
  484. Edit ();
  485. setting_current_cell = false;
  486. }
  487. }
  488. internal void EditRowChanged (DataGridColumnStyle column_style)
  489. {
  490. if (cursor_in_add_row) {
  491. if (!commit_row_changes) { // first change in add row, time to show another row in the ui
  492. commit_row_changes = true;
  493. RecreateDataGridRows (true);
  494. }
  495. }
  496. }
  497. int CurrentRow {
  498. get { return current_cell.RowNumber; }
  499. set { CurrentCell = new DataGridCell (value, current_cell.ColumnNumber); }
  500. }
  501. int CurrentColumn {
  502. get { return current_cell.ColumnNumber; }
  503. set { CurrentCell = new DataGridCell (current_cell.RowNumber, value); }
  504. }
  505. [Browsable(false)]
  506. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  507. public int CurrentRowIndex {
  508. get {
  509. if (ListManager == null)
  510. return -1;
  511. return CurrentRow;
  512. }
  513. set { CurrentRow = value; }
  514. }
  515. [Browsable(false)]
  516. [EditorBrowsable(EditorBrowsableState.Never)]
  517. public override Cursor Cursor {
  518. get { return base.Cursor; }
  519. set { base.Cursor = value; }
  520. }
  521. [DefaultValue(null)]
  522. [Editor ("System.Windows.Forms.Design.DataMemberListEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  523. public string DataMember {
  524. get { return datamember; }
  525. set {
  526. if (BindingContext != null) {
  527. SetDataSource (datasource, value);
  528. }
  529. else {
  530. if (list_manager != null)
  531. list_manager = null;
  532. datamember = value;
  533. refetch_list_manager = true;
  534. }
  535. }
  536. }
  537. [DefaultValue(null)]
  538. [RefreshProperties(RefreshProperties.Repaint)]
  539. [AttributeProvider (typeof (IListSource))]
  540. public object DataSource {
  541. get { return datasource; }
  542. set {
  543. if (BindingContext != null) {
  544. SetDataSource (value, ListManager == null ? datamember : string.Empty);
  545. }
  546. else {
  547. datasource = value;
  548. if (list_manager != null)
  549. datamember = string.Empty;
  550. if (list_manager != null)
  551. list_manager = null;
  552. refetch_list_manager = true;
  553. }
  554. }
  555. }
  556. protected override Size DefaultSize {
  557. get { return new Size (130, 80); }
  558. }
  559. [Browsable(false)]
  560. public int FirstVisibleColumn {
  561. get { return first_visible_column; }
  562. }
  563. [DefaultValue(false)]
  564. public bool FlatMode {
  565. get { return flatmode; }
  566. set {
  567. if (flatmode != value) {
  568. flatmode = value;
  569. OnFlatModeChanged (EventArgs.Empty);
  570. Refresh ();
  571. }
  572. }
  573. }
  574. public override Color ForeColor {
  575. get { return grid_style.ForeColor; }
  576. set { grid_style.ForeColor = value; }
  577. }
  578. public Color GridLineColor {
  579. get { return grid_style.GridLineColor; }
  580. set {
  581. if (value == Color.Empty)
  582. throw new ArgumentException ("Color.Empty value is invalid.");
  583. grid_style.GridLineColor = value;
  584. }
  585. }
  586. [DefaultValue(DataGridLineStyle.Solid)]
  587. public DataGridLineStyle GridLineStyle {
  588. get { return grid_style.GridLineStyle; }
  589. set { grid_style.GridLineStyle = value; }
  590. }
  591. public Color HeaderBackColor {
  592. get { return grid_style.HeaderBackColor; }
  593. set {
  594. if (value == Color.Empty)
  595. throw new ArgumentException ("Color.Empty value is invalid.");
  596. grid_style.HeaderBackColor = value;
  597. }
  598. }
  599. public Font HeaderFont {
  600. get { return grid_style.HeaderFont; }
  601. set { grid_style.HeaderFont = value; }
  602. }
  603. public Color HeaderForeColor {
  604. get { return grid_style.HeaderForeColor; }
  605. set { grid_style.HeaderForeColor = value; }
  606. }
  607. protected ScrollBar HorizScrollBar {
  608. get { return horiz_scrollbar; }
  609. }
  610. internal ScrollBar HScrollBar {
  611. get { return horiz_scrollbar; }
  612. }
  613. internal int HorizPixelOffset {
  614. get { return horiz_pixeloffset; }
  615. }
  616. internal bool IsChanging {
  617. get { return is_changing; }
  618. }
  619. public object this [DataGridCell cell] {
  620. get { return this [cell.RowNumber, cell.ColumnNumber]; }
  621. set { this [cell.RowNumber, cell.ColumnNumber] = value; }
  622. }
  623. public object this [int rowIndex, int columnIndex] {
  624. get { return CurrentTableStyle.GridColumnStyles[columnIndex].GetColumnValueAtRow (ListManager,
  625. rowIndex); }
  626. set {
  627. CurrentTableStyle.GridColumnStyles[columnIndex].SetColumnValueAtRow (ListManager,
  628. rowIndex, value);
  629. // UIA Framework: Raising changes in datasource.
  630. OnUIAGridCellChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh,
  631. new DataGridCell (rowIndex,
  632. columnIndex)));
  633. }
  634. }
  635. public Color LinkColor {
  636. get { return grid_style.LinkColor; }
  637. set { grid_style.LinkColor = value; }
  638. }
  639. internal Font LinkFont {
  640. get { return new Font (Font, FontStyle.Underline); }
  641. }
  642. [Browsable(false)]
  643. [EditorBrowsable(EditorBrowsableState.Never)]
  644. public Color LinkHoverColor {
  645. get { return grid_style.LinkHoverColor; }
  646. set { grid_style.LinkHoverColor = value; }
  647. }
  648. [Browsable(false)]
  649. [EditorBrowsable(EditorBrowsableState.Advanced)]
  650. protected internal CurrencyManager ListManager {
  651. get {
  652. if (list_manager == null && refetch_list_manager) {
  653. SetDataSource (datasource, datamember);
  654. refetch_list_manager = false;
  655. }
  656. return list_manager;
  657. }
  658. set { throw new NotSupportedException ("Operation is not supported."); }
  659. }
  660. public Color ParentRowsBackColor {
  661. get { return parent_rows_backcolor; }
  662. set {
  663. if (parent_rows_backcolor != value) {
  664. parent_rows_backcolor = value;
  665. if (parent_rows_visible) {
  666. Refresh ();
  667. }
  668. }
  669. }
  670. }
  671. public Color ParentRowsForeColor {
  672. get { return parent_rows_forecolor; }
  673. set {
  674. if (parent_rows_forecolor != value) {
  675. parent_rows_forecolor = value;
  676. if (parent_rows_visible) {
  677. Refresh ();
  678. }
  679. }
  680. }
  681. }
  682. [DefaultValue(DataGridParentRowsLabelStyle.Both)]
  683. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  684. public DataGridParentRowsLabelStyle ParentRowsLabelStyle {
  685. get { return parent_rows_label_style; }
  686. set {
  687. if (parent_rows_label_style != value) {
  688. parent_rows_label_style = value;
  689. if (parent_rows_visible) {
  690. Refresh ();
  691. }
  692. OnParentRowsLabelStyleChanged (EventArgs.Empty);
  693. }
  694. }
  695. }
  696. [DefaultValue(true)]
  697. public bool ParentRowsVisible {
  698. get { return parent_rows_visible; }
  699. set {
  700. if (parent_rows_visible != value) {
  701. parent_rows_visible = value;
  702. CalcAreasAndInvalidate ();
  703. OnParentRowsVisibleChanged (EventArgs.Empty);
  704. }
  705. }
  706. }
  707. // Settting this property seems to have no effect.
  708. [DefaultValue(75)]
  709. [TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
  710. public int PreferredColumnWidth {
  711. get { return grid_style.PreferredColumnWidth; }
  712. set { grid_style.PreferredColumnWidth = value; }
  713. }
  714. public int PreferredRowHeight {
  715. get { return grid_style.PreferredRowHeight; }
  716. set { grid_style.PreferredRowHeight = value; }
  717. }
  718. [DefaultValue(false)]
  719. public bool ReadOnly {
  720. get { return _readonly; }
  721. set {
  722. if (_readonly != value) {
  723. _readonly = value;
  724. OnReadOnlyChanged (EventArgs.Empty);
  725. CalcAreasAndInvalidate ();
  726. }
  727. }
  728. }
  729. [DefaultValue(true)]
  730. public bool RowHeadersVisible {
  731. get { return grid_style.RowHeadersVisible; }
  732. set { grid_style.RowHeadersVisible = value; }
  733. }
  734. [DefaultValue(35)]
  735. public int RowHeaderWidth {
  736. get { return grid_style.RowHeaderWidth; }
  737. set { grid_style.RowHeaderWidth = value; }
  738. }
  739. internal DataGridRelationshipRow[] DataGridRows {
  740. get { return rows; }
  741. }
  742. public Color SelectionBackColor {
  743. get { return grid_style.SelectionBackColor; }
  744. set { grid_style.SelectionBackColor = value; }
  745. }
  746. public Color SelectionForeColor {
  747. get { return grid_style.SelectionForeColor; }
  748. set { grid_style.SelectionForeColor = value; }
  749. }
  750. public override ISite Site {
  751. get { return base.Site; }
  752. set { base.Site = value; }
  753. }
  754. [Localizable(true)]
  755. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  756. public GridTableStylesCollection TableStyles {
  757. get { return styles_collection; }
  758. }
  759. [Bindable(false)]
  760. [Browsable(false)]
  761. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  762. [EditorBrowsable(EditorBrowsableState.Never)]
  763. public override string Text {
  764. get { return base.Text; }
  765. set { base.Text = value; }
  766. }
  767. [Browsable(false)]
  768. [EditorBrowsable(EditorBrowsableState.Advanced)]
  769. protected ScrollBar VertScrollBar {
  770. get { return vert_scrollbar; }
  771. }
  772. internal ScrollBar VScrollBar {
  773. get { return vert_scrollbar; }
  774. }
  775. [Browsable(false)]
  776. public int VisibleColumnCount {
  777. get { return visible_column_count; }
  778. }
  779. [Browsable(false)]
  780. public int VisibleRowCount {
  781. get { return visible_row_count; }
  782. }
  783. #endregion // Public Instance Properties
  784. #region Private Instance Properties
  785. internal DataGridTableStyle CurrentTableStyle {
  786. get { return current_style; }
  787. set {
  788. if (current_style != value) {
  789. if (current_style != null)
  790. DisconnectTableStyleEvents ();
  791. current_style = value;
  792. if (current_style != null) {
  793. current_style.DataGrid = this;
  794. ConnectTableStyleEvents ();
  795. }
  796. CalcAreasAndInvalidate ();
  797. }
  798. }
  799. }
  800. internal int FirstVisibleRow {
  801. get { return first_visible_row; }
  802. }
  803. // As opposed to VisibleRowCount, this value is the maximum
  804. // *possible* number of visible rows given our area.
  805. internal int MaxVisibleRowCount {
  806. get {
  807. return cells_area.Height / RowHeight;
  808. }
  809. }
  810. internal int RowsCount {
  811. get { return ListManager != null ? ListManager.Count : 0; }
  812. }
  813. internal int RowHeight {
  814. get {
  815. if (CurrentTableStyle.CurrentPreferredRowHeight > Font.Height + 3 + 1 /* line */)
  816. return CurrentTableStyle.CurrentPreferredRowHeight;
  817. else
  818. return Font.Height + 3 + 1 /* line */;
  819. }
  820. }
  821. internal override bool ScaleChildrenInternal {
  822. get { return false; }
  823. }
  824. internal bool ShowEditRow {
  825. get {
  826. if (ListManager != null && !ListManager.AllowNew)
  827. return false;
  828. return !_readonly;
  829. }
  830. }
  831. internal bool ShowParentRows {
  832. get { return ParentRowsVisible && data_source_stack.Count > 0; }
  833. }
  834. #endregion Private Instance Properties
  835. #region Public Instance Methods
  836. void AbortEditing ()
  837. {
  838. if (is_changing) {
  839. CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].Abort (current_cell.RowNumber);
  840. is_changing = false;
  841. InvalidateRowHeader (current_cell.RowNumber);
  842. }
  843. }
  844. public bool BeginEdit (DataGridColumnStyle gridColumn, int rowNumber)
  845. {
  846. if (is_changing)
  847. return false;
  848. int column = CurrentTableStyle.GridColumnStyles.IndexOf (gridColumn);
  849. if (column < 0)
  850. return false;
  851. CurrentCell = new DataGridCell (rowNumber, column);
  852. /* force editing of CurrentCell if we aren't already editing */
  853. Edit ();
  854. return true;
  855. }
  856. public void BeginInit ()
  857. {
  858. }
  859. protected virtual void CancelEditing ()
  860. {
  861. if (CurrentTableStyle.GridColumnStyles.Count == 0)
  862. return;
  863. CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].ConcedeFocus ();
  864. if (is_changing) {
  865. if (current_cell.ColumnNumber < CurrentTableStyle.GridColumnStyles.Count)
  866. CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].Abort (current_cell.RowNumber);
  867. InvalidateRowHeader (current_cell.RowNumber);
  868. }
  869. if (cursor_in_add_row && !is_changing) {
  870. ListManager.CancelCurrentEdit ();
  871. }
  872. is_changing = false;
  873. is_editing = false;
  874. }
  875. public void Collapse (int row)
  876. {
  877. if (!rows[row].IsExpanded)
  878. return;
  879. SuspendLayout ();
  880. rows[row].IsExpanded = false;
  881. for (int i = 1; i < rows.Length - row; i ++)
  882. rows[row + i].VerticalOffset -= rows[row].RelationHeight;
  883. rows[row].height -= rows[row].RelationHeight;
  884. rows[row].RelationHeight = 0;
  885. ResumeLayout (false);
  886. /* XX need to redraw from @row down */
  887. CalcAreasAndInvalidate ();
  888. }
  889. protected internal virtual void ColumnStartedEditing (Control editingControl)
  890. {
  891. ColumnStartedEditing (editingControl.Bounds);
  892. }
  893. protected internal virtual void ColumnStartedEditing (Rectangle bounds)
  894. {
  895. bool need_invalidate = is_changing == false;
  896. // XXX calculate the row header to invalidate
  897. // instead of using CurrentRow
  898. is_changing = true;
  899. if (cursor_in_add_row && need_invalidate)
  900. RecreateDataGridRows (true);
  901. if (need_invalidate)
  902. InvalidateRowHeader (CurrentRow);
  903. }
  904. protected override AccessibleObject CreateAccessibilityInstance ()
  905. {
  906. return base.CreateAccessibilityInstance ();
  907. }
  908. protected virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop)
  909. {
  910. return CreateGridColumn (prop, false);
  911. }
  912. [MonoTODO ("Not implemented, will throw NotImplementedException")]
  913. protected virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop, bool isDefault)
  914. {
  915. throw new NotImplementedException();
  916. }
  917. protected override void Dispose (bool disposing)
  918. {
  919. base.Dispose (disposing);
  920. }
  921. public bool EndEdit (DataGridColumnStyle gridColumn, int rowNumber, bool shouldAbort)
  922. {
  923. if (shouldAbort || (_readonly || gridColumn.TableStyleReadOnly || gridColumn.ReadOnly))
  924. gridColumn.Abort (rowNumber);
  925. else {
  926. gridColumn.Commit (ListManager, rowNumber);
  927. gridColumn.ConcedeFocus ();
  928. }
  929. if (is_editing || is_changing) {
  930. is_editing = false;
  931. is_changing = false;
  932. InvalidateRowHeader (rowNumber);
  933. }
  934. return true;
  935. }
  936. public void EndInit ()
  937. {
  938. if (grid_style != null)
  939. grid_style.DataGrid = this;
  940. }
  941. public void Expand (int row)
  942. {
  943. if (rows[row].IsExpanded)
  944. return;
  945. rows[row].IsExpanded = true;
  946. int i;
  947. string[] relations = CurrentTableStyle.Relations;
  948. StringBuilder relation_builder = new StringBuilder ("");
  949. for (i = 0; i < relations.Length; i ++) {
  950. if (i > 0)
  951. relation_builder.Append ("\n");
  952. relation_builder.Append (relations[i]);
  953. }
  954. string relation_text = relation_builder.ToString ();
  955. SizeF measured_area = TextRenderer.MeasureString (relation_text, LinkFont);
  956. rows[row].relation_area = new Rectangle (cells_area.X + 1,
  957. 0, /* updated as needed at the usage sites for relation_area */
  958. (int)measured_area.Width + 4,
  959. Font.Height * relations.Length);
  960. for (i = 1; i < rows.Length - row; i ++)
  961. rows[row + i].VerticalOffset += rows[row].relation_area.Height;
  962. rows[row].height += rows[row].relation_area.Height;
  963. rows[row].RelationHeight = rows[row].relation_area.Height;
  964. /* XX need to redraw from @row down */
  965. CalcAreasAndInvalidate ();
  966. }
  967. public Rectangle GetCellBounds (DataGridCell dgc)
  968. {
  969. return GetCellBounds (dgc.RowNumber, dgc.ColumnNumber);
  970. }
  971. public Rectangle GetCellBounds (int row, int col)
  972. {
  973. Rectangle bounds = new Rectangle ();
  974. int col_pixel;
  975. bounds.Width = CurrentTableStyle.GridColumnStyles[col].Width;
  976. bounds.Height = rows[row].Height - rows[row].RelationHeight;
  977. bounds.Y = cells_area.Y + rows[row].VerticalOffset - rows[FirstVisibleRow].VerticalOffset;
  978. col_pixel = GetColumnStartingPixel (col);
  979. bounds.X = cells_area.X + col_pixel - horiz_pixeloffset;
  980. return bounds;
  981. }
  982. public Rectangle GetCurrentCellBounds ()
  983. {
  984. return GetCellBounds (current_cell.RowNumber, current_cell.ColumnNumber);
  985. }
  986. protected virtual string GetOutputTextDelimiter ()
  987. {
  988. return string.Empty;
  989. }
  990. protected virtual void GridHScrolled (object sender, ScrollEventArgs se)
  991. {
  992. if (se.NewValue == horiz_pixeloffset ||
  993. se.Type == ScrollEventType.EndScroll) {
  994. return;
  995. }
  996. ScrollToColumnInPixels (se.NewValue);
  997. }
  998. protected virtual void GridVScrolled (object sender, ScrollEventArgs se)
  999. {
  1000. int old_first_visible_row = first_visible_row;
  1001. first_visible_row = se.NewValue;
  1002. if (first_visible_row == old_first_visible_row)
  1003. return;
  1004. UpdateVisibleRowCount ();
  1005. if (first_visible_row == old_first_visible_row)
  1006. return;
  1007. ScrollToRow (old_first_visible_row, first_visible_row);
  1008. }
  1009. public HitTestInfo HitTest (Point position)
  1010. {
  1011. return HitTest (position.X, position.Y);
  1012. }
  1013. const int RESIZE_HANDLE_HORIZ_SIZE = 5;
  1014. const int RESIZE_HANDLE_VERT_SIZE = 3;
  1015. // From Point to Cell
  1016. public HitTestInfo HitTest (int x, int y)
  1017. {
  1018. if (column_headers_area.Contains (x, y)) {
  1019. int offset_x = x + horiz_pixeloffset;
  1020. int column_x;
  1021. int column_under_mouse = FromPixelToColumn (offset_x, out column_x);
  1022. if (column_under_mouse == -1)
  1023. return new HitTestInfo (-1, -1, HitTestType.None);
  1024. if ((column_x + CurrentTableStyle.GridColumnStyles[column_under_mouse].Width - offset_x < RESIZE_HANDLE_HORIZ_SIZE)
  1025. && column_under_mouse < CurrentTableStyle.GridColumnStyles.Count) {
  1026. return new HitTestInfo (-1, column_under_mouse, HitTestType.ColumnResize);
  1027. }
  1028. else {
  1029. return new HitTestInfo (-1, column_under_mouse, HitTestType.ColumnHeader);
  1030. }
  1031. }
  1032. if (row_headers_area.Contains (x, y)) {
  1033. int posy;
  1034. int rcnt = FirstVisibleRow + VisibleRowCount;
  1035. for (int r = FirstVisibleRow; r < rcnt; r++) {
  1036. posy = cells_area.Y + rows[r].VerticalOffset - rows[FirstVisibleRow].VerticalOffset;
  1037. if (y <= posy + rows[r].Height) {
  1038. if ((posy + rows[r].Height) - y < RESIZE_HANDLE_VERT_SIZE) {
  1039. return new HitTestInfo (r, -1, HitTestType.RowResize);
  1040. }
  1041. else {
  1042. return new HitTestInfo (r, -1, HitTestType.RowHeader);
  1043. }
  1044. }
  1045. }
  1046. }
  1047. if (caption_area.Contains (x, y)) {
  1048. return new HitTestInfo (-1, -1, HitTestType.Caption);
  1049. }
  1050. if (parent_rows.Contains (x, y)) {
  1051. return new HitTestInfo (-1, -1, HitTestType.ParentRows);
  1052. }
  1053. int pos_y, pos_x, width;
  1054. int rowcnt = FirstVisibleRow + VisibleRowCount;
  1055. for (int row = FirstVisibleRow; row < rowcnt; row++) {
  1056. pos_y = cells_area.Y + rows[row].VerticalOffset - rows[FirstVisibleRow].VerticalOffset;
  1057. if (y <= pos_y + rows[row].Height) {
  1058. int col_pixel;
  1059. int column_cnt = first_visible_column + visible_column_count;
  1060. if (column_cnt > 0) {
  1061. for (int column = first_visible_column; column < column_cnt; column++) {
  1062. if (CurrentTableStyle.GridColumnStyles[column].bound == false)
  1063. continue;
  1064. col_pixel = GetColumnStartingPixel (column);
  1065. pos_x = cells_area.X + col_pixel - horiz_pixeloffset;
  1066. width = CurrentTableStyle.GridColumnStyles[column].Width;
  1067. if (x <= pos_x + width) { // Column found
  1068. return new HitTestInfo (row, column, HitTestType.Cell);
  1069. }
  1070. }
  1071. }
  1072. else if (CurrentTableStyle.HasRelations) {
  1073. /* XXX this needs checking against MS somehow... */
  1074. if (x < rows[row].relation_area.X + rows[row].relation_area.Width)
  1075. return new HitTestInfo (row, 0/*XXX?*/, HitTestType.Cell);
  1076. }
  1077. break;
  1078. }
  1079. }
  1080. return new HitTestInfo ();
  1081. }
  1082. public bool IsExpanded (int rowNumber)
  1083. {
  1084. return (rows[rowNumber].IsExpanded);
  1085. }
  1086. public bool IsSelected (int row)
  1087. {
  1088. return rows[row].IsSelected;
  1089. }
  1090. public void NavigateBack ()
  1091. {
  1092. if (data_source_stack.Count == 0)
  1093. return;
  1094. EndEdit ();
  1095. DataGridDataSource source = (DataGridDataSource)data_source_stack.Pop ();
  1096. CurrentTableStyle= (DataGridTableStyle)data_grid_table_style_stack.Pop ();
  1097. grid_style = (DataGridTableStyle) grid_style_stack.Pop ();
  1098. list_manager = source.list_manager;
  1099. rows = source.Rows;
  1100. selected_rows = source.SelectedRows;
  1101. selection_start = source.SelectionStart;
  1102. SetDataSource (source.data_source, source.data_member);
  1103. CurrentCell = source.current;
  1104. }
  1105. public void NavigateTo (int rowNumber, string relationName)
  1106. {
  1107. if (allow_navigation == false)
  1108. return;
  1109. EndEdit ();
  1110. DataGridDataSource previous_source = new DataGridDataSource (this, list_manager, datasource, datamember, list_manager.Current, CurrentCell);
  1111. previous_source.Rows = rows;
  1112. previous_source.SelectedRows = selected_rows;
  1113. previous_source.SelectionStart = selection_start;
  1114. data_source_stack.Push (previous_source);
  1115. data_grid_table_style_stack.Push (CurrentTableStyle);
  1116. grid_style_stack.Push (grid_style);
  1117. grid_style = new DataGridTableStyle ();
  1118. CurrentTableStyle = grid_style;
  1119. rows = null;
  1120. selected_rows = new Hashtable ();
  1121. selection_start = -1;
  1122. DataMember = String.Format ("{0}.{1}", DataMember, relationName);
  1123. OnDataSourceChanged (EventArgs.Empty);
  1124. }
  1125. protected virtual void OnAllowNavigationChanged (EventArgs e)
  1126. {
  1127. EventHandler eh = (EventHandler)(Events [AllowNavigationChangedEvent]);
  1128. if (eh != null)
  1129. eh (this, e);
  1130. }
  1131. protected void OnBackButtonClicked (object sender, EventArgs e)
  1132. {
  1133. EventHandler eh = (EventHandler)(Events [BackButtonClickEvent]);
  1134. if (eh != null)
  1135. eh (this, e);
  1136. }
  1137. protected override void OnBackColorChanged (EventArgs e)
  1138. {
  1139. base.OnBackColorChanged (e);
  1140. }
  1141. protected virtual void OnBackgroundColorChanged (EventArgs e)
  1142. {
  1143. EventHandler eh = (EventHandler)(Events [BackgroundColorChangedEvent]);
  1144. if (eh != null)
  1145. eh (this, e);
  1146. }
  1147. protected override void OnBindingContextChanged (EventArgs e)
  1148. {
  1149. base.OnBindingContextChanged (e);
  1150. SetDataSource (datasource, datamember);
  1151. }
  1152. protected virtual void OnBorderStyleChanged (EventArgs e)
  1153. {
  1154. EventHandler eh = (EventHandler)(Events [BorderStyleChangedEvent]);
  1155. if (eh != null)
  1156. eh (this, e);
  1157. }
  1158. protected virtual void OnCaptionVisibleChanged (EventArgs e)
  1159. {
  1160. EventHandler eh = (EventHandler)(Events [CaptionVisibleChangedEvent]);
  1161. if (eh != null)
  1162. eh (this, e);
  1163. }
  1164. protected virtual void OnCurrentCellChanged (EventArgs e)
  1165. {
  1166. EventHandler eh = (EventHandler)(Events [CurrentCellChangedEvent]);
  1167. if (eh != null)
  1168. eh (this, e);
  1169. }
  1170. protected virtual void OnDataSourceChanged (EventArgs e)
  1171. {
  1172. EventHandler eh = (EventHandler)(Events [DataSourceChangedEvent]);
  1173. if (eh != null)
  1174. eh (this, e);
  1175. }
  1176. protected override void OnEnter (EventArgs e)
  1177. {
  1178. base.OnEnter (e);
  1179. Edit ();
  1180. }
  1181. protected virtual void OnFlatModeChanged (EventArgs e)
  1182. {
  1183. EventHandler eh = (EventHandler)(Events [FlatModeChangedEvent]);
  1184. if (eh != null)
  1185. eh (this, e);
  1186. }
  1187. protected override void OnFontChanged (EventArgs e)
  1188. {
  1189. CalcGridAreas ();
  1190. base.OnFontChanged (e);
  1191. }
  1192. protected override void OnForeColorChanged (EventArgs e)
  1193. {
  1194. base.OnForeColorChanged (e);
  1195. }
  1196. protected override void OnHandleCreated (EventArgs e)
  1197. {
  1198. base.OnHandleCreated (e);
  1199. SetDataSource (datasource, datamember);
  1200. }
  1201. protected override void OnHandleDestroyed (EventArgs e)
  1202. {
  1203. base.OnHandleDestroyed (e);
  1204. }
  1205. // It seems we have repeated code with ProcessKeyPreview, specifically
  1206. // the call to ProcessGridKey. In practice it seems this event is *never* fired
  1207. // since the key events are handled by the current column's textbox.
  1208. // We are keeping commented anyway, in case we need to actually call it.
  1209. protected override void OnKeyDown (KeyEventArgs ke)
  1210. {
  1211. base.OnKeyDown (ke);
  1212. /*if (ProcessGridKey (ke) == true)
  1213. ke.Handled = true;
  1214. // TODO: we probably don't need this check,
  1215. // since current_cell wouldn't have been set
  1216. // to something invalid
  1217. if (CurrentTableStyle.GridColumnStyles.Count > 0) {
  1218. CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].OnKeyDown
  1219. (ke, current_cell.RowNumber, current_cell.ColumnNumber);
  1220. }*/
  1221. }
  1222. protected override void OnKeyPress (KeyPressEventArgs kpe)
  1223. {
  1224. base.OnKeyPress (kpe);
  1225. }
  1226. protected override void OnLayout (LayoutEventArgs levent)
  1227. {
  1228. base.OnLayout (levent);
  1229. CalcAreasAndInvalidate ();
  1230. }
  1231. protected override void OnLeave (EventArgs e)
  1232. {
  1233. base.OnLeave (e);
  1234. EndEdit ();
  1235. if (commit_row_changes)
  1236. ListManager.EndCurrentEdit ();
  1237. else
  1238. ListManager.CancelCurrentEdit ();
  1239. }
  1240. protected override void OnMouseDown (MouseEventArgs e)
  1241. {
  1242. base.OnMouseDown (e);
  1243. bool ctrl_pressed = ((Control.ModifierKeys & Keys.Control) != 0);
  1244. bool shift_pressed = ((Control.ModifierKeys & Keys.Shift) != 0);
  1245. HitTestInfo testinfo;
  1246. testinfo = HitTest (e.X, e.Y);
  1247. switch (testinfo.Type) {
  1248. case HitTestType.Cell:
  1249. if (testinfo.Row < 0 || testinfo.Column < 0)
  1250. break;
  1251. if (rows[testinfo.Row].IsExpanded) {
  1252. Rectangle relation_area = rows[testinfo.Row].relation_area;
  1253. relation_area.Y = rows[testinfo.Row].VerticalOffset + cells_area.Y + rows[testinfo.Row].Height - rows[testinfo.Row].RelationHeight;
  1254. if (relation_area.Contains (e.X, e.Y)) {
  1255. /* the click happened in the relation area, navigate to the new table */
  1256. int relative = e.Y - relation_area.Y;
  1257. NavigateTo (testinfo.Row, CurrentTableStyle.Relations[relative / LinkFont.Height]);
  1258. return;
  1259. }
  1260. }
  1261. DataGridCell new_cell = new DataGridCell (testinfo.Row, testinfo.Column);
  1262. if ((new_cell.Equals (current_cell) == false) || (!is_editing)) {
  1263. ResetSelection ();
  1264. CurrentCell = new_cell;
  1265. Edit ();
  1266. } else {
  1267. CurrentTableStyle.GridColumnStyles[testinfo.Column].OnMouseDown (e, testinfo.Row, testinfo.Column);
  1268. }
  1269. break;
  1270. case HitTestType.RowHeader:
  1271. bool expansion_click = false;
  1272. if (CurrentTableStyle.HasRelations) {
  1273. if (e.X > row_headers_area.X + row_headers_area.Width / 2) {
  1274. /* it's in the +/- space */
  1275. if (IsExpanded (testinfo.Row))
  1276. Collapse (testinfo.Row);
  1277. else
  1278. Expand (testinfo.Row);
  1279. expansion_click = true;
  1280. }
  1281. }
  1282. CancelEditing ();
  1283. CurrentRow = testinfo.Row;
  1284. if (!ctrl_pressed && !shift_pressed && !expansion_click) {
  1285. ResetSelection (); // Invalidates selected rows
  1286. }
  1287. if ((shift_pressed || expansion_click) && selection_start != -1) {
  1288. ShiftSelection (testinfo.Row);
  1289. } else { // ctrl_pressed or single item
  1290. selection_start = testinfo.Row;
  1291. Select (testinfo.Row);
  1292. }
  1293. OnRowHeaderClick (EventArgs.Empty);
  1294. break;
  1295. case HitTestType.ColumnHeader:
  1296. if (CurrentTableStyle.GridColumnStyles.Count == 0)
  1297. break;
  1298. if (AllowSorting == false)
  1299. break;
  1300. if (ListManager.List is IBindingList == false)
  1301. break;
  1302. // Don't do any sort if we are empty, as .net does
  1303. if (ListManager.Count == 0)
  1304. return;
  1305. ListSortDirection direction = ListSortDirection.Ascending;
  1306. PropertyDescriptor prop = CurrentTableStyle.GridColumnStyles[testinfo.Column].PropertyDescriptor;
  1307. IBindingList list = (IBindingList) ListManager.List;
  1308. if (list.SortProperty != null) {
  1309. CurrentTableStyle.GridColumnStyles[list.SortProperty].ArrowDrawingMode
  1310. = DataGridColumnStyle.ArrowDrawing.No;
  1311. }
  1312. if (prop == list.SortProperty && list.SortDirection == ListSortDirection.Ascending) {
  1313. direction = ListSortDirection.Descending;
  1314. }
  1315. CurrentTableStyle.GridColumnStyles[testinfo.Column].ArrowDrawingMode =
  1316. direction == ListSortDirection.Ascending ?
  1317. DataGridColumnStyle.ArrowDrawing.Ascending : DataGridColumnStyle.ArrowDrawing.Descending;
  1318. list.ApplySort (prop, direction);
  1319. Refresh ();
  1320. if (this.is_editing)
  1321. //CurrentTableStyle.GridColumnStyles[CurrentColumn].UpdateUI ();
  1322. this.InvalidateColumn (CurrentTableStyle.GridColumnStyles[CurrentColumn]);
  1323. break;
  1324. case HitTestType.ColumnResize:
  1325. if (e.Clicks == 2) {
  1326. EndEdit ();
  1327. ColumnResize (testinfo.Column);
  1328. } else {
  1329. resize_column = testinfo.Column;
  1330. column_resize_active = true;
  1331. resize_column_x = e.X;
  1332. resize_column_width_delta = 0;
  1333. EndEdit ();
  1334. DrawResizeLineVert (resize_column_x);
  1335. }
  1336. break;
  1337. case HitTestType.RowResize:
  1338. if (e.Clicks == 2) {
  1339. EndEdit ();
  1340. RowResize (testinfo.Row);
  1341. } else {
  1342. resize_row = testinfo.Row;
  1343. row_resize_active = true;
  1344. resize_row_y = e.Y;
  1345. resize_row_height_delta = 0;
  1346. EndEdit ();
  1347. DrawResizeLineHoriz (resize_row_y);
  1348. }
  1349. break;
  1350. case HitTestType.Caption:
  1351. if (back_button_rect.Contains (e.X, e.Y)) {
  1352. back_button_active = true;
  1353. Invalidate (back_button_rect);
  1354. }
  1355. if (parent_rows_button_rect.Contains (e.X, e.Y)) {
  1356. parent_rows_button_active = true;
  1357. Invalidate (parent_rows_button_rect);
  1358. }
  1359. break;
  1360. default:
  1361. break;
  1362. }
  1363. }
  1364. protected override void OnMouseLeave (EventArgs e)
  1365. {
  1366. base.OnMouseLeave (e);
  1367. }
  1368. protected override void OnMouseMove (MouseEventArgs e)
  1369. {
  1370. base.OnMouseMove (e);
  1371. if (column_resize_active) {
  1372. /* erase the old line */
  1373. DrawResizeLineVert (resize_column_x + resize_column_width_delta);
  1374. resize_column_width_delta = e.X - resize_column_x;
  1375. /* draw the new line */
  1376. DrawResizeLineVert (resize_column_x + resize_column_width_delta);
  1377. return;
  1378. }
  1379. else if (row_resize_active) {
  1380. /* erase the old line */
  1381. DrawResizeLineHoriz (resize_row_y + resize_row_height_delta);
  1382. resize_row_height_delta = e.Y - resize_row_y;
  1383. /* draw the new line */
  1384. DrawResizeLineHoriz (resize_row_y + resize_row_height_delta);
  1385. return;
  1386. }
  1387. else {
  1388. /* determine the cursor to use */
  1389. HitTestInfo testinfo;
  1390. testinfo = HitTest (e.X, e.Y);
  1391. switch (testinfo.Type) {
  1392. case HitTestType.ColumnResize:
  1393. Cursor = Cursors.VSplit;
  1394. break;
  1395. case HitTestType.RowResize:
  1396. Cursor = Cursors.HSplit;
  1397. break;
  1398. case HitTestType.Caption:
  1399. Cursor = Cursors.Default;
  1400. if (back_button_rect.Contains (e.X, e.Y)) {
  1401. if (!back_button_mouseover)
  1402. Invalidate (back_button_rect);
  1403. back_button_mouseover = true;
  1404. } else if (back_button_mouseover) {
  1405. Invalidate (back_button_rect);
  1406. back_button_mouseover = false;
  1407. }
  1408. if (parent_rows_button_rect.Contains (e.X, e.Y)) {
  1409. if (parent_rows_button_mouseover)
  1410. Invalidate (parent_rows_button_rect);
  1411. parent_rows_button_mouseover = true;
  1412. } else if (parent_rows_button_mouseover) {
  1413. Invalidate (parent_rows_button_rect);
  1414. parent_rows_button_mouseover = false;
  1415. }
  1416. break;
  1417. case HitTestType.Cell:
  1418. if (rows[testinfo.Row].IsExpanded) {
  1419. Rectangle relation_area = rows[testinfo.Row].relation_area;
  1420. relation_area.Y = rows[testinfo.Row].VerticalOffset + cells_area.Y + rows[testinfo.Row].Height - rows[testinfo.Row].RelationHeight;
  1421. if (relation_area.Contains (e.X, e.Y)) {
  1422. Cursor = Cursors.Hand;
  1423. break;
  1424. }
  1425. }
  1426. Cursor = Cursors.Default;
  1427. break;
  1428. case HitTestType.RowHeader:
  1429. if (e.Button == MouseButtons.Left)
  1430. ShiftSelection (testinfo.Row);
  1431. Cursor = Cursors.Default;
  1432. break;
  1433. default:
  1434. Cursor = Cursors.Default;
  1435. break;
  1436. }
  1437. }
  1438. }
  1439. protected override void OnMouseUp (MouseEventArgs e)
  1440. {
  1441. base.OnMouseUp (e);
  1442. if (column_resize_active) {
  1443. column_resize_active = false;
  1444. if (resize_column_width_delta + CurrentTableStyle.GridColumnStyles[resize_column].Width < 0)
  1445. resize_column_width_delta = -CurrentTableStyle.GridColumnStyles[resize_column].Width;
  1446. CurrentTableStyle.GridColumnStyles[resize_column].Width += resize_column_width_delta;
  1447. width_of_all_columns += resize_column_width_delta;
  1448. Edit ();
  1449. Invalidate ();
  1450. } else if (row_resize_active) {
  1451. row_resize_active = false;
  1452. if (resize_row_height_delta + rows[resize_row].Height < 0)
  1453. resize_row_height_delta = -rows[resize_row].Height;
  1454. rows[resize_row].height = rows[resize_row].Height + resize_row_height_delta;
  1455. for (int i = resize_row + 1; i < rows.Length; i ++)
  1456. rows[i].VerticalOffset += resize_row_height_delta;
  1457. Edit ();
  1458. CalcAreasAndInvalidate ();
  1459. } else if (back_button_active) {
  1460. if (back_button_rect.Contains (e.X, e.Y)) {
  1461. Invalidate (back_button_rect);
  1462. NavigateBack ();
  1463. OnBackButtonClicked (this, EventArgs.Empty);
  1464. }
  1465. back_button_active = false;
  1466. } else if (parent_rows_button_active) {
  1467. if (parent_rows_button_rect.Contains (e.X, e.Y)) {
  1468. Invalidate (parent_rows_button_rect);
  1469. ParentRowsVisible = !ParentRowsVisible;
  1470. OnShowParentDetailsButtonClicked (this, EventArgs.Empty);
  1471. }
  1472. parent_rows_button_active = false;
  1473. }
  1474. }
  1475. protected override void OnMouseWheel (MouseEventArgs e)
  1476. {
  1477. base.OnMouseWheel (e);
  1478. bool ctrl_pressed = ((Control.ModifierKeys & Keys.Control) != 0);
  1479. int pixels;
  1480. if (ctrl_pressed) { // scroll horizontally
  1481. if (!horiz_scrollbar.Visible)
  1482. return;
  1483. if (e.Delta > 0) {
  1484. /* left */
  1485. pixels = Math.Max (horiz_scrollbar.Minimum,
  1486. horiz_scrollbar.Value - horiz_scrollbar.LargeChange);
  1487. } else {
  1488. /* right */
  1489. pixels = Math.Min (horiz_scrollbar.Maximum - horiz_scrollbar.LargeChange + 1,
  1490. horiz_scrollbar.Value + horiz_scrollbar.LargeChange);
  1491. }
  1492. GridHScrolled (this, new ScrollEventArgs (ScrollEventType.ThumbPosition, pixels));
  1493. horiz_scrollbar.Value = pixels;
  1494. } else {
  1495. if (!vert_scrollbar.Visible)
  1496. return;
  1497. if (e.Delta > 0) {
  1498. /* up */
  1499. pixels = Math.Max (vert_scrollbar.Minimum,
  1500. vert_scrollbar.Value - vert_scrollbar.LargeChange);
  1501. } else {
  1502. /* down */
  1503. pixels = Math.Min (vert_scrollbar.Maximum - vert_scrollbar.LargeChange + 1,
  1504. vert_scrollbar.Value + vert_scrollbar.LargeChange);
  1505. }
  1506. GridVScrolled (this, new ScrollEventArgs (ScrollEventType.ThumbPosition, pixels));
  1507. vert_scrollbar.Value = pixels;
  1508. }
  1509. }
  1510. protected void OnNavigate (NavigateEventArgs e)
  1511. {
  1512. EventHandler eh = (EventHandler)(Events [NavigateEvent]);
  1513. if (eh != null)
  1514. eh (this, e);
  1515. }
  1516. protected override void OnPaint (PaintEventArgs pe)
  1517. {
  1518. ThemeEngine.Current.DataGridPaint (pe, this);
  1519. }
  1520. protected override void OnPaintBackground (PaintEventArgs ebe)
  1521. {
  1522. }
  1523. protected virtual void OnParentRowsLabelStyleChanged (EventArgs e)
  1524. {
  1525. EventHandler eh = (EventHandler)(Events [ParentRowsLabelStyleChangedEvent]);
  1526. if (eh != null)
  1527. eh (this, e);
  1528. }
  1529. protected virtual void OnParentRowsVisibleChanged (EventArgs e)
  1530. {
  1531. EventHandler eh = (EventHandler)(Events [ParentRowsVisibleChangedEvent]);
  1532. if (eh != null)
  1533. eh (this, e);
  1534. }
  1535. protected virtual void OnReadOnlyChanged (EventArgs e)
  1536. {
  1537. EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEve

Large files files are truncated, but you can click here to view the full file