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