Implemented a few missing properties
[mono.git] / mcs / class / System.Web / System.Web.UI / ControlCollection.cs
index b1db49799bae40f5420ae17a7041dd3ffd10d62a..13264b4341661747c31f961f8273361979d86dae 100644 (file)
@@ -5,9 +5,7 @@
 //     Duncan Mak  (duncan@ximian.com)
 //     Gonzalo Paniagua Javier (gonzalo@novell.com)
 //
-// Copyright (c) 2002-2004 Novell, Inc. (http://www.novell.com)
-//
-
+// Copyright (c) 2002-2005 Novell, Inc. (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.Collections;
+using System.Security.Permissions;
 
 namespace System.Web.UI {
 
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        public class ControlCollection : ICollection, IEnumerable
        {
                Control owner;
@@ -50,7 +51,11 @@ namespace System.Web.UI {
                        this.owner = owner;
                }
 
-               public int Count {
+               public
+#if NET_2_0
+               virtual
+#endif
+               int Count {
                        get { return count; }
                }
 
@@ -94,10 +99,13 @@ namespace System.Web.UI {
                public virtual void Add (Control child)
                {
                        if (child == null)
-                               throw new ArgumentNullException ();
+                               throw new ArgumentNullException ("child");
 
                        if (readOnly)
-                               throw new HttpException ();
+                               throw new HttpException (Locale.GetText ("Collection is read-only."));
+
+                       if (Object.ReferenceEquals (owner, child))
+                               throw new HttpException (Locale.GetText ("Cannot add collection's owner."));
 
                        EnsureControls ();
                        version++;
@@ -114,7 +122,10 @@ namespace System.Web.UI {
                                throw new ArgumentOutOfRangeException ();
 
                        if (readOnly)
-                               throw new HttpException ();
+                               throw new HttpException (Locale.GetText ("Collection is read-only."));
+
+                       if (Object.ReferenceEquals (owner, child))
+                               throw new HttpException (Locale.GetText ("Cannot add collection's owner."));
 
                        if (index == -1) {
                                Add (child);
@@ -148,22 +159,45 @@ namespace System.Web.UI {
                        return (controls != null && Array.IndexOf (controls, c) != -1);
                }
 
-               public void CopyTo (Array array, int index)
+               public
+#if NET_2_0
+               virtual
+#endif
+               void CopyTo (Array array, int index)
                {
                        if (controls == null)
                                return;
 
-                       controls.CopyTo (array, index);
+                       // can't use controls.CopyTo (array, index);
+                       // as we do not allocate it based on the true 
+                       // numbers of items we have in the collection
+                       // so we must re-implement Array.CopyTo :(
+
+                       if (array == null)
+                               throw new ArgumentNullException ("array");
+                       if (index + count > array.GetLowerBound (0) + array.GetLength (0))
+                               throw new ArgumentException ();
+                       if (array.Rank > 1)
+                               throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
+                       if (index < 0)
+                               throw new ArgumentOutOfRangeException ("index", Locale.GetText ("Value has to be >= 0."));
+
+                       for (int i=0; i < count; i++)
+                               array.SetValue (controls [i], i + index);
                }
 
-               public IEnumerator GetEnumerator ()
+               public
+#if NET_2_0
+               virtual
+#endif
+               IEnumerator GetEnumerator ()
                {
                        return new SimpleEnumerator (this);
                }
 
                public virtual int IndexOf (Control c)
                {
-                       if (controls == null)
+                       if (controls == null || c == null)
                                return -1;
 
                        return Array.IndexOf (controls, c);