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;
59 foreColor = Color.Empty;
60 format = String.Empty;
61 nullValue = string.Empty;
62 padding = Padding.Empty;
63 selectionBackColor = Color.Empty;
64 selectionForeColor = Color.Empty;
66 wrapMode = DataGridViewTriState.NotSet;
69 public DataGridViewCellStyle (DataGridViewCellStyle dataGridViewCellStyle)
71 ApplyStyle(dataGridViewCellStyle);
74 [DefaultValue (DataGridViewContentAlignment.NotSet)]
75 public DataGridViewContentAlignment Alignment {
76 get { return alignment; }
78 if (!Enum.IsDefined(typeof(DataGridViewContentAlignment), value)) {
79 throw new InvalidEnumArgumentException("Value is not valid DataGridViewContentAlignment.");
81 if (alignment != value) {
88 public Color BackColor {
89 get { return backColor; }
91 if (backColor != value) {
99 [EditorBrowsable (EditorBrowsableState.Advanced)]
100 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
101 public object DataSourceNullValue {
102 get { return dataSourceNullValue; }
104 if (dataSourceNullValue != value) {
105 dataSourceNullValue = value;
121 public Color ForeColor {
122 get { return foreColor; }
124 if (foreColor != value) {
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; }
138 if (format != value) {
146 [EditorBrowsable (EditorBrowsableState.Advanced)]
147 public IFormatProvider FormatProvider {
149 if (formatProvider == null)
150 return CultureInfo.CurrentCulture;
151 return formatProvider;
154 if (formatProvider != value) {
155 formatProvider = value;
162 [EditorBrowsable (EditorBrowsableState.Advanced)]
163 public bool IsDataSourceNullValueDefault {
164 get { return dataSourceNullValue != null; }
168 [EditorBrowsable (EditorBrowsableState.Advanced)]
169 public bool IsFormatProviderDefault {
170 get { return formatProvider == null; }
174 [EditorBrowsable (EditorBrowsableState.Advanced)]
175 public bool IsNullValueDefault {
177 if (nullValue is string) {
178 return (string) nullValue == string.Empty;
185 [TypeConverter (typeof (StringConverter))]
186 public object NullValue {
187 get { return nullValue; }
189 if (nullValue != value) {
196 public Padding Padding {
197 get { return padding; }
199 if (padding != value) {
206 public Color SelectionBackColor {
207 get { return selectionBackColor; }
209 if (value != Color.Empty && (int) value.A != 255) {
210 throw new ArgumentException("BackColor can't have alpha transparency component.");
212 if (selectionBackColor != value) {
213 selectionBackColor = value;
219 public Color SelectionForeColor {
220 get { return selectionForeColor; }
222 if (selectionForeColor != value) {
223 selectionForeColor = value;
230 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
241 [DefaultValue (DataGridViewTriState.NotSet)]
242 public DataGridViewTriState WrapMode {
243 get { return wrapMode; }
245 if (!Enum.IsDefined(typeof(DataGridViewTriState), value)) {
246 throw new InvalidEnumArgumentException("Value is not valid DataGridViewTriState.");
248 if (wrapMode != value) {
255 public virtual void ApplyStyle (DataGridViewCellStyle dataGridViewCellStyle)
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;
272 object ICloneable.Clone ()
277 public virtual DataGridViewCellStyle Clone ()
279 return new DataGridViewCellStyle(this);
282 public override bool Equals (object o)
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;
303 public override int GetHashCode ()
305 return base.GetHashCode();
308 public override string ToString ()
310 /////////////////////////////////////// COMPROBAR EN Windows ////////////////////////////////
314 internal event EventHandler StyleChanged;
316 internal void OnStyleChanged ()
318 if (StyleChanged != null) {
319 StyleChanged(this, EventArgs.Empty);
323 internal StringFormat SetAlignment (StringFormat format)
326 case DataGridViewContentAlignment.BottomCenter:
327 case DataGridViewContentAlignment.BottomLeft:
328 case DataGridViewContentAlignment.BottomRight:
329 format.LineAlignment = StringAlignment.Near;
331 case DataGridViewContentAlignment.MiddleCenter:
332 case DataGridViewContentAlignment.MiddleLeft:
333 case DataGridViewContentAlignment.MiddleRight:
334 format.LineAlignment = StringAlignment.Center;
336 case DataGridViewContentAlignment.TopCenter:
337 case DataGridViewContentAlignment.TopLeft:
338 case DataGridViewContentAlignment.TopRight:
339 format.LineAlignment = StringAlignment.Far;
344 case DataGridViewContentAlignment.BottomCenter:
345 case DataGridViewContentAlignment.MiddleCenter:
346 case DataGridViewContentAlignment.TopCenter:
347 format.Alignment = StringAlignment.Center;
349 case DataGridViewContentAlignment.BottomLeft:
350 case DataGridViewContentAlignment.MiddleLeft:
351 case DataGridViewContentAlignment.TopLeft:
352 format.Alignment = StringAlignment.Near;
355 case DataGridViewContentAlignment.BottomRight:
356 case DataGridViewContentAlignment.MiddleRight:
357 case DataGridViewContentAlignment.TopRight:
358 format.Alignment = StringAlignment.Far;