Unit.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.WebControls
  23. * Struct: Unit
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Contact: <[email protected]>, <[email protected]>
  28. * Implementation: yes
  29. * Status: 100%
  30. *
  31. * (C) Gaurav Vaish (2001)
  32. */
  33. using System;
  34. using System.Globalization;
  35. using System.ComponentModel;
  36. using System.Web;
  37. using System.Web.UI;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [TypeConverter(typeof(UnitConverter))]
  41. public struct Unit
  42. {
  43. public static readonly Unit Empty = new Unit();
  44. private static int Min = -32768;
  45. private static int Max = +32767;
  46. private UnitType type;
  47. private double val;
  48. public static Unit Parse(string s)
  49. {
  50. return new Unit(s);
  51. }
  52. public static Unit Parse(string s, CultureInfo culture)
  53. {
  54. return new Unit(s, culture);
  55. }
  56. public static Unit Percentage(double n)
  57. {
  58. return new Unit (n, UnitType.Percentage);
  59. }
  60. public static Unit Pixel(int n)
  61. {
  62. return new Unit (n, UnitType.Pixel);
  63. }
  64. public static Unit Point(int n)
  65. {
  66. return new Unit(n, UnitType.Point);
  67. }
  68. public static bool operator ==(Unit left, Unit right)
  69. {
  70. return (left.type == right.type && left.val == right.val);
  71. }
  72. public static bool operator !=(Unit left, Unit right)
  73. {
  74. return !(left == right);
  75. }
  76. public static implicit operator Unit(int n)
  77. {
  78. return new Unit(n);
  79. }
  80. public Unit(double value)
  81. {
  82. if(value < Min || value > Max)
  83. {
  84. throw new ArgumentOutOfRangeException();
  85. }
  86. val = value;
  87. type = UnitType.Pixel;
  88. }
  89. public Unit(int value)
  90. {
  91. if(value < Min || value > Max)
  92. {
  93. throw new ArgumentOutOfRangeException();
  94. }
  95. val = value;
  96. type = UnitType.Pixel;
  97. }
  98. public Unit(string value): this(value, CultureInfo.CurrentCulture)
  99. {
  100. }
  101. public Unit(double value, UnitType type)
  102. {
  103. if(value < Min || value > Max)
  104. {
  105. throw new ArgumentOutOfRangeException();
  106. }
  107. val = value;
  108. this.type = type;
  109. }
  110. public Unit(string value, CultureInfo culture): this(value, culture, UnitType.Pixel)
  111. {
  112. }
  113. internal Unit(string value, CultureInfo culture, UnitType defType)
  114. {
  115. string valueTrim;
  116. if (value == null || (valueTrim = value.Trim ()).Length == 0) {
  117. val = 0;
  118. type = UnitType.Pixel;
  119. return;
  120. }
  121. if (culture == null)
  122. culture = CultureInfo.CurrentCulture;
  123. string strVal = valueTrim.ToLower ();
  124. int length = strVal.Length;
  125. char c;
  126. int start = -1;
  127. for (int i = 0; i < length; i++) {
  128. c = strVal [i];
  129. if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )
  130. start = i;
  131. }
  132. if (start == -1)
  133. throw new ArgumentException("No digits in 'value'");
  134. start++;
  135. if (start < length) {
  136. type = GetTypeFromString (strVal.Substring (start).Trim ());
  137. val = 0;
  138. } else {
  139. type = defType;
  140. }
  141. try {
  142. string numbers = strVal.Substring (0, start);
  143. if (type == UnitType.Pixel)
  144. val = (double) Int32.Parse (numbers, culture);
  145. else
  146. val = (double) Single.Parse (numbers, culture);
  147. } catch (Exception) {
  148. throw new FormatException ("Error parsing " + value);
  149. }
  150. if (val < Min || val > Max)
  151. throw new ArgumentOutOfRangeException ();
  152. }
  153. private static UnitType GetTypeFromString(string s)
  154. {
  155. if(s == null || s.Length == 0)
  156. return UnitType.Pixel;
  157. s = s.ToLower().Trim();
  158. string[] uTypes = {
  159. "px",
  160. "pt",
  161. "pc",
  162. "in",
  163. "mm",
  164. "cm",
  165. "%",
  166. "em",
  167. "ex"
  168. };
  169. int i = 0;
  170. foreach(string cType in uTypes)
  171. {
  172. if(s == cType)
  173. return (UnitType)Enum.ToObject(typeof(UnitType), (i + 1));
  174. i++;
  175. }
  176. return UnitType.Pixel;
  177. }
  178. private string GetStringFromPixel(UnitType ut)
  179. {
  180. string[] uTypes = {
  181. "px",
  182. "pt",
  183. "pc",
  184. "in",
  185. "mm",
  186. "cm",
  187. "%",
  188. "em",
  189. "ex"
  190. };
  191. if( !Enum.IsDefined(typeof(UnitType), ut) )
  192. return "px";
  193. return uTypes[(int)ut - 1];
  194. }
  195. public bool IsEmpty
  196. {
  197. get
  198. {
  199. return (type == 0);
  200. }
  201. }
  202. public UnitType Type
  203. {
  204. get
  205. {
  206. if(IsEmpty)
  207. return UnitType.Pixel;
  208. return type;
  209. }
  210. }
  211. public double Value
  212. {
  213. get
  214. {
  215. return val;
  216. }
  217. }
  218. public override bool Equals(object obj)
  219. {
  220. if(obj != null && obj is Unit)
  221. {
  222. Unit that = (Unit)obj;
  223. return ( this.type == that.type && this.val == that.val );
  224. }
  225. return false;
  226. }
  227. public override int GetHashCode()
  228. {
  229. return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
  230. }
  231. public override string ToString()
  232. {
  233. if(IsEmpty)
  234. return String.Empty;
  235. return ( val.ToString() + GetStringFromPixel(type) );
  236. }
  237. public string ToString(CultureInfo culture)
  238. {
  239. if(IsEmpty)
  240. return String.Empty;
  241. return ( val.ToString(culture.NumberFormat) + GetStringFromPixel(type) );
  242. }
  243. }
  244. }