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