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