forgotten in last commit
[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 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
9 //
10
11 using System;
12 using System.Web.Compilation;
13 using System.Web.Util;
14
15 namespace System.Web.UI {
16
17         public abstract class TemplateControl : Control, INamingContainer
18         {
19                 static object abortTransaction = new object ();
20                 static object commitTransaction = new object ();
21                 static object error = new object ();
22
23                 #region Constructor
24                 protected TemplateControl ()
25                 {
26                         Construct ();
27                 }
28
29                 #endregion
30
31                 #region Properties
32
33                 protected virtual int AutoHandlers {
34                         get { return 0; }
35                         set { }
36                 }
37
38                 protected virtual bool SupportAutoEvents {
39                         get { return true; }
40                 }
41
42                 #endregion
43
44                 #region Methods
45
46                 protected virtual void Construct ()
47                 {
48                 }
49
50                 [MonoTODO]
51                 protected virtual LiteralControl CreateResourceBasedLiteralControl (int offset,
52                                                                                     int size,
53                                                                                     bool fAsciiOnly)
54                 {
55                         return null;
56                 }
57
58                 protected virtual void FrameworkInitialize ()
59                 {
60                 }
61
62                 Type GetTypeFromControlPath (string virtualPath)
63                 {
64                         if (virtualPath == null)
65                                 throw new ArgumentNullException ("virtualPath");
66
67                         if (virtualPath [0] == '/')
68                                 throw new ArgumentException ("Path cannot be rooted", "virtualPath");
69
70                         virtualPath = PathUtil.Combine (TemplateSourceDirectory, virtualPath);
71
72                         return UserControlCompiler.CompileUserControlType (new UserControlParser (virtualPath));
73                 }
74
75                 public Control LoadControl (string virtualPath)
76                 {
77                         object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
78                         if (control is UserControl)
79                                 ((UserControl) control).InitializeAsUserControl (Page);
80
81                         return (Control) control;
82                 }
83
84                 public ITemplate LoadTemplate (string virtualPath)
85                 {
86                         Type t = GetTypeFromControlPath (virtualPath);
87                         return new SimpleTemplate (t);
88                 }
89
90                 protected virtual void OnAbortTransaction (EventArgs e)
91                 {
92                         EventHandler eh = Events [error] as EventHandler;
93                         if (eh != null)
94                                 eh.Invoke (this, e);
95                 }
96
97                 protected virtual void OnCommitTransaction (EventArgs e)
98                 {
99                         EventHandler eh = Events [commitTransaction] as EventHandler;
100                         if (eh != null)
101                                 eh.Invoke (this, e);
102                 }
103
104                 protected virtual void OnError (EventArgs e)
105                 {
106                         EventHandler eh = Events [abortTransaction] as EventHandler;
107                         if (eh != null)
108                                 eh.Invoke (this, e);
109                 }
110
111                 [MonoTODO]
112                 public Control ParseControl (string content)
113                 {
114                         return null;
115                 }
116
117                 [MonoTODO]
118                 public static object ReadStringResource (Type t)
119                 {
120                         return null;
121                 }
122
123                 [MonoTODO]
124                 protected void SetStringResourcePointer (object stringResourcePointer,
125                                                          int maxResourceOffset)
126                 {
127                 }
128
129                 [MonoTODO]
130                 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
131                                                         int size, bool fAsciiOnly)
132                 {
133                 }
134
135                 #endregion
136
137                 #region Events
138
139                 public event EventHandler AbortTransaction {
140                         add { Events.AddHandler (abortTransaction, value); }
141                         remove { Events.RemoveHandler (abortTransaction, value); }
142                 }
143
144                 public event EventHandler CommitTransaction {
145                         add { Events.AddHandler (commitTransaction, value); }
146                         remove { Events.RemoveHandler (commitTransaction, value); }
147                 }
148
149                 public event EventHandler Error {
150                         add { Events.AddHandler (error, value); }
151                         remove { Events.RemoveHandler (error, value); }
152                 }
153
154                 #endregion
155
156                 class SimpleTemplate : ITemplate
157                 {
158                         Type type;
159
160                         public SimpleTemplate (Type type)
161                         {
162                                 this.type = type;
163                         }
164
165                         public void InstantiateIn (Control control)
166                         {
167                                 Control template = Activator.CreateInstance (type) as Control;
168                                 template.SetBindingContainer (false);
169                                 control.Controls.Add (template);
170                         }
171                 }
172         }
173 }