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.child_controls.Count; i++) {
123 if ((this != c.child_controls[i]) && (c.child_controls[i] is RadioButton)) {
124 if (((RadioButton)(c.child_controls[i])).auto_check) {
125 c.child_controls[i].TabStop = false;
126 ((RadioButton)(c.child_controls[i])).Checked = false;
135 internal override void HaveDoubleClick() {
136 if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
139 internal override void Draw (PaintEventArgs pe) {
140 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
142 #endregion // Private Methods
144 #region Public Instance Properties
145 [DefaultValue(Appearance.Normal)]
147 public Appearance Appearance {
153 if (value != appearance) {
155 if (AppearanceChanged != null) {
156 AppearanceChanged(this, EventArgs.Empty);
164 public bool AutoCheck {
176 [DefaultValue(ContentAlignment.MiddleLeft)]
177 public ContentAlignment CheckAlign {
179 return radiobutton_alignment;
183 if (value != radiobutton_alignment) {
184 radiobutton_alignment = value;
191 [DefaultValue(false)]
192 public bool Checked {
194 if (check_state != CheckState.Unchecked) {
201 if (value && (check_state != CheckState.Checked)) {
203 check_state = CheckState.Checked;
205 OnCheckedChanged(EventArgs.Empty);
206 } else if (!value && (check_state != CheckState.Unchecked)) {
207 check_state = CheckState.Unchecked;
209 OnCheckedChanged(EventArgs.Empty);
214 [DefaultValue(false)]
215 public new bool TabStop {
225 [DefaultValue(ContentAlignment.MiddleLeft)]
227 public override ContentAlignment TextAlign {
229 return text_alignment;
233 if (value != text_alignment) {
234 text_alignment = value;
239 #endregion // Public Instance Properties
241 #region Protected Instance Properties
242 protected override CreateParams CreateParams {
244 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
245 SetStyle(ControlStyles.UserPaint, true);
247 return base.CreateParams;
251 protected override Size DefaultSize {
253 return ThemeEngine.Current.RadioButtonDefaultSize;
256 #endregion // Protected Instance Properties
258 #region Public Instance Methods
259 public void PerformClick() {
260 OnClick(EventArgs.Empty);
263 public override string ToString() {
264 return base.ToString() + ", Checked: " + this.Checked;
266 #endregion // Public Instance Methods
268 #region Protected Instance Methods
269 protected override AccessibleObject CreateAccessibilityInstance() {
272 ao = base.CreateAccessibilityInstance ();
273 ao.role = AccessibleRole.RadioButton;
278 protected virtual void OnCheckedChanged(EventArgs e) {
279 if (CheckedChanged != null) {
280 CheckedChanged(this, e);
284 protected override void OnClick(EventArgs e) {
296 protected override void OnEnter(EventArgs e) {
300 protected override void OnHandleCreated(EventArgs e) {
301 base.OnHandleCreated(e);
304 protected override void OnMouseUp(MouseEventArgs mevent) {
305 base.OnMouseUp(mevent);
308 protected override bool ProcessMnemonic(char charCode) {
309 if (IsMnemonic(charCode, Text) == true) {
315 return base.ProcessMnemonic(charCode);
317 #endregion // Protected Instance Methods
320 public event EventHandler AppearanceChanged;
321 public event EventHandler CheckedChanged;
324 [EditorBrowsable (EditorBrowsableState.Never)]
325 public event EventHandler DoubleClick;