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 OnBackColorChanged (EventArgs.Empty);
176 if (datagrid != null) {
184 public bool ColumnHeadersVisible {
186 return columnheaders_visible;
190 if (columnheaders_visible != value) {
191 columnheaders_visible = value;
192 OnColumnHeadersVisibleChanged (EventArgs.Empty);
194 if (datagrid != null) {
202 public virtual DataGrid DataGrid {
203 get { return datagrid; }
205 if (datagrid != value) {
208 /* now set the value on all our column styles */
209 for (int i = 0; i < column_styles.Count; i ++) {
210 column_styles[i].SetDataGridInternal (datagrid);
216 public Color ForeColor {
217 get { return forecolor; }
220 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
222 if (forecolor != value) {
224 OnForeColorChanged (EventArgs.Empty);
226 if (datagrid != null) {
233 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
235 public virtual GridColumnStylesCollection GridColumnStyles {
236 get { return column_styles; }
239 public Color GridLineColor {
240 get { return gridline_color; }
243 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
245 if (gridline_color != value) {
246 gridline_color = value;
247 OnGridLineColorChanged (EventArgs.Empty);
249 if (datagrid != null) {
256 [DefaultValue(DataGridLineStyle.Solid)]
257 public DataGridLineStyle GridLineStyle {
258 get { return gridline_style; }
261 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
263 if (gridline_style != value) {
264 gridline_style = value;
265 OnGridLineStyleChanged (EventArgs.Empty);
267 if (datagrid != null) {
274 public Color HeaderBackColor {
275 get { return header_backcolor; }
278 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
280 if (value == Color.Empty) {
281 throw new ArgumentNullException ("Color.Empty value is invalid.");
284 if (header_backcolor != value) {
285 header_backcolor = value;
286 OnHeaderBackColorChanged (EventArgs.Empty);
288 if (datagrid != null) {
297 public Font HeaderFont {
298 get { return header_font; }
301 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
304 value = def_header_font;
306 if (header_font != value) {
308 OnHeaderFontChanged (EventArgs.Empty);
310 if (datagrid != null) {
317 public Color HeaderForeColor {
318 get { return header_forecolor; }
321 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
323 if (header_forecolor != value) {
324 header_forecolor = value;
326 if (datagrid != null) {
330 OnHeaderForeColorChanged (EventArgs.Empty);
335 public Color LinkColor {
336 get { return link_color; }
339 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
341 if (link_color != value) {
344 if (datagrid != null) {
348 OnLinkColorChanged (EventArgs.Empty);
354 [EditorBrowsable(EditorBrowsableState.Never)]
356 public Color LinkHoverColor {
357 get { return link_hovercolor; }
359 if (link_hovercolor != value) {
360 link_hovercolor = value;
361 OnLinkHoverColorChanged (EventArgs.Empty);
366 [Editor("System.Windows.Forms.Design.DataGridTableStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
367 public string MappingName {
368 get { return mapping_name; }
373 if (mapping_name != value) {
374 mapping_name = value;
375 OnMappingNameChanged (EventArgs.Empty);
381 [TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
383 public int PreferredColumnWidth {
384 get { return preferredcolumn_width; }
387 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
390 throw new ArgumentException ("PreferredColumnWidth is less than 0");
393 if (preferredcolumn_width != value) {
394 preferredcolumn_width = value;
395 OnPreferredColumnWidthChanged (EventArgs.Empty);
401 public int PreferredRowHeight {
402 get { return preferredrow_height; }
405 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
407 if (preferredrow_height != value) {
408 preferredrow_height = value;
409 OnPreferredRowHeightChanged (EventArgs.Empty);
414 [DefaultValue(false)]
415 public virtual bool ReadOnly {
416 get { return _readonly; }
418 if (_readonly != value) {
420 OnReadOnlyChanged (EventArgs.Empty);
426 public bool RowHeadersVisible {
427 get { return rowheaders_visible; }
429 if (rowheaders_visible != value) {
430 rowheaders_visible = value;
431 OnRowHeadersVisibleChanged (EventArgs.Empty);
438 public int RowHeaderWidth {
439 get { return rowheaders_width; }
441 if (rowheaders_width != value) {
442 rowheaders_width = value;
443 OnRowHeaderWidthChanged (EventArgs.Empty);
448 public Color SelectionBackColor {
449 get { return selection_backcolor; }
452 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
454 if (selection_backcolor != value) {
455 selection_backcolor = value;
456 OnSelectionBackColorChanged (EventArgs.Empty);
461 [Description("The foreground color for the current data grid row")]
462 public Color SelectionForeColor {
463 get { return selection_forecolor; }
466 throw new ArgumentException ("Cannot change the value of this property on the default DataGridTableStyle.");
468 if (selection_forecolor != value) {
469 selection_forecolor = value;
470 OnSelectionForeColorChanged (EventArgs.Empty);
475 #endregion // Public Instance Properties
477 #region Private Instance Properties
478 internal DataGridLineStyle CurrentGridLineStyle {
480 if (is_default && datagrid != null) {
481 return datagrid.GridLineStyle;
484 return gridline_style;
488 internal Color CurrentGridLineColor {
490 if (is_default && datagrid != null) {
491 return datagrid.GridLineColor;
494 return gridline_color;
498 internal Color CurrentHeaderBackColor {
500 if (is_default && datagrid != null) {
501 return datagrid.HeaderBackColor;
504 return header_backcolor;
508 internal Color CurrentHeaderForeColor {
510 if (is_default && datagrid != null) {
511 return datagrid.HeaderForeColor;
514 return header_forecolor;
518 internal int CurrentPreferredColumnWidth {
520 if (is_default && datagrid != null) {
521 return datagrid.PreferredColumnWidth;
524 return preferredcolumn_width;
528 internal int CurrentPreferredRowHeight {
530 if (is_default && datagrid != null) {
531 return datagrid.PreferredRowHeight;
534 return preferredrow_height;
538 internal bool CurrentRowHeadersVisible {
540 if (is_default && datagrid != null) {
541 return datagrid.RowHeadersVisible;
544 return rowheaders_visible;
548 internal bool HasRelations {
549 get { return table_relations.Count > 0; }
552 internal string[] Relations {
554 string[] rel = new string[table_relations.Count];
555 table_relations.CopyTo (rel, 0);
560 #endregion Private Instance Properties
562 #region Public Instance Methods
565 public bool BeginEdit (DataGridColumnStyle gridColumn, int rowNumber)
567 throw new NotImplementedException ();
570 protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop)
572 return CreateGridColumn (prop, false);
575 protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop, bool isDefault)
577 if (prop.PropertyType == typeof (bool))
578 return new DataGridBoolColumn (prop, isDefault);
580 // At least to special cases with formats
581 if (prop.PropertyType.Equals (typeof (DateTime))) {
582 return new DataGridTextBoxColumn (prop, "d", isDefault);
585 if (prop.PropertyType.Equals (typeof (Int32)) ||
586 prop.PropertyType.Equals (typeof (Int16))) {
587 return new DataGridTextBoxColumn (prop, "G", isDefault);
590 return new DataGridTextBoxColumn (prop, isDefault);
594 protected override void Dispose (bool disposing)
596 base.Dispose (disposing);
600 public bool EndEdit ( DataGridColumnStyle gridColumn, int rowNumber, bool shouldAbort)
602 throw new NotImplementedException ();
605 protected virtual void OnAllowSortingChanged (EventArgs e)
607 if (AllowSortingChanged != null) {
608 AllowSortingChanged (this, e);
612 protected virtual void OnAlternatingBackColorChanged (EventArgs e)
614 if (AlternatingBackColorChanged != null) {
615 AlternatingBackColorChanged (this, e);
619 protected virtual void OnBackColorChanged (EventArgs e)
621 if (BackColorChanged != null) {
622 BackColorChanged (this, e);
626 protected virtual void OnColumnHeadersVisibleChanged (EventArgs e)
628 if (ColumnHeadersVisibleChanged != null) {
629 ColumnHeadersVisibleChanged (this, e);
633 protected virtual void OnForeColorChanged (EventArgs e)
635 if (ForeColorChanged != null) {
636 ForeColorChanged (this, e);
640 protected virtual void OnGridLineColorChanged (EventArgs e)
642 if (GridLineColorChanged != null) {
643 GridLineColorChanged (this, e);
647 protected virtual void OnGridLineStyleChanged (EventArgs e)
649 if (GridLineStyleChanged != null) {
650 GridLineStyleChanged (this, e);
654 protected virtual void OnHeaderBackColorChanged (EventArgs e)
656 if (HeaderBackColorChanged != null) {
657 HeaderBackColorChanged (this, e);
661 protected virtual void OnHeaderFontChanged (EventArgs e)
663 if (HeaderFontChanged != null) {
664 HeaderFontChanged (this, e);
668 protected virtual void OnHeaderForeColorChanged (EventArgs e)
670 if (HeaderForeColorChanged != null) {
671 HeaderForeColorChanged (this, e);
675 protected virtual void OnLinkColorChanged (EventArgs e)
677 if (LinkColorChanged != null) {
678 LinkColorChanged (this, e);
682 protected virtual void OnLinkHoverColorChanged (EventArgs e)
684 if (LinkHoverColorChanged != null) {
685 LinkHoverColorChanged (this, e);
689 protected virtual void OnMappingNameChanged (EventArgs e)
691 if (MappingNameChanged != null) {
692 MappingNameChanged(this, e);
696 protected virtual void OnPreferredColumnWidthChanged (EventArgs e)
698 if (PreferredColumnWidthChanged != null) {
699 PreferredColumnWidthChanged (this, e);
703 protected virtual void OnPreferredRowHeightChanged (EventArgs e)
705 if (PreferredRowHeightChanged != null) {
706 PreferredRowHeightChanged (this, e);
710 protected virtual void OnReadOnlyChanged (EventArgs e)
712 if (ReadOnlyChanged != null) {
713 ReadOnlyChanged (this, e);
717 protected virtual void OnRowHeadersVisibleChanged (EventArgs e)
719 if (RowHeadersVisibleChanged != null) {
720 RowHeadersVisibleChanged (this, e);
724 protected virtual void OnRowHeaderWidthChanged (EventArgs e)
726 if (RowHeaderWidthChanged != null) {
727 RowHeaderWidthChanged (this, e);
731 protected virtual void OnSelectionBackColorChanged (EventArgs e)
733 if (SelectionBackColorChanged != null) {
734 SelectionBackColorChanged (this, e);
738 protected virtual void OnSelectionForeColorChanged (EventArgs e)
740 if (SelectionForeColorChanged != null) {
741 SelectionForeColorChanged (this, e);
745 public void ResetAlternatingBackColor ()
747 AlternatingBackColor = def_alternating_backcolor;
750 public void ResetBackColor ()
752 BackColor = def_backcolor;
755 public void ResetForeColor ()
757 ForeColor = def_forecolor;
760 public void ResetGridLineColor ()
762 GridLineColor = def_gridline_color;
765 public void ResetHeaderBackColor ()
767 HeaderBackColor = def_header_backcolor;
770 public void ResetHeaderFont ()
772 HeaderFont = def_header_font;
775 public void ResetHeaderForeColor ()
777 HeaderForeColor = def_header_forecolor;
780 public void ResetLinkColor ()
782 LinkColor = def_link_color;
785 public void ResetLinkHoverColor ()
787 LinkHoverColor = def_link_hovercolor;
790 public void ResetSelectionBackColor ()
792 SelectionBackColor = def_selection_backcolor;
795 public void ResetSelectionForeColor ()
797 SelectionForeColor = def_selection_forecolor;
800 protected virtual bool ShouldSerializeAlternatingBackColor ()
802 return (alternating_backcolor != def_alternating_backcolor);
805 protected bool ShouldSerializeBackColor ()
807 return (backcolor != def_backcolor);
810 protected bool ShouldSerializeForeColor ()
812 return (forecolor != def_forecolor);
815 protected virtual bool ShouldSerializeGridLineColor ()
817 return (gridline_color != def_gridline_color);
820 protected virtual bool ShouldSerializeHeaderBackColor ()
822 return (header_backcolor != def_header_backcolor);
825 protected virtual bool ShouldSerializeHeaderForeColor ()
827 return (header_forecolor != def_header_forecolor);
830 protected virtual bool ShouldSerializeLinkColor ()
832 return (link_color != def_link_color);
835 protected virtual bool ShouldSerializeLinkHoverColor ()
837 return (link_hovercolor != def_link_hovercolor);
840 protected bool ShouldSerializePreferredRowHeight ()
842 return (preferredrow_height != def_preferredrow_height);
845 protected bool ShouldSerializeSelectionBackColor ()
847 return (selection_backcolor != def_selection_backcolor);
850 protected virtual bool ShouldSerializeSelectionForeColor ()
852 return (selection_forecolor != def_selection_forecolor);
854 #endregion // Protected Instance Methods
856 #region Private Instance Properties
857 // Create column styles for this TableStyle
858 internal void CreateColumnsForTable (bool onlyBind)
860 CurrencyManager mgr = manager;
861 DataGridColumnStyle st;
864 mgr = datagrid.ListManager;
870 for (int i = 0; i < column_styles.Count; i ++)
871 column_styles[i].bound = false;
873 table_relations.Clear ();
874 PropertyDescriptorCollection propcol = mgr.GetItemProperties ();
876 for (int i = 0; i < propcol.Count; i++)
878 // The column style is already provided by the user
879 st = column_styles[propcol[i].Name];
882 st.Width = CurrentPreferredColumnWidth;
884 st.PropertyDescriptor = propcol[i];
889 if (onlyBind == true) {
893 if (typeof (IBindingList).IsAssignableFrom (propcol[i].PropertyType)) {
894 table_relations.Add (propcol[i].Name);
896 st = CreateGridColumn (propcol[i], true);
899 st.MappingName = propcol[i].Name;
900 st.HeaderText = propcol[i].Name;
901 st.Width = CurrentPreferredColumnWidth;
902 column_styles.Add (st);
908 #endregion Private Instance Properties
911 public event EventHandler AllowSortingChanged;
912 public event EventHandler AlternatingBackColorChanged;
913 public event EventHandler BackColorChanged;
914 public event EventHandler ColumnHeadersVisibleChanged;
915 public event EventHandler ForeColorChanged;
916 public event EventHandler GridLineColorChanged;
917 public event EventHandler GridLineStyleChanged;
918 public event EventHandler HeaderBackColorChanged;
919 public event EventHandler HeaderFontChanged;
920 public event EventHandler HeaderForeColorChanged;
921 public event EventHandler LinkColorChanged;
922 public event EventHandler LinkHoverColorChanged;
923 public event EventHandler MappingNameChanged;
924 public event EventHandler PreferredColumnWidthChanged;
925 public event EventHandler PreferredRowHeightChanged;
926 public event EventHandler ReadOnlyChanged;
927 public event EventHandler RowHeadersVisibleChanged;
928 public event EventHandler RowHeaderWidthChanged;
929 public event EventHandler SelectionBackColorChanged;
930 public event EventHandler SelectionForeColorChanged;