imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGrid.cs
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 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Jordi Mas i Hernandez <jordi@ximian.com>
24 //
25 //
26
27 // NOT COMPLETE
28
29
30 using System;
31 using System.ComponentModel;
32 using System.Drawing;
33 using System.Runtime.InteropServices;
34 using System.Collections;
35
36 namespace System.Windows.Forms
37 {
38         [DefaultEvent("Navigate")]
39         [DefaultProperty("DataSource")]
40         [Designer("System.Windows.Forms.Design.DataGridDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
41         public class DataGrid : Control, ISupportInitialize, IDataGridEditingService
42         {
43                 [Flags]
44                 [Serializable]
45                 public enum HitTestType
46                 {
47                         None            = 0,
48                         Cell            = 1,
49                         ColumnHeader    = 2,
50                         RowHeader       = 4,
51                         ColumnResize    = 8,
52                         RowResize       = 16,
53                         Caption         = 32,
54                         ParentRows      = 64
55                 }
56
57                 public sealed class HitTestInfo
58                 {
59                         public static readonly HitTestInfo Nowhere = null;
60
61                         #region Local Variables
62                         internal int column;
63                         internal int row;
64                         internal HitTestType type;
65                         #endregion // Local Variables
66
67                         #region Private Constructors
68                         internal HitTestInfo ()
69                         {
70                                 column = 0;
71                                 row = 0;
72                                 type =  HitTestType.None;
73                         }
74                         #endregion
75
76
77                         #region Public Instance Properties
78                         public int Column {
79                                 get { return column; }
80                         }
81
82                         public int Row {
83                                 get { return row; }
84                         }
85                         public DataGrid.HitTestType Type {
86                                 get { return type; }
87                         }
88                         #endregion //Public Instance Properties
89
90                         public override bool Equals (object o)
91                         {
92                                 if (!(o is HitTestInfo))
93                                         return false;
94
95                                 HitTestInfo obj = (HitTestInfo) o;
96                                 return (obj.Column == column && obj.Row == row && obj.Type ==type);
97                         }
98
99                         public override int GetHashCode ()
100                         {
101                                 return row ^ column;
102                         }
103
104                         public override string ToString ()
105                         {
106                                 return base.ToString ();
107                         }
108
109                 }
110
111                 #region Local Variables
112                 private static readonly Color   def_alternating_backcolor = ThemeEngine.Current.DataGridAlternatingBackColor;
113                 private static readonly Color   def_background_color = ThemeEngine.Current.DataGridBackgroundColor;
114                 private static readonly Color   def_caption_backcolor = ThemeEngine.Current.DataGridCaptionBackColor;
115                 private static readonly Color   def_caption_forecolor = ThemeEngine.Current.DataGridCaptionForeColor;
116                 private static readonly Color   def_gridline_color = ThemeEngine.Current.DataGridGridLineColor;
117                 private static readonly Color   def_header_backcolor = ThemeEngine.Current.DataGridHeaderBackColor;
118                 private static readonly Font    def_header_font = ThemeEngine.Current.DefaultFont;
119                 private static readonly Color   def_header_forecolor = ThemeEngine.Current.DataGridHeaderForeColor;
120                 private static readonly Color   def_link_hovercolor = ThemeEngine.Current.DataGridLinkHoverColor;
121                 private static readonly Color   def_parentrowsback_color = ThemeEngine.Current.DataGridParentRowsBackColor;
122                 private static readonly Color   def_parentrowsfore_color = ThemeEngine.Current.DataGridParentRowsForeColor;
123                 private static readonly Color   def_selection_backcolor = ThemeEngine.Current.DataGridSelectionBackColor;
124                 private static readonly Color   def_selection_forecolor = ThemeEngine.Current.DataGridSelectionForeColor;
125                 private static readonly Color   def_link_color = ThemeEngine.Current.DataGridLinkColor;
126                 internal readonly int def_preferredrow_height;
127
128                 private bool allow_navigation;
129                 private bool allow_sorting;
130                 private Color alternating_backcolor;
131                 private Color background_color;
132                 internal BorderStyle border_style;
133                 private Color caption_backcolor;
134                 private Font caption_font;
135                 private Color caption_forecolor;
136                 private string caption_text;
137                 internal bool caption_visible;
138                 internal bool columnheaders_visible;
139                 private object datasource;
140                 private object real_datasource;
141                 private string datamember;
142                 private int firstvisible_column;
143                 private bool flatmode;
144                 private Color gridline_color;
145                 private DataGridLineStyle gridline_style;
146                 private Color header_backcolor;
147                 private Color header_forecolor;
148                 private Font header_font;
149                 private Color link_color;
150                 private Color link_hovercolor;
151                 private Color parentrowsback_color;
152                 private Color parentrowsfore_color;
153                 internal bool parentrows_visible;
154                 private int preferredcolumn_width;
155                 private int preferredrow_height;
156                 private bool _readonly;
157                 internal bool rowheaders_visible;
158                 private Color selection_backcolor;
159                 private Color selection_forecolor;
160                 private int rowheaders_width;
161                 internal int visiblecolumn_count;
162                 internal int visiblerow_count;
163                 internal int first_visiblecolumn;
164                 private GridTableStylesCollection styles_collection;
165                 private DataGridParentRowsLabelStyle parentrowslabel_style;
166                 internal DataGridCell current_cell;
167                 private Color forecolor;
168                 private Color backcolor;
169                 private DataGridTableStyle default_style;
170                 private DataGridTableStyle current_style;
171                 internal HScrollBar horiz_scrollbar;
172                 internal VScrollBar vert_scrollbar;
173                 internal DataGridDrawing grid_drawing;
174                 internal int first_visiblerow;
175                 internal int horz_pixeloffset;
176                 internal bool is_editing;       // Current cell is edit mode
177                 internal bool is_changing;      // Indicates if current cell is been changed (in edit mode)
178                 internal bool is_adding;        // Indicates when we are adding a row
179                 private Hashtable selected_rows;
180                 private bool ctrl_pressed;
181                 private bool shift_pressed;
182                 private bool begininit;
183                 private CurrencyManager cached_currencymgr;
184                 #endregion // Local Variables
185
186                 #region Public Constructors
187                 public DataGrid ()
188                 {
189                         grid_drawing = new DataGridDrawing (this);
190                         allow_navigation = true;
191                         allow_sorting = true;
192                         begininit = false;
193                         alternating_backcolor = def_alternating_backcolor;
194                         background_color = def_background_color;
195                         border_style = BorderStyle.Fixed3D;
196                         caption_backcolor = def_caption_backcolor;
197                         caption_font = null;
198                         caption_forecolor = def_caption_forecolor;
199                         caption_text = string.Empty;
200                         caption_visible = true;
201                         columnheaders_visible = true;
202                         datasource = null;
203                         real_datasource = null;
204                         datamember = string.Empty;
205                         firstvisible_column = 0;
206                         flatmode = false;
207                         gridline_color = def_gridline_color;
208                         gridline_style = DataGridLineStyle.Solid;
209                         header_backcolor = def_header_backcolor;
210                         header_forecolor = def_header_forecolor;
211                         header_font = def_header_font;
212                         link_color = def_link_color;
213                         link_hovercolor = def_link_hovercolor;
214                         parentrowsback_color = def_parentrowsback_color;
215                         parentrowsfore_color = def_parentrowsfore_color;
216                         parentrows_visible = false; // should be true (temp)
217                         preferredcolumn_width = ThemeEngine.Current.DataGridPreferredColumnWidth;
218                         _readonly = false;
219                         rowheaders_visible = true;
220                         selection_backcolor = def_selection_backcolor;
221                         selection_forecolor = def_selection_forecolor;
222                         rowheaders_width = 35;
223                         visiblecolumn_count = 0;
224                         visiblerow_count = 0;
225                         current_cell = new DataGridCell ();
226                         first_visiblerow = 0;
227                         first_visiblecolumn = 0;
228                         horz_pixeloffset = 0;
229                         is_editing = false;
230                         is_changing = false;
231                         is_adding = false;
232                         forecolor = SystemColors.WindowText;
233                         parentrowslabel_style = DataGridParentRowsLabelStyle.Both;
234                         backcolor = SystemColors.Window;
235                         selected_rows = new Hashtable ();
236                         ctrl_pressed = false;
237                         shift_pressed = false;
238                         preferredrow_height = def_preferredrow_height = FontHeight + 3;
239                         cached_currencymgr = null;
240
241                         default_style = new DataGridTableStyle (true);
242                         styles_collection = new GridTableStylesCollection (this);
243                         styles_collection.CollectionChanged += new CollectionChangeEventHandler (OnTableStylesCollectionChanged);
244
245                         CurrentTableStyle = default_style;
246
247                         horiz_scrollbar = new HScrollBar ();
248                         horiz_scrollbar.Scroll += new ScrollEventHandler  (GridHScrolled);
249                         vert_scrollbar = new VScrollBar ();
250                         vert_scrollbar.Scroll += new ScrollEventHandler (GridVScrolled);                        
251                         KeyUp += new KeyEventHandler (OnKeyUpDG);                       
252
253                         SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
254
255                 }
256
257                 #endregion      // Public Constructor
258
259                 #region Public Instance Properties
260
261                 [DefaultValue(true)]
262                 public bool AllowNavigation {
263                         get {
264                                 return allow_navigation;
265                         }
266
267                         set {
268                                 if (allow_navigation != value) {
269                                         allow_navigation = value;
270                                         OnAllowNavigationChanged (EventArgs.Empty);
271                                 }
272                         }
273                 }
274
275                 [DefaultValue(true)]
276                 public bool AllowSorting {
277                         get {
278                                 return allow_sorting;
279                         }
280
281                         set {
282                                 if (allow_sorting != value) {
283                                         allow_sorting = value;
284                                 }
285                         }
286                 }
287
288                 public Color AlternatingBackColor  {
289                         get {
290                                 return alternating_backcolor;
291                         }
292
293                         set {
294                                 if (alternating_backcolor != value) {
295                                         alternating_backcolor = value;
296                                         Refresh ();
297                                 }
298                         }
299                 }
300
301                 public Color BackColor {
302                         get {
303                                 return backcolor;
304                         }
305                         set {
306                                 if (backcolor != value) {
307                                         backcolor = value;
308                                         Refresh ();
309                                 }
310                         }
311                 }
312
313                 public Color BackgroundColor {
314                         get {
315                                 return background_color;
316                         }
317                         set {
318                                  if (background_color != value) {
319                                         background_color = value;
320                                         OnBackgroundColorChanged (EventArgs.Empty);
321                                         Refresh ();
322                                 }
323                         }
324                 }
325
326                 [Browsable(false)]
327                 [EditorBrowsable(EditorBrowsableState.Never)]
328                 public Image BackgroundImage {
329                         get {
330                                 return base.BackgroundImage;
331                         }
332
333                         set {
334                                 base.BackgroundImage = value;
335                         }
336                 }
337
338                 [DispId(-504)]
339                 [DefaultValue(BorderStyle.Fixed3D)]
340                 public BorderStyle BorderStyle {
341                         get {
342                                 return border_style;
343                         }
344
345                         set {
346                                  if (border_style != value) {
347                                         border_style = value;
348                                         CalcAreasAndInvalidate ();
349                                         OnBorderStyleChanged (EventArgs.Empty);                                 
350                                 }
351                         }
352                 }
353
354                 public Color CaptionBackColor {
355                         get {
356                                 return caption_backcolor;
357                         }
358
359                         set {
360                                 if (caption_backcolor != value) {
361                                         caption_backcolor = value;
362                                         grid_drawing.InvalidateCaption ();
363                                 }
364                         }
365                 }
366
367                 [Localizable(true)]
368                 [AmbientValue(null)]
369                 public Font CaptionFont {
370                         get {
371                                 if (caption_font == null) {
372                                         return Font;
373                                 }
374
375                                 return caption_font;
376                         }
377
378                         set {
379                                 if (caption_font != null && caption_font.Equals (value)) {
380                                         return;
381                                 }
382
383                                 caption_font = value;
384                                 CalcAreasAndInvalidate ();
385                         }
386                 }
387
388                 public Color CaptionForeColor {
389                         get {
390                                 return caption_forecolor;
391                         }
392
393                         set {
394                                 if (caption_forecolor != value) {
395                                         caption_forecolor = value;
396                                         grid_drawing.InvalidateCaption ();
397                                 }
398                         }
399                 }
400
401                 [Localizable(true)]
402                 [DefaultValue("")]
403                 public string CaptionText {
404                         get {
405                                 return caption_text;
406                         }
407
408                         set {
409                                 if (caption_text != value) {
410                                         caption_text = value;
411                                         grid_drawing.InvalidateCaption ();
412                                 }
413                         }
414                 }
415
416                 [DefaultValue(true)]
417                 public bool CaptionVisible {
418                         get {
419                                 return caption_visible;
420                         }
421
422                         set {
423                                 if (caption_visible != value) {
424                                         caption_visible = value;
425                                         CalcAreasAndInvalidate ();
426                                         OnCaptionVisibleChanged (EventArgs.Empty);
427                                 }
428                         }
429                 }
430
431                 [DefaultValue(true)]
432                 public bool ColumnHeadersVisible {
433                         get {
434                                 return columnheaders_visible;
435                         }
436
437                         set {
438                                 if (columnheaders_visible != value) {
439                                         columnheaders_visible = value;
440                                         CalcAreasAndInvalidate ();
441                                 }
442                         }
443                 }
444
445                 [Browsable(false)]
446                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
447                 public DataGridCell CurrentCell {
448                         get {
449                                 return current_cell;
450                         }
451
452                         set {
453                                 if (!current_cell.Equals (value)) {
454                                         CancelEditing ();
455                                         EnsureCellVisilibility (value);
456                                         current_cell = value;
457                                         OnCurrentCellChanged (EventArgs.Empty);
458                                 }
459                         }
460                 }
461
462                 [Browsable(false)]
463                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
464                 public int CurrentRowIndex {
465                         get {
466                                 return current_cell.RowNumber;
467                         }
468
469                         set {
470                                 if (current_cell.RowNumber != value) {
471                                         CurrentCell = new DataGridCell (value, current_cell.ColumnNumber);
472                                 }
473                         }
474                 }
475
476                 [Browsable(false)]
477                 [EditorBrowsable(EditorBrowsableState.Never)]
478                 public override Cursor Cursor {
479                         get {
480                                 return base.Cursor;
481                         }
482                         set {
483                                 base.Cursor = value;
484                         }
485                 }
486
487                 [DefaultValue(null)]
488                 [Editor ("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
489                 public string DataMember {
490                         get { return datamember; }
491                         set {
492                                 if (SetDataMember (value)) {                                    
493                                         
494                                         if (styles_collection.Contains (value) == true) {
495                                                 CurrentTableStyle = styles_collection[value];
496                                                 current_style.CreateColumnsForTable (true);
497                                         } else {
498                                                 CurrentTableStyle = default_style;
499                                                 current_style.GridColumnStyles.Clear ();
500                                                 current_style.CreateColumnsForTable (false);
501                                         }                                       
502                                 }
503                         }
504                 }
505
506                 [DefaultValue(null)]
507                 [RefreshProperties(RefreshProperties.Repaint)]
508                 [TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
509                 public object DataSource {
510                         get {
511                                 return datasource;
512                         }
513
514                         set {
515                                 if (SetDataSource (value)) {
516                                         SetNewDataSource ();                                    
517                                 }
518                         }
519                 }
520
521                 protected override Size DefaultSize {
522                         get {
523                                 return new Size (130, 80);
524                         }
525                 }
526
527                 [Browsable(false)]
528                 public int FirstVisibleColumn {
529                         get {
530                                 return firstvisible_column;
531                         }
532                 }
533
534                 [DefaultValue(false)]
535                 public bool FlatMode {
536                         get {
537                                 return flatmode;
538                         }
539
540                         set {
541                                 if (flatmode != value) {
542                                         flatmode = value;
543                                         OnFlatModeChanged (EventArgs.Empty);
544                                         Refresh ();
545                                 }
546                         }
547                 }
548
549                 public Color ForeColor {
550                         get {
551                                 return forecolor;
552                         }
553
554                         set {
555                                 if (forecolor != value) {
556                                         forecolor = value;
557                                         OnForeColorChanged (EventArgs.Empty);
558                                         Refresh ();
559                                 }
560                         }
561                 }
562
563                 public Color GridLineColor {
564                         get {
565                                 return gridline_color;
566                         }
567
568                         set {
569                                 if (value == Color.Empty) {
570                                         throw new ArgumentNullException ("Color.Empty value is invalid.");
571                                 }
572
573                                 if (gridline_color != value) {
574                                         gridline_color = value;
575                                         Refresh ();
576                                 }
577                         }
578                 }
579
580                 [DefaultValue(DataGridLineStyle.Solid)]
581                 public DataGridLineStyle GridLineStyle {
582                         get {
583                                 return gridline_style;
584                         }
585
586                         set {
587                                 if (gridline_style != value) {
588                                         gridline_style = value;
589                                         Refresh ();
590                                 }
591                         }
592                 }
593
594                 public Color HeaderBackColor {
595                         get {
596                                 return header_backcolor;
597                         }
598
599                         set {
600                                 if (value == Color.Empty) {
601                                         throw new ArgumentNullException ("Color.Empty value is invalid.");
602                                 }
603
604                                 if (header_backcolor != value) {
605                                         header_backcolor = value;
606                                         Refresh ();
607                                 }
608                         }
609                 }
610
611                 public Font HeaderFont {
612                         get {
613                                 return header_font;
614                         }
615
616                         set {
617                                 if (header_font != null && !header_font.Equals (value)) {
618                                         header_font = value;
619                                         CalcAreasAndInvalidate ();
620                                 }
621                         }
622                 }
623
624                 public Color HeaderForeColor {
625                         get {
626                                 return header_forecolor;
627                         }
628
629                         set {
630                                 if (header_forecolor != value) {
631                                         header_forecolor = value;
632                                         Refresh ();
633                                 }
634                         }
635                 }
636
637                 protected ScrollBar HorizScrollBar {
638                         get {
639                                 return horiz_scrollbar;
640                         }
641                 }
642
643                 public object this [DataGridCell cell] {
644                         get  {
645                                 return this [cell.RowNumber, cell.ColumnNumber];
646                         }
647
648                         set {
649                                 this [cell.RowNumber, cell.ColumnNumber] = value;
650                         }
651                 }
652
653                 public object this [int rowIndex, int columnIndex] {
654                         get  {
655                                 return CurrentTableStyle.GridColumnStyles[columnIndex].GetColumnValueAtRow (ListManager,
656                                         rowIndex);
657                         }
658
659                         set {
660                                 CurrentTableStyle.GridColumnStyles[columnIndex].SetColumnValueAtRow (ListManager,
661                                         rowIndex, value);
662                         }
663                 }
664
665                 public Color LinkColor {
666                         get {
667                                 return link_color;
668                         }
669                         set {
670                                 if (link_color != value) {
671                                         link_color = value;
672                                         Refresh ();
673                                 }
674                         }
675                 }
676
677                 [ComVisible(false)]
678                 [Browsable(false)]
679                 [EditorBrowsable(EditorBrowsableState.Never)]
680                 public Color LinkHoverColor {
681                         get {
682                                 return link_hovercolor;
683                         }
684
685                         set {
686                                 if (link_hovercolor != value) {
687                                         link_hovercolor = value;
688                                         Refresh ();
689                                 }
690                         }
691                 }
692
693                 [Browsable(false)]
694                 [EditorBrowsable(EditorBrowsableState.Advanced)]
695                 protected internal CurrencyManager ListManager {
696                         get {
697                                 if (BindingContext == null || DataSource  == null) {
698                                         return null;
699                                 }
700
701                                 if (cached_currencymgr != null) {
702                                         return cached_currencymgr;
703                                 }
704
705                                 cached_currencymgr = (CurrencyManager) BindingContext [real_datasource, DataMember];
706                                 return cached_currencymgr;
707                         }
708
709                         set {
710                                 throw new NotSupportedException ("Operation is not supported.");
711                         }
712                 }
713
714                 public Color ParentRowsBackColor {
715                         get {
716                                 return parentrowsback_color;
717                         }
718
719                         set {
720                                 if (parentrowsback_color != value) {
721                                         parentrowsback_color = value;
722                                         if (parentrows_visible) {
723                                                 Refresh ();
724                                         }
725                                 }
726                         }
727                 }
728
729                 public Color ParentRowsForeColor {
730                         get {
731                                 return parentrowsfore_color;
732                         }
733
734                         set {
735                                 if (parentrowsfore_color != value) {
736                                         parentrowsfore_color = value;
737                                         if (parentrows_visible) {
738                                                 Refresh ();
739                                         }
740                                 }
741                         }
742                 }
743
744                 [DefaultValue(DataGridParentRowsLabelStyle.Both)]
745                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
746                 public DataGridParentRowsLabelStyle ParentRowsLabelStyle {
747                         get {
748                                 return parentrowslabel_style;
749                         }
750
751                         set {
752                                 if (parentrowslabel_style != value) {
753                                         parentrowslabel_style = value;
754                                         if (parentrows_visible) {
755                                                 Refresh ();
756                                         }
757
758                                         OnParentRowsLabelStyleChanged (EventArgs.Empty);
759                                 }
760                         }
761                 }
762
763                 [DefaultValue(true)]
764                 public bool ParentRowsVisible {
765                         get {
766                                 return parentrows_visible;
767                         }
768
769                         set {
770                                 if (parentrows_visible != value) {
771                                         parentrows_visible = value;
772                                         CalcAreasAndInvalidate ();
773                                         OnParentRowsVisibleChanged (EventArgs.Empty);
774                                 }
775                         }
776                 }
777
778                 // Settting this property seems to have no effect.
779                 [DefaultValue(75)]
780                 [TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
781                 public int PreferredColumnWidth {
782                         get {
783                                 return preferredcolumn_width;
784                         }
785
786                         set {
787                                 if (value < 0) {
788                                         throw new ArgumentException ("PreferredColumnWidth is less than 0");
789                                 }
790
791                                 if (preferredcolumn_width != value) {
792                                         preferredcolumn_width = value;
793                                         Refresh ();
794                                 }
795                         }
796                 }
797
798                 public int PreferredRowHeight {
799                         get {
800                                 return preferredrow_height;
801                         }
802
803                         set {
804                                 if (preferredrow_height != value) {
805                                         preferredrow_height = value;
806                                         CalcAreasAndInvalidate ();
807                                 }
808                         }
809                 }
810
811                 [DefaultValue(false)]
812                 public bool ReadOnly {
813                         get {
814                                 return _readonly;
815                         }
816
817                         set {
818                                 if (_readonly != value) {
819                                         _readonly = value;
820                                         OnReadOnlyChanged (EventArgs.Empty);
821                                         CalcAreasAndInvalidate ();
822                                 }
823                         }
824                 }
825
826                 [DefaultValue(true)]
827                 public bool RowHeadersVisible {
828                         get {
829                                 return rowheaders_visible;
830                         }
831
832                         set {
833                                 if (rowheaders_visible != value) {
834                                         rowheaders_visible = value;
835                                         CalcAreasAndInvalidate ();
836                                 }
837                         }
838                 }
839
840                 [DefaultValue(35)]
841                 public int RowHeaderWidth {
842                         get {
843                                 return rowheaders_width;
844                         }
845
846                         set {
847                                 if (rowheaders_width != value) {
848                                         rowheaders_width = value;
849                                         CalcAreasAndInvalidate ();
850                                 }
851                         }
852                 }
853
854                 public Color SelectionBackColor {
855                         get {
856                                 return selection_backcolor;
857                         }
858
859                         set {
860                                 if (selection_backcolor != value) {
861                                         selection_backcolor = value;
862                                         Refresh ();
863                                 }
864                         }
865                 }
866
867                 public Color SelectionForeColor  {
868                         get {
869                                 return selection_forecolor;
870                         }
871
872                         set {
873                                 if (selection_forecolor != value) {
874                                         selection_forecolor = value;
875                                         Refresh ();
876                                 }
877                         }
878                 }
879
880                 public override ISite Site {
881                         get {
882                                 return base.Site;
883                         }
884                         set {
885                                 base.Site = value;
886                         }
887                 }
888
889                 [Localizable(true)]
890                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
891                 public GridTableStylesCollection TableStyles {
892                         get { return styles_collection; }
893                 }
894
895                 [Bindable(false)]
896                 [Browsable(false)]
897                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
898                 [EditorBrowsable(EditorBrowsableState.Never)]
899                 public override string Text {
900                         get {
901                                 return base.Text;
902                         }
903                         set {
904                                 base.Text = value;
905                         }
906                 }
907
908                 [Browsable(false)]
909                 [EditorBrowsable(EditorBrowsableState.Advanced)]
910                 protected ScrollBar VertScrollBar {
911                         get {
912                                 return vert_scrollbar;
913                         }
914                 }
915
916                 [Browsable(false)]
917                 public int VisibleColumnCount {
918                         get {
919                                 return visiblecolumn_count;
920                         }
921                 }
922
923                 // Calculated at DataGridDrawing.CalcRowsHeaders
924                 [Browsable(false)]
925                 public int VisibleRowCount {
926                         get {
927                                 return visiblerow_count;
928                         }
929                 }
930
931                 #endregion      // Public Instance Properties
932
933                 #region Private Instance Properties
934                 internal DataGridTableStyle CurrentTableStyle {
935                         get {
936                                 return current_style;
937                         }
938                         set {
939                                 current_style = value;
940                                 current_style.DataGrid = this;
941                                 CalcAreasAndInvalidate ();
942                         }
943                 }
944
945                 internal int FirstVisibleRow {
946                         get { return first_visiblerow; }
947                         set { first_visiblerow = value;}
948                 }
949
950                 internal int RowsCount {
951                         get {                           
952                                 if (ListManager != null) {
953                                         return ListManager.Count;                                       
954                                 }
955
956                                 return 0;
957                         }
958                 }
959
960                 internal int RowHeight {
961                         get {
962                                 if (CurrentTableStyle.CurrentPreferredRowHeight > Font.Height + 3 + 1 /* line */) {
963                                         return CurrentTableStyle.CurrentPreferredRowHeight;
964
965                                 } else {
966                                         return Font.Height + 3 + 1 /* line */;
967                                 }
968                         }
969                 }
970                 
971                 internal bool ShowEditRow {
972                         get {
973                                 return _readonly == false;
974                         }
975                 }
976
977                 #endregion Private Instance Properties
978
979                 #region Public Instance Methods
980
981                 [MonoTODO]
982                 public virtual bool BeginEdit (DataGridColumnStyle gridColumn, int rowNumber)
983                 {
984                         return false;
985                 }
986
987                 public virtual void BeginInit ()
988                 {
989                         begininit = true;
990                 }
991
992                 protected virtual void CancelEditing ()
993                 {                       
994                         CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].Abort (current_cell.RowNumber);
995                         
996                         if (is_adding == true) {
997                                 ListManager.RemoveAt (RowsCount - 1);
998                                 is_adding = false;
999                         }
1000                         
1001                         is_editing = false;
1002                         is_changing = false;
1003                         InvalidateCurrentRowHeader ();
1004                 }
1005
1006                 [MonoTODO]
1007                 public void Collapse (int row)
1008                 {
1009
1010                 }
1011
1012                 protected internal virtual void ColumnStartedEditing (Control editingControl)
1013                 {
1014
1015                 }
1016
1017                 protected internal virtual void ColumnStartedEditing (Rectangle bounds)
1018                 {
1019
1020                 }
1021
1022                 protected override AccessibleObject CreateAccessibilityInstance ()
1023                 {
1024                         return base.CreateAccessibilityInstance ();
1025                 }
1026
1027                 protected virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop)
1028                 {
1029                         return CreateGridColumn (prop, false);
1030                 }
1031
1032                 protected virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop, bool isDefault)
1033                 {
1034                         return CreateGridColumn (prop, isDefault);
1035                 }
1036
1037                 protected override void Dispose (bool disposing)
1038                 {
1039                         base.Dispose (disposing);
1040                 }
1041
1042                 public virtual bool EndEdit (DataGridColumnStyle gridColumn, int rowNumber, bool shouldAbort)
1043                 {                                               
1044                         if (is_adding == true) {                                
1045                                 if (shouldAbort) {
1046                                         ListManager.CancelCurrentEdit ();
1047                                 } else {
1048                                         ListManager.EndCurrentEdit ();
1049                                         CalcAreasAndInvalidate ();
1050                                 }
1051                                 is_adding = false;
1052                         } 
1053
1054                         if (shouldAbort) {
1055                                 gridColumn.Abort (rowNumber);
1056                         } else {
1057                                 gridColumn.Commit (ListManager, rowNumber);
1058                         }
1059
1060                         is_editing = false;
1061                         is_changing = false;
1062                         InvalidateCurrentRowHeader ();
1063                         return true;
1064                 }
1065
1066                 public virtual void EndInit ()
1067                 {
1068                         begininit = false;
1069                 }
1070
1071                 public void Expand (int row)
1072                 {
1073
1074                 }
1075
1076                 public Rectangle GetCellBounds (DataGridCell cell)
1077                 {
1078                         return GetCellBounds (cell.RowNumber, cell.ColumnNumber);
1079                 }
1080
1081                 public Rectangle GetCellBounds (int row, int col)
1082                 {
1083                         return grid_drawing.GetCellBounds (row, col);
1084                 }
1085
1086                 public Rectangle GetCurrentCellBounds ()
1087                 {
1088                         return GetCellBounds (current_cell.RowNumber, current_cell.ColumnNumber);
1089                 }
1090
1091                 protected virtual string GetOutputTextDelimiter ()
1092                 {
1093                         return string.Empty;
1094                 }
1095
1096                 protected virtual void GridHScrolled (object sender, ScrollEventArgs se)
1097                 {
1098                         if (se.NewValue == horz_pixeloffset ||
1099                                 se.Type == ScrollEventType.EndScroll) {
1100                                 return;
1101                         }
1102
1103                         ScrollToColumnInPixels (se.NewValue);
1104                 }
1105
1106                 protected virtual void GridVScrolled (object sender, ScrollEventArgs se)
1107                 {
1108                         int old_first_visiblerow = first_visiblerow;
1109                         first_visiblerow = se.NewValue;
1110                         grid_drawing.UpdateVisibleRowCount ();
1111
1112                         if (first_visiblerow == old_first_visiblerow) {
1113                                 return;
1114                         }
1115
1116                         ScrollToRow (old_first_visiblerow, first_visiblerow);
1117                 }
1118
1119                 public HitTestInfo HitTest (Point position)
1120                 {
1121                         return HitTest (position.X, position.Y);
1122                 }
1123
1124                 public HitTestInfo HitTest (int x, int y)
1125                 {
1126                         return grid_drawing.HitTest (x, y);
1127                 }
1128
1129                 [MonoTODO]
1130                 public bool IsExpanded (int rowNumber)
1131                 {
1132                         return false;
1133                 }
1134
1135                 public bool IsSelected (int row)
1136                 {
1137                         return selected_rows[row] != null;
1138                 }
1139
1140                 [MonoTODO]
1141                 public void NavigateBack ()
1142                 {
1143
1144                 }
1145
1146                 [MonoTODO]
1147                 public void NavigateTo (int rowNumber, string relationName)
1148                 {
1149
1150                 }
1151
1152                 protected virtual void OnAllowNavigationChanged (EventArgs e)
1153                 {
1154                         if (AllowNavigationChanged != null) {
1155                                 AllowNavigationChanged (this, e);
1156                         }
1157                 }
1158
1159                 protected void OnBackButtonClicked (object sender,  EventArgs e)
1160                 {
1161                         if (BackButtonClick != null) {
1162                                 BackButtonClick (sender, e);
1163                         }
1164                 }
1165
1166                 protected override void OnBackColorChanged (EventArgs e)
1167                 {
1168                         base.OnBackColorChanged (e);
1169                 }
1170
1171                 protected virtual void OnBackgroundColorChanged (EventArgs e)
1172                 {
1173                         if (BackgroundColorChanged != null) {
1174                                 BackgroundColorChanged (this, e);
1175                         }
1176                 }
1177
1178                 protected override void OnBindingContextChanged( EventArgs e)
1179                 {
1180                         base.OnBindingContextChanged (e);
1181                 }
1182
1183                 protected virtual void OnBorderStyleChanged (EventArgs e)
1184                 {
1185                         if (BorderStyleChanged != null) {
1186                                 BorderStyleChanged (this, e);
1187                         }
1188                 }
1189
1190                 protected virtual void OnCaptionVisibleChanged (EventArgs e)
1191                 {
1192                         if (CaptionVisibleChanged != null) {
1193                                 CaptionVisibleChanged (this, e);
1194                         }
1195                 }
1196
1197                 protected virtual void OnCurrentCellChanged (EventArgs e)
1198                 {
1199                         if (CurrentCellChanged != null) {
1200                                 CurrentCellChanged (this, e);
1201                         }
1202                 }
1203
1204                 protected virtual void OnDataSourceChanged (EventArgs e)
1205                 {
1206                         if (DataSourceChanged != null) {
1207                                 DataSourceChanged (this, e);
1208                         }
1209                 }
1210
1211                 protected override void OnEnter (EventArgs e)
1212                 {
1213                         base.OnEnter (e);
1214                 }
1215
1216                 protected virtual void OnFlatModeChanged (EventArgs e)
1217                 {
1218                         if (FlatModeChanged != null) {
1219                                 FlatModeChanged (this, e);
1220                         }
1221                 }
1222
1223                 protected override void OnFontChanged (EventArgs e)
1224                 {
1225                         grid_drawing.CalcGridAreas ();
1226                         base.OnFontChanged (e);
1227                 }
1228
1229                 protected override void OnForeColorChanged (EventArgs e)
1230                 {
1231                         base.OnForeColorChanged (e);
1232                 }
1233
1234                 protected override void OnHandleCreated (EventArgs e)
1235                 {
1236                         base.OnHandleCreated (e);
1237                         grid_drawing.CalcGridAreas ();
1238                 }
1239
1240                 protected override void OnHandleDestroyed (EventArgs e)
1241                 {
1242                         base.OnHandleDestroyed (e);
1243                 }
1244
1245                 protected override void OnKeyDown (KeyEventArgs ke)
1246                 {
1247                         base.OnKeyDown (ke);
1248                         
1249                         if (ProcessGridKey (ke) == true) {
1250                                 ke.Handled = true;
1251                         }
1252
1253                         CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].OnKeyDown
1254                                 (ke, current_cell.RowNumber, current_cell.ColumnNumber);
1255                 }
1256
1257                 protected override void OnKeyPress (KeyPressEventArgs kpe)
1258                 {
1259                         base.OnKeyPress (kpe);
1260                 }
1261
1262                 protected override void OnLayout (LayoutEventArgs levent)
1263                 {
1264                         base.OnLayout (levent);
1265                 }
1266
1267                 protected override void OnLeave (EventArgs e)
1268                 {
1269                         base.OnLeave (e);
1270                 }
1271
1272                 protected override void OnMouseDown (MouseEventArgs e)
1273                 {
1274                         base.OnMouseDown (e);
1275
1276                         HitTestInfo testinfo;
1277                         testinfo = grid_drawing.HitTest (e.X, e.Y);
1278
1279                         switch (testinfo.type) {
1280                         case HitTestType.Cell:
1281                         {
1282                                 DataGridCell new_cell = new DataGridCell (testinfo.Row, testinfo.Column);
1283
1284                                 if (new_cell.Equals (current_cell) == false) {
1285                                         CancelEditing ();
1286                                         CurrentCell = new_cell;
1287                                         EditCell (current_cell);
1288
1289                                 } else {
1290                                         CurrentTableStyle.GridColumnStyles[testinfo.Column].OnMouseDown (e, testinfo.Row, testinfo.Column);
1291                                 }
1292
1293                                 break;
1294                         }
1295                         case HitTestType.RowHeader:
1296                         {
1297                                 if (ctrl_pressed == false && shift_pressed == false) {
1298                                         ResetSelection (); // Invalidates selected rows
1299                                 }
1300
1301                                 if (shift_pressed == true) {
1302                                         ShiftSelection (testinfo.Row);
1303                                 } else { // ctrl_pressed or single item
1304                                         Select (testinfo.Row);
1305                                 }
1306
1307                                 CancelEditing ();
1308                                 CurrentCell = new DataGridCell (testinfo.Row, current_cell.ColumnNumber);
1309                                 OnRowHeaderClick (EventArgs.Empty);
1310                                 break;
1311                         }
1312                         default:
1313                                 break;
1314                         }
1315                 }
1316
1317                 protected override void OnMouseLeave (EventArgs e)
1318                 {
1319                         base.OnMouseLeave (e);
1320                 }
1321
1322                 protected override void OnMouseMove (MouseEventArgs e)
1323                 {
1324                         base.OnMouseMove (e);
1325                 }
1326
1327                 protected override void OnMouseUp (MouseEventArgs e)
1328                 {
1329                         base.OnMouseUp (e);
1330                 }
1331
1332                 protected override void OnMouseWheel (MouseEventArgs e)
1333                 {
1334                         base.OnMouseWheel (e);
1335
1336                         if (e.Delta > 0) {
1337                                 if (current_cell.RowNumber > 0) {
1338                                         CurrentCell = new DataGridCell (current_cell.RowNumber - 1, current_cell.ColumnNumber);
1339                                 }
1340                         }
1341                         else {
1342                                 if (current_cell.RowNumber < RowsCount - 1) {
1343                                         CurrentCell = new DataGridCell (current_cell.RowNumber + 1, current_cell.ColumnNumber);                                 
1344                                 }
1345                         }
1346                 }
1347
1348                 protected void OnNavigate (NavigateEventArgs e)
1349                 {
1350                         if (Navigate != null) {
1351                                 Navigate (this, e);
1352                         }
1353                 }
1354
1355                 protected override void OnPaint (PaintEventArgs pe)
1356                 {
1357                         grid_drawing.OnPaint (pe);
1358                 }
1359
1360                 protected override void OnPaintBackground (PaintEventArgs ebe)
1361                 {
1362                         base.OnPaintBackground (ebe);
1363                 }
1364
1365                 protected virtual void OnParentRowsLabelStyleChanged (EventArgs e)
1366                 {
1367                         if (ParentRowsLabelStyleChanged != null) {
1368                                 ParentRowsLabelStyleChanged (this, e);
1369                         }
1370                 }
1371
1372                 protected virtual void OnParentRowsVisibleChanged (EventArgs e)
1373                 {
1374                         if (ParentRowsVisibleChanged != null) {
1375                                 ParentRowsVisibleChanged (this, e);
1376                         }
1377                 }
1378
1379                 protected virtual void OnReadOnlyChanged (EventArgs e)
1380                 {
1381                         if (ReadOnlyChanged != null) {
1382                                 ReadOnlyChanged (this, e);
1383                         }
1384                 }
1385
1386                 protected override void OnResize (EventArgs e)
1387                 {
1388                         base.OnResize (e);
1389                 }
1390
1391                 protected void OnRowHeaderClick (EventArgs e)
1392                 {
1393                         if (RowHeaderClick != null) {
1394                                 RowHeaderClick (this, e);
1395                         }
1396                 }
1397
1398                 protected void OnScroll (EventArgs e)
1399                 {
1400                         if (Scroll != null) {
1401                                 Scroll (this, e);
1402                         }
1403                 }
1404
1405                 protected void OnShowParentDetailsButtonClicked (object sender, EventArgs e)
1406                 {
1407                         if (ShowParentDetailsButtonClick != null) {
1408                                 ShowParentDetailsButtonClick (sender, e);
1409                         }
1410                 }
1411
1412                 protected override bool ProcessDialogKey (Keys keyData)
1413                 {
1414                         return base.ProcessDialogKey (keyData);
1415                 }
1416
1417                 protected bool ProcessGridKey (KeyEventArgs ke)
1418                 {
1419                         if (RowsCount == 0) {
1420                                 return false;
1421                         }
1422
1423                         switch (ke.KeyCode) {
1424                         case Keys.ControlKey:
1425                                 ctrl_pressed = true;
1426                                 break;
1427                         case Keys.ShiftKey:
1428                                 shift_pressed = true;
1429                                 break;
1430                         case Keys.Up:
1431                         {
1432                                 if (current_cell.RowNumber > 0) {
1433                                         CurrentCell = new DataGridCell (current_cell.RowNumber - 1, current_cell.ColumnNumber);
1434                                         EditCell (current_cell);
1435                                 }
1436                                 break;
1437                         }
1438                         case Keys.Down:
1439                         {
1440                                 if (current_cell.RowNumber < RowsCount - 1) {
1441                                         CurrentCell = new DataGridCell (current_cell.RowNumber + 1, current_cell.ColumnNumber);
1442                                         EditCell (current_cell);
1443                                 }
1444                                 break;
1445                         }
1446                         case Keys.Right:
1447                         {
1448                                 if (current_cell.ColumnNumber + 1 < CurrentTableStyle.GridColumnStyles.Count) {
1449                                         CurrentCell = new DataGridCell (current_cell.RowNumber, current_cell.ColumnNumber + 1);
1450                                         EditCell (current_cell);
1451                                 }
1452                                 break;
1453                         }
1454                         case Keys.Left:
1455                         {
1456                                 if (current_cell.ColumnNumber > 0) {
1457                                         CurrentCell = new DataGridCell (current_cell.RowNumber, current_cell.ColumnNumber - 1);
1458                                         EditCell (current_cell);
1459                                 }
1460                                 break;
1461                         }
1462                         case Keys.PageUp:
1463                         {
1464                                 if (current_cell.RowNumber > grid_drawing.VLargeChange) {
1465                                         CurrentCell = new DataGridCell (current_cell.RowNumber - grid_drawing.VLargeChange, current_cell.ColumnNumber);
1466                                 } else {
1467                                         CurrentCell = new DataGridCell (0, current_cell.ColumnNumber);
1468                                 }
1469
1470                                 EditCell (current_cell);
1471                                 break;
1472                         }
1473                         case Keys.PageDown:
1474                         {
1475                                 if (current_cell.RowNumber + grid_drawing.VLargeChange < RowsCount) {
1476                                         CurrentCell = new DataGridCell (current_cell.RowNumber + grid_drawing.VLargeChange, current_cell.ColumnNumber);
1477                                 } else {
1478                                         CurrentCell = new DataGridCell (RowsCount - 1, current_cell.ColumnNumber);
1479                                 }
1480
1481                                 EditCell (current_cell);
1482                                 break;
1483                         }
1484                         case Keys.Home:
1485                         {
1486                                 CurrentCell = new DataGridCell (0, current_cell.ColumnNumber);
1487                                 EditCell (current_cell);
1488                                 break;
1489                         }
1490                         case Keys.End:
1491                         {
1492                                 CurrentCell = new DataGridCell (RowsCount - 1, current_cell.ColumnNumber);
1493                                 EditCell (current_cell);
1494                                 break;
1495                         }
1496                         case Keys.Delete:
1497                         {                               
1498                                 foreach (int row in selected_rows.Keys) {
1499                                         ListManager.RemoveAt (row);                                             
1500                                 }
1501                                 selected_rows.Clear ();
1502                                 CalcAreasAndInvalidate ();
1503                                 break;                                  
1504                         }
1505                         default:
1506                                 return false; // message not processed
1507                         }
1508
1509                         return true; // message processed
1510                 }
1511
1512                 // Called from DataGridTextBox
1513                 protected override bool ProcessKeyPreview (ref Message m)
1514                 {
1515                         Keys key = (Keys) m.WParam.ToInt32 ();
1516                         KeyEventArgs ke = new KeyEventArgs (key);
1517                         if (ProcessGridKey (ke) == true) {
1518                                 return true;
1519                         }
1520
1521                         return base.ProcessKeyPreview (ref m);
1522                 }
1523
1524                 [MonoTODO]      
1525                 protected bool ProcessTabKey (Keys keyData)
1526                 {
1527                         return false;
1528                 }
1529
1530                 public void ResetAlternatingBackColor ()
1531                 {
1532                         alternating_backcolor = def_alternating_backcolor;
1533                 }
1534
1535                 public override void ResetBackColor ()
1536                 {
1537                         background_color = def_background_color;
1538                 }
1539
1540                 public override void ResetForeColor ()
1541                 {
1542                         base.ResetForeColor ();
1543                 }
1544
1545                 public void ResetGridLineColor ()
1546                 {
1547                         gridline_color = def_gridline_color;
1548                 }
1549
1550                 public void ResetHeaderBackColor ()
1551                 {
1552                         header_backcolor = def_header_backcolor;
1553                 }
1554
1555                 public void ResetHeaderFont ()
1556                 {
1557                         header_font = def_header_font;
1558                 }
1559
1560                 public void ResetHeaderForeColor ()
1561                 {
1562                         header_forecolor = def_header_forecolor;
1563                 }
1564
1565                 public void ResetLinkColor ()
1566                 {
1567                         link_color = def_link_color;
1568                 }
1569
1570                 public void ResetLinkHoverColor ()
1571                 {
1572                         link_hovercolor = def_link_hovercolor;
1573                 }
1574
1575                 protected void ResetSelection ()
1576                 {                       
1577                         foreach (int row in selected_rows.Keys) {
1578                                 grid_drawing.InvalidateRow (row);
1579                                 grid_drawing.InvalidateRowHeader (row);
1580                         }
1581
1582                         selected_rows.Clear ();
1583                 }
1584
1585                 public void ResetSelectionBackColor ()
1586                 {
1587                         selection_backcolor = def_selection_backcolor;
1588                 }
1589
1590                 public void ResetSelectionForeColor ()
1591                 {
1592                         selection_forecolor = def_selection_forecolor;
1593                 }
1594
1595                 public void Select (int row)
1596                 {
1597                         if (selected_rows[row] == null) {
1598                                 selected_rows.Add (row, true);
1599                         } else {
1600                                 selected_rows[row] = true;
1601                         }
1602
1603                         grid_drawing.InvalidateRow (row);
1604                 }
1605
1606                 public void SetDataBinding (object dataSource, string dataMember)
1607                 {
1608                         if (SetDataSource (dataSource) == false  && SetDataMember (dataMember) == false) {
1609                                 return;
1610                         }
1611
1612                         SetNewDataSource ();
1613                 }
1614
1615                 protected virtual bool ShouldSerializeAlternatingBackColor ()
1616                 {
1617                         return (alternating_backcolor != def_alternating_backcolor);
1618                 }
1619
1620                 protected virtual bool ShouldSerializeBackgroundColor ()
1621                 {
1622                         return (background_color != def_background_color);
1623                 }
1624
1625                 protected virtual bool ShouldSerializeCaptionBackColor ()
1626                 {
1627                         return (caption_backcolor != def_caption_backcolor);
1628                 }
1629
1630                 protected virtual bool ShouldSerializeCaptionForeColor ()
1631                 {
1632                         return (caption_forecolor != def_caption_forecolor);
1633                 }
1634
1635                 protected virtual bool ShouldSerializeGridLineColor ()
1636                 {
1637                         return (gridline_color != def_gridline_color);
1638                 }
1639
1640                 protected virtual bool ShouldSerializeHeaderBackColor ()
1641                 {
1642                         return (header_backcolor != def_header_backcolor);
1643                 }
1644
1645                 protected bool ShouldSerializeHeaderFont ()
1646                 {
1647                         return (header_font != def_header_font);
1648                 }
1649
1650                 protected virtual bool ShouldSerializeHeaderForeColor ()
1651                 {
1652                         return (header_forecolor != def_header_forecolor);
1653                 }
1654
1655                 protected virtual bool ShouldSerializeLinkHoverColor ()
1656                 {
1657                         return (link_hovercolor != def_link_hovercolor);
1658                 }
1659
1660                 protected virtual bool ShouldSerializeParentRowsBackColor ()
1661                 {
1662                         return (parentrowsback_color != def_parentrowsback_color);
1663                 }
1664
1665                 protected virtual bool ShouldSerializeParentRowsForeColor ()
1666                 {
1667                         return (parentrowsback_color != def_parentrowsback_color);
1668                 }
1669
1670                 protected bool ShouldSerializePreferredRowHeight ()
1671                 {
1672                         return (preferredrow_height != def_preferredrow_height);
1673                 }
1674
1675                 protected bool ShouldSerializeSelectionBackColor ()
1676                 {
1677                         return (selection_backcolor != def_selection_backcolor);
1678                 }
1679
1680                 protected virtual bool ShouldSerializeSelectionForeColor ()
1681                 {
1682                         return (selection_forecolor != def_selection_forecolor);
1683                 }
1684
1685                 public void SubObjectsSiteChange (bool site)
1686                 {
1687
1688                 }
1689
1690                 public void UnSelect (int row)
1691                 {
1692                         selected_rows.Remove (row);
1693                         grid_drawing.InvalidateRow (row);
1694
1695                 }
1696                 #endregion      // Public Instance Methods
1697
1698                 #region Private Instance Methods
1699
1700                 internal void CalcAreasAndInvalidate ()
1701                 {
1702                         grid_drawing.CalcGridAreas ();
1703                         Invalidate ();
1704                 }
1705
1706                 // EndEdit current editing operation
1707                 public virtual bool EndEdit (bool shouldAbort)
1708                 {
1709                         return EndEdit (CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber],
1710                                 current_cell.RowNumber, shouldAbort);
1711                 }
1712
1713                 private void EnsureCellVisilibility (DataGridCell cell)
1714                 {
1715                         if (current_cell.RowNumber != cell.RowNumber) {
1716                                 grid_drawing.InvalidateRowHeader (current_cell.RowNumber);
1717                                 grid_drawing.InvalidateRowHeader (cell.RowNumber);
1718                         }
1719
1720                         if (cell.ColumnNumber < first_visiblecolumn ||
1721                                 cell.ColumnNumber >= first_visiblecolumn + visiblecolumn_count) {
1722
1723                                 int col, pixel;
1724
1725                                 if (cell.ColumnNumber + 1 >= first_visiblecolumn + visiblecolumn_count) {
1726                                         col = 1 + cell.ColumnNumber - visiblecolumn_count;
1727                                 }else {
1728                                         col = cell.RowNumber;
1729                                 }
1730
1731                                 pixel = grid_drawing.GetColumnStartingPixel (col);
1732                                 ScrollToColumnInPixels (pixel);
1733                         }
1734
1735                         if (cell.RowNumber < first_visiblerow ||
1736                                 cell.RowNumber + 1 >= first_visiblerow + visiblerow_count) {
1737
1738                                 if (cell.RowNumber + 1 >= first_visiblerow + visiblerow_count) {
1739                                         int old_first_visiblerow = first_visiblerow;
1740                                         first_visiblerow = 1 + cell.RowNumber - visiblerow_count;
1741                                         grid_drawing.UpdateVisibleRowCount ();
1742                                         ScrollToRow (old_first_visiblerow, first_visiblerow);
1743                                 }else {
1744                                         int old_first_visiblerow = first_visiblerow;
1745                                         first_visiblerow = cell.RowNumber;
1746                                         grid_drawing.UpdateVisibleRowCount ();
1747                                         ScrollToRow (old_first_visiblerow, first_visiblerow);
1748                                 }
1749
1750                                 vert_scrollbar.Value = first_visiblerow;
1751                         }
1752                 }
1753
1754                 internal void InvalidateCurrentRowHeader ()
1755                 {
1756                         grid_drawing.InvalidateRowHeader (current_cell.RowNumber);
1757                 }
1758
1759                 private bool SetDataMember (string member)
1760                 {                       
1761                         if (member == datamember) {
1762                                 return false;
1763                         }
1764
1765                         datamember = member;
1766                         real_datasource = DataSourceHelper.GetResolvedDataSource (datasource, member);
1767                         cached_currencymgr = null;
1768                         return true;
1769                 }
1770
1771                 private bool SetDataSource (object source)
1772                 {
1773                         if (datasource != null && datasource.Equals (source)) {
1774                                 return false;
1775                         }
1776
1777                         if (source != null && source as IListSource != null && source as IList != null) {
1778                                 throw new Exception ("Wrong complex data binding source");
1779                         }
1780
1781                         datasource = source;
1782                         cached_currencymgr = null;
1783                         try {
1784                                 real_datasource = DataSourceHelper.GetResolvedDataSource (datasource, DataMember);
1785                         }catch (Exception e) {
1786                                 datamember = "";
1787                                 real_datasource = source;
1788                         }
1789
1790                         OnDataSourceChanged (EventArgs.Empty);
1791                         return true;
1792                 }
1793
1794                 private void SetNewDataSource ()
1795                 {
1796                         // Create Table Style
1797                         // Create columns Styles
1798                         // Bind data                            
1799                         current_style.GridColumnStyles.Clear ();
1800                         current_style.CreateColumnsForTable (false);
1801                         CalcAreasAndInvalidate ();
1802                 }
1803
1804                 private void OnKeyUpDG (object sender, KeyEventArgs e)
1805                 {
1806                         switch (e.KeyCode) {
1807                         case Keys.ControlKey:
1808                                 ctrl_pressed = false;
1809                                 break;
1810                         case Keys.ShiftKey:
1811                                 shift_pressed = false;
1812                                 break;
1813                         default:
1814                                 break;
1815                         }
1816                 }
1817                 
1818                 private void OnTableStylesCollectionChanged (object sender, CollectionChangeEventArgs e)
1819                 {       
1820                         if (String.Compare (ListManager.ListName, ((DataGridTableStyle)e.Element).MappingName, true) == 0) {                    
1821                                 CurrentTableStyle = (DataGridTableStyle)e.Element;
1822                                 ((DataGridTableStyle) e.Element).CreateColumnsForTable (false);                         
1823                         }
1824                                                 
1825                         CalcAreasAndInvalidate ();
1826                 }
1827
1828                 private void EditCell (DataGridCell cell)
1829                 {
1830                         ResetSelection (); // Invalidates selected rows
1831                         is_editing = false;
1832                         is_changing = false;
1833                         
1834                         if (ShowEditRow && cell.RowNumber >= RowsCount) {
1835                                 ListManager.AddNew ();
1836                                 is_adding = true;
1837                                 Invalidate (); // We have just added a new row
1838                         }
1839                         
1840                         CurrentTableStyle.GridColumnStyles[cell.ColumnNumber].Edit (ListManager,
1841                                 cell.RowNumber, GetCellBounds (cell.RowNumber, cell.ColumnNumber),
1842                                 _readonly, string.Empty, true);
1843                 }
1844
1845                 private void ShiftSelection (int index)
1846                 {
1847                         int shorter_item = -1, dist = RowsCount + 1, cur_dist;                  
1848
1849                         foreach (int row in selected_rows.Keys) {
1850
1851                                 if (row > index) {
1852                                         cur_dist = row - index;
1853                                 }
1854                                 else {
1855                                         cur_dist = index - row;
1856                                 }
1857
1858                                 if (cur_dist < dist) {
1859                                         dist = cur_dist;
1860                                         shorter_item = row;
1861                                 }
1862                         }
1863
1864                         if (shorter_item != -1) {
1865                                 int start, end;
1866
1867                                 if (shorter_item > index) {
1868                                         start = index;
1869                                         end = shorter_item;
1870                                 } else {
1871                                         start = shorter_item;
1872                                         end = index;
1873                                 }
1874
1875                                 ResetSelection ();
1876                                 for (int idx = start; idx <= end; idx++) {
1877                                         Select (idx);
1878                                 }
1879                         }
1880                 }
1881
1882                 private void ScrollToColumnInPixels (int pixel)
1883                 {
1884                         Rectangle invalidate = new Rectangle ();
1885                         Rectangle invalidate_column = new Rectangle ();
1886
1887                         if (pixel > horz_pixeloffset) { // ScrollRight
1888                                 int pixels = pixel - horz_pixeloffset;
1889
1890                                 // Columns header
1891                                 invalidate_column.X = grid_drawing.ColumnsHeadersArea.X + grid_drawing.ColumnsHeadersArea.Width - pixels;
1892                                 invalidate_column.Y = grid_drawing.ColumnsHeadersArea.Y;
1893                                 invalidate_column.Width = pixels;
1894                                 invalidate_column.Height = grid_drawing.ColumnsHeadersArea.Height;
1895                                 XplatUI.ScrollWindow (Handle, grid_drawing.ColumnsHeadersArea, -pixels, 0, false);
1896
1897                                 // Cells
1898                                 invalidate.X = grid_drawing.CellsArea.X + grid_drawing.CellsArea.Width - pixels;
1899                                 invalidate.Y = grid_drawing.CellsArea.Y;
1900                                 invalidate.Width = pixels;
1901                                 invalidate.Height = grid_drawing.CellsArea.Height;
1902                                 XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, -pixels, 0, false);
1903
1904                         } else {
1905                                 int pixels = horz_pixeloffset - pixel;
1906
1907                                 // Columns header
1908                                 invalidate_column.X = grid_drawing.ColumnsHeadersArea.X;
1909                                 invalidate_column.Y = grid_drawing.ColumnsHeadersArea.Y;
1910                                 invalidate_column.Width = pixels;
1911                                 invalidate_column.Height = grid_drawing.ColumnsHeadersArea.Height;
1912                                 XplatUI.ScrollWindow (Handle, grid_drawing.ColumnsHeadersArea, pixels, 0, false);
1913
1914                                 // Cells
1915                                 invalidate.X =  grid_drawing.CellsArea.X;
1916                                 invalidate.Y =  grid_drawing.CellsArea.Y;
1917                                 invalidate.Width = pixels;
1918                                 invalidate.Height = grid_drawing.CellsArea.Height;
1919                                 XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, pixels, 0, false);
1920                         }
1921
1922                         horz_pixeloffset = horiz_scrollbar.Value = pixel;
1923                         grid_drawing.UpdateVisibleColumn ();
1924                         Invalidate (invalidate_column);
1925                         Invalidate (invalidate);
1926                 }
1927
1928                 private void ScrollToRow (int old_row, int new_row)
1929                 {
1930                         Rectangle invalidate = new Rectangle ();
1931
1932                         if (new_row > old_row) { // Scrolldown
1933                                 int scrolled_rows = new_row - old_row;
1934                                 int pixels = scrolled_rows * RowHeight;
1935
1936                                 invalidate.X =  grid_drawing.CellsArea.X;
1937                                 invalidate.Y =  grid_drawing.CellsArea.Y + grid_drawing.CellsArea.Height - pixels;
1938                                 invalidate.Width = grid_drawing.CellsArea.Width;
1939                                 invalidate.Height = pixels;
1940
1941                                 XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, 0, -pixels, false);
1942
1943                         } else { // ScrollUp
1944                                 int scrolled_rows = old_row - new_row;
1945                                 int pixels = scrolled_rows * RowHeight;
1946
1947                                 invalidate.X =  grid_drawing.CellsArea.X;
1948                                 invalidate.Y =  grid_drawing.CellsArea.Y;
1949                                 invalidate.Width = grid_drawing.CellsArea.Width;
1950                                 invalidate.Height = pixels;
1951                                 XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, 0, pixels, false);                                
1952                         }
1953
1954                         // Right now we use ScrollWindow Invalidate, let's leave remarked it here for X11 if need it
1955                         //Invalidate ();
1956                         Invalidate (grid_drawing.RowsHeadersArea);
1957                 }
1958
1959                 #endregion Private Instance Methods
1960
1961
1962                 #region Events
1963                 public event EventHandler AllowNavigationChanged;
1964                 public event EventHandler BackButtonClick;
1965                 public event EventHandler BackgroundColorChanged;
1966
1967                 [Browsable(false)]
1968                 [EditorBrowsable(EditorBrowsableState.Never)]
1969                 public new event EventHandler BackgroundImageChanged;
1970
1971                 public event EventHandler BorderStyleChanged;
1972                 public event EventHandler CaptionVisibleChanged;
1973                 public event EventHandler CurrentCellChanged;
1974
1975                 [Browsable(false)]
1976                 [EditorBrowsable(EditorBrowsableState.Never)]
1977                 public new event EventHandler CursorChanged;
1978
1979                 public event EventHandler DataSourceChanged;
1980                 public event EventHandler FlatModeChanged;
1981                 public event NavigateEventHandler Navigate;
1982                 public event EventHandler ParentRowsLabelStyleChanged;
1983                 public event EventHandler ParentRowsVisibleChanged;
1984                 public event EventHandler ReadOnlyChanged;
1985                 protected event EventHandler RowHeaderClick;
1986                 public event EventHandler Scroll;
1987                 public event EventHandler ShowParentDetailsButtonClick;
1988
1989                 [Browsable(false)]
1990                 [EditorBrowsable(EditorBrowsableState.Never)]
1991                 public new event EventHandler TextChanged;
1992                 #endregion      // Events
1993         }
1994 }