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