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;
80 private bool right_to_left_auto_mirror_image;
83 private ContentAlignment text_align;
84 private TextImageRelation text_image_relation;
85 private string tool_tip_text;
88 private ToolStrip parent;
89 private Size text_size;
92 #region Public Constructors
93 protected ToolStripItem ()
94 : this (String.Empty, null, null, String.Empty)
98 protected ToolStripItem (string text, Image image, EventHandler onClick)
99 : this (text, image, onClick, String.Empty)
103 protected ToolStripItem (string text, Image image, EventHandler onClick, string name)
105 this.alignment = ToolStripItemAlignment.Left;
106 this.anchor = AnchorStyles.Left | AnchorStyles.Top;
107 this.auto_size = true;
108 this.auto_tool_tip = this.DefaultAutoToolTip;
109 this.available = true;
110 this.back_color = Control.DefaultBackColor;
111 this.background_image_layout = ImageLayout.Tile;
112 this.can_select = true;
113 this.display_style = this.DefaultDisplayStyle;
114 this.dock = DockStyle.None;
116 this.font = new Font ("Tahoma", 8.25f);
117 this.fore_color = Control.DefaultForeColor;
119 this.image_align = ContentAlignment.MiddleCenter;
120 this.image_index = -1;
121 this.image_key = string.Empty;
122 this.image_scaling = ToolStripItemImageScaling.SizeToFit;
123 this.image_transparent_color = Color.Empty;
124 this.margin = this.DefaultMargin;
126 this.overflow = ToolStripItemOverflow.AsNeeded;
127 this.padding = this.DefaultPadding;
128 this.placement = ToolStripItemPlacement.None;
129 this.right_to_left = RightToLeft.Inherit;
130 this.bounds.Size = this.DefaultSize;
132 this.text_align = ContentAlignment.MiddleCenter;
133 this.text_image_relation = TextImageRelation.ImageBeforeText;
136 this.Click += onClick;
137 OnLayout (new LayoutEventArgs (null, string.Empty));
141 #region Public Properties
143 [EditorBrowsable (EditorBrowsableState.Advanced)]
144 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
145 public AccessibleObject AccessibilityObject {
147 if (this.accessibility_object == null)
148 this.accessibility_object = CreateAccessibilityInstance ();
150 return this.accessibility_object;
155 [EditorBrowsable (EditorBrowsableState.Advanced)]
156 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
157 public string AccessibleDefaultActionDescription {
159 if (this.accessibility_object == null)
162 return this.accessible_default_action_description;
164 set { this.accessible_default_action_description = value; }
168 [DefaultValue (null)]
169 public string AccessibleDescription {
171 if (this.accessibility_object == null)
174 return this.AccessibilityObject.Description;
176 set { this.AccessibilityObject.description = value; }
180 [DefaultValue (null)]
181 public string AccessibleName {
183 if (this.accessibility_object == null)
186 return this.AccessibilityObject.Name;
188 set { this.AccessibilityObject.Name = value; }
191 [DefaultValue (AccessibleRole.Default)]
192 public AccessibleRole AccessibleRole {
195 if (this.accessibility_object == null)
196 return AccessibleRole.Default;
198 return this.AccessibilityObject.Role;
200 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 public Color ImageTransparentColor {
514 get { return this.image_transparent_color; }
515 set { this.image_transparent_color = value; }
519 public bool IsOnDropDown {
521 if (this.parent != null && this.parent is ToolStripDropDown)
529 public bool IsOnOverflow {
530 get { return this.placement == ToolStripItemPlacement.Overflow; }
533 public Padding Margin {
534 get { return this.margin; }
537 this.CalculateAutoSize ();
541 [DefaultValue (null)]
544 get { return this.name; }
545 set { this.name = value; }
548 [DefaultValue (ToolStripItemOverflow.AsNeeded)]
549 public ToolStripItemOverflow Overflow {
550 get { return this.overflow; }
552 if (this.overflow != value) {
553 if (!Enum.IsDefined (typeof (ToolStripItemOverflow), value))
554 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripItemOverflow", value));
556 this.overflow = value;
559 owner.PerformLayout ();
565 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
566 public ToolStrip Owner {
567 get { return this.owner; }
569 if (this.owner != value) {
571 this.CalculateAutoSize ();
572 OnOwnerChanged (EventArgs.Empty);
578 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
579 public ToolStripItem OwnerItem {
580 get { return this.owner_item; }
583 public virtual Padding Padding {
584 get { return this.padding; }
586 this.padding = value;
587 this.CalculateAutoSize ();
593 public ToolStripItemPlacement Placement {
594 get { return this.placement; }
598 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
599 public virtual bool Pressed { get { return this.is_pressed; } }
601 [MonoTODO ("Stub, not implemented")]
603 public virtual RightToLeft RightToLeft {
604 get { return this.right_to_left; }
605 set { this.right_to_left = value; }
609 public bool RightToLeftAutoMirrorImage {
610 get { return this.right_to_left_auto_mirror_image; }
612 if (this.right_to_left_auto_mirror_image != value) {
613 this.right_to_left_auto_mirror_image = value;
620 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
621 public virtual bool Selected { get { return this.is_selected; } }
624 public virtual Size Size {
626 if (!this.AutoSize && this.explicit_size != Size.Empty)
627 return this.explicit_size;
629 return this.bounds.Size;
632 this.bounds.Size = value;
633 this.explicit_size = value;
636 this.CalculateAutoSize ();
637 this.OnBoundsChanged ();
642 [Localizable (false)]
644 [DefaultValue (null)]
645 [TypeConverter (typeof (StringConverter))]
647 get { return this.tag; }
648 set { this.tag = value; }
653 public virtual string Text
655 get { return this.text; }
657 if (this.text != value) {
659 this.CalculateAutoSize ();
660 this.OnTextChanged (EventArgs.Empty);
667 [DefaultValue (ContentAlignment.MiddleCenter)]
668 public virtual ContentAlignment TextAlign {
669 get { return this.text_align; }
671 if (!Enum.IsDefined (typeof (ContentAlignment), value))
672 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
673 this.text_align = value;
679 [DefaultValue (TextImageRelation.ImageBeforeText)]
680 public TextImageRelation TextImageRelation {
681 get { return this.text_image_relation; }
683 this.text_image_relation = value;
684 this.CalculateAutoSize ();
690 public string ToolTipText {
691 get { return this.tool_tip_text; }
692 set { this.tool_tip_text = value; }
696 public bool Visible {
698 if (this.parent == null)
701 return this.visible && this.parent.Visible;
704 if (this.visible != value) {
705 this.available = value;
706 this.SetVisibleCore (value);
712 [EditorBrowsable (EditorBrowsableState.Always)]
713 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
715 get { return this.Size.Width; }
717 this.bounds.Width = value;
718 this.explicit_size.Width = value;
721 this.CalculateAutoSize ();
722 this.OnBoundsChanged ();
729 #region Protected Properties
730 protected virtual bool DefaultAutoToolTip { get { return false; } }
731 protected virtual ToolStripItemDisplayStyle DefaultDisplayStyle { get { return ToolStripItemDisplayStyle.ImageAndText; } }
732 protected internal virtual Padding DefaultMargin { get { return new Padding (0, 1, 0, 2); } }
733 protected virtual Padding DefaultPadding { get { return new Padding (); } }
734 protected virtual Size DefaultSize { get { return new Size (23, 23); } }
735 protected internal virtual bool DismissWhenClicked { get { return true; } }
737 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
738 protected internal ToolStrip Parent {
739 get { return this.parent; }
741 if (this.parent != value) {
742 ToolStrip old_parent = this.parent;
744 OnParentChanged(old_parent, this.parent);
748 protected internal virtual bool ShowKeyboardCues { get { return false; } }
751 #region Public Methods
752 public ToolStrip GetCurrentParent ()
757 public virtual Size GetPreferredSize (Size constrainingSize)
759 return this.CalculatePreferredSize (constrainingSize);
762 public void Invalidate ()
765 parent.Invalidate (this.bounds);
768 public void Invalidate (Rectangle r)
771 parent.Invalidate (r);
774 public void PerformClick ()
776 this.OnClick (EventArgs.Empty);
779 [EditorBrowsable (EditorBrowsableState.Never)]
780 public virtual void ResetBackColor () { this.BackColor = Control.DefaultBackColor; }
782 [EditorBrowsable (EditorBrowsableState.Never)]
783 public virtual void ResetDisplayStyle () { this.display_style = this.DefaultDisplayStyle; }
785 [EditorBrowsable (EditorBrowsableState.Never)]
786 public virtual void ResetFont () { this.font = new Font ("Tahoma", 8.25f); }
788 [EditorBrowsable (EditorBrowsableState.Never)]
789 public virtual void ResetForeColor () { this.ForeColor = Control.DefaultForeColor; }
791 [EditorBrowsable (EditorBrowsableState.Never)]
792 public virtual void ResetImage () { this.image = null; }
794 [EditorBrowsable (EditorBrowsableState.Never)]
795 public void ResetMargin () { this.margin = this.DefaultMargin; }
797 [EditorBrowsable (EditorBrowsableState.Never)]
798 public void ResetPadding () { this.padding = this.DefaultPadding; }
800 public void Select ()
802 if (!this.is_selected && this.CanSelect) {
803 this.is_selected = true;
805 this.Parent.NotifySelectedChanged (this);
809 public override string ToString ()
815 #region Protected Methods
816 [EditorBrowsable (EditorBrowsableState.Advanced)]
817 protected virtual AccessibleObject CreateAccessibilityInstance ()
819 return new ToolStripItemAccessibleObject (this);
822 protected override void Dispose (bool disposing)
824 if (!is_disposed && disposing)
827 base.Dispose (disposing);
830 protected virtual void OnAvailableChanged (EventArgs e)
832 EventHandler eh = (EventHandler)(Events [AvailableChangedEvent]);
837 [EditorBrowsable (EditorBrowsableState.Advanced)]
838 protected virtual void OnBackColorChanged (EventArgs e)
840 EventHandler eh = (EventHandler)(Events [BackColorChangedEvent]);
845 protected virtual void OnBoundsChanged ()
847 OnLayout (new LayoutEventArgs(null, ""));
850 protected virtual void OnClick (EventArgs e)
852 EventHandler eh = (EventHandler)(Events [ClickEvent]);
857 [EditorBrowsable (EditorBrowsableState.Advanced)]
858 protected virtual void OnDisplayStyleChanged (EventArgs e)
860 EventHandler eh = (EventHandler)(Events [DisplayStyleChangedEvent]);
865 protected virtual void OnDoubleClick (EventArgs e)
867 EventHandler eh = (EventHandler)(Events [DoubleClickEvent]);
871 if (!double_click_enabled)
875 void IDropTarget.OnDragDrop (DragEventArgs e)
880 void IDropTarget.OnDragEnter (DragEventArgs e)
885 void IDropTarget.OnDragLeave (EventArgs e)
890 void IDropTarget.OnDragOver (DragEventArgs e)
895 protected virtual void OnEnabledChanged (EventArgs e)
897 EventHandler eh = (EventHandler)(Events [EnabledChangedEvent]);
902 [EditorBrowsable (EditorBrowsableState.Advanced)]
903 protected virtual void OnFontChanged (EventArgs e)
907 [EditorBrowsable (EditorBrowsableState.Advanced)]
908 protected virtual void OnForeColorChanged (EventArgs e)
910 EventHandler eh = (EventHandler)(Events [ForeColorChangedEvent]);
915 protected virtual void OnLayout (LayoutEventArgs e)
919 protected virtual void OnLocationChanged (EventArgs e)
921 EventHandler eh = (EventHandler)(Events [LocationChangedEvent]);
926 protected virtual void OnMouseDown (MouseEventArgs e)
929 this.is_pressed = true;
932 MouseEventHandler eh = (MouseEventHandler)(Events [MouseDownEvent]);
938 protected virtual void OnMouseEnter (EventArgs e)
942 EventHandler eh = (EventHandler)(Events [MouseEnterEvent]);
947 protected virtual void OnMouseHover (EventArgs e)
950 EventHandler eh = (EventHandler)(Events [MouseHoverEvent]);
956 protected virtual void OnMouseLeave (EventArgs e)
958 if (this.CanSelect) {
959 this.is_selected = false;
960 this.is_pressed = false;
964 EventHandler eh = (EventHandler)(Events [MouseLeaveEvent]);
969 protected virtual void OnMouseMove (MouseEventArgs e)
972 MouseEventHandler eh = (MouseEventHandler)(Events [MouseMoveEvent]);
978 protected virtual void OnMouseUp (MouseEventArgs e)
981 this.is_pressed = false;
984 if (this.IsOnDropDown)
985 if (!(this is ToolStripDropDownItem) || (this as ToolStripDropDownItem).DropDown.Visible == false) {
986 if ((this.Parent as ToolStripDropDown).OwnerItem != null)
987 ((this.Parent as ToolStripDropDown).OwnerItem as ToolStripDropDownItem).HideDropDown ();
989 (this.Parent as ToolStripDropDown).Hide ();
993 MouseEventHandler eh = (MouseEventHandler)(Events [MouseUpEvent]);
999 protected virtual void OnOwnerChanged (EventArgs e)
1001 EventHandler eh = (EventHandler)(Events [OwnerChangedEvent]);
1006 protected internal virtual void OnOwnerFontChanged (EventArgs e)
1010 protected virtual void OnPaint (PaintEventArgs e)
1012 if (this.parent != null)
1013 this.parent.Renderer.DrawItemBackground (new ToolStripItemRenderEventArgs (e.Graphics, this));
1015 PaintEventHandler eh = (PaintEventHandler)(Events [PaintEvent]);
1020 // This is never called.
1021 protected virtual void OnParentBackColorChanged (EventArgs e)
1025 protected virtual void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
1027 if (oldParent != null)
1028 oldParent.PerformLayout ();
1030 if (newParent != null)
1031 newParent.PerformLayout ();
1034 protected internal virtual void OnParentEnabledChanged (EventArgs e)
1036 this.OnEnabledChanged (e);
1039 // This is never called.
1040 protected virtual void OnParentForeColorChanged (EventArgs e)
1044 protected internal virtual void OnParentRightToLeftChanged (EventArgs e)
1046 this.OnRightToLeftChanged (e);
1049 protected virtual void OnRightToLeftChanged (EventArgs e)
1051 EventHandler eh = (EventHandler)(Events[RightToLeftChangedEvent]);
1056 [EditorBrowsable (EditorBrowsableState.Advanced)]
1057 protected virtual void OnTextChanged (EventArgs e)
1059 EventHandler eh = (EventHandler)(Events [TextChangedEvent]);
1064 protected virtual void OnVisibleChanged (EventArgs e)
1066 EventHandler eh = (EventHandler)(Events [VisibleChangedEvent]);
1071 protected internal virtual bool ProcessCmdKey (ref Message m, Keys keyData)
1076 protected internal virtual void SetBounds (Rectangle bounds)
1078 if (this.bounds != bounds) {
1079 this.bounds = bounds;
1084 protected virtual void SetVisibleCore (bool visible)
1086 this.visible = visible;
1087 this.OnVisibleChanged (EventArgs.Empty);
1092 #region Public Events
1093 static object AvailableChangedEvent = new object ();
1094 static object BackColorChangedEvent = new object ();
1095 static object ClickEvent = new object ();
1096 static object DisplayStyleChangedEvent = new object ();
1097 static object DoubleClickEvent = new object ();
1098 static object EnabledChangedEvent = new object ();
1099 static object ForeColorChangedEvent = new object ();
1100 static object LocationChangedEvent = new object ();
1101 static object MouseDownEvent = new object ();
1102 static object MouseEnterEvent = new object ();
1103 static object MouseHoverEvent = new object ();
1104 static object MouseLeaveEvent = new object ();
1105 static object MouseMoveEvent = new object ();
1106 static object MouseUpEvent = new object ();
1107 static object OwnerChangedEvent = new object ();
1108 static object PaintEvent = new object ();
1109 static object RightToLeftChangedEvent = new object ();
1110 static object TextChangedEvent = new object ();
1111 static object VisibleChangedEvent = new object ();
1114 public event EventHandler AvailableChanged {
1115 add { Events.AddHandler (AvailableChangedEvent, value); }
1116 remove {Events.RemoveHandler (AvailableChangedEvent, value); }
1119 public event EventHandler BackColorChanged {
1120 add { Events.AddHandler (BackColorChangedEvent, value); }
1121 remove {Events.RemoveHandler (BackColorChangedEvent, value); }
1124 public event EventHandler Click {
1125 add { Events.AddHandler (ClickEvent, value); }
1126 remove {Events.RemoveHandler (ClickEvent, value); }
1129 public event EventHandler DisplayStyleChanged {
1130 add { Events.AddHandler (DisplayStyleChangedEvent, value); }
1131 remove {Events.RemoveHandler (DisplayStyleChangedEvent, value); }
1134 public event EventHandler DoubleClick {
1135 add { Events.AddHandler (DoubleClickEvent, value); }
1136 remove {Events.RemoveHandler (DoubleClickEvent, value); }
1139 public event EventHandler EnabledChanged {
1140 add { Events.AddHandler (EnabledChangedEvent, value); }
1141 remove {Events.RemoveHandler (EnabledChangedEvent, value); }
1144 public event EventHandler ForeColorChanged {
1145 add { Events.AddHandler (ForeColorChangedEvent, value); }
1146 remove {Events.RemoveHandler (ForeColorChangedEvent, value); }
1149 public event EventHandler LocationChanged {
1150 add { Events.AddHandler (LocationChangedEvent, value); }
1151 remove {Events.RemoveHandler (LocationChangedEvent, value); }
1154 public event MouseEventHandler MouseDown {
1155 add { Events.AddHandler (MouseDownEvent, value); }
1156 remove {Events.RemoveHandler (MouseDownEvent, value); }
1159 public event EventHandler MouseEnter {
1160 add { Events.AddHandler (MouseEnterEvent, value); }
1161 remove {Events.RemoveHandler (MouseEnterEvent, value); }
1164 public event EventHandler MouseHover {
1165 add { Events.AddHandler (MouseHoverEvent, value); }
1166 remove {Events.RemoveHandler (MouseHoverEvent, value); }
1169 public event EventHandler MouseLeave {
1170 add { Events.AddHandler (MouseLeaveEvent, value); }
1171 remove {Events.RemoveHandler (MouseLeaveEvent, value); }
1174 public event MouseEventHandler MouseMove {
1175 add { Events.AddHandler (MouseMoveEvent, value); }
1176 remove {Events.RemoveHandler (MouseMoveEvent, value); }
1179 public event MouseEventHandler MouseUp {
1180 add { Events.AddHandler (MouseUpEvent, value); }
1181 remove {Events.RemoveHandler (MouseUpEvent, value); }
1184 public event EventHandler OwnerChanged {
1185 add { Events.AddHandler (OwnerChangedEvent, value); }
1186 remove {Events.RemoveHandler (OwnerChangedEvent, value); }
1189 public event PaintEventHandler Paint {
1190 add { Events.AddHandler (PaintEvent, value); }
1191 remove {Events.RemoveHandler (PaintEvent, value); }
1194 public event EventHandler RightToLeftChanged {
1195 add { Events.AddHandler (RightToLeftChangedEvent, value); }
1196 remove { Events.RemoveHandler (RightToLeftChangedEvent, value); }
1199 public event EventHandler TextChanged {
1200 add { Events.AddHandler (TextChangedEvent, value); }
1201 remove {Events.RemoveHandler (TextChangedEvent, value); }
1204 public event EventHandler VisibleChanged {
1205 add { Events.AddHandler (VisibleChangedEvent, value); }
1206 remove {Events.RemoveHandler (VisibleChangedEvent, value); }
1210 #region Internal Methods
1211 internal Rectangle AlignInRectangle (Rectangle outer, Size inner, ContentAlignment align)
1216 if (align == ContentAlignment.BottomLeft || align == ContentAlignment.MiddleLeft || align == ContentAlignment.TopLeft)
1218 else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.MiddleCenter || align == ContentAlignment.TopCenter)
1219 x = Math.Max (outer.X + ((outer.Width - inner.Width) / 2), outer.Left);
1220 else if (align == ContentAlignment.BottomRight || align == ContentAlignment.MiddleRight || align == ContentAlignment.TopRight)
1221 x = outer.Right - inner.Width;
1222 if (align == ContentAlignment.TopCenter || align == ContentAlignment.TopLeft || align == ContentAlignment.TopRight)
1224 else if (align == ContentAlignment.MiddleCenter || align == ContentAlignment.MiddleLeft || align == ContentAlignment.MiddleRight)
1225 y = outer.Y + (outer.Height - inner.Height) / 2;
1226 else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.BottomRight || align == ContentAlignment.BottomLeft)
1227 y = outer.Bottom - inner.Height;
1229 return new Rectangle (x, y, Math.Min (inner.Width, outer.Width), Math.Min (inner.Height, outer.Height));
1232 internal void CalculateAutoSize ()
1234 this.text_size = TextRenderer.MeasureText (this.Text == null ? string.Empty: this.text, this.Font, Size.Empty, TextFormatFlags.HidePrefix);
1236 if (!this.auto_size || this is ToolStripControlHost)
1238 //this.text_size.Width += 6;
1240 Size final_size = this.CalculatePreferredSize (Size.Empty);
1242 if (final_size != this.Size) {
1243 this.bounds.Width = final_size.Width;
1244 if (this.parent != null)
1245 this.parent.PerformLayout ();
1249 internal virtual Size CalculatePreferredSize (Size constrainingSize)
1251 if (!this.auto_size)
1252 return this.explicit_size;
1254 Size preferred_size = this.DefaultSize;
1256 switch (this.display_style) {
1257 case ToolStripItemDisplayStyle.Text:
1258 int width = text_size.Width + this.padding.Horizontal;
1259 int height = text_size.Height + this.padding.Vertical;
1260 preferred_size = new Size (width, height);
1262 case ToolStripItemDisplayStyle.Image:
1263 if (this.Image == null)
1264 preferred_size = this.DefaultSize;
1266 switch (this.image_scaling) {
1267 case ToolStripItemImageScaling.None:
1268 preferred_size = this.Image.Size;
1270 case ToolStripItemImageScaling.SizeToFit:
1271 if (this.parent == null)
1272 preferred_size = this.Image.Size;
1274 preferred_size = this.parent.ImageScalingSize;
1279 case ToolStripItemDisplayStyle.ImageAndText:
1280 int width2 = text_size.Width + this.padding.Horizontal;
1281 int height2 = text_size.Height + this.padding.Vertical;
1283 if (this.Image != null) {
1284 switch (this.text_image_relation) {
1285 case TextImageRelation.Overlay:
1286 width2 = Math.Max (width2, this.Image.Width);
1287 height2 = Math.Max (height2, this.Image.Height);
1289 case TextImageRelation.ImageAboveText:
1290 case TextImageRelation.TextAboveImage:
1291 width2 = Math.Max (width2, this.Image.Width);
1292 height2 += this.Image.Height;
1294 case TextImageRelation.ImageBeforeText:
1295 case TextImageRelation.TextBeforeImage:
1296 height2 = Math.Max (height2, this.Image.Height);
1297 width2 += this.Image.Width;
1302 preferred_size = new Size (width2, height2);
1306 if (!(this is ToolStripLabel)) { // Everything but labels have a border
1307 preferred_size.Height += 4;
1308 preferred_size.Width += 4;
1311 // Account for ToolStripDropDownButton's drop down arrow
1312 if (this is ToolStripDropDownButton && (this as ToolStripDropDownButton).ShowDropDownArrow)
1313 preferred_size.Width += 9;
1315 return preferred_size;
1318 internal void CalculateTextAndImageRectangles (out Rectangle text_rect, out Rectangle image_rect)
1320 this.CalculateTextAndImageRectangles (this.ContentRectangle, out text_rect, out image_rect);
1323 internal void CalculateTextAndImageRectangles (Rectangle contentRectangle, out Rectangle text_rect, out Rectangle image_rect)
1325 text_rect = Rectangle.Empty;
1326 image_rect = Rectangle.Empty;
1328 switch (this.display_style) {
1329 case ToolStripItemDisplayStyle.None:
1331 case ToolStripItemDisplayStyle.Text:
1332 if (this.text != string.Empty)
1333 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1335 case ToolStripItemDisplayStyle.Image:
1336 if (this.Image != null)
1337 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1339 case ToolStripItemDisplayStyle.ImageAndText:
1340 if (this.text != string.Empty && this.Image == null)
1341 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1342 else if (this.text == string.Empty && this.Image != null)
1343 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1344 else if (this.text == string.Empty && this.Image == null)
1347 Rectangle text_area;
1348 Rectangle image_area;
1350 switch (this.text_image_relation) {
1351 case TextImageRelation.Overlay:
1352 text_rect = AlignInRectangle (contentRectangle, this.text_size, this.text_align);
1353 image_rect = AlignInRectangle (contentRectangle, GetImageSize (), this.image_align);
1355 case TextImageRelation.ImageAboveText:
1356 text_area = new Rectangle (contentRectangle.Left, contentRectangle.Bottom - (text_size.Height - 4), contentRectangle.Width, text_size.Height - 4);
1357 image_area = new Rectangle (contentRectangle.Left, contentRectangle.Top, contentRectangle.Width, contentRectangle.Height - text_area.Height);
1359 text_rect = AlignInRectangle (text_area, this.text_size, this.text_align);
1360 image_rect = AlignInRectangle (image_area, GetImageSize (), this.image_align);
1362 case TextImageRelation.TextAboveImage:
1363 text_area = new Rectangle (contentRectangle.Left, contentRectangle.Top, contentRectangle.Width, text_size.Height - 4);
1364 image_area = new Rectangle (contentRectangle.Left, text_area.Bottom, contentRectangle.Width, contentRectangle.Height - text_area.Height);
1366 text_rect = AlignInRectangle (text_area, this.text_size, this.text_align);
1367 image_rect = AlignInRectangle (image_area, GetImageSize (), this.image_align);
1369 case TextImageRelation.ImageBeforeText:
1370 LayoutTextBeforeOrAfterImage (contentRectangle, false, text_size, GetImageSize (), text_align, image_align, out text_rect, out image_rect);
1372 case TextImageRelation.TextBeforeImage:
1373 LayoutTextBeforeOrAfterImage (contentRectangle, true, text_size, GetImageSize (), text_align, image_align, out text_rect, out image_rect);
1381 private void LayoutTextBeforeOrAfterImage (Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, ContentAlignment textAlign, ContentAlignment imageAlign, out Rectangle textRect, out Rectangle imageRect)
1383 int element_spacing = 0; // Spacing between the Text and the Image
1384 int total_width = textSize.Width + element_spacing + imageSize.Width;
1385 int excess_width = totalArea.Width - total_width;
1388 Rectangle final_text_rect;
1389 Rectangle final_image_rect;
1391 HorizontalAlignment h_text = GetHorizontalAlignment (textAlign);
1392 HorizontalAlignment h_image = GetHorizontalAlignment (imageAlign);
1394 if (h_image == HorizontalAlignment.Left)
1396 else if (h_image == HorizontalAlignment.Right && h_text == HorizontalAlignment.Right)
1397 offset = excess_width;
1398 else if (h_image == HorizontalAlignment.Center && (h_text == HorizontalAlignment.Left || h_text == HorizontalAlignment.Center))
1399 offset += (int)(excess_width / 3);
1401 offset += (int)(2 * (excess_width / 3));
1404 final_text_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
1405 final_image_rect = new Rectangle (final_text_rect.Right + element_spacing, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
1407 final_image_rect = new Rectangle (totalArea.Left + offset, AlignInRectangle (totalArea, imageSize, imageAlign).Top, imageSize.Width, imageSize.Height);
1408 final_text_rect = new Rectangle (final_image_rect.Right + element_spacing, AlignInRectangle (totalArea, textSize, textAlign).Top, textSize.Width, textSize.Height);
1411 textRect = final_text_rect;
1412 imageRect = final_image_rect;
1415 private HorizontalAlignment GetHorizontalAlignment (ContentAlignment align)
1418 case ContentAlignment.BottomLeft:
1419 case ContentAlignment.MiddleLeft:
1420 case ContentAlignment.TopLeft:
1421 return HorizontalAlignment.Left;
1422 case ContentAlignment.BottomCenter:
1423 case ContentAlignment.MiddleCenter:
1424 case ContentAlignment.TopCenter:
1425 return HorizontalAlignment.Center;
1426 case ContentAlignment.BottomRight:
1427 case ContentAlignment.MiddleRight:
1428 case ContentAlignment.TopRight:
1429 return HorizontalAlignment.Right;
1432 return HorizontalAlignment.Left;
1435 private Size GetImageSize ()
1437 if (this.Image == null)
1440 if (this.image_scaling == ToolStripItemImageScaling.None)
1441 return this.Image.Size;
1443 if (this.Parent == null)
1446 return this.Parent.ImageScalingSize;
1449 internal string GetToolTip ()
1451 if (this.auto_tool_tip && string.IsNullOrEmpty (this.tool_tip_text))
1454 return this.tool_tip_text;
1457 internal void FireEvent (EventArgs e, ToolStripItemEventType met)
1459 // If we're disabled, don't fire any of these events, except Paint
1460 if (!this.Enabled && met != ToolStripItemEventType.Paint)
1464 case ToolStripItemEventType.MouseUp:
1465 this.OnMouseUp ((MouseEventArgs)e);
1466 this.OnClick ((MouseEventArgs)e);
1468 case ToolStripItemEventType.MouseDown:
1469 this.OnMouseDown ((MouseEventArgs)e);
1471 case ToolStripItemEventType.MouseEnter:
1472 this.OnMouseEnter (e);
1474 case ToolStripItemEventType.MouseHover:
1475 this.OnMouseHover (e);
1477 case ToolStripItemEventType.MouseLeave:
1478 this.OnMouseLeave (e);
1480 case ToolStripItemEventType.MouseMove:
1481 this.OnMouseMove ((MouseEventArgs)e);
1483 case ToolStripItemEventType.Paint:
1484 this.OnPaint ((PaintEventArgs)e);
1486 case ToolStripItemEventType.Click:
1492 internal virtual void SetPlacement (ToolStripItemPlacement placement)
1494 this.placement = placement;
1497 internal bool InternalVisible {
1498 get { return this.visible; }
1499 set { this.visible = value; }
1502 internal Point Location {
1503 get { return this.bounds.Location; }
1505 if (this.bounds.Location != value) {
1506 this.bounds.Location = value;
1507 this.OnLocationChanged (EventArgs.Empty);
1513 get { return this.bounds.Y; }
1515 if (this.bounds.Y != value) {
1516 this.bounds.Y = value;
1517 this.OnLocationChanged (EventArgs.Empty);
1523 get { return this.bounds.X; }
1525 if (this.bounds.X != value) {
1526 this.bounds.X = value;
1527 this.OnLocationChanged (EventArgs.Empty);
1532 internal int Right { get { return this.bounds.Right; } }
1533 internal int Bottom { get { return this.bounds.Bottom; } }
1536 public class ToolStripItemAccessibleObject : AccessibleObject
1538 private ToolStripItem owner_item;
1540 public ToolStripItemAccessibleObject (ToolStripItem ownerItem)
1542 if (ownerItem == null)
1543 throw new ArgumentNullException ("ownerItem");
1545 this.owner_item = ownerItem;
1546 base.default_action = string.Empty;
1547 base.keyboard_shortcut = string.Empty;
1548 base.name = string.Empty;
1549 base.value = string.Empty;
1552 #region Public Properties
1553 public override Rectangle Bounds {
1555 return owner_item.Visible ? owner_item.Bounds : Rectangle.Empty;
1559 public override string DefaultAction {
1560 get { return base.DefaultAction; }
1563 public override string Description {
1564 get { return base.Description; }
1567 public override string Help {
1568 get { return base.Help; }
1571 public override string KeyboardShortcut {
1572 get { return base.KeyboardShortcut; }
1575 public override string Name {
1577 if (base.name == string.Empty)
1578 return owner_item.Text;
1582 set { base.Name = value; }
1585 public override AccessibleObject Parent {
1586 get { return base.Parent; }
1589 public override AccessibleRole Role {
1590 get { return base.Role; }
1593 public override AccessibleStates State {
1594 get { return base.State; }
1598 #region Public Methods
1599 public void AddState (AccessibleStates state)
1604 public override void DoDefaultAction ()
1606 base.DoDefaultAction ();
1609 public override int GetHelpTopic (out string FileName)
1611 return base.GetHelpTopic (out FileName);
1614 public override AccessibleObject Navigate (AccessibleNavigation navdir)
1616 return base.Navigate (navdir);
1619 public override string ToString ()
1621 return string.Format ("ToolStripItemAccessibleObject: Owner = {0}", owner_item.ToString());