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