In System.Windows.Forms:
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripButton.cs
blob3c42223c33e3d32e03240c313e2df12cbc39b811
1 //
2 // ToolStripButton.cs
3 //
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:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
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
25 // Authors:
26 // Jonathan Pobst (monkey@jpobst.com)
29 #if NET_2_0
30 using System;
31 using System.Drawing;
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;
75 #endregion
77 #region Public Properties
78 [DefaultValue (true)]
79 public new bool AutoToolTip {
80 get { return base.AutoToolTip; }
81 set { base.AutoToolTip = value; }
84 public override bool CanSelect {
85 get { return true; }
88 [DefaultValue (false)]
89 public bool Checked {
90 get {
91 switch (this.checked_state) {
92 case CheckState.Unchecked:
93 default:
94 return false;
95 case CheckState.Checked:
96 case CheckState.Indeterminate:
97 return true;
100 set {
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);
105 this.Invalidate ();
110 [DefaultValue (false)]
111 public bool CheckOnClick {
112 get { return this.check_on_click; }
113 set { this.check_on_click = value; }
116 [DefaultValue (CheckState.Unchecked)]
117 public CheckState CheckState {
118 get { return this.checked_state; }
119 set {
120 if (this.checked_state != value) {
121 if (!Enum.IsDefined (typeof (CheckState), value))
122 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
124 this.checked_state = value;
125 this.OnCheckedChanged (EventArgs.Empty);
126 this.OnCheckStateChanged (EventArgs.Empty);
127 this.Invalidate ();
131 #endregion
133 #region Protected Properties
134 protected override bool DefaultAutoToolTip { get { return true; } }
135 #endregion
137 #region Public Methods
138 public override Size GetPreferredSize (Size constrainingSize)
140 Size retval = base.GetPreferredSize (constrainingSize);
142 if (retval.Width < 23)
143 retval.Width = 23;
145 return retval;
147 #endregion
149 #region Protected Methods
150 [EditorBrowsable (EditorBrowsableState.Advanced)]
151 protected override AccessibleObject CreateAccessibilityInstance ()
153 ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
155 ao.default_action = "Press";
156 ao.role = AccessibleRole.PushButton;
157 ao.state = AccessibleStates.Focusable;
159 return ao;
162 protected virtual void OnCheckedChanged (EventArgs e)
164 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
165 if (eh != null)
166 eh (this, e);
169 protected virtual void OnCheckStateChanged (EventArgs e)
171 EventHandler eh = (EventHandler)(Events [CheckStateChangedEvent]);
172 if (eh != null)
173 eh (this, e);
176 protected override void OnClick (EventArgs e)
178 if (this.check_on_click)
179 this.Checked = !this.Checked;
181 base.OnClick (e);
183 ToolStrip ts = this.GetTopLevelToolStrip ();
185 if (ts != null)
186 ts.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
189 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
191 base.OnPaint (e);
193 if (this.Owner != null) {
194 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
195 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
197 this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
199 Rectangle text_layout_rect;
200 Rectangle image_layout_rect;
202 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
204 if (text_layout_rect != Rectangle.Empty)
205 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
206 if (image_layout_rect != Rectangle.Empty)
207 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
209 return;
212 #endregion
214 #region Public Events
215 static object CheckedChangedEvent = new object ();
216 static object CheckStateChangedEvent = new object ();
218 public event EventHandler CheckedChanged {
219 add { Events.AddHandler (CheckedChangedEvent, value); }
220 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
223 public event EventHandler CheckStateChanged {
224 add { Events.AddHandler (CheckStateChangedEvent, value); }
225 remove { Events.RemoveHandler (CheckStateChangedEvent, value); }
227 #endregion
230 #endif