Add licensing info
[mono.git] / mcs / class / System.Web / System.Web.UI / UserControl.cs
1 //
2 // System.Web.UI.UserControl.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 //
10
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 using System;
32 using System.ComponentModel;
33 using System.ComponentModel.Design;
34 using System.ComponentModel.Design.Serialization;
35 using System.Web.Caching;
36 using System.Web.SessionState;
37
38 namespace System.Web.UI
39 {
40         [ControlBuilder (typeof (UserControlControlBuilder))]
41         [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
42         [ToolboxItem (false), ParseChildren (true)]
43         [Designer ("System.Web.UI.Design.UserControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
44         [Designer ("Microsoft.VSDesigner.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VSDesigner, typeof (IRootDesigner))]
45         [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
46         public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor
47         {
48                 private bool initialized;
49                 private AttributeCollection attributes;
50                 private StateBag attrBag;
51
52                 public UserControl ()
53                 {
54                         //??
55                 }
56
57                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
58                 [Browsable (false)]
59                 public HttpApplicationState Application
60                 {
61                         get {
62                                 Page p = Page;
63                                 if (p == null)
64                                         return null;
65                                 return p.Application;
66                         }
67                 }
68
69                 private void EnsureAttributes ()
70                 {
71                         if (attributes == null) {
72                                 attrBag = new StateBag (true);
73                                 if (IsTrackingViewState)
74                                         attrBag.TrackViewState ();
75                                 attributes = new AttributeCollection (attrBag);
76                         }
77                 }
78
79                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
80                 [Browsable (false)]
81                 public AttributeCollection Attributes
82                 {
83                         get {
84                                 return attributes;
85                         }
86                 }
87
88                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89                 [Browsable (false)]
90                 public Cache Cache
91                 {
92                         get {
93                                 Page p = Page;
94                                 if (p == null)
95                                         return null;
96                                 return p.Cache;
97                         }
98                 }
99
100                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
101                 [Browsable (false)]
102                 public bool IsPostBack
103                 {
104                         get {
105                                 Page p = Page;
106                                 if (p == null)
107                                         return false;
108                                 return p.IsPostBack;
109                         }
110                 }
111
112                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
113                 [Browsable (false)]
114                 public HttpRequest Request
115                 {
116                         get {
117                                 Page p = Page;
118                                 if (p == null)
119                                         return null;
120                                 return p.Request;
121                         }
122                 }
123
124                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
125                 [Browsable (false)]
126                 public HttpResponse Response
127                 {
128                         get {
129                                 Page p = Page;
130                                 if (p == null)
131                                         return null;
132                                 return p.Response;
133                         }
134                 }
135
136                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
137                 [Browsable (false)]
138                 public HttpServerUtility Server
139                 {
140                         get {
141                                 Page p = Page;
142                                 if (p == null)
143                                         return null;
144                                 return p.Server;
145                         }
146                 }
147
148                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
149                 [Browsable (false)]
150                 public HttpSessionState Session
151                 {
152                         get {
153                                 Page p = Page;
154                                 if (p == null)
155                                         return null;
156                                 return p.Session;
157                         }
158                 }
159
160                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
161                 [Browsable (false)]
162                 public TraceContext Trace
163                 {
164                         get {
165                                 Page p = Page;
166                                 if (p == null)
167                                         return null;
168                                 return p.Trace;
169                         }
170                 }
171
172                 [EditorBrowsable (EditorBrowsableState.Never)]
173                 public void DesignerInitialize ()
174                 {
175                         InitRecursive (null);
176                 }
177
178                 [EditorBrowsable (EditorBrowsableState.Never)]
179                 public void InitializeAsUserControl (Page page)
180                 {
181                         if (initialized)
182                                 return;
183                         initialized = true;
184                         this.Page = page;
185                         WireupAutomaticEvents ();
186                         FrameworkInitialize ();
187                 }
188
189                 public string MapPath (string virtualPath)
190                 {
191                         return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
192                 }
193
194                 protected override void LoadViewState (object savedState)
195                 {
196                         if (savedState != null) {
197                                 Pair p = (Pair) savedState;
198                                 base.LoadViewState (p.First);
199                                 if (p.Second != null) {
200                                         EnsureAttributes ();
201                                         attrBag.LoadViewState (p.Second);
202                                 }
203                         }
204
205                 }
206
207                 protected override void OnInit (EventArgs e)
208                 {
209                         InitializeAsUserControl (Page);
210
211                         base.OnInit(e);
212                 }
213
214                 protected override object SaveViewState ()
215                 {
216                         object baseState = base.SaveViewState();
217                         object attrState = null;
218                         if (attributes != null)
219                                 attrState = attrBag.SaveViewState ();
220                         if (baseState == null && attrState == null)
221                                 return null;
222                         return new Pair (baseState, attrState);
223                 }
224
225                 string IAttributeAccessor.GetAttribute (string name)
226                 {
227                         if (attributes == null)
228                                 return null;
229                         return attributes [name];
230                 }
231                 
232                 void IAttributeAccessor.SetAttribute (string name, string value)
233                 {
234                         EnsureAttributes ();
235                         Attributes [name] = value;
236                 }
237
238                 string IUserControlDesignerAccessor.InnerText
239                 {
240                         get {
241                                 string innerText = ((string) ViewState["!DesignTimeInnerText"]);
242                                 if (innerText == null)
243                                         return string.Empty; 
244                                 return innerText;
245                         }
246                         set { ViewState["!DesignTimeInnerText"] = value; }
247                 }
248
249                 string IUserControlDesignerAccessor.TagName
250                 {
251                         get {
252                                 string innerTag = ((string) ViewState["!DesignTimeTagName"]);
253                                 if (innerTag == null)
254                                         return string.Empty; 
255                                 return innerTag;
256                         }
257                         set { ViewState["!DesignTimeTagName"] = value; }
258                 }
259         }
260 }
261