2007-08-21 Igor Zelmanovich <igorz@mainsoft.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.Util;
38 using System.IO;
39
40 namespace System.Web.UI
41 {
42         [ParseChildren (false)]
43 #if notyet
44         [Designer (...)]
45 #endif
46         [ControlBuilder (typeof (MasterPageControlBuilder))]
47         public class MasterPage: UserControl
48         {
49                 Hashtable definedContentTemplates = new Hashtable ();
50                 Hashtable templates = new Hashtable ();
51                 ArrayList placeholders = new ArrayList ();
52                 List <string> placeholderIds;
53                 
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 { return placeholders; }
71                 }
72                 
73                 [Browsable (false)]
74                 [EditorBrowsable (EditorBrowsableState.Advanced)]
75                 protected internal IDictionary ContentTemplates {
76                         get { return templates; }
77                 }
78                 
79                 [DefaultValueAttribute ("")]
80                 public string MasterPageFile {
81                         get { return parentMasterPageFile; }
82                         set {
83                                 parentMasterPageFile = value;
84                                 parentMasterPage = null;
85                         }
86                 }
87
88                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89                 [BrowsableAttribute (false)]
90                 public MasterPage Master {
91                         get {
92                                 if (parentMasterPage == null && parentMasterPageFile != null)
93                                         parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
94                         
95                                 return parentMasterPage;
96                         }
97                 }
98                 
99                 internal void SetPlaceHolderIds (List <string> ids)
100                 {
101                         placeholderIds = ids;
102                 }
103                 
104                 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
105                                                              string masterPageFile, IDictionary contentTemplateCollection)
106                 {
107                         MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
108                                                                                             owner.Page.MapPath (masterPageFile),
109                                                                                             context);
110                         List <string> phids = masterPage.placeholderIds;                        
111                         if (contentTemplateCollection != null) {
112                                 foreach (string templateName in contentTemplateCollection.Keys) {
113                                         if (phids != null && !phids.Contains (templateName))
114                                                 throw new HttpException (
115                                                         String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
116                                                                 templateName, masterPageFile));
117                                         if (masterPage.ContentTemplates [templateName] == null)
118                                                 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
119                                 }
120                         }
121                         masterPage.Page = owner.Page;
122                         masterPage.InitializeAsUserControlInternal ();
123                         return masterPage;
124                 }
125
126                 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
127                 {
128                         /* XXX need to use virtual paths here? */
129                         if (master.MasterPageFile != null) {
130                                 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
131                                         throw new HttpException ("circular dependency in master page files detected");
132                                 if (master.Master != null) {
133                                         master.Controls.Clear ();
134                                         master.Controls.Add (master.Master);
135                                         appliedMasterPageFiles.Add (master.MasterPageFile);
136                                         MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);
137                                 }
138                         }
139                 }
140         }
141 }
142
143 #endif