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>
24 // Chris Toshok <toshok@ximian.com>
28 using System.ComponentModel;
29 using System.Runtime.InteropServices;
30 using System.Diagnostics;
32 namespace System.Windows.Forms
34 [DesignTimeVisible(false)]
35 [DefaultProperty("Header")]
37 public abstract class DataGridColumnStyle : Component, IDataGridColumnStyleEditingNotificationService
40 protected class DataGridColumnHeaderAccessibleObject : AccessibleObject
42 #region Local Variables
43 private new DataGridColumnStyle owner;
48 public DataGridColumnHeaderAccessibleObject ()
52 public DataGridColumnHeaderAccessibleObject (DataGridColumnStyle owner)
56 #endregion //Constructors
58 #region Public Instance Properties
60 public override Rectangle Bounds {
62 throw new NotImplementedException ();
66 public override string Name {
68 throw new NotImplementedException ();
72 protected DataGridColumnStyle Owner {
76 public override AccessibleObject Parent {
78 throw new NotImplementedException ();
82 public override AccessibleRole Role {
84 throw new NotImplementedException ();
89 #region Public Instance Methods
91 public override AccessibleObject Navigate (AccessibleNavigation navdir)
93 throw new NotImplementedException ();
95 #endregion Public Instance Methods
98 protected class CompModSwitches
100 public CompModSwitches ()
104 #region Public Instance Methods
106 public static TraceSwitch DGEditColumnEditing {
108 throw new NotImplementedException ();
111 #endregion Public Instance Methods
114 internal enum ArrowDrawing
121 #region Local Variables
122 internal HorizontalAlignment alignment;
123 private int fontheight;
124 internal DataGridTableStyle table_style;
125 private string header_text;
126 private string mapping_name;
127 private string null_text;
128 private PropertyDescriptor property_descriptor;
129 private bool _readonly;
131 internal bool is_default;
132 internal DataGrid grid;
133 private DataGridColumnHeaderAccessibleObject accesible_object;
134 private StringFormat string_format_hdr;
135 static string def_null_text = "(null)";
136 private ArrowDrawing arrow_drawing = ArrowDrawing.No;
138 #endregion // Local Variables
141 public DataGridColumnStyle () : this (null)
145 public DataGridColumnStyle (PropertyDescriptor prop)
147 property_descriptor = prop;
151 header_text = string.Empty;
152 mapping_name = string.Empty;
153 null_text = def_null_text;
154 accesible_object = new DataGridColumnHeaderAccessibleObject (this);
155 _readonly = prop == null ? false : prop.IsReadOnly;
159 alignment = HorizontalAlignment.Left;
160 string_format_hdr = new StringFormat ();
161 string_format_hdr.FormatFlags |= StringFormatFlags.NoWrap;
162 string_format_hdr.LineAlignment = StringAlignment.Center;
163 string_format_hdr.Trimming = StringTrimming.Character;
168 #region Public Instance Properties
170 [DefaultValue(HorizontalAlignment.Left)]
171 public virtual HorizontalAlignment Alignment {
176 if (value != alignment) {
179 if (table_style != null && table_style.DataGrid != null) {
180 table_style.DataGrid.Invalidate ();
183 EventHandler eh = (EventHandler)(Events [AlignmentChangedEvent]);
185 eh (this, EventArgs.Empty);
191 public virtual DataGridTableStyle DataGridTableStyle {
192 get { return table_style; }
195 protected int FontHeight {
197 if (fontheight != -1) {
201 if (table_style != null) {
202 //return table_style.DataGrid.FontHeight
206 // TODO: Default Datagrid font height
212 public AccessibleObject HeaderAccessibleObject {
214 return accesible_object;
219 public virtual string HeaderText {
224 if (value != header_text) {
229 EventHandler eh = (EventHandler)(Events [HeaderTextChangedEvent]);
231 eh (this, EventArgs.Empty);
237 [Editor("System.Windows.Forms.Design.DataGridColumnStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
242 public string MappingName {
247 if (value != mapping_name) {
248 mapping_name = value;
250 EventHandler eh = (EventHandler)(Events [MappingNameChangedEvent]);
252 eh (this, EventArgs.Empty);
258 public virtual string NullText {
263 if (value != null_text) {
266 if (table_style != null && table_style.DataGrid != null) {
267 table_style.DataGrid.Invalidate ();
270 EventHandler eh = (EventHandler)(Events [NullTextChangedEvent]);
272 eh (this, EventArgs.Empty);
279 [EditorBrowsable(EditorBrowsableState.Advanced)]
280 public virtual PropertyDescriptor PropertyDescriptor {
282 return property_descriptor;
285 if (value != property_descriptor) {
286 property_descriptor = value;
288 EventHandler eh = (EventHandler)(Events [PropertyDescriptorChangedEvent]);
290 eh (this, EventArgs.Empty);
295 [DefaultValue(false)]
296 public virtual bool ReadOnly {
301 if (value != _readonly) {
304 if (table_style != null && table_style.DataGrid != null) {
305 table_style.DataGrid.CalcAreasAndInvalidate ();
308 EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
310 eh (this, EventArgs.Empty);
317 public virtual int Width {
322 if (value != width) {
325 if (table_style != null && table_style.DataGrid != null) {
326 table_style.DataGrid.CalcAreasAndInvalidate ();
329 EventHandler eh = (EventHandler)(Events [WidthChangedEvent]);
331 eh (this, EventArgs.Empty);
336 #endregion // Public Instance Properties
338 #region Private Instance Properties
340 internal ArrowDrawing ArrowDrawingMode {
341 get { return arrow_drawing; }
342 set { arrow_drawing = value; }
345 internal bool TableStyleReadOnly {
347 return table_style != null && table_style.ReadOnly;
351 internal DataGridTableStyle TableStyle {
352 set { table_style = value; }
355 internal bool IsDefault {
356 get { return is_default; }
358 #endregion Private Instance Properties
360 #region Public Instance Methods
361 protected internal abstract void Abort (int rowNum);
364 protected void BeginUpdate ()
368 protected void CheckValidDataSource (CurrencyManager value)
371 throw new ArgumentNullException ("CurrencyManager cannot be null");
374 if (property_descriptor == null) {
375 property_descriptor = value.GetItemProperties ()[mapping_name];
377 // Console.WriteLine ("mapping name = {0}", mapping_name);
378 // foreach (PropertyDescriptor prop in value.GetItemProperties ()) {
379 // Console.WriteLine (" + prop = {0}", prop.Name);
382 if (property_descriptor == null)
383 throw new InvalidOperationException ("The PropertyDescriptor for this column is a null reference");
385 /*MonoTests.System.Windows.Forms.DataGridColumnStyleTest.GetColumnValueAtRow : System.InvalidOperationException : The 'foo' DataGridColumnStyle cannot be used because it is not associated with a Property or Column in the DataSource.*/
392 protected internal virtual void ColumnStartedEditing (Control editingControl)
396 protected internal abstract bool Commit (CurrencyManager dataSource, int rowNum);
399 protected internal virtual void ConcedeFocus ()
403 protected virtual AccessibleObject CreateHeaderAccessibleObject ()
405 return new DataGridColumnHeaderAccessibleObject (this);
408 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly)
410 Edit (source, rowNum, bounds, readOnly, string.Empty);
413 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string displayText)
415 Edit (source, rowNum, bounds, readOnly, displayText, true);
418 protected internal abstract void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string displayText, bool cellIsVisible);
421 protected void EndUpdate ()
425 protected internal virtual void EnterNullValue () {}
427 protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
429 CheckValidDataSource (source);
430 if (rowNum >= source.Count)
432 return property_descriptor.GetValue (source [rowNum]);
435 protected internal abstract int GetMinimumHeight ();
437 protected internal abstract int GetPreferredHeight (Graphics g, object value);
439 protected internal abstract Size GetPreferredSize (Graphics g, object value);
441 void IDataGridColumnStyleEditingNotificationService.ColumnStartedEditing (Control editingControl)
443 ColumnStartedEditing (editingControl);
446 protected virtual void Invalidate ()
449 grid.InvalidateColumn (this);
452 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum);
453 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight);
455 protected internal virtual void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum,
456 Brush backBrush, Brush foreBrush, bool alignToRight) {}
458 protected internal virtual void ReleaseHostedControl () {}
460 public void ResetHeaderText ()
462 HeaderText = string.Empty;
465 protected internal virtual void SetColumnValueAtRow (CurrencyManager source, int rowNum, object value)
467 CheckValidDataSource (source);
469 IEditableObject editable = source [rowNum] as IEditableObject;
471 if (editable != null)
472 editable.BeginEdit ();
474 property_descriptor.SetValue (source [rowNum], value);
476 if (editable != null)
480 protected virtual void SetDataGrid (DataGrid value)
484 property_descriptor = null;
486 if (value != null && value.ListManager != null)
487 CheckValidDataSource (value.ListManager);
490 protected virtual void SetDataGridInColumn (DataGrid value)
495 internal void SetDataGridInternal (DataGrid value)
497 SetDataGridInColumn (value);
500 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string displayText)
504 #endregion // Public Instance Methods
506 #region Private Instance Methods
507 virtual internal void OnMouseDown (MouseEventArgs e, int row, int column) {}
508 virtual internal void OnKeyDown (KeyEventArgs ke, int row, int column) {}
510 internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
513 g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderBackColor),
516 if (!grid.FlatMode) {
518 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
519 bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
522 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
523 bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
525 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
526 bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 2);
529 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlDark),
530 bounds.X + bounds.Width - 1, bounds.Y + 2 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 2);
536 if (arrow_drawing != ArrowDrawing.No)
539 g.DrawString (HeaderText, DataGridTableStyle.HeaderFont, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderForeColor),
540 bounds, string_format_hdr);
542 if (arrow_drawing != ArrowDrawing.No) {
544 Point pnt = new Point (bounds.X + bounds.Width + 4, bounds.Y + ((bounds.Height - 6)/2));
546 if (arrow_drawing == ArrowDrawing.Ascending) {
547 g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y + 6, pnt.X + 3, pnt.Y);
548 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 6, pnt.Y + 6);
549 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 3, pnt.Y);
551 g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y, pnt.X + 3, pnt.Y + 6);
552 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 6, pnt.Y);
553 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 3, pnt.Y + 6);
559 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
561 g.FillRectangle (backBrush, bounds);
562 PaintGridLine (g, bounds);
565 internal void PaintGridLine (Graphics g, Rectangle bounds)
567 if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
571 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
572 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
574 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
575 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
578 #endregion Private Instance Methods
581 static object AlignmentChangedEvent = new object ();
582 static object FontChangedEvent = new object ();
583 static object HeaderTextChangedEvent = new object ();
584 static object MappingNameChangedEvent = new object ();
585 static object NullTextChangedEvent = new object ();
586 static object PropertyDescriptorChangedEvent = new object ();
587 static object ReadOnlyChangedEvent = new object ();
588 static object WidthChangedEvent = new object ();
590 public event EventHandler AlignmentChanged {
591 add { Events.AddHandler (AlignmentChangedEvent, value); }
592 remove { Events.RemoveHandler (AlignmentChangedEvent, value); }
595 public event EventHandler FontChanged {
596 add { Events.AddHandler (FontChangedEvent, value); }
597 remove { Events.RemoveHandler (FontChangedEvent, value); }
600 public event EventHandler HeaderTextChanged {
601 add { Events.AddHandler (HeaderTextChangedEvent, value); }
602 remove { Events.RemoveHandler (HeaderTextChangedEvent, value); }
605 public event EventHandler MappingNameChanged {
606 add { Events.AddHandler (MappingNameChangedEvent, value); }
607 remove { Events.RemoveHandler (MappingNameChangedEvent, value); }
610 public event EventHandler NullTextChanged {
611 add { Events.AddHandler (NullTextChangedEvent, value); }
612 remove { Events.RemoveHandler (NullTextChangedEvent, value); }
616 [EditorBrowsable(EditorBrowsableState.Advanced)]
617 public event EventHandler PropertyDescriptorChanged {
618 add { Events.AddHandler (PropertyDescriptorChangedEvent, value); }
619 remove { Events.RemoveHandler (PropertyDescriptorChangedEvent, value); }
622 public event EventHandler ReadOnlyChanged {
623 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
624 remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
627 public event EventHandler WidthChanged {
628 add { Events.AddHandler (WidthChangedEvent, value); }
629 remove { Events.RemoveHandler (WidthChangedEvent, value); }