TableStyle.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: TableStyle
  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 (2002)
  12. */
  13. using System;
  14. using System.Web;
  15. using System.Web.UI;
  16. namespace System.Web.UI.WebControls
  17. {
  18. public class TableStyle : Style
  19. {
  20. private static int IMAGE_URL = (0x01 << 16);
  21. private static int CELL_PADD = (0x01 << 17);
  22. private static int CELL_SPAC = (0x01 << 18);
  23. private static int GRID_LINE = (0x01 << 19);
  24. private static int HOR_ALIGN = (0x01 << 20);
  25. public TableStyle(): base()
  26. {
  27. }
  28. public TableStyle(StateBag bag): base(bag)
  29. {
  30. }
  31. public virtual string BackImageUrl
  32. {
  33. get
  34. {
  35. if(IsSet(IMAGE_URL))
  36. return (string)(ViewState["BackImageUrl"]);
  37. return String.Empty;
  38. }
  39. set
  40. {
  41. if(value == null)
  42. throw new ArgumentNullException("BackImageUrl");
  43. ViewState["BackImageUrl"] = value;
  44. Set(IMAGE_URL);
  45. }
  46. }
  47. public virtual int CellPadding
  48. {
  49. get
  50. {
  51. if(IsSet(CELL_PADD))
  52. return (int)(ViewState["CellPadding"]);
  53. return -1;
  54. }
  55. set
  56. {
  57. if(value < -1)
  58. throw new ArgumentOutOfRangeException("CellPadding");
  59. ViewState["CellPadding"] = value;
  60. Set(CELL_PADD);
  61. }
  62. }
  63. public virtual int CellSpacing
  64. {
  65. get
  66. {
  67. if(IsSet(CELL_SPAC))
  68. return (int)(ViewState["CellSpacing"]);
  69. return -1;
  70. }
  71. set
  72. {
  73. if(value < -1)
  74. throw new ArgumentOutOfRangeException("CellSpacing");
  75. ViewState["CellSpacing"] = value;
  76. Set(CELL_SPAC);
  77. }
  78. }
  79. public virtual GridLines GridLines
  80. {
  81. get
  82. {
  83. if(IsSet(GRID_LINE))
  84. return (string)(ViewState["GridLines"]);
  85. return GridLines.Both;
  86. }
  87. set
  88. {
  89. if(!Enum.IsDefined(typeof(GridLines), value))
  90. throw new ArgumentException();
  91. ViewState["GridLines"] = value;
  92. Set(GRID_LINE);
  93. }
  94. }
  95. public virtual HorizontalAlign HorizontalAlign
  96. {
  97. get
  98. {
  99. if(IsSet(HOR_ALIGN))
  100. return (string)(ViewState["HorizontalAlign"]);
  101. return HorizontalAlign.NotSet;
  102. }
  103. set
  104. {
  105. if(!Enum.IsDefined(typeof(HorizontalAlign), value))
  106. throw new ArgumentException();
  107. ViewState["HorizontalAlign"] = value;
  108. Set(HOR_ALIGN);
  109. }
  110. }
  111. public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
  112. {
  113. base.AddAttributesToRender(writer, owner);
  114. if(BackImageUrl.Length > 0)
  115. {
  116. writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "url(" + ResolveUrl(BackImageUrl) + ")");
  117. }
  118. if(CellSpacing >= 0)
  119. {
  120. writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, CellSpacing.ToString(NumerFormatInfo.InvariantInfo));
  121. }
  122. if(CellPadding >= 0)
  123. {
  124. writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, CellPadding.ToString(NumerFormatInfo.InvariantInfo));
  125. }
  126. if(HorizontalAlign != HorizontalAlign.NotSet)
  127. {
  128. writer.AddAttribute(HtmlTextWriterAttribute.Align, Enum.Format(typeof(HorizontalAlign), HorizontalAlign, "G"));
  129. }
  130. string gd = "";
  131. switch(GridLines)
  132. {
  133. case GridLines.None: gd = "";
  134. break;
  135. case GridLines.Horizontal: gd = "cols";
  136. break;
  137. case GridLines.Vertical: gd = "rows";
  138. break;
  139. case GridLines.Both: gd = "all";
  140. break;
  141. }
  142. writer.AddAttribute(HtmlTextWriterAttribute.Rules, gd);
  143. }
  144. private static int IMAGE_URL = (0x01 << 16);
  145. private static int CELL_PADD = (0x01 << 17);
  146. private static int CELL_SPAC = (0x01 << 18);
  147. private static int GRID_LINE = (0x01 << 19);
  148. private static int HOR_ALIGN = (0x01 << 20);
  149. public override void CopyFrom(Style s)
  150. {
  151. if(s != null && s is TableStyle && !s.IsEmpty)
  152. {
  153. base.CopyFrom(s);
  154. TableStyle from = (TableStyle)s;
  155. if(from.IsSet(HOR_ALIGN))
  156. {
  157. HorizontalAlign = from.HorizontalAlign;
  158. }
  159. if(from.IsSet(IMAGE_URL))
  160. {
  161. BackImageUrl = from.BackImageUrl;
  162. }
  163. if(from.IsSet(CELL_PADD))
  164. {
  165. CellPadding = from.CellPadding;
  166. }
  167. if(from.IsSet(CELL_SPAC))
  168. {
  169. CellSpacing = from.CellSpacing;
  170. }
  171. if(from.IsSet(GRID_LINE))
  172. {
  173. GridLines = from.GridLines;
  174. }
  175. }
  176. }
  177. public override void MergeWith(Style s)
  178. {
  179. if(s != null && s is TableStyle && !s.IsEmpty)
  180. {
  181. base.MergeWith(s);
  182. TableStyle with = (TableStyle)s;
  183. if(from.IsSet(HOR_ALIGN) && IsSet(HOR_ALIGN))
  184. {
  185. HorizontalAlign = with.HorizontalAlign;
  186. }
  187. if(from.IsSet(IMAGE_URL) && IsSet(IMAGE_URL))
  188. {
  189. BackImageUrl = with.BackImageUrl;
  190. }
  191. if(from.IsSet(CELL_PADD) && IsSet(CELLL_PADD))
  192. {
  193. CellPadding = with.CellPadding;
  194. }
  195. if(from.IsSet(CELL_SPAC) && IsSet(CELL_SPAC))
  196. {
  197. CellSpacing = with.CellSpacing;
  198. }
  199. if(from.IsSet(GRID_LINE) && IsSet(GRID_LINE))
  200. {
  201. GridLines = with.GridLines;
  202. }
  203. }
  204. }
  205. public override void Reset()
  206. {
  207. if(IsSet(IMAGE_URL))
  208. ViewState.Remove("BackImageUrl");
  209. if(IsSet(HOR_ALIGN))
  210. ViewState.Remove("HorizontalAlign");
  211. if(IsSet(CELL_PADD))
  212. ViewState.Remove("CellPadding");
  213. if(IsSet(CELL_SPAC))
  214. ViewState.Remove("CellSpacing");
  215. if(IsSet(GRID_LINE))
  216. ViewState.Remove("GridLines");
  217. base.Reset();
  218. }
  219. }
  220. }