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
26 using System
.ComponentModel
;
28 using System
.Drawing
.Text
;
29 using System
.Runtime
.InteropServices
;
31 namespace System
.Windows
.Forms
{
32 [DefaultProperty("Checked")]
33 [DefaultEvent("CheckedChanged")]
35 [ClassInterface (ClassInterfaceType
.AutoDispatch
)]
37 [DefaultBindingProperty ("Checked")]
38 [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts
.AssemblySystem_Design
)]
39 [Designer ("System.Windows.Forms.Design.RadioButtonDesigner, " + Consts
.AssemblySystem_Design
)]
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
49 #region RadioButtonAccessibleObject Subclass
51 public class RadioButtonAccessibleObject
: ControlAccessibleObject
{
52 #region RadioButtonAccessibleObject Local Variables
53 private new RadioButton owner
;
54 #endregion // RadioButtonAccessibleObject Local Variables
56 #region RadioButtonAccessibleObject Constructors
57 public RadioButtonAccessibleObject(RadioButton owner
) : base(owner
) {
60 #endregion // RadioButtonAccessibleObject Constructors
62 #region RadioButtonAccessibleObject Properties
63 public override string DefaultAction
{
69 public override AccessibleRole Role
{
71 return AccessibleRole
.RadioButton
;
75 public override AccessibleStates State
{
77 AccessibleStates retval
;
79 retval
= AccessibleStates
.Default
;
81 if (owner
.check_state
== CheckState
.Checked
) {
82 retval
|= AccessibleStates
.Checked
;
86 retval
|= AccessibleStates
.Focused
;
90 retval
|= AccessibleStates
.Focusable
;
96 #endregion // RadioButtonAccessibleObject Properties
98 #region RadioButtonAccessibleObject Methods
99 public override void DoDefaultAction() {
100 owner
.PerformClick();
102 #endregion // RadioButtonAccessibleObject Methods
104 #endregion // RadioButtonAccessibleObject Sub-class
106 #region Public Constructors
107 public RadioButton() {
108 appearance
= Appearance
.Normal
;
110 radiobutton_alignment
= ContentAlignment
.MiddleLeft
;
111 text_alignment
= ContentAlignment
.MiddleLeft
;
114 #endregion // Public Constructors
116 #region Private Methods
117 private void UpdateSiblings() {
120 if (auto_check
== false) {
124 // Remove tabstop property from and uncheck our radio-button siblings
127 for (int i
= 0; i
< c
.Controls
.Count
; i
++) {
128 if ((this != c
.Controls
[i
]) && (c
.Controls
[i
] is RadioButton
)) {
129 if (((RadioButton
)(c
.Controls
[i
])).auto_check
) {
130 c
.Controls
[i
].TabStop
= false;
131 ((RadioButton
)(c
.Controls
[i
])).Checked
= false;
140 internal override void Draw (PaintEventArgs pe
) {
141 ThemeEngine
.Current
.DrawRadioButton (pe
.Graphics
, this.ClientRectangle
, this);
143 #endregion // Private Methods
145 #region Public Instance Properties
146 [DefaultValue(Appearance
.Normal
)]
148 public Appearance Appearance
{
154 if (value != appearance
) {
156 EventHandler eh
= (EventHandler
)(Events
[AppearanceChangedEvent
]);
158 eh (this, EventArgs
.Empty
);
165 public bool AutoCheck
{
179 [DefaultValue(ContentAlignment
.MiddleLeft
)]
180 public ContentAlignment CheckAlign
{
182 return radiobutton_alignment
;
186 if (value != radiobutton_alignment
) {
187 radiobutton_alignment
= value;
194 [DefaultValue(false)]
196 [SettingsBindable (true)]
197 [Bindable (true, BindingDirection
.OneWay
)]
199 public bool Checked
{
201 if (check_state
!= CheckState
.Unchecked
) {
208 if (value && (check_state
!= CheckState
.Checked
)) {
210 check_state
= CheckState
.Checked
;
212 OnCheckedChanged(EventArgs
.Empty
);
213 } else if (!value && (check_state
!= CheckState
.Unchecked
)) {
214 check_state
= CheckState
.Unchecked
;
216 OnCheckedChanged(EventArgs
.Empty
);
221 [DefaultValue(false)]
222 public new bool TabStop
{
223 get { return base.TabStop; }
224 set { base.TabStop = value; }
227 [DefaultValue(ContentAlignment
.MiddleLeft
)]
229 public override ContentAlignment TextAlign
{
231 return text_alignment
;
235 if (value != text_alignment
) {
236 text_alignment
= value;
241 #endregion // Public Instance Properties
243 #region Protected Instance Properties
244 protected override CreateParams CreateParams
{
246 SetStyle(ControlStyles
.AllPaintingInWmPaint
, true);
247 SetStyle(ControlStyles
.UserPaint
, true);
249 return base.CreateParams
;
253 protected override Size DefaultSize
{
255 return ThemeEngine
.Current
.RadioButtonDefaultSize
;
258 #endregion // Protected Instance Properties
260 #region Public Instance Methods
261 public void PerformClick() {
262 OnClick(EventArgs
.Empty
);
265 public override string ToString() {
266 return base.ToString() + ", Checked: " + this.Checked
;
268 #endregion // Public Instance Methods
270 #region Protected Instance Methods
271 protected override AccessibleObject
CreateAccessibilityInstance() {
274 ao
= base.CreateAccessibilityInstance ();
275 ao
.role
= AccessibleRole
.RadioButton
;
280 protected virtual void OnCheckedChanged(EventArgs e
) {
281 EventHandler eh
= (EventHandler
)(Events
[CheckedChangedEvent
]);
286 protected override void OnClick(EventArgs e
) {
298 protected override void OnEnter(EventArgs e
) {
302 protected override void OnHandleCreated(EventArgs e
) {
303 base.OnHandleCreated(e
);
306 protected override void OnMouseUp(MouseEventArgs mevent
) {
307 base.OnMouseUp(mevent
);
310 protected override bool ProcessMnemonic(char charCode
) {
311 if (IsMnemonic(charCode
, Text
) == true) {
317 return base.ProcessMnemonic(charCode
);
319 #endregion // Protected Instance Methods
322 static object AppearanceChangedEvent
= new object ();
323 static object CheckedChangedEvent
= new object ();
325 public event EventHandler AppearanceChanged
{
326 add { Events.AddHandler (AppearanceChangedEvent, value); }
327 remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
330 public event EventHandler CheckedChanged
{
331 add { Events.AddHandler (CheckedChangedEvent, value); }
332 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
336 [EditorBrowsable (EditorBrowsableState
.Never
)]
337 public new event EventHandler DoubleClick
{
338 add { base.DoubleClick += value; }
339 remove { base.DoubleClick -= value; }
344 [EditorBrowsable (EditorBrowsableState
.Never
)]
345 public new event MouseEventHandler MouseDoubleClick
{
346 add { base.MouseDoubleClick += value; }
347 remove { base.MouseDoubleClick -= value; }