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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
28 using System
.ComponentModel
;
30 using System
.Drawing
.Text
;
31 using System
.Runtime
.InteropServices
;
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
44 #region RadioButtonAccessibleObject Subclass
46 public class RadioButtonAccessibleObject
: ControlAccessibleObject
{
47 #region RadioButtonAccessibleObject Local Variables
48 private RadioButton owner
;
49 #endregion // RadioButtonAccessibleObject Local Variables
51 #region RadioButtonAccessibleObject Constructors
52 public RadioButtonAccessibleObject(RadioButton owner
) : base(owner
) {
55 #endregion // RadioButtonAccessibleObject Constructors
57 #region RadioButtonAccessibleObject Properties
58 public override string DefaultAction
{
64 public override AccessibleRole Role
{
66 return AccessibleRole
.RadioButton
;
70 public override AccessibleStates State
{
72 AccessibleStates retval
;
74 retval
= AccessibleStates
.Default
;
76 if (owner
.check_state
== CheckState
.Checked
) {
77 retval
|= AccessibleStates
.Checked
;
81 retval
|= AccessibleStates
.Focused
;
85 retval
|= AccessibleStates
.Focusable
;
91 #endregion // RadioButtonAccessibleObject Properties
93 #region RadioButtonAccessibleObject Methods
94 public override void DoDefaultAction() {
97 #endregion // RadioButtonAccessibleObject Methods
99 #endregion // RadioButtonAccessibleObject Sub-class
101 #region Public Constructors
102 public RadioButton() {
103 appearance
= Appearance
.Normal
;
105 radiobutton_alignment
= ContentAlignment
.MiddleLeft
;
106 text_alignment
= ContentAlignment
.MiddleLeft
;
109 #endregion // Public Constructors
111 #region Private Methods
112 private void UpdateSiblings() {
115 if (auto_check
== false) {
119 // Remove tabstop property from and uncheck our radio-button siblings
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;
135 internal override void Draw (PaintEventArgs pe
) {
136 ThemeEngine
.Current
.DrawRadioButton (pe
.Graphics
, this.ClientRectangle
, this);
138 #endregion // Private Methods
140 #region Public Instance Properties
141 [DefaultValue(Appearance
.Normal
)]
143 public Appearance Appearance
{
149 if (value != appearance
) {
151 EventHandler eh
= (EventHandler
)(Events
[AppearanceChangedEvent
]);
153 eh (this, EventArgs
.Empty
);
160 public bool AutoCheck
{
172 [DefaultValue(ContentAlignment
.MiddleLeft
)]
173 public ContentAlignment CheckAlign
{
175 return radiobutton_alignment
;
179 if (value != radiobutton_alignment
) {
180 radiobutton_alignment
= value;
187 [DefaultValue(false)]
188 public bool Checked
{
190 if (check_state
!= CheckState
.Unchecked
) {
197 if (value && (check_state
!= CheckState
.Checked
)) {
199 check_state
= CheckState
.Checked
;
201 OnCheckedChanged(EventArgs
.Empty
);
202 } else if (!value && (check_state
!= CheckState
.Unchecked
)) {
203 check_state
= CheckState
.Unchecked
;
205 OnCheckedChanged(EventArgs
.Empty
);
210 [DefaultValue(false)]
211 public new bool TabStop
{
212 get { return base.TabStop; }
213 set { base.TabStop = value; }
216 [DefaultValue(ContentAlignment
.MiddleLeft
)]
218 public override ContentAlignment TextAlign
{
220 return text_alignment
;
224 if (value != text_alignment
) {
225 text_alignment
= value;
230 #endregion // Public Instance Properties
232 #region Protected Instance Properties
233 protected override CreateParams CreateParams
{
235 SetStyle(ControlStyles
.AllPaintingInWmPaint
, true);
236 SetStyle(ControlStyles
.UserPaint
, true);
238 return base.CreateParams
;
242 protected override Size DefaultSize
{
244 return ThemeEngine
.Current
.RadioButtonDefaultSize
;
247 #endregion // Protected Instance Properties
249 #region Public Instance Methods
250 public void PerformClick() {
251 OnClick(EventArgs
.Empty
);
254 public override string ToString() {
255 return base.ToString() + ", Checked: " + this.Checked
;
257 #endregion // Public Instance Methods
259 #region Protected Instance Methods
260 protected override AccessibleObject
CreateAccessibilityInstance() {
263 ao
= base.CreateAccessibilityInstance ();
264 ao
.role
= AccessibleRole
.RadioButton
;
269 protected virtual void OnCheckedChanged(EventArgs e
) {
270 EventHandler eh
= (EventHandler
)(Events
[CheckedChangedEvent
]);
275 protected override void OnClick(EventArgs e
) {
287 protected override void OnEnter(EventArgs e
) {
291 protected override void OnHandleCreated(EventArgs e
) {
292 base.OnHandleCreated(e
);
295 protected override void OnMouseUp(MouseEventArgs mevent
) {
296 base.OnMouseUp(mevent
);
299 protected override bool ProcessMnemonic(char charCode
) {
300 if (IsMnemonic(charCode
, Text
) == true) {
306 return base.ProcessMnemonic(charCode
);
308 #endregion // Protected Instance Methods
311 static object AppearanceChangedEvent
= new object ();
312 static object CheckedChangedEvent
= new object ();
314 public event EventHandler AppearanceChanged
{
315 add { Events.AddHandler (AppearanceChangedEvent, value); }
316 remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
319 public event EventHandler CheckedChanged
{
320 add { Events.AddHandler (CheckedChangedEvent, value); }
321 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
325 [EditorBrowsable (EditorBrowsableState
.Never
)]
326 public new event EventHandler DoubleClick
{
327 add { base.DoubleClick += value; }
328 remove { base.DoubleClick -= value; }