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