* ContextMenu.cs : SourceControl, Show method
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / Control.cs
1     //
2     // System.Windows.Forms.Control.cs
3     //
4     // Author:
5     //   stubbed out by Jaak Simm (jaaksimm@firm.ee)
6     //  Dennis Hayes (dennish@rayetk.com)
7     //   WINELib implementation started by John Sohn (jsohn@columbus.rr.com)
8     //   Alexandre Pigolkine (pigolkine@gmx.de)
9     //  Aleksey Ryabchuk (ryabchuk@yahoo.com)
10     //
11     // (C) Ximian, Inc., 2002
12     //
13     
14     using System.ComponentModel;
15     using System.Drawing;
16     using System.Collections;
17         using System.Threading;
18         using System.Text;
19     using System.Runtime.InteropServices;
20     using System.Collections.Specialized;
21
22     namespace System.Windows.Forms {
23     
24         /// <summary>
25         /// Defines the base class for controls, which are components with 
26         /// visual representation.
27         /// </summary>
28         
29         public class Control : Component , ISynchronizeInvoke, IWin32Window {
30     
31
32                 // Helper NativeWindow class to dispatch messages back
33                 // to the Control class
34                 protected class ControlNativeWindow : NativeWindow {
35     
36                         private Control control;
37     
38                         public ControlNativeWindow (Control control) : base() {
39                                 this.control = control;
40                         }
41     
42                         protected override void WndProc (ref Message m) {
43                                         //Console.WriteLine ("Control WndProc Message HWnd {0}, Msg {1}", m.HWnd, m.Msg);
44                                         // Do not call default WndProc here
45                                         // let the control decide what to do
46                                 // base.WndProc (ref m);
47                                 control.WndProc (ref m);
48                         }
49                 }
50                 
51                 // FIXME: not sure if dervied classes should have access
52                 protected ControlNativeWindow window;
53                 private ControlCollection childControls;
54                 private Control parent;
55                 static private Hashtable controlsCollection = new Hashtable ();
56                 static private NativeWindow parkingWindow = null;
57                 private static bool classRegistered = false;
58
59                 // private fields
60                 // it seems these are stored in case the window is not created,
61                 // corresponding properties (below) need to check if window
62                 // is created or not and react accordingly
63                 string accessibleDefaultActionDescription;
64                 string accessibleDescription;
65                 string accessibleName;
66                 AccessibleRole accessibleRole;
67                 bool allowDrop;
68                 AnchorStyles anchor;
69                 protected Color backColor;
70                 Image backgroundImage;
71                 //BindingContext bindingContext;
72                 Rectangle bounds;
73                 Rectangle oldBounds;
74
75                 bool causesValidation;
76                 ContextMenu contextMenu;
77                 DockStyle dock;
78                 bool enabled;
79                 Font font;
80                 protected Color foreColor;
81                 ImeMode imeMode;
82                 bool isAccessible;
83                 // Point location;  // using bounds to store location
84                 string name;
85                 Region region;
86                 RightToLeft rightToLeft;
87                 bool tabStop;
88                 protected string text;
89                 protected bool visible;
90                 protected ControlStyles controlStyles_;
91                 Cursor  cursor;
92
93                 int clientWidth;
94                 int clientHeight;
95
96                 BitVector32 statuses;
97                 private static readonly int LAYOUT_SUSPENDED = BitVector32.CreateMask();
98                 private static readonly int LAYOUT_PENDING   = BitVector32.CreateMask( LAYOUT_SUSPENDED );
99                 private static readonly int DISPOSED         = BitVector32.CreateMask( LAYOUT_PENDING );
100                 private static readonly int RECREATING_HANDLE= BitVector32.CreateMask( DISPOSED );
101                 private static readonly int CREATED          = BitVector32.CreateMask( RECREATING_HANDLE );
102                 private static readonly int DISPOSING        = BitVector32.CreateMask( CREATED );
103
104                         object tag;
105                         protected bool mouseIsInside_;
106
107                         // BeginInvoke() etc. helpers
108                         static int InvokeMessage = Win32.RegisterWindowMessage("mono_control_invoke_helper");
109
110                         // CHECKME: This variable is used to determine whether current thread
111                         // was used to create Control Handle. It take some space but saves a call
112                         // to unmanaged code in ISynchronizeInvoke.IsInvokeRequired.
113                         private int CreatorThreadId_ = 0; 
114
115                         private Queue InvokeQueue_ = new Queue();
116
117                         internal class ControlInvokeHelper : IAsyncResult {
118                                 private Delegate Method_ = null;
119                                 private object[] MethodArgs_ = null;
120                                 private object MethodResult_ = null;
121                                 private ManualResetEvent AsyncWaitHandle_ = new ManualResetEvent(false);
122                                 private bool CompletedSynchronously_ = false;
123                                 private bool IsCompleted_ = false;
124
125                                 public ControlInvokeHelper( Delegate method, object[] args) {
126                                         Method_ = method;
127                                         MethodArgs_ = args;
128                                 }
129
130                                 // IAsyncResult interface 
131                                 object IAsyncResult.AsyncState { 
132                                         get {
133                                                 if( MethodArgs_ != null && MethodArgs_.Length != 0) {
134                                                         return MethodArgs_[MethodArgs_.Length - 1];
135                                                 }
136                                                 return null;
137                                         }
138                                 }
139
140                                 WaitHandle IAsyncResult.AsyncWaitHandle { 
141                                         get {
142                                                 return AsyncWaitHandle_;
143                                         }
144                                 }
145
146                                 bool IAsyncResult.CompletedSynchronously {
147                                         get {
148                                                 return CompletedSynchronously_;
149                                         }
150                                 }
151
152                                 bool IAsyncResult.IsCompleted { 
153                                         get {
154                                                 return IsCompleted_;
155                                         }
156                                 }
157
158                                 internal bool CompletedSynchronously {
159                                         set {
160                                                 CompletedSynchronously_ = value;
161                                         }
162                                 }
163
164                                 internal object MethodResult {
165                                         get {
166                                                 return MethodResult_;
167                                         }
168                                 }
169
170                                 internal void ExecuteMethod() {
171                                         object result = Method_.DynamicInvoke(MethodArgs_);
172                                         lock(this) {
173                                                 MethodResult_ = result;
174                                                 IsCompleted_ = true;
175                                         }
176                                         AsyncWaitHandle_.Set();
177                                 }
178                         }
179
180                 // --- Constructors ---
181     
182                         //Compact Framework //only Control()
183                 public Control ()
184                 {
185                         childControls = CreateControlsInstance ();
186
187                         statuses = new BitVector32(); 
188                         
189                         accessibleDefaultActionDescription = null;
190                         accessibleDescription = null;
191                         accessibleName = null;
192                         accessibleRole = AccessibleRole.Default;
193                         allowDrop = false;
194                         anchor = AnchorStyles.Top | AnchorStyles.Left;
195                         backColor = Control.DefaultBackColor;
196                         backgroundImage = null;
197                         bounds = new Rectangle();
198                         oldBounds = new Rectangle();
199                         // bindingContext = null;
200                         causesValidation = true;
201                         contextMenu = null;
202                         dock = DockStyle.None;
203                         enabled = true;
204                         // font = Control.DefaultFont;
205                         foreColor = Control.DefaultForeColor;
206                         imeMode = ImeMode.Inherit;
207                         isAccessible = false;
208                         // location = new Point (0,0); should be from OS
209                         name = "";
210                         region = null;
211                         rightToLeft = RightToLeft.Inherit;
212                         tabStop = true;
213                         text = "";
214                         visible = true;
215                         parent = null;
216                         cursor = null;
217
218                         oldBounds.Width  = bounds.Width = DefaultSize.Width;
219                         oldBounds.Height = bounds.Height= DefaultSize.Height;
220                         clientWidth      = DefaultSize.Width;
221                         clientHeight     = DefaultSize.Height;
222
223                         mouseIsInside_ = false;
224                         controlStyles_ = ControlStyles.Selectable | ControlStyles.StandardClick | ControlStyles.StandardDoubleClick;
225                                 // Do not create Handle here, only in CreateHandle
226                         // CreateHandle();//sets window handle. FIXME: No it does not
227                 }
228                 
229                 // according to docs, the constructors do not create 
230                 // the (HWND) window
231                 public Control (string text) : this() 
232                 {
233                         Text = text;
234                         // SetWindowTextA (Handle, text);
235                 }
236                 
237                 public Control (Control parent, string text) : this (text) 
238                 {
239                         Parent = parent;
240                         // SetParent (Handle, parent.Handle);
241                 }
242                 
243                 public Control (string text, int left, int top, 
244                                 int width, int height) : this(text) 
245                 {
246                         Left = left;
247                         Top = top;
248                         Width = width;
249                         Height = height;
250                         //SetWindowPos (Handle, (IntPtr) 0, left, top,
251                         //          width, height, 0);
252                 }
253                 
254                 public Control (Control parent,string text,int left, int top,
255                                 int width,int height) : this (parent, text)
256                 {
257                         Left = left;
258                         Top = top;
259                         Width = width;
260                         Height = height;
261                         // SetWindowPos (Handle, (IntPtr) 0, left, top,
262                         //                  width, height, 0);
263                 }
264     
265                 // for internal use only, create a control class
266                 // for an existing, created HWND
267                 private Control (IntPtr existingHandle)
268                 {
269                         window = (ControlNativeWindow) NativeWindow.FromHandle (
270                                 existingHandle);
271                 }
272     
273                 // --- Properties ---
274                 // Properties only supporting .NET framework, not stubbed out:
275                 //  - protected bool RenderRightToLeft {get;}
276                 //  - public IWindowTarget WindowTarget {get; set;}
277                 //[MonoTODO]
278                 //public AccessibleObject AccessibilityObject {
279                 //      get {
280                 //              throw new NotImplementedException ();
281                 //      }
282                 //}
283     
284                 public string AccessibleDefaultActionDescription {
285                         get {
286                                 return accessibleDefaultActionDescription;
287                         }
288                         set {
289                                 accessibleDefaultActionDescription = value;
290                         }
291                 }
292                 
293                 public string AccessibleDescription {
294                         get {
295                                 return accessibleDescription;
296                         }
297                         set {
298                                 accessibleDescription=value;
299                         }
300                 }
301                 
302                 public string AccessibleName {
303                         get {
304                                 return accessibleName;
305                         }
306                         set {
307                                 accessibleName=value;
308                         }
309                 }
310                 
311                 public AccessibleRole AccessibleRole {
312                         get {
313                                 return accessibleRole;
314                         }
315                         set {
316                                 accessibleRole=value;
317                         }
318                 }
319                 
320                 public virtual bool AllowDrop {
321                         get {
322                                 return allowDrop;
323                         }
324                         set {
325                                 allowDrop=value;
326                         }
327                 }
328         
329                 public virtual AnchorStyles Anchor {
330                         get {
331                                 return anchor;
332                         }
333                         set {
334                                 if ( anchor != value ) {
335                                         anchor = value;
336                                         if ( anchor != ( AnchorStyles.Left | AnchorStyles.Top ) )
337                                                 Dock = DockStyle.None;
338                                 }
339                         }
340                 }
341                 
342                         //Compact Framework
343                 public virtual Color BackColor {
344                         get {
345                                         return backColor;
346                         }
347                         set {
348                                         if( backColor != value) {
349                                                 backColor = value;
350                                                 OnBackColorChanged(new EventArgs());
351                                         }
352                         }
353                 }
354                 
355                 public virtual Image BackgroundImage {
356                         get {
357                                 return backgroundImage;
358                         }
359                         set {
360                                 backgroundImage = value;
361                                 // FIXME: force redraw
362                                         Invalidate();
363                         }
364                 }
365     
366                 public virtual BindingContext BindingContext {
367                         get {
368                                 //return bindingContext;
369                                 throw new NotImplementedException ();
370                         }
371                         set {
372                                 //bindingContext=value;
373                                 throw new NotImplementedException ();
374                         }
375                 }
376                 
377                         //Compact Framework
378                 public int Bottom {
379                         get {
380                                 return Top + Height;
381                         }
382                 }
383                 
384                         //Compact Framework
385                 public Rectangle Bounds {
386                         get {
387                                 return bounds;
388                         }
389                         set {
390                                 SetBounds(value.Left, value.Top, value.Width, value.Height);
391                         }
392                 }
393                 
394                 public bool CanFocus {
395                         get {
396                                 if (IsHandleCreated && Visible && Enabled)
397                                         return true;
398                                 return false;
399                         }
400                 }
401                 
402                 [MonoTODO]
403                 public bool CanSelect {
404                         get {
405                                 if ( !GetStyle ( ControlStyles.Selectable ) )
406                                         return false;
407
408                                 if ( Parent == null )
409                                         return false;
410
411                                 Control parent = Parent;
412                                 while ( parent != null ) {
413                                         if ( !parent.Visible || !parent.Enabled )
414                                                 return false;
415                                         parent = parent.Parent;
416                                 }
417
418                                 return true;
419                         }
420                 }
421                 
422                         //Compact Framework
423                 public bool Capture {
424                         get {
425                                 if (IsHandleCreated) {
426                                         IntPtr captured = Win32.GetCapture ();
427                                         if (Handle == captured) 
428                                                 return true;
429                                 }
430                                 return false;
431                         }
432                         set {
433                                 if (IsHandleCreated) {
434                                         if (value)
435                                                 Win32.SetCapture (Handle);
436                                         else {
437                                                 IntPtr captured = Win32.GetCapture ();
438     
439                                                 // if this window is in capture state
440                                                 // release it
441                                                 if (Handle == captured)
442                                                         Win32.ReleaseCapture ();
443                                         }
444                                 }
445                         }
446                 }
447                 
448                 public bool CausesValidation {
449                         get {
450                                 return causesValidation;
451                         }
452                         set {
453                                 causesValidation=value;
454                         }
455                 }
456                 
457                         //Compact Framework
458                 public Rectangle ClientRectangle {
459                         get {
460                                 return new Rectangle ( 0, 0, ClientSize.Width, ClientSize.Height );
461  
462                         }
463                 }
464                 
465                         //Compact Framework
466                         [MonoTODO]
467                         public Size ClientSize {
468                                 get {
469                                         return new Size ( clientWidth, clientHeight);
470                                 }
471                                 set {
472                                         SetClientSizeCore ( value.Width, value.Height );
473                                 }
474                         }
475                 
476                 [MonoTODO]
477                 public string CompanyName {
478                         get {
479                                         //Better than throwing an execption
480                                 return "Company Name";
481                         }
482                 }
483
484                 public bool ContainsFocus {
485                         get {
486                                 if (IsHandleCreated) {
487                                                 if (Focused) return true;
488                                                 foreach (Control ctr in Controls) {
489                                                         if (ctr.ContainsFocus) return true;
490                                                 }
491                                 }
492                                 return false;
493                         }
494                 }
495                 
496                         //Compact Framework
497                 [MonoTODO]
498                 public virtual ContextMenu ContextMenu {
499                         get {
500                                 return contextMenu;
501                         }
502                         set {
503                                 if ( contextMenu != value ) {
504                                         contextMenu = value;
505                                         OnContextMenuChanged ( EventArgs.Empty );
506                                 }
507                         }
508                 }
509                 
510                 public ControlCollection Controls {
511                         get { return childControls; }
512                 }
513                 
514                 public bool Created {
515                         get { return statuses[ CREATED ]; }
516                 }
517                 
518                 protected virtual CreateParams CreateParams {
519                         get {
520                                         CreateParams createParams = new CreateParams ();
521                                         createParams.Caption = Text;
522                                         createParams.X = Left;
523                                         createParams.Y = Top;
524                                         createParams.Width = Width;
525                                         createParams.Height = Height;
526                                         createParams.ClassStyle = 0;
527                                         createParams.ExStyle = 0;
528                                         createParams.Param = 0;
529                                 
530                                         if (parent != null)
531                                                 createParams.Parent = parent.Handle;
532                                         else 
533                                                 createParams.Parent = ParkingWindowHandle;
534           
535                                         createParams.Style = (int) WindowStyles.WS_OVERLAPPED;
536                                         if( visible) {
537                                                 createParams.Style |= (int) WindowStyles.WS_VISIBLE;
538                                         }
539           
540                                 return createParams;
541                         }
542                 }
543                 
544                         internal protected IntPtr ControlRealWndProc = IntPtr.Zero;
545                         internal protected bool SubClassWndProc_ = false;
546
547                         // This function lets Windows or/and default Windows control process message
548                         // Classes have to call it if they do not handle message in WndProc or
549                         // default handling is needed.
550                         protected void CallControlWndProc( ref Message msg) {
551                                 if( ControlRealWndProc != IntPtr.Zero) {
552                                         msg.Result = (IntPtr)Win32.CallWindowProc(ControlRealWndProc, msg.HWnd, (int)msg.Msg, msg.WParam.ToInt32(), msg.LParam.ToInt32());
553                                 }
554                                 else {
555                                         DefWndProc(ref msg);
556                                 }
557                         }
558
559                         // Subclass only native Windows controls. Those classes have to set SubClassWndProc_ to true in contructor
560                         private void SubclassWindow() {
561                                 if( IsHandleCreated && SubClassWndProc_) {
562                                         ControlRealWndProc = Win32.SetWindowLong( Handle, GetWindowLongFlag.GWL_WNDPROC, NativeWindow.GetWindowProc());
563                                 }
564                         }
565
566                         private void UnsubclassWindow() {
567                                 if( IsHandleCreated) {
568                                         Win32.SetWindowLong( Handle, GetWindowLongFlag.GWL_WNDPROC, ControlRealWndProc.ToInt32());
569                                 }
570                         }
571
572                         protected virtual void OnWmCommand (ref Message m) {
573                                 if( m.LParam.ToInt32() != 0) {
574                                         if( m.LParam != Handle) {
575                                                 // Control notification
576                                                 System.Console.WriteLine("Control notification Code {0} Id = Hwnd {1}", m.HiWordWParam, m.LParam.ToInt32());
577                                                 Control.ReflectMessage(m.LParam, ref m);
578                                         }
579                                         else {
580                                                 // Unhandled Control reflection
581                                                 // Derived class didn't handle WM_COMMAND or called base.WndProc in WM_COMMAND handler
582                                                 // CHECKME: Shall we notify user in debug build, throw an exception or just ignore this case ?
583                                         }
584                                 }
585                         }
586
587                 [MonoTODO]
588                 public virtual Cursor Cursor {
589                         get { 
590                                 if ( cursor == null )
591                                         return Cursors.Default;
592                                 return cursor; 
593                         }
594                         set { cursor = value;}
595                 }
596                 
597                         //Compact Framework
598                 [MonoTODO]
599                 // waiting for BindingContext; should be stubbed now
600                 public ControlBindingsCollection DataBindings {
601                         get {
602                                 throw new NotImplementedException ();
603                         }
604                 }
605                 
606                 public static Color DefaultBackColor {
607                         get {
608                                 // FIXME: use GetSystemMetrics?
609                                 return SystemColors.Control;
610                                 //throw new NotImplementedException ();
611                         }
612                 }
613     
614                 //[MonoTODO]
615                 // FIXME: use GetSystemMetrics?
616                 public static Font DefaultFont {
617                         // FIXME: get current system font from GenericSansSerif
618                         //        call ArgumentException not called
619                         get {
620                 //              throw new NotImplementedException ();
621                 //              return (FontFamily.GenericSansSerif);
622                                         return Font.FromHfont(Win32.GetStockObject(GSO_.DEFAULT_GUI_FONT));
623                         }
624                 }
625                 
626                 public static Color DefaultForeColor {
627                         get {
628                                 return SystemColors.ControlText;
629                         }
630                 }
631                 
632                 protected virtual ImeMode DefaultImeMode {
633                         get {
634                                 return ImeMode.Inherit;
635                         }
636                 }
637                 
638                 protected virtual Size DefaultSize {
639                         get {
640                                         //Default label size, this should be correct.
641                                 return new Size(100,23);
642                         }
643                 }
644                 
645                 public virtual Rectangle DisplayRectangle {
646                         get {
647                                 return ClientRectangle;
648                         }
649                 }
650                 
651                 public bool Disposing {
652                         get { return statuses [ DISPOSING ]; }
653                 }
654                 
655                 public virtual DockStyle Dock {
656                         get {
657                                 return dock;
658                         }
659                         set {
660                                 if ( dock != value ) {
661                                         dock = value;
662                                         if ( dock != DockStyle.None )
663                                                  Anchor = ( AnchorStyles.Left | AnchorStyles.Top );
664                                         OnDockChanged ( EventArgs.Empty );                                      
665                                 }
666                         }
667                 }
668     
669                         //Compact Framework
670                 public virtual bool Enabled {
671                         get {
672                                         return enabled;
673                                 //return Win32.IsWindowEnabled (Handle);
674                         }
675                         set {
676                                         if( enabled != value) {
677                                                 Win32.EnableWindow (Handle, value);
678                                                 enabled = value;
679                                                 // FIXME: Disable/enable all children here
680                                                 Invalidate();
681                                         }
682                         }
683                 }
684                 
685                         //Compact Framework
686                 public virtual bool Focused {
687                         get {
688                                         if (IsHandleCreated) {
689                                                 IntPtr focusedWindow = Win32.GetFocus();
690                                                 if (focusedWindow == Handle)
691                                                         return true;
692                                         }
693                                         return false;
694                                 }
695                 }
696                 
697                         //Compact Framework
698                 public virtual Font Font {
699                                 get {
700                                         Font result = font;
701                                         if( result == null) {
702                                                 if( Parent != null) {
703                                                         result = Parent.Font;
704                                                 }
705                                                 if( result == null) {
706                                                         result = Control.DefaultFont;
707                                                 }
708                                         }
709                                         return result;
710                                 }
711                         set {
712                                         font = value;
713                                         if( IsHandleCreated) {
714                                                 Win32.SendMessage(Handle, Msg.WM_SETFONT, Font.ToHfont().ToInt32(), 1);
715                                         }
716                                 }
717                 }
718                 
719                 [MonoTODO]
720                 protected int FontHeight {
721                         get {
722                                 throw new NotImplementedException ();
723                         }
724                         set {
725                                 throw new NotImplementedException ();
726                         }
727                 }
728                 
729                         //Compact Framework
730                 public virtual Color ForeColor {
731                         get {
732                                 return foreColor;
733                         }
734                         set {
735                                 foreColor = value;
736                         }
737                 }
738                 
739                 public bool HasChildren {
740                         get {
741                                 if (childControls.Count >0) 
742                                         return true;
743                                 return false;
744                         }
745                 }
746                 
747                         //Compact Framework
748                 public int Height {
749                         get {
750                                 return bounds.Height;
751                         }
752                         set {
753                                 SetBounds(bounds.X, bounds.Y, bounds.Width, value, BoundsSpecified.Height);
754                         }
755                 }
756                 
757                 public ImeMode ImeMode {
758                         // CHECKME:
759                         get {
760                                 return imeMode;
761                         }
762                         set {
763                                 imeMode=value;
764                         }
765                 }
766                 
767                 public bool IsAccessible {
768                         // CHECKME:
769                         get {
770                                 return isAccessible;
771                         } // default is false
772                         set {
773                                 isAccessible=value;
774                         }
775                 }
776                 
777                 public bool IsDisposed {
778                         get {   return statuses [ DISPOSED ]; }
779                 }
780                 
781                 public bool IsHandleCreated {
782                         get { return window != null && window.Handle != IntPtr.Zero; }
783                 }
784                 
785                         //Compact Framework
786                         public int Left {
787                                 get {
788                                         return bounds.X;
789                                 }
790                                 set {
791                                         SetBounds(value, bounds.Y, bounds.Width, bounds.Height, BoundsSpecified.X);
792                                 }
793                         }
794                 
795                         //Compact Framework
796                         public Point Location {
797                                 // CHECKME:
798                                 get {
799                                         return new Point (Top, Left);
800                                 }
801                                 set {
802                                         SetBounds(value.X, value.Y, bounds.Width, bounds.Height, BoundsSpecified.Location);
803                                 }
804                         }
805                 
806                 public static Keys ModifierKeys {
807                         get {
808                                 Keys keys = Keys.None;
809
810                                 if ( ( Win32.GetKeyState( (int) VirtualKeys.VK_SHIFT ) & 0x8000 ) == 0x8000 )
811                                          keys |= Keys.Shift;
812                                 if ( ( Win32.GetKeyState( (int) VirtualKeys.VK_MENU ) & 0x8000 )  == 0x8000 )
813                                         keys |= Keys.Alt;
814                                 if ( ( Win32.GetKeyState( (int) VirtualKeys.VK_CONTROL) & 0x8000) == 0x8000 )
815                                          keys |= Keys.Control;
816
817                                 return keys;
818                         }
819                 }
820                 
821                         //Compact Framework
822                 [MonoTODO]
823                 public static MouseButtons MouseButtons {
824                         get {
825                                 // FIXME: use GetAsycKeyState?
826                                 throw new NotImplementedException ();
827                         }
828                 }
829                 
830                         //Compact Framework
831                 public static Point MousePosition {
832                         get {
833                                 POINT point = new POINT();
834                                 Win32.GetCursorPos (ref point);
835                                 return new Point ( (int) point.x, (int) point.y);
836                         }
837                 }
838                 
839                 public string Name {
840                         // CHECKME:
841                         get {
842                                 return name;
843                         }
844                         set {
845                                 name = value;
846                         }
847                 }
848                 
849                         //Compact Framework
850                 public Control Parent {
851                         get {
852                                 return parent;
853                                 //IntPtr parent = GetParent (Handle);
854                                 //return FromHandle (parent);
855                         }
856                         set {
857                                         if( parent != value) {
858                                                 Console.WriteLine ("setting parent");
859                                                 parent = value;
860             
861                                                 Console.WriteLine ("add ourself to the parents control");
862                                                 // add ourself to the parents control
863                                                 if ( !parent.Controls.Contains ( this ) )
864                                                         parent.Controls.Add (this);
865
866                                                 // FIXME: Is this logic correct ?
867                                                 if( BackColor == DefaultBackColor) {
868                                                         if( parent.BackColor != BackColor)
869                                                                 OnParentBackColorChanged(new EventArgs());
870                                                 }
871
872                                                 Console.WriteLine ("SetParent");
873                                                 if (IsHandleCreated) {
874                                                         Console.WriteLine ("Handle created");
875                                                         Win32.SetParent (Handle, value.Handle);
876                                                 }
877                                                 /*
878                                                 else if( parent.IsHandleCreated){
879                                                         // CHECKME: Now control is responsible for creating his window
880                                                         // when added to Form, may be things must be reversed.
881                                                         CreateControl();
882                                                 }
883                                                 */                      
884                                         }
885                         }
886                 }
887                 
888                 protected static IntPtr ParkingWindowHandle {
889                         get {
890                                 if ( parkingWindow == null )
891                                         parkingWindow = new NativeWindow ( );
892
893                                 if ( parkingWindow.Handle == IntPtr.Zero ) {
894                                         CreateParams pars = new CreateParams ( );
895                                         pars.ClassName = "mono_native_window";
896                                         pars.Style = (int) WindowStyles.WS_OVERLAPPED;
897                                         parkingWindow.CreateHandle ( pars );
898                                 }
899
900                                 return parkingWindow.Handle;
901                         }
902                 }
903
904                 protected static void RegisterDefaultWindowClass ( )
905                 {
906                         if ( !classRegistered ) {
907                                 WNDCLASS wndClass = new WNDCLASS();
908  
909                                 wndClass.style = (int) (CS_.CS_OWNDC);
910                                 wndClass.lpfnWndProc = NativeWindow.GetWindowProc();
911                                 wndClass.cbClsExtra = 0;
912                                 wndClass.cbWndExtra = 0;
913                                 wndClass.hInstance = (IntPtr)0;
914                                 wndClass.hIcon = (IntPtr)0;
915                                 wndClass.hCursor = Win32.LoadCursor( (IntPtr)0, LC_.IDC_ARROW);
916                                 wndClass.hbrBackground = (IntPtr)((int)GetSysColorIndex.COLOR_BTNFACE + 1);
917                                 wndClass.lpszMenuName = "";
918                                 wndClass.lpszClassName = Win32.DEFAULT_WINDOW_CLASS;
919     
920                                 if (Win32.RegisterClass(ref wndClass) != 0) 
921                                         classRegistered = true; 
922                         }               
923                 }
924
925                 [MonoTODO]
926                 public string ProductName {
927                         get {
928                                         //FIXME:
929                                 return "Product Name";
930                         }
931                 }
932                 
933                 [MonoTODO]
934                 public string ProductVersion {
935                         get {
936                                         //FIXME:
937                                         return "Product Version";
938                         }
939                 }
940                 
941                 [MonoTODO]
942                 public bool RecreatingHandle {
943                         get {
944                                 return statuses [ RECREATING_HANDLE ] ;
945                         }
946                 }
947                 
948                 public Region Region {
949                         // CHECKME:
950                         get {
951                                 return region;
952                         }
953                         set {
954                                 region = value;
955                         }
956                 }
957                 
958                 protected bool ResizeRedraw {
959                         get {   return GetStyle ( ControlStyles.ResizeRedraw ); }
960                         set {   SetStyle ( ControlStyles.ResizeRedraw, value ); }
961                 }
962                 
963                         //Compact Framework
964                 public int Right {
965                         get {
966                                 return Left + Width;
967                         }
968                 }
969                 
970                 [MonoTODO]
971                 public virtual RightToLeft RightToLeft {
972                         // CHECKME:
973                         get {
974                                 return rightToLeft;
975                         }
976                         set {
977                                 rightToLeft=value;
978                         }
979                 }
980                 
981                 [MonoTODO]
982                 protected virtual bool ShowFocusCues {
983                         get {
984                                 throw new NotImplementedException ();
985                         }
986                 }
987                 
988                 [MonoTODO]
989                 protected bool ShowKeyboardCues {
990                         get {
991                                 throw new NotImplementedException ();
992                         }
993                 }
994                 
995                 [MonoTODO]
996                 public override ISite Site {
997                         get {
998                                 throw new NotImplementedException ();
999                         }
1000                         set {
1001                                 throw new NotImplementedException ();
1002                         }
1003                 }
1004                 
1005                 //Compact Framework
1006                 public Size Size {
1007                         get {
1008                                 return new Size(Width, Height);
1009                         }
1010                         set {
1011                                 SetBounds(bounds.X, bounds.Y, value.Width, value.Height, BoundsSpecified.Size);
1012                         }
1013                 }
1014
1015                 internal int tabindex;//for debug/test only. remove
1016                 [MonoTODO]
1017                 public int TabIndex {
1018                         get {
1019                                 return tabindex;
1020                         }
1021                         set {
1022                                 tabindex = value;
1023                         }
1024                 }
1025                 
1026                 public bool TabStop {
1027                         // CHECKME:
1028                         get {
1029                                 return tabStop;
1030                         }
1031                         set {
1032                                 tabStop = value;
1033                         }
1034                 }
1035                 
1036                 [MonoTODO]
1037                 public object Tag {
1038                         get {
1039                                 return tag;
1040                         }
1041                         set {
1042                                 tag = value;
1043                         }
1044                 }
1045                 
1046                         //Compact Framework
1047                 public virtual string Text {
1048                         get {
1049                                         // CHECKME: if we really need to provide back current text of real window
1050                                         // or just our copy in text member.
1051                                 if ( IsHandleCreated ) {
1052                                         int len = Win32.GetWindowTextLengthA (Handle);
1053                                         // FIXME: len is doubled due to some strange behaviour.(of GetWindowText function ?)
1054                                         // instead of 10 characters we can get only 9, even if sb.Capacity is 10.
1055                                         StringBuilder sb = new StringBuilder(len * 2 /*Win32.GetWindowTextLengthA (Handle)*/);
1056                                         Win32.GetWindowText (Handle, sb, sb.Capacity);
1057                                         text = sb.ToString();
1058                                 } 
1059                                 return text;
1060                         }
1061                         set {
1062                                 if ( text != value ) {
1063                                         text = value;
1064             
1065                                         if (IsHandleCreated)
1066                                                 Win32.SetWindowTextA (Handle, value);
1067
1068                                         OnTextChanged ( EventArgs.Empty );
1069                                 }
1070                         }
1071                 }
1072                 
1073                         //Compact Framework
1074                 public int Top {
1075                         get {
1076                                 return bounds.Top;
1077                         }
1078                         set {
1079                                 SetBounds(bounds.X, value, bounds.Width, bounds.Height, BoundsSpecified.Y);
1080                         }
1081                 }
1082                 
1083                 [MonoTODO]
1084                 public Control TopLevelControl {
1085                         get {
1086                                 throw new NotImplementedException ();
1087                         }
1088                 }
1089     
1090                 public bool Visible {
1091                         get {   return visible; }
1092                         set {
1093                                 SetVisibleCore ( value );
1094                         }
1095                 }
1096                 
1097                         //Compact Framework
1098                 public int Width {
1099                         get {
1100                                 return bounds.Width;
1101                         }
1102                         set {
1103                                 SetBounds(bounds.X, bounds.Y, value, bounds.Height, BoundsSpecified.Width);
1104                         }
1105                 }
1106                 
1107                 /// --- methods ---
1108                 /// internal .NET framework supporting methods, not stubbed out:
1109                 /// - protected virtual void NotifyInvalidate(Rectangle invalidatedArea)
1110                 /// - protected void RaiseDragEvent(object key,DragEventArgs e);
1111                 /// - protected void RaiseKeyEvent(object key,KeyEventArgs e);
1112                 /// - protected void RaiseMouseEvent(object key,MouseEventArgs e);
1113                 /// - protected void RaisePaintEvent(object key,PaintEventArgs e);
1114                 /// - protected void ResetMouseEventArgs();
1115                 
1116                 [MonoTODO]
1117                 protected void AccessibilityNotifyClients (
1118                         AccessibleEvents accEvent,int childID) 
1119                 {
1120                         throw new NotImplementedException ();
1121                 }
1122                 
1123                 [MonoTODO]
1124                 public void BringToFront () 
1125                 {
1126                         if ( IsHandleCreated )
1127                                 Win32.SetWindowPos ( Handle, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, 
1128                                         SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE );
1129                 }
1130                 
1131                 public bool Contains (Control ctl) 
1132                 {
1133                         return childControls.Contains (ctl);
1134                 }
1135                 
1136                 public void CreateControl () 
1137                 {
1138                         if ( !Created ) {
1139                                 CreateHandle ();
1140                                 OnCreateControl();
1141                                 statuses [ CREATED ] = true;
1142                         }
1143                 }
1144     
1145                 [MonoTODO]
1146                 protected virtual AccessibleObject CreateAccessibilityInstance() {
1147                         throw new NotImplementedException ();
1148                 }
1149                 
1150                 protected virtual ControlCollection CreateControlsInstance ()
1151                 {
1152                         return new ControlCollection (this);
1153                 }
1154                 
1155                         //Compact Framework
1156                 [MonoTODO]
1157                 public Graphics CreateGraphics () 
1158                 {
1159                                 return Graphics.FromHwnd(Handle);
1160                 }
1161         
1162                 protected virtual void CreateHandle ()
1163                 {
1164                         if( IsDisposed )
1165                                 throw new ObjectDisposedException ( Name );
1166
1167                         if( IsHandleCreated ) 
1168                                 return;
1169
1170                         if( window == null)
1171                                 window = new ControlNativeWindow (this);
1172
1173                         CreateParams createParams = CreateParams;
1174                         if( !Enabled) {
1175                                 createParams.Style |= (int)WindowStyles.WS_DISABLED;
1176                         }
1177                         window.CreateHandle (createParams);
1178
1179                         if( window.Handle != IntPtr.Zero) {
1180                                 if( !controlsCollection.Contains( window.Handle ) )
1181                                         controlsCollection.Add( window.Handle, this );
1182
1183                                 SubclassWindow();
1184
1185                                 CreatorThreadId_ = Win32.GetCurrentThreadId();
1186
1187                                 OnHandleCreated ( EventArgs.Empty );
1188                         }
1189                 }
1190         
1191                 protected virtual void DefWndProc (ref Message m)
1192                 {
1193                         window.DefWndProc(ref m);
1194                 }
1195                 
1196                 protected virtual void DestroyHandle ()
1197                 {
1198                         if ( IsHandleCreated ) {
1199                                 if( Handle != IntPtr.Zero) {
1200                                         controlsCollection.Remove(Handle);
1201                                 }
1202                                 if( window != null) {
1203                                         window.DestroyHandle ();
1204                                 }
1205                         }
1206                 }
1207         
1208                 protected override void Dispose ( bool disposing ) 
1209                 {
1210                         lock ( this ) {
1211                                 statuses [ DISPOSING ] = true;
1212                                 try {
1213                                         if ( disposing ) {
1214                                                 DestroyHandle ( );
1215                                         }
1216                                         // close/free unmanaged resources
1217                                 }
1218                                 finally {
1219                                         base.Dispose( disposing );
1220                                 }
1221                                 statuses [ DISPOSED ] = true;
1222                         }
1223                 }
1224         
1225                 [MonoTODO]
1226                 public DragDropEffects DoDragDrop (
1227                         object data, DragDropEffects allowedEffects)
1228                 {
1229                         throw new NotImplementedException ();
1230                 }
1231         
1232                 //public object EndInvoke(IAsyncResult asyncResult):
1233                 //look under ISynchronizeInvoke methods
1234         
1235                 [MonoTODO]
1236                 public Form FindForm () 
1237                 {
1238                         throw new NotImplementedException ();
1239                 }
1240         
1241                         //Compact Framework
1242                 public bool Focus () 
1243                 {
1244                         if (Win32.SetFocus (Handle) != (IntPtr) 0)
1245                                 return true;
1246                         return false;
1247                 }
1248         
1249                 [MonoTODO]
1250                 public static Control FromChildHandle (IntPtr handle) 
1251                 {
1252                         Control control  = null;
1253                         IntPtr  controlHwnd = handle;
1254                                 while( controlHwnd != IntPtr.Zero) {
1255                                         control  = controlsCollection[controlHwnd] as Control;
1256                                         if( control != null) break;
1257                                         controlHwnd = Win32.GetParent(controlHwnd);
1258                                 }
1259                                 return control;                         
1260                 }
1261         
1262                 public static Control FromHandle (IntPtr handle) 
1263                 {
1264                                 // FIXME: Here we have to check, whether control already exists
1265                         //Control control = new Control (handle);
1266                                 Control control  = controlsCollection[handle] as Control;
1267                         return control;
1268                 }
1269         
1270                 [MonoTODO]
1271                 public Control GetChildAtPoint (Point pt) 
1272                 {
1273                         throw new NotImplementedException ();
1274                 }
1275         
1276                 // [MonoTODO]
1277                 //public IContainerControl GetContainerControl () 
1278                 //{
1279                 //      throw new NotImplementedException ();
1280                 //}
1281
1282                 internal Control getNextFocusedControlCore ( Control parent, Control ctl, bool forward )
1283                 {
1284                         while ( parent.Parent != null )
1285                                 parent = parent.Parent;
1286
1287                         Control next = parent.GetNextControl ( ctl, forward );
1288                         while ( next != null ) {
1289                                 if ( next.TabStop && next.CanFocus )
1290                                         return next;
1291                                 next = parent.GetNextControl ( next, forward );
1292                         }
1293                         return null;
1294                 }
1295
1296                 internal Control getNextFocusedControl ( Control parent, bool forward )
1297                 {
1298                         Control next = getNextFocusedControlCore ( parent, FocusedControl, forward );
1299                         if ( next == null )
1300                                 next = getNextFocusedControlCore ( parent, null, forward );
1301                         return next;
1302                 }
1303                 
1304                 [MonoTODO]
1305                 public Control GetNextControl ( Control ctl, bool forward ) 
1306                 {
1307                         Control next = null;
1308
1309                         if ( ctl == null ) 
1310                                 next = Controls.GetFirstControl ( forward );
1311                         else {
1312                                 if ( forward )
1313                                         next = getNextControlForward ( ctl );
1314                                 else
1315                                         next = getNextControlBackward ( ctl );
1316                         }
1317                         return next;
1318                 }
1319
1320                 private Control getNextControlForward ( Control ctl ) 
1321                 {
1322                         if ( ctl.Controls.Count != 0 )
1323                                 return ctl.Controls.GetFirstControl ( true );
1324
1325                         Control parent = ctl.Parent;
1326                         if ( parent != null ) {
1327                                 while ( parent != null ) {
1328                                         Control next = parent.Controls.GetNextControl ( ctl, true );
1329                                         if ( next != null )
1330                                                 return next;
1331                                         ctl = parent;
1332                                         parent = parent.Parent;
1333                                 }
1334                                 return null;
1335                         }
1336                         else
1337                                 return Controls.GetFirstControl ( true );
1338                 }
1339
1340                 private Control getNextControlBackward ( Control ctl ) 
1341                 {
1342                         Control parent = ctl.Parent;
1343                         if ( parent != null ) {
1344                                 Control next = parent.Controls.GetNextControl ( ctl, false );
1345                                 if ( next != null ) {
1346                                         if ( next.Controls.Count > 0 )
1347                                                 return next.Controls.GetFirstControl ( false );
1348                                         else
1349                                                 return next;
1350                                 }
1351                                 return parent;
1352                         }
1353                         else
1354                                 return Controls.GetFirstControl ( false );
1355                 }
1356
1357                 [MonoTODO]
1358                 protected bool GetStyle (ControlStyles flag) 
1359                 {
1360                         return (controlStyles_ & flag) != 0;
1361                 }
1362         
1363                 [MonoTODO]
1364                 protected bool GetTopLevel () 
1365                 {
1366                         throw new NotImplementedException ();
1367                 }
1368                 
1369                 public void Hide ()
1370                 {
1371                         Visible = false;
1372                 }
1373                 
1374                 [MonoTODO]
1375                 protected virtual void InitLayout () 
1376                 {
1377                                 //FIXME:
1378                 }
1379                 
1380                         //Compact Framework
1381                 public void Invalidate () 
1382                 {
1383                         if (IsHandleCreated) {
1384                                         Win32.InvalidateRect(Handle, IntPtr.Zero, 1);
1385                         }
1386                 }
1387                 
1388                 public void Invalidate ( bool invalidateChildren ) 
1389                 {
1390                         Invalidate ( ) ;
1391                         if ( invalidateChildren ) {
1392                                 foreach ( Control child in Controls )
1393                                         child.Invalidate ( invalidateChildren );
1394                         }
1395                 }
1396                 
1397                 // tries to find appropriate owner for modal form
1398                 internal static Control getOwnerWindow ( Control skip )
1399                 {
1400                         // temporary solution
1401                         IEnumerator cw = controlsCollection.GetEnumerator ( );
1402
1403                         while ( cw.MoveNext ( ) ) {
1404                                 Control c = ( (DictionaryEntry) cw.Current).Value as Control;
1405                                 if ( c != null && c != skip ) {
1406                                         IntPtr parent = Win32.GetParent ( c.Handle );
1407                                         IntPtr owner  = Win32.GetWindow ( c.Handle, (uint) GetWindowConstants.GW_OWNER );
1408                                         if ( parent == IntPtr.Zero && owner == IntPtr.Zero )
1409                                                 return c;
1410                                 }
1411                         }
1412                         return null;
1413                 }
1414
1415                         //Compact Framework
1416                 public void Invalidate (Rectangle rc) 
1417                 {
1418                         if (IsHandleCreated) {
1419                                 RECT rect = new RECT();
1420                                 rect.left = rc.Left;
1421                                 rect.top = rc.Top;
1422                                 rect.right = rc.Right;
1423                                 rect.bottom = rc.Bottom;
1424                                 Win32.InvalidateRect (Handle, ref rect, true);
1425                         }
1426                 }
1427                 
1428                 [MonoTODO]
1429                 public void Invalidate(Region region) 
1430                 {
1431                                 //FIXME:
1432                         }
1433                 
1434                 [MonoTODO]
1435                 public void Invalidate (Rectangle rc, bool invalidateChildren) 
1436                 {
1437                                 //FIXME:
1438                 }
1439                 
1440                 [MonoTODO]
1441                 public void Invalidate(Region region,bool invalidateChildren) 
1442                 {
1443                                 //FIXME:
1444                         }
1445                 
1446                 [MonoTODO]
1447                 protected void InvokeGotFocus (Control toInvoke, EventArgs e) 
1448                 {
1449                                 //FIXME:
1450                         }
1451                 
1452                 [MonoTODO]
1453                 protected void InvokeLostFocus (Control toInvoke, EventArgs e) 
1454                 {
1455                                 //FIXME:
1456                         }
1457                 
1458                 [MonoTODO]
1459                 protected void InvokeOnClick (Control toInvoke, EventArgs e) 
1460                 {
1461                                 //FIXME:
1462                         }
1463                 
1464                 [MonoTODO]
1465                 protected void InvokePaint (Control c, PaintEventArgs e) 
1466                 {
1467                                 //FIXME:
1468                         }
1469                 
1470                 [MonoTODO]
1471                 protected void InvokePaintBackground (
1472                         Control c,PaintEventArgs e) 
1473                 {
1474                                 //FIXME:
1475                         }
1476                 
1477                 [MonoTODO]
1478                 protected virtual bool IsInputChar (char charCode)
1479                 {
1480                         return true;
1481                 }
1482                 
1483                 [MonoTODO]
1484                 protected virtual bool IsInputKey (Keys keyData) 
1485                 {
1486                         return false;
1487                 }
1488                 
1489                 public static bool IsMnemonic (char charCode, string text)
1490                 {
1491                         if ( text == null )
1492                                 return false;
1493                         return text.IndexOf ( "&" + charCode ) > 0;
1494
1495                 }
1496                 
1497                 // methods used with events:
1498                 protected virtual void OnBackColorChanged (EventArgs e)
1499                 {
1500                         if (BackColorChanged != null)
1501                                 BackColorChanged (this, e);
1502
1503                                 foreach( Control ctl in Controls) {
1504                                         ctl.OnParentBackColorChanged(e);
1505                                 }
1506                         }
1507                 
1508                 protected virtual void OnBackgroundImageChanged (EventArgs e)
1509                 {
1510                         if (BackgroundImageChanged != null) 
1511                                 BackgroundImageChanged (this, e);
1512                 }
1513                 
1514                 protected virtual void OnBindingContextChanged (EventArgs e)
1515                 {
1516                         if (BindingContextChanged != null)
1517                                 BindingContextChanged (this, e);
1518                 }
1519                 
1520                 protected virtual void OnCausesValidationChanged (EventArgs e)
1521                 {
1522                         if (CausesValidationChanged != null)
1523                                 CausesValidationChanged (this, e);
1524                 }
1525                 
1526                 protected virtual void OnChangeUICues(UICuesEventArgs e) 
1527                 {
1528                         if (ChangeUICues != null)
1529                                 ChangeUICues (this, e);
1530                 }
1531                 
1532                         //Compact Framework
1533                 protected virtual void OnClick (EventArgs e)
1534                 {
1535                         if (Click != null)
1536                                 Click (this, e);
1537                 }
1538                 
1539     
1540                 protected virtual void OnContextMenuChanged (EventArgs e)
1541                 {
1542                         if (ContextMenuChanged != null)
1543                                 ContextMenuChanged (this, e);
1544                 }
1545                 
1546                 protected virtual void OnControlAdded (ControlEventArgs e)
1547                 {       
1548                         if ( Created ) {
1549                                 e.Control.CreateControl ( );
1550                         }
1551                         e.Control.Visible = Visible;
1552
1553                         if (ControlAdded != null)
1554                                 ControlAdded (this, e);
1555                 }
1556                 
1557                 protected virtual void OnControlRemoved (ControlEventArgs e)
1558                 {
1559                         if (ControlRemoved != null)
1560                                 ControlRemoved (this, e);
1561                 }
1562                 
1563                 protected virtual void OnCreateControl ()
1564                 {
1565                                 //FIXME:
1566                         // create all child windows
1567                         IEnumerator cw = childControls.GetEnumerator();
1568     
1569                         while (cw.MoveNext()) {
1570                                 Console.WriteLine ("Adding Control");
1571                                 Control control = (Control) cw.Current;
1572                                 control.CreateControl ();
1573                                 control.Show ();
1574                         }
1575                         }
1576                 
1577                 protected virtual void OnCursorChanged (EventArgs e)
1578                 {
1579                         if (CursorChanged != null)
1580                                 CursorChanged (this, e);
1581                 }
1582                 
1583                 protected virtual void OnDockChanged (EventArgs e)
1584                 {
1585                         // changing this property does not affect the control directly
1586                         // so have its parent to calculate new layout
1587                         if ( Parent != null )
1588                                 Parent.PerformLayout ( this, "Dock" );
1589                         if (DockChanged != null)
1590                                 DockChanged (this, e);
1591                 }
1592                 
1593                 protected virtual void OnDoubleClick (EventArgs e)
1594                 {
1595                         if (DoubleClick != null)
1596                                 DoubleClick (this, e);
1597                 }
1598                 
1599                 protected virtual void OnDragDrop (DragEventArgs e)
1600                 {
1601                         if (DragDrop != null)
1602                                 DragDrop (this, e);
1603                 }
1604                 
1605                 protected virtual void OnDragEnter (DragEventArgs e)
1606                 {
1607                         if (DragEnter != null)
1608                                 DragEnter (this, e);
1609                 }
1610                 
1611                 protected virtual void OnDragLeave (EventArgs e)
1612                 {
1613                         if (DragLeave != null)
1614                                 DragLeave (this, e);
1615                 }
1616                 
1617                 protected virtual void OnDragOver (DragEventArgs e)
1618                 {
1619                         if (DragOver != null)
1620                                 DragOver (this, e);
1621                 }
1622                 
1623                         //Compact Framework
1624                 protected virtual void OnEnabledChanged (EventArgs e)
1625                 {
1626                         if (EnabledChanged != null)
1627                                 EnabledChanged (this, e);
1628                 }
1629                 
1630                 protected virtual void OnEnter (EventArgs e)
1631                 {
1632                         if (Enter != null)
1633                                 Enter (this, e);
1634                 }
1635                 
1636                 protected virtual void OnFontChanged (EventArgs e)
1637                 {
1638                         if (FontChanged != null)
1639                                 FontChanged (this, e);
1640                 }
1641                 
1642                 protected virtual void OnForeColorChanged (EventArgs e) 
1643                 {
1644                         if (ForeColorChanged != null)
1645                                 ForeColorChanged (this, e);
1646                 }
1647                 
1648                 protected virtual void OnGiveFeedback (GiveFeedbackEventArgs e)
1649                 {
1650                         if (GiveFeedback != null)
1651                                 GiveFeedback (this, e);
1652                 }
1653                 
1654                         //Compact Framework
1655                 protected virtual void OnGotFocus (EventArgs e) 
1656                 {
1657                         if (GotFocus != null)
1658                                 GotFocus (this, e);
1659                 }
1660                 
1661                 protected virtual void OnHandleCreated (EventArgs e) 
1662                 {
1663                         Console.WriteLine ("OnHandleCreated");
1664
1665                                 //if( font != null) {
1666                                 //      Win32.SendMessage( Handle, Msg.WM_SETFONT, font.ToHfont().ToInt32(), 0);
1667                                 //}
1668                                 Win32.SendMessage( Handle, Msg.WM_SETFONT, Font.ToHfont().ToInt32(), 0);
1669                                 Win32.SetWindowText( Handle, text);
1670
1671                         if (HandleCreated != null)
1672                                 HandleCreated (this, e);
1673     
1674                 }
1675                 
1676                 protected virtual void OnHandleDestroyed (EventArgs e) 
1677                 {
1678                                 if( Handle != IntPtr.Zero) {
1679                                         controlsCollection.Remove(Handle);
1680                                 }
1681                                 
1682                         if (HandleDestroyed != null) {
1683                                 HandleDestroyed (this, e);
1684                         }
1685                 }
1686                 
1687                 protected virtual void OnHelpRequested (HelpEventArgs e) 
1688                 {
1689                         if (HelpRequested != null)
1690                                 HelpRequested (this, e);
1691                 }
1692                 
1693                 protected virtual void OnImeModeChanged (EventArgs e) 
1694                 {
1695                         if (ImeModeChanged != null)
1696                                 ImeModeChanged (this, e);
1697                 }
1698                 
1699                 protected virtual void OnInvalidated (InvalidateEventArgs e) 
1700                 {
1701                         if (Invalidated != null)
1702                                 Invalidated (this, e);
1703                 }
1704                 
1705                         //Compact Framework
1706                 protected virtual void OnKeyDown (KeyEventArgs e) 
1707                 {
1708                         if (KeyDown != null)
1709                                 KeyDown (this, e);
1710                 }
1711                 
1712                         //Compact Framework
1713                 protected virtual void OnKeyPress (KeyPressEventArgs e) 
1714                 {
1715                         if (KeyPress != null)
1716                                 KeyPress (this, e);
1717                 }
1718                 
1719                         //Compact Framework
1720                 protected virtual void OnKeyUp (KeyEventArgs e) 
1721                 {
1722                         if (KeyUp != null)
1723                                 KeyUp (this, e);
1724     
1725                 }
1726                 
1727                 protected virtual void OnLayout (LayoutEventArgs e) 
1728                 {
1729                         DoDockAndAnchorLayout ( e );
1730                         if (Layout != null)
1731                                 Layout (this, e);
1732                 }
1733                 
1734                 protected virtual void OnLeave (EventArgs e) 
1735                 {
1736                         if (Leave != null)
1737                                 Leave (this, e);
1738                 }
1739                 
1740                 protected virtual void OnLocationChanged (EventArgs e) 
1741                 {
1742                         if (LocationChanged != null)
1743                                 LocationChanged (this, e);
1744                 }
1745                 
1746                         //Compact Framework
1747                 protected virtual void OnLostFocus (EventArgs e) 
1748                 {
1749                         if (LostFocus != null)
1750                                 LostFocus (this, e);
1751                 }
1752                 
1753                         //Compact Framework
1754                 protected virtual void OnMouseDown (MouseEventArgs e) 
1755                 {
1756                         if (MouseDown != null)
1757                                 MouseDown (this, e);
1758                 }
1759                 
1760                 protected virtual void OnMouseEnter (EventArgs e) 
1761                 {
1762                                 //System.Console.WriteLine("OnMouseEnter");
1763                         if (MouseEnter != null)
1764                                 MouseEnter (this, e);
1765                 }
1766     
1767                 protected virtual void OnMouseHover (EventArgs e) 
1768                 {
1769                         if (MouseHover != null)
1770                                 MouseHover (this, e);
1771                 }
1772                 
1773                 protected virtual void OnMouseLeave (EventArgs e) 
1774                 {
1775                                 //System.Console.WriteLine("OnMouseLeave");
1776
1777                                 mouseIsInside_ = false;
1778                         if (MouseLeave != null)
1779                                 MouseLeave (this, e);
1780                 }
1781                 
1782                         //Compact Framework
1783                 protected virtual void OnMouseMove (MouseEventArgs e) 
1784                 {
1785                                 // If enter and mouse pressed - do not process
1786                                 if( ((e.Button & MouseButtons.Left) != 0) && !mouseIsInside_) return;
1787
1788                                 if( !mouseIsInside_) {
1789                                         TRACKMOUSEEVENT tme = new TRACKMOUSEEVENT();
1790                                         tme.cbSize = 16;
1791                                         tme.hWnd = Handle;
1792                                         tme.dwFlags = (int)TrackerEventFlags.TME_LEAVE;
1793                                         tme.dwHoverTime = 0;
1794
1795                                         bool result = Win32.TrackMouseEvent(ref tme);
1796                                         if( !result) {
1797                                                 System.Console.WriteLine("{0}",Win32.FormatMessage(Win32.GetLastError()));
1798                                         }
1799                                 }
1800
1801                                 POINT pt = new POINT();
1802                                 pt.x = e.X;
1803                                 pt.y = e.Y;
1804                                 Win32.ClientToScreen(Handle, ref pt);
1805                                 IntPtr wndUnderMouse = Win32.WindowFromPoint(pt);
1806
1807                                 if( wndUnderMouse != Handle) {
1808                                         // we are outside of the window
1809                                         if( mouseIsInside_) {
1810                                                 OnMouseLeave(new EventArgs());
1811                                                 mouseIsInside_ = false;
1812                                         }
1813                                 }
1814                                 else {
1815                                         if( !mouseIsInside_) {
1816                                                 mouseIsInside_ = true;
1817                                                 OnMouseEnter(new EventArgs());
1818                                         }
1819                                 }
1820                         if (MouseMove != null)
1821                                 MouseMove (this, e);
1822                 }
1823                 
1824                         //Compact Framework
1825                 protected virtual void OnMouseUp (MouseEventArgs e) 
1826                 {
1827                         if (MouseUp != null)
1828                                 MouseUp (this, e);
1829                 }
1830                 
1831                 protected virtual void OnMouseWheel (MouseEventArgs e) 
1832                 {
1833                         if (MouseWheel != null)
1834                                 MouseWheel (this, e);
1835                 }
1836                 
1837                 protected virtual void OnMove (EventArgs e) 
1838                 {
1839                         if (Move != null)
1840                                 Move (this, e);
1841                 }
1842                 
1843                 protected virtual void OnNotifyMessage (Message m) 
1844                 {
1845                                 //FIXME:
1846                         }
1847                 
1848                         //Compact Framework
1849                 protected virtual void OnPaint (PaintEventArgs e)
1850                 {
1851                         if (Paint != null)
1852                                 Paint (this, e);
1853                 }
1854                 
1855                         //Compact Framework
1856                 protected virtual void OnPaintBackground (PaintEventArgs e) 
1857                 {
1858                 if( GetStyle(ControlStyles.UserPaint)) {
1859                     Brush br = new SolidBrush(BackColor);
1860                     e.Graphics.FillRectangle(br, e.ClipRectangle);
1861                     br.Dispose();
1862                 }
1863             }
1864                 
1865                 protected virtual void OnParentBackColorChanged (EventArgs e) 
1866                 {
1867                                 BackColor = Parent.BackColor;
1868                                 // FIXME: setting BackColor fires the BackColorChanged event,
1869                                 // so we do not need to call this here
1870                                 /*
1871                         if (BackColorChanged != null)
1872                                 BackColorChanged (this, e);
1873                                 */                                      
1874                 }
1875                 
1876                 protected virtual void OnParentBackgroundImageChanged (
1877                         EventArgs e) 
1878                 {
1879                         if (BackgroundImageChanged != null)
1880                                 BackgroundImageChanged (this, e);
1881                 }
1882                 
1883                 protected virtual void OnParentBindingContextChanged (
1884                         EventArgs e) 
1885                 {
1886                         if (BindingContextChanged != null)
1887                                 BindingContextChanged (this, e);
1888                 }
1889                 
1890                         //Compact Framework
1891                 protected virtual void OnParentChanged (EventArgs e) 
1892                 {
1893                         if (ParentChanged != null)
1894                                 ParentChanged (this, e);
1895                 }
1896                 
1897                 protected virtual void OnParentEnabledChanged (EventArgs e) 
1898                 {
1899                         if (EnabledChanged != null)
1900                                 EnabledChanged (this, e);
1901                 }
1902                 
1903                 protected virtual void OnParentFontChanged (EventArgs e) 
1904                 {
1905                         if (FontChanged != null)
1906                                 FontChanged (this, e);
1907                 }
1908                 
1909                 protected virtual void OnParentForeColorChanged (EventArgs e) 
1910                 {
1911                         if (ForeColorChanged != null)
1912                                 ForeColorChanged (this, e);
1913                 }
1914                 
1915                 protected virtual void OnParentRightToLeftChanged (
1916                         EventArgs e) 
1917                 {
1918                         if (RightToLeftChanged != null)
1919                                 RightToLeftChanged (this, e);
1920                 }
1921                 
1922                 protected virtual void OnParentVisibleChanged (EventArgs e) 
1923                 {
1924                         if (VisibleChanged != null)
1925                                 VisibleChanged (this, e);
1926                 }
1927                 
1928                 protected virtual void OnQueryContinueDrag (
1929                         QueryContinueDragEventArgs e) 
1930                 {
1931                         if (QueryContinueDrag != null)
1932                                 QueryContinueDrag (this, e);
1933                 }
1934                 
1935                         //Compact Framework
1936                 protected virtual void OnResize (EventArgs e) 
1937                 {
1938                         if (Resize != null)
1939                                 Resize (this, e);
1940
1941                         PerformLayout ( this, "Bounds" );
1942                 }
1943                 
1944                 protected virtual void OnRightToLeftChanged (EventArgs e) 
1945                 {
1946                         if (RightToLeftChanged != null)
1947                                 RightToLeftChanged (this, e);
1948                 }
1949                 
1950                 protected virtual void OnSizeChanged (EventArgs e) 
1951                 {
1952                         OnResize ( e );
1953                         if (SizeChanged != null)
1954                                 SizeChanged (this, e);
1955                 }
1956                 
1957                 protected virtual void OnStyleChanged (EventArgs e) 
1958                 {
1959                         if (StyleChanged != null)
1960                                 StyleChanged (this, e);
1961                 }
1962                 
1963                 protected virtual void OnSystemColorsChanged (EventArgs e) 
1964                 {
1965                         if (SystemColorsChanged != null)
1966                                 SystemColorsChanged (this, e);
1967                 }
1968                 
1969                 protected virtual void OnTabIndexChanged (EventArgs e) 
1970                 {
1971                         if (TabIndexChanged != null)
1972                                 TabIndexChanged (this, e);
1973                 }
1974                 
1975                 protected virtual void OnTabStopChanged (EventArgs e) 
1976                 {
1977                         if (TabStopChanged != null)
1978                                 TabStopChanged (this, e);
1979                 }
1980                 
1981                         //Compact Framework
1982                 protected virtual void OnTextChanged (EventArgs e) 
1983                 {
1984                         if (TextChanged != null)
1985                                 TextChanged (this, e);
1986                 }
1987     
1988                 //[MonoTODO] // this doesn't seem to be documented
1989     //          protected virtual void OnTextAlignChanged (EventArgs e) {
1990     //                  TextAlignChanged (this, e);
1991     //          }
1992                 
1993                 protected virtual void OnValidated (EventArgs e) 
1994                 {
1995                         if (Validated != null)
1996                                 Validated (this, e);
1997                 }
1998                 
1999                 //[MonoTODO]
2000                 // CancelEventArgs not ready
2001                 //protected virtual void OnValidating(CancelEventArgs e) 
2002                 //{
2003                 //      throw new NotImplementedException ();
2004                 //}
2005                 
2006                 [MonoTODO]
2007                 protected virtual void OnVisibleChanged (EventArgs e) 
2008                 {
2009                         if (VisibleChanged != null)
2010                                 VisibleChanged (this, e);
2011
2012                         PerformLayout ( );
2013                 }
2014                 // --- end of methods for events ---
2015                 
2016                 
2017                 [MonoTODO]
2018                 public void PerformLayout () 
2019                 {
2020                         PerformLayout ( null, null );
2021                 }
2022                 
2023                 [MonoTODO]
2024                 public void PerformLayout (Control affectedControl,
2025                                            string affectedProperty) 
2026                 {
2027                         if ( !statuses [ LAYOUT_SUSPENDED ] )
2028                                 OnLayout ( new LayoutEventArgs ( affectedControl, affectedProperty ) );
2029                         else
2030                                 statuses [ LAYOUT_PENDING ] = true;
2031                 }
2032                 
2033                         //Compact Framework
2034                 [MonoTODO]
2035                 public Point PointToClient (Point p) 
2036                 {
2037                         throw new NotImplementedException ();
2038                 }
2039                 
2040                         //Compact Framework
2041                 [MonoTODO]
2042                 public Point PointToScreen (Point p) 
2043                 {
2044                         throw new NotImplementedException ();
2045                 }
2046                 
2047                 [MonoTODO]
2048                 public virtual bool PreProcessMessage (ref Message msg) 
2049                 {
2050                         if ( msg.Msg == Msg.WM_KEYDOWN ) {
2051                                 Keys keyData = (Keys)msg.WParam.ToInt32( );
2052                                 if ( !ProcessCmdKey ( ref msg, keyData ) ) {
2053                                         if ( IsInputKey ( keyData ) )
2054                                                 return false;
2055
2056                                         return ProcessDialogKey ( keyData );
2057                                 }
2058                                 return true;
2059                         }
2060                         else if ( msg.Msg == Msg.WM_CHAR ) {
2061                                 if ( IsInputChar ( (char) msg.WParam ) )
2062                                         return false;
2063
2064                                 return ProcessDialogChar ( (char) msg.WParam );
2065                         }
2066
2067                         return false;
2068                 }
2069                 
2070                 [MonoTODO]
2071                 protected virtual bool ProcessCmdKey (ref Message msg,
2072                                                       Keys keyData) 
2073                 {
2074                         // do something with context menu
2075
2076                         if ( Parent != null )
2077                                 return Parent.ProcessCmdKey ( ref msg, keyData );
2078                         return false;
2079                 }
2080                 
2081                 [MonoTODO]
2082                 protected virtual bool ProcessDialogChar (char charCode) 
2083                 {
2084                         if ( Parent != null )
2085                                 return Parent.ProcessDialogChar ( charCode );
2086                         return false;
2087                 }
2088                 
2089                 [MonoTODO]
2090                 protected virtual bool ProcessDialogKey (Keys keyData) 
2091                 {
2092                         if ( Parent != null )
2093                                 return Parent.ProcessDialogKey ( keyData );
2094                         return false;
2095                 }
2096                 
2097                 [MonoTODO]
2098                 protected virtual bool ProcessKeyEventArgs (ref Message m) 
2099                 {
2100                         bool handled = false;
2101
2102                         switch ( m.Msg ) {
2103                         case Msg.WM_KEYDOWN:
2104                                 KeyEventArgs args_down = new KeyEventArgs ( (Keys)m.WParam.ToInt32() );
2105                                 OnKeyDown ( args_down );
2106                                 handled = args_down.Handled;
2107                         break;                  
2108                         case Msg.WM_CHAR:
2109                                 KeyPressEventArgs args_press = new KeyPressEventArgs ( (char) m.WParam );
2110                                 OnKeyPress ( args_press );
2111                                 handled = args_press.Handled;
2112                         break;
2113                         case Msg.WM_KEYUP:
2114                                 KeyEventArgs args_up = new KeyEventArgs ( (Keys)m.WParam.ToInt32() );
2115                                 OnKeyUp ( args_up );
2116                                 handled = args_up.Handled;
2117                         break;
2118                         }
2119                         
2120                         return handled;
2121                 }
2122                 
2123                 [MonoTODO]
2124                 protected internal virtual bool ProcessKeyMessage ( ref Message m) 
2125                 {
2126                         if ( Parent != null ) {
2127                                 if ( !Parent.ProcessKeyPreview ( ref m ) )
2128                                         return ProcessKeyEventArgs ( ref m );
2129                         }
2130                         return false;
2131                 }
2132                 
2133                 [MonoTODO]
2134                 protected virtual bool ProcessKeyPreview (ref Message m) 
2135                 {
2136                         if ( Parent != null )
2137                                 return Parent.ProcessKeyPreview ( ref m );
2138                         return false;
2139                 }
2140                 
2141                 // This default implementation of the ProcessMnemonic method simply
2142                 // returns false to indicate that the control has no mnemonic.
2143                 protected virtual bool ProcessMnemonic (char charCode) 
2144                 {
2145                         return false;
2146                 }
2147                 
2148                 // used when properties/values of Control 
2149                 // are big enough to warrant recreating the HWND
2150                 protected void RecreateHandle() 
2151                 {
2152                         statuses [ RECREATING_HANDLE ] = true;
2153                         if( IsHandleCreated) {
2154                                 DestroyHandle ();
2155                                 CreateHandle ();
2156
2157                                 UpdateZOrder ( );
2158
2159                                 IEnumerator cw = childControls.GetEnumerator();
2160                                 while ( cw.MoveNext() )
2161                                         (( Control )cw.Current).RecreateHandle ( );
2162
2163                         }
2164                         statuses [ RECREATING_HANDLE ] = false;
2165                 }
2166                 
2167                         //Compact Framework
2168                 [MonoTODO]
2169                 public Rectangle RectangleToClient (Rectangle r) 
2170                 {
2171                                 // FIXME: What to return if Handle is not created yet ?
2172                                 RECT rect = new RECT();
2173                                 rect.left = r.Left;
2174                                 rect.top = r.Top;
2175                                 rect.right = r.Right;
2176                                 rect.bottom = r.Bottom;
2177                                 Win32.ScreenToClient(Handle,ref rect);
2178                                 return new Rectangle( rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
2179                 }
2180                 
2181                         //Compact Framework
2182                 [MonoTODO]
2183                 public Rectangle RectangleToScreen (Rectangle r) 
2184                 {
2185                                 // FIXME: What to return if Handle is not created yet ?
2186                                 RECT rect = new RECT();
2187                                 rect.left = r.Left;
2188                                 rect.top = r.Top;
2189                                 rect.right = r.Right;
2190                                 rect.bottom = r.Bottom;
2191                                 Win32.ClientToScreen(Handle,ref rect);
2192                                 return new Rectangle( rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
2193                         }
2194                 
2195                 [MonoTODO]
2196                 protected static bool ReflectMessage (IntPtr hWnd, ref Message m) {
2197                                 bool result = false;
2198                                 Control cntrl = Control.FromHandle( hWnd);
2199                                 if( cntrl != null) {
2200                                         cntrl.WndProc(ref m);
2201                                         result = true;
2202                                 }
2203                                 return result;
2204                         }
2205                 
2206                         //Compact Framework
2207                 public virtual void Refresh () 
2208                 {
2209                         //RECT rect = (RECT) null;
2210                         //InvalidateRect (Handle, ref rect, true);
2211                         Win32.UpdateWindow (Handle);
2212                 }
2213                 
2214                 [MonoTODO]
2215                 public virtual void ResetBackColor () 
2216                 {
2217                                 //FIXME:
2218                         }
2219                 
2220                 [MonoTODO]
2221                 public void ResetBindings () 
2222                 {
2223                                 //FIXME:
2224                         }
2225                 
2226                 [MonoTODO]
2227                 public virtual void ResetFont () 
2228                 {
2229                                 //FIXME:
2230                         }
2231                 
2232                 [MonoTODO]
2233                 public virtual void ResetForeColor () 
2234                 {
2235                                 //FIXME:
2236                         }
2237                 
2238                 [MonoTODO]
2239                 public void ResetImeMode () 
2240                 {
2241                                 //FIXME:
2242                         }
2243                 
2244                 [MonoTODO]
2245                 public virtual void ResetRightToLeft () 
2246                 {
2247                                 //FIXME:
2248                         }
2249                 
2250                 [MonoTODO]
2251                 public virtual void ResetText () 
2252                 {
2253                                 //FIXME:
2254                         }
2255                 
2256                 [MonoTODO]
2257                 public void ResumeLayout () 
2258                 {
2259                         statuses [ LAYOUT_SUSPENDED ] = false;
2260                         if ( statuses [ LAYOUT_PENDING ] ) {
2261                                 PerformLayout ( );
2262                                 statuses [ LAYOUT_PENDING ] = false;
2263                         }
2264                 }
2265                 
2266                 [MonoTODO]
2267                 public void ResumeLayout (bool performLayout) 
2268                 {
2269                         statuses [ LAYOUT_SUSPENDED ] = false;
2270                         if ( performLayout && statuses [ LAYOUT_PENDING ] ) {
2271                                 PerformLayout ( );
2272                                 statuses [ LAYOUT_PENDING ] = false;
2273                         }
2274                 }
2275                 
2276                 [MonoTODO]
2277                 protected ContentAlignment RtlTranslateAlignment (
2278                         ContentAlignment align) 
2279                 {
2280                         throw new NotImplementedException ();
2281                 }
2282                 
2283                 [MonoTODO]
2284                 protected HorizontalAlignment RtlTranslateAlignment (
2285                         HorizontalAlignment align) 
2286                 {
2287                         throw new NotImplementedException ();
2288                 }
2289                 
2290                 [MonoTODO]
2291                 protected LeftRightAlignment RtlTranslateAlignment (
2292                         LeftRightAlignment align) 
2293                 {
2294                         throw new NotImplementedException ();
2295                 }
2296                 
2297                 [MonoTODO]
2298                 protected ContentAlignment RtlTranslateContent (
2299                         ContentAlignment align) 
2300                 {
2301                         throw new NotImplementedException ();
2302                 }
2303                 
2304                 [MonoTODO]
2305                 protected HorizontalAlignment RtlTranslateHorizontal (
2306                         HorizontalAlignment align) 
2307                 {
2308                         throw new NotImplementedException ();
2309                 }
2310                 
2311                 [MonoTODO]
2312                 protected LeftRightAlignment RtlTranslateLeftRight (
2313                         LeftRightAlignment align) 
2314                 {
2315                         throw new NotImplementedException ();
2316                 }
2317                 
2318                 [MonoTODO]
2319                 public void Scale (float ratio) 
2320                 {
2321                         Scale ( ratio, ratio );
2322                 }
2323                 
2324                 [MonoTODO]
2325                 public void Scale (float dx,float dy) 
2326                 {
2327                         SuspendLayout();
2328                         ScaleCore ( dx, dy );
2329                         IEnumerator cw = childControls.GetEnumerator();
2330                         while ( cw.MoveNext() ) {
2331                                 Control control = (Control) cw.Current;
2332                                 control.Scale ( dx, dy );
2333                         }
2334                         ResumeLayout();
2335                 }
2336                 
2337                 [MonoTODO]
2338                 protected virtual void ScaleCore (float dx, float dy) 
2339                 {
2340                         Location   = new Point ( (int) (Left * dx), (int) (Top * dy) );
2341                         ClientSize = new Size  ((int) ( ClientSize.Width * dx ),
2342                                                 (int) ( ClientSize.Height * dy) );
2343                 }
2344                 
2345                 [MonoTODO]
2346                 public void Select () 
2347                 {
2348                                 //FIXME:
2349                         }
2350                 
2351                 [MonoTODO]
2352                 protected virtual void Select (bool directed,bool forward) 
2353                 {
2354                                 //FIXME:
2355                         }
2356                 
2357                 [MonoTODO]
2358                 public bool SelectNextControl (Control ctl, bool forward, 
2359                                                bool tabStopOnly, 
2360                                                bool nested, bool wrap)
2361                 {
2362                         throw new NotImplementedException ();
2363                 }
2364                 
2365                         //Compact Framework
2366                 [MonoTODO]
2367                 public void SendToBack () 
2368                 {
2369                                 //FIXME:
2370                 }
2371                 
2372                 [MonoTODO]
2373                 public void SetBounds (int x, int y, int width, int height) 
2374                 {
2375                         SetBounds(x, y, width, height, BoundsSpecified.All);
2376                 }
2377                 
2378                 [MonoTODO]
2379                 public void SetBounds (int x, int y, int width, int height, BoundsSpecified specified) 
2380                 {
2381                         SetBoundsCore( x, y, width, height, specified);
2382                 }
2383                 
2384                 [MonoTODO]
2385                 protected virtual void SetBoundsCore ( int x, int y, int width, int height, BoundsSpecified specified) 
2386                 {
2387                         if( (specified & BoundsSpecified.X) == 0)      x = Left;
2388                         if( (specified & BoundsSpecified.Y) == 0)      y = Top;
2389                         if( (specified & BoundsSpecified.Width) == 0)  width = Width;
2390                         if( (specified & BoundsSpecified.Height) == 0) height = Height;
2391
2392                         if( IsHandleCreated ) {
2393                                 SetWindowPosFlags flags = SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_DRAWFRAME;
2394                                 Win32.SetWindowPos( Handle, SetWindowPosZOrder.HWND_NOTOPMOST, x, y, width, height, flags);
2395                         }
2396
2397                         UpdateBounds( x, y, width, height );
2398                 }
2399                 
2400                 protected virtual bool MenuPresent {
2401                         get { return false; }
2402                 }
2403
2404                 [MonoTODO]
2405                 protected virtual void SetClientSizeCore (int x, int y)
2406                 {
2407                         RECT rc   = new RECT();
2408                         rc.right  = x;
2409                         rc.bottom = y;
2410                         
2411                         CreateParams pars = CreateParams;               
2412                         Win32.AdjustWindowRectEx ( ref rc, pars.Style, MenuPresent ? 1 : 0, pars.ExStyle );
2413
2414                         Size = new Size( rc.right - rc.left, rc.bottom - rc.top );
2415                 }
2416                 
2417                 [MonoTODO]
2418                 protected void SetStyle (ControlStyles flag, bool value) 
2419                 {
2420                         if( value) {
2421                                 controlStyles_ |= flag;
2422                         }
2423                         else {
2424                                 controlStyles_ &= ~flag;
2425                         }
2426                         }
2427                 
2428                 protected void SetTopLevel (bool value)
2429                 {
2430                         if (value)
2431                                 // FIXME: verify on whether this is supposed
2432                                 // to activate/deactive the window
2433                                 Win32.SetWindowPos (Handle, 
2434                                                 SetWindowPosZOrder.HWND_NOTOPMOST,
2435                                                 0, 0, 0, 0, 0);
2436                         else
2437                                 // FIXME: this does not make sense but
2438                                 // the docs say the window is hidden
2439                                 Win32.ShowWindow (Handle, ShowWindowStyles.SW_HIDE);
2440                 }
2441                 
2442                 [MonoTODO]
2443                 protected virtual void SetVisibleCore ( bool value )
2444                 {
2445                         bool visibleChanged = ( visible != value );
2446
2447                         visible = value;
2448
2449                         if ( visibleChanged )
2450                                 OnVisibleChanged ( EventArgs.Empty );
2451
2452                         if ( IsHandleCreated ) {
2453                                 SetWindowPosFlags flags = value ? SetWindowPosFlags.SWP_SHOWWINDOW : SetWindowPosFlags.SWP_HIDEWINDOW;
2454                                 flags |= SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE;
2455                                 Win32.SetWindowPos( Handle, 0, 0, 0, 0, 0, flags );
2456                         }
2457
2458                         foreach ( Control c in Controls )
2459                                 c.Visible = value ;
2460                 }
2461                 
2462                 public void Show () 
2463                 {
2464                         Visible = true;
2465                 }
2466                 
2467                 public void SuspendLayout () 
2468                 {
2469                         statuses [ LAYOUT_SUSPENDED ] = true;
2470                 }
2471                 
2472                         //Compact Framework
2473                 public void Update () 
2474                 {
2475                         Win32.UpdateWindow (Handle);
2476                 }
2477                 
2478                 [MonoTODO]
2479                 protected void UpdateBounds () 
2480                 {       // update control bounds with current size and position
2481
2482                         // currently, this function is called in responce to
2483                         // window events, so I assume that all window handles
2484                         // are created
2485                         RECT rect = new RECT ( );
2486                         Win32.GetWindowRect ( Handle, ref rect );
2487
2488                         IntPtr parent = Win32.GetParent ( Handle );
2489                         if ( parent != IntPtr.Zero ) {
2490                                 Win32.ScreenToClient( parent, ref rect );
2491                         }
2492
2493                         UpdateBounds ( rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top );
2494                 }
2495                 
2496                 [MonoTODO]
2497                 protected void UpdateBounds (int x, int y, int width, int height) 
2498                 {
2499                         int clWidth = width;
2500                         int clHeight = height;
2501
2502                         CreateParams pars = CreateParams;
2503                         // this check should be removed when all controls will use base
2504                         // implementation of CreateParams
2505                         if ( pars != null ) {
2506                                 RECT rc   = new RECT();
2507                                 rc.right  = width;
2508                                 rc.bottom = height;
2509                                 
2510                                 Win32.AdjustWindowRectEx ( ref rc, pars.Style, MenuPresent ? 1 : 0, pars.ExStyle );
2511
2512                                 clWidth  -= ( ( rc.right - rc.left ) - clWidth );
2513                                 clHeight -= ( ( rc.bottom - rc.top ) - clHeight);
2514                         }
2515                         
2516                         UpdateBounds ( x , y, width, height, clWidth, clHeight );
2517                 }
2518                 
2519                 [MonoTODO]
2520                 protected void UpdateBounds (
2521                         int x, int y, int width, int height, int clientWidth,
2522                         int clientHeight)
2523                 {
2524                         oldBounds.X = bounds.X;
2525                         oldBounds.Y = bounds.Y;
2526                         oldBounds.Width = bounds.Width;
2527                         oldBounds.Height = bounds.Height;
2528
2529                         bool bLocationChanged = ( bounds.X != x ) || ( bounds.Y != y );
2530                         bounds.X = x;
2531                         bounds.Y = y;
2532                         
2533                         bool bSizeChanged = ( bounds.Width  != width ) || ( bounds.Height != height );
2534                         bounds.Width  = width;
2535                         bounds.Height = height;
2536
2537                         this.clientWidth   = clientWidth;
2538                         this.clientHeight  = clientHeight;
2539
2540                         if ( bLocationChanged )
2541                                 OnLocationChanged ( EventArgs.Empty );
2542                         if ( bSizeChanged )
2543                                 OnSizeChanged ( EventArgs.Empty );
2544                 }
2545                 
2546                 [MonoTODO]
2547                 protected void UpdateStyles () 
2548                 {
2549                                 //FIXME:
2550                         }
2551                 
2552                 [MonoTODO]
2553                 protected void UpdateZOrder () 
2554                 {
2555                         if ( !IsHandleCreated || Parent == null )
2556                                 return;
2557
2558                         int position = Parent.Controls.GetChildIndex ( this , false );
2559                         switch ( position ) {
2560                         case  0:
2561                                 BringToFront();
2562                         break;
2563                                 // not in collection for some reason
2564                         case -1:
2565                         break;
2566                         default:
2567                                 Control prev = Parent.Controls [ position - 1 ];
2568                                 if ( prev.IsHandleCreated )
2569                                         Win32.SetWindowPos( Handle, prev.Handle, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE );
2570                         break;
2571                         }
2572                 }
2573                 
2574
2575                         internal MouseEventArgs Msg2MouseEventArgs( ref Message msg) {
2576                                 MouseButtons mb = MouseButtons.None;
2577                                 KeyStatusFlags keyIndicator = (KeyStatusFlags)msg.WParam.ToInt32();
2578                                 if( (keyIndicator & KeyStatusFlags.MK_LBUTTON) != 0) {
2579                                         mb |= MouseButtons.Left;
2580                                 }
2581                                 if( (keyIndicator & KeyStatusFlags.MK_RBUTTON) != 0) {
2582                                         mb |= MouseButtons.Right;
2583                                 }
2584                                 if( (keyIndicator & KeyStatusFlags.MK_MBUTTON) != 0) {
2585                                         mb |= MouseButtons.Middle;
2586                                 }
2587                                 if( (keyIndicator & KeyStatusFlags.MK_XBUTTON1) != 0) {
2588                                         mb |= MouseButtons.XButton1;
2589                                 }
2590                                 if( (keyIndicator & KeyStatusFlags.MK_XBUTTON2) != 0) {
2591                                         mb |= MouseButtons.XButton2;
2592                                 }
2593
2594                                 return new MouseEventArgs( mb, (mb != MouseButtons.None) ? 1: 0, msg.LoWordLParam, msg.HiWordLParam, 0);
2595                         }
2596
2597                 // WndProc - calls appriate On... function for the give
2598                 // message
2599                 //
2600                 // These On... functions do not appear to be called by
2601                 // WndProc:
2602                 //
2603                 // background color/image handled by WinForms
2604                 // OnBackColorChanged
2605                 // OnBackgroundImageChanged
2606                 // OnForeColorChanged
2607                 // OnPaintBackground
2608                 //
2609                 // controls are added/removed by WinForms
2610                 // OnControlAdded
2611                 // OnControlRemoved
2612                 // OnCreateControl
2613                 //
2614                 // OnBindingContextChanged
2615                 // OnCausesValidationChanged
2616                 // OnChangeUICues
2617                 // OnContextMenuChanged
2618                 // OnRightToLeftChanged
2619                 // OnGiveFeedback
2620                 // OnLayout
2621                 // OnDockChanged
2622                 // OnCursorChanged
2623                 // OnTextAlignChanged
2624                 // OnValidated
2625                 // OnValidating
2626                 // OnTabIndexChanged
2627                 // OnTabStopChanged
2628                 // OnLocationChanged
2629                 //
2630                 // FIXME: may be one of the WM_IME_ messages
2631                 // OnImeModeChanged 
2632                 //
2633                 // InvalidateRect is called by no Invalidate message exists
2634                 // OnInvalidated
2635                 //
2636                 // these messages ARE not called by WNDPROC according to docs
2637                 // OnParentBackColorChanged 
2638                 // OnParentBackgroundImageChanged
2639                 // OnParentBindingContextChanged
2640                 // OnParentChanged
2641                 // OnParentEnabledChanged
2642                 // OnParentFontChanged
2643                 // OnParentForeColorChanged
2644                 // OnParentRightToLeftChanged
2645                 // OnParentVisibleChanged
2646                 //
2647                 protected virtual void WndProc(ref Message m) 
2648                 {
2649                         EventArgs eventArgs = new EventArgs ();
2650                         // FIXME: paintEventArgs is not being created properly
2651                                 // FIXME: Graphics does not have a public constructor, you must get one from .NET
2652                         //PaintEventArgs paintEventArgs = new PaintEventArgs (
2653                         //      new Graphics(), new Rectangle());
2654
2655                                 if( (uint)m.Msg == Control.InvokeMessage) {
2656                                         ControlInvokeHelper helper = null;
2657                                         lock( InvokeQueue_.SyncRoot) {
2658                                                 if( InvokeQueue_.Count > 0) {
2659                                                         helper = (ControlInvokeHelper)InvokeQueue_.Dequeue();
2660                                                 }
2661                                         }
2662                                         if( helper != null) {
2663                                                 helper.ExecuteMethod();
2664                                         }
2665                                         return;
2666                                 }
2667                                 else if( m.Msg == Msg.WM_COMMAND) {
2668                                         // Notification
2669                                         m.Result = (IntPtr)1;
2670                                         OnWmCommand (ref m);
2671                                         if( m.Result != IntPtr.Zero) {
2672                                                 CallControlWndProc (ref m);
2673                                         }
2674                                         return;
2675                                 }
2676
2677                         switch (m.Msg) {
2678                         case Msg.WM_CREATE:
2679                                 Console.WriteLine ("WM_CREATE");
2680                                 OnHandleCreated (eventArgs);
2681                                 break;
2682                         case Msg.WM_LBUTTONDBLCLK:
2683                                 OnDoubleClick (eventArgs);
2684                                         CallControlWndProc(ref m);
2685                                 break;
2686                                 // OnDragDrop
2687                                 // OnDragEnter
2688                                 // OnDragLeave
2689                                 // OnDragOver
2690                                 // OnQueryContinueDrag
2691                         case Msg.WM_ENABLE:
2692                                 OnEnabledChanged (eventArgs);
2693                                         CallControlWndProc(ref m);
2694                                         break;
2695                         case Msg.WM_SETFOCUS:
2696                                 OnEnter (eventArgs);
2697                                 OnGotFocus (eventArgs);
2698                                         CallControlWndProc(ref m);
2699                                         break;
2700                         case Msg.WM_FONTCHANGE:
2701                                 OnFontChanged (eventArgs);
2702                                         CallControlWndProc(ref m);
2703                                         break;
2704                         case Msg.WM_DESTROY:
2705                                 OnHandleDestroyed (eventArgs);
2706                                         CallControlWndProc(ref m);
2707                                         break;
2708                         case Msg.WM_HELP:
2709                                 // FIXME:
2710                                 //OnHelpRequested (eventArgs);
2711                                         CallControlWndProc(ref m);
2712                                         break;
2713                         case Msg.WM_KEYDOWN:
2714                                 if ( !ProcessKeyMessage ( ref m ) )
2715                                         CallControlWndProc( ref m );
2716                         break;
2717                         case Msg.WM_CHAR:
2718                                 if ( !ProcessKeyMessage ( ref m ) )
2719                                         CallControlWndProc( ref m );
2720                         break;
2721                         case Msg.WM_KEYUP:
2722                                 if ( !ProcessKeyMessage ( ref m ) )
2723                                         CallControlWndProc( ref m );
2724                         break;
2725                         case Msg.WM_KILLFOCUS:
2726                                 OnLeave (eventArgs);
2727                                 OnLostFocus (eventArgs);
2728                                         CallControlWndProc(ref m);
2729                                         break;
2730                         case Msg.WM_MOUSEACTIVATE:
2731                                 //OnMouseEnter (eventArgs);
2732                                         CallControlWndProc(ref m);
2733                                         break;
2734                         case Msg.WM_MOUSEHOVER: // called by TrackMouseEvent
2735                                 OnMouseHover (eventArgs);
2736                                         CallControlWndProc(ref m);
2737                                         break;
2738                         case Msg.WM_MOUSELEAVE: // called by TrackMouseEvent
2739                                 OnMouseLeave (eventArgs);
2740                                         CallControlWndProc(ref m);
2741                                         break;
2742                         case Msg.WM_MOUSEMOVE:
2743                                 // FIXME:
2744                                 OnMouseMove (Msg2MouseEventArgs(ref m));
2745                                         CallControlWndProc(ref m);
2746                                         break;
2747                                 case Msg.WM_LBUTTONDOWN:
2748                                         // FIXME:
2749                                         //OnMouseDown (eventArgs);
2750                                         CallControlWndProc(ref m);
2751                                         break;
2752                                 case Msg.WM_LBUTTONUP:
2753                                 // FIXME:
2754                                 //OnMouseUp (eventArgs);
2755                                         CallControlWndProc(ref m);
2756                                         break;
2757                         case Msg.WM_MOUSEWHEEL:
2758                                 // FIXME:
2759                                 //OnMouseWheel (eventArgs);
2760                                         CallControlWndProc(ref m);
2761                                         break;
2762                         case Msg.WM_MOVE:
2763                                 OnMove (eventArgs);
2764                                 UpdateBounds ( );
2765                                 CallControlWndProc(ref m);
2766                                 break;
2767                                 case Msg.WM_NOTIFY:
2768                                         NMHDR nmhdr = (NMHDR)Marshal.PtrToStructure ( m.LParam,
2769                                                                         typeof ( NMHDR ) );
2770                                         if( !Control.ReflectMessage( nmhdr.hwndFrom, ref m )) 
2771                                                 CallControlWndProc(ref m);
2772                                         
2773                                         // FIXME: get NM_CLICKED msg from pnmh
2774                                         // OnClick (eventArgs);
2775                                         //OnNotifyMessage (eventArgs);
2776                                         break;
2777                         case Msg.WM_ERASEBKGND:
2778                                 if( GetStyle(ControlStyles.UserPaint)){
2779                                         if( !GetStyle(ControlStyles.AllPaintingInWmPaint)) {
2780                                                         PaintEventArgs eraseEventArgs = new PaintEventArgs( Graphics.FromHdc(m.WParam), new Rectangle(new Point(0,0),Size));
2781                                                 OnPaintBackground(eraseEventArgs);
2782                                                         eraseEventArgs.Dispose();
2783                                         }
2784                                         m.Result = (IntPtr)1;
2785                                 }
2786                                 else {
2787                                                 CallControlWndProc(ref m);
2788                                 }
2789                                 break;
2790                                 case Msg.WM_PAINT:
2791                                         if( !GetStyle(ControlStyles.UserPaint)) {
2792                                                 CallControlWndProc(ref m);
2793                                         }
2794                                         else {
2795                                                 PAINTSTRUCT     ps = new PAINTSTRUCT();
2796                                                 IntPtr hdc = Win32.BeginPaint( Handle, ref ps);
2797                                                 Rectangle rc = new Rectangle();
2798                                                 rc.X = ps.rcPaint.left;
2799                                                 rc.Y = ps.rcPaint.top;
2800                                                 rc.Width = ps.rcPaint.right - ps.rcPaint.left;
2801                                                 rc.Height = ps.rcPaint.bottom - ps.rcPaint.top;
2802                                                 PaintEventArgs paintEventArgs = new PaintEventArgs( Graphics.FromHdc(hdc), rc);
2803                                         if( GetStyle(ControlStyles.AllPaintingInWmPaint)) {
2804                                                 OnPaintBackground(paintEventArgs);
2805                                         }
2806                                                 OnPaint (paintEventArgs);
2807                                                 paintEventArgs.Dispose();
2808                                                 Win32.EndPaint(Handle, ref ps);
2809                                         }
2810                                         break;
2811                         case Msg.WM_SIZE:
2812                                 if( GetStyle(ControlStyles.ResizeRedraw)) {
2813                                         Invalidate();           
2814                                 }
2815                                 CallControlWndProc(ref m);
2816                                 break;
2817                         case Msg.WM_WINDOWPOSCHANGED:
2818                                 UpdateBounds ( );
2819                                 CallControlWndProc(ref m);
2820                         break;
2821                         case Msg.WM_STYLECHANGED:
2822                                 OnStyleChanged (eventArgs);
2823                                         CallControlWndProc(ref m);
2824                                         break;
2825                         case Msg.WM_SYSCOLORCHANGE:
2826                                 OnSystemColorsChanged (eventArgs);
2827                                         CallControlWndProc(ref m);
2828                                         break;
2829                         case Msg.WM_SETTEXT:
2830                                 //OnTextChanged (eventArgs);
2831                                         CallControlWndProc(ref m);
2832                                         break;
2833                         case Msg.WM_SETFONT:
2834                                 //OnTextChanged (eventArgs);
2835                                         CallControlWndProc(ref m);
2836                                         break;
2837                         case Msg.WM_SHOWWINDOW:
2838                                 OnVisibleChanged (eventArgs);
2839                                         CallControlWndProc(ref m);
2840                                         break;
2841                                 case Msg.WM_CTLCOLORLISTBOX:
2842                                         Win32.SetTextColor( m.WParam, Win32.RGB(ForeColor));
2843                                         //Win32.SetBkColor( m.WParam, 0x00FF00);
2844                                         //m.Result = Win32.GetStockObject(GSO_.LTGRAY_BRUSH);
2845                                         break;
2846                                 case Msg.WM_MEASUREITEM:
2847                                         ReflectMessage( m.WParam, ref m);
2848                                         break;
2849                                 case Msg.WM_DRAWITEM:
2850                                         Control.ReflectMessage( m.WParam, ref m);
2851                                         break;
2852                                 case Msg.WM_HSCROLL:
2853                                 case Msg.WM_VSCROLL:
2854                                         if(!Control.ReflectMessage( m.LParam, ref m )) {
2855                                         CallControlWndProc(ref m);
2856                                         }
2857                                         break;
2858                                 case Msg.WM_SETCURSOR:
2859                                         if ( cursor != null && cursor.Handle != IntPtr.Zero ) {
2860                                                 Win32.SetCursor ( cursor.Handle );
2861                                                 m.Result = (IntPtr)1;
2862                                         } else
2863                                                 CallControlWndProc( ref m );
2864                                 break;
2865                                 case Msg.WM_RBUTTONDOWN:
2866                                         if ( contextMenu != null ) {
2867                                                 contextMenu.Show ( this, 
2868                                                         new Point ( Win32.HIGH_ORDER ( m.LParam.ToInt32() ),
2869                                                                     Win32.LOW_ORDER ( m.LParam.ToInt32() ) ) );
2870                                         }
2871                                         CallControlWndProc( ref m );
2872                                 break;
2873                                 default:
2874                                         CallControlWndProc(ref m);
2875 /*
2876                                         if( ControlRealWndProc != IntPtr.Zero) {
2877                                                 CallControlWndProc(ref m);
2878                                         }
2879                                         else {
2880                                                 DefWndProc (ref m);
2881                                         }
2882 */                                      
2883                                 break;
2884                         }
2885                 }
2886
2887                 private void DoAnchor(Control ctrl) {
2888                         int deltaWidth = Bounds.Width - oldBounds.Width;
2889                         int deltaHeight = Bounds.Height - oldBounds.Height;
2890                         int halfDeltaWidth = deltaWidth / 2;
2891                         int halfDeltaHeight = deltaHeight / 2;
2892                         Rectangle controlBounds = ctrl.Bounds;
2893                         if ((ctrl.Anchor & AnchorStyles.Left) == 0) {
2894                                 controlBounds.X += halfDeltaWidth;
2895                         }
2896                         if ((ctrl.Anchor & AnchorStyles.Top) == 0) {
2897                                 controlBounds.Y += halfDeltaHeight;
2898                         }
2899                         if ((ctrl.Anchor & AnchorStyles.Right) == AnchorStyles.Right) {
2900                                 if ((ctrl.Anchor & AnchorStyles.Left) == AnchorStyles.Left) {
2901                                         controlBounds.Width += deltaWidth;
2902                                 }
2903                                 else {
2904                                         controlBounds.X += halfDeltaWidth;
2905                                 }
2906                         }
2907                         if ((ctrl.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom) {
2908                                 if ((ctrl.Anchor & AnchorStyles.Top) == AnchorStyles.Top) {
2909                                         controlBounds.Height += deltaHeight;
2910                                 }
2911                                 else {
2912                                         controlBounds.Y += halfDeltaHeight;
2913                                 }
2914                         }
2915                         ctrl.Bounds = controlBounds;
2916                 }
2917
2918                 private void DoDockAndAnchorLayout ( LayoutEventArgs e ) {
2919                         Rectangle area = DisplayRectangle;
2920                         
2921                         for ( int i = childControls.Count - 1; i >= 0; i-- ) {
2922                                 Control control = childControls[i];
2923                                 
2924                                 switch ( control.Dock ) {
2925                                 case DockStyle.Bottom:
2926                                         control.SetBounds ( area.Left, area.Bottom - control.Height,
2927                                                                 area.Width, control.Height );
2928                                         area.Height -= control.Height;
2929                                 break;
2930                                 case DockStyle.Top:
2931                                         control.SetBounds ( area.Left, area.Y, area.Width, control.Height );
2932                                         area.Y += control.Height;
2933                                         area.Height -= control.Height;
2934                                 break;
2935                                 case DockStyle.Right:
2936                                         control.SetBounds ( area.Right - control.Width, area.Top,
2937                                                                 control.Width, area.Height );
2938                                         area.Width -= control.Width;
2939                                 break;
2940                                 case DockStyle.Left:
2941                                         control.SetBounds ( area.Left, area.Y, control.Width, area.Height );
2942                                         area.X += control.Width;
2943                                         area.Width -= control.Width;
2944                                 break;
2945                                 case DockStyle.None:
2946                                         DoAnchor(control);
2947                                 break;
2948                                 }
2949                         }
2950
2951                         for ( int i = childControls.Count - 1; i >= 0; i-- ) {
2952                                 Control control = childControls[i];
2953                                 
2954                                 if ( control.Dock == DockStyle.Fill ) {
2955                                         control.SetBounds ( area.X, area.Y, area.Width, area.Height );
2956                                 }       
2957                         }
2958                 }
2959                 
2960                 internal static Control FocusedControl {
2961                         get {
2962                                 IEnumerator cw = controlsCollection.GetEnumerator ( );
2963
2964                                 while ( cw.MoveNext ( ) ) {
2965                                         Control c = ( (DictionaryEntry) cw.Current).Value as Control;
2966
2967                                         if ( c.Focused ) return c;
2968                                 }
2969
2970                                 return null;
2971                         }
2972                 }
2973
2974                 internal Control getParentForm ( )
2975                 {
2976                         Control parent = this.Parent;
2977                         while ( parent != null ) {
2978                                 if ( parent is Form ) 
2979                                         return parent;
2980                                 parent = parent.Parent;
2981                         }
2982                         return null;
2983                 }
2984
2985                 /// --- Control: events ---
2986                 public event EventHandler BackColorChanged;
2987                 public event EventHandler BackgroundImageChanged;
2988                 public event EventHandler BindingContextChanged;
2989                 public event EventHandler CausesValidationChanged;
2990                 public event UICuesEventHandler ChangeUICues;
2991                 
2992                         //Compact Framework
2993                 public event EventHandler Click;
2994                 
2995                 public event EventHandler ContextMenuChanged;
2996                 public event ControlEventHandler ControlAdded;
2997                 public event ControlEventHandler ControlRemoved;
2998                 public event EventHandler CursorChanged;
2999                 public event EventHandler DockChanged;
3000                 public event EventHandler DoubleClick;
3001                 public event DragEventHandler DragDrop;
3002                 public event DragEventHandler DragEnter;
3003                 public event EventHandler DragLeave;
3004                 public event DragEventHandler DragOver;
3005
3006                         //Compact Framework
3007                 public event EventHandler EnabledChanged;
3008                 
3009                 public event EventHandler Enter;
3010                 public event EventHandler FontChanged;
3011                 public event EventHandler ForeColorChanged;
3012                 public event GiveFeedbackEventHandler GiveFeedback;
3013                 
3014                         //Compact Framework
3015                 public event EventHandler GotFocus;
3016                 
3017                 public event EventHandler HandleCreated;
3018                 public event EventHandler HandleDestroyed;
3019                 public event HelpEventHandler HelpRequested;
3020                 public event EventHandler ImeModeChanged;
3021                 public event InvalidateEventHandler Invalidated;
3022                 
3023                         //Compact Framework
3024                 public event KeyEventHandler KeyDown;
3025                 
3026                         //Compact Framework
3027                 public event KeyPressEventHandler KeyPress;
3028                 
3029                         //Compact Framework
3030                 public event KeyEventHandler KeyUp;
3031                 
3032                 public event LayoutEventHandler Layout;
3033                 public event EventHandler Leave;
3034                 public event EventHandler LocationChanged;
3035                 
3036                         //Compact Framework
3037                 public event EventHandler LostFocus;
3038
3039                         //Compact Framework
3040                 public event MouseEventHandler MouseDown;
3041                 
3042                 public event EventHandler MouseEnter;
3043                 public event EventHandler MouseHover;
3044                 public event EventHandler MouseLeave;
3045                 
3046                         //Compact Framework
3047                 public event MouseEventHandler MouseMove;
3048                 
3049                         //Compact Framework
3050                 public event MouseEventHandler MouseUp;
3051                 
3052                 public event MouseEventHandler MouseWheel;
3053                 public event EventHandler Move;
3054                 
3055                         //Compact Framework
3056                 public event PaintEventHandler Paint;
3057                 
3058                         //Compact Framework
3059                 public event EventHandler ParentChanged;
3060                 
3061                 public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp;
3062                 public event QueryContinueDragEventHandler QueryContinueDrag;
3063                 
3064                         //Compact Framework
3065                 public event EventHandler Resize;
3066                 
3067                 public event EventHandler RightToLeftChanged;
3068                 public event EventHandler SizeChanged;
3069                 public event EventHandler StyleChanged;
3070                 public event EventHandler SystemColorsChanged;
3071                 public event EventHandler TabIndexChanged;
3072                 public event EventHandler TabStopChanged;
3073                 
3074                         //Compact Framework
3075                 public event EventHandler TextChanged;
3076                 
3077                 public event EventHandler Validated;
3078                 //[MonoTODO]
3079                 // CancelEventHandler not yet defined
3080                 //public event CancelEventHandler Validating {
3081                 
3082                 public event EventHandler VisibleChanged;
3083                 
3084                 /// --- IWin32Window properties
3085                 public IntPtr Handle {
3086                         get { 
3087                                 // If the handle has not yet been created,
3088                                 // referencing this property will force the
3089                                 // handle to be created. ( MSDN )
3090
3091                                 if ( !IsHandleCreated ) 
3092                                         CreateHandle ( );
3093
3094                                 return window.Handle;
3095                         }
3096                 }
3097                 
3098                 /// --- ISynchronizeInvoke properties ---
3099                 [MonoTODO]
3100                 public bool InvokeRequired {
3101                         get { 
3102                                         return CreatorThreadId_ != Win32.GetCurrentThreadId(); 
3103                                 }
3104                 }
3105                 
3106                         private IAsyncResult DoInvoke( Delegate method, object[] args) {
3107                                 IAsyncResult result = null;
3108                                 ControlInvokeHelper helper = new ControlInvokeHelper(method, args);
3109                                 if( InvokeRequired) {
3110                                         lock( this) {
3111                                                 lock( InvokeQueue_.SyncRoot) {
3112                                                         InvokeQueue_.Enqueue(helper);
3113                                                 }
3114                                                 Win32.PostMessage(Handle, Control.InvokeMessage, 0, 0);
3115                                                 result = helper;
3116                                         }
3117                                 }
3118                                 else {
3119                                         helper.CompletedSynchronously = true;
3120                                         helper.ExecuteMethod();
3121                                         result = helper;
3122                                 }
3123                                 return result;
3124                         }
3125
3126                 /// --- ISynchronizeInvoke methods ---
3127                 [MonoTODO]
3128                 public IAsyncResult BeginInvoke (Delegate method) 
3129                 {
3130                                 return DoInvoke( method, null);
3131                 }
3132                 
3133                 [MonoTODO]
3134                 public IAsyncResult BeginInvoke (Delegate method, object[] args) 
3135                 {
3136                                 return DoInvoke( method, args);
3137                         }
3138                 
3139                 [MonoTODO]
3140                 public object EndInvoke (IAsyncResult asyncResult) 
3141                 {
3142                                 object result = null;
3143                                 ControlInvokeHelper helper = asyncResult as ControlInvokeHelper;
3144                                 if( helper != null) {
3145                                         if( !asyncResult.CompletedSynchronously) {
3146                                                 asyncResult.AsyncWaitHandle.WaitOne();
3147                                         }
3148                                         result = helper.MethodResult;
3149                                 }
3150                                 return result;
3151                         }
3152                 
3153                 //Compact Framework
3154                 [MonoTODO]
3155                 public object Invoke (Delegate method) 
3156                 {
3157                                 return Invoke( method, null);
3158                         }
3159                 
3160                 //[MonoTODO]
3161                 public object Invoke (Delegate method, object[] args) 
3162                 {
3163                                 IAsyncResult result = BeginInvoke(method, args);
3164                                 return EndInvoke(result);
3165                         }
3166                 
3167                 /// sub-class: Control.ControlAccessibleObject
3168                 /// <summary>
3169                 /// Provides information about a control that can be used by an accessibility application.
3170                 /// </summary>
3171                 public class ControlAccessibleObject : AccessibleObject {
3172                         // AccessibleObject not ready to be base class
3173                         /// --- ControlAccessibleObject.constructor ---
3174                         [MonoTODO]
3175                         public ControlAccessibleObject (Control ownerControl) 
3176                         {
3177                                 throw new NotImplementedException ();
3178                         }
3179                         
3180                         
3181                         /// --- ControlAccessibleObject Properties ---
3182                         [MonoTODO]
3183                         public override string DefaultAction {
3184                                 get {
3185                                                 //FIXME:
3186                                                 return base.DefaultAction;
3187                                         }
3188                         }
3189                         
3190                         [MonoTODO]
3191                         public override string Description {
3192                                 get {
3193                                                 //FIXME:
3194                                                 return base.Description;
3195                                         }
3196                         }
3197                         
3198                         [MonoTODO]
3199                         public IntPtr Handle {
3200                                 get {
3201                                                 throw new NotImplementedException ();
3202                                         }
3203                                 set {
3204                                                 //FIXME:
3205                                         }
3206                         }
3207                         
3208                         [MonoTODO]
3209                         public override string Help {
3210                                 get {
3211                                                 //FIXME:
3212                                                 return base.Help;
3213                                         }
3214                         }
3215                         
3216                         [MonoTODO]
3217                         public override string KeyboardShortcut {
3218                                 get {
3219                                                 //FIXME:
3220                                                 return base.KeyboardShortcut;
3221                                         }
3222                         }
3223                         
3224                         [MonoTODO]
3225                         public override string Name {
3226                                 get {
3227                                                 //FIXME:
3228                                                 return base.Name;
3229                                         }
3230                                 set {
3231                                                 //FIXME:
3232                                                 base.Name = value;
3233                                         }
3234                         }
3235                         
3236                         [MonoTODO]
3237                         public Control Owner {
3238                                 get { 
3239                                                 throw new NotImplementedException ();
3240                                         }
3241                         }
3242                         
3243                         [MonoTODO]
3244                         public override AccessibleRole Role {
3245                                 get {
3246                                                 //FIXME:
3247                                                 return base.Role;
3248                                         }
3249                         }
3250                         
3251                         /// --- ControlAccessibleObject Methods ---
3252                         [MonoTODO]
3253                         public override int GetHelpTopic(out string fileName) 
3254                         {
3255                                         //FIXME:
3256                                         return base.GetHelpTopic(out fileName);
3257                                 }
3258                         
3259                         [MonoTODO]
3260                         public void NotifyClients (AccessibleEvents accEvent) 
3261                         {
3262                                         //FIXME:
3263                         }
3264                         
3265                         [MonoTODO]
3266                         public void NotifyClients (AccessibleEvents accEvent,
3267                                                    int childID) 
3268                         {
3269                                         //FIXME:
3270                                 }
3271                         
3272                         [MonoTODO]
3273                         public override string ToString ()
3274                         {
3275                                         //FIXME:
3276                                         return base.ToString();
3277                         }
3278                 }
3279                 
3280                 /// sub-class: Control.ControlCollection
3281                 /// <summary>
3282                 /// Represents a collection of Control objects
3283                 /// </summary>
3284                 public class ControlCollection : IList, ICollection, IEnumerable, ICloneable {
3285     
3286                         class ControlComparer : IComparer {
3287
3288                                 int IComparer.Compare( object x, object y )
3289                                 {
3290                                         int tx = ( ( Control )x ).TabIndex;
3291                                         int ty = ( ( Control )y ).TabIndex;
3292
3293                                         if ( tx > ty )
3294                                                 return 1;
3295                                         else if ( tx < ty )
3296                                                 return -1;
3297                                         else
3298                                                 return 0;
3299                                 }
3300                         }
3301
3302                         private ArrayList collection = new ArrayList ();
3303                         protected Control owner;
3304     
3305                         /// --- ControlCollection.constructor ---
3306                         public ControlCollection (Control owner) 
3307                         {
3308                                 this.owner = owner;
3309                         }
3310                 
3311                         /// --- ControlCollection Properties ---
3312                         public int Count {
3313                                 get {
3314                                                 return collection.Count;
3315                                         }
3316                         }
3317                 
3318                         public bool IsReadOnly {
3319                                 get {
3320                                                 return collection.IsReadOnly;
3321                                         }
3322                         }
3323                         
3324                         public virtual Control this [int index] {
3325                                 get {
3326                                                 return (Control) collection[index];
3327                                         }
3328                         }
3329                 
3330                         public virtual void Add (Control value) 
3331                         {
3332                                 if( !Contains(value)) {
3333                                         collection.Add (value);
3334                                         value.Parent = owner;
3335                                         owner.OnControlAdded ( new ControlEventArgs ( value ) );
3336                                 }
3337                         }
3338                         
3339                         public virtual void AddRange (Control[] controls) 
3340                         {
3341                                         for(int i = 0; i < controls.Length; i++) {
3342                                                 Add(controls[i]);
3343                                         }
3344                         }
3345                         
3346                         public virtual void Clear () 
3347                         {
3348                                 collection.Clear ();
3349                         }
3350                 
3351                         public bool Contains (Control control) 
3352                         {
3353                                 return collection.Contains (control);
3354                         }
3355                         
3356                         public void CopyTo (Array dest,int index) 
3357                         {
3358                                 collection.CopyTo (dest, index);
3359                         }
3360                         
3361                         [MonoTODO]
3362                         public override bool Equals (object obj) 
3363                         {
3364                                         //FIXME:
3365                                         return base.Equals(obj);
3366                         }
3367
3368                         public int GetChildIndex ( Control child )
3369                         {
3370                                 return GetChildIndex ( child, true );
3371                         }
3372
3373                         public int GetChildIndex ( Control child, bool throwException )
3374                         {
3375                                 int index = collection.IndexOf ( child );
3376                                 if ( index == -1 && throwException )
3377                                         throw new ArgumentException( "'child' is not a child control of this parent.");
3378                                 return index;
3379                         }
3380
3381                         public IEnumerator GetEnumerator () 
3382                         {
3383                                 return collection.GetEnumerator ();
3384                         }
3385                         
3386                         [MonoTODO]
3387                         public override int GetHashCode () 
3388                         {
3389                                 //FIXME:
3390                                 return base.GetHashCode();
3391                         }
3392                         
3393                         public int IndexOf (Control control) 
3394                         {
3395                                 return collection.IndexOf (control);
3396                         }
3397                         
3398                         public virtual void Remove (Control value) 
3399                         {
3400                                 collection.Remove (value);
3401                         }
3402                         
3403                         public void RemoveAt (int index) 
3404                         {
3405                                 Remove ( this [ index ] );
3406                                 // have to give a chance to handle this situation in derived class
3407                                 //collection.RemoveAt (index);
3408                         }
3409                         
3410                         public void SetChildIndex ( Control child, int newIndex ) 
3411                         {
3412                                 int oldIndex = collection.IndexOf ( child );
3413                                 if ( oldIndex == -1 )
3414                                         throw new ArgumentException( "'child' is not a child control of this parent.");
3415
3416                                 if ( oldIndex != newIndex ) {
3417                                         collection.Remove ( child );
3418
3419                                         if ( newIndex >= collection.Count )
3420                                                 collection.Add ( child );
3421                                         else
3422                                                 collection.Insert ( newIndex, child );
3423                                 }
3424                         }
3425
3426                         internal Control GetFirstControl ( bool direction )
3427                         {
3428                                 if ( collection.Count == 0 )
3429                                         return null;
3430
3431                                 ArrayList copy = collection.Clone ( ) as ArrayList;
3432                                 copy.Sort ( new ControlComparer ( ) );
3433                                 
3434                                 if ( direction )
3435                                         return copy [0] as Control;
3436                                 else {
3437                                         Control last = copy[ collection.Count - 1 ] as Control;
3438                                         if ( last.Controls.Count == 0 )
3439                                                 return last;
3440                                         else
3441                                                 return last.Controls.GetFirstControl ( false );
3442                                 }
3443                         }
3444
3445
3446                         internal Control GetNextControl ( Control ctl, bool forward )
3447                         {
3448                                 if ( collection.Count == 0 )
3449                                         return null;
3450
3451                                 ArrayList copy = collection.Clone ( ) as ArrayList;
3452                                 copy.Sort ( new ControlComparer (  ) );
3453
3454                                 int index = copy.IndexOf ( ctl ) + ( forward ? 1 : -1 );
3455
3456                                 if ( ( forward && index  < copy.Count ) || ( !forward && index >= 0 ) )
3457                                         return copy[index] as Control;
3458
3459                                 return null;
3460                         }
3461
3462                         /// --- ControlCollection.IClonable methods ---
3463                         [MonoTODO]
3464                         object ICloneable.Clone ()
3465                         {
3466                                 throw new NotImplementedException ();
3467                         }
3468                         
3469                         /// --- ControlCollection.IList properties ---
3470                         bool IList.IsFixedSize {
3471                                 get {
3472                                                 return collection.IsFixedSize;
3473                                         }
3474                         }
3475     
3476                         object IList.this [int index] {
3477                                 get {
3478                                                 return collection[index];
3479                                         }
3480                                 set {
3481                                                 collection[index] = value;
3482                                         }
3483                         }
3484     
3485                         object ICollection.SyncRoot {
3486                                 get {
3487                                                 return collection.SyncRoot;
3488                                         }
3489                         }
3490         
3491                         bool ICollection.IsSynchronized {
3492                                 get {
3493                                                 return collection.IsSynchronized;
3494                                         }
3495                         }
3496                         
3497                         /// --- ControlCollection.IList methods ---
3498                         int IList.Add (object control) 
3499                         {
3500                                 return collection.Add (control);
3501                         }
3502                 
3503                         bool IList.Contains (object control) 
3504                         {
3505                                 return collection.Contains (control);
3506                         }
3507                 
3508                         int IList.IndexOf (object control) 
3509                         {
3510                                 return collection.IndexOf (control);
3511                         }
3512                 
3513                         void IList.Insert (int index,object value) 
3514                         {
3515                                 collection.Insert (index, value);
3516                         }
3517                 
3518                         void IList.Remove (object control) 
3519                         {
3520                                 collection.Remove (control);
3521                         }
3522                 }  // --- end of Control.ControlCollection ---
3523         }
3524     }