2
0

Unit.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Struct: Unit
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Globalization;
  15. using System.ComponentModel;
  16. using System.Web;
  17. using System.Web.UI;
  18. namespace System.Web.UI.WebControls
  19. {
  20. public struct Unit
  21. {
  22. public static readonly Unit Empty = new Unit();
  23. private static int Min = -32768;
  24. private static int Max = +32767;
  25. private UnitType type;
  26. private double val;
  27. public static Unit Parse(string s)
  28. {
  29. return new Unit(s);
  30. }
  31. public static Unit Parse(string s, CultureInfo culture)
  32. {
  33. return new Unit(s, culture);
  34. }
  35. public static Unit Percentage(double n)
  36. {
  37. return new Unit(n);
  38. }
  39. public static Unit Pixel(int n)
  40. {
  41. return new Unit(n);
  42. }
  43. public static Unit Point(int n)
  44. {
  45. return new Unit(n, UnitType.Point);
  46. }
  47. public static bool operator ==(Unit left, Unit right)
  48. {
  49. return (left.type == right.type && left.val == right.val);
  50. }
  51. public static bool operator !=(Unit left, Unit right)
  52. {
  53. return !(left == right);
  54. }
  55. public static implicit operator Unit(int n)
  56. {
  57. return new Unit(n);
  58. }
  59. public Unit(double value)
  60. {
  61. if(value < Min || value > Max)
  62. {
  63. throw new ArgumentOutOfRangeException();
  64. }
  65. val = value;
  66. type = UnitType.Pixel;
  67. }
  68. public Unit(int value)
  69. {
  70. if(value < Min || value > Max)
  71. {
  72. throw new ArgumentOutOfRangeException();
  73. }
  74. val = value;
  75. type = UnitType.Pixel;
  76. }
  77. public Unit(string value): this(value, CultureInfo.CurrentCulture)
  78. {
  79. }
  80. public Unit(double value, UnitType type)
  81. {
  82. if(value < Min || value > Max)
  83. {
  84. throw new ArgumentOutOfRangeException();
  85. }
  86. val = value;
  87. this.type = type;
  88. }
  89. public Unit(string value, CultureInfo culture): this(value, culture, UnitType.Pixel)
  90. {
  91. }
  92. internal Unit(string value, CultureInfo culture, UnitType defType)
  93. {
  94. string valueTrim;
  95. if (value == null || (valueTrim = value.Trim ()).Length == 0) {
  96. val = 0;
  97. type = UnitType.Pixel;
  98. return;
  99. }
  100. if (culture == null)
  101. culture = CultureInfo.CurrentCulture;
  102. string strVal = valueTrim.ToLower ();
  103. int length = strVal.Length;
  104. char c;
  105. int start = -1;
  106. for (int i = 0; i < length; i++) {
  107. c = strVal [i];
  108. if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )
  109. start = i;
  110. }
  111. if (start == -1)
  112. throw new ArgumentException("No digits in 'value'");
  113. start++;
  114. if (start < length) {
  115. type = GetTypeFromString (strVal.Substring (start).Trim ());
  116. val = 0;
  117. } else {
  118. type = defType;
  119. }
  120. try {
  121. string numbers = strVal.Substring (0, start);
  122. if (type == UnitType.Pixel)
  123. val = (double) Int32.Parse (numbers, culture);
  124. else
  125. val = (double) Single.Parse (numbers, culture);
  126. } catch (Exception) {
  127. throw new FormatException ("Error parsing " + value);
  128. }
  129. if (val < Min || val > Max)
  130. throw new ArgumentOutOfRangeException ();
  131. }
  132. private static UnitType GetTypeFromString(string s)
  133. {
  134. if(s == null || s.Length == 0)
  135. return UnitType.Pixel;
  136. s = s.ToLower().Trim();
  137. string[] uTypes = {
  138. "px",
  139. "pt",
  140. "pc",
  141. "in",
  142. "mm",
  143. "cm",
  144. "%",
  145. "em",
  146. "ex"
  147. };
  148. int i = 0;
  149. foreach(string cType in uTypes)
  150. {
  151. if(s == cType)
  152. return (UnitType)Enum.ToObject(typeof(UnitType), (i + 1));
  153. i++;
  154. }
  155. return UnitType.Pixel;
  156. }
  157. private string GetStringFromPixel(UnitType ut)
  158. {
  159. string[] uTypes = {
  160. "px",
  161. "pt",
  162. "pc",
  163. "in",
  164. "mm",
  165. "cm",
  166. "%",
  167. "em",
  168. "ex"
  169. };
  170. if( !Enum.IsDefined(typeof(UnitType), ut) )
  171. return "px";
  172. return uTypes[(int)ut - 1];
  173. }
  174. public bool IsEmpty
  175. {
  176. get
  177. {
  178. return (type == 0);
  179. }
  180. }
  181. public UnitType Type
  182. {
  183. get
  184. {
  185. if(IsEmpty)
  186. return UnitType.Pixel;
  187. return type;
  188. }
  189. }
  190. public double Value
  191. {
  192. get
  193. {
  194. return val;
  195. }
  196. }
  197. public override bool Equals(object obj)
  198. {
  199. if(obj != null && obj is Unit)
  200. {
  201. Unit that = (Unit)obj;
  202. return ( this.type == that.type && this.val == that.val );
  203. }
  204. return false;
  205. }
  206. public override int GetHashCode()
  207. {
  208. return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
  209. }
  210. public override string ToString()
  211. {
  212. if(IsEmpty)
  213. return String.Empty;
  214. return ( val.ToString() + GetStringFromPixel(type) );
  215. }
  216. public string ToString(CultureInfo culture)
  217. {
  218. if(IsEmpty)
  219. return String.Empty;
  220. return ( val.ToString(culture) + GetStringFromPixel(type) );
  221. }
  222. }
  223. }