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