Unit.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. {
  128. val = (double)((new Int32Converter()).ConvertFromString(null, culture, strVal.Substring(0, start + 1)));
  129. } else
  130. {
  131. val = (double)((new SingleConverter()).ConvertFromString(null, culture, strVal.Substring(0, start + 1)));
  132. }
  133. } catch(Exception)
  134. {
  135. throw new ArgumentOutOfRangeException();
  136. }
  137. if(val < Min || val > Max)
  138. throw new ArgumentOutOfRangeException();
  139. }
  140. private UnitType GetTypeFromString(string s)
  141. {
  142. if(s == null || s.Length == 0)
  143. return UnitType.Pixel;
  144. s = s.ToLower().Trim();
  145. string[] uTypes = {
  146. "px",
  147. "pt",
  148. "pc",
  149. "in",
  150. "mm",
  151. "cm",
  152. "%",
  153. "em",
  154. "ex"
  155. };
  156. int i = 0;
  157. foreach(string cType in uTypes)
  158. {
  159. if(s == cType)
  160. return (UnitType)Enum.ToObject(typeof(UnitType), (i + 1));
  161. i++;
  162. }
  163. return UnitType.Pixel;
  164. }
  165. private string GetStringFromPixel(UnitType ut)
  166. {
  167. string[] uTypes = {
  168. "px",
  169. "pt",
  170. "pc",
  171. "in",
  172. "mm",
  173. "cm",
  174. "%",
  175. "em",
  176. "ex"
  177. };
  178. if( !Enum.IsDefined(typeof(UnitType), ut) )
  179. return "px";
  180. return uTypes[(int)ut - 1];
  181. }
  182. public bool IsEmpty
  183. {
  184. get
  185. {
  186. return (type == 0);
  187. }
  188. }
  189. public UnitType Type
  190. {
  191. get
  192. {
  193. if(IsEmpty)
  194. return UnitType.Pixel;
  195. return type;
  196. }
  197. }
  198. public double Value
  199. {
  200. get
  201. {
  202. return val;
  203. }
  204. }
  205. public override bool Equals(object obj)
  206. {
  207. if(obj != null && obj is Unit)
  208. {
  209. Unit that = (Unit)obj;
  210. return ( this.type == that.type && this.val == that.val );
  211. }
  212. return false;
  213. }
  214. public override int GetHashCode()
  215. {
  216. return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
  217. }
  218. public override string ToString()
  219. {
  220. if(IsEmpty)
  221. return String.Empty;
  222. return ( val.ToString() + GetStringFromPixel(type) );
  223. }
  224. public string ToString(CultureInfo culture)
  225. {
  226. if(IsEmpty)
  227. return String.Empty;
  228. return ( val.ToString(culture) + GetStringFromPixel(type) );
  229. }
  230. }
  231. }