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