4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
33 using System.ComponentModel;
34 using System.Runtime.InteropServices;
36 namespace System.Windows.Forms
38 [DefaultEvent ("Click")]
39 [DefaultProperty ("Text")]
40 [DesignTimeVisible (false)]
42 [Designer ("System.Windows.Forms.Design.ToolStripItemDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 public abstract class ToolStripItem : Component, IDropTarget, IComponent, IDisposable
45 #region Private Variables
46 private AccessibleObject accessibility_object;
47 private string accessible_default_action_description;
48 private bool allow_drop;
49 private ToolStripItemAlignment alignment;
50 private AnchorStyles anchor;
51 private bool available;
52 private bool auto_size;
53 private bool auto_tool_tip;
54 private Color back_color;
55 private Image background_image;
56 private ImageLayout background_image_layout;
57 private Rectangle bounds;
58 private bool can_select;
59 private ToolStripItemDisplayStyle display_style;
60 private DockStyle dock;
61 private bool double_click_enabled;
63 private Size explicit_size;
65 private Color fore_color;
67 private ContentAlignment image_align;
68 private int image_index;
69 private string image_key;
70 private ToolStripItemImageScaling image_scaling;
71 private Color image_transparent_color;
72 private bool is_disposed;
73 internal bool is_pressed;
74 private bool is_selected;
75 private Padding margin;
76 private MergeAction merge_action;
77 private int merge_index;
79 private ToolStripItemOverflow overflow;
80 private ToolStrip owner;
81 internal ToolStripItem owner_item;
82 private Padding padding;
83 private ToolStripItemPlacement placement;
84 private RightToLeft right_to_left;
85 private bool right_to_left_auto_mirror_image;
88 private ContentAlignment text_align;
89 private ToolStripTextDirection text_direction;
90 private TextImageRelation text_image_relation;
91 private string tool_tip_text;
94 private EventHandler frame_handler; // For animating images
95 private ToolStrip parent;
96 private Size text_size;
99 #region Public Constructors
100 protected ToolStripItem ()
101 : this (String.Empty, null, null, String.Empty)
105 protected ToolStripItem (string text, Image image, EventHandler onClick)
106 : this (text, image, onClick, String.Empty)
110 protected ToolStripItem (string text, Image image, EventHandler onClick, string name)
112 this.alignment = ToolStripItemAlignment.Left;
113 this.anchor = AnchorStyles.Left | AnchorStyles.Top;
114 this.auto_size = true;
115 this.auto_tool_tip = this.DefaultAutoToolTip;
116 this.available = true;
117 this.back_color = Color.Empty;
118 this.background_image_layout = ImageLayout.Tile;
119 this.can_select = true;
120 this.display_style = this.DefaultDisplayStyle;
121 this.dock = DockStyle.None;
123 this.fore_color = Color.Empty;
125 this.image_align = ContentAlignment.MiddleCenter;
126 this.image_index = -1;
127 this.image_key = string.Empty;
128 this.image_scaling = ToolStripItemImageScaling.SizeToFit;
129 this.image_transparent_color = Color.Empty;
130 this.margin = this.DefaultMargin;
131 this.merge_action = MergeAction.Append;
132 this.merge_index = -1;
134 this.overflow = ToolStripItemOverflow.AsNeeded;
135 this.padding = this.DefaultPadding;
136 this.placement = ToolStripItemPlacement.None;
137 this.right_to_left = RightToLeft.Inherit;
138 this.bounds.Size = this.DefaultSize;
140 this.text_align = ContentAlignment.MiddleCenter;
141 this.text_direction = DefaultTextDirection;
142 this.text_image_relation = TextImageRelation.ImageBeforeText;
145 this.Click += onClick;
146 OnLayout (new LayoutEventArgs (null, string.Empty));
150 #region Public Properties
152 [EditorBrowsable (EditorBrowsableState.Advanced)]
153 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
154 public AccessibleObject AccessibilityObject {
156 if (this.accessibility_object == null)
157 this.accessibility_object = CreateAccessibilityInstance ();
159 return this.accessibility_object;
164 [EditorBrowsable (EditorBrowsableState.Advanced)]
165 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
166 public string AccessibleDefaultActionDescription {
168 if (this.accessibility_object == null)
171 return this.accessible_default_action_description;
173 set { this.accessible_default_action_description = value; }
177 [DefaultValue (null)]
178 public string AccessibleDescription {
180 if (this.accessibility_object == null)
183 return this.AccessibilityObject.Description;
185 set { this.AccessibilityObject.description = value; }
189 [DefaultValue (null)]
190 public string AccessibleName {
192 if (this.accessibility_object == null)
195 return this.AccessibilityObject.Name;
197 set { this.AccessibilityObject.Name = value; }
200 [DefaultValue (AccessibleRole.Default)]
201 public AccessibleRole AccessibleRole {
204 if (this.accessibility_object == null)
205 return AccessibleRole.Default;
207 return this.AccessibilityObject.Role;
209 set { this.AccessibilityObject.role = value; }
212 [DefaultValue (ToolStripItemAlignment.Left)]
213 public ToolStripItemAlignment Alignment {
214 get { return this.alignment; }
216 if (!Enum.IsDefined (typeof (ToolStripItemAlignment), value))
217 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripItemAlignment", value));
219 if (this.alignment != value) {
220 this.alignment = value;
221 this.CalculateAutoSize ();
226 [MonoTODO ("Stub, does nothing")]
228 [DefaultValue (false)]
229 [EditorBrowsable (EditorBrowsableState.Advanced)]
230 public virtual bool AllowDrop {
231 get { return this.allow_drop; }
232 set { this.allow_drop = value; }
236 [DefaultValue (AnchorStyles.Top | AnchorStyles.Left)]
237 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
238 public AnchorStyles Anchor {
239 get { return this.anchor; }
240 set { this.anchor = value; }
244 [DefaultValue (true)]
245 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
246 [RefreshProperties (RefreshProperties.All)]
247 public bool AutoSize {
248 get { return this.auto_size; }
250 this.auto_size = value;
251 this.CalculateAutoSize ();
255 [DefaultValue (false)]
256 public bool AutoToolTip {
257 get { return this.auto_tool_tip; }
258 set { this.auto_tool_tip = value; }
262 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
263 public bool Available {
264 get { return this.available; }
266 if (this.available != value) {
270 if (this.parent != null)
271 parent.PerformLayout ();
273 OnAvailableChanged (EventArgs.Empty);
274 OnVisibleChanged (EventArgs.Empty);
279 public virtual Color BackColor {
281 if (back_color != Color.Empty)
285 return parent.BackColor;
287 return Control.DefaultBackColor;
290 if (this.back_color != value) {
292 OnBackColorChanged (EventArgs.Empty);
299 [DefaultValue (null)]
300 public virtual Image BackgroundImage {
301 get { return this.background_image; }
303 if (this.background_image != value) {
304 this.background_image = value;
311 [DefaultValue (ImageLayout.Tile)]
312 public virtual ImageLayout BackgroundImageLayout {
313 get { return this.background_image_layout; }
315 if (this.background_image_layout != value) {
316 this.background_image_layout = value;
323 public virtual Rectangle Bounds {
324 get { return this.bounds; }
328 public virtual bool CanSelect {
329 get { return this.can_select; }
333 public Rectangle ContentRectangle {
335 // ToolStripLabels don't have a border
336 if (this is ToolStripLabel || this is ToolStripStatusLabel)
337 return new Rectangle (0, 0, this.bounds.Width, this.bounds.Height);
339 if (this is ToolStripDropDownButton && (this as ToolStripDropDownButton).ShowDropDownArrow)
340 return new Rectangle (2, 2, this.bounds.Width - 13, this.bounds.Height - 4);
342 return new Rectangle (2, 2, this.bounds.Width - 4, this.bounds.Height - 4);
346 public virtual ToolStripItemDisplayStyle DisplayStyle {
347 get { return this.display_style; }
349 if (this.display_style != value) {
350 this.display_style = value;
351 this.CalculateAutoSize ();
352 OnDisplayStyleChanged (EventArgs.Empty);
358 public bool IsDisposed {
359 get { return this.is_disposed; }
363 [DefaultValue (DockStyle.None)]
364 public DockStyle Dock {
365 get { return this.dock; }
367 if (this.dock != value) {
368 if (!Enum.IsDefined (typeof (DockStyle), value))
369 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for DockStyle", value));
372 this.CalculateAutoSize ();
377 [DefaultValue (false)]
378 public bool DoubleClickEnabled {
379 get { return this.double_click_enabled; }
380 set { this.double_click_enabled = value; }
384 [DefaultValue (true)]
385 public virtual bool Enabled {
398 if (this.enabled != value) {
399 this.enabled = value;
400 OnEnabledChanged (EventArgs.Empty);
407 public virtual Font Font {
418 if (this.font != value) {
420 this.CalculateAutoSize ();
421 this.OnFontChanged (EventArgs.Empty);
427 public virtual Color ForeColor {
429 if (fore_color != Color.Empty)
433 return parent.ForeColor;
435 return Control.DefaultForeColor;
438 if (this.fore_color != value) {
439 this.fore_color = value;
440 this.OnForeColorChanged (EventArgs.Empty);
447 [EditorBrowsable (EditorBrowsableState.Always)]
448 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
450 get { return this.Size.Height; }
452 this.Size = new Size (this.Size.Width, value);
453 this.explicit_size.Height = value;
456 this.CalculateAutoSize ();
457 this.OnBoundsChanged ();
464 public virtual Image Image {
466 if (this.image != null)
469 if (this.image_index >= 0)
470 if (this.owner != null && this.owner.ImageList != null && this.owner.ImageList.Images.Count > this.image_index)
471 return this.owner.ImageList.Images[this.image_index];
474 if (!string.IsNullOrEmpty (this.image_key))
475 if (this.owner != null && this.owner.ImageList != null && this.owner.ImageList.Images.Count > this.image_index)
476 return this.owner.ImageList.Images[this.image_key];
481 if (this.image != value) {
485 this.image_index = -1;
486 this.image_key = string.Empty;
487 this.CalculateAutoSize ();
496 [DefaultValue (ContentAlignment.MiddleCenter)]
497 public ContentAlignment ImageAlign {
498 get { return this.image_align; }
500 if (!Enum.IsDefined (typeof (ContentAlignment), value))
501 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
503 if (image_align != value) {
504 this.image_align = value;
505 this.CalculateAutoSize ();
512 [RelatedImageList ("Owner.ImageList")]
513 [TypeConverter (typeof (NoneExcludedImageIndexConverter))]
514 [RefreshProperties (RefreshProperties.Repaint)]
515 [Editor ("System.Windows.Forms.Design.ToolStripImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
516 public int ImageIndex {
517 get { return this.image_index; }
519 if (this.image_index != value) {
520 // Lamespec: MSDN says ArgumentException, tests say otherwise
522 throw new ArgumentOutOfRangeException ("ImageIndex cannot be less than -1");
524 this.image_index = value;
526 this.image_key = string.Empty;
527 this.CalculateAutoSize ();
535 [RelatedImageList ("Owner.ImageList")]
536 [TypeConverter (typeof (ImageKeyConverter))]
537 [RefreshProperties (RefreshProperties.Repaint)]
538 [Editor ("System.Windows.Forms.Design.ToolStripImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
539 public string ImageKey {
540 get { return this.image_key; }
542 if (this.image_key != value) {
544 this.image_index = -1;
545 this.image_key = value;
546 this.CalculateAutoSize ();
553 [DefaultValue (ToolStripItemImageScaling.SizeToFit)]
554 public ToolStripItemImageScaling ImageScaling {
555 get { return this.image_scaling; }
557 if (image_scaling != value) {
558 this.image_scaling = value;
559 this.CalculateAutoSize ();
565 public Color ImageTransparentColor {
566 get { return this.image_transparent_color; }
567 set { this.image_transparent_color = value; }
571 public bool IsOnDropDown {
573 if (this.parent != null && this.parent is ToolStripDropDown)
581 public bool IsOnOverflow {
582 get { return this.placement == ToolStripItemPlacement.Overflow; }
585 public Padding Margin {
586 get { return this.margin; }
589 this.CalculateAutoSize ();
593 [DefaultValue (MergeAction.Append)]
594 public MergeAction MergeAction {
595 get { return this.merge_action; }
597 if (!Enum.IsDefined (typeof (MergeAction), value))
598 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for MergeAction", value));
600 this.merge_action = value;
605 public int MergeIndex {
606 get { return this.merge_index; }
607 set { this.merge_index = value; }
610 [DefaultValue (null)]
613 get { return this.name; }
614 set { this.name = value; }
617 [DefaultValue (ToolStripItemOverflow.AsNeeded)]
618 public ToolStripItemOverflow Overflow {
619 get { return this.overflow; }
621 if (this.overflow != value) {
622 if (!Enum.IsDefined (typeof (ToolStripItemOverflow), value))
623 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripItemOverflow", value));
625 this.overflow = value;
628 owner.PerformLayout ();
634 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
635 public ToolStrip Owner {
636 get { return this.owner; }
638 if (this.owner != value) {
639 if (this.owner != null)
640 this.owner.Items.Remove (this);
643 value.Items.Add (this);
651 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
652 public ToolStripItem OwnerItem {
653 get { return this.owner_item; }
656 public virtual Padding Padding {
657 get { return this.padding; }
659 this.padding = value;
660 this.CalculateAutoSize ();
666 public ToolStripItemPlacement Placement {
667 get { return this.placement; }
671 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
672 public virtual bool Pressed { get { return this.is_pressed; } }
674 [MonoTODO ("RTL not implemented")]
676 public virtual RightToLeft RightToLeft {
677 get { return this.right_to_left; }
679 if (this.right_to_left != value) {
680 this.right_to_left = value;
681 this.OnRightToLeftChanged (EventArgs.Empty);
687 [DefaultValue (false)]
688 public bool RightToLeftAutoMirrorImage {
689 get { return this.right_to_left_auto_mirror_image; }
691 if (this.right_to_left_auto_mirror_image != value) {
692 this.right_to_left_auto_mirror_image = value;
699 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
700 public virtual bool Selected { get { return this.is_selected; } }
703 public virtual Size Size {
705 if (!this.AutoSize && this.explicit_size != Size.Empty)
706 return this.explicit_size;
708 return this.bounds.Size;
711 this.bounds.Size = value;
712 this.explicit_size = value;
715 this.CalculateAutoSize ();
716 this.OnBoundsChanged ();
721 [Localizable (false)]
723 [DefaultValue (null)]
724 [TypeConverter (typeof (StringConverter))]
726 get { return this.tag; }
727 set { this.tag = value; }
732 public virtual string Text
734 get { return this.text; }
736 if (this.text != value) {
739 this.CalculateAutoSize ();
741 this.OnTextChanged (EventArgs.Empty);
747 [DefaultValue (ContentAlignment.MiddleCenter)]
748 public virtual ContentAlignment TextAlign {
749 get { return this.text_align; }
751 if (!Enum.IsDefined (typeof (ContentAlignment), value))
752 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
754 if (this.text_align != value) {
755 this.text_align = value;
756 this.CalculateAutoSize ();
761 public virtual ToolStripTextDirection TextDirection {
763 if (this.text_direction == ToolStripTextDirection.Inherit) {
764 if (this.Parent != null)
765 return this.Parent.TextDirection;
767 return ToolStripTextDirection.Horizontal;
770 return this.text_direction;
773 if (!Enum.IsDefined (typeof (ToolStripTextDirection), value))
774 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripTextDirection", value));
776 if (this.text_direction != value) {
777 this.text_direction = value;
778 this.CalculateAutoSize ();
785 [DefaultValue (TextImageRelation.ImageBeforeText)]
786 public TextImageRelation TextImageRelation {
787 get { return this.text_image_relation; }
789 this.text_image_relation = value;
790 this.CalculateAutoSize ();
796 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
797 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
798 public string ToolTipText {
799 get { return this.tool_tip_text; }
800 set { this.tool_tip_text = value; }
804 public bool Visible {
806 if (this.parent == null)
809 return this.visible && this.parent.Visible;
812 if (this.visible != value) {
813 this.available = value;
814 this.SetVisibleCore (value);
815 if (this.Owner != null)
816 this.Owner.PerformLayout ();
822 [EditorBrowsable (EditorBrowsableState.Always)]
823 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
825 get { return this.Size.Width; }
827 this.Size = new Size (value, this.Size.Height);
828 this.explicit_size.Width = value;
831 this.CalculateAutoSize ();
832 this.OnBoundsChanged ();
839 #region Protected Properties
840 protected virtual bool DefaultAutoToolTip { get { return false; } }
841 protected virtual ToolStripItemDisplayStyle DefaultDisplayStyle { get { return ToolStripItemDisplayStyle.ImageAndText; } }
842 protected internal virtual Padding DefaultMargin { get { return new Padding (0, 1, 0, 2); } }
843 protected virtual Padding DefaultPadding { get { return new Padding (); } }
844 protected virtual Size DefaultSize { get { return new Size (23, 23); } }
845 protected internal virtual bool DismissWhenClicked { get { return true; } }
847 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
848 protected internal ToolStrip Parent {
849 get { return this.parent; }
851 if (this.parent != value) {
852 ToolStrip old_parent = this.parent;
854 OnParentChanged(old_parent, this.parent);
858 protected internal virtual bool ShowKeyboardCues { get { return false; } }
861 #region Public Methods
862 [MonoTODO ("Stub, does nothing")]
863 [EditorBrowsable (EditorBrowsableState.Advanced)]
864 public DragDropEffects DoDragDrop (Object data, DragDropEffects allowedEffects)
866 return allowedEffects;
869 public ToolStrip GetCurrentParent ()
874 public virtual Size GetPreferredSize (Size constrainingSize)
876 return this.CalculatePreferredSize (constrainingSize);
879 public void Invalidate ()
882 parent.Invalidate (this.bounds);
885 public void Invalidate (Rectangle r)
888 parent.Invalidate (r);
891 public void PerformClick ()
893 this.OnClick (EventArgs.Empty);
896 [EditorBrowsable (EditorBrowsableState.Never)]
897 public virtual void ResetBackColor () { this.BackColor = Color.Empty; }
899 [EditorBrowsable (EditorBrowsableState.Never)]
900 public virtual void ResetDisplayStyle () { this.display_style = this.DefaultDisplayStyle; }
902 [EditorBrowsable (EditorBrowsableState.Never)]
903 public virtual void ResetFont () { this.font = null; }
905 [EditorBrowsable (EditorBrowsableState.Never)]
906 public virtual void ResetForeColor () { this.ForeColor = Color.Empty; }
908 [EditorBrowsable (EditorBrowsableState.Never)]
909 public virtual void ResetImage () { this.image = null; }
911 [EditorBrowsable (EditorBrowsableState.Never)]
912 public void ResetMargin () { this.margin = this.DefaultMargin; }
914 [EditorBrowsable (EditorBrowsableState.Never)]
915 public void ResetPadding () { this.padding = this.DefaultPadding; }
917 [EditorBrowsable (EditorBrowsableState.Never)]
918 public virtual void ResetRightToLeft () { this.right_to_left = RightToLeft.Inherit; }
920 [EditorBrowsable (EditorBrowsableState.Never)]
921 public virtual void ResetTextDirection () { this.TextDirection = this.DefaultTextDirection; }
923 public void Select ()
925 if (!this.is_selected && this.CanSelect) {
926 this.is_selected = true;
928 if (this.Parent != null) {
929 if (this.Visible && this.Parent.Focused && this is ToolStripControlHost)
930 (this as ToolStripControlHost).Focus ();
933 this.Parent.NotifySelectedChanged (this);
935 OnUIASelectionChanged ();
939 public override string ToString ()
945 #region Protected Methods
946 [EditorBrowsable (EditorBrowsableState.Advanced)]
947 protected virtual AccessibleObject CreateAccessibilityInstance ()
949 return new ToolStripItemAccessibleObject (this);
952 protected override void Dispose (bool disposing)
954 if (!is_disposed && disposing)
963 owner.Items.Remove (this);
965 base.Dispose (disposing);
968 protected internal virtual bool IsInputChar (char charCode)
973 protected internal virtual bool IsInputKey (Keys keyData)
978 protected virtual void OnAvailableChanged (EventArgs e)
980 EventHandler eh = (EventHandler)(Events [AvailableChangedEvent]);
985 [EditorBrowsable (EditorBrowsableState.Advanced)]
986 protected virtual void OnBackColorChanged (EventArgs e)
988 EventHandler eh = (EventHandler)(Events [BackColorChangedEvent]);
993 protected virtual void OnBoundsChanged ()
995 OnLayout (new LayoutEventArgs(null, string.Empty));
998 protected virtual void OnClick (EventArgs e)
1000 EventHandler eh = (EventHandler)(Events [ClickEvent]);
1005 [EditorBrowsable (EditorBrowsableState.Advanced)]
1006 protected virtual void OnDisplayStyleChanged (EventArgs e)
1008 EventHandler eh = (EventHandler)(Events [DisplayStyleChangedEvent]);
1013 protected virtual void OnDoubleClick (EventArgs e)
1015 EventHandler eh = (EventHandler)(Events [DoubleClickEvent]);
1020 [EditorBrowsable (EditorBrowsableState.Advanced)]
1021 protected virtual void OnDragDrop (DragEventArgs dragEvent)
1023 DragEventHandler eh = (DragEventHandler)(Events[DragDropEvent]);
1025 eh (this, dragEvent);
1028 [EditorBrowsable (EditorBrowsableState.Advanced)]
1029 protected virtual void OnDragEnter (DragEventArgs dragEvent)
1031 DragEventHandler eh = (DragEventHandler)(Events[DragEnterEvent]);
1033 eh (this, dragEvent);
1036 [EditorBrowsable (EditorBrowsableState.Advanced)]
1037 protected virtual void OnDragLeave (EventArgs e)
1039 EventHandler eh = (EventHandler)(Events[DragLeaveEvent]);
1044 [EditorBrowsable (EditorBrowsableState.Advanced)]
1045 protected virtual void OnDragOver (DragEventArgs dragEvent)
1047 DragEventHandler eh = (DragEventHandler)(Events[DragOverEvent]);
1049 eh (this, dragEvent);
1052 protected virtual void OnEnabledChanged (EventArgs e)
1054 EventHandler eh = (EventHandler)(Events [EnabledChangedEvent]);
1059 [EditorBrowsable (EditorBrowsableState.Advanced)]
1060 protected virtual void OnFontChanged (EventArgs e)
1064 [EditorBrowsable (EditorBrowsableState.Advanced)]
1065 protected virtual void OnForeColorChanged (EventArgs e)
1067 EventHandler eh = (EventHandler)(Events [ForeColorChangedEvent]);
1072 [EditorBrowsable (EditorBrowsableState.Advanced)]
1073 protected virtual void OnGiveFeedback (GiveFeedbackEventArgs giveFeedbackEvent)
1075 GiveFeedbackEventHandler eh = (GiveFeedbackEventHandler)(Events[GiveFeedbackEvent]);
1077 eh (this, giveFeedbackEvent);
1080 protected virtual void OnLayout (LayoutEventArgs e)
1084 protected virtual void OnLocationChanged (EventArgs e)
1086 EventHandler eh = (EventHandler)(Events [LocationChangedEvent]);
1091 protected virtual void OnMouseDown (MouseEventArgs e)
1094 this.is_pressed = true;
1097 MouseEventHandler eh = (MouseEventHandler)(Events [MouseDownEvent]);
1103 protected virtual void OnMouseEnter (EventArgs e)
1107 EventHandler eh = (EventHandler)(Events [MouseEnterEvent]);
1112 protected virtual void OnMouseHover (EventArgs e)
1115 EventHandler eh = (EventHandler)(Events [MouseHoverEvent]);
1121 protected virtual void OnMouseLeave (EventArgs e)
1123 if (this.CanSelect) {
1124 this.is_selected = false;
1125 this.is_pressed = false;
1127 OnUIASelectionChanged ();
1130 EventHandler eh = (EventHandler)(Events [MouseLeaveEvent]);
1135 protected virtual void OnMouseMove (MouseEventArgs mea)
1138 MouseEventHandler eh = (MouseEventHandler)(Events [MouseMoveEvent]);
1144 protected virtual void OnMouseUp (MouseEventArgs e)
1147 this.is_pressed = false;
1150 if (this.IsOnDropDown)
1151 if (!(this is ToolStripDropDownItem) || !(this as ToolStripDropDownItem).HasDropDownItems || (this as ToolStripDropDownItem).DropDown.Visible == false) {
1152 if ((this.Parent as ToolStripDropDown).OwnerItem != null)
1153 ((this.Parent as ToolStripDropDown).OwnerItem as ToolStripDropDownItem).HideDropDown ();
1155 (this.Parent as ToolStripDropDown).Hide ();
1159 MouseEventHandler eh = (MouseEventHandler)(Events [MouseUpEvent]);
1165 protected virtual void OnOwnerChanged (EventArgs e)
1167 EventHandler eh = (EventHandler)(Events [OwnerChangedEvent]);
1172 [EditorBrowsable (EditorBrowsableState.Advanced)]
1173 protected internal virtual void OnOwnerFontChanged (EventArgs e)
1175 this.CalculateAutoSize ();
1176 OnFontChanged (EventArgs.Empty);
1179 void OnPaintInternal (PaintEventArgs e)
1181 // Have the background rendered independently from OnPaint
1182 if (this.parent != null)
1183 this.parent.Renderer.DrawItemBackground (new ToolStripItemRenderEventArgs (e.Graphics, this));
1188 protected virtual void OnPaint (PaintEventArgs e)
1190 PaintEventHandler eh = (PaintEventHandler)(Events [PaintEvent]);
1195 // This is never called.
1196 [EditorBrowsable (EditorBrowsableState.Advanced)]
1197 protected virtual void OnParentBackColorChanged (EventArgs e)
1201 protected virtual void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
1203 this.text_size = TextRenderer.MeasureText (this.Text == null ? string.Empty : this.text, this.Font, Size.Empty, TextFormatFlags.HidePrefix);
1205 if (oldParent != null)
1206 oldParent.PerformLayout ();
1208 if (newParent != null)
1209 newParent.PerformLayout ();
1212 protected internal virtual void OnParentEnabledChanged (EventArgs e)
1214 this.OnEnabledChanged (e);
1217 // This is never called.
1218 [EditorBrowsable (EditorBrowsableState.Advanced)]
1219 protected virtual void OnParentForeColorChanged (EventArgs e)
1223 [EditorBrowsable (EditorBrowsableState.Advanced)]
1224 protected internal virtual void OnParentRightToLeftChanged (EventArgs e)
1226 this.OnRightToLeftChanged (e);
1229 [EditorBrowsable (EditorBrowsableState.Advanced)]
1230 protected virtual void OnQueryContinueDrag (QueryContinueDragEventArgs queryContinueDragEvent)
1232 QueryContinueDragEventHandler eh = (QueryContinueDragEventHandler)(Events[QueryContinueDragEvent]);
1234 eh (this, queryContinueDragEvent);
1237 protected virtual void OnRightToLeftChanged (EventArgs e)
1239 EventHandler eh = (EventHandler)(Events[RightToLeftChangedEvent]);
1244 [EditorBrowsable (EditorBrowsableState.Advanced)]
1245 protected virtual void OnTextChanged (EventArgs e)
1247 EventHandler eh = (EventHandler)(Events [TextChangedEvent]);
1252 protected virtual void OnVisibleChanged (EventArgs e)
1254 EventHandler eh = (EventHandler)(Events [VisibleChangedEvent]);
1259 protected internal virtual bool ProcessCmdKey (ref Message m, Keys keyData)
1264 protected internal virtual bool ProcessDialogKey (Keys keyData)
1266 if (this.Selected && keyData == Keys.Enter) {
1267 this.FireEvent (EventArgs.Empty, ToolStripItemEventType.Click);
1274 // ProcessMnemonic will only be called if we are supposed to handle
1275 // it. None of that fancy "thinking" needed!
1276 protected internal virtual bool ProcessMnemonic (char charCode)
1278 ToolStripManager.SetActiveToolStrip (this.Parent, true);
1279 this.PerformClick ();
1283 protected internal virtual void SetBounds (Rectangle bounds)
1285 if (this.bounds != bounds) {
1286 this.bounds = bounds;
1291 protected virtual void SetVisibleCore (bool visible)
1293 this.visible = visible;
1294 this.OnVisibleChanged (EventArgs.Empty);
1304 #region Public Events
1305 static object AvailableChangedEvent = new object ();
1306 static object BackColorChangedEvent = new object ();
1307 static object ClickEvent = new object ();
1308 static object DisplayStyleChangedEvent = new object ();
1309 static object DoubleClickEvent = new object ();
1310 static object DragDropEvent = new object ();
1311 static object DragEnterEvent = new object ();
1312 static object DragLeaveEvent = new object ();
1313 static object DragOverEvent = new object ();
1314 static object EnabledChangedEvent = new object ();
1315 static object ForeColorChangedEvent = new object ();
1316 static object GiveFeedbackEvent = new object ();
1317 static object LocationChangedEvent = new object ();
1318 static object MouseDownEvent = new object ();
1319 static object MouseEnterEvent = new object ();
1320 static object MouseHoverEvent = new object ();
1321 static object MouseLeaveEvent = new object ();
1322 static object MouseMoveEvent = new object ();
1323 static object MouseUpEvent = new object ();
1324 static object OwnerChangedEvent = new object ();
1325 static object PaintEvent = new object ();
1326 static object QueryAccessibilityHelpEvent = new object ();
1327 static object QueryContinueDragEvent = new object ();
1328 static object RightToLeftChangedEvent = new object ();
1329 static object TextChangedEvent = new object ();
1330 static object VisibleChangedEvent = new object ();
1333 public event EventHandler AvailableChanged {
1334 add { Events.AddHandler (AvailableChangedEvent, value); }
1335 remove {Events.RemoveHandler (AvailableChangedEvent, value); }
1338 public event EventHandler BackColorChanged {
1339 add { Events.AddHandler (BackColorChangedEvent, value); }
1340 remove {Events.RemoveHandler (BackColorChangedEvent, value); }
1343 public event EventHandler Click {
1344 add { Events.AddHandler (ClickEvent, value); }
1345 remove {Events.RemoveHandler (ClickEvent, value); }
1348 public event EventHandler DisplayStyleChanged {
1349 add { Events.AddHandler (DisplayStyleChangedEvent, value); }
1350 remove {Events.RemoveHandler (DisplayStyleChangedEvent, value); }
1353 public event EventHandler DoubleClick {
1354 add { Events.AddHandler (DoubleClickEvent, value); }
1355 remove {Events.RemoveHandler (DoubleClickEvent, value); }
1358 [MonoTODO ("Event never raised")]
1360 [EditorBrowsable (EditorBrowsableState.Advanced)]
1361 public event DragEventHandler DragDrop {
1362 add { Events.AddHandler (DragDropEvent, value); }
1363 remove { Events.RemoveHandler (DragDropEvent, value); }
1366 [MonoTODO ("Event never raised")]
1368 [EditorBrowsable (EditorBrowsableState.Advanced)]
1369 public event DragEventHandler DragEnter {
1370 add { Events.AddHandler (DragEnterEvent, value); }
1371 remove { Events.RemoveHandler (DragEnterEvent, value); }
1374 [MonoTODO ("Event never raised")]
1376 [EditorBrowsable (EditorBrowsableState.Advanced)]
1377 public event EventHandler DragLeave {
1378 add { Events.AddHandler (DragLeaveEvent, value); }
1379 remove { Events.RemoveHandler (DragLeaveEvent, value); }
1382 [MonoTODO ("Event never raised")]
1384 [EditorBrowsable (EditorBrowsableState.Advanced)]
1385 public event DragEventHandler DragOver {
1386 add { Events.AddHandler (DragOverEvent, value); }
1387 remove { Events.RemoveHandler (DragOverEvent, value); }
1390 public event EventHandler EnabledChanged {
1391 add { Events.AddHandler (EnabledChangedEvent, value); }
1392 remove {Events.RemoveHandler (EnabledChangedEvent, value); }
1395 public event EventHandler ForeColorChanged {
1396 add { Events.AddHandler (ForeColorChangedEvent, value); }
1397 remove {Events.RemoveHandler (ForeColorChangedEvent, value); }
1400 [MonoTODO ("Event never raised")]
1402 [EditorBrowsable (EditorBrowsableState.Advanced)]
1403 public event GiveFeedbackEventHandler GiveFeedback {
1404 add { Events.AddHandler (GiveFeedbackEvent, value); }
1405 remove { Events.RemoveHandler (GiveFeedbackEvent, value); }
1408 public event EventHandler LocationChanged {
1409 add { Events.AddHandler (LocationChangedEvent, value); }
1410 remove {Events.RemoveHandler (LocationChangedEvent, value); }
1413 public event MouseEventHandler MouseDown {
1414 add { Events.AddHandler (MouseDownEvent, value); }
1415 remove {Events.RemoveHandler (MouseDownEvent, value); }
1418 public event EventHandler MouseEnter {
1419 add { Events.AddHandler (MouseEnterEvent, value); }
1420 remove {Events.RemoveHandler (MouseEnterEvent, value); }
1423 public event EventHandler MouseHover {
1424 add { Events.AddHandler (MouseHoverEvent, value); }
1425 remove {Events.RemoveHandler (MouseHoverEvent, value); }
1428 public event EventHandler MouseLeave {
1429 add { Events.AddHandler (MouseLeaveEvent, value); }
1430 remove {Events.RemoveHandler (MouseLeaveEvent, value); }
1433 public event MouseEventHandler MouseMove {
1434 add { Events.AddHandler (MouseMoveEvent, value); }
1435 remove {Events.RemoveHandler (MouseMoveEvent, value); }
1438 public event MouseEventHandler MouseUp {
1439 add { Events.AddHandler (MouseUpEvent, value); }
1440 remove {Events.RemoveHandler (MouseUpEvent, value); }
1443 public event EventHandler OwnerChanged {
1444 add { Events.AddHandler (OwnerChangedEvent, value); }
1445 remove {Events.RemoveHandler (OwnerChangedEvent, value); }
1448 public event PaintEventHandler Paint {
1449 add { Events.AddHandler (PaintEvent, value); }
1450 remove {Events.RemoveHandler (PaintEvent, value); }
1453 [MonoTODO ("Event never raised")]
1454 public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp {
1455 add { Events.AddHandler (QueryAccessibilityHelpEvent, value); }
1456 remove { Events.RemoveHandler (QueryAccessibilityHelpEvent, value); }
1459 [MonoTODO ("Event never raised")]
1461 [EditorBrowsable (EditorBrowsableState.Advanced)]
1462 public event QueryContinueDragEventHandler QueryContinueDrag {
1463 add { Events.AddHandler (QueryContinueDragEvent, value); }
1464 remove { Events.RemoveHandler (QueryContinueDragEvent, value); }
1467 public event EventHandler RightToLeftChanged {
1468 add { Events.AddHandler (RightToLeftChangedEvent, value); }
1469 remove { Events.RemoveHandler (RightToLeftChangedEvent, value); }
1472 public event EventHandler TextChanged {
1473 add { Events.AddHandler (TextChangedEvent, value); }
1474 remove {Events.RemoveHandler (TextChangedEvent, value); }
1477 public event EventHandler VisibleChanged {
1478 add { Events.AddHandler (VisibleChangedEvent, value); }
1479 remove {Events.RemoveHandler (VisibleChangedEvent, value); }
1483 #region Internal Methods
1484 internal Rectangle AlignInRectangle (Rectangle outer, Size inner, ContentAlignment align)
1489 if (align == ContentAlignment.BottomLeft || align == ContentAlignment.MiddleLeft || align == ContentAlignment.TopLeft)
1491 else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.MiddleCenter || align == ContentAlignment.TopCenter)
1492 x = Math.Max (outer.X + ((outer.Width - inner.Width) / 2), outer.Left);
1493 else if (align == ContentAlignment.BottomRight || align == ContentAlignment.MiddleRight || align == ContentAlignment.TopRight)
1494 x = outer.Right - inner.Width;
1495 if (align == ContentAlignment.TopCenter || align == ContentAlignment.TopLeft || align == ContentAlignment.TopRight)
1497 else if (align == ContentAlignment.MiddleCenter || align == ContentAlignment.MiddleLeft || align == ContentAlignment.MiddleRight)
1498 y = outer.Y + (outer.Height - inner.Height) / 2;
1499 else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.BottomRight || align == ContentAlignment.BottomLeft)
1500 y = outer.Bottom - inner.Height;
1502 return new Rectangle (x, y, Math.Min (inner.Width, outer.Width), Math.Min (inner.Height, outer.Height));
1505 internal void CalculateAutoSize ()
1507 this.text_size = TextRenderer.MeasureText (this.Text == null ? string.Empty: this.text, this.Font, Size.Empty, TextFormatFlags.HidePrefix);
1509 // If our text is rotated, flip the width and height
1510 ToolStripTextDirection direction = this.TextDirection;
1512 if (direction == ToolStripTextDirection.Vertical270 || direction == ToolStripTextDirection.Vertical90)
1513 this.text_size = new Size (this.text_size.Height, this.text_size.Width);
1515 if (!this.auto_size || this is ToolStripControlHost)
1517 //this.text_size.Width += 6;
1519 Size final_size = this.CalculatePreferredSize (Size.Empty);
1521 if (final_size != this.Size) {
1522 this.bounds.Width = final_size.Width;
1523 if (this.parent != null)
1524 this.parent.PerformLayout ();
1528 internal virtual Size CalculatePreferredSize (Size constrainingSize)
1530 if (!this.auto_size)
1531 return this.explicit_size;
1533 Size preferred_size = this.DefaultSize;
1535 switch (this.display_style) {
1536 case ToolStripItemDisplayStyle.Text:
1537 int width = text_size.Width + this.padding.Horizontal;
1538 int height = text_size.Height + this.padding.Vertical;
1539 preferred_size = new Size (width, height);
1541 case ToolStripItemDisplayStyle.Image:
1542 if (this.GetImageSize () == Size.Empty)
1543 preferred_size = this.DefaultSize;
1545 switch (this.image_scaling) {
1546 case ToolStripItemImageScaling.None:
1547 preferred_size = this.GetImageSize ();
1549 case ToolStripItemImageScaling.SizeToFit:
1550 if (this.parent == null)
1551 preferred_size = this.GetImageSize ();
1553 preferred_size = this.parent.ImageScalingSize;
1558 case ToolStripItemDisplayStyle.ImageAndText:
1559 int width2 = text_size.Width + this.padding.Horizontal;
1560 int height2 = text_size.Height + this.padding.Vertical;
1562 if (this.GetImageSize () != Size.Empty) {
1563 Size image_size = this.GetImageSize ();
1565 if (this.image_scaling == ToolStripItemImageScaling.SizeToFit && this.parent != null)
1566 image_size = this.parent.ImageScalingSize;
1568 switch (this.text_image_relation) {
1569 case TextImageRelation.Overlay:
1570 width2 = Math.Max (width2, image_size.Width);
1571 height2 = Math.Max (height2, image_size.Height);
1573 case TextImageRelation.ImageAboveText:
1574 case TextImageRelation.TextAboveImage:
1575 width2 = Math.Max (width2, image_size.Width);
1576 height2 += image_size.Height;
1578 case TextImageRelation.ImageBeforeText:
1579 case TextImageRelation.TextBeforeImage:
1580 height2 = Math.Max (height2, image_size.Height);
1581 width2 += image_size.Width;
1586 preferred_size = new Size (width2, height2);
1590 if (!(this is ToolStripLabel)) { // Everything but labels have a border
1591 preferred_size.Height += 4;
1592 preferred_size.Width += 4;
1595 return preferred_size;
1598 internal void CalculateTextAndImageRectangles (out Rectangle text_rect, out Rectangle image_rect)
1600 this.CalculateTextAndImageRectangles (this.ContentRectangle, out text_rect, out image_rect);
1603 internal void CalculateTextAndImageRectangles (Rectangle contentRectangle, out Rectangle text_rect, out Rectangle image_rect)
1605 text_rect = Rectangle.Empty;
1606 image_rect = Rectangle.Empty;
1608 switch (this.display_style) {
1609 case ToolStripItemDisplayStyle.None:
1611 case ToolStripItemDisplayStyle.Text:
1612 if (this.text != string.Empty)
1613 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1615 case ToolStripItemDisplayStyle.Image:
1616 if (this.Image != null && this.UseImageMargin)
1617 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1619 case ToolStripItemDisplayStyle.ImageAndText:
1620 if (this.text != string.Empty && (this.Image == null || !this.UseImageMargin))
1621 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1622 else if (this.text == string.Empty && (this.Image == null || !this.UseImageMargin))
1624 else if (this.text == string.Empty && this.Image != null)
1625 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1627 Rectangle text_area;
1628 Rectangle image_area;
1630 switch (this.text_image_relation) {
1631 case TextImageRelation.Overlay:
1632 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1633 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1635 case TextImageRelation.ImageAboveText:
1636 text_area = new Rectangle (contentRectangle.Left, contentRectangle.Bottom - (text_size.Height - 4), contentRectangle.Width, text_size.Height - 4);
1637 image_area = new Rectangle (contentRectangle.Left, contentRectangle.Top, contentRectangle.Width, contentRectangle.Height - text_area.Height);
1639 text_rect = AlignInRectangle (text_area, this.text_size, this.text_align);
1640 image_rect = AlignInRectangle (image_area, GetImageSize (), this.image_align);
1642 case TextImageRelation.TextAboveImage:
1643 text_area = new Rectangle (contentRectangle.Left, contentRectangle.Top, contentRectangle.Width, text_size.Height - 4);
1644 image_area = new Rectangle (contentRectangle.Left, text_area.Bottom, contentRectangle.Width, contentRectangle.Height - text_area.Height);
1646 text_rect = AlignInRectangle (text_area, this.text_size, this.text_align);
1647 image_rect = AlignInRectangle (image_area, GetImageSize (), this.image_align);
1649 case TextImageRelation.ImageBeforeText:
1650 LayoutTextBeforeOrAfterImage (contentRectangle, false, text_size, GetImageSize (), text_align, image_align, out text_rect, out image_rect);
1652 case TextImageRelation.TextBeforeImage:
1653 LayoutTextBeforeOrAfterImage (contentRectangle, true, text_size, GetImageSize (), text_align, image_align, out text_rect, out image_rect);
1661 private static Font DefaultFont { get { return new Font ("Tahoma", 8.25f); } }
1663 internal virtual ToolStripTextDirection DefaultTextDirection { get { return ToolStripTextDirection.Inherit; } }
1665 internal virtual void Dismiss (ToolStripDropDownCloseReason reason)
1668 this.is_selected = false;
1670 OnUIASelectionChanged ();
1674 internal virtual ToolStrip GetTopLevelToolStrip ()
1676 if (this.Parent != null)
1677 return this.Parent.GetTopLevelToolStrip ();
1682 private void LayoutTextBeforeOrAfterImage (Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, ContentAlignment textAlign, ContentAlignment imageAlign, out Rectangle textRect, out Rectangle imageRect)
1684 int element_spacing = 0; // Spacing between the Text and the Image
1685 int total_width = textSize.Width + element_spacing + imageSize.Width;
1686 int excess_width = totalArea.Width - total_width;
1689 Rectangle final_text_rect;
1690 Rectangle final_image_rect;
1692 HorizontalAlignment h_text = GetHorizontalAlignment (textAlign);
1693 HorizontalAlignment h_image = GetHorizontalAlignment (imageAlign);
1695 if (h_image == HorizontalAlignment.Left)
1697 else if (h_image == HorizontalAlignment.Right && h_text == HorizontalAlignment.Right)
1698 offset = excess_width;
1699 else if (h_image == HorizontalAlignment.Center && (h_text == HorizontalAlignment.Left || h_text == HorizontalAlignment.Center))
1700 offset += (int)(excess_width / 3);
1702 offset += (int)(2 * (excess_width / 3));
1705 final_text_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
1706 final_image_rect = new Rectangle (final_text_rect.Right + element_spacing, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
1708 final_image_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
1709 final_text_rect = new Rectangle (final_image_rect.Right + element_spacing, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
1712 textRect = final_text_rect;
1713 imageRect = final_image_rect;
1716 private HorizontalAlignment GetHorizontalAlignment (ContentAlignment align)
1719 case ContentAlignment.BottomLeft:
1720 case ContentAlignment.MiddleLeft:
1721 case ContentAlignment.TopLeft:
1722 return HorizontalAlignment.Left;
1723 case ContentAlignment.BottomCenter:
1724 case ContentAlignment.MiddleCenter:
1725 case ContentAlignment.TopCenter:
1726 return HorizontalAlignment.Center;
1727 case ContentAlignment.BottomRight:
1728 case ContentAlignment.MiddleRight:
1729 case ContentAlignment.TopRight:
1730 return HorizontalAlignment.Right;
1733 return HorizontalAlignment.Left;
1736 internal Size GetImageSize ()
1738 // Get the actual size of our internal image -or-
1739 // Get the ImageList.ImageSize if we are using ImageLists
1740 if (this.image_scaling == ToolStripItemImageScaling.None) {
1741 if (this.image != null)
1744 if (this.image_index >= 0 || !string.IsNullOrEmpty (this.image_key))
1745 if (this.owner != null && this.owner.ImageList != null)
1746 return this.owner.ImageList.ImageSize;
1748 // If we have an image and a parent, return ImageScalingSize
1749 if (this.Parent == null)
1752 if (this.image != null)
1753 return this.Parent.ImageScalingSize;
1755 if (this.image_index >= 0 || !string.IsNullOrEmpty (this.image_key))
1756 if (this.owner != null && this.owner.ImageList != null)
1757 return this.Parent.ImageScalingSize;
1763 internal string GetToolTip ()
1765 if (this.auto_tool_tip && string.IsNullOrEmpty (this.tool_tip_text))
1768 return this.tool_tip_text;
1771 internal void FireEvent (EventArgs e, ToolStripItemEventType met)
1773 // If we're disabled, don't fire any of these events, except Paint
1774 if (!this.Enabled && met != ToolStripItemEventType.Paint)
1778 case ToolStripItemEventType.MouseUp:
1779 this.HandleClick (((MouseEventArgs)e).Clicks, e);
1780 this.OnMouseUp ((MouseEventArgs)e);
1782 case ToolStripItemEventType.MouseDown:
1783 this.OnMouseDown ((MouseEventArgs)e);
1785 case ToolStripItemEventType.MouseEnter:
1786 this.OnMouseEnter (e);
1788 case ToolStripItemEventType.MouseHover:
1789 this.OnMouseHover (e);
1791 case ToolStripItemEventType.MouseLeave:
1792 this.OnMouseLeave (e);
1794 case ToolStripItemEventType.MouseMove:
1795 this.OnMouseMove ((MouseEventArgs)e);
1797 case ToolStripItemEventType.Paint:
1798 this.OnPaintInternal ((PaintEventArgs)e);
1800 case ToolStripItemEventType.Click:
1801 this.HandleClick (1, e);
1806 internal virtual void HandleClick (int mouse_clicks, EventArgs e)
1808 this.Parent.HandleItemClick (this);
1809 if (mouse_clicks == 2 && double_click_enabled)
1810 this.OnDoubleClick (e);
1815 internal virtual void SetPlacement (ToolStripItemPlacement placement)
1817 this.placement = placement;
1820 private void BeginAnimation ()
1822 if (image != null && ImageAnimator.CanAnimate (image)) {
1823 frame_handler = new EventHandler (OnAnimateImage);
1824 ImageAnimator.Animate (image, frame_handler);
1828 private void OnAnimateImage (object sender, EventArgs e)
1830 // This is called from a worker thread,BeginInvoke is used
1831 // so the control is updated from the correct thread
1833 // Check if we have a handle again, since it may have gotten
1834 // destroyed since the last time we checked.
1835 if (Parent == null || !Parent.IsHandleCreated)
1838 Parent.BeginInvoke (new EventHandler (UpdateAnimatedImage), new object[] { this, e });
1841 private void StopAnimation ()
1843 if (frame_handler == null)
1846 ImageAnimator.StopAnimate (image, frame_handler);
1847 frame_handler = null;
1850 private void UpdateAnimatedImage (object sender, EventArgs e)
1852 // Check if we have a handle again, since it may have gotten
1853 // destroyed since the last time we checked.
1854 if (Parent == null || !Parent.IsHandleCreated)
1857 ImageAnimator.UpdateFrames (image);
1861 internal bool ShowMargin {
1863 if (!this.IsOnDropDown)
1866 if (!(this.Owner is ToolStripDropDownMenu))
1869 ToolStripDropDownMenu tsddm = (ToolStripDropDownMenu)this.Owner;
1871 return tsddm.ShowCheckMargin || tsddm.ShowImageMargin;
1875 internal bool UseImageMargin {
1877 if (!this.IsOnDropDown)
1880 if (!(this.Owner is ToolStripDropDownMenu))
1883 ToolStripDropDownMenu tsddm = (ToolStripDropDownMenu)this.Owner;
1885 return tsddm.ShowImageMargin || tsddm.ShowCheckMargin;
1889 internal virtual bool InternalVisible {
1890 get { return this.visible; }
1891 set { this.visible = value; Invalidate (); }
1894 internal ToolStrip InternalOwner {
1896 if (this.owner != value) {
1898 this.CalculateAutoSize ();
1899 OnOwnerChanged (EventArgs.Empty);
1904 internal Point Location {
1905 get { return this.bounds.Location; }
1907 if (this.bounds.Location != value) {
1908 this.bounds.Location = value;
1909 this.OnLocationChanged (EventArgs.Empty);
1915 get { return this.bounds.Y; }
1917 if (this.bounds.Y != value) {
1918 this.bounds.Y = value;
1919 this.OnLocationChanged (EventArgs.Empty);
1925 get { return this.bounds.X; }
1927 if (this.bounds.X != value) {
1928 this.bounds.X = value;
1929 this.OnLocationChanged (EventArgs.Empty);
1934 internal int Right { get { return this.bounds.Right; } }
1935 internal int Bottom { get { return this.bounds.Bottom; } }
1938 #region IDropTarget Members
1939 void IDropTarget.OnDragDrop (DragEventArgs dragEvent)
1941 OnDragDrop (dragEvent);
1944 void IDropTarget.OnDragEnter (DragEventArgs dragEvent)
1946 OnDragEnter (dragEvent);
1949 void IDropTarget.OnDragLeave (EventArgs e)
1954 void IDropTarget.OnDragOver (DragEventArgs dragEvent)
1956 OnDragOver (dragEvent);
1960 #region UIA Framework: Methods, Properties and Events
1962 static object UIASelectionChangedEvent = new object ();
1964 internal event EventHandler UIASelectionChanged {
1965 add { Events.AddHandler (UIASelectionChangedEvent, value); }
1966 remove { Events.RemoveHandler (UIASelectionChangedEvent, value); }
1969 internal void OnUIASelectionChanged ()
1971 EventHandler eh = (EventHandler)(Events [UIASelectionChangedEvent]);
1973 eh (this, EventArgs.Empty);
1979 public class ToolStripItemAccessibleObject : AccessibleObject
1981 internal ToolStripItem owner_item;
1983 public ToolStripItemAccessibleObject (ToolStripItem ownerItem)
1985 if (ownerItem == null)
1986 throw new ArgumentNullException ("ownerItem");
1988 this.owner_item = ownerItem;
1989 base.default_action = string.Empty;
1990 base.keyboard_shortcut = string.Empty;
1991 base.name = string.Empty;
1992 base.value = string.Empty;
1995 #region Public Properties
1996 public override Rectangle Bounds {
1998 return owner_item.Visible ? owner_item.Bounds : Rectangle.Empty;
2002 public override string DefaultAction {
2003 get { return base.DefaultAction; }
2006 public override string Description {
2007 get { return base.Description; }
2010 public override string Help {
2011 get { return base.Help; }
2014 public override string KeyboardShortcut {
2015 get { return base.KeyboardShortcut; }
2018 public override string Name {
2020 if (base.name == string.Empty)
2021 return owner_item.Text;
2025 set { base.Name = value; }
2028 public override AccessibleObject Parent {
2029 get { return base.Parent; }
2032 public override AccessibleRole Role {
2033 get { return base.Role; }
2036 public override AccessibleStates State {
2037 get { return base.State; }
2041 #region Public Methods
2042 public void AddState (AccessibleStates state)
2047 public override void DoDefaultAction ()
2049 base.DoDefaultAction ();
2052 public override int GetHelpTopic (out string fileName)
2054 return base.GetHelpTopic (out fileName);
2057 public override AccessibleObject Navigate (AccessibleNavigation navigationDirection)
2059 return base.Navigate (navigationDirection);
2062 public override string ToString ()
2064 return string.Format ("ToolStripItemAccessibleObject: Owner = {0}", owner_item.ToString());
2070 internal class NoneExcludedImageIndexConverter : ImageIndexConverter