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>
28 using System.ComponentModel;
29 using System.Globalization;
31 namespace System.Windows.Forms {
33 [Editor ("System.Windows.Forms.Design.DataGridViewCellStyleEditor, " + Consts.AssemblySystem_Design,
34 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
35 [TypeConverter (typeof (DataGridViewCellStyleConverter))]
36 public class DataGridViewCellStyle : ICloneable {
38 private DataGridViewContentAlignment alignment;
39 private Color backColor;
40 private object dataSourceNullValue;
42 private Color foreColor;
43 private string format;
44 private IFormatProvider formatProvider;
45 private object nullValue;
46 private Padding padding;
47 private Color selectionBackColor;
48 private Color selectionForeColor;
50 private DataGridViewTriState wrapMode;
52 public DataGridViewCellStyle ()
54 alignment = DataGridViewContentAlignment.NotSet;
55 backColor = Color.Empty;
56 dataSourceNullValue = DBNull.Value;
58 foreColor = Color.Empty;
59 format = String.Empty;
60 nullValue = string.Empty;
61 padding = Padding.Empty;
62 selectionBackColor = Color.Empty;
63 selectionForeColor = Color.Empty;
65 wrapMode = DataGridViewTriState.NotSet;
68 public DataGridViewCellStyle (DataGridViewCellStyle dataGridViewCellStyle)
70 this.alignment = dataGridViewCellStyle.alignment;
71 this.backColor = dataGridViewCellStyle.backColor;
72 this.dataSourceNullValue = dataGridViewCellStyle.dataSourceNullValue;
73 this.font = dataGridViewCellStyle.font;
74 this.foreColor = dataGridViewCellStyle.foreColor;
75 this.format = dataGridViewCellStyle.format;
76 this.formatProvider = dataGridViewCellStyle.formatProvider;
77 this.nullValue = dataGridViewCellStyle.nullValue;
78 this.padding = dataGridViewCellStyle.padding;
79 this.selectionBackColor = dataGridViewCellStyle.selectionBackColor;
80 this.selectionForeColor = dataGridViewCellStyle.selectionForeColor;
81 this.tag = dataGridViewCellStyle.tag;
82 this.wrapMode = dataGridViewCellStyle.wrapMode;
85 [DefaultValue (DataGridViewContentAlignment.NotSet)]
86 public DataGridViewContentAlignment Alignment {
87 get { return alignment; }
89 if (!Enum.IsDefined(typeof(DataGridViewContentAlignment), value)) {
90 throw new InvalidEnumArgumentException("Value is not valid DataGridViewContentAlignment.");
92 if (alignment != value) {
99 public Color BackColor {
100 get { return backColor; }
102 if (backColor != value) {
110 [EditorBrowsable (EditorBrowsableState.Advanced)]
111 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
112 public object DataSourceNullValue {
113 get { return dataSourceNullValue; }
115 if (dataSourceNullValue != value) {
116 dataSourceNullValue = value;
132 public Color ForeColor {
133 get { return foreColor; }
135 if (foreColor != value) {
143 [Editor ("System.Windows.Forms.Design.FormatStringEditor, " + Consts.AssemblySystem_Design,
144 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
145 [EditorBrowsable (EditorBrowsableState.Advanced)]
146 public string Format {
147 get { return format; }
149 if (format != value) {
157 [EditorBrowsable (EditorBrowsableState.Advanced)]
158 public IFormatProvider FormatProvider {
160 if (formatProvider == null)
161 return CultureInfo.CurrentCulture;
162 return formatProvider;
165 if (formatProvider != value) {
166 formatProvider = value;
173 [EditorBrowsable (EditorBrowsableState.Advanced)]
174 public bool IsDataSourceNullValueDefault {
175 get { return dataSourceNullValue != null; }
179 [EditorBrowsable (EditorBrowsableState.Advanced)]
180 public bool IsFormatProviderDefault {
181 get { return formatProvider == null; }
185 [EditorBrowsable (EditorBrowsableState.Advanced)]
186 public bool IsNullValueDefault {
188 if (nullValue is string) {
189 return (string) nullValue == string.Empty;
196 [TypeConverter (typeof (StringConverter))]
197 public object NullValue {
198 get { return nullValue; }
200 if (nullValue != value) {
207 public Padding Padding {
208 get { return padding; }
210 if (padding != value) {
217 public Color SelectionBackColor {
218 get { return selectionBackColor; }
220 if (selectionBackColor != value) {
221 selectionBackColor = value;
227 public Color SelectionForeColor {
228 get { return selectionForeColor; }
230 if (selectionForeColor != value) {
231 selectionForeColor = value;
238 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
249 [DefaultValue (DataGridViewTriState.NotSet)]
250 public DataGridViewTriState WrapMode {
251 get { return wrapMode; }
253 if (!Enum.IsDefined(typeof(DataGridViewTriState), value)) {
254 throw new InvalidEnumArgumentException("Value is not valid DataGridViewTriState.");
256 if (wrapMode != value) {
263 public virtual void ApplyStyle (DataGridViewCellStyle dataGridViewCellStyle)
265 // We should only apply the new style if it is 'set'.
266 if (dataGridViewCellStyle.alignment != DataGridViewContentAlignment.NotSet)
267 this.alignment = dataGridViewCellStyle.alignment;
268 if (dataGridViewCellStyle.backColor != Color.Empty)
269 this.backColor = dataGridViewCellStyle.backColor;
270 if (dataGridViewCellStyle.dataSourceNullValue != DBNull.Value)
271 this.dataSourceNullValue = dataGridViewCellStyle.dataSourceNullValue;
272 if (dataGridViewCellStyle.font != null)
273 this.font = dataGridViewCellStyle.font;
274 if (dataGridViewCellStyle.foreColor != Color.Empty)
275 this.foreColor = dataGridViewCellStyle.foreColor;
276 if (dataGridViewCellStyle.format != string.Empty)
277 this.format = dataGridViewCellStyle.format;
278 if (dataGridViewCellStyle.formatProvider != null)
279 this.formatProvider = dataGridViewCellStyle.formatProvider;
280 if (dataGridViewCellStyle.nullValue != null)
281 this.nullValue = dataGridViewCellStyle.nullValue;
282 if (dataGridViewCellStyle.padding != Padding.Empty)
283 this.padding = dataGridViewCellStyle.padding;
284 if (dataGridViewCellStyle.selectionBackColor != Color.Empty)
285 this.selectionBackColor = dataGridViewCellStyle.selectionBackColor;
286 if (dataGridViewCellStyle.selectionForeColor != Color.Empty)
287 this.selectionForeColor = dataGridViewCellStyle.selectionForeColor;
288 if (dataGridViewCellStyle.tag != null)
289 this.tag = dataGridViewCellStyle.tag;
290 if (dataGridViewCellStyle.wrapMode != DataGridViewTriState.NotSet)
291 this.wrapMode = dataGridViewCellStyle.wrapMode;
294 object ICloneable.Clone ()
299 public virtual DataGridViewCellStyle Clone ()
301 return new DataGridViewCellStyle(this);
304 public override bool Equals (object o)
306 if (o is DataGridViewCellStyle) {
307 DataGridViewCellStyle o_aux = (DataGridViewCellStyle) o;
308 return this.alignment == o_aux.alignment &&
309 this.backColor == o_aux.backColor &&
310 this.dataSourceNullValue == o_aux.dataSourceNullValue &&
311 this.font == o_aux.font &&
312 this.foreColor == o_aux.foreColor &&
313 this.format == o_aux.format &&
314 this.formatProvider == o_aux.formatProvider &&
315 this.nullValue == o_aux.nullValue &&
316 this.padding == o_aux.padding &&
317 this.selectionBackColor == o_aux.selectionBackColor &&
318 this.selectionForeColor == o_aux.selectionForeColor &&
319 this.tag == o_aux.tag &&
320 this.wrapMode == o_aux.wrapMode;
325 public override int GetHashCode ()
327 return base.GetHashCode();
330 public override string ToString ()
332 /////////////////////////////////////// COMPROBAR EN Windows ////////////////////////////////
336 internal event EventHandler StyleChanged;
338 internal void OnStyleChanged ()
340 if (StyleChanged != null) {
341 StyleChanged(this, EventArgs.Empty);
345 internal StringFormat SetAlignment (StringFormat format)
348 case DataGridViewContentAlignment.BottomCenter:
349 case DataGridViewContentAlignment.BottomLeft:
350 case DataGridViewContentAlignment.BottomRight:
351 format.LineAlignment = StringAlignment.Near;
353 case DataGridViewContentAlignment.MiddleCenter:
354 case DataGridViewContentAlignment.MiddleLeft:
355 case DataGridViewContentAlignment.MiddleRight:
356 format.LineAlignment = StringAlignment.Center;
358 case DataGridViewContentAlignment.TopCenter:
359 case DataGridViewContentAlignment.TopLeft:
360 case DataGridViewContentAlignment.TopRight:
361 format.LineAlignment = StringAlignment.Far;
366 case DataGridViewContentAlignment.BottomCenter:
367 case DataGridViewContentAlignment.MiddleCenter:
368 case DataGridViewContentAlignment.TopCenter:
369 format.Alignment = StringAlignment.Center;
371 case DataGridViewContentAlignment.BottomLeft:
372 case DataGridViewContentAlignment.MiddleLeft:
373 case DataGridViewContentAlignment.TopLeft:
374 format.Alignment = StringAlignment.Near;
377 case DataGridViewContentAlignment.BottomRight:
378 case DataGridViewContentAlignment.MiddleRight:
379 case DataGridViewContentAlignment.TopRight:
380 format.Alignment = StringAlignment.Far;