Browse Source

2007-01-07 Jonathan Pobst <[email protected]>

	* Control.cs: This was messy.  2.0 moves much of ControlCollection
	to ArrangedElementCollection.  Implemented this with as few #if's as 
	possible (which is still too many).

svn path=/trunk/mcs/; revision=70677
Jonathan Pobst 19 years ago
parent
commit
d5f35e06fe

+ 1 - 1
mcs/class/Managed.Windows.Forms/System.Windows.Forms.Layout/ArrangedElementCollection.cs

@@ -33,7 +33,7 @@ namespace System.Windows.Forms.Layout
 {
 	public class ArrangedElementCollection : IList, ICollection, IEnumerable
 	{
-		private ArrayList list;
+		internal ArrayList list;
 
 		internal ArrangedElementCollection ()
 		{

+ 4 - 0
mcs/class/Managed.Windows.Forms/System.Windows.Forms.Layout/ChangeLog

@@ -1,3 +1,7 @@
+2007-01-07  Jonathan Pobst  <[email protected]>
+
+	* ArrangedElementCollection.cs: Make list internal.
+
 2006-12-28  Chris Toshok  <[email protected]>
 
 	* DefaultLayout.cs: split out the various parts (docking,

+ 6 - 0
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog

@@ -1,3 +1,9 @@
+2007-01-07  Jonathan Pobst  <[email protected]>
+
+	* Control.cs: This was messy.  2.0 moves much of ControlCollection
+	to ArrangedElementCollection.  Implemented this with as few #if's as 
+	possible (which is still too many).
+
 2007-01-07  Jonathan Pobst  <[email protected]>
 
 	* Control.cs: Implement SizeFromClientSize() [2.0].

+ 87 - 5
mcs/class/Managed.Windows.Forms/System.Windows.Forms/Control.cs

@@ -385,15 +385,19 @@ namespace System.Windows.Forms
 				Dispose ();
 			}
 		}
+
+		[ListBindable (false)]
 #if NET_2_0
 		[ComVisible (false)]
+		public class ControlCollection : Layout.ArrangedElementCollection, IList, ICollection, ICloneable, IEnumerable {
 #else
 		[DesignerSerializer("System.Windows.Forms.Design.ControlCollectionCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
-#endif
-		[ListBindable(false)]
 		public class ControlCollection : IList, ICollection, ICloneable, IEnumerable {
+#endif
 			#region	ControlCollection Local Variables
+#if !NET_2_0
 			ArrayList list;
+#endif
 			ArrayList impl_list;
 			Control[] all_controls;
 			Control owner;
@@ -402,7 +406,9 @@ namespace System.Windows.Forms
 			#region ControlCollection Public Constructor
 			public ControlCollection(Control owner) {
 				this.owner=owner;
+#if !NET_2_0
 				this.list=new ArrayList();
+#endif
 			}
 			#endregion
 
@@ -412,9 +418,11 @@ namespace System.Windows.Forms
 			}
 
 
+#if !NET_2_0
 			public int Count {
 				get { return list.Count; }
 			}
+#endif
 
 #if NET_2_0
 			bool IList.IsReadOnly
@@ -427,6 +435,22 @@ namespace System.Windows.Forms
 				}
 			}
 
+#if NET_2_0
+			public Control Owner { get { return this.owner; } }
+			
+			public virtual Control this[string key] {
+				get { 
+					int index = IndexOfKey (key);
+					
+					if (index >= 0)
+						return this[index];
+						
+					return null;
+				}
+			}
+			
+			new
+#endif
 			public virtual Control this[int index] {
 				get {
 					if (index < 0 || index >= list.Count) {
@@ -434,10 +458,12 @@ namespace System.Windows.Forms
 					}
 					return (Control)list[index];
 				}
+				
+				
 			}
 			#endregion // ControlCollection Public Instance Properties
 			
-			#region	ControlCollection Private Instance Methods
+			#region	ControlCollection Instance Methods
 			public virtual void Add (Control value)
 			{
 				if (value == null)
@@ -536,6 +562,9 @@ namespace System.Windows.Forms
 				}
 			}
 
+#if NET_2_0
+			new
+#endif
 			public virtual void Clear ()
 			{
 				all_controls = null;
@@ -588,17 +617,24 @@ namespace System.Windows.Forms
 				return Contains (value) || ImplicitContains (value);
 			}
 
+#if NET_2_0
+			public virtual bool ContainsKey (string key)
+			{
+				return IndexOfKey (key) >= 0;
+			}
+#endif
+
 			void ICollection.CopyTo (Array array, int index)
 			{
 				CopyTo (array, index);
 			}
 
+#if !NET_2_0
 			public void CopyTo (Array array, int index)
 			{
 				list.CopyTo(array, index);
 			}
 
-#if !NET_2_0
 			public override bool Equals (object other)
 			{
 				if (other is ControlCollection && (((ControlCollection)other).owner==this.owner)) {
@@ -609,6 +645,27 @@ namespace System.Windows.Forms
 			}
 #endif
 
+#if NET_2_0
+			// LAMESPEC: MSDN says AE, MS implementation throws ANE
+			public Control[] Find (string key, bool searchAllChildren)
+			{
+				if (string.IsNullOrEmpty (key))
+					throw new ArgumentNullException ("key");
+					
+				ArrayList al = new ArrayList ();
+				
+				foreach (Control c in list) {
+					if (c.Name.Equals (key, StringComparison.CurrentCultureIgnoreCase))
+						al.Add (c);
+						
+					if (searchAllChildren)
+						al.AddRange (c.Controls.Find (key, true));
+				}
+				
+				return (Control[])al.ToArray (typeof (Control));
+			}
+#endif
+
 			public int GetChildIndex(Control child) {
 				return GetChildIndex(child, false);
 			}
@@ -630,7 +687,7 @@ namespace System.Windows.Forms
 			}
 
 #if NET_2_0
-			public virtual IEnumerator
+			public override IEnumerator
 #else
 			public IEnumerator
 #endif
@@ -674,6 +731,20 @@ namespace System.Windows.Forms
 				return list.IndexOf(control);
 			}
 
+#if NET_2_0
+			public virtual int IndexOfKey (string key)
+			{
+				if (string.IsNullOrEmpty (key))
+					return -1;
+					
+				for (int i = 0; i < list.Count; i++)
+					if (((Control)list[i]).Name.Equals (key, StringComparison.CurrentCultureIgnoreCase))
+						return i;
+						
+				return -1;
+			}
+#endif
+
 			public virtual void Remove(Control value)
 			{
 				if (value == null)
@@ -702,6 +773,9 @@ namespace System.Windows.Forms
 				owner.UpdateChildrenZOrder ();
 			}
 
+#if NET_2_0
+			new
+#endif
 			public void RemoveAt(int index)
 			{
 				if (index < 0 || index >= list.Count) {
@@ -711,6 +785,14 @@ namespace System.Windows.Forms
 			}
 
 #if NET_2_0
+			public virtual void RemoveByKey (string key)
+			{
+				int index = IndexOfKey (key);
+				
+				if (index >= 0)
+					RemoveAt (index);
+			}	
+		
 			public virtual void
 #else
 			public void

+ 4 - 0
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog

@@ -1,3 +1,7 @@
+2007-01-08  Jonathan Pobst  <[email protected]>
+
+	* ControlTest.cs: Added tests for 2.0 ControlCollection methods.
+
 2007-01-05  Gert Driesen  <[email protected]>
 
 	* ControlTest.cs: Added test for bug #80456.

+ 68 - 0
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ControlTest.cs

@@ -1898,5 +1898,73 @@ namespace MonoTests.System.Windows.Forms
 			private bool _layoutInvoked;
 		}
 	}
+	
+	[TestFixture]
+	public class ControlCollectionTest
+	{
+		[Test]
+		public void ControlCollectionTests ()
+		{
+			Control c = new Control ();
+			c.Name = "A";
+			Control c2 = new Control ();
+			c2.Name = "B";
+			Control c3 = new Control ();
+			c3.Name = "a";
+			Control c4 = new Control ();
+			c4.Name = "B";
+			Control c5 = new Control ();
+			c5.Name = "a";
+			
+			c.Controls.Add (c2);
+			c.Controls.Add (c3);
+			c2.Controls.Add (c4);
+			c2.Controls.Add (c5);
+			
+			// this[key]
+			Assert.AreSame (c2, c.Controls["B"], "A1");
+			Assert.AreSame (c2, c.Controls["b"], "A2");
+			
+			// Owner
+			Assert.AreSame (c, c.Controls.Owner, "A3");
+			
+			// ContainsKey
+			Assert.AreEqual (true, c.Controls.ContainsKey ("A"), "A4");
+			Assert.AreEqual (true, c.Controls.ContainsKey ("a"), "A5");
+			Assert.AreEqual (false, c.Controls.ContainsKey ("C"), "A6"); 
+		
+			// Find
+			Assert.AreEqual (1, c.Controls.Find ("A", false).Length, "A7");
+			Assert.AreEqual (1, c.Controls.Find ("a", false).Length, "A8");
+			Assert.AreEqual (0, c.Controls.Find ("C", false).Length, "A9");
+
+			Assert.AreEqual (2, c.Controls.Find ("A", true).Length, "A10");
+			Assert.AreEqual (2, c.Controls.Find ("a", true).Length, "A11");
+			Assert.AreEqual (0, c.Controls.Find ("C", true).Length, "A12");
+			Assert.AreEqual (1, c2.Controls.Find ("b", true).Length, "A13");
+			
+			// IndexOfKey
+			Assert.AreEqual (1, c.Controls.IndexOfKey ("A"), "A14");
+			Assert.AreEqual (1, c.Controls.IndexOfKey ("a"), "A15");
+			Assert.AreEqual (-1, c.Controls.IndexOfKey ("C"), "A16");
+	
+			// RemoveByKey
+			c.Controls.RemoveByKey ("A");
+			Assert.AreEqual (1, c.Controls.Count, "A17");
+			
+			c.Controls.RemoveByKey ("b");
+			Assert.AreEqual (0, c.Controls.Count, "A18");
+
+			c.Controls.RemoveByKey (null);
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentNullException))]
+		public void ControlCollectionFindANE ()
+		{
+			Control c = new Control ();
+			c.Controls.Find ("", false);
+		}
+	}
 #endif
 }