Implemented a few missing properties
[mono.git] / mcs / class / System.Web / System.Web.UI / Control.cs
index 0565bb1db0c188991e9f318b546c4d24cbf81ee6..c7295546cbbb035d6829202171ee965969590038 100644 (file)
 // This will provide extra information when trace is enabled. Might be too verbose.
 #define MONO_TRACE
 
-using System;
 using System.Collections;
 using System.ComponentModel;
 using System.ComponentModel.Design;
 using System.ComponentModel.Design.Serialization;
+using System.Security.Permissions;
 using System.Web;
 using System.Web.Util;
 #if NET_2_0
@@ -50,6 +50,10 @@ using System.IO;
 
 namespace System.Web.UI
 {
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       // attributes
        [DefaultProperty ("ID"), DesignerCategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require)]
        [ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, " + Consts.AssemblySystem_Design)]
        [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
@@ -86,6 +90,7 @@ namespace System.Web.UI
 
                string uniqueID;
                string _userId;
+               TemplateControl _templateControl;
                ControlCollection _controls;
                Control _namingContainer;
                Page _page;
@@ -154,18 +159,16 @@ namespace System.Web.UI
                        }
                }
 
+               string _appRelativeTemplateSourceDirectory = "~/";
+
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                [Browsable (false)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public string AppRelativeTemplateSourceDirectory 
                {
-                       get {
-                               throw new NotImplementedException ();
-                       }
+                       get { return _appRelativeTemplateSourceDirectory; }
                        [EditorBrowsable (EditorBrowsableState.Never)]
-                       set {
-                               throw new NotImplementedException ();
-                       }
+                       set     { _appRelativeTemplateSourceDirectory = value; }
                }
                
 #endif         
@@ -175,7 +178,7 @@ namespace System.Web.UI
                public Control BindingContainer {
                        get {
                                Control container = NamingContainer;
-                               if ((container.stateMask & BINDING_CONTAINER) == 0)
+                               if (container != null && (container.stateMask & BINDING_CONTAINER) == 0)
                                        container = container.BindingContainer;
                                return container;
                        }
@@ -189,20 +192,23 @@ namespace System.Web.UI
                                string client = UniqueID;
 
                                if (client != null)
-                                       client = client.Replace (':', '_');
-
+                                       client = client.Replace (':', ClientIDSeparator);
+                               
                                return client;
                        }
                }
 
 #if NET_2_0
-               protected char ClientIDSeparator 
+               protected char ClientIDSeparator
+#else
+               internal protected char ClientIDSeparator
+#endif         
                {
                        get {
-                               throw new NotImplementedException ();
+                               return '_';
                        }
                }
-#endif         
+
 
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [Browsable (false)]
@@ -217,11 +223,10 @@ namespace System.Web.UI
                 }
 
 #if NET_2_0
+               [MonoTODO ("revisit once we have a real design strategy")]
                protected internal bool DesignMode 
                {
-                       get {
-                               throw new NotImplementedException ();
-                       }
+                       get { return false; }
                }
 #endif         
 
@@ -260,7 +265,7 @@ namespace System.Web.UI
                protected char IdSeparator 
                {
                        get {
-                               throw new NotImplementedException ();
+                               return '$';
                        }
                }
 
@@ -274,14 +279,21 @@ namespace System.Web.UI
                protected internal bool IsViewStateEnabled 
                {
                        get {
-                               throw new NotImplementedException ();
+                               if (Page == null)
+                                       return false;
+
+                               for (Control control = this; control != null; control = control.Parent)
+                                       if (!control.EnableViewState)
+                                               return false;
+
+                               return true;
                        }
                }
 
                protected bool LoadViewStateByID 
                {
                        get {
-                               throw new NotImplementedException ();
+                               return false;
                        }
                }
 #endif         
@@ -353,11 +365,11 @@ namespace System.Web.UI
                public TemplateControl TemplateControl 
                {
                        get {
-                               throw new NotImplementedException ();
+                               return _templateControl;
                        }
                        [EditorBrowsable (EditorBrowsableState.Never)]
                        set {
-                               throw new NotImplementedException ();
+                               _templateControl = value;
                        }
                }
 #endif         
@@ -433,8 +445,10 @@ namespace System.Web.UI
                 protected bool ChildControlsCreated {
                         get { return ((stateMask & CHILD_CONTROLS_CREATED) != 0); }
                         set {
-                               if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0)
-                                       Controls.Clear();
+                               if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0) {
+                                       if (_controls != null)
+                                               _controls.Clear();
+                               }
 
                                SetMask (CHILD_CONTROLS_CREATED, value);
                         }
@@ -534,7 +548,7 @@ namespace System.Web.UI
                        if (!HasControls ())
                                return;
 
-                       foreach (Control c in Controls)
+                       foreach (Control c in _controls)
                                c.NullifyUniqueID ();
                }
 
@@ -584,10 +598,19 @@ namespace System.Web.UI
                 }
 
 #if NET_2_0
+               [MonoTODO]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public virtual void ApplyStyleSheetSkin (Page page)
                {
-                       throw new NotImplementedException ();
+                       if (!EnableTheming) /* this enough? */
+                               return;
+
+                       /* apply the style sheet skin here */
+                       if (page.StyleSheetPageTheme != null) {
+                               ControlSkin cs = page.StyleSheetPageTheme.GetControlSkin (GetType(), SkinID);
+                               if (cs != null)
+                                       cs.ApplySkin (this);
+                       }
                }
 #endif         
 
@@ -656,7 +679,7 @@ namespace System.Web.UI
 
                protected bool IsLiteralContent()
                {
-                       if (HasControls () && Controls.Count == 1 && (Controls [0] is LiteralControl))
+                       if (HasControls () && _controls.Count == 1 && (_controls [0] is LiteralControl))
                                return true;
 
                        return false;
@@ -674,7 +697,7 @@ namespace System.Web.UI
                                return null;
 
                        Control result = null;
-                       foreach (Control c in Controls) {
+                       foreach (Control c in _controls) {
                                if (String.Compare (id, c._userId, true) == 0) {
                                        if (result != null && result != c) {
                                                throw new HttpException ("1 Found more than one control with ID '" + id + "'");
@@ -920,10 +943,10 @@ namespace System.Web.UI
                 {
                         if (_renderMethodDelegate != null) {
                                 _renderMethodDelegate (writer, this);
-                       } else if (HasControls ()) {
-                               int len = Controls.Count;
+                       } else if (_controls != null) {
+                               int len = _controls.Count;
                                for (int i = 0; i < len; i++) {
-                                       Control c = Controls [i];
+                                       Control c = _controls [i];
 #if NET_2_0
                                        if (c.Adapter != null)
                                                c.RenderControl (writer, c.Adapter);
@@ -1042,9 +1065,9 @@ namespace System.Web.UI
                        if (!HasControls ())
                                return;
                        
-                       int len = Controls.Count;
+                       int len = _controls.Count;
                        for (int i = 0; i < len; i++) {
-                               Control c = Controls [i];
+                               Control c = _controls [i];
                                c.DataBind ();
                        }
                }
@@ -1066,11 +1089,11 @@ namespace System.Web.UI
                                HttpContext ctx = Context;
                                TraceContext trace = (ctx != null) ? ctx.Trace : null;
                                int pos = 0;
-                               if (trace.IsEnabled)
+                               if ((trace != null) && trace.IsEnabled)
                                        pos = ctx.Response.GetOutputByteCount ();
 
                                Render(writer);
-                               if (trace.IsEnabled) {
+                               if ((trace != null) && trace.IsEnabled) {
                                        int size = ctx.Response.GetOutputByteCount () - pos;
                                        trace.SaveSize (this, size >= 0 ? size : 0);
                                }
@@ -1107,6 +1130,34 @@ namespace System.Web.UI
                        HttpResponse resp = Context.Response;
                        return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
                }
+
+
+#if NET_2_0            
+               public
+#else
+               internal
+#endif
+               string ResolveClientUrl (string relativeUrl)
+               {
+                       if (relativeUrl == null)
+                               throw new ArgumentNullException ("relativeUrl");
+
+                       if (relativeUrl == "")
+                               return "";
+
+                       if (relativeUrl [0] == '#')
+                               return relativeUrl;
+                       
+                       string ts = TemplateSourceDirectory;
+                       if (ts == "" || !UrlUtils.IsRelativeUrl (relativeUrl))
+                               return relativeUrl;
+
+                       HttpResponse resp = Context.Response;
+                       string absoluteUrl = resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
+                       if (absoluteUrl.StartsWith (ts + "/"))
+                               return absoluteUrl.Substring (ts.Length + 1);
+                       return absoluteUrl;
+               }
                
                internal bool HasRenderMethodDelegate () {
                        return _renderMethodDelegate != null;
@@ -1135,10 +1186,10 @@ namespace System.Web.UI
 #endif
                                OnLoad (EventArgs.Empty);
                         if (HasControls ()) {
-                               int len = Controls.Count;
+                               int len = _controls.Count;
                                for (int i=0;i<len;i++)
                                {
-                                       Control c = Controls[i];
+                                       Control c = _controls[i];
                                        c.LoadRecursive ();
                                }
                        }
@@ -1161,10 +1212,10 @@ namespace System.Web.UI
                        }
 #endif
                        if (HasControls ()) {
-                               int len = Controls.Count;
+                               int len = _controls.Count;
                                for (int i=0;i<len;i++)
                                {
-                                       Control c = Controls[i];                                        
+                                       Control c = _controls[i];                                       
                                        c.UnloadRecursive (dispose);
                                }
                        }
@@ -1204,10 +1255,10 @@ namespace System.Web.UI
                                if (!HasControls ())
                                        return;
                                
-                               int len = Controls.Count;
+                               int len = _controls.Count;
                                for (int i=0;i<len;i++)
                                {
-                                       Control c = Controls[i];
+                                       Control c = _controls[i];
                                        c.PreRenderRecursiveInternal ();
                                }
 #if MONO_TRACE
@@ -1228,7 +1279,6 @@ namespace System.Web.UI
                                trace.Write ("control", String.Format ("InitRecursive {0} {1}", _userId, type_name));
                        }
 #endif
-
                         if (HasControls ()) {
                                if ((stateMask & IS_NAMING_CONTAINER) != 0)
                                        namingContainer = this;
@@ -1238,10 +1288,10 @@ namespace System.Web.UI
                                    namingContainer.AutoID)
                                        namingContainer._userId = namingContainer.GetDefaultName () + "b";
 
-                               int len = Controls.Count;
+                               int len = _controls.Count;
                                for (int i=0;i<len;i++)
                                {
-                                       Control c = Controls[i];
+                                       Control c = _controls[i];
                                        c._page = Page;
                                        c._namingContainer = namingContainer;
                                        if (namingContainer != null && c._userId == null && c.AutoID)
@@ -1252,6 +1302,8 @@ namespace System.Web.UI
 
                        stateMask |= INITING;
 #if NET_2_0
+                       ApplyTheme ();
+                       
                        if (Adapter != null)
                                Adapter.OnInit (EventArgs.Empty);
                        else
@@ -1286,10 +1338,10 @@ namespace System.Web.UI
                        int idx = -1;
                        if (HasControls ())
                        {
-                               int len = Controls.Count;
+                               int len = _controls.Count;
                                for (int i=0;i<len;i++)
                                {
-                                       Control ctrl = Controls[i];
+                                       Control ctrl = _controls[i];
                                        object ctrlState = ctrl.SaveViewStateRecursive ();
                                        idx++;
                                        if (ctrlState == null)
@@ -1366,6 +1418,32 @@ namespace System.Web.UI
 #endif
                        stateMask |= VIEWSTATE_LOADED;
                 }
+
+#if NET_2_0
+               internal ControlSkin controlSkin;
+
+                internal void ApplyTheme ()
+                {
+#if MONO_TRACE
+                       TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
+                       string type_name = null;
+                       if (trace != null) {
+                               type_name = GetType ().Name;
+                               trace.Write ("control", String.Format ("ApplyThemeRecursive {0} {1}", _userId, type_name));
+                       }
+#endif
+                       if (Page.PageTheme != null && EnableTheming) {
+                               ControlSkin controlSkin = Page.PageTheme.GetControlSkin (GetType (), SkinID);
+                               if (controlSkin != null)
+                                       controlSkin.ApplySkin (this);
+                       }
+
+#if MONO_TRACE
+                       if (trace != null)
+                               trace.Write ("control", String.Format ("End ApplyThemeRecursive {0} {1}", _userId, type_name));
+#endif
+                }
+#endif
                 
                internal bool AutoID
                {
@@ -1390,14 +1468,28 @@ namespace System.Web.UI
 #if NET_2_0
 
                string skinId = string.Empty;
+               bool _enableTheming = true;
                
                [Browsable (false)]
                [Themeable (false)]
                [DefaultValue (true)]
                public virtual bool EnableTheming
                {
-                       get { return (stateMask & ENABLE_THEMING) != 0; }
-                       set { SetMask (ENABLE_THEMING, value); }
+                       get
+                       {
+                               if ((stateMask & ENABLE_THEMING) != 0)
+                                       return _enableTheming;
+
+                               if (_parent != null)
+                                       return _parent.EnableTheming;
+
+                               return true;
+                       }
+                       set 
+                       { 
+                               SetMask (ENABLE_THEMING, true);
+                               _enableTheming = value;
+                       }
                }
                
                [Browsable (false)]
@@ -1408,19 +1500,6 @@ namespace System.Web.UI
                        get { return skinId; }
                        set { skinId = value; }
                }
-               
-               /* This shouldnt be in 2.0, and I can't find it in 1.1
-                * either, so leaving alone for now.
-                */
-               protected string GetWebResourceUrl (string resourceName)
-               {
-                       return Page.ClientScript.GetWebResourceUrl (GetType(), resourceName); 
-               } 
-
-               string IUrlResolutionService.ResolveClientUrl (string url)
-               {
-                       throw new NotImplementedException ();               
-               }
 
                ControlBuilder IControlBuilderAccessor.ControlBuilder { 
                        get {throw new NotImplementedException (); }