* TextBoxBase.cs: Take HideSelection into account when
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripDropDownItem.cs
blob0df8ac06a3da01f8f2a89f57ec8a07c73ff22965
1 //
2 // ToolStripDropDownItem.cs
3 //
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:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
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
25 // Authors:
26 // Jonathan Pobst (monkey@jpobst.com)
28 #if NET_2_0
30 using System;
31 using System.Drawing;
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);
63 #endregion
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; }
73 set {
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; }
92 #endregion
94 #region Protected Properties
95 protected internal virtual Point DropDownLocation {
96 get {
97 Point p;
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;
103 return p;
105 else
106 p = new Point (this.Bounds.Left, this.Bounds.Bottom - 1);
108 return Parent.PointToScreen (p);
111 #endregion
113 #region Public Methods
114 public void HideDropDown ()
116 if (!this.DropDown.Visible)
117 return;
119 this.DropDown.Close (ToolStripDropDownCloseReason.CloseCalled);
120 this.is_pressed = false;
121 this.Invalidate ();
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);
133 #endregion
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]);
154 if (eh != null)
155 eh (this, e);
158 protected virtual void OnDropDownHide (EventArgs e)
162 protected internal virtual void OnDropDownItemClicked (ToolStripItemClickedEventArgs e)
164 ToolStripItemClickedEventHandler eh = (ToolStripItemClickedEventHandler)(Events [DropDownClosedEvent]);
165 if (eh != null)
166 eh (this, e);
169 protected internal virtual void OnDropDownOpened (EventArgs e)
171 EventHandler eh = (EventHandler)(Events [DropDownOpenedEvent]);
172 if (eh != null)
173 eh (this, e);
176 protected virtual void OnDropDownShow (EventArgs e)
180 protected override void OnFontChanged (EventArgs e)
182 base.OnFontChanged (e);
184 #endregion
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); }
211 #endregion
213 #region Internal Methods
214 internal void HideDropDown (ToolStripDropDownCloseReason reason)
216 if (!this.DropDown.Visible)
217 return;
219 this.DropDown.Close (reason);
220 this.is_pressed = false;
221 this.Invalidate ();
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;
230 #endregion
233 #endif