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