svn path=/branches/mono-1-1-9/mcs/; revision=51206
[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                 #region Local Variables
112                 internal HorizontalAlignment alignment;
113                 private int fontheight;
114                 internal DataGridTableStyle table_style;
115                 private string header_text;
116                 private string mapping_name;
117                 private string null_text;
118                 private PropertyDescriptor property_descriptor;
119                 private bool _readonly;
120                 private int width;
121                 internal bool is_default;
122                 internal DataGrid grid;
123                 private DataGridColumnHeaderAccessibleObject accesible_object;
124                 private StringFormat string_format_hdr;
125                 static string def_null_text = "(null)";
126                 #endregion      // Local Variables
127
128                 #region Constructors
129                 public DataGridColumnStyle ()
130                 {
131                         CommmonConstructor ();
132                         property_descriptor = null;
133                 }
134
135                 public DataGridColumnStyle (PropertyDescriptor prop)
136                 {
137                         CommmonConstructor ();
138                         property_descriptor = prop;
139                 }
140
141                 private void CommmonConstructor ()
142                 {
143                         fontheight = -1;
144                         table_style = null;
145                         header_text = string.Empty;
146                         mapping_name  = string.Empty;
147                         null_text = def_null_text;
148                         accesible_object = new DataGridColumnHeaderAccessibleObject (this);
149                         _readonly = false;
150                         width = -1;
151                         grid = null;
152                         is_default = false;
153                         alignment = HorizontalAlignment.Left;
154                         string_format_hdr = new StringFormat ();
155                         string_format_hdr.FormatFlags |= StringFormatFlags.NoWrap;
156                         string_format_hdr.LineAlignment  = StringAlignment.Center;
157                 }
158
159                 #endregion
160
161                 #region Public Instance Properties
162                 [Localizable(true)]
163                 [DefaultValue(HorizontalAlignment.Left)]
164                 public virtual HorizontalAlignment Alignment {
165                         get {
166                                 return alignment;
167                         }
168                         set {                           
169                                 if (value != alignment) {
170                                         alignment = value;
171                                         
172                                         if (table_style != null && table_style.DataGrid != null) {
173                                                 table_style.DataGrid.Invalidate ();
174                                         }
175                                         
176                                         if (AlignmentChanged != null) {
177                                                 AlignmentChanged (this, EventArgs.Empty);
178                                         }                                       
179                                 }
180                         }
181                 }
182
183                 [Browsable(false)]
184                 public virtual DataGridTableStyle DataGridTableStyle {
185                         get {
186                                 return table_style;
187                         }                       
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                                         if (table_style != null && table_style.DataGrid != null) {
223                                                 table_style.DataGrid.Invalidate ();
224                                         }
225
226                                         if (HeaderTextChanged != null) {
227                                                 HeaderTextChanged (this, EventArgs.Empty);
228                                         }
229                                 }
230                         }
231                 }
232
233                 [Editor("System.Windows.Forms.Design.DataGridColumnStyleMappingNameEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
234                 [Localizable(true)]
235                 public string MappingName {
236                         get {
237                                 return mapping_name;
238                         }
239                         set {
240                                 if (value != mapping_name) {
241                                         mapping_name = value;
242
243                                         if (MappingNameChanged != null) {
244                                                 MappingNameChanged (this, EventArgs.Empty);
245                                         }
246                                 }
247                         }
248                 }
249
250                 [Localizable(true)]
251                 public virtual string NullText {
252                         get {
253                                 return null_text;
254                         }
255                         set {
256                                 if (value != null_text) {
257                                         null_text = value;
258                                         
259                                         if (table_style != null && table_style.DataGrid != null) {
260                                                 table_style.DataGrid.Invalidate ();
261                                         }
262
263                                         if (NullTextChanged != null) {
264                                                 NullTextChanged (this, EventArgs.Empty);
265                                         }
266                                 }
267                         }
268                 }
269
270                 [Browsable(false)]
271                 [DefaultValue(null)]
272                 [EditorBrowsable(EditorBrowsableState.Advanced)]
273                 public virtual PropertyDescriptor PropertyDescriptor {
274                         get {
275                                 return property_descriptor;
276                         }
277                         set {
278                                 if (value != property_descriptor) {
279                                         property_descriptor = value;                                    
280
281                                         if (PropertyDescriptorChanged != null) {
282                                                 PropertyDescriptorChanged (this, EventArgs.Empty);
283                                         }
284                                 }
285                         }
286                 }
287
288                 [DefaultValue(false)]
289                 public virtual bool ReadOnly  {
290                         get {
291                                 return _readonly;
292                         }
293                         set {
294                                 if (value != _readonly) {
295                                         _readonly = value;
296                                         
297                                         if (table_style != null && table_style.DataGrid != null) {
298                                                 table_style.DataGrid.CalcAreasAndInvalidate ();
299                                         }
300                                         
301                                         if (ReadOnlyChanged != null) {
302                                                 ReadOnlyChanged (this, EventArgs.Empty);
303                                         }
304                                 }
305                         }
306                 }
307
308                 [DefaultValue(100)]
309                 [Localizable(true)]
310                 public virtual int Width {
311                         get {
312                                 return width;
313                         }
314                         set {
315                                 if (value != width) {
316                                         width = value;
317                                         
318                                         if (table_style != null && table_style.DataGrid != null) {
319                                                 table_style.DataGrid.CalcAreasAndInvalidate ();
320                                         }
321
322                                         if (WidthChanged != null) {
323                                                 WidthChanged (this, EventArgs.Empty);
324                                         }
325                                 }
326                         }
327                 }
328
329                 #endregion      // Public Instance Properties
330                 
331                 #region Private Instance Properties
332                 
333                 // The logic seems to be that: 
334                 // - If DataGrid.ReadOnly is true all the tables and columns are readonly ignoring other settings
335                 // - If DataGridTableStyle.ReadOnly is true all columns are readonly ignoring other settings
336                 // - If DataGrid.ReadOnly and DataGridTableStyle.ReadOnly are false, the columns settings are mandatory
337                 //
338                 internal bool ParentReadOnly {
339                         get {                           
340                                 if (grid != null) {
341                                         if (grid.ReadOnly == true) {
342                                                 return true;
343                                         }
344                                         
345                                         if (grid.ListManager != null && grid.ListManager.CanAddRows == false) {
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                                 throw new ApplicationException ("The PropertyDescriptor for this column is a null reference");
386                         }
387                 }
388
389                 [MonoTODO]
390                 protected internal virtual void ColumnStartedEditing (Control editingControl)
391                 {
392
393                 }
394
395                 protected internal abstract bool Commit (CurrencyManager dataSource, int rowNum);
396
397
398                 protected internal virtual void ConcedeFocus ()
399                 {
400
401                 }
402                 
403                 protected virtual AccessibleObject CreateHeaderAccessibleObject ()
404                 {
405                         return new DataGridColumnHeaderAccessibleObject (this);
406                 }
407
408                 protected internal virtual void Edit (CurrencyManager source, int rowNum,  Rectangle bounds,  bool readOnly)
409                 {
410                         Edit (source, rowNum, bounds, readOnly, string.Empty);
411                 }
412                 
413                 protected internal virtual void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText)
414                 {       
415                         Edit (source, rowNum, bounds, readOnly, instantText, true);
416                 }
417
418                 protected internal abstract void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly,   string instantText,  bool cellIsVisible);
419
420
421                 [MonoTODO]
422                 protected void EndUpdate ()
423                 {
424
425                 }
426
427                 protected internal virtual void EnterNullValue () {}
428                 
429                 protected internal virtual object GetColumnValueAtRow (CurrencyManager source, int rowNum)
430                 {                       
431                         CheckValidDataSource (source);
432                         return property_descriptor.GetValue (source.GetItem (rowNum));
433                 }
434
435                 protected internal abstract int GetMinimumHeight ();
436
437                 protected internal abstract int GetPreferredHeight (Graphics g, object value);
438
439                 protected internal abstract Size GetPreferredSize (Graphics g,  object value);
440
441                 void  IDataGridColumnStyleEditingNotificationService.ColumnStartedEditing (Control editingControl)
442                 {
443
444                 }
445
446                 protected virtual void Invalidate ()
447                 {
448                         grid.grid_drawing.InvalidateColumn (this);
449                 }
450
451                 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum);
452                 protected internal abstract void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight);
453                 
454                 protected internal virtual void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum,
455                         Brush backBrush,  Brush foreBrush, bool alignToRight) {}
456
457                 protected internal virtual void ReleaseHostedControl () {}
458
459                 public void ResetHeaderText ()
460                 {
461                         HeaderText = string.Empty;
462                 }
463
464                 protected internal virtual void SetColumnValueAtRow (CurrencyManager source, int rowNum,  object value)
465                 {
466                         CheckValidDataSource (source);
467                         property_descriptor.SetValue (source.GetItem (rowNum), value);                  
468                 }
469
470                 protected virtual void SetDataGrid (DataGrid value)
471                 {
472                         grid = value;
473                         
474                         if (property_descriptor != null || value == null || value.ListManager == null) {
475                                 return;
476                         }
477                         
478                         PropertyDescriptorCollection propcol = value.ListManager.GetItemProperties ();
479                         for (int i = 0; i < propcol.Count ; i++) {
480                                 if (propcol[i].Name == mapping_name) {
481                                         property_descriptor = propcol[i];
482                                         break;
483                                 }
484                         }                       
485                 }
486
487                 protected virtual void SetDataGridInColumn (DataGrid value)
488                 {
489                         SetDataGrid (value);
490                 }
491                 
492                 internal void SetDataGridInternal (DataGrid value)
493                 {
494                         SetDataGridInColumn (value);
495                 }
496
497                 protected internal virtual void UpdateUI (CurrencyManager source, int rowNum, string instantText)
498                 {
499
500                 }
501                 #endregion      // Public Instance Methods
502                 
503                 #region Private Instance Methods
504                 virtual internal void OnMouseDown (MouseEventArgs e, int row, int column) {}
505                 virtual internal void OnKeyDown (KeyEventArgs ke, int row, int column) {}
506                 
507                 internal void PaintHeader (Graphics g, Rectangle bounds, int colNum)
508                 {       
509                         // Background
510                         g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderBackColor), 
511                                 bounds);
512                                 
513                         if (grid.FlatMode == false) {                   
514                         
515                                 // Paint Borders
516                                 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),                 
517                                         bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
518                                 
519                                 if (colNum == 0) {      
520                                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
521                                                 bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
522                                 } else {
523                                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlLight),
524                                                 bounds.X, bounds.Y + 2, bounds.X, bounds.Y + bounds.Height - 2);
525                                 }
526                                 
527                                 g.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorControlDark),
528                                         bounds.X + bounds.Width - 1, bounds.Y + 2 , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 2);
529                         }
530                         
531                         bounds.X += 2;
532                         bounds.Width -= 2;
533                         g.DrawString (HeaderText, DataGridTableStyle.HeaderFont, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.CurrentHeaderForeColor), 
534                                 bounds, string_format_hdr);
535                 }
536                                 
537                 internal void PaintNewRow (Graphics g, Rectangle bounds, Brush backBrush, Brush foreBrush)
538                 {
539                         g.FillRectangle (backBrush, bounds);
540                         PaintGridLine (g, bounds);
541                 }
542                 
543                 internal void PaintGridLine (Graphics g, Rectangle bounds)
544                 {
545                         if (table_style.CurrentGridLineStyle != DataGridLineStyle.Solid) {
546                                 return;
547                         }
548                         
549                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
550                                 bounds.X, bounds.Y + bounds.Height - 1, bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1);
551                         
552                         g.DrawLine (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor),
553                                 bounds.X + bounds.Width - 1, bounds.Y , bounds.X + bounds.Width - 1, bounds.Y + bounds.Height);
554                 }
555                 
556                 #endregion Private Instance Methods
557
558
559                 #region Events
560                 public event EventHandler AlignmentChanged;
561                 public event EventHandler FontChanged;
562                 public event EventHandler HeaderTextChanged;
563                 public event EventHandler MappingNameChanged;
564                 public event EventHandler NullTextChanged;
565
566                 [Browsable(false)]
567                 [EditorBrowsable(EditorBrowsableState.Advanced)]
568                 public event EventHandler PropertyDescriptorChanged;
569                 public event EventHandler ReadOnlyChanged;
570                 public event EventHandler WidthChanged;
571                 #endregion      // Events
572         }
573 }