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