Copied remotely
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / RadioButton.cs
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 //
10
11 //
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.
30 //
31 using System.Drawing;
32 using System.Drawing.Text;
33 using System.ComponentModel;
34
35 namespace System.Windows.Forms {
36
37         // <summary>
38         // Represents a Windows radio button
39         // </summary>
40
41     public class RadioButton : ButtonBase {
42
43                 Appearance              appearance;
44                 bool                    autoCheck;
45                 ContentAlignment        checkAlign;
46                 ContentAlignment        _textAlign;
47                 bool                    _checked;
48
49                 public RadioButton()
50                 {
51                         SubClassWndProc_ = true;
52                         SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
53                         //callWinControlProcMask &= ~(CallWinControlProcMask.MOUSE_MESSAGES | CallWinControlProcMask.KEYBOARD_MESSAGES);
54                         
55                         appearance = Appearance.Normal;
56                         checkAlign = ContentAlignment.MiddleLeft;
57                         TextAlign  = ContentAlignment.MiddleLeft;
58                         autoCheck = true;
59                         _checked = false;
60                 }
61
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));
69
70                                 if ( appearance != value ) {
71                                         int oldStyle = AppearanceStyle;
72                                         appearance = value;
73                                         
74                                         if ( IsHandleCreated )
75                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, AppearanceStyle );
76
77                                         if ( AppearanceChanged != null )
78                                                 AppearanceChanged ( this, EventArgs.Empty );
79                                 }
80                         }
81                 }
82
83                 public bool AutoCheck {
84                         get {   return autoCheck; }
85                         set {
86                                 if ( autoCheck != value ) {
87                                         int oldStyle = AutoCheckStyle;
88                                         autoCheck = value;
89
90                                         if ( IsHandleCreated )
91                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, AutoCheckStyle );
92                                 }
93                         }
94                 }
95
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));
104
105                                 if ( checkAlign != value ) {
106                                         checkAlign = value;
107                                 }
108                         }
109                 }
110
111                 public bool Checked {
112                         get {   return _checked; }
113                         set {
114                                 if ( _checked != value ) {
115                                         _checked = value;
116                                         OnCheckedChanged ( EventArgs.Empty );
117                                         Invalidate ();
118                                 }
119                         }
120                 }
121
122                 [MonoTODO]
123                 public override ContentAlignment TextAlign {
124                         get {   return _textAlign;      }
125                         set {   _textAlign = value;     }
126                 }
127
128                 public void PerformClick()
129                 {
130                         OnClick ( EventArgs.Empty );
131                 }
132
133                 public override string ToString()
134                 {
135                         return GetType().FullName.ToString () + ", Checked: " + Checked.ToString ( );
136                 }
137
138                 public event EventHandler AppearanceChanged;
139                 public event EventHandler CheckedChanged;
140
141                 [MonoTODO]
142                 protected override CreateParams CreateParams {
143                         get {
144                                 CreateParams createParams = base.CreateParams;
145  
146                                 createParams.ClassName = "BUTTON";
147
148                                 createParams.Style = (int) (
149                                         (int)WindowStyles.WS_CHILD | 
150                                         (int)WindowStyles.WS_VISIBLE |
151                                         (int)WindowStyles.WS_CLIPSIBLINGS);
152
153                                 createParams.Style |= AutoCheckStyle | AppearanceStyle;
154                                 createParams.Style |= (int)Win32.ContentAlignment2SystemButtonStyle( _textAlign );
155                                 createParams.Style |= (int)ButtonStyles.BS_OWNERDRAW;
156
157                                 return createParams;
158                         }
159                 }
160
161                 protected override ImeMode DefaultImeMode {
162                         get {   return ImeMode.Disable; }
163                 }
164
165                 protected override Size DefaultSize {
166                         get {   return new Size(104,24); }
167                 }
168
169                 [MonoTODO]
170                 protected override AccessibleObject CreateAccessibilityInstance()
171                 {
172                         throw new NotImplementedException ();
173                 }
174
175                 protected virtual void OnCheckedChanged(EventArgs e)
176                 {
177                         if ( CheckedChanged != null )
178                                 CheckedChanged ( this, e );
179                 }
180
181                 [MonoTODO]
182                 protected override void OnClick(EventArgs e)
183                 {
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;
190                                         }
191                                 }
192                         }
193                         base.OnClick ( e );
194                 }
195
196                 [MonoTODO]
197                 protected override void OnEnter(EventArgs e)
198                 {
199 //FIXME                 throw new NotImplementedException ();
200                 }
201
202                 [MonoTODO]
203                 protected override void OnHandleCreated(EventArgs e)
204                 {
205                         base.OnHandleCreated ( e );
206                 }
207
208                 protected override void OnMouseUp(MouseEventArgs e) 
209                 {
210                         OnClick (EventArgs.Empty);
211                         base.OnMouseUp(e);
212                 }
213                 
214                 [MonoTODO]
215                 protected override bool ProcessMnemonic(char charCode)
216                 {
217                         throw new NotImplementedException ();
218                 }
219
220                 private int AutoCheckStyle
221                 {
222                         get { return (int) ( AutoCheck ?  ButtonStyles.BS_AUTORADIOBUTTON : ButtonStyles.BS_RADIOBUTTON ); }
223                 }
224
225                 private int AppearanceStyle
226                 {
227                         get { return (int) ( Appearance == Appearance.Normal ? 0 : ButtonStyles.BS_PUSHLIKE ); }
228                 }
229
230                 internal override void ButtonPaint (PaintEventArgs pevent) 
231                 {
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;
239                         
240                         // Clear the radiobutton background
241                         SolidBrush sb = new SolidBrush (BackColor);
242                         paintOn.FillRectangle (sb, pevent.ClipRectangle);
243                         sb.Dispose ();
244                         
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);
248
249                         if (0 != (CheckAlign & (ContentAlignment.BottomLeft | ContentAlignment.BottomCenter | ContentAlignment.BottomRight))) {
250                                 checkRect.Y = paintBounds.Bottom - CheckSize;
251                         }
252                         else if(0 != (CheckAlign & (ContentAlignment.MiddleLeft | ContentAlignment.MiddleCenter | ContentAlignment.MiddleRight))) {
253                                 checkRect.Y = paintBounds.Top + paintBounds.Height / 2 - CheckSize / 2;
254                         }
255                         
256                         if (0 != (CheckAlign & (ContentAlignment.TopRight | ContentAlignment.MiddleRight | ContentAlignment.BottomRight))) {
257                                 checkRect.X = paintBounds.Right - CheckSize;
258                                 textRect.X = paintBounds.Left;
259                         }
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;
264                         }
265                         
266                         if (FlatStyle == FlatStyle.Flat) {
267                                 buttonState |= ButtonState.Flat;
268                         }
269                         
270                         if (Checked) {
271                                 buttonState |= ButtonState.Checked;
272                         }
273                         
274                         ControlPaint.DrawRadioButton (paintOn, checkRect, buttonState);
275                         
276                         sb=new SolidBrush(ForeColor);
277                         paintOn.DrawString(Text, Font, sb, textRect, Win32.ContentAlignment2StringFormat(_textAlign, HotkeyPrefix.Show));
278                         sb.Dispose();
279                         
280                         if (Focused) {
281                                 ControlPaint.DrawFocusRectangle (paintOn, textRect);
282                         }
283                         
284                         pevent.Graphics.DrawImage(bmp, 0, 0, paintBounds.Width, paintBounds.Height);
285                         paintOn.Dispose ();
286                         bmp.Dispose();
287                 }
288          }
289 }