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