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 static string def_null_text = "(null)";
135 private ArrowDrawing arrow_drawing = ArrowDrawing.No;
137 #endregion // Local Variables
140 public DataGridColumnStyle () : this (null)
144 public DataGridColumnStyle (PropertyDescriptor prop)
146 property_descriptor = prop;
150 header_text = string.Empty;
151 mapping_name = string.Empty;
152 null_text = def_null_text;
153 accesible_object = new DataGridColumnHeaderAccessibleObject (this);
154 _readonly = prop == null ? false : prop.IsReadOnly;
158 alignment = HorizontalAlignment.Left;
163 #region Public Instance Properties
165 [DefaultValue(HorizontalAlignment.Left)]
166 public virtual HorizontalAlignment Alignment {
171 if (value != alignment) {
174 if (table_style != null && table_style.DataGrid != null) {
175 table_style.DataGrid.Invalidate ();
178 EventHandler eh = (EventHandler)(Events [AlignmentChangedEvent]);
180 eh (this, EventArgs.Empty);
186 public virtual DataGridTableStyle DataGridTableStyle {
187 get { return table_style; }
190 protected int FontHeight {
192 if (fontheight != -1) {
196 if (table_style != null) {
197 //return table_style.DataGrid.FontHeight
201 // TODO: Default Datagrid font height
207 public AccessibleObject HeaderAccessibleObject {
209 return accesible_object;
214 public virtual string HeaderText {
219 if (value != header_text) {
224 EventHandler eh = (EventHandler)(Events [HeaderTextChangedEvent]);
226 eh (this, EventArgs.Empty);
232 [Editor("System.Windows.Forms.Design.DataGridColumnStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
237 public string MappingName {
242 if (value != mapping_name) {
243 mapping_name = value;
245 EventHandler eh = (EventHandler)(Events [MappingNameChangedEvent]);
247 eh (this, EventArgs.Empty);
253 public virtual string NullText {
258 if (value != null_text) {
261 if (table_style != null && table_style.DataGrid != null) {
262 table_style.DataGrid.Invalidate ();
265 EventHandler eh = (EventHandler)(Events [NullTextChangedEvent]);
267 eh (this, EventArgs.Empty);
274 [EditorBrowsable(EditorBrowsableState.Advanced)]
275 public virtual PropertyDescriptor PropertyDescriptor {
277 return property_descriptor;
280 if (value != property_descriptor) {
281 property_descriptor = value;
283 EventHandler eh = (EventHandler)(Events [PropertyDescriptorChangedEvent]);
285 eh (this, EventArgs.Empty);
290 [DefaultValue(false)]
291 public virtual bool ReadOnly {
296 if (value != _readonly) {
299 if (table_style != null && table_style.DataGrid != null) {
300 table_style.DataGrid.CalcAreasAndInvalidate ();
303 EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
305 eh (this, EventArgs.Empty);
312 public virtual int Width {
317 if (value != width) {
320 if (table_style != null && table_style.DataGrid != null) {
321 table_style.DataGrid.CalcAreasAndInvalidate ();
324 EventHandler eh = (EventHandler)(Events [WidthChangedEvent]);
326 eh (this, EventArgs.Empty);
331 #endregion // Public Instance Properties
333 #region Private Instance Properties
335 internal ArrowDrawing ArrowDrawingMode {
336 get { return arrow_drawing; }
337 set { arrow_drawing = value; }
340 internal bool TableStyleReadOnly {
342 return table_style != null && table_style.ReadOnly;
346 internal DataGridTableStyle TableStyle {
347 set { table_style = value; }
350 internal bool IsDefault {
351 get { return is_default; }
353 #endregion Private Instance Properties
355 #region Public Instance Methods
356 protected internal abstract void Abort (int rowNum);
359 protected void BeginUpdate ()
363 protected void CheckValidDataSource (CurrencyManager value)
366 throw new ArgumentNullException ("CurrencyManager cannot be null");
369 if (property_descriptor == null) {
370 property_descriptor = value.GetItemProperties ()[mapping_name];
372 // Console.WriteLine ("mapping name = {0}", mapping_name);
373 // foreach (PropertyDescriptor prop in value.GetItemProperties ()) {
374 // Console.WriteLine (" + prop = {0}", prop.Name);
377 if (property_descriptor == null)
378 throw new InvalidOperationException ("The PropertyDescriptor for this column is a null reference");
380 /*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.*/
387 protected internal virtual void ColumnStartedEditing (Control editingControl)
391 protected internal abstract bool Commit (CurrencyManager dataSource, int rowNum);
394 protected internal virtual void ConcedeFocus ()
398 protected virtual AccessibleObject CreateHeaderAccessibleObject ()
400 return new DataGridColumnHeaderAccessibleObject (this);
403 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly)
405 Edit (source, rowNum, bounds, readOnly, string.Empty);
409 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string displayText)
411 string instantText = displayText;
413 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText)
416 Edit (source, rowNum, bounds, readOnly, instantText, true);
419 protected internal abstract void Edit (CurrencyManager source,
420 int rowNum, Rectangle bounds, bool readOnly,
430 protected void EndUpdate ()
434 protected internal virtual void EnterNullValue () {}
436 protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
438 CheckValidDataSource (source);
439 if (rowNum >= source.Count)
441 return property_descriptor.GetValue (source [rowNum]);
444 protected internal abstract int GetMinimumHeight ();
446 protected internal abstract int GetPreferredHeight (Graphics g, object value);
448 protected internal abstract Size GetPreferredSize (Graphics g, object value);
450 void IDataGridColumnStyleEditingNotificationService.ColumnStartedEditing (Control editingControl)
452 ColumnStartedEditing (editingControl);
455 protected virtual void Invalidate ()
458 grid.InvalidateColumn (this);
461 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum);
462 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight);
464 protected internal virtual void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum,
465 Brush backBrush, Brush foreBrush, bool alignToRight) {}
467 protected internal virtual void ReleaseHostedControl () {}
469 public void ResetHeaderText ()
471 HeaderText = string.Empty;
474 protected internal virtual void SetColumnValueAtRow (CurrencyManager source, int rowNum, object value)
476 CheckValidDataSource (source);
478 IEditableObject editable = source [rowNum] as IEditableObject;
480 if (editable != null)
481 editable.BeginEdit ();
483 property_descriptor.SetValue (source [rowNum], value);
486 protected virtual void SetDataGrid (DataGrid value)
490 property_descriptor = null;
492 if (value != null && value.ListManager != null)
493 CheckValidDataSource (value.ListManager);
496 protected virtual void SetDataGridInColumn (DataGrid value)
501 internal void SetDataGridInternal (DataGrid value)
503 SetDataGridInColumn (value);
507 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string displayText)
511 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)
524 ThemeEngine.Current.DataGridPaintColumnHeader (g, bounds, grid, colNum);
527 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
529 g.FillRectangle (backBrush, bounds);
530 PaintGridLine (g, bounds);
533 internal void PaintGridLine (Graphics g, Rectangle bounds)
535 if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
539 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
540 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
542 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
543 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
546 #endregion Private Instance Methods
549 static object AlignmentChangedEvent = new object ();
550 static object FontChangedEvent = new object ();
551 static object HeaderTextChangedEvent = new object ();
552 static object MappingNameChangedEvent = new object ();
553 static object NullTextChangedEvent = new object ();
554 static object PropertyDescriptorChangedEvent = new object ();
555 static object ReadOnlyChangedEvent = new object ();
556 static object WidthChangedEvent = new object ();
558 public event EventHandler AlignmentChanged {
559 add { Events.AddHandler (AlignmentChangedEvent, value); }
560 remove { Events.RemoveHandler (AlignmentChangedEvent, value); }
563 public event EventHandler FontChanged {
564 add { Events.AddHandler (FontChangedEvent, value); }
565 remove { Events.RemoveHandler (FontChangedEvent, value); }
568 public event EventHandler HeaderTextChanged {
569 add { Events.AddHandler (HeaderTextChangedEvent, value); }
570 remove { Events.RemoveHandler (HeaderTextChangedEvent, value); }
573 public event EventHandler MappingNameChanged {
574 add { Events.AddHandler (MappingNameChangedEvent, value); }
575 remove { Events.RemoveHandler (MappingNameChangedEvent, value); }
578 public event EventHandler NullTextChanged {
579 add { Events.AddHandler (NullTextChangedEvent, value); }
580 remove { Events.RemoveHandler (NullTextChangedEvent, value); }
584 [EditorBrowsable(EditorBrowsableState.Advanced)]
585 public event EventHandler PropertyDescriptorChanged {
586 add { Events.AddHandler (PropertyDescriptorChangedEvent, value); }
587 remove { Events.RemoveHandler (PropertyDescriptorChangedEvent, value); }
590 public event EventHandler ReadOnlyChanged {
591 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
592 remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
595 public event EventHandler WidthChanged {
596 add { Events.AddHandler (WidthChangedEvent, value); }
597 remove { Events.RemoveHandler (WidthChangedEvent, value); }