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