a2766d204e8e517a21714a9400d3c70550c09636
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Control.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok            pbartok@novell.com
24 //
25 // Partially based on work by:
26 //      Aleksey Ryabchuk        ryabchuk@yahoo.com
27 //      Alexandre Pigolkine     pigolkine@gmx.de
28 //      Dennis Hayes            dennish@raytek.com
29 //      Jaak Simm               jaaksimm@firm.ee
30 //      John Sohn               jsohn@columbus.rr.com
31 //
32
33 #undef DebugRecreate
34 #undef DebugFocus
35
36 using System;
37 using System.ComponentModel;
38 using System.ComponentModel.Design;
39 using System.ComponentModel.Design.Serialization;
40 using System.Collections;
41 using System.Diagnostics;
42 using System.Drawing;
43 using System.Drawing.Drawing2D;
44 using System.Reflection;
45 using System.Runtime.InteropServices;
46 using System.Security;
47 using System.Threading;
48
49 namespace System.Windows.Forms
50 {
51 #if NET_2_0
52         [ComVisible(true)]
53         [ClassInterface (ClassInterfaceType.AutoDispatch)]
54 #endif
55         [Designer("System.Windows.Forms.Design.ControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
56         [DefaultProperty("Text")]
57         [DefaultEvent("Click")]
58         [DesignerSerializer("System.Windows.Forms.Design.ControlCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
59         [ToolboxItemFilter("System.Windows.Forms")]
60         public class Control : Component, ISynchronizeInvoke, IWin32Window
61 #if NET_2_0
62                 , IBindableComponent, IDropTarget
63 #endif
64         {
65                 #region Local Variables
66
67                 // Basic
68                 internal Rectangle              bounds;                 // bounding rectangle for control (client area + decorations)
69                 Rectangle               explicit_bounds; // explicitly set bounds
70                 internal object                 creator_thread;         // thread that created the control
71                 internal                ControlNativeWindow     window;                 // object for native window handle
72                 private                 IWindowTarget window_target;
73                 string                  name; // for object naming
74
75                 // State
76                 bool                    is_created; // true if OnCreateControl has been sent
77                 internal bool           has_focus;              // true if control has focus
78                 internal bool           is_visible;             // true if control is visible
79                 internal bool           is_entered;             // is the mouse inside the control?
80                 internal bool           is_enabled;             // true if control is enabled (usable/not grayed out)
81                 bool                    is_accessible; // true if the control is visible to accessibility applications
82                 bool                    is_captured; // tracks if the control has captured the mouse
83                 internal bool                   is_toplevel;            // tracks if the control is a toplevel window
84                 bool                    is_recreating; // tracks if the handle for the control is being recreated
85                 bool                    causes_validation; // tracks if validation is executed on changes
86                 bool                    is_focusing; // tracks if Focus has been called on the control and has not yet finished
87                 int                     tab_index; // position in tab order of siblings
88                 bool                    tab_stop; // is the control a tab stop?
89                 bool                    is_disposed; // has the window already been disposed?
90                 Size                    client_size; // size of the client area (window excluding decorations)
91                 Rectangle               client_rect; // rectangle with the client area (window excluding decorations)
92                 ControlStyles           control_style; // rather win32-specific, style bits for control
93                 ImeMode                 ime_mode;
94                 object                  control_tag; // object that contains data about our control
95                 internal int                    mouse_clicks;           // Counter for mouse clicks
96                 Cursor                  cursor; // Cursor for the window
97                 internal bool                   allow_drop;             // true if the control accepts droping objects on it   
98                 Region                  clip_region; // User-specified clip region for the window
99
100                 // Visuals
101                 internal Color                  foreground_color;       // foreground color for control
102                 internal Color                  background_color;       // background color for control
103                 Image                   background_image; // background image for control
104                 internal Font                   font;                   // font for control
105                 string                  text; // window/title text for control
106                 internal                BorderStyle             border_style;           // Border style of control
107
108                 // Layout
109                 internal enum LayoutType {
110                         Anchor,
111                         Dock
112                 }
113                 Layout.LayoutEngine layout_engine;
114                 int layout_suspended;
115                 bool layout_pending; // true if our parent needs to re-layout us
116                 internal AnchorStyles anchor_style; // anchoring requirements for our control
117                 internal DockStyle dock_style; // docking requirements for our control
118                 LayoutType layout_type;
119
120                 // Please leave the next 2 as internal until DefaultLayout (2.0) is rewritten
121                 internal int                    dist_right; // distance to the right border of the parent
122                 internal int                    dist_bottom; // distance to the bottom border of the parent
123
124                 // to be categorized...
125                 ControlCollection       child_controls; // our children
126                 Control                 parent; // our parent control
127                 AccessibleObject        accessibility_object; // object that contains accessibility information about our control
128                 BindingContext          binding_context;
129                 RightToLeft             right_to_left; // drawing direction for control
130                 ContextMenu             context_menu; // Context menu associated with the control
131                 internal bool           use_compatible_text_rendering;
132
133                 // double buffering
134                 DoubleBuffer            backbuffer;
135                 
136                 // to implement DeviceContext without depending on double buffering
137                 Bitmap bmp;
138                 Graphics bmp_g;
139
140                 ControlBindingsCollection data_bindings;
141
142 #if NET_2_0
143                 static bool verify_thread_handle;
144                 Padding padding;
145                 ImageLayout backgroundimage_layout;
146                 Size maximum_size;
147                 Size minimum_size;
148                 Padding margin;
149                 private ContextMenuStrip context_menu_strip;
150 #endif
151
152                 #endregion      // Local Variables
153
154                 #region Private Classes
155                 // This helper class allows us to dispatch messages to Control.WndProc
156                 internal class ControlNativeWindow : NativeWindow {
157                         private Control owner;
158
159                         public ControlNativeWindow(Control control) : base() {
160                                 this.owner=control;
161                         }
162
163
164                         public Control Owner {
165                                 get {
166                                         return owner;
167                                 }
168                         }
169
170                         protected override void OnHandleChange()
171                         {
172                                 this.owner.WindowTarget.OnHandleChange(this.owner.Handle);
173                         }
174
175                         static internal Control ControlFromHandle(IntPtr hWnd) {
176                                 ControlNativeWindow     window;
177
178                                 window = (ControlNativeWindow)window_collection[hWnd];
179                                 if (window != null) {
180                                         return window.owner;
181                                 }
182
183                                 return null;
184                         }
185
186                         static internal Control ControlFromChildHandle (IntPtr handle) {
187                                 ControlNativeWindow     window;
188
189                                 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
190                                 while (hwnd != null) {
191                                         window = (ControlNativeWindow)window_collection[hwnd.Handle];
192                                         if (window != null) {
193                                                 return window.owner;
194                                         }
195                                         hwnd = hwnd.Parent;
196                                 }
197
198                                 return null;
199                         }
200
201                         protected override void WndProc(ref Message m) {
202                                 owner.WindowTarget.OnMessage(ref m);
203                         }
204                 }
205
206                 private class ControlWindowTarget : IWindowTarget
207                 {
208                         private Control control;
209
210                         public ControlWindowTarget(Control control)
211                         {
212                                 this.control = control;
213                         }
214
215                         public void OnHandleChange(IntPtr newHandle) 
216                         {
217                         }
218
219                         public void OnMessage(ref Message m) 
220                         {
221                                 control.WndProc(ref m);
222                         }
223                 }
224                 #endregion
225                 
226                 #region Public Classes
227                 [ComVisible(true)]
228                 public class ControlAccessibleObject : AccessibleObject {
229                         IntPtr handle;
230
231                         #region ControlAccessibleObject Constructors
232                         public ControlAccessibleObject(Control ownerControl)
233                                 : base (ownerControl)
234                         {
235                                 if (ownerControl == null)
236                                         throw new ArgumentNullException ("owner");
237
238                                 handle = ownerControl.Handle;
239                         }
240                         #endregion      // ControlAccessibleObject Constructors
241
242                         #region ControlAccessibleObject Public Instance Properties
243                         public override string DefaultAction {
244                                 get {
245                                         return base.DefaultAction;
246                                 }
247                         }
248
249                         public override string Description {
250                                 get {
251                                         return base.Description;
252                                 }
253                         }
254
255                         public IntPtr Handle {
256                                 get {
257                                         return handle;
258                                 }
259
260                                 set {
261                                         // We don't want to let them set it
262                                 }
263                         }
264
265                         public override string Help {
266                                 get {
267                                         return base.Help;
268                                 }
269                         }
270
271                         public override string KeyboardShortcut {
272                                 get {
273                                         return base.KeyboardShortcut;
274                                 }
275                         }
276
277                         public override string Name {
278                                 get {
279                                         return base.Name;
280                                 }
281
282                                 set {
283                                         base.Name = value;
284                                 }
285                         }
286
287                         public Control Owner {
288                                 get {
289                                         return base.owner;
290                                 }
291                         }
292
293                         public override AccessibleObject Parent {
294                                 get {
295                                         return base.Parent;
296                                 }
297                         }
298
299
300                         public override AccessibleRole Role {
301                                 get {
302                                         return base.Role;
303                                 }
304                         }
305                         #endregion      // ControlAccessibleObject Public Instance Properties
306
307                         #region ControlAccessibleObject Public Instance Methods
308                         public override int GetHelpTopic(out string FileName) {
309                                 return base.GetHelpTopic (out FileName);
310                         }
311
312                         [MonoTODO ("Implement this")]
313                         public void NotifyClients(AccessibleEvents accEvent) {
314                                 throw new NotImplementedException();
315                         }
316
317                         [MonoTODO ("Implement this")]
318                         public void NotifyClients(AccessibleEvents accEvent, int childID) {
319                         }
320
321                         public override string ToString() {
322                                 return "ControlAccessibleObject: Owner = " + owner.ToString() + ", Text: " + owner.text;
323                         }
324
325                         #endregion      // ControlAccessibleObject Public Instance Methods
326                 }
327
328                 private class DoubleBuffer : IDisposable
329                 {
330                         public Region InvalidRegion;
331                         private Stack real_graphics;
332                         private object back_buffer;
333                         private Control parent;
334                         private bool pending_disposal;
335                         
336                         public DoubleBuffer (Control parent) {
337                                 this.parent = parent;
338                                 real_graphics = new Stack ();
339                                 int width = parent.Width;
340                                 int height = parent.Height;
341
342                                 if (width < 1) width = 1;
343                                 if (height < 1) height = 1;
344
345                                 XplatUI.CreateOffscreenDrawable (parent.Handle, width, height, out back_buffer);
346                                 Invalidate ();
347                         }
348                         
349                         public void Blit (PaintEventArgs pe) {
350                                 Graphics buffered_graphics;
351                                 buffered_graphics = XplatUI.GetOffscreenGraphics (back_buffer);
352                                 XplatUI.BlitFromOffscreen (parent.Handle, pe.Graphics, back_buffer, buffered_graphics, pe.ClipRectangle);
353                                 buffered_graphics.Dispose ();
354                         }
355                         
356                         public void Start (PaintEventArgs pe) {
357                                 // We need to get the graphics for every paint.
358                                 real_graphics.Push(pe.SetGraphics (XplatUI.GetOffscreenGraphics (back_buffer)));
359                         }
360
361                         public void End (PaintEventArgs pe) {
362                                 Graphics buffered_graphics;
363                                 buffered_graphics = pe.SetGraphics ((Graphics) real_graphics.Pop ());
364
365                                 if (pending_disposal) 
366                                         Dispose ();
367                                 else {
368                                         XplatUI.BlitFromOffscreen (parent.Handle, pe.Graphics, back_buffer, buffered_graphics, pe.ClipRectangle);
369                                         InvalidRegion.Exclude (pe.ClipRectangle);
370                                 }
371                                 buffered_graphics.Dispose ();
372                         }
373                         
374                         public void Invalidate () {
375                                 if (InvalidRegion != null)
376                                         InvalidRegion.Dispose ();
377                                 InvalidRegion = new Region (parent.ClientRectangle);
378                         }
379                         
380                         public void Dispose () {
381                                 if (real_graphics.Count > 0) {
382                                         pending_disposal = true;
383                                         return;
384                                 }
385                                 
386                                 XplatUI.DestroyOffscreenDrawable (back_buffer);
387
388                                 if (InvalidRegion != null)
389                                         InvalidRegion.Dispose ();
390                                 InvalidRegion = null;
391                                 back_buffer = null;
392                                 GC.SuppressFinalize (this);
393                         }
394
395                         #region IDisposable Members
396                         void IDisposable.Dispose () {
397                                 Dispose ();
398                         }
399                         #endregion
400                         
401                         ~DoubleBuffer () {
402                                 Dispose ();
403                         }
404                 }
405
406                 [ListBindable (false)]
407 #if NET_2_0
408                 [ComVisible (false)]
409                 public class ControlCollection : Layout.ArrangedElementCollection, IList, ICollection, ICloneable, IEnumerable {
410 #else
411                 [DesignerSerializer("System.Windows.Forms.Design.ControlCollectionCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
412                 public class ControlCollection : IList, ICollection, ICloneable, IEnumerable {
413 #endif
414                         #region ControlCollection Local Variables
415 #if !NET_2_0
416                         ArrayList list;
417 #endif
418                         ArrayList impl_list;
419                         Control[] all_controls;
420                         Control owner;
421                         #endregion      // ControlCollection Local Variables
422
423                         #region ControlCollection Public Constructor
424                         public ControlCollection(Control owner) {
425                                 this.owner=owner;
426 #if !NET_2_0
427                                 this.list=new ArrayList();
428 #endif
429                         }
430                         #endregion
431
432                         #region ControlCollection Public Instance Properties
433                         int ICollection.Count {
434                                 get { return Count; }
435                         }
436
437
438 #if !NET_2_0
439                         public int Count {
440                                 get { return list.Count; }
441                         }
442 #endif
443
444 #if NET_2_0
445                         bool IList.IsReadOnly
446 #else
447                         public bool IsReadOnly
448 #endif
449                         {
450                                 get {
451                                         return list.IsReadOnly;
452                                 }
453                         }
454
455 #if NET_2_0
456                         public Control Owner { get { return this.owner; } }
457                         
458                         public virtual Control this[string key] {
459                                 get { 
460                                         int index = IndexOfKey (key);
461                                         
462                                         if (index >= 0)
463                                                 return this[index];
464                                                 
465                                         return null;
466                                 }
467                         }
468                         
469                         new
470 #endif
471                         public virtual Control this[int index] {
472                                 get {
473                                         if (index < 0 || index >= list.Count) {
474                                                 throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
475                                         }
476                                         return (Control)list[index];
477                                 }
478                                 
479                                 
480                         }
481                         #endregion // ControlCollection Public Instance Properties
482                         
483                         #region ControlCollection Instance Methods
484                         public virtual void Add (Control value) {
485                                 if (value == null)
486                                         return;
487
488                                 Form form_value = value as Form;
489                                 Form form_owner = owner as Form;
490                                 bool owner_permits_toplevels = (owner is MdiClient) || (form_owner != null && form_owner.IsMdiContainer);
491                                 bool child_is_toplevel = value.GetTopLevel();
492                                 bool child_is_mdichild = form_value != null && form_value.IsMdiChild;
493
494                                 if (child_is_toplevel && !(owner_permits_toplevels && child_is_mdichild))
495                                         throw new ArgumentException("Cannot add a top level control to a control.", "value");
496                                 
497                                 if (child_is_mdichild && form_value.MdiParent != null && form_value.MdiParent != owner && form_value.MdiParent != owner.Parent) {
498                                         throw new ArgumentException ("Form cannot be added to the Controls collection that has a valid MDI parent.", "value");
499                                 }
500                                 
501                                 if (Contains (value)) {
502                                         owner.PerformLayout();
503                                         return;
504                                 }
505
506                                 if (value.tab_index == -1) {
507                                         int     end;
508                                         int     index;
509                                         int     use;
510
511                                         use = 0;
512                                         end = owner.child_controls.Count;
513                                         for (int i = 0; i < end; i++) {
514                                                 index = owner.child_controls[i].tab_index;
515                                                 if (index >= use) {
516                                                         use = index + 1;
517                                                 }
518                                         }
519                                         value.tab_index = use;
520                                 }
521
522                                 if (value.parent != null) {
523                                         value.parent.Controls.Remove(value);
524                                 }
525
526                                 all_controls = null;
527                                 list.Add (value);
528
529                                 value.ChangeParent(owner);
530
531                                 value.InitLayout();
532
533                                 if (owner.Visible)
534                                         owner.UpdateChildrenZOrder();
535                                 owner.PerformLayout(value, "Parent");
536                                 owner.OnControlAdded(new ControlEventArgs(value));
537                         }
538                         
539                         internal void AddToList (Control c) {
540                                 all_controls = null;
541                                 list.Add (c);
542                         }
543
544                         internal virtual void AddImplicit (Control control) {
545                                 if (impl_list == null)
546                                         impl_list = new ArrayList ();
547
548                                 if (AllContains (control)) {
549                                         owner.PerformLayout ();
550                                         return;
551                                 }
552
553                                 if (control.parent != null) {
554                                         control.parent.Controls.Remove(control);
555                                 }
556
557                                 all_controls = null;
558                                 impl_list.Add (control);
559
560                                 control.ChangeParent (owner);
561                                 control.InitLayout ();
562                                 if (owner.Visible)
563                                         owner.UpdateChildrenZOrder ();
564                                 
565                                 // If we are adding a new control that isn't
566                                 // visible, don't trigger a layout
567                                 if (control.VisibleInternal)
568                                         owner.PerformLayout (control, "Parent");
569                         }
570 #if NET_2_0
571                         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
572 #endif
573                         public virtual void AddRange (Control[] controls) {
574                                 if (controls == null)
575                                         throw new ArgumentNullException ("controls");
576
577                                 owner.SuspendLayout ();
578
579                                 try {
580                                         for (int i = 0; i < controls.Length; i++) 
581                                                 Add (controls[i]);
582                                 } finally {
583                                         owner.ResumeLayout ();
584                                 }
585                         }
586
587                         internal virtual void AddRangeImplicit (Control [] controls) {
588                                 if (controls == null)
589                                         throw new ArgumentNullException ("controls");
590
591                                 owner.SuspendLayout ();
592
593                                 try {
594                                         for (int i = 0; i < controls.Length; i++)
595                                                 AddImplicit (controls [i]);
596                                 } finally {
597                                         owner.ResumeLayout ();
598                                 }
599                         }
600
601 #if NET_2_0
602                         new
603 #endif
604                         public virtual void Clear () {
605                                 all_controls = null;
606
607                                 // MS sends remove events in reverse order
608                                 while (list.Count > 0) {
609                                         Remove((Control)list[list.Count - 1]);
610                                 }
611                         }
612
613                         internal virtual void ClearImplicit () {
614                                 if (impl_list == null)
615                                         return;
616                                 all_controls = null;
617                                 impl_list.Clear ();
618                         }
619
620                         public bool Contains (Control value) {
621                                 for (int i = list.Count; i > 0; ) {
622                                         i--;
623                                         
624                                         if (list [i] == value) {
625                                                 // Do we need to do anything here?
626                                                 return true;
627                                         }
628                                 }
629                                 return false;
630                         }
631
632                         internal bool ImplicitContains (Control value) {
633                                 if (impl_list == null)
634                                         return false;
635
636                                 for (int i = impl_list.Count; i > 0; ) {
637                                         i--;
638                                         
639                                         if (impl_list [i] == value) {
640                                                 // Do we need to do anything here?
641                                                 return true;
642                                         }
643                                 }
644                                 return false;
645                         }
646
647                         internal bool AllContains (Control value) {
648                                 return Contains (value) || ImplicitContains (value);
649                         }
650
651 #if NET_2_0
652                         public virtual bool ContainsKey (string key)
653                         {
654                                 return IndexOfKey (key) >= 0;
655                         }
656 #endif
657
658                         void ICollection.CopyTo (Array array, int index) {
659                                 CopyTo (array, index);
660                         }
661
662 #if !NET_2_0
663                         public void CopyTo (Array array, int index) {
664                                 list.CopyTo(array, index);
665                         }
666
667                         public override bool Equals (object other) {
668                                 if (other is ControlCollection && (((ControlCollection)other).owner==this.owner)) {
669                                         return(true);
670                                 } else {
671                                         return(false);
672                                 }
673                         }
674 #endif
675
676 #if NET_2_0
677                         // LAMESPEC: MSDN says AE, MS implementation throws ANE
678                         public Control[] Find (string key, bool searchAllChildren)
679                         {
680                                 if (string.IsNullOrEmpty (key))
681                                         throw new ArgumentNullException ("key");
682                                         
683                                 ArrayList al = new ArrayList ();
684                                 
685                                 foreach (Control c in list) {
686                                         if (c.Name.Equals (key, StringComparison.CurrentCultureIgnoreCase))
687                                                 al.Add (c);
688                                                 
689                                         if (searchAllChildren)
690                                                 al.AddRange (c.Controls.Find (key, true));
691                                 }
692                                 
693                                 return (Control[])al.ToArray (typeof (Control));
694                         }
695 #endif
696
697                         public int GetChildIndex(Control child) {
698                                 return GetChildIndex(child, false);
699                         }
700
701 #if NET_2_0
702                         public virtual int
703 #else
704                         public int
705 #endif
706                         GetChildIndex(Control child, bool throwException) {
707                                 int index;
708
709                                 index=list.IndexOf(child);
710
711                                 if (index==-1 && throwException) {
712                                         throw new ArgumentException("Not a child control", "child");
713                                 }
714                                 return index;
715                         }
716
717 #if NET_2_0
718                         public override IEnumerator
719 #else
720                         public IEnumerator
721 #endif
722                         GetEnumerator () {
723                                 return list.GetEnumerator();
724                         }
725
726                         internal IEnumerator GetAllEnumerator () {
727                                 Control [] res = GetAllControls ();
728                                 return res.GetEnumerator ();
729                         }
730
731                         internal Control [] GetAllControls () {
732                                 if (all_controls != null)
733                                         return all_controls;
734
735                                 if (impl_list == null) {
736                                         all_controls = (Control []) list.ToArray (typeof (Control));
737                                         return all_controls;
738                                 }
739                                 
740                                 all_controls = new Control [list.Count + impl_list.Count];
741                                 impl_list.CopyTo (all_controls);
742                                 list.CopyTo (all_controls, impl_list.Count);
743
744                                 return all_controls;
745                         }
746
747 #if !NET_2_0
748                         public override int GetHashCode() {
749                                 return base.GetHashCode();
750                         }
751 #endif
752
753                         public int IndexOf(Control control) {
754                                 return list.IndexOf(control);
755                         }
756
757 #if NET_2_0
758                         public virtual int IndexOfKey (string key)
759                         {
760                                 if (string.IsNullOrEmpty (key))
761                                         return -1;
762                                         
763                                 for (int i = 0; i < list.Count; i++)
764                                         if (((Control)list[i]).Name.Equals (key, StringComparison.CurrentCultureIgnoreCase))
765                                                 return i;
766                                                 
767                                 return -1;
768                         }
769 #endif
770
771                         public virtual void Remove(Control value) {
772                                 if (value == null)
773                                         return;
774
775                                 owner.PerformLayout(value, "Parent");
776                                 owner.OnControlRemoved(new ControlEventArgs(value));
777
778                                 all_controls = null;
779                                 list.Remove(value);
780
781                                 value.ChangeParent(null);
782
783                                 owner.UpdateChildrenZOrder();
784                         }
785
786                         internal virtual void RemoveImplicit (Control control) {
787                                 if (impl_list != null) {
788                                         all_controls = null;
789                                         owner.PerformLayout (control, "Parent");
790                                         owner.OnControlRemoved (new ControlEventArgs (control));
791                                         impl_list.Remove (control);
792                                 }
793                                 control.ChangeParent (null);
794                                 owner.UpdateChildrenZOrder ();
795                         }
796
797 #if NET_2_0
798                         new
799 #endif
800                         public void RemoveAt(int index) {
801                                 if (index < 0 || index >= list.Count) {
802                                         throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
803                                 }
804                                 Remove ((Control)list[index]);
805                         }
806
807 #if NET_2_0
808                         public virtual void RemoveByKey (string key)
809                         {
810                                 int index = IndexOfKey (key);
811                                 
812                                 if (index >= 0)
813                                         RemoveAt (index);
814                         }       
815 #endif
816
817 #if NET_2_0
818                         public virtual void
819 #else
820                         public void
821 #endif
822                         SetChildIndex(Control child, int newIndex) {
823                                 if (child == null)
824                                         throw new ArgumentNullException ("child");
825
826                                 int     old_index;
827
828                                 old_index=list.IndexOf(child);
829                                 if (old_index==-1) {
830                                         throw new ArgumentException("Not a child control", "child");
831                                 }
832
833                                 if (old_index==newIndex) {
834                                         return;
835                                 }
836
837                                 all_controls = null;
838                                 list.RemoveAt(old_index);
839
840                                 if (newIndex>list.Count) {
841                                         list.Add(child);
842                                 } else {
843                                         list.Insert(newIndex, child);
844                                 }
845                                 child.UpdateZOrder();
846                                 owner.PerformLayout();
847                         }
848                         #endregion // ControlCollection Private Instance Methods
849
850                         #region ControlCollection Interface Properties
851                         object IList.this[int index] {
852                                 get {
853                                         if (index<0 || index>=list.Count) {
854                                                 throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
855                                         }
856                                         return this[index];
857                                 }
858
859                                 set {
860                                         if (!(value is Control)) {
861                                                 throw new ArgumentException("Object of type Control required", "value");
862                                         }
863
864                                         all_controls = null;
865                                         Control ctrl = (Control) value;
866                                         list[index]= ctrl;
867
868                                         ctrl.ChangeParent(owner);
869
870                                         ctrl.InitLayout();
871
872                                         owner.UpdateChildrenZOrder();
873                                         owner.PerformLayout(ctrl, "Parent");
874                                 }
875                         }
876
877                         bool IList.IsFixedSize {
878                                 get {
879                                         return false;
880                                 }
881                         }
882
883                         bool ICollection.IsSynchronized {
884                                 get {
885                                         return list.IsSynchronized;
886                                 }
887                         }
888
889                         object ICollection.SyncRoot {
890                                 get {
891                                         return list.SyncRoot;
892                                 }
893                         }
894                         #endregion // ControlCollection Interface Properties
895
896                         #region ControlCollection Interface Methods
897                         int IList.Add(object value) {
898                                 if (!(value is Control)) {
899                                         throw new ArgumentException("Object of type Control required", "value");
900                                 }
901
902                                 if (value == null) {
903                                         throw new ArgumentException("value", "Cannot add null controls");
904                                 }
905
906                                 bool owner_permits_toplevels = (owner is MdiClient) || (owner is Form && ((Form)owner).IsMdiContainer);
907                                 bool child_is_toplevel = ((Control)value).GetTopLevel();
908                                 bool child_is_mdichild = (value is Form && ((Form)value).IsMdiChild);
909
910                                 if (child_is_toplevel && !(owner_permits_toplevels && child_is_mdichild))
911                                         throw new ArgumentException("Cannot add a top level control to a control.", "value");
912
913                                 return list.Add(value);
914                         }
915
916                         bool IList.Contains(object value) {
917                                 if (!(value is Control)) {
918                                         throw new ArgumentException("Object of type Control required", "value");
919                                 }
920
921                                 return this.Contains((Control) value);
922                         }
923
924                         int IList.IndexOf(object value) {
925                                 if (!(value is Control)) {
926                                         throw new ArgumentException("Object of type Control  required", "value");
927                                 }
928
929                                 return this.IndexOf((Control) value);
930                         }
931
932                         void IList.Insert(int index, object value) {
933                                 if (!(value is Control)) {
934                                         throw new ArgumentException("Object of type Control required", "value");
935                                 }
936                                 all_controls = null;
937                                 list.Insert(index, value);
938                         }
939
940                         void IList.Remove(object value) {
941                                 if (!(value is Control)) {
942                                         throw new ArgumentException("Object of type Control required", "value");
943                                 }
944                                 all_controls = null;
945                                 list.Remove(value);
946                         }
947
948                         Object ICloneable.Clone() {
949                                 ControlCollection clone = new ControlCollection(this.owner);
950                                 clone.list=(ArrayList)list.Clone();             // FIXME: Do we need this?
951                                 return clone;
952                         }
953                         #endregion // ControlCollection Interface Methods
954                 }
955                 #endregion      // ControlCollection Class
956                 
957                 #region Public Constructors
958                 public Control ()
959                 {
960                         layout_type = LayoutType.Anchor;
961                         anchor_style = AnchorStyles.Top | AnchorStyles.Left;
962
963                         is_created = false;
964                         is_visible = true;
965                         is_captured = false;
966                         is_disposed = false;
967                         is_enabled = true;
968                         is_entered = false;
969                         layout_pending = false;
970                         is_toplevel = false;
971                         causes_validation = true;
972                         has_focus = false;
973                         layout_suspended = 0;
974                         mouse_clicks = 1;
975                         tab_index = -1;
976                         cursor = null;
977                         right_to_left = RightToLeft.Inherit;
978                         border_style = BorderStyle.None;
979                         background_color = Color.Empty;
980                         dist_right = 0;
981                         dist_bottom = 0;
982                         tab_stop = true;
983                         ime_mode = ImeMode.Inherit;
984                         use_compatible_text_rendering = true;
985
986 #if NET_2_0
987                         backgroundimage_layout = ImageLayout.Tile;
988                         use_compatible_text_rendering = Application.use_compatible_text_rendering;
989                         padding = new Padding(0);
990                         maximum_size = new Size();
991                         minimum_size = new Size();
992                         margin = this.DefaultMargin;
993 #endif
994
995                         control_style = ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | 
996                                         ControlStyles.Selectable | ControlStyles.StandardClick | 
997                                         ControlStyles.StandardDoubleClick;
998 #if NET_2_0
999                         control_style |= ControlStyles.UseTextForAccessibility;
1000 #endif
1001
1002                         parent = null;
1003                         background_image = null;
1004                         text = string.Empty;
1005                         name = string.Empty;
1006
1007                         window_target = new ControlWindowTarget(this);
1008                         window = new ControlNativeWindow(this);
1009                         child_controls = CreateControlsInstance();
1010                         client_size = new Size(DefaultSize.Width, DefaultSize.Height);
1011                         client_rect = new Rectangle(0, 0, DefaultSize.Width, DefaultSize.Height);
1012                         bounds.Size = InternalSizeFromClientSize (client_size);
1013                         explicit_bounds = bounds;
1014                 }
1015
1016                 public Control (Control parent, string text) : this()
1017                 {
1018                         Text=text;
1019                         Parent=parent;
1020                 }
1021
1022                 public Control (Control parent, string text, int left, int top, int width, int height) : this()
1023                 {
1024                         Parent=parent;
1025                         bounds.X=left;
1026                         bounds.Y=top;
1027                         bounds.Width=width;
1028                         bounds.Height=height;
1029                         SetBounds(left, top, width, height, BoundsSpecified.All);
1030                         Text=text;
1031                 }
1032
1033                 public Control (string text) : this()
1034                 {
1035                         Text=text;
1036                 }
1037
1038                 public Control (string text, int left, int top, int width, int height) : this()
1039                 {
1040                         bounds.X=left;
1041                         bounds.Y=top;
1042                         bounds.Width=width;
1043                         bounds.Height=height;
1044                         SetBounds(left, top, width, height, BoundsSpecified.All);
1045                         Text=text;
1046                 }
1047
1048                 private delegate void RemoveDelegate(object c);
1049
1050                 protected override void Dispose (bool disposing)
1051                 {
1052                         if (!is_disposed && disposing) {
1053                                 Capture = false;
1054
1055                                 DisposeBackBuffer ();
1056
1057                                 if (bmp != null) {
1058                                         bmp.Dispose ();
1059                                         bmp = null;
1060                                 }
1061                                 if (bmp_g != null) {
1062                                         bmp_g.Dispose ();
1063                                         bmp_g = null;
1064                                 }
1065                                 
1066                                 if (this.InvokeRequired) {
1067                                         if (Application.MessageLoop && IsHandleCreated) {
1068                                                 this.BeginInvokeInternal(new MethodInvoker(DestroyHandle), null);
1069                                         }
1070                                 } else {
1071                                         DestroyHandle();
1072                                 }
1073
1074                                 if (parent != null) {
1075                                         parent.Controls.Remove(this);
1076                                 }
1077
1078                                 Control [] children = child_controls.GetAllControls ();
1079                                 for (int i=0; i<children.Length; i++) {
1080                                         children[i].parent = null;      // Need to set to null or our child will try and remove from ourselves and crash
1081                                         children[i].Dispose();
1082                                 }
1083                         }
1084                         is_disposed = true;
1085                         is_visible = false;
1086                         base.Dispose(disposing);
1087                 }
1088                 #endregion      // Public Constructors
1089
1090                 #region Internal Properties
1091                 // Control is currently selected, like Focused, except maintains state
1092                 // when Form loses focus
1093                 internal bool InternalSelected {
1094                         get {
1095                                 IContainerControl container;
1096                         
1097                                 container = GetContainerControl();
1098                                 
1099                                 if (container != null && container.ActiveControl == this)
1100                                         return true;
1101                                         
1102                                 return false;
1103                         }
1104                 }
1105                 
1106                 // Mouse is currently within the control's bounds
1107                 internal bool Entered {
1108                         get { return this.is_entered; }
1109                 }
1110
1111                 internal bool VisibleInternal {
1112                         get { return is_visible; }
1113                 }
1114
1115                 internal LayoutType ControlLayoutType {
1116                         get { return layout_type; }
1117                 }
1118
1119                 internal BorderStyle InternalBorderStyle {
1120                         get {
1121                                 return border_style;
1122                         }
1123
1124                         set {
1125                                 if (!Enum.IsDefined (typeof (BorderStyle), value))
1126                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for BorderStyle", value));
1127
1128                                 if (border_style != value) {
1129                                         border_style = value;
1130
1131                                         if (IsHandleCreated) {
1132                                                 XplatUI.SetBorderStyle (window.Handle, (FormBorderStyle)border_style);
1133                                                 RecreateHandle ();
1134                                                 Refresh ();
1135                                         }
1136                                 }
1137                         }
1138                 }
1139                 
1140                 internal Size InternalClientSize { set { this.client_size = value; } }
1141                 internal virtual bool ActivateOnShow { get { return true; } }
1142                 #endregion      // Internal Properties
1143
1144                 #region Private & Internal Methods
1145                 
1146 #if NET_2_0
1147                 void IDropTarget.OnDragDrop (DragEventArgs e)
1148                 {
1149                         OnDragDrop (e);
1150                 }
1151                 
1152                 void IDropTarget.OnDragEnter (DragEventArgs e)
1153                 {
1154                         OnDragEnter (e);
1155                 }
1156                 
1157                 void IDropTarget.OnDragLeave (EventArgs e)
1158                 {
1159                         OnDragLeave (e);
1160                 }
1161
1162                 void IDropTarget.OnDragOver (DragEventArgs e)
1163                 {
1164                         OnDragOver (e);
1165                 }
1166 #endif
1167
1168                 internal IAsyncResult BeginInvokeInternal (Delegate method, object [] args) {
1169                         return BeginInvokeInternal (method, args, FindControlToInvokeOn ());
1170                 }
1171
1172                 internal IAsyncResult BeginInvokeInternal (Delegate method, object [] args, Control control) {
1173                         AsyncMethodResult       result;
1174                         AsyncMethodData         data;
1175
1176                         result = new AsyncMethodResult ();
1177                         data = new AsyncMethodData ();
1178
1179                         data.Handle = control.Handle;
1180                         data.Method = method;
1181                         data.Args = args;
1182                         data.Result = result;
1183
1184 #if NET_2_0
1185                         if (!ExecutionContext.IsFlowSuppressed ()) {
1186                                 data.Context = ExecutionContext.Capture ();
1187                         }
1188 #else
1189 #if !MWF_ON_MSRUNTIME
1190                         if (SecurityManager.SecurityEnabled) {
1191                                 data.Stack = CompressedStack.GetCompressedStack ();
1192                         }
1193 #endif
1194 #endif
1195
1196                         XplatUI.SendAsyncMethod (data);
1197                         return result;
1198                 }
1199
1200                 
1201                 internal void PointToClient (ref int x, ref int y) {
1202                         XplatUI.ScreenToClient (Handle, ref x, ref y);
1203                 }
1204
1205                 internal void PointToScreen (ref int x, ref int y) {
1206                         XplatUI.ClientToScreen (Handle, ref x, ref y);
1207                 }
1208
1209                 internal bool IsRecreating {
1210                         get {
1211                                 return is_recreating;
1212                         }
1213                 }
1214
1215                 internal Graphics DeviceContext {
1216                         get {
1217                                 if (bmp_g == null) {
1218                                         bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
1219                                         bmp_g = Graphics.FromImage (bmp);
1220                                 }
1221                                 return bmp_g;
1222                         }
1223                 }
1224                 
1225                 private Control FindControlToInvokeOn ()
1226                 {
1227                         Control p = this;
1228                         do {
1229                                 if (p.IsHandleCreated)
1230                                         break;
1231                                 p = p.parent;
1232                         } while (p != null);
1233
1234                         if (p == null || !p.IsHandleCreated)
1235                                 throw new InvalidOperationException ("Cannot call Invoke or BeginInvoke on a control until the window handle is created");
1236                         
1237                         return p;
1238                 }
1239
1240                 private void InvalidateBackBuffer () {
1241                         if (backbuffer != null)
1242                                 backbuffer.Invalidate ();
1243                 }
1244
1245                 private DoubleBuffer GetBackBuffer () {
1246                         if (backbuffer == null)
1247                                 backbuffer = new DoubleBuffer (this);
1248                         return backbuffer;
1249                 }
1250
1251                 private void DisposeBackBuffer () {
1252                         if (backbuffer != null) {
1253                                 backbuffer.Dispose ();
1254                                 backbuffer = null;
1255                         }
1256                 }
1257
1258                 internal static void SetChildColor(Control parent) {
1259                         Control child;
1260
1261                         for (int i=0; i < parent.child_controls.Count; i++) {
1262                                 child=parent.child_controls[i];
1263                                 if (child.child_controls.Count>0) {
1264                                         SetChildColor(child);
1265                                 }
1266                         }
1267                 }
1268
1269                 internal bool Select(Control control) {
1270                         IContainerControl       container;
1271
1272                         if (control == null) {
1273                                 return false;
1274                         }
1275
1276                         container = GetContainerControl();
1277                         if (container != null && (Control)container != control) {
1278                                 container.ActiveControl = control;
1279                         }
1280                         else if (control.IsHandleCreated) {
1281                                 XplatUI.SetFocus(control.window.Handle);
1282                         }
1283                         return true;
1284                 }
1285
1286                 internal virtual void DoDefaultAction() {
1287                         // Only here to be overriden by our actual controls; this is needed by the accessibility class
1288                 }
1289
1290                 internal static IntPtr MakeParam (int low, int high){
1291                         return new IntPtr (high << 16 | low & 0xffff);
1292                 }
1293
1294                 internal static int LowOrder (int param) {
1295                         return ((int)(short)(param & 0xffff));
1296                 }
1297
1298                 internal static int HighOrder (int param) {
1299                         return ((int)(short)(param >> 16));
1300                 }
1301
1302                 // This method exists so controls overriding OnPaintBackground can have default background painting done
1303                 internal virtual void PaintControlBackground (PaintEventArgs pevent) {
1304                         if (GetStyle(ControlStyles.SupportsTransparentBackColor) && (BackColor.A != 0xff)) {
1305                                 if (parent != null) {
1306                                         PaintEventArgs  parent_pe;
1307                                         GraphicsState   state;
1308
1309                                         parent_pe = new PaintEventArgs(pevent.Graphics, new Rectangle(pevent.ClipRectangle.X + Left, pevent.ClipRectangle.Y + Top, pevent.ClipRectangle.Width, pevent.ClipRectangle.Height));
1310
1311                                         state = parent_pe.Graphics.Save();
1312                                         parent_pe.Graphics.TranslateTransform(-Left, -Top);
1313                                         parent.OnPaintBackground(parent_pe);
1314                                         parent_pe.Graphics.Restore(state);
1315
1316                                         state = parent_pe.Graphics.Save();
1317                                         parent_pe.Graphics.TranslateTransform(-Left, -Top);
1318                                         parent.OnPaint(parent_pe);
1319                                         parent_pe.Graphics.Restore(state);
1320                                         parent_pe.SetGraphics(null);
1321                                 }
1322                         }
1323
1324                         if ((clip_region != null) && (XplatUI.UserClipWontExposeParent)) {
1325                                 if (parent != null) {
1326                                         PaintEventArgs  parent_pe;
1327                                         Region          region;
1328                                         GraphicsState   state;
1329                                         Hwnd            hwnd;
1330
1331                                         hwnd = Hwnd.ObjectFromHandle(Handle);
1332
1333                                         if (hwnd != null) {
1334                                                 parent_pe = new PaintEventArgs(pevent.Graphics, new Rectangle(pevent.ClipRectangle.X + Left, pevent.ClipRectangle.Y + Top, pevent.ClipRectangle.Width, pevent.ClipRectangle.Height));
1335
1336                                                 region = new Region ();
1337                                                 region.MakeEmpty();
1338                                                 region.Union(ClientRectangle);
1339
1340                                                 foreach (Rectangle r in hwnd.ClipRectangles) {
1341                                                         region.Union (r);
1342                                                 }
1343
1344                                                 state = parent_pe.Graphics.Save();
1345                                                 parent_pe.Graphics.Clip = region;
1346
1347                                                 parent_pe.Graphics.TranslateTransform(-Left, -Top);
1348                                                 parent.OnPaintBackground(parent_pe);
1349                                                 parent_pe.Graphics.Restore(state);
1350
1351                                                 state = parent_pe.Graphics.Save();
1352                                                 parent_pe.Graphics.Clip = region;
1353
1354                                                 parent_pe.Graphics.TranslateTransform(-Left, -Top);
1355                                                 parent.OnPaint(parent_pe);
1356                                                 parent_pe.Graphics.Restore(state);
1357                                                 parent_pe.SetGraphics(null);
1358
1359                                                 region.Intersect(clip_region);
1360                                                 pevent.Graphics.Clip = region;
1361                                         }
1362                                 }
1363                         }
1364
1365                         if (background_image == null) {
1366                                 pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), new Rectangle(pevent.ClipRectangle.X - 1, pevent.ClipRectangle.Y - 1, pevent.ClipRectangle.Width + 2, pevent.ClipRectangle.Height + 2));
1367                                 return;
1368                         }
1369
1370                         DrawBackgroundImage (pevent.Graphics);
1371                 }
1372
1373                 void DrawBackgroundImage (Graphics g) {
1374 #if NET_2_0
1375                         Rectangle drawing_rectangle = new Rectangle ();
1376                         g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
1377                                 
1378                         switch (backgroundimage_layout)
1379                         {
1380                         case ImageLayout.Tile:
1381                                 using (TextureBrush b = new TextureBrush (background_image, WrapMode.Tile)) {
1382                                         g.FillRectangle (b, ClientRectangle);
1383                                 }
1384                                 return;
1385                         case ImageLayout.Center:
1386                                 drawing_rectangle.Location = new Point (ClientSize.Width / 2 - background_image.Width / 2, ClientSize.Height / 2 - background_image.Height / 2);
1387                                 drawing_rectangle.Size = background_image.Size;
1388                                 break;
1389                         case ImageLayout.None:
1390                                 drawing_rectangle.Location = Point.Empty;
1391                                 drawing_rectangle.Size = background_image.Size;
1392                                 break;
1393                         case ImageLayout.Stretch:
1394                                 drawing_rectangle = ClientRectangle;
1395                                 break;
1396                         case ImageLayout.Zoom:
1397                                 drawing_rectangle = ClientRectangle;
1398                                 if ((float)background_image.Width / (float)background_image.Height < (float)drawing_rectangle.Width / (float) drawing_rectangle.Height) {
1399                                         drawing_rectangle.Width = (int) (background_image.Width * ((float)drawing_rectangle.Height / (float)background_image.Height));
1400                                         drawing_rectangle.X = (ClientRectangle.Width - drawing_rectangle.Width) / 2;
1401                                 } else {
1402                                         drawing_rectangle.Height = (int) (background_image.Height * ((float)drawing_rectangle.Width / (float)background_image.Width));
1403                                         drawing_rectangle.Y = (ClientRectangle.Height - drawing_rectangle.Height) / 2;
1404                                 }
1405                                 break;
1406                         default:
1407                                 return;
1408                         }
1409
1410                         g.DrawImage (background_image, drawing_rectangle);
1411
1412 #else
1413                         using (TextureBrush b = new TextureBrush (background_image, WrapMode.Tile)) {
1414                                 g.FillRectangle (b, ClientRectangle);
1415                         }
1416 #endif
1417                 }
1418
1419                 internal virtual void DndEnter (DragEventArgs e) {
1420                         try {
1421                                 OnDragEnter (e);
1422                         } catch { }
1423                 }
1424
1425                 internal virtual void DndOver (DragEventArgs e) {
1426                         try {
1427                                 OnDragOver (e);
1428                         } catch { }
1429                 }
1430
1431                 internal virtual void DndDrop (DragEventArgs e) {
1432                         try {
1433                                 OnDragDrop (e);
1434                         } catch (Exception exc) {
1435                                 Console.Error.WriteLine ("MWF: Exception while dropping:");
1436                                 Console.Error.WriteLine (exc);
1437                         }
1438                 }
1439
1440                 internal virtual void DndLeave (EventArgs e) {
1441                         try {
1442                                 OnDragLeave (e);
1443                         } catch { }
1444                 }
1445
1446                 internal virtual void DndFeedback(GiveFeedbackEventArgs e) {
1447                         try {
1448                                 OnGiveFeedback(e);
1449                         } catch { }
1450                 }
1451
1452                 internal virtual void DndContinueDrag(QueryContinueDragEventArgs e) {
1453                         try {
1454                                 OnQueryContinueDrag(e);
1455                         } catch { }
1456                 }
1457                 
1458                 internal static MouseButtons FromParamToMouseButtons (int param) {              
1459                         MouseButtons buttons = MouseButtons.None;
1460                                         
1461                         if ((param & (int) MsgButtons.MK_LBUTTON) != 0)
1462                                 buttons |= MouseButtons.Left;
1463                         
1464                         if ((param & (int) MsgButtons.MK_MBUTTON) != 0)
1465                                 buttons |= MouseButtons.Middle;
1466                                 
1467                         if ((param & (int) MsgButtons.MK_RBUTTON) != 0)
1468                                 buttons |= MouseButtons.Right;
1469                                 
1470                         return buttons;
1471
1472                 }
1473
1474                 internal virtual void FireEnter () {
1475                         OnEnter (EventArgs.Empty);
1476                 }
1477
1478                 internal virtual void FireLeave () {
1479                         OnLeave (EventArgs.Empty);
1480                 }
1481
1482                 internal virtual void FireValidating (CancelEventArgs ce) {
1483                         OnValidating (ce);
1484                 }
1485
1486                 internal virtual void FireValidated () {
1487                         OnValidated (EventArgs.Empty);
1488                 }
1489
1490                 internal virtual bool ProcessControlMnemonic(char charCode) {
1491                         return ProcessMnemonic(charCode);
1492                 }
1493
1494                 private static Control FindFlatForward(Control container, Control start) {
1495                         Control found;
1496                         int     index;
1497                         int     end;
1498
1499                         found = null;
1500                         end = container.child_controls.Count;
1501
1502                         if (start != null) {
1503                                 index = start.tab_index;
1504                         } else {
1505                                 index = -1;
1506                         }
1507
1508                         for (int i = 0, pos = -1; i < end; i++) {
1509                                 if (start == container.child_controls[i]) {
1510                                         pos = i;
1511                                         continue;
1512                                 }
1513
1514                                 if (found == null) {
1515                                         if (container.child_controls[i].tab_index > index || (pos > -1 && pos < i && container.child_controls[i].tab_index == index)) {
1516                                                 found = container.child_controls[i];
1517                                         }
1518                                 } else if (found.tab_index > container.child_controls[i].tab_index) {
1519                                         if (container.child_controls[i].tab_index > index) {
1520                                                 found = container.child_controls[i];
1521                                         }
1522                                 }
1523                         }
1524                         return found;
1525                 }
1526
1527                 private static Control FindControlForward(Control container, Control start) {
1528                         Control found;
1529
1530                         found = null;
1531
1532                         if (start == null) {
1533                                 return FindFlatForward(container, start);
1534                         }
1535
1536                         if (start.child_controls != null && start.child_controls.Count > 0 && 
1537                                 (start == container || !((start is IContainerControl) &&  start.GetStyle(ControlStyles.ContainerControl)))) {
1538                                 return FindControlForward(start, null);
1539                         }
1540                         else {
1541                                 while (start != container) {
1542                                         found = FindFlatForward(start.parent, start);
1543                                         if (found != null) {
1544                                                 return found;
1545                                         }
1546                                         start = start.parent;
1547                                 }
1548                         }
1549                         return null;
1550                 }
1551
1552                 private static Control FindFlatBackward(Control container, Control start) {
1553                         Control found;
1554                         int     index;
1555                         int     end;
1556
1557                         found = null;
1558                         end = container.child_controls.Count;
1559
1560                         if (start != null) {
1561                                 index = start.tab_index;
1562                         } else {
1563                                 // FIXME: Possible speed-up: Keep the highest taborder index in the container
1564                                 index = -1;
1565                                 for (int i = 0; i < end; i++) {
1566                                         if (container.child_controls[i].tab_index > index) {
1567                                                 index = container.child_controls[i].tab_index;
1568                                         }
1569                                 }
1570                                 index++;
1571                         }
1572
1573                         bool hit = false;
1574                                         
1575                         for (int i = end - 1; i >= 0; i--) {
1576                                 if (start == container.child_controls[i]) {
1577                                         hit = true;
1578                                         continue;
1579                                 }
1580
1581                                 if (found == null || found.tab_index < container.child_controls[i].tab_index) {
1582                                         if (container.child_controls[i].tab_index < index || (hit && container.child_controls[i].tab_index == index))
1583                                                 found = container.child_controls[i];
1584
1585                                 }
1586                         }
1587                         return found;
1588                 }
1589
1590                 private static Control FindControlBackward(Control container, Control start) {
1591
1592                         Control found = null;
1593
1594                         if (start == null) {
1595                                 found = FindFlatBackward(container, start);
1596                         }
1597                         else if (start != container) {
1598                                 if (start.parent != null) {
1599                                         found = FindFlatBackward(start.parent, start);
1600
1601                                         if (found == null) {
1602                                                 if (start.parent != container)
1603                                                         return start.parent;
1604                                                 return null;
1605                                         }
1606                                 }
1607                         }
1608                 
1609                         if (found == null || start.parent == null)
1610                                 found = start;
1611
1612                         while (found != null && (found == container || (!((found is IContainerControl) && found.GetStyle(ControlStyles.ContainerControl))) &&
1613                                 found.child_controls != null && found.child_controls.Count > 0)) {
1614 //                              while (ctl.child_controls != null && ctl.child_controls.Count > 0 && 
1615 //                                      (ctl == this || (!((ctl is IContainerControl) && ctl.GetStyle(ControlStyles.ContainerControl))))) {
1616                                 found = FindFlatBackward(found, null);
1617                         }
1618
1619                         return found;
1620
1621 /*
1622                         Control found;
1623
1624                         found = null;
1625
1626                         if (start != null) {
1627                                 found = FindFlatBackward(start.parent, start);
1628                                 if (found == null) {
1629                                         if (start.parent != container) {
1630                                                 return start.parent;
1631                                         }
1632                                 }
1633                         }
1634                         if (found == null) {
1635                                 found = FindFlatBackward(container, start);
1636                         }
1637
1638                         if (container != start) {
1639                                 while ((found != null) && (!found.Contains(start)) && found.child_controls != null && found.child_controls.Count > 0 && !(found is IContainerControl)) {// || found.GetStyle(ControlStyles.ContainerControl))) {
1640                                         found = FindControlBackward(found, null);
1641                                          if (found != null) {
1642                                                 return found;
1643                                         }
1644                                 }
1645                         }
1646                         return found;
1647 */                      
1648                 }
1649
1650                 internal virtual void HandleClick(int clicks, MouseEventArgs me) {
1651                         if (GetStyle(ControlStyles.StandardClick)) {
1652                                 if ((clicks > 1) && GetStyle(ControlStyles.StandardDoubleClick)) {
1653 #if !NET_2_0
1654                                         OnDoubleClick(EventArgs.Empty);
1655                                 } else {
1656                                         OnClick(EventArgs.Empty);
1657 #else
1658                                         OnDoubleClick(me);
1659                                         OnMouseDoubleClick (me);
1660                                 } else {
1661                                         OnClick(me);
1662                                         OnMouseClick (me);
1663 #endif
1664                                 }
1665                         }
1666                 }
1667                 
1668                 internal void CaptureWithConfine (Control ConfineWindow) {
1669                         if (this.IsHandleCreated && !is_captured) {
1670                                 is_captured = true;
1671                                 XplatUI.GrabWindow (this.window.Handle, ConfineWindow.Handle);
1672                         }
1673                 }
1674
1675                 private void CheckDataBindings () {
1676                         if (data_bindings == null)
1677                                 return;
1678
1679                         BindingContext binding_context = BindingContext;
1680                         foreach (Binding binding in data_bindings) {
1681                                 binding.Check (binding_context);
1682                         }
1683                 }
1684
1685                 private void ChangeParent(Control new_parent) {
1686                         bool            pre_enabled;
1687                         bool            pre_visible;
1688                         Font            pre_font;
1689                         Color           pre_fore_color;
1690                         Color           pre_back_color;
1691                         RightToLeft     pre_rtl;
1692
1693                         // These properties are inherited from our parent
1694                         // Get them pre parent-change and then send events
1695                         // if they are changed after we have our new parent
1696                         pre_enabled = Enabled;
1697                         pre_visible = Visible;
1698                         pre_font = Font;
1699                         pre_fore_color = ForeColor;
1700                         pre_back_color = BackColor;
1701                         pre_rtl = RightToLeft;
1702                         // MS doesn't seem to send a CursorChangedEvent
1703
1704                         parent = new_parent;
1705
1706                         if (IsHandleCreated) {
1707                                 XplatUI.SetParent(Handle,
1708                                                   (new_parent == null || !new_parent.IsHandleCreated) ? IntPtr.Zero : new_parent.Handle);
1709                                 if (this is Form ) {
1710                                         ((Form) this).ChangingParent (new_parent);
1711                                 }
1712                         }
1713
1714                         OnParentChanged(EventArgs.Empty);
1715
1716                         if (pre_enabled != Enabled) {
1717                                 OnEnabledChanged(EventArgs.Empty);
1718                         }
1719
1720                         if (pre_visible != Visible) {
1721                                 OnVisibleChanged(EventArgs.Empty);
1722                         }
1723
1724                         if (pre_font != Font) {
1725                                 OnFontChanged(EventArgs.Empty);
1726                         }
1727
1728                         if (pre_fore_color != ForeColor) {
1729                                 OnForeColorChanged(EventArgs.Empty);
1730                         }
1731
1732                         if (pre_back_color != BackColor) {
1733                                 OnBackColorChanged(EventArgs.Empty);
1734                         }
1735
1736                         if (pre_rtl != RightToLeft) {
1737                                 // MS sneaks a OnCreateControl and OnHandleCreated in here, I guess
1738                                 // because when RTL changes they have to recreate the win32 control
1739                                 // We don't really need that (until someone runs into compatibility issues)
1740                                 OnRightToLeftChanged(EventArgs.Empty);
1741                         }
1742
1743                         if ((new_parent != null) && new_parent.Created && is_visible && !Created) {
1744                                 CreateControl();
1745                         }
1746
1747                         if ((binding_context == null) && Created) {
1748                                 OnBindingContextChanged(EventArgs.Empty);
1749                         }
1750                 }
1751
1752                 // Sometimes we need to do this calculation without it being virtual (constructor)
1753                 internal Size InternalSizeFromClientSize (Size clientSize)
1754                 {
1755                         Rectangle ClientRect;
1756                         Rectangle WindowRect;
1757                         CreateParams cp;
1758
1759                         ClientRect = new Rectangle (0, 0, clientSize.Width, clientSize.Height);
1760                         cp = this.CreateParams;
1761
1762                         if (XplatUI.CalculateWindowRect (ref ClientRect, cp.Style, cp.ExStyle, null, out WindowRect))
1763                                 return new Size (WindowRect.Width, WindowRect.Height);
1764
1765                         return Size.Empty;
1766                 }
1767
1768                 private void UpdateDistances() {
1769                         if (parent != null) {
1770                                 if (bounds.Width >= 0)
1771                                         dist_right = parent.ClientSize.Width - bounds.X - bounds.Width;
1772                                 if (bounds.Height >= 0)
1773                                         dist_bottom = parent.ClientSize.Height - bounds.Y - bounds.Height;
1774                         }
1775                 }
1776                 
1777                 private bool UseDoubleBuffering {
1778                         get {
1779                                 if (!ThemeEngine.Current.DoubleBufferingSupported)
1780                                         return false;
1781
1782 #if NET_2_0
1783                                 if (DoubleBuffered)
1784                                         return true;
1785 #endif
1786                                 return (control_style & ControlStyles.DoubleBuffer) != 0;
1787                         }
1788                 }
1789                 #endregion      // Private & Internal Methods
1790
1791                 #region Public Static Properties
1792                 public static Color DefaultBackColor {
1793                         get {
1794                                 return ThemeEngine.Current.DefaultControlBackColor;
1795                         }
1796                 }
1797
1798                 public static Font DefaultFont {
1799                         get {
1800                                 return ThemeEngine.Current.DefaultFont;
1801                         }
1802                 }
1803
1804                 public static Color DefaultForeColor {
1805                         get {
1806                                 return ThemeEngine.Current.DefaultControlForeColor;
1807                         }
1808                 }
1809
1810                 public static Keys ModifierKeys {
1811                         get {
1812                                 return XplatUI.State.ModifierKeys;
1813                         }
1814                 }
1815
1816                 public static MouseButtons MouseButtons {
1817                         get {
1818                                 return XplatUI.State.MouseButtons;
1819                         }
1820                 }
1821
1822                 public static Point MousePosition {
1823                         get {
1824                                 return Cursor.Position;
1825                         }
1826                 }
1827                 
1828 #if NET_2_0
1829                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1830                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1831                 [Browsable (false)]
1832                 [MonoTODO]
1833                 public static bool CheckForIllegalCrossThreadCalls 
1834                 {
1835                         get {
1836                                 return verify_thread_handle;
1837                         }
1838
1839                         set {
1840                                 verify_thread_handle = value;
1841                         }
1842                 }
1843 #endif
1844                 #endregion      // Public Static Properties
1845
1846                 #region Public Instance Properties
1847                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1848                 [Browsable(false)]
1849                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1850                 public AccessibleObject AccessibilityObject {
1851                         get {
1852                                 if (accessibility_object==null) {
1853                                         accessibility_object=CreateAccessibilityInstance();
1854                                 }
1855                                 return accessibility_object;
1856                         }
1857                 }
1858
1859                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1860                 [Browsable(false)]
1861                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1862                 public string AccessibleDefaultActionDescription {
1863                         get {
1864                                 if (accessibility_object != null)
1865                                         return accessibility_object.default_action;
1866                                 else
1867                                         return null;
1868                         }
1869
1870                         set {
1871                                 if (accessibility_object != null)
1872                                         accessibility_object.default_action = value;
1873                         }
1874                 }
1875
1876                 [Localizable(true)]
1877                 [DefaultValue(null)]
1878                 [MWFCategory("Accessibility")]
1879                 public string AccessibleDescription {
1880                         get {
1881                                 if (accessibility_object != null)
1882                                         return accessibility_object.description;
1883                                 else
1884                                         return null;
1885                         }
1886
1887                         set {
1888                                 if (accessibility_object != null)
1889                                         accessibility_object.description = value;
1890                         }
1891                 }
1892
1893                 [Localizable(true)]
1894                 [DefaultValue(null)]
1895                 [MWFCategory("Accessibility")]
1896                 public string AccessibleName {
1897                         get {
1898                                 if (accessibility_object != null)
1899                                         return accessibility_object.Name;
1900                                 else
1901                                         return null;
1902                         }
1903
1904                         set {
1905                                 if (accessibility_object != null)
1906                                         accessibility_object.Name = value;
1907                         }
1908                 }
1909
1910                 [DefaultValue(AccessibleRole.Default)]
1911                 [MWFDescription("Role of the control"), MWFCategory("Accessibility")]
1912                 public AccessibleRole AccessibleRole {
1913                         get {
1914                                 if (accessibility_object != null)
1915                                         return accessibility_object.role;
1916                                 else
1917                                         return AccessibleRole.Default;
1918                         }
1919
1920                         set {
1921                                 if (accessibility_object != null)
1922                                         accessibility_object.role = value;
1923                         }
1924                 }
1925
1926                 [DefaultValue(false)]
1927                 [MWFCategory("Behavior")]
1928                 public virtual bool AllowDrop {
1929                         get {
1930                                 return allow_drop;
1931                         }
1932
1933                         set {
1934                                 if (allow_drop == value)
1935                                         return;
1936                                 allow_drop = value;
1937                                 if (IsHandleCreated) {
1938                                         UpdateStyles();
1939                                         XplatUI.SetAllowDrop (Handle, value);
1940                                 }
1941                         }
1942                 }
1943
1944                 [Localizable(true)]
1945                 [RefreshProperties(RefreshProperties.Repaint)]
1946                 [DefaultValue(AnchorStyles.Top | AnchorStyles.Left)]
1947                 [MWFCategory("Layout")]
1948                 public virtual AnchorStyles Anchor {
1949                         get {
1950                                 return anchor_style;
1951                         }
1952
1953                         set {
1954                                 layout_type = LayoutType.Anchor;
1955
1956                                 if (anchor_style == value)
1957                                         return;
1958                                         
1959                                 anchor_style=value;
1960                                 dock_style = DockStyle.None;
1961                                 
1962                                 UpdateDistances ();
1963
1964                                 if (parent != null)
1965                                         parent.PerformLayout(this, "Anchor");
1966                         }
1967                 }
1968
1969 #if NET_2_0
1970                 // XXX: Implement me!
1971                 bool auto_size;
1972                 
1973                 [RefreshProperties (RefreshProperties.All)]
1974                 [Localizable (true)]
1975                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1976                 [Browsable (false)]
1977                 [EditorBrowsable (EditorBrowsableState.Never)]
1978                 [DefaultValue (false)]
1979                 [MonoTODO("This method currently does nothing")]
1980                 public virtual bool AutoSize {
1981                         get { return auto_size; }
1982                         set {
1983                                 if (this.auto_size != value) {
1984                                         auto_size = value;
1985                                         OnAutoSizeChanged (EventArgs.Empty);
1986                                 }
1987                         }
1988                 }
1989                 
1990 #if NET_2_0
1991                 [AmbientValue ("{Width=0, Height=0}")]
1992 #else
1993                 [AmbientValue (typeof(Size), "0, 0")]
1994 #endif
1995                 public virtual Size MaximumSize {
1996                         get {
1997                                 return maximum_size;
1998                         }
1999                         set {
2000                                 if (maximum_size != value) {
2001                                         maximum_size = value;
2002                                         Size = PreferredSize;
2003                                 }
2004                         }
2005                 }
2006
2007                 public virtual Size MinimumSize {
2008                         get {
2009                                 return minimum_size;
2010                         }
2011                         set {
2012                                 if (minimum_size != value) {
2013                                         minimum_size = value;
2014                                         Size = PreferredSize;
2015                                 }
2016                         }
2017                 }
2018 #endif // NET_2_0
2019
2020                 [DispId(-501)]
2021                 [MWFCategory("Appearance")]
2022                 public virtual Color BackColor {
2023                         get {
2024                                 if (background_color.IsEmpty) {
2025                                         if (parent!=null) {
2026                                                 Color pcolor = parent.BackColor;
2027                                                 if (pcolor.A == 0xff || GetStyle(ControlStyles.SupportsTransparentBackColor))
2028                                                         return pcolor;
2029                                         }
2030                                         return DefaultBackColor;
2031                                 }
2032                                 return background_color;
2033                         }
2034
2035                         set {
2036                                 if (!value.IsEmpty && (value.A != 0xff) && !GetStyle(ControlStyles.SupportsTransparentBackColor)) {
2037                                         throw new ArgumentException("Transparent background colors are not supported on this control");
2038                                 }
2039
2040                                 if (background_color != value) {
2041                                         background_color=value;
2042                                         SetChildColor(this);
2043                                         OnBackColorChanged(EventArgs.Empty);
2044                                         Invalidate();
2045                                 }
2046                         }
2047                 }
2048
2049                 [Localizable(true)]
2050                 [DefaultValue(null)]
2051                 [MWFCategory("Appearance")]
2052                 public virtual Image BackgroundImage {
2053                         get {
2054                                 return background_image;
2055                         }
2056
2057                         set {
2058                                 if (background_image!=value) {
2059                                         background_image=value;
2060                                         OnBackgroundImageChanged(EventArgs.Empty);
2061                                         Invalidate ();
2062                                 }
2063                         }
2064                 }
2065
2066 #if NET_2_0
2067                 [DefaultValue (ImageLayout.Tile)]
2068                 [Localizable (true)]
2069                 public virtual ImageLayout BackgroundImageLayout {
2070                         get {
2071                                 return backgroundimage_layout;
2072                         }
2073                         set {
2074                                 if (Array.IndexOf (Enum.GetValues (typeof (ImageLayout)), value) == -1)
2075                                         throw new InvalidEnumArgumentException ("value", (int) value, typeof(ImageLayout));
2076
2077                                 if (value != backgroundimage_layout) {
2078                                         backgroundimage_layout = value;
2079                                         Invalidate ();
2080                                         OnBackgroundImageLayoutChanged (EventArgs.Empty);
2081                                 }
2082                                 
2083                         }
2084                 } 
2085 #endif
2086                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2087                 [Browsable(false)]
2088                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2089                 public virtual BindingContext BindingContext {
2090                         get {
2091                                 if (binding_context != null)
2092                                         return binding_context;
2093                                 if (Parent == null)
2094                                         return null;
2095                                 binding_context = Parent.BindingContext;
2096                                 return binding_context;
2097                         }
2098                         set {
2099                                 if (binding_context != value) {
2100                                         binding_context = value;
2101                                         OnBindingContextChanged(EventArgs.Empty);
2102                                 }
2103                         }
2104                 }
2105
2106                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2107                 [Browsable(false)]
2108                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2109                 public int Bottom {
2110                         get {
2111                                 return bounds.Y+bounds.Height;
2112                         }
2113                 }
2114
2115                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2116                 [Browsable(false)]
2117                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2118                 public Rectangle Bounds {
2119                         get {
2120                                 return this.bounds;
2121                         }
2122
2123                         set {
2124                                 SetBounds(value.Left, value.Top, value.Width, value.Height, BoundsSpecified.All);
2125                         }
2126                 }
2127
2128                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2129                 [Browsable(false)]
2130                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2131                 public bool CanFocus {
2132                         get {
2133                                 if (IsHandleCreated && Visible && Enabled) {
2134                                         return true;
2135                                 }
2136                                 return false;
2137                         }
2138                 }
2139
2140                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2141                 [Browsable(false)]
2142                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2143                 public bool CanSelect {
2144                         get {
2145                                 Control parent;
2146
2147                                 if (!GetStyle(ControlStyles.Selectable)) {
2148                                         return false;
2149                                 }
2150
2151                                 parent = this;
2152                                 while (parent != null) {
2153                                         if (!parent.is_visible || !parent.is_enabled) {
2154                                                 return false;
2155                                         }
2156
2157                                         parent = parent.parent;
2158                                 }
2159                                 return true;
2160                         }
2161                 }
2162
2163                 internal virtual bool InternalCapture {
2164                         get {
2165                                 return Capture;
2166                         }
2167
2168                         set {
2169                                 Capture = value;
2170                         }
2171                 }
2172
2173                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2174                 [Browsable(false)]
2175                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2176                 public bool Capture {
2177                         get {
2178                                 return this.is_captured;
2179                         }
2180
2181                         set {
2182                                 // Call OnMouseCaptureChanged when we get WM_CAPTURECHANGED.
2183                                 if (value != is_captured) {
2184                                         if (value) {
2185                                                 is_captured = true;
2186                                                 XplatUI.GrabWindow(Handle, IntPtr.Zero);
2187                                         } else {
2188                                                 if (IsHandleCreated)
2189                                                         XplatUI.UngrabWindow(Handle);
2190                                                 is_captured = false;
2191                                         }
2192                                 }
2193                         }
2194                 }
2195
2196                 [DefaultValue(true)]
2197                 [MWFCategory("Focus")]
2198                 public bool CausesValidation {
2199                         get {
2200                                 return this.causes_validation;
2201                         }
2202
2203                         set {
2204                                 if (this.causes_validation != value) {
2205                                         causes_validation = value;
2206                                         OnCausesValidationChanged(EventArgs.Empty);
2207                                 }
2208                         }
2209                 }
2210
2211                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2212                 [Browsable(false)]
2213                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2214                 public Rectangle ClientRectangle {
2215                         get {
2216                                 client_rect.Width = client_size.Width;
2217                                 client_rect.Height = client_size.Height;
2218                                 return client_rect;
2219                         }
2220                 }
2221
2222                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2223                 [Browsable(false)]
2224                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2225                 public Size ClientSize {
2226                         get {
2227 #if notneeded
2228                                 if ((this is Form) && (((Form)this).form_parent_window != null)) {
2229                                         return ((Form)this).form_parent_window.ClientSize;
2230                                 }
2231 #endif
2232
2233                                 return client_size;
2234                         }
2235
2236                         set {
2237                                 this.SetClientSizeCore(value.Width, value.Height);
2238 #if NET_2_0
2239                                 this.OnClientSizeChanged (EventArgs.Empty);
2240 #endif
2241                         }
2242                 }
2243
2244                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2245                 [Browsable(false)]
2246                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2247                 [DescriptionAttribute("ControlCompanyNameDescr")]
2248                 public String CompanyName {
2249                         get {
2250                                 return "Mono Project, Novell, Inc.";
2251                         }
2252                 }
2253
2254                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2255                 [Browsable(false)]
2256                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2257                 public bool ContainsFocus {
2258                         get {
2259                                 IntPtr focused_window;
2260
2261                                 focused_window = XplatUI.GetFocus();
2262                                 if (IsHandleCreated) {
2263                                         if (focused_window == Handle) {
2264                                                 return true;
2265                                         }
2266
2267                                         for (int i=0; i < child_controls.Count; i++) {
2268                                                 if (child_controls[i].ContainsFocus) {
2269                                                         return true;
2270                                                 }
2271                                         }
2272                                 }
2273                                 return false;
2274                         }
2275                 }
2276 #if NET_2_0
2277                 [Browsable (false)]
2278 #endif
2279                 [DefaultValue(null)]
2280                 [MWFCategory("Behavior")]
2281                 public virtual ContextMenu ContextMenu {
2282                         get {
2283                                 return GetContextMenuInternal ();
2284                         }
2285
2286                         set {
2287                                 if (context_menu != value) {
2288                                         context_menu = value;
2289                                         OnContextMenuChanged(EventArgs.Empty);
2290                                 }
2291                         }
2292                 }
2293
2294                 internal virtual ContextMenu GetContextMenuInternal () {
2295                         return context_menu;
2296                 }
2297
2298 #if NET_2_0
2299                 [DefaultValue (null)]
2300                 public virtual ContextMenuStrip ContextMenuStrip {
2301                         get { return this.context_menu_strip; }
2302                         set { 
2303                                 if (this.context_menu_strip != value) {
2304                                         this.context_menu_strip = value;
2305                                         OnContextMenuStripChanged (EventArgs.Empty);
2306                                 }
2307                         }
2308                 }
2309 #endif
2310
2311                 [Browsable(false)]
2312                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
2313                 public ControlCollection Controls {
2314                         get {
2315                                 return this.child_controls;
2316                         }
2317                 }
2318
2319                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2320                 [Browsable(false)]
2321                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2322                 public bool Created {
2323                         get {
2324                                 return (!is_disposed && is_created);
2325                         }
2326                 }
2327
2328                 [AmbientValue(null)]
2329                 [MWFCategory("Appearance")]
2330                 public virtual Cursor Cursor {
2331                         get {
2332                                 if (cursor != null) {
2333                                         return cursor;
2334                                 }
2335
2336                                 if (parent != null) {
2337                                         return parent.Cursor;
2338                                 }
2339
2340                                 return Cursors.Default;
2341                         }
2342
2343                         set {
2344                                 if (cursor != value) {
2345                                         Point pt;
2346
2347                                         cursor = value;
2348                                         
2349                                         if (IsHandleCreated) {
2350                                                 pt = Cursor.Position;
2351
2352                                                 if (bounds.Contains(pt) || Capture) {
2353                                                         if (GetChildAtPoint(pt) == null) {
2354                                                                 if (cursor != null) {
2355                                                                         XplatUI.SetCursor(window.Handle, cursor.handle);
2356                                                                 } else {
2357                                                                         if (parent != null) {
2358                                                                                 XplatUI.SetCursor(window.Handle, parent.Cursor.handle);
2359                                                                         } else {
2360                                                                                 XplatUI.SetCursor(window.Handle, Cursors.Default.handle);
2361                                                                         }
2362                                                                 }
2363                                                         }
2364                                                 }
2365                                         }
2366
2367                                         OnCursorChanged(EventArgs.Empty);
2368                                 }
2369                         }
2370                 }
2371
2372
2373                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
2374                 [ParenthesizePropertyName(true)]
2375                 [RefreshProperties(RefreshProperties.All)]
2376                 [MWFCategory("Data")]
2377                 public ControlBindingsCollection DataBindings {
2378                         get {
2379                                 if (data_bindings == null)
2380                                         data_bindings = new ControlBindingsCollection (this);
2381                                 return data_bindings;
2382                         }
2383                 }
2384
2385                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2386                 [Browsable(false)]
2387                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2388                 public virtual Rectangle DisplayRectangle {
2389                         get {
2390                                 return ClientRectangle;
2391                         }
2392                 }
2393
2394                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2395                 [Browsable(false)]
2396                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2397                 public bool Disposing {
2398                         get {
2399                                 return is_disposed;
2400                         }
2401                 }
2402
2403                 [Localizable(true)]
2404                 [RefreshProperties(RefreshProperties.Repaint)]
2405                 [DefaultValue(DockStyle.None)]
2406                 [MWFCategory("Layout")]
2407                 public virtual DockStyle Dock {
2408                         get {
2409                                 return dock_style;
2410                         }
2411
2412                         set {
2413                                 layout_type = LayoutType.Dock;
2414
2415                                 if (dock_style == value) {
2416                                         return;
2417                                 }
2418
2419                                 if (!Enum.IsDefined (typeof (DockStyle), value)) {
2420                                         throw new InvalidEnumArgumentException ("value", (int) value,
2421                                                 typeof (DockStyle));
2422                                 }
2423
2424                                 dock_style = value;
2425                                 anchor_style = AnchorStyles.Top | AnchorStyles.Left;
2426
2427                                 if (dock_style == DockStyle.None) {
2428                                         if (explicit_bounds == Rectangle.Empty)
2429                                                 Bounds = new Rectangle (new Point (0, 0), DefaultSize);
2430                                         else
2431                                                 Bounds = explicit_bounds;
2432                                 }
2433
2434                                 if (parent != null) {
2435                                         parent.PerformLayout(this, "Dock");
2436                                 }
2437
2438                                 OnDockChanged(EventArgs.Empty);
2439                         }
2440                 }
2441
2442 #if NET_2_0
2443                 protected virtual bool DoubleBuffered {
2444                         get {
2445                                 return (control_style & ControlStyles.OptimizedDoubleBuffer) != 0;
2446                         }
2447
2448                         set {
2449                                 if (value == DoubleBuffered)
2450                                         return;
2451                                 if (value) {
2452                                         SetStyle (ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
2453                                 } else {
2454                                         SetStyle (ControlStyles.OptimizedDoubleBuffer, false);
2455                                 }
2456                                 
2457                         }
2458                 }
2459 #endif
2460                 
2461                 [DispId(-514)]
2462                 [Localizable(true)]
2463                 [MWFCategory("Behavior")]
2464                 public bool Enabled {
2465                         get {
2466                                 if (!is_enabled) {
2467                                         return false;
2468                                 }
2469
2470                                 if (parent != null) {
2471                                         return parent.Enabled;
2472                                 }
2473
2474                                 return true;
2475                         }
2476
2477                         set {
2478                                 if (this.is_enabled != value) {
2479                                         bool old_value = is_enabled;
2480
2481                                         is_enabled = value;
2482                                         if (old_value != value && !value && this.has_focus)
2483                                                 SelectNextControl(this, true, true, true, true);
2484
2485                                         OnEnabledChanged (EventArgs.Empty);
2486                                 }
2487                         }
2488                 }
2489
2490                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2491                 [Browsable(false)]
2492                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2493                 public virtual bool Focused {
2494                         get {
2495                                 return this.has_focus;
2496                         }
2497                 }
2498
2499                 [DispId(-512)]
2500                 [AmbientValue(null)]
2501                 [Localizable(true)]
2502                 [MWFCategory("Appearance")]
2503                 public virtual Font Font {
2504                         get {
2505                                 if (font != null) {
2506                                         return font;
2507                                 }
2508
2509                                 if (Parent != null && Parent.Font != null) {
2510                                         return Parent.Font;
2511                                 }
2512
2513                                 return DefaultFont;
2514                         }
2515
2516                         [param:MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Font))]
2517                         set {
2518                                 if (font != null && font.Equals (value)) {
2519                                         return;
2520                                 }
2521
2522                                 font = value;
2523                                 Invalidate();
2524                                 OnFontChanged (EventArgs.Empty);
2525                                 PerformLayout ();
2526                         }
2527                 }
2528
2529                 [DispId(-513)]
2530                 [MWFCategory("Appearance")]
2531                 public virtual Color ForeColor {
2532                         get {
2533                                 if (foreground_color.IsEmpty) {
2534                                         if (parent!=null) {
2535                                                 return parent.ForeColor;
2536                                         }
2537                                         return DefaultForeColor;
2538                                 }
2539                                 return foreground_color;
2540                         }
2541
2542                         set {
2543                                 if (foreground_color != value) {
2544                                         foreground_color=value;
2545                                         Invalidate();
2546                                         OnForeColorChanged(EventArgs.Empty);
2547                                 }
2548                         }
2549                 }
2550
2551                 [DispId(-515)]
2552                 [Browsable(false)]
2553                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2554                 public IntPtr Handle {                                                  // IWin32Window
2555                         get {
2556 #if NET_2_0
2557                                 if (verify_thread_handle) {
2558                                         if (this.InvokeRequired) {
2559                                                 throw new InvalidOperationException("Cross-thread access of handle detected. Handle access only valid on thread that created the control");
2560                                         }
2561                                 }
2562 #endif
2563                                 if (!IsHandleCreated) {
2564                                         CreateHandle();
2565                                 }
2566                                 return window.Handle;
2567                         }
2568                 }
2569
2570                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2571                 [Browsable(false)]
2572                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2573                 public bool HasChildren {
2574                         get {
2575                                 if (this.child_controls.Count>0) {
2576                                         return true;
2577                                 }
2578                                 return false;
2579                         }
2580                 }
2581
2582                 [EditorBrowsable(EditorBrowsableState.Always)]
2583                 [Browsable(false)]
2584                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2585                 public int Height {
2586                         get {
2587                                 return this.bounds.Height;
2588                         }
2589
2590                         set {
2591                                 SetBounds(bounds.X, bounds.Y, bounds.Width, value, BoundsSpecified.Height);
2592                         }
2593                 }
2594
2595                 [AmbientValue(ImeMode.Inherit)]
2596                 [Localizable(true)]
2597                 [MWFCategory("Behavior")]
2598                 public ImeMode ImeMode {
2599                         get {
2600                                 if (ime_mode == ImeMode.Inherit) {
2601                                         if (parent != null)
2602                                                 return parent.ImeMode;
2603                                         else
2604                                                 return ImeMode.NoControl; // default value
2605                                 }
2606                                 return ime_mode;
2607                         }
2608
2609                         set {
2610                                 if (ime_mode != value) {
2611                                         ime_mode = value;
2612
2613                                         OnImeModeChanged(EventArgs.Empty);
2614                                 }
2615                         }
2616                 }
2617
2618                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2619                 [Browsable(false)]
2620                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2621                 public bool InvokeRequired {                                            // ISynchronizeInvoke
2622                         get {
2623                                 if (creator_thread != null && creator_thread!=Thread.CurrentThread) {
2624                                         return true;
2625                                 }
2626                                 return false;
2627                         }
2628                 }
2629
2630                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2631                 [Browsable(false)]
2632                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2633                 public bool IsAccessible {
2634                         get {
2635                                 return is_accessible;
2636                         }
2637
2638                         set {
2639                                 is_accessible = value;
2640                         }
2641                 }
2642
2643                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2644                 [Browsable(false)]
2645                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2646                 public bool IsDisposed {
2647                         get {
2648                                 return this.is_disposed;
2649                         }
2650                 }
2651
2652                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2653                 [Browsable(false)]
2654                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2655                 public bool IsHandleCreated {
2656                         get {
2657                                 if (window == null || window.Handle == IntPtr.Zero)
2658                                         return false;
2659
2660                                 Hwnd hwnd = Hwnd.ObjectFromHandle (window.Handle);
2661                                 if (hwnd != null && hwnd.zombie)
2662                                         return false;
2663
2664                                 return true;
2665                         }
2666                 }
2667
2668                 [Browsable (false)]
2669                 [EditorBrowsable (EditorBrowsableState.Advanced)]
2670 #if NET_2_0
2671                 public virtual
2672 #endif
2673                 Layout.LayoutEngine LayoutEngine {
2674                         get {
2675                                 if (layout_engine == null)
2676                                         layout_engine = new Layout.DefaultLayout ();
2677                                 return layout_engine;
2678                         }
2679                 }
2680
2681                 [EditorBrowsable(EditorBrowsableState.Always)]
2682                 [Browsable(false)]
2683                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2684                 public int Left {
2685                         get {
2686                                 return this.bounds.X;
2687                         }
2688
2689                         set {
2690                                 SetBounds(value, bounds.Y, bounds.Width, bounds.Height, BoundsSpecified.X);
2691                         }
2692                 }
2693
2694                 [Localizable(true)]
2695                 [MWFCategory("Layout")]
2696                 public Point Location {
2697                         get {
2698                                 return new Point(bounds.X, bounds.Y);
2699                         }
2700
2701                         set {
2702                                 SetBounds(value.X, value.Y, bounds.Width, bounds.Height, BoundsSpecified.Location);
2703                         }
2704                 }
2705
2706 #if NET_2_0
2707                 [Localizable (true)]
2708                 public Padding Margin {
2709                         get { return this.margin; }
2710                         set { 
2711                                 if (this.margin != value) {
2712                                         this.margin = value; 
2713                                         OnMarginChanged (EventArgs.Empty);
2714                                 }
2715                         }
2716                 }
2717 #endif
2718
2719                 [Browsable(false)]
2720                 public string Name {
2721                         get {
2722                                 return name;
2723                         }
2724
2725                         set {
2726                                 name = value;
2727                         }
2728                 }
2729
2730 #if NET_2_0
2731                 [Localizable(true)]
2732                 public Padding Padding {
2733                         get {
2734                                 return padding;
2735                         }
2736
2737                         set {
2738                                 if (padding != value) {
2739                                         padding = value;
2740                                         OnPaddingChanged (EventArgs.Empty);
2741                                         PerformLayout ();
2742                                 }
2743                         }
2744                 }
2745 #endif
2746
2747                 [Browsable(false)]
2748                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2749                 public Control Parent {
2750                         get {
2751                                 return this.parent;
2752                         }
2753
2754                         set {
2755                                 if (value == this) {
2756                                         throw new ArgumentException("A circular control reference has been made. A control cannot be owned or parented to itself.");
2757                                 }
2758
2759                                 if (parent!=value) {
2760                                         if (value==null) {
2761                                                 parent.Controls.Remove(this);
2762                                                 parent = null;
2763                                                 return;
2764                                         }
2765
2766                                         value.Controls.Add(this);
2767                                 }
2768                         }
2769                 }
2770
2771 #if NET_2_0
2772                 [Browsable (false)]
2773                 public Size PreferredSize {
2774                         get { return this.GetPreferredSize (Size.Empty); }
2775                 }
2776 #endif
2777
2778                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2779                 [Browsable(false)]
2780                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2781                 public string ProductName {
2782                         get {
2783                                 Type t = typeof (AssemblyProductAttribute);
2784                                 Assembly assembly = GetType().Module.Assembly;
2785                                 object [] attrs = assembly.GetCustomAttributes (t, false);
2786                                 AssemblyProductAttribute a = null;
2787                                 // On MS we get a NullRefException if product attribute is not
2788                                 // set. 
2789                                 if (attrs != null && attrs.Length > 0)
2790                                         a = (AssemblyProductAttribute) attrs [0];
2791                                 if (a == null) {
2792                                         return GetType ().Namespace;
2793                                 }
2794                                 return a.Product;
2795                         }
2796                 }
2797
2798                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2799                 [Browsable(false)]
2800                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2801                 public string ProductVersion {
2802                         get {
2803                                 Type t = typeof (AssemblyVersionAttribute);
2804                                 Assembly assembly = GetType().Module.Assembly;
2805                                 object [] attrs = assembly.GetCustomAttributes (t, false);
2806                                 if (attrs == null || attrs.Length < 1)
2807                                         return "1.0.0.0";
2808                                 return ((AssemblyVersionAttribute)attrs [0]).Version;
2809                         }
2810                 }
2811
2812                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2813                 [Browsable(false)]
2814                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2815                 public bool RecreatingHandle {
2816                         get {
2817                                 return is_recreating;
2818                         }
2819                 }
2820
2821                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2822                 [Browsable(false)]
2823                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2824                 public Region Region {
2825                         get {
2826                                 return clip_region;
2827                         }
2828
2829                         set {
2830                                 if (clip_region != value) {
2831                                         if (value != null && IsHandleCreated)
2832                                                 XplatUI.SetClipRegion(Handle, value);
2833
2834                                         clip_region = value;
2835 #if NET_2_0
2836                                         OnRegionChanged (EventArgs.Empty);
2837 #endif
2838                                 }
2839                         }
2840                 }
2841
2842                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2843                 [Browsable(false)]
2844                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2845                 public int Right {
2846                         get {
2847                                 return this.bounds.X+this.bounds.Width;
2848                         }
2849                 }
2850
2851                 [AmbientValue(RightToLeft.Inherit)]
2852                 [Localizable(true)]
2853                 [MWFCategory("Appearance")]
2854                 public virtual RightToLeft RightToLeft {
2855                         get {
2856                                 if (right_to_left == RightToLeft.Inherit) {
2857                                         if (parent != null)
2858                                                 return parent.RightToLeft;
2859                                         else
2860                                                 return RightToLeft.No; // default value
2861                                 }
2862                                 return right_to_left;
2863                         }
2864
2865                         set {
2866                                 if (value != right_to_left) {
2867                                         right_to_left = value;
2868                                         OnRightToLeftChanged(EventArgs.Empty);
2869                                         PerformLayout ();
2870                                 }
2871                         }
2872                 }
2873
2874                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2875                 public override ISite Site {
2876                         get {
2877                                 return base.Site;
2878                         }
2879
2880                         set {
2881                                 base.Site = value;
2882
2883                                 if (value != null) {
2884                                         AmbientProperties ap = (AmbientProperties) value.GetService (typeof (AmbientProperties));
2885                                         if (ap != null) {
2886                                                 BackColor = ap.BackColor;
2887                                                 ForeColor = ap.ForeColor;
2888                                                 Cursor = ap.Cursor;
2889                                                 Font = ap.Font;
2890                                         }
2891                                 }
2892                         }
2893                 }
2894
2895                 [Localizable(true)]
2896                 [MWFCategory("Layout")]
2897                 public Size Size {
2898                         get {
2899                                 return new Size(Width, Height);
2900                         }
2901
2902                         set {
2903                                 SetBounds(bounds.X, bounds.Y, value.Width, value.Height, BoundsSpecified.Size);
2904                         }
2905                 }
2906
2907                 [Localizable(true)]
2908                 [MergableProperty(false)]
2909                 [MWFCategory("Behavior")]
2910                 public int TabIndex {
2911                         get {
2912                                 if (tab_index != -1) {
2913                                         return tab_index;
2914                                 }
2915                                 return 0;
2916                         }
2917
2918                         set {
2919                                 if (tab_index != value) {
2920                                         tab_index = value;
2921                                         OnTabIndexChanged(EventArgs.Empty);
2922                                 }
2923                         }
2924                 }
2925
2926                 [DispId(-516)]
2927                 [DefaultValue(true)]
2928                 [MWFCategory("Behavior")]
2929                 public bool TabStop {
2930                         get {
2931                                 return tab_stop;
2932                         }
2933
2934                         set {
2935                                 if (tab_stop != value) {
2936                                         tab_stop = value;
2937                                         OnTabStopChanged(EventArgs.Empty);
2938                                 }
2939                         }
2940                 }
2941
2942                 [Localizable(false)]
2943                 [Bindable(true)]
2944                 [TypeConverter(typeof(StringConverter))]
2945                 [DefaultValue(null)]
2946                 [MWFCategory("Data")]
2947                 public object Tag {
2948                         get {
2949                                 return control_tag;
2950                         }
2951
2952                         set {
2953                                 control_tag = value;
2954                         }
2955                 }
2956
2957                 [DispId(-517)]
2958                 [Localizable(true)]
2959                 [BindableAttribute(true)]
2960                 [MWFCategory("Appearance")]
2961                 public virtual string Text {
2962                         get {
2963                                 // Our implementation ignores ControlStyles.CacheText - we always cache
2964                                 return this.text;
2965                         }
2966
2967                         set {
2968                                 if (value == null) {
2969                                         value = String.Empty;
2970                                 }
2971
2972                                 if (text!=value) {
2973                                         text=value;
2974                                         if (IsHandleCreated) {
2975                                                 /* we need to call .SetWindowStyle here instead of just .Text
2976                                                    because the presence/absence of Text (== "" or not) can cause
2977                                                    other window style things to appear/disappear */
2978                                                 XplatUI.SetWindowStyle(window.Handle, CreateParams);
2979                                                 XplatUI.Text(Handle, text);
2980                                         }
2981                                         OnTextChanged (EventArgs.Empty);
2982                                 }
2983                         }
2984                 }
2985
2986                 [EditorBrowsable(EditorBrowsableState.Always)]
2987                 [Browsable(false)]
2988                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2989                 public int Top {
2990                         get {
2991                                 return this.bounds.Y;
2992                         }
2993
2994                         set {
2995                                 SetBounds(bounds.X, value, bounds.Width, bounds.Height, BoundsSpecified.Y);
2996                         }
2997                 }
2998
2999                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3000                 [Browsable(false)]
3001                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
3002                 public Control TopLevelControl {
3003                         get {
3004                                 Control p = this;
3005
3006                                 while (p.parent != null) {
3007                                         p = p.parent;
3008                                 }
3009
3010                                 return p is Form ? p : null;
3011                         }
3012                 }
3013
3014                 [Localizable(true)]
3015                 [MWFCategory("Behavior")]
3016                 public bool Visible {
3017                         get {
3018                                 if (!is_visible) {
3019                                         return false;
3020                                 } else if (parent != null) {
3021                                         return parent.Visible;
3022                                 }
3023
3024                                 return true;
3025                         }
3026
3027                         set {
3028                                 SetVisibleCore(value);
3029                         }
3030                 }
3031
3032                 [EditorBrowsable(EditorBrowsableState.Always)]
3033                 [Browsable(false)]
3034                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
3035                 public int Width {
3036                         get {
3037                                 return this.bounds.Width;
3038                         }
3039
3040                         set {
3041                                 SetBounds(bounds.X, bounds.Y, value, bounds.Height, BoundsSpecified.Width);
3042                         }
3043                 }
3044
3045                 [EditorBrowsable(EditorBrowsableState.Never)]
3046                 [Browsable(false)]
3047                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
3048                 public IWindowTarget WindowTarget {
3049                         get { return window_target; }
3050                         set { window_target = value; }
3051                 }
3052                 #endregion      // Public Instance Properties
3053
3054                 #region Protected Instance Properties
3055                 protected virtual CreateParams CreateParams {
3056                         get {
3057                                 CreateParams create_params = new CreateParams();
3058
3059                                 try {
3060                                         create_params.Caption = Text;
3061                                 }
3062                                 catch {
3063                                         create_params.Caption = text;
3064                                 }
3065
3066                                 try {
3067                                         create_params.X = Left;
3068                                 }
3069                                 catch {
3070                                         create_params.X = this.bounds.X;
3071                                 }
3072
3073                                 try {
3074                                         create_params.Y = Top;
3075                                 }
3076                                 catch {
3077                                         create_params.Y = this.bounds.Y;
3078                                 }
3079
3080                                 try {
3081                                         create_params.Width = Width;
3082                                 }
3083                                 catch {
3084                                         create_params.Width = this.bounds.Width;
3085                                 }
3086
3087                                 try {
3088                                         create_params.Height = Height;
3089                                 }
3090                                 catch {
3091                                         create_params.Height = this.bounds.Height;
3092                                 }
3093
3094
3095                                 create_params.ClassName = XplatUI.DefaultClassName;
3096                                 create_params.ClassStyle = (int)(XplatUIWin32.ClassStyle.CS_OWNDC | XplatUIWin32.ClassStyle.CS_DBLCLKS);
3097                                 create_params.ExStyle = 0;
3098                                 create_params.Param = 0;
3099
3100                                 if (allow_drop) {
3101                                         create_params.ExStyle |= (int)WindowExStyles.WS_EX_ACCEPTFILES;
3102                                 }
3103
3104                                 if ((parent!=null) && (parent.IsHandleCreated)) {
3105                                         create_params.Parent = parent.Handle;
3106                                 }
3107
3108                                 create_params.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
3109
3110                                 if (is_visible) {
3111                                         create_params.Style |= (int)WindowStyles.WS_VISIBLE;
3112                                 }
3113
3114                                 if (!is_enabled) {
3115                                         create_params.Style |= (int)WindowStyles.WS_DISABLED;
3116                                 }
3117
3118                                 switch (border_style) {
3119                                         case BorderStyle.FixedSingle:
3120                                                 create_params.Style |= (int) WindowStyles.WS_BORDER;
3121                                                 break;
3122                                         case BorderStyle.Fixed3D:
3123                                                 create_params.ExStyle |= (int) WindowExStyles.WS_EX_CLIENTEDGE;
3124                                                 break;
3125                                 }
3126
3127                                 return create_params;
3128                         }
3129                 }
3130
3131 #if NET_2_0
3132                 protected virtual Cursor DefaultCursor { get { return Cursors.Default; } }
3133 #endif
3134
3135                 protected virtual ImeMode DefaultImeMode {
3136                         get {
3137                                 return ImeMode.Inherit;
3138                         }
3139                 }
3140
3141 #if NET_2_0
3142                 protected virtual Padding DefaultMargin {
3143                         get { return new Padding (3); }
3144                 }
3145                 
3146                 protected virtual Size DefaultMaximumSize { get { return new Size (); } }
3147                 protected virtual Size DefaultMinimumSize { get { return new Size (); } }
3148                 protected virtual Padding DefaultPadding { get { return new Padding (); } }
3149 #endif
3150
3151                 protected virtual Size DefaultSize {
3152                         get {
3153                                 return new Size(0, 0);
3154                         }
3155                 }
3156
3157                 protected int FontHeight {
3158                         get {
3159                                 return Font.Height;
3160                         }
3161
3162                         set {
3163                                 ;; // Nothing to do
3164                         }
3165                 }
3166 #if NET_2_0
3167                 [Obsolete ()]
3168 #endif
3169                 protected bool RenderRightToLeft {
3170                         get {
3171                                 return (this.right_to_left == RightToLeft.Yes);
3172                         }
3173                 }
3174
3175                 protected bool ResizeRedraw {
3176                         get {
3177                                 return GetStyle(ControlStyles.ResizeRedraw);
3178                         }
3179
3180                         set {
3181                                 SetStyle(ControlStyles.ResizeRedraw, value);
3182                         }
3183                 }
3184
3185                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3186                 [Browsable(false)]
3187                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
3188                 protected virtual bool ShowFocusCues {
3189                         get {
3190                                 return true;
3191                         }
3192                 }
3193
3194                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3195                 [Browsable(false)]
3196                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
3197 #if NET_2_0
3198                 internal virtual
3199 #endif
3200                 protected bool ShowKeyboardCues {
3201                         get {
3202                                 return true;
3203                         }
3204                 }
3205                 #endregion      // Protected Instance Properties
3206
3207                 #region Public Static Methods
3208                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3209                 public static Control FromChildHandle(IntPtr handle) {
3210                         return Control.ControlNativeWindow.ControlFromChildHandle (handle);
3211                 }
3212
3213                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3214                 public static Control FromHandle(IntPtr handle) {
3215                         return Control.ControlNativeWindow.ControlFromHandle(handle);
3216                 }
3217
3218                 public static bool IsMnemonic(char charCode, string text) {
3219                         int amp;
3220
3221                         amp = text.IndexOf('&');
3222
3223                         if (amp != -1) {
3224                                 if (amp + 1 < text.Length) {
3225                                         if (text[amp + 1] != '&') {
3226                                                 if (Char.ToUpper(charCode) == Char.ToUpper(text.ToCharArray(amp + 1, 1)[0])) {
3227                                                         return true;
3228                                                 }       
3229                                         }
3230                                 }
3231                         }
3232                         return false;
3233                 }
3234                 #endregion
3235
3236                 #region Protected Static Methods
3237                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3238                 protected static bool ReflectMessage(IntPtr hWnd, ref Message m) {
3239                         Control c;
3240
3241                         c = Control.FromHandle(hWnd);
3242
3243                         if (c != null) {
3244                                 c.WndProc(ref m);
3245                                 return true;
3246                         }
3247                         return false;
3248                 }
3249                 #endregion
3250
3251                 #region Public Instance Methods
3252                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3253                 public IAsyncResult BeginInvoke(Delegate method) {
3254                         object [] prms = null;
3255                         if (method is EventHandler)
3256                                 prms = new object [] { this, EventArgs.Empty };
3257                         return BeginInvokeInternal(method, prms);
3258                 }
3259
3260                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3261 #if NET_2_0
3262                 public IAsyncResult BeginInvoke (Delegate method, params object[] args)
3263 #else
3264                 public IAsyncResult BeginInvoke (Delegate method, object[] args)
3265 #endif
3266                 {
3267                         return BeginInvokeInternal (method, args);
3268                 }
3269
3270                 public void BringToFront() {
3271                         if (parent != null) {
3272                                 parent.child_controls.SetChildIndex(this, 0);
3273                         }
3274                         else if (IsHandleCreated) {
3275                                 XplatUI.SetZOrder(Handle, IntPtr.Zero, false, false);
3276                         }
3277                 }
3278
3279                 public bool Contains(Control ctl) {
3280                         while (ctl != null) {
3281                                 ctl = ctl.parent;
3282                                 if (ctl == this) {
3283                                         return true;
3284                                 }
3285                         }
3286                         return false;
3287                 }
3288
3289                 public void CreateControl () {
3290                         if (is_disposed) {
3291                                 throw new ObjectDisposedException(GetType().FullName.ToString());
3292                         }
3293                         if (is_created) {
3294                                 return;
3295                         }
3296
3297                         if (!is_visible) {
3298                                 return;
3299                         }
3300                         
3301                         if (!IsHandleCreated) {
3302                                 CreateHandle();
3303                         }
3304
3305                         if (!is_created) {
3306                                 is_created = true;
3307
3308                                 if (binding_context == null) {  // seem to be sent whenever it's null?
3309                                         OnBindingContextChanged(EventArgs.Empty);
3310                                 }
3311
3312                                 OnCreateControl();
3313                         }
3314                 }
3315
3316                 public Graphics CreateGraphics() {
3317                         if (!IsHandleCreated) {
3318                                 this.CreateHandle();
3319                         }
3320                         return Graphics.FromHwnd(this.window.Handle);
3321                 }
3322
3323                 public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects) {
3324                         if (IsHandleCreated)
3325                                 return XplatUI.StartDrag(Handle, data, allowedEffects);
3326                         else
3327                                 return DragDropEffects.None;
3328                 }
3329
3330                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3331                 public object EndInvoke (IAsyncResult async_result) {
3332                         AsyncMethodResult result = (AsyncMethodResult) async_result;
3333                         return result.EndInvoke ();
3334                 }
3335
3336                 public Form FindForm() {
3337                         Control c;
3338
3339                         c = this;
3340                         while (c != null) {
3341                                 if (c is Form) {
3342                                         return (Form)c;
3343                                 }
3344                                 c = c.Parent;
3345                         }
3346                         return null;
3347                 }
3348 #if NET_2_0
3349                 [EditorBrowsable (EditorBrowsableState.Advanced)]
3350 #endif
3351                 public bool Focus() {
3352                         return FocusInternal (false);
3353                 }
3354
3355                 internal virtual bool FocusInternal (bool skip_check) {
3356                         if (skip_check || (CanFocus && IsHandleCreated && !has_focus && !is_focusing)) {
3357                                 is_focusing = true;
3358                                 Select(this);
3359                                 is_focusing = false;
3360                         }
3361                         return has_focus;
3362                 }
3363
3364                 public Control GetChildAtPoint(Point pt) {
3365                         // MS's version causes the handle to be created.  The stack trace shows that get_Handle is called here, but
3366                         // we'll just call CreateHandle instead.
3367                         CreateHandle ();
3368                         
3369                         // Microsoft's version of this function doesn't seem to work, so I can't check
3370                         // if we only consider children or also grandchildren, etc.
3371                         // I'm gonna say 'children only'
3372                         for (int i=0; i<child_controls.Count; i++) {
3373                                 if (child_controls[i].Bounds.Contains(pt)) {
3374                                         return child_controls[i];
3375                                 }
3376                         }
3377                         return null;
3378                 }
3379
3380                 public IContainerControl GetContainerControl() {
3381                         Control current = this;
3382
3383                         while (current!=null) {
3384                                 if ((current is IContainerControl) && ((current.control_style & ControlStyles.ContainerControl)!=0)) {
3385                                         return (IContainerControl)current;
3386                                 }
3387                                 current = current.parent;
3388                         }
3389                         return null;
3390                 }
3391
3392                 public Control GetNextControl(Control ctl, bool forward) {
3393
3394                         if (!this.Contains(ctl)) {
3395                                 ctl = this;
3396                         }
3397
3398                         if (forward) {
3399                                 ctl = FindControlForward(this, ctl);
3400                         }
3401                         else {
3402                                 ctl = FindControlBackward(this, ctl);
3403                         }
3404
3405                         if (ctl != this) {
3406                                 return ctl;
3407                         }
3408                         return null;
3409                 }
3410
3411 #if NET_2_0
3412                 [EditorBrowsable (EditorBrowsableState.Advanced)]
3413                 public virtual Size GetPreferredSize (Size proposedSize) {
3414                         Size retsize = this.explicit_bounds.Size;
3415                         
3416                         // If we're bigger than the MaximumSize, fix that
3417                         if (this.maximum_size.Width != 0 && retsize.Width > this.maximum_size.Width)
3418                                 retsize.Width = this.maximum_size.Width;
3419                         if (this.maximum_size.Height != 0 && retsize.Height > this.maximum_size.Height)
3420                                 retsize.Height = this.maximum_size.Height;
3421                                 
3422                         // If we're smaller than the MinimumSize, fix that
3423                         if (this.minimum_size.Width != 0 && retsize.Width < this.minimum_size.Width)
3424                                 retsize.Width = this.minimum_size.Width;
3425                         if (this.minimum_size.Height != 0 && retsize.Height < this.minimum_size.Height)
3426                                 retsize.Height = this.minimum_size.Height;
3427                                 
3428                         return retsize;
3429                 }
3430 #endif
3431
3432                 public void Hide() {
3433                         this.Visible = false;
3434                 }
3435
3436                 public void Invalidate() {
3437                         Invalidate(ClientRectangle, false);
3438                 }
3439
3440                 public void Invalidate(bool invalidateChildren) {
3441                         Invalidate(ClientRectangle, invalidateChildren);
3442                 }
3443
3444                 public void Invalidate(System.Drawing.Rectangle rc) {
3445                         Invalidate(rc, false);
3446                 }
3447
3448                 public void Invalidate(System.Drawing.Rectangle rc, bool invalidateChildren) {
3449                         // Win32 invalidates control including when Width and Height is equal 0
3450                         // or is not visible, only Paint event must be care about this.
3451                         if (!IsHandleCreated)
3452                                 return;
3453
3454                         if  (rc.Width > 0 && rc.Height > 0) {
3455
3456                                 NotifyInvalidate(rc);
3457
3458                                 XplatUI.Invalidate(Handle, rc, false);
3459
3460                                 if (invalidateChildren) {
3461                                         Control [] controls = child_controls.GetAllControls ();
3462                                         for (int i=0; i<controls.Length; i++)
3463                                                 controls [i].Invalidate ();
3464                                 }
3465                         }
3466                         OnInvalidated(new InvalidateEventArgs(rc));
3467                 }
3468
3469                 public void Invalidate(System.Drawing.Region region) {
3470                         Invalidate(region, false);
3471                 }
3472
3473                 public void Invalidate(System.Drawing.Region region, bool invalidateChildren) {
3474                         RectangleF bounds = region.GetBounds (CreateGraphics ());
3475                         Invalidate (new Rectangle ((int) bounds.X, (int) bounds.Y, (int) bounds.Width, (int) bounds.Height),
3476                                         invalidateChildren);
3477                 }
3478
3479                 public object Invoke (Delegate method) {
3480                         object [] prms = null;
3481                         if (method is EventHandler)
3482                                 prms = new object [] { this, EventArgs.Empty };
3483
3484                         return Invoke(method, prms);
3485                 }
3486 #if NET_2_0
3487                 public object Invoke (Delegate method, params object [] args) {
3488 #else
3489                 public object Invoke (Delegate method, object[] args) {
3490 #endif
3491                         Control control = FindControlToInvokeOn ();
3492                         
3493                         if (!this.InvokeRequired) {
3494                                 return method.DynamicInvoke(args);
3495                         }
3496
3497                         IAsyncResult result = BeginInvokeInternal (method, args, control);
3498                         return EndInvoke(result);
3499                 }
3500
3501                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3502                 public void PerformLayout() {
3503                         PerformLayout(null, null);
3504                 }
3505
3506                 internal void SetImplicitBounds (int x, int y, int width, int height) {
3507                         Rectangle saved_bounds = explicit_bounds;
3508                         SetBounds (x, y, width, height);
3509                         explicit_bounds = saved_bounds;
3510                 }
3511
3512                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3513                 public void PerformLayout(Control affectedControl, string affectedProperty) {
3514                         LayoutEventArgs levent = new LayoutEventArgs(affectedControl, affectedProperty);
3515
3516                         if (layout_suspended > 0) {
3517                                 layout_pending = true;
3518                                 return;
3519                         }
3520
3521                         layout_pending = false;
3522
3523                         // Prevent us from getting messed up
3524                         layout_suspended++;
3525
3526                         // Perform all Dock and Anchor calculations
3527                         try {
3528                                 OnLayout(levent);
3529                         }
3530
3531                                 // Need to make sure we decremend layout_suspended
3532                         finally {
3533                                 layout_suspended--;
3534                         }
3535                 }
3536
3537                 public Point PointToClient (Point p) {
3538                         int x = p.X;
3539                         int y = p.Y;
3540
3541                         XplatUI.ScreenToClient (Handle, ref x, ref y);
3542
3543                         return new Point (x, y);
3544                 }
3545
3546                 public Point PointToScreen(Point p) {
3547                         int x = p.X;
3548                         int y = p.Y;
3549
3550                         XplatUI.ClientToScreen(Handle, ref x, ref y);
3551
3552                         return new Point(x, y);
3553                 }
3554
3555                 public virtual bool PreProcessMessage(ref Message msg) {
3556                         return InternalPreProcessMessage (ref msg);
3557                 }
3558
3559                 internal virtual bool InternalPreProcessMessage (ref Message msg) {
3560                         Keys key_data;
3561
3562                         if ((msg.Msg == (int)Msg.WM_KEYDOWN) || (msg.Msg == (int)Msg.WM_SYSKEYDOWN)) {
3563                                 key_data = (Keys)msg.WParam.ToInt32() | XplatUI.State.ModifierKeys;
3564
3565                                 if (!ProcessCmdKey(ref msg, key_data)) {
3566                                         if (IsInputKey(key_data)) {
3567                                                 return false;
3568                                         }
3569
3570                                         return ProcessDialogKey(key_data);
3571                                 }
3572
3573                                 return true;
3574                         } else if (msg.Msg == (int)Msg.WM_CHAR) {
3575                                 if (IsInputChar((char)msg.WParam)) {
3576                                         return false;
3577                                 }
3578                                 return ProcessDialogChar((char)msg.WParam);
3579                         } else if (msg.Msg == (int)Msg.WM_SYSCHAR) {
3580                                 return ProcessDialogChar((char)msg.WParam);
3581                         }
3582                         return false;
3583                 }
3584
3585                 public Rectangle RectangleToClient(Rectangle r) {
3586                         return new Rectangle(PointToClient(r.Location), r.Size);
3587                 }
3588
3589                 public Rectangle RectangleToScreen(Rectangle r) {
3590                         return new Rectangle(PointToScreen(r.Location), r.Size);
3591                 }
3592
3593                 public virtual void Refresh() {
3594                         if (IsHandleCreated && Visible) {
3595                                 Invalidate();
3596                                 XplatUI.UpdateWindow(window.Handle);
3597
3598                                 Control [] controls = child_controls.GetAllControls ();
3599                                 for (int i=0; i < controls.Length; i++) {
3600                                         controls[i].Refresh();
3601                                 }
3602                                 
3603                         }
3604                 }
3605
3606                 [EditorBrowsable(EditorBrowsableState.Never)]
3607                 public virtual void ResetBackColor() {
3608                         BackColor = Color.Empty;
3609                 }
3610
3611                 [EditorBrowsable(EditorBrowsableState.Never)]
3612                 public void ResetBindings() {
3613                         if (data_bindings != null)
3614                                 data_bindings.Clear();
3615                 }
3616
3617                 [EditorBrowsable(EditorBrowsableState.Never)]
3618                 public virtual void ResetCursor() {
3619                         Cursor = null;
3620                 }
3621
3622                 [EditorBrowsable(EditorBrowsableState.Never)]
3623                 public virtual void ResetFont() {
3624                         font = null;
3625                 }
3626
3627                 [EditorBrowsable(EditorBrowsableState.Never)]
3628                 public virtual void ResetForeColor() {
3629                         foreground_color = Color.Empty;
3630                 }
3631
3632                 [EditorBrowsable(EditorBrowsableState.Never)]
3633                 public void ResetImeMode() {
3634                         ime_mode = DefaultImeMode;
3635                 }
3636
3637                 [EditorBrowsable(EditorBrowsableState.Never)]
3638                 public virtual void ResetRightToLeft() {
3639                         right_to_left = RightToLeft.Inherit;
3640                 }
3641
3642                 public virtual void ResetText() {
3643                         text = String.Empty;
3644                 }
3645
3646                 public void ResumeLayout() {
3647                         ResumeLayout (true);
3648                 }
3649
3650                 public void ResumeLayout(bool performLayout) {
3651                         if (layout_suspended > 0) {
3652                                 layout_suspended--;
3653                         }
3654
3655                         if (layout_suspended == 0) {
3656                                 if (performLayout && layout_pending) {
3657                                         PerformLayout();
3658                                 }
3659                         }
3660                 }
3661 #if NET_2_0
3662                 [EditorBrowsable (EditorBrowsableState.Never)]
3663                 [Obsolete ()]
3664 #endif
3665                 public void Scale(float ratio) {
3666                         ScaleCore(ratio, ratio);
3667                 }
3668                 
3669 #if NET_2_0
3670                 [EditorBrowsable (EditorBrowsableState.Never)]
3671                 [Obsolete ()]
3672 #endif
3673                 public void Scale(float dx, float dy) {
3674                         ScaleCore(dx, dy);
3675                 }
3676
3677 #if NET_2_0
3678                 [EditorBrowsable (EditorBrowsableState.Advanced)]
3679                 public void Scale(SizeF factor) {
3680                         ScaleCore(factor.Width, factor.Height);
3681                 }
3682 #endif
3683
3684                 public void Select() {
3685                         Select(false, false);   
3686                 }
3687
3688 #if DebugFocus
3689                 private void printTree(Control c, string t) {
3690                         foreach(Control i in c.child_controls) {
3691                                 Console.WriteLine ("{2}{0}.TabIndex={1}", i, i.tab_index, t);
3692                                 printTree (i, t+"\t");
3693                         }
3694                 }
3695 #endif
3696                 public bool SelectNextControl(Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) {
3697                         Control c;
3698
3699 #if DebugFocus
3700                         Console.WriteLine("{0}", this.FindForm());
3701                         printTree(this, "\t");
3702 #endif
3703
3704                         if (!this.Contains(ctl) || (!nested && (ctl.parent != this))) {
3705                                 ctl = null;
3706                         }
3707                         c = ctl;
3708                         do {
3709                                 c = GetNextControl(c, forward);
3710                                 if (c == null) {
3711                                         if (wrap) {
3712                                                 wrap = false;
3713                                                 continue;
3714                                         }
3715                                         break;
3716                                 }
3717
3718                                 if (c.CanSelect && ((c.parent == this) || nested) && (c.tab_stop || !tabStopOnly)) {
3719                                         c.Select (true, true);
3720                                         return true;
3721                                 }
3722                         } while (c != ctl); // If we wrap back to ourselves we stop
3723
3724                         return false;
3725                 }
3726
3727                 public void SendToBack() {
3728                         if (parent != null) {
3729                                 parent.child_controls.SetChildIndex(this, parent.child_controls.Count);
3730                         }
3731                 }
3732
3733                 public void SetBounds(int x, int y, int width, int height) {
3734                         SetBounds(x, y, width, height, BoundsSpecified.All);
3735                 }
3736
3737                 public void SetBounds(int x, int y, int width, int height, BoundsSpecified specified) {
3738                         if ((specified & BoundsSpecified.X) != BoundsSpecified.X) {
3739                                 x = Left;
3740                         }
3741
3742                         if ((specified & BoundsSpecified.Y) != BoundsSpecified.Y) {
3743                                 y = Top;
3744                         }
3745
3746                         if ((specified & BoundsSpecified.Width) != BoundsSpecified.Width) {
3747                                 width = Width;
3748                         }
3749
3750                         if ((specified & BoundsSpecified.Height) != BoundsSpecified.Height) {
3751                                 height = Height;
3752                         }
3753
3754                         SetBoundsCore(x, y, width, height, specified);
3755                         if (parent != null)
3756                                 parent.PerformLayout(this, "Bounds");
3757                 }
3758
3759                 public void Show () {
3760                         this.Visible = true;
3761                 }
3762
3763                 public void SuspendLayout() {
3764                         layout_suspended++;
3765                 }
3766
3767                 public void Update() {
3768                         if (IsHandleCreated) {
3769                                 XplatUI.UpdateWindow(window.Handle);
3770                         }
3771                 }
3772                 #endregion      // Public Instance Methods
3773
3774                 #region Protected Instance Methods
3775                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3776                 protected void AccessibilityNotifyClients(AccessibleEvents accEvent, int childID) {
3777                         // turns out this method causes handle
3778                         // creation in 1.1.  at first I thought this
3779                         // would be accomplished just by using
3780                         // get_AccessibilityObject, which would route
3781                         // through CreateAccessibilityInstance, which
3782                         // calls CreateControl.  This isn't the case,
3783                         // though (as overriding
3784                         // CreateAccessibilityInstance and adding a
3785                         // CWL shows nothing.  So we fudge it and put
3786                         // a CreateHandle here.
3787
3788 #if ONLY_1_1
3789                         CreateHandle ();
3790 #endif
3791
3792                         if (accessibility_object != null && accessibility_object is ControlAccessibleObject)
3793                                 ((ControlAccessibleObject)accessibility_object).NotifyClients (accEvent, childID);
3794                 }
3795
3796                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3797                 protected virtual AccessibleObject CreateAccessibilityInstance() {
3798                         CreateControl ();
3799                         return new Control.ControlAccessibleObject(this);
3800                 }
3801
3802                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3803                 protected virtual ControlCollection CreateControlsInstance() {
3804                         return new ControlCollection(this);
3805                 }
3806
3807                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3808                 protected virtual void CreateHandle() {
3809                         if (IsDisposed) {
3810                                 throw new ObjectDisposedException(GetType().FullName.ToString());
3811                         }
3812
3813                         if (IsHandleCreated && !is_recreating) {
3814                                 return;
3815                         }
3816
3817                         CreateParams create_params = CreateParams;
3818                         window.CreateHandle(create_params);
3819
3820                         if (window.Handle != IntPtr.Zero) {
3821                                 creator_thread = Thread.CurrentThread;
3822
3823                                 XplatUI.EnableWindow(window.Handle, is_enabled);
3824
3825                                 if (clip_region != null) {
3826                                         XplatUI.SetClipRegion(window.Handle, clip_region);
3827                                 }
3828
3829                                 // Set our handle with our parent
3830                                 if ((parent != null) && (parent.IsHandleCreated)) {
3831                                         XplatUI.SetParent(window.Handle, parent.Handle);
3832                                 }
3833
3834                                 UpdateStyles();
3835                                 XplatUI.SetAllowDrop (window.Handle, allow_drop);
3836
3837                                 // Find out where the window manager placed us
3838                                 if ((CreateParams.Style & (int)WindowStyles.WS_CHILD) != 0) {
3839                                         XplatUI.SetBorderStyle(window.Handle, (FormBorderStyle)border_style);
3840                                 }
3841                                 UpdateBounds();
3842                         }
3843                 }
3844
3845                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3846                 protected virtual void DefWndProc(ref Message m) {
3847                         window.DefWndProc(ref m);
3848                 }
3849
3850                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3851                 protected virtual void DestroyHandle() {
3852                         if (IsHandleCreated) {
3853                                 if (window != null) {
3854                                         window.DestroyHandle();
3855                                 }
3856                         }
3857                 }
3858
3859 #if NET_2_0
3860                 protected virtual AccessibleObject GetAccessibilityObjectById (int objectId)
3861                 {
3862                         // XXX need to implement this.
3863                         return null;
3864                 }
3865 #endif
3866
3867                 protected internal bool GetStyle(ControlStyles flag) {
3868                         return (control_style & flag) != 0;
3869                 }
3870
3871                 protected bool GetTopLevel() {
3872                         return is_toplevel;
3873                 }
3874
3875                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3876                 protected virtual void InitLayout() {
3877                         UpdateDistances();
3878                 }
3879
3880                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3881                 protected void InvokeGotFocus(Control toInvoke, EventArgs e) {
3882                         toInvoke.OnGotFocus(e);
3883                 }
3884
3885                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3886                 protected void InvokeLostFocus(Control toInvoke, EventArgs e) {
3887                         toInvoke.OnLostFocus(e);
3888                 }
3889
3890                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3891                 protected void InvokeOnClick(Control toInvoke, EventArgs e) {
3892                         toInvoke.OnClick(e);
3893                 }
3894
3895                 protected void InvokePaint(Control toInvoke, PaintEventArgs e) {
3896                         toInvoke.OnPaint(e);
3897                 }
3898
3899                 protected void InvokePaintBackground(Control toInvoke, PaintEventArgs e) {
3900                         toInvoke.OnPaintBackground(e);
3901                 }
3902
3903                 protected virtual bool IsInputChar (char charCode) {
3904                         // XXX on MS.NET this method causes the handle to be created..
3905                         CreateHandle ();
3906
3907                         return true;
3908                 }
3909
3910                 protected virtual bool IsInputKey (Keys keyData) {
3911                         // Doc says this one calls IsInputChar; not sure what to do with that
3912                         return false;
3913                 }
3914
3915                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3916                 protected virtual void NotifyInvalidate(Rectangle invalidatedArea) {
3917                         // override me?
3918                 }
3919
3920                 protected virtual bool ProcessCmdKey(ref Message msg, Keys keyData) {
3921                         if ((context_menu != null) && context_menu.ProcessCmdKey(ref msg, keyData)) {
3922                                 return true;
3923                         }
3924
3925                         if (parent != null) {
3926                                 return parent.ProcessCmdKey(ref msg, keyData);
3927                         }
3928
3929                         return false;
3930                 }
3931
3932                 protected virtual bool ProcessDialogChar(char charCode) {
3933                         if (parent != null) {
3934                                 return parent.ProcessDialogChar (charCode);
3935                         }
3936
3937                         return false;
3938                 }
3939
3940                 protected virtual bool ProcessDialogKey (Keys keyData) {
3941                         if (parent != null) {
3942                                 return parent.ProcessDialogKey (keyData);
3943                         }
3944
3945                         return false;
3946                 }
3947
3948                 protected virtual bool ProcessKeyEventArgs (ref Message msg) {
3949                         KeyEventArgs            key_event;
3950
3951                         switch (msg.Msg) {
3952                                 case (int)Msg.WM_SYSKEYDOWN:
3953                                 case (int)Msg.WM_KEYDOWN: {
3954                                         key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
3955                                         OnKeyDown (key_event);
3956                                         return key_event.Handled;
3957                                 }
3958
3959                                 case (int)Msg.WM_SYSKEYUP:
3960                                 case (int)Msg.WM_KEYUP: {
3961                                         key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
3962                                         OnKeyUp (key_event);
3963                                         return key_event.Handled;
3964                                 }
3965
3966                                 case (int)Msg.WM_SYSCHAR:
3967                                 case (int)Msg.WM_CHAR: {
3968                                         KeyPressEventArgs       key_press_event;
3969
3970                                         key_press_event = new KeyPressEventArgs((char)msg.WParam);
3971                                         OnKeyPress(key_press_event);
3972 #if NET_2_0
3973                                         msg.WParam = (IntPtr)key_press_event.KeyChar;
3974 #endif
3975                                         return key_press_event.Handled;
3976                                 }
3977
3978                                 default: {
3979                                         break;
3980                                 }
3981                         }
3982
3983                         return false;
3984                 }
3985
3986                 protected internal virtual bool ProcessKeyMessage(ref Message msg) {
3987                         if (parent != null) {
3988                                 if (parent.ProcessKeyPreview(ref msg)) {
3989                                         return true;
3990                                 }
3991                         }
3992
3993                         return ProcessKeyEventArgs(ref msg);
3994                 }
3995
3996                 protected virtual bool ProcessKeyPreview(ref Message msg) {
3997                         if (parent != null) {
3998                                 return parent.ProcessKeyPreview(ref msg);
3999                         }
4000
4001                         return false;
4002                 }
4003
4004                 protected virtual bool ProcessMnemonic(char charCode) {
4005                         // override me
4006                         return false;
4007                 }
4008
4009                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4010                 protected void RaiseDragEvent(object key, DragEventArgs e) {
4011                         // MS Internal
4012                 }
4013
4014                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4015                 protected void RaiseKeyEvent(object key, KeyEventArgs e) {
4016                         // MS Internal
4017                 }
4018
4019                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4020                 protected void RaiseMouseEvent(object key, MouseEventArgs e) {
4021                         // MS Internal
4022                 }
4023
4024                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4025                 protected void RaisePaintEvent(object key, PaintEventArgs e) {
4026                         // MS Internal
4027                 }
4028
4029                 private void SetIsRecreating () {
4030                         is_recreating=true;
4031
4032                         foreach (Control c in Controls.GetAllControls()) {
4033                                 c.SetIsRecreating ();
4034                         }
4035                 }
4036
4037                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4038                 protected void RecreateHandle() {
4039                         if (!IsHandleCreated)
4040                                 return;
4041
4042 #if DebugRecreate
4043                         Console.WriteLine("Recreating control {0}", XplatUI.Window(window.Handle));
4044 #endif
4045
4046                         SetIsRecreating ();
4047
4048                         if (IsHandleCreated) {
4049 #if DebugRecreate
4050                                 Console.WriteLine(" + handle is created, destroying it.");
4051 #endif
4052                                 DestroyHandle();
4053                                 // WM_DESTROY will CreateHandle for us
4054                         } else {
4055 #if DebugRecreate
4056                                 Console.WriteLine(" + handle is not created, creating it.");
4057 #endif
4058                                 if (!is_created) {
4059                                         CreateControl();
4060                                 } else {
4061                                         CreateHandle();
4062                                 }
4063
4064                                 is_recreating = false;
4065 #if DebugRecreate
4066                                 Console.WriteLine (" + new handle = {0:X}", Handle.ToInt32());
4067 #endif
4068                         }
4069
4070                 }
4071
4072                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4073                 protected void ResetMouseEventArgs() {
4074                         // MS Internal
4075                 }
4076
4077                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4078                 protected ContentAlignment RtlTranslateAlignment(ContentAlignment align) {
4079                         if (right_to_left == RightToLeft.No) {
4080                                 return align;
4081                         }
4082
4083                         switch (align) {
4084                                 case ContentAlignment.TopLeft: {
4085                                         return ContentAlignment.TopRight;
4086                                 }
4087
4088                                 case ContentAlignment.TopRight: {
4089                                         return ContentAlignment.TopLeft;
4090                                 }
4091
4092                                 case ContentAlignment.MiddleLeft: {
4093                                         return ContentAlignment.MiddleRight;
4094                                 }
4095
4096                                 case ContentAlignment.MiddleRight: {
4097                                         return ContentAlignment.MiddleLeft;
4098                                 }
4099
4100                                 case ContentAlignment.BottomLeft: {
4101                                         return ContentAlignment.BottomRight;
4102                                 }
4103
4104                                 case ContentAlignment.BottomRight: {
4105                                         return ContentAlignment.BottomLeft;
4106                                 }
4107
4108                                 default: {
4109                                         // if it's center it doesn't change
4110                                         return align;
4111                                 }
4112                         }
4113                 }
4114
4115                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4116                 protected HorizontalAlignment RtlTranslateAlignment(HorizontalAlignment align) {
4117                         if ((right_to_left == RightToLeft.No) || (align == HorizontalAlignment.Center)) {
4118                                 return align;
4119                         }
4120
4121                         if (align == HorizontalAlignment.Left) {
4122                                 return HorizontalAlignment.Right;
4123                         }
4124
4125                         // align must be HorizontalAlignment.Right
4126                         return HorizontalAlignment.Left;
4127                 }
4128
4129                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4130                 protected LeftRightAlignment RtlTranslateAlignment(LeftRightAlignment align) {
4131                         if (right_to_left == RightToLeft.No) {
4132                                 return align;
4133                         }
4134
4135                         if (align == LeftRightAlignment.Left) {
4136                                 return LeftRightAlignment.Right;
4137                         }
4138
4139                         // align must be LeftRightAlignment.Right;
4140                         return LeftRightAlignment.Left;
4141                 }
4142
4143                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4144                 protected ContentAlignment RtlTranslateContent(ContentAlignment align) {
4145                         return RtlTranslateAlignment(align);
4146                 }
4147
4148                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4149                 protected HorizontalAlignment RtlTranslateHorizontal(HorizontalAlignment align) {
4150                         return RtlTranslateAlignment(align);
4151                 }
4152
4153                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4154                 protected LeftRightAlignment RtlTranslateLeftRight(LeftRightAlignment align) {
4155                         return RtlTranslateAlignment(align);
4156                 }
4157
4158 #if NET_2_0
4159                 [EditorBrowsable (EditorBrowsableState.Never)]
4160 #else
4161                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4162 #endif
4163                 protected virtual void ScaleCore(float dx, float dy) {
4164                         Point   location;
4165                         Size    size;
4166
4167                         SuspendLayout();
4168
4169                         location = new Point((int)(Left * dx), (int)(Top * dy));
4170                         size = this.ClientSize;
4171
4172                         if (!GetStyle(ControlStyles.FixedWidth)) {
4173                                 size.Width = (int)(size.Width * dx);
4174                         }
4175
4176                         if (!GetStyle(ControlStyles.FixedHeight)) {
4177                                 size.Height = (int)(size.Height * dy);
4178                         }
4179
4180                         SetBounds(location.X, location.Y, size.Width, size.Height, BoundsSpecified.All);
4181
4182                         /* Now scale our children */
4183                         Control [] controls = child_controls.GetAllControls ();
4184                         for (int i=0; i < controls.Length; i++) {
4185                                 controls[i].Scale(dx, dy);
4186                         }
4187
4188                         ResumeLayout();
4189                 }
4190
4191                 protected virtual void Select(bool directed, bool forward) {
4192                         IContainerControl       container;
4193                         
4194                         container = GetContainerControl();
4195                         if (container != null && (Control)container != this)
4196                                 container.ActiveControl = this;
4197                 }
4198
4199                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4200                 protected virtual void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
4201                         // SetBoundsCore updates the Win32 control itself. UpdateBounds updates the controls variables and fires events, I'm guessing - pdb
4202                         if (IsHandleCreated) {
4203                                 XplatUI.SetWindowPos(Handle, x, y, width, height);
4204
4205                                 // Win32 automatically changes negative width/height to 0.
4206                                 // The control has already been sent a WM_WINDOWPOSCHANGED message and it has the correct
4207                                 // data, but it'll be overwritten when we call UpdateBounds unless we get the updated
4208                                 // size.
4209                                 if (width < 0 || height < 0) {
4210                                         int cw, ch, ix, iy;
4211                                         XplatUI.GetWindowPos(Handle, this is Form, out ix, out iy, out width, out height, out cw, out ch);
4212                                 }
4213                         }
4214
4215                         UpdateBounds(x, y, width, height);
4216
4217                         UpdateDistances();
4218                 }
4219
4220                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4221                 protected virtual void SetClientSizeCore(int x, int y) {
4222                         Size NewSize = InternalSizeFromClientSize (new Size (x, y));
4223                         
4224                         if (NewSize != Size.Empty)
4225                                 SetBounds (bounds.X, bounds.Y, NewSize.Width, NewSize.Height, BoundsSpecified.Size);
4226                 }
4227
4228                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4229                 protected internal void SetStyle(ControlStyles flag, bool value) {
4230                         if (value) {
4231                                 control_style |= flag;
4232                         } else {
4233                                 control_style &= ~flag;
4234                         }
4235                 }
4236
4237                 protected void SetTopLevel(bool value) {
4238                         if ((GetTopLevel() != value) && (parent != null)) {
4239                                 throw new ArgumentException ("Cannot change toplevel style of a parented control.");
4240                         }
4241
4242                         if (this is Form) {
4243                                 if (IsHandleCreated && value != Visible) {
4244                                         Visible = value;
4245                                 }
4246                         } else {
4247                                 // XXX MS.NET causes handle to be created here
4248                                 CreateHandle ();
4249                         }
4250                         is_toplevel = value;
4251                 }
4252
4253                 protected virtual void SetVisibleCore(bool value) {
4254                         if (value != is_visible) {
4255                                 is_visible = value;
4256                                 
4257                                 if (is_visible && ((window.Handle == IntPtr.Zero) || !is_created)) {
4258                                         CreateControl();
4259                                 }
4260
4261                                 if (IsHandleCreated) {
4262                                         XplatUI.SetVisible(Handle, is_visible, true);
4263                                         // Explicitly move Toplevel windows to where we want them;
4264                                         // apparently moving unmapped toplevel windows doesn't work
4265                                         if (is_visible && (this is Form)) {
4266                                                 XplatUI.SetWindowPos(window.Handle, bounds.X, bounds.Y, bounds.Width, bounds.Height);
4267                                         }
4268                                 }
4269                                 else {
4270                                         OnVisibleChanged(EventArgs.Empty);
4271                                 }
4272                         }
4273                 }
4274
4275                 [EditorBrowsable (EditorBrowsableState.Advanced)]
4276 #if NET_2_0
4277                 protected 
4278 #else
4279                 internal
4280 #endif
4281                 virtual Size SizeFromClientSize (Size clientSize) {
4282                         return InternalSizeFromClientSize (clientSize);
4283                 }
4284
4285                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4286                 protected void UpdateBounds() {
4287                         if (!IsHandleCreated)
4288                                 return;
4289
4290                         int     x;
4291                         int     y;
4292                         int     width;
4293                         int     height;
4294                         int     client_width;
4295                         int     client_height;
4296
4297                         XplatUI.GetWindowPos(this.Handle, this is Form, out x, out y, out width, out height, out client_width, out client_height);
4298
4299                         UpdateBounds(x, y, width, height, client_width, client_height);
4300                 }
4301
4302                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4303                 protected void UpdateBounds(int x, int y, int width, int height) {
4304                         CreateParams    cp;
4305                         Rectangle       rect;
4306
4307                         // Calculate client rectangle
4308                         rect = new Rectangle(0, 0, 0, 0);
4309                         cp = CreateParams;
4310
4311                         XplatUI.CalculateWindowRect(ref rect, cp.Style, cp.ExStyle, cp.menu, out rect);
4312                         UpdateBounds(x, y, width, height, width - (rect.Right - rect.Left), height - (rect.Bottom - rect.Top));
4313                 }
4314
4315                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4316                 protected void UpdateBounds(int x, int y, int width, int height, int clientWidth, int clientHeight) {
4317                         // UpdateBounds only seems to set our sizes and fire events but not update the GUI window to match
4318                         bool    moved   = false;
4319                         bool    resized = false;
4320
4321                         // Needed to generate required notifications
4322                         if ((this.bounds.X!=x) || (this.bounds.Y!=y)) {
4323                                 moved=true;
4324                         }
4325
4326                         if ((this.Bounds.Width!=width) || (this.Bounds.Height!=height)) {
4327                                 resized=true;
4328                         }
4329
4330                         bounds.X=x;
4331                         bounds.Y=y;
4332                         bounds.Width=width;
4333                         bounds.Height=height;
4334
4335                         // Assume explicit bounds set. SetImplicitBounds will restore old bounds
4336                         explicit_bounds = bounds;
4337
4338                         client_size.Width=clientWidth;
4339                         client_size.Height=clientHeight;
4340
4341                         if (moved) {
4342                                 OnLocationChanged(EventArgs.Empty);
4343
4344                                 if (!background_color.IsEmpty && background_color.A < byte.MaxValue)
4345                                         Invalidate ();
4346                         }
4347
4348                         if (resized) {
4349                                 OnSizeChanged(EventArgs.Empty);
4350 #if NET_2_0
4351                                 OnClientSizeChanged (EventArgs.Empty);
4352 #endif
4353                         }
4354                 }
4355
4356                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4357                 protected void UpdateStyles() {
4358                         if (!IsHandleCreated) {
4359                                 return;
4360                         }
4361
4362                         XplatUI.SetWindowStyle(window.Handle, CreateParams);
4363                         OnStyleChanged(EventArgs.Empty);
4364                 }
4365
4366                 private void UpdateZOrderOfChild(Control child) {
4367                         if (IsHandleCreated && child.IsHandleCreated && (child.parent == this)) {
4368                                 int     index;
4369
4370                                 index = child_controls.IndexOf(child);
4371
4372                                 if (index > 0) {
4373                                         XplatUI.SetZOrder(child.Handle, child_controls[index - 1].Handle, false, false);
4374                                 } else {
4375                                         IntPtr after = AfterTopMostControl ();
4376                                         if (after != IntPtr.Zero)
4377                                                 XplatUI.SetZOrder (child.Handle, after, false, false);
4378                                         else
4379                                                 XplatUI.SetZOrder (child.Handle, IntPtr.Zero, true, false);
4380                                 }
4381                         }
4382                 }
4383                 
4384                 // Override this if there is a control that shall always remain on
4385                 // top of other controls (such as scrollbars). If there are several
4386                 // of these controls, the bottom-most should be returned.
4387                 internal virtual IntPtr AfterTopMostControl () {
4388                         return IntPtr.Zero;
4389                 }
4390
4391                 // internal because we need to call it from ScrollableControl.OnVisibleChanged
4392                 internal void UpdateChildrenZOrder() {
4393                         Control [] controls;
4394
4395                         if (!IsHandleCreated) {
4396                                 return;
4397                         }
4398
4399                         // XXX This code is severely broken.  It leaks
4400                         // the "zero_sized" abstraction out of the X11
4401                         // backend and into Control.cs.  It'll work on
4402                         // windows simply by virtue of windows never
4403                         // setting that field to true.
4404                         //
4405                         // basically what we need to guard against is
4406                         // calling XplatUI.SetZOrder on an hwnd that
4407                         // corresponds to an unmapped X window.
4408                         controls = child_controls.GetAllControls ();
4409
4410                         ArrayList children_to_order = new ArrayList ();
4411
4412                         for (int i = 0; i < controls.Length; i ++) {
4413                                 if (!controls[i].IsHandleCreated || !controls[i].VisibleInternal)
4414                                         continue;
4415
4416                                 Hwnd hwnd = Hwnd.ObjectFromHandle (controls[i].Handle);
4417                                 if (hwnd.zero_sized)
4418                                         continue;
4419
4420                                 children_to_order.Add (controls[i]);
4421                         }
4422
4423                         for (int i = 1; i < children_to_order.Count; i ++) {
4424                                 Control upper = (Control)children_to_order[i-1];
4425                                 Control lower = (Control)children_to_order[i];
4426
4427                                 XplatUI.SetZOrder(lower.Handle, upper.Handle, false, false);
4428                         }
4429                 }
4430
4431                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4432                 protected void UpdateZOrder() {
4433                         if (parent != null) {
4434                                 parent.UpdateZOrderOfChild(this);
4435                         }
4436                 }
4437
4438                 protected virtual void WndProc(ref Message m) {
4439 #if debug
4440                         Console.WriteLine("Control {0} received message {1}", window.Handle == IntPtr.Zero ? this.Text : XplatUI.Window(window.Handle), m.ToString ());
4441 #endif
4442                         if ((this.control_style & ControlStyles.EnableNotifyMessage) != 0) {
4443                                 OnNotifyMessage(m);
4444                         }
4445
4446                         switch((Msg)m.Msg) {
4447                                 case Msg.WM_DESTROY: {
4448                                         WmDestroy(ref m);
4449                                         return;
4450                                 }
4451
4452                                 case Msg.WM_WINDOWPOSCHANGED: {
4453                                         WmWindowPosChanged(ref m);
4454                                         return;
4455                                 }
4456
4457                                         // Nice description of what should happen when handling WM_PAINT
4458                                         // can be found here: http://pluralsight.com/wiki/default.aspx/Craig/FlickerFreeControlDrawing.html
4459                                         // and here http://msdn.microsoft.com/msdnmag/issues/06/03/WindowsFormsPerformance/
4460                                 case Msg.WM_PAINT: {
4461                                         WmPaint (ref m);
4462                                         return;
4463                                 }
4464                                         
4465                                 // The DefWndProc will never have to handle this, we always paint the background in managed code
4466                                 // In theory this code would look at ControlStyles.AllPaintingInWmPaint and and call OnPaintBackground
4467                                 // here but it just makes things more complicated...
4468                                 case Msg.WM_ERASEBKGND: {
4469                                         WmEraseBackground (ref m);
4470                                         return;
4471                                 }
4472
4473                                 case Msg.WM_LBUTTONUP: {
4474                                         WmLButtonUp (ref m);
4475                                         return;
4476                                 }
4477                                         
4478                                 case Msg.WM_LBUTTONDOWN: {
4479                                         WmLButtonDown (ref m);
4480                                         return;
4481                                 }
4482
4483                                 case Msg.WM_LBUTTONDBLCLK: {
4484                                         WmLButtonDblClick (ref m);
4485                                         return;
4486                                 }
4487
4488                                 case Msg.WM_MBUTTONUP: {
4489                                         WmMButtonUp (ref m);
4490                                         return;
4491                                 }
4492                                         
4493                                 case Msg.WM_MBUTTONDOWN: {                                      
4494                                         WmMButtonDown (ref m);
4495                                         return;
4496                                 }
4497
4498                                 case Msg.WM_MBUTTONDBLCLK: {
4499                                         WmMButtonDblClick (ref m);
4500                                         return;
4501                                 }
4502
4503                                 case Msg.WM_RBUTTONUP: {
4504                                         WmRButtonUp (ref m);
4505                                         return;
4506                                 }
4507                                         
4508                                 case Msg.WM_RBUTTONDOWN: {                                      
4509                                         WmRButtonDown (ref m);
4510                                         return;
4511                                 }
4512
4513                                 case Msg.WM_RBUTTONDBLCLK: {
4514                                         WmRButtonDblClick (ref m);
4515                                         return;
4516                                 }
4517
4518                                 case Msg.WM_CONTEXTMENU: {
4519                                         WmContextMenu (ref m);
4520                                         return;
4521                                 }
4522
4523                                 case Msg.WM_MOUSEWHEEL: {
4524                                         WmMouseWheel (ref m);
4525                                         return;
4526                                 }
4527
4528
4529                                 case Msg.WM_MOUSEMOVE: {
4530                                         WmMouseMove (ref m);
4531                                         return;
4532                                 }
4533
4534                                 case Msg.WM_SHOWWINDOW: {
4535                                         WmShowWindow (ref m);
4536                                         return;
4537                                 }
4538
4539                                 case Msg.WM_CREATE: {
4540                                         WmCreate (ref m);
4541                                         return;
4542                                 }
4543
4544                                 case Msg.WM_MOUSE_ENTER: {
4545                                         WmMouseEnter (ref m);
4546                                         return;
4547                                 }
4548
4549                                 case Msg.WM_MOUSELEAVE: {
4550                                         WmMouseLeave (ref m);
4551                                         return;
4552                                 }
4553
4554                                 case Msg.WM_MOUSEHOVER: {
4555                                         WmMouseHover (ref m);
4556                                         return;
4557                                 }
4558
4559                                 case Msg.WM_SYSKEYUP: {
4560                                         WmSysKeyUp (ref m);
4561                                         return;
4562                                 }
4563
4564                                 case Msg.WM_SYSKEYDOWN:
4565                                 case Msg.WM_KEYDOWN:
4566                                 case Msg.WM_KEYUP:
4567                                 case Msg.WM_SYSCHAR:
4568                                 case Msg.WM_CHAR: {
4569                                         WmKeys (ref m);
4570                                         return;
4571                                 }
4572
4573                                 case Msg.WM_HELP: {
4574                                         WmHelp (ref m);
4575                                         return;
4576                                 }
4577
4578                                 case Msg.WM_KILLFOCUS: {
4579                                         WmKillFocus (ref m);
4580                                         return;
4581                                 }
4582
4583                                 case Msg.WM_SETFOCUS: {
4584                                         WmSetFocus (ref m);
4585                                         return;
4586                                 }
4587                                         
4588                                 case Msg.WM_SYSCOLORCHANGE: {
4589                                         WmSysColorChange (ref m);
4590                                         return;
4591                                 }
4592
4593                                 case Msg.WM_SETCURSOR: {
4594                                         WmSetCursor (ref m);
4595                                         return;
4596                                 }
4597
4598                                 case Msg.WM_CAPTURECHANGED: {
4599                                         WmCaptureChanged (ref m);
4600                                         return;
4601                                 }
4602
4603                                 default:
4604                                         DefWndProc(ref m);
4605                                         return;
4606                         }
4607                 }
4608                 #endregion      // Public Instance Methods
4609
4610                 #region WM methods
4611                 
4612                 private void WmDestroy (ref Message m) {
4613                         OnHandleDestroyed(EventArgs.Empty);
4614 #if DebugRecreate
4615                         IntPtr handle = window.Handle;
4616 #endif
4617                         window.InvalidateHandle();
4618
4619                         if (is_recreating) {
4620 #if DebugRecreate
4621                                 Console.WriteLine ("Creating handle for {0:X}", handle.ToInt32());
4622 #endif
4623                                 CreateHandle();
4624 #if DebugRecreate
4625                                 Console.WriteLine (" + new handle = {0:X}", Handle.ToInt32());
4626 #endif
4627                                 is_recreating = false;
4628                         }
4629                 }
4630
4631                 private void WmWindowPosChanged (ref Message m) {
4632                         if (Visible) {
4633                                 Rectangle save_bounds = explicit_bounds;
4634                                 UpdateBounds();
4635                                 explicit_bounds = save_bounds;
4636                                 if (GetStyle(ControlStyles.ResizeRedraw)) {
4637                                         Invalidate();
4638                                 }
4639                         }
4640                 }
4641
4642
4643                 // Nice description of what should happen when handling WM_PAINT
4644                 // can be found here: http://pluralsight.com/wiki/default.aspx/Craig/FlickerFreeControlDrawing.html
4645                 // and here http://msdn.microsoft.com/msdnmag/issues/06/03/WindowsFormsPerformance/
4646                 private void WmPaint (ref Message m) {
4647                         PaintEventArgs  paint_event;
4648
4649                         IntPtr handle = Handle;
4650
4651                         paint_event = XplatUI.PaintEventStart (handle, true);
4652
4653                         if (paint_event == null)
4654                                 return;
4655
4656                         DoubleBuffer current_buffer = null;
4657                         if (UseDoubleBuffering) {
4658                                 current_buffer = GetBackBuffer ();
4659                                 if (!current_buffer.InvalidRegion.IsVisible (paint_event.ClipRectangle)) {
4660                                         // Just blit the previous image
4661                                         current_buffer.Blit (paint_event);
4662                                         XplatUI.PaintEventEnd (handle, true);
4663                                         return;
4664                                 }
4665                                 current_buffer.Start (paint_event);
4666                         }
4667                                 
4668                         if (!GetStyle(ControlStyles.Opaque)) {
4669                                 OnPaintBackground (paint_event);
4670                         }
4671
4672                         // Button-derived controls choose to ignore their Opaque style, give them a chance to draw their background anyways
4673                         OnPaintBackgroundInternal (paint_event);
4674
4675                         OnPaintInternal(paint_event);
4676                         if (!paint_event.Handled) {
4677                                 OnPaint (paint_event);
4678                         }
4679
4680                         if (current_buffer != null) {
4681                                 current_buffer.End (paint_event);
4682                         }
4683
4684
4685                         XplatUI.PaintEventEnd (handle, true);
4686                 }
4687
4688                 private void WmEraseBackground (ref Message m) {
4689                         // The DefWndProc will never have to handle this, we always paint the background in managed code
4690                         // In theory this code would look at ControlStyles.AllPaintingInWmPaint and and call OnPaintBackground
4691                         // here but it just makes things more complicated...
4692                         m.Result = (IntPtr)1;
4693                 }
4694
4695                 private void WmLButtonUp (ref Message m) {
4696                         MouseEventArgs me;
4697
4698                         me = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Left, 
4699                                 mouse_clicks, 
4700                                 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4701                                 0);
4702
4703                         HandleClick(mouse_clicks, me);
4704                         OnMouseUp (me);
4705
4706                         if (InternalCapture) {
4707                                 InternalCapture = false;
4708                         }
4709
4710                         if (mouse_clicks > 1) {
4711                                 mouse_clicks = 1;
4712                         }
4713                 }
4714                                         
4715                 private void WmLButtonDown (ref Message m) {
4716                         if (CanSelect) {
4717                                 Select (true, true);
4718                         }
4719                         InternalCapture = true;
4720                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
4721                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4722                                 0));
4723                 }
4724
4725                 private void WmLButtonDblClick (ref Message m) {
4726                         InternalCapture = true;
4727                         mouse_clicks++;
4728                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
4729                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4730                                 0));
4731                 }
4732
4733                 private void WmMButtonUp (ref Message m) {
4734                         MouseEventArgs me;
4735
4736                         me = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Middle, 
4737                                 mouse_clicks, 
4738                                 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4739                                 0);
4740
4741                         HandleClick(mouse_clicks, me);
4742                         OnMouseUp (me);
4743                         if (InternalCapture) {
4744                                 InternalCapture = false;
4745                         }
4746                         if (mouse_clicks > 1) {
4747                                 mouse_clicks = 1;
4748                         }
4749                 }
4750                                         
4751                 private void WmMButtonDown (ref Message m) {
4752                         InternalCapture = true;
4753                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
4754                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4755                                 0));
4756                 }
4757
4758                 private void WmMButtonDblClick (ref Message m) {
4759                         InternalCapture = true;
4760                         mouse_clicks++;
4761                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
4762                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4763                                 0));
4764                 }
4765
4766                 private void WmRButtonUp (ref Message m) {
4767                         MouseEventArgs  me;
4768                         Point           pt;
4769
4770                         pt = new Point(LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()));
4771                         pt = PointToScreen(pt);
4772
4773                         XplatUI.SendMessage(m.HWnd, Msg.WM_CONTEXTMENU, m.HWnd, (IntPtr)(pt.X + (pt.Y << 16)));
4774
4775                         me = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Right, 
4776                                 mouse_clicks, 
4777                                 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4778                                 0);
4779
4780                         HandleClick(mouse_clicks, me);
4781                         OnMouseUp (me);
4782
4783                         if (InternalCapture) {
4784                                 InternalCapture = false;
4785                         }
4786
4787                         if (mouse_clicks > 1) {
4788                                 mouse_clicks = 1;
4789                         }
4790                 }
4791                                         
4792                 private void WmRButtonDown (ref Message m) {
4793                         InternalCapture = true;
4794                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
4795                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4796                                 0));
4797                 }
4798
4799                 private void WmRButtonDblClick (ref Message m) {
4800                         InternalCapture = true;
4801                         mouse_clicks++;
4802                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
4803                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4804                                 0));
4805                 }
4806
4807                 private void WmContextMenu (ref Message m) {
4808                         if (context_menu != null) {
4809                                 Point   pt;
4810
4811                                 pt = new Point(LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()));
4812
4813                                 if (pt.X == -1 || pt.Y == -1) {
4814                                         pt.X = (this.Width / 2) + this.Left;
4815                                         pt.Y = (this.Height / 2) + this.Top;
4816                                         pt = this.PointToScreen (pt);
4817                                 }
4818                                         
4819                                 context_menu.Show (this, PointToClient (pt));
4820                                 return;
4821                         }
4822
4823 #if NET_2_0
4824                                 // If there isn't a regular context menu, show the Strip version
4825                                 if (context_menu == null && context_menu_strip != null) {
4826                                         Point pt;
4827
4828                                         pt = new Point (LowOrder ((int)m.LParam.ToInt32 ()), HighOrder ((int)m.LParam.ToInt32 ()));
4829                                         
4830                                         if (pt.X == -1 || pt.Y == -1) { 
4831                                                 pt.X = (this.Width / 2) + this.Left; 
4832                                                 pt.Y = (this.Height /2) + this.Top; 
4833                                                 pt = this.PointToScreen (pt);
4834                                         }
4835                                         
4836                                         context_menu_strip.Show (this, PointToClient (pt));
4837                                         return;
4838                                 }
4839 #endif
4840                         DefWndProc(ref m);
4841                 }
4842
4843                 private void WmCreate (ref Message m) {
4844                         OnHandleCreated(EventArgs.Empty);
4845                 }
4846
4847                 private void WmMouseWheel (ref Message m) {
4848                         DefWndProc(ref m);
4849                         OnMouseWheel (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
4850                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4851                                 HighOrder(m.WParam.ToInt32())));
4852                 }
4853
4854
4855                 private void WmMouseMove (ref Message m) {
4856                         OnMouseMove  (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
4857                                 mouse_clicks, 
4858                                 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
4859                                 0));
4860                 }
4861
4862                 private void WmMouseEnter (ref Message m) {
4863                         if (is_entered) {
4864                                 return;
4865                         }
4866                         is_entered = true;
4867                         OnMouseEnter(EventArgs.Empty);
4868                 }
4869
4870                 private void WmMouseLeave (ref Message m) {
4871                         is_entered=false;
4872                         OnMouseLeave(EventArgs.Empty);
4873                 }
4874
4875                 private void WmMouseHover (ref Message m) {
4876                         OnMouseHover(EventArgs.Empty);
4877                 }
4878
4879                 private void WmShowWindow (ref Message m) {
4880                         if (m.WParam.ToInt32() != 0) {
4881                                 CreateControl ();
4882                                 /* if we're being shown, make sure our child controls all have their handles created */
4883                                 Control [] controls = child_controls.GetAllControls ();
4884                                 for (int i=0; i<controls.Length; i++) {
4885                                         if (controls [i].is_visible) {
4886                                                 controls [i].CreateControl ();
4887                                                 XplatUI.SetParent(controls[i].Handle, window.Handle); 
4888                                         }
4889                                 }
4890                         }
4891                         else {
4892                                 if (parent != null && Focused) {
4893                                         Control container;
4894
4895                                         // Need to start at parent, GetContainerControl might return ourselves if we're a container
4896                                         container = (Control)parent.GetContainerControl();
4897                                         if (container != null) {
4898                                                 container.SelectNextControl(this, true, true, true, true);
4899                                         }
4900                                 }
4901                         }
4902
4903                         if (is_toplevel || (this is Form && ((Form) this).IsMdiChild)) /* XXX make sure this works for mdi forms */
4904                                 OnVisibleChanged(EventArgs.Empty);
4905                 }
4906
4907                 private void WmSysKeyUp (ref Message m) {
4908                         if (ProcessKeyMessage(ref m)) {
4909                                 m.Result = IntPtr.Zero;
4910                                 return;
4911                         }
4912
4913                         if ((m.WParam.ToInt32() & (int)Keys.KeyCode) == (int)Keys.Menu) {
4914                                 Form    form;
4915
4916                                 form = FindForm();
4917                                 if (form != null && form.ActiveMenu != null) {
4918                                         form.ActiveMenu.ProcessCmdKey(ref m, (Keys)m.WParam.ToInt32());
4919                                 }
4920                         }
4921
4922                         DefWndProc (ref m);
4923                 }
4924
4925                 private void WmKeys (ref Message m) {
4926                         if (ProcessKeyMessage(ref m)) {
4927                                 m.Result = IntPtr.Zero;
4928                                 return;
4929                         }
4930                         DefWndProc (ref m);
4931                 }
4932
4933                 private void WmHelp (ref Message m) {
4934                         Point   mouse_pos;
4935                         if (m.LParam != IntPtr.Zero) {
4936                                 HELPINFO        hi;
4937
4938                                 hi = new HELPINFO();
4939
4940                                 hi = (HELPINFO) Marshal.PtrToStructure (m.LParam, typeof (HELPINFO));
4941                                 mouse_pos = new Point(hi.MousePos.x, hi.MousePos.y);
4942                         } else {
4943                                 mouse_pos = Control.MousePosition;
4944                         }
4945                         OnHelpRequested(new HelpEventArgs(mouse_pos));
4946                         m.Result = (IntPtr)1;
4947                 }
4948
4949                 private void WmKillFocus (ref Message m) {
4950                         this.has_focus = false;
4951                         OnLostFocus (EventArgs.Empty);
4952                 }
4953
4954                 private void WmSetFocus (ref Message m) {
4955                         if (!has_focus) {
4956                                 this.has_focus = true;
4957                                 OnGotFocus (EventArgs.Empty);
4958                         }
4959                 }
4960                                         
4961                 private void WmSysColorChange (ref Message m) {
4962                         ThemeEngine.Current.ResetDefaults();
4963                         OnSystemColorsChanged(EventArgs.Empty);
4964                 }
4965
4966                 private void WmSetCursor (ref Message m) {
4967                         if ((cursor == null) || ((HitTest)(m.LParam.ToInt32() & 0xffff) != HitTest.HTCLIENT)) {
4968                                 DefWndProc(ref m);
4969                                 return;
4970                         }
4971
4972                         XplatUI.SetCursor(window.Handle, cursor.handle);
4973                         m.Result = (IntPtr)1;
4974                 }
4975
4976                 private void WmCaptureChanged (ref Message m) {
4977                         is_captured = false;
4978                         OnMouseCaptureChanged (EventArgs.Empty);
4979                         m.Result = (IntPtr) 0;
4980                 }
4981
4982
4983                 #endregion
4984
4985                 #region OnXXX methods
4986 #if NET_2_0
4987                 protected virtual void OnAutoSizeChanged (EventArgs e)
4988                 {
4989                         EventHandler eh = (EventHandler)(Events[AutoSizeChangedEvent]);
4990                         if (eh != null)
4991                                 eh (this, e);
4992                 }
4993 #endif
4994
4995                 [EditorBrowsable (EditorBrowsableState.Advanced)]
4996                 protected virtual void OnBackColorChanged(EventArgs e) {
4997                         EventHandler eh = (EventHandler)(Events [BackColorChangedEvent]);
4998                         if (eh != null)
4999                                 eh (this, e);
5000                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackColorChanged(e);
5001                 }
5002
5003                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5004                 protected virtual void OnBackgroundImageChanged(EventArgs e) {
5005                         EventHandler eh = (EventHandler)(Events [BackgroundImageChangedEvent]);
5006                         if (eh != null)
5007                                 eh (this, e);
5008                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackgroundImageChanged(e);
5009                 }
5010
5011 #if NET_2_0
5012                 [EditorBrowsable (EditorBrowsableState.Advanced)]
5013                 protected virtual void OnBackgroundImageLayoutChanged (EventArgs e)
5014                 {
5015                         EventHandler eh = (EventHandler)(Events[BackgroundImageLayoutChangedEvent]);
5016                         if (eh != null)
5017                                 eh (this, e);
5018                 }
5019 #endif
5020
5021                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5022                 protected virtual void OnBindingContextChanged(EventArgs e) {
5023                         CheckDataBindings ();
5024                         EventHandler eh = (EventHandler)(Events [BindingContextChangedEvent]);
5025                         if (eh != null)
5026                                 eh (this, e);
5027                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBindingContextChanged(e);
5028                 }
5029
5030                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5031                 protected virtual void OnCausesValidationChanged(EventArgs e) {
5032                         EventHandler eh = (EventHandler)(Events [CausesValidationChangedEvent]);
5033                         if (eh != null)
5034                                 eh (this, e);
5035                 }
5036
5037                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5038                 protected virtual void OnChangeUICues(UICuesEventArgs e) {
5039                         UICuesEventHandler eh = (UICuesEventHandler)(Events [ChangeUICuesEvent]);
5040                         if (eh != null)
5041                                 eh (this, e);
5042                 }
5043
5044                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5045                 protected virtual void OnClick(EventArgs e) {
5046                         EventHandler eh = (EventHandler)(Events [ClickEvent]);
5047                         if (eh != null)
5048                                 eh (this, e);
5049                 }
5050
5051 #if NET_2_0
5052                 [EditorBrowsable (EditorBrowsableState.Advanced)]
5053                 protected virtual void OnClientSizeChanged (EventArgs e)
5054                 {
5055                         EventHandler eh = (EventHandler)(Events[ClientSizeChangedEvent]);
5056                         if (eh != null)
5057                                 eh (this, e);
5058                 }
5059 #endif
5060
5061                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5062                 protected virtual void OnContextMenuChanged(EventArgs e) {
5063                         EventHandler eh = (EventHandler)(Events [ContextMenuChangedEvent]);
5064                         if (eh != null)
5065                                 eh (this, e);
5066                 }
5067
5068 #if NET_2_0
5069                 [EditorBrowsable (EditorBrowsableState.Advanced)]
5070                 protected virtual void OnContextMenuStripChanged (EventArgs e) {
5071                         EventHandler eh = (EventHandler)(Events [ContextMenuStripChangedEvent]);
5072                         if (eh != null)
5073                                 eh (this, e);
5074                 }
5075 #endif
5076
5077                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5078                 protected virtual void OnControlAdded(ControlEventArgs e) {
5079                         ControlEventHandler eh = (ControlEventHandler)(Events [ControlAddedEvent]);
5080                         if (eh != null)
5081                                 eh (this, e);
5082                 }
5083
5084                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5085                 protected virtual void OnControlRemoved(ControlEventArgs e) {
5086                         ControlEventHandler eh = (ControlEventHandler)(Events [ControlRemovedEvent]);
5087                         if (eh != null)
5088                                 eh (this, e);
5089                 }
5090
5091                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5092                 protected virtual void OnCreateControl() {
5093                         // Override me!
5094                 }
5095
5096                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5097                 protected virtual void OnCursorChanged(EventArgs e) {
5098                         EventHandler eh = (EventHandler)(Events [CursorChangedEvent]);
5099                         if (eh != null)
5100                                 eh (this, e);
5101                 }
5102
5103                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5104                 protected virtual void OnDockChanged(EventArgs e) {
5105                         EventHandler eh = (EventHandler)(Events [DockChangedEvent]);
5106                         if (eh != null)
5107                                 eh (this, e);
5108                 }
5109
5110                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5111                 protected virtual void OnDoubleClick(EventArgs e) {
5112                         EventHandler eh = (EventHandler)(Events [DoubleClickEvent]);
5113                         if (eh != null)
5114                                 eh (this, e);
5115                 }
5116
5117                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5118                 protected virtual void OnDragDrop(DragEventArgs drgevent) {
5119                         DragEventHandler eh = (DragEventHandler)(Events [DragDropEvent]);
5120                         if (eh != null)
5121                                 eh (this, drgevent);
5122                 }
5123
5124                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5125                 protected virtual void OnDragEnter(DragEventArgs drgevent) {
5126                         DragEventHandler eh = (DragEventHandler)(Events [DragEnterEvent]);
5127                         if (eh != null)
5128                                 eh (this, drgevent);
5129                 }
5130
5131                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5132                 protected virtual void OnDragLeave(EventArgs e) {
5133                         EventHandler eh = (EventHandler)(Events [DragLeaveEvent]);
5134                         if (eh != null)
5135                                 eh (this, e);
5136                 }
5137
5138                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5139                 protected virtual void OnDragOver(DragEventArgs drgevent) {
5140                         DragEventHandler eh = (DragEventHandler)(Events [DragOverEvent]);
5141                         if (eh != null)
5142                                 eh (this, drgevent);
5143                 }
5144
5145                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5146                 protected virtual void OnEnabledChanged(EventArgs e) {
5147                         if (IsHandleCreated) {
5148                                 if (this is Form) {
5149                                         if (((Form)this).context == null) {
5150                                                 XplatUI.EnableWindow(window.Handle, Enabled);
5151                                         }
5152                                 } else {
5153                                         XplatUI.EnableWindow(window.Handle, Enabled);
5154                                 }
5155                                 Refresh();
5156                         }
5157
5158                         EventHandler eh = (EventHandler)(Events [EnabledChangedEvent]);
5159                         if (eh != null)
5160                                 eh (this, e);
5161
5162                         for (int i=0; i<child_controls.Count; i++) {
5163                                 child_controls[i].OnParentEnabledChanged(e);
5164                         }
5165                 }
5166
5167                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5168                 protected virtual void OnEnter(EventArgs e) {
5169                         EventHandler eh = (EventHandler)(Events [EnterEvent]);
5170                         if (eh != null)
5171                                 eh (this, e);
5172                 }
5173
5174                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5175                 protected virtual void OnFontChanged(EventArgs e) {
5176                         EventHandler eh = (EventHandler)(Events [FontChangedEvent]);
5177                         if (eh != null)
5178                                 eh (this, e);
5179                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentFontChanged(e);
5180                 }
5181
5182                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5183                 protected virtual void OnForeColorChanged(EventArgs e) {
5184                         EventHandler eh = (EventHandler)(Events [ForeColorChangedEvent]);
5185                         if (eh != null)
5186                                 eh (this, e);
5187                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentForeColorChanged(e);
5188                 }
5189
5190                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5191                 protected virtual void OnGiveFeedback(GiveFeedbackEventArgs gfbevent) {
5192                         GiveFeedbackEventHandler eh = (GiveFeedbackEventHandler)(Events [GiveFeedbackEvent]);
5193                         if (eh != null)
5194                                 eh (this, gfbevent);
5195                 }
5196                 
5197                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5198                 protected virtual void OnGotFocus(EventArgs e) {
5199                         EventHandler eh = (EventHandler)(Events [GotFocusEvent]);
5200                         if (eh != null)
5201                                 eh (this, e);
5202                 }
5203
5204                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5205                 protected virtual void OnHandleCreated(EventArgs e) {
5206                         EventHandler eh = (EventHandler)(Events [HandleCreatedEvent]);
5207                         if (eh != null)
5208                                 eh (this, e);
5209                 }
5210
5211                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5212                 protected virtual void OnHandleDestroyed(EventArgs e) {
5213                         EventHandler eh = (EventHandler)(Events [HandleDestroyedEvent]);
5214                         if (eh != null)
5215                                 eh (this, e);
5216                 }
5217
5218                 internal void RaiseHelpRequested (HelpEventArgs hevent) {
5219                         OnHelpRequested (hevent);
5220                 }
5221                 
5222                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5223                 protected virtual void OnHelpRequested(HelpEventArgs hevent) {
5224                         HelpEventHandler eh = (HelpEventHandler)(Events [HelpRequestedEvent]);
5225                         if (eh != null)
5226                                 eh (this, hevent);
5227                 }
5228
5229                 protected virtual void OnImeModeChanged(EventArgs e) {
5230                         EventHandler eh = (EventHandler)(Events [ImeModeChangedEvent]);
5231                         if (eh != null)
5232                                 eh (this, e);
5233                 }
5234
5235                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5236                 protected virtual void OnInvalidated(InvalidateEventArgs e) {
5237                         if (UseDoubleBuffering) {
5238                                 // should this block be here?  seems like it
5239                                 // would be more at home in
5240                                 // NotifyInvalidated..
5241                                 if (e.InvalidRect == ClientRectangle) {
5242                                         InvalidateBackBuffer ();
5243                                 } else if (backbuffer != null){
5244                                         // we need this Inflate call here so
5245                                         // that the border of the rectangle is
5246                                         // considered Visible (the
5247                                         // invalid_region.IsVisible call) in
5248                                         // the WM_PAINT handling below.
5249                                         Rectangle r = Rectangle.Inflate(e.InvalidRect, 1,1);
5250                                         backbuffer.InvalidRegion.Union (r);
5251                                 }
5252                         }
5253
5254                         InvalidateEventHandler eh = (InvalidateEventHandler)(Events [InvalidatedEvent]);
5255                         if (eh != null)
5256                                 eh (this, e);
5257                 }
5258
5259                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5260                 protected virtual void OnKeyDown(KeyEventArgs e) {
5261                         KeyEventHandler eh = (KeyEventHandler)(Events [KeyDownEvent]);
5262                         if (eh != null)
5263                                 eh (this, e);
5264                 }
5265
5266                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5267                 protected virtual void OnKeyPress(KeyPressEventArgs e) {
5268                         KeyPressEventHandler eh = (KeyPressEventHandler)(Events [KeyPressEvent]);
5269                         if (eh != null)
5270                                 eh (this, e);
5271                 }
5272
5273                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5274                 protected virtual void OnKeyUp(KeyEventArgs e) {
5275                         KeyEventHandler eh = (KeyEventHandler)(Events [KeyUpEvent]);
5276                         if (eh != null)
5277                                 eh (this, e);
5278                 }
5279
5280                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5281                 protected virtual void OnLayout(LayoutEventArgs levent) {
5282                         LayoutEventHandler eh = (LayoutEventHandler)(Events [LayoutEvent]);
5283                         if (eh != null)
5284                                 eh (this, levent);
5285
5286                         LayoutEngine.Layout (this, levent);
5287                 }
5288
5289                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5290                 protected virtual void OnLeave(EventArgs e) {
5291                         EventHandler eh = (EventHandler)(Events [LeaveEvent]);
5292                         if (eh != null)
5293                                 eh (this, e);
5294                 }
5295
5296                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5297                 protected virtual void OnLocationChanged(EventArgs e) {
5298                         OnMove(e);
5299                         EventHandler eh = (EventHandler)(Events [LocationChangedEvent]);
5300                         if (eh != null)
5301                                 eh (this, e);
5302                 }
5303
5304                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5305                 protected virtual void OnLostFocus(EventArgs e) {
5306                         EventHandler eh = (EventHandler)(Events [LostFocusEvent]);
5307                         if (eh != null)
5308                                 eh (this, e);
5309                 }
5310
5311 #if NET_2_0
5312                 protected virtual void OnMarginChanged (EventArgs e)
5313                 {
5314                         EventHandler eh = (EventHandler)(Events[MarginChangedEvent]);
5315                         if (eh != null)
5316                                 eh (this, e);
5317                 }
5318 #endif
5319                 [EditorBrowsable (EditorBrowsableState.Advanced)]
5320 #if NET_2_0
5321                 protected virtual void OnMouseCaptureChanged (EventArgs e)
5322 #else
5323                 internal virtual void OnMouseCaptureChanged (EventArgs e)
5324 #endif
5325                 {
5326                         EventHandler eh = (EventHandler)(Events [MouseCaptureChangedEvent]);
5327                         if (eh != null)
5328                                 eh (this, e);
5329                 }
5330
5331 #if NET_2_0
5332                 [EditorBrowsable (EditorBrowsableState.Advanced)]
5333                 protected virtual void OnMouseClick (MouseEventArgs e)
5334                 {
5335                         MouseEventHandler eh = (MouseEventHandler)(Events [MouseClickEvent]);
5336                         if (eh != null)
5337                                 eh (this, e);
5338                 }
5339
5340                 [EditorBrowsable (EditorBrowsableState.Advanced)]
5341                 protected virtual void OnMouseDoubleClick (MouseEventArgs e)
5342                 {
5343                         MouseEventHandler eh = (MouseEventHandler)(Events [MouseDoubleClickEvent]);
5344                         if (eh != null)
5345                                 eh (this, e);
5346                 }
5347 #endif
5348
5349                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5350                 protected virtual void OnMouseDown(MouseEventArgs e) {
5351                         MouseEventHandler eh = (MouseEventHandler)(Events [MouseDownEvent]);
5352                         if (eh != null)
5353                                 eh (this, e);
5354                 }
5355
5356                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5357                 protected virtual void OnMouseEnter(EventArgs e) {
5358                         EventHandler eh = (EventHandler)(Events [MouseEnterEvent]);
5359                         if (eh != null)
5360                                 eh (this, e);
5361                 }
5362
5363                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5364                 protected virtual void OnMouseHover(EventArgs e) {
5365                         EventHandler eh = (EventHandler)(Events [MouseHoverEvent]);
5366                         if (eh != null)
5367                                 eh (this, e);
5368                 }
5369
5370                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5371                 protected virtual void OnMouseLeave(EventArgs e) {
5372                         EventHandler eh = (EventHandler)(Events [MouseLeaveEvent]);
5373                         if (eh != null)
5374                                 eh (this, e);
5375                 }
5376
5377                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5378                 protected virtual void OnMouseMove(MouseEventArgs e) {
5379                         MouseEventHandler eh = (MouseEventHandler)(Events [MouseMoveEvent]);
5380                         if (eh != null)
5381                                 eh (this, e);
5382                 }
5383
5384                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5385                 protected virtual void OnMouseUp(MouseEventArgs e) {
5386                         MouseEventHandler eh = (MouseEventHandler)(Events [MouseUpEvent]);
5387                         if (eh != null)
5388                                 eh (this, e);
5389                 }
5390
5391                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5392                 protected virtual void OnMouseWheel(MouseEventArgs e) {
5393                         MouseEventHandler eh = (MouseEventHandler)(Events [MouseWheelEvent]);
5394                         if (eh != null)
5395                                 eh (this, e);
5396                 }
5397
5398                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5399                 protected virtual void OnMove(EventArgs e) {
5400                         EventHandler eh = (EventHandler)(Events [MoveEvent]);
5401                         if (eh != null)
5402                                 eh (this, e);
5403                 }
5404
5405                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5406                 protected virtual void OnNotifyMessage(Message m) {
5407                         // Override me!
5408                 }
5409
5410 #if NET_2_0
5411                 protected virtual void OnPaddingChanged (EventArgs e) {
5412                         EventHandler eh = (EventHandler) (Events [PaddingChangedEvent]);
5413                         if (eh != null)
5414                                 eh (this, e);
5415                 }
5416 #endif
5417
5418                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5419                 protected virtual void OnPaint(PaintEventArgs e) {
5420                         PaintEventHandler eh = (PaintEventHandler)(Events [PaintEvent]);
5421                         if (eh != null)
5422                                 eh (this, e);
5423                 }
5424
5425                 internal virtual void OnPaintBackgroundInternal(PaintEventArgs e) {
5426                         // Override me
5427                 }
5428
5429                 internal virtual void OnPaintInternal(PaintEventArgs e) {
5430                         // Override me
5431                 }
5432
5433                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5434                 protected virtual void OnPaintBackground(PaintEventArgs pevent) {
5435                         PaintControlBackground (pevent);
5436                 }
5437
5438                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5439                 protected virtual void OnParentBackColorChanged(EventArgs e) {
5440                         if (background_color.IsEmpty && background_image==null) {
5441                                 Invalidate();
5442                                 OnBackColorChanged(e);
5443                         }
5444                 }
5445
5446                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5447                 protected virtual void OnParentBackgroundImageChanged(EventArgs e) {
5448                         Invalidate();
5449                         OnBackgroundImageChanged(e);
5450                 }
5451
5452                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5453                 protected virtual void OnParentBindingContextChanged(EventArgs e) {
5454                         if (binding_context==null) {
5455                                 binding_context=Parent.binding_context;
5456                                 OnBindingContextChanged(e);
5457                         }
5458                 }
5459
5460                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5461                 protected virtual void OnParentChanged(EventArgs e) {
5462                         EventHandler eh = (EventHandler)(Events [ParentChangedEvent]);
5463                         if (eh != null)
5464                                 eh (this, e);
5465                 }
5466
5467                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5468                 protected virtual void OnParentEnabledChanged(EventArgs e) {
5469                         if (is_enabled) {
5470                                 OnEnabledChanged(e);
5471                         }
5472                 }
5473
5474                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5475                 protected virtual void OnParentFontChanged(EventArgs e) {
5476                         if (font==null) {
5477                                 Invalidate();
5478                                 OnFontChanged(e);
5479                         }
5480                 }
5481
5482                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5483                 protected virtual void OnParentForeColorChanged(EventArgs e) {
5484                         if (foreground_color.IsEmpty) {
5485                                 Invalidate();
5486                                 OnForeColorChanged(e);
5487                         }
5488                 }
5489
5490                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5491                 protected virtual void OnParentRightToLeftChanged(EventArgs e) {
5492                         if (right_to_left==RightToLeft.Inherit) {
5493                                 Invalidate();
5494                                 OnRightToLeftChanged(e);
5495                         }
5496                 }
5497
5498                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5499                 protected virtual void OnParentVisibleChanged(EventArgs e) {
5500                         if (is_visible) {
5501                                 OnVisibleChanged(e);
5502                         }
5503                 }
5504
5505                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5506                 protected virtual void OnQueryContinueDrag(QueryContinueDragEventArgs e) {
5507                         QueryContinueDragEventHandler eh = (QueryContinueDragEventHandler)(Events [QueryContinueDragEvent]);
5508                         if (eh != null)
5509                                 eh (this, e);
5510                 }
5511
5512 #if NET_2_0
5513                 [EditorBrowsable (EditorBrowsableState.Advanced)]
5514                 protected virtual void OnRegionChanged (EventArgs e)
5515                 {
5516                         EventHandler eh = (EventHandler)(Events[RegionChangedEvent]);
5517                         if (eh != null)
5518                                 eh (this, e);
5519                 }
5520 #endif
5521
5522                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5523                 protected virtual void OnResize(EventArgs e) {
5524                         PerformLayout(this, "Bounds");
5525
5526                         EventHandler eh = (EventHandler)(Events [ResizeEvent]);
5527                         if (eh != null)
5528                                 eh (this, e);
5529                 }
5530
5531                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5532                 protected virtual void OnRightToLeftChanged(EventArgs e) {
5533                         EventHandler eh = (EventHandler)(Events [RightToLeftChangedEvent]);
5534                         if (eh != null)
5535                                 eh (this, e);
5536                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentRightToLeftChanged(e);
5537                 }
5538
5539                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5540                 protected virtual void OnSizeChanged(EventArgs e) {
5541                         DisposeBackBuffer ();
5542                         OnResize(e);
5543                         EventHandler eh = (EventHandler)(Events [SizeChangedEvent]);
5544                         if (eh != null)
5545                                 eh (this, e);
5546                 }
5547
5548                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5549                 protected virtual void OnStyleChanged(EventArgs e) {
5550                         EventHandler eh = (EventHandler)(Events [StyleChangedEvent]);
5551                         if (eh != null)
5552                                 eh (this, e);
5553                 }
5554
5555                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5556                 protected virtual void OnSystemColorsChanged(EventArgs e) {
5557                         EventHandler eh = (EventHandler)(Events [SystemColorsChangedEvent]);
5558                         if (eh != null)
5559                                 eh (this, e);
5560                 }
5561
5562                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5563                 protected virtual void OnTabIndexChanged(EventArgs e) {
5564                         EventHandler eh = (EventHandler)(Events [TabIndexChangedEvent]);
5565                         if (eh != null)
5566                                 eh (this, e);
5567                 }
5568
5569                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5570                 protected virtual void OnTabStopChanged(EventArgs e) {
5571                         EventHandler eh = (EventHandler)(Events [TabStopChangedEvent]);
5572                         if (eh != null)
5573                                 eh (this, e);
5574                 }
5575
5576                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5577                 protected virtual void OnTextChanged(EventArgs e) {
5578                         EventHandler eh = (EventHandler)(Events [TextChangedEvent]);
5579                         if (eh != null)
5580                                 eh (this, e);
5581                 }
5582
5583                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5584                 protected virtual void OnValidated(EventArgs e) {
5585                         EventHandler eh = (EventHandler)(Events [ValidatedEvent]);
5586                         if (eh != null)
5587                                 eh (this, e);
5588                 }
5589
5590                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5591                 protected virtual void OnValidating(System.ComponentModel.CancelEventArgs e) {
5592                         CancelEventHandler eh = (CancelEventHandler)(Events [ValidatingEvent]);
5593                         if (eh != null)
5594                                 eh (this, e);
5595                 }
5596
5597                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5598                 protected virtual void OnVisibleChanged(EventArgs e) {
5599                         EventHandler eh = (EventHandler)(Events [VisibleChangedEvent]);
5600                         if (eh != null)
5601                                 eh (this, e);
5602
5603                         // We need to tell our kids
5604                         for (int i=0; i<child_controls.Count; i++) {
5605                                 if (child_controls[i].Visible) {
5606                                         child_controls[i].OnParentVisibleChanged(e);
5607                                 }
5608                         }
5609                 }
5610                 #endregion      // OnXXX methods
5611
5612                 #region Events
5613 #if NET_2_0
5614                 static object AutoSizeChangedEvent = new object ();
5615 #endif
5616                 static object BackColorChangedEvent = new object ();
5617                 static object BackgroundImageChangedEvent = new object ();
5618 #if NET_2_0
5619                 static object BackgroundImageLayoutChangedEvent = new object ();
5620 #endif
5621                 static object BindingContextChangedEvent = new object ();
5622                 static object CausesValidationChangedEvent = new object ();
5623                 static object ChangeUICuesEvent = new object ();
5624                 static object ClickEvent = new object ();
5625 #if NET_2_0
5626                 static object ClientSizeChangedEvent = new object ();
5627 #endif
5628                 static object ContextMenuChangedEvent = new object ();
5629 #if NET_2_0
5630                 static object ContextMenuStripChangedEvent = new object ();
5631 #endif
5632                 static object ControlAddedEvent = new object ();
5633                 static object ControlRemovedEvent = new object ();
5634                 static object CursorChangedEvent = new object ();
5635                 static object DockChangedEvent = new object ();
5636                 static object DoubleClickEvent = new object ();
5637                 static object DragDropEvent = new object ();
5638                 static object DragEnterEvent = new object ();
5639                 static object DragLeaveEvent = new object ();
5640                 static object DragOverEvent = new object ();
5641                 static object EnabledChangedEvent = new object ();
5642                 static object EnterEvent = new object ();
5643                 static object FontChangedEvent = new object ();
5644                 static object ForeColorChangedEvent = new object ();
5645                 static object GiveFeedbackEvent = new object ();
5646                 static object GotFocusEvent = new object ();
5647                 static object HandleCreatedEvent = new object ();
5648                 static object HandleDestroyedEvent = new object ();
5649                 static object HelpRequestedEvent = new object ();
5650                 static object ImeModeChangedEvent = new object ();
5651                 static object InvalidatedEvent = new object ();
5652                 static object KeyDownEvent = new object ();
5653                 static object KeyPressEvent = new object ();
5654                 static object KeyUpEvent = new object ();
5655                 static object LayoutEvent = new object ();
5656                 static object LeaveEvent = new object ();
5657                 static object LocationChangedEvent = new object ();
5658                 static object LostFocusEvent = new object ();
5659 #if NET_2_0
5660                 static object MarginChangedEvent = new object ();
5661 #endif
5662                 static object MouseCaptureChangedEvent = new object ();
5663 #if NET_2_0
5664                 static object MouseClickEvent = new object ();
5665                 static object MouseDoubleClickEvent = new object ();
5666 #endif
5667                 static object MouseDownEvent = new object ();
5668                 static object MouseEnterEvent = new object ();
5669                 static object MouseHoverEvent = new object ();
5670                 static object MouseLeaveEvent = new object ();
5671                 static object MouseMoveEvent = new object ();
5672                 static object MouseUpEvent = new object ();
5673                 static object MouseWheelEvent = new object ();
5674                 static object MoveEvent = new object ();
5675 #if NET_2_0
5676                 static object PaddingChangedEvent = new object ();
5677 #endif
5678                 static object PaintEvent = new object ();
5679                 static object ParentChangedEvent = new object ();
5680 #if NET_2_0
5681                 static object PreviewKeyDownEvent = new object ();
5682 #endif
5683                 static object QueryAccessibilityHelpEvent = new object ();
5684                 static object QueryContinueDragEvent = new object ();
5685 #if NET_2_0
5686                 static object RegionChangedEvent = new object ();
5687 #endif
5688                 static object ResizeEvent = new object ();
5689                 static object RightToLeftChangedEvent = new object ();
5690                 static object SizeChangedEvent = new object ();
5691                 static object StyleChangedEvent = new object ();
5692                 static object SystemColorsChangedEvent = new object ();
5693                 static object TabIndexChangedEvent = new object ();
5694                 static object TabStopChangedEvent = new object ();
5695                 static object TextChangedEvent = new object ();
5696                 static object ValidatedEvent = new object ();
5697                 static object ValidatingEvent = new object ();
5698                 static object VisibleChangedEvent = new object ();
5699
5700 #if NET_2_0
5701                 [Browsable (false)]
5702                 [EditorBrowsable (EditorBrowsableState.Never)]
5703                 public event EventHandler AutoSizeChanged {
5704                         add { Events.AddHandler (AutoSizeChangedEvent, value);}
5705                         remove {Events.RemoveHandler (AutoSizeChangedEvent, value);}
5706                 }
5707 #endif
5708                 public event EventHandler BackColorChanged {
5709                         add { Events.AddHandler (BackColorChangedEvent, value); }
5710                         remove { Events.RemoveHandler (BackColorChangedEvent, value); }
5711                 }
5712
5713                 public event EventHandler BackgroundImageChanged {
5714                         add { Events.AddHandler (BackgroundImageChangedEvent, value); }
5715                         remove { Events.RemoveHandler (BackgroundImageChangedEvent, value); }
5716                 }
5717
5718 #if NET_2_0     
5719                 public event EventHandler BackgroundImageLayoutChanged {
5720                         add {Events.AddHandler (BackgroundImageLayoutChangedEvent, value);}
5721                         remove {Events.RemoveHandler (BackgroundImageLayoutChangedEvent, value);}
5722                 }
5723 #endif
5724
5725                 public event EventHandler BindingContextChanged {
5726                         add { Events.AddHandler (BindingContextChangedEvent, value); }
5727                         remove { Events.RemoveHandler (BindingContextChangedEvent, value); }
5728                 }
5729
5730                 public event EventHandler CausesValidationChanged {
5731                         add { Events.AddHandler (CausesValidationChangedEvent, value); }
5732                         remove { Events.RemoveHandler (CausesValidationChangedEvent, value); }
5733                 }
5734
5735                 public event UICuesEventHandler ChangeUICues {
5736                         add { Events.AddHandler (ChangeUICuesEvent, value); }
5737                         remove { Events.RemoveHandler (ChangeUICuesEvent, value); }
5738                 }
5739
5740                 public event EventHandler Click {
5741                         add { Events.AddHandler (ClickEvent, value); }
5742                         remove { Events.RemoveHandler (ClickEvent, value); }
5743                 }
5744                 
5745 #if NET_2_0
5746                 public event EventHandler ClientSizeChanged {
5747                         add {Events.AddHandler (ClientSizeChangedEvent, value);}
5748                         remove {Events.RemoveHandler (ClientSizeChangedEvent, value);}
5749                 }
5750 #endif
5751
5752 #if NET_2_0
5753                 [Browsable (false)]
5754 #endif
5755                 public event EventHandler ContextMenuChanged {
5756                         add { Events.AddHandler (ContextMenuChangedEvent, value); }
5757                         remove { Events.RemoveHandler (ContextMenuChangedEvent, value); }
5758                 }
5759
5760 #if NET_2_0
5761                 public event EventHandler ContextMenuStripChanged {
5762                         add { Events.AddHandler (ContextMenuStripChangedEvent, value); }
5763                         remove { Events.RemoveHandler (ContextMenuStripChangedEvent, value);}
5764                 }
5765 #endif
5766
5767
5768                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5769 #if NET_2_0
5770                 [Browsable(true)]
5771 #else 
5772                 [Browsable(false)]
5773 #endif
5774                 public event ControlEventHandler ControlAdded {
5775                         add { Events.AddHandler (ControlAddedEvent, value); }
5776                         remove { Events.RemoveHandler (ControlAddedEvent, value); }
5777                 }
5778
5779                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5780 #if NET_2_0
5781                 [Browsable(true)]
5782 #else 
5783                 [Browsable(false)]
5784 #endif
5785                 public event ControlEventHandler ControlRemoved {
5786                         add { Events.AddHandler (ControlRemovedEvent, value); }
5787                         remove { Events.RemoveHandler (ControlRemovedEvent, value); }
5788                 }
5789
5790                 [MWFDescription("Fired when the cursor for the control has been changed"), MWFCategory("PropertyChanged")]
5791                 public event EventHandler CursorChanged {
5792                         add { Events.AddHandler (CursorChangedEvent, value); }
5793                         remove { Events.RemoveHandler (CursorChangedEvent, value); }
5794                 }
5795                 public event EventHandler DockChanged {
5796                         add { Events.AddHandler (DockChangedEvent, value); }
5797                         remove { Events.RemoveHandler (DockChangedEvent, value); }
5798                 }
5799
5800                 public event EventHandler DoubleClick {
5801                         add { Events.AddHandler (DoubleClickEvent, value); }
5802                         remove { Events.RemoveHandler (DoubleClickEvent, value); }
5803                 }
5804
5805                 public event DragEventHandler DragDrop {
5806                         add { Events.AddHandler (DragDropEvent, value); }
5807                         remove { Events.RemoveHandler (DragDropEvent, value); }
5808                 }
5809
5810                 public event DragEventHandler DragEnter {
5811                         add { Events.AddHandler (DragEnterEvent, value); }
5812                         remove { Events.RemoveHandler (DragEnterEvent, value); }
5813                 }
5814
5815                 public event EventHandler DragLeave {
5816                         add { Events.AddHandler (DragLeaveEvent, value); }
5817                         remove { Events.RemoveHandler (DragLeaveEvent, value); }
5818                 }
5819
5820                 public event DragEventHandler DragOver {
5821                         add { Events.AddHandler (DragOverEvent, value); }
5822                         remove { Events.RemoveHandler (DragOverEvent, value); }
5823                 }
5824
5825                 public event EventHandler EnabledChanged {
5826                         add { Events.AddHandler (EnabledChangedEvent, value); }
5827                         remove { Events.RemoveHandler (EnabledChangedEvent, value); }
5828                 }
5829
5830                 public event EventHandler Enter {
5831                         add { Events.AddHandler (EnterEvent, value); }
5832                         remove { Events.RemoveHandler (EnterEvent, value); }
5833                 }
5834
5835                 public event EventHandler FontChanged {
5836                         add { Events.AddHandler (FontChangedEvent, value); }
5837                         remove { Events.RemoveHandler (FontChangedEvent, value); }
5838                 }
5839
5840                 public event EventHandler ForeColorChanged {
5841                         add { Events.AddHandler (ForeColorChangedEvent, value); }
5842                         remove { Events.RemoveHandler (ForeColorChangedEvent, value); }
5843                 }
5844
5845                 public event GiveFeedbackEventHandler GiveFeedback {
5846                         add { Events.AddHandler (GiveFeedbackEvent, value); }
5847                         remove { Events.RemoveHandler (GiveFeedbackEvent, value); }
5848                 }
5849
5850                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5851                 [Browsable(false)]
5852                 public event EventHandler GotFocus {
5853                         add { Events.AddHandler (GotFocusEvent, value); }
5854                         remove { Events.RemoveHandler (GotFocusEvent, value); }
5855                 }
5856
5857
5858                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5859                 [Browsable(false)]
5860                 public event EventHandler HandleCreated {
5861                         add { Events.AddHandler (HandleCreatedEvent, value); }
5862                         remove { Events.RemoveHandler (HandleCreatedEvent, value); }
5863                 }
5864
5865                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5866                 [Browsable(false)]
5867                 public event EventHandler HandleDestroyed {
5868                         add { Events.AddHandler (HandleDestroyedEvent, value); }
5869                         remove { Events.RemoveHandler (HandleDestroyedEvent, value); }
5870                 }
5871
5872                 public event HelpEventHandler HelpRequested {
5873                         add { Events.AddHandler (HelpRequestedEvent, value); }
5874                         remove { Events.RemoveHandler (HelpRequestedEvent, value); }
5875                 }
5876
5877                 public event EventHandler ImeModeChanged {
5878                         add { Events.AddHandler (ImeModeChangedEvent, value); }
5879                         remove { Events.RemoveHandler (ImeModeChangedEvent, value); }
5880                 }
5881
5882                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5883                 [Browsable(false)]
5884                 public event InvalidateEventHandler Invalidated {
5885                         add { Events.AddHandler (InvalidatedEvent, value); }
5886                         remove { Events.RemoveHandler (InvalidatedEvent, value); }
5887                 }
5888
5889                 public event KeyEventHandler KeyDown {
5890                         add { Events.AddHandler (KeyDownEvent, value); }
5891                         remove { Events.RemoveHandler (KeyDownEvent, value); }
5892                 }
5893
5894                 public event KeyPressEventHandler KeyPress {
5895                         add { Events.AddHandler (KeyPressEvent, value); }
5896                         remove { Events.RemoveHandler (KeyPressEvent, value); }
5897                 }
5898
5899                 public event KeyEventHandler KeyUp {
5900                         add { Events.AddHandler (KeyUpEvent, value); }
5901                         remove { Events.RemoveHandler (KeyUpEvent, value); }
5902                 }
5903
5904                 public event LayoutEventHandler Layout {
5905                         add { Events.AddHandler (LayoutEvent, value); }
5906                         remove { Events.RemoveHandler (LayoutEvent, value); }
5907                 }
5908
5909                 public event EventHandler Leave {
5910                         add { Events.AddHandler (LeaveEvent, value); }
5911                         remove { Events.RemoveHandler (LeaveEvent, value); }
5912                 }
5913
5914                 public event EventHandler LocationChanged {
5915                         add { Events.AddHandler (LocationChangedEvent, value); }
5916                         remove { Events.RemoveHandler (LocationChangedEvent, value); }
5917                 }
5918
5919                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5920                 [Browsable(false)]
5921                 public event EventHandler LostFocus {
5922                         add { Events.AddHandler (LostFocusEvent, value); }
5923                         remove { Events.RemoveHandler (LostFocusEvent, value); }
5924                 }
5925
5926 #if NET_2_0
5927                 public event EventHandler MarginChanged {
5928                         add { Events.AddHandler (MarginChangedEvent, value); }
5929                         remove {Events.RemoveHandler (MarginChangedEvent, value); }
5930                 }
5931 #endif
5932 #if NET_2_0
5933                 public event EventHandler MouseCaptureChanged {
5934 #else
5935                 internal event EventHandler MouseCaptureChanged {
5936 #endif
5937                         add { Events.AddHandler (MouseCaptureChangedEvent, value); }
5938                         remove { Events.RemoveHandler (MouseCaptureChangedEvent, value); }
5939                 }
5940 #if NET_2_0             
5941                 public event MouseEventHandler MouseClick
5942                 {
5943                         add { Events.AddHandler (MouseClickEvent, value); }
5944                         remove { Events.RemoveHandler (MouseClickEvent, value); }
5945                 }
5946                 public event MouseEventHandler MouseDoubleClick
5947                 {
5948                         add { Events.AddHandler (MouseDoubleClickEvent, value); }
5949                         remove { Events.RemoveHandler (MouseDoubleClickEvent, value); }
5950                 }
5951 #endif
5952                 public event MouseEventHandler MouseDown {
5953                         add { Events.AddHandler (MouseDownEvent, value); }
5954                         remove { Events.RemoveHandler (MouseDownEvent, value); }
5955                 }
5956
5957                 public event EventHandler MouseEnter {
5958                         add { Events.AddHandler (MouseEnterEvent, value); }
5959                         remove { Events.RemoveHandler (MouseEnterEvent, value); }
5960                 }
5961
5962                 public event EventHandler MouseHover {
5963                         add { Events.AddHandler (MouseHoverEvent, value); }
5964                         remove { Events.RemoveHandler (MouseHoverEvent, value); }
5965                 }
5966
5967                 public event EventHandler MouseLeave {
5968                         add { Events.AddHandler (MouseLeaveEvent, value); }
5969                         remove { Events.RemoveHandler (MouseLeaveEvent, value); }
5970                 }
5971
5972                 public event MouseEventHandler MouseMove {
5973                         add { Events.AddHandler (MouseMoveEvent, value); }
5974                         remove { Events.RemoveHandler (MouseMoveEvent, value); }
5975                 }
5976
5977                 public event MouseEventHandler MouseUp {
5978                         add { Events.AddHandler (MouseUpEvent, value); }
5979                         remove { Events.RemoveHandler (MouseUpEvent, value); }
5980                 }
5981
5982                 [EditorBrowsable(EditorBrowsableState.Advanced)]
5983                 [Browsable(false)]
5984                 public event MouseEventHandler MouseWheel {
5985                         add { Events.AddHandler (MouseWheelEvent, value); }
5986                         remove { Events.RemoveHandler (MouseWheelEvent, value); }
5987                 }
5988
5989                 public event EventHandler Move {
5990                         add { Events.AddHandler (MoveEvent, value); }
5991                         remove { Events.RemoveHandler (MoveEvent, value); }
5992                 }
5993 #if NET_2_0
5994                 public event EventHandler PaddingChanged
5995                 {
5996                         add { Events.AddHandler (PaddingChangedEvent, value); }
5997                         remove { Events.RemoveHandler (PaddingChangedEvent, value); }
5998                 }
5999 #endif
6000                 public event PaintEventHandler Paint {
6001                         add { Events.AddHandler (PaintEvent, value); }
6002                         remove { Events.RemoveHandler (PaintEvent, value); }
6003                 }
6004
6005                 public event EventHandler ParentChanged {
6006                         add { Events.AddHandler (ParentChangedEvent, value); }
6007                         remove { Events.RemoveHandler (ParentChangedEvent, value); }
6008                 }
6009
6010 #if NET_2_0
6011                 public event PreviewKeyDownEventHandler PreviewKeyDown {
6012                         add { Events.AddHandler (PreviewKeyDownEvent, value); }
6013                         remove { Events.RemoveHandler (PreviewKeyDownEvent, value); }
6014                 }
6015 #endif
6016
6017                 public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp {
6018                         add { Events.AddHandler (QueryAccessibilityHelpEvent, value); }
6019                         remove { Events.RemoveHandler (QueryAccessibilityHelpEvent, value); }
6020                 }
6021
6022                 public event QueryContinueDragEventHandler QueryContinueDrag {
6023                         add { Events.AddHandler (QueryContinueDragEvent, value); }
6024                         remove { Events.RemoveHandler (QueryContinueDragEvent, value); }
6025                 }
6026
6027 #if NET_2_0
6028                 public event EventHandler RegionChanged {
6029                         add { Events.AddHandler (RegionChangedEvent, value); }
6030                         remove { Events.RemoveHandler (RegionChangedEvent, value); }
6031                 }
6032 #endif
6033
6034 #if NET_2_0     
6035                 [EditorBrowsable (EditorBrowsableState.Advanced)]
6036 #endif
6037                 public event EventHandler Resize {
6038                         add { Events.AddHandler (ResizeEvent, value); }
6039                         remove { Events.RemoveHandler (ResizeEvent, value); }
6040                 }
6041
6042                 public event EventHandler RightToLeftChanged {
6043                         add { Events.AddHandler (RightToLeftChangedEvent, value); }
6044                         remove { Events.RemoveHandler (RightToLeftChangedEvent, value); }
6045                 }
6046
6047                 public event EventHandler SizeChanged {
6048                         add { Events.AddHandler (SizeChangedEvent, value); }
6049                         remove { Events.RemoveHandler (SizeChangedEvent, value); }
6050                 }
6051
6052                 public event EventHandler StyleChanged {
6053                         add { Events.AddHandler (StyleChangedEvent, value); }
6054                         remove { Events.RemoveHandler (StyleChangedEvent, value); }
6055                 }
6056
6057                 public event EventHandler SystemColorsChanged {
6058                         add { Events.AddHandler (SystemColorsChangedEvent, value); }
6059                         remove { Events.RemoveHandler (SystemColorsChangedEvent, value); }
6060                 }
6061
6062                 public event EventHandler TabIndexChanged {
6063                         add { Events.AddHandler (TabIndexChangedEvent, value); }
6064                         remove { Events.RemoveHandler (TabIndexChangedEvent, value); }
6065                 }
6066
6067                 public event EventHandler TabStopChanged {
6068                         add { Events.AddHandler (TabStopChangedEvent, value); }
6069                         remove { Events.RemoveHandler (TabStopChangedEvent, value); }
6070                 }
6071
6072                 public event EventHandler TextChanged {
6073                         add { Events.AddHandler (TextChangedEvent, value); }
6074                         remove { Events.RemoveHandler (TextChangedEvent, value); }
6075                 }
6076
6077                 public event EventHandler Validated {
6078                         add { Events.AddHandler (ValidatedEvent, value); }
6079                         remove { Events.RemoveHandler (ValidatedEvent, value); }
6080                 }
6081
6082                 public event CancelEventHandler Validating {
6083                         add { Events.AddHandler (ValidatingEvent, value); }
6084                         remove { Events.RemoveHandler (ValidatingEvent, value); }
6085                 }
6086
6087                 public event EventHandler VisibleChanged {
6088                         add { Events.AddHandler (VisibleChangedEvent, value); }
6089                         remove { Events.RemoveHandler (VisibleChangedEvent, value); }
6090                 }
6091
6092                 #endregion      // Events
6093         }
6094 }