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