Unit.cs 4.9 KB

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