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