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