2007-01-31 Igor Zelmanovich <igorz@mainsoft.com>
[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
35 namespace System.Web.UI.WebControls {
36         [TypeConverter  (typeof (FontUnitConverter))]
37 #if NET_2_0
38         [Serializable]
39 #else
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 #endif
43         public struct FontUnit {
44                 FontSize type;
45                 Unit unit;
46                 
47                 public static readonly FontUnit Empty;
48                 public static readonly FontUnit Smaller = new FontUnit (FontSize.Smaller);
49                 public static readonly FontUnit Larger = new FontUnit (FontSize.Larger);
50                 public static readonly FontUnit XXSmall = new FontUnit (FontSize.XXSmall);
51                 public static readonly FontUnit XSmall = new FontUnit (FontSize.XSmall);
52                 public static readonly FontUnit Small = new FontUnit (FontSize.Small);
53                 public static readonly FontUnit Medium = new FontUnit (FontSize.Medium);
54                 public static readonly FontUnit Large = new FontUnit (FontSize.Large);
55                 public static readonly FontUnit XLarge = new FontUnit (FontSize.XLarge);
56                 public static readonly FontUnit XXLarge = new FontUnit (FontSize.XXLarge);
57
58                 static string [] font_size_names = new string [] {null, null, "Smaller", "Larger", "XX-Small", "X-Small", "Small",
59                                                                 "Medium", "Large", "X-Large", "XX-Large" };
60                 
61                 public FontUnit (FontSize type)
62                 {
63                         int t = (int) type;
64                         
65                         if (t < 0 || t > (int)FontSize.XXLarge)
66                                 throw new ArgumentOutOfRangeException ("type");
67                         
68                         this.type = type;
69
70                         if (type == FontSize.AsUnit)
71                                 unit = new Unit (10, UnitType.Point);
72                         else
73                                 unit = Unit.Empty;
74                 }
75
76                 public FontUnit (int value) : this (new Unit (value, UnitType.Point))
77                 {
78                 }
79
80 #if NET_2_0
81                 public FontUnit (double value) : this (new Unit (value, UnitType.Point))
82                 {
83                 }
84
85                 public FontUnit (double value, UnitType type) : this (new Unit (value, type))
86                 {
87                 }
88 #endif
89
90                 public FontUnit (Unit value)
91                 {
92                         type = FontSize.AsUnit;
93                         unit = value;
94                 }
95                 
96                 public FontUnit (string value) : this (value, Thread.CurrentThread.CurrentCulture) {}
97
98                 public FontUnit (string value, CultureInfo culture)
99                 {
100                         if (value == null || value == String.Empty){
101                                 type = FontSize.NotSet;
102                                 unit = Unit.Empty;
103                                 return;
104                         }
105
106                         switch (value.ToLower (CultureInfo.InvariantCulture)){
107                         case "smaller": type = FontSize.Smaller; break;
108                         case "larger": type = FontSize.Larger; break;
109                         case "xxsmall": type = FontSize.XXSmall; break;
110                         case "xx-small": type = FontSize.XXSmall; break;
111                         case "xsmall": type = FontSize.XSmall; break;
112                         case "x-small": type = FontSize.XSmall; break;
113                         case "small": type = FontSize.Small; break;
114                         case "medium": type = FontSize.Medium; break;
115                         case "large": type = FontSize.Large; break;
116                         case "xlarge": type = FontSize.XLarge; break;
117                         case "x-large": type = FontSize.XLarge; break;
118                         case "xxlarge": type = FontSize.XXLarge; break;
119                         case "xx-large": type = FontSize.XXLarge; break;
120                         default:
121                                 type = FontSize.AsUnit;
122                                 unit = new Unit (value, culture);
123                                 return;
124                         }
125                         unit = Unit.Empty;
126                 }
127                 
128                 public bool IsEmpty {
129                         get {
130                                 return type == FontSize.NotSet;
131                         }
132                 }
133
134                 public FontSize Type {
135                         get {
136                                 return type;
137                         }
138                 }
139
140                 public Unit Unit {
141                         get {
142                                 return unit;
143                         }
144                 }
145                 
146                 public static FontUnit Parse (string s)
147                 {
148                         return new FontUnit (s);
149                 }
150
151                 public static FontUnit Parse (string s, CultureInfo culture)
152                 {
153                         return new FontUnit (s, culture);
154                 }
155
156                 public static FontUnit Point (int n)
157                 {
158                         return new FontUnit (n);
159                 }
160                 public override bool Equals (object obj)
161                 {
162                         if (obj is FontUnit){
163                                 FontUnit other = (FontUnit) obj;
164                                 return (other.type == type && other.unit == unit);
165                         }
166                         return false;
167                 }
168                 
169                 public override int GetHashCode ()
170                 {
171                         return type.GetHashCode () ^ unit.GetHashCode ();
172                 }
173                 
174                 public static bool operator == (FontUnit left, FontUnit right)
175                 {
176                         return left.type == right.type && left.unit == right.unit;
177                 }
178
179                 public static bool operator != (FontUnit left, FontUnit right)
180                 {
181                         return left.type != right.type || left.unit != right.unit;
182                 }
183                 
184                 public static implicit operator FontUnit (int n)
185                 {
186                         return new FontUnit (n);
187                 }
188
189 #if NET_2_0
190                 public string ToString (IFormatProvider fmt)
191                 {
192                         if (type == FontSize.NotSet)
193                                 return "";
194                         else if (type == FontSize.AsUnit)
195                                 return unit.ToString (fmt);
196                         else
197                                 return font_size_names [(int) type];
198                 }
199 #endif
200
201                 public string ToString (CultureInfo culture)
202                 {
203                         if (type == FontSize.NotSet)
204                                 return "";
205
206                         if (type == FontSize.AsUnit)
207                                 return unit.ToString (culture);
208
209                         return font_size_names [(int) type];
210                 }
211                         
212                 public override string ToString ()
213                 {
214                         return ToString (CultureInfo.InvariantCulture);
215                 }
216                 
217         }
218 }
219