Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolBar.cs
index f7ded597f9016da6c52636fa36efaf69b2c43c4b..37e8f29eb2a949a1e9188dec9478ed0c69143087 100644 (file)
@@ -37,10 +37,8 @@ using System.Runtime.InteropServices;
 
 namespace System.Windows.Forms
 {
-#if NET_2_0
        [ComVisible (true)]
        [ClassInterface (ClassInterfaceType.AutoDispatch)]
-#endif
        [DefaultEvent ("ButtonClick")]
        [DefaultProperty ("Buttons")]
        [Designer ("System.Windows.Forms.Design.ToolBarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
@@ -57,14 +55,12 @@ namespace System.Windows.Forms
                static object ButtonClickEvent = new object ();
                static object ButtonDropDownEvent = new object ();
 
-#if NET_2_0
                [Browsable (true)]
                [EditorBrowsable (EditorBrowsableState.Always)]
                public new event EventHandler AutoSizeChanged {
                        add { base.AutoSizeChanged += value; }
                        remove { base.AutoSizeChanged -= value; }
                }
-#endif
 
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Never)]
@@ -80,14 +76,12 @@ namespace System.Windows.Forms
                        remove { base.BackgroundImageChanged -= value; }
                }
 
-#if NET_2_0
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Never)]
                public new event EventHandler BackgroundImageLayoutChanged {
                        add { base.BackgroundImageLayoutChanged += value; }
                        remove { base.BackgroundImageLayoutChanged -= value; }
                }
-#endif
 
                public event ToolBarButtonClickEventHandler ButtonClick {
                        add { Events.AddHandler (ButtonClickEvent, value); }
@@ -181,13 +175,11 @@ namespace System.Windows.Forms
                        get { return ThemeEngine.Current.ToolBarDefaultSize; }
                }
 
-#if NET_2_0
                [EditorBrowsable (EditorBrowsableState.Never)]
                protected override bool DoubleBuffered {
                        get { return base.DoubleBuffered; }
                        set { base.DoubleBuffered = value; }
                }
-#endif
                #endregion
 
                ToolBarAppearance appearance = ToolBarAppearance.Normal;
@@ -208,18 +200,12 @@ namespace System.Windows.Forms
 
                bool autosize = true;
 
-#if NET_2_0
                [Browsable (true)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
                [EditorBrowsable (EditorBrowsableState.Always)]
-#endif
                [DefaultValue (true)]
                [Localizable (true)]
-               public 
-#if NET_2_0
-               override
-#endif
-               bool AutoSize {
+               public override bool AutoSize {
                        get { return autosize; }
                        set {
                                if (value == autosize)
@@ -253,14 +239,12 @@ namespace System.Windows.Forms
                        set { base.BackgroundImage = value; }
                }
 
-#if NET_2_0
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Never)]
                public override ImageLayout BackgroundImageLayout {
                        get { return base.BackgroundImageLayout; }
                        set { base.BackgroundImageLayout = value; }
                }
-#endif
 
                [DefaultValue (BorderStyle.None)]
                [DispIdAttribute (-504)]
@@ -524,8 +508,31 @@ namespace System.Windows.Forms
                        base.Dispose (disposing);
                }
 
-               protected virtual void OnButtonClick (ToolBarButtonClickEventArgs e)
+               private ToolBarButton button_for_focus = null;
+               
+               internal void UIAPerformClick (ToolBarButton button)
                {
+                       ToolBarItem previous_item = current_item;
+                       current_item = null;
+                       
+                       foreach (ToolBarItem item in items)
+                               if (item.Button == button) {
+                                       current_item = item;
+                                       break;
+                               }
+
+                       try {
+                               if (current_item == null)
+                                       throw new ArgumentException ("button", "The button specified is not part of this toolbar");
+                               PerformButtonClick (new ToolBarButtonClickEventArgs (button));
+                       } finally {
+                               current_item = previous_item;
+                       }
+               }
+               
+               void PerformButtonClick (ToolBarButtonClickEventArgs e)
+               {
+                       // Only change pushed for ToogleButton
                        if (e.Button.Style == ToolBarButtonStyle.ToggleButton) {
                                if (! e.Button.Pushed)
                                        e.Button.Pushed = true;
@@ -536,6 +543,14 @@ namespace System.Windows.Forms
                        current_item.Pressed = false;
                        current_item.Invalidate ();
                        
+                       button_for_focus = current_item.Button;
+                       button_for_focus.UIAHasFocus = true;
+
+                       OnButtonClick (e);
+               }
+
+               protected virtual void OnButtonClick (ToolBarButtonClickEventArgs e)
+               {                       
                        ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonClickEvent]);
                        if (eh != null)
                                eh (this, e);
@@ -550,10 +565,13 @@ namespace System.Windows.Forms
                        if (e.Button.DropDownMenu == null)
                                return;
 
-                       ToolBarItem item = current_item;
+                       ShowDropDownMenu (current_item);
+               }
 
+               internal void ShowDropDownMenu (ToolBarItem item)
+               {
                        Point loc = new Point (item.Rectangle.X + 1, item.Rectangle.Bottom + 1);
-                       ((ContextMenu) e.Button.DropDownMenu).Show (this, loc);
+                       ((ContextMenu) item.Button.DropDownMenu).Show (this, loc);
 
                        item.DDPressed = false;
                        item.Hilight = false;
@@ -577,7 +595,6 @@ namespace System.Windows.Forms
                        LayoutToolBar ();
                }
 
-#if NET_2_0
                protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
                {
                        specified &= ~BoundsSpecified.Height;
@@ -592,7 +609,6 @@ namespace System.Windows.Forms
                        
                        base.ScaleCore (dx, dy);
                }
-#endif
 
                private int requested_size = -1;
 
@@ -618,7 +634,7 @@ namespace System.Windows.Forms
                {
                        if (msg.Msg == (int)Msg.WM_KEYDOWN) {
                                Keys key_data = (Keys)msg.WParam.ToInt32();
-                               if (HandleKeyDown (key_data))
+                               if (HandleKeyDown (ref msg, key_data))
                                        return true;
                        } 
                        return base.InternalPreProcessMessage (ref msg);
@@ -627,8 +643,28 @@ namespace System.Windows.Forms
                #endregion Protected Methods
 
                #region Private Methods
+               internal int CurrentItem {
+                       get {
+                               return Array.IndexOf (items, current_item);
+                       }
+                       set {
+                               if (current_item != null)
+                                       current_item.Hilight = false;
+
+                               current_item = value == -1 ? null : items [value];
+
+                               if (current_item != null)
+                                       current_item.Hilight = true;
+                       }
+
+               }
+
                private void FocusChanged (object sender, EventArgs args)
                {
+                       if (!Focused && button_for_focus != null)
+                               button_for_focus.UIAHasFocus = false;
+                       button_for_focus = null;
+                       
                        if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
                                return;
 
@@ -652,11 +688,15 @@ namespace System.Windows.Forms
                        }
                }
 
-               private bool HandleKeyDown (Keys key_data)
+               private bool HandleKeyDown (ref Message msg, Keys key_data)
                {
                        if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
                                return false;
 
+                       // Handle the key as needed if the current item is a dropdownbutton.
+                       if (HandleKeyOnDropDown (ref msg, key_data))
+                               return true;
+
                        switch (key_data) {
                                case Keys.Left:
                                case Keys.Up:
@@ -666,9 +706,39 @@ namespace System.Windows.Forms
                                case Keys.Down:
                                        HighlightButton (1);
                                        return true;
-                               default:
-                                       return false;
+                               case Keys.Enter:
+                               case Keys.Space:
+                                       if (current_item != null) {
+                                               OnButtonClick (new ToolBarButtonClickEventArgs (current_item.Button));
+                                               return true;
+                                       }
+                                       break;
                        }
+
+                       return false;
+               }
+
+               bool HandleKeyOnDropDown (ref Message msg, Keys key_data)
+               {
+                       if (current_item == null || current_item.Button.Style != ToolBarButtonStyle.DropDownButton ||
+                                       current_item.Button.DropDownMenu == null)
+                               return false;
+
+                       Menu dropdown_menu = current_item.Button.DropDownMenu;
+
+                       if (dropdown_menu.Tracker.active) {
+                               dropdown_menu.ProcessCmdKey (ref msg, key_data);
+                               return true; // always true if the menu is active
+                       }
+
+                       if (key_data == Keys.Up || key_data == Keys.Down) {
+                               current_item.DDPressed = true;
+                               current_item.Invalidate ();
+                               OnButtonDropDown (new ToolBarButtonClickEventArgs (current_item.Button));
+                               return true;
+                       }
+
+                       return false;
                }
 
                void HighlightButton (int offset)
@@ -698,7 +768,9 @@ namespace System.Windows.Forms
 
                        if (curr_item != null)
                                curr_item.Hilight = false;
-                       (enabled [next] as ToolBarItem).Hilight = true;
+
+                       current_item = enabled [next] as ToolBarItem;
+                       current_item.Hilight = true;
                }
 
                private void ToolBar_BackgroundImageChanged (object sender, EventArgs args)
@@ -776,7 +848,7 @@ namespace System.Windows.Forms
                                        // Fire a ButtonClick
                                        current_item = item;
                                        if ((item.Pressed) && ((me.Button & MouseButtons.Left) == MouseButtons.Left))
-                                               OnButtonClick (new ToolBarButtonClickEventArgs (item.Button));
+                                               PerformButtonClick (new ToolBarButtonClickEventArgs (item.Button));
                                } else if (item.Pressed) {
                                        item.Pressed = false;
                                        item.Invalidate ();
@@ -1111,7 +1183,6 @@ namespace System.Windows.Forms
                        #endregion
 
                        #region UIA Framework Events
-#if NET_2_0
                        static object UIACollectionChangedEvent = new object ();
                        
                        internal event CollectionChangeEventHandler UIACollectionChanged {
@@ -1126,7 +1197,6 @@ namespace System.Windows.Forms
                                if (eh != null)
                                        eh (owner, e);
                        }
-#endif
                        #endregion
 
                        #region constructors
@@ -1151,24 +1221,18 @@ namespace System.Windows.Forms
                        public virtual ToolBarButton this [int index] {
                                get { return (ToolBarButton) list [index]; }
                                set {
-#if NET_2_0
                                        // UIA Framework Event: Button Removed
                                        OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
-#endif
 
                                        value.SetParent (owner);
                                        list [index] = value;
                                        owner.Redraw (true);
 
-#if NET_2_0
-
                                // UIA Framework Event: Button Added
                                OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
-#endif
                                }
                        }
 
-#if NET_2_0
                        public virtual ToolBarButton this[string key] {
                                get {
                                        if (string.IsNullOrEmpty (key))
@@ -1181,7 +1245,6 @@ namespace System.Windows.Forms
                                        return null;
                                }
                        }
-#endif
 
                        bool ICollection.IsSynchronized {
                                get { return list.IsSynchronized; }
@@ -1220,10 +1283,8 @@ namespace System.Windows.Forms
                                if (redraw)
                                        owner.Redraw (true);
 
-#if NET_2_0
                                // UIA Framework Event: Button Added
                                OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, result));
-#endif
 
                                return result;
                        }
@@ -1246,10 +1307,8 @@ namespace System.Windows.Forms
                                list.Clear ();
                                owner.Redraw (false);
 
-#if NET_2_0
                                // UIA Framework Event: Button Cleared
                                OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, -1));
-#endif
                        }
 
                        public bool Contains (ToolBarButton button)
@@ -1257,12 +1316,10 @@ namespace System.Windows.Forms
                                return list.Contains (button);
                        }
 
-#if NET_2_0
                        public virtual bool ContainsKey (string key)
                        {
                                return !(this[key] == null);
                        }
-#endif
 
                        public IEnumerator GetEnumerator ()
                        {
@@ -1324,22 +1381,18 @@ namespace System.Windows.Forms
                                return list.IndexOf (button);
                        }
 
-#if NET_2_0
                        public virtual int IndexOfKey (string key)
                        {
                                return IndexOf (this[key]);
                        }
-#endif
 
                        public void Insert (int index, ToolBarButton button)
                        {
                                list.Insert (index, button);
                                owner.Redraw (true);
 
-#if NET_2_0
                                // UIA Framework Event: Button Added
                                OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
-#endif
                        }
 
                        public void Remove (ToolBarButton button)
@@ -1353,18 +1406,14 @@ namespace System.Windows.Forms
                                list.RemoveAt (index);
                                owner.Redraw (true);
 
-#if NET_2_0
                                // UIA Framework Event: Button Removed
                                OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
-#endif
                        }
 
-#if NET_2_0
                        public virtual void RemoveByKey (string key)
                        {
                                Remove (this[key]);
                        }
-#endif
                        #endregion methods
                }