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