New test.
[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                                         if (AlignmentChanged != null) {
180                                                 AlignmentChanged (this, EventArgs.Empty);
181                                         }                                       
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                                         if (HeaderTextChanged != null) {
226                                                 HeaderTextChanged (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                 public string MappingName {
235                         get {
236                                 return mapping_name;
237                         }
238                         set {
239                                 if (value != mapping_name) {
240                                         mapping_name = value;
241
242                                         if (MappingNameChanged != null) {
243                                                 MappingNameChanged (this, EventArgs.Empty);
244                                         }
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                                         if (NullTextChanged != null) {
263                                                 NullTextChanged (this, EventArgs.Empty);
264                                         }
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                                         if (PropertyDescriptorChanged != null) {
281                                                 PropertyDescriptorChanged (this, EventArgs.Empty);
282                                         }
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                                         if (ReadOnlyChanged != null) {
301                                                 ReadOnlyChanged (this, EventArgs.Empty);
302                                         }
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                                         if (WidthChanged != null) {
322                                                 WidthChanged (this, EventArgs.Empty);
323                                         }
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
489                 protected virtual void SetDataGrid (DataGrid value)
490                 {
491                         grid = value;
492
493                         property_descriptor = null;
494                 }
495
496                 protected virtual void SetDataGridInColumn (DataGrid value)
497                 {
498                         SetDataGrid (value);
499                 }
500                 
501                 internal void SetDataGridInternal (DataGrid value)
502                 {
503                         SetDataGridInColumn (value);
504                 }
505
506                 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string instantText)
507                 {
508
509                 }
510                 #endregion      // Public Instance Methods
511                 
512                 #region Private Instance Methods
513                 virtual internal void OnMouseDown (MouseEventArgs e, int row, int column) {}
514                 virtual internal void OnKeyDown (KeyEventArgs ke, int row, int column) {}
515                 
516                 internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
517                 {       
518                         // Background
519                         g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderBackColor), 
520                                 bounds);
521                                 
522                         if (grid.FlatMode == false) {                   
523                         
524                                 // Paint Borders
525                                 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),                 
526                                         bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
527                                 
528                                 if (colNum == 0) {      
529                                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
530                                                 bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
531                                 } else {
532                                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
533                                                 bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 2);
534                                 }
535                                 
536                                 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlDark),
537                                         bounds.X + bounds.Width - 1, bounds.Y + 2 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 2);
538                         }
539                         
540                         bounds.X += 2;
541                         bounds.Width -= 2;
542                         g.DrawString (HeaderText, DataGridTableStyle.HeaderFont, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderForeColor), 
543                                 bounds, string_format_hdr);
544
545                         if (arrow_drawing != ArrowDrawing.No) {
546                                 // Draw 6 x 6
547                                 Point pnt = new Point (bounds.X + bounds.Width  - 12, bounds.Y + ((bounds.Height - 6)/2));
548                                 
549                                 if (arrow_drawing == ArrowDrawing.Ascending) {
550                                         g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y + 6, pnt.X + 3, pnt.Y);
551                                         g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 6, pnt.Y + 6);
552                                         g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y + 6, pnt.X + 3, pnt.Y);
553                                 } else {
554                                         g.DrawLine (SystemPens.ControlLightLight, pnt.X + 6, pnt.Y, pnt.X + 3, pnt.Y + 6);
555                                         g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 6, pnt.Y);
556                                         g.DrawLine (SystemPens.ControlDark, pnt.X, pnt.Y, pnt.X + 3, pnt.Y + 6);
557                                 }
558                                 
559                         }
560                 }
561                                 
562                 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
563                 {
564                         g.FillRectangle (backBrush, bounds);
565                         PaintGridLine (g, bounds);
566                 }
567                 
568                 internal void PaintGridLine (Graphics g, Rectangle bounds)
569                 {
570                         if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
571                                 return;
572                         }
573                         
574                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
575                                 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
576                         
577                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
578                                 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
579                 }
580                 
581                 #endregion Private Instance Methods
582
583
584                 #region Events
585                 public event EventHandler AlignmentChanged;
586                 public event EventHandler FontChanged;
587                 public event EventHandler HeaderTextChanged;
588                 public event EventHandler MappingNameChanged;
589                 public event EventHandler NullTextChanged;
590
591                 [Browsable(false)]
592                 [EditorBrowsable(EditorBrowsableState.Advanced)]
593                 public event EventHandler PropertyDescriptorChanged;
594                 public event EventHandler ReadOnlyChanged;
595                 public event EventHandler WidthChanged;
596                 #endregion      // Events
597         }
598 }