2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Panel.cs
1 //
2 // System.Web.UI.WebControls.Panel.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // TODO: Are we missing something in LoadViewState?
10 // What to do in AddParsedSubObject
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.ComponentModel;
33 using System.Security.Permissions;
34
35 namespace System.Web.UI.WebControls {
36
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         // attributes
41         [Designer ("System.Web.UI.Design.WebControls.PanelDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
42         [ParseChildren (false)]
43         [PersistChildren (true)]
44         [ToolboxData ("<{0}:Panel runat=server>Panel</{0}:Panel>")]
45         public class Panel : WebControl {
46
47                 public Panel () : base (HtmlTextWriterTag.Div) 
48                 {
49                 }
50                 
51                 protected override void AddAttributesToRender (HtmlTextWriter w)
52                 {
53                         base.AddAttributesToRender (w);
54                         
55                         string image = BackImageUrl;
56                         if (image != "") {
57                                 image = ResolveClientUrl (image);
58 #if !NET_2_0 // see HtmlTextWriter.WriteStyleAttribute(string, string, bool) 
59                                 image = String.Concat ("url(", image, ")");
60 #endif
61                                 w.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, image);
62                         }
63
64 #if NET_2_0
65                         if (!String.IsNullOrEmpty (DefaultButton) && Page != null) {
66                                 Control button = FindControl (DefaultButton);
67                                 if (button == null || !(button is IButtonControl))
68                                         throw new InvalidOperationException (String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.", ID));
69
70                                 Page.ClientScript.RegisterWebFormClientScript ();
71
72                                 w.AddAttribute ("onkeypress",
73                                                 "javascript:return " + Page.WebFormScriptReference + ".WebForm_FireDefaultButton(event, '" + button.ClientID + "')");
74                         }
75
76                         if (Direction != ContentDirection.NotSet) {
77                                 w.AddAttribute (HtmlTextWriterAttribute.Dir, Direction == ContentDirection.RightToLeft ? "rtl" : "ltr", false);
78                         }
79
80                         switch (ScrollBars) {
81                         case ScrollBars.Auto:
82                                 w.AddStyleAttribute (HtmlTextWriterStyle.Overflow, "auto");
83                                 break;
84                         case ScrollBars.Both:
85                                 w.AddStyleAttribute (HtmlTextWriterStyle.Overflow, "scroll");
86                                 break;
87                         case ScrollBars.Horizontal:
88                                 w.AddStyleAttribute (HtmlTextWriterStyle.OverflowX, "scroll");
89                                 break;
90                         case ScrollBars.Vertical:
91                                 w.AddStyleAttribute (HtmlTextWriterStyle.OverflowY, "scroll");
92                                 break;
93                         }
94
95 #endif
96
97                         if (!Wrap) {
98 #if NET_2_0
99                                 w.AddStyleAttribute (HtmlTextWriterStyle.WhiteSpace, "nowrap");
100 #else
101                                 w.AddAttribute (HtmlTextWriterAttribute.Nowrap, "nowrap");
102 #endif
103                         }
104
105                         string align = "";
106
107                         switch (HorizontalAlign) {
108                         case HorizontalAlign.Center: align = "center"; break;
109                         case HorizontalAlign.Justify: align = "justify"; break;
110                         case HorizontalAlign.Left: align = "left"; break;
111                         case HorizontalAlign.Right: align = "right"; break;
112                         }
113
114                         if (align != "")
115 #if NET_2_0
116                                 w.AddStyleAttribute (HtmlTextWriterStyle.TextAlign, align);
117 #else
118                                 w.AddAttribute (HtmlTextWriterAttribute.Align, align);
119 #endif
120                 }
121                 
122 #if !NET_2_0
123                 [Bindable(true)]
124                 [DefaultValue("")]
125                 [Editor("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
126                 [WebSysDescription ("")]
127                 [WebCategory ("Appearance")]
128                 public virtual string BackImageUrl {
129                         get {
130                                 return ViewState.GetString ("BackImageUrl", "");
131                         }
132                         
133                         set {
134                                 ViewState ["BackImageUrl"] = value;
135                         }
136                 }
137                 
138                 [Bindable(true)]
139                 [DefaultValue(HorizontalAlign.NotSet)]
140                 [WebSysDescription ("")]
141                 [WebCategory ("Layout")]
142                 public virtual HorizontalAlign HorizontalAlign {
143                         get {
144                                 return (HorizontalAlign) ViewState.GetInt ("HorizontalAlign", (int) HorizontalAlign.NotSet);
145                         }
146                         set {
147                                 ViewState ["HorizontalAlign"] = (int) value;
148                         }
149                 }
150                 
151                 [Bindable(true)]
152                 [DefaultValue(true)]
153                 [WebSysDescription ("")]
154                 [WebCategory ("Layout")]
155                 public virtual bool Wrap {
156                         get {
157                                 return ViewState.GetBool ("Wrap", true);
158                         }
159                         set {
160                                 ViewState ["Wrap"] = value;
161                         }
162                 }
163 #endif
164
165 #if NET_2_0
166                 PanelStyle PanelStyle {
167                         get { return (ControlStyle as PanelStyle); }
168                 }
169                 
170                 [Bindable (true)]
171                 [DefaultValue ("")]
172                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
173                 [WebSysDescription ("")]
174                 [WebCategory ("Appearance")]
175                 public virtual string BackImageUrl {
176                         get {
177                                 if (ControlStyleCreated) {
178                                         if (PanelStyle != null)
179                                                 return PanelStyle.BackImageUrl;
180                                         else
181                                                 return ViewState.GetString ("BackImageUrl", String.Empty);
182                                 }
183                                 return String.Empty;
184                         }
185                         set {
186                                 if(PanelStyle!=null)
187                                         PanelStyle.BackImageUrl = value;
188                                 else
189                                         ViewState ["BackImageUrl"] = value;
190                         }
191                 }
192
193                 [Bindable (true)]
194                 [DefaultValue (HorizontalAlign.NotSet)]
195                 [WebSysDescription ("")]
196                 [WebCategory ("Layout")]
197                 public virtual HorizontalAlign HorizontalAlign {
198                         get {
199                                 if (ControlStyleCreated) {
200                                         if (PanelStyle != null)
201                                                 return PanelStyle.HorizontalAlign;
202                                         else
203                                                 return ViewState ["HorizontalAlign"] != null ? (HorizontalAlign) ViewState ["HorizontalAlign"] : HorizontalAlign.NotSet;
204                                 }
205                                 return HorizontalAlign.NotSet;
206                         }
207                         set {
208                                 if (PanelStyle != null)
209                                         PanelStyle.HorizontalAlign = value;
210                                 else
211                                         ViewState ["HorizontalAlign"] = value;
212                         }
213                 }
214
215                 [Bindable (true)]
216                 [DefaultValue (true)]
217                 [WebSysDescription ("")]
218                 [WebCategory ("Layout")]
219                 public virtual bool Wrap {
220                         get {
221                                 if (ControlStyleCreated) {
222                                         if (PanelStyle != null)
223                                                 return PanelStyle.Wrap;
224                                         else
225                                                 return ViewState.GetBool ("Wrap", true);
226                                 }
227                                 return true;
228                         }
229                         set {
230                                 if (PanelStyle != null)
231                                         PanelStyle.Wrap = value;
232                                 else
233                                         ViewState ["Wrap"] = value;
234                         }
235                 }
236                 
237                 [ThemeableAttribute (false)]
238                 public virtual string DefaultButton {
239                         get {
240                                 return ViewState.GetString ("DefaultButton", String.Empty);
241                         }
242                         set {
243                                 ViewState ["DefaultButton"] = value;
244                         }
245                 }
246
247                 public virtual ContentDirection Direction {
248                         get {
249                                 if (ControlStyleCreated) {
250                                         if (PanelStyle != null)
251                                                 return PanelStyle.Direction;
252                                         else
253                                                 return ViewState ["Direction"] != null ? (ContentDirection) ViewState ["Direction"] : ContentDirection.NotSet;
254                                 }
255                                 return ContentDirection.NotSet;
256                         }
257                         set {
258                                 if (PanelStyle != null)
259                                         PanelStyle.Direction = value;
260                                 else
261                                         ViewState ["Direction"] = value;
262                         }
263                 }
264
265                 [LocalizableAttribute (true)]
266                 public virtual string GroupingText {
267                         get {
268                                 return ViewState.GetString ("GroupingText", String.Empty);
269                         }
270                         set {
271                                 ViewState ["GroupingText"] = value;
272                         }
273                 }
274
275                 public virtual ScrollBars ScrollBars {
276                         get {
277                                 if (ControlStyleCreated) {
278                                         if (PanelStyle != null)
279                                                 return PanelStyle.ScrollBars;
280                                         else
281                                                 return ViewState ["ScrollBars"] != null ? (ScrollBars) ViewState ["Direction"] : ScrollBars.None;
282                                 }
283                                 return ScrollBars.None;
284                         }
285                         set {
286                                 if (PanelStyle != null)
287                                         PanelStyle.ScrollBars = value;
288                                 else
289                                         ViewState ["ScrollBars"] = value;
290                         }
291                 }
292
293                 protected override Style CreateControlStyle ()
294                 {
295                         return new PanelStyle (ViewState);
296                 }
297
298                 public override void RenderBeginTag (HtmlTextWriter writer)
299                 {
300                         base.RenderBeginTag (writer);
301                         if (!String.IsNullOrEmpty (GroupingText)) {
302                                 writer.RenderBeginTag (HtmlTextWriterTag.Fieldset);
303                                 writer.RenderBeginTag (HtmlTextWriterTag.Legend);
304                                 writer.Write (GroupingText);
305                                 writer.RenderEndTag ();
306                         }
307                 }
308
309                 public override void RenderEndTag (HtmlTextWriter writer)
310                 {
311                         if (!String.IsNullOrEmpty (GroupingText)) {
312                                 writer.RenderEndTag (); // Fieldset
313                         }
314                         base.RenderEndTag (writer);
315                 }
316 #endif
317         }
318 }