TableStyle.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. namespace System.Web.UI.WebControls {
  33. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. public class TableStyle : Style {
  36. public TableStyle ()
  37. {
  38. }
  39. public TableStyle (StateBag bag)
  40. : base (bag)
  41. {
  42. }
  43. #if NET_2_0
  44. [NotifyParentProperty (true)]
  45. [UrlProperty]
  46. #else
  47. [Bindable (true)]
  48. #endif
  49. [DefaultValue ("")]
  50. [WebSysDescription ("")]
  51. [WebCategory ("Appearance")]
  52. public virtual string BackImageUrl {
  53. get {
  54. if ((styles & Styles.BackImageUrl) == 0)
  55. return String.Empty;
  56. return (string) ViewState ["BackImageUrl"];
  57. }
  58. set {
  59. if (value == null)
  60. throw new ArgumentNullException ("BackImageUrl");
  61. ViewState ["BackImageUrl"] = value;
  62. styles |= Styles.BackImageUrl;
  63. }
  64. }
  65. #if NET_2_0
  66. [NotifyParentProperty (true)]
  67. #else
  68. [Bindable (true)]
  69. #endif
  70. [DefaultValue (-1)]
  71. [WebSysDescription ("")]
  72. [WebCategory ("Appearance")]
  73. public virtual int CellPadding {
  74. get {
  75. if ((styles & Styles.CellPadding) == 0)
  76. return -1;
  77. return (int) ViewState ["CellPadding"];
  78. }
  79. set {
  80. if (value < -1)
  81. throw new ArgumentOutOfRangeException ("< -1");
  82. ViewState ["CellPadding"] = value;
  83. styles |= Styles.CellPadding;
  84. }
  85. }
  86. #if NET_2_0
  87. [NotifyParentProperty (true)]
  88. #else
  89. [Bindable (true)]
  90. #endif
  91. [DefaultValue (-1)]
  92. [WebSysDescription ("")]
  93. [WebCategory ("Appearance")]
  94. public virtual int CellSpacing {
  95. get {
  96. if ((styles & Styles.CellSpacing) == 0)
  97. return -1;
  98. return (int) ViewState ["CellSpacing"];
  99. }
  100. set {
  101. if (value < -1)
  102. throw new ArgumentOutOfRangeException ("< -1");
  103. ViewState ["CellSpacing"] = value;
  104. styles |= Styles.CellSpacing;
  105. }
  106. }
  107. // LAMESPEC: default is documented to be Both
  108. #if NET_2_0
  109. [NotifyParentProperty (true)]
  110. #else
  111. [Bindable (true)]
  112. #endif
  113. [DefaultValue (GridLines.None)]
  114. [WebSysDescription ("")]
  115. [WebCategory ("Appearance")]
  116. public virtual GridLines GridLines {
  117. get {
  118. if ((styles & Styles.GridLines) == 0)
  119. return GridLines.None;
  120. return (GridLines) ViewState ["GridLines"];
  121. }
  122. set {
  123. // avoid reflection
  124. if ((value < GridLines.None) || (value > GridLines.Both)) {
  125. // LAMESPEC: documented as ArgumentException
  126. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid GridLines value."));
  127. }
  128. ViewState ["GridLines"] = value;
  129. styles |= Styles.GridLines;
  130. }
  131. }
  132. #if NET_2_0
  133. [NotifyParentProperty (true)]
  134. #else
  135. [Bindable (true)]
  136. #endif
  137. [DefaultValue (HorizontalAlign.NotSet)]
  138. [WebSysDescription ("")]
  139. [WebCategory ("Layout")]
  140. public virtual HorizontalAlign HorizontalAlign {
  141. get {
  142. if ((styles & Styles.HorizontalAlign) == 0)
  143. return HorizontalAlign.NotSet;
  144. return (HorizontalAlign) ViewState ["HorizontalAlign"];
  145. }
  146. set {
  147. // avoid reflection
  148. if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
  149. // LAMESPEC: documented as ArgumentException
  150. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
  151. }
  152. ViewState ["HorizontalAlign"] = value;
  153. styles |= Styles.HorizontalAlign;
  154. }
  155. }
  156. public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
  157. {
  158. base.AddAttributesToRender (writer, owner);
  159. if (writer == null)
  160. return;
  161. // note: avoid calling properties multiple times
  162. int i = CellPadding;
  163. if (i != -1)
  164. writer.AddAttribute (HtmlTextWriterAttribute.Cellpadding, i.ToString (CultureInfo.InvariantCulture));
  165. i = CellSpacing;
  166. if (i != -1) {
  167. writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, i.ToString (CultureInfo.InvariantCulture));
  168. if (i == 0) {
  169. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
  170. }
  171. }
  172. GridLines g = GridLines;
  173. switch (g) {
  174. case GridLines.Horizontal:
  175. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
  176. break;
  177. case GridLines.Vertical:
  178. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
  179. break;
  180. case GridLines.Both:
  181. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
  182. break;
  183. }
  184. // note: avoid ToString on the enum
  185. switch (HorizontalAlign) {
  186. case HorizontalAlign.Left:
  187. writer.AddAttribute (HtmlTextWriterAttribute.Align, "Left");
  188. break;
  189. case HorizontalAlign.Center:
  190. writer.AddAttribute (HtmlTextWriterAttribute.Align, "Center");
  191. break;
  192. case HorizontalAlign.Right:
  193. writer.AddAttribute (HtmlTextWriterAttribute.Align, "Right");
  194. break;
  195. case HorizontalAlign.Justify:
  196. writer.AddAttribute (HtmlTextWriterAttribute.Align, "Justify");
  197. break;
  198. }
  199. // border (=0) is always present (and base class doesn't seems to add it)
  200. // but border is "promoted" to 1 if gridlines are present (with BorderWidth == 0)
  201. if (g == GridLines.None) {
  202. writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
  203. } else if (BorderWidth.IsEmpty) {
  204. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
  205. } else {
  206. writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (CultureInfo.InvariantCulture));
  207. }
  208. #if !NET_2_0
  209. string s = BackImageUrl;
  210. if (s.Length > 0) {
  211. if (owner != null)
  212. s = owner.ResolveClientUrl (s);
  213. s = String.Concat ("url(", s, ")");
  214. writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
  215. }
  216. #endif
  217. }
  218. private void Copy (string name, Styles s, Style source)
  219. {
  220. if ((source.styles & s) != 0) {
  221. object o = source.ViewState [name];
  222. if (o != null) {
  223. ViewState [name] = o;
  224. styles |= s;
  225. }
  226. }
  227. }
  228. public override void CopyFrom (Style s)
  229. {
  230. // note: styles is copied in base
  231. base.CopyFrom (s);
  232. if ((s != null) && !s.IsEmpty) {
  233. Copy ("BackImageUrl", Styles.BackImageUrl, s);
  234. Copy ("CellPadding", Styles.CellPadding, s);
  235. Copy ("CellSpacing", Styles.CellSpacing, s);
  236. Copy ("GridLines", Styles.GridLines, s);
  237. Copy ("HorizontalAlign", Styles.HorizontalAlign, s);
  238. }
  239. }
  240. private void Merge (string name, Styles s, Style source)
  241. {
  242. if ((styles & s) == 0 && (source.styles & s) != 0) {
  243. object o = source.ViewState [name];
  244. if (o != null) {
  245. ViewState [name] = o;
  246. styles |= s;
  247. }
  248. }
  249. }
  250. public override void MergeWith (Style s)
  251. {
  252. // if we're empty then it's like a copy
  253. if (IsEmpty) {
  254. CopyFrom (s);
  255. } else {
  256. base.MergeWith (s);
  257. if ((s != null) && !s.IsEmpty) {
  258. Merge ("BackImageUrl", Styles.BackImageUrl, s);
  259. Merge ("CellPadding", Styles.CellPadding, s);
  260. Merge ("CellSpacing", Styles.CellSpacing, s);
  261. Merge ("GridLines", Styles.GridLines, s);
  262. Merge ("HorizontalAlign", Styles.HorizontalAlign, s);
  263. }
  264. }
  265. }
  266. public override void Reset ()
  267. {
  268. if ((styles & Styles.BackImageUrl) != 0)
  269. ViewState.Remove ("BackImageUrl");
  270. if ((styles & Styles.CellPadding) != 0)
  271. ViewState.Remove ("CellPadding");
  272. if ((styles & Styles.CellSpacing) != 0)
  273. ViewState.Remove ("CellSpacing");
  274. if ((styles & Styles.GridLines) != 0)
  275. ViewState.Remove ("GridLines");
  276. if ((styles & Styles.HorizontalAlign) != 0)
  277. ViewState.Remove ("HorizontalAlign");
  278. // call base at the end because "styles" will reset there
  279. base.Reset ();
  280. }
  281. #if NET_2_0
  282. protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
  283. {
  284. if (attributes != null) {
  285. string url = BackImageUrl;
  286. if (url.Length > 0) {
  287. if (urlResolver != null)
  288. url = urlResolver.ResolveClientUrl (url);
  289. attributes.Add (HtmlTextWriterStyle.BackgroundImage, url);
  290. }
  291. }
  292. base.FillStyleAttributes (attributes, urlResolver);
  293. }
  294. #endif
  295. internal override void LoadViewStateInternal()
  296. {
  297. if (viewstate["BackImageUrl"] != null) {
  298. styles |= Styles.BackImageUrl;
  299. }
  300. if (viewstate["CellPadding"] != null) {
  301. styles |= Styles.CellPadding;
  302. }
  303. if (viewstate["CellSpacing"] != null) {
  304. styles |= Styles.CellSpacing;
  305. }
  306. if (viewstate["GridLines"] != null) {
  307. styles |= Styles.GridLines;
  308. }
  309. if (viewstate["HorizontalAlign"] != null) {
  310. styles |= Styles.HorizontalAlign;
  311. }
  312. base.LoadViewStateInternal();
  313. }
  314. }
  315. }