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