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