updating to the latest module.
[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                         tab_stop = false;
108                         GotFocus +=new EventHandler(ReceivedFocus);
109                 }
110                 #endregion      // Public Constructors
111
112                 #region Private Methods
113                 private void UpdateSiblings() {
114                         Control c;
115
116                         if (auto_check == false) {
117                                 return;
118                         }
119
120                         // Remove tabstop property from and uncheck our radio-button siblings
121                         c = this.parent;
122                         if (c != null) {
123                                 for (int i = 0; i < c.child_controls.Count; i++) {
124                                         if ((this != c.child_controls[i]) && (c.child_controls[i] is RadioButton)) {
125                                                 if (((RadioButton)(c.child_controls[i])).auto_check) {
126                                                         c.child_controls[i].TabStop = false;
127                                                         ((RadioButton)(c.child_controls[i])).Checked = false;
128                                                 }
129                                         }
130                                 }
131                         }
132
133                         this.TabStop = true;
134                 }
135
136                 internal override void HaveDoubleClick() {
137                         if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
138                 }
139
140                 internal override void Draw (PaintEventArgs pe) {
141                         if (redraw) {
142                                 ThemeEngine.Current.DrawRadioButton(this.DeviceContext, this.ClientRectangle, this);
143                                 redraw = false;
144                         }
145                 }
146 \r
147                 private void ReceivedFocus(object sender, EventArgs e) {\r
148                         OnClick(e);\r
149                 }\r
150                 #endregion      // Private Methods
151
152                 #region Public Instance Properties
153                 [DefaultValue(Appearance.Normal)]
154                 [Localizable(true)]
155                 public Appearance Appearance {
156                         get {
157                                 return appearance;
158                         }
159
160                         set {
161                                 if (value != appearance) {
162                                         appearance = value;
163                                         if (AppearanceChanged != null) {
164                                                 AppearanceChanged(this, EventArgs.Empty);
165                                         }
166                                         Redraw();
167                                 }
168                         }
169                 }
170
171                 [DefaultValue(true)]
172                 public bool AutoCheck {
173                         get {
174                                 return auto_check;
175                         }
176
177                         set {
178                                 auto_check = value;
179                         }
180                 }
181
182                 [Bindable(true)]
183                 [Localizable(true)]
184                 [DefaultValue(ContentAlignment.MiddleLeft)]
185                 public ContentAlignment CheckAlign {
186                         get {
187                                 return radiobutton_alignment;
188                         }
189
190                         set {
191                                 if (value != radiobutton_alignment) {
192                                         radiobutton_alignment = value;
193
194                                         Redraw();
195                                 }
196                         }
197                 }
198
199                 [DefaultValue(false)]
200                 public bool Checked {
201                         get {
202                                 if (check_state != CheckState.Unchecked) {
203                                         return true;
204                                 }
205                                 return false;
206                         }
207
208                         set {
209                                 if (value && (check_state != CheckState.Checked)) {
210                                         UpdateSiblings();
211                                         check_state = CheckState.Checked;
212                                         Redraw();
213                                         OnCheckedChanged(EventArgs.Empty);
214                                 } else if (!value && (check_state != CheckState.Unchecked)) {
215                                         check_state = CheckState.Unchecked;
216                                         Redraw();
217                                         OnCheckedChanged(EventArgs.Empty);
218                                 }
219                         }
220                 }
221
222                 [DefaultValue(false)]
223                 public new bool TabStop {
224                         get {
225                                 return tab_stop;
226                         }
227
228                         set {
229                                 tab_stop = value;
230                         }
231                 }
232
233                 [DefaultValue(ContentAlignment.MiddleLeft)]
234                 [Localizable(true)]
235                 public override ContentAlignment TextAlign {
236                         get {
237                                 return text_alignment;
238                         }
239
240                         set {
241                                 if (value != text_alignment) {
242                                         text_alignment = value;
243                                         Redraw();
244                                 }
245                         }
246                 }
247                 #endregion      // Public Instance Properties
248
249                 #region Protected Instance Properties
250                 protected override CreateParams CreateParams {
251                         get {
252                                 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
253                                 SetStyle(ControlStyles.UserPaint, true);
254
255                                 return base.CreateParams;
256                         }
257                 }
258
259                 protected override Size DefaultSize {
260                         get {
261                                 return ThemeEngine.Current.RadioButtonDefaultSize;
262                         }
263                 }
264                 #endregion      // Protected Instance Properties
265
266                 #region Public Instance Methods
267                 public void PerformClick() {
268                         OnClick(EventArgs.Empty);
269                 }
270
271                 public override string ToString() {
272                         return base.ToString() + ", Checked: " + this.Checked;
273                 }
274                 #endregion      // Public Instance Methods
275
276                 #region Protected Instance Methods
277                 protected override AccessibleObject CreateAccessibilityInstance() {
278                         return base.CreateAccessibilityInstance ();
279                 }
280
281                 protected virtual void OnCheckedChanged(EventArgs e) {
282                         if (CheckedChanged != null) {
283                                 CheckedChanged(this, e);
284                         }
285                 }
286
287                 protected override void OnClick(EventArgs e) {
288                         if (auto_check) {
289                                 if (!Checked) {
290                                         Checked = true;
291                                 }
292                         } else {
293                                 Checked = !Checked;
294                         }
295                 }
296
297                 protected override void OnEnter(EventArgs e) {
298                         base.OnEnter(e);
299                 }
300
301                 protected override void OnHandleCreated(EventArgs e) {
302                         base.OnHandleCreated(e);
303                 }
304
305                 protected override void OnMouseUp(MouseEventArgs mevent) {
306                         base.OnMouseUp(mevent);
307                 }
308
309                 protected override bool ProcessMnemonic(char charCode) {
310                         if (IsMnemonic(charCode, Text) == true) {
311                                 Select();
312                                 PerformClick();
313                                 return true;
314                         }
315                         
316                         return base.ProcessMnemonic(charCode);
317                 }
318                 #endregion      // Protected Instance Methods
319
320                 #region Events
321                 public event EventHandler       AppearanceChanged;
322                 public event EventHandler       CheckedChanged;
323
324                 [Browsable(false)]
325                 [EditorBrowsable (EditorBrowsableState.Never)]
326                 public event EventHandler       DoubleClick;
327                 #endregion      // Events\r
328         }
329 }