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