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;
34 namespace System.Windows.Forms
36 public class ToolStripMenuItem : ToolStripDropDownItem
38 private CheckState checked_state;
39 private bool check_on_click;
40 private string shortcut_display_string;
41 private Keys shortcut_keys = Keys.None;
42 private bool show_shortcut_keys = true;
43 private Form mdi_client_form;
45 #region Public Constructors
46 public ToolStripMenuItem ()
51 public ToolStripMenuItem (Image image)
52 : base (string.Empty, image, null, string.Empty)
56 public ToolStripMenuItem (string text)
57 : base (text, null, null, string.Empty)
61 public ToolStripMenuItem (string text, Image image)
62 : base (text, image, null, string.Empty)
66 public ToolStripMenuItem (string text, Image image, EventHandler onClick)
67 : base (text, image, onClick, string.Empty)
71 public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
72 : base (text, image, null, string.Empty)
74 if (dropDownItems != null)
75 foreach (ToolStripItem tsi in dropDownItems)
76 this.DropDownItems.Add (tsi);
79 public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
80 : base (text, image, onClick, string.Empty)
84 public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
85 : base (text, image, onClick, name)
90 #region Public Properties
92 [DefaultValue (false)]
95 switch (this.checked_state) {
96 case CheckState.Unchecked:
99 case CheckState.Checked:
100 case CheckState.Indeterminate:
105 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
106 this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
108 this.OnCheckedChanged (EventArgs.Empty);
113 [DefaultValue (false)]
114 public bool CheckOnClick {
115 get { return this.check_on_click; }
116 set { this.check_on_click = value; }
120 [DefaultValue (CheckState.Unchecked)]
121 public CheckState CheckState
123 get { return this.checked_state; }
126 if (!Enum.IsDefined (typeof (CheckState), value))
127 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
129 this.checked_state = value;
131 this.OnCheckStateChanged (EventArgs.Empty);
135 public override bool Enabled {
136 get { return base.Enabled; }
137 set { base.Enabled = value; }
140 public bool IsMdiWindowListEntry {
141 get { return this.mdi_client_form != null; }
144 [MonoTODO ("Renderer doesn't support shortcut keys yet, they will never show.")]
146 public bool ShowShortcutKeys {
147 get { return this.show_shortcut_keys; }
148 set { this.show_shortcut_keys = value; }
151 [MonoTODO ("Keyboard navigation not implemented.")]
153 public string ShortcutKeyDisplayString {
154 get { return this.shortcut_display_string; }
155 set { this.shortcut_display_string = value; }
158 [MonoTODO ("Keyboard navigation not implemented.")]
160 public Keys ShortcutKeys {
161 get { return this.shortcut_keys; }
162 set { this.shortcut_keys = value; }
166 #region Protected Properties
167 protected internal override Padding DefaultMargin {
168 get { return new Padding (0); }
171 protected override Padding DefaultPadding {
172 get { return new Padding (4, 0, 4, 0); }
175 protected override Size DefaultSize {
176 get { return new Size (32, 19); }
180 #region Protected Methods
181 protected override ToolStripDropDown CreateDefaultDropDown ()
183 return new ToolStripDropDownMenu ();
186 protected override void Dispose (bool disposing)
188 base.Dispose (disposing);
191 protected virtual void OnCheckedChanged (EventArgs e)
193 EventHandler eh = (EventHandler)Events [CheckedChangedEvent];
198 protected virtual void OnCheckStateChanged (EventArgs e)
200 EventHandler eh = (EventHandler)Events [CheckStateChangedEvent];
205 protected override void OnClick (EventArgs e)
210 if (this.IsOnDropDown) {
211 if (this.HasDropDownItems)
214 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
216 (this.Parent as ToolStripDropDown).Close (ToolStripDropDownCloseReason.ItemClicked);
217 ToolStripManager.FireAppFocusChanged (this.Parent);
219 Object parent = this.Parent;
221 // Find the top level MenuStrip to inform it to close all open
222 // dropdowns because this one was clicked
223 while (parent != null) {
224 if (parent is MenuStrip)
225 (parent as MenuStrip).HideMenus (true, ToolStripDropDownCloseReason.ItemClicked);
227 if (parent is ToolStripDropDown)
228 parent = (parent as ToolStripDropDown).OwnerItem;
229 else if (parent is ToolStripItem)
230 parent = (parent as ToolStripItem).Parent;
236 if (this.IsMdiWindowListEntry) {
237 this.mdi_client_form.MdiParent.MdiContainer.ActivateChild (this.mdi_client_form);
241 if (this.check_on_click)
242 this.Checked = !this.Checked;
247 protected override void OnDropDownHide (EventArgs e)
249 base.OnDropDownHide (e);
252 protected override void OnDropDownShow (EventArgs e)
254 base.OnDropDownShow (e);
257 protected override void OnFontChanged (EventArgs e)
259 base.OnFontChanged (e);
262 protected override void OnMouseDown (MouseEventArgs e)
264 if (this.HasDropDownItems)
265 if (!this.DropDown.Visible)
266 this.ShowDropDown ();
268 base.OnMouseDown (e);
271 protected override void OnMouseEnter (EventArgs e)
273 if (this.IsOnDropDown && this.HasDropDownItems)
274 this.ShowDropDown ();
276 base.OnMouseEnter (e);
279 protected override void OnMouseLeave (EventArgs e)
281 base.OnMouseLeave (e);
284 protected override void OnMouseUp (MouseEventArgs e)
289 protected override void OnOwnerChanged (EventArgs e)
291 base.OnOwnerChanged (e);
294 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
298 if (this.Owner != null) {
299 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
300 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
302 if (this.IsOnDropDown)
303 this.Owner.Renderer.DrawMenuItemBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
305 this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
307 Rectangle text_layout_rect;
308 Rectangle image_layout_rect;
310 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
312 if (this.IsOnDropDown) {
313 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
314 if (image_layout_rect != Rectangle.Empty)
315 image_layout_rect = new Rectangle (4, 3, draw_image.Width, draw_image.Height);
318 this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
320 if (text_layout_rect != Rectangle.Empty)
321 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
323 if (image_layout_rect != Rectangle.Empty)
324 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
326 if (this.IsOnDropDown && this.HasDropDownItems)
327 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
333 #region Public Events
334 static object CheckedChangedEvent = new object ();
335 static object CheckStateChangedEvent = new object ();
337 public event EventHandler CheckedChanged {
338 add { Events.AddHandler (CheckedChangedEvent, value); }
339 remove {Events.RemoveHandler (CheckedChangedEvent, value); }
342 public event EventHandler CheckStateChanged {
343 add { Events.AddHandler (CheckStateChangedEvent, value); }
344 remove {Events.RemoveHandler (CheckStateChangedEvent, value); }
348 #region Internal Properties
349 internal Form MdiClientForm {
350 get { return this.mdi_client_form; }
351 set { this.mdi_client_form = value; }