* TreeView.cs: Don't draw the selected node when we lose
[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                         SetStyle(ControlStyles.StandardDoubleClick, false);
104                 }
105                 #endregion      // Public Constructors
106
107                 #region Internal Methods
108                 internal override void Draw (PaintEventArgs pe) {
109                         ThemeEngine.Current.DrawCheckBox (pe.Graphics, this.ClientRectangle, this);
110                 }
111
112                 internal override void HaveDoubleClick() {
113                         if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
114                 }
115                 #endregion      // Internal Methods
116
117                 #region Public Instance Properties
118                 [DefaultValue(Appearance.Normal)]
119                 [Localizable(true)]
120                 public Appearance Appearance {
121                         get {
122                                 return appearance;
123                         }
124
125                         set {
126                                 if (value != appearance) {
127                                         appearance = value;
128                                         if (AppearanceChanged != null) {
129                                                 AppearanceChanged(this, EventArgs.Empty);
130                                         }
131                                         Redraw();
132                                 }
133                         }
134                 }
135
136                 [DefaultValue(true)]
137                 public bool AutoCheck {
138                         get {
139                                 return auto_check;
140                         }
141
142                         set {
143                                 auto_check = value;
144                         }
145                 }
146
147                 [Bindable(true)]
148                 [Localizable(true)]
149                 [DefaultValue(ContentAlignment.MiddleLeft)]
150                 public ContentAlignment CheckAlign {
151                         get {
152                                 return check_alignment;
153                         }
154
155                         set {
156                                 if (value != check_alignment) {
157                                         check_alignment = value;
158
159                                         Redraw();
160                                 }
161                         }
162                 }
163
164                 [Bindable(true)]
165                 [RefreshProperties(RefreshProperties.All)]
166                 [DefaultValue(false)]
167                 public bool Checked {
168                         get {
169                                 if (check_state != CheckState.Unchecked) {
170                                         return true;
171                                 }
172                                 return false;
173                         }
174
175                         set {
176                                 if (value && (check_state != CheckState.Checked)) {
177                                         check_state = CheckState.Checked;
178                                         Redraw();
179                                         OnCheckedChanged(EventArgs.Empty);
180                                 } else if (!value && (check_state != CheckState.Unchecked)) {
181                                         check_state = CheckState.Unchecked;
182                                         Redraw();
183                                         OnCheckedChanged(EventArgs.Empty);
184                                 }
185                         }
186                 }
187
188                 [DefaultValue(CheckState.Unchecked)]
189                 [RefreshProperties(RefreshProperties.All)]
190                 [Bindable(true)]
191                 public CheckState CheckState {
192                         get {
193                                 return check_state;
194                         }
195
196                         set {
197                                 if (value != check_state) {
198                                         bool    was_checked = (check_state != CheckState.Unchecked);
199
200                                         check_state = value;
201
202                                         if (was_checked != (check_state != CheckState.Unchecked)) {
203                                                 OnCheckedChanged(EventArgs.Empty);
204                                         }
205
206                                         OnCheckStateChanged(EventArgs.Empty);
207                                         Redraw();
208                                 }
209                         }
210                 }
211
212                 [DefaultValue(ContentAlignment.MiddleLeft)]
213                 [Localizable(true)]
214                 public override ContentAlignment TextAlign {
215                         get {
216                                 return text_alignment;
217                         }
218
219                         set {
220                                 if (value != text_alignment) {
221                                         text_alignment = value;
222                                         Redraw();
223                                 }
224                         }
225                 }
226
227
228                 [DefaultValue(false)]
229                 public bool ThreeState {
230                         get {
231                                 return three_state;
232                         }
233
234                         set {
235                                 three_state = value;
236                         }
237                 }
238                 #endregion      // Public Instance Properties
239
240                 #region Protected Instance Properties
241                 protected override CreateParams CreateParams {
242                         get {
243                                 return base.CreateParams;
244                         }
245                 }
246
247                 protected override Size DefaultSize {
248                         get {
249                                 return new Size(104, 24);
250                         }
251                 }
252                 #endregion      // Protected Instance Properties
253
254                 #region Public Instance Methods
255                 public override string ToString() {
256                         return base.ToString() + ", CheckState: " + (int)check_state;
257                 }
258                 #endregion      // Public Instance Methods
259
260                 #region Protected Instance Methods
261                 protected override AccessibleObject CreateAccessibilityInstance() {
262                         AccessibleObject        ao;
263
264                         ao = base.CreateAccessibilityInstance ();
265                         ao.role = AccessibleRole.CheckButton;
266
267                         return ao;
268                 }
269
270                 protected virtual void OnAppearanceChanged(EventArgs e) {
271                         if (AppearanceChanged != null) {
272                                 AppearanceChanged(this, e);
273                         }
274                 }
275
276                 protected virtual void OnCheckedChanged(EventArgs e) {
277                         if (CheckedChanged != null) {
278                                 CheckedChanged(this, e);
279                         }
280                 }
281
282                 protected virtual void OnCheckStateChanged(EventArgs e) {
283                         if (CheckStateChanged != null) {
284                                 CheckStateChanged(this, e);
285                         }
286                 }
287
288                 protected override void OnClick(EventArgs e) {
289                         if (auto_check) {
290                                 switch(check_state) {
291                                         case CheckState.Unchecked: {
292                                                 if (three_state) {
293                                                         CheckState = CheckState.Indeterminate;
294                                                 } else {
295                                                         CheckState = CheckState.Checked;
296                                                 }
297                                                 break;
298                                         }
299
300                                         case CheckState.Indeterminate: {
301                                                 CheckState = CheckState.Checked;
302                                                 break;
303                                         }
304
305                                         case CheckState.Checked: {
306                                                 CheckState = CheckState.Unchecked;
307                                                 break;
308                                         }
309                                 }
310                         }
311                         
312                         base.OnClick (e);
313                 }
314
315                 protected override void OnHandleCreated(EventArgs e) {
316                         base.OnHandleCreated (e);
317                 }
318
319                 protected override void OnMouseUp(MouseEventArgs mevent) {
320                         base.OnMouseUp (mevent);
321                 }
322
323                 protected override bool ProcessMnemonic(char charCode) {
324                         if (IsMnemonic(charCode, Text) == true) {
325                                 Select();
326                                 OnClick(EventArgs.Empty);
327                                 return true;
328                         }
329                         
330                         return base.ProcessMnemonic(charCode);
331                 }
332                 #endregion      // Protected Instance Methods
333
334                 #region Events
335                 public event EventHandler       AppearanceChanged;
336                 public event EventHandler       CheckedChanged;
337                 public event EventHandler       CheckStateChanged;
338                 #endregion      // Events
339
340                 #region Events
341                 [Browsable(false)]
342                 [EditorBrowsable (EditorBrowsableState.Never)]
343                 public event EventHandler DoubleClick;
344                 #endregion      // Events
345         }
346 }