New test.
[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.ComponentModel;
36 using System.Web.Util;
37 using System.IO;
38
39 namespace System.Web.UI
40 {
41         [ParseChildren (false)]
42 #if notyet
43         [Designer (...)]
44 #endif
45         [ControlBuilder (typeof (MasterPageControlBuilder))]
46         public class MasterPage: UserControl
47         {
48                 Hashtable definedContentTemplates = new Hashtable ();
49                 Hashtable templates = new Hashtable ();
50                 ArrayList placeholders = new ArrayList ();
51
52                 string parentMasterPageFile = null;
53                 MasterPage parentMasterPage;
54
55                 [EditorBrowsable (EditorBrowsableState.Advanced)]
56                 protected internal void AddContentTemplate (string templateName, ITemplate template)
57                 {
58                         // LAMESPEC: should be ArgumentException
59                         if (definedContentTemplates.ContainsKey (templateName))
60                                 throw new HttpException ("Multiple contents applied to " + templateName);
61
62                         definedContentTemplates [templateName] = template;
63                 }
64                 
65                 [Browsable (false)]
66                 [EditorBrowsable (EditorBrowsableState.Advanced)]
67                 protected internal IList ContentPlaceHolders {
68                         get { return placeholders; }
69                 }
70                 
71                 [Browsable (false)]
72                 [EditorBrowsable (EditorBrowsableState.Advanced)]
73                 protected internal IDictionary ContentTemplates {
74                         get { return templates; }
75                 }
76                 
77                 [DefaultValueAttribute ("")]
78                 public string MasterPageFile {
79                         get { return parentMasterPageFile; }
80                         set {
81                                 parentMasterPageFile = value;
82                                 parentMasterPage = null;
83                         }
84                 }
85
86                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
87                 [BrowsableAttribute (false)]
88                 public MasterPage Master {
89                         get {
90                                 if (parentMasterPage == null && parentMasterPageFile != null)
91                                         parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
92                         
93                                 return parentMasterPage;
94                         }
95                 }
96
97                 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context, string masterPageFile, IDictionary contentTemplateCollection)
98                 {
99                         MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile, owner.Page.MapPath (masterPageFile), context);
100                         if (contentTemplateCollection != null) {
101                                 foreach (string templateName in contentTemplateCollection.Keys) {
102                                         if (masterPage.ContentTemplates[templateName] == null)
103                                                 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
104                                 }
105                         }
106                         masterPage.Page = owner.Page;
107                         masterPage.InitializeAsUserControlInternal ();
108                         return masterPage;
109                 }
110
111                 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
112                 {
113                         /* XXX need to use virtual paths here? */
114                         if (master.MasterPageFile != null) {
115                                 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
116                                         throw new HttpException ("circular dependency in master page files detected");
117                                 if (master.Master != null) {
118                                         master.Controls.Clear ();
119                                         master.Controls.Add (master.Master);
120                                         appliedMasterPageFiles.Add (master.MasterPageFile);
121                                         MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);
122                                 }
123                         }
124                 }
125         }
126 }
127
128 #endif