update readme (#21797)
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / CheckBox.cs
blob3d8ac100058e776ae9ad21e4764365ec1a823f57
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
22 // Authors:
23 // Dennis Hayes dennish@raytek.com
24 // Peter Bartok pbartok@novell.com
27 using System;
28 using System.ComponentModel;
29 using System.Drawing;
30 using System.Runtime.InteropServices;
32 namespace System.Windows.Forms {
33 [DefaultProperty("Checked")]
34 [DefaultEvent("CheckedChanged")]
35 [ComVisible (true)]
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
49 [ComVisible(true)]
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 {
63 get {
64 return "Select";
68 public override AccessibleRole Role {
69 get {
70 return AccessibleRole.CheckButton;
74 public override AccessibleStates State {
75 get {
76 AccessibleStates retval;
78 retval = AccessibleStates.Default;
80 if (owner.check_state == CheckState.Checked) {
81 retval |= AccessibleStates.Checked;
84 if (owner.Focused) {
85 retval |= AccessibleStates.Focused;
88 if (owner.CanFocus) {
89 retval |= AccessibleStates.Focusable;
92 return retval;
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
107 public CheckBox() {
108 appearance = Appearance.Normal;
109 auto_check = true;
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);
130 // Draw our button
131 if (FlatStyle != FlatStyle.System)
132 ThemeEngine.Current.DrawCheckBox (pe.Graphics, this, glyph_rectangle, text_rectangle, image_rectangle, pe.ClipRectangle);
133 else
134 ThemeEngine.Current.DrawCheckBox (pe.Graphics, this.ClientRectangle, this);
137 internal override Size GetPreferredSizeCore (Size proposedSize)
139 if (this.AutoSize)
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)]
152 [Localizable(true)]
153 public Appearance Appearance {
154 get {
155 return appearance;
158 set {
159 if (value != appearance) {
160 appearance = value;
161 OnAppearanceChanged (EventArgs.Empty);
163 if (Parent != null)
164 Parent.PerformLayout (this, "Appearance");
165 Invalidate();
170 [DefaultValue(true)]
171 public bool AutoCheck {
172 get {
173 return auto_check;
176 set {
177 auto_check = value;
181 [Bindable(true)]
182 [Localizable(true)]
183 [DefaultValue(ContentAlignment.MiddleLeft)]
184 public ContentAlignment CheckAlign {
185 get {
186 return check_alignment;
189 set {
190 if (value != check_alignment) {
191 check_alignment = value;
192 if (Parent != null)
193 Parent.PerformLayout (this, "CheckAlign");
194 Invalidate();
199 [Bindable(true)]
200 [RefreshProperties(RefreshProperties.All)]
201 [DefaultValue(false)]
202 [SettingsBindable (true)]
203 public bool Checked {
204 get {
205 if (check_state != CheckState.Unchecked) {
206 return true;
208 return false;
211 set {
212 if (value && (check_state != CheckState.Checked)) {
213 check_state = CheckState.Checked;
214 Invalidate();
215 OnCheckedChanged(EventArgs.Empty);
216 } else if (!value && (check_state != CheckState.Unchecked)) {
217 check_state = CheckState.Unchecked;
218 Invalidate();
219 OnCheckedChanged(EventArgs.Empty);
224 [DefaultValue(CheckState.Unchecked)]
225 [RefreshProperties(RefreshProperties.All)]
226 [Bindable(true)]
227 public CheckState CheckState {
228 get {
229 return check_state;
232 set {
233 if (value != check_state) {
234 bool was_checked = (check_state != CheckState.Unchecked);
236 check_state = value;
238 if (was_checked != (check_state != CheckState.Unchecked)) {
239 OnCheckedChanged(EventArgs.Empty);
242 OnCheckStateChanged(EventArgs.Empty);
243 Invalidate();
248 [DefaultValue(ContentAlignment.MiddleLeft)]
249 [Localizable(true)]
250 public override ContentAlignment TextAlign {
251 get { return base.TextAlign; }
252 set { base.TextAlign = value; }
256 [DefaultValue(false)]
257 public bool ThreeState {
258 get {
259 return three_state;
262 set {
263 three_state = value;
266 #endregion // Public Instance Properties
268 #region Protected Instance Properties
269 protected override CreateParams CreateParams {
270 get {
271 return base.CreateParams;
275 protected override Size DefaultSize {
276 get {
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() {
290 AccessibleObject ao;
292 ao = base.CreateAccessibilityInstance ();
293 ao.role = AccessibleRole.CheckButton;
295 return ao;
298 protected virtual void OnAppearanceChanged(EventArgs e) {
299 EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
300 if (eh != null)
301 eh (this, e);
304 protected virtual void OnCheckedChanged(EventArgs e) {
305 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
306 if (eh != null)
307 eh (this, e);
310 protected virtual void OnCheckStateChanged(EventArgs e) {
311 EventHandler eh = (EventHandler)(Events [CheckStateChangedEvent]);
312 if (eh != null)
313 eh (this, e);
316 protected override void OnClick(EventArgs e) {
317 if (auto_check) {
318 switch(check_state) {
319 case CheckState.Unchecked: {
320 if (three_state) {
321 CheckState = CheckState.Indeterminate;
322 } else {
323 CheckState = CheckState.Checked;
325 break;
328 case CheckState.Indeterminate: {
329 CheckState = CheckState.Checked;
330 break;
333 case CheckState.Checked: {
334 CheckState = CheckState.Unchecked;
335 break;
340 base.OnClick (e);
343 protected override void OnHandleCreated(EventArgs e) {
344 base.OnHandleCreated (e);
347 protected override void OnKeyDown (KeyEventArgs e)
349 base.OnKeyDown (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) {
358 Select();
359 OnClick(EventArgs.Empty);
360 return true;
363 return base.ProcessMnemonic(charCode);
365 #endregion // Protected Instance Methods
367 #region Events
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); }
387 [Browsable (false)]
388 [EditorBrowsable (EditorBrowsableState.Never)]
389 public new event MouseEventHandler MouseDoubleClick {
390 add { base.MouseDoubleClick += value; }
391 remove { base.MouseDoubleClick -= value; }
393 #endregion // Events
395 #region Events
396 // XXX have a look at this and determine if it
397 // manipulates base.DoubleClick, and see if
398 // HaveDoubleClick can just call OnDoubleClick.
399 [Browsable(false)]
400 [EditorBrowsable (EditorBrowsableState.Never)]
401 public new event EventHandler DoubleClick;
402 #endregion // Events