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