* Application.cs: fix compilation errors when debug is enabled.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolBarButton.cs
index 7be4caa1749f5eca36ddf5fe5c7c399000d416b7..037b4ec731c2198443a737a72a002b82cd8a0970 100644 (file)
@@ -1,4 +1,3 @@
-//
 // System.Windows.Forms.ToolBarButton.cs
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-// Copyright (C) 2004 Novell, Inc. (http://www.novell.com)
+// Copyright (C) 2004-2006 Novell, Inc. (http://www.novell.com)
 //
 // Authors:
 //     Ravindra (rkumar@novell.com)
-//
-// TODO:
-//     - Adding a button to two toolbars
-//
-
+//     Mike Kestner <mkestner@novell.com>
 
-// NOT COMPLETE
 
 using System.ComponentModel;
 using System.ComponentModel.Design;
@@ -40,7 +34,7 @@ using System.Drawing.Imaging;
 namespace System.Windows.Forms
 {
        [DefaultProperty ("Text")]
-       [Designer ("System.Windows.Forms.Design.ToolBarButtonDesigner, " + Consts.AssemblySystem_Design)]
+       [Designer ("System.Windows.Forms.Design.ToolBarButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
        [DesignTimeVisible (false)]
        [ToolboxItem (false)]
        public class ToolBarButton : Component
@@ -57,12 +51,9 @@ namespace System.Windows.Forms
                private string text = "";
                private string tooltip = "";
                private bool visible = true;
-               private Point location = new Point (ThemeEngine.Current.ToolBarGripWidth,
-                                                   ThemeEngine.Current.ToolBarGripWidth);
                internal bool dd_pressed = false; // to check for a mouse down on dropdown rect
                internal bool hilight = false;    // to hilight buttons in flat style
                internal bool inside = false;     // to handle the mouse move event with mouse pressed
-               internal bool wrapper = false;    // to mark a wrapping button
                internal bool pressed = false;    // this is to check for mouse down on a button
                #endregion
 
@@ -79,16 +70,60 @@ namespace System.Windows.Forms
                internal bool Hilight {
                        get { return hilight; }
                        set {
-                               if (! pushed)
-                                       hilight = value;
-                               else
-                                       hilight = false;        
+                               if (hilight == value)
+                                       return;
+
+                               hilight = value;
+                               Invalidate ();
+                       }
+               }
+
+               internal Image Image {
+                       get {
+                               if (Parent == null || Parent.ImageList == null)
+                                       return null;
+
+                               ImageList list = Parent.ImageList;
+                               if (ImageIndex > -1 && ImageIndex < list.Images.Count)
+                                       return list.Images [ImageIndex];
+
+                               return null;
+                       }
+               }
+
+               Rectangle image_rect;
+               internal Rectangle ImageRectangle {
+                       get {
+                               Rectangle result = image_rect;
+                               result.X += bounds.X;
+                               result.Y += bounds.Y;
+                               return result; 
+                       }
+               }
+
+               Rectangle text_rect;
+               internal Rectangle TextRectangle {
+                       get { 
+                               Rectangle result = text_rect;
+                               result.X += bounds.X;
+                               result.Y += bounds.Y;
+                               return result; 
                        }
                }
 
+               Rectangle bounds;
                internal Point Location {
-                       get { return location; }
-                       set { location = value; }
+                       get { return bounds.Location; }
+                       set { 
+                               if (bounds.Location == value)
+                                       return;
+
+                               if (bounds != Rectangle.Empty)
+                                       Invalidate ();
+
+                               bounds.Location = value;
+                               Invalidate ();
+                       }
                }
 
                internal bool Pressed {
@@ -100,11 +135,6 @@ namespace System.Windows.Forms
                        }
                        set { pressed = value; }
                }
-
-               internal bool Wrapper {
-                       get { return wrapper; }
-                       set { wrapper = value; }
-               }
                #endregion internal properties
 
                #region properties
@@ -130,8 +160,7 @@ namespace System.Windows.Forms
                                        return;
 
                                enabled = value;
-                               if (parent != null)
-                                       parent.Redraw (false);
+                               Invalidate ();
                        }
                }
 
@@ -149,8 +178,10 @@ namespace System.Windows.Forms
                                        return;
 
                                image_index = value;
-                               if (parent != null)
-                                       parent.Redraw (true);
+                               if (bounds.Size != CalculateSize ())
+                                       Parent.Redraw (true);
+                               else
+                                       Invalidate ();
                        }
                }
 
@@ -167,8 +198,7 @@ namespace System.Windows.Forms
                                        return;
 
                                partial_push = value;
-                               if (parent != null)
-                                       parent.Redraw (false);
+                               Invalidate ();
                        }
                }
 
@@ -180,20 +210,18 @@ namespace System.Windows.Forms
                                        return;
 
                                pushed = value;
-                               if (pushed)
-                                       hilight = false;
-                               if (parent != null)
-                                       parent.Redraw (false);
+                               Invalidate ();
                        }
                }
 
                public Rectangle Rectangle {
                        get {
-                               if (parent == null)
-                                       return Rectangle.Empty;
-                               else if (visible && parent.Visible)
-                                       return parent.GetChildBounds (this);
-                               else
+                               if (Visible && Parent != null) {
+                                       Rectangle result = bounds;
+                                       if (Style == ToolBarButtonStyle.DropDownButton && Parent.DropDownArrows)
+                                               result.Width += ThemeEngine.Current.ToolBarDropDownWidth;
+                                       return result;
+                               } else
                                        return Rectangle.Empty;
                        }
                }
@@ -207,8 +235,7 @@ namespace System.Windows.Forms
                                        return;
 
                                style = value;
-                               if (parent != null)
-                                       parent.Redraw (true);
+                               Invalidate ();
                        }
                }
 
@@ -226,12 +253,14 @@ namespace System.Windows.Forms
                public string Text {
                        get { return text; }
                        set {
+                               if (value == null) value = "";
+
                                if (value == text)
                                        return;
 
                                text = value;
-                               if (parent != null)
-                                       parent.Redraw (true);
+                               if (Parent != null)
+                                       Parent.Redraw (true);
                        }
                }
 
@@ -239,7 +268,10 @@ namespace System.Windows.Forms
                [Localizable (true)]
                public string ToolTipText {
                        get { return tooltip; }
-                       set { tooltip = value; }
+                       set {
+                               if (value == null) value = "";
+                               tooltip = value;
+                       }
                }
 
                [DefaultValue (true)]
@@ -251,8 +283,8 @@ namespace System.Windows.Forms
                                        return;
 
                                visible = value;
-                               if (parent != null)
-                                       parent.Redraw (true);
+                               if (Parent != null)
+                                       Parent.Redraw (true);
                        }
                }
                #endregion
@@ -260,24 +292,110 @@ namespace System.Windows.Forms
                #region internal methods
                internal void SetParent (ToolBar parent)
                {
+                       if (Parent == parent)
+                               return;
+
+                       if (Parent != null)
+                               Parent.Buttons.Remove (this);
+
                        this.parent = parent;
                }
 
-               internal void Dump ()
+               internal bool Layout ()
                {
-                       Console.WriteLine ("TBButton: style: " + this.Style);
-                       Console.WriteLine ("TBButton: wrapper: " + this.Wrapper);
-                       Console.WriteLine ("TBButton: hilight: " + this.Hilight);
-                       Console.WriteLine ("TBButton: loc: " + this.Location);
-                       Console.WriteLine ("TBButton: rect: " + this.Rectangle);
-                       Console.WriteLine ("TBButton: txt: " + this.Text);
-                       Console.WriteLine ("TBButton: visible " + this.Visible);
-                       Console.WriteLine ("TBButton: enabled: " + this.Enabled);
-                       Console.WriteLine ("TBButton: image index: " + this.ImageIndex);
-                       Console.WriteLine ("TBButton: pushed: " + this.Pushed);
-                       Console.WriteLine ("TBButton: partial push: " + this.PartialPush);
+                       if (Parent == null || !Visible)
+                               return false;
+
+                       Size psize = Parent.ButtonSize;
+                       Size size = psize;
+                       if ((!Parent.SizeSpecified) || (Style == ToolBarButtonStyle.Separator)) {
+                               size = CalculateSize ();
+                               if (size.Width == 0 || size.Height == 0)
+                                       size = psize;
+                       }
+                       return Layout (size);
                }
-               #endregion
+
+               internal bool Layout (Size size)
+               {
+                       if (Parent == null || !Visible)
+                               return false;
+
+                       bounds.Size = size;
+
+                       Size image_size = (Parent.ImageSize == Size.Empty) ? new Size (16, 16) : Parent.ImageSize;
+                       int grip = ThemeEngine.Current.ToolBarImageGripWidth;
+
+                       Rectangle new_image_rect, new_text_rect;
+                       
+                       if (Parent.TextAlign == ToolBarTextAlign.Underneath) {
+                               new_image_rect = new Rectangle ((bounds.Size.Width - image_size.Width) / 2 - grip, 0, image_size.Width + 2 + grip, image_size.Height + 2 * grip);
+                               new_text_rect = new Rectangle (0, new_image_rect.Height, bounds.Size.Width, bounds.Size.Height - new_image_rect.Height - 2 * grip);
+                       } else {
+                               new_image_rect = new Rectangle (0, 0, image_size.Width + 2 * grip, image_size.Height + 2 * grip);
+                               new_text_rect = new Rectangle (new_image_rect.Width, 0, bounds.Size.Width - new_image_rect.Width, bounds.Size.Height - 2 * grip);
+                       }
+
+                       bool changed = false;
+
+                       if (new_image_rect != image_rect || new_text_rect != text_rect)
+                               changed = true;
+
+                       image_rect = new_image_rect;
+                       text_rect = new_text_rect;
+
+                       return changed;
+               }
+
+               const int text_padding = 3;
+
+               Size TextSize {
+                       get {
+                               SizeF sz = Parent.DeviceContext.MeasureString (Text, Parent.Font);
+                               if (sz == SizeF.Empty)
+                                       return Size.Empty;
+                               return new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, (int) Math.Ceiling (sz.Height));
+                       }
+               }
+
+               Size CalculateSize ()
+               {
+                       if (Parent == null)
+                               return Size.Empty;
+
+                       Theme theme = ThemeEngine.Current;
+
+                       int ht = Parent.ButtonSize.Height + 2 * theme.ToolBarGripWidth;
+
+                       if (Style == ToolBarButtonStyle.Separator)
+                               return new Size (theme.ToolBarSeparatorWidth, ht);
+
+                       Size size = TextSize;
+                       Size image_size = (Parent.ImageSize == Size.Empty) ? new Size (16, 16) : Parent.ImageSize;
+
+                       int image_width = image_size.Width + 2 * theme.ToolBarImageGripWidth; 
+                       int image_height = image_size.Height + 2 * theme.ToolBarImageGripWidth; 
+
+                       if (Parent.TextAlign == ToolBarTextAlign.Right) {
+                               size.Width =  image_width + size.Width;
+                               size.Height = (size.Height > image_height) ? size.Height : image_height;
+                       } else {
+                               size.Height = image_height + size.Height;
+                               size.Width = (size.Width > image_width) ? size.Width : image_width;
+                       }
+
+                       size.Width += theme.ToolBarGripWidth;
+                       size.Height += theme.ToolBarGripWidth;
+                       return size;
+               }
+
+               internal void Invalidate ()
+               {
+                       if (Parent != null)
+                               Parent.Invalidate (Rectangle);
+               }
+
+               #endregion Internal Methods
 
                #region methods
                protected override void Dispose (bool disposing)