2 // ToolStripMenuItem.cs
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;
33 using System.Windows.Forms.Design;
34 using System.ComponentModel.Design.Serialization;
36 namespace System.Windows.Forms
38 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
39 [DesignerSerializer ("System.Windows.Forms.Design.ToolStripMenuItemCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
40 public class ToolStripMenuItem : ToolStripDropDownItem
42 private CheckState checked_state;
43 private bool check_on_click;
44 private bool close_on_mouse_release;
45 private string shortcut_display_string;
46 private Keys shortcut_keys = Keys.None;
47 private bool show_shortcut_keys = true;
48 private Form mdi_client_form;
50 #region Public Constructors
51 public ToolStripMenuItem ()
52 : this (null, null, null, string.Empty)
56 public ToolStripMenuItem (Image image)
57 : this (null, image, null, string.Empty)
61 public ToolStripMenuItem (string text)
62 : this (text, null, null, string.Empty)
66 public ToolStripMenuItem (string text, Image image)
67 : this (text, image, null, string.Empty)
71 public ToolStripMenuItem (string text, Image image, EventHandler onClick)
72 : this (text, image, onClick, string.Empty)
76 public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
77 : this (text, image, null, string.Empty)
79 if (dropDownItems != null)
80 foreach (ToolStripItem tsi in dropDownItems)
81 this.DropDownItems.Add (tsi);
84 public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
85 : this (text, image, onClick, string.Empty)
89 public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
90 : base (text, image, onClick, name)
92 base.Overflow = ToolStripItemOverflow.Never;
96 #region Public Properties
98 [DefaultValue (false)]
99 [RefreshProperties (RefreshProperties.All)]
100 public bool Checked {
102 switch (this.checked_state) {
103 case CheckState.Unchecked:
106 case CheckState.Checked:
107 case CheckState.Indeterminate:
112 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
113 this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
115 this.OnCheckedChanged (EventArgs.Empty);
120 [DefaultValue (false)]
121 public bool CheckOnClick {
122 get { return this.check_on_click; }
123 set { this.check_on_click = value; }
127 [DefaultValue (CheckState.Unchecked)]
128 [RefreshProperties (RefreshProperties.All)]
129 public CheckState CheckState {
130 get { return this.checked_state; }
133 if (!Enum.IsDefined (typeof (CheckState), value))
134 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
136 this.checked_state = value;
138 this.OnCheckStateChanged (EventArgs.Empty);
142 public override bool Enabled {
143 get { return base.Enabled; }
144 set { base.Enabled = value; }
148 public bool IsMdiWindowListEntry {
149 get { return this.mdi_client_form != null; }
152 [DefaultValue (ToolStripItemOverflow.Never)]
153 public new ToolStripItemOverflow Overflow {
154 get { return base.Overflow; }
155 set { base.Overflow = value; }
159 [DefaultValue (true)]
160 public bool ShowShortcutKeys {
161 get { return this.show_shortcut_keys; }
162 set { this.show_shortcut_keys = value; }
166 [DefaultValue (null)]
167 public string ShortcutKeyDisplayString {
168 get { return this.shortcut_display_string; }
169 set { this.shortcut_display_string = value; }
173 [DefaultValue (Keys.None)]
174 public Keys ShortcutKeys {
175 get { return this.shortcut_keys; }
177 if (this.shortcut_keys != value) {
178 this.shortcut_keys = value;
180 if (this.Parent != null)
181 ToolStripManager.AddToolStripMenuItem (this);
187 #region Protected Properties
188 protected internal override Padding DefaultMargin {
189 get { return new Padding (0); }
192 protected override Padding DefaultPadding {
193 get { return new Padding (4, 0, 4, 0); }
196 protected override Size DefaultSize {
197 get { return new Size (32, 19); }
201 #region Protected Methods
202 [EditorBrowsable (EditorBrowsableState.Advanced)]
203 protected override AccessibleObject CreateAccessibilityInstance ()
205 return new ToolStripMenuItemAccessibleObject ();
208 protected override ToolStripDropDown CreateDefaultDropDown ()
210 ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
211 tsddm.OwnerItem = this;
215 protected override void Dispose (bool disposing)
217 base.Dispose (disposing);
220 protected virtual void OnCheckedChanged (EventArgs e)
222 EventHandler eh = (EventHandler)Events [CheckedChangedEvent];
227 protected virtual void OnCheckStateChanged (EventArgs e)
229 EventHandler eh = (EventHandler)Events [CheckStateChangedEvent];
234 protected override void OnClick (EventArgs e)
239 if (this.HasDropDownItems) {
244 if (this.OwnerItem is ToolStripDropDownItem)
245 (this.OwnerItem as ToolStripDropDownItem).OnDropDownItemClicked (new ToolStripItemClickedEventArgs (this));
247 if (this.IsOnDropDown)
248 this.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.ItemClicked);
250 if (this.IsMdiWindowListEntry) {
251 this.mdi_client_form.MdiParent.MdiContainer.ActivateChild (this.mdi_client_form);
255 if (this.check_on_click)
256 this.Checked = !this.Checked;
260 if (!this.IsOnDropDown && !this.HasDropDownItems)
261 this.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.ItemClicked);
264 protected override void OnDropDownHide (EventArgs e)
266 base.OnDropDownHide (e);
269 protected override void OnDropDownShow (EventArgs e)
271 base.OnDropDownShow (e);
274 protected override void OnFontChanged (EventArgs e)
276 base.OnFontChanged (e);
279 protected override void OnMouseDown (MouseEventArgs e)
281 if (!this.IsOnDropDown && this.HasDropDownItems && this.DropDown.Visible)
282 this.close_on_mouse_release = true;
284 if (this.HasDropDownItems && Enabled)
285 if (!this.DropDown.Visible)
286 this.ShowDropDown ();
288 base.OnMouseDown (e);
291 protected override void OnMouseEnter (EventArgs e)
293 if (this.IsOnDropDown && this.HasDropDownItems && Enabled)
294 this.ShowDropDown ();
296 base.OnMouseEnter (e);
299 protected override void OnMouseLeave (EventArgs e)
301 base.OnMouseLeave (e);
304 protected override void OnMouseUp (MouseEventArgs e)
306 if (this.close_on_mouse_release) {
307 this.DropDown.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
309 this.close_on_mouse_release = false;
311 if (!this.IsOnDropDown && this.Parent is MenuStrip)
312 (this.Parent as MenuStrip).MenuDroppedDown = false;
315 if (!this.HasDropDownItems && Enabled)
319 protected override void OnOwnerChanged (EventArgs e)
321 base.OnOwnerChanged (e);
324 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
328 // Can't render without an owner
329 if (this.Owner == null)
332 // If DropDown.ShowImageMargin is false, we don't display the image
333 Image draw_image = this.UseImageMargin ? this.Image : null;
335 // Figure out our text color
336 Color font_color = this.ForeColor == SystemColors.ControlText ? SystemColors.MenuText : this.ForeColor;
338 if ((this.Selected || this.Pressed) && this.IsOnDropDown && font_color == SystemColors.MenuText)
339 font_color = SystemColors.HighlightText;
341 if (!this.Enabled && this.ForeColor == SystemColors.ControlText)
342 font_color = SystemColors.GrayText;
344 // Gray stuff out if we're disabled
345 draw_image = this.Enabled ? draw_image : ToolStripRenderer.CreateDisabledImage (draw_image);
347 // Draw our background
348 this.Owner.Renderer.DrawMenuItemBackground (new ToolStripItemRenderEventArgs (e.Graphics, this));
350 // Figure out where our text and image go
351 Rectangle text_layout_rect;
352 Rectangle image_layout_rect;
354 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
356 if (this.IsOnDropDown) {
357 if (!this.UseImageMargin) {
358 image_layout_rect = Rectangle.Empty;
359 text_layout_rect = new Rectangle (8, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
361 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
363 if (image_layout_rect != Rectangle.Empty)
364 image_layout_rect = new Rectangle (new Point (4, 3), base.GetImageSize ());
367 if (this.Checked && this.ShowMargin)
368 this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
370 if (text_layout_rect != Rectangle.Empty)
371 this.Owner.Renderer.DrawItemText (new ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
373 string key_string = GetShortcutDisplayString ();
375 if (!string.IsNullOrEmpty (key_string) && !this.HasDropDownItems) {
377 Size key_string_size = TextRenderer.MeasureText (key_string, this.Font);
378 Rectangle key_string_rect = new Rectangle (this.ContentRectangle.Right - key_string_size.Width - offset, text_layout_rect.Top, key_string_size.Width, text_layout_rect.Height);
379 this.Owner.Renderer.DrawItemText (new ToolStripItemTextRenderEventArgs (e.Graphics, this, key_string, key_string_rect, font_color, this.Font, this.TextAlign));
382 if (image_layout_rect != Rectangle.Empty)
383 this.Owner.Renderer.DrawItemImage (new ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
385 if (this.IsOnDropDown && this.HasDropDownItems)
386 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
390 protected internal override bool ProcessCmdKey (ref Message m, Keys keyData)
392 if (this.Enabled && keyData == this.shortcut_keys) {
393 this.FireEvent (EventArgs.Empty, ToolStripItemEventType.Click);
397 return base.ProcessCmdKey (ref m, keyData);
400 protected internal override bool ProcessMnemonic (char charCode)
403 this.Parent.ChangeSelection (this);
405 if (this.HasDropDownItems) {
406 ToolStripManager.SetActiveToolStrip (this.Parent, true);
407 this.ShowDropDown ();
408 this.DropDown.SelectNextToolStripItem (null, true);
410 this.PerformClick ();
415 protected internal override void SetBounds (Rectangle rect)
417 base.SetBounds (rect);
421 #region Public Events
422 static object CheckedChangedEvent = new object ();
423 static object CheckStateChangedEvent = new object ();
425 public event EventHandler CheckedChanged {
426 add { Events.AddHandler (CheckedChangedEvent, value); }
427 remove {Events.RemoveHandler (CheckedChangedEvent, value); }
430 public event EventHandler CheckStateChanged {
431 add { Events.AddHandler (CheckStateChangedEvent, value); }
432 remove {Events.RemoveHandler (CheckStateChangedEvent, value); }
436 #region Internal Properties
437 internal Form MdiClientForm {
438 get { return this.mdi_client_form; }
439 set { this.mdi_client_form = value; }
443 #region Internal Methods
444 internal override Size CalculatePreferredSize (Size constrainingSize)
446 Size base_size = base.CalculatePreferredSize (constrainingSize);
448 string key_string = GetShortcutDisplayString ();
450 if (string.IsNullOrEmpty (key_string))
453 Size text_size = TextRenderer.MeasureText (key_string, this.Font);
455 return new Size (base_size.Width + text_size.Width - 25, base_size.Height);
458 internal string GetShortcutDisplayString ()
460 if (this.show_shortcut_keys == false)
463 string key_string = string.Empty;
465 if (!string.IsNullOrEmpty (this.shortcut_display_string))
466 key_string = this.shortcut_display_string;
467 else if (this.shortcut_keys != Keys.None) {
468 KeysConverter kc = new KeysConverter ();
469 key_string = kc.ConvertToString (this.shortcut_keys);
475 internal void HandleAutoExpansion ()
477 if (this.HasDropDownItems) {
478 this.ShowDropDown ();
479 this.DropDown.SelectNextToolStripItem (null, true);
483 internal override void HandleClick (EventArgs e)
488 Parent.Invalidate ();
492 #region ToolStripMenuItemAccessibleObject
493 private class ToolStripMenuItemAccessibleObject : AccessibleObject