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