2010-04-25 Ivan Zlatev <ivan@ivanz.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridColumnStyle.cs
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Jordi Mas i Hernandez <jordi@ximian.com>
24 //      Chris Toshok <toshok@ximian.com>
25 //
26
27 using System.Drawing;
28 using System.ComponentModel;
29 using System.Runtime.InteropServices;
30 using System.Diagnostics;
31
32 namespace System.Windows.Forms
33 {
34         [DesignTimeVisible(false)]
35         [DefaultProperty("Header")]
36         [ToolboxItem(false)]
37         public abstract class DataGridColumnStyle : Component, IDataGridColumnStyleEditingNotificationService
38         {
39                 [ComVisible(true)]
40                 protected class DataGridColumnHeaderAccessibleObject : AccessibleObject
41                 {
42                         #region Local Variables
43                         private new DataGridColumnStyle owner;
44                         #endregion
45
46                         #region Constructors
47 #if NET_2_0
48                         public DataGridColumnHeaderAccessibleObject ()
49                         {
50                         }
51 #endif          
52                         public DataGridColumnHeaderAccessibleObject (DataGridColumnStyle owner)
53                         {
54                                 this.owner = owner;
55                         }
56                         #endregion //Constructors
57
58                         #region Public Instance Properties
59                         [MonoTODO ("Not implemented, will throw NotImplementedException")]
60                         public override Rectangle Bounds {
61                                 get {
62                                         throw new NotImplementedException ();
63                                 }
64                         }
65
66                         public override string Name {
67                                 get {
68                                         throw new NotImplementedException ();
69                                 }
70                         }
71
72                         protected DataGridColumnStyle Owner {
73                                 get { return owner; }
74                         }
75
76                         public override AccessibleObject Parent {
77                                 get {
78                                         throw new NotImplementedException ();
79                                 }
80                         }
81
82                         public override AccessibleRole Role {
83                                 get {
84                                         throw new NotImplementedException ();
85                                 }
86                         }
87                         #endregion
88
89                         #region Public Instance Methods
90                         [MonoTODO ("Not implemented, will throw NotImplementedException")]
91                         public override AccessibleObject Navigate (AccessibleNavigation navdir)
92                         {
93                                 throw new NotImplementedException ();
94                         }
95                         #endregion Public Instance Methods
96                 }
97
98                 protected class CompModSwitches
99                 {
100                         public CompModSwitches ()
101                         {
102                         }
103
104                         #region Public Instance Methods
105                         [MonoTODO ("Not implemented, will throw NotImplementedException")]
106                         public static TraceSwitch DGEditColumnEditing {
107                                 get {
108                                         throw new NotImplementedException ();
109                                 }
110                         }
111                         #endregion Public Instance Methods
112                 }
113                 
114                 internal enum ArrowDrawing
115                 {
116                         No = 0,
117                         Ascending = 1,
118                         Descending = 2
119                 }
120                 
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;
130                 private int width;
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;
136                 internal bool bound;
137                 #endregion      // Local Variables
138
139                 #region Constructors
140                 public DataGridColumnStyle () : this (null)
141                 {
142                 }
143
144                 public DataGridColumnStyle (PropertyDescriptor prop)
145                 {
146                         property_descriptor = prop;
147
148                         fontheight = -1;
149                         table_style = null;
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;
155                         width = -1;
156                         grid = null;
157                         is_default = false;
158                         alignment = HorizontalAlignment.Left;
159                 }
160
161                 #endregion
162
163                 #region Public Instance Properties
164                 [Localizable(true)]
165                 [DefaultValue(HorizontalAlignment.Left)]
166                 public virtual HorizontalAlignment Alignment {
167                         get {
168                                 return alignment;
169                         }
170                         set {
171                                 if (value != alignment) {
172                                         alignment = value;
173                                         
174                                         if (table_style != null && table_style.DataGrid != null) {
175                                                 table_style.DataGrid.Invalidate ();
176                                         }
177                                         
178                                         EventHandler eh = (EventHandler)(Events [AlignmentChangedEvent]);
179                                         if (eh != null)
180                                                 eh (this, EventArgs.Empty);
181                                 }
182                         }
183                 }
184
185                 [Browsable(false)]
186                 public virtual DataGridTableStyle DataGridTableStyle {
187                         get { return table_style; }
188                 }
189                 
190                 protected int FontHeight {
191                         get {
192                                 if (fontheight != -1) {
193                                         return fontheight;
194                                 }
195
196                                 if (table_style != null) {
197                                         //return table_style.DataGrid.FontHeight
198                                         return -1;
199                                 }
200
201                                 // TODO: Default Datagrid font height
202                                 return -1;
203                         }
204                 }
205
206                 [Browsable(false)]
207                 public AccessibleObject HeaderAccessibleObject {
208                         get {
209                                 return accesible_object;
210                         }
211                 }
212
213                 [Localizable(true)]
214                 public virtual string HeaderText {
215                         get {
216                                 return header_text;
217                         }
218                         set {
219                                 if (value != header_text) {
220                                         header_text = value;
221                                         
222                                         Invalidate ();
223
224                                         EventHandler eh = (EventHandler)(Events [HeaderTextChangedEvent]);
225                                         if (eh != null)
226                                                 eh (this, EventArgs.Empty);
227                                 }
228                         }
229                 }
230
231
232                 [Editor("System.Windows.Forms.Design.DataGridColumnStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
233                 [Localizable(true)]
234 #if NET_2_0
235                 [DefaultValue ("")]
236 #endif
237                 public string MappingName {
238                         get {
239                                 return mapping_name;
240                         }
241                         set {
242                                 if (value != mapping_name) {
243                                         mapping_name = value;
244
245                                         EventHandler eh = (EventHandler)(Events [MappingNameChangedEvent]);
246                                         if (eh != null)
247                                                 eh (this, EventArgs.Empty);
248                                 }
249                         }
250                 }
251
252                 [Localizable(true)]
253                 public virtual string NullText {
254                         get {
255                                 return null_text;
256                         }
257                         set {
258                                 if (value != null_text) {
259                                         null_text = value;
260                                         
261                                         if (table_style != null && table_style.DataGrid != null) {
262                                                 table_style.DataGrid.Invalidate ();
263                                         }
264
265                                         EventHandler eh = (EventHandler)(Events [NullTextChangedEvent]);
266                                         if (eh != null)
267                                                 eh (this, EventArgs.Empty);
268                                 }
269                         }
270                 }
271
272                 [Browsable(false)]
273                 [DefaultValue(null)]
274                 [EditorBrowsable(EditorBrowsableState.Advanced)]
275                 public virtual PropertyDescriptor PropertyDescriptor {
276                         get {
277                                 return property_descriptor;
278                         }
279                         set {
280                                 if (value != property_descriptor) {
281                                         property_descriptor = value;
282
283                                         EventHandler eh = (EventHandler)(Events [PropertyDescriptorChangedEvent]);
284                                         if (eh != null)
285                                                 eh (this, EventArgs.Empty);
286                                 }
287                         }
288                 }
289
290                 [DefaultValue(false)]
291                 public virtual bool ReadOnly {
292                         get {
293                                 return _readonly;
294                         }
295                         set {
296                                 if (value != _readonly) {
297                                         _readonly = value;
298                                         
299                                         if (table_style != null && table_style.DataGrid != null) {
300                                                 table_style.DataGrid.CalcAreasAndInvalidate ();
301                                         }
302                                         
303                                         EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
304                                         if (eh != null)
305                                                 eh (this, EventArgs.Empty);
306                                 }
307                         }
308                 }
309
310                 [DefaultValue(100)]
311                 [Localizable(true)]
312                 public virtual int Width {
313                         get {
314                                 return width;
315                         }
316                         set {
317                                 if (value != width) {
318                                         width = value;
319                                         
320                                         if (table_style != null && table_style.DataGrid != null) {
321                                                 table_style.DataGrid.CalcAreasAndInvalidate ();
322                                         }
323
324                                         EventHandler eh = (EventHandler)(Events [WidthChangedEvent]);
325                                         if (eh != null)
326                                                 eh (this, EventArgs.Empty);
327                                 }
328                         }
329                 }
330
331                 #endregion      // Public Instance Properties
332                 
333                 #region Private Instance Properties
334
335                 internal ArrowDrawing ArrowDrawingMode {
336                         get { return arrow_drawing; }
337                         set { arrow_drawing = value; }
338                 }
339                 
340                 internal bool TableStyleReadOnly {
341                         get {
342                                 return table_style != null && table_style.ReadOnly; 
343                         }
344                 }
345                 
346                 internal DataGridTableStyle TableStyle {
347                         set { table_style = value; }
348                 }
349                 
350                 internal bool IsDefault {
351                         get { return is_default; }
352                 }
353                 #endregion Private Instance Properties
354
355                 #region Public Instance Methods
356                 protected internal abstract void Abort (int rowNum);
357
358                 [MonoTODO ("Will not suspend updates")]
359                 protected void BeginUpdate ()
360                 {
361                 }
362                 
363                 protected void CheckValidDataSource (CurrencyManager value)
364                 {
365                         if (value == null) {
366                                 throw new ArgumentNullException ("CurrencyManager cannot be null");
367                         }
368                         
369                         if (property_descriptor == null) {
370                                 property_descriptor = value.GetItemProperties ()[mapping_name];
371
372 //                              Console.WriteLine ("mapping name = {0}", mapping_name);
373 //                              foreach (PropertyDescriptor prop in value.GetItemProperties ()) {
374 //                                      Console.WriteLine (" + prop = {0}", prop.Name);
375 //                              }
376
377                                 if (property_descriptor == null)
378                                         throw new InvalidOperationException ("The PropertyDescriptor for this column is a null reference");
379
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.*/
381
382                                 
383                         }
384                 }
385
386                 protected internal virtual void ColumnStartedEditing (Control editingControl)
387                 {
388                 }
389
390                 protected internal abstract bool Commit (CurrencyManager dataSource, int rowNum);
391
392
393                 protected internal virtual void ConcedeFocus ()
394                 {
395                 }
396                 
397                 protected virtual AccessibleObject CreateHeaderAccessibleObject ()
398                 {
399                         return new DataGridColumnHeaderAccessibleObject (this);
400                 }
401
402                 protected internal virtual void Edit (CurrencyManager source, int rowNum,  Rectangle bounds,  bool readOnly)
403                 {
404                         Edit (source, rowNum, bounds, readOnly, string.Empty);
405                 }
406
407 #if NET_2_0
408                 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string displayText)
409                 {
410                         string instantText = displayText;
411 #else
412                 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText)
413                 {
414 #endif
415                         Edit (source, rowNum, bounds, readOnly, instantText, true);
416                 }
417
418                 protected internal abstract void Edit (CurrencyManager source,
419                         int rowNum, Rectangle bounds, bool readOnly,
420 #if NET_2_0
421                         string displayText,
422 #else
423                         string instantText,
424 #endif
425                         bool cellIsVisible);
426
427
428                 protected void EndUpdate ()
429                 {
430                 }
431
432                 protected internal virtual void EnterNullValue () {}
433                 
434                 protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
435                 {
436                         CheckValidDataSource (source);
437                         if (rowNum >= source.Count)
438                                 return DBNull.Value;
439                         return property_descriptor.GetValue (source [rowNum]);
440                 }
441
442                 protected internal abstract int GetMinimumHeight ();
443
444                 protected internal abstract int GetPreferredHeight (Graphics g, object value);
445
446                 protected internal abstract Size GetPreferredSize (Graphics g,  object value);
447
448                 void IDataGridColumnStyleEditingNotificationService.ColumnStartedEditing (Control editingControl)
449                 {
450                         ColumnStartedEditing (editingControl);
451                 }
452
453                 protected virtual void Invalidate ()
454                 {
455                         if (grid != null)
456                                 grid.InvalidateColumn (this);
457                 }
458
459                 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum);
460                 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight);
461                 
462                 protected internal virtual void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum,
463                         Brush backBrush,  Brush foreBrush, bool alignToRight) {}
464
465                 protected internal virtual void ReleaseHostedControl () {}
466
467                 public void ResetHeaderText ()
468                 {
469                         HeaderText = string.Empty;
470                 }
471
472                 protected internal virtual void SetColumnValueAtRow (CurrencyManager source, int rowNum,  object value)
473                 {
474                         CheckValidDataSource (source);
475
476                         IEditableObject editable = source [rowNum] as IEditableObject;
477
478                         if (editable != null)
479                                 editable.BeginEdit ();
480
481                         property_descriptor.SetValue (source [rowNum], value);
482                 }
483
484                 protected virtual void SetDataGrid (DataGrid value)
485                 {
486                         grid = value;
487
488                         property_descriptor = null;
489
490                         // we don't check whether the DataGrid.ListManager is valid or not.
491                         // This is done by .net later as requiered, but not at this point.
492                 }
493
494                 protected virtual void SetDataGridInColumn (DataGrid value)
495                 {
496                         SetDataGrid (value);
497                 }
498                 
499                 internal void SetDataGridInternal (DataGrid value)
500                 {
501                         SetDataGridInColumn (value);
502                 }
503
504 #if NET_2_0
505                 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string displayText)
506                 {
507                 }
508 #else
509                 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string instantText)
510                 {
511                 }
512 #endif
513
514                 #endregion      // Public Instance Methods
515                 
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) {}
519                 
520                 internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
521                 {
522                         ThemeEngine.Current.DataGridPaintColumnHeader (g, bounds, grid, colNum);
523                 }
524                 
525                 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
526                 {
527                         g.FillRectangle (backBrush, bounds);
528                         PaintGridLine (g, bounds);
529                 }
530                 
531                 internal void PaintGridLine (Graphics g, Rectangle bounds)
532                 {
533                         if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
534                                 return;
535                         }
536                         
537                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
538                                 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
539                         
540                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
541                                 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
542                 }
543                 
544                 #endregion Private Instance Methods
545
546                 #region Events
547                 static object AlignmentChangedEvent = new object ();
548                 static object FontChangedEvent = new object ();
549                 static object HeaderTextChangedEvent = new object ();
550                 static object MappingNameChangedEvent = new object ();
551                 static object NullTextChangedEvent = new object ();
552                 static object PropertyDescriptorChangedEvent = new object ();
553                 static object ReadOnlyChangedEvent = new object ();
554                 static object WidthChangedEvent = new object ();
555
556                 public event EventHandler AlignmentChanged {
557                         add { Events.AddHandler (AlignmentChangedEvent, value); }
558                         remove { Events.RemoveHandler (AlignmentChangedEvent, value); }
559                 }
560
561                 public event EventHandler FontChanged {
562                         add { Events.AddHandler (FontChangedEvent, value); }
563                         remove { Events.RemoveHandler (FontChangedEvent, value); }
564                 }
565
566                 public event EventHandler HeaderTextChanged {
567                         add { Events.AddHandler (HeaderTextChangedEvent, value); }
568                         remove { Events.RemoveHandler (HeaderTextChangedEvent, value); }
569                 }
570
571                 public event EventHandler MappingNameChanged {
572                         add { Events.AddHandler (MappingNameChangedEvent, value); }
573                         remove { Events.RemoveHandler (MappingNameChangedEvent, value); }
574                 }
575
576                 public event EventHandler NullTextChanged {
577                         add { Events.AddHandler (NullTextChangedEvent, value); }
578                         remove { Events.RemoveHandler (NullTextChangedEvent, value); }
579                 }
580
581                 [Browsable(false)]
582                 [EditorBrowsable(EditorBrowsableState.Advanced)]
583                 public event EventHandler PropertyDescriptorChanged {
584                         add { Events.AddHandler (PropertyDescriptorChangedEvent, value); }
585                         remove { Events.RemoveHandler (PropertyDescriptorChangedEvent, value); }
586                 }
587
588                 public event EventHandler ReadOnlyChanged {
589                         add { Events.AddHandler (ReadOnlyChangedEvent, value); }
590                         remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
591                 }
592
593                 public event EventHandler WidthChanged {
594                         add { Events.AddHandler (WidthChangedEvent, value); }
595                         remove { Events.RemoveHandler (WidthChangedEvent, value); }
596                 }
597                 #endregion      // Events
598         }
599 }