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