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