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