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