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