* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridTableStyle.cs
index e6fece740752aa72d7967e2a508f0bb79c6c45b0..bcd97f4233c04e3aae516d5f7059c443f75aaf61 100644 (file)
@@ -28,6 +28,7 @@
 // NOT COMPLETE
 //
 
+using System.Collections;
 using System.ComponentModel;
 using System.Drawing;
 using System.Runtime.InteropServices;
@@ -42,19 +43,20 @@ namespace System.Windows.Forms
        public class DataGridTableStyle : Component, IDataGridEditingService
        {
                public static DataGridTableStyle DefaultTableStyle = new DataGridTableStyle (true);
-               
+
                #region Local Variables
-               private static readonly Color           def_alternating_backcolor = SystemColors.Window;
-               private static readonly Color           def_backcolor = SystemColors.Window;
+               private static readonly Color           def_alternating_backcolor = ThemeEngine.Current.DataGridAlternatingBackColor;
+               private static readonly Color           def_backcolor = ThemeEngine.Current.DataGridBackColor;
                private static readonly Color           def_forecolor = SystemColors.WindowText;
-               private static readonly Color           def_gridline_color = SystemColors.Control;
-               private static readonly Color           def_header_backcolor = SystemColors.Control;
-               private static readonly Font            def_header_font = null;
-               private static readonly Color           def_header_forecolor = SystemColors.ControlText;
-               private static readonly Color           def_link_color = SystemColors.HotTrack;
-               private static readonly Color           def_link_hovercolor = SystemColors.HotTrack;
-               private static readonly Color           def_selection_backcolor = SystemColors.ActiveCaption;
-               private static readonly Color           def_selection_forecolor = SystemColors.ActiveCaptionText;
+               private static readonly Color           def_gridline_color = ThemeEngine.Current.DataGridGridLineColor;
+               private static readonly Color           def_header_backcolor = ThemeEngine.Current.DataGridHeaderBackColor;
+               private static readonly Font            def_header_font = ThemeEngine.Current.DefaultFont;
+               private static readonly Color           def_header_forecolor = ThemeEngine.Current.DataGridHeaderForeColor;
+               private static readonly Color           def_link_color = ThemeEngine.Current.DataGridLinkColor;
+               private static readonly Color           def_link_hovercolor = ThemeEngine.Current.DataGridLinkHoverColor;
+               private static readonly Color           def_selection_backcolor = ThemeEngine.Current.DataGridSelectionBackColor;
+               private static readonly Color           def_selection_forecolor = ThemeEngine.Current.DataGridSelectionForeColor;
+               private static readonly int             def_preferredrow_height = ThemeEngine.Current.DefaultFont.Height + 3;
 
                private bool                            allow_sorting;
                private DataGrid                        datagrid;
@@ -79,36 +81,24 @@ namespace System.Windows.Forms
                private Color                           backcolor;
                private Color                           forecolor;
                private bool                            is_default;
+               internal ArrayList                      table_relations;
                CurrencyManager                         manager;
                #endregion      // Local Variables
 
                #region Constructors
                public DataGridTableStyle ()
+                       : this (false)
                {
-                       CommonConstructor ();
-                       is_default = false;
                }
 
                public DataGridTableStyle (bool isDefaultTableStyle)
                {
-                       CommonConstructor ();
                        is_default = isDefaultTableStyle;
-               }
-
-               // TODO: What to do with the CurrencyManager
-               public DataGridTableStyle (CurrencyManager listManager)
-               {
-                       CommonConstructor ();
-                       is_default = false;
-                       manager = listManager;
-               }
-
-               private void CommonConstructor ()
-               {
                        allow_sorting = true;
                        datagrid = null;
                        header_forecolor = def_header_forecolor;
                        mapping_name = string.Empty;
+                       table_relations = new ArrayList ();
                        column_styles = new GridColumnStylesCollection (this);
 
                        alternating_backcolor = def_alternating_backcolor;
@@ -129,45 +119,63 @@ namespace System.Windows.Forms
                        backcolor = def_backcolor;
                        forecolor = def_forecolor;
                }
+
+               public DataGridTableStyle (CurrencyManager listManager)
+                       : this (false)
+               {
+                       manager = listManager;
+               }
                #endregion
 
                #region Public Instance Properties
                [DefaultValue(true)]
                public bool AllowSorting {
-                       get {
-                               return allow_sorting;
-                       }
-
+                       get { return allow_sorting; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (allow_sorting != value) {
                                        allow_sorting = value;
                                        OnAllowSortingChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
 
                public Color AlternatingBackColor {
-                       get {
-                               return alternating_backcolor;
-                       }
-
+                       get { return alternating_backcolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (alternating_backcolor != value) {
                                        alternating_backcolor = value;
                                        OnAlternatingBackColorChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
 
                public Color BackColor {
-                       get {
-                               return backcolor;
-                       }
-
+                       get { return backcolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (backcolor != value) {
                                        backcolor = value;
                                        OnBackColorChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
@@ -182,32 +190,42 @@ namespace System.Windows.Forms
                                if (columnheaders_visible != value) {
                                        columnheaders_visible = value;
                                        OnColumnHeadersVisibleChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
 
                [Browsable(false)]
                public virtual DataGrid DataGrid {
-                       get {
-                               return datagrid;
-                       }
-
+                       get { return datagrid; }
                        set {
                                if (datagrid != value) {
                                        datagrid = value;
+
+                                       /* now set the value on all our column styles */
+                                       for (int i = 0; i < column_styles.Count; i ++) {
+                                               column_styles[i].SetDataGridInternal (datagrid);
+                                       }
                                }
                        }
                }
 
                public Color ForeColor {
-                       get {
-                               return forecolor;
-                       }
-
+                       get { return forecolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (forecolor != value) {
                                        forecolor = value;
                                        OnForeColorChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
@@ -219,41 +237,57 @@ namespace System.Windows.Forms
                }
 
                public Color GridLineColor {
-                       get {
-                               return gridline_color;
-                       }
-
+                       get { return gridline_color; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (gridline_color != value) {
                                        gridline_color = value;
                                        OnGridLineColorChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
 
                [DefaultValue(DataGridLineStyle.Solid)]
                public DataGridLineStyle GridLineStyle {
-                       get {
-                               return gridline_style;
-                       }
-
+                       get { return gridline_style; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (gridline_style != value) {
                                        gridline_style = value;
                                        OnGridLineStyleChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
 
                public Color HeaderBackColor {
-                       get {
-                               return header_backcolor;
-                       }
-
+                       get { return header_backcolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
+                               if (value == Color.Empty) {
+                                       throw new ArgumentNullException ("Color.Empty value is invalid.");
+                               }
+
                                if (header_backcolor != value) {
                                        header_backcolor = value;
                                        OnHeaderBackColorChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
@@ -261,46 +295,56 @@ namespace System.Windows.Forms
                [AmbientValue(null)]
                [Localizable(true)]
                public Font HeaderFont {
-                       get {
-                               if (header_font != null)
-                                       return header_font;
-
-                               if (datagrid != null)
-                                       return datagrid.Font;
+                       get { return header_font; }
+                       set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
 
-                               return ThemeEngine.Current.DefaultFont;
-                       }
+                               if (value == null)
+                                       value = def_header_font;
 
-                       set {
                                if (header_font != value) {
                                        header_font = value;
                                        OnHeaderFontChanged (EventArgs.Empty);
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
                                }
                        }
                }
 
                public Color HeaderForeColor {
-                       get {
-                               return header_forecolor;
-                       }
-
+                       get { return header_forecolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
 
                                if (header_forecolor != value) {
                                        header_forecolor = value;
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
+
                                        OnHeaderForeColorChanged (EventArgs.Empty);
                                }
                        }
                }
 
                public Color LinkColor {
-                       get {
-                               return link_color;
-                       }
-
+                       get { return link_color; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (link_color != value) {
                                        link_color = value;
+
+                                       if (datagrid != null) {
+                                               datagrid.Refresh ();
+                                       }
+
                                        OnLinkColorChanged (EventArgs.Empty);
                                }
                        }
@@ -310,10 +354,7 @@ namespace System.Windows.Forms
                [EditorBrowsable(EditorBrowsableState.Never)]
                [Browsable(false)]
                public Color LinkHoverColor {
-                       get {
-                               return link_hovercolor;
-                       }
-
+                       get { return link_hovercolor; }
                        set {
                                if (link_hovercolor != value) {
                                        link_hovercolor = value;
@@ -324,11 +365,11 @@ namespace System.Windows.Forms
 
                [Editor("System.Windows.Forms.Design.DataGridTableStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
                public string MappingName {
-                       get {
-                               return mapping_name;
-                       }
-
+                       get { return mapping_name; }
                        set {
+                               if (value == null)
+                                       value = "";
+
                                if (mapping_name != value) {
                                        mapping_name = value;
                                        OnMappingNameChanged (EventArgs.Empty);
@@ -340,15 +381,15 @@ namespace System.Windows.Forms
                [TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
                [Localizable(true)]
                public int PreferredColumnWidth {
-                       get {
-                               return preferredcolumn_width;
-                       }
-
+                       get { return preferredcolumn_width; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (value < 0) {
                                        throw new ArgumentException ("PreferredColumnWidth is less than 0");
                                }
-                               
+
                                if (preferredcolumn_width != value) {
                                        preferredcolumn_width = value;
                                        OnPreferredColumnWidthChanged (EventArgs.Empty);
@@ -358,11 +399,11 @@ namespace System.Windows.Forms
 
                [Localizable(true)]
                public int PreferredRowHeight {
-                       get {
-                               return preferredrow_height;
-                       }
-
+                       get { return preferredrow_height; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (preferredrow_height != value) {
                                        preferredrow_height = value;
                                        OnPreferredRowHeightChanged (EventArgs.Empty);
@@ -371,11 +412,8 @@ namespace System.Windows.Forms
                }
 
                [DefaultValue(false)]
-               public bool ReadOnly {
-                       get {
-                               return _readonly;
-                       }
-
+               public virtual bool ReadOnly {
+                       get { return _readonly; }
                        set {
                                if (_readonly != value) {
                                        _readonly = value;
@@ -386,10 +424,7 @@ namespace System.Windows.Forms
 
                [DefaultValue(true)]
                public bool RowHeadersVisible {
-                       get {
-                               return rowheaders_visible;
-                       }
-
+                       get { return rowheaders_visible; }
                        set {
                                if (rowheaders_visible != value) {
                                        rowheaders_visible = value;
@@ -401,10 +436,7 @@ namespace System.Windows.Forms
                [DefaultValue(35)]
                [Localizable(true)]
                public int RowHeaderWidth {
-                       get {
-                               return rowheaders_width;
-                       }
-
+                       get { return rowheaders_width; }
                        set {
                                if (rowheaders_width != value) {
                                        rowheaders_width = value;
@@ -414,11 +446,11 @@ namespace System.Windows.Forms
                }
 
                public Color SelectionBackColor {
-                       get {
-                               return selection_backcolor;
-                       }
-
+                       get { return selection_backcolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (selection_backcolor != value) {
                                        selection_backcolor = value;
                                        OnSelectionBackColorChanged (EventArgs.Empty);
@@ -428,11 +460,11 @@ namespace System.Windows.Forms
 
                [Description("The foreground color for the current data grid row")]
                public Color SelectionForeColor  {
-                       get {
-                               return selection_forecolor;
-                       }
-
+                       get { return selection_forecolor; }
                        set {
+                               if (is_default)
+                                       throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
+
                                if (selection_forecolor != value) {
                                        selection_forecolor = value;
                                        OnSelectionForeColorChanged (EventArgs.Empty);
@@ -442,41 +474,121 @@ namespace System.Windows.Forms
 
                #endregion      // Public Instance Properties
 
+               #region Private Instance Properties
+               internal DataGridLineStyle CurrentGridLineStyle {
+                       get {
+                               if (is_default && datagrid != null) {
+                                       return datagrid.GridLineStyle;
+                               }
+
+                               return gridline_style;
+                       }
+               }
+
+               internal Color CurrentGridLineColor {
+                       get {
+                               if (is_default && datagrid != null) {
+                                       return datagrid.GridLineColor;
+                               }
+
+                               return gridline_color;
+                       }
+               }
+
+               internal Color CurrentHeaderBackColor {
+                       get {
+                               if (is_default && datagrid != null) {
+                                       return datagrid.HeaderBackColor;
+                               }
+
+                               return header_backcolor;
+                       }
+               }
+
+               internal Color CurrentHeaderForeColor {
+                       get {
+                               if (is_default && datagrid != null) {
+                                       return datagrid.HeaderForeColor;
+                               }
+
+                               return header_forecolor;
+                       }
+               }
+
+               internal int CurrentPreferredColumnWidth {
+                       get {
+                               if (is_default && datagrid != null) {
+                                       return datagrid.PreferredColumnWidth;
+                               }
+
+                               return preferredcolumn_width;
+                       }
+               }
+
+               internal int CurrentPreferredRowHeight {
+                       get {
+                               if (is_default && datagrid != null) {
+                                       return datagrid.PreferredRowHeight;
+                               }
+
+                               return preferredrow_height;
+                       }
+               }
+               
+               internal bool CurrentRowHeadersVisible {
+                       get {
+                               if (is_default && datagrid != null) {
+                                       return datagrid.RowHeadersVisible;
+                               }
+
+                               return rowheaders_visible;
+                       }
+               }
+
+               internal bool HasRelations {
+                       get { return table_relations.Count > 0; }
+               }
+
+               internal string[] Relations {
+                       get {
+                               string[] rel = new string[table_relations.Count];
+                               table_relations.CopyTo (rel, 0);
+                               return rel;
+                       }
+               }
+
+               #endregion Private Instance Properties
+
                #region Public Instance Methods
 
                [MonoTODO]
-               public virtual bool BeginEdit (DataGridColumnStyle gridColumn,  int rowNumber)
+               public bool BeginEdit (DataGridColumnStyle gridColumn,  int rowNumber)
                {
                        throw new NotImplementedException ();
                }
 
                protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop)
-               {       
+               {
                        return CreateGridColumn (prop,  false);
                }
-               
+
                protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop,  bool isDefault)
-               {                       
-                       if (DataGridBoolColumn.CanRenderType (prop.PropertyType)) {
+               {
+                       if (prop.PropertyType == typeof (bool))
                                return new DataGridBoolColumn (prop, isDefault);
-                       }
-                       
-                       if (DataGridTextBoxColumn.CanRenderType (prop.PropertyType)) {
-                               
+                       else {
                                // At least to special cases with formats
                                if (prop.PropertyType.Equals (typeof (DateTime))) {
-                                       return new DataGridTextBoxColumn (prop, "d", isDefault);                                        
+                                       return new DataGridTextBoxColumn (prop, "d", isDefault);
                                }
-                               
+
                                if (prop.PropertyType.Equals (typeof (Int32)) ||
                                        prop.PropertyType.Equals (typeof (Int16))) {
-                                       return new DataGridTextBoxColumn (prop, "G", isDefault);                                        
+                                       return new DataGridTextBoxColumn (prop, "G", isDefault);
                                }
-                               
-                               return new DataGridTextBoxColumn (prop, isDefault);                             
+
+                               return new DataGridTextBoxColumn (prop, isDefault);
                        }
-                       
-                       throw new NotImplementedException ();
                }
 
                protected override void Dispose (bool disposing)
@@ -485,7 +597,7 @@ namespace System.Windows.Forms
                }
 
                [MonoTODO]
-               public virtual bool EndEdit ( DataGridColumnStyle gridColumn,  int rowNumber,  bool shouldAbort)
+               public bool EndEdit ( DataGridColumnStyle gridColumn,  int rowNumber,  bool shouldAbort)
                {
                        throw new NotImplementedException ();
                }
@@ -727,7 +839,7 @@ namespace System.Windows.Forms
 
                protected bool ShouldSerializePreferredRowHeight ()
                {
-                       return (preferredrow_height != ThemeEngine.Current.DefaultFont.Height + 3);
+                       return (preferredrow_height != def_preferredrow_height);
                }
 
                protected bool ShouldSerializeSelectionBackColor ()
@@ -740,48 +852,55 @@ namespace System.Windows.Forms
                        return (selection_forecolor != def_selection_forecolor);
                }
                #endregion      // Protected Instance Methods
-               
-               #region Private Instance Properties             
+
+               #region Private Instance Properties
                // Create column styles for this TableStyle
-               internal void CreateColumnsForTable (
+               internal void CreateColumnsForTable (bool onlyBind)
                {
-                       CurrencyManager mgr = null;                     
-                       
-                       /*if (manager != null) {
-                               mgr = manager;
-                       } else {
-                               if (datagrid != null) {
-                                       mgr = datagrid.ListManager;
-                               }
-                       }*/
+                       CurrencyManager mgr = manager;
+                       DataGridColumnStyle st;
 
-                       mgr = datagrid.ListManager;
-                       
                        if (mgr == null) {
-                               return;
+                               mgr = datagrid.ListManager;
+
+                               if (mgr == null)
+                                       return;
                        }
-                       
-                       column_styles.Clear ();
-                       PropertyDescriptorCollection propcol = mgr.GetItemProperties ();                        
-                       
+
+                       table_relations.Clear ();
+                       PropertyDescriptorCollection propcol = mgr.GetItemProperties ();
+
                        for (int i = 0; i < propcol.Count; i++)
-                       {                               
-                               // TODO: What to do with relations?
-                               if (propcol[i].ComponentType.ToString () == "System.Data.DataTablePropertyDescriptor") {                                        
-                                       Console.WriteLine ("CreateColumnsForTable::System.Data.DataTablePropertyDescriptor");
-                                       
+                       {
+                               // The column style is already provided by the user
+                               st = column_styles[propcol[i].Name];
+                               if (st != null) {
+                                       if (st.Width == -1)
+                                               st.Width = CurrentPreferredColumnWidth;
+
+                                       st.PropertyDescriptor = propcol[i];
+
+                                       continue;
+                               }
+
+                               if (onlyBind == true) {
+                                       continue;
+                               }
+
+                               if (typeof (IBindingList).IsAssignableFrom (propcol[i].PropertyType)) {
+                                       table_relations.Add (propcol[i].Name);
                                } else {
-                                       DataGridColumnStyle st = CreateGridColumn (propcol[i],  true);
-                                       st.TableStyle = this;
+                                       st = CreateGridColumn (propcol[i],  true);
+                                       st.grid = datagrid;
                                        st.MappingName = propcol[i].Name;
                                        st.HeaderText = propcol[i].Name;
-                                       st.Width = PreferredColumnWidth;                                        
-                                       column_styles.Add (st);                                 
-                               }                               
+                                       st.Width = CurrentPreferredColumnWidth;
+                                       column_styles.Add (st);
+                               }
                        }
-                       
+
                }
-               
+
                #endregion Private Instance Properties
 
                #region Events