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")]
38 [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts.AssemblySystem_Design)]
39 [Designer ("System.Windows.Forms.Design.RadioButtonDesigner, " + Consts.AssemblySystem_Design)]
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
52 public class RadioButtonAccessibleObject : ButtonBaseAccessibleObject {
54 public class RadioButtonAccessibleObject : ControlAccessibleObject {
56 #region RadioButtonAccessibleObject Local Variables
57 private new RadioButton owner;
58 #endregion // RadioButtonAccessibleObject Local Variables
60 #region RadioButtonAccessibleObject Constructors
61 public RadioButtonAccessibleObject(RadioButton owner) : base(owner) {
64 #endregion // RadioButtonAccessibleObject Constructors
66 #region RadioButtonAccessibleObject Properties
67 public override string DefaultAction {
73 public override AccessibleRole Role {
75 return AccessibleRole.RadioButton;
79 public override AccessibleStates State {
81 AccessibleStates retval;
83 retval = AccessibleStates.Default;
85 if (owner.check_state == CheckState.Checked) {
86 retval |= AccessibleStates.Checked;
90 retval |= AccessibleStates.Focused;
94 retval |= AccessibleStates.Focusable;
100 #endregion // RadioButtonAccessibleObject Properties
102 #region RadioButtonAccessibleObject Methods
103 public override void DoDefaultAction() {
104 owner.PerformClick();
106 #endregion // RadioButtonAccessibleObject Methods
108 #endregion // RadioButtonAccessibleObject Sub-class
110 #region Public Constructors
111 public RadioButton() {
112 appearance = Appearance.Normal;
114 radiobutton_alignment = ContentAlignment.MiddleLeft;
115 TextAlign = ContentAlignment.MiddleLeft;
118 #endregion // Public Constructors
120 #region Private Methods
121 private void UpdateSiblings() {
124 if (auto_check == false) {
128 // Remove tabstop property from and uncheck our radio-button siblings
131 for (int i = 0; i < c.Controls.Count; i++) {
132 if ((this != c.Controls[i]) && (c.Controls[i] is RadioButton)) {
133 if (((RadioButton)(c.Controls[i])).auto_check) {
134 c.Controls[i].TabStop = false;
135 ((RadioButton)(c.Controls[i])).Checked = false;
144 internal override void Draw (PaintEventArgs pe) {
146 // FIXME: This should be called every time something that can affect it
147 // is changed, not every paint. Can only change so many things at a time.
149 // Figure out where our text and image should go
150 Rectangle glyph_rectangle;
151 Rectangle text_rectangle;
152 Rectangle image_rectangle;
154 ThemeEngine.Current.CalculateRadioButtonTextAndImageLayout (this, Point.Empty, out glyph_rectangle, out text_rectangle, out image_rectangle);
157 if (FlatStyle != FlatStyle.System)
158 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this, glyph_rectangle, text_rectangle, image_rectangle, pe.ClipRectangle);
160 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
162 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
167 internal override Size GetPreferredSizeCore (Size proposedSize)
170 return ThemeEngine.Current.CalculateRadioButtonAutoSize (this);
172 return base.GetPreferredSizeCore (proposedSize);
175 #endregion // Private Methods
177 #region Public Instance Properties
178 [DefaultValue(Appearance.Normal)]
180 public Appearance Appearance {
186 if (value != appearance) {
188 EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
190 eh (this, EventArgs.Empty);
197 public bool AutoCheck {
211 [DefaultValue(ContentAlignment.MiddleLeft)]
212 public ContentAlignment CheckAlign {
214 return radiobutton_alignment;
218 if (value != radiobutton_alignment) {
219 radiobutton_alignment = value;
226 [DefaultValue(false)]
228 [SettingsBindable (true)]
229 [Bindable (true, BindingDirection.OneWay)]
231 public bool Checked {
233 if (check_state != CheckState.Unchecked) {
240 if (value && (check_state != CheckState.Checked)) {
242 check_state = CheckState.Checked;
244 OnCheckedChanged(EventArgs.Empty);
245 } else if (!value && (check_state != CheckState.Unchecked)) {
246 check_state = CheckState.Unchecked;
248 OnCheckedChanged(EventArgs.Empty);
253 [DefaultValue(false)]
254 public new bool TabStop {
255 get { return base.TabStop; }
256 set { base.TabStop = value; }
259 [DefaultValue(ContentAlignment.MiddleLeft)]
261 public override ContentAlignment TextAlign {
262 get { return base.TextAlign; }
263 set { base.TextAlign = value; }
265 #endregion // Public Instance Properties
267 #region Protected Instance Properties
268 protected override CreateParams CreateParams {
270 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
271 SetStyle(ControlStyles.UserPaint, true);
273 return base.CreateParams;
277 protected override Size DefaultSize {
279 return ThemeEngine.Current.RadioButtonDefaultSize;
282 #endregion // Protected Instance Properties
284 #region Public Instance Methods
285 public void PerformClick() {
286 OnClick(EventArgs.Empty);
289 public override string ToString() {
290 return base.ToString() + ", Checked: " + this.Checked;
292 #endregion // Public Instance Methods
294 #region Protected Instance Methods
295 protected override AccessibleObject CreateAccessibilityInstance() {
298 ao = base.CreateAccessibilityInstance ();
299 ao.role = AccessibleRole.RadioButton;
304 protected virtual void OnCheckedChanged(EventArgs e) {
305 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
310 protected override void OnClick(EventArgs e) {
322 protected override void OnEnter(EventArgs e) {
326 protected override void OnHandleCreated(EventArgs e) {
327 base.OnHandleCreated(e);
330 protected override void OnMouseUp(MouseEventArgs mevent) {
331 base.OnMouseUp(mevent);
334 protected override bool ProcessMnemonic(char charCode) {
335 if (IsMnemonic(charCode, Text) == true) {
341 return base.ProcessMnemonic(charCode);
343 #endregion // Protected Instance Methods
346 static object AppearanceChangedEvent = new object ();
347 static object CheckedChangedEvent = new object ();
349 public event EventHandler AppearanceChanged {
350 add { Events.AddHandler (AppearanceChangedEvent, value); }
351 remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
354 public event EventHandler CheckedChanged {
355 add { Events.AddHandler (CheckedChangedEvent, value); }
356 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
360 [EditorBrowsable (EditorBrowsableState.Never)]
361 public new event EventHandler DoubleClick {
362 add { base.DoubleClick += value; }
363 remove { base.DoubleClick -= value; }
368 [EditorBrowsable (EditorBrowsableState.Never)]
369 public new event MouseEventHandler MouseDoubleClick {
370 add { base.MouseDoubleClick += value; }
371 remove { base.MouseDoubleClick -= value; }