Copied remotely
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / ButtonBase.cs
1 //
2 // System.Windows.Forms.ButtonBase.cs
3 //
4 // Author:
5 //   stubbed out by Jaak Simm (jaaksimm@firm.ee)
6 //        implemented for Gtk+ by Rachel Hestilow (hestilow@ximian.com)
7 //   Dennis Hayes (dennish@Raytek.com)
8 //   WINELib implementation started by John Sohn (jsohn@columbus.rr.com)
9 //   Alexandre Pigokine (pigolkine@gmx.de)
10 //
11 // (C) Ximian, Inc., 2002/3
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.ComponentModel;
36 using System.Drawing;
37
38 namespace System.Windows.Forms {
39
40         /// <summary>
41         /// Implements the basic functionality common to button controls.
42         /// </summary>
43
44         public abstract class ButtonBase : Control {
45
46                 // private fields
47                 FlatStyle flatStyle;
48                 Image image;
49                 ContentAlignment imageAlign;
50                 int imageIndex;
51                 ContentAlignment textAlign;
52                 ImeMode imeMode;
53                 bool isDefault;
54                 Label label;
55                 bool isPushed;
56
57 //              
58 //              // --- Constructor ---
59                 protected ButtonBase() : base() 
60                 {
61                         flatStyle = FlatStyle.Standard;
62                         image = null;
63                         imageAlign = ContentAlignment.MiddleCenter;
64                         imageIndex = -1;
65                         textAlign = ContentAlignment.MiddleCenter;
66                         imeMode = ImeMode.Inherit;
67                         isDefault = false;
68                         isPushed = false;
69                 }
70
71                 // --- Properties ---
72                 protected override CreateParams CreateParams {
73                         get { return base.CreateParams; }
74                 }
75                 
76                 protected override ImeMode DefaultImeMode {
77                         get {
78                                 return ImeMode.Inherit;
79                         }
80                 }
81                 
82                 protected override Size DefaultSize {
83                         get {
84                                 return new Size(75,23);// default size for button.
85                         }
86                 }
87                 
88                 public FlatStyle FlatStyle {
89                         get { return flatStyle; }
90                         set { 
91                                 if( flatStyle != value) {
92                                         flatStyle = value; 
93
94                                         if( flatStyle == FlatStyle.System) {
95                                                 Win32.UpdateWindowStyle(Handle, (int)ButtonStyles.BS_OWNERDRAW, 0);
96                                         } else {
97                                                 Win32.UpdateWindowStyle(Handle, 0, (int)ButtonStyles.BS_OWNERDRAW);
98                                         }
99                                         Invalidate();
100                                 }
101                         }
102                 }
103                 
104                 public Image Image {
105                         get { return image; }
106                         set { 
107                                 image = value; 
108                                 Invalidate();
109                         }
110                 }
111                         [MonoTODO]
112                 public ImageList ImageList {
113                         get {
114                                 throw new NotImplementedException ();
115                         }
116                         set{
117                                 //fixme:
118                         }
119
120                 }
121
122                 public ContentAlignment ImageAlign {
123                         get { return imageAlign; }
124                         set { 
125                                 if( imageAlign != value) {
126                                         imageAlign = value;
127                                         Invalidate();
128                                 }
129                         }
130                 }
131                 
132                 public int ImageIndex {
133                         get { return imageIndex; }
134                         set { imageIndex=value; }
135                 }
136                 
137                 public new ImeMode ImeMode {
138                         get {
139                                 return imeMode; }
140                         set {
141                                 imeMode = value;
142                         }
143                 }
144                 
145                 protected bool IsDefault {
146                         get {
147                                 return isDefault;
148                         }
149                         set {
150                                 isDefault = value;
151                         }
152                 }
153
154                 internal bool Pushed {
155                         get {
156                                 return isPushed;
157                         }
158                 }
159
160                 [MonoTODO]
161                 public virtual ContentAlignment TextAlign {
162                         get { 
163                                 return textAlign; 
164                         }
165                         set { 
166                                 if( textAlign != value) {
167                                         textAlign = value;
168                                         Win32.UpdateWindowStyle(Handle, (int)0xF00, (int)Win32.ContentAlignment2SystemButtonStyle(textAlign));
169                                         Invalidate();
170                                 }
171                         }
172                 }
173
174                 /// --- Methods ---
175                 protected override void Dispose(bool disposing){
176                         base.Dispose(disposing);
177                 }
178
179                 protected void ResetFlagsandPaint(){
180                 }
181                 
182                 
183                 protected override AccessibleObject CreateAccessibilityInstance() 
184                 {
185                         return base.CreateAccessibilityInstance();
186                 }
187                 
188                 /// [methods for events]
189                 protected override void OnEnabledChanged (EventArgs e) 
190                 {
191                         base.OnEnabledChanged (e);
192                 }
193                 
194                 protected override void OnGotFocus (EventArgs e) 
195                 {
196                         base.OnGotFocus (e);
197                 }
198                 
199                 protected override void OnKeyDown (KeyEventArgs kevent) 
200                 {
201                         base.OnKeyDown (kevent);
202                 }
203                 
204                 protected override void OnKeyUp (KeyEventArgs kevent) 
205                 {
206                         base.OnKeyUp (kevent);
207                 }
208                 
209                 protected override void OnLostFocus (EventArgs e) 
210                 {
211                         base.OnLostFocus (e);
212                 }
213                 
214                 protected override void OnMouseDown (MouseEventArgs mevent) 
215                 {
216                         if ((mevent.Button & MouseButtons.Left) == MouseButtons.Left) {
217                                 isPushed = true;
218                                 Invalidate(); 
219                         }
220
221                         base.OnMouseDown (mevent);
222                 }
223                 
224                 protected override void OnMouseEnter (EventArgs eventargs) 
225                 {
226                                 base.OnMouseEnter(eventargs);
227                                 if( FlatStyle == FlatStyle.Flat || FlatStyle == FlatStyle.Popup) {
228                                         Invalidate();
229                                 }
230                 }
231                 
232                 protected override void OnMouseLeave (EventArgs eventargs) 
233                 {
234                         base.OnMouseLeave(eventargs);
235                         if( FlatStyle == FlatStyle.Flat || FlatStyle == FlatStyle.Popup) {
236                                 Invalidate();
237                         }
238                 }
239                 
240                 protected override void OnMouseMove (MouseEventArgs mevent) 
241                 {
242                         base.OnMouseMove (mevent);
243                 }
244                 
245                 protected override void OnMouseUp (MouseEventArgs mevent) 
246                 {
247                         isPushed = false;
248                         Invalidate(); 
249                         base.OnMouseUp (mevent);
250                 }
251                 
252                 internal virtual void ButtonPaint (PaintEventArgs pevent) {
253                 }
254
255                 protected override void OnPaint (PaintEventArgs pevent) 
256                 {
257                         base.OnPaint (pevent);
258                         ButtonPaint (pevent);
259                 }
260                 
261                 protected override void OnParentChanged (EventArgs e) 
262                 {
263                         base.OnParentChanged (e);
264                 }
265                 
266                 protected override void OnTextChanged (EventArgs e) 
267                 {
268                         base.OnTextChanged (e);
269                 }
270                 
271                 protected override void OnVisibleChanged (EventArgs e) 
272                 {
273                         base.OnVisibleChanged (e);
274                 }
275                 /// end of [methods for events]
276                 
277                 protected override void WndProc (ref Message m) 
278                 {
279                         switch ((Msg) m.Msg) {
280                                 case Msg.WM_COMMAND: {
281                                         switch(m.HiWordWParam) {
282                                                 case (uint)ButtonNotification.BN_CLICKED: {
283                                                         OnClick(new ControlEventArgs(this));
284                                                         Win32.SendMessage(Handle, Msg.WM_SETFOCUS, (int)Handle, 0);
285                                                         CallControlWndProc(ref m);
286                                                         break;
287                                                 }
288
289                                                 case (uint)ButtonNotification.BN_DOUBLECLICKED: {
290                                                         OnClick(new ControlEventArgs(this));
291                                                         CallControlWndProc(ref m);
292                                                         break;
293                                                 }
294
295                                                 case (uint)ButtonNotification.BN_SETFOCUS: {
296                                                         OnGotFocus(new ControlEventArgs(this));
297                                                         break;
298                                                 }
299
300                                                 case (uint)ButtonNotification.BN_KILLFOCUS: {
301                                                         OnLostFocus(new ControlEventArgs(this));
302                                                         break;
303                                                 }
304
305                                                 default:
306                                                         CallControlWndProc(ref m);
307                                                         break;
308                                         }
309                                         break;
310                                 }
311
312                                 case Msg.WM_DRAWITEM: {
313                                         m.Result = (IntPtr)1;
314                                         break;
315                                 }
316
317                                 case Msg.WM_PAINT: {
318                                         PAINTSTRUCT             ps      = new PAINTSTRUCT ();
319                                         IntPtr                  hdc = Win32.BeginPaint (Handle, ref ps);
320                                         Rectangle               rc = new Rectangle ();
321
322                                         rc.X = ps.rcPaint.left;
323                                         rc.Y = ps.rcPaint.top;
324                                         rc.Width = ps.rcPaint.right - ps.rcPaint.left;
325                                         rc.Height = ps.rcPaint.bottom - ps.rcPaint.top;
326                                         PaintEventArgs paintEventArgs = new PaintEventArgs (Graphics.FromHdc (hdc), rc);
327                                         OnPaint (paintEventArgs);
328                                         paintEventArgs.Dispose ();
329                                         Win32.EndPaint (Handle, ref ps);
330                                         break;
331                                 }
332
333                                 default: {
334                                         base.WndProc (ref m);
335                                         break;
336                                 }
337                         }
338                 }
339
340
341                 /// --- ButtonBase.ButtonBaseAccessibleObject ---
342                 /// the class isonly used for .NET framework
343                 /// 
344                 //public class ButtonBaseAccessibleObject : Control.ControlAccessibleObject{
345                 //      private ButtonBaseAccessibleObject() : base(/* need "control" parameter here. "this", "base" no go.*/){
346                 //}
347                 //}
348         }
349 }