2007-03-02 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Button.cs
1 //
2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
9 // 
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
12 // 
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 // Copyright (c) 2004-2005 Novell, Inc.
22 //
23 // Authors:
24 //      Peter Bartok    pbartok@novell.com
25 //
26
27 using System.ComponentModel;
28 using System.Drawing;
29 using System.Drawing.Text;
30 using System.Runtime.InteropServices;
31
32 namespace System.Windows.Forms {
33 #if NET_2_0
34         [ClassInterface (ClassInterfaceType.AutoDispatch)]
35         [ComVisible (true)]
36         [Designer ("System.Windows.Forms.Design.ButtonBaseDesigner, " + Consts.AssemblySystem_Design,
37                    "System.ComponentModel.Design.IDesigner")]
38 #endif
39         public class Button : ButtonBase, IButtonControl {
40                 #region Local variables
41                 DialogResult    dialog_result;
42                 #endregion      // Local variables
43
44                 #region Public Constructors
45                 public Button() {
46                         dialog_result = DialogResult.None;
47                         SetStyle(ControlStyles.StandardDoubleClick, false);
48                 }
49                 #endregion      // Public Constructors
50
51                 #region Internal methods
52                 internal override void HaveDoubleClick() {
53                         EventHandler eh = (EventHandler)(Events [DoubleClickEvent]);
54                         if (eh != null)
55                                 eh (this, EventArgs.Empty);
56                 }
57                 
58 #if NET_2_0
59                 internal override void Draw (PaintEventArgs pevent)
60                 {
61                         // System style does not use any of the new 2.0 stuff
62                         if (this.FlatStyle == FlatStyle.System) {
63                                 base.Draw (pevent);
64                                 return;
65                         }
66
67                         // FIXME: This should be called every time something that can affect it
68                         // is changed, not every paint.  Can only change so many things at a time.
69
70                         // Figure out where our text and image should go
71                         Rectangle text_rectangle;
72                         Rectangle image_rectangle;
73
74                         ThemeEngine.Current.CalculateButtonTextAndImageLayout (this, out text_rectangle, out image_rectangle);
75
76                         // Draw our button
77                         if (this.FlatStyle == FlatStyle.Standard)
78                                 ThemeEngine.Current.DrawButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
79                         else if (this.FlatStyle == FlatStyle.Flat)
80                                 ThemeEngine.Current.DrawFlatButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
81                         else if (this.FlatStyle == FlatStyle.Popup)
82                                 ThemeEngine.Current.DrawPopupButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
83                 }
84 #endif
85                 #endregion      // Internal methods
86
87                 #region Public Instance Properties
88                 [DefaultValue(DialogResult.None)]
89                 public virtual DialogResult DialogResult {              // IButtonControl
90                         get {
91                                 return dialog_result;
92                         }
93                         set {
94                                 dialog_result = value;
95                         }
96                 }
97                 #endregion      // Public Instance Properties
98
99                 #region Protected Instance Properties
100                 protected override CreateParams CreateParams {
101                         get {
102                                 return base.CreateParams;
103                         }
104                 }
105                 #endregion      // Protected Instance Properties
106
107                 #region Public Instance Methods
108                 public virtual void NotifyDefault(bool value) { // IButtonControl
109                         this.IsDefault = value;
110                 }
111
112                 public void PerformClick() {                    // IButtonControl
113                         if (CanSelect)
114                                 OnClick(EventArgs.Empty);
115                 }
116
117                 public override string ToString() {
118                         return base.ToString() + ", Text: " + this.Text;
119                 }
120                 #endregion      // Public Instance Methods
121
122                 #region Protected Instance Methods
123                 protected override void OnClick(EventArgs e) {
124                         if (dialog_result != DialogResult.None) {
125                                 Form p = FindForm ();
126
127                                 if (p != null) {
128                                         p.DialogResult = dialog_result;
129                                 }
130                         }
131                         base.OnClick(e);
132                 }
133
134                 protected override void OnMouseUp(MouseEventArgs e) {
135                         base.OnMouseUp (e);
136                 }
137
138                 protected override bool ProcessMnemonic(char charCode) {
139                         if (this.UseMnemonic && IsMnemonic(charCode, Text) == true) {
140                                 PerformClick();
141                                 return true;
142                         }
143                         
144                         return base.ProcessMnemonic(charCode);
145                 }
146
147                 protected override void WndProc(ref Message m) {
148                         base.WndProc (ref m);
149                 }
150                 #endregion      // Protected Instance Methods
151
152                 #region Events
153                 static object DoubleClickEvent = new object ();
154
155                 // XXX this and HaveDoubleClick above need to be
156                 // looked at.. it's far more likely that this just
157                 // uses base.DoubleClick, and HaveDoubleClick goes
158                 // away in favor of calls to OnDoubleClick.
159                 [Browsable(false)]
160                 [EditorBrowsable (EditorBrowsableState.Advanced)]
161                 public new event EventHandler DoubleClick {
162                         add { Events.AddHandler (DoubleClickEvent, value); }
163                         remove { Events.RemoveHandler (DoubleClickEvent, value); }
164                 }
165                 
166 #if NET_2_0
167                 public new event MouseEventHandler MouseDoubleClick {
168                         add { base.MouseDoubleClick += value; }
169                         remove { base.MouseDoubleClick -= value; }
170                 }
171 #endif
172                 #endregion      // Events
173                 
174 #if NET_2_0
175                 protected override void OnFontChanged (EventArgs e)
176                 {
177                         base.OnFontChanged (e);
178                 }
179                 
180                 protected override void OnMouseEnter (EventArgs e)
181                 {
182                         base.OnMouseEnter (e);
183                 }
184                 
185                 protected override void OnMouseLeave (EventArgs e)
186                 {
187                         base.OnMouseLeave (e);
188                 }
189
190                 protected override void OnTextChanged (EventArgs e)
191                 {
192                         base.OnTextChanged (e);
193                 }
194 #endif
195         }
196 }