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)
31 using System.ComponentModel;
32 using System.Windows.Forms.Design;
34 namespace System.Windows.Forms
36 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip)]
37 public class ToolStripButton : ToolStripItem
39 private CheckState checked_state;
40 private bool check_on_click;
42 #region Public Constructors
43 public ToolStripButton ()
44 : this (null, null, null, String.Empty)
48 public ToolStripButton (Image image)
49 : this (null, image, null, String.Empty)
53 public ToolStripButton (string text)
54 : this (text, null, null, String.Empty)
58 public ToolStripButton (string text, Image image)
59 : this (text, image, null, String.Empty)
63 public ToolStripButton (string text, Image image, EventHandler onClick)
64 : this (text, image, onClick, String.Empty)
68 public ToolStripButton (string text, Image image, EventHandler onClick, string name)
69 : base (text, image, onClick, name)
71 this.checked_state = CheckState.Unchecked;
72 this.ToolTipText = String.Empty;
76 #region Public Properties
78 public new bool AutoToolTip {
79 get { return base.AutoToolTip; }
80 set { base.AutoToolTip = value; }
83 public override bool CanSelect {
87 [DefaultValue (false)]
90 switch (this.checked_state) {
91 case CheckState.Unchecked:
94 case CheckState.Checked:
95 case CheckState.Indeterminate:
100 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
101 this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
102 this.OnCheckedChanged (EventArgs.Empty);
103 this.OnCheckStateChanged (EventArgs.Empty);
109 [DefaultValue (false)]
110 public bool CheckOnClick {
111 get { return this.check_on_click; }
113 if (this.check_on_click != value) {
114 this.check_on_click = value;
115 OnUIACheckOnClickChangedEvent (EventArgs.Empty);
120 [DefaultValue (CheckState.Unchecked)]
121 public CheckState CheckState {
122 get { return this.checked_state; }
124 if (this.checked_state != value) {
125 if (!Enum.IsDefined (typeof (CheckState), value))
126 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
128 this.checked_state = value;
129 this.OnCheckedChanged (EventArgs.Empty);
130 this.OnCheckStateChanged (EventArgs.Empty);
137 #region Protected Properties
138 protected override bool DefaultAutoToolTip { get { return true; } }
141 #region Public Methods
142 public override Size GetPreferredSize (Size constrainingSize)
144 Size retval = base.GetPreferredSize (constrainingSize);
146 if (retval.Width < 23)
153 #region Protected Methods
154 [EditorBrowsable (EditorBrowsableState.Advanced)]
155 protected override AccessibleObject CreateAccessibilityInstance ()
157 ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
159 ao.default_action = "Press";
160 ao.role = AccessibleRole.PushButton;
161 ao.state = AccessibleStates.Focusable;
166 protected virtual void OnCheckedChanged (EventArgs e)
168 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
173 protected virtual void OnCheckStateChanged (EventArgs e)
175 EventHandler eh = (EventHandler)(Events [CheckStateChangedEvent]);
180 protected override void OnClick (EventArgs e)
182 if (this.check_on_click)
183 this.Checked = !this.Checked;
187 ToolStrip ts = this.GetTopLevelToolStrip ();
190 ts.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
193 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
197 if (this.Owner != null) {
198 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
199 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
201 this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
203 Rectangle text_layout_rect;
204 Rectangle image_layout_rect;
206 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
208 if (text_layout_rect != Rectangle.Empty)
209 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
210 if (image_layout_rect != Rectangle.Empty)
211 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
218 #region Public Events
219 static object CheckedChangedEvent = new object ();
220 static object CheckStateChangedEvent = new object ();
222 public event EventHandler CheckedChanged {
223 add { Events.AddHandler (CheckedChangedEvent, value); }
224 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
227 public event EventHandler CheckStateChanged {
228 add { Events.AddHandler (CheckStateChangedEvent, value); }
229 remove { Events.RemoveHandler (CheckStateChangedEvent, value); }
233 #region UIA Framework Events
234 static object UIACheckOnClickChangedEvent = new object ();
236 internal event EventHandler UIACheckOnClickChanged {
237 add { Events.AddHandler (UIACheckOnClickChangedEvent, value); }
238 remove { Events.RemoveHandler (UIACheckOnClickChangedEvent, value); }
241 internal void OnUIACheckOnClickChangedEvent (EventArgs args)
244 = (EventHandler) Events [UIACheckOnClickChangedEvent];