2007-05-10 Jonathan Pobst <monkey@jpobst.com>
authorJonathan Pobst <monkey@jpobst.com>
Thu, 10 May 2007 20:03:53 +0000 (20:03 -0000)
committerJonathan Pobst <monkey@jpobst.com>
Thu, 10 May 2007 20:03:53 +0000 (20:03 -0000)
* ToolStrip.cs: Use new internal ToolStripItemCollection constructor.
* ToolStripItemCollection.cs: Lots of fixes to when events get called
and parent/owner gets changed based on gert's unit tests.

2007-05-10  Jonathan Pobst  <monkey@jpobst.com>

* ToolStripItemCollectionTest.cs: Enable tests.

svn path=/trunk/mcs/; revision=77160

mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ToolStrip.cs
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ToolStripItemCollection.cs
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ToolStripItemCollectionTest.cs

index 66f30867fe79ff865a63f6efa08b8b16402e956b..3eb7bb5299a04b6292ef3eb0cd189fec63ec4c92 100644 (file)
@@ -1,3 +1,9 @@
+2007-05-10  Jonathan Pobst  <monkey@jpobst.com>
+
+       * ToolStrip.cs: Use new internal ToolStripItemCollection constructor.
+       * ToolStripItemCollection.cs: Lots of fixes to when events get called
+       and parent/owner gets changed based on gert's unit tests.
+
 2007-05-10  Rolf Bjarne Kvinge <RKvinge@novell.com> 
 
        * MaskedTextBox.cs: Started implementing parts of it.
index 1e77bfc194d70d316b740d89d4d55c700b9512d7..08089ad6e3d96aa8765359e9fdd2fff3d86c4eb1 100644 (file)
@@ -92,14 +92,15 @@ namespace System.Windows.Forms
                        SetStyle (ControlStyles.SupportsTransparentBackColor, true);
 
                        this.SuspendLayout ();
-                       this.items = new ToolStripItemCollection (this, items);
+                       
+                       this.items = new ToolStripItemCollection (this, items, true);
                        this.allow_merge = true;
                        base.AutoSize = true;
                        this.back_color = Control.DefaultBackColor;
                        this.can_overflow = true;
                        base.CausesValidation = false;
                        this.default_drop_down_direction = ToolStripDropDownDirection.BelowRight;
-                       this.displayed_items = new ToolStripItemCollection (this, null);
+                       this.displayed_items = new ToolStripItemCollection (this, null, true);
                        this.Dock = this.DefaultDock;
                        base.Font = new Font ("Tahoma", 8.25f);
                        this.fore_color = Control.DefaultForeColor;
@@ -728,7 +729,9 @@ namespace System.Windows.Forms
                        e.Item.Available = true;
                        e.Item.SetPlacement (ToolStripItemPlacement.Main);
                        this.DoAutoSize ();
-                       this.PerformLayout ();
+                       
+                       if (this.Created)
+                               this.PerformLayout ();
                        
                        ToolStripItemEventHandler eh = (ToolStripItemEventHandler)(Events [ItemAddedEvent]);
                        if (eh != null)
index 84d4c777891cf656341a48432ca1989236a3fc2a..f73676ecc2bf403f87434362d0c9ccbee6843931 100644 (file)
@@ -39,18 +39,34 @@ namespace System.Windows.Forms
        public class ToolStripItemCollection : ArrangedElementCollection, IList, ICollection, IEnumerable
        {
                private ToolStrip owner;
-
+               private bool internal_created;
+               
                #region Public Constructor
                public ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value) : base ()
                {
                        if (owner == null)
                                throw new ArgumentNullException ("owner");
 
+                       if (value == null)
+                               throw new ArgumentNullException ("toolStripItems");
+
                        this.owner = owner;
 
+                       foreach (ToolStripItem tsi in value)
+                               this.AddNoOwnerOrLayout (tsi);
+               }
+
+               internal ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value, bool internalcreated) : base ()
+               {
+                       if (owner == null)
+                               throw new ArgumentNullException ("owner");
+
+                       this.internal_created = internalcreated;
+                       this.owner = owner;
+                       
                        if (value != null)
                                foreach (ToolStripItem tsi in value)
-                                       this.Add (tsi);
+                                       this.AddNoOwnerOrLayout (tsi);
                }
                #endregion
 
@@ -91,14 +107,15 @@ namespace System.Windows.Forms
                                throw new ArgumentNullException ("value");
 
                        value.Owner = owner;
-                       value.Parent = owner;
-                       
+                               
                        if (value is ToolStripMenuItem && (value as ToolStripMenuItem).ShortcutKeys != Keys.None)
                                ToolStripManager.AddToolStripMenuItem ((ToolStripMenuItem)value);
                                
                        int index = base.Add (value);
                        
-                       owner.OnItemAdded (new ToolStripItemEventArgs (value));
+                       if (this.internal_created)
+                               owner.OnItemAdded (new ToolStripItemEventArgs (value));
+                               
                        return index;
                }
 
@@ -211,15 +228,18 @@ namespace System.Windows.Forms
                        if (value == null)
                                throw new ArgumentNullException ("value");
 
-                       value.Owner = owner;
-                       value.Parent = owner;
-
                        if (value is ToolStripMenuItem && (value as ToolStripMenuItem).ShortcutKeys != Keys.None)
                                ToolStripManager.AddToolStripMenuItem ((ToolStripMenuItem)value);
 
                        base.Insert (index, value);
-                       owner.OnItemAdded (new ToolStripItemEventArgs (value));
-                       owner.PerformLayout ();
+                       
+                       if (internal_created) {
+                               value.Owner = owner;
+                               owner.OnItemAdded (new ToolStripItemEventArgs (value));
+                       }
+                       
+                       if (owner.Created)
+                               owner.PerformLayout ();
                }
 
                public void Remove (ToolStripItem value)
@@ -228,8 +248,17 @@ namespace System.Windows.Forms
                                throw new NotSupportedException ("This collection is read-only");
 
                        base.Remove (value);
-                       owner.OnItemRemoved (new ToolStripItemEventArgs (value));
-                       owner.PerformLayout ();
+                       
+                       if (value != null && internal_created) {
+                               value.Owner = null;
+                               value.Parent = null;
+                       }
+                       
+                       if (internal_created)
+                               owner.OnItemRemoved (new ToolStripItemEventArgs (value));
+                       
+                       if (owner.Created)      
+                               owner.PerformLayout ();
                }
 
                public new void RemoveAt (int index)
@@ -238,9 +267,7 @@ namespace System.Windows.Forms
                                throw new NotSupportedException ("This collection is read-only");
 
                        ToolStripItem tsi = (ToolStripItem)base[index];
-                       base.RemoveAt (index);
-                       owner.OnItemRemoved (new ToolStripItemEventArgs (tsi));
-                       owner.PerformLayout ();
+                       this.Remove (tsi);
                }
 
                public virtual void RemoveByKey (string key)
index 229c62d4ca5476cc12bae726100ae63438cf1c84..e4bd6741a2c2d59f0789750d8f4e20e2d114fc53 100644 (file)
@@ -1,3 +1,7 @@
+2007-05-10  Jonathan Pobst  <monkey@jpobst.com>
+
+       * ToolStripItemCollectionTest.cs: Enable tests.
+
 2007-05-10  Rolf Bjarne Kvinge <RKvinge@novell.com> 
 
        * MaskedTextBoxTest.cs: Added more tests.
index 251499dbcfe74f3b868de70bb2654692a13bd22e..c7960d379df4638f99a2512af7f3285d647d5dc9 100644 (file)
@@ -36,7 +36,6 @@ using NUnit.Framework;
 namespace MonoTests.System.Windows.Forms
 {
        [TestFixture]
-       [Category ("NotWorking")]
        public class ToolStripItemCollectionTests
        {
                private List<ToolStripItem> itemsAdded;
@@ -101,6 +100,13 @@ namespace MonoTests.System.Windows.Forms
                        }
                }
 
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void ConstructorANE ()
+               {
+                       new ToolStripItemCollection (new ToolStrip (), (ToolStripItem[])null);
+               }
+               
                [Test]
                public void Constructor_Items_Null ()
                {
@@ -149,6 +155,41 @@ namespace MonoTests.System.Windows.Forms
                        Assert.IsNull (buttonC.ParentToolStrip, "#C7");
                }
 
+               [Test]
+               public void Insert_Owned_CreateControl ()
+               {
+                       ToolStrip toolStrip = CreateToolStrip ();
+                       toolStrip.CreateControl ();
+                       ToolStripItemCollection items = toolStrip.Items;
+
+                       MockToolStripButton buttonA = new MockToolStripButton ("A");
+                       items.Insert (0, buttonA);
+                       Assert.AreEqual (1, items.Count, "#A1");
+                       Assert.AreEqual (1, itemsAdded.Count, "#A2");
+                       Assert.AreSame (buttonA, items[0], "#A3");
+                       Assert.AreSame (toolStrip, buttonA.Owner, "#A4");
+                       Assert.IsNotNull (buttonA.ParentToolStrip, "#A5");
+
+                       MockToolStripButton buttonB = new MockToolStripButton ("B");
+                       items.Insert (0, buttonB);
+                       Assert.AreEqual (2, items.Count, "#B1");
+                       Assert.AreEqual (2, itemsAdded.Count, "#B2");
+                       Assert.AreSame (buttonB, items[0], "#B3");
+                       Assert.AreSame (buttonA, items[1], "#B4");
+                       Assert.AreSame (toolStrip, buttonB.Owner, "#B5");
+                       Assert.IsNotNull (buttonB.ParentToolStrip, "#B6");
+
+                       MockToolStripButton buttonC = new MockToolStripButton ("C");
+                       items.Insert (1, buttonC);
+                       Assert.AreEqual (3, items.Count, "#C1");
+                       Assert.AreEqual (3, itemsAdded.Count, "#C2");
+                       Assert.AreSame (buttonB, items[0], "#C3");
+                       Assert.AreSame (buttonC, items[1], "#C4");
+                       Assert.AreSame (buttonA, items[2], "#C5");
+                       Assert.AreSame (toolStrip, buttonC.Owner, "#C6");
+                       Assert.IsNotNull (buttonC.ParentToolStrip, "#C7");
+               }
+
                [Test]
                public void Insert_StandAlone ()
                {