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