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; }
113 set { this.check_on_click = value; }
116 [DefaultValue (CheckState
.Unchecked
)]
117 public CheckState CheckState
{
118 get { return this.checked_state; }
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
);
133 #region Protected Properties
134 protected override bool DefaultAutoToolTip { get { return true; }
}
137 #region Public Methods
138 public override Size
GetPreferredSize (Size constrainingSize
)
140 Size retval
= base.GetPreferredSize (constrainingSize
);
142 if (retval
.Width
< 23)
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
;
162 protected virtual void OnCheckedChanged (EventArgs e
)
164 EventHandler eh
= (EventHandler
)(Events
[CheckedChangedEvent
]);
169 protected virtual void OnCheckStateChanged (EventArgs e
)
171 EventHandler eh
= (EventHandler
)(Events
[CheckStateChangedEvent
]);
176 protected override void OnClick (EventArgs e
)
178 if (this.check_on_click
)
179 this.Checked
= !this.Checked
;
183 ToolStrip ts
= this.GetTopLevelToolStrip ();
186 ts
.Dismiss (ToolStripDropDownCloseReason
.ItemClicked
);
189 protected override void OnPaint (System
.Windows
.Forms
.PaintEventArgs 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
));
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); }