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