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