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