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