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;
134 #endregion // Local Variables
137 public DataGridColumnStyle ()
139 CommmonConstructor ();
140 property_descriptor = null;
143 public DataGridColumnStyle (PropertyDescriptor prop)
145 CommmonConstructor ();
146 property_descriptor = prop;
149 private void CommmonConstructor ()
153 header_text = string.Empty;
154 mapping_name = string.Empty;
155 null_text = def_null_text;
156 accesible_object = new DataGridColumnHeaderAccessibleObject (this);
161 alignment = HorizontalAlignment.Left;
162 string_format_hdr = new StringFormat ();
163 string_format_hdr.FormatFlags |= StringFormatFlags.NoWrap;
164 string_format_hdr.LineAlignment = StringAlignment.Center;
169 #region Public Instance Properties
171 [DefaultValue(HorizontalAlignment.Left)]
172 public virtual HorizontalAlignment Alignment {
177 if (value != alignment) {
180 if (table_style != null && table_style.DataGrid != null) {
181 table_style.DataGrid.Invalidate ();
184 if (AlignmentChanged != null) {
185 AlignmentChanged (this, EventArgs.Empty);
192 public virtual DataGridTableStyle DataGridTableStyle {
198 protected int FontHeight {
200 if (fontheight != -1) {
204 if (table_style != null) {
205 //return table_style.DataGrid.FontHeight
209 // TODO: Default Datagrid font height
215 public AccessibleObject HeaderAccessibleObject {
217 return accesible_object;
222 public virtual string HeaderText {
227 if (value != header_text) {
230 if (table_style != null && table_style.DataGrid != null) {
231 table_style.DataGrid.Invalidate ();
234 if (HeaderTextChanged != null) {
235 HeaderTextChanged (this, EventArgs.Empty);
241 [Editor("System.Windows.Forms.Design.DataGridColumnStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
243 public string MappingName {
248 if (value != mapping_name) {
249 mapping_name = value;
251 if (MappingNameChanged != null) {
252 MappingNameChanged (this, EventArgs.Empty);
259 public virtual string NullText {
264 if (value != null_text) {
267 if (table_style != null && table_style.DataGrid != null) {
268 table_style.DataGrid.Invalidate ();
271 if (NullTextChanged != null) {
272 NullTextChanged (this, EventArgs.Empty);
280 [EditorBrowsable(EditorBrowsableState.Advanced)]
281 public virtual PropertyDescriptor PropertyDescriptor {
283 return property_descriptor;
286 if (value != property_descriptor) {
287 property_descriptor = value;
289 if (PropertyDescriptorChanged != null) {
290 PropertyDescriptorChanged (this, EventArgs.Empty);
296 [DefaultValue(false)]
297 public virtual bool ReadOnly {
302 if (value != _readonly) {
305 if (table_style != null && table_style.DataGrid != null) {
306 table_style.DataGrid.CalcAreasAndInvalidate ();
309 if (ReadOnlyChanged != null) {
310 ReadOnlyChanged (this, EventArgs.Empty);
318 public virtual int Width {
323 if (value != width) {
326 if (table_style != null && table_style.DataGrid != null) {
327 table_style.DataGrid.CalcAreasAndInvalidate ();
330 if (WidthChanged != null) {
331 WidthChanged (this, EventArgs.Empty);
337 #endregion // Public Instance Properties
339 #region Private Instance Properties
341 internal ArrowDrawing ArrowDrawingMode {
342 get { return arrow_drawing; }
343 set { arrow_drawing = value; }
346 // The logic seems to be that:
347 // - If DataGrid.ReadOnly is true all the tables and columns are readonly ignoring other settings
348 // - If DataGridTableStyle.ReadOnly is true all columns are readonly ignoring other settings
349 // - If DataGrid.ReadOnly and DataGridTableStyle.ReadOnly are false, the columns settings are mandatory
351 internal bool ParentReadOnly {
354 if (grid.ReadOnly == true) {
358 if (grid.ListManager != null && grid.ListManager.CanAddRows == false) {
363 if (table_style != null) {
364 if (table_style.ReadOnly == true) {
373 internal DataGridTableStyle TableStyle {
374 set { table_style = value; }
377 internal bool IsDefault {
378 get { return is_default; }
380 #endregion Private Instance Properties
382 #region Public Instance Methods
383 protected internal abstract void Abort (int rowNum);
386 protected void BeginUpdate ()
391 protected void CheckValidDataSource (CurrencyManager value)
394 throw new ArgumentNullException ("CurrencyManager cannot be null");
397 if (property_descriptor == null) {
398 throw new ApplicationException ("The PropertyDescriptor for this column is a null reference");
403 protected internal virtual void ColumnStartedEditing (Control editingControl)
408 protected internal abstract bool Commit (CurrencyManager dataSource, int rowNum);
411 protected internal virtual void ConcedeFocus ()
416 protected virtual AccessibleObject CreateHeaderAccessibleObject ()
418 return new DataGridColumnHeaderAccessibleObject (this);
421 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly)
423 Edit (source, rowNum, bounds, readOnly, string.Empty);
426 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText)
428 Edit (source, rowNum, bounds, readOnly, instantText, true);
431 protected internal abstract void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible);
435 protected void EndUpdate ()
440 protected internal virtual void EnterNullValue () {}
442 protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
444 CheckValidDataSource (source);
445 return property_descriptor.GetValue (source.GetItem (rowNum));
448 protected internal abstract int GetMinimumHeight ();
450 protected internal abstract int GetPreferredHeight (Graphics g, object value);
452 protected internal abstract Size GetPreferredSize (Graphics g, object value);
454 void IDataGridColumnStyleEditingNotificationService.ColumnStartedEditing (Control editingControl)
459 protected virtual void Invalidate ()
461 grid.grid_drawing.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);
480 property_descriptor.SetValue (source.GetItem (rowNum), value);
483 protected virtual void SetDataGrid (DataGrid value)
487 if (property_descriptor != null || value == null || value.ListManager == null) {
491 PropertyDescriptorCollection propcol = value.ListManager.GetItemProperties ();
492 for (int i = 0; i < propcol.Count ; i++) {
493 if (propcol[i].Name == mapping_name) {
494 property_descriptor = propcol[i];
500 protected virtual void SetDataGridInColumn (DataGrid value)
505 internal void SetDataGridInternal (DataGrid value)
507 SetDataGridInColumn (value);
510 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string instantText)
514 #endregion // Public Instance Methods
516 #region Private Instance Methods
517 virtual internal void OnMouseDown (MouseEventArgs e, int row, int column) {}
518 virtual internal void OnKeyDown (KeyEventArgs ke, int row, int column) {}
520 internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
523 g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderBackColor),
526 if (grid.FlatMode == false) {
529 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
530 bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
533 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
534 bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
536 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
537 bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 2);
540 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlDark),
541 bounds.X + bounds.Width - 1, bounds.Y + 2 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 2);
546 g.DrawString (HeaderText, DataGridTableStyle.HeaderFont, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderForeColor),
547 bounds, string_format_hdr);
549 if (arrow_drawing != ArrowDrawing.No) {
551 Point pnt = new Point (bounds.X + bounds.Width - 12, bounds.Y + ((bounds.Height - 6)/2));
553 if (arrow_drawing == ArrowDrawing.Ascending) {
554 g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y + 6, pnt.X + 3, pnt.Y);
555 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 6, pnt.Y + 6);
556 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 3, pnt.Y);
558 g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y, pnt.X + 3, pnt.Y + 6);
559 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 6, pnt.Y);
560 g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 3, pnt.Y + 6);
566 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
568 g.FillRectangle (backBrush, bounds);
569 PaintGridLine (g, bounds);
572 internal void PaintGridLine (Graphics g, Rectangle bounds)
574 if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
578 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
579 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
581 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
582 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
585 #endregion Private Instance Methods
589 public event EventHandler AlignmentChanged;
590 public event EventHandler FontChanged;
591 public event EventHandler HeaderTextChanged;
592 public event EventHandler MappingNameChanged;
593 public event EventHandler NullTextChanged;
596 [EditorBrowsable(EditorBrowsableState.Advanced)]
597 public event EventHandler PropertyDescriptorChanged;
598 public event EventHandler ReadOnlyChanged;
599 public event EventHandler WidthChanged;