also, disable the call do BindColumns in
[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                                         OnAppearanceChanged (EventArgs.Empty);
129                                         Redraw();
130                                 }
131                         }
132                 }
133
134                 [DefaultValue(true)]
135                 public bool AutoCheck {
136                         get {
137                                 return auto_check;
138                         }
139
140                         set {
141                                 auto_check = value;
142                         }
143                 }
144
145                 [Bindable(true)]
146                 [Localizable(true)]
147                 [DefaultValue(ContentAlignment.MiddleLeft)]
148                 public ContentAlignment CheckAlign {
149                         get {
150                                 return check_alignment;
151                         }
152
153                         set {
154                                 if (value != check_alignment) {
155                                         check_alignment = value;
156
157                                         Redraw();
158                                 }
159                         }
160                 }
161
162                 [Bindable(true)]
163                 [RefreshProperties(RefreshProperties.All)]
164                 [DefaultValue(false)]
165                 public bool Checked {
166                         get {
167                                 if (check_state != CheckState.Unchecked) {
168                                         return true;
169                                 }
170                                 return false;
171                         }
172
173                         set {
174                                 if (value && (check_state != CheckState.Checked)) {
175                                         check_state = CheckState.Checked;
176                                         Redraw();
177                                         OnCheckedChanged(EventArgs.Empty);
178                                 } else if (!value && (check_state != CheckState.Unchecked)) {
179                                         check_state = CheckState.Unchecked;
180                                         Redraw();
181                                         OnCheckedChanged(EventArgs.Empty);
182                                 }
183                         }
184                 }
185
186                 [DefaultValue(CheckState.Unchecked)]
187                 [RefreshProperties(RefreshProperties.All)]
188                 [Bindable(true)]
189                 public CheckState CheckState {
190                         get {
191                                 return check_state;
192                         }
193
194                         set {
195                                 if (value != check_state) {
196                                         bool    was_checked = (check_state != CheckState.Unchecked);
197
198                                         check_state = value;
199
200                                         if (was_checked != (check_state != CheckState.Unchecked)) {
201                                                 OnCheckedChanged(EventArgs.Empty);
202                                         }
203
204                                         OnCheckStateChanged(EventArgs.Empty);
205                                         Redraw();
206                                 }
207                         }
208                 }
209
210                 [DefaultValue(ContentAlignment.MiddleLeft)]
211                 [Localizable(true)]
212                 public override ContentAlignment TextAlign {
213                         get {
214                                 return text_alignment;
215                         }
216
217                         set {
218                                 if (value != text_alignment) {
219                                         text_alignment = value;
220                                         Redraw();
221                                 }
222                         }
223                 }
224
225
226                 [DefaultValue(false)]
227                 public bool ThreeState {
228                         get {
229                                 return three_state;
230                         }
231
232                         set {
233                                 three_state = value;
234                         }
235                 }
236                 #endregion      // Public Instance Properties
237
238                 #region Protected Instance Properties
239                 protected override CreateParams CreateParams {
240                         get {
241                                 return base.CreateParams;
242                         }
243                 }
244
245                 protected override Size DefaultSize {
246                         get {
247                                 return new Size(104, 24);
248                         }
249                 }
250                 #endregion      // Protected Instance Properties
251
252                 #region Public Instance Methods
253                 public override string ToString() {
254                         return base.ToString() + ", CheckState: " + (int)check_state;
255                 }
256                 #endregion      // Public Instance Methods
257
258                 #region Protected Instance Methods
259                 protected override AccessibleObject CreateAccessibilityInstance() {
260                         AccessibleObject        ao;
261
262                         ao = base.CreateAccessibilityInstance ();
263                         ao.role = AccessibleRole.CheckButton;
264
265                         return ao;
266                 }
267
268                 protected virtual void OnAppearanceChanged(EventArgs e) {
269                         EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
270                         if (eh != null)
271                                 eh (this, e);
272                 }
273
274                 protected virtual void OnCheckedChanged(EventArgs e) {
275                         EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
276                         if (eh != null)
277                                 eh (this, e);
278                 }
279
280                 protected virtual void OnCheckStateChanged(EventArgs e) {
281                         EventHandler eh = (EventHandler)(Events [CheckStateChangedEvent]);
282                         if (eh != null)
283                                 eh (this, e);
284                 }
285
286                 protected override void OnClick(EventArgs e) {
287                         if (auto_check) {
288                                 switch(check_state) {
289                                         case CheckState.Unchecked: {
290                                                 if (three_state) {
291                                                         CheckState = CheckState.Indeterminate;
292                                                 } else {
293                                                         CheckState = CheckState.Checked;
294                                                 }
295                                                 break;
296                                         }
297
298                                         case CheckState.Indeterminate: {
299                                                 CheckState = CheckState.Checked;
300                                                 break;
301                                         }
302
303                                         case CheckState.Checked: {
304                                                 CheckState = CheckState.Unchecked;
305                                                 break;
306                                         }
307                                 }
308                         }
309                         
310                         base.OnClick (e);
311                 }
312
313                 protected override void OnHandleCreated(EventArgs e) {
314                         base.OnHandleCreated (e);
315                 }
316
317                 protected override void OnMouseUp(MouseEventArgs mevent) {
318                         base.OnMouseUp (mevent);
319                 }
320
321                 protected override bool ProcessMnemonic(char charCode) {
322                         if (IsMnemonic(charCode, Text) == true) {
323                                 Select();
324                                 OnClick(EventArgs.Empty);
325                                 return true;
326                         }
327                         
328                         return base.ProcessMnemonic(charCode);
329                 }
330                 #endregion      // Protected Instance Methods
331
332                 #region Events
333                 static object AppearanceChangedEvent = new object ();
334                 static object CheckedChangedEvent = new object ();
335                 static object CheckStateChangedEvent = new object ();
336
337                 public event EventHandler AppearanceChanged {
338                         add { Events.AddHandler (AppearanceChangedEvent, value); }
339                         remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
340                 }
341
342                 public event EventHandler CheckedChanged {
343                         add { Events.AddHandler (CheckedChangedEvent, value); }
344                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
345                 }
346
347                 public event EventHandler CheckStateChanged {
348                         add { Events.AddHandler (CheckStateChangedEvent, value); }
349                         remove { Events.RemoveHandler (CheckStateChangedEvent, value); }
350                 }
351                 #endregion      // Events
352
353                 #region Events
354                 // XXX have a look at this and determine if it
355                 // manipulates base.DoubleClick, and see if
356                 // HaveDoubleClick can just call OnDoubleClick.
357                 [Browsable(false)]
358                 [EditorBrowsable (EditorBrowsableState.Never)]
359                 public new event EventHandler DoubleClick;
360                 #endregion      // Events
361         }
362 }