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;
35 namespace System.Windows.Forms
37 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
38 public class ToolStripMenuItem : ToolStripDropDownItem
40 private CheckState checked_state;
41 private bool check_on_click;
42 private string shortcut_display_string;
43 private Keys shortcut_keys = Keys.None;
44 private bool show_shortcut_keys = true;
45 private Form mdi_client_form;
47 #region Public Constructors
48 public ToolStripMenuItem ()
53 public ToolStripMenuItem (Image image)
54 : base (string.Empty, image, null, string.Empty)
58 public ToolStripMenuItem (string text)
59 : base (text, null, null, string.Empty)
63 public ToolStripMenuItem (string text, Image image)
64 : base (text, image, null, string.Empty)
68 public ToolStripMenuItem (string text, Image image, EventHandler onClick)
69 : base (text, image, onClick, string.Empty)
73 public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
74 : base (text, image, null, string.Empty)
76 if (dropDownItems != null)
77 foreach (ToolStripItem tsi in dropDownItems)
78 this.DropDownItems.Add (tsi);
81 public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
82 : base (text, image, onClick, string.Empty)
86 public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
87 : base (text, image, onClick, name)
92 #region Public Properties
94 [DefaultValue (false)]
95 [RefreshProperties (RefreshProperties.Repaint)]
98 switch (this.checked_state) {
99 case CheckState.Unchecked:
102 case CheckState.Checked:
103 case CheckState.Indeterminate:
108 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
109 this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
111 this.OnCheckedChanged (EventArgs.Empty);
116 [DefaultValue (false)]
117 public bool CheckOnClick {
118 get { return this.check_on_click; }
119 set { this.check_on_click = value; }
123 [DefaultValue (CheckState.Unchecked)]
124 [RefreshProperties (RefreshProperties.Repaint)]
125 public CheckState CheckState {
126 get { return this.checked_state; }
129 if (!Enum.IsDefined (typeof (CheckState), value))
130 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
132 this.checked_state = value;
134 this.OnCheckStateChanged (EventArgs.Empty);
138 public override bool Enabled {
139 get { return base.Enabled; }
140 set { base.Enabled = value; }
144 public bool IsMdiWindowListEntry {
145 get { return this.mdi_client_form != null; }
148 [MonoTODO ("Renderer doesn't support shortcut keys yet, they will never show.")]
150 public bool ShowShortcutKeys {
151 get { return this.show_shortcut_keys; }
152 set { this.show_shortcut_keys = value; }
155 [MonoTODO ("Keyboard navigation not implemented.")]
157 public string ShortcutKeyDisplayString {
158 get { return this.shortcut_display_string; }
159 set { this.shortcut_display_string = value; }
162 [MonoTODO ("Keyboard navigation not implemented.")]
164 public Keys ShortcutKeys {
165 get { return this.shortcut_keys; }
166 set { this.shortcut_keys = value; }
170 #region Protected Properties
171 protected internal override Padding DefaultMargin {
172 get { return new Padding (0); }
175 protected override Padding DefaultPadding {
176 get { return new Padding (4, 0, 4, 0); }
179 protected override Size DefaultSize {
180 get { return new Size (32, 19); }
184 #region Protected Methods
185 protected override ToolStripDropDown CreateDefaultDropDown ()
187 ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
188 tsddm.OwnerItem = this;
192 protected override void Dispose (bool disposing)
194 base.Dispose (disposing);
197 protected virtual void OnCheckedChanged (EventArgs e)
199 EventHandler eh = (EventHandler)Events [CheckedChangedEvent];
204 protected virtual void OnCheckStateChanged (EventArgs e)
206 EventHandler eh = (EventHandler)Events [CheckStateChangedEvent];
211 protected override void OnClick (EventArgs e)
216 if (this.IsOnDropDown) {
217 if (this.HasDropDownItems)
220 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
222 (this.Parent as ToolStripDropDown).Close (ToolStripDropDownCloseReason.ItemClicked);
223 ToolStripManager.FireAppFocusChanged (this.Parent);
225 Object parent = this.Parent;
227 // Find the top level MenuStrip to inform it to close all open
228 // dropdowns because this one was clicked
229 while (parent != null) {
230 if (parent is MenuStrip)
231 (parent as MenuStrip).HideMenus (true, ToolStripDropDownCloseReason.ItemClicked);
233 if (parent is ToolStripDropDown)
234 parent = (parent as ToolStripDropDown).OwnerItem;
235 else if (parent is ToolStripItem)
236 parent = (parent as ToolStripItem).Parent;
242 if (this.IsMdiWindowListEntry) {
243 this.mdi_client_form.MdiParent.MdiContainer.ActivateChild (this.mdi_client_form);
247 if (this.check_on_click)
248 this.Checked = !this.Checked;
253 protected override void OnDropDownHide (EventArgs e)
255 base.OnDropDownHide (e);
258 protected override void OnDropDownShow (EventArgs e)
260 base.OnDropDownShow (e);
263 protected override void OnFontChanged (EventArgs e)
265 base.OnFontChanged (e);
268 protected override void OnMouseDown (MouseEventArgs e)
270 if (this.HasDropDownItems && Enabled)
271 if (!this.DropDown.Visible)
272 this.ShowDropDown ();
274 base.OnMouseDown (e);
277 protected override void OnMouseEnter (EventArgs e)
279 if (this.IsOnDropDown && this.HasDropDownItems && Enabled)
280 this.ShowDropDown ();
282 base.OnMouseEnter (e);
285 protected override void OnMouseLeave (EventArgs e)
287 base.OnMouseLeave (e);
290 protected override void OnMouseUp (MouseEventArgs e)
292 if (!this.HasDropDownItems && Enabled)
296 protected override void OnOwnerChanged (EventArgs e)
298 base.OnOwnerChanged (e);
301 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
305 if (this.Owner != null) {
306 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
307 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
309 if (this.IsOnDropDown)
310 this.Owner.Renderer.DrawMenuItemBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
312 this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
314 Rectangle text_layout_rect;
315 Rectangle image_layout_rect;
317 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
319 if (this.IsOnDropDown) {
320 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
321 if (image_layout_rect != Rectangle.Empty)
322 image_layout_rect = new Rectangle (4, 3, draw_image.Width, draw_image.Height);
325 this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
327 if (text_layout_rect != Rectangle.Empty)
328 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
330 if (image_layout_rect != Rectangle.Empty)
331 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
333 if (this.IsOnDropDown && this.HasDropDownItems)
334 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
339 protected internal override void SetBounds (Rectangle bounds)
341 base.SetBounds (bounds);
345 #region Public Events
346 static object CheckedChangedEvent = new object ();
347 static object CheckStateChangedEvent = new object ();
349 public event EventHandler CheckedChanged {
350 add { Events.AddHandler (CheckedChangedEvent, value); }
351 remove {Events.RemoveHandler (CheckedChangedEvent, value); }
354 public event EventHandler CheckStateChanged {
355 add { Events.AddHandler (CheckStateChangedEvent, value); }
356 remove {Events.RemoveHandler (CheckStateChangedEvent, value); }
360 #region Internal Properties
361 internal Form MdiClientForm {
362 get { return this.mdi_client_form; }
363 set { this.mdi_client_form = value; }