Unit.cs 4.8 KB

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