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 (Bounds.Left, 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, 0, 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 ();
139 if (this.drop_down_button_width != value) {
140 this.drop_down_button_width = value;
141 CalculateAutoSize ();
147 public Rectangle SplitterBounds {
148 get { return new Rectangle (this.Bounds.Width - this.drop_down_button_width - 1, 0, 1, this.Height); }
152 #region Protected Properties
153 protected override bool DefaultAutoToolTip {
157 protected internal override bool DismissWhenClicked {
162 #region Public Methods
163 public override Size GetPreferredSize (Size constrainingSize)
165 // base should calculate the button part for us, add the splitter
166 // and drop down arrow part to that
167 Size s = base.GetPreferredSize (constrainingSize);
172 // If we are a fixed size, we can't add more in for the drop down
173 // button, but we can for autosize
175 s.Width += (this.drop_down_button_width - 2);
180 public virtual void OnButtonDoubleClick (EventArgs e)
182 EventHandler eh = (EventHandler)(Events [ButtonDoubleClickEvent]);
187 public void PerformButtonClick ()
190 this.OnButtonClick (EventArgs.Empty);
193 [EditorBrowsable (EditorBrowsableState.Never)]
194 public virtual void ResetDropDownButtonWidth ()
196 this.DropDownButtonWidth = 11;
200 #region Protected Methods
201 protected override AccessibleObject CreateAccessibilityInstance ()
203 return new ToolStripSplitButtonAccessibleObject (this);
206 protected override ToolStripDropDown CreateDefaultDropDown ()
208 ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
209 tsddm.OwnerItem = this;
213 protected virtual void OnButtonClick (EventArgs e)
215 EventHandler eh = (EventHandler)Events [ButtonClickEvent];
220 protected virtual void OnDefaultItemChanged (EventArgs e)
222 EventHandler eh = (EventHandler)Events [DefaultItemChangedEvent];
227 protected override void OnMouseDown (MouseEventArgs e)
229 if (this.ButtonBounds.Contains (e.Location))
231 this.button_pressed = true;
233 base.OnMouseDown (e);
235 else if (this.DropDownButtonBounds.Contains (e.Location))
237 if (this.DropDown.Visible)
238 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
240 this.ShowDropDown ();
243 base.OnMouseDown (e);
247 protected override void OnMouseLeave (EventArgs e)
249 this.drop_down_button_selected = false;
250 this.button_pressed = false;
254 base.OnMouseLeave (e);
257 protected override void OnMouseUp (MouseEventArgs e)
259 this.button_pressed = false;
265 protected override void OnPaint (PaintEventArgs e)
269 if (this.Owner != null) {
270 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
271 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
273 this.Owner.Renderer.DrawSplitButton (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
275 Rectangle text_layout_rect;
276 Rectangle image_layout_rect;
278 Rectangle r = this.ContentRectangle;
279 r.Width -= (this.drop_down_button_width + 1);
281 this.CalculateTextAndImageRectangles (r, out text_layout_rect, out image_layout_rect);
283 if (text_layout_rect != Rectangle.Empty)
284 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
285 if (image_layout_rect != Rectangle.Empty)
286 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
288 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Width - 9, 1, 6, this.Height), Color.Black, ArrowDirection.Down));
294 protected override void OnRightToLeftChanged (EventArgs e)
296 base.OnRightToLeftChanged (e);
299 protected internal override bool ProcessDialogKey (Keys keyData)
301 if (this.Selected && keyData == Keys.Enter && this.DefaultItem != null) {
302 this.DefaultItem.FireEvent (EventArgs.Empty, ToolStripItemEventType.Click);
306 return base.ProcessDialogKey (keyData);
309 protected internal override bool ProcessMnemonic (char charCode)
312 this.Parent.ChangeSelection (this);
314 if (this.HasDropDownItems)
315 this.ShowDropDown ();
317 this.PerformClick ();
323 #region Internal Methods
324 internal override void HandleClick (int mouse_clicks, EventArgs e)
326 base.HandleClick (mouse_clicks, e);
328 MouseEventArgs mea = e as MouseEventArgs;
331 if (ButtonBounds.Contains (mea.Location))
332 OnButtonClick (EventArgs.Empty);
336 #region Public Events
337 static object ButtonClickEvent = new object ();
338 static object ButtonDoubleClickEvent = new object ();
339 static object DefaultItemChangedEvent = new object ();
341 public event EventHandler ButtonClick {
342 add { Events.AddHandler (ButtonClickEvent, value); }
343 remove {Events.RemoveHandler (ButtonClickEvent, value); }
345 public event EventHandler ButtonDoubleClick {
346 add { Events.AddHandler (ButtonDoubleClickEvent, value); }
347 remove {Events.RemoveHandler (ButtonDoubleClickEvent, value); }
349 public event EventHandler DefaultItemChanged {
350 add { Events.AddHandler (DefaultItemChangedEvent, value); }
351 remove {Events.RemoveHandler (DefaultItemChangedEvent, value); }
355 #region ToolStripSplitButtonAccessibleObject Class
356 public class ToolStripSplitButtonAccessibleObject : ToolStripItemAccessibleObject
358 #region Public Constructor
359 public ToolStripSplitButtonAccessibleObject (ToolStripSplitButton item) : base (item)
364 #region Public Method
365 public override void DoDefaultAction ()
367 (owner_item as ToolStripSplitButton).PerformButtonClick ();