Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStrip.cs
index ba013b03ead07043f6a874b931fe1745e88fa29f..30c0d89f5fb40633ee7a05e3dc555e6408b74e67 100644 (file)
@@ -26,7 +26,6 @@
 //     Jonathan Pobst (monkey@jpobst.com)
 //
 
-#if NET_2_0
 using System;
 using System.Runtime.InteropServices;
 using System.ComponentModel;
@@ -727,16 +726,22 @@ namespace System.Windows.Forms
                protected override void Dispose (bool disposing)
                {
                        if (!IsDisposed) {
-                               CloseToolTip (null);
-                               // ToolStripItem.Dispose modifes the collection,
-                               // so we iterate it in reverse order
-                               for (int i = Items.Count - 1; i >= 0; i--)
-                                       Items [i].Dispose ();
-                                       
-                               if (this.overflow_button != null && this.overflow_button.drop_down != null)
-                                       this.overflow_button.drop_down.Dispose ();
 
-                               ToolStripManager.RemoveToolStrip (this);
+                               if(disposing) {
+                                       // Event Handler must be stopped before disposing Items.
+                                       Events.Dispose();
+
+                                       CloseToolTip (null);
+                                       // ToolStripItem.Dispose modifes the collection,
+                                       // so we iterate it in reverse order
+                                       for (int i = Items.Count - 1; i >= 0; i--)
+                                               Items [i].Dispose ();
+
+                                       if (this.overflow_button != null && this.overflow_button.drop_down != null)
+                                               this.overflow_button.drop_down.Dispose ();
+
+                                       ToolStripManager.RemoveToolStrip (this);
+                               }
                                base.Dispose (disposing);
                        }
                }
@@ -889,7 +894,7 @@ namespace System.Windows.Forms
                                if (this is MenuStrip && mouse_currently_over is ToolStripMenuItem && !(mouse_currently_over as ToolStripMenuItem).HasDropDownItems)
                                        return;
                        } else {
-                               this.HideMenus (true, ToolStripDropDownCloseReason.AppClicked);
+                               this.Dismiss (ToolStripDropDownCloseReason.AppClicked);
                        }
                        
                        if (this is MenuStrip)
@@ -1426,25 +1431,28 @@ namespace System.Windows.Forms
                                Point currentLocation = Point.Empty;
                                int tallest = 0;
                                
-                               foreach (ToolStripItem tsi in items) {
-                                       if ((DisplayRectangle.Width - currentLocation.X) < (tsi.Width + tsi.Margin.Horizontal)) {
+                               foreach (ToolStripItem tsi in items)
+                                       if (tsi.Available) {
+                                               Size tsi_preferred = tsi.GetPreferredSize (Size.Empty);
+
+                                               if ((DisplayRectangle.Width - currentLocation.X) < (tsi_preferred.Width + tsi.Margin.Horizontal)) {
 
-                                               currentLocation.Y += tallest;
-                                               tallest = 0;
+                                                       currentLocation.Y += tallest;
+                                                       tallest = 0;
+                                                       
+                                                       currentLocation.X = DisplayRectangle.Left;
+                                               }
+
+                                               // Offset the left margin and set the control to our point
+                                               currentLocation.Offset (tsi.Margin.Left, 0);
+                                               tallest = Math.Max (tallest, tsi_preferred.Height + tsi.Margin.Vertical);
                                                
-                                               currentLocation.X = DisplayRectangle.Left;
+                                               // Update our location pointer
+                                               currentLocation.X += tsi_preferred.Width + tsi.Margin.Right;
                                        }
 
-                                       // Offset the left margin and set the control to our point
-                                       currentLocation.Offset (tsi.Margin.Left, 0);
-                                       tallest = Math.Max (tallest, tsi.Height + tsi.Margin.Vertical);
-                                       
-                                       // Update our location pointer
-                                       currentLocation.X += tsi.Width + tsi.Margin.Right;
-                               }
-
                                currentLocation.Y += tallest;
-                               return new Size (currentLocation.X, currentLocation.Y);
+                               return new Size (currentLocation.X + this.Padding.Horizontal, currentLocation.Y + this.Padding.Vertical);
                        }
                                
                        if (this.orientation == Orientation.Vertical) {
@@ -1495,17 +1503,6 @@ namespace System.Windows.Forms
                        this.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.ItemClicked);
                }
                
-               internal void HideMenus (bool release, ToolStripDropDownCloseReason reason)
-               {
-                       if (this is MenuStrip && release && menu_selected)
-                               (this as MenuStrip).FireMenuDeactivate ();
-                               
-                       if (release)
-                               menu_selected = false;
-                               
-                       NotifySelectedChanged (null);
-               }
-
                internal void NotifySelectedChanged (ToolStripItem tsi)
                {
                        foreach (ToolStripItem tsi2 in this.DisplayedItems)
@@ -1786,4 +1783,3 @@ namespace System.Windows.Forms
                #endregion
        }
 }
-#endif