**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / RadioButton.cs
blob78d981991a9cb3e9952d1ae8c2084a422a929400
1 //
2 // System.Windows.Forms.RadioButton.cs
3 //
4 // Author:
5 // stubbed out by Daniel Carrera (dcarrera@math.toronto.edu)
6 // Dennis Hayes (dennish@raytek.com)
7 // implemented by Aleksey Ryabchuk (ryabchuk@yahoo.com)
8 // (C) 2002/3 Ximian, Inc
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Drawing;
32 using System.Drawing.Text;
33 using System.ComponentModel;
35 namespace System.Windows.Forms {
37 // <summary>
38 // Represents a Windows radio button
39 // </summary>
41 public class RadioButton : ButtonBase {
43 Appearance appearance;
44 bool autoCheck;
45 ContentAlignment checkAlign;
46 ContentAlignment _textAlign;
47 bool _checked;
49 public RadioButton()
51 SubClassWndProc_ = true;
52 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
53 //callWinControlProcMask &= ~(CallWinControlProcMask.MOUSE_MESSAGES | CallWinControlProcMask.KEYBOARD_MESSAGES);
55 appearance = Appearance.Normal;
56 checkAlign = ContentAlignment.MiddleLeft;
57 TextAlign = ContentAlignment.MiddleLeft;
58 autoCheck = true;
59 _checked = false;
62 public Appearance Appearance {
63 get { return appearance; }
64 set {
65 if ( !Enum.IsDefined ( typeof(Appearance), value ) )
66 throw new InvalidEnumArgumentException( "Appearance",
67 (int)value,
68 typeof(Appearance));
70 if ( appearance != value ) {
71 int oldStyle = AppearanceStyle;
72 appearance = value;
74 if ( IsHandleCreated )
75 Win32.UpdateWindowStyle ( Handle, oldStyle, AppearanceStyle );
77 if ( AppearanceChanged != null )
78 AppearanceChanged ( this, EventArgs.Empty );
83 public bool AutoCheck {
84 get { return autoCheck; }
85 set {
86 if ( autoCheck != value ) {
87 int oldStyle = AutoCheckStyle;
88 autoCheck = value;
90 if ( IsHandleCreated )
91 Win32.UpdateWindowStyle ( Handle, oldStyle, AutoCheckStyle );
96 [MonoTODO]
97 public ContentAlignment CheckAlign {
98 get { return checkAlign; }
99 set {
100 if ( !Enum.IsDefined ( typeof(ContentAlignment), value ) )
101 throw new InvalidEnumArgumentException( "CheckAlign",
102 (int)value,
103 typeof(Appearance));
105 if ( checkAlign != value ) {
106 checkAlign = value;
111 public bool Checked {
112 get { return _checked; }
113 set {
114 if ( _checked != value ) {
115 _checked = value;
116 OnCheckedChanged ( EventArgs.Empty );
117 Invalidate ();
122 [MonoTODO]
123 public override ContentAlignment TextAlign {
124 get { return _textAlign; }
125 set { _textAlign = value; }
128 public void PerformClick()
130 OnClick ( EventArgs.Empty );
133 public override string ToString()
135 return GetType().FullName.ToString () + ", Checked: " + Checked.ToString ( );
138 public event EventHandler AppearanceChanged;
139 public event EventHandler CheckedChanged;
141 [MonoTODO]
142 protected override CreateParams CreateParams {
143 get {
144 CreateParams createParams = base.CreateParams;
146 createParams.ClassName = "BUTTON";
148 createParams.Style = (int) (
149 (int)WindowStyles.WS_CHILD |
150 (int)WindowStyles.WS_VISIBLE |
151 (int)WindowStyles.WS_CLIPSIBLINGS);
153 createParams.Style |= AutoCheckStyle | AppearanceStyle;
154 createParams.Style |= (int)Win32.ContentAlignment2SystemButtonStyle( _textAlign );
155 createParams.Style |= (int)ButtonStyles.BS_OWNERDRAW;
157 return createParams;
161 protected override ImeMode DefaultImeMode {
162 get { return ImeMode.Disable; }
165 protected override Size DefaultSize {
166 get { return new Size(104,24); }
169 [MonoTODO]
170 protected override AccessibleObject CreateAccessibilityInstance()
172 throw new NotImplementedException ();
175 protected virtual void OnCheckedChanged(EventArgs e)
177 if ( CheckedChanged != null )
178 CheckedChanged ( this, e );
181 [MonoTODO]
182 protected override void OnClick(EventArgs e)
184 if (AutoCheck && !Checked) {
185 Checked = true;
186 foreach (Control ctr in Parent.Controls) {
187 RadioButton rbtn = ctr as RadioButton;
188 if (rbtn != null && rbtn != this) {
189 rbtn.Checked = false;
193 base.OnClick ( e );
196 [MonoTODO]
197 protected override void OnEnter(EventArgs e)
199 //FIXME throw new NotImplementedException ();
202 [MonoTODO]
203 protected override void OnHandleCreated(EventArgs e)
205 base.OnHandleCreated ( e );
208 protected override void OnMouseUp(MouseEventArgs e)
210 OnClick (EventArgs.Empty);
211 base.OnMouseUp(e);
214 [MonoTODO]
215 protected override bool ProcessMnemonic(char charCode)
217 throw new NotImplementedException ();
220 private int AutoCheckStyle
222 get { return (int) ( AutoCheck ? ButtonStyles.BS_AUTORADIOBUTTON : ButtonStyles.BS_RADIOBUTTON ); }
225 private int AppearanceStyle
227 get { return (int) ( Appearance == Appearance.Normal ? 0 : ButtonStyles.BS_PUSHLIKE ); }
230 internal override void ButtonPaint (PaintEventArgs pevent)
232 Rectangle paintBounds = ClientRectangle;
233 Bitmap bmp = new Bitmap( paintBounds.Width, paintBounds.Height, pevent.Graphics);
234 Graphics paintOn = Graphics.FromImage(bmp);
235 int CheckSize = 12; // Might not be correct
236 Rectangle checkRect;
237 Rectangle textRect;
238 ButtonState buttonState = ButtonState.Normal;
240 // Clear the radiobutton background
241 SolidBrush sb = new SolidBrush (BackColor);
242 paintOn.FillRectangle (sb, pevent.ClipRectangle);
243 sb.Dispose ();
245 // Location of button and text
246 checkRect = new Rectangle (paintBounds.Left, paintBounds.Top, CheckSize, CheckSize);
247 textRect = new Rectangle (checkRect.Right + 3, paintBounds.Top, paintBounds.Width - checkRect.Width - 4, paintBounds.Height);
249 if (0 != (CheckAlign & (ContentAlignment.BottomLeft | ContentAlignment.BottomCenter | ContentAlignment.BottomRight))) {
250 checkRect.Y = paintBounds.Bottom - CheckSize;
252 else if(0 != (CheckAlign & (ContentAlignment.MiddleLeft | ContentAlignment.MiddleCenter | ContentAlignment.MiddleRight))) {
253 checkRect.Y = paintBounds.Top + paintBounds.Height / 2 - CheckSize / 2;
256 if (0 != (CheckAlign & (ContentAlignment.TopRight | ContentAlignment.MiddleRight | ContentAlignment.BottomRight))) {
257 checkRect.X = paintBounds.Right - CheckSize;
258 textRect.X = paintBounds.Left;
260 else if(0 != (CheckAlign & (ContentAlignment.TopCenter | ContentAlignment.MiddleCenter | ContentAlignment.BottomCenter))) {
261 checkRect.X = paintBounds.Left + paintBounds.Width / 2 - CheckSize / 2;
262 textRect.X = paintBounds.Left;
263 textRect.Width = paintBounds.Width;
266 if (FlatStyle == FlatStyle.Flat) {
267 buttonState |= ButtonState.Flat;
270 if (Checked) {
271 buttonState |= ButtonState.Checked;
274 ControlPaint.DrawRadioButton (paintOn, checkRect, buttonState);
276 sb=new SolidBrush(ForeColor);
277 paintOn.DrawString(Text, Font, sb, textRect, Win32.ContentAlignment2StringFormat(_textAlign, HotkeyPrefix.Show));
278 sb.Dispose();
280 if (Focused) {
281 ControlPaint.DrawFocusRectangle (paintOn, textRect);
284 pevent.Graphics.DrawImage(bmp, 0, 0, paintBounds.Width, paintBounds.Height);
285 paintOn.Dispose ();
286 bmp.Dispose();