CellPadding.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.ComponentModel;
  32. using System.ComponentModel.Design.Serialization;
  33. using System.Globalization;
  34. using System.Reflection;
  35. using System.Runtime.InteropServices;
  36. namespace XPTable.Models
  37. {
  38. #region CellPadding
  39. /// <summary>
  40. /// Specifies the amount of space between the border and any contained
  41. /// items along each edge of an object
  42. /// </summary>
  43. [Serializable(),
  44. StructLayout(LayoutKind.Sequential),
  45. TypeConverter(typeof(CellPaddingConverter))]
  46. public struct CellPadding
  47. {
  48. #region Class Data
  49. /// <summary>
  50. /// Represents a Padding structure with its properties
  51. /// left uninitialized
  52. /// </summary>
  53. public static readonly CellPadding Empty = new CellPadding(0, 0, 0, 0);
  54. /// <summary>
  55. /// The width of the left padding
  56. /// </summary>
  57. private int left;
  58. /// <summary>
  59. /// The width of the right padding
  60. /// </summary>
  61. private int right;
  62. /// <summary>
  63. /// The width of the top padding
  64. /// </summary>
  65. private int top;
  66. /// <summary>
  67. /// The width of the bottom padding
  68. /// </summary>
  69. private int bottom;
  70. #endregion
  71. #region Constructor
  72. /// <summary>
  73. /// Initializes a new instance of the Padding class
  74. /// </summary>
  75. /// <param name="left">The width of the left padding value</param>
  76. /// <param name="top">The height of top padding value</param>
  77. /// <param name="right">The width of the right padding value</param>
  78. /// <param name="bottom">The height of bottom padding value</param>
  79. public CellPadding(int left, int top, int right, int bottom)
  80. {
  81. this.left = left;
  82. this.right = right;
  83. this.top = top;
  84. this.bottom = bottom;
  85. }
  86. #endregion
  87. #region Methods
  88. /// <summary>
  89. /// Tests whether obj is a CellPadding structure with the same values as
  90. /// this Padding structure
  91. /// </summary>
  92. /// <param name="obj">The Object to test</param>
  93. /// <returns>This method returns true if obj is a CellPadding structure
  94. /// and its Left, Top, Right, and Bottom properties are equal to
  95. /// the corresponding properties of this CellPadding structure;
  96. /// otherwise, false</returns>
  97. public override bool Equals(object obj)
  98. {
  99. if (!(obj is CellPadding))
  100. {
  101. return false;
  102. }
  103. CellPadding padding = (CellPadding) obj;
  104. if (((padding.Left == this.Left) && (padding.Top == this.Top)) && (padding.Right == this.Right))
  105. {
  106. return (padding.Bottom == this.Bottom);
  107. }
  108. return false;
  109. }
  110. /// <summary>
  111. /// Returns the hash code for this CellPadding structure
  112. /// </summary>
  113. /// <returns>An integer that represents the hashcode for this
  114. /// padding</returns>
  115. public override int GetHashCode()
  116. {
  117. return (((this.Left ^ ((this.Top << 13) | (this.Top >> 0x13))) ^ ((this.Right << 0x1a) | (this.Right >> 6))) ^ ((this.Bottom << 7) | (this.Bottom >> 0x19)));
  118. }
  119. #endregion
  120. #region Properties
  121. /// <summary>
  122. /// Gets or sets the width of the left padding value
  123. /// </summary>
  124. public int Left
  125. {
  126. get
  127. {
  128. return this.left;
  129. }
  130. set
  131. {
  132. if (value < 0)
  133. {
  134. throw new ArgumentException("Padding value cannot be negative");
  135. }
  136. this.left = value;
  137. }
  138. }
  139. /// <summary>
  140. /// Gets or sets the width of the right padding value
  141. /// </summary>
  142. public int Right
  143. {
  144. get
  145. {
  146. return this.right;
  147. }
  148. set
  149. {
  150. if (value < 0)
  151. {
  152. throw new ArgumentException("Padding value cannot be negative");
  153. }
  154. this.right = value;
  155. }
  156. }
  157. /// <summary>
  158. /// Gets or sets the height of the top padding value
  159. /// </summary>
  160. public int Top
  161. {
  162. get
  163. {
  164. return this.top;
  165. }
  166. set
  167. {
  168. if (value < 0)
  169. {
  170. throw new ArgumentException("Padding value cannot be negative");
  171. }
  172. this.top = value;
  173. }
  174. }
  175. /// <summary>
  176. /// Gets or sets the height of the bottom padding value
  177. /// </summary>
  178. public int Bottom
  179. {
  180. get
  181. {
  182. return this.bottom;
  183. }
  184. set
  185. {
  186. if (value < 0)
  187. {
  188. throw new ArgumentException("Padding value cannot be negative");
  189. }
  190. this.bottom = value;
  191. }
  192. }
  193. /// <summary>
  194. /// Tests whether all numeric properties of this CellPadding have
  195. /// values of zero
  196. /// </summary>
  197. [Browsable(false)]
  198. public bool IsEmpty
  199. {
  200. get
  201. {
  202. if (((this.Left == 0) && (this.Top == 0)) && (this.Right == 0))
  203. {
  204. return (this.Bottom == 0);
  205. }
  206. return false;
  207. }
  208. }
  209. #endregion
  210. #region Operators
  211. /// <summary>
  212. /// Tests whether two CellPadding structures have equal Left, Top,
  213. /// Right, and Bottom properties
  214. /// </summary>
  215. /// <param name="left">The CellPadding structure that is to the left
  216. /// of the equality operator</param>
  217. /// <param name="right">The CellPadding structure that is to the right
  218. /// of the equality operator</param>
  219. /// <returns>This operator returns true if the two CellPadding structures
  220. /// have equal Left, Top, Right, and Bottom properties</returns>
  221. public static bool operator ==(CellPadding left, CellPadding right)
  222. {
  223. if (((left.Left == right.Left) && (left.Top == right.Top)) && (left.Right == right.Right))
  224. {
  225. return (left.Bottom == right.Bottom);
  226. }
  227. return false;
  228. }
  229. /// <summary>
  230. /// Tests whether two CellPadding structures differ in their Left, Top,
  231. /// Right, and Bottom properties
  232. /// </summary>
  233. /// <param name="left">The CellPadding structure that is to the left
  234. /// of the equality operator</param>
  235. /// <param name="right">The CellPadding structure that is to the right
  236. /// of the equality operator</param>
  237. /// <returns>This operator returns true if any of the Left, Top, Right,
  238. /// and Bottom properties of the two CellPadding structures are unequal;
  239. /// otherwise false</returns>
  240. public static bool operator !=(CellPadding left, CellPadding right)
  241. {
  242. return !(left == right);
  243. }
  244. #endregion
  245. }
  246. #endregion
  247. #region CellPaddingConverter
  248. /// <summary>
  249. /// A custom TypeConverter used to help convert CellPadding objects from
  250. /// one Type to another
  251. /// </summary>
  252. internal class CellPaddingConverter : TypeConverter
  253. {
  254. /// <summary>
  255. /// Returns whether this converter can convert an object of the
  256. /// given type to the type of this converter, using the specified context
  257. /// </summary>
  258. /// <param name="context">An ITypeDescriptorContext that provides
  259. /// a format context</param>
  260. /// <param name="sourceType">A Type that represents the type you
  261. /// want to convert from</param>
  262. /// <returns>true if this converter can perform the conversion;
  263. /// otherwise, false</returns>
  264. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  265. {
  266. if (sourceType == typeof(string))
  267. {
  268. return true;
  269. }
  270. return base.CanConvertFrom(context, sourceType);
  271. }
  272. /// <summary>
  273. /// Returns whether this converter can convert the object to the
  274. /// specified type, using the specified context
  275. /// </summary>
  276. /// <param name="context">An ITypeDescriptorContext that provides a
  277. /// format context</param>
  278. /// <param name="destinationType">A Type that represents the type you
  279. /// want to convert to</param>
  280. /// <returns>true if this converter can perform the conversion;
  281. /// otherwise, false</returns>
  282. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  283. {
  284. if (destinationType == typeof(InstanceDescriptor))
  285. {
  286. return true;
  287. }
  288. return base.CanConvertTo(context, destinationType);
  289. }
  290. /// <summary>
  291. /// Converts the given object to the type of this converter, using
  292. /// the specified context and culture information
  293. /// </summary>
  294. /// <param name="context">An ITypeDescriptorContext that provides a
  295. /// format context</param>
  296. /// <param name="culture">The CultureInfo to use as the current culture</param>
  297. /// <param name="value">The Object to convert</param>
  298. /// <returns>An Object that represents the converted value</returns>
  299. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  300. {
  301. if (value is string)
  302. {
  303. string text = ((string) value).Trim();
  304. if (text.Length == 0)
  305. {
  306. return null;
  307. }
  308. if (culture == null)
  309. {
  310. culture = CultureInfo.CurrentCulture;
  311. }
  312. char[] listSeparators = culture.TextInfo.ListSeparator.ToCharArray();
  313. string[] s = text.Split(listSeparators);
  314. if (s.Length < 4)
  315. {
  316. return null;
  317. }
  318. return new CellPadding(int.Parse(s[0]), int.Parse(s[1]), int.Parse(s[2]), int.Parse(s[3]));
  319. }
  320. return base.ConvertFrom(context, culture, value);
  321. }
  322. /// <summary>
  323. /// Converts the given value object to the specified type, using
  324. /// the specified context and culture information
  325. /// </summary>
  326. /// <param name="context">An ITypeDescriptorContext that provides
  327. /// a format context</param>
  328. /// <param name="culture">A CultureInfo object. If a null reference
  329. /// is passed, the current culture is assumed</param>
  330. /// <param name="value">The Object to convert</param>
  331. /// <param name="destinationType">The Type to convert the value
  332. /// parameter to</param>
  333. /// <returns>An Object that represents the converted value</returns>
  334. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  335. {
  336. if (destinationType == null)
  337. {
  338. throw new ArgumentNullException("destinationType");
  339. }
  340. if ((destinationType == typeof(string)) && (value is CellPadding))
  341. {
  342. CellPadding p = (CellPadding) value;
  343. if (culture == null)
  344. {
  345. culture = CultureInfo.CurrentCulture;
  346. }
  347. string separator = culture.TextInfo.ListSeparator + " ";
  348. TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
  349. string[] s = new string[4];
  350. s[0] = converter.ConvertToString(context, culture, p.Left);
  351. s[1] = converter.ConvertToString(context, culture, p.Top);
  352. s[2] = converter.ConvertToString(context, culture, p.Right);
  353. s[3] = converter.ConvertToString(context, culture, p.Bottom);
  354. return string.Join(separator, s);
  355. }
  356. if ((destinationType == typeof(InstanceDescriptor)) && (value is CellPadding))
  357. {
  358. CellPadding p = (CellPadding) value;
  359. Type[] t = new Type[4];
  360. t[0] = t[1] = t[2] = t[3] = typeof(int);
  361. ConstructorInfo info = typeof(CellPadding).GetConstructor(t);
  362. if (info != null)
  363. {
  364. object[] o = new object[4];
  365. o[0] = p.Left;
  366. o[1] = p.Top;
  367. o[2] = p.Right;
  368. o[3] = p.Bottom;
  369. return new InstanceDescriptor(info, o);
  370. }
  371. }
  372. return base.ConvertTo(context, culture, value, destinationType);
  373. }
  374. /// <summary>
  375. /// Creates an instance of the Type that this TypeConverter is associated
  376. /// with, using the specified context, given a set of property values for
  377. /// the object
  378. /// </summary>
  379. /// <param name="context">An ITypeDescriptorContext that provides a format
  380. /// context</param>
  381. /// <param name="propertyValues">An IDictionary of new property values</param>
  382. /// <returns>An Object representing the given IDictionary, or a null
  383. /// reference if the object cannot be created</returns>
  384. public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
  385. {
  386. return new CellPadding((int) propertyValues["Left"],
  387. (int) propertyValues["Top"],
  388. (int) propertyValues["Right"],
  389. (int) propertyValues["Bottom"]);
  390. }
  391. /// <summary>
  392. /// Returns whether changing a value on this object requires a call to
  393. /// CreateInstance to create a new value, using the specified context
  394. /// </summary>
  395. /// <param name="context">An ITypeDescriptorContext that provides a
  396. /// format context</param>
  397. /// <returns>true if changing a property on this object requires a call
  398. /// to CreateInstance to create a new value; otherwise, false</returns>
  399. public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
  400. {
  401. return true;
  402. }
  403. /// <summary>
  404. /// Returns a collection of properties for the type of array specified
  405. /// by the value parameter, using the specified context and attributes
  406. /// </summary>
  407. /// <param name="context">An ITypeDescriptorContext that provides a format
  408. /// context</param>
  409. /// <param name="value">An Object that specifies the type of array for
  410. /// which to get properties</param>
  411. /// <param name="attributes">An array of type Attribute that is used as
  412. /// a filter</param>
  413. /// <returns>A PropertyDescriptorCollection with the properties that are
  414. /// exposed for this data type, or a null reference if there are no
  415. /// properties</returns>
  416. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
  417. {
  418. PropertyDescriptorCollection collection = TypeDescriptor.GetProperties(typeof(CellPadding), attributes);
  419. string[] s = new string[4];
  420. s[0] = "Left";
  421. s[1] = "Top";
  422. s[2] = "Right";
  423. s[3] = "Bottom";
  424. return collection.Sort(s);
  425. }
  426. /// <summary>
  427. /// Returns whether this object supports properties, using the specified context
  428. /// </summary>
  429. /// <param name="context">An ITypeDescriptorContext that provides a format context</param>
  430. /// <returns>true if GetProperties should be called to find the properties of this
  431. /// object; otherwise, false</returns>
  432. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  433. {
  434. return true;
  435. }
  436. }
  437. #endregion
  438. }