* LosFormatter.cs: Avoid using Position when stream is unseekable.
[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.IO;
40
41 namespace System.Web.UI
42 {
43         [ParseChildren (false)]
44 #if notyet
45         [Designer (...)]
46 #endif
47         [ControlBuilder (typeof (MasterPageControlBuilder))]
48         public class MasterPage: UserControl
49         {
50                 Hashtable definedContentTemplates = new Hashtable ();
51                 Hashtable templates = new Hashtable ();
52                 ArrayList placeholders = new ArrayList ();
53                 List <string> placeholderIds;
54                 
55                 string parentMasterPageFile = null;
56                 MasterPage parentMasterPage;
57
58                 [EditorBrowsable (EditorBrowsableState.Advanced)]
59                 protected internal void AddContentTemplate (string templateName, ITemplate template)
60                 {
61                         // LAMESPEC: should be ArgumentException
62                         if (definedContentTemplates.ContainsKey (templateName))
63                                 throw new HttpException ("Multiple contents applied to " + templateName);
64
65                         definedContentTemplates [templateName] = template;
66                 }
67                 
68                 [Browsable (false)]
69                 [EditorBrowsable (EditorBrowsableState.Advanced)]
70                 protected internal IList ContentPlaceHolders {
71                         get { return placeholders; }
72                 }
73                 
74                 [Browsable (false)]
75                 [EditorBrowsable (EditorBrowsableState.Advanced)]
76                 protected internal IDictionary ContentTemplates {
77                         get { return templates; }
78                 }
79                 
80                 [DefaultValueAttribute ("")]
81                 public string MasterPageFile {
82                         get { return parentMasterPageFile; }
83                         set {
84                                 parentMasterPageFile = value;
85                                 parentMasterPage = null;
86                         }
87                 }
88
89                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
90                 [BrowsableAttribute (false)]
91                 public MasterPage Master {
92                         get {
93                                 if (parentMasterPage == null && parentMasterPageFile != null)
94                                         parentMasterPage = MasterPage.CreateMasterPage (this, Context, parentMasterPageFile, definedContentTemplates);
95                         
96                                 return parentMasterPage;
97                         }
98                 }
99                 
100                 internal void SetPlaceHolderIds (List <string> ids)
101                 {
102                         placeholderIds = ids;
103                 }
104                 
105                 internal static MasterPage CreateMasterPage (TemplateControl owner, HttpContext context,
106                                                              string masterPageFile, IDictionary contentTemplateCollection)
107                 {
108 #if TARGET_JVM
109                         MasterPage masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile,
110                                                                                             owner.Page.MapPath (masterPageFile),
111                                                                                             context);
112 #else
113                         MasterPage masterPage = BuildManager.CreateInstanceFromVirtualPath (masterPageFile, typeof (MasterPage)) as MasterPage;
114 #endif
115                         if (masterPage == null)
116                                 throw new HttpException ("Failed to create MasterPage instance for '" + masterPageFile + "'.");
117                         
118                         List <string> phids = masterPage.placeholderIds;
119                         if (contentTemplateCollection != null) {
120                                 foreach (string templateName in contentTemplateCollection.Keys) {
121                                         if (phids != null && !phids.Contains (templateName))
122                                                 throw new HttpException (
123                                                         String.Format ("Cannot find ContentPlaceHolder '{0}' in the master page '{1}'",
124                                                                 templateName, masterPageFile));
125                                         if (masterPage.ContentTemplates [templateName] == null)
126                                                 masterPage.ContentTemplates [templateName] = contentTemplateCollection[templateName];
127                                 }
128                         }
129                         masterPage.Page = owner.Page;
130                         masterPage.InitializeAsUserControlInternal ();
131                         return masterPage;
132                 }
133
134                 internal static void ApplyMasterPageRecursive (MasterPage master, IList appliedMasterPageFiles)
135                 {
136                         /* XXX need to use virtual paths here? */
137                         if (master.MasterPageFile != null) {
138                                 if (appliedMasterPageFiles.Contains (master.MasterPageFile))
139                                         throw new HttpException ("circular dependency in master page files detected");
140                                 if (master.Master != null) {
141                                         master.Controls.Clear ();
142                                         master.Controls.Add (master.Master);
143                                         appliedMasterPageFiles.Add (master.MasterPageFile);
144                                         MasterPage.ApplyMasterPageRecursive (master.Master, appliedMasterPageFiles);
145                                 }
146                         }
147                 }
148         }
149 }
150
151 #endif