* UnsafeNativeMethods.cs: added IEnumVariant interface,
[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 using System.ComponentModel;
15 using System.Drawing;
16
17 namespace System.Windows.Forms {
18
19         /// <summary>
20         /// Implements the basic functionality common to button controls.
21         /// </summary>
22
23         public abstract class ButtonBase : Control {
24
25                 // private fields
26                 FlatStyle flatStyle;
27                 Image image;
28                 ContentAlignment imageAlign;
29                 int imageIndex;
30                 ContentAlignment textAlign;
31                 ImeMode imeMode;
32                 bool isDefault;
33                 Label label;
34                 bool isPushed;
35
36 //              
37 //              // --- Constructor ---
38                 protected ButtonBase() : base() 
39                 {
40                         flatStyle = FlatStyle.Standard;
41                         image = null;
42                         imageAlign = ContentAlignment.MiddleCenter;
43                         imageIndex = -1;
44                         textAlign = ContentAlignment.MiddleCenter;
45                         imeMode = ImeMode.Inherit;
46                         isDefault = false;
47                         isPushed = false;
48                 }
49
50                 // --- Properties ---
51                 protected override CreateParams CreateParams {
52                         get { return base.CreateParams; }
53                 }
54                 
55                 protected override ImeMode DefaultImeMode {
56                         get {
57                                 return ImeMode.Inherit;
58                         }
59                 }
60                 
61                 protected override Size DefaultSize {
62                         get {
63                                 return new Size(75,23);// default size for button.
64                         }
65                 }
66                 
67                 public FlatStyle FlatStyle {
68                         get { return flatStyle; }
69                         set { 
70                                 if( flatStyle != value) {
71                                         flatStyle = value; 
72
73                                         if( flatStyle == FlatStyle.System) {
74                                                 Win32.UpdateWindowStyle(Handle, (int)ButtonStyles.BS_OWNERDRAW, 0);
75                                         } else {
76                                                 Win32.UpdateWindowStyle(Handle, 0, (int)ButtonStyles.BS_OWNERDRAW);
77                                         }
78                                         Invalidate();
79                                 }
80                         }
81                 }
82                 
83                 public Image Image {
84                         get { return image; }
85                         set { 
86                                 image = value; 
87                                 Invalidate();
88                         }
89                 }
90                         [MonoTODO]
91                 public ImageList ImageList {
92                         get {
93                                 throw new NotImplementedException ();
94                         }
95                         set{
96                                 //fixme:
97                         }
98
99                 }
100
101                 public ContentAlignment ImageAlign {
102                         get { return imageAlign; }
103                         set { 
104                                 if( imageAlign != value) {
105                                         imageAlign = value;
106                                         Invalidate();
107                                 }
108                         }
109                 }
110                 
111                 public int ImageIndex {
112                         get { return imageIndex; }
113                         set { imageIndex=value; }
114                 }
115                 
116                 public new ImeMode ImeMode {
117                         get {
118                                 return imeMode; }
119                         set {
120                                 imeMode = value;
121                         }
122                 }
123                 
124                 protected bool IsDefault {
125                         get {
126                                 return isDefault;
127                         }
128                         set {
129                                 isDefault = value;
130                         }
131                 }
132
133                 internal bool Pushed {
134                         get {
135                                 return isPushed;
136                         }
137                 }
138
139                 [MonoTODO]
140                 public virtual ContentAlignment TextAlign {
141                         get { 
142                                 return textAlign; 
143                         }
144                         set { 
145                                 if( textAlign != value) {
146                                         textAlign = value;
147                                         Win32.UpdateWindowStyle(Handle, (int)0xF00, (int)Win32.ContentAlignment2SystemButtonStyle(textAlign));
148                                         Invalidate();
149                                 }
150                         }
151                 }
152
153                 /// --- Methods ---
154                 protected override void Dispose(bool disposing){
155                         base.Dispose(disposing);
156                 }
157
158                 protected void ResetFlagsandPaint(){
159                 }
160                 
161                 
162                 protected override AccessibleObject CreateAccessibilityInstance() 
163                 {
164                         return base.CreateAccessibilityInstance();
165                 }
166                 
167                 /// [methods for events]
168                 protected override void OnEnabledChanged (EventArgs e) 
169                 {
170                         base.OnEnabledChanged (e);
171                 }
172                 
173                 protected override void OnGotFocus (EventArgs e) 
174                 {
175                         base.OnGotFocus (e);
176                 }
177                 
178                 protected override void OnKeyDown (KeyEventArgs kevent) 
179                 {
180                         base.OnKeyDown (kevent);
181                 }
182                 
183                 protected override void OnKeyUp (KeyEventArgs kevent) 
184                 {
185                         base.OnKeyUp (kevent);
186                 }
187                 
188                 protected override void OnLostFocus (EventArgs e) 
189                 {
190                         base.OnLostFocus (e);
191                 }
192                 
193                 protected override void OnMouseDown (MouseEventArgs mevent) 
194                 {
195                         if ((mevent.Button & MouseButtons.Left) == MouseButtons.Left) {
196                                 isPushed = true;
197                                 Invalidate(); 
198                         }
199
200                         base.OnMouseDown (mevent);
201                 }
202                 
203                 protected override void OnMouseEnter (EventArgs eventargs) 
204                 {
205                                 base.OnMouseEnter(eventargs);
206                                 if( FlatStyle == FlatStyle.Flat || FlatStyle == FlatStyle.Popup) {
207                                         Invalidate();
208                                 }
209                 }
210                 
211                 protected override void OnMouseLeave (EventArgs eventargs) 
212                 {
213                         base.OnMouseLeave(eventargs);
214                         if( FlatStyle == FlatStyle.Flat || FlatStyle == FlatStyle.Popup) {
215                                 Invalidate();
216                         }
217                 }
218                 
219                 protected override void OnMouseMove (MouseEventArgs mevent) 
220                 {
221                         base.OnMouseMove (mevent);
222                 }
223                 
224                 protected override void OnMouseUp (MouseEventArgs mevent) 
225                 {
226                         isPushed = false;
227                         Invalidate(); 
228                         base.OnMouseUp (mevent);
229                 }
230                 
231                 internal virtual void ButtonPaint (PaintEventArgs pevent) {
232                 }
233
234                 protected override void OnPaint (PaintEventArgs pevent) 
235                 {
236                         base.OnPaint (pevent);
237                         ButtonPaint (pevent);
238                 }
239                 
240                 protected override void OnParentChanged (EventArgs e) 
241                 {
242                         base.OnParentChanged (e);
243                 }
244                 
245                 protected override void OnTextChanged (EventArgs e) 
246                 {
247                         base.OnTextChanged (e);
248                 }
249                 
250                 protected override void OnVisibleChanged (EventArgs e) 
251                 {
252                         base.OnVisibleChanged (e);
253                 }
254                 /// end of [methods for events]
255                 
256                 protected override void WndProc (ref Message m) 
257                 {
258                         switch ((Msg) m.Msg) {
259                                 case Msg.WM_COMMAND: {
260                                         switch(m.HiWordWParam) {
261                                                 case (uint)ButtonNotification.BN_CLICKED: {
262                                                         OnClick(new ControlEventArgs(this));
263                                                         Win32.SendMessage(Handle, Msg.WM_SETFOCUS, (int)Handle, 0);
264                                                         CallControlWndProc(ref m);
265                                                         break;
266                                                 }
267
268                                                 case (uint)ButtonNotification.BN_DOUBLECLICKED: {
269                                                         OnClick(new ControlEventArgs(this));
270                                                         CallControlWndProc(ref m);
271                                                         break;
272                                                 }
273
274                                                 case (uint)ButtonNotification.BN_SETFOCUS: {
275                                                         OnGotFocus(new ControlEventArgs(this));
276                                                         break;
277                                                 }
278
279                                                 case (uint)ButtonNotification.BN_KILLFOCUS: {
280                                                         OnLostFocus(new ControlEventArgs(this));
281                                                         break;
282                                                 }
283
284                                                 default:
285                                                         CallControlWndProc(ref m);
286                                                         break;
287                                         }
288                                         break;
289                                 }
290
291                                 case Msg.WM_DRAWITEM: {
292                                         m.Result = (IntPtr)1;
293                                         break;
294                                 }
295
296                                 case Msg.WM_PAINT: {
297                                         PAINTSTRUCT             ps      = new PAINTSTRUCT ();
298                                         IntPtr                  hdc = Win32.BeginPaint (Handle, ref ps);
299                                         Rectangle               rc = new Rectangle ();
300
301                                         rc.X = ps.rcPaint.left;
302                                         rc.Y = ps.rcPaint.top;
303                                         rc.Width = ps.rcPaint.right - ps.rcPaint.left;
304                                         rc.Height = ps.rcPaint.bottom - ps.rcPaint.top;
305                                         PaintEventArgs paintEventArgs = new PaintEventArgs (Graphics.FromHdc (hdc), rc);
306                                         OnPaint (paintEventArgs);
307                                         paintEventArgs.Dispose ();
308                                         Win32.EndPaint (Handle, ref ps);
309                                         break;
310                                 }
311
312                                 default: {
313                                         base.WndProc (ref m);
314                                         break;
315                                 }
316                         }
317                 }
318
319
320                 /// --- ButtonBase.ButtonBaseAccessibleObject ---
321                 /// the class isonly used for .NET framework
322                 /// 
323                 //public class ButtonBaseAccessibleObject : Control.ControlAccessibleObject{
324                 //      private ButtonBaseAccessibleObject() : base(/* need "control" parameter here. "this", "base" no go.*/){
325                 //}
326                 //}
327         }
328 }