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);
180 public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
182 base.AddAttributesToRender (writer, owner);
186 // note: avoid calling properties multiple times
189 writer.AddAttribute (HtmlTextWriterAttribute.Cellpadding, i.ToString (Helpers.InvariantCulture), false);
193 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, i.ToString (Helpers.InvariantCulture), false);
195 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
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 // border (=0) is always present (and base class doesn't seems to add it)
229 // but border is "promoted" to 1 if gridlines are present (with BorderWidth == 0)
230 if (g == GridLines.None) {
231 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0", false);
232 } else if (BorderWidth.IsEmpty) {
233 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1", false);
235 writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (Helpers.InvariantCulture));
239 string s = BackImageUrl;
242 s = owner.ResolveClientUrl (s);
243 s = String.Concat ("url(", s, ")");
244 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
249 void Copy (string name, TableStyles s, Style source)
251 if (source.CheckBit ((int) s)) {
252 object o = source.ViewState [name];
254 ViewState [name] = o;
260 public override void CopyFrom (Style s)
262 // note: styles is copied in base
264 if ((s != null) && !s.IsEmpty) {
265 Copy ("BackImageUrl", TableStyles.BackImageUrl, s);
266 Copy ("CellPadding", TableStyles.CellPadding, s);
267 Copy ("CellSpacing", TableStyles.CellSpacing, s);
268 Copy ("GridLines", TableStyles.GridLines, s);
269 Copy ("HorizontalAlign", TableStyles.HorizontalAlign, s);
273 void Merge (string name, TableStyles s, Style source)
275 if ((!CheckBit ((int) s)) && (source.CheckBit ((int) s))) {
276 object o = source.ViewState [name];
278 ViewState [name] = o;
284 public override void MergeWith (Style s)
286 // if we're empty then it's like a copy
291 if ((s != null) && !s.IsEmpty) {
292 Merge ("BackImageUrl", TableStyles.BackImageUrl, s);
293 Merge ("CellPadding", TableStyles.CellPadding, s);
294 Merge ("CellSpacing", TableStyles.CellSpacing, s);
295 Merge ("GridLines", TableStyles.GridLines, s);
296 Merge ("HorizontalAlign", TableStyles.HorizontalAlign, s);
301 public override void Reset ()
303 if (CheckBit ((int) TableStyles.BackImageUrl))
304 ViewState.Remove ("BackImageUrl");
305 if (CheckBit ((int) TableStyles.CellPadding))
306 ViewState.Remove ("CellPadding");
307 if (CheckBit ((int) TableStyles.CellSpacing))
308 ViewState.Remove ("CellSpacing");
309 if (CheckBit ((int) TableStyles.GridLines))
310 ViewState.Remove ("GridLines");
311 if (CheckBit ((int) TableStyles.HorizontalAlign))
312 ViewState.Remove ("HorizontalAlign");
313 // call base at the end because "styles" will reset there
317 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
319 if (attributes != null) {
320 string url = BackImageUrl;
321 if (url.Length > 0) {
322 if (urlResolver != null)
323 url = urlResolver.ResolveClientUrl (url);
324 attributes.Add (HtmlTextWriterStyle.BackgroundImage, url);
327 base.FillStyleAttributes (attributes, urlResolver);