Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripItem.cs
index e8850562120a5b5a17736c661dc2652978bc0fbd..68b6091cbaefa7a94373a957b115420ed05a2022 100644 (file)
@@ -26,7 +26,6 @@
 //     Jonathan Pobst (monkey@jpobst.com)
 //
 
-#if NET_2_0
 
 using System;
 using System.Drawing;
@@ -114,14 +113,13 @@ namespace System.Windows.Forms
                        this.auto_size = true;
                        this.auto_tool_tip = this.DefaultAutoToolTip;
                        this.available = true;
-                       this.back_color = Control.DefaultBackColor;
+                       this.back_color = Color.Empty;
                        this.background_image_layout = ImageLayout.Tile;
                        this.can_select = true;
                        this.display_style = this.DefaultDisplayStyle;
                        this.dock = DockStyle.None;
                        this.enabled = true;
-                       this.font = new Font ("Tahoma", 8.25f);
-                       this.fore_color = Control.DefaultForeColor;
+                       this.fore_color = Color.Empty;
                        this.image = image;
                        this.image_align = ContentAlignment.MiddleCenter;
                        this.image_index = -1;
@@ -217,11 +215,14 @@ namespace System.Windows.Forms
                                if (!Enum.IsDefined (typeof (ToolStripItemAlignment), value))
                                        throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripItemAlignment", value));
 
-                               this.alignment = value;
+                               if (this.alignment != value) {
+                                       this.alignment = value;
+                                       this.CalculateAutoSize (); 
+                               }
                        }
                }
 
-               [MonoTODO]
+               [MonoTODO ("Stub, does nothing")]
                [Browsable (false)]
                [DefaultValue (false)]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
@@ -275,7 +276,15 @@ namespace System.Windows.Forms
                }
 
                public virtual Color BackColor {
-                       get { return this.back_color; }
+                       get {
+                               if (back_color != Color.Empty)
+                                       return back_color;
+
+                               if (Parent != null)
+                                       return parent.BackColor;
+
+                               return Control.DefaultBackColor;
+                       }
                        set {
                                if (this.back_color != value) {
                                        back_color = value;
@@ -340,8 +349,6 @@ namespace System.Windows.Forms
                                        this.display_style = value; 
                                        this.CalculateAutoSize (); 
                                        OnDisplayStyleChanged (EventArgs.Empty);
-                                       if (this.Parent != null)
-                                               this.Parent.PerformLayout ();
                                }
                        }
                }
@@ -396,9 +403,16 @@ namespace System.Windows.Forms
                }
 
                [Localizable (true)]
-               public virtual Font Font
-               {
-                       get { return this.font; }
+               public virtual Font Font {
+                       get { 
+                               if (font != null)
+                                       return font;
+                                       
+                               if (Parent != null)
+                                       return Parent.Font;
+                                       
+                               return DefaultFont;
+                       }
                        set { 
                                if (this.font != value) {
                                        this.font = value; 
@@ -410,7 +424,15 @@ namespace System.Windows.Forms
                }
 
                public virtual Color ForeColor {
-                       get { return this.fore_color; }
+                       get { 
+                               if (fore_color != Color.Empty)
+                                       return fore_color;
+                                       
+                               if (Parent != null)
+                                       return parent.ForeColor;
+                                       
+                               return Control.DefaultForeColor;
+                       }
                        set { 
                                if (this.fore_color != value) {
                                        this.fore_color = value; 
@@ -444,12 +466,12 @@ namespace System.Windows.Forms
                                        return this.image;
                                        
                                if (this.image_index >= 0)
-                                       if (this.owner != null && this.owner.ImageList != null)
+                                       if (this.owner != null && this.owner.ImageList != null && this.owner.ImageList.Images.Count > this.image_index)
                                                return this.owner.ImageList.Images[this.image_index];
 
 
                                if (!string.IsNullOrEmpty (this.image_key))
-                                       if (this.owner != null && this.owner.ImageList != null)
+                                       if (this.owner != null && this.owner.ImageList != null && this.owner.ImageList.Images.Count > this.image_index)
                                                return this.owner.ImageList.Images[this.image_key];
                                                
                                return null;
@@ -477,8 +499,10 @@ namespace System.Windows.Forms
                                if (!Enum.IsDefined (typeof (ContentAlignment), value))
                                        throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
 
-                               this.image_align = value;
-                               this.Invalidate ();
+                               if (image_align != value) {
+                                       this.image_align = value;
+                                       this.CalculateAutoSize (); 
+                               }
                        }
                }
 
@@ -529,9 +553,10 @@ namespace System.Windows.Forms
                public ToolStripItemImageScaling ImageScaling {
                        get { return this.image_scaling; }
                        set { 
-                               this.image_scaling = value; 
-                               this.CalculateAutoSize (); 
-                               this.Invalidate (); 
+                               if (image_scaling != value) {
+                                       this.image_scaling = value; 
+                                       this.CalculateAutoSize (); 
+                               }
                        }
                }
 
@@ -610,9 +635,13 @@ namespace System.Windows.Forms
                        get { return this.owner; }
                        set { 
                                if (this.owner != value) {
-                                       this.owner = value; 
-                                       this.CalculateAutoSize (); 
-                                       OnOwnerChanged (EventArgs.Empty);
+                                       if (this.owner != null)
+                                               this.owner.Items.Remove (this);
+                                       
+                                       if (value != null)      
+                                               value.Items.Add (this);
+                                       else
+                                               this.owner = null;
                                }
                        }
                }
@@ -641,7 +670,7 @@ namespace System.Windows.Forms
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public virtual bool Pressed { get { return this.is_pressed; } }
 
-               [MonoTODO ("Stub, not implemented")]
+               [MonoTODO ("RTL not implemented")]
                [Localizable (true)]
                public virtual RightToLeft RightToLeft {
                        get { return this.right_to_left; }
@@ -720,8 +749,11 @@ namespace System.Windows.Forms
                        set {
                                if (!Enum.IsDefined (typeof (ContentAlignment), value))
                                        throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
-                               this.text_align = value;
-                               this.Invalidate ();
+
+                               if (this.text_align != value) {
+                                       this.text_align = value;
+                                       this.CalculateAutoSize (); 
+                               }
                        }
                }
 
@@ -826,7 +858,7 @@ namespace System.Windows.Forms
                #endregion
 
                #region Public Methods
-               [MonoTODO ("Stub")]
+               [MonoTODO ("Stub, does nothing")]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public DragDropEffects DoDragDrop (Object data, DragDropEffects allowedEffects)
                {
@@ -861,16 +893,16 @@ namespace System.Windows.Forms
                }
 
                [EditorBrowsable (EditorBrowsableState.Never)]
-               public virtual void ResetBackColor () { this.BackColor = Control.DefaultBackColor; }
+               public virtual void ResetBackColor () { this.BackColor = Color.Empty; }
 
                [EditorBrowsable (EditorBrowsableState.Never)]
                public virtual void ResetDisplayStyle () { this.display_style = this.DefaultDisplayStyle; }
 
                [EditorBrowsable (EditorBrowsableState.Never)]
-               public virtual void ResetFont () { this.font = new Font ("Tahoma", 8.25f); }
+               public virtual void ResetFont () { this.font = null; }
 
                [EditorBrowsable (EditorBrowsableState.Never)]
-               public virtual void ResetForeColor () { this.ForeColor = Control.DefaultForeColor; }
+               public virtual void ResetForeColor () { this.ForeColor = Color.Empty; }
 
                [EditorBrowsable (EditorBrowsableState.Never)]
                public virtual void ResetImage () { this.image = null; }
@@ -892,11 +924,14 @@ namespace System.Windows.Forms
                        if (!this.is_selected && this.CanSelect) {
                                this.is_selected = true;
                                
-                               if (this.Visible && this.Parent.Focused && this is ToolStripControlHost)
-                                       (this as ToolStripControlHost).Focus ();
-                                       
-                               this.Invalidate ();
-                               this.Parent.NotifySelectedChanged (this);
+                               if (this.Parent != null) {
+                                       if (this.Visible && this.Parent.Focused && this is ToolStripControlHost)
+                                               (this as ToolStripControlHost).Focus ();
+                                               
+                                       this.Invalidate ();
+                                       this.Parent.NotifySelectedChanged (this);
+                               }
+                               OnUIASelectionChanged ();
                        }
                }
 
@@ -922,6 +957,9 @@ namespace System.Windows.Forms
                                StopAnimation ();
                                image = null;
                        }
+
+                       if (owner != null)
+                               owner.Items.Remove (this);
                        
                        base.Dispose (disposing);
                }
@@ -976,9 +1014,6 @@ namespace System.Windows.Forms
                        EventHandler eh = (EventHandler)(Events [DoubleClickEvent]);
                        if (eh != null)
                                eh (this, e);
-
-                       if (!double_click_enabled)
-                               OnClick (e);
                }
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]
@@ -1088,6 +1123,7 @@ namespace System.Windows.Forms
                                this.is_selected = false;
                                this.is_pressed = false;
                                this.Invalidate ();
+                               OnUIASelectionChanged ();
                        }
 
                        EventHandler eh = (EventHandler)(Events [MouseLeaveEvent]);
@@ -1135,13 +1171,21 @@ namespace System.Windows.Forms
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                protected internal virtual void OnOwnerFontChanged (EventArgs e)
                {
+                       this.CalculateAutoSize ();
+                       OnFontChanged (EventArgs.Empty);
                }
-               
-               protected virtual void OnPaint (PaintEventArgs e)
+
+               void OnPaintInternal (PaintEventArgs e)
                {
+                       // Have the background rendered independently from OnPaint
                        if (this.parent != null)
                                this.parent.Renderer.DrawItemBackground (new ToolStripItemRenderEventArgs (e.Graphics, this));
-                               
+
+                       OnPaint (e);
+               }
+
+               protected virtual void OnPaint (PaintEventArgs e)
+               {
                        PaintEventHandler eh = (PaintEventHandler)(Events [PaintEvent]);
                        if (eh != null)
                                eh (this, e);
@@ -1155,6 +1199,8 @@ namespace System.Windows.Forms
                
                protected virtual void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
                {
+                       this.text_size = TextRenderer.MeasureText (this.Text == null ? string.Empty : this.text, this.Font, Size.Empty, TextFormatFlags.HidePrefix);
+                       
                        if (oldParent != null)
                                oldParent.PerformLayout ();
                                
@@ -1308,7 +1354,7 @@ namespace System.Windows.Forms
                        remove {Events.RemoveHandler (DoubleClickEvent, value); }
                }
 
-               [MonoTODO ("Not raised")]
+               [MonoTODO ("Event never raised")]
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public event DragEventHandler DragDrop {
@@ -1316,7 +1362,7 @@ namespace System.Windows.Forms
                        remove { Events.RemoveHandler (DragDropEvent, value); }
                }
 
-               [MonoTODO ("Not raised")]
+               [MonoTODO ("Event never raised")]
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public event DragEventHandler DragEnter {
@@ -1324,7 +1370,7 @@ namespace System.Windows.Forms
                        remove { Events.RemoveHandler (DragEnterEvent, value); }
                }
 
-               [MonoTODO ("Not raised")]
+               [MonoTODO ("Event never raised")]
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public event EventHandler DragLeave {
@@ -1332,7 +1378,7 @@ namespace System.Windows.Forms
                        remove { Events.RemoveHandler (DragLeaveEvent, value); }
                }
 
-               [MonoTODO ("Not raised")]
+               [MonoTODO ("Event never raised")]
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public event DragEventHandler DragOver {
@@ -1350,7 +1396,7 @@ namespace System.Windows.Forms
                        remove {Events.RemoveHandler (ForeColorChangedEvent, value); }
                }
 
-               [MonoTODO ("Not raised")]
+               [MonoTODO ("Event never raised")]
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public event GiveFeedbackEventHandler GiveFeedback {
@@ -1403,13 +1449,13 @@ namespace System.Windows.Forms
                        remove {Events.RemoveHandler (PaintEvent, value); }
                }
 
-               [MonoTODO ("Not raised")]
+               [MonoTODO ("Event never raised")]
                public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp {
                        add { Events.AddHandler (QueryAccessibilityHelpEvent, value); }
                        remove { Events.RemoveHandler (QueryAccessibilityHelpEvent, value); }
                }
 
-               [MonoTODO ("Not raised")]
+               [MonoTODO ("Event never raised")]
                [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public event QueryContinueDragEventHandler QueryContinueDrag {
@@ -1492,16 +1538,16 @@ namespace System.Windows.Forms
                                        preferred_size = new Size (width, height);
                                        break;
                                case ToolStripItemDisplayStyle.Image:
-                                       if (this.Image == null)
+                                       if (this.GetImageSize () == Size.Empty)
                                                preferred_size = this.DefaultSize;
                                        else {
                                                switch (this.image_scaling) {
                                                        case ToolStripItemImageScaling.None:
-                                                               preferred_size = this.Image.Size;
+                                                               preferred_size = this.GetImageSize ();
                                                                break;
                                                        case ToolStripItemImageScaling.SizeToFit:
                                                                if (this.parent == null)
-                                                                       preferred_size = this.Image.Size;
+                                                                       preferred_size = this.GetImageSize ();
                                                                else
                                                                        preferred_size = this.parent.ImageScalingSize;
                                                                break;
@@ -1512,8 +1558,8 @@ namespace System.Windows.Forms
                                        int width2 = text_size.Width + this.padding.Horizontal;
                                        int height2 = text_size.Height + this.padding.Vertical;
 
-                                       if (this.Image != null) {
-                                               Size image_size = this.Image.Size;
+                                       if (this.GetImageSize () != Size.Empty) {
+                                               Size image_size = this.GetImageSize ();
                                                
                                                if (this.image_scaling == ToolStripItemImageScaling.SizeToFit && this.parent != null)
                                                        image_size = this.parent.ImageScalingSize;
@@ -1544,10 +1590,6 @@ namespace System.Windows.Forms
                                preferred_size.Height += 4;
                                preferred_size.Width += 4;
                        }
-                       
-                       // Account for ToolStripDropDownButton's drop down arrow
-                       if (this is ToolStripDropDownButton && (this as ToolStripDropDownButton).ShowDropDownArrow)
-                               preferred_size.Width += 9;
 
                        return preferred_size;
                }
@@ -1615,6 +1657,8 @@ namespace System.Windows.Forms
                        }
                }
 
+               private static Font DefaultFont { get { return new Font ("Tahoma", 8.25f); } }
+               
                internal virtual ToolStripTextDirection DefaultTextDirection { get { return ToolStripTextDirection.Inherit; } }
 
                internal virtual void Dismiss (ToolStripDropDownCloseReason reason)
@@ -1622,6 +1666,7 @@ namespace System.Windows.Forms
                        if (is_selected) {
                                this.is_selected = false;
                                this.Invalidate ();
+                               OnUIASelectionChanged ();
                        }
                }
                
@@ -1689,16 +1734,29 @@ namespace System.Windows.Forms
                
                internal Size GetImageSize ()
                {
-                       if (this.Image == null)
-                               return Size.Empty;
+                       // Get the actual size of our internal image -or-
+                       // Get the ImageList.ImageSize if we are using ImageLists
+                       if (this.image_scaling == ToolStripItemImageScaling.None) {
+                               if (this.image != null)
+                                       return image.Size;
+                                       
+                               if (this.image_index >= 0 || !string.IsNullOrEmpty (this.image_key))
+                                       if (this.owner != null && this.owner.ImageList != null)
+                                               return this.owner.ImageList.ImageSize;
+                       } else {
+                               // If we have an image and a parent, return ImageScalingSize
+                               if (this.Parent == null)
+                                       return Size.Empty;
+                                       
+                               if (this.image != null)
+                                       return this.Parent.ImageScalingSize;
+
+                               if (this.image_index >= 0 || !string.IsNullOrEmpty (this.image_key))
+                                       if (this.owner != null && this.owner.ImageList != null)
+                                               return this.Parent.ImageScalingSize;
+                       }
                        
-                       if (this.image_scaling == ToolStripItemImageScaling.None)
-                               return this.Image.Size;
-                               
-                       if (this.Parent == null)
-                               return Size.Empty;
-                               
-                       return this.Parent.ImageScalingSize;
+                       return Size.Empty;
                }
                
                internal string GetToolTip ()
@@ -1717,8 +1775,8 @@ namespace System.Windows.Forms
                                
                        switch (met) {
                                case ToolStripItemEventType.MouseUp:
+                                       this.HandleClick (((MouseEventArgs)e).Clicks, e);
                                        this.OnMouseUp ((MouseEventArgs)e);
-                                       this.HandleClick (e);
                                        break;
                                case ToolStripItemEventType.MouseDown:
                                        this.OnMouseDown ((MouseEventArgs)e);
@@ -1736,18 +1794,23 @@ namespace System.Windows.Forms
                                        this.OnMouseMove ((MouseEventArgs)e);
                                        break;
                                case ToolStripItemEventType.Paint:
-                                       this.OnPaint ((PaintEventArgs)e);
+                                       this.OnPaintInternal ((PaintEventArgs)e);
                                        break;
                                case ToolStripItemEventType.Click:
-                                       this.HandleClick (e);
+                                       this.HandleClick (1, e);
                                        break;
                        }
                }
                
-               internal virtual void HandleClick (EventArgs e)
+               internal virtual void HandleClick (int mouse_clicks, EventArgs e)
                {
+                       if (Parent == null)
+                               return;
                        this.Parent.HandleItemClick (this);
-                       this.OnClick (e);
+                       if (mouse_clicks == 2 && double_click_enabled)
+                               this.OnDoubleClick (e);
+                       else
+                               this.OnClick (e);
                }
                
                internal virtual void SetPlacement (ToolStripItemPlacement placement)
@@ -1802,7 +1865,7 @@ namespace System.Windows.Forms
                                        return true;
 
                                if (!(this.Owner is ToolStripDropDownMenu))
-                                       return true;
+                                       return false;
 
                                ToolStripDropDownMenu tsddm = (ToolStripDropDownMenu)this.Owner;
 
@@ -1816,7 +1879,7 @@ namespace System.Windows.Forms
                                        return true;
 
                                if (!(this.Owner is ToolStripDropDownMenu))
-                                       return true;
+                                       return false;
 
                                ToolStripDropDownMenu tsddm = (ToolStripDropDownMenu)this.Owner;
 
@@ -1828,7 +1891,18 @@ namespace System.Windows.Forms
                        get { return this.visible; }
                        set { this.visible = value; Invalidate (); }
                }
-               
+
+               internal ToolStrip InternalOwner {
+                       set {
+                               if (this.owner != value) {
+                                       this.owner = value;
+                                       if (this.owner != null)
+                                               this.CalculateAutoSize ();
+                                       OnOwnerChanged (EventArgs.Empty);
+                               }
+                       }
+               }
+
                internal Point Location {
                        get { return this.bounds.Location; }
                        set {
@@ -1885,6 +1959,24 @@ namespace System.Windows.Forms
                }
                #endregion
 
+               #region UIA Framework: Methods, Properties and Events
+
+               static object UIASelectionChangedEvent = new object ();
+
+               internal event EventHandler UIASelectionChanged {
+                       add { Events.AddHandler (UIASelectionChangedEvent, value); }
+                       remove { Events.RemoveHandler (UIASelectionChangedEvent, value); }
+               }
+
+               internal void OnUIASelectionChanged ()
+               {
+                       EventHandler eh = (EventHandler)(Events [UIASelectionChangedEvent]);
+                       if (eh != null)
+                               eh (this, EventArgs.Empty);
+               }
+               
+               #endregion
+
                [ComVisible (true)]
                public class ToolStripItemAccessibleObject : AccessibleObject
                {
@@ -1981,5 +2073,3 @@ namespace System.Windows.Forms
        {
        }
 }
-
-#endif