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 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip)]
38 public class ToolStripButton : ToolStripItem
40 private CheckState checked_state;
41 private bool check_on_click;
43 #region Public Constructors
44 public ToolStripButton ()
45 : this (null, null, null, String.Empty)
49 public ToolStripButton (Image image)
50 : this (null, image, null, String.Empty)
54 public ToolStripButton (string text)
55 : this (text, null, null, String.Empty)
59 public ToolStripButton (string text, Image image)
60 : this (text, image, null, String.Empty)
64 public ToolStripButton (string text, Image image, EventHandler onClick)
65 : this (text, image, onClick, String.Empty)
69 public ToolStripButton (string text, Image image, EventHandler onClick, string name)
70 : base (text, image, onClick, name)
72 this.checked_state = CheckState.Unchecked;
73 this.ToolTipText = String.Empty;
77 #region Public Properties
79 public new bool AutoToolTip {
80 get { return base.AutoToolTip; }
81 set { base.AutoToolTip = value; }
84 public override bool CanSelect {
88 [DefaultValue (false)]
91 switch (this.checked_state) {
92 case CheckState.Unchecked:
95 case CheckState.Checked:
96 case CheckState.Indeterminate:
101 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
102 this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
103 this.OnCheckedChanged (EventArgs.Empty);
104 this.OnCheckStateChanged (EventArgs.Empty);
110 [DefaultValue (false)]
111 public bool CheckOnClick {
112 get { return this.check_on_click; }
114 if (this.check_on_click != value) {
115 this.check_on_click = value;
116 OnUIACheckOnClickChangedEvent (EventArgs.Empty);
121 [DefaultValue (CheckState.Unchecked)]
122 public CheckState CheckState {
123 get { return this.checked_state; }
125 if (this.checked_state != value) {
126 if (!Enum.IsDefined (typeof (CheckState), value))
127 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
129 this.checked_state = value;
130 this.OnCheckedChanged (EventArgs.Empty);
131 this.OnCheckStateChanged (EventArgs.Empty);
138 #region Protected Properties
139 protected override bool DefaultAutoToolTip { get { return true; } }
142 #region Public Methods
143 public override Size GetPreferredSize (Size constrainingSize)
145 Size retval = base.GetPreferredSize (constrainingSize);
147 if (retval.Width < 23)
154 #region Protected Methods
155 [EditorBrowsable (EditorBrowsableState.Advanced)]
156 protected override AccessibleObject CreateAccessibilityInstance ()
158 ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
160 ao.default_action = "Press";
161 ao.role = AccessibleRole.PushButton;
162 ao.state = AccessibleStates.Focusable;
167 protected virtual void OnCheckedChanged (EventArgs e)
169 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
174 protected virtual void OnCheckStateChanged (EventArgs e)
176 EventHandler eh = (EventHandler)(Events [CheckStateChangedEvent]);
181 protected override void OnClick (EventArgs e)
183 if (this.check_on_click)
184 this.Checked = !this.Checked;
188 ToolStrip ts = this.GetTopLevelToolStrip ();
191 ts.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
194 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
198 if (this.Owner != null) {
199 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
200 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
202 this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
204 Rectangle text_layout_rect;
205 Rectangle image_layout_rect;
207 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
209 if (text_layout_rect != Rectangle.Empty)
210 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
211 if (image_layout_rect != Rectangle.Empty)
212 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
219 #region Public Events
220 static object CheckedChangedEvent = new object ();
221 static object CheckStateChangedEvent = new object ();
223 public event EventHandler CheckedChanged {
224 add { Events.AddHandler (CheckedChangedEvent, value); }
225 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
228 public event EventHandler CheckStateChanged {
229 add { Events.AddHandler (CheckStateChangedEvent, value); }
230 remove { Events.RemoveHandler (CheckStateChangedEvent, value); }
234 #region UIA Framework Events
235 static object UIACheckOnClickChangedEvent = new object ();
237 internal event EventHandler UIACheckOnClickChanged {
238 add { Events.AddHandler (UIACheckOnClickChangedEvent, value); }
239 remove { Events.RemoveHandler (UIACheckOnClickChangedEvent, value); }
242 internal void OnUIACheckOnClickChangedEvent (EventArgs args)
245 = (EventHandler) Events [UIACheckOnClickChangedEvent];