Remove excessive shortcut key matching in ToolStrip
[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         [ClassInterface (ClassInterfaceType.AutoDispatch)]
34         [ComVisible (true)]
35         [Designer ("System.Windows.Forms.Design.ButtonBaseDesigner, " + Consts.AssemblySystem_Design,
36                    "System.ComponentModel.Design.IDesigner")]
37         public class Button : ButtonBase, IButtonControl {
38                 #region Local variables
39                 DialogResult    dialog_result;
40                 #endregion      // Local variables
41
42                 #region Public Constructors
43                 public Button ()
44                 {
45                         dialog_result = DialogResult.None;
46                         SetStyle (ControlStyles.StandardDoubleClick, false);
47                 }
48                 #endregion      // Public Constructors
49
50                 #region Public Properties
51                 [Browsable (true)]
52                 [Localizable (true)]
53                 [DefaultValue (AutoSizeMode.GrowOnly)]
54                 [MWFCategory("Layout")]
55                 public AutoSizeMode AutoSizeMode {
56                         get { return base.GetAutoSizeMode (); }
57                         set { base.SetAutoSizeMode (value); }
58                 }
59
60                 [DefaultValue (DialogResult.None)]
61                 [MWFCategory("Behavior")]
62                 public virtual DialogResult DialogResult {      // IButtonControl
63                         get { return dialog_result; }
64                         set { dialog_result = value; }
65                 }
66                 #endregion      // Public Properties
67
68                 #region Protected Properties
69                 protected override CreateParams CreateParams {
70                         get { return base.CreateParams; }
71                 }
72                 #endregion      // Protected Properties
73
74                 #region Public Methods
75                 public virtual void NotifyDefault (bool value)  // IButtonControl
76                 {       
77                         this.IsDefault = value;
78                 }
79
80                 public void PerformClick ()                     // IButtonControl
81                 {                       
82                         if (CanSelect)
83                                 OnClick (EventArgs.Empty);
84                 }
85
86                 public override string ToString ()
87                 {
88                         return base.ToString () + ", Text: " + this.Text;
89                 }
90                 #endregion      // Public Methods
91
92                 #region Protected Methods
93                 protected override void OnClick (EventArgs e)
94                 {
95                         if (dialog_result != DialogResult.None) {
96                                 Form p = FindForm ();
97
98                                 if (p != null)
99                                         p.DialogResult = dialog_result;
100                         }
101                         
102                         base.OnClick (e);
103                 }
104
105                 protected override void OnFontChanged (EventArgs e)
106                 {
107                         base.OnFontChanged (e);
108                 }
109                 
110                 protected override void OnMouseEnter (EventArgs e)
111                 {
112                         base.OnMouseEnter (e);
113                 }
114                 
115                 protected override void OnMouseLeave (EventArgs e)
116                 {
117                         base.OnMouseLeave (e);
118                 }
119
120                 protected override void OnMouseUp (MouseEventArgs mevent)
121                 {
122                         base.OnMouseUp (mevent);
123                 }
124
125                 protected override void OnTextChanged (EventArgs e)
126                 {
127                         base.OnTextChanged (e);
128                 }
129
130                 protected override bool ProcessMnemonic (char charCode)
131                 {
132                         if (this.UseMnemonic && IsMnemonic (charCode, Text) == true) {
133                                 PerformClick ();
134                                 return true;
135                         }
136
137                         return base.ProcessMnemonic (charCode);
138                 }
139
140                 protected override void WndProc (ref Message m)
141                 {
142                         base.WndProc (ref m);
143                 }
144                 #endregion      // Protected Methods
145
146                 #region Events
147                 [Browsable (false)]
148                 [EditorBrowsable (EditorBrowsableState.Advanced)]
149                 public new event EventHandler DoubleClick {
150                         add { base.DoubleClick += value; }
151                         remove { base.DoubleClick -= value; }
152                 }
153
154                 [Browsable (false)]
155                 [EditorBrowsable (EditorBrowsableState.Advanced)]
156                 public new event MouseEventHandler MouseDoubleClick {
157                         add { base.MouseDoubleClick += value; }
158                         remove { base.MouseDoubleClick -= value; }
159                 }
160                 #endregion      // Events
161
162                 #region Internal methods
163                 internal override void Draw (PaintEventArgs pevent)
164                 {
165                         // System style does not use any of the new 2.0 stuff
166                         if (this.FlatStyle == FlatStyle.System) {
167                                 base.Draw (pevent);
168                                 return;
169                         }
170
171                         // FIXME: This should be called every time something that can affect it
172                         // is changed, not every paint.  Can only change so many things at a time.
173
174                         // Figure out where our text and image should go
175                         Rectangle text_rectangle;
176                         Rectangle image_rectangle;
177
178                         ThemeEngine.Current.CalculateButtonTextAndImageLayout (this, out text_rectangle, out image_rectangle);
179
180                         // Draw our button
181                         if (this.FlatStyle == FlatStyle.Standard)
182                                 ThemeEngine.Current.DrawButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
183                         else if (this.FlatStyle == FlatStyle.Flat)
184                                 ThemeEngine.Current.DrawFlatButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
185                         else if (this.FlatStyle == FlatStyle.Popup)
186                                 ThemeEngine.Current.DrawPopupButton (pevent.Graphics, this, text_rectangle, image_rectangle, pevent.ClipRectangle);
187                 }
188
189                 internal override Size GetPreferredSizeCore (Size proposedSize)
190                 {
191                         if (this.AutoSize)
192                                 return ThemeEngine.Current.CalculateButtonAutoSize (this);
193                                 
194                         return base.GetPreferredSizeCore (proposedSize);
195                 }
196                 #endregion      // Internal methods
197         }
198 }