* TabControl.cs: Show the tooltip depending on the value
[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                         dataSourceNullValue = DBNull.Value;
59                         font = null;
60                         foreColor = Color.Empty;
61                         format = String.Empty;
62                         nullValue = string.Empty;
63                         padding = Padding.Empty;
64                         selectionBackColor = Color.Empty;
65                         selectionForeColor = Color.Empty;
66                         tag = null;
67                         wrapMode = DataGridViewTriState.NotSet;
68                 }
69
70                 public DataGridViewCellStyle (DataGridViewCellStyle dataGridViewCellStyle)
71                 {
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;
85                 }
86
87                 [DefaultValue (DataGridViewContentAlignment.NotSet)]
88                 public DataGridViewContentAlignment Alignment {
89                         get { return alignment; }
90                         set {
91                                 if (!Enum.IsDefined(typeof(DataGridViewContentAlignment), value)) {
92                                         throw new InvalidEnumArgumentException("Value is not valid DataGridViewContentAlignment.");
93                                 }
94                                 if (alignment != value) {
95                                         alignment = value;
96                                         OnStyleChanged();
97                                 }
98                         }
99                 }
100
101                 public Color BackColor {
102                         get { return backColor; }
103                         set {
104                                 if (backColor != value) {
105                                         backColor = value;
106                                         OnStyleChanged();
107                                 }
108                         }
109                 }
110
111                 [Browsable (false)]
112                 [EditorBrowsable (EditorBrowsableState.Advanced)]
113                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
114                 public object DataSourceNullValue {
115                         get { return dataSourceNullValue; }
116                         set {
117                                 if (dataSourceNullValue != value) {
118                                         dataSourceNullValue = value;
119                                         OnStyleChanged();
120                                 }
121                         }
122                 }
123
124                 public Font Font {
125                         get { return font; }
126                         set {
127                                 if (font != value) {
128                                         font = value;
129                                         OnStyleChanged();
130                                 }
131                         }
132                 }
133
134                 public Color ForeColor {
135                         get { return foreColor; }
136                         set {
137                                 if (foreColor != value) {
138                                         foreColor = value;
139                                         OnStyleChanged();
140                                 }
141                         }
142                 }
143
144                 [DefaultValue ("")]
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; }
150                         set {
151                                 if (format != value) {
152                                         format = value;
153                                         OnStyleChanged();
154                                 }
155                         }
156                 }
157
158                 [Browsable (false)]
159                 [EditorBrowsable (EditorBrowsableState.Advanced)]
160                 public IFormatProvider FormatProvider {
161                         get {
162                                 if (formatProvider == null)
163                                         return CultureInfo.CurrentCulture;
164                                 return formatProvider;
165                         }
166                         set {
167                                 if (formatProvider != value) {
168                                         formatProvider = value;
169                                         OnStyleChanged();
170                                 }
171                         }
172                 }
173
174                 [Browsable (false)]
175                 [EditorBrowsable (EditorBrowsableState.Advanced)]
176                 public bool IsDataSourceNullValueDefault {
177                         get { return dataSourceNullValue != null; }
178                 }
179
180                 [Browsable (false)]
181                 [EditorBrowsable (EditorBrowsableState.Advanced)]
182                 public bool IsFormatProviderDefault {
183                         get { return formatProvider == null; }
184                 }
185
186                 [Browsable (false)]
187                 [EditorBrowsable (EditorBrowsableState.Advanced)]
188                 public bool IsNullValueDefault {
189                         get {
190                                 if (nullValue is string) {
191                                         return (string) nullValue == string.Empty;
192                                 }
193                                 return false;
194                         }
195                 }
196
197                 [DefaultValue ("")]
198                 [TypeConverter (typeof (StringConverter))]
199                 public object NullValue {
200                         get { return nullValue; }
201                         set {
202                                 if (nullValue != value) {
203                                         nullValue = value;
204                                         OnStyleChanged();
205                                 }
206                         }
207                 }
208
209                 public Padding Padding {
210                         get { return padding; }
211                         set {
212                                 if (padding != value) {
213                                         padding = value;
214                                         OnStyleChanged();
215                                 }
216                         }
217                 }
218
219                 public Color SelectionBackColor {
220                         get { return selectionBackColor; }
221                         set {
222                                 if (selectionBackColor != value) {
223                                         selectionBackColor = value;
224                                         OnStyleChanged();
225                                 }
226                         }
227                 }
228
229                 public Color SelectionForeColor {
230                         get { return selectionForeColor; }
231                         set {
232                                 if (selectionForeColor != value) {
233                                         selectionForeColor = value;
234                                         OnStyleChanged();
235                                 }
236                         }
237                 }
238
239                 [Browsable (false)]
240                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
241                 public object Tag {
242                         get { return tag; }
243                         set {
244                                 if (tag != value) {
245                                         tag = value;
246                                         OnStyleChanged();
247                                 }
248                         }
249                 }
250
251                 [DefaultValue (DataGridViewTriState.NotSet)]
252                 public DataGridViewTriState WrapMode {
253                         get { return wrapMode; }
254                         set {
255                                 if (!Enum.IsDefined(typeof(DataGridViewTriState), value)) {
256                                         throw new InvalidEnumArgumentException("Value is not valid DataGridViewTriState.");
257                                 }
258                                 if (wrapMode != value) {
259                                         wrapMode = value;
260                                         OnStyleChanged();
261                                 }
262                         }
263                 }
264
265                 public virtual void ApplyStyle (DataGridViewCellStyle dataGridViewCellStyle)
266                 {
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;
294                 }
295
296                 object ICloneable.Clone ()
297                 {
298                         return Clone ();
299                 }
300
301                 public virtual DataGridViewCellStyle Clone ()
302                 {
303                         return new DataGridViewCellStyle(this);
304                 }
305
306                 public override bool Equals (object o)
307                 {
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;
323                         }
324                         return false;
325                 }
326
327                 public override int GetHashCode ()
328                 {
329                         return base.GetHashCode();
330                 }
331
332                 public override string ToString ()
333                 {
334                         /////////////////////////////////////// COMPROBAR EN Windows ////////////////////////////////
335                         return "";
336                 }
337
338                 internal event EventHandler StyleChanged;
339
340                 internal void OnStyleChanged ()
341                 {
342                         if (StyleChanged != null) {
343                                 StyleChanged(this, EventArgs.Empty);
344                         }
345                 }
346                         
347                 internal StringFormat SetAlignment (StringFormat format)
348                 {
349                         switch (Alignment) {
350                         case DataGridViewContentAlignment.BottomCenter:
351                         case DataGridViewContentAlignment.BottomLeft:
352                         case DataGridViewContentAlignment.BottomRight:
353                                 format.LineAlignment = StringAlignment.Near;
354                                 break;
355                         case DataGridViewContentAlignment.MiddleCenter:
356                         case DataGridViewContentAlignment.MiddleLeft:
357                         case DataGridViewContentAlignment.MiddleRight:
358                                 format.LineAlignment = StringAlignment.Center;
359                                 break;
360                         case DataGridViewContentAlignment.TopCenter:
361                         case DataGridViewContentAlignment.TopLeft:
362                         case DataGridViewContentAlignment.TopRight:
363                                 format.LineAlignment = StringAlignment.Far;
364                                 break;
365                         }
366
367                         switch (Alignment) {
368                         case DataGridViewContentAlignment.BottomCenter:
369                         case DataGridViewContentAlignment.MiddleCenter:
370                         case DataGridViewContentAlignment.TopCenter:
371                                 format.Alignment = StringAlignment.Center;
372                                 break;
373                         case DataGridViewContentAlignment.BottomLeft:
374                         case DataGridViewContentAlignment.MiddleLeft:
375                         case DataGridViewContentAlignment.TopLeft:
376                                 format.Alignment = StringAlignment.Near;
377                                 break;
378
379                         case DataGridViewContentAlignment.BottomRight:
380                         case DataGridViewContentAlignment.MiddleRight:
381                         case DataGridViewContentAlignment.TopRight:
382                                 format.Alignment = StringAlignment.Far;
383                                 break;
384                         }
385                         return format;
386                 }
387         }
388
389 }
390
391 #endif