2003-07-17 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[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 using System;
13 using System.Collections;
14 using System.ComponentModel;
15 using System.Reflection;
16 using System.Web.Compilation;
17 using System.Web.Util;
18
19 namespace System.Web.UI {
20
21         public abstract class TemplateControl : Control, INamingContainer
22         {
23                 static object abortTransaction = new object ();
24                 static object commitTransaction = new object ();
25                 static object error = new object ();
26                 static string [] methodNames = { "Page_Init",
27                                                  "Page_Load",
28                                                  "Page_DataBind",
29                                                  "Page_PreRender",
30                                                  "Page_Dispose",
31                                                  "Page_Error" };
32
33                 const BindingFlags bflags = BindingFlags.Public |
34                                             BindingFlags.NonPublic |
35                                             BindingFlags.Instance;
36
37                 #region Constructor
38                 protected TemplateControl ()
39                 {
40                         Construct ();
41                 }
42
43                 #endregion
44
45                 #region Properties
46
47                 protected virtual int AutoHandlers {
48                         get { return 0; }
49                         set { }
50                 }
51
52                 protected virtual bool SupportAutoEvents {
53                         get { return true; }
54                 }
55
56                 #endregion
57
58                 #region Methods
59
60                 protected virtual void Construct ()
61                 {
62                 }
63
64                 [MonoTODO]
65                 protected LiteralControl CreateResourceBasedLiteralControl (int offset,
66                                                                                     int size,
67                                                                                     bool fAsciiOnly)
68                 {
69                         return null;
70                 }
71
72                 internal void WireupAutomaticEvents ()
73                 {
74                         if (!SupportAutoEvents || !AutoEventWireup)
75                                 return;
76
77                         Type type = GetType ();
78                         foreach (MethodInfo method in type.GetMethods (bflags)) {
79                                 int pos = Array.IndexOf (methodNames, method.Name);
80                                 if (pos == -1)
81                                         continue;
82
83                                 string name = methodNames [pos];
84                                 pos = name.IndexOf ("_");
85                                 if (pos == -1 || pos + 1 == name.Length)
86                                         continue;
87
88                                 if (method.ReturnType != typeof (void))
89                                         continue;
90
91                                 ParameterInfo [] parms = method.GetParameters ();
92                                 if (parms.Length != 2 ||
93                                     parms [0].ParameterType != typeof (object) ||
94                                     parms [1].ParameterType != typeof (EventArgs))
95                                     continue;
96
97                                 string eventName = name.Substring (pos + 1);
98                                 EventInfo evt = type.GetEvent (eventName);
99                                 if (evt == null)
100                                         continue;
101
102                                 evt.AddEventHandler (this, Delegate.CreateDelegate (typeof (EventHandler), method));
103                         }
104                 }
105
106                 [EditorBrowsable (EditorBrowsableState.Never)]
107                 protected virtual void FrameworkInitialize ()
108                 {
109                 }
110
111                 Type GetTypeFromControlPath (string virtualPath)
112                 {
113                         if (virtualPath == null)
114                                 throw new ArgumentNullException ("virtualPath");
115
116                         return UserControlParser.GetCompiledType (TemplateSourceDirectory, virtualPath, Context);
117                 }
118
119                 public Control LoadControl (string virtualPath)
120                 {
121                         object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
122                         if (control is UserControl)
123                                 ((UserControl) control).InitializeAsUserControl (Page);
124
125                         return (Control) control;
126                 }
127
128                 public ITemplate LoadTemplate (string virtualPath)
129                 {
130                         Type t = GetTypeFromControlPath (virtualPath);
131                         return new SimpleTemplate (t);
132                 }
133
134                 protected virtual void OnAbortTransaction (EventArgs e)
135                 {
136                         EventHandler eh = Events [error] as EventHandler;
137                         if (eh != null)
138                                 eh.Invoke (this, e);
139                 }
140
141                 protected virtual void OnCommitTransaction (EventArgs e)
142                 {
143                         EventHandler eh = Events [commitTransaction] as EventHandler;
144                         if (eh != null)
145                                 eh.Invoke (this, e);
146                 }
147
148                 protected virtual void OnError (EventArgs e)
149                 {
150                         EventHandler eh = Events [abortTransaction] as EventHandler;
151                         if (eh != null)
152                                 eh.Invoke (this, e);
153                 }
154
155                 [MonoTODO]
156                 public Control ParseControl (string content)
157                 {
158                         return null;
159                 }
160
161                 [MonoTODO]
162                 [EditorBrowsable (EditorBrowsableState.Never)]
163                 public static object ReadStringResource (Type t)
164                 {
165                         return null;
166                 }
167
168                 [MonoTODO]
169                 [EditorBrowsable (EditorBrowsableState.Never)]
170                 protected void SetStringResourcePointer (object stringResourcePointer,
171                                                          int maxResourceOffset)
172                 {
173                 }
174
175                 [MonoTODO]
176                 [EditorBrowsable (EditorBrowsableState.Never)]
177                 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
178                                                         int size, bool fAsciiOnly)
179                 {
180                 }
181
182                 #endregion
183
184                 #region Events
185
186                 [WebSysDescription ("Raised when the user aborts a transaction.")]
187                 public event EventHandler AbortTransaction {
188                         add { Events.AddHandler (abortTransaction, value); }
189                         remove { Events.RemoveHandler (abortTransaction, value); }
190                 }
191
192                 [WebSysDescription ("Raised when the user initiates a transaction.")]
193                 public event EventHandler CommitTransaction {
194                         add { Events.AddHandler (commitTransaction, value); }
195                         remove { Events.RemoveHandler (commitTransaction, value); }
196                 }
197
198                 [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
199                 public event EventHandler Error {
200                         add { Events.AddHandler (error, value); }
201                         remove { Events.RemoveHandler (error, value); }
202                 }
203
204                 #endregion
205
206                 class SimpleTemplate : ITemplate
207                 {
208                         Type type;
209
210                         public SimpleTemplate (Type type)
211                         {
212                                 this.type = type;
213                         }
214
215                         public void InstantiateIn (Control control)
216                         {
217                                 Control template = Activator.CreateInstance (type) as Control;
218                                 template.SetBindingContainer (false);
219                                 control.Controls.Add (template);
220                         }
221                 }
222         }
223 }