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