merge -r 58060:58217
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TableStyle.cs
1 //
2 // System.Web.UI.WebControls.TableStyle.cs
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.Security.Permissions;
32 using System.Web.UI;
33
34 namespace System.Web.UI.WebControls {
35
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         public class TableStyle : Style {
39
40                 public TableStyle ()
41                 {
42                 }
43
44                 public TableStyle (StateBag bag)
45                         : base (bag)
46                 {
47                 }
48
49
50 #if NET_2_0
51                 [NotifyParentProperty (true)]
52                 [UrlProperty]
53 #else
54                 [Bindable (true)]
55 #endif
56                 [DefaultValue ("")]
57                 [WebSysDescription ("")]
58                 [WebCategory ("Appearance")]
59                 public virtual string BackImageUrl {
60                         get {
61                                 if ((styles & Styles.BackImageUrl) == 0)
62                                         return String.Empty;
63                                 return (string) ViewState ["BackImageUrl"];
64                         }
65                         set {
66                                 if (value == null)
67                                         throw new ArgumentNullException ("BackImageUrl");
68                                 ViewState ["BackImageUrl"] = value;
69                                 styles |= Styles.BackImageUrl;
70                         }
71                 }
72
73 #if NET_2_0
74                 [NotifyParentProperty (true)]
75 #else
76                 [Bindable (true)]
77 #endif
78                 [DefaultValue (-1)]
79                 [WebSysDescription ("")]
80                 [WebCategory ("Appearance")]
81                 public virtual int CellPadding {
82                         get {
83                                 if ((styles & Styles.CellPadding) == 0)
84                                         return -1;
85                                 return (int) ViewState ["CellPadding"];
86                         }
87                         set {
88                                 if (value < -1)
89                                         throw new ArgumentOutOfRangeException ("< -1");
90                                 ViewState ["CellPadding"] = value;
91                                 styles |= Styles.CellPadding;
92                         }
93                 }
94
95 #if NET_2_0
96                 [NotifyParentProperty (true)]
97 #else
98                 [Bindable (true)]
99 #endif
100                 [DefaultValue (-1)]
101                 [WebSysDescription ("")]
102                 [WebCategory ("Appearance")]
103                 public virtual int CellSpacing {
104                         get {
105                                 if ((styles & Styles.CellSpacing) == 0)
106                                         return -1;
107                                 return (int) ViewState ["CellSpacing"];
108                         }
109                         set {
110                                 if (value < -1)
111                                         throw new ArgumentOutOfRangeException ("< -1");
112                                 ViewState ["CellSpacing"] = value;
113                                 styles |= Styles.CellSpacing;
114                         }
115                 }
116
117                 // LAMESPEC: default is documented to be Both
118 #if NET_2_0
119                 [NotifyParentProperty (true)]
120 #else
121                 [Bindable (true)]
122 #endif
123                 [DefaultValue (GridLines.None)]
124                 [WebSysDescription ("")]
125                 [WebCategory ("Appearance")]
126                 public virtual GridLines GridLines {
127                         get {
128                                 if ((styles & Styles.GridLines) == 0)
129                                         return GridLines.None;
130                                 return (GridLines) ViewState ["GridLines"];
131                         }
132                         set {
133                                 // avoid reflection
134                                 if ((value < GridLines.None) || (value > GridLines.Both)) {
135                                         // LAMESPEC: documented as ArgumentException
136                                         throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid GridLines value."));
137                                 }
138                                 ViewState ["GridLines"] = value;
139                                 styles |= Styles.GridLines;
140                         }
141                 }
142
143 #if NET_2_0
144                 [NotifyParentProperty (true)]
145 #else
146                 [Bindable (true)]
147 #endif
148                 [DefaultValue (HorizontalAlign.NotSet)]
149                 [WebSysDescription ("")]
150                 [WebCategory ("Layout")]
151                 public virtual HorizontalAlign HorizontalAlign {
152                         get {
153                                 if ((styles & Styles.HorizontalAlign) == 0)
154                                         return HorizontalAlign.NotSet;
155                                 return (HorizontalAlign) ViewState ["HorizontalAlign"];
156                         }
157                         set {
158                                 // avoid reflection
159                                 if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
160                                         // LAMESPEC: documented as ArgumentException
161                                         throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
162                                 }
163                                 ViewState ["HorizontalAlign"] = value;
164                                 styles |= Styles.HorizontalAlign;
165                         }
166                 }
167
168
169                 public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
170                 {
171                         base.AddAttributesToRender (writer, owner);
172                         if (writer == null)
173                                 return;
174
175                         // note: avoid calling properties multiple times
176                         int i = CellPadding;
177                         if (i != -1)
178                                 writer.AddAttribute (HtmlTextWriterAttribute.Cellpadding, i.ToString (CultureInfo.InvariantCulture));
179                         
180                         i = CellSpacing;
181                         if (i != -1)
182                                 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, i.ToString (CultureInfo.InvariantCulture));
183
184                         GridLines g = GridLines;
185                         switch (g) {
186                         case GridLines.Horizontal:
187                                 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
188                                 break;
189                         case GridLines.Vertical:
190                                 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
191                                 break;
192                         case GridLines.Both:
193                                 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
194                                 break;
195                         }
196
197                         // note: avoid ToString on the enum
198                         switch (HorizontalAlign) {
199                         case HorizontalAlign.Left:
200                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Left");
201                                 break;
202                         case HorizontalAlign.Center:
203                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Center");
204                                 break;
205                         case HorizontalAlign.Right:
206                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Right");
207                                 break;
208                         case HorizontalAlign.Justify:
209                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Justify");
210                                 break;
211                         }
212
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");
219                         } else {
220                                 writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (CultureInfo.InvariantCulture));
221                         }
222
223                         string s = BackImageUrl;
224                         if (s.Length > 0) {
225                                 if (owner != null)
226                                         s = owner.ResolveUrl (s);
227 #if ONLY_1_1
228                                 s = String.Concat ("url(", s, ")");
229 #endif
230                                 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
231                         }
232                 }
233
234                 private void Copy (string name, Styles s, Style source)
235                 {
236                         if ((source.styles & s) != 0) {
237                                 object o = source.ViewState [name];
238                                 if (o != null) {
239                                         ViewState [name] = o;
240                                         styles |= s;
241                                 }
242                         }
243                 }
244
245                 public override void CopyFrom (Style s)
246                 {
247                         // note: styles is copied in base
248                         base.CopyFrom (s);
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);
255                         }
256                 }
257
258                 private void Merge (string name, Styles s, Style source)
259                 {
260                         if ((styles & s) == 0 && (source.styles & s) != 0) {
261                                 object o = source.ViewState [name];
262                                 if (o != null) {
263                                         ViewState [name] = o;
264                                         styles |= s;
265                                 }
266                         }
267                 }
268
269                 public override void MergeWith (Style s)
270                 {
271                         // if we're empty then it's like a copy
272                         if (IsEmpty) {
273                                 CopyFrom (s);
274                         } else {
275                                 base.MergeWith (s);
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);
282                                 }
283                         }
284                 }
285
286                 public override void Reset ()
287                 {
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
299                         base.Reset ();
300                 }
301 #if NET_2_0
302                 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
303                 {
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);
310                                 }
311                         }
312                         base.FillStyleAttributes (attributes, urlResolver);
313                 }
314 #endif
315
316                 internal override void LoadViewStateInternal()
317                 {
318                         if (viewstate["BackImageUrl"] != null) {
319                                 styles |= Styles.BackImageUrl;
320                         }
321                         if (viewstate["CellPadding"] != null) {
322                                 styles |= Styles.CellPadding;
323                         }
324                         if (viewstate["CellSpacing"] != null) {
325                                 styles |= Styles.CellSpacing;
326                         }
327                         if (viewstate["GridLines"] != null) {
328                                 styles |= Styles.GridLines;
329                         }
330                         if (viewstate["HorizontalAlign"] != null) {
331                                 styles |= Styles.HorizontalAlign;
332                         }
333
334                         base.LoadViewStateInternal();
335                 }
336         }
337 }