[jit] Enable partial generic sharing when not using AOT as an experiment.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripDropDown.cs
index 8dfbacf206148f1fd12676080a75586183bf7bee..f47ca8a835bcd186338a184e8bc065abd72cf3ab 100644 (file)
@@ -26,7 +26,6 @@
 //     Jonathan Pobst (monkey@jpobst.com)
 //
 
-#if NET_2_0
 using System.Drawing;
 using System.Runtime.InteropServices;
 using System.ComponentModel;
@@ -232,9 +231,12 @@ namespace System.Windows.Forms
                        get { return this.owner_item; }
                        set { this.owner_item = value; 
                                
-                               if (this.owner_item != null)
+                               if (this.owner_item != null) {
                                        if (this.owner_item.Owner != null && this.owner_item.Owner.RenderMode != ToolStripRenderMode.ManagerRenderMode)
                                                this.Renderer = this.owner_item.Owner.Renderer;
+
+                                       Font = owner_item.Font;
+                               }
                        }
                }
 
@@ -387,7 +389,8 @@ namespace System.Windows.Forms
                {
                        if (control == null)
                                throw new ArgumentNullException ("control");
-                               
+                       
+                       XplatUI.SetOwner (Handle, control.Handle);
                        Show (control.PointToScreen (position), DefaultDropDownDirection);
                }
                
@@ -401,7 +404,7 @@ namespace System.Windows.Forms
                        this.PerformLayout ();
                        
                        Point show_point = position;
-                       Point max_screen = new Point (SystemInformation.WorkingArea.Width, SystemInformation.WorkingArea.Height);
+                       Point max_screen = new Point (SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
                        
                        if (this is ContextMenuStrip) {
                                // If we are going to go offscreen, adjust our direction so we don't...
@@ -445,20 +448,20 @@ namespace System.Windows.Forms
                                                        direction = ToolStripDropDownDirection.BelowRight;
                                                break;
                                        case ToolStripDropDownDirection.BelowLeft:
-                                               if (show_point.Y + this.Height > max_screen.Y)
+                                               if (show_point.Y + this.Height > max_screen.Y && show_point.Y - this.Height > 0)
                                                        direction = ToolStripDropDownDirection.AboveLeft;
                                                break;
                                        case ToolStripDropDownDirection.BelowRight:
                                        case ToolStripDropDownDirection.Default:
-                                               if (show_point.Y + this.Height > max_screen.Y)
+                                               if (show_point.Y + this.Height > max_screen.Y && show_point.Y - this.Height > 0)
                                                        direction = ToolStripDropDownDirection.AboveRight;
                                                break;
                                        case ToolStripDropDownDirection.Left:
-                                               if (show_point.Y + this.Height > max_screen.Y)
+                                               if (show_point.Y + this.Height > max_screen.Y && show_point.Y - this.Height > 0)
                                                        direction = ToolStripDropDownDirection.AboveLeft;
                                                break;
                                        case ToolStripDropDownDirection.Right:
-                                               if (show_point.Y + this.Height > max_screen.Y)
+                                               if (show_point.Y + this.Height > max_screen.Y && show_point.Y - this.Height > 0)
                                                        direction = ToolStripDropDownDirection.AboveRight;
                                                break;
                                }
@@ -482,12 +485,18 @@ namespace System.Windows.Forms
                                        break;
                        }
 
-                       // Fix offscreen positions
+                       // Fix offscreen horizontal positions
                        if ((show_point.X + this.Width) > max_screen.X)
                                show_point.X = max_screen.X - this.Width;
                        if (show_point.X < 0)
                                show_point.X = 0;
-                       
+
+                       // Fix offscreen vertical positions
+                       if ((show_point.Y + this.Height) > max_screen.Y)
+                               show_point.Y = max_screen.Y - this.Height;
+                       if (show_point.Y < 0)
+                               show_point.Y = 0;
+
                        if (this.Location != show_point)
                                this.Location = show_point;
 
@@ -521,6 +530,7 @@ namespace System.Windows.Forms
                        if (control == null)
                                throw new ArgumentNullException ("control");
 
+                       XplatUI.SetOwner (Handle, control.Handle);
                        Show (control.PointToScreen (position), direction);
                }
                #endregion
@@ -575,7 +585,7 @@ namespace System.Windows.Forms
 
                protected override void OnLayout (LayoutEventArgs e)
                {
-                       // Find the widest menu item
+                       // Find the widest menu item, so we know how wide to make our dropdown
                        int widest = 0;
 
                        foreach (ToolStripItem tsi in this.Items) {
@@ -584,12 +594,13 @@ namespace System.Windows.Forms
                                        
                                tsi.SetPlacement (ToolStripItemPlacement.Main);
                                
-                               if (tsi.GetPreferredSize (Size.Empty).Width > widest)
-                                       widest = tsi.GetPreferredSize (Size.Empty).Width;
+                               widest = Math.Max (widest, tsi.GetPreferredSize (Size.Empty).Width + tsi.Margin.Horizontal);
                        }
                        
+                       // Add any padding our dropdown has set
+                       widest += this.Padding.Horizontal;
+                       
                        int x = this.Padding.Left;
-                       widest += 68 - this.Padding.Horizontal;
                        int y = this.Padding.Top;
 
                        foreach (ToolStripItem tsi in this.Items) {
@@ -600,16 +611,20 @@ namespace System.Windows.Forms
 
                                int height = 0;
 
-                               if (tsi is ToolStripSeparator)
+                               Size preferred_size = tsi.GetPreferredSize (Size.Empty);
+
+                               if (preferred_size.Height > 22)
+                                       height = preferred_size.Height;
+                               else if (tsi is ToolStripSeparator)
                                        height = 7;
                                else
                                        height = 22;
 
-                               tsi.SetBounds (new Rectangle (x, y, widest, height));
-                               y += tsi.Height + tsi.Margin.Bottom;
+                               tsi.SetBounds (new Rectangle (x, y, preferred_size.Width, height));
+                               y += height + tsi.Margin.Bottom;
                        }
 
-                       this.Size = new Size (widest + this.Padding.Horizontal, y + this.Padding.Bottom);// + 2);
+                       this.Size = new Size (widest, y + this.Padding.Bottom);
                        this.SetDisplayedItems ();
                        this.OnLayoutCompleted (EventArgs.Empty);
                        this.Invalidate ();
@@ -645,6 +660,14 @@ namespace System.Windows.Forms
                protected override void OnVisibleChanged (EventArgs e)
                {
                        base.OnVisibleChanged (e);
+
+                       if (owner_item != null && owner_item is ToolStripDropDownItem) {
+                               ToolStripDropDownItem dropdown_owner = (ToolStripDropDownItem)owner_item;
+                               if (Visible)
+                                       dropdown_owner.OnDropDownOpened (EventArgs.Empty);
+                               else
+                                       dropdown_owner.OnDropDownClosed (EventArgs.Empty);
+                       }
                }
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]
@@ -910,6 +933,13 @@ namespace System.Windows.Forms
                {
                        this.Close (reason);
                        base.Dismiss (reason);
+
+                       // ContextMenuStrip won't have a parent
+                       if (this.OwnerItem == null)
+                               return;
+                       
+                       // Ensure Submenu loes keyboard capture when closing.
+                       ToolStripManager.SetActiveToolStrip (null, false);                      
                }
 
                internal override ToolStrip GetTopLevelToolStrip ()
@@ -938,6 +968,10 @@ namespace System.Windows.Forms
                                case Keys.Escape:
                                        this.Dismiss (ToolStripDropDownCloseReason.Keyboard);
                                        
+                                       // ContextMenuStrip won't have a parent
+                                       if (this.OwnerItem == null)
+                                               return true;
+                                               
                                        ToolStrip parent_strip = this.OwnerItem.Parent;
                                        ToolStripManager.SetActiveToolStrip (parent_strip, true);
                                        
@@ -1021,4 +1055,3 @@ namespace System.Windows.Forms
                #endregion
        }
 }
-#endif