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