2 // ToolStripSplitButton.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.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 ToolStripItem default_item;
43 private bool drop_down_button_selected;
44 private int drop_down_button_width;
46 #region Public Constructors
47 public ToolStripSplitButton()
48 : this (string.Empty, null, null, string.Empty)
52 public ToolStripSplitButton (Image image)
53 : this (string.Empty, image, null, string.Empty)
57 public ToolStripSplitButton (string text)
58 : this (text, null, null, string.Empty)
62 public ToolStripSplitButton (string text, Image image)
63 : this (text, image, null, string.Empty)
67 public ToolStripSplitButton (string text, Image image, EventHandler onClick)
68 : this (text, image, onClick, string.Empty)
72 public ToolStripSplitButton (string text, Image image, params ToolStripItem[] dropDownItems)
73 : base (text, image, dropDownItems)
75 this.ResetDropDownButtonWidth ();
78 public ToolStripSplitButton (string text, Image image, EventHandler onClick, string name)
79 : base (text, image, onClick, name)
81 this.ResetDropDownButtonWidth ();
85 #region Public Properties
87 public new bool AutoToolTip {
88 get { return base.AutoToolTip; }
89 set { base.AutoToolTip = value; }
93 public Rectangle ButtonBounds {
94 get { return new Rectangle (this.Bounds.Left, this.Bounds.Top, this.Bounds.Width - this.drop_down_button_width - 1, this.Height); }
98 public bool ButtonPressed {
99 get { return this.button_pressed; }
103 public bool ButtonSelected {
104 get { return base.Selected; }
108 [DefaultValue (null)]
109 public ToolStripItem DefaultItem {
110 get { return this.default_item; }
112 if (this.default_item != value) {
113 this.default_item = value;
114 this.OnDefaultItemChanged (EventArgs.Empty);
120 public Rectangle DropDownButtonBounds {
121 get { return new Rectangle (this.Bounds.Right - this.drop_down_button_width, this.Bounds.Top, this.drop_down_button_width, this.Bounds.Height); }
125 public bool DropDownButtonPressed {
126 get { return this.drop_down_button_selected || (this.HasDropDownItems && this.DropDown.Visible); }
130 public bool DropDownButtonSelected {
131 get { return base.Selected; }
134 public int DropDownButtonWidth {
135 get { return this.drop_down_button_width; }
138 throw new ArgumentOutOfRangeException ();
140 this.drop_down_button_width = value;
145 public Rectangle SplitterBounds {
146 get { return new Rectangle (this.Bounds.Width - this.drop_down_button_width - 1, this.Bounds.Top, 1, this.Height); }
150 #region Protected Properties
151 protected override bool DefaultAutoToolTip {
155 protected internal override bool DismissWhenClicked {
160 #region Public Methods
161 public override Size GetPreferredSize (Size constrainingSize)
163 // base should calculate the button part for us, add the splitter
164 // and drop down arrow part to that
165 Size s = base.GetPreferredSize (constrainingSize);
170 s.Width += (this.drop_down_button_width - 2);
175 public virtual void OnButtonDoubleClick (EventArgs e)
177 EventHandler eh = (EventHandler)(Events [ButtonDoubleClickEvent]);
182 public void PerformButtonClick ()
185 this.OnButtonClick (EventArgs.Empty);
188 [EditorBrowsable (EditorBrowsableState.Never)]
189 public virtual void ResetDropDownButtonWidth ()
191 this.DropDownButtonWidth = 11;
195 #region Protected Methods
196 protected override AccessibleObject CreateAccessibilityInstance ()
198 return new ToolStripSplitButtonAccessibleObject (this);
201 protected override ToolStripDropDown CreateDefaultDropDown ()
203 ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
204 tsddm.OwnerItem = this;
208 protected virtual void OnButtonClick (EventArgs e)
210 EventHandler eh = (EventHandler)Events [ButtonClickEvent];
215 protected virtual void OnDefaultItemChanged (EventArgs e)
217 EventHandler eh = (EventHandler)Events [DefaultItemChangedEvent];
222 protected override void OnMouseDown (MouseEventArgs e)
224 if (this.ButtonBounds.Contains (e.Location))
226 this.button_pressed = true;
228 base.OnMouseDown (e);
230 else if (this.DropDownButtonBounds.Contains (e.Location))
232 if (this.DropDown.Visible)
233 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
235 this.ShowDropDown ();
241 protected override void OnMouseLeave (EventArgs e)
243 this.drop_down_button_selected = false;
244 this.button_pressed = false;
248 base.OnMouseLeave (e);
251 protected override void OnMouseUp (MouseEventArgs e)
253 this.button_pressed = false;
256 if (this.ButtonBounds.Contains (e.Location))
260 protected override void OnPaint (PaintEventArgs e)
264 if (this.Owner != null) {
265 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
266 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
268 this.Owner.Renderer.DrawSplitButton (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
270 Rectangle text_layout_rect;
271 Rectangle image_layout_rect;
273 Rectangle r = this.ContentRectangle;
274 r.Width -= (this.drop_down_button_width + 1);
276 this.CalculateTextAndImageRectangles (r, out text_layout_rect, out image_layout_rect);
278 if (text_layout_rect != Rectangle.Empty)
279 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
280 if (image_layout_rect != Rectangle.Empty)
281 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
283 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Width - 9, 1, 6, this.Height), Color.Black, ArrowDirection.Down));
289 protected override void OnRightToLeftChanged (EventArgs e)
291 base.OnRightToLeftChanged (e);
294 protected internal override bool ProcessDialogKey (Keys keyData)
296 if (this.Selected && keyData == Keys.Enter && this.DefaultItem != null) {
297 this.DefaultItem.FireEvent (EventArgs.Empty, ToolStripItemEventType.Click);
301 return base.ProcessDialogKey (keyData);
304 protected internal override bool ProcessMnemonic (char charCode)
307 this.Parent.ChangeSelection (this);
309 if (this.HasDropDownItems)
310 this.ShowDropDown ();
312 this.PerformClick ();
318 #region Public Events
319 static object ButtonClickEvent = new object ();
320 static object ButtonDoubleClickEvent = new object ();
321 static object DefaultItemChangedEvent = new object ();
323 public event EventHandler ButtonClick {
324 add { Events.AddHandler (ButtonClickEvent, value); }
325 remove {Events.RemoveHandler (ButtonClickEvent, value); }
327 public event EventHandler ButtonDoubleClick {
328 add { Events.AddHandler (ButtonDoubleClickEvent, value); }
329 remove {Events.RemoveHandler (ButtonDoubleClickEvent, value); }
331 public event EventHandler DefaultItemChanged {
332 add { Events.AddHandler (DefaultItemChangedEvent, value); }
333 remove {Events.RemoveHandler (DefaultItemChangedEvent, value); }
337 #region ToolStripSplitButtonAccessibleObject Class
338 public class ToolStripSplitButtonAccessibleObject : ToolStripItemAccessibleObject
340 #region Public Constructor
341 public ToolStripSplitButtonAccessibleObject (ToolStripSplitButton item) : base (item)
346 #region Public Method
347 public override void DoDefaultAction ()
349 (owner_item as ToolStripSplitButton).PerformButtonClick ();