[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / RadioButton.cs
blob89fb9d1a32f591d4e411929d93699ac84e9d09b5
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 [ClassInterface (ClassInterfaceType.AutoDispatch)]
35 [ComVisible (true)]
36 [DefaultBindingProperty ("Checked")]
37 [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts.AssemblySystem_Design)]
38 [Designer ("System.Windows.Forms.Design.RadioButtonDesigner, " + Consts.AssemblySystem_Design)]
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
48 [ComVisible(true)]
49 public class RadioButtonAccessibleObject : ButtonBaseAccessibleObject {
50 #region RadioButtonAccessibleObject Local Variables
51 private new RadioButton owner;
52 #endregion // RadioButtonAccessibleObject Local Variables
54 #region RadioButtonAccessibleObject Constructors
55 public RadioButtonAccessibleObject(RadioButton owner) : base(owner) {
56 this.owner = owner;
58 #endregion // RadioButtonAccessibleObject Constructors
60 #region RadioButtonAccessibleObject Properties
61 public override string DefaultAction {
62 get {
63 return "Select";
67 public override AccessibleRole Role {
68 get {
69 return AccessibleRole.RadioButton;
73 public override AccessibleStates State {
74 get {
75 AccessibleStates retval;
77 retval = AccessibleStates.Default;
79 if (owner.check_state == CheckState.Checked) {
80 retval |= AccessibleStates.Checked;
83 if (owner.Focused) {
84 retval |= AccessibleStates.Focused;
87 if (owner.CanFocus) {
88 retval |= AccessibleStates.Focusable;
91 return retval;
94 #endregion // RadioButtonAccessibleObject Properties
96 #region RadioButtonAccessibleObject Methods
97 public override void DoDefaultAction() {
98 owner.PerformClick();
100 #endregion // RadioButtonAccessibleObject Methods
102 #endregion // RadioButtonAccessibleObject Sub-class
104 #region Public Constructors
105 public RadioButton() {
106 appearance = Appearance.Normal;
107 auto_check = true;
108 radiobutton_alignment = ContentAlignment.MiddleLeft;
109 TextAlign = ContentAlignment.MiddleLeft;
110 TabStop = false;
112 #endregion // Public Constructors
114 #region Private Methods
116 // When getting OnEnter we need to set Checked as true in case none of the sibling radio
117 // buttons is checked.
118 private void PerformDefaultCheck ()
120 // if we are already checked, no need to check the other controls
121 if (!auto_check || Checked)
122 return;
124 bool is_any_selected = false;
125 Control c = Parent;
126 if (c != null) {
127 for (int i = 0; i < c.Controls.Count; i++) {
128 RadioButton rb = c.Controls [i] as RadioButton;
129 if (rb == null || !rb.auto_check)
130 continue;
132 if (rb.check_state == CheckState.Checked) {
133 is_any_selected = true;
134 break;
139 if (!is_any_selected)
140 Checked = true;
143 private void UpdateSiblings() {
144 Control c;
146 if (auto_check == false) {
147 return;
150 // Remove tabstop property from and uncheck our radio-button siblings
151 c = this.Parent;
152 if (c != null) {
153 for (int i = 0; i < c.Controls.Count; i++) {
154 if ((this != c.Controls[i]) && (c.Controls[i] is RadioButton)) {
155 if (((RadioButton)(c.Controls[i])).auto_check) {
156 c.Controls[i].TabStop = false;
157 ((RadioButton)(c.Controls[i])).Checked = false;
163 this.TabStop = true;
166 internal override void Draw (PaintEventArgs pe) {
167 // FIXME: This should be called every time something that can affect it
168 // is changed, not every paint. Can only change so many things at a time.
170 // Figure out where our text and image should go
171 Rectangle glyph_rectangle;
172 Rectangle text_rectangle;
173 Rectangle image_rectangle;
175 ThemeEngine.Current.CalculateRadioButtonTextAndImageLayout (this, Point.Empty, out glyph_rectangle, out text_rectangle, out image_rectangle);
177 // Draw our button
178 if (FlatStyle != FlatStyle.System && Appearance != Appearance.Button)
179 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this, glyph_rectangle, text_rectangle, image_rectangle, pe.ClipRectangle);
180 else
181 ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
184 internal override Size GetPreferredSizeCore (Size proposedSize)
186 if (this.AutoSize)
187 return ThemeEngine.Current.CalculateRadioButtonAutoSize (this);
189 return base.GetPreferredSizeCore (proposedSize);
191 #endregion // Private Methods
193 #region Public Instance Properties
194 [DefaultValue(Appearance.Normal)]
195 [Localizable(true)]
196 public Appearance Appearance {
197 get {
198 return appearance;
201 set {
202 if (value != appearance) {
203 appearance = value;
204 EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
205 if (eh != null)
206 eh (this, EventArgs.Empty);
207 if (Parent != null)
208 Parent.PerformLayout (this, "Appearance");
209 Invalidate();
214 [DefaultValue(true)]
215 public bool AutoCheck {
216 get {
217 return auto_check;
220 set {
221 auto_check = value;
225 [Localizable(true)]
226 [DefaultValue(ContentAlignment.MiddleLeft)]
227 public ContentAlignment CheckAlign {
228 get {
229 return radiobutton_alignment;
232 set {
233 if (value != radiobutton_alignment) {
234 radiobutton_alignment = value;
236 Invalidate();
241 [DefaultValue(false)]
242 [SettingsBindable (true)]
243 [Bindable (true, BindingDirection.OneWay)]
244 public bool Checked {
245 get {
246 if (check_state != CheckState.Unchecked) {
247 return true;
249 return false;
252 set {
253 if (value && (check_state != CheckState.Checked)) {
254 check_state = CheckState.Checked;
255 Invalidate();
256 UpdateSiblings();
257 OnCheckedChanged(EventArgs.Empty);
258 } else if (!value && (check_state != CheckState.Unchecked)) {
259 TabStop = false;
260 check_state = CheckState.Unchecked;
261 Invalidate();
262 OnCheckedChanged(EventArgs.Empty);
267 [DefaultValue(false)]
268 public new bool TabStop {
269 get { return base.TabStop; }
270 set { base.TabStop = value; }
273 [DefaultValue(ContentAlignment.MiddleLeft)]
274 [Localizable(true)]
275 public override ContentAlignment TextAlign {
276 get { return base.TextAlign; }
277 set { base.TextAlign = value; }
279 #endregion // Public Instance Properties
281 #region Protected Instance Properties
282 protected override CreateParams CreateParams {
283 get {
284 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
285 SetStyle(ControlStyles.UserPaint, true);
287 return base.CreateParams;
291 protected override Size DefaultSize {
292 get {
293 return ThemeEngine.Current.RadioButtonDefaultSize;
296 #endregion // Protected Instance Properties
298 #region Public Instance Methods
299 public void PerformClick() {
300 OnClick(EventArgs.Empty);
303 public override string ToString() {
304 return base.ToString() + ", Checked: " + this.Checked;
306 #endregion // Public Instance Methods
308 #region Protected Instance Methods
309 protected override AccessibleObject CreateAccessibilityInstance() {
310 AccessibleObject ao;
312 ao = base.CreateAccessibilityInstance ();
313 ao.role = AccessibleRole.RadioButton;
315 return ao;
318 protected virtual void OnCheckedChanged(EventArgs e) {
319 EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
320 if (eh != null)
321 eh (this, e);
324 protected override void OnClick(EventArgs e) {
325 if (auto_check) {
326 if (!Checked) {
327 Checked = true;
331 base.OnClick (e);
334 protected override void OnEnter(EventArgs e) {
335 PerformDefaultCheck ();
336 base.OnEnter(e);
339 protected override void OnHandleCreated(EventArgs e) {
340 base.OnHandleCreated(e);
343 protected override void OnMouseUp(MouseEventArgs mevent) {
344 base.OnMouseUp(mevent);
347 protected override bool ProcessMnemonic(char charCode) {
348 if (IsMnemonic(charCode, Text) == true) {
349 Select();
350 PerformClick();
351 return true;
354 return base.ProcessMnemonic(charCode);
356 #endregion // Protected Instance Methods
358 #region Events
359 static object AppearanceChangedEvent = new object ();
360 static object CheckedChangedEvent = new object ();
362 public event EventHandler AppearanceChanged {
363 add { Events.AddHandler (AppearanceChangedEvent, value); }
364 remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
367 public event EventHandler CheckedChanged {
368 add { Events.AddHandler (CheckedChangedEvent, value); }
369 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
372 [Browsable(false)]
373 [EditorBrowsable (EditorBrowsableState.Never)]
374 public new event EventHandler DoubleClick {
375 add { base.DoubleClick += value; }
376 remove { base.DoubleClick -= value; }
379 [Browsable (false)]
380 [EditorBrowsable (EditorBrowsableState.Never)]
381 public new event MouseEventHandler MouseDoubleClick {
382 add { base.MouseDoubleClick += value; }
383 remove { base.MouseDoubleClick -= value; }
385 #endregion // Events