Flush (work in progress)
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / FontUnit.cs
1 //
2 // System.Web.UI.WebControls.FontUnit.cs
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@novell.com)
6 //   Ben Maurer (bmaurer@ximian.com).
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Threading;
31 using System.Globalization;
32 using System.ComponentModel;
33 using System.Security.Permissions;
34 using System.Web.Util;
35
36 namespace System.Web.UI.WebControls {
37         [TypeConverter  (typeof (FontUnitConverter))]
38 #if NET_2_0
39         [Serializable]
40 #else
41         // CAS
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 #endif
44         public struct FontUnit {
45                 FontSize type;
46                 Unit unit;
47                 
48                 public static readonly FontUnit Empty;
49                 public static readonly FontUnit Smaller = new FontUnit (FontSize.Smaller);
50                 public static readonly FontUnit Larger = new FontUnit (FontSize.Larger);
51                 public static readonly FontUnit XXSmall = new FontUnit (FontSize.XXSmall);
52                 public static readonly FontUnit XSmall = new FontUnit (FontSize.XSmall);
53                 public static readonly FontUnit Small = new FontUnit (FontSize.Small);
54                 public static readonly FontUnit Medium = new FontUnit (FontSize.Medium);
55                 public static readonly FontUnit Large = new FontUnit (FontSize.Large);
56                 public static readonly FontUnit XLarge = new FontUnit (FontSize.XLarge);
57                 public static readonly FontUnit XXLarge = new FontUnit (FontSize.XXLarge);
58
59                 static string [] font_size_names = new string [] {null, null, "Smaller", "Larger", "XX-Small", "X-Small", "Small",
60                                                                 "Medium", "Large", "X-Large", "XX-Large" };
61                 
62                 public FontUnit (FontSize type)
63                 {
64                         int t = (int) type;
65                         
66                         if (t < 0 || t > (int)FontSize.XXLarge)
67                                 throw new ArgumentOutOfRangeException ("type");
68                         
69                         this.type = type;
70
71                         if (type == FontSize.AsUnit)
72                                 unit = new Unit (10, UnitType.Point);
73                         else
74                                 unit = Unit.Empty;
75                 }
76
77                 public FontUnit (int value) : this (new Unit (value, UnitType.Point))
78                 {
79                 }
80
81 #if NET_2_0
82                 public FontUnit (double value) : this (new Unit (value, UnitType.Point))
83                 {
84                 }
85
86                 public FontUnit (double value, UnitType type) : this (new Unit (value, type))
87                 {
88                 }
89 #endif
90
91                 public FontUnit (Unit value)
92                 {
93                         type = FontSize.AsUnit;
94                         unit = value;
95                 }
96                 
97                 public FontUnit (string value) : this (value, Thread.CurrentThread.CurrentCulture) {}
98
99                 public FontUnit (string value, CultureInfo culture)
100                 {
101                         if (value == null || value == String.Empty){
102                                 type = FontSize.NotSet;
103                                 unit = Unit.Empty;
104                                 return;
105                         }
106
107                         switch (value.ToLower (Helpers.InvariantCulture)){
108                         case "smaller": type = FontSize.Smaller; break;
109                         case "larger": type = FontSize.Larger; break;
110                         case "xxsmall": type = FontSize.XXSmall; break;
111                         case "xx-small": type = FontSize.XXSmall; break;
112                         case "xsmall": type = FontSize.XSmall; break;
113                         case "x-small": type = FontSize.XSmall; break;
114                         case "small": type = FontSize.Small; break;
115                         case "medium": type = FontSize.Medium; break;
116                         case "large": type = FontSize.Large; break;
117                         case "xlarge": type = FontSize.XLarge; break;
118                         case "x-large": type = FontSize.XLarge; break;
119                         case "xxlarge": type = FontSize.XXLarge; break;
120                         case "xx-large": type = FontSize.XXLarge; break;
121                         default:
122                                 type = FontSize.AsUnit;
123                                 unit = new Unit (value, culture);
124                                 return;
125                         }
126                         unit = Unit.Empty;
127                 }
128                 
129                 public bool IsEmpty {
130                         get {
131                                 return type == FontSize.NotSet;
132                         }
133                 }
134
135                 public FontSize Type {
136                         get {
137                                 return type;
138                         }
139                 }
140
141                 public Unit Unit {
142                         get {
143                                 return unit;
144                         }
145                 }
146                 
147                 public static FontUnit Parse (string s)
148                 {
149                         return new FontUnit (s);
150                 }
151
152                 public static FontUnit Parse (string s, CultureInfo culture)
153                 {
154                         return new FontUnit (s, culture);
155                 }
156
157                 public static FontUnit Point (int n)
158                 {
159                         return new FontUnit (n);
160                 }
161                 public override bool Equals (object obj)
162                 {
163                         if (obj is FontUnit){
164                                 FontUnit other = (FontUnit) obj;
165                                 return (other.type == type && other.unit == unit);
166                         }
167                         return false;
168                 }
169                 
170                 public override int GetHashCode ()
171                 {
172                         return type.GetHashCode () ^ unit.GetHashCode ();
173                 }
174                 
175                 public static bool operator == (FontUnit left, FontUnit right)
176                 {
177                         return left.type == right.type && left.unit == right.unit;
178                 }
179
180                 public static bool operator != (FontUnit left, FontUnit right)
181                 {
182                         return left.type != right.type || left.unit != right.unit;
183                 }
184                 
185                 public static implicit operator FontUnit (int n)
186                 {
187                         return new FontUnit (n);
188                 }
189
190 #if NET_2_0
191                 public string ToString (IFormatProvider fmt)
192                 {
193                         if (type == FontSize.NotSet)
194                                 return "";
195                         else if (type == FontSize.AsUnit)
196                                 return unit.ToString (fmt);
197                         else
198                                 return font_size_names [(int) type];
199                 }
200 #endif
201
202                 public string ToString (CultureInfo culture)
203                 {
204                         if (type == FontSize.NotSet)
205                                 return "";
206
207                         if (type == FontSize.AsUnit)
208                                 return unit.ToString (culture);
209
210                         return font_size_names [(int) type];
211                 }
212                         
213                 public override string ToString ()
214                 {
215                         return ToString (CultureInfo.CurrentCulture);
216                 }
217                 
218         }
219 }
220