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 {
44 public TableStyle (StateBag bag)
51 [NotifyParentProperty (true)]
57 [WebSysDescription ("")]
58 [WebCategory ("Appearance")]
59 public virtual string BackImageUrl {
61 if ((styles & Styles.BackImageUrl) == 0)
63 return (string) ViewState ["BackImageUrl"];
67 throw new ArgumentNullException ("BackImageUrl");
68 ViewState ["BackImageUrl"] = value;
69 styles |= Styles.BackImageUrl;
74 [NotifyParentProperty (true)]
79 [WebSysDescription ("")]
80 [WebCategory ("Appearance")]
81 public virtual int CellPadding {
83 if ((styles & Styles.CellPadding) == 0)
85 return (int) ViewState ["CellPadding"];
89 throw new ArgumentOutOfRangeException ("< -1");
90 ViewState ["CellPadding"] = value;
91 styles |= Styles.CellPadding;
96 [NotifyParentProperty (true)]
101 [WebSysDescription ("")]
102 [WebCategory ("Appearance")]
103 public virtual int CellSpacing {
105 if ((styles & Styles.CellSpacing) == 0)
107 return (int) ViewState ["CellSpacing"];
111 throw new ArgumentOutOfRangeException ("< -1");
112 ViewState ["CellSpacing"] = value;
113 styles |= Styles.CellSpacing;
117 // LAMESPEC: default is documented to be Both
119 [NotifyParentProperty (true)]
123 [DefaultValue (GridLines.None)]
124 [WebSysDescription ("")]
125 [WebCategory ("Appearance")]
126 public virtual GridLines GridLines {
128 if ((styles & Styles.GridLines) == 0)
129 return GridLines.None;
130 return (GridLines) ViewState ["GridLines"];
134 if ((value < GridLines.None) || (value > GridLines.Both)) {
135 // LAMESPEC: documented as ArgumentException
136 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid GridLines value."));
138 ViewState ["GridLines"] = value;
139 styles |= Styles.GridLines;
144 [NotifyParentProperty (true)]
148 [DefaultValue (HorizontalAlign.NotSet)]
149 [WebSysDescription ("")]
150 [WebCategory ("Layout")]
151 public virtual HorizontalAlign HorizontalAlign {
153 if ((styles & Styles.HorizontalAlign) == 0)
154 return HorizontalAlign.NotSet;
155 return (HorizontalAlign) ViewState ["HorizontalAlign"];
159 if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
160 // LAMESPEC: documented as ArgumentException
161 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
163 ViewState ["HorizontalAlign"] = value;
164 styles |= Styles.HorizontalAlign;
169 public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
171 base.AddAttributesToRender (writer, owner);
175 // note: avoid calling properties multiple times
178 writer.AddAttribute (HtmlTextWriterAttribute.Cellpadding, i.ToString (CultureInfo.InvariantCulture));
182 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, i.ToString (CultureInfo.InvariantCulture));
184 GridLines g = GridLines;
186 case GridLines.Horizontal:
187 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
189 case GridLines.Vertical:
190 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
193 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
197 // note: avoid ToString on the enum
198 switch (HorizontalAlign) {
199 case HorizontalAlign.Left:
200 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Left");
202 case HorizontalAlign.Center:
203 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Center");
205 case HorizontalAlign.Right:
206 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Right");
208 case HorizontalAlign.Justify:
209 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Justify");
213 // border (=0) is always present (and base class doesn't seems to add it)
214 // but border is "promoted" to 1 if gridlines are present (with BorderWidth == 0)
215 if (g == GridLines.None) {
216 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
217 } else if (BorderWidth.IsEmpty) {
218 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
220 writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (CultureInfo.InvariantCulture));
223 string s = BackImageUrl;
226 s = owner.ResolveUrl (s);
228 s = String.Concat ("url(", s, ")");
230 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
234 private void Copy (string name, Styles s, Style source)
236 if ((source.styles & s) != 0) {
237 object o = source.ViewState [name];
239 ViewState [name] = o;
245 public override void CopyFrom (Style s)
247 // note: styles is copied in base
249 if ((s != null) && !s.IsEmpty) {
250 Copy ("BackImageUrl", Styles.BackImageUrl, s);
251 Copy ("CellPadding", Styles.CellPadding, s);
252 Copy ("CellSpacing", Styles.CellSpacing, s);
253 Copy ("GridLines", Styles.GridLines, s);
254 Copy ("HorizontalAlign", Styles.HorizontalAlign, s);
258 private void Merge (string name, Styles s, Style source)
260 if ((styles & s) == 0 && (source.styles & s) != 0) {
261 object o = source.ViewState [name];
263 ViewState [name] = o;
269 public override void MergeWith (Style s)
271 // if we're empty then it's like a copy
276 if ((s != null) && !s.IsEmpty) {
277 Merge ("BackImageUrl", Styles.BackImageUrl, s);
278 Merge ("CellPadding", Styles.CellPadding, s);
279 Merge ("CellSpacing", Styles.CellSpacing, s);
280 Merge ("GridLines", Styles.GridLines, s);
281 Merge ("HorizontalAlign", Styles.HorizontalAlign, s);
286 public override void Reset ()
288 if ((styles & Styles.BackImageUrl) != 0)
289 ViewState.Remove ("BackImageUrl");
290 if ((styles & Styles.CellPadding) != 0)
291 ViewState.Remove ("CellPadding");
292 if ((styles & Styles.CellSpacing) != 0)
293 ViewState.Remove ("CellSpacing");
294 if ((styles & Styles.GridLines) != 0)
295 ViewState.Remove ("GridLines");
296 if ((styles & Styles.HorizontalAlign) != 0)
297 ViewState.Remove ("HorizontalAlign");
298 // call base at the end because "styles" will reset there
302 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
304 if (attributes != null) {
305 string url = BackImageUrl;
306 if (url.Length > 0) {
307 if (urlResolver != null)
308 url = urlResolver.ResolveClientUrl (url);
309 attributes.Add (HtmlTextWriterStyle.BackgroundImage, url);
312 base.FillStyleAttributes (attributes, urlResolver);
316 internal override void LoadViewStateInternal()
318 if (viewstate["BackImageUrl"] != null) {
319 styles |= Styles.BackImageUrl;
321 if (viewstate["CellPadding"] != null) {
322 styles |= Styles.CellPadding;
324 if (viewstate["CellSpacing"] != null) {
325 styles |= Styles.CellSpacing;
327 if (viewstate["GridLines"] != null) {
328 styles |= Styles.GridLines;
330 if (viewstate["HorizontalAlign"] != null) {
331 styles |= Styles.HorizontalAlign;
334 base.LoadViewStateInternal();