* Application.cs: fix compilation errors when debug is enabled.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolBarButton.cs
index 0338ca6321a7bc5dadccb384399257633937d2ec..037b4ec731c2198443a737a72a002b82cd8a0970 100644 (file)
@@ -70,10 +70,11 @@ 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 ();
                        }
                }
 
@@ -112,7 +113,7 @@ namespace System.Windows.Forms
 
                Rectangle bounds;
                internal Point Location {
-                       //get { return location; }
+                       get { return bounds.Location; }
                        set { 
                                if (bounds.Location == value)
                                        return;
@@ -177,7 +178,10 @@ namespace System.Windows.Forms
                                        return;
 
                                image_index = value;
-                               Invalidate ();
+                               if (bounds.Size != CalculateSize ())
+                                       Parent.Redraw (true);
+                               else
+                                       Invalidate ();
                        }
                }
 
@@ -206,15 +210,13 @@ namespace System.Windows.Forms
                                        return;
 
                                pushed = value;
-                               if (pushed)
-                                       hilight = false;
                                Invalidate ();
                        }
                }
 
                public Rectangle Rectangle {
                        get {
-                               if (Visible && Parent != null && Parent.Visible) {
+                               if (Visible && Parent != null) {
                                        Rectangle result = bounds;
                                        if (Style == ToolBarButtonStyle.DropDownButton && Parent.DropDownArrows)
                                                result.Width += ThemeEngine.Current.ToolBarDropDownWidth;
@@ -251,11 +253,14 @@ namespace System.Windows.Forms
                public string Text {
                        get { return text; }
                        set {
+                               if (value == null) value = "";
+
                                if (value == text)
                                        return;
 
                                text = value;
-                               Invalidate ();
+                               if (Parent != null)
+                                       Parent.Redraw (true);
                        }
                }
 
@@ -263,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)]
@@ -293,45 +301,68 @@ namespace System.Windows.Forms
                        this.parent = parent;
                }
 
-               internal void Layout ()
+               internal bool Layout ()
                {
                        if (Parent == null || !Visible)
-                               return;
-
-                       Layout (Parent.ButtonSize);
+                               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);
                }
 
-               internal void Layout (Size size)
+               internal bool Layout (Size size)
                {
                        if (Parent == null || !Visible)
-                               return;
+                               return false;
 
                        bounds.Size = size;
 
-                       Image image = Image;
-                       if (image == null) {
-                               text_rect = new Rectangle (Point.Empty, size);
-                               image_rect = Rectangle.Empty;
-                               return;
-                       }
-
+                       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) {
-                               image_rect = new Rectangle ((size.Width - image.Width) / 2 - grip, 0, image.Width + 2 + grip, image.Height + 2 * grip);
-
-                               text_rect = new Rectangle (0, image_rect.Bottom, size.Width, size.Height - image_rect.Height);
+                               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 {
-                               image_rect = new Rectangle (0, 0, image.Width + 2 * grip, image.Height + 2 * grip);
-
-                               text_rect = new Rectangle (image_rect.Right, 0, size.Width - image_rect.Width, size.Height);
+                               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;
@@ -339,17 +370,11 @@ namespace System.Windows.Forms
                        if (Style == ToolBarButtonStyle.Separator)
                                return new Size (theme.ToolBarSeparatorWidth, ht);
 
-                       SizeF sz = Parent.DeviceContext.MeasureString (Text, Parent.Font);
-                       Size size = new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, 
-                                             (int) Math.Ceiling (sz.Height));
-
-                       Image image = Image;
-
-                       if (image == null)
-                               return size;
+                       Size size = TextSize;
+                       Size image_size = (Parent.ImageSize == Size.Empty) ? new Size (16, 16) : Parent.ImageSize;
 
-                       int image_width = image.Width + 2 * theme.ToolBarImageGripWidth; 
-                       int image_height = image.Height + 2 * theme.ToolBarImageGripWidth; 
+                       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;
@@ -364,7 +389,7 @@ namespace System.Windows.Forms
                        return size;
                }
 
-               void Invalidate ()
+               internal void Invalidate ()
                {
                        if (Parent != null)
                                Parent.Invalidate (Rectangle);