2005-09-08 Peter Dennis Bartok <pbartok@novell.com>
[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-2005 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 using System.Runtime.InteropServices;
33
34 namespace System.Windows.Forms {
35         [DefaultProperty("Checked")]
36         [DefaultEvent("CheckedChanged")]
37         public class CheckBox : ButtonBase {
38                 #region Local Variables
39                 internal Appearance             appearance;
40                 internal bool                   auto_check;
41                 internal ContentAlignment       check_alignment;
42                 internal CheckState             check_state;
43                 internal bool                   three_state;
44                 #endregion      // Local Variables
45
46                 #region CheckBoxAccessibleObject Subclass
47                 [ComVisible(true)]
48                 public class CheckBoxAccessibleObject : ButtonBaseAccessibleObject {
49                         #region CheckBoxAccessibleObject Local Variables
50                         private CheckBox owner;
51                         #endregion      // CheckBoxAccessibleObject Local Variables
52
53                         #region CheckBoxAccessibleObject Constructors
54                         public CheckBoxAccessibleObject(Control owner) : base(owner) {
55                                 this.owner = (CheckBox)owner;
56                         }
57                         #endregion      // CheckBoxAccessibleObject Constructors
58
59                         #region CheckBoxAccessibleObject Properties
60                         public override string DefaultAction {
61                                 get {
62                                         return "Select";
63                                 }
64                         }
65
66                         public override AccessibleRole Role {
67                                 get {
68                                         return AccessibleRole.CheckButton;
69                                 }
70                         }
71
72                         public override AccessibleStates State {
73                                 get {
74                                         AccessibleStates        retval;
75
76                                         retval = AccessibleStates.Default;
77
78                                         if (owner.check_state == CheckState.Checked) {
79                                                 retval |= AccessibleStates.Checked;
80                                         }
81
82                                         if (owner.Focused) {
83                                                 retval |= AccessibleStates.Focused;
84                                         }
85
86                                         if (owner.CanFocus) {
87                                                 retval |= AccessibleStates.Focusable;
88                                         }
89
90                                         return retval;
91                                 }
92                         }
93                         #endregion      // CheckBoxAccessibleObject Properties
94                 }
95                 #endregion      // CheckBoxAccessibleObject Sub-class
96
97                 #region Public Constructors
98                 public CheckBox() {
99                         appearance = Appearance.Normal;
100                         auto_check = true;
101                         check_alignment = ContentAlignment.MiddleLeft;
102                         text_alignment = ContentAlignment.MiddleLeft;
103                 }
104                 #endregion      // Public Constructors
105
106                 #region Internal Methods
107                 internal override void Draw (PaintEventArgs pe) {
108                         ThemeEngine.Current.DrawCheckBox (pe.Graphics, this.ClientRectangle, this);
109                 }
110
111                 internal override void HaveDoubleClick() {
112                         if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
113                 }
114                 #endregion      // Internal Methods
115
116                 #region Public Instance Properties
117                 [DefaultValue(Appearance.Normal)]
118                 [Localizable(true)]
119                 public Appearance Appearance {
120                         get {
121                                 return appearance;
122                         }
123
124                         set {
125                                 if (value != appearance) {
126                                         appearance = value;
127                                         if (AppearanceChanged != null) {
128                                                 AppearanceChanged(this, EventArgs.Empty);
129                                         }
130                                         Redraw();
131                                 }
132                         }
133                 }
134
135                 [DefaultValue(true)]
136                 public bool AutoCheck {
137                         get {
138                                 return auto_check;
139                         }
140
141                         set {
142                                 auto_check = value;
143                         }
144                 }
145
146                 [Bindable(true)]
147                 [Localizable(true)]
148                 [DefaultValue(ContentAlignment.MiddleLeft)]
149                 public ContentAlignment CheckAlign {
150                         get {
151                                 return check_alignment;
152                         }
153
154                         set {
155                                 if (value != check_alignment) {
156                                         check_alignment = value;
157
158                                         Redraw();
159                                 }
160                         }
161                 }
162
163                 [Bindable(true)]
164                 [RefreshProperties(RefreshProperties.All)]
165                 [DefaultValue(false)]
166                 public bool Checked {
167                         get {
168                                 if (check_state != CheckState.Unchecked) {
169                                         return true;
170                                 }
171                                 return false;
172                         }
173
174                         set {
175                                 if (value && (check_state != CheckState.Checked)) {
176                                         check_state = CheckState.Checked;
177                                         Redraw();
178                                         OnCheckedChanged(EventArgs.Empty);
179                                 } else if (!value && (check_state != CheckState.Unchecked)) {
180                                         check_state = CheckState.Unchecked;
181                                         Redraw();
182                                         OnCheckedChanged(EventArgs.Empty);
183                                 }
184                         }
185                 }
186
187                 [DefaultValue(CheckState.Unchecked)]
188                 [RefreshProperties(RefreshProperties.All)]
189                 [Bindable(true)]
190                 public CheckState CheckState {
191                         get {
192                                 return check_state;
193                         }
194
195                         set {
196                                 if (value != check_state) {
197                                         bool    was_checked = (check_state != CheckState.Unchecked);
198
199                                         check_state = value;
200
201                                         if (was_checked != (check_state != CheckState.Unchecked)) {
202                                                 OnCheckedChanged(EventArgs.Empty);
203                                         }
204
205                                         OnCheckStateChanged(EventArgs.Empty);
206                                         Redraw();
207                                 }
208                         }
209                 }
210
211                 [DefaultValue(ContentAlignment.MiddleLeft)]
212                 [Localizable(true)]
213                 public override ContentAlignment TextAlign {
214                         get {
215                                 return text_alignment;
216                         }
217
218                         set {
219                                 if (value != text_alignment) {
220                                         text_alignment = value;
221                                         Redraw();
222                                 }
223                         }
224                 }
225
226
227                 [DefaultValue(false)]
228                 public bool ThreeState {
229                         get {
230                                 return three_state;
231                         }
232
233                         set {
234                                 three_state = value;
235                         }
236                 }
237                 #endregion      // Public Instance Properties
238
239                 #region Protected Instance Properties
240                 protected override CreateParams CreateParams {
241                         get {
242                                 return base.CreateParams;
243                         }
244                 }
245
246                 protected override Size DefaultSize {
247                         get {
248                                 return new Size(104, 24);
249                         }
250                 }
251                 #endregion      // Protected Instance Properties
252
253                 #region Public Instance Methods
254                 public override string ToString() {
255                         return base.ToString() + ", CheckState: " + (int)check_state;
256                 }
257                 #endregion      // Public Instance Methods
258
259                 #region Protected Instance Methods
260                 protected override AccessibleObject CreateAccessibilityInstance() {
261                         AccessibleObject        ao;
262
263                         ao = base.CreateAccessibilityInstance ();
264                         ao.role = AccessibleRole.CheckButton;
265
266                         return ao;
267                 }
268
269                 protected virtual void OnAppearanceChanged(EventArgs e) {
270                         if (AppearanceChanged != null) {
271                                 AppearanceChanged(this, e);
272                         }
273                 }
274
275                 protected virtual void OnCheckedChanged(EventArgs e) {
276                         if (CheckedChanged != null) {
277                                 CheckedChanged(this, e);
278                         }
279                 }
280
281                 protected virtual void OnCheckStateChanged(EventArgs e) {
282                         if (CheckStateChanged != null) {
283                                 CheckStateChanged(this, e);
284                         }
285                 }
286
287                 protected override void OnClick(EventArgs e) {
288                         if (auto_check) {
289                                 switch(check_state) {
290                                         case CheckState.Unchecked: {
291                                                 if (three_state) {
292                                                         CheckState = CheckState.Indeterminate;
293                                                 } else {
294                                                         CheckState = CheckState.Checked;
295                                                 }
296                                                 break;
297                                         }
298
299                                         case CheckState.Indeterminate: {
300                                                 CheckState = CheckState.Checked;
301                                                 break;
302                                         }
303
304                                         case CheckState.Checked: {
305                                                 CheckState = CheckState.Unchecked;
306                                                 break;
307                                         }
308                                 }
309                         }
310                         
311                         base.OnClick (e);
312                 }
313
314                 protected override void OnHandleCreated(EventArgs e) {
315                         base.OnHandleCreated (e);
316                 }
317
318                 protected override void OnMouseUp(MouseEventArgs mevent) {
319                         base.OnMouseUp (mevent);
320                 }
321
322                 protected override bool ProcessMnemonic(char charCode) {
323                         if (IsMnemonic(charCode, Text) == true) {
324                                 Select();
325                                 OnClick(EventArgs.Empty);
326                                 return true;
327                         }
328                         
329                         return base.ProcessMnemonic(charCode);
330                 }
331                 #endregion      // Protected Instance Methods
332
333                 #region Events
334                 public event EventHandler       AppearanceChanged;
335                 public event EventHandler       CheckedChanged;
336                 public event EventHandler       CheckStateChanged;
337                 #endregion      // Events
338
339                 #region Events
340                 [Browsable(false)]
341                 [EditorBrowsable (EditorBrowsableState.Never)]
342                 public event EventHandler DoubleClick;
343                 #endregion      // Events
344         }
345 }