TableStyle.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // System.Web.UI.WebControls.TableStyle.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.ComponentModel;
  29. using System.Globalization;
  30. using System.Security.Permissions;
  31. using System.Web.UI;
  32. using System.Web.Util;
  33. namespace System.Web.UI.WebControls {
  34. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. public class TableStyle : Style {
  37. [Flags]
  38. enum TableStyles
  39. {
  40. BackImageUrl = 0x00010000,
  41. CellPadding = 0x00020000,
  42. CellSpacing = 0x00040000,
  43. GridLines = 0x00080000,
  44. HorizontalAlign = 0x00100000,
  45. }
  46. public TableStyle ()
  47. {
  48. }
  49. public TableStyle (StateBag bag)
  50. : base (bag)
  51. {
  52. }
  53. #if NET_2_0
  54. [NotifyParentProperty (true)]
  55. [UrlProperty]
  56. #else
  57. [Bindable (true)]
  58. #endif
  59. [DefaultValue ("")]
  60. [WebSysDescription ("")]
  61. [WebCategory ("Appearance")]
  62. public virtual string BackImageUrl {
  63. get {
  64. if (!CheckBit ((int) TableStyles.BackImageUrl))
  65. return String.Empty;
  66. return (string) ViewState ["BackImageUrl"];
  67. }
  68. set {
  69. if (value == null)
  70. throw new ArgumentNullException ("BackImageUrl");
  71. ViewState ["BackImageUrl"] = value;
  72. SetBit ((int) TableStyles.BackImageUrl);
  73. }
  74. }
  75. #if NET_2_0
  76. [NotifyParentProperty (true)]
  77. #else
  78. [Bindable (true)]
  79. #endif
  80. [DefaultValue (-1)]
  81. [WebSysDescription ("")]
  82. [WebCategory ("Appearance")]
  83. public virtual int CellPadding {
  84. get {
  85. if (!CheckBit ((int) TableStyles.CellPadding))
  86. return -1;
  87. return (int) ViewState ["CellPadding"];
  88. }
  89. set {
  90. if (value < -1)
  91. throw new ArgumentOutOfRangeException ("< -1");
  92. ViewState ["CellPadding"] = value;
  93. SetBit ((int) TableStyles.CellPadding);
  94. }
  95. }
  96. #if NET_2_0
  97. [NotifyParentProperty (true)]
  98. #else
  99. [Bindable (true)]
  100. #endif
  101. [DefaultValue (-1)]
  102. [WebSysDescription ("")]
  103. [WebCategory ("Appearance")]
  104. public virtual int CellSpacing {
  105. get {
  106. if (!CheckBit ((int) TableStyles.CellSpacing))
  107. return -1;
  108. return (int) ViewState ["CellSpacing"];
  109. }
  110. set {
  111. if (value < -1)
  112. throw new ArgumentOutOfRangeException ("< -1");
  113. ViewState ["CellSpacing"] = value;
  114. SetBit ((int) TableStyles.CellSpacing);
  115. }
  116. }
  117. // LAMESPEC: default is documented to be Both
  118. #if NET_2_0
  119. [NotifyParentProperty (true)]
  120. #else
  121. [Bindable (true)]
  122. #endif
  123. [DefaultValue (GridLines.None)]
  124. [WebSysDescription ("")]
  125. [WebCategory ("Appearance")]
  126. public virtual GridLines GridLines {
  127. get {
  128. if (!CheckBit ((int) TableStyles.GridLines))
  129. return GridLines.None;
  130. return (GridLines) ViewState ["GridLines"];
  131. }
  132. set {
  133. // avoid reflection
  134. if ((value < GridLines.None) || (value > GridLines.Both)) {
  135. // LAMESPEC: documented as ArgumentException
  136. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid GridLines value."));
  137. }
  138. ViewState ["GridLines"] = value;
  139. SetBit ((int) TableStyles.GridLines);
  140. }
  141. }
  142. #if NET_2_0
  143. [NotifyParentProperty (true)]
  144. #else
  145. [Bindable (true)]
  146. #endif
  147. [DefaultValue (HorizontalAlign.NotSet)]
  148. [WebSysDescription ("")]
  149. [WebCategory ("Layout")]
  150. public virtual HorizontalAlign HorizontalAlign {
  151. get {
  152. if (!CheckBit ((int) TableStyles.HorizontalAlign))
  153. return HorizontalAlign.NotSet;
  154. return (HorizontalAlign) ViewState ["HorizontalAlign"];
  155. }
  156. set {
  157. // avoid reflection
  158. if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
  159. // LAMESPEC: documented as ArgumentException
  160. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
  161. }
  162. ViewState ["HorizontalAlign"] = value;
  163. SetBit ((int) TableStyles.HorizontalAlign);
  164. }
  165. }
  166. #if NET_4_0
  167. [MonoTODO ("collapse style should be rendered only for browsers which support that.")]
  168. #endif
  169. public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
  170. {
  171. base.AddAttributesToRender (writer, owner);
  172. if (writer == null)
  173. return;
  174. // note: avoid calling properties multiple times
  175. int i = CellPadding;
  176. if (i != -1)
  177. writer.AddAttribute (HtmlTextWriterAttribute.Cellpadding, i.ToString (Helpers.InvariantCulture), false);
  178. i = CellSpacing;
  179. if (i != -1) {
  180. writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, i.ToString (Helpers.InvariantCulture), false);
  181. if (i == 0)
  182. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
  183. }
  184. GridLines g = GridLines;
  185. switch (g) {
  186. case GridLines.Horizontal:
  187. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows", false);
  188. break;
  189. case GridLines.Vertical:
  190. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols", false);
  191. break;
  192. case GridLines.Both:
  193. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all", false);
  194. break;
  195. }
  196. // note: avoid ToString on the enum
  197. switch (HorizontalAlign) {
  198. case HorizontalAlign.Left:
  199. writer.AddAttribute (HtmlTextWriterAttribute.Align, "left", false);
  200. break;
  201. case HorizontalAlign.Center:
  202. writer.AddAttribute (HtmlTextWriterAttribute.Align, "center", false);
  203. break;
  204. case HorizontalAlign.Right:
  205. writer.AddAttribute (HtmlTextWriterAttribute.Align, "right", false);
  206. break;
  207. case HorizontalAlign.Justify:
  208. writer.AddAttribute (HtmlTextWriterAttribute.Align, "justify", false);
  209. break;
  210. }
  211. #if NET_4_0
  212. if (g != GridLines.None && BorderWidth.IsEmpty)
  213. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1", false);
  214. #else
  215. // border (=0) is always present (and base class doesn't seems to add it)
  216. // but border is "promoted" to 1 if gridlines are present (with BorderWidth == 0)
  217. if (g == GridLines.None) {
  218. writer.AddAttribute (HtmlTextWriterAttribute.Border, "0", false);
  219. } else if (BorderWidth.IsEmpty) {
  220. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1", false);
  221. } else {
  222. writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (Helpers.InvariantCulture));
  223. }
  224. #endif
  225. #if !NET_2_0
  226. string s = BackImageUrl;
  227. if (s.Length > 0) {
  228. if (owner != null)
  229. s = owner.ResolveClientUrl (s);
  230. s = String.Concat ("url(", s, ")");
  231. writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
  232. }
  233. #endif
  234. }
  235. void Copy (string name, TableStyles s, Style source)
  236. {
  237. if (source.CheckBit ((int) s)) {
  238. object o = source.ViewState [name];
  239. if (o != null) {
  240. ViewState [name] = o;
  241. SetBit ((int) s);
  242. }
  243. }
  244. }
  245. public override void CopyFrom (Style s)
  246. {
  247. // note: styles is copied in base
  248. base.CopyFrom (s);
  249. if ((s != null) && !s.IsEmpty) {
  250. Copy ("BackImageUrl", TableStyles.BackImageUrl, s);
  251. Copy ("CellPadding", TableStyles.CellPadding, s);
  252. Copy ("CellSpacing", TableStyles.CellSpacing, s);
  253. Copy ("GridLines", TableStyles.GridLines, s);
  254. Copy ("HorizontalAlign", TableStyles.HorizontalAlign, s);
  255. }
  256. }
  257. void Merge (string name, TableStyles s, Style source)
  258. {
  259. if ((!CheckBit ((int) s)) && (source.CheckBit ((int) s))) {
  260. object o = source.ViewState [name];
  261. if (o != null) {
  262. ViewState [name] = o;
  263. SetBit ((int) s);
  264. }
  265. }
  266. }
  267. public override void MergeWith (Style s)
  268. {
  269. // if we're empty then it's like a copy
  270. if (IsEmpty) {
  271. CopyFrom (s);
  272. } else {
  273. base.MergeWith (s);
  274. if ((s != null) && !s.IsEmpty) {
  275. Merge ("BackImageUrl", TableStyles.BackImageUrl, s);
  276. Merge ("CellPadding", TableStyles.CellPadding, s);
  277. Merge ("CellSpacing", TableStyles.CellSpacing, s);
  278. Merge ("GridLines", TableStyles.GridLines, s);
  279. Merge ("HorizontalAlign", TableStyles.HorizontalAlign, s);
  280. }
  281. }
  282. }
  283. public override void Reset ()
  284. {
  285. if (CheckBit ((int) TableStyles.BackImageUrl))
  286. ViewState.Remove ("BackImageUrl");
  287. if (CheckBit ((int) TableStyles.CellPadding))
  288. ViewState.Remove ("CellPadding");
  289. if (CheckBit ((int) TableStyles.CellSpacing))
  290. ViewState.Remove ("CellSpacing");
  291. if (CheckBit ((int) TableStyles.GridLines))
  292. ViewState.Remove ("GridLines");
  293. if (CheckBit ((int) TableStyles.HorizontalAlign))
  294. ViewState.Remove ("HorizontalAlign");
  295. // call base at the end because "styles" will reset there
  296. base.Reset ();
  297. }
  298. #if NET_2_0
  299. protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
  300. {
  301. if (attributes != null) {
  302. string url = BackImageUrl;
  303. if (url.Length > 0) {
  304. if (urlResolver != null)
  305. url = urlResolver.ResolveClientUrl (url);
  306. attributes.Add (HtmlTextWriterStyle.BackgroundImage, url);
  307. }
  308. }
  309. base.FillStyleAttributes (attributes, urlResolver);
  310. }
  311. #endif
  312. }
  313. }