| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- //
- // ToolStripMenuItem.cs
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2006 Jonathan Pobst
- //
- // Authors:
- // Jonathan Pobst ([email protected])
- //
- #if NET_2_0
- using System;
- using System.Drawing;
- using System.ComponentModel;
- namespace System.Windows.Forms
- {
- public class ToolStripMenuItem : ToolStripDropDownItem
- {
- private CheckState checked_state;
- private bool check_on_click;
- #region Public Constructors
- public ToolStripMenuItem ()
- : base ()
- {
- }
- public ToolStripMenuItem (Image image)
- : base (string.Empty, image, null, string.Empty)
- {
- }
- public ToolStripMenuItem (string text)
- : base (text, null, null, string.Empty)
- {
- }
- public ToolStripMenuItem (string text, Image image)
- : base (text, image, null, string.Empty)
- {
- }
- public ToolStripMenuItem (string text, Image image, EventHandler onClick)
- : base (text, image, onClick, string.Empty)
- {
- }
- public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
- : base (text, image, null, string.Empty)
- {
- if (dropDownItems != null)
- foreach (ToolStripItem tsi in dropDownItems)
- this.DropDownItems.Add (tsi);
- }
- public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
- : base (text, image, onClick, string.Empty)
- {
- }
- public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
- : base (text, image, onClick, name)
- {
- }
- #endregion
- #region Public Properties
- [Bindable (true)]
- [DefaultValue (false)]
- public bool Checked {
- get {
- switch (this.checked_state) {
- case CheckState.Unchecked:
- default:
- return false;
- case CheckState.Checked:
- case CheckState.Indeterminate:
- return true;
- }
- }
- set {
- if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
- this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
- this.OnCheckedChanged (EventArgs.Empty);
- this.Invalidate ();
- }
- }
- }
- [DefaultValue (false)]
- public bool CheckOnClick {
- get { return this.check_on_click; }
- set { this.check_on_click = value; }
- }
- [Bindable (true)]
- [DefaultValue (CheckState.Unchecked)]
- public CheckState CheckState
- {
- get { return this.checked_state; }
- set
- {
- if (!Enum.IsDefined (typeof (CheckState), value))
- throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
- this.checked_state = value;
- OnCheckStateChanged (EventArgs.Empty);
- this.Invalidate ();
- }
- }
- public override bool Enabled {
- get { return base.Enabled; }
- set { base.Enabled = value; }
- }
- #endregion
- #region Protected Properties
- protected internal override Padding DefaultMargin {
- get { return new Padding (0); }
- }
- protected override Padding DefaultPadding {
- get { return new Padding (4, 0, 4, 0); }
- }
- protected override Size DefaultSize {
- get { return new Size (32, 19); }
- }
- #endregion
- #region Protected Methods
- protected override ToolStripDropDown CreateDefaultDropDown ()
- {
- return base.CreateDefaultDropDown ();
- }
- protected override void Dispose (bool disposing)
- {
- base.Dispose (disposing);
- }
- protected virtual void OnCheckedChanged (EventArgs e)
- {
- if (CheckedChanged != null) CheckedChanged (this, e);
- }
- protected virtual void OnCheckStateChanged (EventArgs e)
- {
- if (CheckStateChanged != null) CheckStateChanged (this, e);
- }
- protected override void OnClick (EventArgs e)
- {
- if (!this.Enabled)
- return;
-
- base.OnClick (e);
- if (this.IsOnDropDown) {
- if (this.HasDropDownItems)
- return;
- this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
- (this.Parent as ToolStripDropDown).Close (ToolStripDropDownCloseReason.ItemClicked);
- Object parent = this.Parent;
- // Find the top level MenuStrip to inform it to close all open
- // dropdowns because this one was clicked
- while (parent != null) {
- if (parent is MenuStrip)
- (parent as MenuStrip).HideMenus (true, ToolStripDropDownCloseReason.ItemClicked);
- if (parent is ToolStripDropDown)
- parent = (parent as ToolStripDropDown).OwnerItem;
- else if (parent is ToolStripItem)
- parent = (parent as ToolStripItem).Parent;
- else
- break;
- }
- }
- if (this.check_on_click)
- this.Checked = !this.Checked;
- }
- protected override void OnDropDownHide (EventArgs e)
- {
- base.OnDropDownHide (e);
- }
- protected override void OnDropDownShow (EventArgs e)
- {
- base.OnDropDownShow (e);
- }
- protected override void OnFontChanged (EventArgs e)
- {
- base.OnFontChanged (e);
- }
- protected override void OnMouseDown (MouseEventArgs e)
- {
- base.OnMouseDown (e);
- if (this.HasDropDownItems)
- if (!this.DropDown.Visible)
- this.ShowDropDown ();
- }
- protected override void OnMouseEnter (EventArgs e)
- {
- base.OnMouseEnter (e);
- if (this.IsOnDropDown && this.HasDropDownItems)
- this.ShowDropDown ();
- }
- protected override void OnMouseLeave (EventArgs e)
- {
- base.OnMouseLeave (e);
- }
- protected override void OnMouseUp (MouseEventArgs e)
- {
- base.OnMouseUp (e);
- }
- protected override void OnOwnerChanged (EventArgs e)
- {
- base.OnOwnerChanged (e);
- }
- protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
- {
- base.OnPaint (e);
- if (this.Owner != null) {
- Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
- Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
- if (this.IsOnDropDown)
- this.Owner.Renderer.DrawMenuItemBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
- else
- this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
- Rectangle text_layout_rect;
- Rectangle image_layout_rect;
- this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
- if (this.IsOnDropDown) {
- text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
- if (image_layout_rect != Rectangle.Empty)
- image_layout_rect = new Rectangle (4, 3, draw_image.Width, draw_image.Height);
- if (this.Checked)
- this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
- }
- if (text_layout_rect != Rectangle.Empty)
- this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
- if (image_layout_rect != Rectangle.Empty)
- this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
- if (this.IsOnDropDown && this.HasDropDownItems)
- this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
- return;
- }
- }
- #endregion
- #region Public Events
- public event EventHandler CheckedChanged;
- public event EventHandler CheckStateChanged;
- #endregion
- }
- }
- #endif
|