* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / System.Web.UI / Control.cs
index c725be938e1f525685ed2b8932948715de81af86..83521a7b9de50cf2437e09220253e95467f7da32 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;
@@ -136,6 +141,9 @@ namespace System.Web.UI
                        stateMask = ENABLE_VIEWSTATE | VISIBLE | AUTOID | BINDING_CONTAINER | AUTO_EVENT_WIREUP;
                         if (this is INamingContainer)
                                stateMask |= IS_NAMING_CONTAINER;
+#if NET_2_0
+                       stateMask |= ENABLE_THEMING;
+#endif
                 }
 
 #if NET_2_0
@@ -175,7 +183,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;
                        }
@@ -217,11 +225,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         
 
@@ -274,7 +281,14 @@ 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;
                        }
                }
 
@@ -353,11 +367,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 +447,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 +550,7 @@ namespace System.Web.UI
                        if (!HasControls ())
                                return;
 
-                       foreach (Control c in Controls)
+                       foreach (Control c in _controls)
                                c.NullifyUniqueID ();
                }
 
@@ -584,10 +600,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 +681,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 +699,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 +945,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 +1067,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 ();
                        }
                }
@@ -1062,8 +1087,19 @@ namespace System.Web.UI
 #endif
                void RenderControl (HtmlTextWriter writer)
                {
-                   if ((stateMask & VISIBLE) != 0)
-                           Render(writer);
+                       if ((stateMask & VISIBLE) != 0) {
+                               HttpContext ctx = Context;
+                               TraceContext trace = (ctx != null) ? ctx.Trace : null;
+                               int pos = 0;
+                               if ((trace != null) && trace.IsEnabled)
+                                       pos = ctx.Response.GetOutputByteCount ();
+
+                               Render(writer);
+                               if ((trace != null) && trace.IsEnabled) {
+                                       int size = ctx.Response.GetOutputByteCount () - pos;
+                                       trace.SaveSize (this, size >= 0 ? size : 0);
+                               }
+                       }
                }
 
 #if NET_2_0
@@ -1124,10 +1160,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 ();
                                }
                        }
@@ -1150,10 +1186,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);
                                }
                        }
@@ -1193,10 +1229,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
@@ -1217,7 +1253,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;
@@ -1227,10 +1262,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)
@@ -1241,6 +1276,8 @@ namespace System.Web.UI
 
                        stateMask |= INITING;
 #if NET_2_0
+                       ApplyTheme ();
+                       
                        if (Adapter != null)
                                Adapter.OnInit (EventArgs.Empty);
                        else
@@ -1275,10 +1312,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)
@@ -1355,6 +1392,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) {
+                               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
                {
@@ -1398,15 +1461,7 @@ namespace System.Web.UI
                        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)
+               public string ResolveClientUrl (string url)
                {
                        throw new NotImplementedException ();               
                }