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