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) 2005 Novell, Inc. (http://www.novell.com)
23 // Jordi Mas i Hernandez <jordi@ximian.com>
30 using System.ComponentModel;
31 using System.Runtime.InteropServices;
32 using System.Diagnostics;
34 namespace System.Windows.Forms
36 [DesignTimeVisible(false)]
37 [DefaultProperty("Header")]
39 public abstract class DataGridColumnStyle : Component, IDataGridColumnStyleEditingNotificationService
42 protected class DataGridColumnHeaderAccessibleObject : AccessibleObject
44 #region Local Variables
45 private DataGridColumnStyle owner;
49 public DataGridColumnHeaderAccessibleObject (DataGridColumnStyle columnstyle)
53 #endregion //Constructors
55 #region Public Instance Properties
57 public override Rectangle Bounds {
59 throw new NotImplementedException ();
63 public override string Name {
65 throw new NotImplementedException ();
69 protected DataGridColumnStyle Owner {
73 public override AccessibleObject Parent {
75 throw new NotImplementedException ();
79 public override AccessibleRole Role {
81 throw new NotImplementedException ();
86 #region Public Instance Methods
88 public override AccessibleObject Navigate (AccessibleNavigation navdir)
90 throw new NotImplementedException ();
92 #endregion Public Instance Methods
95 protected class CompModSwitches
97 public CompModSwitches ()
101 #region Public Instance Methods
103 public static TraceSwitch DGEditColumnEditing {
105 throw new NotImplementedException ();
108 #endregion Public Instance Methods
111 internal enum ArrowDrawing
118 #region Local Variables
119 internal HorizontalAlignment alignment;
120 private int fontheight;
121 internal DataGridTableStyle table_style;
122 private string header_text;
123 private string mapping_name;
124 private string null_text;
125 private PropertyDescriptor property_descriptor;
126 private bool _readonly;
128 internal bool is_default;
129 internal DataGrid grid;
130 private DataGridColumnHeaderAccessibleObject accesible_object;
131 private StringFormat string_format_hdr;
132 static string def_null_text = "(null)";
133 private ArrowDrawing arrow_drawing = ArrowDrawing.No;
135 #endregion // Local Variables
138 public DataGridColumnStyle () : this (null)
142 public DataGridColumnStyle (PropertyDescriptor prop)
144 property_descriptor = prop;
148 header_text = string.Empty;
149 mapping_name = string.Empty;
150 null_text = def_null_text;
151 accesible_object = new DataGridColumnHeaderAccessibleObject (this);
156 alignment = HorizontalAlignment.Left;
157 string_format_hdr = new StringFormat ();
158 string_format_hdr.FormatFlags |= StringFormatFlags.NoWrap;
159 string_format_hdr.LineAlignment = StringAlignment.Center;
164 #region Public Instance Properties
166 [DefaultValue(HorizontalAlignment.Left)]
167 public virtual HorizontalAlignment Alignment {
172 if (value != alignment) {
175 if (table_style != null && table_style.DataGrid != null) {
176 table_style.DataGrid.Invalidate ();
179 EventHandler eh = (EventHandler)(Events [AlignmentChangedEvent]);
181 eh (this, EventArgs.Empty);
187 public virtual DataGridTableStyle DataGridTableStyle {
188 get { return table_style; }
191 protected int FontHeight {
193 if (fontheight != -1) {
197 if (table_style != null) {
198 //return table_style.DataGrid.FontHeight
202 // TODO: Default Datagrid font height
208 public AccessibleObject HeaderAccessibleObject {
210 return accesible_object;
215 public virtual string HeaderText {
220 if (value != header_text) {
225 EventHandler eh = (EventHandler)(Events [HeaderTextChangedEvent]);
227 eh (this, EventArgs.Empty);
232 [Editor("System.Windows.Forms.Design.DataGridColumnStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
234 public string MappingName {
239 if (value != mapping_name) {
240 mapping_name = value;
242 EventHandler eh = (EventHandler)(Events [MappingNameChangedEvent]);
244 eh (this, EventArgs.Empty);
250 public virtual string NullText {
255 if (value != null_text) {
258 if (table_style != null && table_style.DataGrid != null) {
259 table_style.DataGrid.Invalidate ();
262 EventHandler eh = (EventHandler)(Events [NullTextChangedEvent]);
264 eh (this, EventArgs.Empty);
271 [EditorBrowsable(EditorBrowsableState.Advanced)]
272 public virtual PropertyDescriptor PropertyDescriptor {
274 return property_descriptor;
277 if (value != property_descriptor) {
278 property_descriptor = value;
280 EventHandler eh = (EventHandler)(Events [PropertyDescriptorChangedEvent]);
282 eh (this, EventArgs.Empty);
287 [DefaultValue(false)]
288 public virtual bool ReadOnly {
293 if (value != _readonly) {
296 if (table_style != null && table_style.DataGrid != null) {
297 table_style.DataGrid.CalcAreasAndInvalidate ();
300 EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
302 eh (this, EventArgs.Empty);
309 public virtual int Width {
314 if (value != width) {
317 if (table_style != null && table_style.DataGrid != null) {
318 table_style.DataGrid.CalcAreasAndInvalidate ();
321 EventHandler eh = (EventHandler)(Events [WidthChangedEvent]);
323 eh (this, EventArgs.Empty);
328 #endregion // Public Instance Properties
330 #region Private Instance Properties
332 internal ArrowDrawing ArrowDrawingMode {
333 get { return arrow_drawing; }
334 set { arrow_drawing = value; }
337 // The logic seems to be that:
338 // - If DataGrid.ReadOnly is true all the tables and columns are readonly ignoring other settings
339 // - If DataGridTableStyle.ReadOnly is true all columns are readonly ignoring other settings
340 // - If DataGrid.ReadOnly and DataGridTableStyle.ReadOnly are false, the columns settings are mandatory
342 internal bool ParentReadOnly {
345 if (grid.ReadOnly == true) {
350 if (table_style != null) {
351 if (table_style.ReadOnly == true) {
360 internal DataGridTableStyle TableStyle {
361 set { table_style = value; }
364 internal bool IsDefault {
365 get { return is_default; }
367 #endregion Private Instance Properties
369 #region Public Instance Methods
370 protected internal abstract void Abort (int rowNum);
373 protected void BeginUpdate ()
378 protected void CheckValidDataSource (CurrencyManager value)
381 throw new ArgumentNullException ("CurrencyManager cannot be null");
384 if (property_descriptor == null) {
385 PropertyDescriptorCollection propcol = value.GetItemProperties ();
387 for (int i = 0; i < propcol.Count ; i++) {
388 if (propcol[i].Name == mapping_name) {
389 property_descriptor = propcol[i];
394 if (property_descriptor == null)
395 throw new InvalidOperationException ("The PropertyDescriptor for this column is a null reference");
400 protected internal virtual void ColumnStartedEditing (Control editingControl)
405 protected internal abstract bool Commit (CurrencyManager dataSource, int rowNum);
408 protected internal virtual void ConcedeFocus ()
413 protected virtual AccessibleObject CreateHeaderAccessibleObject ()
415 return new DataGridColumnHeaderAccessibleObject (this);
418 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly)
420 Edit (source, rowNum, bounds, readOnly, string.Empty);
423 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText)
425 Edit (source, rowNum, bounds, readOnly, instantText, true);
428 protected internal abstract void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible);
432 protected void EndUpdate ()
437 protected internal virtual void EnterNullValue () {}
439 protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
441 CheckValidDataSource (source);
442 if (rowNum >= source.Count)
444 return property_descriptor.GetValue (source [rowNum]);
447 protected internal abstract int GetMinimumHeight ();
449 protected internal abstract int GetPreferredHeight (Graphics g, object value);
451 protected internal abstract Size GetPreferredSize (Graphics g, object value);
453 void IDataGridColumnStyleEditingNotificationService.ColumnStartedEditing (Control editingControl)
455 ColumnStartedEditing (editingControl);
458 protected virtual void Invalidate ()
461 grid.InvalidateColumn (this);
464 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum);
465 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight);
467 protected internal virtual void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum,
468 Brush backBrush, Brush foreBrush, bool alignToRight) {}
470 protected internal virtual void ReleaseHostedControl () {}
472 public void ResetHeaderText ()
474 HeaderText = string.Empty;
477 protected internal virtual void SetColumnValueAtRow (CurrencyManager source, int rowNum, object value)
479 CheckValidDataSource (source);
481 IEditableObject editable = source [rowNum] as IEditableObject;
483 if (editable != null)
484 editable.BeginEdit ();
486 property_descriptor.SetValue (source [rowNum], value);
488 if (editable != null)
492 protected virtual void SetDataGrid (DataGrid value)
496 property_descriptor = null;
498 if (value.ListManager != null)
499 CheckValidDataSource (value.ListManager);
502 protected virtual void SetDataGridInColumn (DataGrid value)
507 internal void SetDataGridInternal (DataGrid value)
509 SetDataGridInColumn (value);
512 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string instantText)
516 #endregion // Public Instance Methods
518 #region Private Instance Methods
519 virtual internal void OnMouseDown (MouseEventArgs e, int row, int column) {}
520 virtual internal void OnKeyDown (KeyEventArgs ke, int row, int column) {}
522 internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
525 g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderBackColor),
528 if (grid.FlatMode == false) {
531 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
532 bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
535 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
536 bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
538 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
539 bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 2);
542 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlDark),
543 bounds.X + bounds.Width - 1, bounds.Y + 2 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 2);
548 g.DrawString (HeaderText, DataGridTableStyle.HeaderFont, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderForeColor),
549 bounds, string_format_hdr);
551 if (arrow_drawing != ArrowDrawing.No) {
553 Point pnt = new Point (bounds.X + bounds.Width - 12, bounds.Y + ((bounds.Height - 6)/2));
555 if (arrow_drawing == ArrowDrawing.Ascending) {
556 g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y + 6, pnt.X + 3, pnt.Y);
557 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 6, pnt.Y + 6);
558 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 3, pnt.Y);
560 g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y, pnt.X + 3, pnt.Y + 6);
561 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 6, pnt.Y);
562 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 3, pnt.Y + 6);
568 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
570 g.FillRectangle (backBrush, bounds);
571 PaintGridLine (g, bounds);
574 internal void PaintGridLine (Graphics g, Rectangle bounds)
576 if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
580 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
581 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
583 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
584 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
587 #endregion Private Instance Methods
591 static object AlignmentChangedEvent = new object ();
592 static object FontChangedEvent = new object ();
593 static object HeaderTextChangedEvent = new object ();
594 static object MappingNameChangedEvent = new object ();
595 static object NullTextChangedEvent = new object ();
596 static object PropertyDescriptorChangedEvent = new object ();
597 static object ReadOnlyChangedEvent = new object ();
598 static object WidthChangedEvent = new object ();
600 public event EventHandler AlignmentChanged {
601 add { Events.AddHandler (AlignmentChangedEvent, value); }
602 remove { Events.RemoveHandler (AlignmentChangedEvent, value); }
605 public event EventHandler FontChanged {
606 add { Events.AddHandler (FontChangedEvent, value); }
607 remove { Events.RemoveHandler (FontChangedEvent, value); }
610 public event EventHandler HeaderTextChanged {
611 add { Events.AddHandler (HeaderTextChangedEvent, value); }
612 remove { Events.RemoveHandler (HeaderTextChangedEvent, value); }
615 public event EventHandler MappingNameChanged {
616 add { Events.AddHandler (MappingNameChangedEvent, value); }
617 remove { Events.RemoveHandler (MappingNameChangedEvent, value); }
620 public event EventHandler NullTextChanged {
621 add { Events.AddHandler (NullTextChangedEvent, value); }
622 remove { Events.RemoveHandler (NullTextChangedEvent, value); }
626 [EditorBrowsable(EditorBrowsableState.Advanced)]
627 public event EventHandler PropertyDescriptorChanged {
628 add { Events.AddHandler (PropertyDescriptorChangedEvent, value); }
629 remove { Events.RemoveHandler (PropertyDescriptorChangedEvent, value); }
632 public event EventHandler ReadOnlyChanged {
633 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
634 remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
637 public event EventHandler WidthChanged {
638 add { Events.AddHandler (WidthChangedEvent, value); }
639 remove { Events.RemoveHandler (WidthChangedEvent, value); }