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