2 // System.Windows.Forms.RadioButton.cs
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
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Drawing.Text;
33 using System.ComponentModel;
35 namespace System.Windows.Forms {
38 // Represents a Windows radio button
41 public class RadioButton : ButtonBase {
43 Appearance appearance;
45 ContentAlignment checkAlign;
46 ContentAlignment _textAlign;
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;
62 public Appearance Appearance {
63 get { return appearance; }
65 if ( !Enum.IsDefined ( typeof(Appearance), value ) )
66 throw new InvalidEnumArgumentException( "Appearance",
70 if ( appearance != value ) {
71 int oldStyle = AppearanceStyle;
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; }
86 if ( autoCheck != value ) {
87 int oldStyle = AutoCheckStyle;
90 if ( IsHandleCreated )
91 Win32.UpdateWindowStyle ( Handle, oldStyle, AutoCheckStyle );
97 public ContentAlignment CheckAlign {
98 get { return checkAlign; }
100 if ( !Enum.IsDefined ( typeof(ContentAlignment), value ) )
101 throw new InvalidEnumArgumentException( "CheckAlign",
105 if ( checkAlign != value ) {
111 public bool Checked {
112 get { return _checked; }
114 if ( _checked != value ) {
116 OnCheckedChanged ( EventArgs.Empty );
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;
142 protected override CreateParams CreateParams {
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;
161 protected override ImeMode DefaultImeMode {
162 get { return ImeMode.Disable; }
165 protected override Size DefaultSize {
166 get { return new Size(104,24); }
170 protected override AccessibleObject CreateAccessibilityInstance()
172 throw new NotImplementedException ();
175 protected virtual void OnCheckedChanged(EventArgs e)
177 if ( CheckedChanged != null )
178 CheckedChanged ( this, e );
182 protected override void OnClick(EventArgs e)
184 if (AutoCheck && !Checked) {
186 foreach (Control ctr in Parent.Controls) {
187 RadioButton rbtn = ctr as RadioButton;
188 if (rbtn != null && rbtn != this) {
189 rbtn.Checked = false;
197 protected override void OnEnter(EventArgs e)
199 //FIXME throw new NotImplementedException ();
203 protected override void OnHandleCreated(EventArgs e)
205 base.OnHandleCreated ( e );
208 protected override void OnMouseUp(MouseEventArgs e)
210 OnClick (EventArgs.Empty);
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
238 ButtonState buttonState = ButtonState.Normal;
240 // Clear the radiobutton background
241 SolidBrush sb = new SolidBrush (BackColor);
242 paintOn.FillRectangle (sb, pevent.ClipRectangle);
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;
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));
281 ControlPaint.DrawFocusRectangle (paintOn, textRect);
284 pevent.Graphics.DrawImage(bmp, 0, 0, paintBounds.Width, paintBounds.Height);