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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2005 Novell, Inc.
24 // Peter Bartok <pbartok@novell.com>
25 // Jordi Mas i Hernandez <jordi@ximian.com>
31 using System.Collections;
32 using System.ComponentModel;
34 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
39 namespace System.Windows.Forms
41 [DesignTimeVisible(false)]
43 public class DataGridTableStyle : Component, IDataGridEditingService
45 public static DataGridTableStyle DefaultTableStyle = new DataGridTableStyle (true);
47 #region Local Variables
48 private static readonly Color def_alternating_backcolor = ThemeEngine.Current.DataGridAlternatingBackColor;
49 private static readonly Color def_backcolor = ThemeEngine.Current.DataGridBackColor;
50 private static readonly Color def_forecolor = SystemColors.WindowText;
51 private static readonly Color def_gridline_color = ThemeEngine.Current.DataGridGridLineColor;
52 private static readonly Color def_header_backcolor = ThemeEngine.Current.DataGridHeaderBackColor;
53 private static readonly Font def_header_font = ThemeEngine.Current.DefaultFont;
54 private static readonly Color def_header_forecolor = ThemeEngine.Current.DataGridHeaderForeColor;
55 private static readonly Color def_link_color = ThemeEngine.Current.DataGridLinkColor;
56 private static readonly Color def_link_hovercolor = ThemeEngine.Current.DataGridLinkHoverColor;
57 private static readonly Color def_selection_backcolor = ThemeEngine.Current.DataGridSelectionBackColor;
58 private static readonly Color def_selection_forecolor = ThemeEngine.Current.DataGridSelectionForeColor;
59 private static readonly int def_preferredrow_height = ThemeEngine.Current.DefaultFont.Height + 3;
61 private bool allow_sorting;
62 private DataGrid datagrid;
63 private Color header_forecolor;
64 private string mapping_name;
65 private Color alternating_backcolor;
66 private bool columnheaders_visible;
67 private GridColumnStylesCollection column_styles;
68 private Color gridline_color;
69 private DataGridLineStyle gridline_style;
70 private Color header_backcolor;
71 private Font header_font;
72 private Color link_color;
73 private Color link_hovercolor;
74 private int preferredcolumn_width;
75 private int preferredrow_height;
76 private bool _readonly;
77 private bool rowheaders_visible;
78 private Color selection_backcolor;
79 private Color selection_forecolor;
80 private int rowheaders_width;
81 private Color backcolor;
82 private Color forecolor;
83 private bool is_default;
84 internal ArrayList table_relations;
85 CurrencyManager manager;
86 #endregion // Local Variables
89 public DataGridTableStyle ()
94 public DataGridTableStyle (bool isDefaultTableStyle)
96 is_default = isDefaultTableStyle;
99 header_forecolor = def_header_forecolor;
100 mapping_name = string.Empty;
101 table_relations = new ArrayList ();
102 column_styles = new GridColumnStylesCollection (this);
104 alternating_backcolor = def_alternating_backcolor;
105 columnheaders_visible = true;
106 gridline_color = def_gridline_color;
107 gridline_style = DataGridLineStyle.Solid;
108 header_backcolor = def_header_backcolor;
109 header_font = def_header_font;
110 link_color = def_link_color;
111 link_hovercolor = def_link_hovercolor;
112 preferredcolumn_width = ThemeEngine.Current.DataGridPreferredColumnWidth;
113 preferredrow_height = ThemeEngine.Current.DefaultFont.Height + 3;
115 rowheaders_visible = true;
116 selection_backcolor = def_selection_backcolor;
117 selection_forecolor = def_selection_forecolor;
118 rowheaders_width = 35;
119 backcolor = def_backcolor;
120 forecolor = def_forecolor;
123 public DataGridTableStyle (CurrencyManager listManager)
126 manager = listManager;
130 #region Public Instance Properties
132 public bool AllowSorting {
133 get { return allow_sorting; }
136 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
138 if (allow_sorting != value) {
139 allow_sorting = value;
140 OnAllowSortingChanged (EventArgs.Empty);
142 if (datagrid != null) {
149 public Color AlternatingBackColor {
150 get { return alternating_backcolor; }
153 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
155 if (alternating_backcolor != value) {
156 alternating_backcolor = value;
157 OnAlternatingBackColorChanged (EventArgs.Empty);
159 if (datagrid != null) {
166 public Color BackColor {
167 get { return backcolor; }
170 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
172 if (backcolor != value) {
174 // XXX This should be OnBackColorChanged, MS made a c&p error, I think
175 OnForeColorChanged (EventArgs.Empty);
177 if (datagrid != null) {
185 public bool ColumnHeadersVisible {
187 return columnheaders_visible;
191 if (columnheaders_visible != value) {
192 columnheaders_visible = value;
193 OnColumnHeadersVisibleChanged (EventArgs.Empty);
195 if (datagrid != null) {
203 public virtual DataGrid DataGrid {
204 get { return datagrid; }
206 if (datagrid != value) {
209 /* now set the value on all our column styles */
210 for (int i = 0; i < column_styles.Count; i ++) {
211 column_styles[i].SetDataGridInternal (datagrid);
217 public Color ForeColor {
218 get { return forecolor; }
221 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
223 if (forecolor != value) {
225 // XXX This should be OnForeColorChanged, MS made a c&p error, I think
226 OnBackColorChanged (EventArgs.Empty);
228 if (datagrid != null) {
235 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
237 public virtual GridColumnStylesCollection GridColumnStyles {
238 get { return column_styles; }
241 public Color GridLineColor {
242 get { return gridline_color; }
245 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
247 if (gridline_color != value) {
248 gridline_color = value;
249 OnGridLineColorChanged (EventArgs.Empty);
251 if (datagrid != null) {
258 [DefaultValue(DataGridLineStyle.Solid)]
259 public DataGridLineStyle GridLineStyle {
260 get { return gridline_style; }
263 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
265 if (gridline_style != value) {
266 gridline_style = value;
267 OnGridLineStyleChanged (EventArgs.Empty);
269 if (datagrid != null) {
276 public Color HeaderBackColor {
277 get { return header_backcolor; }
280 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
282 if (value == Color.Empty) {
283 throw new ArgumentNullException ("Color.Empty value is invalid.");
286 if (header_backcolor != value) {
287 header_backcolor = value;
288 OnHeaderBackColorChanged (EventArgs.Empty);
290 if (datagrid != null) {
299 public Font HeaderFont {
300 get { return header_font; }
303 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
306 value = def_header_font;
308 if (header_font != value) {
310 OnHeaderFontChanged (EventArgs.Empty);
312 if (datagrid != null) {
319 public Color HeaderForeColor {
320 get { return header_forecolor; }
323 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
325 if (header_forecolor != value) {
326 header_forecolor = value;
328 if (datagrid != null) {
332 OnHeaderForeColorChanged (EventArgs.Empty);
337 public Color LinkColor {
338 get { return link_color; }
341 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
343 if (link_color != value) {
346 if (datagrid != null) {
350 OnLinkColorChanged (EventArgs.Empty);
356 [EditorBrowsable(EditorBrowsableState.Never)]
358 public Color LinkHoverColor {
359 get { return link_hovercolor; }
361 if (link_hovercolor != value) {
362 link_hovercolor = value;
363 // XXX MS doesn't emit this event (even though they should...)
364 // OnLinkHoverColorChanged (EventArgs.Empty);
369 [Editor("System.Windows.Forms.Design.DataGridTableStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
370 public string MappingName {
371 get { return mapping_name; }
376 if (mapping_name != value) {
377 mapping_name = value;
378 OnMappingNameChanged (EventArgs.Empty);
384 [TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
386 public int PreferredColumnWidth {
387 get { return preferredcolumn_width; }
390 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
393 throw new ArgumentException ("PreferredColumnWidth is less than 0");
396 if (preferredcolumn_width != value) {
397 preferredcolumn_width = value;
398 OnPreferredColumnWidthChanged (EventArgs.Empty);
404 public int PreferredRowHeight {
405 get { return preferredrow_height; }
408 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
410 if (preferredrow_height != value) {
411 preferredrow_height = value;
412 OnPreferredRowHeightChanged (EventArgs.Empty);
417 [DefaultValue(false)]
418 public virtual bool ReadOnly {
419 get { return _readonly; }
421 if (_readonly != value) {
423 OnReadOnlyChanged (EventArgs.Empty);
429 public bool RowHeadersVisible {
430 get { return rowheaders_visible; }
432 if (rowheaders_visible != value) {
433 rowheaders_visible = value;
434 OnRowHeadersVisibleChanged (EventArgs.Empty);
441 public int RowHeaderWidth {
442 get { return rowheaders_width; }
444 if (rowheaders_width != value) {
445 rowheaders_width = value;
446 OnRowHeaderWidthChanged (EventArgs.Empty);
451 public Color SelectionBackColor {
452 get { return selection_backcolor; }
455 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
457 if (selection_backcolor != value) {
458 selection_backcolor = value;
459 OnSelectionBackColorChanged (EventArgs.Empty);
464 [Description("The foreground color for the current data grid row")]
465 public Color SelectionForeColor {
466 get { return selection_forecolor; }
469 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
471 if (selection_forecolor != value) {
472 selection_forecolor = value;
473 OnSelectionForeColorChanged (EventArgs.Empty);
478 #endregion // Public Instance Properties
480 #region Private Instance Properties
481 internal DataGridLineStyle CurrentGridLineStyle {
483 if (is_default && datagrid != null) {
484 return datagrid.GridLineStyle;
487 return gridline_style;
491 internal Color CurrentGridLineColor {
493 if (is_default && datagrid != null) {
494 return datagrid.GridLineColor;
497 return gridline_color;
501 internal Color CurrentHeaderBackColor {
503 if (is_default && datagrid != null) {
504 return datagrid.HeaderBackColor;
507 return header_backcolor;
511 internal Color CurrentHeaderForeColor {
513 if (is_default && datagrid != null) {
514 return datagrid.HeaderForeColor;
517 return header_forecolor;
521 internal int CurrentPreferredColumnWidth {
523 if (is_default && datagrid != null) {
524 return datagrid.PreferredColumnWidth;
527 return preferredcolumn_width;
531 internal int CurrentPreferredRowHeight {
533 if (is_default && datagrid != null) {
534 return datagrid.PreferredRowHeight;
537 return preferredrow_height;
541 internal bool CurrentRowHeadersVisible {
543 if (is_default && datagrid != null) {
544 return datagrid.RowHeadersVisible;
547 return rowheaders_visible;
551 internal bool HasRelations {
552 get { return table_relations.Count > 0; }
555 internal string[] Relations {
557 string[] rel = new string[table_relations.Count];
558 table_relations.CopyTo (rel, 0);
563 #endregion Private Instance Properties
565 #region Public Instance Methods
568 public bool BeginEdit (DataGridColumnStyle gridColumn, int rowNumber)
570 throw new NotImplementedException ();
573 protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop)
575 return CreateGridColumn (prop, false);
578 protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop, bool isDefault)
580 if (prop.PropertyType == typeof (bool))
581 return new DataGridBoolColumn (prop, isDefault);
583 // At least to special cases with formats
584 if (prop.PropertyType.Equals (typeof (DateTime))) {
585 return new DataGridTextBoxColumn (prop, "d", isDefault);
588 if (prop.PropertyType.Equals (typeof (Int32)) ||
589 prop.PropertyType.Equals (typeof (Int16))) {
590 return new DataGridTextBoxColumn (prop, "G", isDefault);
593 return new DataGridTextBoxColumn (prop, isDefault);
597 protected override void Dispose (bool disposing)
599 base.Dispose (disposing);
603 public bool EndEdit ( DataGridColumnStyle gridColumn, int rowNumber, bool shouldAbort)
605 throw new NotImplementedException ();
608 protected virtual void OnAllowSortingChanged (EventArgs e)
610 if (AllowSortingChanged != null) {
611 AllowSortingChanged (this, e);
615 protected virtual void OnAlternatingBackColorChanged (EventArgs e)
617 if (AlternatingBackColorChanged != null) {
618 AlternatingBackColorChanged (this, e);
622 protected virtual void OnBackColorChanged (EventArgs e)
624 if (BackColorChanged != null) {
625 BackColorChanged (this, e);
629 protected virtual void OnColumnHeadersVisibleChanged (EventArgs e)
631 if (ColumnHeadersVisibleChanged != null) {
632 ColumnHeadersVisibleChanged (this, e);
636 protected virtual void OnForeColorChanged (EventArgs e)
638 if (ForeColorChanged != null) {
639 ForeColorChanged (this, e);
643 protected virtual void OnGridLineColorChanged (EventArgs e)
645 if (GridLineColorChanged != null) {
646 GridLineColorChanged (this, e);
650 protected virtual void OnGridLineStyleChanged (EventArgs e)
652 if (GridLineStyleChanged != null) {
653 GridLineStyleChanged (this, e);
657 protected virtual void OnHeaderBackColorChanged (EventArgs e)
659 if (HeaderBackColorChanged != null) {
660 HeaderBackColorChanged (this, e);
664 protected virtual void OnHeaderFontChanged (EventArgs e)
666 if (HeaderFontChanged != null) {
667 HeaderFontChanged (this, e);
671 protected virtual void OnHeaderForeColorChanged (EventArgs e)
673 if (HeaderForeColorChanged != null) {
674 HeaderForeColorChanged (this, e);
678 protected virtual void OnLinkColorChanged (EventArgs e)
680 if (LinkColorChanged != null) {
681 LinkColorChanged (this, e);
685 protected virtual void OnLinkHoverColorChanged (EventArgs e)
687 if (LinkHoverColorChanged != null) {
688 LinkHoverColorChanged (this, e);
692 protected virtual void OnMappingNameChanged (EventArgs e)
694 if (MappingNameChanged != null) {
695 MappingNameChanged(this, e);
699 protected virtual void OnPreferredColumnWidthChanged (EventArgs e)
701 if (PreferredColumnWidthChanged != null) {
702 PreferredColumnWidthChanged (this, e);
706 protected virtual void OnPreferredRowHeightChanged (EventArgs e)
708 if (PreferredRowHeightChanged != null) {
709 PreferredRowHeightChanged (this, e);
713 protected virtual void OnReadOnlyChanged (EventArgs e)
715 if (ReadOnlyChanged != null) {
716 ReadOnlyChanged (this, e);
720 protected virtual void OnRowHeadersVisibleChanged (EventArgs e)
722 if (RowHeadersVisibleChanged != null) {
723 RowHeadersVisibleChanged (this, e);
727 protected virtual void OnRowHeaderWidthChanged (EventArgs e)
729 if (RowHeaderWidthChanged != null) {
730 RowHeaderWidthChanged (this, e);
734 protected virtual void OnSelectionBackColorChanged (EventArgs e)
736 if (SelectionBackColorChanged != null) {
737 SelectionBackColorChanged (this, e);
741 protected virtual void OnSelectionForeColorChanged (EventArgs e)
743 if (SelectionForeColorChanged != null) {
744 SelectionForeColorChanged (this, e);
748 public void ResetAlternatingBackColor ()
750 AlternatingBackColor = def_alternating_backcolor;
753 public void ResetBackColor ()
755 BackColor = def_backcolor;
758 public void ResetForeColor ()
760 ForeColor = def_forecolor;
763 public void ResetGridLineColor ()
765 GridLineColor = def_gridline_color;
768 public void ResetHeaderBackColor ()
770 HeaderBackColor = def_header_backcolor;
773 public void ResetHeaderFont ()
775 HeaderFont = def_header_font;
778 public void ResetHeaderForeColor ()
780 HeaderForeColor = def_header_forecolor;
783 public void ResetLinkColor ()
785 LinkColor = def_link_color;
788 public void ResetLinkHoverColor ()
790 LinkHoverColor = def_link_hovercolor;
793 public void ResetSelectionBackColor ()
795 SelectionBackColor = def_selection_backcolor;
798 public void ResetSelectionForeColor ()
800 SelectionForeColor = def_selection_forecolor;
803 protected virtual bool ShouldSerializeAlternatingBackColor ()
805 return (alternating_backcolor != def_alternating_backcolor);
808 protected bool ShouldSerializeBackColor ()
810 return (backcolor != def_backcolor);
813 protected bool ShouldSerializeForeColor ()
815 return (forecolor != def_forecolor);
818 protected virtual bool ShouldSerializeGridLineColor ()
820 return (gridline_color != def_gridline_color);
823 protected virtual bool ShouldSerializeHeaderBackColor ()
825 return (header_backcolor != def_header_backcolor);
828 protected virtual bool ShouldSerializeHeaderForeColor ()
830 return (header_forecolor != def_header_forecolor);
833 protected virtual bool ShouldSerializeLinkColor ()
835 return (link_color != def_link_color);
838 protected virtual bool ShouldSerializeLinkHoverColor ()
840 return (link_hovercolor != def_link_hovercolor);
843 protected bool ShouldSerializePreferredRowHeight ()
845 return (preferredrow_height != def_preferredrow_height);
848 protected bool ShouldSerializeSelectionBackColor ()
850 return (selection_backcolor != def_selection_backcolor);
853 protected virtual bool ShouldSerializeSelectionForeColor ()
855 return (selection_forecolor != def_selection_forecolor);
857 #endregion // Protected Instance Methods
859 #region Private Instance Properties
860 // Create column styles for this TableStyle
861 internal void CreateColumnsForTable (bool onlyBind)
863 CurrencyManager mgr = manager;
864 DataGridColumnStyle st;
867 mgr = datagrid.ListManager;
873 for (int i = 0; i < column_styles.Count; i ++)
874 column_styles[i].bound = false;
876 table_relations.Clear ();
877 PropertyDescriptorCollection propcol = mgr.GetItemProperties ();
879 for (int i = 0; i < propcol.Count; i++)
881 // The column style is already provided by the user
882 st = column_styles[propcol[i].Name];
885 st.Width = CurrentPreferredColumnWidth;
887 st.PropertyDescriptor = propcol[i];
892 if (onlyBind == true) {
896 if (typeof (IBindingList).IsAssignableFrom (propcol[i].PropertyType)) {
897 table_relations.Add (propcol[i].Name);
899 st = CreateGridColumn (propcol[i], true);
902 st.MappingName = propcol[i].Name;
903 st.HeaderText = propcol[i].Name;
904 st.Width = CurrentPreferredColumnWidth;
905 column_styles.Add (st);
911 #endregion Private Instance Properties
914 public event EventHandler AllowSortingChanged;
915 public event EventHandler AlternatingBackColorChanged;
916 public event EventHandler BackColorChanged;
917 public event EventHandler ColumnHeadersVisibleChanged;
918 public event EventHandler ForeColorChanged;
919 public event EventHandler GridLineColorChanged;
920 public event EventHandler GridLineStyleChanged;
921 public event EventHandler HeaderBackColorChanged;
922 public event EventHandler HeaderFontChanged;
923 public event EventHandler HeaderForeColorChanged;
924 public event EventHandler LinkColorChanged;
925 public event EventHandler LinkHoverColorChanged;
926 public event EventHandler MappingNameChanged;
927 public event EventHandler PreferredColumnWidthChanged;
928 public event EventHandler PreferredRowHeightChanged;
929 public event EventHandler ReadOnlyChanged;
930 public event EventHandler RowHeadersVisibleChanged;
931 public event EventHandler RowHeaderWidthChanged;
932 public event EventHandler SelectionBackColorChanged;
933 public event EventHandler SelectionForeColorChanged;