Unit.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. this.val = 0;
  95. this.type = UnitType.Pixel;
  96. if(value == null || value.Length == 0)
  97. {
  98. this.val = 0;
  99. this.type = UnitType.Pixel;
  100. }
  101. if(culture == null)
  102. culture = CultureInfo.CurrentCulture;
  103. string strVal = value.Trim().ToLower();
  104. char c;
  105. int start = -1;
  106. //int current = 0;
  107. for(int i = 0; i < strVal.Length; i++)
  108. {
  109. c = strVal[i];
  110. if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )
  111. start = i;
  112. }
  113. if(start == -1)
  114. throw new ArgumentException();
  115. if( (start + 1) < strVal.Length)
  116. {
  117. this.type = (UnitType)GetTypeFromString(strVal.Substring(start + 1).Trim());
  118. this.val = 0;
  119. } else
  120. {
  121. this.type = defType;
  122. this.val = 0;
  123. }
  124. try
  125. {
  126. if(type == UnitType.Pixel)
  127. val = (double) Int32.Parse (strVal.Substring (0, start + 1), culture);
  128. else
  129. val = (double) Single.Parse (strVal.Substring (0, start + 1), culture);
  130. } catch(Exception)
  131. {
  132. throw new ArgumentOutOfRangeException();
  133. }
  134. if(val < Min || val > Max)
  135. throw new ArgumentOutOfRangeException();
  136. }
  137. private UnitType GetTypeFromString(string s)
  138. {
  139. if(s == null || s.Length == 0)
  140. return UnitType.Pixel;
  141. s = s.ToLower().Trim();
  142. string[] uTypes = {
  143. "px",
  144. "pt",
  145. "pc",
  146. "in",
  147. "mm",
  148. "cm",
  149. "%",
  150. "em",
  151. "ex"
  152. };
  153. int i = 0;
  154. foreach(string cType in uTypes)
  155. {
  156. if(s == cType)
  157. return (UnitType)Enum.ToObject(typeof(UnitType), (i + 1));
  158. i++;
  159. }
  160. return UnitType.Pixel;
  161. }
  162. private string GetStringFromPixel(UnitType ut)
  163. {
  164. string[] uTypes = {
  165. "px",
  166. "pt",
  167. "pc",
  168. "in",
  169. "mm",
  170. "cm",
  171. "%",
  172. "em",
  173. "ex"
  174. };
  175. if( !Enum.IsDefined(typeof(UnitType), ut) )
  176. return "px";
  177. return uTypes[(int)ut - 1];
  178. }
  179. public bool IsEmpty
  180. {
  181. get
  182. {
  183. return (type == 0);
  184. }
  185. }
  186. public UnitType Type
  187. {
  188. get
  189. {
  190. if(IsEmpty)
  191. return UnitType.Pixel;
  192. return type;
  193. }
  194. }
  195. public double Value
  196. {
  197. get
  198. {
  199. return val;
  200. }
  201. }
  202. public override bool Equals(object obj)
  203. {
  204. if(obj != null && obj is Unit)
  205. {
  206. Unit that = (Unit)obj;
  207. return ( this.type == that.type && this.val == that.val );
  208. }
  209. return false;
  210. }
  211. public override int GetHashCode()
  212. {
  213. return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
  214. }
  215. public override string ToString()
  216. {
  217. if(IsEmpty)
  218. return String.Empty;
  219. return ( val.ToString() + GetStringFromPixel(type) );
  220. }
  221. public string ToString(CultureInfo culture)
  222. {
  223. if(IsEmpty)
  224. return String.Empty;
  225. return ( val.ToString(culture) + GetStringFromPixel(type) );
  226. }
  227. }
  228. }