New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripDropDownItem.cs
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.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 #if NET_2_0
29
30 using System;
31 using System.Drawing;
32 using System.ComponentModel;
33 using System.Threading;
34
35 namespace System.Windows.Forms
36 {
37         public abstract class ToolStripDropDownItem : ToolStripItem
38         {
39                 private ToolStripDropDown drop_down;
40                 private ToolStripDropDownDirection drop_down_direction;
41
42                 #region Protected Constructors
43                 protected ToolStripDropDownItem () : this (string.Empty, null, null, string.Empty)
44                 {
45                 }
46
47                 protected ToolStripDropDownItem (string text, Image image, EventHandler onClick)
48                         : this (text, image, onClick, string.Empty)
49                 {
50                 }
51
52                 protected ToolStripDropDownItem (string text, Image image, params ToolStripItem[] dropDownItems)
53                         : this (text, image, null, string.Empty)
54                 {
55                 }
56
57                 protected ToolStripDropDownItem (string text, Image image, EventHandler onClick, string name)
58                         : base (text, image, onClick, name)
59                 {
60                         this.drop_down = CreateDefaultDropDown ();
61                         this.drop_down.ItemAdded += new ToolStripItemEventHandler (DropDown_ItemAdded);
62                 }
63                 #endregion
64
65                 #region Public Properties
66                 public ToolStripDropDown DropDown {
67                         get { return this.drop_down; }
68                         set { this.drop_down = value; }
69                 }
70
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));
76
77                                 this.drop_down_direction = value;
78                         }
79                 }
80
81                 public ToolStripItemCollection DropDownItems {
82                         get { return this.drop_down.Items; }
83                 }
84
85                 public virtual bool HasDropDownItems {
86                         get { return this.drop_down.Items.Count != 0; }
87                 }
88
89                 public override bool Pressed {
90                         get { return base.Pressed && this.HasDropDownItems; }
91                 }
92                 #endregion
93
94                 #region Protected Properties
95                 protected internal virtual Point DropDownLocation {
96                         get {
97                                 Point p;
98
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;
104                                 }
105                                 else
106                                         p = new Point (this.Bounds.Left, this.Bounds.Bottom - 1);
107
108                                 return Parent.PointToScreen (p);
109                         }
110                 }
111                 #endregion
112
113                 #region Public Methods
114                 public void HideDropDown ()
115                 {
116                         if (!this.DropDown.Visible)
117                                 return;
118
119                         this.DropDown.Close (ToolStripDropDownCloseReason.CloseCalled);
120                         this.is_pressed = false;
121                         this.Invalidate ();
122                         this.OnDropDownHide (EventArgs.Empty);
123                         this.OnDropDownClosed (EventArgs.Empty);
124                 }
125
126                 public void ShowDropDown ()
127                 {
128                         this.DropDown.OwnerItem = this;
129                         
130                         this.DropDown.Show (this.DropDownLocation);
131                         this.OnDropDownShow (EventArgs.Empty);
132                 }
133                 #endregion
134
135                 #region Protected Methods
136                 protected virtual ToolStripDropDown CreateDefaultDropDown ()
137                 {
138                         return new ToolStripDropDown ();
139                 }
140
141                 protected override void Dispose (bool disposing)
142                 {
143                         base.Dispose (disposing);
144                 }
145
146                 protected override void OnBoundsChanged ()
147                 {
148                         base.OnBoundsChanged ();
149                 }
150
151                 protected internal virtual void OnDropDownClosed (EventArgs e)
152                 {
153                         if (DropDownClosed != null) DropDownClosed (this, e);
154                 }
155
156                 protected virtual void OnDropDownHide (EventArgs e)
157                 {
158                 }
159
160                 protected internal virtual void OnDropDownItemClicked (ToolStripItemClickedEventArgs e)
161                 {
162                         if (DropDownItemClicked != null) DropDownItemClicked (this, e);
163                 }
164
165                 protected internal virtual void OnDropDownOpened (EventArgs e)
166                 {
167                         if (DropDownOpened != null) DropDownOpened (this, e);
168                 }
169
170                 protected virtual void OnDropDownShow (EventArgs e)
171                 {
172                 }
173
174                 protected override void OnFontChanged (EventArgs e)
175                 {
176                         base.OnFontChanged (e);
177                 }
178                 #endregion
179
180                 #region Public Events
181                 public event EventHandler DropDownClosed;
182                 public event ToolStripItemClickedEventHandler DropDownItemClicked;
183                 public event EventHandler DropDownOpened;
184                 public event EventHandler DropDownOpening;
185                 #endregion
186
187                 #region Internal Methods
188                 internal void HideDropDown (ToolStripDropDownCloseReason reason)
189                 {
190                         if (!this.DropDown.Visible)
191                                 return;
192
193                         this.DropDown.Close (reason);
194                         this.is_pressed = false;
195                         this.Invalidate ();
196                         this.OnDropDownHide (EventArgs.Empty);
197                         this.OnDropDownClosed (EventArgs.Empty);
198                 }
199                 
200                 private void DropDown_ItemAdded (object sender, ToolStripItemEventArgs e)
201                 {
202                         e.Item.owner_item = this;
203                 }
204                 #endregion
205         }
206 }
207 #endif