TableStyle.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. GridLines g = GridLines;
  169. switch (g) {
  170. case GridLines.Horizontal:
  171. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
  172. break;
  173. case GridLines.Vertical:
  174. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
  175. break;
  176. case GridLines.Both:
  177. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
  178. break;
  179. }
  180. // note: avoid ToString on the enum
  181. switch (HorizontalAlign) {
  182. case HorizontalAlign.Left:
  183. writer.AddAttribute (HtmlTextWriterAttribute.Align, "Left");
  184. break;
  185. case HorizontalAlign.Center:
  186. writer.AddAttribute (HtmlTextWriterAttribute.Align, "Center");
  187. break;
  188. case HorizontalAlign.Right:
  189. writer.AddAttribute (HtmlTextWriterAttribute.Align, "Right");
  190. break;
  191. case HorizontalAlign.Justify:
  192. writer.AddAttribute (HtmlTextWriterAttribute.Align, "Justify");
  193. break;
  194. }
  195. // border (=0) is always present (and base class doesn't seems to add it)
  196. // but border is "promoted" to 1 if gridlines are present (with BorderWidth == 0)
  197. if (g == GridLines.None) {
  198. writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
  199. } else if (BorderWidth.IsEmpty) {
  200. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
  201. } else {
  202. writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (CultureInfo.InvariantCulture));
  203. }
  204. string s = BackImageUrl;
  205. if (s.Length > 0) {
  206. if (owner != null)
  207. s = owner.ResolveUrl (s);
  208. #if ONLY_1_1
  209. s = String.Concat ("url(", s, ")");
  210. #endif
  211. writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
  212. }
  213. }
  214. private void Copy (string name, Styles s, Style source)
  215. {
  216. if ((source.styles & s) != 0) {
  217. object o = source.ViewState [name];
  218. if (o != null) {
  219. ViewState [name] = o;
  220. styles |= s;
  221. }
  222. }
  223. }
  224. public override void CopyFrom (Style s)
  225. {
  226. // note: styles is copied in base
  227. base.CopyFrom (s);
  228. if ((s != null) && !s.IsEmpty) {
  229. Copy ("BackImageUrl", Styles.BackImageUrl, s);
  230. Copy ("CellPadding", Styles.CellPadding, s);
  231. Copy ("CellSpacing", Styles.CellSpacing, s);
  232. Copy ("GridLines", Styles.GridLines, s);
  233. Copy ("HorizontalAlign", Styles.HorizontalAlign, s);
  234. }
  235. }
  236. private void Merge (string name, Styles s, Style source)
  237. {
  238. if ((styles & s) == 0 && (source.styles & s) != 0) {
  239. object o = source.ViewState [name];
  240. if (o != null) {
  241. ViewState [name] = o;
  242. styles |= s;
  243. }
  244. }
  245. }
  246. public override void MergeWith (Style s)
  247. {
  248. // if we're empty then it's like a copy
  249. if (IsEmpty) {
  250. CopyFrom (s);
  251. } else {
  252. base.MergeWith (s);
  253. if ((s != null) && !s.IsEmpty) {
  254. Merge ("BackImageUrl", Styles.BackImageUrl, s);
  255. Merge ("CellPadding", Styles.CellPadding, s);
  256. Merge ("CellSpacing", Styles.CellSpacing, s);
  257. Merge ("GridLines", Styles.GridLines, s);
  258. Merge ("HorizontalAlign", Styles.HorizontalAlign, s);
  259. }
  260. }
  261. }
  262. public override void Reset ()
  263. {
  264. if ((styles & Styles.BackImageUrl) != 0)
  265. ViewState.Remove ("BackImageUrl");
  266. if ((styles & Styles.CellPadding) != 0)
  267. ViewState.Remove ("CellPadding");
  268. if ((styles & Styles.CellSpacing) != 0)
  269. ViewState.Remove ("CellSpacing");
  270. if ((styles & Styles.GridLines) != 0)
  271. ViewState.Remove ("GridLines");
  272. if ((styles & Styles.HorizontalAlign) != 0)
  273. ViewState.Remove ("HorizontalAlign");
  274. // call base at the end because "styles" will reset there
  275. base.Reset ();
  276. }
  277. #if NET_2_0
  278. protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
  279. {
  280. if (attributes != null) {
  281. string url = BackImageUrl;
  282. if (url.Length > 0) {
  283. if (urlResolver != null)
  284. url = urlResolver.ResolveClientUrl (url);
  285. attributes.Add (HtmlTextWriterStyle.BackgroundImage, url);
  286. }
  287. }
  288. base.FillStyleAttributes (attributes, urlResolver);
  289. }
  290. #endif
  291. internal override void LoadViewStateInternal()
  292. {
  293. if (viewstate["BackImageUrl"] != null) {
  294. styles |= Styles.BackImageUrl;
  295. }
  296. if (viewstate["CellPadding"] != null) {
  297. styles |= Styles.CellPadding;
  298. }
  299. if (viewstate["CellSpacing"] != null) {
  300. styles |= Styles.CellSpacing;
  301. }
  302. if (viewstate["BackGridLines"] != null) {
  303. styles |= Styles.GridLines;
  304. }
  305. if (viewstate["HorizontalAlign"] != null) {
  306. styles |= Styles.HorizontalAlign;
  307. }
  308. base.LoadViewStateInternal();
  309. }
  310. }
  311. }