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