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;
47 public DataGridColumnHeaderAccessibleObject ()
50 public DataGridColumnHeaderAccessibleObject (DataGridColumnStyle owner)
54 #endregion //Constructors
56 #region Public Instance Properties
57 [MonoTODO ("Not implemented, will throw NotImplementedException")]
58 public override Rectangle Bounds {
60 throw new NotImplementedException ();
64 public override string Name {
66 throw new NotImplementedException ();
70 protected DataGridColumnStyle Owner {
74 public override AccessibleObject Parent {
76 throw new NotImplementedException ();
80 public override AccessibleRole Role {
82 throw new NotImplementedException ();
87 #region Public Instance Methods
88 [MonoTODO ("Not implemented, will throw NotImplementedException")]
89 public override AccessibleObject Navigate (AccessibleNavigation navdir)
91 throw new NotImplementedException ();
93 #endregion Public Instance Methods
96 protected class CompModSwitches
98 public CompModSwitches ()
102 #region Public Instance Methods
103 [MonoTODO ("Not implemented, will throw NotImplementedException")]
104 public static TraceSwitch DGEditColumnEditing {
106 throw new NotImplementedException ();
109 #endregion Public Instance Methods
112 internal enum ArrowDrawing
119 #region Local Variables
120 internal HorizontalAlignment alignment;
121 private int fontheight;
122 internal DataGridTableStyle table_style;
123 private string header_text;
124 private string mapping_name;
125 private string null_text;
126 private PropertyDescriptor property_descriptor;
127 private bool _readonly;
129 internal bool is_default;
130 internal DataGrid grid;
131 private DataGridColumnHeaderAccessibleObject accesible_object;
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);
152 _readonly = prop == null ? false : prop.IsReadOnly;
156 alignment = HorizontalAlignment.Left;
161 #region Public Instance Properties
163 [DefaultValue(HorizontalAlignment.Left)]
164 public virtual HorizontalAlignment Alignment {
169 if (value != alignment) {
172 if (table_style != null && table_style.DataGrid != null) {
173 table_style.DataGrid.Invalidate ();
176 EventHandler eh = (EventHandler)(Events [AlignmentChangedEvent]);
178 eh (this, EventArgs.Empty);
184 public virtual DataGridTableStyle DataGridTableStyle {
185 get { return table_style; }
188 protected int FontHeight {
190 if (fontheight != -1) {
194 if (table_style != null) {
195 //return table_style.DataGrid.FontHeight
199 // TODO: Default Datagrid font height
205 public AccessibleObject HeaderAccessibleObject {
207 return accesible_object;
212 public virtual string HeaderText {
217 if (value != header_text) {
222 EventHandler eh = (EventHandler)(Events [HeaderTextChangedEvent]);
224 eh (this, EventArgs.Empty);
230 [Editor("System.Windows.Forms.Design.DataGridColumnStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
233 public string MappingName {
238 if (value != mapping_name) {
239 mapping_name = value;
241 EventHandler eh = (EventHandler)(Events [MappingNameChangedEvent]);
243 eh (this, EventArgs.Empty);
249 public virtual string NullText {
254 if (value != null_text) {
257 if (table_style != null && table_style.DataGrid != null) {
258 table_style.DataGrid.Invalidate ();
261 EventHandler eh = (EventHandler)(Events [NullTextChangedEvent]);
263 eh (this, EventArgs.Empty);
270 [EditorBrowsable(EditorBrowsableState.Advanced)]
271 public virtual PropertyDescriptor PropertyDescriptor {
273 return property_descriptor;
276 if (value != property_descriptor) {
277 property_descriptor = value;
279 EventHandler eh = (EventHandler)(Events [PropertyDescriptorChangedEvent]);
281 eh (this, EventArgs.Empty);
286 [DefaultValue(false)]
287 public virtual bool ReadOnly {
292 if (value != _readonly) {
295 if (table_style != null && table_style.DataGrid != null) {
296 table_style.DataGrid.CalcAreasAndInvalidate ();
299 EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
301 eh (this, EventArgs.Empty);
308 public virtual int Width {
313 if (value != width) {
316 if (table_style != null && table_style.DataGrid != null) {
317 table_style.DataGrid.CalcAreasAndInvalidate ();
320 EventHandler eh = (EventHandler)(Events [WidthChangedEvent]);
322 eh (this, EventArgs.Empty);
327 #endregion // Public Instance Properties
329 #region Private Instance Properties
331 internal ArrowDrawing ArrowDrawingMode {
332 get { return arrow_drawing; }
333 set { arrow_drawing = value; }
336 internal bool TableStyleReadOnly {
338 return table_style != null && table_style.ReadOnly;
342 internal DataGridTableStyle TableStyle {
343 set { table_style = value; }
346 internal bool IsDefault {
347 get { return is_default; }
349 #endregion Private Instance Properties
351 #region Public Instance Methods
352 protected internal abstract void Abort (int rowNum);
354 [MonoTODO ("Will not suspend updates")]
355 protected void BeginUpdate ()
359 protected void CheckValidDataSource (CurrencyManager value)
362 throw new ArgumentNullException ("CurrencyManager cannot be null");
365 if (property_descriptor == null) {
366 property_descriptor = value.GetItemProperties ()[mapping_name];
368 // Console.WriteLine ("mapping name = {0}", mapping_name);
369 // foreach (PropertyDescriptor prop in value.GetItemProperties ()) {
370 // Console.WriteLine (" + prop = {0}", prop.Name);
373 if (property_descriptor == null)
374 throw new InvalidOperationException ("The PropertyDescriptor for this column is a null reference");
376 /*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.*/
382 protected internal virtual void ColumnStartedEditing (Control editingControl)
386 protected internal abstract bool Commit (CurrencyManager dataSource, int rowNum);
389 protected internal virtual void ConcedeFocus ()
393 protected virtual AccessibleObject CreateHeaderAccessibleObject ()
395 return new DataGridColumnHeaderAccessibleObject (this);
398 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly)
400 Edit (source, rowNum, bounds, readOnly, string.Empty);
403 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string displayText)
405 string instantText = displayText;
406 Edit (source, rowNum, bounds, readOnly, instantText, true);
409 protected internal abstract void Edit (CurrencyManager source,
410 int rowNum, Rectangle bounds, bool readOnly,
415 protected void EndUpdate ()
419 protected internal virtual void EnterNullValue () {}
421 protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
423 CheckValidDataSource (source);
424 if (rowNum >= source.Count)
426 return property_descriptor.GetValue (source [rowNum]);
429 protected internal abstract int GetMinimumHeight ();
431 protected internal abstract int GetPreferredHeight (Graphics g, object value);
433 protected internal abstract Size GetPreferredSize (Graphics g, object value);
435 void IDataGridColumnStyleEditingNotificationService.ColumnStartedEditing (Control editingControl)
437 ColumnStartedEditing (editingControl);
440 protected virtual void Invalidate ()
443 grid.InvalidateColumn (this);
446 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum);
447 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight);
449 protected internal virtual void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum,
450 Brush backBrush, Brush foreBrush, bool alignToRight) {}
452 protected internal virtual void ReleaseHostedControl () {}
454 public void ResetHeaderText ()
456 HeaderText = string.Empty;
459 protected internal virtual void SetColumnValueAtRow (CurrencyManager source, int rowNum, object value)
461 CheckValidDataSource (source);
463 IEditableObject editable = source [rowNum] as IEditableObject;
465 if (editable != null)
466 editable.BeginEdit ();
468 property_descriptor.SetValue (source [rowNum], value);
471 protected virtual void SetDataGrid (DataGrid value)
475 property_descriptor = null;
477 // we don't check whether the DataGrid.ListManager is valid or not.
478 // This is done by .net later as requiered, but not at this point.
481 protected virtual void SetDataGridInColumn (DataGrid value)
486 internal void SetDataGridInternal (DataGrid value)
488 SetDataGridInColumn (value);
491 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string displayText)
495 #endregion // Public Instance Methods
497 #region Private Instance Methods
498 virtual internal void OnMouseDown (MouseEventArgs e, int row, int column) {}
499 virtual internal void OnKeyDown (KeyEventArgs ke, int row, int column) {}
501 internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
503 ThemeEngine.Current.DataGridPaintColumnHeader (g, bounds, grid, colNum);
506 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
508 g.FillRectangle (backBrush, bounds);
509 PaintGridLine (g, bounds);
512 internal void PaintGridLine (Graphics g, Rectangle bounds)
514 if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
518 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
519 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
521 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
522 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
525 #endregion Private Instance Methods
528 static object AlignmentChangedEvent = new object ();
529 static object FontChangedEvent = new object ();
530 static object HeaderTextChangedEvent = new object ();
531 static object MappingNameChangedEvent = new object ();
532 static object NullTextChangedEvent = new object ();
533 static object PropertyDescriptorChangedEvent = new object ();
534 static object ReadOnlyChangedEvent = new object ();
535 static object WidthChangedEvent = new object ();
537 public event EventHandler AlignmentChanged {
538 add { Events.AddHandler (AlignmentChangedEvent, value); }
539 remove { Events.RemoveHandler (AlignmentChangedEvent, value); }
542 public event EventHandler FontChanged {
543 add { Events.AddHandler (FontChangedEvent, value); }
544 remove { Events.RemoveHandler (FontChangedEvent, value); }
547 public event EventHandler HeaderTextChanged {
548 add { Events.AddHandler (HeaderTextChangedEvent, value); }
549 remove { Events.RemoveHandler (HeaderTextChangedEvent, value); }
552 public event EventHandler MappingNameChanged {
553 add { Events.AddHandler (MappingNameChangedEvent, value); }
554 remove { Events.RemoveHandler (MappingNameChangedEvent, value); }
557 public event EventHandler NullTextChanged {
558 add { Events.AddHandler (NullTextChangedEvent, value); }
559 remove { Events.RemoveHandler (NullTextChangedEvent, value); }
563 [EditorBrowsable(EditorBrowsableState.Advanced)]
564 public event EventHandler PropertyDescriptorChanged {
565 add { Events.AddHandler (PropertyDescriptorChangedEvent, value); }
566 remove { Events.RemoveHandler (PropertyDescriptorChangedEvent, value); }
569 public event EventHandler ReadOnlyChanged {
570 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
571 remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
574 public event EventHandler WidthChanged {
575 add { Events.AddHandler (WidthChangedEvent, value); }
576 remove { Events.RemoveHandler (WidthChangedEvent, value); }