* Control.cs: ControlCollection.Count must be public. Fixed build of
[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                 #endregion      // Internal methods
58
59                 #region Public Instance Properties
60                 [DefaultValue(DialogResult.None)]
61                 public virtual DialogResult DialogResult {              // IButtonControl
62                         get {
63                                 return dialog_result;
64                         }
65                         set {
66                                 dialog_result = value;
67                         }
68                 }
69                 #endregion      // Public Instance Properties
70
71                 #region Protected Instance Properties
72                 protected override CreateParams CreateParams {
73                         get {
74                                 return base.CreateParams;
75                         }
76                 }
77                 #endregion      // Protected Instance Properties
78
79                 #region Public Instance Methods
80                 public virtual void NotifyDefault(bool value) { // IButtonControl
81                         this.IsDefault = value;
82                 }
83
84                 public void PerformClick() {                    // IButtonControl
85                         if (CanSelect)
86                                 OnClick(EventArgs.Empty);
87                 }
88
89                 public override string ToString() {
90                         return base.ToString() + ", Text: " + this.Text;
91                 }
92                 #endregion      // Public Instance Methods
93
94                 #region Protected Instance Methods
95                 protected override void OnClick(EventArgs e) {
96                         if (dialog_result != DialogResult.None) {
97                                 Form p = Parent as Form;
98
99                                 if (p != null) {
100                                         p.DialogResult = dialog_result;
101                                 }
102                         }
103                         base.OnClick(e);
104                 }
105
106                 protected override void OnMouseUp(MouseEventArgs e) {
107                         base.OnMouseUp (e);
108                 }
109
110                 protected override bool ProcessMnemonic(char charCode) {
111                         if (IsMnemonic(charCode, Text) == true) {
112                                 PerformClick();
113                                 return true;
114                         }
115                         
116                         return base.ProcessMnemonic(charCode);
117                 }
118
119                 protected override void WndProc(ref Message m) {
120                         base.WndProc (ref m);
121                 }
122                 #endregion      // Protected Instance Methods
123
124                 #region Events
125                 static object DoubleClickEvent = new object ();
126
127                 // XXX this and HaveDoubleClick above need to be
128                 // looked at.. it's far more likely that this just
129                 // uses base.DoubleClick, and HaveDoubleClick goes
130                 // away in favor of calls to OnDoubleClick.
131                 [Browsable(false)]
132                 [EditorBrowsable (EditorBrowsableState.Advanced)]
133                 public new event EventHandler DoubleClick {
134                         add { Events.AddHandler (DoubleClickEvent, value); }
135                         remove { Events.RemoveHandler (DoubleClickEvent, value); }
136                 }
137                 #endregion      // Events
138 #if NET_2_0
139                 protected override void OnMouseEnter (EventArgs e)
140                 {
141                         base.OnMouseEnter (e);
142                 }
143                 
144                 protected override void OnMouseLeave (EventArgs e)
145                 {
146                         base.OnMouseLeave (e);
147                 }
148
149                 protected override void OnTextChanged (EventArgs e)
150                 {
151                         base.OnTextChanged (e);
152                 }
153 #endif
154         }
155 }