* DataGridTextBoxColumn.cs: remove some spew.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridColumnStyle.cs
index 498de6612fe574285d0cd1a6fee873b61699dfa6..ab40ab003eda7572b9f261ca7de231a6e84855ec 100644 (file)
@@ -107,7 +107,14 @@ namespace System.Windows.Forms
                        }
                        #endregion Public Instance Methods
                }
-
+               
+               internal enum ArrowDrawing
+               {
+                       No              = 0,
+                       Ascending       = 1,
+                       Descending      = 2
+               }               
+               
                #region Local Variables
                internal HorizontalAlignment alignment;
                private int fontheight;
@@ -116,36 +123,33 @@ namespace System.Windows.Forms
                private string mapping_name;
                private string null_text;
                private PropertyDescriptor property_descriptor;
-               private bool read_only;
+               private bool _readonly;
                private int width;
                internal bool is_default;
                internal DataGrid grid;
                private DataGridColumnHeaderAccessibleObject accesible_object;
                private StringFormat string_format_hdr;
+               static string def_null_text = "(null)";
+               private ArrowDrawing arrow_drawing = ArrowDrawing.No;
+               internal bool bound;
                #endregion      // Local Variables
 
                #region Constructors
-               public DataGridColumnStyle ()
+               public DataGridColumnStyle () : this (null)
                {
-                       CommmonConstructor ();
-                       property_descriptor = null;
                }
 
                public DataGridColumnStyle (PropertyDescriptor prop)
                {
-                       CommmonConstructor ();
                        property_descriptor = prop;
-               }
 
-               private void CommmonConstructor ()
-               {
                        fontheight = -1;
                        table_style = null;
                        header_text = string.Empty;
-                       mapping_name  = "(null)";
-                       null_text = string.Empty;
+                       mapping_name  = string.Empty;
+                       null_text = def_null_text;
                        accesible_object = new DataGridColumnHeaderAccessibleObject (this);
-                       read_only = false;
+                       _readonly = false;
                        width = -1;
                        grid = null;
                        is_default = false;
@@ -172,18 +176,16 @@ namespace System.Windows.Forms
                                                table_style.DataGrid.Invalidate ();
                                        }
                                        
-                                       if (AlignmentChanged != null) {
-                                               AlignmentChanged (this, EventArgs.Empty);
-                                       }                                       
+                                       EventHandler eh = (EventHandler)(Events [AlignmentChangedEvent]);
+                                       if (eh != null)
+                                               eh (this, EventArgs.Empty);
                                }
                        }
                }
 
                [Browsable(false)]
                public virtual DataGridTableStyle DataGridTableStyle {
-                       get {
-                               return table_style;
-                       }                       
+                       get { return table_style; }                     
                }
                
                protected int FontHeight {
@@ -218,13 +220,11 @@ namespace System.Windows.Forms
                                if (value != header_text) {
                                        header_text = value;
                                        
-                                       if (table_style != null && table_style.DataGrid != null) {
-                                               table_style.DataGrid.Invalidate ();
-                                       }
+                                       Invalidate ();
 
-                                       if (HeaderTextChanged != null) {
-                                               HeaderTextChanged (this, EventArgs.Empty);
-                                       }
+                                       EventHandler eh = (EventHandler)(Events [HeaderTextChangedEvent]);
+                                       if (eh != null)
+                                               eh (this, EventArgs.Empty);
                                }
                        }
                }
@@ -239,9 +239,9 @@ namespace System.Windows.Forms
                                if (value != mapping_name) {
                                        mapping_name = value;
 
-                                       if (MappingNameChanged != null) {
-                                               MappingNameChanged (this, EventArgs.Empty);
-                                       }
+                                       EventHandler eh = (EventHandler)(Events [MappingNameChangedEvent]);
+                                       if (eh != null)
+                                               eh (this, EventArgs.Empty);
                                }
                        }
                }
@@ -259,9 +259,9 @@ namespace System.Windows.Forms
                                                table_style.DataGrid.Invalidate ();
                                        }
 
-                                       if (NullTextChanged != null) {
-                                               NullTextChanged (this, EventArgs.Empty);
-                                       }
+                                       EventHandler eh = (EventHandler)(Events [NullTextChangedEvent]);
+                                       if (eh != null)
+                                               eh (this, EventArgs.Empty);
                                }
                        }
                }
@@ -277,9 +277,9 @@ namespace System.Windows.Forms
                                if (value != property_descriptor) {
                                        property_descriptor = value;                                    
 
-                                       if (PropertyDescriptorChanged != null) {
-                                               PropertyDescriptorChanged (this, EventArgs.Empty);
-                                       }
+                                       EventHandler eh = (EventHandler)(Events [PropertyDescriptorChangedEvent]);
+                                       if (eh != null)
+                                               eh (this, EventArgs.Empty);
                                }
                        }
                }
@@ -287,19 +287,19 @@ namespace System.Windows.Forms
                [DefaultValue(false)]
                public virtual bool ReadOnly  {
                        get {
-                               return read_only;
+                               return _readonly;
                        }
                        set {
-                               if (value != read_only) {
-                                       read_only = value;
+                               if (value != _readonly) {
+                                       _readonly = value;
                                        
                                        if (table_style != null && table_style.DataGrid != null) {
                                                table_style.DataGrid.CalcAreasAndInvalidate ();
                                        }
                                        
-                                       if (ReadOnlyChanged != null) {
-                                               ReadOnlyChanged (this, EventArgs.Empty);
-                                       }
+                                       EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
+                                       if (eh != null)
+                                               eh (this, EventArgs.Empty);
                                }
                        }
                }
@@ -318,9 +318,9 @@ namespace System.Windows.Forms
                                                table_style.DataGrid.CalcAreasAndInvalidate ();
                                        }
 
-                                       if (WidthChanged != null) {
-                                               WidthChanged (this, EventArgs.Empty);
-                                       }
+                                       EventHandler eh = (EventHandler)(Events [WidthChangedEvent]);
+                                       if (eh != null)
+                                               eh (this, EventArgs.Empty);
                                }
                        }
                }
@@ -328,6 +328,35 @@ namespace System.Windows.Forms
                #endregion      // Public Instance Properties
                
                #region Private Instance Properties
+
+               internal ArrowDrawing ArrowDrawingMode {
+                       get { return arrow_drawing; }
+                       set { arrow_drawing = value; }
+               }
+               
+               // The logic seems to be that: 
+               // - If DataGrid.ReadOnly is true all the tables and columns are readonly ignoring other settings
+               // - If DataGridTableStyle.ReadOnly is true all columns are readonly ignoring other settings
+               // - If DataGrid.ReadOnly and DataGridTableStyle.ReadOnly are false, the columns settings are mandatory
+               //
+               internal bool ParentReadOnly {
+                       get {
+                               if (grid != null) {
+                                       if (grid.ReadOnly == true) {
+                                               return true;
+                                       }
+                               }
+                               
+                               if (table_style != null) {
+                                       if (table_style.ReadOnly == true) {
+                                               return true;
+                                       }
+                               }
+                               
+                               return false;
+                       }
+               }
+               
                internal DataGridTableStyle TableStyle {
                        set { table_style = value; }
                }
@@ -353,7 +382,17 @@ namespace System.Windows.Forms
                        }
                        
                        if (property_descriptor == null) {
-                               throw new ApplicationException ("The PropertyDescriptor for this column is a null reference");
+                               PropertyDescriptorCollection propcol = value.GetItemProperties ();
+
+                               for (int i = 0; i < propcol.Count ; i++) {
+                                       if (propcol[i].Name == mapping_name) {
+                                               property_descriptor = propcol[i];
+                                               break;
+                                       }
+                               }
+
+                               if (property_descriptor == null)
+                                       throw new InvalidOperationException ("The PropertyDescriptor for this column is a null reference");
                        }
                }
 
@@ -400,7 +439,9 @@ namespace System.Windows.Forms
                protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
                {                       
                        CheckValidDataSource (source);
-                       return property_descriptor.GetValue (source.GetItem (rowNum));
+                       if (rowNum >= source.Count)
+                               return DBNull.Value;
+                       return property_descriptor.GetValue (source [rowNum]);
                }
 
                protected internal abstract int GetMinimumHeight ();
@@ -411,12 +452,13 @@ namespace System.Windows.Forms
 
                void  IDataGridColumnStyleEditingNotificationService.ColumnStartedEditing (Control editingControl)
                {
-
+                       ColumnStartedEditing (editingControl);
                }
 
                protected virtual void Invalidate ()
                {
-                       grid.grid_drawing.InvalidateColumn (this);
+                       if (grid != null)
+                               grid.InvalidateColumn (this);
                }
 
                protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum);
@@ -435,24 +477,26 @@ namespace System.Windows.Forms
                protected internal virtual void SetColumnValueAtRow (CurrencyManager source, int rowNum,  object value)
                {
                        CheckValidDataSource (source);
-                       property_descriptor.SetValue (source.GetItem (rowNum), value);
+
+                       IEditableObject editable = source [rowNum] as IEditableObject;
+
+                       if (editable != null)
+                               editable.BeginEdit ();
+
+                       property_descriptor.SetValue (source [rowNum], value);
+
+                       if (editable != null)
+                               editable.EndEdit ();
                }
 
                protected virtual void SetDataGrid (DataGrid value)
                {
                        grid = value;
-                       
-                       if (property_descriptor != null || value == null || value.ListManager == null) {
-                               return;
-                       }
-                       
-                       PropertyDescriptorCollection propcol = value.ListManager.GetItemProperties ();
-                       for (int i = 0; i < propcol.Count ; i++) {
-                               if (propcol[i].Name == mapping_name) {
-                                       property_descriptor = propcol[i];
-                                       break;
-                               }
-                       }                       
+
+                       property_descriptor = null;
+
+                       if (value.ListManager != null)
+                               CheckValidDataSource (value.ListManager);
                }
 
                protected virtual void SetDataGridInColumn (DataGrid value)
@@ -478,45 +522,122 @@ namespace System.Windows.Forms
                internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
                {       
                        // Background
-                       g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.DataGrid.ParentRowsBackColor), 
+                       g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderBackColor), 
                                bounds);
+                               
+                       if (grid.FlatMode == false) {                   
                        
-                       // Paint Borders                        
-                       g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),                        
-                               bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
+                               // Paint Borders
+                               g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),                 
+                                       bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
+                               
+                               if (colNum == 0) {      
+                                       g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
+                                               bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
+                               } else {
+                                       g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
+                                               bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 2);
+                               }
+                               
+                               g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlDark),
+                                       bounds.X + bounds.Width - 1, bounds.Y + 2 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 2);
+                       }
                        
-                       if (colNum == 0) {      
-                               g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
-                                       bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
-                       } else {
-                               g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
-                                       bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 2);
+                       bounds.X += 2;
+                       bounds.Width -= 2;
+                       g.DrawString (HeaderText, DataGridTableStyle.HeaderFont, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderForeColor), 
+                               bounds, string_format_hdr);
+
+                       if (arrow_drawing != ArrowDrawing.No) {
+                               // Draw 6 x 6
+                               Point pnt = new Point (bounds.X + bounds.Width  - 12, bounds.Y + ((bounds.Height - 6)/2));
+                               
+                               if (arrow_drawing == ArrowDrawing.Ascending) {
+                                       g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y + 6, pnt.X + 3, pnt.Y);
+                                       g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 6, pnt.Y + 6);
+                                       g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 3, pnt.Y);
+                               } else {
+                                       g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y, pnt.X + 3, pnt.Y + 6);
+                                       g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 6, pnt.Y);
+                                       g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 3, pnt.Y + 6);
+                               }
+                               
+                       }
+               }
+                               
+               internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
+               {
+                       g.FillRectangle (backBrush, bounds);
+                       PaintGridLine (g, bounds);
+               }
+               
+               internal void PaintGridLine (Graphics g, Rectangle bounds)
+               {
+                       if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
+                               return;
                        }
                        
-                       g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
-                               bounds.X + bounds.Width - 1, bounds.Y + 2 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 2);
+                       g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
+                               bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
                        
-                       bounds.X += 3;
-                       bounds.Width -= 3;
-                       g.DrawString (HeaderText, DataGridTableStyle.DataGrid.Font, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), 
-                               bounds, string_format_hdr);
+                       g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
+                               bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
                }
                
                #endregion Private Instance Methods
 
 
                #region Events
-               public event EventHandler AlignmentChanged;
-               public event EventHandler FontChanged;
-               public event EventHandler HeaderTextChanged;
-               public event EventHandler MappingNameChanged;
-               public event EventHandler NullTextChanged;
+               static object AlignmentChangedEvent = new object ();
+               static object FontChangedEvent = new object ();
+               static object HeaderTextChangedEvent = new object ();
+               static object MappingNameChangedEvent = new object ();
+               static object NullTextChangedEvent = new object ();
+               static object PropertyDescriptorChangedEvent = new object ();
+               static object ReadOnlyChangedEvent = new object ();
+               static object WidthChangedEvent = new object ();
+
+               public event EventHandler AlignmentChanged {
+                       add { Events.AddHandler (AlignmentChangedEvent, value); }
+                       remove { Events.RemoveHandler (AlignmentChangedEvent, value); }
+               }
+
+               public event EventHandler FontChanged {
+                       add { Events.AddHandler (FontChangedEvent, value); }
+                       remove { Events.RemoveHandler (FontChangedEvent, value); }
+               }
+
+               public event EventHandler HeaderTextChanged {
+                       add { Events.AddHandler (HeaderTextChangedEvent, value); }
+                       remove { Events.RemoveHandler (HeaderTextChangedEvent, value); }
+               }
+
+               public event EventHandler MappingNameChanged {
+                       add { Events.AddHandler (MappingNameChangedEvent, value); }
+                       remove { Events.RemoveHandler (MappingNameChangedEvent, value); }
+               }
+
+               public event EventHandler NullTextChanged {
+                       add { Events.AddHandler (NullTextChangedEvent, value); }
+                       remove { Events.RemoveHandler (NullTextChangedEvent, value); }
+               }
 
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Advanced)]
-               public event EventHandler PropertyDescriptorChanged;
-               public event EventHandler ReadOnlyChanged;
-               public event EventHandler WidthChanged;
+               public event EventHandler PropertyDescriptorChanged {
+                       add { Events.AddHandler (PropertyDescriptorChangedEvent, value); }
+                       remove { Events.RemoveHandler (PropertyDescriptorChangedEvent, value); }
+               }
+
+               public event EventHandler ReadOnlyChanged {
+                       add { Events.AddHandler (ReadOnlyChangedEvent, value); }
+                       remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
+               }
+
+               public event EventHandler WidthChanged {
+                       add { Events.AddHandler (WidthChangedEvent, value); }
+                       remove { Events.RemoveHandler (WidthChangedEvent, value); }
+               }
                #endregion      // Events
        }
 }