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