* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridTableStyle.cs
index 91d6869aeb88b7e49af75c79ce08c743840f932f..bcd97f4233c04e3aae516d5f7059c443f75aaf61 100644 (file)
@@ -28,6 +28,7 @@
 // NOT COMPLETE
 //
 
+using System.Collections;
 using System.ComponentModel;
 using System.Drawing;
 using System.Runtime.InteropServices;
@@ -44,17 +45,18 @@ namespace System.Windows.Forms
                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,16 +119,22 @@ 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);
@@ -151,11 +147,11 @@ namespace System.Windows.Forms
                }
 
                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);
@@ -168,11 +164,11 @@ namespace System.Windows.Forms
                }
 
                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);
@@ -204,23 +200,25 @@ namespace System.Windows.Forms
 
                [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);
@@ -239,11 +237,11 @@ 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);
@@ -257,11 +255,11 @@ namespace System.Windows.Forms
 
                [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);
@@ -274,11 +272,11 @@ namespace System.Windows.Forms
                }
 
                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.");
                                }
@@ -297,17 +295,14 @@ namespace System.Windows.Forms
                [AmbientValue(null)]
                [Localizable(true)]
                public Font HeaderFont {
-                       get {
-                               if (header_font != null)
-                                       return header_font;
-
-                               if (datagrid != null)
-                                       return datagrid.HeaderFont;
+                       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);
@@ -320,11 +315,10 @@ namespace System.Windows.Forms
                }
 
                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;
@@ -339,11 +333,11 @@ namespace System.Windows.Forms
                }
 
                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;
 
@@ -360,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;
@@ -374,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);
@@ -390,11 +381,11 @@ 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");
                                }
@@ -408,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);
@@ -421,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;
@@ -436,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;
@@ -451,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;
@@ -464,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);
@@ -478,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);
@@ -563,13 +545,24 @@ namespace System.Windows.Forms
                        }
                }
 
+               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 ();
                }
@@ -581,12 +574,9 @@ namespace System.Windows.Forms
 
                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);
@@ -599,8 +589,6 @@ namespace System.Windows.Forms
 
                                return new DataGridTextBoxColumn (prop, isDefault);
                        }
-
-                       throw new NotImplementedException ();
                }
 
                protected override void Dispose (bool disposing)
@@ -609,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 ();
                }
@@ -851,7 +839,7 @@ namespace System.Windows.Forms
 
                protected bool ShouldSerializePreferredRowHeight ()
                {
-                       return (preferredrow_height != datagrid.def_preferredrow_height);
+                       return (preferredrow_height != def_preferredrow_height);
                }
 
                protected bool ShouldSerializeSelectionBackColor ()
@@ -869,22 +857,29 @@ namespace System.Windows.Forms
                // Create column styles for this TableStyle
                internal void CreateColumnsForTable (bool onlyBind)
                {
-                       CurrencyManager mgr = null;
-                       mgr = datagrid.ListManager;
+                       CurrencyManager mgr = manager;
+                       DataGridColumnStyle st;
 
                        if (mgr == null) {
-                               return;
+                               mgr = datagrid.ListManager;
+
+                               if (mgr == null)
+                                       return;
                        }
 
+                       table_relations.Clear ();
                        PropertyDescriptorCollection propcol = mgr.GetItemProperties ();
 
                        for (int i = 0; i < propcol.Count; i++)
                        {
                                // The column style is already provided by the user
-                               if (column_styles[propcol[i].Name] != null &&
-                                       column_styles[propcol[i].Name].IsDefault == false) {
-                                       column_styles[propcol[i].Name].table_style = this;
-                                       column_styles[propcol[i].Name].SetDataGridInternal (datagrid);
+                               st = column_styles[propcol[i].Name];
+                               if (st != null) {
+                                       if (st.Width == -1)
+                                               st.Width = CurrentPreferredColumnWidth;
+
+                                       st.PropertyDescriptor = propcol[i];
+
                                        continue;
                                }
 
@@ -892,13 +887,10 @@ namespace System.Windows.Forms
                                        continue;
                                }
 
-
-                               // TODO: What to do with relations?
-                               if (propcol[i].ComponentType.ToString () == "System.Data.DataTablePropertyDescriptor") {
-                                       Console.WriteLine ("CreateColumnsForTable::System.Data.DataTablePropertyDescriptor");
-
+                               if (typeof (IBindingList).IsAssignableFrom (propcol[i].PropertyType)) {
+                                       table_relations.Add (propcol[i].Name);
                                } else {
-                                       DataGridColumnStyle st = CreateGridColumn (propcol[i],  true);
+                                       st = CreateGridColumn (propcol[i],  true);
                                        st.grid = datagrid;
                                        st.MappingName = propcol[i].Name;
                                        st.HeaderText = propcol[i].Name;