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