2008-12-02 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RadioButton.cs
index ace406fa54eb379010e0aed7cbb38bbaf572478c..0ef26cc1576ee1428cd2590e1c9de0c2ccafb348 100644 (file)
@@ -23,8 +23,6 @@
 //     Peter Bartok    pbartok@novell.com
 //
 
-// COMPLETE
-
 using System.ComponentModel;
 using System.Drawing;
 using System.Drawing.Text;
@@ -33,6 +31,13 @@ using System.Runtime.InteropServices;
 namespace System.Windows.Forms {
        [DefaultProperty("Checked")]
        [DefaultEvent("CheckedChanged")]
+#if NET_2_0
+       [ClassInterface (ClassInterfaceType.AutoDispatch)]
+       [ComVisible (true)]
+       [DefaultBindingProperty ("Checked")]
+       [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts.AssemblySystem_Design)]
+       [Designer ("System.Windows.Forms.Design.RadioButtonDesigner, " + Consts.AssemblySystem_Design)]
+#endif
        public class RadioButton : ButtonBase {
                #region Local Variables
                internal Appearance             appearance;
@@ -43,9 +48,13 @@ namespace System.Windows.Forms {
 
                #region RadioButtonAccessibleObject Subclass
                [ComVisible(true)]
+#if NET_2_0
+               public class RadioButtonAccessibleObject : ButtonBaseAccessibleObject {
+#else
                public class RadioButtonAccessibleObject : ControlAccessibleObject {
+#endif
                        #region RadioButtonAccessibleObject Local Variables
-                       private RadioButton     owner;
+                       private new RadioButton owner;
                        #endregion      // RadioButtonAccessibleObject Local Variables
 
                        #region RadioButtonAccessibleObject Constructors
@@ -103,9 +112,8 @@ namespace System.Windows.Forms {
                        appearance = Appearance.Normal;
                        auto_check = true;
                        radiobutton_alignment = ContentAlignment.MiddleLeft;
-                       text_alignment = ContentAlignment.MiddleLeft;
-                       tab_stop = false;
-                       GotFocus +=new EventHandler(ReceivedFocus);
+                       TextAlign = ContentAlignment.MiddleLeft;
+                       TabStop = false;
                }
                #endregion      // Public Constructors
 
@@ -118,13 +126,13 @@ namespace System.Windows.Forms {
                        }
 
                        // Remove tabstop property from and uncheck our radio-button siblings
-                       c = this.parent;
+                       c = this.Parent;
                        if (c != null) {
-                               for (int i = 0; i < c.child_controls.Count; i++) {
-                                       if ((this != c.child_controls[i]) && (c.child_controls[i] is RadioButton)) {
-                                               if (((RadioButton)(c.child_controls[i])).auto_check) {
-                                                       c.child_controls[i].TabStop = false;
-                                                       ((RadioButton)(c.child_controls[i])).Checked = false;
+                               for (int i = 0; i < c.Controls.Count; i++) {
+                                       if ((this != c.Controls[i]) && (c.Controls[i] is RadioButton)) {
+                                               if (((RadioButton)(c.Controls[i])).auto_check) {
+                                                       c.Controls[i].TabStop = false;
+                                                       ((RadioButton)(c.Controls[i])).Checked = false;
                                                }
                                        }
                                }
@@ -133,17 +141,37 @@ namespace System.Windows.Forms {
                        this.TabStop = true;
                }
 
-               internal override void HaveDoubleClick() {
-                       if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
-               }
-
                internal override void Draw (PaintEventArgs pe) {
+#if NET_2_0
+                       // FIXME: This should be called every time something that can affect it
+                       // is changed, not every paint.  Can only change so many things at a time.
+
+                       // Figure out where our text and image should go
+                       Rectangle glyph_rectangle;
+                       Rectangle text_rectangle;
+                       Rectangle image_rectangle;
+
+                       ThemeEngine.Current.CalculateRadioButtonTextAndImageLayout (this, Point.Empty, out glyph_rectangle, out text_rectangle, out image_rectangle);
+
+                       // Draw our button
+                       if (FlatStyle != FlatStyle.System)
+                               ThemeEngine.Current.DrawRadioButton (pe.Graphics, this, glyph_rectangle, text_rectangle, image_rectangle, pe.ClipRectangle);
+                       else
+                               ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
+#else
                        ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
+#endif
                }
 
-               private void ReceivedFocus(object sender, EventArgs e) {
-                       OnClick(e);
+#if NET_2_0
+               internal override Size GetPreferredSizeCore (Size proposedSize)
+               {
+                       if (this.AutoSize)
+                               return ThemeEngine.Current.CalculateRadioButtonAutoSize (this);
+
+                       return base.GetPreferredSizeCore (proposedSize);
                }
+#endif
                #endregion      // Private Methods
 
                #region Public Instance Properties
@@ -157,10 +185,12 @@ namespace System.Windows.Forms {
                        set {
                                if (value != appearance) {
                                        appearance = value;
-                                       if (AppearanceChanged != null) {
-                                               AppearanceChanged(this, EventArgs.Empty);
-                                       }
-                                       Redraw();
+                                       EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
+                                       if (eh != null)
+                                               eh (this, EventArgs.Empty);
+                                       if (Parent != null)
+                                               Parent.PerformLayout (this, "Appearance");
+                                       Invalidate();
                                }
                        }
                }
@@ -176,7 +206,9 @@ namespace System.Windows.Forms {
                        }
                }
 
+#if !NET_2_0
                [Bindable(true)]
+#endif
                [Localizable(true)]
                [DefaultValue(ContentAlignment.MiddleLeft)]
                public ContentAlignment CheckAlign {
@@ -188,12 +220,16 @@ namespace System.Windows.Forms {
                                if (value != radiobutton_alignment) {
                                        radiobutton_alignment = value;
 
-                                       Redraw();
+                                       Invalidate();
                                }
                        }
                }
 
                [DefaultValue(false)]
+#if NET_2_0
+               [SettingsBindable (true)]
+               [Bindable (true, BindingDirection.OneWay)]
+#endif
                public bool Checked {
                        get {
                                if (check_state != CheckState.Unchecked) {
@@ -206,11 +242,11 @@ namespace System.Windows.Forms {
                                if (value && (check_state != CheckState.Checked)) {
                                        UpdateSiblings();
                                        check_state = CheckState.Checked;
-                                       Redraw();
+                                       Invalidate();
                                        OnCheckedChanged(EventArgs.Empty);
                                } else if (!value && (check_state != CheckState.Unchecked)) {
                                        check_state = CheckState.Unchecked;
-                                       Redraw();
+                                       Invalidate();
                                        OnCheckedChanged(EventArgs.Empty);
                                }
                        }
@@ -218,28 +254,15 @@ namespace System.Windows.Forms {
 
                [DefaultValue(false)]
                public new bool TabStop {
-                       get {
-                               return tab_stop;
-                       }
-
-                       set {
-                               tab_stop = value;
-                       }
+                       get { return base.TabStop; }
+                       set { base.TabStop = value; }
                }
 
                [DefaultValue(ContentAlignment.MiddleLeft)]
                [Localizable(true)]
                public override ContentAlignment TextAlign {
-                       get {
-                               return text_alignment;
-                       }
-
-                       set {
-                               if (value != text_alignment) {
-                                       text_alignment = value;
-                                       Redraw();
-                               }
-                       }
+                       get { return base.TextAlign; }
+                       set { base.TextAlign = value; }
                }
                #endregion      // Public Instance Properties
 
@@ -281,9 +304,9 @@ namespace System.Windows.Forms {
                }
 
                protected virtual void OnCheckedChanged(EventArgs e) {
-                       if (CheckedChanged != null) {
-                               CheckedChanged(this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected override void OnClick(EventArgs e) {
@@ -322,12 +345,34 @@ namespace System.Windows.Forms {
                #endregion      // Protected Instance Methods
 
                #region Events
-               public event EventHandler       AppearanceChanged;
-               public event EventHandler       CheckedChanged;
+               static object AppearanceChangedEvent = new object ();
+               static object CheckedChangedEvent = new object ();
+
+               public event EventHandler AppearanceChanged {
+                       add { Events.AddHandler (AppearanceChangedEvent, value); }
+                       remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
+               }
+
+               public event EventHandler CheckedChanged {
+                       add { Events.AddHandler (CheckedChangedEvent, value); }
+                       remove { Events.RemoveHandler (CheckedChangedEvent, value); }
+               }
 
                [Browsable(false)]
                [EditorBrowsable (EditorBrowsableState.Never)]
-               public event EventHandler       DoubleClick;
+               public new event EventHandler DoubleClick {
+                       add { base.DoubleClick += value; }
+                       remove { base.DoubleClick -= value; }
+               }
+               
+#if NET_2_0
+               [Browsable (false)]
+               [EditorBrowsable (EditorBrowsableState.Never)]
+               public new event MouseEventHandler MouseDoubleClick { 
+                       add { base.MouseDoubleClick += value; }
+                       remove { base.MouseDoubleClick -= value; }
+               }
+#endif
                #endregion      // Events
        }
 }