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