* Application.cs: fix compilation errors when debug is enabled.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RadioButton.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 //      Peter Bartok    pbartok@novell.com
24 //
25
26 // COMPLETE
27
28 using System.ComponentModel;
29 using System.Drawing;
30 using System.Drawing.Text;
31 using System.Runtime.InteropServices;
32
33 namespace System.Windows.Forms {
34         [DefaultProperty("Checked")]
35         [DefaultEvent("CheckedChanged")]
36         public class RadioButton : ButtonBase {
37                 #region Local Variables
38                 internal Appearance             appearance;
39                 internal bool                   auto_check;
40                 internal ContentAlignment       radiobutton_alignment;
41                 internal CheckState             check_state;
42                 #endregion      // Local Variables
43
44                 #region RadioButtonAccessibleObject Subclass
45                 [ComVisible(true)]
46                 public class RadioButtonAccessibleObject : ControlAccessibleObject {
47                         #region RadioButtonAccessibleObject Local Variables
48                         private RadioButton     owner;
49                         #endregion      // RadioButtonAccessibleObject Local Variables
50
51                         #region RadioButtonAccessibleObject Constructors
52                         public RadioButtonAccessibleObject(RadioButton owner) : base(owner) {
53                                 this.owner = owner;
54                         }
55                         #endregion      // RadioButtonAccessibleObject Constructors
56
57                         #region RadioButtonAccessibleObject Properties
58                         public override string DefaultAction {
59                                 get {
60                                         return "Select";
61                                 }
62                         }
63
64                         public override AccessibleRole Role {
65                                 get {
66                                         return AccessibleRole.RadioButton;
67                                 }
68                         }
69
70                         public override AccessibleStates State {
71                                 get {
72                                         AccessibleStates        retval;
73
74                                         retval = AccessibleStates.Default;
75
76                                         if (owner.check_state == CheckState.Checked) {
77                                                 retval |= AccessibleStates.Checked;
78                                         }
79
80                                         if (owner.Focused) {
81                                                 retval |= AccessibleStates.Focused;
82                                         }
83
84                                         if (owner.CanFocus) {
85                                                 retval |= AccessibleStates.Focusable;
86                                         }
87
88                                         return retval;
89                                 }
90                         }
91                         #endregion      // RadioButtonAccessibleObject Properties
92
93                         #region RadioButtonAccessibleObject Methods
94                         public override void DoDefaultAction() {
95                                 owner.PerformClick();
96                         }
97                         #endregion      // RadioButtonAccessibleObject Methods
98                 }
99                 #endregion      // RadioButtonAccessibleObject Sub-class
100
101                 #region Public Constructors
102                 public RadioButton() {
103                         appearance = Appearance.Normal;
104                         auto_check = true;
105                         radiobutton_alignment = ContentAlignment.MiddleLeft;
106                         text_alignment = ContentAlignment.MiddleLeft;
107                         TabStop = false;
108                 }
109                 #endregion      // Public Constructors
110
111                 #region Private Methods
112                 private void UpdateSiblings() {
113                         Control c;
114
115                         if (auto_check == false) {
116                                 return;
117                         }
118
119                         // Remove tabstop property from and uncheck our radio-button siblings
120                         c = this.Parent;
121                         if (c != null) {
122                                 for (int i = 0; i < c.Controls.Count; i++) {
123                                         if ((this != c.Controls[i]) && (c.Controls[i] is RadioButton)) {
124                                                 if (((RadioButton)(c.Controls[i])).auto_check) {
125                                                         c.Controls[i].TabStop = false;
126                                                         ((RadioButton)(c.Controls[i])).Checked = false;
127                                                 }
128                                         }
129                                 }
130                         }
131
132                         this.TabStop = true;
133                 }
134
135                 internal override void Draw (PaintEventArgs pe) {
136                         ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
137                 }
138                 #endregion      // Private Methods
139
140                 #region Public Instance Properties
141                 [DefaultValue(Appearance.Normal)]
142                 [Localizable(true)]
143                 public Appearance Appearance {
144                         get {
145                                 return appearance;
146                         }
147
148                         set {
149                                 if (value != appearance) {
150                                         appearance = value;
151                                         EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
152                                         if (eh != null)
153                                                 eh (this, EventArgs.Empty);
154                                         Redraw();
155                                 }
156                         }
157                 }
158
159                 [DefaultValue(true)]
160                 public bool AutoCheck {
161                         get {
162                                 return auto_check;
163                         }
164
165                         set {
166                                 auto_check = value;
167                         }
168                 }
169
170                 [Bindable(true)]
171                 [Localizable(true)]
172                 [DefaultValue(ContentAlignment.MiddleLeft)]
173                 public ContentAlignment CheckAlign {
174                         get {
175                                 return radiobutton_alignment;
176                         }
177
178                         set {
179                                 if (value != radiobutton_alignment) {
180                                         radiobutton_alignment = value;
181
182                                         Redraw();
183                                 }
184                         }
185                 }
186
187                 [DefaultValue(false)]
188                 public bool Checked {
189                         get {
190                                 if (check_state != CheckState.Unchecked) {
191                                         return true;
192                                 }
193                                 return false;
194                         }
195
196                         set {
197                                 if (value && (check_state != CheckState.Checked)) {
198                                         UpdateSiblings();
199                                         check_state = CheckState.Checked;
200                                         Redraw();
201                                         OnCheckedChanged(EventArgs.Empty);
202                                 } else if (!value && (check_state != CheckState.Unchecked)) {
203                                         check_state = CheckState.Unchecked;
204                                         Redraw();
205                                         OnCheckedChanged(EventArgs.Empty);
206                                 }
207                         }
208                 }
209
210                 [DefaultValue(false)]
211                 public new bool TabStop {
212                         get { return base.TabStop; }
213                         set { base.TabStop = value; }
214                 }
215
216                 [DefaultValue(ContentAlignment.MiddleLeft)]
217                 [Localizable(true)]
218                 public override ContentAlignment TextAlign {
219                         get {
220                                 return text_alignment;
221                         }
222
223                         set {
224                                 if (value != text_alignment) {
225                                         text_alignment = value;
226                                         Redraw();
227                                 }
228                         }
229                 }
230                 #endregion      // Public Instance Properties
231
232                 #region Protected Instance Properties
233                 protected override CreateParams CreateParams {
234                         get {
235                                 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
236                                 SetStyle(ControlStyles.UserPaint, true);
237
238                                 return base.CreateParams;
239                         }
240                 }
241
242                 protected override Size DefaultSize {
243                         get {
244                                 return ThemeEngine.Current.RadioButtonDefaultSize;
245                         }
246                 }
247                 #endregion      // Protected Instance Properties
248
249                 #region Public Instance Methods
250                 public void PerformClick() {
251                         OnClick(EventArgs.Empty);
252                 }
253
254                 public override string ToString() {
255                         return base.ToString() + ", Checked: " + this.Checked;
256                 }
257                 #endregion      // Public Instance Methods
258
259                 #region Protected Instance Methods
260                 protected override AccessibleObject CreateAccessibilityInstance() {
261                         AccessibleObject        ao;
262
263                         ao = base.CreateAccessibilityInstance ();
264                         ao.role = AccessibleRole.RadioButton;
265
266                         return ao;
267                 }
268
269                 protected virtual void OnCheckedChanged(EventArgs e) {
270                         EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
271                         if (eh != null)
272                                 eh (this, e);
273                 }
274
275                 protected override void OnClick(EventArgs e) {
276                         if (auto_check) {
277                                 if (!Checked) {
278                                         Checked = true;
279                                 }
280                         } else {
281                                 Checked = !Checked;
282                         }
283                         
284                         base.OnClick (e);
285                 }
286
287                 protected override void OnEnter(EventArgs e) {
288                         base.OnEnter(e);
289                 }
290
291                 protected override void OnHandleCreated(EventArgs e) {
292                         base.OnHandleCreated(e);
293                 }
294
295                 protected override void OnMouseUp(MouseEventArgs mevent) {
296                         base.OnMouseUp(mevent);
297                 }
298
299                 protected override bool ProcessMnemonic(char charCode) {
300                         if (IsMnemonic(charCode, Text) == true) {
301                                 Select();
302                                 PerformClick();
303                                 return true;
304                         }
305                         
306                         return base.ProcessMnemonic(charCode);
307                 }
308                 #endregion      // Protected Instance Methods
309
310                 #region Events
311                 static object AppearanceChangedEvent = new object ();
312                 static object CheckedChangedEvent = new object ();
313
314                 public event EventHandler AppearanceChanged {
315                         add { Events.AddHandler (AppearanceChangedEvent, value); }
316                         remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
317                 }
318
319                 public event EventHandler CheckedChanged {
320                         add { Events.AddHandler (CheckedChangedEvent, value); }
321                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
322                 }
323
324                 [Browsable(false)]
325                 [EditorBrowsable (EditorBrowsableState.Never)]
326                 public new event EventHandler DoubleClick {
327                         add { base.DoubleClick += value; }
328                         remove { base.DoubleClick -= value; }
329                 }
330                 #endregion      // Events
331         }
332 }