2010-04-27 Ivan Zlatev <ivan@ivanz.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewColumn.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 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25
26
27 #if NET_2_0
28
29 using System;
30 using System.ComponentModel;
31
32 namespace System.Windows.Forms {
33
34         [Designer ("System.Windows.Forms.Design.DataGridViewColumnDesigner, " + Consts.AssemblySystem_Design,
35                    "System.ComponentModel.Design.IDesigner")]
36         [TypeConverter (typeof (DataGridViewColumnConverter))]
37         [ToolboxItem ("")]
38         [DesignTimeVisible (false)]
39         public class DataGridViewColumn : DataGridViewBand, IComponent, IDisposable {
40                 private bool auto_generated;
41                 private DataGridViewAutoSizeColumnMode autoSizeMode;
42                 private DataGridViewCell cellTemplate;
43                 private ContextMenuStrip contextMenuStrip;
44                 private string dataPropertyName;
45                 private int displayIndex;
46                 private int dividerWidth;
47                 private float fillWeight;
48                 private bool frozen;
49                 private DataGridViewColumnHeaderCell headerCell;
50                 private bool isDataBound;
51                 private int minimumWidth = 5;
52                 private string name = "";
53                 private bool readOnly;
54                 private ISite site;
55                 private DataGridViewColumnSortMode sortMode;
56                 private string toolTipText;
57                 private Type valueType;
58                 private bool visible = true;
59                 private int width = 100;
60                 private int dataColumnIndex;
61
62                 private bool headerTextSet = false;
63
64                 public DataGridViewColumn () {
65                         cellTemplate = null;
66                         base.DefaultCellStyle = new DataGridViewCellStyle();
67                         readOnly = false;
68                         headerCell = new DataGridViewColumnHeaderCell();
69                         headerCell.SetColumnIndex(Index);
70                         headerCell.Value = string.Empty;
71                         displayIndex = -1;
72                         dataColumnIndex = -1;
73                         dataPropertyName = string.Empty;
74                         fillWeight = 100.0F;
75                         sortMode = DataGridViewColumnSortMode.NotSortable;
76                         SetState (DataGridViewElementStates.Visible);
77                 }
78
79                 public DataGridViewColumn (DataGridViewCell cellTemplate) : this () {
80                         this.cellTemplate = (DataGridViewCell) cellTemplate.Clone();
81                 }
82
83                 [DefaultValue (DataGridViewAutoSizeColumnMode.NotSet)]
84                 [RefreshProperties (RefreshProperties.Repaint)]
85                 public DataGridViewAutoSizeColumnMode AutoSizeMode {
86                         get { return autoSizeMode; }
87                         set {
88                                 if (autoSizeMode != value) {
89                                         DataGridViewAutoSizeColumnMode old_value = autoSizeMode;
90                                         autoSizeMode = value;
91                                         
92                                         if (DataGridView != null) {
93                                                 DataGridView.OnAutoSizeColumnModeChanged (new DataGridViewAutoSizeColumnModeEventArgs (this, old_value));
94                                                 DataGridView.AutoResizeColumnsInternal ();
95                                         }
96                                 }
97                         }
98                 }
99
100                 [Browsable (false)]
101                 [EditorBrowsable (EditorBrowsableState.Advanced)]
102                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
103                 public virtual DataGridViewCell CellTemplate {
104                         get { return cellTemplate; }
105                         set { cellTemplate = value; }
106                 }
107
108                 [Browsable (false)]
109                 [EditorBrowsable (EditorBrowsableState.Advanced)]
110                 public Type CellType {
111                         get {
112                                 if (cellTemplate == null) {
113                                         return null;
114                                 }
115                                 return cellTemplate.GetType();
116                         }
117                 }
118
119                 [DefaultValue (null)]
120                 public override ContextMenuStrip ContextMenuStrip {
121                         get { return contextMenuStrip; }
122                         set {
123                                 if (contextMenuStrip != value) {
124                                         contextMenuStrip = value;
125                                         if (DataGridView != null) {
126                                                 DataGridView.OnColumnContextMenuStripChanged(new DataGridViewColumnEventArgs(this));
127                                         }
128                                 }
129                         }
130                 }
131
132                 [Browsable (true)]
133                 [DefaultValue ("")]
134                 [Editor ("System.Windows.Forms.Design.DataGridViewColumnDataPropertyNameEditor, " + Consts.AssemblySystem_Design,
135                          typeof (System.Drawing.Design.UITypeEditor))]
136                 [TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
137                 public string DataPropertyName {
138                         get { return dataPropertyName; }
139                         set {
140                                 if (dataPropertyName != value) {
141                                         dataPropertyName = value;
142                                         if (DataGridView != null) {
143                                                 DataGridView.OnColumnDataPropertyNameChanged(new DataGridViewColumnEventArgs(this));
144                                         }
145                                 }
146                         }
147                 }
148
149                 [Browsable (true)]
150                 public override DataGridViewCellStyle DefaultCellStyle {
151                         get {
152                                 return base.DefaultCellStyle;
153                         }
154                         set {
155                                 if (DefaultCellStyle != value) {
156                                         base.DefaultCellStyle = value;
157                                         if (DataGridView != null) {
158                                                 DataGridView.OnColumnDefaultCellStyleChanged(new DataGridViewColumnEventArgs(this));
159                                         }
160                                 }
161                         }
162                 }
163
164                 [Browsable (false)]
165                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
166                 public int DisplayIndex {
167                         get {
168                                 if (displayIndex < 0) {
169                                         return Index;
170                                 }
171                                 return displayIndex;
172                         }
173                         set {
174                                 if (displayIndex != value) {
175                                         if (value < 0 || value > Int32.MaxValue) {
176                                                 throw new ArgumentOutOfRangeException("DisplayIndex is out of range");
177                                         }
178                                         displayIndex = value;
179                                         if (DataGridView != null) {
180                                                 DataGridView.Columns.RegenerateSortedList ();
181                                                 DataGridView.OnColumnDisplayIndexChanged(new DataGridViewColumnEventArgs(this));
182                                         }
183                                 }
184                         }
185                 }
186
187                 internal int DisplayIndexInternal {
188                         get { return DisplayIndex; }
189                         set { displayIndex = value; }
190                 }
191
192                 internal int DataColumnIndex {
193                         get { return dataColumnIndex; }
194                         set { 
195                                 dataColumnIndex = value;
196                                 if (dataColumnIndex >= 0)
197                                         isDataBound = true;
198                         }
199                 }
200
201                 [DefaultValue (0)]
202                 public int DividerWidth {
203                         get { return dividerWidth; }
204                         set {
205                                 if (dividerWidth != value) {
206                                         dividerWidth = value;
207                                         if (DataGridView != null) {
208                                                 DataGridView.OnColumnDividerWidthChanged(new DataGridViewColumnEventArgs(this));
209                                         }
210                                 }
211                         }
212                 }
213
214                 [DefaultValue (100)]
215                 public float FillWeight {
216                         get { return fillWeight; }
217                         set {
218                                 fillWeight = value;
219                                 /* When the System.Windows.Forms.DataGridViewColumn.InheritedAutoSizeMode property value is System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill, the column is resized along with other columns in that mode so that all visible columns in the control exactly fill the horizontal width of the available display area. All fill-mode columns in the control divide the available space in proportions determined by their System.Windows.Forms.DataGridViewColumn.FillWeight property values. For more information about column fill mode, see Column Fill Mode in the Windows Forms DataGridView Control.
220
221 The maximum sum of System.Windows.Forms.DataGridViewColumn.FillWeight values for all columns in a System.Windows.Forms.DataGridView control is 65535.
222                                 */
223                         }
224                 }
225
226                 [DefaultValue (false)]
227                 [RefreshProperties (RefreshProperties.All)]
228                 public override bool Frozen {
229                         get { return frozen; }
230                         set { frozen = value; }
231                 }
232                 /* When a column is frozen, all the columns to its left (or to its right in right-to-left languages) are frozen as well. The frozen and unfrozen columns form two groups. If column repositioning is enabled by setting the System.Windows.Forms.DataGridView.AllowUserToOrderColumns property to true, the user cannot drag a column from one group to the other.
233 Example */
234
235                 [Browsable (false)]
236                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
237                 public DataGridViewColumnHeaderCell HeaderCell {
238                         get {
239                                 return headerCell;
240                         }
241                         set {
242                                 if (headerCell != value) {
243                                         headerCell = value;
244                                         if (DataGridView != null) {
245                                                 DataGridView.OnColumnHeaderCellChanged(new DataGridViewColumnEventArgs(this));
246                                         }
247                                 }
248                         }
249                 }
250
251                 [Localizable (true)]
252                 public string HeaderText {
253                         get {
254                                 if (headerCell.Value == null) {
255                                         return String.Empty;
256                                 }
257                                 return (string) headerCell.Value;
258                         }
259                         set {
260                                 headerCell.Value = value;
261                                 headerTextSet = true;
262                         }
263                 }
264
265                 internal bool AutoGenerated { get { return auto_generated; } set { auto_generated = value; } }
266                 internal bool HeaderTextSet { get { return headerTextSet; } }
267                 
268                 [Browsable (false)]
269                 [EditorBrowsable (EditorBrowsableState.Advanced)]
270                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
271                 public DataGridViewAutoSizeColumnMode InheritedAutoSizeMode {
272                         get {
273                                 if (this.DataGridView == null)
274                                         return this.autoSizeMode;
275                                 
276                                 if (this.autoSizeMode != DataGridViewAutoSizeColumnMode.NotSet)
277                                         return this.autoSizeMode;
278                                 
279                                 switch (this.DataGridView.AutoSizeColumnsMode) {
280                                 case DataGridViewAutoSizeColumnsMode.AllCells:
281                                         return DataGridViewAutoSizeColumnMode.AllCells;
282                                 case DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader:
283                                         return DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
284                                 case DataGridViewAutoSizeColumnsMode.ColumnHeader:
285                                         return DataGridViewAutoSizeColumnMode.ColumnHeader;
286                                 case DataGridViewAutoSizeColumnsMode.DisplayedCells:
287                                         return DataGridViewAutoSizeColumnMode.DisplayedCells;
288                                 case DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeader:
289                                         return DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;
290                                 case DataGridViewAutoSizeColumnsMode.Fill:
291                                         return DataGridViewAutoSizeColumnMode.Fill;
292                                 default:
293                                         return DataGridViewAutoSizeColumnMode.None;
294                                 }                               
295                         }
296                 }
297
298                 [Browsable (false)]
299                 public override DataGridViewCellStyle InheritedStyle {
300                         get {
301                                 if (DataGridView == null) {
302                                         return base.DefaultCellStyle;
303                                 }
304                                 else {
305                                         if (base.DefaultCellStyle == null) {
306                                                 return DataGridView.DefaultCellStyle;
307                                         }
308                                         else {
309                                                 DataGridViewCellStyle style = (DataGridViewCellStyle) base.DefaultCellStyle.Clone();
310                                                 /////// Combination with dataGridView.DefaultCellStyle
311                                                 return style;
312                                         }
313                                 }
314                         }
315                 }
316
317                 [Browsable (false)]
318                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
319                 public bool IsDataBound {
320                         get { return isDataBound; }
321                 }
322
323                 [DefaultValue (5)]
324                 [RefreshProperties (RefreshProperties.Repaint)]
325                 [Localizable (true)]
326                 public int MinimumWidth {
327                         get { return minimumWidth; }
328                         set {
329                                 if (minimumWidth != value) {
330                                         if (value < 2 || value > Int32.MaxValue) {
331                                                 throw new ArgumentOutOfRangeException("MinimumWidth is out of range");
332                                         }
333                                         minimumWidth = value;
334                                         if (DataGridView != null) {
335                                                 DataGridView.OnColumnMinimumWidthChanged(new DataGridViewColumnEventArgs(this));
336                                         }
337                                 }
338                         }
339                 }
340
341                 [Browsable (false)]
342                 public string Name {
343                         get { return name; }
344                         set {
345                                 if (name != value) {
346                                         if (value == null)
347                                                 name = string.Empty;
348                                         else
349                                                 name = value;
350                                         if (!headerTextSet) {
351                                                 headerCell.Value = name;
352                                         }
353                                         if (DataGridView != null) {
354                                                 DataGridView.OnColumnNameChanged(new DataGridViewColumnEventArgs(this));
355                                         }
356                                 }
357                         }
358                 }
359
360                 public override bool ReadOnly {
361                         get {
362
363                                 if (DataGridView != null && DataGridView.ReadOnly)
364                                         return true;
365
366                                 return readOnly;
367                         }
368                         set { readOnly = value; }
369                 }
370
371                 public override DataGridViewTriState Resizable {
372                         get { return base.Resizable; }
373                         set { base.Resizable = value; }
374                 }
375
376                 [Browsable (false)]
377                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
378                 public ISite Site {
379                         get { return site; }
380                         set { site = value; }
381                 }
382
383                 [DefaultValue (DataGridViewColumnSortMode.NotSortable)]
384                 public DataGridViewColumnSortMode SortMode {
385                         get { return sortMode; }
386                         set {
387                                 if (DataGridView != null && value == DataGridViewColumnSortMode.Automatic) {
388                                         if (DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect ||
389                                             DataGridView.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect)
390                                                 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic "+
391                                                                                      "while the DataGridView control's SelectionMode "+
392                                                                                      "is set to FullColumnSelect or ColumnHeaderSelect.");
393                                 }
394
395                                 if (sortMode != value) {
396                                         sortMode = value;
397                                         if (DataGridView != null) {
398                                                 DataGridView.OnColumnSortModeChanged(new DataGridViewColumnEventArgs(this));
399                                         }
400                                 }
401                         }
402                 }
403
404                 [DefaultValue ("")]
405                 [Localizable (true)]
406                 public string ToolTipText {
407                         get {
408                                 if (toolTipText == null)
409                                         return string.Empty;
410                                 return toolTipText; }
411                         set {
412                                 if (toolTipText != value) {
413                                         toolTipText = value;
414                                         if (DataGridView != null) {
415                                                 DataGridView.OnColumnToolTipTextChanged(new DataGridViewColumnEventArgs(this));
416                                         }
417                                 }
418                         }
419                 }
420
421                 [Browsable (false)]
422                 [DefaultValue (null)]
423                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
424                 public Type ValueType {
425                         get { return valueType; }
426                         set { valueType = value; }
427                 }
428
429                 [DefaultValue (true)]
430                 [Localizable (true)]
431                 public override bool Visible {
432                         get { return visible; }
433                         set {
434                                 visible = value;
435                                 if (DataGridView != null)
436                                         DataGridView.Invalidate ();
437                         }
438                 }
439
440                 [Localizable (true)]
441                 [RefreshProperties (RefreshProperties.Repaint)]
442                 public int Width {
443                         get { return width; }
444                         set {
445                                 if (width != value) {
446                                         if (value < minimumWidth) {
447                                                 throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
448                                         }
449                                         width = value;
450                                         if (DataGridView != null)  {
451                                                 DataGridView.Invalidate ();
452                                                 DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
453                                         }
454
455                                 }
456                         }
457                 }
458
459                 // XXX should we do something like Component.Events?
460                 [Browsable (false)]
461                 [EditorBrowsable (EditorBrowsableState.Advanced)]
462                 public event EventHandler Disposed;
463
464                 public override object Clone () {
465                         return this.MemberwiseClone();
466                         /*
467                         DataGridViewColumn result = new DataGridViewColumn();
468                         return result;
469                         */
470                 }
471
472                 public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
473                         switch (autoSizeColumnMode) {
474                         case DataGridViewAutoSizeColumnMode.NotSet:
475                         case DataGridViewAutoSizeColumnMode.None:
476                         case DataGridViewAutoSizeColumnMode.Fill:
477                                 throw new ArgumentException("AutoSizeColumnMode is invalid");
478                         }
479                         if (fixedHeight) {
480                                 return 0;
481                         }
482                         else {
483                                 return 0;
484                         }
485                 }
486
487                 public override string ToString () {
488                         return Name + ", Index: " + base.Index.ToString() + ".";
489                 }
490
491                 protected override void Dispose (bool disposing) {
492                         if (disposing) {
493                         }
494                 }
495
496                 internal override void SetDataGridView (DataGridView dataGridView) {
497                         if (sortMode == DataGridViewColumnSortMode.Automatic && dataGridView != null && dataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect) {
498                                 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
499                         }
500                         
501                         base.SetDataGridView (dataGridView);
502                         headerCell.SetDataGridView(dataGridView);
503                 }
504
505                 internal override void SetIndex (int index) {
506                         base.SetIndex(index);
507                         headerCell.SetColumnIndex(Index);
508                 }
509
510                 internal override void SetState (DataGridViewElementStates state) {
511                         if (State != state) {
512                                 base.SetState(state);
513                                 if (DataGridView != null) {
514                                         DataGridView.OnColumnStateChanged(new DataGridViewColumnStateChangedEventArgs(this, state));
515                                 }
516                         }
517                 }
518
519         }
520         
521         internal class DataGridViewColumnConverter : TypeConverter
522         {
523         }
524 }
525
526 #endif