merge -r 60439:60440
[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                                 if (i == 0) {
184                                         writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
185                                 }
186                         }
187
188                         GridLines g = GridLines;
189                         switch (g) {
190                         case GridLines.Horizontal:
191                                 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
192                                 break;
193                         case GridLines.Vertical:
194                                 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
195                                 break;
196                         case GridLines.Both:
197                                 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
198                                 break;
199                         }
200
201                         // note: avoid ToString on the enum
202                         switch (HorizontalAlign) {
203                         case HorizontalAlign.Left:
204                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Left");
205                                 break;
206                         case HorizontalAlign.Center:
207                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Center");
208                                 break;
209                         case HorizontalAlign.Right:
210                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Right");
211                                 break;
212                         case HorizontalAlign.Justify:
213                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "Justify");
214                                 break;
215                         }
216
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");
223                         } else {
224                                 writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (CultureInfo.InvariantCulture));
225                         }
226
227                         string s = BackImageUrl;
228                         if (s.Length > 0) {
229                                 if (owner != null)
230                                         s = owner.ResolveUrl (s);
231 #if ONLY_1_1
232                                 s = String.Concat ("url(", s, ")");
233 #endif
234                                 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
235                         }
236                 }
237
238                 private void Copy (string name, Styles s, Style source)
239                 {
240                         if ((source.styles & s) != 0) {
241                                 object o = source.ViewState [name];
242                                 if (o != null) {
243                                         ViewState [name] = o;
244                                         styles |= s;
245                                 }
246                         }
247                 }
248
249                 public override void CopyFrom (Style s)
250                 {
251                         // note: styles is copied in base
252                         base.CopyFrom (s);
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);
259                         }
260                 }
261
262                 private void Merge (string name, Styles s, Style source)
263                 {
264                         if ((styles & s) == 0 && (source.styles & s) != 0) {
265                                 object o = source.ViewState [name];
266                                 if (o != null) {
267                                         ViewState [name] = o;
268                                         styles |= s;
269                                 }
270                         }
271                 }
272
273                 public override void MergeWith (Style s)
274                 {
275                         // if we're empty then it's like a copy
276                         if (IsEmpty) {
277                                 CopyFrom (s);
278                         } else {
279                                 base.MergeWith (s);
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);
286                                 }
287                         }
288                 }
289
290                 public override void Reset ()
291                 {
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
303                         base.Reset ();
304                 }
305 #if NET_2_0
306                 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
307                 {
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);
314                                 }
315                         }
316                         base.FillStyleAttributes (attributes, urlResolver);
317                 }
318 #endif
319
320                 internal override void LoadViewStateInternal()
321                 {
322                         if (viewstate["BackImageUrl"] != null) {
323                                 styles |= Styles.BackImageUrl;
324                         }
325                         if (viewstate["CellPadding"] != null) {
326                                 styles |= Styles.CellPadding;
327                         }
328                         if (viewstate["CellSpacing"] != null) {
329                                 styles |= Styles.CellSpacing;
330                         }
331                         if (viewstate["GridLines"] != null) {
332                                 styles |= Styles.GridLines;
333                         }
334                         if (viewstate["HorizontalAlign"] != null) {
335                                 styles |= Styles.HorizontalAlign;
336                         }
337
338                         base.LoadViewStateInternal();
339                 }
340         }
341 }