2007-03-19 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripSplitButton.cs
blob3cd9329f07ff9d06a7aef93cfda92fb8c28e4389
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.
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.Windows.Forms.Design;
35 namespace System.Windows.Forms
37 [DefaultEvent ("ButtonClick")]
38 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
39 public class ToolStripSplitButton : ToolStripDropDownItem
41 private bool button_pressed;
42 private bool drop_down_button_selected;
43 private int drop_down_button_width;
45 #region Public Constructors
46 public ToolStripSplitButton()
47 : this (string.Empty, null, null, string.Empty)
51 public ToolStripSplitButton (Image image)
52 : this (string.Empty, image, null, string.Empty)
56 public ToolStripSplitButton (string text)
57 : this (text, null, null, string.Empty)
61 public ToolStripSplitButton (string text, Image image)
62 : this (text, image, null, string.Empty)
66 public ToolStripSplitButton (string text, Image image, EventHandler onClick)
67 : this (text, image, onClick, string.Empty)
71 public ToolStripSplitButton (string text, Image image, params ToolStripItem[] dropDownItems)
72 : base (text, image, dropDownItems)
74 this.ResetDropDownButtonWidth ();
77 public ToolStripSplitButton (string text, Image image, EventHandler onClick, string name)
78 : base (text, image, onClick, name)
80 this.ResetDropDownButtonWidth ();
82 #endregion
84 #region Public Properties
85 [DefaultValue (true)]
86 public new bool AutoToolTip {
87 get { return base.AutoToolTip; }
88 set { base.AutoToolTip = value; }
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); }
96 [Browsable (false)]
97 public bool ButtonPressed {
98 get { return this.button_pressed; }
101 [Browsable (false)]
102 public bool ButtonSelected {
103 get { return base.Selected; }
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); }
111 [Browsable (false)]
112 public bool DropDownButtonPressed {
113 get { return this.drop_down_button_selected || this.DropDown.Visible; }
116 [Browsable (false)]
117 public bool DropDownButtonSelected {
118 get { return base.Selected; }
121 public int DropDownButtonWidth {
122 get { return this.drop_down_button_width; }
123 set {
124 if (value < 0)
125 throw new ArgumentOutOfRangeException ();
127 this.drop_down_button_width = value;
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); }
135 #endregion
137 #region Protected Properties
138 protected override bool DefaultAutoToolTip {
139 get { return true; }
142 protected internal override bool DismissWhenClicked {
143 get { return true; }
145 #endregion
147 #region Public Methods
148 public override Size GetPreferredSize (Size constrainingSize)
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);
154 if (s.Width < 23)
155 s.Width = 23;
157 s.Width += (this.drop_down_button_width - 2);
159 return s;
162 public virtual void OnButtonDoubleClick (EventArgs e)
164 EventHandler eh = (EventHandler)(Events [ButtonDoubleClickEvent]);
165 if (eh != null)
166 eh (this, e);
169 public void PerformButtonClick ()
171 if (this.Enabled)
172 this.OnButtonClick (EventArgs.Empty);
175 [EditorBrowsable (EditorBrowsableState.Never)]
176 public virtual void ResetDropDownButtonWidth ()
178 this.DropDownButtonWidth = 11;
180 #endregion
182 #region Protected Methods
183 protected override ToolStripDropDown CreateDefaultDropDown ()
185 ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
186 tsddm.OwnerItem = this;
187 return tsddm;
190 protected virtual void OnButtonClick (EventArgs e)
192 EventHandler eh = (EventHandler)Events [ButtonClickEvent];
193 if (eh != null)
194 eh (this, e);
197 protected virtual void OnDefaultItemChanged (EventArgs e)
199 EventHandler eh = (EventHandler)Events [DefaultItemChangedEvent];
200 if (eh != null)
201 eh (this, e);
204 protected override void OnMouseDown (MouseEventArgs e)
206 if (this.ButtonBounds.Contains (e.Location))
208 this.button_pressed = true;
209 this.Invalidate ();
210 base.OnMouseDown (e);
212 else if (this.DropDownButtonBounds.Contains (e.Location))
214 if (this.DropDown.Visible)
215 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
216 else
217 this.ShowDropDown ();
219 this.Invalidate ();
223 protected override void OnMouseLeave (EventArgs e)
225 this.drop_down_button_selected = false;
226 this.button_pressed = false;
228 this.Invalidate ();
230 base.OnMouseLeave (e);
233 protected override void OnMouseUp (MouseEventArgs e)
235 this.button_pressed = false;
236 this.Invalidate ();
238 if (this.ButtonBounds.Contains (e.Location))
239 base.OnMouseUp (e);
242 protected override void OnPaint (PaintEventArgs e)
244 base.OnPaint (e);
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);
250 this.Owner.Renderer.DrawSplitButton (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
252 Rectangle text_layout_rect;
253 Rectangle image_layout_rect;
255 Rectangle r = this.ContentRectangle;
256 r.Width -= (this.drop_down_button_width + 1);
258 this.CalculateTextAndImageRectangles (r, out text_layout_rect, out image_layout_rect);
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));
265 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Width - 9, 1, 6, this.Height), Color.Black, ArrowDirection.Down));
267 return;
271 #endregion
273 #region Public Events
274 static object ButtonClickEvent = new object ();
275 static object ButtonDoubleClickEvent = new object ();
276 static object DefaultItemChangedEvent = new object ();
278 public event EventHandler ButtonClick {
279 add { Events.AddHandler (ButtonClickEvent, value); }
280 remove {Events.RemoveHandler (ButtonClickEvent, value); }
282 public event EventHandler ButtonDoubleClick {
283 add { Events.AddHandler (ButtonDoubleClickEvent, value); }
284 remove {Events.RemoveHandler (ButtonDoubleClickEvent, value); }
286 public event EventHandler DefaultItemChanged {
287 add { Events.AddHandler (DefaultItemChangedEvent, value); }
288 remove {Events.RemoveHandler (DefaultItemChangedEvent, value); }
290 #endregion
293 #endif