In System.Windows.Forms:
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RadioButton.cs
blob22410805d34fbda463abb608b15e183e730528a1
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
22 // Authors:
23 // Peter Bartok pbartok@novell.com
26 // COMPLETE
28 using System.ComponentModel;
29 using System.Drawing;
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
45 [ComVisible(true)]
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) {
53 this.owner = owner;
55 #endregion // RadioButtonAccessibleObject Constructors
57 #region RadioButtonAccessibleObject Properties
58 public override string DefaultAction {
59 get {
60 return "Select";
64 public override AccessibleRole Role {
65 get {
66 return AccessibleRole.RadioButton;
70 public override AccessibleStates State {
71 get {
72 AccessibleStates retval;
74 retval = AccessibleStates.Default;
76 if (owner.check_state == CheckState.Checked) {
77 retval |= AccessibleStates.Checked;
80 if (owner.Focused) {
81 retval |= AccessibleStates.Focused;
84 if (owner.CanFocus) {
85 retval |= AccessibleStates.Focusable;
88 return retval;
91 #endregion // RadioButtonAccessibleObject Properties
93 #region RadioButtonAccessibleObject Methods
94 public override void DoDefaultAction() {
95 owner.PerformClick();
97 #endregion // RadioButtonAccessibleObject Methods
99 #endregion // RadioButtonAccessibleObject Sub-class
101 #region Public Constructors
102 public RadioButton() {
103 appearance = Appearance.Normal;
104 auto_check = true;
105 radiobutton_alignment = ContentAlignment.MiddleLeft;
106 text_alignment = ContentAlignment.MiddleLeft;
107 TabStop = false;
109 #endregion // Public Constructors
111 #region Private Methods
112 private void UpdateSiblings() {
113 Control c;
115 if (auto_check == false) {
116 return;
119 // Remove tabstop property from and uncheck our radio-button siblings
120 c = this.Parent;
121 if (c != null) {
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;
132 this.TabStop = true;
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)]
142 [Localizable(true)]
143 public Appearance Appearance {
144 get {
145 return appearance;
148 set {
149 if (value != appearance) {
150 appearance = value;
151 EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
152 if (eh != null)
153 eh (this, EventArgs.Empty);
154 Redraw();
159 [DefaultValue(true)]
160 public bool AutoCheck {
161 get {
162 return auto_check;
165 set {
166 auto_check = value;
170 [Bindable(true)]
171 [Localizable(true)]
172 [DefaultValue(ContentAlignment.MiddleLeft)]
173 public ContentAlignment CheckAlign {
174 get {
175 return radiobutton_alignment;
178 set {
179 if (value != radiobutton_alignment) {
180 radiobutton_alignment = value;
182 Redraw();
187 [DefaultValue(false)]
188 public bool Checked {
189 get {
190 if (check_state != CheckState.Unchecked) {
191 return true;
193 return false;
196 set {
197 if (value && (check_state != CheckState.Checked)) {
198 UpdateSiblings();
199 check_state = CheckState.Checked;
200 Redraw();
201 OnCheckedChanged(EventArgs.Empty);
202 } else if (!value && (check_state != CheckState.Unchecked)) {
203 check_state = CheckState.Unchecked;
204 Redraw();
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)]
217 [Localizable(true)]
218 public override ContentAlignment TextAlign {
219 get {
220 return text_alignment;
223 set {
224 if (value != text_alignment) {
225 text_alignment = value;
226 Redraw();
230 #endregion // Public Instance Properties
232 #region Protected Instance Properties
233 protected override CreateParams CreateParams {
234 get {
235 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
236 SetStyle(ControlStyles.UserPaint, true);
238 return base.CreateParams;
242 protected override Size DefaultSize {
243 get {
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() {
261 AccessibleObject ao;
263 ao = base.CreateAccessibilityInstance ();
264 ao.role = AccessibleRole.RadioButton;
266 return ao;
269 protected virtual void OnCheckedChanged(EventArgs e) {
270 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
271 if (eh != null)
272 eh (this, e);
275 protected override void OnClick(EventArgs e) {
276 if (auto_check) {
277 if (!Checked) {
278 Checked = true;
280 } else {
281 Checked = !Checked;
284 base.OnClick (e);
287 protected override void OnEnter(EventArgs e) {
288 base.OnEnter(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) {
301 Select();
302 PerformClick();
303 return true;
306 return base.ProcessMnemonic(charCode);
308 #endregion // Protected Instance Methods
310 #region Events
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); }
324 [Browsable(false)]
325 [EditorBrowsable (EditorBrowsableState.Never)]
326 public new event EventHandler DoubleClick {
327 add { base.DoubleClick += value; }
328 remove { base.DoubleClick -= value; }
330 #endregion // Events