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