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.ComponentModel;
33 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
38 namespace System.Windows.Forms
40 [DesignTimeVisible(false)]
42 public class DataGridTableStyle : Component, IDataGridEditingService
44 public static DataGridTableStyle DefaultTableStyle = new DataGridTableStyle (true);
46 #region Local Variables
47 private static readonly Color def_alternating_backcolor = SystemColors.Window;
48 private static readonly Color def_backcolor = SystemColors.Window;
49 private static readonly Color def_forecolor = SystemColors.WindowText;
50 private static readonly Color def_gridline_color = SystemColors.Control;
51 private static readonly Color def_header_backcolor = SystemColors.Control;
52 private static readonly Font def_header_font = null;
53 private static readonly Color def_header_forecolor = SystemColors.ControlText;
54 private static readonly Color def_link_color = SystemColors.HotTrack;
55 private static readonly Color def_link_hovercolor = SystemColors.HotTrack;
56 private static readonly Color def_selection_backcolor = SystemColors.ActiveCaption;
57 private static readonly Color def_selection_forecolor = SystemColors.ActiveCaptionText;
59 private bool allow_sorting;
60 private DataGrid datagrid;
61 private Color header_forecolor;
62 private string mapping_name;
63 private Color alternating_backcolor;
64 private bool columnheaders_visible;
65 private GridColumnStylesCollection column_styles;
66 private Color gridline_color;
67 private DataGridLineStyle gridline_style;
68 private Color header_backcolor;
69 private Font header_font;
70 private Color link_color;
71 private Color link_hovercolor;
72 private int preferredcolumn_width;
73 private int preferredrow_height;
74 private bool _readonly;
75 private bool rowheaders_visible;
76 private Color selection_backcolor;
77 private Color selection_forecolor;
78 private int rowheaders_width;
79 private Color backcolor;
80 private Color forecolor;
81 private bool is_default;
82 CurrencyManager manager;
83 #endregion // Local Variables
86 public DataGridTableStyle ()
92 public DataGridTableStyle (bool isDefaultTableStyle)
95 is_default = isDefaultTableStyle;
98 // TODO: What to do with the CurrencyManager
99 public DataGridTableStyle (CurrencyManager listManager)
101 CommonConstructor ();
103 manager = listManager;
106 private void CommonConstructor ()
108 allow_sorting = true;
110 header_forecolor = def_header_forecolor;
111 mapping_name = string.Empty;
112 column_styles = new GridColumnStylesCollection (this);
114 alternating_backcolor = def_alternating_backcolor;
115 columnheaders_visible = true;
116 gridline_color = def_gridline_color;
117 gridline_style = DataGridLineStyle.Solid;
118 header_backcolor = def_header_backcolor;
119 header_font = def_header_font;
120 link_color = def_link_color;
121 link_hovercolor = def_link_hovercolor;
122 preferredcolumn_width = ThemeEngine.Current.DataGridPreferredColumnWidth;
123 preferredrow_height = ThemeEngine.Current.DefaultFont.Height + 3;
125 rowheaders_visible = true;
126 selection_backcolor = def_selection_backcolor;
127 selection_forecolor = def_selection_forecolor;
128 rowheaders_width = 35;
129 backcolor = def_backcolor;
130 forecolor = def_forecolor;
134 #region Public Instance Properties
136 public bool AllowSorting {
138 return allow_sorting;
142 if (allow_sorting != value) {
143 allow_sorting = value;
144 OnAllowSortingChanged (EventArgs.Empty);
146 if (datagrid != null) {
153 public Color AlternatingBackColor {
155 return alternating_backcolor;
159 if (alternating_backcolor != value) {
160 alternating_backcolor = value;
161 OnAlternatingBackColorChanged (EventArgs.Empty);
163 if (datagrid != null) {
170 public Color BackColor {
176 if (backcolor != value) {
178 OnBackColorChanged (EventArgs.Empty);
180 if (datagrid != null) {
188 public bool ColumnHeadersVisible {
190 return columnheaders_visible;
194 if (columnheaders_visible != value) {
195 columnheaders_visible = value;
196 OnColumnHeadersVisibleChanged (EventArgs.Empty);
198 if (datagrid != null) {
206 public virtual DataGrid DataGrid {
212 if (datagrid != value) {
218 public Color ForeColor {
224 if (forecolor != value) {
226 OnForeColorChanged (EventArgs.Empty);
228 if (datagrid != null) {
235 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
237 public virtual GridColumnStylesCollection GridColumnStyles {
238 get { return column_styles; }
241 public Color GridLineColor {
243 return gridline_color;
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 {
261 return gridline_style;
265 if (gridline_style != value) {
266 gridline_style = value;
267 OnGridLineStyleChanged (EventArgs.Empty);
269 if (datagrid != null) {
276 public Color HeaderBackColor {
278 return header_backcolor;
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 {
301 if (header_font != null)
304 if (datagrid != null)
305 return datagrid.HeaderFont;
307 return ThemeEngine.Current.DefaultFont;
311 if (header_font != value) {
313 OnHeaderFontChanged (EventArgs.Empty);
315 if (datagrid != null) {
322 public Color HeaderForeColor {
324 return header_forecolor;
329 if (header_forecolor != value) {
330 header_forecolor = value;
332 if (datagrid != null) {
336 OnHeaderForeColorChanged (EventArgs.Empty);
341 public Color LinkColor {
347 if (link_color != value) {
350 if (datagrid != null) {
354 OnLinkColorChanged (EventArgs.Empty);
360 [EditorBrowsable(EditorBrowsableState.Never)]
362 public Color LinkHoverColor {
364 return link_hovercolor;
368 if (link_hovercolor != value) {
369 link_hovercolor = value;
370 OnLinkHoverColorChanged (EventArgs.Empty);
375 [Editor("System.Windows.Forms.Design.DataGridTableStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
376 public string MappingName {
382 if (mapping_name != value) {
383 mapping_name = value;
384 OnMappingNameChanged (EventArgs.Empty);
390 [TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
392 public int PreferredColumnWidth {
394 return preferredcolumn_width;
399 throw new ArgumentException ("PreferredColumnWidth is less than 0");
402 if (preferredcolumn_width != value) {
403 preferredcolumn_width = value;
404 OnPreferredColumnWidthChanged (EventArgs.Empty);
410 public int PreferredRowHeight {
412 return preferredrow_height;
416 if (preferredrow_height != value) {
417 preferredrow_height = value;
418 OnPreferredRowHeightChanged (EventArgs.Empty);
423 [DefaultValue(false)]
424 public virtual bool ReadOnly {
430 if (_readonly != value) {
432 OnReadOnlyChanged (EventArgs.Empty);
438 public bool RowHeadersVisible {
440 return rowheaders_visible;
444 if (rowheaders_visible != value) {
445 rowheaders_visible = value;
446 OnRowHeadersVisibleChanged (EventArgs.Empty);
453 public int RowHeaderWidth {
455 return rowheaders_width;
459 if (rowheaders_width != value) {
460 rowheaders_width = value;
461 OnRowHeaderWidthChanged (EventArgs.Empty);
466 public Color SelectionBackColor {
468 return selection_backcolor;
472 if (selection_backcolor != value) {
473 selection_backcolor = value;
474 OnSelectionBackColorChanged (EventArgs.Empty);
479 [Description("The foreground color for the current data grid row")]
480 public Color SelectionForeColor {
482 return selection_forecolor;
486 if (selection_forecolor != value) {
487 selection_forecolor = value;
488 OnSelectionForeColorChanged (EventArgs.Empty);
493 #endregion // Public Instance Properties
495 #region Private Instance Properties
496 internal DataGridLineStyle CurrentGridLineStyle {
498 if (is_default && datagrid != null) {
499 return datagrid.GridLineStyle;
502 return gridline_style;
506 internal Color CurrentGridLineColor {
508 if (is_default && datagrid != null) {
509 return datagrid.GridLineColor;
512 return gridline_color;
516 internal Color CurrentHeaderBackColor {
518 if (is_default && datagrid != null) {
519 return datagrid.HeaderBackColor;
522 return header_backcolor;
526 internal Color CurrentHeaderForeColor {
528 if (is_default && datagrid != null) {
529 return datagrid.HeaderForeColor;
532 return header_forecolor;
536 internal int CurrentPreferredColumnWidth {
538 if (is_default && datagrid != null) {
539 return datagrid.PreferredColumnWidth;
542 return preferredcolumn_width;
546 internal int CurrentPreferredRowHeight {
548 if (is_default && datagrid != null) {
549 return datagrid.PreferredRowHeight;
552 return preferredrow_height;
556 internal bool CurrentRowHeadersVisible {
558 if (is_default && datagrid != null) {
559 return datagrid.RowHeadersVisible;
562 return rowheaders_visible;
567 #endregion Private Instance Properties
569 #region Public Instance Methods
572 public bool BeginEdit (DataGridColumnStyle gridColumn, int rowNumber)
574 throw new NotImplementedException ();
577 protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop)
579 return CreateGridColumn (prop, false);
582 protected internal virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop, bool isDefault)
584 if (DataGridBoolColumn.CanRenderType (prop.PropertyType)) {
585 return new DataGridBoolColumn (prop, isDefault);
588 if (DataGridTextBoxColumn.CanRenderType (prop.PropertyType)) {
590 // At least to special cases with formats
591 if (prop.PropertyType.Equals (typeof (DateTime))) {
592 return new DataGridTextBoxColumn (prop, "d", isDefault);
595 if (prop.PropertyType.Equals (typeof (Int32)) ||
596 prop.PropertyType.Equals (typeof (Int16))) {
597 return new DataGridTextBoxColumn (prop, "G", isDefault);
600 return new DataGridTextBoxColumn (prop, isDefault);
603 throw new NotImplementedException ();
606 protected override void Dispose (bool disposing)
608 base.Dispose (disposing);
612 public bool EndEdit ( DataGridColumnStyle gridColumn, int rowNumber, bool shouldAbort)
614 throw new NotImplementedException ();
617 protected virtual void OnAllowSortingChanged (EventArgs e)
619 if (AllowSortingChanged != null) {
620 AllowSortingChanged (this, e);
624 protected virtual void OnAlternatingBackColorChanged (EventArgs e)
626 if (AlternatingBackColorChanged != null) {
627 AlternatingBackColorChanged (this, e);
631 protected virtual void OnBackColorChanged (EventArgs e)
633 if (BackColorChanged != null) {
634 BackColorChanged (this, e);
638 protected virtual void OnColumnHeadersVisibleChanged (EventArgs e)
640 if (ColumnHeadersVisibleChanged != null) {
641 ColumnHeadersVisibleChanged (this, e);
645 protected virtual void OnForeColorChanged (EventArgs e)
647 if (ForeColorChanged != null) {
648 ForeColorChanged (this, e);
652 protected virtual void OnGridLineColorChanged (EventArgs e)
654 if (GridLineColorChanged != null) {
655 GridLineColorChanged (this, e);
659 protected virtual void OnGridLineStyleChanged (EventArgs e)
661 if (GridLineStyleChanged != null) {
662 GridLineStyleChanged (this, e);
666 protected virtual void OnHeaderBackColorChanged (EventArgs e)
668 if (HeaderBackColorChanged != null) {
669 HeaderBackColorChanged (this, e);
673 protected virtual void OnHeaderFontChanged (EventArgs e)
675 if (HeaderFontChanged != null) {
676 HeaderFontChanged (this, e);
680 protected virtual void OnHeaderForeColorChanged (EventArgs e)
682 if (HeaderForeColorChanged != null) {
683 HeaderForeColorChanged (this, e);
687 protected virtual void OnLinkColorChanged (EventArgs e)
689 if (LinkColorChanged != null) {
690 LinkColorChanged (this, e);
694 protected virtual void OnLinkHoverColorChanged (EventArgs e)
696 if (LinkHoverColorChanged != null) {
697 LinkHoverColorChanged (this, e);
701 protected virtual void OnMappingNameChanged (EventArgs e)
703 if (MappingNameChanged != null) {
704 MappingNameChanged(this, e);
708 protected virtual void OnPreferredColumnWidthChanged (EventArgs e)
710 if (PreferredColumnWidthChanged != null) {
711 PreferredColumnWidthChanged (this, e);
715 protected virtual void OnPreferredRowHeightChanged (EventArgs e)
717 if (PreferredRowHeightChanged != null) {
718 PreferredRowHeightChanged (this, e);
722 protected virtual void OnReadOnlyChanged (EventArgs e)
724 if (ReadOnlyChanged != null) {
725 ReadOnlyChanged (this, e);
729 protected virtual void OnRowHeadersVisibleChanged (EventArgs e)
731 if (RowHeadersVisibleChanged != null) {
732 RowHeadersVisibleChanged (this, e);
736 protected virtual void OnRowHeaderWidthChanged (EventArgs e)
738 if (RowHeaderWidthChanged != null) {
739 RowHeaderWidthChanged (this, e);
743 protected virtual void OnSelectionBackColorChanged (EventArgs e)
745 if (SelectionBackColorChanged != null) {
746 SelectionBackColorChanged (this, e);
750 protected virtual void OnSelectionForeColorChanged (EventArgs e)
752 if (SelectionForeColorChanged != null) {
753 SelectionForeColorChanged (this, e);
757 public void ResetAlternatingBackColor ()
759 AlternatingBackColor = def_alternating_backcolor;
762 public void ResetBackColor ()
764 BackColor = def_backcolor;
767 public void ResetForeColor ()
769 ForeColor = def_forecolor;
772 public void ResetGridLineColor ()
774 GridLineColor = def_gridline_color;
777 public void ResetHeaderBackColor ()
779 HeaderBackColor = def_header_backcolor;
782 public void ResetHeaderFont ()
784 HeaderFont = def_header_font;
787 public void ResetHeaderForeColor ()
789 HeaderForeColor = def_header_forecolor;
792 public void ResetLinkColor ()
794 LinkColor = def_link_color;
797 public void ResetLinkHoverColor ()
799 LinkHoverColor = def_link_hovercolor;
802 public void ResetSelectionBackColor ()
804 SelectionBackColor = def_selection_backcolor;
807 public void ResetSelectionForeColor ()
809 SelectionForeColor = def_selection_forecolor;
812 protected virtual bool ShouldSerializeAlternatingBackColor ()
814 return (alternating_backcolor != def_alternating_backcolor);
817 protected bool ShouldSerializeBackColor ()
819 return (backcolor != def_backcolor);
822 protected bool ShouldSerializeForeColor ()
824 return (forecolor != def_forecolor);
827 protected virtual bool ShouldSerializeGridLineColor ()
829 return (gridline_color != def_gridline_color);
832 protected virtual bool ShouldSerializeHeaderBackColor ()
834 return (header_backcolor != def_header_backcolor);
837 protected virtual bool ShouldSerializeHeaderForeColor ()
839 return (header_forecolor != def_header_forecolor);
842 protected virtual bool ShouldSerializeLinkColor ()
844 return (link_color != def_link_color);
847 protected virtual bool ShouldSerializeLinkHoverColor ()
849 return (link_hovercolor != def_link_hovercolor);
852 protected bool ShouldSerializePreferredRowHeight ()
854 return (preferredrow_height != datagrid.def_preferredrow_height);
857 protected bool ShouldSerializeSelectionBackColor ()
859 return (selection_backcolor != def_selection_backcolor);
862 protected virtual bool ShouldSerializeSelectionForeColor ()
864 return (selection_forecolor != def_selection_forecolor);
866 #endregion // Protected Instance Methods
868 #region Private Instance Properties
869 // Create column styles for this TableStyle
870 internal void CreateColumnsForTable (bool onlyBind)
872 CurrencyManager mgr = null;
873 DataGridColumnStyle st;
874 mgr = datagrid.ListManager;
880 PropertyDescriptorCollection propcol = mgr.GetItemProperties ();
882 for (int i = 0; i < propcol.Count; i++)
884 // The column style is already provided by the user
885 st = column_styles[propcol[i].Name];
887 st.table_style = this;
888 st.SetDataGridInternal (datagrid);
891 st.Width = CurrentPreferredColumnWidth;
896 if (onlyBind == true) {
901 // TODO: What to do with relations?
902 if (propcol[i].ComponentType.ToString () == "System.Data.DataTablePropertyDescriptor") {
903 Console.WriteLine ("CreateColumnsForTable::System.Data.DataTablePropertyDescriptor");
906 st = CreateGridColumn (propcol[i], true);
908 st.MappingName = propcol[i].Name;
909 st.HeaderText = propcol[i].Name;
910 st.Width = CurrentPreferredColumnWidth;
911 column_styles.Add (st);
917 #endregion Private Instance Properties
920 public event EventHandler AllowSortingChanged;
921 public event EventHandler AlternatingBackColorChanged;
922 public event EventHandler BackColorChanged;
923 public event EventHandler ColumnHeadersVisibleChanged;
924 public event EventHandler ForeColorChanged;
925 public event EventHandler GridLineColorChanged;
926 public event EventHandler GridLineStyleChanged;
927 public event EventHandler HeaderBackColorChanged;
928 public event EventHandler HeaderFontChanged;
929 public event EventHandler HeaderForeColorChanged;
930 public event EventHandler LinkColorChanged;
931 public event EventHandler LinkHoverColorChanged;
932 public event EventHandler MappingNameChanged;
933 public event EventHandler PreferredColumnWidthChanged;
934 public event EventHandler PreferredRowHeightChanged;
935 public event EventHandler ReadOnlyChanged;
936 public event EventHandler RowHeadersVisibleChanged;
937 public event EventHandler RowHeaderWidthChanged;
938 public event EventHandler SelectionBackColorChanged;
939 public event EventHandler SelectionForeColorChanged;