Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Unit.cs « System.Web.UI.WebControls « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 67c03bbd58de7eaf1c55ead26ea9252bf7637f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
 * Namespace: System.Web.UI.WebControls
 * Struct:    Unit
 *
 * Author:  Gaurav Vaish
 * Maintainer: gvaish@iitk.ac.in
 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
 * Implementation: yes
 * Status:  100%
 *
 * (C) Gaurav Vaish (2001)
 */

using System;
using System.Globalization;
using System.ComponentModel;
using System.Web;
using System.Web.UI;

namespace System.Web.UI.WebControls
{
	public struct Unit
	{
		public static readonly Unit Empty = new Unit();

		private static int Min = -32768;
		private static int Max = +32767;

		private UnitType type;
		private double   val;

		public static Unit Parse(string s)
		{
			return new Unit(s);
		}

		public static Unit Parse(string s, CultureInfo culture)
		{
			return new Unit(s, culture);
		}

		public static Unit Percentage(double n)
		{
			return new Unit(n);
		}

		public static Unit Pixel(int n)
		{
			return new Unit(n);
		}

		public static Unit Point(int n)
		{
			return new Unit(n, UnitType.Point);
		}

		public static bool operator ==(Unit left, Unit right)
		{
			return (left.type == right.type && left.val == right.val);
		}

		public static bool operator !=(Unit left, Unit right)
		{
			return !(left == right);
		}

		public static implicit operator Unit(int n)
		{
			return new Unit(n);
		}

		public Unit(double value)
		{
			if(value < Min || value > Max)
			{
				throw new ArgumentOutOfRangeException();
			}
			val = value;
			type = UnitType.Pixel;
		}

		public Unit(int value)
		{
			if(value < Min || value > Max)
			{
				throw new ArgumentOutOfRangeException();
			}
			val = value;
			type = UnitType.Pixel;
		}

		public Unit(string value): this(value, CultureInfo.CurrentCulture)
		{
		}

		public Unit(double value, UnitType type)
		{
			if(value < Min || value > Max)
			{
				throw new ArgumentOutOfRangeException();
			}
			val = value;
			this.type = type;
		}

		public Unit(string value, CultureInfo culture): this(value, culture, UnitType.Pixel)
		{
		}

		internal Unit(string value, CultureInfo culture, UnitType defType)
		{
			this.val = 0;
			this.type = UnitType.Pixel;
			if(value == null || value.Length == 0)
			{
				this.val = 0;
				this.type = UnitType.Pixel;
			}
			if(culture == null)
				culture = CultureInfo.CurrentCulture;
			string strVal = value.Trim().ToLower();
			char c;
			int start = -1;
			//int current = 0;
			for(int i = 0; i < strVal.Length; i++)
			{
				c = strVal[i];
				if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )
					start = i;
			}
			if(start == -1)
				throw new ArgumentException();
			if( (start + 1) < strVal.Length)
			{
				this.type = (UnitType)GetTypeFromString(strVal.Substring(start + 1).Trim());
				this.val  = 0;
			} else
			{
				this.type = defType;
				this.val  = 0;
			}
			try
			{
				if(type == UnitType.Pixel)
					val = (double) Int32.Parse (strVal.Substring (0, start + 1), culture);
				else
					val = (double) Single.Parse (strVal.Substring (0, start + 1), culture);
			} catch(Exception)
			{
				throw new ArgumentOutOfRangeException();
			}
			if(val < Min || val > Max)
				throw new ArgumentOutOfRangeException();
		}

		private UnitType GetTypeFromString(string s)
		{
			if(s == null || s.Length == 0)
				return UnitType.Pixel;
			s = s.ToLower().Trim();
			string[] uTypes = {
				"px",
				"pt",
				"pc",
				"in",
				"mm",
				"cm",
				"%",
				"em",
				"ex"
			};
			int i = 0;
			foreach(string cType in uTypes)
			{
				if(s == cType)
					return (UnitType)Enum.ToObject(typeof(UnitType), (i + 1));
				i++;
			}
			return UnitType.Pixel;
		}

		private string GetStringFromPixel(UnitType ut)
		{
			string[] uTypes = {
				"px",
				"pt",
				"pc",
				"in",
				"mm",
				"cm",
				"%",
				"em",
				"ex"
			};
			if( !Enum.IsDefined(typeof(UnitType), ut) )
				return "px";
			return uTypes[(int)ut - 1];
		}

		public bool IsEmpty
		{
			get
			{
				return (type == 0);
			}
		}

		public UnitType Type
		{
			get
			{
				if(IsEmpty)
					return UnitType.Pixel;
				return type;
			}
		}

		public double Value
		{
			get
			{
				return val;
			}
		}

		public override bool Equals(object obj)
		{
			if(obj != null && obj is Unit)
			{
				Unit that = (Unit)obj;
				return ( this.type == that.type && this.val == that.val );
			}
			return false;
		}

		public override int GetHashCode()
		{
			return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
		}

		public override string ToString()
		{
			if(IsEmpty)
				return String.Empty;
			return ( val.ToString() + GetStringFromPixel(type) );
		}

		public string ToString(CultureInfo culture)
		{
			if(IsEmpty)
				return String.Empty;
			return ( val.ToString(culture) + GetStringFromPixel(type) );
		}
	}
}