2 // System.Web.UI.WebControls.TableStyle.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.Security.Permissions;
33 using System.Web.Util;
35 namespace System.Web.UI.WebControls {
37 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 public class TableStyle : Style {
44 BackImageUrl = 0x00010000,
45 CellPadding = 0x00020000,
46 CellSpacing = 0x00040000,
47 GridLines = 0x00080000,
48 HorizontalAlign = 0x00100000,
55 public TableStyle (StateBag bag)
62 [NotifyParentProperty (true)]
68 [WebSysDescription ("")]
69 [WebCategory ("Appearance")]
70 public virtual string BackImageUrl {
72 if (!CheckBit ((int) TableStyles.BackImageUrl))
74 return (string) ViewState ["BackImageUrl"];
78 throw new ArgumentNullException ("BackImageUrl");
79 ViewState ["BackImageUrl"] = value;
80 SetBit ((int) TableStyles.BackImageUrl);
85 [NotifyParentProperty (true)]
90 [WebSysDescription ("")]
91 [WebCategory ("Appearance")]
92 public virtual int CellPadding {
94 if (!CheckBit ((int) TableStyles.CellPadding))
96 return (int) ViewState ["CellPadding"];
100 throw new ArgumentOutOfRangeException ("< -1");
101 ViewState ["CellPadding"] = value;
102 SetBit ((int) TableStyles.CellPadding);
107 [NotifyParentProperty (true)]
112 [WebSysDescription ("")]
113 [WebCategory ("Appearance")]
114 public virtual int CellSpacing {
116 if (!CheckBit ((int) TableStyles.CellSpacing))
118 return (int) ViewState ["CellSpacing"];
122 throw new ArgumentOutOfRangeException ("< -1");
123 ViewState ["CellSpacing"] = value;
124 SetBit ((int) TableStyles.CellSpacing);
128 // LAMESPEC: default is documented to be Both
130 [NotifyParentProperty (true)]
134 [DefaultValue (GridLines.None)]
135 [WebSysDescription ("")]
136 [WebCategory ("Appearance")]
137 public virtual GridLines GridLines {
139 if (!CheckBit ((int) TableStyles.GridLines))
140 return GridLines.None;
141 return (GridLines) ViewState ["GridLines"];
145 if ((value < GridLines.None) || (value > GridLines.Both)) {
146 // LAMESPEC: documented as ArgumentException
147 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid GridLines value."));
149 ViewState ["GridLines"] = value;
150 SetBit ((int) TableStyles.GridLines);
155 [NotifyParentProperty (true)]
159 [DefaultValue (HorizontalAlign.NotSet)]
160 [WebSysDescription ("")]
161 [WebCategory ("Layout")]
162 public virtual HorizontalAlign HorizontalAlign {
164 if (!CheckBit ((int) TableStyles.HorizontalAlign))
165 return HorizontalAlign.NotSet;
166 return (HorizontalAlign) ViewState ["HorizontalAlign"];
170 if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
171 // LAMESPEC: documented as ArgumentException
172 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
174 ViewState ["HorizontalAlign"] = value;
175 SetBit ((int) TableStyles.HorizontalAlign);
179 [MonoTODO ("collapse style should be rendered only for browsers which support that.")]
181 public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
183 base.AddAttributesToRender (writer, owner);
187 // note: avoid calling properties multiple times
190 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, i.ToString (Helpers.InvariantCulture), false);
192 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
197 writer.AddAttribute (HtmlTextWriterAttribute.Cellpadding, i.ToString (Helpers.InvariantCulture), false);
199 GridLines g = GridLines;
201 case GridLines.Horizontal:
202 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows", false);
204 case GridLines.Vertical:
205 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols", false);
208 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all", false);
212 // note: avoid ToString on the enum
213 switch (HorizontalAlign) {
214 case HorizontalAlign.Left:
215 writer.AddAttribute (HtmlTextWriterAttribute.Align, "left", false);
217 case HorizontalAlign.Center:
218 writer.AddAttribute (HtmlTextWriterAttribute.Align, "center", false);
220 case HorizontalAlign.Right:
221 writer.AddAttribute (HtmlTextWriterAttribute.Align, "right", false);
223 case HorizontalAlign.Justify:
224 writer.AddAttribute (HtmlTextWriterAttribute.Align, "justify", false);
228 if (g != GridLines.None && BorderWidth.IsEmpty)
229 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1", false);
231 // border (=0) is always present (and base class doesn't seems to add it)
232 // but border is "promoted" to 1 if gridlines are present (with BorderWidth == 0)
233 if (g == GridLines.None) {
234 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0", false);
235 } else if (BorderWidth.IsEmpty) {
236 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1", false);
238 writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (Helpers.InvariantCulture));
242 string s = BackImageUrl;
245 s = owner.ResolveClientUrl (s);
246 s = String.Concat ("url(", s, ")");
247 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
252 void Copy (string name, TableStyles s, Style source)
254 if (source.CheckBit ((int) s)) {
255 object o = source.ViewState [name];
257 ViewState [name] = o;
263 public override void CopyFrom (Style s)
265 // note: styles is copied in base
267 if ((s != null) && !s.IsEmpty) {
268 Copy ("BackImageUrl", TableStyles.BackImageUrl, s);
269 Copy ("CellPadding", TableStyles.CellPadding, s);
270 Copy ("CellSpacing", TableStyles.CellSpacing, s);
271 Copy ("GridLines", TableStyles.GridLines, s);
272 Copy ("HorizontalAlign", TableStyles.HorizontalAlign, s);
276 void Merge (string name, TableStyles s, Style source)
278 if ((!CheckBit ((int) s)) && (source.CheckBit ((int) s))) {
279 object o = source.ViewState [name];
281 ViewState [name] = o;
287 public override void MergeWith (Style s)
289 // if we're empty then it's like a copy
294 if ((s != null) && !s.IsEmpty) {
295 Merge ("BackImageUrl", TableStyles.BackImageUrl, s);
296 Merge ("CellPadding", TableStyles.CellPadding, s);
297 Merge ("CellSpacing", TableStyles.CellSpacing, s);
298 Merge ("GridLines", TableStyles.GridLines, s);
299 Merge ("HorizontalAlign", TableStyles.HorizontalAlign, s);
304 public override void Reset ()
306 if (CheckBit ((int) TableStyles.BackImageUrl))
307 ViewState.Remove ("BackImageUrl");
308 if (CheckBit ((int) TableStyles.CellPadding))
309 ViewState.Remove ("CellPadding");
310 if (CheckBit ((int) TableStyles.CellSpacing))
311 ViewState.Remove ("CellSpacing");
312 if (CheckBit ((int) TableStyles.GridLines))
313 ViewState.Remove ("GridLines");
314 if (CheckBit ((int) TableStyles.HorizontalAlign))
315 ViewState.Remove ("HorizontalAlign");
316 // call base at the end because "styles" will reset there
320 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
322 if (attributes != null) {
323 string url = BackImageUrl;
324 if (url.Length > 0) {
325 if (urlResolver != null)
326 url = urlResolver.ResolveClientUrl (url);
327 attributes.Add (HtmlTextWriterStyle.BackgroundImage, url);
330 base.FillStyleAttributes (attributes, urlResolver);