Mark tests as not working under TARGET_JVM
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripSplitButton.cs
1 //
2 // ToolStripSplitButton.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.Windows.Forms.Design;
34
35 namespace System.Windows.Forms
36 {
37         [DefaultEvent ("ButtonClick")]
38         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
39         public class ToolStripSplitButton : ToolStripDropDownItem
40         {
41                 private bool button_pressed;
42                 private bool drop_down_button_selected;
43                 private int drop_down_button_width;
44                 
45                 #region Public Constructors
46                 public ToolStripSplitButton()
47                         : this (string.Empty, null, null, string.Empty)
48                 {
49                 }
50                 
51                 public ToolStripSplitButton (Image image)
52                         : this (string.Empty, image, null, string.Empty)
53                 {
54                 }
55                 
56                 public ToolStripSplitButton (string text)
57                         : this (text, null, null, string.Empty)
58                 {
59                 }
60                 
61                 public ToolStripSplitButton (string text, Image image)
62                         : this (text, image, null, string.Empty)
63                 {
64                 }
65                 
66                 public ToolStripSplitButton (string text, Image image, EventHandler onClick)
67                         : this (text, image, onClick, string.Empty)
68                 {
69                 }
70                 
71                 public ToolStripSplitButton (string text, Image image, params ToolStripItem[] dropDownItems)
72                         : base (text, image, dropDownItems)
73                 {
74                         this.ResetDropDownButtonWidth ();
75                 }
76
77                 public ToolStripSplitButton (string text, Image image, EventHandler onClick, string name)
78                         : base (text, image, onClick, name)
79                 {
80                         this.ResetDropDownButtonWidth ();
81                 }
82                 #endregion
83
84                 #region Public Properties
85                 [DefaultValue (true)]
86                 public new bool AutoToolTip {
87                         get { return base.AutoToolTip; }
88                         set { base.AutoToolTip = value; }
89                 }
90
91                 [Browsable (false)]
92                 public Rectangle ButtonBounds {
93                         get { return new Rectangle (this.Bounds.Left, this.Bounds.Top, this.Bounds.Width - this.drop_down_button_width - 1, this.Height); }
94                 }
95
96                 [Browsable (false)]
97                 public bool ButtonPressed {
98                         get { return this.button_pressed; }
99                 }
100
101                 [Browsable (false)]
102                 public bool ButtonSelected {
103                         get { return base.Selected; }
104                 }
105
106                 [Browsable (false)]
107                 public Rectangle DropDownButtonBounds {
108                         get { return new Rectangle (this.Bounds.Right - this.drop_down_button_width, this.Bounds.Top, this.drop_down_button_width, this.Bounds.Height); }
109                 }
110
111                 [Browsable (false)]
112                 public bool DropDownButtonPressed {
113                         get { return this.drop_down_button_selected || this.DropDown.Visible; }
114                 }
115
116                 [Browsable (false)]
117                 public bool DropDownButtonSelected {
118                         get { return base.Selected; }
119                 }
120                 
121                 public int DropDownButtonWidth {
122                         get { return this.drop_down_button_width; }
123                         set { 
124                                 if (value < 0)
125                                         throw new ArgumentOutOfRangeException ();
126                                         
127                                 this.drop_down_button_width = value;
128                         }
129                 }
130
131                 [Browsable (false)]
132                 public Rectangle SplitterBounds {
133                         get { return new Rectangle (this.Bounds.Width - this.drop_down_button_width - 1, this.Bounds.Top, 1, this.Height); }
134                 }
135                 #endregion
136
137                 #region Protected Properties
138                 protected override bool DefaultAutoToolTip {
139                         get { return true; }
140                 }
141
142                 protected internal override bool DismissWhenClicked {
143                         get { return true; }
144                 }
145                 #endregion
146
147                 #region Public Methods
148                 public override Size GetPreferredSize (Size constrainingSize)
149                 {
150                         // base should calculate the button part for us, add the splitter
151                         // and drop down arrow part to that
152                         Size s = base.GetPreferredSize (constrainingSize);
153
154                         if (s.Width < 23)
155                                 s.Width = 23;
156
157                         s.Width += (this.drop_down_button_width - 2);
158                         
159                         return s;
160                 }
161                 
162                 public virtual void OnButtonDoubleClick (EventArgs e)
163                 {
164                         EventHandler eh = (EventHandler)(Events [ButtonDoubleClickEvent]);
165                         if (eh != null)
166                                 eh (this, e);
167                 }
168                 
169                 public void PerformButtonClick ()
170                 {
171                         if (this.Enabled)
172                                 this.OnButtonClick (EventArgs.Empty);
173                 }
174                 
175                 [EditorBrowsable (EditorBrowsableState.Never)]
176                 public virtual void ResetDropDownButtonWidth ()
177                 {
178                         this.DropDownButtonWidth = 11;
179                 }
180                 #endregion
181
182                 #region Protected Methods
183                 protected override ToolStripDropDown CreateDefaultDropDown ()
184                 {
185                         ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
186                         tsddm.OwnerItem = this;
187                         return tsddm;
188                 }
189                 
190                 protected virtual void OnButtonClick (EventArgs e)
191                 {
192                         EventHandler eh = (EventHandler)Events [ButtonClickEvent];
193                         if (eh != null)
194                                 eh (this, e);
195                 }
196                 
197                 protected virtual void OnDefaultItemChanged (EventArgs e)
198                 {
199                         EventHandler eh = (EventHandler)Events [DefaultItemChangedEvent];
200                         if (eh != null)
201                                 eh (this, e);
202                 }
203
204                 protected override void OnMouseDown (MouseEventArgs e)
205                 {
206                         if (this.ButtonBounds.Contains (e.Location))
207                         {
208                                 this.button_pressed = true;
209                                 this.Invalidate ();
210                                 base.OnMouseDown (e);
211                         }
212                         else if (this.DropDownButtonBounds.Contains (e.Location))
213                         {
214                                 if (this.DropDown.Visible)
215                                         this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
216                                 else
217                                         this.ShowDropDown ();
218                         
219                                 this.Invalidate ();
220                         }
221                 }
222
223                 protected override void OnMouseLeave (EventArgs e)
224                 {
225                         this.drop_down_button_selected = false;
226                         this.button_pressed = false;
227                         
228                         this.Invalidate ();
229                         
230                         base.OnMouseLeave (e);
231                 }
232
233                 protected override void OnMouseUp (MouseEventArgs e)
234                 {
235                         this.button_pressed = false;
236                         this.Invalidate ();
237                         
238                         if (this.ButtonBounds.Contains (e.Location))
239                                 base.OnMouseUp (e);
240                 }
241                 
242                 protected override void OnPaint (PaintEventArgs e)
243                 {
244                         base.OnPaint (e);
245
246                         if (this.Owner != null) {
247                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
248                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
249
250                                 this.Owner.Renderer.DrawSplitButton (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
251
252                                 Rectangle text_layout_rect;
253                                 Rectangle image_layout_rect;
254
255                                 Rectangle r = this.ContentRectangle;
256                                 r.Width -= (this.drop_down_button_width + 1);
257                                 
258                                 this.CalculateTextAndImageRectangles (r, out text_layout_rect, out image_layout_rect);
259
260                                 if (text_layout_rect != Rectangle.Empty)
261                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
262                                 if (image_layout_rect != Rectangle.Empty)
263                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
264
265                                 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Width - 9, 1, 6, this.Height), Color.Black, ArrowDirection.Down));
266                                 
267                                 return;
268                         }
269                 }
270
271                 #endregion
272                 
273                 #region Public Events
274                 static object ButtonClickEvent = new object ();
275                 static object ButtonDoubleClickEvent = new object ();
276                 static object DefaultItemChangedEvent = new object ();
277
278                 public event EventHandler ButtonClick {
279                         add { Events.AddHandler (ButtonClickEvent, value); }
280                         remove {Events.RemoveHandler (ButtonClickEvent, value); }
281                 }
282                 public event EventHandler ButtonDoubleClick {
283                         add { Events.AddHandler (ButtonDoubleClickEvent, value); }
284                         remove {Events.RemoveHandler (ButtonDoubleClickEvent, value); }
285                 }
286                 public event EventHandler DefaultItemChanged {
287                         add { Events.AddHandler (DefaultItemChangedEvent, value); }
288                         remove {Events.RemoveHandler (DefaultItemChangedEvent, value); }
289                 }
290                 #endregion
291         }
292 }
293 #endif