* ImageList.cs: When the image stream is set pull all the images
[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 // 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 : ControlAccessibleObject {
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                 }
104                 #endregion      // Public Constructors
105
106                 #region Internal Methods
107                 internal override void Draw (PaintEventArgs pe) {
108                         if (redraw) {
109                                 ThemeEngine.Current.DrawCheckBox (this.DeviceContext, this.ClientRectangle, this);
110                                 redraw = false;
111                         }
112                 }
113
114                 internal override void HaveDoubleClick() {
115                         if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
116                 }
117                 #endregion      // Internal Methods
118
119                 #region Public Instance Properties
120                 [DefaultValue(Appearance.Normal)]
121                 [Localizable(true)]
122                 public Appearance Appearance {
123                         get {
124                                 return appearance;
125                         }
126
127                         set {
128                                 if (value != appearance) {
129                                         appearance = value;
130                                         if (AppearanceChanged != null) {
131                                                 AppearanceChanged(this, EventArgs.Empty);
132                                         }
133                                         Redraw();
134                                 }
135                         }
136                 }
137
138                 [DefaultValue(true)]
139                 public bool AutoCheck {
140                         get {
141                                 return auto_check;
142                         }
143
144                         set {
145                                 auto_check = value;
146                         }
147                 }
148
149                 [Bindable(true)]
150                 [Localizable(true)]
151                 [DefaultValue(ContentAlignment.MiddleLeft)]
152                 public ContentAlignment CheckAlign {
153                         get {
154                                 return check_alignment;
155                         }
156
157                         set {
158                                 if (value != check_alignment) {
159                                         check_alignment = value;
160
161                                         Redraw();
162                                 }
163                         }
164                 }
165
166                 [Bindable(true)]
167                 [RefreshProperties(RefreshProperties.All)]
168                 [DefaultValue(false)]
169                 public bool Checked {
170                         get {
171                                 if (check_state != CheckState.Unchecked) {
172                                         return true;
173                                 }
174                                 return false;
175                         }
176
177                         set {
178                                 if (value && (check_state != CheckState.Checked)) {
179                                         check_state = CheckState.Checked;
180                                         Redraw();
181                                         OnCheckedChanged(EventArgs.Empty);
182                                 } else if (!value && (check_state != CheckState.Unchecked)) {
183                                         check_state = CheckState.Unchecked;
184                                         Redraw();
185                                         OnCheckedChanged(EventArgs.Empty);
186                                 }
187                         }
188                 }
189
190                 [DefaultValue(CheckState.Unchecked)]
191                 [RefreshProperties(RefreshProperties.All)]
192                 [Bindable(true)]
193                 public CheckState CheckState {
194                         get {
195                                 return check_state;
196                         }
197
198                         set {
199                                 if (value != check_state) {
200                                         bool    was_checked = (check_state != CheckState.Unchecked);
201
202                                         check_state = value;
203
204                                         if (was_checked != (check_state != CheckState.Unchecked)) {
205                                                 OnCheckedChanged(EventArgs.Empty);
206                                         }
207
208                                         OnCheckStateChanged(EventArgs.Empty);
209                                         Redraw();
210                                 }
211                         }
212                 }
213
214                 [DefaultValue(ContentAlignment.MiddleLeft)]
215                 [Localizable(true)]
216                 public override ContentAlignment TextAlign {
217                         get {
218                                 return text_alignment;
219                         }
220
221                         set {
222                                 if (value != text_alignment) {
223                                         text_alignment = value;
224                                         Redraw();
225                                 }
226                         }
227                 }
228
229
230                 [DefaultValue(false)]
231                 public bool ThreeState {
232                         get {
233                                 return three_state;
234                         }
235
236                         set {
237                                 three_state = value;
238                         }
239                 }
240                 #endregion      // Public Instance Properties
241
242                 #region Protected Instance Properties
243                 protected override CreateParams CreateParams {
244                         get {
245                                 return base.CreateParams;
246                         }
247                 }
248
249                 protected override Size DefaultSize {
250                         get {
251                                 return new Size(104, 24);
252                         }
253                 }
254                 #endregion      // Protected Instance Properties
255
256                 #region Public Instance Methods
257                 public override string ToString() {
258                         return base.ToString() + ", CheckState: " + (int)check_state;
259                 }
260                 #endregion      // Public Instance Methods
261
262                 #region Protected Instance Methods
263                 protected override AccessibleObject CreateAccessibilityInstance() {
264                         return base.CreateAccessibilityInstance ();
265                 }
266
267                 protected virtual void OnAppearanceChanged(EventArgs e) {
268                         if (AppearanceChanged != null) {
269                                 AppearanceChanged(this, e);
270                         }
271                 }
272
273                 protected virtual void OnCheckedChanged(EventArgs e) {
274                         if (CheckedChanged != null) {
275                                 CheckedChanged(this, e);
276                         }
277                 }
278
279                 protected virtual void OnCheckStateChanged(EventArgs e) {
280                         if (CheckStateChanged != null) {
281                                 CheckStateChanged(this, e);
282                         }
283                 }
284
285                 protected override void OnClick(EventArgs e) {
286                         if (auto_check) {
287                                 switch(check_state) {
288                                         case CheckState.Unchecked: {
289                                                 if (three_state) {
290                                                         CheckState = CheckState.Indeterminate;
291                                                 } else {
292                                                         CheckState = CheckState.Checked;
293                                                 }
294                                                 break;
295                                         }
296
297                                         case CheckState.Indeterminate: {
298                                                 CheckState = CheckState.Checked;
299                                                 break;
300                                         }
301
302                                         case CheckState.Checked: {
303                                                 CheckState = CheckState.Unchecked;
304                                                 break;
305                                         }
306                                 }
307                         }
308                 }
309
310                 protected override void OnHandleCreated(EventArgs e) {
311                         base.OnHandleCreated (e);
312                 }
313
314                 protected override void OnMouseUp(MouseEventArgs e) {
315                         base.OnMouseUp (e);
316                 }
317
318                 protected override bool ProcessMnemonic(char charCode) {
319                         if (IsMnemonic(charCode, Text) == true) {
320                                 Select();
321                                 OnClick(EventArgs.Empty);
322                                 return true;
323                         }
324                         
325                         return base.ProcessMnemonic(charCode);
326                 }
327                 #endregion      // Protected Instance Methods
328
329                 #region Events
330                 public event EventHandler       AppearanceChanged;
331                 public event EventHandler       CheckedChanged;
332                 public event EventHandler       CheckStateChanged;
333                 #endregion      // Events
334
335                 #region Events
336                 [Browsable(false)]
337                 [EditorBrowsable (EditorBrowsableState.Never)]
338                 public event EventHandler DoubleClick;
339                 #endregion      // Events
340         }
341 }