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