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 #region Local Variables
112 internal HorizontalAlignment alignment;
113 private int fontheight;
114 internal DataGridTableStyle table_style;
115 private string header_text;
116 private string mapping_name;
117 private string null_text;
118 private PropertyDescriptor property_descriptor;
119 private bool _readonly;
121 internal bool is_default;
122 internal DataGrid grid;
123 private DataGridColumnHeaderAccessibleObject accesible_object;
124 private StringFormat string_format_hdr;
125 static string def_null_text = "(null)";
126 #endregion // Local Variables
129 public DataGridColumnStyle ()
131 CommmonConstructor ();
132 property_descriptor = null;
135 public DataGridColumnStyle (PropertyDescriptor prop)
137 CommmonConstructor ();
138 property_descriptor = prop;
141 private void CommmonConstructor ()
145 header_text = string.Empty;
146 mapping_name = string.Empty;
147 null_text = def_null_text;
148 accesible_object = new DataGridColumnHeaderAccessibleObject (this);
153 alignment = HorizontalAlignment.Left;
154 string_format_hdr = new StringFormat ();
155 string_format_hdr.FormatFlags |= StringFormatFlags.NoWrap;
156 string_format_hdr.LineAlignment = StringAlignment.Center;
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 if (AlignmentChanged != null) {
177 AlignmentChanged (this, EventArgs.Empty);
184 public virtual DataGridTableStyle DataGridTableStyle {
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) {
222 if (table_style != null && table_style.DataGrid != null) {
223 table_style.DataGrid.Invalidate ();
226 if (HeaderTextChanged != null) {
227 HeaderTextChanged (this, EventArgs.Empty);
233 [Editor("System.Windows.Forms.Design.DataGridColumnStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
235 public string MappingName {
240 if (value != mapping_name) {
241 mapping_name = value;
243 if (MappingNameChanged != null) {
244 MappingNameChanged (this, EventArgs.Empty);
251 public virtual string NullText {
256 if (value != null_text) {
259 if (table_style != null && table_style.DataGrid != null) {
260 table_style.DataGrid.Invalidate ();
263 if (NullTextChanged != null) {
264 NullTextChanged (this, EventArgs.Empty);
272 [EditorBrowsable(EditorBrowsableState.Advanced)]
273 public virtual PropertyDescriptor PropertyDescriptor {
275 return property_descriptor;
278 if (value != property_descriptor) {
279 property_descriptor = value;
281 if (PropertyDescriptorChanged != null) {
282 PropertyDescriptorChanged (this, EventArgs.Empty);
288 [DefaultValue(false)]
289 public virtual bool ReadOnly {
294 if (value != _readonly) {
297 if (table_style != null && table_style.DataGrid != null) {
298 table_style.DataGrid.CalcAreasAndInvalidate ();
301 if (ReadOnlyChanged != null) {
302 ReadOnlyChanged (this, EventArgs.Empty);
310 public virtual int Width {
315 if (value != width) {
318 if (table_style != null && table_style.DataGrid != null) {
319 table_style.DataGrid.CalcAreasAndInvalidate ();
322 if (WidthChanged != null) {
323 WidthChanged (this, EventArgs.Empty);
329 #endregion // Public Instance Properties
331 #region Private Instance Properties
333 // The logic seems to be that:
334 // - If DataGrid.ReadOnly is true all the tables and columns are readonly ignoring other settings
335 // - If DataGridTableStyle.ReadOnly is true all columns are readonly ignoring other settings
336 // - If DataGrid.ReadOnly and DataGridTableStyle.ReadOnly are false, the columns settings are mandatory
338 internal bool ParentReadOnly {
341 if (grid.ReadOnly == true) {
345 if (grid.ListManager != null && grid.ListManager.CanAddRows == false) {
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 throw new ApplicationException ("The PropertyDescriptor for this column is a null reference");
390 protected internal virtual void ColumnStartedEditing (Control editingControl)
395 protected internal abstract bool Commit (CurrencyManager dataSource, int rowNum);
398 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 instantText)
415 Edit (source, rowNum, bounds, readOnly, instantText, true);
418 protected internal abstract void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible);
422 protected void EndUpdate ()
427 protected internal virtual void EnterNullValue () {}
429 protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
431 CheckValidDataSource (source);
432 return property_descriptor.GetValue (source.GetItem (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)
446 protected virtual void Invalidate ()
448 grid.grid_drawing.InvalidateColumn (this);
451 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum);
452 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight);
454 protected internal virtual void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum,
455 Brush backBrush, Brush foreBrush, bool alignToRight) {}
457 protected internal virtual void ReleaseHostedControl () {}
459 public void ResetHeaderText ()
461 HeaderText = string.Empty;
464 protected internal virtual void SetColumnValueAtRow (CurrencyManager source, int rowNum, object value)
466 CheckValidDataSource (source);
467 property_descriptor.SetValue (source.GetItem (rowNum), value);
470 protected virtual void SetDataGrid (DataGrid value)
474 if (property_descriptor != null || value == null || value.ListManager == null) {
478 PropertyDescriptorCollection propcol = value.ListManager.GetItemProperties ();
479 for (int i = 0; i < propcol.Count ; i++) {
480 if (propcol[i].Name == mapping_name) {
481 property_descriptor = propcol[i];
487 protected virtual void SetDataGridInColumn (DataGrid value)
492 internal void SetDataGridInternal (DataGrid value)
494 SetDataGridInColumn (value);
497 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string instantText)
501 #endregion // Public Instance Methods
503 #region Private Instance Methods
504 virtual internal void OnMouseDown (MouseEventArgs e, int row, int column) {}
505 virtual internal void OnKeyDown (KeyEventArgs ke, int row, int column) {}
507 internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
510 g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderBackColor),
513 if (grid.FlatMode == false) {
516 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
517 bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
520 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
521 bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
523 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
524 bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 2);
527 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlDark),
528 bounds.X + bounds.Width - 1, bounds.Y + 2 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 2);
533 g.DrawString (HeaderText, DataGridTableStyle.HeaderFont, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderForeColor),
534 bounds, string_format_hdr);
537 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
539 g.FillRectangle (backBrush, bounds);
540 PaintGridLine (g, bounds);
543 internal void PaintGridLine (Graphics g, Rectangle bounds)
545 if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
549 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
550 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
552 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
553 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
556 #endregion Private Instance Methods
560 public event EventHandler AlignmentChanged;
561 public event EventHandler FontChanged;
562 public event EventHandler HeaderTextChanged;
563 public event EventHandler MappingNameChanged;
564 public event EventHandler NullTextChanged;
567 [EditorBrowsable(EditorBrowsableState.Advanced)]
568 public event EventHandler PropertyDescriptorChanged;
569 public event EventHandler ReadOnlyChanged;
570 public event EventHandler WidthChanged;