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