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 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 ();
84 #region Public Properties
86 public new bool AutoToolTip
{
87 get { return base.AutoToolTip; }
88 set { base.AutoToolTip = value; }
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); }
97 public bool ButtonPressed
{
98 get { return this.button_pressed; }
102 public bool ButtonSelected
{
103 get { return base.Selected; }
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); }
112 public bool DropDownButtonPressed
{
113 get { return this.drop_down_button_selected || this.DropDown.Visible; }
117 public bool DropDownButtonSelected
{
118 get { return base.Selected; }
121 public int DropDownButtonWidth
{
122 get { return this.drop_down_button_width; }
125 throw new ArgumentOutOfRangeException ();
127 this.drop_down_button_width
= value;
132 public Rectangle SplitterBounds
{
133 get { return new Rectangle (this.Bounds.Width - this.drop_down_button_width - 1, this.Bounds.Top, 1, this.Height); }
137 #region Protected Properties
138 protected override bool DefaultAutoToolTip
{
142 protected internal override bool DismissWhenClicked
{
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
);
157 s
.Width
+= (this.drop_down_button_width
- 2);
162 public virtual void OnButtonDoubleClick (EventArgs e
)
164 EventHandler eh
= (EventHandler
)(Events
[ButtonDoubleClickEvent
]);
169 public void PerformButtonClick ()
172 this.OnButtonClick (EventArgs
.Empty
);
175 [EditorBrowsable (EditorBrowsableState
.Never
)]
176 public virtual void ResetDropDownButtonWidth ()
178 this.DropDownButtonWidth
= 11;
182 #region Protected Methods
183 protected override ToolStripDropDown
CreateDefaultDropDown ()
185 ToolStripDropDownMenu tsddm
= new ToolStripDropDownMenu ();
186 tsddm
.OwnerItem
= this;
190 protected virtual void OnButtonClick (EventArgs e
)
192 EventHandler eh
= (EventHandler
)Events
[ButtonClickEvent
];
197 protected virtual void OnDefaultItemChanged (EventArgs e
)
199 EventHandler eh
= (EventHandler
)Events
[DefaultItemChangedEvent
];
204 protected override void OnMouseDown (MouseEventArgs e
)
206 if (this.ButtonBounds
.Contains (e
.Location
))
208 this.button_pressed
= true;
210 base.OnMouseDown (e
);
212 else if (this.DropDownButtonBounds
.Contains (e
.Location
))
214 if (this.DropDown
.Visible
)
215 this.HideDropDown (ToolStripDropDownCloseReason
.ItemClicked
);
217 this.ShowDropDown ();
223 protected override void OnMouseLeave (EventArgs e
)
225 this.drop_down_button_selected
= false;
226 this.button_pressed
= false;
230 base.OnMouseLeave (e
);
233 protected override void OnMouseUp (MouseEventArgs e
)
235 this.button_pressed
= false;
238 if (this.ButtonBounds
.Contains (e
.Location
))
242 protected override void OnPaint (PaintEventArgs 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
));
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); }