- Added support for Enter and Escape keys in dialogs
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / CheckBox.cs
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.
19 //
20 // Copyright (c) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Dennis Hayes    dennish@raytek.com
24 //      Peter Bartok    pbartok@novell.com
25 //
26
27 // COMPLETE
28
29 using System;
30 using System.ComponentModel;
31 using System.Drawing;
32
33 namespace System.Windows.Forms {
34         [DefaultProperty("Checked")]
35         [DefaultEvent("CheckedChanged")]
36         public class CheckBox : ButtonBase {
37                 #region Local Variables
38                 internal Appearance             appearance;
39                 internal bool                   auto_check;
40                 internal ContentAlignment       check_alignment;
41                 internal CheckState             check_state;
42                 internal bool                   three_state;
43                 #endregion      // Local Variables
44
45
46                 #region Public Constructors
47                 public CheckBox() {
48                         appearance = Appearance.Normal;
49                         auto_check = true;
50                         check_alignment = ContentAlignment.MiddleLeft;
51                         text_alignment = ContentAlignment.MiddleLeft;
52                 }
53                 #endregion      // Public Constructors
54
55                 #region Internal Methods
56                 internal override void Draw (PaintEventArgs pe) {
57                         if (redraw) {
58                                 ThemeEngine.Current.DrawCheckBox (this.DeviceContext, this.ClientRectangle, this);
59                                 redraw = false;
60                         }
61                 }
62
63                 internal override void HaveDoubleClick() {
64                         if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
65                 }
66                 #endregion      // Internal Methods
67
68                 #region Public Instance Properties
69                 [DefaultValue(Appearance.Normal)]
70                 [Localizable(true)]
71                 public Appearance Appearance {
72                         get {
73                                 return appearance;
74                         }
75
76                         set {
77                                 if (value != appearance) {
78                                         appearance = value;
79                                         if (AppearanceChanged != null) {
80                                                 AppearanceChanged(this, EventArgs.Empty);
81                                         }
82                                         Redraw();
83                                 }
84                         }
85                 }
86
87                 [DefaultValue(true)]
88                 public bool AutoCheck {
89                         get {
90                                 return auto_check;
91                         }
92
93                         set {
94                                 auto_check = value;
95                         }
96                 }
97
98                 [Bindable(true)]
99                 [Localizable(true)]
100                 [DefaultValue(ContentAlignment.MiddleLeft)]
101                 public ContentAlignment CheckAlign {
102                         get {
103                                 return check_alignment;
104                         }
105
106                         set {
107                                 if (value != check_alignment) {
108                                         check_alignment = value;
109
110                                         Redraw();
111                                 }
112                         }
113                 }
114
115                 [Bindable(true)]
116                 [RefreshProperties(RefreshProperties.All)]
117                 [DefaultValue(false)]
118                 public bool Checked {
119                         get {
120                                 if (check_state != CheckState.Unchecked) {
121                                         return true;
122                                 }
123                                 return false;
124                         }
125
126                         set {
127                                 if (value && (check_state != CheckState.Checked)) {
128                                         check_state = CheckState.Checked;
129                                         Redraw();
130                                         OnCheckedChanged(EventArgs.Empty);
131                                 } else if (!value && (check_state != CheckState.Unchecked)) {
132                                         check_state = CheckState.Unchecked;
133                                         Redraw();
134                                         OnCheckedChanged(EventArgs.Empty);
135                                 }
136                         }
137                 }
138
139                 [DefaultValue(CheckState.Unchecked)]
140                 [RefreshProperties(RefreshProperties.All)]
141                 [Bindable(true)]
142                 public CheckState CheckState {
143                         get {
144                                 return check_state;
145                         }
146
147                         set {
148                                 if (value != check_state) {
149                                         bool    was_checked = (check_state != CheckState.Unchecked);
150
151                                         check_state = value;
152
153                                         if (was_checked != (check_state != CheckState.Unchecked)) {
154                                                 OnCheckedChanged(EventArgs.Empty);
155                                         }
156
157                                         OnCheckStateChanged(EventArgs.Empty);
158                                         Redraw();
159                                 }
160                         }
161                 }
162
163                 [DefaultValue(ContentAlignment.MiddleLeft)]
164                 [Localizable(true)]
165                 public override ContentAlignment TextAlign {
166                         get {
167                                 return text_alignment;
168                         }
169
170                         set {
171                                 if (value != text_alignment) {
172                                         text_alignment = value;
173                                         Redraw();
174                                 }
175                         }
176                 }
177
178
179                 [DefaultValue(false)]
180                 public bool ThreeState {
181                         get {
182                                 return three_state;
183                         }
184
185                         set {
186                                 three_state = value;
187                         }
188                 }
189                 #endregion      // Public Instance Properties
190
191                 #region Protected Instance Properties
192                 protected override CreateParams CreateParams {
193                         get {
194                                 return base.CreateParams;
195                         }
196                 }
197
198                 protected override Size DefaultSize {
199                         get {
200                                 return new Size(104, 24);
201                         }
202                 }
203                 #endregion      // Protected Instance Properties
204
205                 #region Public Instance Methods
206                 public override string ToString() {
207                         return base.ToString() + ", CheckState: " + (int)check_state;
208                 }
209                 #endregion      // Public Instance Methods
210
211                 #region Protected Instance Methods
212                 protected override AccessibleObject CreateAccessibilityInstance() {
213                         return base.CreateAccessibilityInstance ();
214                 }
215
216                 protected virtual void OnAppearanceChanged(EventArgs e) {
217                         if (AppearanceChanged != null) {
218                                 AppearanceChanged(this, e);
219                         }
220                 }
221
222                 protected virtual void OnCheckedChanged(EventArgs e) {
223                         if (CheckedChanged != null) {
224                                 CheckedChanged(this, e);
225                         }
226                 }
227
228                 protected virtual void OnCheckStateChanged(EventArgs e) {
229                         if (CheckStateChanged != null) {
230                                 CheckStateChanged(this, e);
231                         }
232                 }
233
234                 protected override void OnClick(EventArgs e) {
235                         if (auto_check) {
236                                 switch(check_state) {
237                                         case CheckState.Unchecked: {
238                                                 if (three_state) {
239                                                         CheckState = CheckState.Indeterminate;
240                                                 } else {
241                                                         CheckState = CheckState.Checked;
242                                                 }
243                                                 break;
244                                         }
245
246                                         case CheckState.Indeterminate: {
247                                                 CheckState = CheckState.Checked;
248                                                 break;
249                                         }
250
251                                         case CheckState.Checked: {
252                                                 CheckState = CheckState.Unchecked;
253                                                 break;
254                                         }
255                                 }
256                         }
257                 }
258
259                 protected override void OnHandleCreated(EventArgs e) {
260                         base.OnHandleCreated (e);
261                 }
262
263                 protected override void OnMouseUp(MouseEventArgs e) {
264                         base.OnMouseUp (e);
265                 }
266
267                 protected override bool ProcessMnemonic(char charCode) {
268                         return base.ProcessMnemonic (charCode);
269                 }
270                 #endregion      // Protected Instance Methods
271
272                 #region Events
273                 public event EventHandler       AppearanceChanged;
274                 public event EventHandler       CheckedChanged;
275                 public event EventHandler       CheckStateChanged;
276                 #endregion      // Events
277
278                 #region Events
279                 [Browsable(false)]
280                 [EditorBrowsable (EditorBrowsableState.Never)]
281                 public event EventHandler DoubleClick;
282                 #endregion      // Events
283         }
284 }