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)
32 using System.ComponentModel;
34 namespace System.Windows.Forms
36 [DefaultEvent ("Click")]
37 [DefaultProperty ("Text")]
38 [DesignTimeVisible (false)]
40 public abstract class ToolStripItem : Component, IDropTarget, IComponent, IDisposable
42 #region Private Variables
43 private AccessibleObject accessibility_object;
44 private string accessible_default_action_description;
45 private bool allow_drop;
46 private ToolStripItemAlignment alignment;
47 private AnchorStyles anchor;
48 private bool available;
49 private bool auto_size;
50 private bool auto_tool_tip;
51 private Color back_color;
52 private Image background_image;
53 private ImageLayout background_image_layout;
54 private Rectangle bounds;
55 private bool can_select;
56 private ToolStripItemDisplayStyle display_style;
57 private DockStyle dock;
58 private bool double_click_enabled;
60 private Size explicit_size;
62 private Color fore_color;
64 private ContentAlignment image_align;
65 private int image_index;
66 private string image_key;
67 private ToolStripItemImageScaling image_scaling;
68 private Color image_transparent_color;
69 private bool is_disposed;
70 internal bool is_pressed;
71 private bool is_selected;
72 private Padding margin;
74 private ToolStripItemOverflow overflow;
75 private ToolStrip owner;
76 internal ToolStripItem owner_item;
77 private Padding padding;
78 private ToolStripItemPlacement placement;
79 private RightToLeft right_to_left;
82 private ContentAlignment text_align;
83 private TextImageRelation text_image_relation;
84 private string tool_tip_text;
87 private ToolStrip parent;
88 private Size text_size;
91 #region Public Constructors
92 protected ToolStripItem ()
93 : this (String.Empty, null, null, String.Empty)
97 protected ToolStripItem (string text, Image image, EventHandler onClick)
98 : this (text, image, onClick, String.Empty)
102 protected ToolStripItem (string text, Image image, EventHandler onClick, string name)
104 this.alignment = ToolStripItemAlignment.Left;
105 this.anchor = AnchorStyles.Left | AnchorStyles.Top;
106 this.auto_size = true;
107 this.auto_tool_tip = this.DefaultAutoToolTip;
108 this.available = true;
109 this.back_color = Control.DefaultBackColor;
110 this.background_image_layout = ImageLayout.Tile;
111 this.can_select = true;
112 this.display_style = this.DefaultDisplayStyle;
113 this.dock = DockStyle.None;
115 this.font = new Font ("Tahoma", 8.25f);
116 this.fore_color = Control.DefaultForeColor;
118 this.image_align = ContentAlignment.MiddleCenter;
119 this.image_index = -1;
120 this.image_key = string.Empty;
121 this.image_scaling = ToolStripItemImageScaling.SizeToFit;
122 this.image_transparent_color = Color.Empty;
123 this.margin = this.DefaultMargin;
125 this.overflow = ToolStripItemOverflow.AsNeeded;
126 this.padding = this.DefaultPadding;
127 this.placement = ToolStripItemPlacement.None;
128 this.right_to_left = RightToLeft.Inherit;
129 this.bounds.Size = this.DefaultSize;
131 this.text_align = ContentAlignment.MiddleCenter;
132 this.text_image_relation = TextImageRelation.ImageBeforeText;
135 this.Click += onClick;
136 OnLayout (new LayoutEventArgs (null, string.Empty));
140 #region Public Properties
142 [EditorBrowsable (EditorBrowsableState.Advanced)]
143 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
144 public AccessibleObject AccessibilityObject {
146 if (this.accessibility_object == null)
147 this.accessibility_object = CreateAccessibilityInstance ();
149 return this.accessibility_object;
154 [EditorBrowsable (EditorBrowsableState.Advanced)]
155 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
156 public string AccessibleDefaultActionDescription {
158 if (this.accessibility_object == null)
161 return this.accessible_default_action_description;
163 set { this.accessible_default_action_description = value; }
167 [DefaultValue (null)]
168 public string AccessibleDescription {
170 if (this.accessibility_object == null)
173 return this.AccessibilityObject.Description;
175 set { this.AccessibilityObject.description = value; }
179 [DefaultValue (null)]
180 public string AccessibleName {
182 if (this.accessibility_object == null)
185 return this.AccessibilityObject.Name;
187 set { this.AccessibilityObject.Name = value; }
190 [DefaultValue (AccessibleRole.Default)]
191 public AccessibleRole AccessibleRole {
194 if (this.accessibility_object == null)
195 return AccessibleRole.Default;
197 return this.AccessibilityObject.Role;
199 set { this.AccessibilityObject.role = value; }
203 [DefaultValue (ToolStripItemAlignment.Left)]
204 public ToolStripItemAlignment Alignment {
205 get { return this.alignment; }
207 if (!Enum.IsDefined (typeof (ToolStripItemAlignment), value))
208 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripItemAlignment", value));
210 this.alignment = value;
215 public virtual bool AllowDrop {
217 return this.allow_drop;
221 this.allow_drop = value;
226 [DefaultValue (AnchorStyles.Top | AnchorStyles.Left)]
227 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
228 public AnchorStyles Anchor {
229 get { return this.anchor; }
230 set { this.anchor = value; }
234 [DefaultValue (true)]
235 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
236 [RefreshProperties (RefreshProperties.All)]
237 public bool AutoSize {
238 get { return this.auto_size; }
240 this.auto_size = value;
241 this.CalculateAutoSize ();
245 [DefaultValue (false)]
246 public bool AutoToolTip {
247 get { return this.auto_tool_tip; }
248 set { this.auto_tool_tip = value; }
252 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
253 public bool Available {
254 get { return this.available; }
256 if (this.available != value) {
260 if (this.parent != null)
261 parent.PerformLayout ();
263 OnAvailableChanged (EventArgs.Empty);
264 OnVisibleChanged (EventArgs.Empty);
269 public virtual Color BackColor {
270 get { return this.back_color; }
272 if (this.back_color != value) {
274 OnBackColorChanged (EventArgs.Empty);
281 [DefaultValue (null)]
282 public virtual Image BackgroundImage {
283 get { return this.background_image; }
285 if (this.background_image != value) {
286 this.background_image = value;
293 [DefaultValue (ImageLayout.Tile)]
294 public virtual ImageLayout BackgroundImageLayout {
295 get { return this.background_image_layout; }
297 if (this.background_image_layout != value) {
298 this.background_image_layout = value;
305 public virtual Rectangle Bounds {
306 get { return this.bounds; }
310 public virtual bool CanSelect {
311 get { return this.can_select; }
315 public Rectangle ContentRectangle {
317 // ToolStripLabels don't have a border
318 if (this is ToolStripLabel || this is ToolStripStatusLabel)
319 return new Rectangle (0, 0, this.bounds.Width, this.bounds.Height);
321 if (this is ToolStripDropDownButton && (this as ToolStripDropDownButton).ShowDropDownArrow)
322 return new Rectangle (2, 2, this.bounds.Width - 13, this.bounds.Height - 4);
324 return new Rectangle (2, 2, this.bounds.Width - 4, this.bounds.Height - 4);
328 public virtual ToolStripItemDisplayStyle DisplayStyle {
329 get { return this.display_style; }
331 if (this.display_style != value) {
332 this.display_style = value;
333 this.CalculateAutoSize ();
334 OnDisplayStyleChanged (EventArgs.Empty);
335 if (this.Parent != null)
336 this.Parent.PerformLayout ();
341 public bool IsDisposed {
342 get { return this.is_disposed; }
346 [DefaultValue (DockStyle.None)]
347 public DockStyle Dock {
348 get { return this.dock; }
350 if (this.dock != value) {
351 if (!Enum.IsDefined (typeof (DockStyle), value))
352 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for DockStyle", value));
355 this.CalculateAutoSize ();
360 [DefaultValue (false)]
361 public bool DoubleClickEnabled {
362 get { return this.double_click_enabled; }
363 set { this.double_click_enabled = value; }
367 [DefaultValue (true)]
368 public virtual bool Enabled {
369 get { return enabled; }
371 if (this.enabled != value) {
372 this.enabled = value;
373 OnEnabledChanged (EventArgs.Empty);
380 public virtual Font Font
382 get { return this.font; }
384 if (this.font != value) {
386 this.CalculateAutoSize ();
387 this.OnFontChanged (EventArgs.Empty);
393 public virtual Color ForeColor {
394 get { return this.fore_color; }
396 if (this.fore_color != value) {
397 this.fore_color = value;
398 this.OnForeColorChanged (EventArgs.Empty);
405 [EditorBrowsable (EditorBrowsableState.Always)]
406 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
408 get { return this.Size.Height; }
410 this.bounds.Height = value;
411 this.explicit_size.Height = value;
414 this.CalculateAutoSize ();
415 this.OnBoundsChanged ();
422 public virtual Image Image {
424 if (this.image != null)
427 if (this.image_index >= 0)
428 if (this.owner != null && this.owner.ImageList != null)
429 return this.owner.ImageList.Images[this.image_index];
432 if (!string.IsNullOrEmpty (this.image_key))
433 if (this.owner != null && this.owner.ImageList != null)
434 return this.owner.ImageList.Images[this.image_key];
439 if (this.image != value) {
441 this.image_index = -1;
442 this.image_key = string.Empty;
443 this.CalculateAutoSize ();
450 [DefaultValue (ContentAlignment.MiddleCenter)]
451 public ContentAlignment ImageAlign {
452 get { return this.image_align; }
454 if (!Enum.IsDefined (typeof (ContentAlignment), value))
455 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
457 this.image_align = value;
464 [RelatedImageList ("Owner.ImageList")]
465 [RefreshProperties (RefreshProperties.Repaint)]
466 public int ImageIndex {
467 get { return this.image_index; }
469 if (this.image_index != value) {
470 // Lamespec: MSDN says ArgumentException, tests say otherwise
472 throw new ArgumentOutOfRangeException ("ImageIndex cannot be less than -1");
474 this.image_index = value;
476 this.image_key = string.Empty;
477 this.CalculateAutoSize ();
485 [RelatedImageList ("Owner.ImageList")]
486 [RefreshProperties (RefreshProperties.Repaint)]
487 public string ImageKey
489 get { return this.image_key; }
491 if (this.image_key != value) {
493 this.image_index = -1;
494 this.image_key = value;
495 this.CalculateAutoSize ();
502 [DefaultValue (ToolStripItemImageScaling.SizeToFit)]
503 public ToolStripItemImageScaling ImageScaling {
504 get { return this.image_scaling; }
506 this.image_scaling = value;
507 this.CalculateAutoSize ();
513 [MonoTODO ("Stub, doesn't actually do anything yet.")]
514 public Color ImageTransparentColor {
515 get { return this.image_transparent_color; }
516 set { this.image_transparent_color = value; }
520 public bool IsOnDropDown {
522 if (this.parent != null && this.parent is ToolStripDropDown)
530 public bool IsOnOverflow {
531 get { return this.placement == ToolStripItemPlacement.Overflow; }
534 public Padding Margin {
535 get { return this.margin; }
538 this.CalculateAutoSize ();
542 [DefaultValue (null)]
545 get { return this.name; }
546 set { this.name = value; }
549 [DefaultValue (ToolStripItemOverflow.AsNeeded)]
550 public ToolStripItemOverflow Overflow {
551 get { return this.overflow; }
553 if (this.overflow != value) {
554 if (!Enum.IsDefined (typeof (ToolStripItemOverflow), value))
555 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripItemOverflow", value));
557 this.overflow = value;
560 owner.PerformLayout ();
566 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
567 public ToolStrip Owner {
568 get { return this.owner; }
570 if (this.owner != value) {
572 this.CalculateAutoSize ();
573 OnOwnerChanged (EventArgs.Empty);
579 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
580 public ToolStripItem OwnerItem {
581 get { return this.owner_item; }
584 public virtual Padding Padding {
585 get { return this.padding; }
587 this.padding = value;
588 this.CalculateAutoSize ();
594 public ToolStripItemPlacement Placement {
595 get { return this.placement; }
599 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
600 public virtual bool Pressed { get { return this.is_pressed; } }
602 [MonoTODO ("Stub, not implemented")]
604 public virtual RightToLeft RightToLeft {
605 get { return this.right_to_left; }
606 set { this.right_to_left = value; }
610 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
611 public virtual bool Selected { get { return this.is_selected; } }
614 public virtual Size Size {
616 if (!this.AutoSize && this.explicit_size != Size.Empty)
617 return this.explicit_size;
619 return this.bounds.Size;
622 this.bounds.Size = value;
623 this.explicit_size = value;
626 this.CalculateAutoSize ();
627 this.OnBoundsChanged ();
632 [Localizable (false)]
634 [DefaultValue (null)]
635 [TypeConverter (typeof (StringConverter))]
637 get { return this.tag; }
638 set { this.tag = value; }
643 public virtual string Text
645 get { return this.text; }
647 if (this.text != value) {
649 this.CalculateAutoSize ();
650 this.OnTextChanged (EventArgs.Empty);
657 [DefaultValue (ContentAlignment.MiddleCenter)]
658 public virtual ContentAlignment TextAlign {
659 get { return this.text_align; }
661 if (!Enum.IsDefined (typeof (ContentAlignment), value))
662 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
663 this.text_align = value;
669 [DefaultValue (TextImageRelation.ImageBeforeText)]
670 public TextImageRelation TextImageRelation {
671 get { return this.text_image_relation; }
673 this.text_image_relation = value;
674 this.CalculateAutoSize ();
680 public string ToolTipText {
681 get { return this.tool_tip_text; }
682 set { this.tool_tip_text = value; }
686 public bool Visible {
688 if (this.parent == null)
691 return this.visible && this.parent.Visible;
694 if (this.visible != value) {
695 this.available = value;
696 this.SetVisibleCore (value);
702 [EditorBrowsable (EditorBrowsableState.Always)]
703 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
705 get { return this.Size.Width; }
707 this.bounds.Width = value;
708 this.explicit_size.Width = value;
711 this.CalculateAutoSize ();
712 this.OnBoundsChanged ();
719 #region Protected Properties
720 protected virtual bool DefaultAutoToolTip { get { return false; } }
721 protected virtual ToolStripItemDisplayStyle DefaultDisplayStyle { get { return ToolStripItemDisplayStyle.ImageAndText; } }
722 protected internal virtual Padding DefaultMargin { get { return new Padding (0, 1, 0, 2); } }
723 protected virtual Padding DefaultPadding { get { return new Padding (); } }
724 protected virtual Size DefaultSize { get { return new Size (23, 23); } }
725 protected internal virtual bool DismissWhenClicked { get { return true; } }
727 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
728 protected internal ToolStrip Parent {
729 get { return this.parent; }
731 if (this.parent != value) {
732 ToolStrip old_parent = this.parent;
734 OnParentChanged(old_parent, this.parent);
738 protected internal virtual bool ShowKeyboardCues { get { return false; } }
741 #region Public Methods
742 public ToolStrip GetCurrentParent ()
747 public virtual Size GetPreferredSize (Size constrainingSize)
749 return this.CalculatePreferredSize (constrainingSize);
752 public void Invalidate ()
755 parent.Invalidate (this.bounds);
758 public void Invalidate (Rectangle r)
761 parent.Invalidate (r);
764 public void PerformClick ()
766 this.OnClick (EventArgs.Empty);
769 [EditorBrowsable (EditorBrowsableState.Never)]
770 public virtual void ResetBackColor () { this.BackColor = Control.DefaultBackColor; }
772 [EditorBrowsable (EditorBrowsableState.Never)]
773 public virtual void ResetDisplayStyle () { this.display_style = this.DefaultDisplayStyle; }
775 [EditorBrowsable (EditorBrowsableState.Never)]
776 public virtual void ResetFont () { this.font = new Font ("Tahoma", 8.25f); }
778 [EditorBrowsable (EditorBrowsableState.Never)]
779 public virtual void ResetForeColor () { this.ForeColor = Control.DefaultForeColor; }
781 [EditorBrowsable (EditorBrowsableState.Never)]
782 public virtual void ResetImage () { this.image = null; }
784 [EditorBrowsable (EditorBrowsableState.Never)]
785 public void ResetMargin () { this.margin = this.DefaultMargin; }
787 [EditorBrowsable (EditorBrowsableState.Never)]
788 public void ResetPadding () { this.padding = this.DefaultPadding; }
790 public void Select ()
792 if (!this.is_selected && this.CanSelect) {
793 this.is_selected = true;
795 this.Parent.NotifySelectedChanged (this);
799 public override string ToString ()
805 #region Protected Methods
806 [EditorBrowsable (EditorBrowsableState.Advanced)]
807 protected virtual AccessibleObject CreateAccessibilityInstance ()
809 return new ToolStripItemAccessibleObject (this);
812 protected override void Dispose (bool disposing)
814 if (!is_disposed && disposing)
817 base.Dispose (disposing);
820 protected virtual void OnAvailableChanged (EventArgs e)
822 EventHandler eh = (EventHandler)(Events [AvailableChangedEvent]);
827 [EditorBrowsable (EditorBrowsableState.Advanced)]
828 protected virtual void OnBackColorChanged (EventArgs e)
830 EventHandler eh = (EventHandler)(Events [BackColorChangedEvent]);
835 protected virtual void OnBoundsChanged ()
837 OnLayout (new LayoutEventArgs(null, ""));
840 protected virtual void OnClick (EventArgs e)
842 EventHandler eh = (EventHandler)(Events [ClickEvent]);
847 [EditorBrowsable (EditorBrowsableState.Advanced)]
848 protected virtual void OnDisplayStyleChanged (EventArgs e)
850 EventHandler eh = (EventHandler)(Events [DisplayStyleChangedEvent]);
855 protected virtual void OnDoubleClick (EventArgs e)
857 EventHandler eh = (EventHandler)(Events [DoubleClickEvent]);
861 if (!double_click_enabled)
865 void IDropTarget.OnDragDrop (DragEventArgs e)
870 void IDropTarget.OnDragEnter (DragEventArgs e)
875 void IDropTarget.OnDragLeave (EventArgs e)
880 void IDropTarget.OnDragOver (DragEventArgs e)
885 protected virtual void OnEnabledChanged (EventArgs e)
887 EventHandler eh = (EventHandler)(Events [EnabledChangedEvent]);
892 [EditorBrowsable (EditorBrowsableState.Advanced)]
893 protected virtual void OnFontChanged (EventArgs e)
897 [EditorBrowsable (EditorBrowsableState.Advanced)]
898 protected virtual void OnForeColorChanged (EventArgs e)
900 EventHandler eh = (EventHandler)(Events [ForeColorChangedEvent]);
905 protected virtual void OnLayout (LayoutEventArgs e)
909 protected virtual void OnLocationChanged (EventArgs e)
911 EventHandler eh = (EventHandler)(Events [LocationChangedEvent]);
916 protected virtual void OnMouseDown (MouseEventArgs e)
919 this.is_pressed = true;
922 MouseEventHandler eh = (MouseEventHandler)(Events [MouseDownEvent]);
928 protected virtual void OnMouseEnter (EventArgs e)
932 EventHandler eh = (EventHandler)(Events [MouseEnterEvent]);
937 protected virtual void OnMouseHover (EventArgs e)
940 EventHandler eh = (EventHandler)(Events [MouseHoverEvent]);
946 protected virtual void OnMouseLeave (EventArgs e)
948 if (this.CanSelect) {
949 this.is_selected = false;
950 this.is_pressed = false;
954 EventHandler eh = (EventHandler)(Events [MouseLeaveEvent]);
959 protected virtual void OnMouseMove (MouseEventArgs e)
962 MouseEventHandler eh = (MouseEventHandler)(Events [MouseMoveEvent]);
968 protected virtual void OnMouseUp (MouseEventArgs e)
971 this.is_pressed = false;
974 if (this.IsOnDropDown)
975 if (!(this is ToolStripDropDownItem) || (this as ToolStripDropDownItem).DropDown.Visible == false) {
976 if ((this.Parent as ToolStripDropDown).OwnerItem != null)
977 ((this.Parent as ToolStripDropDown).OwnerItem as ToolStripDropDownItem).HideDropDown ();
979 (this.Parent as ToolStripDropDown).Hide ();
983 MouseEventHandler eh = (MouseEventHandler)(Events [MouseUpEvent]);
989 protected virtual void OnOwnerChanged (EventArgs e)
991 EventHandler eh = (EventHandler)(Events [OwnerChangedEvent]);
996 protected internal virtual void OnOwnerFontChanged (EventArgs e)
1000 protected virtual void OnPaint (PaintEventArgs e)
1002 if (this.parent != null)
1003 this.parent.Renderer.DrawItemBackground (new ToolStripItemRenderEventArgs (e.Graphics, this));
1005 PaintEventHandler eh = (PaintEventHandler)(Events [PaintEvent]);
1010 // This is never called.
1011 protected virtual void OnParentBackColorChanged (EventArgs e)
1015 protected virtual void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
1017 if (oldParent != null)
1018 oldParent.PerformLayout ();
1020 if (newParent != null)
1021 newParent.PerformLayout ();
1024 protected internal virtual void OnParentEnabledChanged (EventArgs e)
1026 this.OnEnabledChanged (e);
1029 // This is never called.
1030 protected virtual void OnParentForeColorChanged (EventArgs e)
1034 [EditorBrowsable (EditorBrowsableState.Advanced)]
1035 protected virtual void OnTextChanged (EventArgs e)
1037 EventHandler eh = (EventHandler)(Events [TextChangedEvent]);
1042 protected virtual void OnVisibleChanged (EventArgs e)
1044 EventHandler eh = (EventHandler)(Events [VisibleChangedEvent]);
1049 protected internal virtual bool ProcessCmdKey (ref Message m, Keys keyData)
1054 protected internal virtual void SetBounds (Rectangle bounds)
1056 if (this.bounds != bounds) {
1057 this.bounds = bounds;
1062 protected virtual void SetVisibleCore (bool visible)
1064 this.visible = visible;
1065 this.OnVisibleChanged (EventArgs.Empty);
1070 #region Public Events
1071 static object AvailableChangedEvent = new object ();
1072 static object BackColorChangedEvent = new object ();
1073 static object ClickEvent = new object ();
1074 static object DisplayStyleChangedEvent = new object ();
1075 static object DoubleClickEvent = new object ();
1076 static object EnabledChangedEvent = new object ();
1077 static object ForeColorChangedEvent = new object ();
1078 static object LocationChangedEvent = new object ();
1079 static object MouseDownEvent = new object ();
1080 static object MouseEnterEvent = new object ();
1081 static object MouseHoverEvent = new object ();
1082 static object MouseLeaveEvent = new object ();
1083 static object MouseMoveEvent = new object ();
1084 static object MouseUpEvent = new object ();
1085 static object OwnerChangedEvent = new object ();
1086 static object PaintEvent = new object ();
1087 static object TextChangedEvent = new object ();
1088 static object VisibleChangedEvent = new object ();
1091 public event EventHandler AvailableChanged {
1092 add { Events.AddHandler (AvailableChangedEvent, value); }
1093 remove {Events.RemoveHandler (AvailableChangedEvent, value); }
1096 public event EventHandler BackColorChanged {
1097 add { Events.AddHandler (BackColorChangedEvent, value); }
1098 remove {Events.RemoveHandler (BackColorChangedEvent, value); }
1101 public event EventHandler Click {
1102 add { Events.AddHandler (ClickEvent, value); }
1103 remove {Events.RemoveHandler (ClickEvent, value); }
1106 public event EventHandler DisplayStyleChanged {
1107 add { Events.AddHandler (DisplayStyleChangedEvent, value); }
1108 remove {Events.RemoveHandler (DisplayStyleChangedEvent, value); }
1111 public event EventHandler DoubleClick {
1112 add { Events.AddHandler (DoubleClickEvent, value); }
1113 remove {Events.RemoveHandler (DoubleClickEvent, value); }
1116 public event EventHandler EnabledChanged {
1117 add { Events.AddHandler (EnabledChangedEvent, value); }
1118 remove {Events.RemoveHandler (EnabledChangedEvent, value); }
1121 public event EventHandler ForeColorChanged {
1122 add { Events.AddHandler (ForeColorChangedEvent, value); }
1123 remove {Events.RemoveHandler (ForeColorChangedEvent, value); }
1126 public event EventHandler LocationChanged {
1127 add { Events.AddHandler (LocationChangedEvent, value); }
1128 remove {Events.RemoveHandler (LocationChangedEvent, value); }
1131 public event MouseEventHandler MouseDown {
1132 add { Events.AddHandler (MouseDownEvent, value); }
1133 remove {Events.RemoveHandler (MouseDownEvent, value); }
1136 public event EventHandler MouseEnter {
1137 add { Events.AddHandler (MouseEnterEvent, value); }
1138 remove {Events.RemoveHandler (MouseEnterEvent, value); }
1141 public event EventHandler MouseHover {
1142 add { Events.AddHandler (MouseHoverEvent, value); }
1143 remove {Events.RemoveHandler (MouseHoverEvent, value); }
1146 public event EventHandler MouseLeave {
1147 add { Events.AddHandler (MouseLeaveEvent, value); }
1148 remove {Events.RemoveHandler (MouseLeaveEvent, value); }
1151 public event MouseEventHandler MouseMove {
1152 add { Events.AddHandler (MouseMoveEvent, value); }
1153 remove {Events.RemoveHandler (MouseMoveEvent, value); }
1156 public event MouseEventHandler MouseUp {
1157 add { Events.AddHandler (MouseUpEvent, value); }
1158 remove {Events.RemoveHandler (MouseUpEvent, value); }
1161 public event EventHandler OwnerChanged {
1162 add { Events.AddHandler (OwnerChangedEvent, value); }
1163 remove {Events.RemoveHandler (OwnerChangedEvent, value); }
1166 public event PaintEventHandler Paint {
1167 add { Events.AddHandler (PaintEvent, value); }
1168 remove {Events.RemoveHandler (PaintEvent, value); }
1171 public event EventHandler TextChanged {
1172 add { Events.AddHandler (TextChangedEvent, value); }
1173 remove {Events.RemoveHandler (TextChangedEvent, value); }
1176 public event EventHandler VisibleChanged {
1177 add { Events.AddHandler (VisibleChangedEvent, value); }
1178 remove {Events.RemoveHandler (VisibleChangedEvent, value); }
1182 #region Internal Methods
1183 internal Rectangle AlignInRectangle (Rectangle outer, Size inner, ContentAlignment align)
1188 if (align == ContentAlignment.BottomLeft || align == ContentAlignment.MiddleLeft || align == ContentAlignment.TopLeft)
1190 else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.MiddleCenter || align == ContentAlignment.TopCenter)
1191 x = Math.Max (outer.X + ((outer.Width - inner.Width) / 2), outer.Left);
1192 else if (align == ContentAlignment.BottomRight || align == ContentAlignment.MiddleRight || align == ContentAlignment.TopRight)
1193 x = outer.Right - inner.Width;
1194 if (align == ContentAlignment.TopCenter || align == ContentAlignment.TopLeft || align == ContentAlignment.TopRight)
1196 else if (align == ContentAlignment.MiddleCenter || align == ContentAlignment.MiddleLeft || align == ContentAlignment.MiddleRight)
1197 y = outer.Y + (outer.Height - inner.Height) / 2;
1198 else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.BottomRight || align == ContentAlignment.BottomLeft)
1199 y = outer.Bottom - inner.Height;
1201 return new Rectangle (x, y, Math.Min (inner.Width, outer.Width), Math.Min (inner.Height, outer.Height));
1204 internal void CalculateAutoSize ()
1206 this.text_size = TextRenderer.MeasureText (this.Text == null ? string.Empty: this.text, this.Font, Size.Empty, TextFormatFlags.HidePrefix);
1208 if (!this.auto_size || this is ToolStripControlHost)
1210 //this.text_size.Width += 6;
1212 Size final_size = this.CalculatePreferredSize (Size.Empty);
1214 if (final_size != this.Size) {
1215 this.bounds.Width = final_size.Width;
1216 if (this.parent != null)
1217 this.parent.PerformLayout ();
1221 internal virtual Size CalculatePreferredSize (Size constrainingSize)
1223 if (!this.auto_size)
1224 return this.explicit_size;
1226 Size preferred_size = this.DefaultSize;
1228 switch (this.display_style) {
1229 case ToolStripItemDisplayStyle.Text:
1230 int width = text_size.Width + this.padding.Horizontal;
1231 int height = text_size.Height + this.padding.Vertical;
1232 preferred_size = new Size (width, height);
1234 case ToolStripItemDisplayStyle.Image:
1235 if (this.Image == null)
1236 preferred_size = this.DefaultSize;
1238 switch (this.image_scaling) {
1239 case ToolStripItemImageScaling.None:
1240 preferred_size = this.Image.Size;
1242 case ToolStripItemImageScaling.SizeToFit:
1243 if (this.parent == null)
1244 preferred_size = this.Image.Size;
1246 preferred_size = this.parent.ImageScalingSize;
1251 case ToolStripItemDisplayStyle.ImageAndText:
1252 int width2 = text_size.Width + this.padding.Horizontal;
1253 int height2 = text_size.Height + this.padding.Vertical;
1255 if (this.Image != null) {
1256 switch (this.text_image_relation) {
1257 case TextImageRelation.Overlay:
1258 width2 = Math.Max (width2, this.Image.Width);
1259 height2 = Math.Max (height2, this.Image.Height);
1261 case TextImageRelation.ImageAboveText:
1262 case TextImageRelation.TextAboveImage:
1263 width2 = Math.Max (width2, this.Image.Width);
1264 height2 += this.Image.Height;
1266 case TextImageRelation.ImageBeforeText:
1267 case TextImageRelation.TextBeforeImage:
1268 height2 = Math.Max (height2, this.Image.Height);
1269 width2 += this.Image.Width;
1274 preferred_size = new Size (width2, height2);
1278 if (!(this is ToolStripLabel)) { // Everything but labels have a border
1279 preferred_size.Height += 4;
1280 preferred_size.Width += 4;
1283 // Account for ToolStripDropDownButton's drop down arrow
1284 if (this is ToolStripDropDownButton && (this as ToolStripDropDownButton).ShowDropDownArrow)
1285 preferred_size.Width += 9;
1287 return preferred_size;
1290 internal void CalculateTextAndImageRectangles (out Rectangle text_rect, out Rectangle image_rect)
1292 this.CalculateTextAndImageRectangles (this.ContentRectangle, out text_rect, out image_rect);
1295 internal void CalculateTextAndImageRectangles (Rectangle contentRectangle, out Rectangle text_rect, out Rectangle image_rect)
1297 text_rect = Rectangle.Empty;
1298 image_rect = Rectangle.Empty;
1300 switch (this.display_style) {
1301 case ToolStripItemDisplayStyle.None:
1303 case ToolStripItemDisplayStyle.Text:
1304 if (this.text != string.Empty)
1305 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1307 case ToolStripItemDisplayStyle.Image:
1308 if (this.Image != null)
1309 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1311 case ToolStripItemDisplayStyle.ImageAndText:
1312 if (this.text != string.Empty && this.Image == null)
1313 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1314 else if (this.text == string.Empty && this.Image != null)
1315 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1316 else if (this.text == string.Empty && this.Image == null)
1319 Rectangle text_area;
1320 Rectangle image_area;
1322 switch (this.text_image_relation) {
1323 case TextImageRelation.Overlay:
1324 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1325 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1327 case TextImageRelation.ImageAboveText:
1328 text_area = new Rectangle (contentRectangle.Left, contentRectangle.Bottom - (text_size.Height - 4), contentRectangle.Width, text_size.Height - 4);
1329 image_area = new Rectangle (contentRectangle.Left, contentRectangle.Top, contentRectangle.Width, contentRectangle.Height - text_area.Height);
1331 text_rect = AlignInRectangle (text_area, this.text_size, this.text_align);
1332 image_rect = AlignInRectangle (image_area, GetImageSize (), this.image_align);
1334 case TextImageRelation.TextAboveImage:
1335 text_area = new Rectangle (contentRectangle.Left, contentRectangle.Top, contentRectangle.Width, text_size.Height - 4);
1336 image_area = new Rectangle (contentRectangle.Left, text_area.Bottom, contentRectangle.Width, contentRectangle.Height - text_area.Height);
1338 text_rect = AlignInRectangle (text_area, this.text_size, this.text_align);
1339 image_rect = AlignInRectangle (image_area, GetImageSize (), this.image_align);
1341 case TextImageRelation.ImageBeforeText:
1342 LayoutTextBeforeOrAfterImage (contentRectangle, false, text_size, GetImageSize (), text_align, image_align, out text_rect, out image_rect);
1344 case TextImageRelation.TextBeforeImage:
1345 LayoutTextBeforeOrAfterImage (contentRectangle, true, text_size, GetImageSize (), text_align, image_align, out text_rect, out image_rect);
1353 private void LayoutTextBeforeOrAfterImage (Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, ContentAlignment textAlign, ContentAlignment imageAlign, out Rectangle textRect, out Rectangle imageRect)
1355 int element_spacing = 0; // Spacing between the Text and the Image
1356 int total_width = textSize.Width + element_spacing + imageSize.Width;
1357 int excess_width = totalArea.Width - total_width;
1360 Rectangle final_text_rect;
1361 Rectangle final_image_rect;
1363 HorizontalAlignment h_text = GetHorizontalAlignment (textAlign);
1364 HorizontalAlignment h_image = GetHorizontalAlignment (imageAlign);
1366 if (h_image == HorizontalAlignment.Left)
1368 else if (h_image == HorizontalAlignment.Right && h_text == HorizontalAlignment.Right)
1369 offset = excess_width;
1370 else if (h_image == HorizontalAlignment.Center && (h_text == HorizontalAlignment.Left || h_text == HorizontalAlignment.Center))
1371 offset += (int)(excess_width / 3);
1373 offset += (int)(2 * (excess_width / 3));
1376 final_text_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
1377 final_image_rect = new Rectangle (final_text_rect.Right + element_spacing, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
1379 final_image_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
1380 final_text_rect = new Rectangle (final_image_rect.Right + element_spacing, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
1383 textRect = final_text_rect;
1384 imageRect = final_image_rect;
1387 private HorizontalAlignment GetHorizontalAlignment (ContentAlignment align)
1390 case ContentAlignment.BottomLeft:
1391 case ContentAlignment.MiddleLeft:
1392 case ContentAlignment.TopLeft:
1393 return HorizontalAlignment.Left;
1394 case ContentAlignment.BottomCenter:
1395 case ContentAlignment.MiddleCenter:
1396 case ContentAlignment.TopCenter:
1397 return HorizontalAlignment.Center;
1398 case ContentAlignment.BottomRight:
1399 case ContentAlignment.MiddleRight:
1400 case ContentAlignment.TopRight:
1401 return HorizontalAlignment.Right;
1404 return HorizontalAlignment.Left;
1407 private Size GetImageSize ()
1409 if (this.Image == null)
1412 if (this.image_scaling == ToolStripItemImageScaling.None)
1413 return this.Image.Size;
1415 if (this.Parent == null)
1418 return this.Parent.ImageScalingSize;
1421 internal string GetToolTip ()
1423 if (this.auto_tool_tip && string.IsNullOrEmpty (this.tool_tip_text))
1426 return this.tool_tip_text;
1429 internal void FireEvent (EventArgs e, ToolStripItemEventType met)
1432 case ToolStripItemEventType.MouseUp:
1433 this.OnMouseUp ((MouseEventArgs)e);
1434 this.OnClick ((MouseEventArgs)e);
1436 case ToolStripItemEventType.MouseDown:
1437 this.OnMouseDown ((MouseEventArgs)e);
1439 case ToolStripItemEventType.MouseEnter:
1440 this.OnMouseEnter (e);
1442 case ToolStripItemEventType.MouseHover:
1443 this.OnMouseHover (e);
1445 case ToolStripItemEventType.MouseLeave:
1446 this.OnMouseLeave (e);
1448 case ToolStripItemEventType.MouseMove:
1449 this.OnMouseMove ((MouseEventArgs)e);
1451 case ToolStripItemEventType.Paint:
1452 this.OnPaint ((PaintEventArgs)e);
1454 case ToolStripItemEventType.Click:
1460 internal virtual void SetPlacement (ToolStripItemPlacement placement)
1462 this.placement = placement;
1465 internal bool InternalVisible {
1466 get { return this.visible; }
1467 set { this.visible = value; }
1470 internal Point Location {
1471 get { return this.bounds.Location; }
1473 if (this.bounds.Location != value) {
1474 this.bounds.Location = value;
1475 this.OnLocationChanged (EventArgs.Empty);
1481 get { return this.bounds.Y; }
1483 if (this.bounds.Y != value) {
1484 this.bounds.Y = value;
1485 this.OnLocationChanged (EventArgs.Empty);
1491 get { return this.bounds.X; }
1493 if (this.bounds.X != value) {
1494 this.bounds.X = value;
1495 this.OnLocationChanged (EventArgs.Empty);
1500 internal int Right { get { return this.bounds.Right; } }
1501 internal int Bottom { get { return this.bounds.Bottom; } }
1504 public class ToolStripItemAccessibleObject : AccessibleObject
1506 private ToolStripItem owner_item;
1508 public ToolStripItemAccessibleObject (ToolStripItem ownerItem)
1510 if (ownerItem == null)
1511 throw new ArgumentNullException ("ownerItem");
1513 this.owner_item = ownerItem;
1514 base.default_action = string.Empty;
1515 base.keyboard_shortcut = string.Empty;
1516 base.name = string.Empty;
1517 base.value = string.Empty;
1520 #region Public Properties
1521 public override Rectangle Bounds {
1523 return owner_item.Visible ? owner_item.Bounds : Rectangle.Empty;
1527 public override string DefaultAction {
1528 get { return base.DefaultAction; }
1531 public override string Description {
1532 get { return base.Description; }
1535 public override string Help {
1536 get { return base.Help; }
1539 public override string KeyboardShortcut {
1540 get { return base.KeyboardShortcut; }
1543 public override string Name {
1545 if (base.name == string.Empty)
1546 return owner_item.Text;
1550 set { base.Name = value; }
1553 public override AccessibleObject Parent {
1554 get { return base.Parent; }
1557 public override AccessibleRole Role {
1558 get { return base.Role; }
1561 public override AccessibleStates State {
1562 get { return base.State; }
1566 #region Public Methods
1567 public void AddState (AccessibleStates state)
1572 public override void DoDefaultAction ()
1574 base.DoDefaultAction ();
1577 public override int GetHelpTopic (out string FileName)
1579 return base.GetHelpTopic (out FileName);
1582 public override AccessibleObject Navigate (AccessibleNavigation navdir)
1584 return base.Navigate (navdir);
1587 public override string ToString ()
1589 return string.Format ("ToolStripItemAccessibleObject: Owner = {0}", owner_item.ToString());