2006-12-26 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolBar.cs
index 25b7aecc4f53a7004ad0ba3f7a657e2345d946f4..27af4c0709d50b80e7c35b41467e99ad2b4269e4 100644 (file)
@@ -52,6 +52,9 @@ namespace System.Windows.Forms
                #endregion Instance Variables
 
                #region Events
+               static object ButtonClickEvent = new object ();
+               static object ButtonDropDownEvent = new object ();
+
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Never)]
                public new event EventHandler BackColorChanged {
@@ -66,8 +69,15 @@ namespace System.Windows.Forms
                        remove { base.BackgroundImageChanged -= value; }
                }
 
-               public event ToolBarButtonClickEventHandler ButtonClick;
-               public event ToolBarButtonClickEventHandler ButtonDropDown;
+               public event ToolBarButtonClickEventHandler ButtonClick {
+                       add { Events.AddHandler (ButtonClickEvent, value); }
+                       remove {Events.RemoveHandler (ButtonClickEvent, value); }
+               }
+
+               public event ToolBarButtonClickEventHandler ButtonDropDown {
+                       add { Events.AddHandler (ButtonDropDownEvent, value); }
+                       remove {Events.RemoveHandler (ButtonDropDownEvent, value); }
+               }
 
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Never)]
@@ -120,6 +130,7 @@ namespace System.Windows.Forms
                        MouseLeave += new EventHandler (ToolBar_MouseLeave);
                        MouseMove += new MouseEventHandler (ToolBar_MouseMove);
                        MouseUp += new MouseEventHandler (ToolBar_MouseUp);
+                       BackgroundImageChanged += new EventHandler (ToolBar_BackgroundImageChanged);
 
                        TabStop = false;
                        
@@ -163,7 +174,7 @@ namespace System.Windows.Forms
 
                [DefaultValue (true)]
                [Localizable (true)]
-               public bool AutoSize {
+               public new bool AutoSize {
                        get { return autosize; }
                        set {
                                if (value == autosize)
@@ -191,15 +202,8 @@ namespace System.Windows.Forms
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Never)]
                public override Image BackgroundImage {
-                       get { return background_image; }
-                       set {
-                               if (value == background_image)
-                                       return;
-
-                               background_image = value;
-                               OnBackgroundImageChanged (EventArgs.Empty);
-                               Redraw (false);
-                       }
+                       get { return base.BackgroundImage; }
+                       set { base.BackgroundImage = value; }
                }
 
                [DefaultValue (BorderStyle.None)]
@@ -320,7 +324,9 @@ namespace System.Windows.Forms
                        }
                }
 
-               ImeMode ime_mode;
+               // XXX this should probably go away and it should call
+               // into Control.ImeMode instead.
+               new ImeMode ime_mode = ImeMode.Disable;
 
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Never)]
@@ -348,7 +354,8 @@ namespace System.Windows.Forms
                        }
                }
 
-               bool show_tooltips = false;
+               // Default value is "false" but after make a test in .NET we get "true" result as default.  
+               bool show_tooltips = true;
 
                [DefaultValue (false)]
                [Localizable (true)]
@@ -368,14 +375,13 @@ namespace System.Windows.Forms
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [EditorBrowsable (EditorBrowsableState.Never)]
                public override string Text {
-                       get { return text; } 
+                       get { return base.Text; } 
                        set {
-                               if (value == text)
+                               if (value == base.Text)
                                        return;
 
-                               text = value;
+                               base.Text = value;
                                Redraw (true);
-                               OnTextChanged (EventArgs.Empty);
                        }
                }
 
@@ -449,14 +455,16 @@ namespace System.Windows.Forms
 
                        e.Button.Invalidate ();
 
-                       if (ButtonClick != null)
-                               ButtonClick (this, e);
+                       ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonClickEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnButtonDropDown (ToolBarButtonClickEventArgs e) 
                {
-                       if (ButtonDropDown != null)
-                               ButtonDropDown (this, e);
+                       ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonDropDownEvent]);
+                       if (eh != null)
+                               eh (this, e);
 
                        if (e.Button.DropDownMenu == null)
                                return;
@@ -486,14 +494,15 @@ namespace System.Windows.Forms
                        if (Width <= 0 || Height <= 0 || !Visible)
                                return;
 
-                       Redraw (true, background_image != null);
+                       Redraw (true, BackgroundImage != null);
                }
 
                int requested_height = -1;
 
                protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
                {
-                       if ((specified & BoundsSpecified.Height) != 0)
+                       // New height requested
+                       if (!AutoSize && (requested_height != height) && ((specified & BoundsSpecified.Height) != BoundsSpecified.None)) 
                                requested_height = height;
                        
                        base.SetBoundsCore (x, y, width, height, specified);
@@ -589,6 +598,11 @@ namespace System.Windows.Forms
                        (enabled [next] as ToolBarButton).Hilight = true;
                }
 
+               private void ToolBar_BackgroundImageChanged (object sender, EventArgs args)
+               {
+                       Redraw (false);
+               }
+
                private void ToolBar_MouseDown (object sender, MouseEventArgs me)
                {
                        if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
@@ -803,7 +817,7 @@ namespace System.Windows.Forms
                {
                        bool invalidate = true;
                        if (recalculate) {
-                               invalidate = Layout ();
+                               invalidate = LayoutToolBar ();
                        }
 
                        if (force || invalidate)
@@ -873,81 +887,61 @@ namespace System.Windows.Forms
                        }
                }
 
-               bool Layout ()
+               bool LayoutToolBar ()
                {
                        bool changed = false;
                        Theme theme = ThemeEngine.Current;
                        int x = theme.ToolBarGripWidth;
                        int y = theme.ToolBarGripWidth;
 
-                       Size button_size = AdjustedButtonSize;
-
-                       int ht = button_size.Height + theme.ToolBarGripWidth;
+                       Size adjusted_size = AdjustedButtonSize;
+                       int ht = adjusted_size.Height + theme.ToolBarGripWidth;
 
-                       if (Wrappable && Parent != null) {
-                               int separator_index = -1;
+                       int separator_index = -1;
 
-                               for (int i = 0; i < buttons.Count; i++) {
-                                       ToolBarButton button = buttons [i];
+                       for (int i = 0; i < buttons.Count; i++) {
+                               ToolBarButton button = buttons [i];
 
-                                       if (!button.Visible)
-                                               continue;
-
-                                       if (size_specified && (button.Style != ToolBarButtonStyle.Separator)) {
-                                               if (button.Layout (button_size))
-                                                       changed = true;
-                                       }
-                                       else {
-                                               if (button.Layout ())
-                                                       changed = true;
-                                       }
+                               if (!button.Visible)
+                                       continue;
 
-                                       bool is_separator = button.Style == ToolBarButtonStyle.Separator;
-
-                                       if (x + button.Rectangle.Width < Width || is_separator) {
-                                               if (button.Location.X != x || button.Location.Y != y)
-                                                       changed = true;
-                                               button.Location = new Point (x, y);
-                                               x += button.Rectangle.Width;
-                                               if (is_separator)
-                                                       separator_index = i;
-                                       } else if (separator_index > 0) { 
-                                               i = separator_index;
-                                               separator_index = -1;
-                                               x = theme.ToolBarGripWidth;
-                                               y += ht; 
-                                       } else {
-                                               x = theme.ToolBarGripWidth;
-                                               y += ht; 
-                                               if (button.Location.X != x || button.Location.Y != y)
-                                                       changed = true;
-                                               button.Location = new Point (x, y);
-                                               x += button.Rectangle.Width;
-                                       }
-                               }
-                               if (AutoSize)
-                                       Height = y + ht;
-                       } else {
-                               if (AutoSize)
-                                       Height = ht;
+                               if (size_specified && (button.Style != ToolBarButtonStyle.Separator))
+                                       changed = button.Layout (adjusted_size);
                                else
-                                       Height = DefaultSize.Height;
-                               foreach (ToolBarButton button in buttons) {
-                                       if (size_specified) {
-                                               if (button.Layout (button_size))
-                                                       changed = true;
-                                       }
-                                       else {
-                                               if (button.Layout ())
-                                                       changed = true;
-                                       }
+                                       changed = button.Layout ();
+                               
+                               bool is_separator = button.Style == ToolBarButtonStyle.Separator;
+
+                               if (x + button.Rectangle.Width < Width || is_separator || !Wrappable) {
+                                       if (button.Location.X != x || button.Location.Y != y)
+                                               changed = true;
+                                       button.Location = new Point (x, y);
+                                       x += button.Rectangle.Width;
+                                       if (is_separator)
+                                               separator_index = i;
+                               } else if (separator_index > 0) {
+                                       i = separator_index;
+                                       separator_index = -1;
+                                       x = theme.ToolBarGripWidth;
+                                       y += ht; 
+                               } else {
+                                       x = theme.ToolBarGripWidth;
+                                       y += ht; 
                                        if (button.Location.X != x || button.Location.Y != y)
                                                changed = true;
                                        button.Location = new Point (x, y);
                                        x += button.Rectangle.Width;
                                }
                        }
-
+                       
+                       if (Parent == null)
+                               return changed;
+                       
+                       if (AutoSize)
+                               Height = ht + (Wrappable ? y : 0);
+                       else
+                               Height = requested_height;
+                       
                        return changed;
                }
                #endregion Private Methods