refactoring
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TableItemStyle.cs
1 //
2 // System.Web.UI.WebControls.TableItemStyle.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 TableItemStyle : Style {
39
40                 public TableItemStyle ()
41                 {
42                 }
43
44                 public TableItemStyle (StateBag bag)
45                         : base (bag)
46                 {
47                 }
48
49 #if ONLY_1_1
50                 [Bindable (true)]
51 #endif
52                 [DefaultValue (HorizontalAlign.NotSet)]
53                 [NotifyParentProperty (true)]
54                 [WebSysDescription ("")]
55                 [WebCategory("Layout")]
56                 public virtual HorizontalAlign HorizontalAlign {
57                         get {
58                                 if ((styles & Styles.HorizontalAlign) == 0)
59                                         return HorizontalAlign.NotSet;
60                                 return (HorizontalAlign) ViewState ["HorizontalAlign"];
61                         }
62                         set {
63                                 // avoid reflection
64                                 if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
65                                         // LAMESPEC: documented as ArgumentException
66                                         throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
67                                 }
68                                 ViewState ["HorizontalAlign"] = value;
69                                 styles |= Styles.HorizontalAlign;
70                         }
71                 }
72
73 #if ONLY_1_1
74                 [Bindable (true)]
75 #endif
76                 [DefaultValue (VerticalAlign.NotSet)]
77                 [NotifyParentProperty (true)]
78                 [WebSysDescription ("")]
79                 [WebCategory("Layout")]
80                 public virtual VerticalAlign VerticalAlign {
81                         get {
82                                 if ((styles & Styles.VerticalAlign) == 0)
83                                         return VerticalAlign.NotSet;
84                                 return (VerticalAlign) ViewState ["VerticalAlign"];
85                         }
86                         set {
87                                 // avoid reflection
88                                 if ((value < VerticalAlign.NotSet) || (value > VerticalAlign.Bottom)) {
89                                         // LAMESPEC: documented as ArgumentException
90                                         throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid VerticalAlign value."));
91                                 }
92                                 ViewState ["VerticalAlign"] = value;
93                                 styles |= Styles.VerticalAlign;
94                         }
95                 }
96
97 #if ONLY_1_1
98                 [Bindable (true)]
99 #endif
100                 [DefaultValue (true)]
101                 [NotifyParentProperty (true)]
102                 [WebSysDescription ("")]
103                 [WebCategory("Layout")]
104                 public virtual bool Wrap {
105                         get {
106                                 if ((styles & Styles.Wrap) == 0)
107                                         return true;
108                                 return (bool) ViewState ["Wrap"];
109                         }
110                         set {
111                                 ViewState ["Wrap"] = value;
112                                         styles |= Styles.Wrap;
113                         }
114                 }
115
116
117                 public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
118                 {
119                         base.AddAttributesToRender (writer, owner);
120                         if (writer == null)
121                                 return;
122
123                         // note: avoid ToString on the enum
124                         switch (HorizontalAlign) {
125                         case HorizontalAlign.Left:
126                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "left");
127                                 break;
128                         case HorizontalAlign.Center:
129                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "center");
130                                 break;
131                         case HorizontalAlign.Right:
132                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "right");
133                                 break;
134                         case HorizontalAlign.Justify:
135                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "justify");
136                                 break;
137                         }
138
139                         // note: avoid ToString on the enum
140                         switch (VerticalAlign) {
141                         case VerticalAlign.Top:
142                                 writer.AddAttribute (HtmlTextWriterAttribute.Valign, "top");
143                                 break;
144                         case VerticalAlign.Middle:
145                                 writer.AddAttribute (HtmlTextWriterAttribute.Valign, "middle");
146                                 break;
147                         case VerticalAlign.Bottom:
148                                 writer.AddAttribute (HtmlTextWriterAttribute.Valign, "bottom");
149                                 break;
150                         }
151
152                         if (!Wrap) {
153 #if NET_2_0
154                                 writer.AddStyleAttribute (HtmlTextWriterStyle.WhiteSpace, "nowrap");
155 #else
156                                 writer.AddAttribute (HtmlTextWriterAttribute.Nowrap, "nowrap");
157 #endif
158                         }
159                 }
160
161                 private void Copy (string name, Styles s, Style source)
162                 {
163                         if ((source.styles & s) != 0) {
164                                 object o = source.ViewState [name];
165                                 if (o != null) {
166                                         ViewState [name] = o;
167                                         styles |= s;
168                                 }
169                         }
170                 }
171
172                 public override void CopyFrom (Style s)
173                 {
174                         base.CopyFrom (s);
175                         if (s != null && !s.IsEmpty) {
176                                 Copy ("HorizontalAlign", Styles.HorizontalAlign, s);
177                                 Copy ("VerticalAlign", Styles.VerticalAlign, s);
178                                 Copy ("Wrap", Styles.Wrap, s);
179                         }
180                 }
181
182                 private void Merge (string name, Styles s, Style source)
183                 {
184                         if ((styles & s) == 0 && (source.styles & s) != 0) {
185                                 object o = source.ViewState [name];
186                                 if (o != null) {
187                                         ViewState [name] = o;
188                                         styles |= s;
189                                 }
190                         }
191                 }
192
193                 public override void MergeWith (Style s)
194                 {
195                         // if we're empty then it's like a copy
196                         if (IsEmpty) {
197                                 CopyFrom (s);
198                         } else {
199                                 base.MergeWith (s);
200                                 if (s != null) {
201                                         Merge ("HorizontalAlign", Styles.HorizontalAlign, s);
202                                         Merge ("VerticalAlign", Styles.VerticalAlign, s);
203                                         Merge ("Wrap", Styles.Wrap, s);
204                                 }
205                         }
206                 }
207
208                 public override void Reset ()
209                 {
210                         if ((styles & Styles.HorizontalAlign) != 0)
211                                 ViewState.Remove ("HorizontalAlign");
212                         if ((styles & Styles.VerticalAlign) != 0)
213                                 ViewState.Remove ("VerticalAlign");
214                         if ((styles & Styles.Wrap) != 0)
215                                 ViewState.Remove ("Wrap");
216                         // call base at the end because "styles" will reset there
217                         base.Reset ();
218                 }
219
220                 internal override void LoadViewStateInternal()
221                 {
222                         if (viewstate["VerticalAlign"] != null) {
223                                 styles |= Styles.VerticalAlign;
224                         }
225                         if (viewstate["Wrap"] != null) {
226                                 styles |= Styles.Wrap;
227                         }
228
229                         base.LoadViewStateInternal();
230                 }
231         }
232 }