also, disable the call do BindColumns in
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewCellStyle.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.Drawing;
30 using System.ComponentModel;
31 using System.Globalization;
32
33 namespace System.Windows.Forms {
34
35         [Editor ("System.Windows.Forms.Design.DataGridViewCellStyleEditor, " + Consts.AssemblySystem_Design,
36                  "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
37         [TypeConverter (typeof (DataGridViewCellStyleConverter))]
38         public class DataGridViewCellStyle : ICloneable {
39
40                 private DataGridViewContentAlignment alignment;
41                 private Color backColor;
42                 private object dataSourceNullValue;
43                 private Font font;
44                 private Color foreColor;
45                 private string format;
46                 private IFormatProvider formatProvider;
47                 private object nullValue;
48                 private Padding padding;
49                 private Color selectionBackColor;
50                 private Color selectionForeColor;
51                 private object tag;
52                 private DataGridViewTriState wrapMode;
53
54                 public DataGridViewCellStyle ()
55                 {
56                         alignment = DataGridViewContentAlignment.NotSet;
57                         backColor = Color.Empty;
58                         font = null;
59                         foreColor = Color.Empty;
60                         format = String.Empty;
61                         nullValue = string.Empty;
62                         padding = Padding.Empty;
63                         selectionBackColor = Color.Empty;
64                         selectionForeColor = Color.Empty;
65                         tag = null;
66                         wrapMode = DataGridViewTriState.NotSet;
67                 }
68
69                 public DataGridViewCellStyle (DataGridViewCellStyle dataGridViewCellStyle)
70                 {
71                         ApplyStyle(dataGridViewCellStyle);
72                 }
73
74                 [DefaultValue (DataGridViewContentAlignment.NotSet)]
75                 public DataGridViewContentAlignment Alignment {
76                         get { return alignment; }
77                         set {
78                                 if (!Enum.IsDefined(typeof(DataGridViewContentAlignment), value)) {
79                                         throw new InvalidEnumArgumentException("Value is not valid DataGridViewContentAlignment.");
80                                 }
81                                 if (alignment != value) {
82                                         alignment = value;
83                                         OnStyleChanged();
84                                 }
85                         }
86                 }
87
88                 public Color BackColor {
89                         get { return backColor; }
90                         set {
91                                 if (backColor != value) {
92                                         backColor = value;
93                                         OnStyleChanged();
94                                 }
95                         }
96                 }
97
98                 [Browsable (false)]
99                 [EditorBrowsable (EditorBrowsableState.Advanced)]
100                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
101                 public object DataSourceNullValue {
102                         get { return dataSourceNullValue; }
103                         set {
104                                 if (dataSourceNullValue != value) {
105                                         dataSourceNullValue = value;
106                                         OnStyleChanged();
107                                 }
108                         }
109                 }
110
111                 public Font Font {
112                         get { return font; }
113                         set {
114                                 if (font != value) {
115                                         font = value;
116                                         OnStyleChanged();
117                                 }
118                         }
119                 }
120
121                 public Color ForeColor {
122                         get { return foreColor; }
123                         set {
124                                 if (foreColor != value) {
125                                         foreColor = value;
126                                         OnStyleChanged();
127                                 }
128                         }
129                 }
130
131                 [DefaultValue ("")]
132                 [Editor ("System.Windows.Forms.Design.FormatStringEditor, " + Consts.AssemblySystem_Design,
133                          "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
134                 [EditorBrowsable (EditorBrowsableState.Advanced)]
135                 public string Format {
136                         get { return format; }
137                         set {
138                                 if (format != value) {
139                                         format = value;
140                                         OnStyleChanged();
141                                 }
142                         }
143                 }
144
145                 [Browsable (false)]
146                 [EditorBrowsable (EditorBrowsableState.Advanced)]
147                 public IFormatProvider FormatProvider {
148                         get {
149                                 if (formatProvider == null)
150                                         return CultureInfo.CurrentCulture;
151                                 return formatProvider;
152                         }
153                         set {
154                                 if (formatProvider != value) {
155                                         formatProvider = value;
156                                         OnStyleChanged();
157                                 }
158                         }
159                 }
160
161                 [Browsable (false)]
162                 [EditorBrowsable (EditorBrowsableState.Advanced)]
163                 public bool IsDataSourceNullValueDefault {
164                         get { return dataSourceNullValue != null; }
165                 }
166
167                 [Browsable (false)]
168                 [EditorBrowsable (EditorBrowsableState.Advanced)]
169                 public bool IsFormatProviderDefault {
170                         get { return formatProvider == null; }
171                 }
172
173                 [Browsable (false)]
174                 [EditorBrowsable (EditorBrowsableState.Advanced)]
175                 public bool IsNullValueDefault {
176                         get {
177                                 if (nullValue is string) {
178                                         return (string) nullValue == string.Empty;
179                                 }
180                                 return false;
181                         }
182                 }
183
184                 [DefaultValue ("")]
185                 [TypeConverter (typeof (StringConverter))]
186                 public object NullValue {
187                         get { return nullValue; }
188                         set {
189                                 if (nullValue != value) {
190                                         nullValue = value;
191                                         OnStyleChanged();
192                                 }
193                         }
194                 }
195
196                 public Padding Padding {
197                         get { return padding; }
198                         set {
199                                 if (padding != value) {
200                                         padding = value;
201                                         OnStyleChanged();
202                                 }
203                         }
204                 }
205
206                 public Color SelectionBackColor {
207                         get { return selectionBackColor; }
208                         set {
209                                 if (value != Color.Empty && (int) value.A != 255) {
210                                         throw new ArgumentException("BackColor can't have alpha transparency component.");
211                                 }
212                                 if (selectionBackColor != value) {
213                                         selectionBackColor = value;
214                                         OnStyleChanged();
215                                 }
216                         }
217                 }
218
219                 public Color SelectionForeColor {
220                         get { return selectionForeColor; }
221                         set {
222                                 if (selectionForeColor != value) {
223                                         selectionForeColor = value;
224                                         OnStyleChanged();
225                                 }
226                         }
227                 }
228
229                 [Browsable (false)]
230                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
231                 public object Tag {
232                         get { return tag; }
233                         set {
234                                 if (tag != value) {
235                                         tag = value;
236                                         OnStyleChanged();
237                                 }
238                         }
239                 }
240
241                 [DefaultValue (DataGridViewTriState.NotSet)]
242                 public DataGridViewTriState WrapMode {
243                         get { return wrapMode; }
244                         set {
245                                 if (!Enum.IsDefined(typeof(DataGridViewTriState), value)) {
246                                         throw new InvalidEnumArgumentException("Value is not valid DataGridViewTriState.");
247                                 }
248                                 if (wrapMode != value) {
249                                         wrapMode = value;
250                                         OnStyleChanged();
251                                 }
252                         }
253                 }
254
255                 public virtual void ApplyStyle (DataGridViewCellStyle dataGridViewCellStyle)
256                 {
257                         this.alignment = dataGridViewCellStyle.alignment;
258                         this.backColor = dataGridViewCellStyle.backColor;
259                         this.dataSourceNullValue = dataGridViewCellStyle.dataSourceNullValue;
260                         this.font = dataGridViewCellStyle.font;
261                         this.foreColor = dataGridViewCellStyle.foreColor;
262                         this.format = dataGridViewCellStyle.format;
263                         this.formatProvider = dataGridViewCellStyle.formatProvider;
264                         this.nullValue = dataGridViewCellStyle.nullValue;
265                         this.padding = dataGridViewCellStyle.padding;
266                         this.selectionBackColor = dataGridViewCellStyle.selectionBackColor;
267                         this.selectionForeColor = dataGridViewCellStyle.selectionForeColor;
268                         this.tag = dataGridViewCellStyle.tag;
269                         this.wrapMode = dataGridViewCellStyle.wrapMode;
270                 }
271
272                 object ICloneable.Clone ()
273                 {
274                         return Clone ();
275                 }
276
277                 public virtual DataGridViewCellStyle Clone ()
278                 {
279                         return new DataGridViewCellStyle(this);
280                 }
281
282                 public override bool Equals (object o)
283                 {
284                         if (o is DataGridViewCellStyle) {
285                                 DataGridViewCellStyle o_aux = (DataGridViewCellStyle) o;
286                                 return this.alignment == o_aux.alignment &&
287                                         this.backColor == o_aux.backColor &&
288                                         this.dataSourceNullValue == o_aux.dataSourceNullValue &&
289                                         this.font == o_aux.font &&
290                                         this.foreColor == o_aux.foreColor &&
291                                         this.format == o_aux.format &&
292                                         this.formatProvider == o_aux.formatProvider &&
293                                         this.nullValue == o_aux.nullValue &&
294                                         this.padding == o_aux.padding &&
295                                         this.selectionBackColor == o_aux.selectionBackColor &&
296                                         this.selectionForeColor == o_aux.selectionForeColor &&
297                                         this.tag == o_aux.tag &&
298                                         this.wrapMode == o_aux.wrapMode;
299                         }
300                         return false;
301                 }
302
303                 public override int GetHashCode ()
304                 {
305                         return base.GetHashCode();
306                 }
307
308                 public override string ToString ()
309                 {
310                         /////////////////////////////////////// COMPROBAR EN Windows ////////////////////////////////
311                         return "";
312                 }
313
314                 internal event EventHandler StyleChanged;
315
316                 internal void OnStyleChanged ()
317                 {
318                         if (StyleChanged != null) {
319                                 StyleChanged(this, EventArgs.Empty);
320                         }
321                 }
322
323         }
324
325 }
326
327 #endif