Copied remotely
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RadioButton.cs
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.
19 //
20 // Copyright (c) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 // $Log: RadioButton.cs,v $
26 // Revision 1.8  2004/10/15 13:32:45  ravindra
27 //      - Renamed Paint() method to Draw() for clarity. Also, moved
28 //      DrawImage() to OnPaint().
29 //
30 // Revision 1.7  2004/10/15 13:25:50  ravindra
31 //      - Removed Redraw (), we get it from ButtonBase.
32 //      - Implemented Paint (), to do class specific painting.
33 //
34 // Revision 1.6  2004/10/05 18:23:54  jackson
35 // Fix ctor
36 //
37 // Revision 1.5  2004/09/30 17:34:08  jackson
38 // Fix typo. Patch by John BouAntoun.
39 //
40 // Revision 1.4  2004/09/28 18:44:25  pbartok
41 // - Streamlined Theme interfaces:
42 //   * Each DrawXXX method for a control now is passed the object for the
43 //     control to be drawn in order to allow accessing any state the theme
44 //     might require
45 //
46 //   * ControlPaint methods for the theme now have a CP prefix to avoid
47 //     name clashes with the Draw methods for controls
48 //
49 //   * Every control now retrieves it's DefaultSize from the current theme
50 //
51 // Revision 1.3  2004/09/02 20:26:48  pbartok
52 // - Added missing RadioButton.RadioButtonAccessibleObject class
53 //
54 // Revision 1.2  2004/09/01 20:44:11  pbartok
55 // - Fixed state
56 //
57 // Revision 1.1  2004/09/01 20:40:02  pbartok
58 // - Functional initial check-in
59 //
60 //
61 //
62
63 // COMPLETE
64
65 using System.Drawing;
66 using System.Drawing.Text;
67
68 namespace System.Windows.Forms {
69         public class RadioButton : ButtonBase {
70                 #region Local Variables
71                 internal Appearance             appearance;
72                 internal bool                   auto_check;
73                 internal ContentAlignment       radiobutton_alignment;
74                 internal ContentAlignment       text_alignment;
75                 internal CheckState             check_state;
76                 private bool                    is_tabstop;
77                 #endregion      // Local Variables
78
79                 #region RadioButtonAccessibleObject Subclass
80                 public class RadioButtonAccessibleObject : ControlAccessibleObject {
81                         #region RadioButtonAccessibleObject Local Variables
82                         private RadioButton     owner;
83                         #endregion      // RadioButtonAccessibleObject Local Variables
84
85                         #region RadioButtonAccessibleObject Constructors
86                         public RadioButtonAccessibleObject(RadioButton owner) : base(owner) {
87                                 this.owner = owner;
88                         }
89                         #endregion      // RadioButtonAccessibleObject Constructors
90
91                         #region RadioButtonAccessibleObject Properties
92                         public override string DefaultAction {
93                                 get {
94                                         return "Select";
95                                 }
96                         }
97
98                         public override AccessibleRole Role {
99                                 get {
100                                         return AccessibleRole.RadioButton;
101                                 }
102                         }
103
104                         public override AccessibleStates State {
105                                 get {
106                                         AccessibleStates        retval;
107
108                                         retval = AccessibleStates.Default;
109
110                                         if (owner.check_state == CheckState.Checked) {
111                                                 retval |= AccessibleStates.Checked;
112                                         }
113
114                                         if (owner.Focused) {
115                                                 retval |= AccessibleStates.Focused;
116                                         }
117
118                                         if (owner.CanFocus) {
119                                                 retval |= AccessibleStates.Focusable;
120                                         }
121
122                                         return retval;
123                                 }
124                         }
125                         #endregion      // RadioButtonAccessibleObject Properties
126
127                         #region RadioButtonAccessibleObject Methods
128                         public override void DoDefaultAction() {
129                                 owner.PerformClick();
130                         }
131                         #endregion      // RadioButtonAccessibleObject Methods
132                 }
133                 #endregion      // RadioButtonAccessibleObject Sub-class
134
135                 #region Public Constructors
136                 public RadioButton() {
137                         appearance = Appearance.Normal;
138                         auto_check = true;
139                         radiobutton_alignment = ContentAlignment.MiddleLeft;
140                         text_alignment = ContentAlignment.MiddleLeft;
141                         is_tabstop = false;
142                 }
143                 #endregion      // Public Constructors
144
145                 #region Public Instance Properties
146                 public Appearance Appearance {
147                         get {
148                                 return appearance;
149                         }
150
151                         set {
152                                 if (value != appearance) {
153                                         appearance = value;
154                                         if (AppearanceChanged != null) {
155                                                 AppearanceChanged(this, EventArgs.Empty);
156                                         }
157                                         Redraw();
158                                 }
159                         }
160                 }
161
162                 public bool AutoCheck {
163                         get {
164                                 return auto_check;
165                         }
166
167                         set {
168                                 auto_check = value;
169                         }
170                 }
171
172                 public ContentAlignment CheckAlign {
173                         get {
174                                 return radiobutton_alignment;
175                         }
176
177                         set {
178                                 if (value != radiobutton_alignment) {
179                                         radiobutton_alignment = value;
180
181                                         Redraw();
182                                 }
183                         }
184                 }
185
186                 public bool Checked {
187                         get {
188                                 if (check_state != CheckState.Unchecked) {
189                                         return true;
190                                 }
191                                 return false;
192                         }
193
194                         set {
195                                 if (value && (check_state != CheckState.Checked)) {
196                                         check_state = CheckState.Checked;
197                                         Redraw();
198                                         OnCheckedChanged(EventArgs.Empty);
199                                 } else if (!value && (check_state != CheckState.Unchecked)) {
200                                         check_state = CheckState.Unchecked;
201                                         Redraw();
202                                         OnCheckedChanged(EventArgs.Empty);
203                                 }
204                         }
205                 }
206
207                 public bool TabStop {
208                         get {
209                                 return is_tabstop;
210                         }
211
212                         set {
213                                 is_tabstop = value;
214                         }
215                 }
216
217                 public override ContentAlignment TextAlign {
218                         get {
219                                 return text_alignment;
220                         }
221
222                         set {
223                                 if (value != text_alignment) {
224                                         text_alignment = value;
225                                         Redraw();
226                                 }
227                         }
228                 }
229                 #endregion      // Public Instance Properties
230
231                 #region Protected Instance Properties
232                 protected override CreateParams CreateParams {
233                         get {
234                                 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
235                                 SetStyle(ControlStyles.UserPaint, true);
236
237                                 return base.CreateParams;
238                         }
239                 }
240
241                 protected override Size DefaultSize {
242                         get {
243                                 return ThemeEngine.Current.RadioButtonDefaultSize;
244                         }
245                 }
246                 #endregion      // Protected Instance Properties
247
248                 #region Public Instance Methods
249                 public void PerformClick() {\r
250                         OnClick(EventArgs.Empty);\r
251                 }\r
252 \r
253                 public override string ToString() {\r
254                         return base.ToString() + ", Checked: " + this.Checked;\r
255                 }\r
256                 #endregion      // Public Instance Methods
257
258                 #region Protected Instance Methods
259                 protected override AccessibleObject CreateAccessibilityInstance() {
260                         return base.CreateAccessibilityInstance ();
261                 }
262
263                 protected virtual void OnCheckedChanged(EventArgs e) {
264                         if (CheckedChanged != null) {
265                                 CheckedChanged(this, e);
266                         }
267                 }
268
269                 protected override void OnClick(EventArgs e) {
270                         if (auto_check) {
271                                 Checked = !Checked;
272                         }
273                 }
274
275                 protected override void OnEnter(EventArgs e) {
276                         base.OnEnter(e);
277                 }
278
279                 protected override void OnHandleCreated(EventArgs e) {
280                         base.OnHandleCreated(e);
281                 }
282
283                 protected override void OnMouseUp(MouseEventArgs mevent) {
284                         base.OnMouseUp(mevent);
285                 }
286
287                 protected override bool ProcessMnemonic(char charCode) {
288                         return base.ProcessMnemonic(charCode);
289                 }
290                 #endregion      // Protected Instance Methods
291
292                 #region Events
293                 public event EventHandler       AppearanceChanged;
294                 public event EventHandler       CheckedChanged;
295                 #endregion      // Events
296
297                 #region Internal Drawing Code
298                 internal override void Draw (PaintEventArgs pe) {
299                         if (redraw) {
300                                 ThemeEngine.Current.DrawRadioButton(this.DeviceContext, this.ClientRectangle, this);
301                                 redraw = false;
302                         }
303                 }
304                 #endregion      // Internal Drawing Code
305         }
306 }