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;
41 #region Public Constructors
42 public ToolStripMenuItem ()
47 public ToolStripMenuItem (Image image)
48 : base (string.Empty, image, null, string.Empty)
52 public ToolStripMenuItem (string text)
53 : base (text, null, null, string.Empty)
57 public ToolStripMenuItem (string text, Image image)
58 : base (text, image, null, string.Empty)
62 public ToolStripMenuItem (string text, Image image, EventHandler onClick)
63 : base (text, image, onClick, string.Empty)
67 public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
68 : base (text, image, null, string.Empty)
70 if (dropDownItems != null)
71 foreach (ToolStripItem tsi in dropDownItems)
72 this.DropDownItems.Add (tsi);
75 public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
76 : base (text, image, onClick, string.Empty)
80 public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
81 : base (text, image, onClick, name)
86 #region Public Properties
88 [DefaultValue (false)]
91 switch (this.checked_state) {
92 case CheckState.Unchecked:
95 case CheckState.Checked:
96 case CheckState.Indeterminate:
101 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
102 this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
103 this.OnCheckedChanged (EventArgs.Empty);
109 [DefaultValue (false)]
110 public bool CheckOnClick {
111 get { return this.check_on_click; }
112 set { this.check_on_click = value; }
116 [DefaultValue (CheckState.Unchecked)]
117 public CheckState CheckState
119 get { return this.checked_state; }
122 if (!Enum.IsDefined (typeof (CheckState), value))
123 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
125 this.checked_state = value;
126 OnCheckStateChanged (EventArgs.Empty);
131 public override bool Enabled {
132 get { return base.Enabled; }
133 set { base.Enabled = value; }
137 #region Protected Properties
138 protected internal override Padding DefaultMargin {
139 get { return new Padding (0); }
142 protected override Padding DefaultPadding {
143 get { return new Padding (4, 0, 4, 0); }
146 protected override Size DefaultSize {
147 get { return new Size (32, 19); }
151 #region Protected Methods
152 protected override ToolStripDropDown CreateDefaultDropDown ()
154 return base.CreateDefaultDropDown ();
157 protected override void Dispose (bool disposing)
159 base.Dispose (disposing);
162 protected virtual void OnCheckedChanged (EventArgs e)
164 if (CheckedChanged != null) CheckedChanged (this, e);
167 protected virtual void OnCheckStateChanged (EventArgs e)
169 if (CheckStateChanged != null) CheckStateChanged (this, e);
172 protected override void OnClick (EventArgs e)
179 if (this.IsOnDropDown) {
180 if (this.HasDropDownItems)
183 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
184 (this.Parent as ToolStripDropDown).Close (ToolStripDropDownCloseReason.ItemClicked);
186 Object parent = this.Parent;
188 // Find the top level MenuStrip to inform it to close all open
189 // dropdowns because this one was clicked
190 while (parent != null) {
191 if (parent is MenuStrip)
192 (parent as MenuStrip).HideMenus (true, ToolStripDropDownCloseReason.ItemClicked);
194 if (parent is ToolStripDropDown)
195 parent = (parent as ToolStripDropDown).OwnerItem;
196 else if (parent is ToolStripItem)
197 parent = (parent as ToolStripItem).Parent;
203 if (this.check_on_click)
204 this.Checked = !this.Checked;
207 protected override void OnDropDownHide (EventArgs e)
209 base.OnDropDownHide (e);
212 protected override void OnDropDownShow (EventArgs e)
214 base.OnDropDownShow (e);
217 protected override void OnFontChanged (EventArgs e)
219 base.OnFontChanged (e);
222 protected override void OnMouseDown (MouseEventArgs e)
224 base.OnMouseDown (e);
226 if (this.HasDropDownItems)
227 if (!this.DropDown.Visible)
228 this.ShowDropDown ();
231 protected override void OnMouseEnter (EventArgs e)
233 base.OnMouseEnter (e);
235 if (this.IsOnDropDown && this.HasDropDownItems)
236 this.ShowDropDown ();
239 protected override void OnMouseLeave (EventArgs e)
241 base.OnMouseLeave (e);
244 protected override void OnMouseUp (MouseEventArgs e)
249 protected override void OnOwnerChanged (EventArgs e)
251 base.OnOwnerChanged (e);
254 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
258 if (this.Owner != null) {
259 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
260 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
262 if (this.IsOnDropDown)
263 this.Owner.Renderer.DrawMenuItemBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
265 this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
267 Rectangle text_layout_rect;
268 Rectangle image_layout_rect;
270 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
272 if (this.IsOnDropDown) {
273 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
274 if (image_layout_rect != Rectangle.Empty)
275 image_layout_rect = new Rectangle (4, 3, draw_image.Width, draw_image.Height);
278 this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
280 if (text_layout_rect != Rectangle.Empty)
281 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
283 if (image_layout_rect != Rectangle.Empty)
284 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
286 if (this.IsOnDropDown && this.HasDropDownItems)
287 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
293 #region Public Events
294 public event EventHandler CheckedChanged;
295 public event EventHandler CheckStateChanged;