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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Pedro MartÃnez Juliá <pedromj@gmail.com>
30 using System.ComponentModel;
31 using System.Globalization;
33 namespace System.Windows.Forms {
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 {
40 private DataGridViewContentAlignment alignment;
41 private Color backColor;
42 private object dataSourceNullValue;
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;
52 private DataGridViewTriState wrapMode;
54 public DataGridViewCellStyle ()
56 alignment = DataGridViewContentAlignment.NotSet;
57 backColor = Color.Empty;
58 dataSourceNullValue = DBNull.Value;
60 foreColor = Color.Empty;
61 format = String.Empty;
62 nullValue = string.Empty;
63 padding = Padding.Empty;
64 selectionBackColor = Color.Empty;
65 selectionForeColor = Color.Empty;
67 wrapMode = DataGridViewTriState.NotSet;
70 public DataGridViewCellStyle (DataGridViewCellStyle dataGridViewCellStyle)
72 this.alignment = dataGridViewCellStyle.alignment;
73 this.backColor = dataGridViewCellStyle.backColor;
74 this.dataSourceNullValue = dataGridViewCellStyle.dataSourceNullValue;
75 this.font = dataGridViewCellStyle.font;
76 this.foreColor = dataGridViewCellStyle.foreColor;
77 this.format = dataGridViewCellStyle.format;
78 this.formatProvider = dataGridViewCellStyle.formatProvider;
79 this.nullValue = dataGridViewCellStyle.nullValue;
80 this.padding = dataGridViewCellStyle.padding;
81 this.selectionBackColor = dataGridViewCellStyle.selectionBackColor;
82 this.selectionForeColor = dataGridViewCellStyle.selectionForeColor;
83 this.tag = dataGridViewCellStyle.tag;
84 this.wrapMode = dataGridViewCellStyle.wrapMode;
87 [DefaultValue (DataGridViewContentAlignment.NotSet)]
88 public DataGridViewContentAlignment Alignment {
89 get { return alignment; }
91 if (!Enum.IsDefined(typeof(DataGridViewContentAlignment), value)) {
92 throw new InvalidEnumArgumentException("Value is not valid DataGridViewContentAlignment.");
94 if (alignment != value) {
101 public Color BackColor {
102 get { return backColor; }
104 if (backColor != value) {
112 [EditorBrowsable (EditorBrowsableState.Advanced)]
113 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
114 public object DataSourceNullValue {
115 get { return dataSourceNullValue; }
117 if (dataSourceNullValue != value) {
118 dataSourceNullValue = value;
134 public Color ForeColor {
135 get { return foreColor; }
137 if (foreColor != value) {
145 [Editor ("System.Windows.Forms.Design.FormatStringEditor, " + Consts.AssemblySystem_Design,
146 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
147 [EditorBrowsable (EditorBrowsableState.Advanced)]
148 public string Format {
149 get { return format; }
151 if (format != value) {
159 [EditorBrowsable (EditorBrowsableState.Advanced)]
160 public IFormatProvider FormatProvider {
162 if (formatProvider == null)
163 return CultureInfo.CurrentCulture;
164 return formatProvider;
167 if (formatProvider != value) {
168 formatProvider = value;
175 [EditorBrowsable (EditorBrowsableState.Advanced)]
176 public bool IsDataSourceNullValueDefault {
177 get { return dataSourceNullValue != null; }
181 [EditorBrowsable (EditorBrowsableState.Advanced)]
182 public bool IsFormatProviderDefault {
183 get { return formatProvider == null; }
187 [EditorBrowsable (EditorBrowsableState.Advanced)]
188 public bool IsNullValueDefault {
190 if (nullValue is string) {
191 return (string) nullValue == string.Empty;
198 [TypeConverter (typeof (StringConverter))]
199 public object NullValue {
200 get { return nullValue; }
202 if (nullValue != value) {
209 public Padding Padding {
210 get { return padding; }
212 if (padding != value) {
219 public Color SelectionBackColor {
220 get { return selectionBackColor; }
222 if (selectionBackColor != value) {
223 selectionBackColor = value;
229 public Color SelectionForeColor {
230 get { return selectionForeColor; }
232 if (selectionForeColor != value) {
233 selectionForeColor = value;
240 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
251 [DefaultValue (DataGridViewTriState.NotSet)]
252 public DataGridViewTriState WrapMode {
253 get { return wrapMode; }
255 if (!Enum.IsDefined(typeof(DataGridViewTriState), value)) {
256 throw new InvalidEnumArgumentException("Value is not valid DataGridViewTriState.");
258 if (wrapMode != value) {
265 public virtual void ApplyStyle (DataGridViewCellStyle dataGridViewCellStyle)
267 // We should only apply the new style if it is 'set'.
268 if (dataGridViewCellStyle.alignment != DataGridViewContentAlignment.NotSet)
269 this.alignment = dataGridViewCellStyle.alignment;
270 if (dataGridViewCellStyle.backColor != Color.Empty)
271 this.backColor = dataGridViewCellStyle.backColor;
272 if (dataGridViewCellStyle.dataSourceNullValue != DBNull.Value)
273 this.dataSourceNullValue = dataGridViewCellStyle.dataSourceNullValue;
274 if (dataGridViewCellStyle.font != null)
275 this.font = dataGridViewCellStyle.font;
276 if (dataGridViewCellStyle.foreColor != Color.Empty)
277 this.foreColor = dataGridViewCellStyle.foreColor;
278 if (dataGridViewCellStyle.format != string.Empty)
279 this.format = dataGridViewCellStyle.format;
280 if (dataGridViewCellStyle.formatProvider != null)
281 this.formatProvider = dataGridViewCellStyle.formatProvider;
282 if (dataGridViewCellStyle.nullValue != null)
283 this.nullValue = dataGridViewCellStyle.nullValue;
284 if (dataGridViewCellStyle.padding != Padding.Empty)
285 this.padding = dataGridViewCellStyle.padding;
286 if (dataGridViewCellStyle.selectionBackColor != Color.Empty)
287 this.selectionBackColor = dataGridViewCellStyle.selectionBackColor;
288 if (dataGridViewCellStyle.selectionForeColor != Color.Empty)
289 this.selectionForeColor = dataGridViewCellStyle.selectionForeColor;
290 if (dataGridViewCellStyle.tag != null)
291 this.tag = dataGridViewCellStyle.tag;
292 if (dataGridViewCellStyle.wrapMode != DataGridViewTriState.NotSet)
293 this.wrapMode = dataGridViewCellStyle.wrapMode;
296 object ICloneable.Clone ()
301 public virtual DataGridViewCellStyle Clone ()
303 return new DataGridViewCellStyle(this);
306 public override bool Equals (object o)
308 if (o is DataGridViewCellStyle) {
309 DataGridViewCellStyle o_aux = (DataGridViewCellStyle) o;
310 return this.alignment == o_aux.alignment &&
311 this.backColor == o_aux.backColor &&
312 this.dataSourceNullValue == o_aux.dataSourceNullValue &&
313 this.font == o_aux.font &&
314 this.foreColor == o_aux.foreColor &&
315 this.format == o_aux.format &&
316 this.formatProvider == o_aux.formatProvider &&
317 this.nullValue == o_aux.nullValue &&
318 this.padding == o_aux.padding &&
319 this.selectionBackColor == o_aux.selectionBackColor &&
320 this.selectionForeColor == o_aux.selectionForeColor &&
321 this.tag == o_aux.tag &&
322 this.wrapMode == o_aux.wrapMode;
327 public override int GetHashCode ()
329 return base.GetHashCode();
332 public override string ToString ()
334 /////////////////////////////////////// COMPROBAR EN Windows ////////////////////////////////
338 internal event EventHandler StyleChanged;
340 internal void OnStyleChanged ()
342 if (StyleChanged != null) {
343 StyleChanged(this, EventArgs.Empty);
347 internal StringFormat SetAlignment (StringFormat format)
350 case DataGridViewContentAlignment.BottomCenter:
351 case DataGridViewContentAlignment.BottomLeft:
352 case DataGridViewContentAlignment.BottomRight:
353 format.LineAlignment = StringAlignment.Near;
355 case DataGridViewContentAlignment.MiddleCenter:
356 case DataGridViewContentAlignment.MiddleLeft:
357 case DataGridViewContentAlignment.MiddleRight:
358 format.LineAlignment = StringAlignment.Center;
360 case DataGridViewContentAlignment.TopCenter:
361 case DataGridViewContentAlignment.TopLeft:
362 case DataGridViewContentAlignment.TopRight:
363 format.LineAlignment = StringAlignment.Far;
368 case DataGridViewContentAlignment.BottomCenter:
369 case DataGridViewContentAlignment.MiddleCenter:
370 case DataGridViewContentAlignment.TopCenter:
371 format.Alignment = StringAlignment.Center;
373 case DataGridViewContentAlignment.BottomLeft:
374 case DataGridViewContentAlignment.MiddleLeft:
375 case DataGridViewContentAlignment.TopLeft:
376 format.Alignment = StringAlignment.Near;
379 case DataGridViewContentAlignment.BottomRight:
380 case DataGridViewContentAlignment.MiddleRight:
381 case DataGridViewContentAlignment.TopRight:
382 format.Alignment = StringAlignment.Far;