- Fix from John BouAntoun: Now properly sets the Appearance property
[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 // $Log: CheckBox.cs,v $
28 // Revision 1.8  2004/10/13 02:46:22  pbartok
29 // - Fix from John BouAntoun: Now properly sets the Appearance property
30 //
31 // Revision 1.7  2004/09/28 18:44:25  pbartok
32 // - Streamlined Theme interfaces:
33 //   * Each DrawXXX method for a control now is passed the object for the
34 //     control to be drawn in order to allow accessing any state the theme
35 //     might require
36 //
37 //   * ControlPaint methods for the theme now have a CP prefix to avoid
38 //     name clashes with the Draw methods for controls
39 //
40 //   * Every control now retrieves it's DefaultSize from the current theme
41 //
42 // Revision 1.6  2004/09/01 20:01:24  pbartok
43 // - Added missing default
44 // - Added missing region mark
45 //
46 // Revision 1.5  2004/09/01 01:55:58  pbartok
47 // - Fixed to match the removal of the needs_redraw concept
48 //
49 // Revision 1.4  2004/08/31 18:48:31  pbartok
50 // - Finished (famous last words)
51 //
52 // Revision 1.3  2004/08/30 20:42:26  pbartok
53 // - Implemented CheckBox drawing code
54 //
55 // Revision 1.2  2004/08/30 15:44:20  pbartok
56 // - Updated to fix broken build. Not complete yet.
57 //
58 // Revision 1.1  2004/07/09 05:21:25  pbartok
59 // - Initial check-in
60 //
61 //
62
63 // COMPLETE
64
65 using System;
66 using System.Drawing;
67
68 namespace System.Windows.Forms {
69         public class CheckBox : ButtonBase {
70                 #region Local Variables
71                 internal Appearance             appearance;
72                 internal bool                   auto_check;
73                 internal ContentAlignment       check_alignment;
74                 internal ContentAlignment       text_alignment;
75                 internal CheckState             check_state;
76                 internal bool                   three_state;
77                 #endregion      // Local Variables
78
79
80                 #region Public Constructors
81                 public CheckBox() {
82                         appearance = Appearance.Normal;
83                         auto_check = true;
84                         check_alignment = ContentAlignment.MiddleLeft;
85                         text_alignment = ContentAlignment.MiddleLeft;
86                 }
87                 #endregion      // Public Constructors
88
89                 #region Public Instance Properties
90                 public Appearance Appearance {
91                         get {
92                                 return appearance;
93                         }
94
95                         set {
96                                 if (value != appearance) {
97                                         appearance = value;
98                                         if (AppearanceChanged != null) {
99                                                 AppearanceChanged(this, EventArgs.Empty);
100                                         }
101                                         Redraw();
102                                 }
103                         }
104                 }
105
106                 public bool AutoCheck {
107                         get {
108                                 return auto_check;
109                         }
110
111                         set {
112                                 auto_check = value;
113                         }
114                 }
115
116                 public ContentAlignment CheckAlign {
117                         get {
118                                 return check_alignment;
119                         }
120
121                         set {
122                                 if (value != check_alignment) {
123                                         check_alignment = value;
124
125                                         Redraw();
126                                 }
127                         }
128                 }
129
130                 public bool Checked {
131                         get {
132                                 if (check_state != CheckState.Unchecked) {
133                                         return true;
134                                 }
135                                 return false;
136                         }
137
138                         set {
139                                 if (value && (check_state != CheckState.Checked)) {
140                                         check_state = CheckState.Checked;
141                                         Redraw();
142                                         OnCheckedChanged(EventArgs.Empty);
143                                 } else if (!value && (check_state != CheckState.Unchecked)) {
144                                         check_state = CheckState.Unchecked;
145                                         Redraw();
146                                         OnCheckedChanged(EventArgs.Empty);
147                                 }
148                         }
149                 }
150
151                 public CheckState CheckState {
152                         get {
153                                 return check_state;
154                         }
155
156                         set {
157                                 if (value != check_state) {
158                                         bool    was_checked = (check_state != CheckState.Unchecked);
159
160                                         check_state = value;
161
162                                         if (was_checked != (check_state != CheckState.Unchecked)) {
163                                                 OnCheckedChanged(EventArgs.Empty);
164                                         }
165
166                                         OnCheckStateChanged(EventArgs.Empty);
167                                         Redraw();
168                                 }
169                         }
170                 }
171
172                 public override ContentAlignment TextAlign {
173                         get {
174                                 return text_alignment;
175                         }
176
177                         set {
178                                 if (value != text_alignment) {
179                                         text_alignment = value;
180                                         Redraw();
181                                 }
182                         }
183                 }
184
185
186                 public bool ThreeState {
187                         get {
188                                 return three_state;
189                         }
190
191                         set {
192                                 three_state = value;
193                         }
194                 }
195                 #endregion      // Public Instance Properties
196
197                 #region Protected Instance Properties
198                 protected override CreateParams CreateParams {
199                         get {
200                                 return base.CreateParams;
201                         }
202                 }
203
204                 protected override Size DefaultSize {
205                         get {
206                                 return new Size(104, 24);
207                         }
208                 }
209                 #endregion      // Protected Instance Properties
210
211                 #region Public Instance Methods
212                 public override string ToString() {
213                         return base.ToString() + ", CheckState: " + (int)check_state;
214                 }
215                 #endregion      // Public Instance Methods
216
217                 #region Protected Instance Methods
218                 protected override AccessibleObject CreateAccessibilityInstance() {
219                         return base.CreateAccessibilityInstance ();
220                 }
221
222                 protected virtual void OnAppearanceChanged(EventArgs e) {
223                         if (AppearanceChanged != null) {
224                                 AppearanceChanged(this, e);
225                         }
226                 }
227
228                 protected virtual void OnCheckedChanged(EventArgs e) {
229                         if (CheckedChanged != null) {
230                                 CheckedChanged(this, e);
231                         }
232                 }
233
234                 protected virtual void OnCheckStateChanged(EventArgs e) {
235                         if (CheckStateChanged != null) {
236                                 CheckStateChanged(this, e);
237                         }
238                 }
239
240                 protected override void OnClick(EventArgs e) {
241                         if (auto_check) {
242                                 switch(check_state) {
243                                         case CheckState.Unchecked: {
244                                                 if (three_state) {
245                                                         CheckState = CheckState.Indeterminate;
246                                                 } else {
247                                                         CheckState = CheckState.Checked;
248                                                 }
249                                                 break;
250                                         }
251
252                                         case CheckState.Indeterminate: {
253                                                 CheckState = CheckState.Checked;
254                                                 break;
255                                         }
256
257                                         case CheckState.Checked: {
258                                                 CheckState = CheckState.Unchecked;
259                                                 break;
260                                         }
261                                 }
262                         }
263                 }
264
265                 protected override void OnHandleCreated(EventArgs e) {
266                         base.OnHandleCreated (e);
267                 }
268
269                 protected override void OnMouseUp(MouseEventArgs e) {
270                         base.OnMouseUp (e);
271                 }
272
273                 protected override bool ProcessMnemonic(char charCode) {
274                         return base.ProcessMnemonic (charCode);
275                 }
276                 #endregion      // Protected Instance Methods
277
278                 #region Events
279                 public event EventHandler       AppearanceChanged;
280                 public event EventHandler       CheckedChanged;
281                 public event EventHandler       CheckStateChanged;
282                 #endregion      // Events
283
284                 #region Internal drawing code
285                 internal override void Redraw() {\r
286                         ThemeEngine.Current.DrawCheckBox(this.DeviceContext, this.ClientRectangle, this);
287                         Refresh();
288                 }\r
289                 #endregion      // Internal drawing code
290         }
291 }