TableStyle.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // System.Web.UI.WebControls.TableStyle.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.ComponentModel;
  33. using System.Globalization;
  34. using System.Web;
  35. using System.Web.UI;
  36. namespace System.Web.UI.WebControls
  37. {
  38. public class TableStyle : Style
  39. {
  40. private static int IMAGE_URL = (0x01 << 16);
  41. private static int CELL_PADD = (0x01 << 17);
  42. private static int CELL_SPAC = (0x01 << 18);
  43. private static int GRID_LINE = (0x01 << 19);
  44. private static int HOR_ALIGN = (0x01 << 20);
  45. public TableStyle(): base()
  46. {
  47. }
  48. public TableStyle(StateBag bag): base(bag)
  49. {
  50. }
  51. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  52. [WebSysDescription ("An Url specifying the background image for the table.")]
  53. public virtual string BackImageUrl
  54. {
  55. get
  56. {
  57. if(IsSet(IMAGE_URL))
  58. return (string)(ViewState["BackImageUrl"]);
  59. return String.Empty;
  60. }
  61. set
  62. {
  63. if(value == null)
  64. throw new ArgumentNullException("value");
  65. ViewState["BackImageUrl"] = value;
  66. Set(IMAGE_URL);
  67. }
  68. }
  69. [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
  70. [WebSysDescription ("The space left around the borders within a cell.")]
  71. public virtual int CellPadding
  72. {
  73. get
  74. {
  75. if(IsSet(CELL_PADD))
  76. return (int)(ViewState["CellPadding"]);
  77. return -1;
  78. }
  79. set
  80. {
  81. if(value < -1)
  82. throw new ArgumentOutOfRangeException("value", "CellPadding value has to be -1 for 'not set' or a value >= 0");
  83. ViewState["CellPadding"] = value;
  84. Set(CELL_PADD);
  85. }
  86. }
  87. [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
  88. [WebSysDescription ("The space left between cells.")]
  89. public virtual int CellSpacing
  90. {
  91. get
  92. {
  93. if(IsSet(CELL_SPAC))
  94. return (int)(ViewState["CellSpacing"]);
  95. return -1;
  96. }
  97. set
  98. {
  99. if(value < -1)
  100. throw new ArgumentOutOfRangeException("value"," CellSpacing value has to be -1 for 'not set' or a value >= 0");
  101. ViewState["CellSpacing"] = value;
  102. Set(CELL_SPAC);
  103. }
  104. }
  105. [DefaultValue (typeof (GridLines), "None"), Bindable (true), WebCategory ("Appearance")]
  106. [WebSysDescription ("The type of grid that a table uses.")]
  107. public virtual GridLines GridLines
  108. {
  109. get
  110. {
  111. if(IsSet(GRID_LINE))
  112. return (GridLines)(ViewState["GridLines"]);
  113. return GridLines.None;
  114. }
  115. set
  116. {
  117. if(!Enum.IsDefined(typeof(GridLines), value))
  118. throw new ArgumentOutOfRangeException("value"," Gridlines value has to be a valid enumeration member");
  119. ViewState["GridLines"] = value;
  120. Set(GRID_LINE);
  121. }
  122. }
  123. [DefaultValue (typeof (HorizontalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
  124. [WebSysDescription ("The horizonal alignment of the table.")]
  125. public virtual HorizontalAlign HorizontalAlign
  126. {
  127. get
  128. {
  129. if(IsSet(HOR_ALIGN))
  130. return (HorizontalAlign)(ViewState["HorizontalAlign"]);
  131. return HorizontalAlign.NotSet;
  132. }
  133. set
  134. {
  135. if(!Enum.IsDefined(typeof(HorizontalAlign), value))
  136. throw new ArgumentOutOfRangeException("value"," Gridlines value has to be a valid enumeration member");
  137. ViewState["HorizontalAlign"] = value;
  138. Set(HOR_ALIGN);
  139. }
  140. }
  141. public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
  142. {
  143. base.AddAttributesToRender(writer, owner);
  144. if(BackImageUrl.Length > 0)
  145. {
  146. writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "url(" + owner.ResolveUrl(BackImageUrl) + ")");
  147. }
  148. if(CellSpacing >= 0)
  149. {
  150. writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, CellSpacing.ToString(NumberFormatInfo.InvariantInfo));
  151. if(CellSpacing == 0)
  152. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
  153. }
  154. if(CellPadding >= 0)
  155. {
  156. writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, CellPadding.ToString(NumberFormatInfo.InvariantInfo));
  157. }
  158. if(HorizontalAlign != HorizontalAlign.NotSet)
  159. {
  160. writer.AddAttribute(HtmlTextWriterAttribute.Align, Enum.Format(typeof(HorizontalAlign), HorizontalAlign, "G"));
  161. }
  162. string gd = String.Empty;
  163. switch(GridLines)
  164. {
  165. case GridLines.None: break;
  166. case GridLines.Horizontal: gd = "rows";
  167. break;
  168. case GridLines.Vertical: gd = "cols";
  169. break;
  170. case GridLines.Both: gd = "all";
  171. break;
  172. }
  173. writer.AddAttribute(HtmlTextWriterAttribute.Rules, gd);
  174. }
  175. public override void CopyFrom(Style s)
  176. {
  177. if (s == null || s.IsEmpty)
  178. return;
  179. base.CopyFrom (s);
  180. TableStyle from = s as TableStyle;
  181. if (from == null)
  182. return;
  183. if (from.IsSet (HOR_ALIGN))
  184. HorizontalAlign = from.HorizontalAlign;
  185. if (from.IsSet (IMAGE_URL))
  186. BackImageUrl = from.BackImageUrl;
  187. if (from.IsSet (CELL_PADD))
  188. CellPadding = from.CellPadding;
  189. if (from.IsSet (CELL_SPAC))
  190. CellSpacing = from.CellSpacing;
  191. if (from.IsSet (GRID_LINE))
  192. GridLines = from.GridLines;
  193. }
  194. public override void MergeWith(Style s)
  195. {
  196. if(s != null && !s.IsEmpty)
  197. {
  198. if (IsEmpty) {
  199. CopyFrom (s);
  200. return;
  201. }
  202. base.MergeWith(s);
  203. if (!(s is TableStyle))
  204. return;
  205. TableStyle with = (TableStyle)s;
  206. if(with.IsSet(HOR_ALIGN) && !IsSet(HOR_ALIGN))
  207. {
  208. HorizontalAlign = with.HorizontalAlign;
  209. }
  210. if(with.IsSet(IMAGE_URL) && !IsSet(IMAGE_URL))
  211. {
  212. BackImageUrl = with.BackImageUrl;
  213. }
  214. if(with.IsSet(CELL_PADD) && !IsSet(CELL_PADD))
  215. {
  216. CellPadding = with.CellPadding;
  217. }
  218. if(with.IsSet(CELL_SPAC) && !IsSet(CELL_SPAC))
  219. {
  220. CellSpacing = with.CellSpacing;
  221. }
  222. if(with.IsSet(GRID_LINE) && !IsSet(GRID_LINE))
  223. {
  224. GridLines = with.GridLines;
  225. }
  226. }
  227. }
  228. public override void Reset()
  229. {
  230. if(IsSet(IMAGE_URL))
  231. ViewState.Remove("BackImageUrl");
  232. if(IsSet(HOR_ALIGN))
  233. ViewState.Remove("HorizontalAlign");
  234. if(IsSet(CELL_PADD))
  235. ViewState.Remove("CellPadding");
  236. if(IsSet(CELL_SPAC))
  237. ViewState.Remove("CellSpacing");
  238. if(IsSet(GRID_LINE))
  239. ViewState.Remove("GridLines");
  240. base.Reset();
  241. }
  242. }
  243. }