1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Dennis Hayes dennish@raytek.com
24 // Peter Bartok pbartok@novell.com
28 using System
.ComponentModel
;
30 using System
.Runtime
.InteropServices
;
32 namespace System
.Windows
.Forms
{
33 [DefaultProperty("Checked")]
34 [DefaultEvent("CheckedChanged")]
36 [ClassInterface (ClassInterfaceType
.AutoDispatch
)]
37 [DefaultBindingProperty ("CheckState")]
38 [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts
.AssemblySystem_Design
)]
39 public class CheckBox
: ButtonBase
{
40 #region Local Variables
41 internal Appearance appearance
;
42 internal bool auto_check
;
43 internal ContentAlignment check_alignment
;
44 internal CheckState check_state
;
45 internal bool three_state
;
46 #endregion // Local Variables
48 #region CheckBoxAccessibleObject Subclass
50 public class CheckBoxAccessibleObject
: ButtonBaseAccessibleObject
{
51 #region CheckBoxAccessibleObject Local Variables
52 private new CheckBox owner
;
53 #endregion // CheckBoxAccessibleObject Local Variables
55 #region CheckBoxAccessibleObject Constructors
56 public CheckBoxAccessibleObject(Control owner
) : base(owner
) {
57 this.owner
= (CheckBox
)owner
;
59 #endregion // CheckBoxAccessibleObject Constructors
61 #region CheckBoxAccessibleObject Properties
62 public override string DefaultAction
{
68 public override AccessibleRole Role
{
70 return AccessibleRole
.CheckButton
;
74 public override AccessibleStates State
{
76 AccessibleStates retval
;
78 retval
= AccessibleStates
.Default
;
80 if (owner
.check_state
== CheckState
.Checked
) {
81 retval
|= AccessibleStates
.Checked
;
85 retval
|= AccessibleStates
.Focused
;
89 retval
|= AccessibleStates
.Focusable
;
95 #endregion // CheckBoxAccessibleObject Properties
97 #region CheckBoxAccessibleObject Methods
98 public override void DoDefaultAction ()
100 owner
.Checked
= !owner
.Checked
;
102 #endregion // CheckBoxAccessibleObject Methods
104 #endregion // CheckBoxAccessibleObject Sub-class
106 #region Public Constructors
108 appearance
= Appearance
.Normal
;
110 check_alignment
= ContentAlignment
.MiddleLeft
;
111 TextAlign
= ContentAlignment
.MiddleLeft
;
112 SetStyle(ControlStyles
.StandardDoubleClick
, false);
113 SetAutoSizeMode (AutoSizeMode
.GrowAndShrink
);
114 can_cache_preferred_size
= true;
116 #endregion // Public Constructors
118 #region Internal Methods
119 internal override void Draw (PaintEventArgs pe
) {
120 // FIXME: This should be called every time something that can affect it
121 // is changed, not every paint. Can only change so many things at a time.
123 // Figure out where our text and image should go
124 Rectangle glyph_rectangle
;
125 Rectangle text_rectangle
;
126 Rectangle image_rectangle
;
128 ThemeEngine
.Current
.CalculateCheckBoxTextAndImageLayout (this, Point
.Empty
, out glyph_rectangle
, out text_rectangle
, out image_rectangle
);
131 if (FlatStyle
!= FlatStyle
.System
)
132 ThemeEngine
.Current
.DrawCheckBox (pe
.Graphics
, this, glyph_rectangle
, text_rectangle
, image_rectangle
, pe
.ClipRectangle
);
134 ThemeEngine
.Current
.DrawCheckBox (pe
.Graphics
, this.ClientRectangle
, this);
137 internal override Size
GetPreferredSizeCore (Size proposedSize
)
140 return ThemeEngine
.Current
.CalculateCheckBoxAutoSize (this);
142 return base.GetPreferredSizeCore (proposedSize
);
145 internal override void HaveDoubleClick() {
146 if (DoubleClick
!= null) DoubleClick(this, EventArgs
.Empty
);
148 #endregion // Internal Methods
150 #region Public Instance Properties
151 [DefaultValue(Appearance
.Normal
)]
153 public Appearance Appearance
{
159 if (value != appearance
) {
161 OnAppearanceChanged (EventArgs
.Empty
);
164 Parent
.PerformLayout (this, "Appearance");
171 public bool AutoCheck
{
183 [DefaultValue(ContentAlignment
.MiddleLeft
)]
184 public ContentAlignment CheckAlign
{
186 return check_alignment
;
190 if (value != check_alignment
) {
191 check_alignment
= value;
193 Parent
.PerformLayout (this, "CheckAlign");
200 [RefreshProperties(RefreshProperties
.All
)]
201 [DefaultValue(false)]
202 [SettingsBindable (true)]
203 public bool Checked
{
205 if (check_state
!= CheckState
.Unchecked
) {
212 if (value && (check_state
!= CheckState
.Checked
)) {
213 check_state
= CheckState
.Checked
;
215 OnCheckedChanged(EventArgs
.Empty
);
216 } else if (!value && (check_state
!= CheckState
.Unchecked
)) {
217 check_state
= CheckState
.Unchecked
;
219 OnCheckedChanged(EventArgs
.Empty
);
224 [DefaultValue(CheckState
.Unchecked
)]
225 [RefreshProperties(RefreshProperties
.All
)]
227 public CheckState CheckState
{
233 if (value != check_state
) {
234 bool was_checked
= (check_state
!= CheckState
.Unchecked
);
238 if (was_checked
!= (check_state
!= CheckState
.Unchecked
)) {
239 OnCheckedChanged(EventArgs
.Empty
);
242 OnCheckStateChanged(EventArgs
.Empty
);
248 [DefaultValue(ContentAlignment
.MiddleLeft
)]
250 public override ContentAlignment TextAlign
{
251 get { return base.TextAlign; }
252 set { base.TextAlign = value; }
256 [DefaultValue(false)]
257 public bool ThreeState
{
266 #endregion // Public Instance Properties
268 #region Protected Instance Properties
269 protected override CreateParams CreateParams
{
271 return base.CreateParams
;
275 protected override Size DefaultSize
{
277 return new Size(104, 24);
280 #endregion // Protected Instance Properties
282 #region Public Instance Methods
283 public override string ToString() {
284 return base.ToString() + ", CheckState: " + (int)check_state
;
286 #endregion // Public Instance Methods
288 #region Protected Instance Methods
289 protected override AccessibleObject
CreateAccessibilityInstance() {
292 ao
= base.CreateAccessibilityInstance ();
293 ao
.role
= AccessibleRole
.CheckButton
;
298 protected virtual void OnAppearanceChanged(EventArgs e
) {
299 EventHandler eh
= (EventHandler
)(Events
[AppearanceChangedEvent
]);
304 protected virtual void OnCheckedChanged(EventArgs e
) {
305 EventHandler eh
= (EventHandler
)(Events
[CheckedChangedEvent
]);
310 protected virtual void OnCheckStateChanged(EventArgs e
) {
311 EventHandler eh
= (EventHandler
)(Events
[CheckStateChangedEvent
]);
316 protected override void OnClick(EventArgs e
) {
318 switch(check_state
) {
319 case CheckState
.Unchecked
: {
321 CheckState
= CheckState
.Indeterminate
;
323 CheckState
= CheckState
.Checked
;
328 case CheckState
.Indeterminate
: {
329 CheckState
= CheckState
.Checked
;
333 case CheckState
.Checked
: {
334 CheckState
= CheckState
.Unchecked
;
343 protected override void OnHandleCreated(EventArgs e
) {
344 base.OnHandleCreated (e
);
347 protected override void OnKeyDown (KeyEventArgs e
)
352 protected override void OnMouseUp(MouseEventArgs mevent
) {
353 base.OnMouseUp (mevent
);
356 protected override bool ProcessMnemonic(char charCode
) {
357 if (IsMnemonic(charCode
, Text
) == true) {
359 OnClick(EventArgs
.Empty
);
363 return base.ProcessMnemonic(charCode
);
365 #endregion // Protected Instance Methods
368 static object AppearanceChangedEvent
= new object ();
369 static object CheckedChangedEvent
= new object ();
370 static object CheckStateChangedEvent
= new object ();
372 public event EventHandler AppearanceChanged
{
373 add { Events.AddHandler (AppearanceChangedEvent, value); }
374 remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
377 public event EventHandler CheckedChanged
{
378 add { Events.AddHandler (CheckedChangedEvent, value); }
379 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
382 public event EventHandler CheckStateChanged
{
383 add { Events.AddHandler (CheckStateChangedEvent, value); }
384 remove { Events.RemoveHandler (CheckStateChangedEvent, value); }
388 [EditorBrowsable (EditorBrowsableState
.Never
)]
389 public new event MouseEventHandler MouseDoubleClick
{
390 add { base.MouseDoubleClick += value; }
391 remove { base.MouseDoubleClick -= value; }
396 // XXX have a look at this and determine if it
397 // manipulates base.DoubleClick, and see if
398 // HaveDoubleClick can just call OnDoubleClick.
400 [EditorBrowsable (EditorBrowsableState
.Never
)]
401 public new event EventHandler DoubleClick
;