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