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 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
188 GridLines g = GridLines;
190 case GridLines.Horizontal:
191 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
193 case GridLines.Vertical:
194 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
197 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
201 // note: avoid ToString on the enum
202 switch (HorizontalAlign) {
203 case HorizontalAlign.Left:
204 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Left");
206 case HorizontalAlign.Center:
207 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Center");
209 case HorizontalAlign.Right:
210 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Right");
212 case HorizontalAlign.Justify:
213 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Justify");
217 // border (=0) is always present (and base class doesn't seems to add it)
218 // but border is "promoted" to 1 if gridlines are present (with BorderWidth == 0)
219 if (g == GridLines.None) {
220 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
221 } else if (BorderWidth.IsEmpty) {
222 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
224 writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (CultureInfo.InvariantCulture));
227 string s = BackImageUrl;
230 s = owner.ResolveUrl (s);
232 s = String.Concat ("url(", s, ")");
234 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
238 private void Copy (string name, Styles s, Style source)
240 if ((source.styles & s) != 0) {
241 object o = source.ViewState [name];
243 ViewState [name] = o;
249 public override void CopyFrom (Style s)
251 // note: styles is copied in base
253 if ((s != null) && !s.IsEmpty) {
254 Copy ("BackImageUrl", Styles.BackImageUrl, s);
255 Copy ("CellPadding", Styles.CellPadding, s);
256 Copy ("CellSpacing", Styles.CellSpacing, s);
257 Copy ("GridLines", Styles.GridLines, s);
258 Copy ("HorizontalAlign", Styles.HorizontalAlign, s);
262 private void Merge (string name, Styles s, Style source)
264 if ((styles & s) == 0 && (source.styles & s) != 0) {
265 object o = source.ViewState [name];
267 ViewState [name] = o;
273 public override void MergeWith (Style s)
275 // if we're empty then it's like a copy
280 if ((s != null) && !s.IsEmpty) {
281 Merge ("BackImageUrl", Styles.BackImageUrl, s);
282 Merge ("CellPadding", Styles.CellPadding, s);
283 Merge ("CellSpacing", Styles.CellSpacing, s);
284 Merge ("GridLines", Styles.GridLines, s);
285 Merge ("HorizontalAlign", Styles.HorizontalAlign, s);
290 public override void Reset ()
292 if ((styles & Styles.BackImageUrl) != 0)
293 ViewState.Remove ("BackImageUrl");
294 if ((styles & Styles.CellPadding) != 0)
295 ViewState.Remove ("CellPadding");
296 if ((styles & Styles.CellSpacing) != 0)
297 ViewState.Remove ("CellSpacing");
298 if ((styles & Styles.GridLines) != 0)
299 ViewState.Remove ("GridLines");
300 if ((styles & Styles.HorizontalAlign) != 0)
301 ViewState.Remove ("HorizontalAlign");
302 // call base at the end because "styles" will reset there
306 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
308 if (attributes != null) {
309 string url = BackImageUrl;
310 if (url.Length > 0) {
311 if (urlResolver != null)
312 url = urlResolver.ResolveClientUrl (url);
313 attributes.Add (HtmlTextWriterStyle.BackgroundImage, url);
316 base.FillStyleAttributes (attributes, urlResolver);
320 internal override void LoadViewStateInternal()
322 if (viewstate["BackImageUrl"] != null) {
323 styles |= Styles.BackImageUrl;
325 if (viewstate["CellPadding"] != null) {
326 styles |= Styles.CellPadding;
328 if (viewstate["CellSpacing"] != null) {
329 styles |= Styles.CellSpacing;
331 if (viewstate["GridLines"] != null) {
332 styles |= Styles.GridLines;
334 if (viewstate["HorizontalAlign"] != null) {
335 styles |= Styles.HorizontalAlign;
338 base.LoadViewStateInternal();