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