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