2007-09-28 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Button.cs
index 28f942f7febbd668aa31f0e4c4d9a1c07f2671c3..8772aa4e6888260efd8e619e2ab86a74601b92b7 100644 (file)
 //     Peter Bartok    pbartok@novell.com
 //
 
-// COMPLETE
-
 using System.ComponentModel;
 using System.Drawing;
 using System.Drawing.Text;
+using System.Runtime.InteropServices;
 
 namespace System.Windows.Forms {
+#if NET_2_0
+       [ClassInterface (ClassInterfaceType.AutoDispatch)]
+       [ComVisible (true)]
+       [Designer ("System.Windows.Forms.Design.ButtonBaseDesigner, " + Consts.AssemblySystem_Design,
+                  "System.ComponentModel.Design.IDesigner")]
+#endif
        public class Button : ButtonBase, IButtonControl {
                #region Local variables
                DialogResult    dialog_result;
                #endregion      // Local variables
 
                #region Public Constructors
-               public Button() {
+               public Button ()
+               {
                        dialog_result = DialogResult.None;
-                       SetStyle(ControlStyles.StandardDoubleClick, false);
+                       SetStyle (ControlStyles.StandardDoubleClick, false);
                }
                #endregion      // Public Constructors
 
-               #region Internal methods
-               internal override void HaveDoubleClick() {
-                       if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
+               #region Public Properties
+#if NET_2_0
+               [Browsable (true)]
+               [Localizable (true)]
+               [DefaultValue (AutoSizeMode.GrowOnly)]
+               public AutoSizeMode AutoSizeMode {
+                       get { return base.GetAutoSizeMode (); }
+                       set { base.SetAutoSizeMode (value); }
                }
-               #endregion      // Internal methods
+#endif
 
-               #region Public Instance Properties
-               [DefaultValue(DialogResult.None)]
-               public virtual DialogResult DialogResult {              // IButtonControl
-                       get {
-                               return dialog_result;
-                       }
-                       set {
-                               dialog_result = value;
-                       }
+               [DefaultValue (DialogResult.None)]
+               public virtual DialogResult DialogResult {      // IButtonControl
+                       get { return dialog_result; }
+                       set { dialog_result = value; }
                }
-               #endregion      // Public Instance Properties
+               #endregion      // Public Properties
 
-               #region Protected Instance Properties
+               #region Protected Properties
                protected override CreateParams CreateParams {
-                       get {
-                               return base.CreateParams;
-                       }
+                       get { return base.CreateParams; }
                }
-               #endregion      // Protected Instance Properties
+               #endregion      // Protected Properties
 
-               #region Public Instance Methods
-               public virtual void NotifyDefault(bool value) { // IButtonControl
+               #region Public Methods
+               public virtual void NotifyDefault (bool value)  // IButtonControl
+               {       
                        this.IsDefault = value;
                }
 
-               public void PerformClick() {                    // IButtonControl
-                       OnClick(EventArgs.Empty);
+               public void PerformClick ()                     // IButtonControl
+               {                       
+                       if (CanSelect)
+                               OnClick (EventArgs.Empty);
                }
 
-               public override string ToString() {
-                       return base.ToString() + ", Text: " + this.text;
+               public override string ToString ()
+               {
+                       return base.ToString () + ", Text: " + this.Text;
                }
-               #endregion      // Public Instance Methods
+               #endregion      // Public Methods
 
-               #region Protected Instance Methods
-               protected override void OnClick(EventArgs e) {
+               #region Protected Methods
+               protected override void OnClick (EventArgs e)
+               {
                        if (dialog_result != DialogResult.None) {
-                               Form p = Parent as Form;
+                               Form p = FindForm ();
 
-                               if (p != null) {
+                               if (p != null)
                                        p.DialogResult = dialog_result;
-                               }
                        }
-                       base.OnClick(e);
+                       
+                       base.OnClick (e);
                }
 
-               protected override void OnMouseUp(MouseEventArgs e) {
+#if NET_2_0
+               protected override void OnFontChanged (EventArgs e)
+               {
+                       base.OnFontChanged (e);
+               }
+               
+               protected override void OnMouseEnter (EventArgs e)
+               {
+                       base.OnMouseEnter (e);
+               }
+               
+               protected override void OnMouseLeave (EventArgs e)
+               {
+                       base.OnMouseLeave (e);
+               }
+#endif
+
+               protected override void OnMouseUp (MouseEventArgs e)
+               {
                        base.OnMouseUp (e);
                }
 
-               protected override bool ProcessMnemonic(char charCode) {
-                       if (IsMnemonic(charCode, Text) == true) {
-                               PerformClick();
+#if NET_2_0
+               protected override void OnTextChanged (EventArgs e)
+               {
+                       base.OnTextChanged (e);
+               }
+#endif
+
+               protected override bool ProcessMnemonic (char charCode)
+               {
+                       if (this.UseMnemonic && IsMnemonic (charCode, Text) == true) {
+                               PerformClick ();
                                return true;
                        }
-                       
-                       return base.ProcessMnemonic(charCode);
+
+                       return base.ProcessMnemonic (charCode);
                }
 
-               protected override void WndProc(ref Message m) {
+               protected override void WndProc (ref Message m)
+               {
                        base.WndProc (ref m);
                }
-               #endregion      // Protected Instance Methods
+               #endregion      // Protected Methods
 
                #region Events
-               [Browsable(false)]
+               [Browsable (false)]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
-               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.Advanced)]
+               public new event MouseEventHandler MouseDoubleClick {
+                       add { base.MouseDoubleClick += value; }
+                       remove { base.MouseDoubleClick -= value; }
+               }
+#endif
                #endregion      // Events
+
+               #region Internal methods
+#if NET_2_0
+               internal override void Draw (PaintEventArgs pevent)
+               {
+                       // System style does not use any of the new 2.0 stuff
+                       if (this.FlatStyle == FlatStyle.System) {
+                               base.Draw (pevent);
+                               return;
+                       }
+
+                       // 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 text_rectangle;
+                       Rectangle image_rectangle;
+
+                       ThemeEngine.Current.CalculateButtonTextAndImageLayout (this, out text_rectangle, out image_rectangle);
+
+                       // Draw our button
+                       if (this.FlatStyle == FlatStyle.Standard)
+                               ThemeEngine.Current.DrawButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
+                       else if (this.FlatStyle == FlatStyle.Flat)
+                               ThemeEngine.Current.DrawFlatButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
+                       else if (this.FlatStyle == FlatStyle.Popup)
+                               ThemeEngine.Current.DrawPopupButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
+               }
+
+               internal override Size GetPreferredSizeCore (Size proposedSize)
+               {
+                       if (this.AutoSize)
+                               return ThemeEngine.Current.CalculateButtonAutoSize (this);
+                               
+                       return base.GetPreferredSizeCore (proposedSize);
+               }
+#endif
+               #endregion      // Internal methods
        }
 }