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