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