Merge pull request #1695 from gregoryyoung/master
[mono.git] / mcs / class / System.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 using System.ComponentModel;
27 using System.Drawing;
28 using System.Drawing.Text;
29 using System.Runtime.InteropServices;
30
31 namespace System.Windows.Forms {
32         [DefaultProperty("Checked")]
33         [DefaultEvent("CheckedChanged")]
34         [ClassInterface (ClassInterfaceType.AutoDispatch)]
35         [ComVisible (true)]
36         [DefaultBindingProperty ("Checked")]
37         [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts.AssemblySystem_Design)]
38         [Designer ("System.Windows.Forms.Design.RadioButtonDesigner, " + Consts.AssemblySystem_Design)]
39         public class RadioButton : ButtonBase {
40                 #region Local Variables
41                 internal Appearance             appearance;
42                 internal bool                   auto_check;
43                 internal ContentAlignment       radiobutton_alignment;
44                 internal CheckState             check_state;
45                 #endregion      // Local Variables
46
47                 #region RadioButtonAccessibleObject Subclass
48                 [ComVisible(true)]
49                 public class RadioButtonAccessibleObject : ButtonBaseAccessibleObject {
50                         #region RadioButtonAccessibleObject Local Variables
51                         private new RadioButton owner;
52                         #endregion      // RadioButtonAccessibleObject Local Variables
53
54                         #region RadioButtonAccessibleObject Constructors
55                         public RadioButtonAccessibleObject(RadioButton owner) : base(owner) {
56                                 this.owner = owner;
57                         }
58                         #endregion      // RadioButtonAccessibleObject Constructors
59
60                         #region RadioButtonAccessibleObject Properties
61                         public override string DefaultAction {
62                                 get {
63                                         return "Select";
64                                 }
65                         }
66
67                         public override AccessibleRole Role {
68                                 get {
69                                         return AccessibleRole.RadioButton;
70                                 }
71                         }
72
73                         public override AccessibleStates State {
74                                 get {
75                                         AccessibleStates        retval;
76
77                                         retval = AccessibleStates.Default;
78
79                                         if (owner.check_state == CheckState.Checked) {
80                                                 retval |= AccessibleStates.Checked;
81                                         }
82
83                                         if (owner.Focused) {
84                                                 retval |= AccessibleStates.Focused;
85                                         }
86
87                                         if (owner.CanFocus) {
88                                                 retval |= AccessibleStates.Focusable;
89                                         }
90
91                                         return retval;
92                                 }
93                         }
94                         #endregion      // RadioButtonAccessibleObject Properties
95
96                         #region RadioButtonAccessibleObject Methods
97                         public override void DoDefaultAction() {
98                                 owner.PerformClick();
99                         }
100                         #endregion      // RadioButtonAccessibleObject Methods
101                 }
102                 #endregion      // RadioButtonAccessibleObject Sub-class
103
104                 #region Public Constructors
105                 public RadioButton() {
106                         appearance = Appearance.Normal;
107                         auto_check = true;
108                         radiobutton_alignment = ContentAlignment.MiddleLeft;
109                         TextAlign = ContentAlignment.MiddleLeft;
110                         TabStop = false;
111                 }
112                 #endregion      // Public Constructors
113
114                 #region Private Methods
115
116                 // When getting OnEnter we need to set Checked as true in case none of the sibling radio
117                 // buttons is checked.
118                 private void PerformDefaultCheck ()
119                 {
120                         // if we are already checked, no need to check the other controls
121                         if (!auto_check || Checked)
122                                 return;
123
124                         bool is_any_selected = false;
125                         Control c = Parent;
126                         if (c != null) {
127                                 for (int i = 0; i < c.Controls.Count; i++) {
128                                         RadioButton rb = c.Controls [i] as RadioButton;
129                                         if (rb == null || !rb.auto_check)
130                                                 continue;
131
132                                         if (rb.check_state == CheckState.Checked) {
133                                                 is_any_selected = true;
134                                                 break;
135                                         }
136                                 }
137                         }
138
139                         if (!is_any_selected)
140                                 Checked = true;
141                 }
142
143                 private void UpdateSiblings() {
144                         Control c;
145
146                         if (auto_check == false) {
147                                 return;
148                         }
149
150                         // Remove tabstop property from and uncheck our radio-button siblings
151                         c = this.Parent;
152                         if (c != null) {
153                                 for (int i = 0; i < c.Controls.Count; i++) {
154                                         if ((this != c.Controls[i]) && (c.Controls[i] is RadioButton)) {
155                                                 if (((RadioButton)(c.Controls[i])).auto_check) {
156                                                         c.Controls[i].TabStop = false;
157                                                         ((RadioButton)(c.Controls[i])).Checked = false;
158                                                 }
159                                         }
160                                 }
161                         }
162
163                         this.TabStop = true;
164                 }
165
166                 internal override void Draw (PaintEventArgs pe) {
167                         // FIXME: This should be called every time something that can affect it
168                         // is changed, not every paint.  Can only change so many things at a time.
169
170                         // Figure out where our text and image should go
171                         Rectangle glyph_rectangle;
172                         Rectangle text_rectangle;
173                         Rectangle image_rectangle;
174
175                         ThemeEngine.Current.CalculateRadioButtonTextAndImageLayout (this, Point.Empty, out glyph_rectangle, out text_rectangle, out image_rectangle);
176
177                         // Draw our button
178                         if (FlatStyle != FlatStyle.System && Appearance != Appearance.Button)
179                                 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this, glyph_rectangle, text_rectangle, image_rectangle, pe.ClipRectangle);
180                         else
181                                 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
182                 }
183
184                 internal override Size GetPreferredSizeCore (Size proposedSize)
185                 {
186                         if (this.AutoSize)
187                                 return ThemeEngine.Current.CalculateRadioButtonAutoSize (this);
188
189                         return base.GetPreferredSizeCore (proposedSize);
190                 }
191                 #endregion      // Private Methods
192
193                 #region Public Instance Properties
194                 [DefaultValue(Appearance.Normal)]
195                 [Localizable(true)]
196                 public Appearance Appearance {
197                         get {
198                                 return appearance;
199                         }
200
201                         set {
202                                 if (value != appearance) {
203                                         appearance = value;
204                                         EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
205                                         if (eh != null)
206                                                 eh (this, EventArgs.Empty);
207                                         if (Parent != null)
208                                                 Parent.PerformLayout (this, "Appearance");
209                                         Invalidate();
210                                 }
211                         }
212                 }
213
214                 [DefaultValue(true)]
215                 public bool AutoCheck {
216                         get {
217                                 return auto_check;
218                         }
219
220                         set {
221                                 auto_check = value;
222                         }
223                 }
224
225                 [Localizable(true)]
226                 [DefaultValue(ContentAlignment.MiddleLeft)]
227                 public ContentAlignment CheckAlign {
228                         get {
229                                 return radiobutton_alignment;
230                         }
231
232                         set {
233                                 if (value != radiobutton_alignment) {
234                                         radiobutton_alignment = value;
235
236                                         Invalidate();
237                                 }
238                         }
239                 }
240
241                 [DefaultValue(false)]
242                 [SettingsBindable (true)]
243                 [Bindable (true, BindingDirection.OneWay)]
244                 public bool Checked {
245                         get {
246                                 if (check_state != CheckState.Unchecked) {
247                                         return true;
248                                 }
249                                 return false;
250                         }
251
252                         set {
253                                 if (value && (check_state != CheckState.Checked)) {
254                                         check_state = CheckState.Checked;
255                                         Invalidate();
256                                         UpdateSiblings();
257                                         OnCheckedChanged(EventArgs.Empty);
258                                 } else if (!value && (check_state != CheckState.Unchecked)) {
259                                         TabStop = false;
260                                         check_state = CheckState.Unchecked;
261                                         Invalidate();
262                                         OnCheckedChanged(EventArgs.Empty);
263                                 }
264                         }
265                 }
266
267                 [DefaultValue(false)]
268                 public new bool TabStop {
269                         get { return base.TabStop; }
270                         set { base.TabStop = value; }
271                 }
272
273                 [DefaultValue(ContentAlignment.MiddleLeft)]
274                 [Localizable(true)]
275                 public override ContentAlignment TextAlign {
276                         get { return base.TextAlign; }
277                         set { base.TextAlign = value; }
278                 }
279                 #endregion      // Public Instance Properties
280
281                 #region Protected Instance Properties
282                 protected override CreateParams CreateParams {
283                         get {
284                                 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
285                                 SetStyle(ControlStyles.UserPaint, true);
286
287                                 return base.CreateParams;
288                         }
289                 }
290
291                 protected override Size DefaultSize {
292                         get {
293                                 return ThemeEngine.Current.RadioButtonDefaultSize;
294                         }
295                 }
296                 #endregion      // Protected Instance Properties
297
298                 #region Public Instance Methods
299                 public void PerformClick() {
300                         OnClick(EventArgs.Empty);
301                 }
302
303                 public override string ToString() {
304                         return base.ToString() + ", Checked: " + this.Checked;
305                 }
306                 #endregion      // Public Instance Methods
307
308                 #region Protected Instance Methods
309                 protected override AccessibleObject CreateAccessibilityInstance() {
310                         AccessibleObject        ao;
311
312                         ao = base.CreateAccessibilityInstance ();
313                         ao.role = AccessibleRole.RadioButton;
314
315                         return ao;
316                 }
317
318                 protected virtual void OnCheckedChanged(EventArgs e) {
319                         EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
320                         if (eh != null)
321                                 eh (this, e);
322                 }
323
324                 protected override void OnClick(EventArgs e) {
325                         if (auto_check) {
326                                 if (!Checked) {
327                                         Checked = true;
328                                 }
329                         } else {
330                                 Checked = !Checked;
331                         }
332                         
333                         base.OnClick (e);
334                 }
335
336                 protected override void OnEnter(EventArgs e) {
337                         PerformDefaultCheck ();
338                         base.OnEnter(e);
339                 }
340
341                 protected override void OnHandleCreated(EventArgs e) {
342                         base.OnHandleCreated(e);
343                 }
344
345                 protected override void OnMouseUp(MouseEventArgs mevent) {
346                         base.OnMouseUp(mevent);
347                 }
348
349                 protected override bool ProcessMnemonic(char charCode) {
350                         if (IsMnemonic(charCode, Text) == true) {
351                                 Select();
352                                 PerformClick();
353                                 return true;
354                         }
355                         
356                         return base.ProcessMnemonic(charCode);
357                 }
358                 #endregion      // Protected Instance Methods
359
360                 #region Events
361                 static object AppearanceChangedEvent = new object ();
362                 static object CheckedChangedEvent = new object ();
363
364                 public event EventHandler AppearanceChanged {
365                         add { Events.AddHandler (AppearanceChangedEvent, value); }
366                         remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
367                 }
368
369                 public event EventHandler CheckedChanged {
370                         add { Events.AddHandler (CheckedChangedEvent, value); }
371                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
372                 }
373
374                 [Browsable(false)]
375                 [EditorBrowsable (EditorBrowsableState.Never)]
376                 public new event EventHandler DoubleClick {
377                         add { base.DoubleClick += value; }
378                         remove { base.DoubleClick -= value; }
379                 }
380                 
381                 [Browsable (false)]
382                 [EditorBrowsable (EditorBrowsableState.Never)]
383                 public new event MouseEventHandler MouseDoubleClick { 
384                         add { base.MouseDoubleClick += value; }
385                         remove { base.MouseDoubleClick -= value; }
386                 }
387                 #endregion      // Events
388         }
389 }