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.DropDown.Visible; }
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 EventHandler eh
= (EventHandler
)(Events
[DropDownClosedEvent
]);
158 protected virtual void OnDropDownHide (EventArgs e
)
162 protected internal virtual void OnDropDownItemClicked (ToolStripItemClickedEventArgs e
)
164 ToolStripItemClickedEventHandler eh
= (ToolStripItemClickedEventHandler
)(Events
[DropDownClosedEvent
]);
169 protected internal virtual void OnDropDownOpened (EventArgs e
)
171 EventHandler eh
= (EventHandler
)(Events
[DropDownOpenedEvent
]);
176 protected virtual void OnDropDownShow (EventArgs e
)
180 protected override void OnFontChanged (EventArgs e
)
182 base.OnFontChanged (e
);
186 #region Public Events
187 static object DropDownClosedEvent
= new object ();
188 static object DropDownItemClickedEvent
= new object ();
189 static object DropDownOpenedEvent
= new object ();
190 static object DropDownOpeningEvent
= new object ();
192 public event EventHandler DropDownClosed
{
193 add { Events.AddHandler (DropDownClosedEvent, value); }
194 remove { Events.RemoveHandler (DropDownClosedEvent, value); }
197 public event ToolStripItemClickedEventHandler DropDownItemClicked
{
198 add { Events.AddHandler (DropDownItemClickedEvent, value); }
199 remove { Events.RemoveHandler (DropDownItemClickedEvent, value); }
202 public event EventHandler DropDownOpened
{
203 add { Events.AddHandler (DropDownOpenedEvent, value); }
204 remove { Events.RemoveHandler (DropDownOpenedEvent, value); }
207 public event EventHandler DropDownOpening
{
208 add { Events.AddHandler (DropDownOpeningEvent, value); }
209 remove { Events.RemoveHandler (DropDownOpeningEvent, value); }
213 #region Internal Methods
214 internal void HideDropDown (ToolStripDropDownCloseReason reason
)
216 if (!this.DropDown
.Visible
)
219 this.DropDown
.Close (reason
);
220 this.is_pressed
= false;
222 this.OnDropDownHide (EventArgs
.Empty
);
223 this.OnDropDownClosed (EventArgs
.Empty
);
226 private void DropDown_ItemAdded (object sender
, ToolStripItemEventArgs e
)
228 e
.Item
.owner_item
= this;