**** Merged from MCS ****
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / CheckBox.cs
blobd54093872c5506c553d2351d41d5743d23785482
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 Novell, Inc.
22 // Authors:
23 // Dennis Hayes dennish@raytek.com
24 // Peter Bartok pbartok@novell.com
27 // $Log: CheckBox.cs,v $
28 // Revision 1.10 2004/10/15 13:32:45 ravindra
29 // - Renamed Paint() method to Draw() for clarity. Also, moved
30 // DrawImage() to OnPaint().
32 // Revision 1.9 2004/10/15 13:25:50 ravindra
33 // - Removed Redraw (), we get it from ButtonBase.
34 // - Implemented Paint (), to do class specific painting.
36 // Revision 1.8 2004/10/13 02:46:22 pbartok
37 // - Fix from John BouAntoun: Now properly sets the Appearance property
39 // Revision 1.7 2004/09/28 18:44:25 pbartok
40 // - Streamlined Theme interfaces:
41 // * Each DrawXXX method for a control now is passed the object for the
42 // control to be drawn in order to allow accessing any state the theme
43 // might require
45 // * ControlPaint methods for the theme now have a CP prefix to avoid
46 // name clashes with the Draw methods for controls
48 // * Every control now retrieves it's DefaultSize from the current theme
50 // Revision 1.6 2004/09/01 20:01:24 pbartok
51 // - Added missing default
52 // - Added missing region mark
54 // Revision 1.5 2004/09/01 01:55:58 pbartok
55 // - Fixed to match the removal of the needs_redraw concept
57 // Revision 1.4 2004/08/31 18:48:31 pbartok
58 // - Finished (famous last words)
60 // Revision 1.3 2004/08/30 20:42:26 pbartok
61 // - Implemented CheckBox drawing code
63 // Revision 1.2 2004/08/30 15:44:20 pbartok
64 // - Updated to fix broken build. Not complete yet.
66 // Revision 1.1 2004/07/09 05:21:25 pbartok
67 // - Initial check-in
71 // COMPLETE
73 using System;
74 using System.Drawing;
76 namespace System.Windows.Forms {
77 public class CheckBox : ButtonBase {
78 #region Local Variables
79 internal Appearance appearance;
80 internal bool auto_check;
81 internal ContentAlignment check_alignment;
82 internal ContentAlignment text_alignment;
83 internal CheckState check_state;
84 internal bool three_state;
85 #endregion // Local Variables
88 #region Public Constructors
89 public CheckBox() {
90 appearance = Appearance.Normal;
91 auto_check = true;
92 check_alignment = ContentAlignment.MiddleLeft;
93 text_alignment = ContentAlignment.MiddleLeft;
95 #endregion // Public Constructors
97 #region Public Instance Properties
98 public Appearance Appearance {
99 get {
100 return appearance;
103 set {
104 if (value != appearance) {
105 appearance = value;
106 if (AppearanceChanged != null) {
107 AppearanceChanged(this, EventArgs.Empty);
109 Redraw();
114 public bool AutoCheck {
115 get {
116 return auto_check;
119 set {
120 auto_check = value;
124 public ContentAlignment CheckAlign {
125 get {
126 return check_alignment;
129 set {
130 if (value != check_alignment) {
131 check_alignment = value;
133 Redraw();
138 public bool Checked {
139 get {
140 if (check_state != CheckState.Unchecked) {
141 return true;
143 return false;
146 set {
147 if (value && (check_state != CheckState.Checked)) {
148 check_state = CheckState.Checked;
149 Redraw();
150 OnCheckedChanged(EventArgs.Empty);
151 } else if (!value && (check_state != CheckState.Unchecked)) {
152 check_state = CheckState.Unchecked;
153 Redraw();
154 OnCheckedChanged(EventArgs.Empty);
159 public CheckState CheckState {
160 get {
161 return check_state;
164 set {
165 if (value != check_state) {
166 bool was_checked = (check_state != CheckState.Unchecked);
168 check_state = value;
170 if (was_checked != (check_state != CheckState.Unchecked)) {
171 OnCheckedChanged(EventArgs.Empty);
174 OnCheckStateChanged(EventArgs.Empty);
175 Redraw();
180 public override ContentAlignment TextAlign {
181 get {
182 return text_alignment;
185 set {
186 if (value != text_alignment) {
187 text_alignment = value;
188 Redraw();
194 public bool ThreeState {
195 get {
196 return three_state;
199 set {
200 three_state = value;
203 #endregion // Public Instance Properties
205 #region Protected Instance Properties
206 protected override CreateParams CreateParams {
207 get {
208 return base.CreateParams;
212 protected override Size DefaultSize {
213 get {
214 return new Size(104, 24);
217 #endregion // Protected Instance Properties
219 #region Public Instance Methods
220 public override string ToString() {
221 return base.ToString() + ", CheckState: " + (int)check_state;
223 #endregion // Public Instance Methods
225 #region Protected Instance Methods
226 protected override AccessibleObject CreateAccessibilityInstance() {
227 return base.CreateAccessibilityInstance ();
230 protected virtual void OnAppearanceChanged(EventArgs e) {
231 if (AppearanceChanged != null) {
232 AppearanceChanged(this, e);
236 protected virtual void OnCheckedChanged(EventArgs e) {
237 if (CheckedChanged != null) {
238 CheckedChanged(this, e);
242 protected virtual void OnCheckStateChanged(EventArgs e) {
243 if (CheckStateChanged != null) {
244 CheckStateChanged(this, e);
248 protected override void OnClick(EventArgs e) {
249 if (auto_check) {
250 switch(check_state) {
251 case CheckState.Unchecked: {
252 if (three_state) {
253 CheckState = CheckState.Indeterminate;
254 } else {
255 CheckState = CheckState.Checked;
257 break;
260 case CheckState.Indeterminate: {
261 CheckState = CheckState.Checked;
262 break;
265 case CheckState.Checked: {
266 CheckState = CheckState.Unchecked;
267 break;
273 protected override void OnHandleCreated(EventArgs e) {
274 base.OnHandleCreated (e);
277 protected override void OnMouseUp(MouseEventArgs e) {
278 base.OnMouseUp (e);
281 protected override bool ProcessMnemonic(char charCode) {
282 return base.ProcessMnemonic (charCode);
284 #endregion // Protected Instance Methods
286 #region Events
287 public event EventHandler AppearanceChanged;
288 public event EventHandler CheckedChanged;
289 public event EventHandler CheckStateChanged;
290 #endregion // Events
292 #region Internal drawing code
293 internal override void Draw (PaintEventArgs pe) {
294 if (redraw) {
295 ThemeEngine.Current.DrawCheckBox (this.DeviceContext, this.ClientRectangle, this);
296 redraw = false;
299 #endregion // Internal drawing code