Renderer.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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.Drawing;
  33. using System.Windows.Forms;
  34. using XPTable.Models;
  35. using XPTable.Themes;
  36. namespace XPTable.Renderers
  37. {
  38. /// <summary>
  39. /// Base class for Renderers
  40. /// </summary>
  41. public abstract class Renderer : IRenderer, IDisposable
  42. {
  43. #region Class Data
  44. /// <summary>
  45. /// A StringFormat object that specifies how the Renderers
  46. /// contents are drawn
  47. /// </summary>
  48. private StringFormat stringFormat;
  49. /// <summary>
  50. /// The brush used to draw the Renderers background
  51. /// </summary>
  52. private SolidBrush backBrush;
  53. /// <summary>
  54. /// The brush used to draw the Renderers foreground
  55. /// </summary>
  56. private SolidBrush foreBrush;
  57. /// <summary>
  58. /// A Rectangle that specifies the size and location of the Renderer
  59. /// </summary>
  60. private Rectangle bounds;
  61. /// <summary>
  62. /// The Font of the text displayed by the Renderer
  63. /// </summary>
  64. private Font font;
  65. /// <summary>
  66. /// The width of a Cells border
  67. /// </summary>
  68. protected static int BorderWidth = 1;
  69. #endregion
  70. #region Constructor
  71. /// <summary>
  72. /// Initializes a new instance of the Renderer class with default settings
  73. /// </summary>
  74. protected Renderer()
  75. {
  76. this.bounds = Rectangle.Empty;
  77. this.font = null;
  78. this.stringFormat = new StringFormat();
  79. this.stringFormat.LineAlignment = StringAlignment.Center;
  80. this.stringFormat.Alignment = StringAlignment.Near;
  81. this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;
  82. this.stringFormat.Trimming = StringTrimming.EllipsisCharacter;
  83. this.backBrush = new SolidBrush(Color.Transparent);
  84. this.foreBrush = new SolidBrush(Color.Black);
  85. }
  86. #endregion
  87. #region Methods
  88. /// <summary>
  89. /// Releases the unmanaged resources used by the Renderer and
  90. /// optionally releases the managed resources
  91. /// </summary>
  92. public virtual void Dispose()
  93. {
  94. if (this.backBrush != null)
  95. {
  96. this.backBrush.Dispose();
  97. this.backBrush = null;
  98. }
  99. if (this.foreBrush != null)
  100. {
  101. this.foreBrush.Dispose();
  102. this.foreBrush = null;
  103. }
  104. }
  105. /// <summary>
  106. /// Sets the color of the brush used to draw the background
  107. /// </summary>
  108. /// <param name="color">The color of the brush</param>
  109. protected void SetBackBrushColor(Color color)
  110. {
  111. if (this.BackBrush.Color != color)
  112. {
  113. this.BackBrush.Color = color;
  114. }
  115. }
  116. /// <summary>
  117. /// Sets the color of the brush used to draw the foreground
  118. /// </summary>
  119. /// <param name="color">The color of the brush</param>
  120. protected void SetForeBrushColor(Color color)
  121. {
  122. if (this.ForeBrush.Color != color)
  123. {
  124. this.ForeBrush.Color = color;
  125. }
  126. }
  127. #endregion
  128. #region Properties
  129. /// <summary>
  130. /// Gets the rectangle that represents the client area of the Renderer
  131. /// </summary>
  132. [Browsable(false)]
  133. public abstract Rectangle ClientRectangle
  134. {
  135. get;
  136. }
  137. /// <summary>
  138. /// Gets or sets the size and location of the Renderer
  139. /// </summary>
  140. [Browsable(false)]
  141. public Rectangle Bounds
  142. {
  143. get
  144. {
  145. return this.bounds;
  146. }
  147. set
  148. {
  149. this.bounds = value;
  150. }
  151. }
  152. /// <summary>
  153. /// Gets or sets the font of the text displayed by the Renderer
  154. /// </summary>
  155. [Category("Appearance"),
  156. Description("The font used to draw the text")]
  157. public Font Font
  158. {
  159. get
  160. {
  161. return this.font;
  162. }
  163. set
  164. {
  165. if (value == null)
  166. {
  167. value = Control.DefaultFont;
  168. }
  169. if (this.font != value)
  170. {
  171. this.font = value;
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Gets the brush used to draw the Renderers background
  177. /// </summary>
  178. protected SolidBrush BackBrush
  179. {
  180. get
  181. {
  182. return this.backBrush;
  183. }
  184. }
  185. /// <summary>
  186. /// Gets the brush used to draw the Renderers foreground
  187. /// </summary>
  188. protected SolidBrush ForeBrush
  189. {
  190. get
  191. {
  192. return this.foreBrush;
  193. }
  194. }
  195. /// <summary>
  196. /// Gets or sets the foreground Color of the Renderer
  197. /// </summary>
  198. [Category("Appearance"),
  199. Description("The foreground color used to display text and graphics")]
  200. public Color ForeColor
  201. {
  202. get
  203. {
  204. return this.ForeBrush.Color;
  205. }
  206. set
  207. {
  208. this.SetForeBrushColor(value);
  209. }
  210. }
  211. /// <summary>
  212. /// Gets or sets the background Color of the Renderer
  213. /// </summary>
  214. [Category("Appearance"),
  215. Description("The background color used to display text and graphics")]
  216. public Color BackColor
  217. {
  218. get
  219. {
  220. return this.BackBrush.Color;
  221. }
  222. set
  223. {
  224. this.SetBackBrushColor(value);
  225. }
  226. }
  227. /// <summary>
  228. /// Gets or sets a StringFormat object that specifies how the Renderers
  229. /// contents are drawn
  230. /// </summary>
  231. protected StringFormat StringFormat
  232. {
  233. get
  234. {
  235. return this.stringFormat;
  236. }
  237. set
  238. {
  239. this.stringFormat = value;
  240. }
  241. }
  242. /// <summary>
  243. /// Gets or sets a StringTrimming enumeration that indicates how text that
  244. /// is drawn by the Renderer is trimmed when it exceeds the edges of the
  245. /// layout rectangle
  246. /// </summary>
  247. [Browsable(false)]
  248. public StringTrimming Trimming
  249. {
  250. get
  251. {
  252. return this.stringFormat.Trimming;
  253. }
  254. set
  255. {
  256. this.stringFormat.Trimming = value;
  257. }
  258. }
  259. /// <summary>
  260. /// Gets or sets how the Renderers contents are aligned horizontally
  261. /// </summary>
  262. [Browsable(false)]
  263. public ColumnAlignment Alignment
  264. {
  265. get
  266. {
  267. switch (this.stringFormat.Alignment)
  268. {
  269. case StringAlignment.Near:
  270. return ColumnAlignment.Left;
  271. case StringAlignment.Center:
  272. return ColumnAlignment.Center;
  273. case StringAlignment.Far:
  274. return ColumnAlignment.Right;
  275. }
  276. return ColumnAlignment.Left;
  277. }
  278. set
  279. {
  280. switch (value)
  281. {
  282. case ColumnAlignment.Left:
  283. this.stringFormat.Alignment = StringAlignment.Near;
  284. break;
  285. case ColumnAlignment.Center:
  286. this.stringFormat.Alignment = StringAlignment.Center;
  287. break;
  288. case ColumnAlignment.Right:
  289. this.stringFormat.Alignment = StringAlignment.Far;
  290. break;
  291. }
  292. }
  293. }
  294. /// <summary>
  295. /// Gets or sets how the Renderers contents are aligned vertically
  296. /// </summary>
  297. [Browsable(false)]
  298. public RowAlignment LineAlignment
  299. {
  300. get
  301. {
  302. switch (this.stringFormat.LineAlignment)
  303. {
  304. case StringAlignment.Near:
  305. return RowAlignment.Top;
  306. case StringAlignment.Center:
  307. return RowAlignment.Center;
  308. case StringAlignment.Far:
  309. return RowAlignment.Bottom;
  310. }
  311. return RowAlignment.Center;
  312. }
  313. set
  314. {
  315. switch (value)
  316. {
  317. case RowAlignment.Top:
  318. this.stringFormat.LineAlignment = StringAlignment.Near;
  319. break;
  320. case RowAlignment.Center:
  321. this.stringFormat.LineAlignment = StringAlignment.Center;
  322. break;
  323. case RowAlignment.Bottom:
  324. this.stringFormat.LineAlignment = StringAlignment.Far;
  325. break;
  326. }
  327. }
  328. }
  329. /// <summary>
  330. /// Gets whether Visual Styles are enabled for the application
  331. /// </summary>
  332. protected bool VisualStylesEnabled
  333. {
  334. get
  335. {
  336. return ThemeManager.VisualStylesEnabled;
  337. }
  338. }
  339. #endregion
  340. }
  341. }