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