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