Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[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
166 #if NET_2_0
167                 internal override Size GetPreferredSizeCore (Size proposedSize)
168                 {
169                         if (this.AutoSize)
170                                 return ThemeEngine.Current.CalculateRadioButtonAutoSize (this);
171
172                         return base.GetPreferredSizeCore (proposedSize);
173                 }
174 #endif
175                 #endregion      // Private Methods
176
177                 #region Public Instance Properties
178                 [DefaultValue(Appearance.Normal)]
179                 [Localizable(true)]
180                 public Appearance Appearance {
181                         get {
182                                 return appearance;
183                         }
184
185                         set {
186                                 if (value != appearance) {
187                                         appearance = value;
188                                         EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
189                                         if (eh != null)
190                                                 eh (this, EventArgs.Empty);
191                                         if (Parent != null)
192                                                 Parent.PerformLayout (this, "Appearance");
193                                         Invalidate();
194                                 }
195                         }
196                 }
197
198                 [DefaultValue(true)]
199                 public bool AutoCheck {
200                         get {
201                                 return auto_check;
202                         }
203
204                         set {
205                                 auto_check = value;
206                         }
207                 }
208
209 #if !NET_2_0
210                 [Bindable(true)]
211 #endif
212                 [Localizable(true)]
213                 [DefaultValue(ContentAlignment.MiddleLeft)]
214                 public ContentAlignment CheckAlign {
215                         get {
216                                 return radiobutton_alignment;
217                         }
218
219                         set {
220                                 if (value != radiobutton_alignment) {
221                                         radiobutton_alignment = value;
222
223                                         Invalidate();
224                                 }
225                         }
226                 }
227
228                 [DefaultValue(false)]
229 #if NET_2_0
230                 [SettingsBindable (true)]
231                 [Bindable (true, BindingDirection.OneWay)]
232 #endif
233                 public bool Checked {
234                         get {
235                                 if (check_state != CheckState.Unchecked) {
236                                         return true;
237                                 }
238                                 return false;
239                         }
240
241                         set {
242                                 if (value && (check_state != CheckState.Checked)) {
243                                         UpdateSiblings();
244                                         check_state = CheckState.Checked;
245                                         Invalidate();
246                                         OnCheckedChanged(EventArgs.Empty);
247                                 } else if (!value && (check_state != CheckState.Unchecked)) {
248                                         check_state = CheckState.Unchecked;
249                                         Invalidate();
250                                         OnCheckedChanged(EventArgs.Empty);
251                                 }
252                         }
253                 }
254
255                 [DefaultValue(false)]
256                 public new bool TabStop {
257                         get { return base.TabStop; }
258                         set { base.TabStop = value; }
259                 }
260
261                 [DefaultValue(ContentAlignment.MiddleLeft)]
262                 [Localizable(true)]
263                 public override ContentAlignment TextAlign {
264                         get { return base.TextAlign; }
265                         set { base.TextAlign = value; }
266                 }
267                 #endregion      // Public Instance Properties
268
269                 #region Protected Instance Properties
270                 protected override CreateParams CreateParams {
271                         get {
272                                 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
273                                 SetStyle(ControlStyles.UserPaint, true);
274
275                                 return base.CreateParams;
276                         }
277                 }
278
279                 protected override Size DefaultSize {
280                         get {
281                                 return ThemeEngine.Current.RadioButtonDefaultSize;
282                         }
283                 }
284                 #endregion      // Protected Instance Properties
285
286                 #region Public Instance Methods
287                 public void PerformClick() {
288                         OnClick(EventArgs.Empty);
289                 }
290
291                 public override string ToString() {
292                         return base.ToString() + ", Checked: " + this.Checked;
293                 }
294                 #endregion      // Public Instance Methods
295
296                 #region Protected Instance Methods
297                 protected override AccessibleObject CreateAccessibilityInstance() {
298                         AccessibleObject        ao;
299
300                         ao = base.CreateAccessibilityInstance ();
301                         ao.role = AccessibleRole.RadioButton;
302
303                         return ao;
304                 }
305
306                 protected virtual void OnCheckedChanged(EventArgs e) {
307                         EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
308                         if (eh != null)
309                                 eh (this, e);
310                 }
311
312                 protected override void OnClick(EventArgs e) {
313                         if (auto_check) {
314                                 if (!Checked) {
315                                         Checked = true;
316                                 }
317                         } else {
318                                 Checked = !Checked;
319                         }
320                         
321                         base.OnClick (e);
322                 }
323
324                 protected override void OnEnter(EventArgs e) {
325                         base.OnEnter(e);
326                 }
327
328                 protected override void OnHandleCreated(EventArgs e) {
329                         base.OnHandleCreated(e);
330                 }
331
332                 protected override void OnMouseUp(MouseEventArgs mevent) {
333                         base.OnMouseUp(mevent);
334                 }
335
336                 protected override bool ProcessMnemonic(char charCode) {
337                         if (IsMnemonic(charCode, Text) == true) {
338                                 Select();
339                                 PerformClick();
340                                 return true;
341                         }
342                         
343                         return base.ProcessMnemonic(charCode);
344                 }
345                 #endregion      // Protected Instance Methods
346
347                 #region Events
348                 static object AppearanceChangedEvent = new object ();
349                 static object CheckedChangedEvent = new object ();
350
351                 public event EventHandler AppearanceChanged {
352                         add { Events.AddHandler (AppearanceChangedEvent, value); }
353                         remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
354                 }
355
356                 public event EventHandler CheckedChanged {
357                         add { Events.AddHandler (CheckedChangedEvent, value); }
358                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
359                 }
360
361                 [Browsable(false)]
362                 [EditorBrowsable (EditorBrowsableState.Never)]
363                 public new event EventHandler DoubleClick {
364                         add { base.DoubleClick += value; }
365                         remove { base.DoubleClick -= value; }
366                 }
367                 
368 #if NET_2_0
369                 [Browsable (false)]
370                 [EditorBrowsable (EditorBrowsableState.Never)]
371                 public new event MouseEventHandler MouseDoubleClick { 
372                         add { base.MouseDoubleClick += value; }
373                         remove { base.MouseDoubleClick -= value; }
374                 }
375 #endif
376                 #endregion      // Events
377         }
378 }