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