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