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