Add licensing info
[mono.git] / mcs / class / System.Web / System.Web.UI / TemplateControl.cs
1 //
2 // System.Web.UI.TemplateControl.cs
3 //
4 // Authors:
5 //   Duncan Mak  (duncan@ximian.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.ComponentModel;
36 using System.Reflection;
37 using System.Web;
38 using System.Web.Compilation;
39 using System.Web.Util;
40
41 namespace System.Web.UI {
42
43         public abstract class TemplateControl : Control, INamingContainer
44         {
45                 static object abortTransaction = new object ();
46                 static object commitTransaction = new object ();
47                 static object error = new object ();
48                 static string [] methodNames = { "Page_Init",
49                                                  "Page_Load",
50                                                  "Page_DataBind",
51                                                  "Page_PreRender",
52                                                  "Page_Disposed",
53                                                  "Page_Error",
54                                                  "Page_Unload",
55                                                  "Page_AbortTransaction",
56                                                  "Page_CommitTransaction" };
57
58                 const BindingFlags bflags = BindingFlags.Public |
59                                             BindingFlags.NonPublic |
60                                             BindingFlags.Instance;
61
62                 #region Constructor
63                 protected TemplateControl ()
64                 {
65                         Construct ();
66                 }
67
68                 #endregion
69
70                 #region Properties
71                 [EditorBrowsable (EditorBrowsableState.Never)]
72                 protected virtual int AutoHandlers {
73                         get { return 0; }
74                         set { }
75                 }
76
77                 [EditorBrowsable (EditorBrowsableState.Never)]
78                 protected virtual bool SupportAutoEvents {
79                         get { return true; }
80                 }
81
82                 #endregion
83
84                 #region Methods
85
86                 protected virtual void Construct ()
87                 {
88                 }
89
90                 [MonoTODO]
91                 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
92                                                                                     int size,
93                                                                                     bool fAsciiOnly)
94                 {
95                         return null;
96                 }
97
98                 internal void WireupAutomaticEvents ()
99                 {
100                         if (!SupportAutoEvents || !AutoEventWireup)
101                                 return;
102
103                         Type type = GetType ();
104                         foreach (string methodName in methodNames) {
105                                 MethodInfo method = type.GetMethod (methodName, bflags);
106                                 if (method == null)
107                                         continue;
108
109                                 if (method.ReturnType != typeof (void))
110                                         continue;
111
112                                 ParameterInfo [] parms = method.GetParameters ();
113                                 int length = parms.Length;
114                                 bool noParams = (length == 0);
115                                 if (!noParams && (length != 2 ||
116                                     parms [0].ParameterType != typeof (object) ||
117                                     parms [1].ParameterType != typeof (EventArgs)))
118                                     continue;
119
120                                 int pos = methodName.IndexOf ("_");
121                                 string eventName = methodName.Substring (pos + 1);
122                                 EventInfo evt = type.GetEvent (eventName);
123                                 if (evt == null) {
124                                         /* This should never happen */
125                                         continue;
126                                 }
127
128                                 if (noParams) {
129                                         NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
130                                         evt.AddEventHandler (this, npi.FakeDelegate);
131                                 } else {
132                                         evt.AddEventHandler (this, Delegate.CreateDelegate (
133                                                         typeof (EventHandler), this, methodName));
134                                 }
135                         }
136                 }
137
138                 [EditorBrowsable (EditorBrowsableState.Never)]
139                 protected virtual void FrameworkInitialize ()
140                 {
141                 }
142
143                 Type GetTypeFromControlPath (string virtualPath)
144                 {
145                         if (virtualPath == null)
146                                 throw new ArgumentNullException ("virtualPath");
147
148                         string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
149                         string realpath = Context.Request.MapPath (vpath);
150                         return UserControlParser.GetCompiledType (vpath, realpath, Context);
151                 }
152
153                 public Control LoadControl (string virtualPath)
154                 {
155                         object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
156                         if (control is UserControl)
157                                 ((UserControl) control).InitializeAsUserControl (Page);
158
159                         return (Control) control;
160                 }
161
162                 public ITemplate LoadTemplate (string virtualPath)
163                 {
164                         Type t = GetTypeFromControlPath (virtualPath);
165                         return new SimpleTemplate (t);
166                 }
167
168                 protected virtual void OnAbortTransaction (EventArgs e)
169                 {
170                         EventHandler eh = Events [abortTransaction] as EventHandler;
171                         if (eh != null)
172                                 eh (this, e);
173                 }
174
175                 protected virtual void OnCommitTransaction (EventArgs e)
176                 {
177                         EventHandler eh = Events [commitTransaction] as EventHandler;
178                         if (eh != null)
179                                 eh (this, e);
180                 }
181
182                 protected virtual void OnError (EventArgs e)
183                 {
184                         EventHandler eh = Events [error] as EventHandler;
185                         if (eh != null)
186                                 eh (this, e);
187                 }
188
189                 [MonoTODO]
190                 public Control ParseControl (string content)
191                 {
192                         return null;
193                 }
194
195                 [MonoTODO]
196                 [EditorBrowsable (EditorBrowsableState.Never)]
197                 public static object ReadStringResource (Type t)
198                 {
199                         return null;
200                 }
201
202                 [MonoTODO]
203                 [EditorBrowsable (EditorBrowsableState.Never)]
204                 protected void SetStringResourcePointer (object stringResourcePointer,
205                                                          int maxResourceOffset)
206                 {
207                 }
208
209                 [MonoTODO]
210                 [EditorBrowsable (EditorBrowsableState.Never)]
211                 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
212                                                         int size, bool fAsciiOnly)
213                 {
214                 }
215
216                 #endregion
217
218                 #region Events
219
220                 [WebSysDescription ("Raised when the user aborts a transaction.")]
221                 public event EventHandler AbortTransaction {
222                         add { Events.AddHandler (abortTransaction, value); }
223                         remove { Events.RemoveHandler (abortTransaction, value); }
224                 }
225
226                 [WebSysDescription ("Raised when the user initiates a transaction.")]
227                 public event EventHandler CommitTransaction {
228                         add { Events.AddHandler (commitTransaction, value); }
229                         remove { Events.RemoveHandler (commitTransaction, value); }
230                 }
231
232                 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
233                 public event EventHandler Error {
234                         add { Events.AddHandler (error, value); }
235                         remove { Events.RemoveHandler (error, value); }
236                 }
237
238                 #endregion
239
240                 class SimpleTemplate : ITemplate
241                 {
242                         Type type;
243
244                         public SimpleTemplate (Type type)
245                         {
246                                 this.type = type;
247                         }
248
249                         public void InstantiateIn (Control control)
250                         {
251                                 Control template = Activator.CreateInstance (type) as Control;
252                                 template.SetBindingContainer (false);
253                                 control.Controls.Add (template);
254                         }
255                 }
256
257         }
258 }