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;
34 namespace System.Web.UI.WebControls {
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 public class TableStyle : Style {
43 BackImageUrl = 0x00010000,
44 CellPadding = 0x00020000,
45 CellSpacing = 0x00040000,
46 GridLines = 0x00080000,
47 HorizontalAlign = 0x00100000,
54 public TableStyle (StateBag bag)
61 [NotifyParentProperty (true)]
67 [WebSysDescription ("")]
68 [WebCategory ("Appearance")]
69 public virtual string BackImageUrl {
71 if (!CheckBit ((int) TableStyles.BackImageUrl))
73 return (string) ViewState ["BackImageUrl"];
77 throw new ArgumentNullException ("BackImageUrl");
78 ViewState ["BackImageUrl"] = value;
79 SetBit ((int) TableStyles.BackImageUrl);
84 [NotifyParentProperty (true)]
89 [WebSysDescription ("")]
90 [WebCategory ("Appearance")]
91 public virtual int CellPadding {
93 if (!CheckBit ((int) TableStyles.CellPadding))
95 return (int) ViewState ["CellPadding"];
99 throw new ArgumentOutOfRangeException ("< -1");
100 ViewState ["CellPadding"] = value;
101 SetBit ((int) TableStyles.CellPadding);
106 [NotifyParentProperty (true)]
111 [WebSysDescription ("")]
112 [WebCategory ("Appearance")]
113 public virtual int CellSpacing {
115 if (!CheckBit ((int) TableStyles.CellSpacing))
117 return (int) ViewState ["CellSpacing"];
121 throw new ArgumentOutOfRangeException ("< -1");
122 ViewState ["CellSpacing"] = value;
123 SetBit ((int) TableStyles.CellSpacing);
127 // LAMESPEC: default is documented to be Both
129 [NotifyParentProperty (true)]
133 [DefaultValue (GridLines.None)]
134 [WebSysDescription ("")]
135 [WebCategory ("Appearance")]
136 public virtual GridLines GridLines {
138 if (!CheckBit ((int) TableStyles.GridLines))
139 return GridLines.None;
140 return (GridLines) ViewState ["GridLines"];
144 if ((value < GridLines.None) || (value > GridLines.Both)) {
145 // LAMESPEC: documented as ArgumentException
146 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid GridLines value."));
148 ViewState ["GridLines"] = value;
149 SetBit ((int) TableStyles.GridLines);
154 [NotifyParentProperty (true)]
158 [DefaultValue (HorizontalAlign.NotSet)]
159 [WebSysDescription ("")]
160 [WebCategory ("Layout")]
161 public virtual HorizontalAlign HorizontalAlign {
163 if (!CheckBit ((int) TableStyles.HorizontalAlign))
164 return HorizontalAlign.NotSet;
165 return (HorizontalAlign) ViewState ["HorizontalAlign"];
169 if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
170 // LAMESPEC: documented as ArgumentException
171 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
173 ViewState ["HorizontalAlign"] = value;
174 SetBit ((int) TableStyles.HorizontalAlign);
179 public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
181 base.AddAttributesToRender (writer, owner);
185 // note: avoid calling properties multiple times
188 writer.AddAttribute (HtmlTextWriterAttribute.Cellpadding, i.ToString (CultureInfo.InvariantCulture), false);
192 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, i.ToString (CultureInfo.InvariantCulture), false);
194 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
198 GridLines g = GridLines;
200 case GridLines.Horizontal:
201 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows", false);
203 case GridLines.Vertical:
204 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols", false);
207 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all", false);
211 // note: avoid ToString on the enum
212 switch (HorizontalAlign) {
213 case HorizontalAlign.Left:
214 writer.AddAttribute (HtmlTextWriterAttribute.Align, "left", false);
216 case HorizontalAlign.Center:
217 writer.AddAttribute (HtmlTextWriterAttribute.Align, "center", false);
219 case HorizontalAlign.Right:
220 writer.AddAttribute (HtmlTextWriterAttribute.Align, "right", false);
222 case HorizontalAlign.Justify:
223 writer.AddAttribute (HtmlTextWriterAttribute.Align, "justify", false);
227 // border (=0) is always present (and base class doesn't seems to add it)
228 // but border is "promoted" to 1 if gridlines are present (with BorderWidth == 0)
229 if (g == GridLines.None) {
230 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0", false);
231 } else if (BorderWidth.IsEmpty) {
232 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1", false);
234 writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (CultureInfo.InvariantCulture));
238 string s = BackImageUrl;
241 s = owner.ResolveClientUrl (s);
242 s = String.Concat ("url(", s, ")");
243 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
248 private void Copy (string name, TableStyles s, Style source)
250 if (source.CheckBit ((int) s)) {
251 object o = source.ViewState [name];
253 ViewState [name] = o;
259 public override void CopyFrom (Style s)
261 // note: styles is copied in base
263 if ((s != null) && !s.IsEmpty) {
264 Copy ("BackImageUrl", TableStyles.BackImageUrl, s);
265 Copy ("CellPadding", TableStyles.CellPadding, s);
266 Copy ("CellSpacing", TableStyles.CellSpacing, s);
267 Copy ("GridLines", TableStyles.GridLines, s);
268 Copy ("HorizontalAlign", TableStyles.HorizontalAlign, s);
272 private void Merge (string name, TableStyles s, Style source)
274 if ((!CheckBit ((int) s)) && (source.CheckBit ((int) s))) {
275 object o = source.ViewState [name];
277 ViewState [name] = o;
283 public override void MergeWith (Style s)
285 // if we're empty then it's like a copy
290 if ((s != null) && !s.IsEmpty) {
291 Merge ("BackImageUrl", TableStyles.BackImageUrl, s);
292 Merge ("CellPadding", TableStyles.CellPadding, s);
293 Merge ("CellSpacing", TableStyles.CellSpacing, s);
294 Merge ("GridLines", TableStyles.GridLines, s);
295 Merge ("HorizontalAlign", TableStyles.HorizontalAlign, s);
300 public override void Reset ()
302 if (CheckBit ((int) TableStyles.BackImageUrl))
303 ViewState.Remove ("BackImageUrl");
304 if (CheckBit ((int) TableStyles.CellPadding))
305 ViewState.Remove ("CellPadding");
306 if (CheckBit ((int) TableStyles.CellSpacing))
307 ViewState.Remove ("CellSpacing");
308 if (CheckBit ((int) TableStyles.GridLines))
309 ViewState.Remove ("GridLines");
310 if (CheckBit ((int) TableStyles.HorizontalAlign))
311 ViewState.Remove ("HorizontalAlign");
312 // call base at the end because "styles" will reset there
316 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
318 if (attributes != null) {
319 string url = BackImageUrl;
320 if (url.Length > 0) {
321 if (urlResolver != null)
322 url = urlResolver.ResolveClientUrl (url);
323 attributes.Add (HtmlTextWriterStyle.BackgroundImage, url);
326 base.FillStyleAttributes (attributes, urlResolver);