2 // ToolStripDropDownItem.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.Threading;
35 namespace System.Windows.Forms
37 public abstract class ToolStripDropDownItem : ToolStripItem
39 private ToolStripDropDown drop_down;
40 private ToolStripDropDownDirection drop_down_direction;
42 #region Protected Constructors
43 protected ToolStripDropDownItem () : this (string.Empty, null, null, string.Empty)
47 protected ToolStripDropDownItem (string text, Image image, EventHandler onClick)
48 : this (text, image, onClick, string.Empty)
52 protected ToolStripDropDownItem (string text, Image image, params ToolStripItem[] dropDownItems)
53 : this (text, image, null, string.Empty)
57 protected ToolStripDropDownItem (string text, Image image, EventHandler onClick, string name)
58 : base (text, image, onClick, name)
60 this.drop_down = CreateDefaultDropDown ();
61 this.drop_down.ItemAdded += new ToolStripItemEventHandler (DropDown_ItemAdded);
65 #region Public Properties
66 public ToolStripDropDown DropDown {
67 get { return this.drop_down; }
68 set { this.drop_down = value; }
71 public ToolStripDropDownDirection DropDownDirection {
72 get { return this.drop_down_direction; }
74 if (!Enum.IsDefined (typeof (ToolStripDropDownDirection), value))
75 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripDropDownDirection", value));
77 this.drop_down_direction = value;
81 public ToolStripItemCollection DropDownItems {
82 get { return this.drop_down.Items; }
85 public virtual bool HasDropDownItems {
86 get { return this.drop_down.Items.Count != 0; }
89 public override bool Pressed {
90 get { return base.Pressed && this.HasDropDownItems; }
94 #region Protected Properties
95 protected internal virtual Point DropDownLocation {
99 if (this.IsOnDropDown) {
100 p = Parent.PointToScreen (new Point (this.Bounds.Left, this.Bounds.Top - 1));
101 p.X += this.Bounds.Width;
102 p.Y += this.Bounds.Left;
106 p = new Point (this.Bounds.Left, this.Bounds.Bottom - 1);
108 return Parent.PointToScreen (p);
113 #region Public Methods
114 public void HideDropDown ()
116 if (!this.DropDown.Visible)
119 this.DropDown.Close (ToolStripDropDownCloseReason.CloseCalled);
120 this.is_pressed = false;
122 this.OnDropDownHide (EventArgs.Empty);
123 this.OnDropDownClosed (EventArgs.Empty);
126 public void ShowDropDown ()
128 this.DropDown.OwnerItem = this;
130 this.DropDown.Show (this.DropDownLocation);
131 this.OnDropDownShow (EventArgs.Empty);
135 #region Protected Methods
136 protected virtual ToolStripDropDown CreateDefaultDropDown ()
138 return new ToolStripDropDown ();
141 protected override void Dispose (bool disposing)
143 base.Dispose (disposing);
146 protected override void OnBoundsChanged ()
148 base.OnBoundsChanged ();
151 protected internal virtual void OnDropDownClosed (EventArgs e)
153 if (DropDownClosed != null) DropDownClosed (this, e);
156 protected virtual void OnDropDownHide (EventArgs e)
160 protected internal virtual void OnDropDownItemClicked (ToolStripItemClickedEventArgs e)
162 if (DropDownItemClicked != null) DropDownItemClicked (this, e);
165 protected internal virtual void OnDropDownOpened (EventArgs e)
167 if (DropDownOpened != null) DropDownOpened (this, e);
170 protected virtual void OnDropDownShow (EventArgs e)
174 protected override void OnFontChanged (EventArgs e)
176 base.OnFontChanged (e);
180 #region Public Events
181 public event EventHandler DropDownClosed;
182 public event ToolStripItemClickedEventHandler DropDownItemClicked;
183 public event EventHandler DropDownOpened;
184 public event EventHandler DropDownOpening;
187 #region Internal Methods
188 internal void HideDropDown (ToolStripDropDownCloseReason reason)
190 if (!this.DropDown.Visible)
193 this.DropDown.Close (reason);
194 this.is_pressed = false;
196 this.OnDropDownHide (EventArgs.Empty);
197 this.OnDropDownClosed (EventArgs.Empty);
200 private void DropDown_ItemAdded (object sender, ToolStripItemEventArgs e)
202 e.Item.owner_item = this;