2007-05-03 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RadioButton.cs
blob13230187d4aa777014193a7c2a8e51e7b4543e1c
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 using System.ComponentModel;
27 using System.Drawing;
28 using System.Drawing.Text;
29 using System.Runtime.InteropServices;
31 namespace System.Windows.Forms {
32 [DefaultProperty("Checked")]
33 [DefaultEvent("CheckedChanged")]
34 #if NET_2_0
35 [ClassInterface (ClassInterfaceType.AutoDispatch)]
36 [ComVisible (true)]
37 [DefaultBindingProperty ("Checked")]
38 [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts.AssemblySystem_Design)]
39 [Designer ("System.Windows.Forms.Design.RadioButtonDesigner, " + Consts.AssemblySystem_Design)]
40 #endif
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
50 [ComVisible(true)]
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) {
58 this.owner = owner;
60 #endregion // RadioButtonAccessibleObject Constructors
62 #region RadioButtonAccessibleObject Properties
63 public override string DefaultAction {
64 get {
65 return "Select";
69 public override AccessibleRole Role {
70 get {
71 return AccessibleRole.RadioButton;
75 public override AccessibleStates State {
76 get {
77 AccessibleStates retval;
79 retval = AccessibleStates.Default;
81 if (owner.check_state == CheckState.Checked) {
82 retval |= AccessibleStates.Checked;
85 if (owner.Focused) {
86 retval |= AccessibleStates.Focused;
89 if (owner.CanFocus) {
90 retval |= AccessibleStates.Focusable;
93 return retval;
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;
109 auto_check = true;
110 radiobutton_alignment = ContentAlignment.MiddleLeft;
111 text_alignment = ContentAlignment.MiddleLeft;
112 TabStop = false;
114 #endregion // Public Constructors
116 #region Private Methods
117 private void UpdateSiblings() {
118 Control c;
120 if (auto_check == false) {
121 return;
124 // Remove tabstop property from and uncheck our radio-button siblings
125 c = this.Parent;
126 if (c != null) {
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;
137 this.TabStop = true;
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)]
147 [Localizable(true)]
148 public Appearance Appearance {
149 get {
150 return appearance;
153 set {
154 if (value != appearance) {
155 appearance = value;
156 EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
157 if (eh != null)
158 eh (this, EventArgs.Empty);
159 Redraw();
164 [DefaultValue(true)]
165 public bool AutoCheck {
166 get {
167 return auto_check;
170 set {
171 auto_check = value;
175 #if !NET_2_0
176 [Bindable(true)]
177 #endif
178 [Localizable(true)]
179 [DefaultValue(ContentAlignment.MiddleLeft)]
180 public ContentAlignment CheckAlign {
181 get {
182 return radiobutton_alignment;
185 set {
186 if (value != radiobutton_alignment) {
187 radiobutton_alignment = value;
189 Redraw();
194 [DefaultValue(false)]
195 #if NET_2_0
196 [SettingsBindable (true)]
197 [Bindable (true, BindingDirection.OneWay)]
198 #endif
199 public bool Checked {
200 get {
201 if (check_state != CheckState.Unchecked) {
202 return true;
204 return false;
207 set {
208 if (value && (check_state != CheckState.Checked)) {
209 UpdateSiblings();
210 check_state = CheckState.Checked;
211 Redraw();
212 OnCheckedChanged(EventArgs.Empty);
213 } else if (!value && (check_state != CheckState.Unchecked)) {
214 check_state = CheckState.Unchecked;
215 Redraw();
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)]
228 [Localizable(true)]
229 public override ContentAlignment TextAlign {
230 get {
231 return text_alignment;
234 set {
235 if (value != text_alignment) {
236 text_alignment = value;
237 Redraw();
241 #endregion // Public Instance Properties
243 #region Protected Instance Properties
244 protected override CreateParams CreateParams {
245 get {
246 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
247 SetStyle(ControlStyles.UserPaint, true);
249 return base.CreateParams;
253 protected override Size DefaultSize {
254 get {
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() {
272 AccessibleObject ao;
274 ao = base.CreateAccessibilityInstance ();
275 ao.role = AccessibleRole.RadioButton;
277 return ao;
280 protected virtual void OnCheckedChanged(EventArgs e) {
281 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
282 if (eh != null)
283 eh (this, e);
286 protected override void OnClick(EventArgs e) {
287 if (auto_check) {
288 if (!Checked) {
289 Checked = true;
291 } else {
292 Checked = !Checked;
295 base.OnClick (e);
298 protected override void OnEnter(EventArgs e) {
299 base.OnEnter(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) {
312 Select();
313 PerformClick();
314 return true;
317 return base.ProcessMnemonic(charCode);
319 #endregion // Protected Instance Methods
321 #region Events
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); }
335 [Browsable(false)]
336 [EditorBrowsable (EditorBrowsableState.Never)]
337 public new event EventHandler DoubleClick {
338 add { base.DoubleClick += value; }
339 remove { base.DoubleClick -= value; }
342 #if NET_2_0
343 [Browsable (false)]
344 [EditorBrowsable (EditorBrowsableState.Never)]
345 public new event MouseEventHandler MouseDoubleClick {
346 add { base.MouseDoubleClick += value; }
347 remove { base.MouseDoubleClick -= value; }
349 #endif
350 #endregion // Events