2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / MasterPage.cs
1 //
2 // System.Web.UI.MasterPage.cs
3 //
4 // Authors:
5 //   Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005 Novell, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32
33 using System;
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.ComponentModel;
37 using System.Web.Compilation;
38 using System.Web.Util;
39 using System.Web.UI.WebControls;
40 using System.IO;
41
42 namespace System.Web.UI
43 {
44         [ParseChildren (false)]
45 #if notyet
46         [Designer (...)]
47 #endif
48         [ControlBuilder (typeof (MasterPageControlBuilder))]
49         public class MasterPage: UserControl
50         {
51                 Hashtable definedContentTemplates = new Hashtable ();
52                 Hashtable templates = new Hashtable ();
53                 List <string> placeholders;
54                 string parentMasterPageFile = null;
55                 MasterPage parentMasterPage;
56
57                 [EditorBrowsable (EditorBrowsableState.Advanced)]
58                 protected internal void AddContentTemplate (string templateName, ITemplate template)
59                 {
60                         // LAMESPEC: should be ArgumentException
61                         if (definedContentTemplates.ContainsKey (templateName))
62                                 throw new HttpException ("Multiple contents applied to " + templateName);
63
64                         definedContentTemplates [templateName] = template;
65                 }
66                 
67                 [Browsable (false)]
68                 [EditorBrowsable (EditorBrowsableState.Advanced)]
69                 protected internal IList ContentPlaceHolders {
70                         get {
71                                 if (placeholders == null)
72                                         placeholders = new List <string> ();
73                                 return placeholders;
74                         }
75                 }
76                 
77                 [Browsable (false)]
78                 [EditorBrowsable (EditorBrowsableState.Advanced)]
79                 protected internal IDictionary ContentTemplates {
80                         get { return templates; }
81                 }
82                 
83                 [DefaultValueAttribute ("")]
84                 public string MasterPageFile {
85                         get { return parentMasterPageFile; }
86                         set {
87                                 parentMasterPageFile = value;
88                                 parentMasterPage = null;
89                         }
90                 }
91
92                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93                 [BrowsableAttribute (false)]
94                 public MasterPage Master {
95                         get {
96                                 if (parentMasterPage == null && parentMasterPageFile != null)
97                                         parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
98                         
99                                 return parentMasterPage;
100                         }
101                 }               
102                 
103                 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
104                                                              string masterPageFile, IDictionary contentTemplateCollection)
105                 {
106 #if TARGET_JVM
107                         MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
108                                                                                             owner.Page.MapPath (masterPageFile),
109                                                                                             context);
110 #else
111                         MasterPage masterPage = BuildManager.CreateInstanceFromVirtualPath (masterPageFile, typeof (MasterPage)) as MasterPage;
112 #endif
113                         if (masterPage == null)
114                                 throw new HttpException ("Failed to create MasterPage instance for '" + masterPageFile + "'.");
115
116                         if (contentTemplateCollection != null) {
117                                 foreach (string templateName in contentTemplateCollection.Keys) {
118                                         if (masterPage.ContentTemplates [templateName] == null)
119                                                 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
120                                 }
121                         }
122                         
123                         masterPage.Page = owner.Page;
124                         masterPage.InitializeAsUserControlInternal ();
125
126                         List <string> placeholders = masterPage.placeholders;
127                         if (contentTemplateCollection != null && placeholders != null && placeholders.Count > 0) {
128                                 foreach (string templateName in contentTemplateCollection.Keys) {
129                                         if (!placeholders.Contains (templateName.ToLowerInvariant ())) {
130                                                 throw new HttpException (
131                                                         String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
132                                                                        templateName, masterPageFile));
133                                         }
134                                 }
135                         }
136
137                         return masterPage;
138                 }
139
140                 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
141                 {
142                         /* XXX need to use virtual paths here? */
143                         if (master.MasterPageFile != null) {
144                                 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
145                                         throw new HttpException ("circular dependency in master page files detected");
146                                 if (master.Master != null) {
147                                         master.Controls.Clear ();
148                                         master.Controls.Add (master.Master);
149                                         appliedMasterPageFiles.Add (master.MasterPageFile);
150                                         MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);
151                                 }
152                         }
153                 }
154         }
155 }
156
157 #endif