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")]
34 [ClassInterface (ClassInterfaceType
.AutoDispatch
)]
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
47 #region RadioButtonAccessibleObject Subclass
49 public class RadioButtonAccessibleObject
: ButtonBaseAccessibleObject
{
50 #region RadioButtonAccessibleObject Local Variables
51 private new RadioButton owner
;
52 #endregion // RadioButtonAccessibleObject Local Variables
54 #region RadioButtonAccessibleObject Constructors
55 public RadioButtonAccessibleObject(RadioButton owner
) : base(owner
) {
58 #endregion // RadioButtonAccessibleObject Constructors
60 #region RadioButtonAccessibleObject Properties
61 public override string DefaultAction
{
67 public override AccessibleRole Role
{
69 return AccessibleRole
.RadioButton
;
73 public override AccessibleStates State
{
75 AccessibleStates retval
;
77 retval
= AccessibleStates
.Default
;
79 if (owner
.check_state
== CheckState
.Checked
) {
80 retval
|= AccessibleStates
.Checked
;
84 retval
|= AccessibleStates
.Focused
;
88 retval
|= AccessibleStates
.Focusable
;
94 #endregion // RadioButtonAccessibleObject Properties
96 #region RadioButtonAccessibleObject Methods
97 public override void DoDefaultAction() {
100 #endregion // RadioButtonAccessibleObject Methods
102 #endregion // RadioButtonAccessibleObject Sub-class
104 #region Public Constructors
105 public RadioButton() {
106 appearance
= Appearance
.Normal
;
108 radiobutton_alignment
= ContentAlignment
.MiddleLeft
;
109 TextAlign
= ContentAlignment
.MiddleLeft
;
112 #endregion // Public Constructors
114 #region Private Methods
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 ()
120 // if we are already checked, no need to check the other controls
121 if (!auto_check
|| Checked
)
124 bool is_any_selected
= false;
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
)
132 if (rb
.check_state
== CheckState
.Checked
) {
133 is_any_selected
= true;
139 if (!is_any_selected
)
143 private void UpdateSiblings() {
146 if (auto_check
== false) {
150 // Remove tabstop property from and uncheck our radio-button siblings
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;
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.
170 // Figure out where our text and image should go
171 Rectangle glyph_rectangle
;
172 Rectangle text_rectangle
;
173 Rectangle image_rectangle
;
175 ThemeEngine
.Current
.CalculateRadioButtonTextAndImageLayout (this, Point
.Empty
, out glyph_rectangle
, out text_rectangle
, out image_rectangle
);
178 if (FlatStyle
!= FlatStyle
.System
&& Appearance
!= Appearance
.Button
)
179 ThemeEngine
.Current
.DrawRadioButton (pe
.Graphics
, this, glyph_rectangle
, text_rectangle
, image_rectangle
, pe
.ClipRectangle
);
181 ThemeEngine
.Current
.DrawRadioButton (pe
.Graphics
, this.ClientRectangle
, this);
184 internal override Size
GetPreferredSizeCore (Size proposedSize
)
187 return ThemeEngine
.Current
.CalculateRadioButtonAutoSize (this);
189 return base.GetPreferredSizeCore (proposedSize
);
191 #endregion // Private Methods
193 #region Public Instance Properties
194 [DefaultValue(Appearance
.Normal
)]
196 public Appearance Appearance
{
202 if (value != appearance
) {
204 EventHandler eh
= (EventHandler
)(Events
[AppearanceChangedEvent
]);
206 eh (this, EventArgs
.Empty
);
208 Parent
.PerformLayout (this, "Appearance");
215 public bool AutoCheck
{
226 [DefaultValue(ContentAlignment
.MiddleLeft
)]
227 public ContentAlignment CheckAlign
{
229 return radiobutton_alignment
;
233 if (value != radiobutton_alignment
) {
234 radiobutton_alignment
= value;
241 [DefaultValue(false)]
242 [SettingsBindable (true)]
243 [Bindable (true, BindingDirection
.OneWay
)]
244 public bool Checked
{
246 if (check_state
!= CheckState
.Unchecked
) {
253 if (value && (check_state
!= CheckState
.Checked
)) {
254 check_state
= CheckState
.Checked
;
257 OnCheckedChanged(EventArgs
.Empty
);
258 } else if (!value && (check_state
!= CheckState
.Unchecked
)) {
260 check_state
= CheckState
.Unchecked
;
262 OnCheckedChanged(EventArgs
.Empty
);
267 [DefaultValue(false)]
268 public new bool TabStop
{
269 get { return base.TabStop; }
270 set { base.TabStop = value; }
273 [DefaultValue(ContentAlignment
.MiddleLeft
)]
275 public override ContentAlignment TextAlign
{
276 get { return base.TextAlign; }
277 set { base.TextAlign = value; }
279 #endregion // Public Instance Properties
281 #region Protected Instance Properties
282 protected override CreateParams CreateParams
{
284 SetStyle(ControlStyles
.AllPaintingInWmPaint
, true);
285 SetStyle(ControlStyles
.UserPaint
, true);
287 return base.CreateParams
;
291 protected override Size DefaultSize
{
293 return ThemeEngine
.Current
.RadioButtonDefaultSize
;
296 #endregion // Protected Instance Properties
298 #region Public Instance Methods
299 public void PerformClick() {
300 OnClick(EventArgs
.Empty
);
303 public override string ToString() {
304 return base.ToString() + ", Checked: " + this.Checked
;
306 #endregion // Public Instance Methods
308 #region Protected Instance Methods
309 protected override AccessibleObject
CreateAccessibilityInstance() {
312 ao
= base.CreateAccessibilityInstance ();
313 ao
.role
= AccessibleRole
.RadioButton
;
318 protected virtual void OnCheckedChanged(EventArgs e
) {
319 EventHandler eh
= (EventHandler
)(Events
[CheckedChangedEvent
]);
324 protected override void OnClick(EventArgs e
) {
334 protected override void OnEnter(EventArgs e
) {
335 PerformDefaultCheck ();
339 protected override void OnHandleCreated(EventArgs e
) {
340 base.OnHandleCreated(e
);
343 protected override void OnMouseUp(MouseEventArgs mevent
) {
344 base.OnMouseUp(mevent
);
347 protected override bool ProcessMnemonic(char charCode
) {
348 if (IsMnemonic(charCode
, Text
) == true) {
354 return base.ProcessMnemonic(charCode
);
356 #endregion // Protected Instance Methods
359 static object AppearanceChangedEvent
= new object ();
360 static object CheckedChangedEvent
= new object ();
362 public event EventHandler AppearanceChanged
{
363 add { Events.AddHandler (AppearanceChangedEvent, value); }
364 remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
367 public event EventHandler CheckedChanged
{
368 add { Events.AddHandler (CheckedChangedEvent, value); }
369 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
373 [EditorBrowsable (EditorBrowsableState
.Never
)]
374 public new event EventHandler DoubleClick
{
375 add { base.DoubleClick += value; }
376 remove { base.DoubleClick -= value; }
380 [EditorBrowsable (EditorBrowsableState
.Never
)]
381 public new event MouseEventHandler MouseDoubleClick
{
382 add { base.MouseDoubleClick += value; }
383 remove { base.MouseDoubleClick -= value; }