2006-12-26 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolBar.cs
index aa5b1abbf2dd9871e9923a2c6c830db8f2fd7bd5..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)]
@@ -113,12 +123,17 @@ namespace System.Windows.Forms
                        buttons = new ToolBarButtonCollection (this);
                        dock_style = DockStyle.Top;
                        
+                       GotFocus += new EventHandler (FocusChanged);
+                       LostFocus += new EventHandler (FocusChanged);
                        MouseDown += new MouseEventHandler (ToolBar_MouseDown);
+                       MouseHover += new EventHandler (ToolBar_MouseHover);
                        MouseLeave += new EventHandler (ToolBar_MouseLeave);
                        MouseMove += new MouseEventHandler (ToolBar_MouseMove);
                        MouseUp += new MouseEventHandler (ToolBar_MouseUp);
-                       Paint += new PaintEventHandler (ToolBar_Paint);
+                       BackgroundImageChanged += new EventHandler (ToolBar_BackgroundImageChanged);
 
+                       TabStop = false;
+                       
                        SetStyle (ControlStyles.UserPaint, false);
                        SetStyle (ControlStyles.FixedHeight, true);
                }
@@ -151,7 +166,7 @@ namespace System.Windows.Forms
                                        return;
 
                                appearance = value;
-                               Redraw (false);
+                               Redraw (true);
                        }
                }
 
@@ -159,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)
@@ -187,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)]
@@ -222,14 +230,17 @@ namespace System.Windows.Forms
                        get {
                                if (button_size.IsEmpty) {
                                        if (buttons.Count == 0)
-                                               return new Size (39, 36);
+                                               return new Size (24, 22);
+                                       Size result = CalcButtonSize ();
+                                       if (result.IsEmpty)
+                                               return new Size (24, 22);
                                        else
-                                               return CalcButtonSize ();
+                                               return result;
                                }
                                return button_size;
                        }
                        set {
-                               size_specified = true;
+                               size_specified = value != Size.Empty;
                                if (button_size == value)
                                        return;
 
@@ -259,7 +270,7 @@ namespace System.Windows.Forms
                        set { base.Dock = value; } 
                }
 
-               bool drop_down_arrows = false;
+               bool drop_down_arrows = true;
 
                [DefaultValue (false)]
                [Localizable (true)]
@@ -293,7 +304,12 @@ namespace System.Windows.Forms
                [DefaultValue (null)]
                public ImageList ImageList {
                        get { return image_list; }
-                       set { image_list = value; }
+                       set { 
+                               if (image_list == value)
+                                       return;
+                               image_list = value;
+                               Redraw (true);
+                       }
                }
 
                [Browsable (false)]
@@ -308,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)]
@@ -336,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)]
@@ -356,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);
                        }
                }
 
@@ -404,9 +422,9 @@ namespace System.Windows.Forms
                        int count = this.Buttons.Count;
 
                        if (count == 0)
-                               return string.Format ("System.Windows.Forms.ToolBar, Button.Count: 0");
+                               return string.Format ("System.Windows.Forms.ToolBar, Buttons.Count: 0");
                        else
-                               return string.Format ("System.Windows.Forms.ToolBar, Button.Count: {0}, Buttons[0]: {1}",
+                               return string.Format ("System.Windows.Forms.ToolBar, Buttons.Count: {0}, Buttons[0]: {1}",
                                                      count, this.Buttons [0].ToString ());
                }
                #endregion Public Methods
@@ -435,17 +453,18 @@ namespace System.Windows.Forms
                        }
                        e.Button.pressed = false;
 
-                       Invalidate (e.Button.Rectangle);
-                       Redraw (false);
+                       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;
@@ -475,11 +494,17 @@ namespace System.Windows.Forms
                        if (Width <= 0 || Height <= 0 || !Visible)
                                return;
 
-                       Redraw (true);
+                       Redraw (true, BackgroundImage != null);
                }
 
+               int requested_height = -1;
+
                protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
                {
+                       // New height requested
+                       if (!AutoSize && (requested_height != height) && ((specified & BoundsSpecified.Height) != BoundsSpecified.None)) 
+                               requested_height = height;
+                       
                        base.SetBoundsCore (x, y, width, height, specified);
                }
 
@@ -488,16 +513,112 @@ namespace System.Windows.Forms
                        base.WndProc (ref m);
                }
 
+               internal override bool InternalPreProcessMessage (ref Message msg)
+               {
+                       if (msg.Msg == (int)Msg.WM_KEYDOWN) {
+                               Keys key_data = (Keys)msg.WParam.ToInt32();
+                               if (HandleKeyDown (key_data))
+                                       return true;
+                       } 
+                       return base.InternalPreProcessMessage (ref msg);
+               }
+                       
                #endregion Protected Methods
 
                #region Private Methods
+               private void FocusChanged (object sender, EventArgs args)
+               {
+                       if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
+                               return;
+
+                       ToolBarButton prelit = null;
+                       foreach (ToolBarButton b in Buttons)
+                               if (b.Hilight) {
+                                       prelit = b;
+                                       break;
+                               }
+
+                       if (Focused && prelit == null)
+                               foreach (ToolBarButton btn in Buttons) {
+                                       if (btn.Enabled) {
+                                               btn.Hilight = true;
+                                               break;
+                                       }
+                               }
+                       else if (prelit != null)
+                               prelit.Hilight = false;
+               }
+
+               private bool HandleKeyDown (Keys key_data)
+               {
+                       if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
+                               return false;
+
+                       switch (key_data) {
+                       case Keys.Left:
+                       case Keys.Up:
+                               HighlightButton (-1);
+                               return true;
+                       case Keys.Right:
+                       case Keys.Down:
+                               HighlightButton (1);
+                               return true;
+                       default:
+                               return false;
+                       }
+               }
+
+               void HighlightButton (int offset)
+               {
+                       ArrayList enabled = new ArrayList ();
+                       int count = 0;
+                       int start = -1;
+                       ToolBarButton curr_button = null;
+                       foreach (ToolBarButton btn in Buttons) {
+                               if (btn.Hilight) {
+                                       start = count;
+                                       curr_button = btn;
+                               }
+
+                               if (btn.Enabled) {
+                                       enabled.Add (btn);
+                                       count++;
+                               }
+                       }
+
+                       int next = (start + offset) % count;
+                       if (next < 0)
+                               next = count - 1;
+
+                       if (next == start)
+                               return;
+
+                       if (curr_button != null)
+                               curr_button.Hilight = false;
+                       (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) 
+                       if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
                                return;
 
                        Point loc = new Point (me.X, me.Y);
 
+                       if (ButtonAtPoint (loc) == null)
+                               return;
+                       
+                       // Hide tooltip when left mouse button 
+                       if ((tip_window != null) && (tip_window.Visible) && ((me.Button & MouseButtons.Left) == MouseButtons.Left)) {
+                               TipDownTimer.Stop ();
+                               tip_window.Hide ();
+                       }
+                       
                        // draw the pushed button
                        foreach (ToolBarButton button in buttons) {
                                if (button.Enabled && button.Rectangle.Contains (loc)) {
@@ -505,8 +626,11 @@ namespace System.Windows.Forms
                                        // We don't redraw the dropdown rect.
                                        if (button.Style == ToolBarButtonStyle.DropDownButton) {
                                                Rectangle rect = button.Rectangle;
-                                               rect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
-                                               rect.X = button.Rectangle.Right - rect.Width;
+                                               if (DropDownArrows) {
+                                                       rect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
+                                                       rect.X = button.Rectangle.Right - rect.Width;
+                                               }
+                                               
                                                if (rect.Contains (loc)) {
                                                        if (button.DropDownMenu != null) {
                                                                button.dd_pressed = true;
@@ -517,7 +641,7 @@ namespace System.Windows.Forms
                                        }
                                        button.pressed = true;
                                        button.inside = true;
-                                       Invalidate (button.Rectangle);
+                                       button.Invalidate ();
                                        break;
                                }
                        }
@@ -525,12 +649,14 @@ namespace System.Windows.Forms
 
                private void ToolBar_MouseUp (object sender, MouseEventArgs me)
                {
-                       if (!Enabled) 
+                       if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
                                return;
 
                        Point loc = new Point (me.X, me.Y);
 
                        // draw the normal button
+                       // Make a copy in case the list is modified during enumeration
+                       ArrayList buttons = new ArrayList (this.buttons);
                        foreach (ToolBarButton button in buttons) {
                                if (button.Enabled && button.Rectangle.Contains (loc)) {
                                        if (button.Style == ToolBarButtonStyle.DropDownButton) {
@@ -544,25 +670,75 @@ namespace System.Windows.Forms
                                                }
                                        }
                                        // Fire a ButtonClick
-                                       if (button.pressed)
+                                       if ((button.pressed) && ((me.Button & MouseButtons.Left) == MouseButtons.Left))
                                                OnButtonClick (new ToolBarButtonClickEventArgs (button));
                                } else if (button.pressed) {
                                        button.pressed = false;
-                                       Invalidate (button.Rectangle);
+                                       button.Invalidate ();
+                               }
+                       }
+               }
+
+               private ToolBarButton ButtonAtPoint (Point pt)
+               {
+                       foreach (ToolBarButton button in buttons)
+                               if (button.Rectangle.Contains (pt)) 
+                                       return button;
+
+                       return null;
+               }
+
+               ToolTip.ToolTipWindow tip_window = null;
+               Timer tipdown_timer = null;
+
+               private void PopDownTip (object o, EventArgs args)
+               {
+                       tip_window.Hide ();
+               }
+
+               private Timer TipDownTimer {
+                       get {
+                               if (tipdown_timer == null) {
+                                       tipdown_timer = new Timer ();
+                                       tipdown_timer.Enabled = false;
+                                       tipdown_timer.Interval = 5000;
+                                       tipdown_timer.Tick += new EventHandler (PopDownTip);
                                }
+                               return tipdown_timer;
                        }
                }
 
+               private void ToolBar_MouseHover (object sender, EventArgs e)
+               {
+                       if (Capture)
+                               return;
+
+                       if (tip_window == null)
+                               tip_window = new ToolTip.ToolTipWindow ();
+
+                       ToolBarButton btn = ButtonAtPoint (PointToClient (Control.MousePosition));
+                       current_button = btn;
+
+                       if (btn == null || btn.ToolTipText.Length == 0)
+                               return;
+
+                       tip_window.Present (this, btn.ToolTipText);
+                       TipDownTimer.Start ();
+               }
+
                private void ToolBar_MouseLeave (object sender, EventArgs e)
                {
-                       if (!Enabled || appearance != ToolBarAppearance.Flat || current_button == null) 
+                       if (tipdown_timer != null)
+                               tipdown_timer.Dispose ();
+                       tipdown_timer = null;
+                       if (tip_window != null)
+                               tip_window.Dispose ();
+                       tip_window = null;
+
+                       if (!Enabled || current_button == null) 
                                return;
 
-                       if (current_button.Hilight) {
-                               current_button.Hilight = false;
-                               Invalidate (current_button.Rectangle);
-                               Redraw (false);
-                       }
+                       current_button.Hilight = false;
                        current_button = null;
                }
 
@@ -571,9 +747,14 @@ namespace System.Windows.Forms
                        if (!Enabled) 
                                return;
 
+                       if (tip_window != null && tip_window.Visible) {
+                               TipDownTimer.Stop ();
+                               TipDownTimer.Start ();
+                       }
+
                        Point loc = new Point (me.X, me.Y);
 
-                       if (this.Capture) {
+                       if (Capture) {
                                // If the button was pressed and we leave, release the 
                                // button press and vice versa
                                foreach (ToolBarButton button in buttons) {
@@ -581,138 +762,187 @@ namespace System.Windows.Forms
                                            (button.inside != button.Rectangle.Contains (loc))) {
                                                button.inside = button.Rectangle.Contains (loc);
                                                button.Hilight = false;
-                                               Invalidate (button.Rectangle);
-                                               Redraw (false);
                                                break;
                                        }
                                }
-                       }
-                       // following is only for flat style toolbar
-                       else if (appearance == ToolBarAppearance.Flat) {
-                               if (current_button != null && current_button.Rectangle.Contains (loc)) {
-                                       if (current_button.Hilight || current_button.Pushed)
+                               return;
+                       } 
+
+                       if (current_button != null && current_button.Rectangle.Contains (loc)) {
+                               if (appearance == ToolBarAppearance.Flat) {
+                                       if (current_button.Hilight || current_button.Pushed || !current_button.Enabled)
                                                return;
                                        current_button.Hilight = true;
-                                       Invalidate (current_button.Rectangle);
-                                       Redraw (false);
                                }
-                               else {
+                       } else {
+                               if (tip_window != null) {
+                                       if (tip_window.Visible) {
+                                               tip_window.Hide ();
+                                               TipDownTimer.Stop ();
+                                       }
+                                       current_button = ButtonAtPoint (loc);
+                                       if (current_button != null && current_button.ToolTipText.Length > 0) {
+                                               tip_window.Present (this, current_button.ToolTipText);
+                                               TipDownTimer.Start ();
+                                       }
+                               }
+
+                               if (appearance == ToolBarAppearance.Flat) {
                                        foreach (ToolBarButton button in buttons) {
                                                if (button.Rectangle.Contains (loc) && button.Enabled) {
                                                        current_button = button;
                                                        if (current_button.Hilight || current_button.Pushed)
                                                                continue;
                                                        current_button.Hilight = true;
-                                                       Invalidate (current_button.Rectangle);
-                                                       Redraw (false);
                                                }
                                                else if (button.Hilight) {
                                                        button.Hilight = false;
-                                                       Invalidate (button.Rectangle);
-                                                       Redraw (false);
                                                }
                                        }
                                }
                        }
                }
 
-               private void ToolBar_Paint (object sender, PaintEventArgs pevent)
+               internal override void OnPaintInternal (PaintEventArgs pevent)
                {
                        ThemeEngine.Current.DrawToolBar (pevent.Graphics, pevent.ClipRectangle, this);
                }
 
                internal void Redraw (bool recalculate)
                {
-                       if (recalculate)
-                               Layout ();
+                       Redraw (recalculate, true);
+               }
+
+               internal void Redraw (bool recalculate, bool force)
+               {
+                       bool invalidate = true;
+                       if (recalculate) {
+                               invalidate = LayoutToolBar ();
+                       }
+
+                       if (force || invalidate)
+                               Invalidate ();
+               }
 
-                       Refresh ();
+               internal bool SizeSpecified {
+                       get { return size_specified; }
                }
 
+               const int text_padding = 3;
+
                private Size CalcButtonSize ()
                {
-                       String longestText = buttons [0].Text;
-                       for (int i = 1; i < buttons.Count; i++) {
-                               if (buttons[i].Text.Length > longestText.Length)
-                                       longestText = buttons[i].Text;
+                       if (Buttons.Count == 0)
+                               return Size.Empty;
+
+                       string longest_text = Buttons [0].Text;
+                       for (int i = 1; i < Buttons.Count; i++) {
+                               if (Buttons[i].Text.Length > longest_text.Length)
+                                       longest_text = Buttons[i].Text;
                        }
 
-                       SizeF sz = this.DeviceContext.MeasureString (longestText, this.Font);
-                       Size size = new Size ((int) Math.Ceiling (sz.Width), (int) Math.Ceiling (sz.Height));
+                       Size size = Size.Empty;
+                       if (longest_text != null && longest_text.Length > 0) {
+                               SizeF sz = DeviceContext.MeasureString (longest_text, Font);
+                               if (sz != SizeF.Empty)
+                                       size = new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, (int) Math.Ceiling (sz.Height));
+                       }
 
-                       if (ImageList != null) {
-                               // adjustment for the image grip 
-                               int imgWidth = this.ImageSize.Width + 2 * ThemeEngine.Current.ToolBarImageGripWidth; 
-                               int imgHeight = this.ImageSize.Height + 2 * ThemeEngine.Current.ToolBarImageGripWidth;
+                       Size img_size = ImageList == null ? new Size (16, 16) : ImageSize;
 
-                               if (text_alignment == ToolBarTextAlign.Right) {
-                                       size.Width = imgWidth + size.Width;
-                                       size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
-                               }
-                               else {
-                                       size.Height = imgHeight + size.Height;
-                                       size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
-                               }
+                       Theme theme = ThemeEngine.Current;
+                       int imgWidth = img_size.Width + 2 * theme.ToolBarImageGripWidth; 
+                       int imgHeight = img_size.Height + 2 * theme.ToolBarImageGripWidth;
+
+                       if (text_alignment == ToolBarTextAlign.Right) {
+                               size.Width = imgWidth + size.Width;
+                               size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
+                       } else {
+                               size.Height = imgHeight + size.Height;
+                               size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
                        }
+
+                       size.Width += theme.ToolBarImageGripWidth;
+                       size.Height += theme.ToolBarImageGripWidth;
                        return size;
                }
 
-               void Layout ()
+               // Flat toolbars disregard specified sizes.  Normal toolbars grow the
+               // button size to be at least large enough to show the image.
+               Size AdjustedButtonSize {
+                       get {
+                               Size size = ButtonSize;
+                               if (size_specified) {
+                                       if (Appearance == ToolBarAppearance.Flat)
+                                               size = CalcButtonSize ();
+                                       else {
+                                               int grip = ThemeEngine.Current.ToolBarImageGripWidth;
+                                               if (size.Width < ImageSize.Width + 2 * grip )
+                                                       size.Width = ImageSize.Width + 2 * grip;
+                                               if (size.Height < ImageSize.Height + 2 * grip)
+                                                       size.Height = ImageSize.Height + 2 * grip;
+                                       }
+                               }
+                               return size;
+                       }
+               }
+
+               bool LayoutToolBar ()
                {
+                       bool changed = false;
                        Theme theme = ThemeEngine.Current;
-                       int ht = ButtonSize.Height + theme.ToolBarGripWidth;
                        int x = theme.ToolBarGripWidth;
                        int y = theme.ToolBarGripWidth;
 
-                       if (Wrappable) {
-                               int separator_index = -1;
+                       Size adjusted_size = AdjustedButtonSize;
+                       int ht = adjusted_size.Height + theme.ToolBarGripWidth;
 
-                               for (int i = 0; i < buttons.Count; i++) {
-                                       ToolBarButton button = buttons [i];
+                       int separator_index = -1;
 
-                                       if (!button.Visible)
-                                               continue;
+                       for (int i = 0; i < buttons.Count; i++) {
+                               ToolBarButton button = buttons [i];
 
-                                       if (size_specified)
-                                               button.Layout (ButtonSize);
-                                       else
-                                               button.Layout ();
-
-                                       bool is_separator = button.Style == ToolBarButtonStyle.Separator;
-
-                                       if (x + button.Rectangle.Width < Width || is_separator) {
-                                               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; 
-                                               button.Location = new Point (x, y);
-                                               x += button.Rectangle.Width;
-                                       }
-                               }
-                               if (AutoSize)
-                                       Height = y + ht;
-                       } else {
-                               if (AutoSize)
-                                       Height = ht;
+                               if (!button.Visible)
+                                       continue;
+
+                               if (size_specified && (button.Style != ToolBarButtonStyle.Separator))
+                                       changed = button.Layout (adjusted_size);
                                else
-                                       Height = DefaultSize.Height;
-                               foreach (ToolBarButton button in buttons) {
-                                       if (size_specified)
-                                               button.Layout (ButtonSize);
-                                       else
-                                               button.Layout ();
+                                       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
 
@@ -722,6 +952,7 @@ namespace System.Windows.Forms
                        #region instance variables
                        private ArrayList list;
                        private ToolBar owner;
+                       private bool redraw;
                        #endregion
 
                        #region constructors
@@ -729,16 +960,17 @@ namespace System.Windows.Forms
                        {
                                this.owner = owner;
                                list = new ArrayList ();
+                               redraw = true;
                        }
                        #endregion
 
                        #region properties
                        [Browsable (false)]
-                       public virtual int Count {
+                       public int Count {
                                get { return list.Count; }
                        }
 
-                       public virtual bool IsReadOnly {
+                       public bool IsReadOnly {
                                get { return list.IsReadOnly; }
                        }
 
@@ -785,17 +1017,25 @@ namespace System.Windows.Forms
                                int result;
                                button.SetParent (owner);
                                result = list.Add (button);
-                               owner.Redraw (true);
+                               if (redraw)
+                                       owner.Redraw (true);
                                return result;
                        }
 
                        public void AddRange (ToolBarButton [] buttons)
                        {
-                               foreach (ToolBarButton button in buttons)
-                                       Add (button);
+                               try {
+                                       redraw = false;
+                                       foreach (ToolBarButton button in buttons)
+                                               Add (button);
+                               }
+                               finally {
+                                       redraw = true;
+                                       owner.Redraw (true);
+                               }
                        }
 
-                       public virtual void Clear ()
+                       public void Clear ()
                        {
                                list.Clear ();
                                owner.Redraw (false);
@@ -806,7 +1046,7 @@ namespace System.Windows.Forms
                                return list.Contains (button);
                        }
 
-                       public virtual IEnumerator GetEnumerator ()
+                       public IEnumerator GetEnumerator ()
                        {
                                return list.GetEnumerator ();
                        }
@@ -878,7 +1118,7 @@ namespace System.Windows.Forms
                                owner.Redraw (true);
                        }
 
-                       public virtual void RemoveAt (int index)
+                       public void RemoveAt (int index)
                        {
                                list.RemoveAt (index);
                                owner.Redraw (true);