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")]
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 : ControlAccessibleObject {
50 #region RadioButtonAccessibleObject Local Variables
51 private 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 text_alignment = ContentAlignment.MiddleLeft;
112 #endregion // Public Constructors
114 #region Private Methods
115 private void UpdateSiblings() {
118 if (auto_check == false) {
122 // Remove tabstop property from and uncheck our radio-button siblings
125 for (int i = 0; i < c.Controls.Count; i++) {
126 if ((this != c.Controls[i]) && (c.Controls[i] is RadioButton)) {
127 if (((RadioButton)(c.Controls[i])).auto_check) {
128 c.Controls[i].TabStop = false;
129 ((RadioButton)(c.Controls[i])).Checked = false;
138 internal override void Draw (PaintEventArgs pe) {
139 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
141 #endregion // Private Methods
143 #region Public Instance Properties
144 [DefaultValue(Appearance.Normal)]
146 public Appearance Appearance {
152 if (value != appearance) {
154 EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
156 eh (this, EventArgs.Empty);
163 public bool AutoCheck {
177 [DefaultValue(ContentAlignment.MiddleLeft)]
178 public ContentAlignment CheckAlign {
180 return radiobutton_alignment;
184 if (value != radiobutton_alignment) {
185 radiobutton_alignment = value;
192 [DefaultValue(false)]
194 [SettingsBindable (true)]
195 [Bindable (true, BindingDirection.OneWay)]
197 public bool Checked {
199 if (check_state != CheckState.Unchecked) {
206 if (value && (check_state != CheckState.Checked)) {
208 check_state = CheckState.Checked;
210 OnCheckedChanged(EventArgs.Empty);
211 } else if (!value && (check_state != CheckState.Unchecked)) {
212 check_state = CheckState.Unchecked;
214 OnCheckedChanged(EventArgs.Empty);
219 [DefaultValue(false)]
220 public new bool TabStop {
221 get { return base.TabStop; }
222 set { base.TabStop = value; }
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 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
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 static object AppearanceChangedEvent = new object ();
321 static object CheckedChangedEvent = new object ();
323 public event EventHandler AppearanceChanged {
324 add { Events.AddHandler (AppearanceChangedEvent, value); }
325 remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
328 public event EventHandler CheckedChanged {
329 add { Events.AddHandler (CheckedChangedEvent, value); }
330 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
334 [EditorBrowsable (EditorBrowsableState.Never)]
335 public new event EventHandler DoubleClick {
336 add { base.DoubleClick += value; }
337 remove { base.DoubleClick -= value; }