TableStyle.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. using System;
  12. using System.ComponentModel;
  13. using System.Globalization;
  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. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  32. [WebSysDescription ("An Url specifying the background image for the table.")]
  33. public virtual string BackImageUrl
  34. {
  35. get
  36. {
  37. if(IsSet(IMAGE_URL))
  38. return (string)(ViewState["BackImageUrl"]);
  39. return String.Empty;
  40. }
  41. set
  42. {
  43. if(value == null)
  44. throw new ArgumentNullException("value");
  45. ViewState["BackImageUrl"] = value;
  46. Set(IMAGE_URL);
  47. }
  48. }
  49. [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
  50. [WebSysDescription ("The space left around the borders within a cell.")]
  51. public virtual int CellPadding
  52. {
  53. get
  54. {
  55. if(IsSet(CELL_PADD))
  56. return (int)(ViewState["CellPadding"]);
  57. return -1;
  58. }
  59. set
  60. {
  61. if(value < -1)
  62. throw new ArgumentOutOfRangeException("value", "CellPadding value has to be -1 for 'not set' or a value >= 0");
  63. ViewState["CellPadding"] = value;
  64. Set(CELL_PADD);
  65. }
  66. }
  67. [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
  68. [WebSysDescription ("The space left between cells.")]
  69. public virtual int CellSpacing
  70. {
  71. get
  72. {
  73. if(IsSet(CELL_SPAC))
  74. return (int)(ViewState["CellSpacing"]);
  75. return -1;
  76. }
  77. set
  78. {
  79. if(value < -1)
  80. throw new ArgumentOutOfRangeException("value"," CellSpacing value has to be -1 for 'not set' or a value >= 0");
  81. ViewState["CellSpacing"] = value;
  82. Set(CELL_SPAC);
  83. }
  84. }
  85. [DefaultValue (typeof (GridLines), "None"), Bindable (true), WebCategory ("Appearance")]
  86. [WebSysDescription ("The type of grid that a table uses.")]
  87. public virtual GridLines GridLines
  88. {
  89. get
  90. {
  91. if(IsSet(GRID_LINE))
  92. return (GridLines)(ViewState["GridLines"]);
  93. return GridLines.None;
  94. }
  95. set
  96. {
  97. if(!Enum.IsDefined(typeof(GridLines), value))
  98. throw new ArgumentOutOfRangeException("value"," Gridlines value has to be a valid enumeration member");
  99. ViewState["GridLines"] = value;
  100. Set(GRID_LINE);
  101. }
  102. }
  103. [DefaultValue (typeof (HorizontalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
  104. [WebSysDescription ("The horizonal alignment of the table.")]
  105. public virtual HorizontalAlign HorizontalAlign
  106. {
  107. get
  108. {
  109. if(IsSet(HOR_ALIGN))
  110. return (HorizontalAlign)(ViewState["HorizontalAlign"]);
  111. return HorizontalAlign.NotSet;
  112. }
  113. set
  114. {
  115. if(!Enum.IsDefined(typeof(HorizontalAlign), value))
  116. throw new ArgumentOutOfRangeException("value"," Gridlines value has to be a valid enumeration member");
  117. ViewState["HorizontalAlign"] = value;
  118. Set(HOR_ALIGN);
  119. }
  120. }
  121. public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
  122. {
  123. base.AddAttributesToRender(writer, owner);
  124. if(BackImageUrl.Length > 0)
  125. {
  126. writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "url(" + owner.ResolveUrl(BackImageUrl) + ")");
  127. }
  128. if(CellSpacing >= 0)
  129. {
  130. writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, CellSpacing.ToString(NumberFormatInfo.InvariantInfo));
  131. if(CellSpacing == 0)
  132. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
  133. }
  134. if(CellPadding >= 0)
  135. {
  136. writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, CellPadding.ToString(NumberFormatInfo.InvariantInfo));
  137. }
  138. if(HorizontalAlign != HorizontalAlign.NotSet)
  139. {
  140. writer.AddAttribute(HtmlTextWriterAttribute.Align, Enum.Format(typeof(HorizontalAlign), HorizontalAlign, "G"));
  141. }
  142. string gd = String.Empty;
  143. switch(GridLines)
  144. {
  145. case GridLines.None: break;
  146. case GridLines.Horizontal: gd = "rows";
  147. break;
  148. case GridLines.Vertical: gd = "cols";
  149. break;
  150. case GridLines.Both: gd = "all";
  151. break;
  152. }
  153. writer.AddAttribute(HtmlTextWriterAttribute.Rules, gd);
  154. }
  155. public override void CopyFrom(Style s)
  156. {
  157. if (s == null || s.IsEmpty)
  158. return;
  159. base.CopyFrom (s);
  160. TableStyle from = s as TableStyle;
  161. if (from == null)
  162. return;
  163. if (from.IsSet (HOR_ALIGN))
  164. HorizontalAlign = from.HorizontalAlign;
  165. if (from.IsSet (IMAGE_URL))
  166. BackImageUrl = from.BackImageUrl;
  167. if (from.IsSet (CELL_PADD))
  168. CellPadding = from.CellPadding;
  169. if (from.IsSet (CELL_SPAC))
  170. CellSpacing = from.CellSpacing;
  171. if (from.IsSet (GRID_LINE))
  172. GridLines = from.GridLines;
  173. }
  174. public override void MergeWith(Style s)
  175. {
  176. if(s != null && !s.IsEmpty)
  177. {
  178. if (IsEmpty) {
  179. CopyFrom (s);
  180. return;
  181. }
  182. base.MergeWith(s);
  183. if (!(s is TableStyle))
  184. return;
  185. TableStyle with = (TableStyle)s;
  186. if(with.IsSet(HOR_ALIGN) && !IsSet(HOR_ALIGN))
  187. {
  188. HorizontalAlign = with.HorizontalAlign;
  189. }
  190. if(with.IsSet(IMAGE_URL) && !IsSet(IMAGE_URL))
  191. {
  192. BackImageUrl = with.BackImageUrl;
  193. }
  194. if(with.IsSet(CELL_PADD) && !IsSet(CELL_PADD))
  195. {
  196. CellPadding = with.CellPadding;
  197. }
  198. if(with.IsSet(CELL_SPAC) && !IsSet(CELL_SPAC))
  199. {
  200. CellSpacing = with.CellSpacing;
  201. }
  202. if(with.IsSet(GRID_LINE) && !IsSet(GRID_LINE))
  203. {
  204. GridLines = with.GridLines;
  205. }
  206. }
  207. }
  208. public override void Reset()
  209. {
  210. if(IsSet(IMAGE_URL))
  211. ViewState.Remove("BackImageUrl");
  212. if(IsSet(HOR_ALIGN))
  213. ViewState.Remove("HorizontalAlign");
  214. if(IsSet(CELL_PADD))
  215. ViewState.Remove("CellPadding");
  216. if(IsSet(CELL_SPAC))
  217. ViewState.Remove("CellSpacing");
  218. if(IsSet(GRID_LINE))
  219. ViewState.Remove("GridLines");
  220. base.Reset();
  221. }
  222. }
  223. }