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