TableStyle.cs 5.3 KB

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