- Removed debug output
[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
2585                                 // Find out where the window manager placed us
2586                                 UpdateBounds();
2587                                 UpdateStyles();
2588                         }
2589
2590                         if (window.Handle!=IntPtr.Zero) {
2591                                 if (!controls.Contains(window.Handle)) {
2592                                         controls.Add(this);
2593                                 }
2594
2595                                 creator_thread = Thread.CurrentThread;
2596
2597                                 XplatUI.SetWindowBackground(window.Handle, this.BackColor);
2598
2599                                 OnHandleCreated(EventArgs.Empty);
2600                         }
2601                 }
2602
2603                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2604                 protected virtual void DefWndProc(ref Message m) {
2605                         window.DefWndProc(ref m);
2606                 }
2607
2608                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2609                 protected virtual void DestroyHandle() {
2610                         if (IsHandleCreated) {
2611                                 if (Handle != IntPtr.Zero) {
2612                                         controls.Remove(Handle);
2613                                 }
2614
2615                                 if (window != null) {
2616                                         window.DestroyHandle();
2617                                 }
2618                         }
2619                 }
2620
2621                 protected bool GetStyle(ControlStyles flag) {
2622                         return (control_style & flag) != 0;
2623                 }
2624
2625                 protected bool GetTopLevel() {
2626                         return is_toplevel;
2627                 }
2628
2629                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2630                 protected virtual void InitLayout() {
2631                         if (parent != null) {
2632                                 parent.PerformLayout(this, "parent");
2633                         }
2634                 }
2635
2636                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2637                 protected void InvokeGotFocus(Control toInvoke, EventArgs e) {
2638                         toInvoke.OnGotFocus(e);
2639                 }
2640
2641                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2642                 protected void InvokeLostFocus(Control toInvoke, EventArgs e) {
2643                         toInvoke.OnLostFocus(e);
2644                 }
2645
2646                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2647                 protected void InvokeOnClick(Control toInvoke, EventArgs e) {
2648                         toInvoke.OnClick(e);
2649                 }
2650
2651                 protected void InvokePaint(Control toInvoke, PaintEventArgs e) {
2652                         toInvoke.OnPaint(e);
2653                 }
2654
2655                 protected void InvokePaintBackground(Control toInvoke, PaintEventArgs e) {
2656                         toInvoke.OnPaintBackground(e);
2657                 }
2658
2659                 protected virtual bool IsInputChar (char charCode) {
2660                         if (parent != null) {
2661                                 return parent.IsInputChar(charCode);
2662                         }
2663
2664                         return true;
2665                 }
2666
2667                 protected virtual bool IsInputKey (Keys keyData) {
2668                         // Doc says this one calls IsInputChar; not sure what to do with that
2669                         return false;
2670                 }
2671
2672                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2673                 protected virtual void NotifyInvalidate(Rectangle invalidatedArea) {
2674                         // override me?
2675                 }
2676
2677                 protected virtual bool ProcessCmdKey(ref Message msg, Keys keyData) {
2678                         if ((context_menu != null) && context_menu.ProcessCmdKey(ref msg, keyData)) {
2679                                 return true;
2680                         }
2681
2682                         if (parent != null) {
2683                                 return parent.ProcessCmdKey(ref msg, keyData);
2684                         }
2685
2686                         return false;
2687                 }
2688
2689                 protected virtual bool ProcessDialogChar(char charCode) {
2690                         if (parent != null) {
2691                                 return parent.ProcessDialogChar (charCode);
2692                         }
2693
2694                         return false;
2695                 }
2696
2697                 protected virtual bool ProcessDialogKey (Keys keyData) {
2698                         if (parent != null) {
2699                                 return parent.ProcessDialogKey (keyData);
2700                         }
2701
2702                         return false;
2703                 }
2704
2705                 protected virtual bool ProcessKeyEventArgs (ref Message msg)
2706                 {
2707                         KeyEventArgs            key_event;
2708
2709                         PreProcessMessage(ref msg);
2710
2711                         switch (msg.Msg) {
2712                                 case (int)Msg.WM_KEYDOWN: {
2713                                         key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
2714                                         OnKeyDown (key_event);
2715                                         return key_event.Handled;
2716                                 }
2717                                 case (int)Msg.WM_KEYUP: {
2718                                         key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
2719                                         OnKeyUp (key_event);
2720                                         return key_event.Handled;
2721                                 }
2722
2723                                 case (int)Msg.WM_CHAR: {
2724                                         KeyPressEventArgs       key_press_event;
2725
2726                                         key_press_event = new KeyPressEventArgs((char)msg.WParam);
2727                                         OnKeyPress(key_press_event);
2728                                         return key_press_event.Handled;
2729                                 }
2730
2731                                 default: {
2732                                         break;
2733                                 }
2734                         }
2735
2736                         return false;
2737                 }
2738
2739                 protected internal virtual bool ProcessKeyMessage(ref Message msg) {
2740                         if (parent != null) {
2741                                 if (parent.ProcessKeyPreview(ref msg)) {
2742                                         return true;
2743                                 }
2744                         }
2745
2746                         return ProcessKeyEventArgs(ref msg);
2747                 }
2748
2749                 protected virtual bool ProcessKeyPreview(ref Message msg) {
2750                         if (parent != null) {
2751                                 return parent.ProcessKeyPreview(ref msg);
2752                         }
2753
2754                         return false;
2755                 }
2756
2757                 protected virtual bool ProcessMnemonic(char charCode) {
2758                         // override me
2759                         return false;
2760                 }
2761
2762                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2763                 protected void RaiseDragEvent(object key, DragEventArgs e) {
2764                         // MS Internal
2765                 }
2766
2767                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2768                 protected void RaiseKeyEvent(object key, KeyEventArgs e) {
2769                         // MS Internal
2770                 }
2771
2772                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2773                 protected void RaiseMouseEvent(object key, MouseEventArgs e) {
2774                         // MS Internal
2775                 }
2776
2777                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2778                 protected void RaisePaintEvent(object key, PaintEventArgs e) {
2779                         // MS Internal
2780                 }
2781
2782                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2783                 protected void RecreateHandle() {
2784                         IEnumerator child = child_controls.GetEnumerator();
2785
2786                         is_recreating=true;
2787
2788                         if (IsHandleCreated) {
2789                                 DestroyHandle();
2790                                 CreateHandle();
2791
2792                                 // FIXME ZOrder?
2793
2794                                 while (child.MoveNext()) {
2795                                         ((Control)child.Current).RecreateHandle();
2796                                 }
2797                         } else {
2798                                 CreateHandle();
2799                         }
2800
2801                         is_recreating = false;
2802                 }
2803
2804                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2805                 protected void ResetMouseEventArgs() {
2806                         // MS Internal
2807                 }
2808
2809                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2810                 protected ContentAlignment RtlTranslateAlignment(ContentAlignment align) {
2811                         if (right_to_left == RightToLeft.No) {
2812                                 return align;
2813                         }
2814
2815                         switch (align) {
2816                                 case ContentAlignment.TopLeft: {
2817                                         return ContentAlignment.TopRight;
2818                                 }
2819
2820                                 case ContentAlignment.TopRight: {
2821                                         return ContentAlignment.TopLeft;
2822                                 }
2823
2824                                 case ContentAlignment.MiddleLeft: {
2825                                         return ContentAlignment.MiddleRight;
2826                                 }
2827
2828                                 case ContentAlignment.MiddleRight: {
2829                                         return ContentAlignment.MiddleLeft;
2830                                 }
2831
2832                                 case ContentAlignment.BottomLeft: {
2833                                         return ContentAlignment.BottomRight;
2834                                 }
2835
2836                                 case ContentAlignment.BottomRight: {
2837                                         return ContentAlignment.BottomLeft;
2838                                 }
2839
2840                                 default: {
2841                                         // if it's center it doesn't change
2842                                         return align;
2843                                 }
2844                         }
2845                 }
2846
2847                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2848                 protected HorizontalAlignment RtlTranslateAlignment(HorizontalAlignment align) {
2849                         if ((right_to_left == RightToLeft.No) || (align == HorizontalAlignment.Center)) {
2850                                 return align;
2851                         }
2852
2853                         if (align == HorizontalAlignment.Left) {
2854                                 return HorizontalAlignment.Right;
2855                         }
2856
2857                         // align must be HorizontalAlignment.Right
2858                         return HorizontalAlignment.Left;
2859                 }
2860
2861                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2862                 protected LeftRightAlignment RtlTranslateAlignment(LeftRightAlignment align) {
2863                         if (right_to_left == RightToLeft.No) {
2864                                 return align;
2865                         }
2866
2867                         if (align == LeftRightAlignment.Left) {
2868                                 return LeftRightAlignment.Right;
2869                         }
2870
2871                         // align must be LeftRightAlignment.Right;
2872                         return LeftRightAlignment.Left;
2873                 }
2874
2875                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2876                 protected ContentAlignment RtlTranslateContent(ContentAlignment align) {
2877                         return RtlTranslateAlignment(align);
2878                 }
2879
2880                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2881                 protected HorizontalAlignment RtlTranslateHorizontal(HorizontalAlignment align) {
2882                         return RtlTranslateAlignment(align);
2883                 }
2884
2885                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2886                 protected LeftRightAlignment RtlTranslateLeftRight(LeftRightAlignment align) {
2887                         return RtlTranslateAlignment(align);
2888                 }
2889
2890                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2891                 protected virtual void ScaleCore(float dx, float dy) {
2892                         Point   location;
2893                         Size    size;
2894
2895                         SuspendLayout();
2896
2897                         location = new Point((int)(Left * dx), (int)(Top * dy));
2898                         size = this.ClientSize;
2899                         
2900
2901                         if (!GetStyle(ControlStyles.FixedWidth)) {
2902                                 size.Width = (int)(size.Width * dx);
2903                         }
2904
2905                         if (!GetStyle(ControlStyles.FixedHeight)) {
2906                                 size.Height = (int)(size.Height * dy);
2907                         }
2908
2909                         Location = location;
2910                         ClientSize = size;
2911
2912                         /* Now scale our children */
2913                         for (int i=0; i < child_controls.Count; i++) {
2914                                 child_controls[i].Scale(dx, dy);
2915                         }
2916
2917                         ResumeLayout();
2918                 }
2919
2920                 protected virtual void Select(bool directed, bool forward) {
2921                         int     index;
2922                         bool    result;
2923
2924                         if (!directed) {
2925                                 // Select this control
2926                                 Select(this);
2927                                 return;
2928                         }
2929
2930                         if (parent == null) {
2931                                 return;
2932                         }
2933
2934                         index = parent.child_controls.IndexOf(this);
2935                         result = false;
2936
2937                         do {
2938                                 if (forward) {
2939                                         if ((index+1) < parent.child_controls.Count) {
2940                                                 index++;
2941                                         } else {
2942                                                 index = 0;
2943                                         }
2944                                 } else {
2945                                         if (index>0) {
2946                                                 index++;
2947                                         } else {
2948                                                 index = parent.child_controls.Count-1;
2949                                         }
2950                                 }
2951                                 result = Select(parent.child_controls[index]);
2952                         } while (!result && parent.child_controls[index] != this);
2953                 }
2954
2955                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2956                 protected virtual void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
2957                         // SetBoundsCore updates the Win32 control itself. UpdateBounds updates the controls variables and fires events, I'm guessing - pdb
2958                         if ((specified & BoundsSpecified.X) != BoundsSpecified.X) {
2959                                 x = Left;
2960                         }
2961
2962                         if ((specified & BoundsSpecified.Y) != BoundsSpecified.Y) {
2963                                 y = Top;
2964                         }
2965
2966                         if ((specified & BoundsSpecified.Width)!= BoundsSpecified.Width) {
2967                                 width = Width;
2968                         }
2969
2970                         if ((specified & BoundsSpecified.Height) != BoundsSpecified.Height) {
2971                                 height = Height;
2972                         }
2973
2974                         if (IsHandleCreated) {
2975                                 XplatUI.SetWindowPos(Handle, x, y, width, height);
2976                         }
2977                         UpdateBounds(x, y, width, height);
2978                 }
2979
2980                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2981                 protected virtual void SetClientSizeCore(int x, int y) {
2982                         // Calculate the actual window size from the client size (it usually stays the same or grows)
2983                         Rectangle       ClientRect;
2984                         Rectangle       WindowRect;
2985                         CreateParams    cp;
2986
2987                         ClientRect = new Rectangle(0, 0, x, y);
2988                         cp = this.CreateParams;
2989
2990                         if (XplatUI.CalculateWindowRect(Handle, ref ClientRect, cp.Style, false, out WindowRect)==false) {
2991                                 return;
2992                         }
2993
2994                         SetBoundsCore(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
2995                 }
2996
2997                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2998                 protected void SetStyle(ControlStyles flag, bool value) {
2999                         if (value) {
3000                                 control_style |= flag;
3001                         } else {
3002                                 control_style &= ~flag;
3003                         }
3004                 }
3005
3006                 protected void SetTopLevel(bool value) {
3007                         if ((GetTopLevel() != value) && (parent != null)) {
3008                                 throw new Exception();
3009                         }
3010
3011                         if (this is Form) {
3012                                 if (value == true) {
3013                                         if (!Visible) {
3014                                                 Visible = true;
3015                                         }
3016                                 } else {
3017                                         if (Visible) {
3018                                                 Visible = false;
3019                                         }
3020                                 }
3021                         }
3022                         is_toplevel = value;
3023                 }
3024
3025                 protected virtual void SetVisibleCore(bool value) {
3026                         if (value!=is_visible) {
3027                                 is_visible=value;
3028                                 XplatUI.SetVisible(Handle, value);
3029                                 // Explicitly move Toplevel windows to where we want them;
3030                                 // apparently moving unmapped toplevel windows doesn't work
3031                                 if (is_visible && (this is Form.FormParentWindow)) {
3032                                         XplatUI.SetWindowPos(window.Handle, bounds.X, bounds.Y, bounds.Width, bounds.Height);
3033                                 }
3034                                 OnVisibleChanged(EventArgs.Empty);
3035                                 if (!is_visible) {
3036                                         if (dc_mem != null) {
3037                                                 dc_mem.Dispose();
3038                                                 dc_mem = null;
3039                                         }
3040
3041                                         if (bmp_mem != null) {
3042                                                 bmp_mem.Dispose();
3043                                                 bmp_mem = null;
3044                                         }
3045                                 } else {
3046                                         this.CreateBuffers(bounds.Width, bounds.Height);
3047                                 }
3048
3049                                 // FIXME - deal with focus
3050
3051                                 if (parent != null) {
3052                                         parent.PerformLayout(this, "visible");
3053                                 } else {
3054                                         PerformLayout(this, "visible");
3055                                 }
3056                         }
3057                 }
3058         
3059                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3060                 protected void UpdateBounds() {
3061                         int     x;
3062                         int     y;
3063                         int     width;
3064                         int     height;
3065                         int     client_width;
3066                         int     client_height;
3067
3068                         if (!IsHandleCreated) {
3069                                 CreateHandle();
3070                         }
3071
3072                         XplatUI.GetWindowPos(this.Handle, this is Form.FormParentWindow, out x, out y, out width, out height, out client_width, out client_height);
3073                         UpdateBounds(x, y, width, height, client_width, client_height);
3074                 }
3075
3076                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3077                 protected void UpdateBounds(int x, int y, int width, int height) {
3078                         // UpdateBounds only seems to set our sizes and fire events but not update the GUI window to match
3079                         bool    moved   = false;
3080                         bool    resized = false;
3081
3082                         int     client_x_diff = this.bounds.Width-this.client_size.Width;
3083                         int     client_y_diff = this.bounds.Height-this.client_size.Height;
3084
3085                         // Needed to generate required notifications
3086                         if ((this.bounds.X!=x) || (this.bounds.Y!=y)) {
3087                                 moved=true;
3088                         }
3089
3090                         if ((this.Bounds.Width!=width) || (this.Bounds.Height!=height)) {
3091                                 resized=true;
3092                         }
3093
3094                         bounds.X=x;
3095                         bounds.Y=y;
3096                         bounds.Width=width;
3097                         bounds.Height=height;
3098
3099                         // Update client rectangle as well
3100                         if (this.layout_suspended==0) {
3101                                 prev_size.Width=client_size.Width;
3102                                 prev_size.Height=client_size.Height;
3103                         }
3104
3105                         client_size.Width=width-client_x_diff;
3106                         client_size.Height=height-client_y_diff;
3107
3108                         if (moved) {
3109                                 OnLocationChanged(EventArgs.Empty);
3110                         }
3111
3112                         if (resized) {
3113                                 OnSizeChanged(EventArgs.Empty);
3114                         }
3115                 }
3116
3117                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3118                 protected void UpdateBounds(int x, int y, int width, int height, int clientWidth, int clientHeight) {
3119                         UpdateBounds(x, y, width, height);
3120
3121                         this.client_size.Width=clientWidth;
3122                         this.client_size.Height=clientHeight;
3123                 }
3124
3125                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3126                 protected void UpdateStyles() {
3127                         if (!IsHandleCreated) {
3128                                 return;
3129                         }
3130
3131                         if ( !(this is Form) && !(this is Form.FormParentWindow)) {
3132                                 XplatUI.SetWindowStyle(window.Handle, CreateParams);
3133                         } else {
3134                                 if (this is Form) {
3135                                         XplatUI.SetWindowStyle(((Form)this).form_parent_window.window.Handle, ((Form)this).CreateFormParams);
3136                                 } else {
3137                                         XplatUI.SetWindowStyle(((Form.FormParentWindow)this).window.Handle, ((Form.FormParentWindow)this).owner.CreateFormParams);
3138                                 }
3139                         }
3140                 }
3141
3142                 protected void UpdateZOrder() {
3143                         int     children;
3144 #if not
3145                         Control ctl;
3146
3147                         if (parent == null) {
3148                                 return;
3149                         }
3150
3151                         ctl = parent;
3152
3153                         children = ctl.child_controls.Count;
3154                         for (int i = 1; i < children; i++ ) {
3155                                 XplatUI.SetZOrder(ctl.child_controls[i].window.Handle, ctl.child_controls[i-1].window.Handle, false, false); 
3156                         }
3157 #else
3158                         children = child_controls.Count;
3159                         for (int i = 1; i < children; i++ ) {
3160                                 XplatUI.SetZOrder(child_controls[i].window.Handle, child_controls[i-1].window.Handle, false, false); 
3161                         }
3162 #endif
3163                 }
3164
3165                 protected virtual void WndProc(ref Message m) {
3166 #if debug
3167                         Console.WriteLine("Received message {0}", m);
3168 #endif
3169                         if ((this.control_style & ControlStyles.EnableNotifyMessage) != 0) {
3170                                 OnNotifyMessage(m);
3171                         }
3172
3173                         switch((Msg)m.Msg) {
3174                         case Msg.WM_WINDOWPOSCHANGED: {
3175                                 if (Visible) {
3176                                         UpdateBounds();
3177                                         if (GetStyle(ControlStyles.ResizeRedraw)) {
3178                                                 Invalidate();
3179                                         }
3180                                 }
3181                                 return;
3182                         }
3183
3184                         case Msg.WM_PAINT: {                            
3185                                 PaintEventArgs  paint_event;
3186
3187                                 paint_event = XplatUI.PaintEventStart(Handle);
3188                                 OnPaint(paint_event);
3189                                 XplatUI.PaintEventEnd(Handle);
3190                                 DefWndProc(ref m);      
3191                                 return;
3192                         }
3193                                 
3194                         case Msg.WM_ERASEBKGND: {
3195                                 if (GetStyle (ControlStyles.UserPaint)) {
3196                                         if (!GetStyle(ControlStyles.AllPaintingInWmPaint)) {
3197                                                 PaintEventArgs eraseEventArgs = new PaintEventArgs (m.WParam == IntPtr.Zero ? Graphics.FromHwnd (m.HWnd) :
3198                                                                 Graphics.FromHdc (m.WParam), new Rectangle (new Point (0,0),Size));
3199                                                 OnPaintBackground (eraseEventArgs);
3200                                         }
3201                                         m.Result = (IntPtr)1;
3202                                 } else {
3203                                         m.Result = IntPtr.Zero;
3204                                         DefWndProc (ref m);     
3205                                 }                                       
3206                                         
3207                                 return;
3208                         }
3209
3210                         case Msg.WM_LBUTTONUP: {
3211                                 HandleClick(mouse_clicks);
3212                                 OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Left, 
3213                                         mouse_clicks, 
3214                                         LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3215                                         0));
3216                                 if (mouse_clicks > 1) {
3217                                         mouse_clicks = 1;
3218                                 }
3219                                 return;
3220                         }
3221                                 
3222                         case Msg.WM_LBUTTONDOWN: {
3223                                 if (CanSelect && !is_selected) {
3224                                         Select(this);
3225                                 }
3226                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3227                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3228                                         0));
3229                                         
3230                                 return;
3231                         }
3232
3233                         case Msg.WM_LBUTTONDBLCLK: {
3234                                 mouse_clicks++;
3235                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3236                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3237                                         0));
3238
3239                                 return;
3240                         }
3241
3242                         case Msg.WM_MBUTTONUP: {
3243                                 HandleClick(mouse_clicks);
3244                                 OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Middle, 
3245                                         mouse_clicks, 
3246                                         LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3247                                         0));
3248                                 if (mouse_clicks > 1) {
3249                                         mouse_clicks = 1;
3250                                 }
3251                                 return;
3252                         }
3253                                 
3254                         case Msg.WM_MBUTTONDOWN: {                                      
3255                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3256                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3257                                         0));
3258                                         
3259                                 return;
3260                         }
3261
3262                         case Msg.WM_MBUTTONDBLCLK: {
3263                                 mouse_clicks++;
3264                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3265                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3266                                         0));
3267                                 return;
3268                         }
3269
3270                         case Msg.WM_RBUTTONUP: {
3271                                 HandleClick(mouse_clicks);
3272                                 OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Right, 
3273                                         mouse_clicks, 
3274                                         LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3275                                         0));
3276                                 if (mouse_clicks > 1) {
3277                                         mouse_clicks = 1;
3278                                 }
3279                                 return;
3280                         }
3281                                 
3282                         case Msg.WM_RBUTTONDOWN: {                                      
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_RBUTTONDBLCLK: {
3290                                 mouse_clicks++;
3291                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3292                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3293                                         0));
3294                                 return;
3295                         }
3296
3297                         case Msg.WM_MOUSEWHEEL: {                               
3298
3299                                 OnMouseWheel (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3300                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3301                                         HighOrder(m.WParam.ToInt32())));
3302                                 return;
3303                         }
3304
3305                                 
3306                         case Msg.WM_MOUSEMOVE: {                                        
3307                                 OnMouseMove  (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3308                                         mouse_clicks, 
3309                                         LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3310                                         0));
3311                                 return;
3312                         }
3313
3314                         case Msg.WM_MOUSE_ENTER: {
3315                                 if (is_entered) {
3316                                         return;
3317                                 }
3318                                 is_entered = true;
3319                                 OnMouseEnter(EventArgs.Empty);
3320                                 return;
3321                         }
3322
3323                         case Msg.WM_MOUSE_LEAVE: {
3324                                 is_entered=false;
3325                                 OnMouseLeave(EventArgs.Empty);
3326                                 return;
3327                         }
3328
3329                         case Msg.WM_MOUSEHOVER: {
3330                                 OnMouseHover(EventArgs.Empty);
3331                                 return;
3332                         }
3333                         
3334                         case Msg.WM_KEYDOWN: {
3335                                 if (!ProcessKeyMessage(ref m)) {
3336                                         DefWndProc (ref m);
3337                                 }
3338                                 return;
3339                         }
3340
3341                         case Msg.WM_KEYUP: {
3342                                 if (!ProcessKeyMessage(ref m)) {
3343                                         DefWndProc (ref m);
3344                                 }
3345                                 return;
3346                         }               
3347
3348                         case Msg.WM_CHAR: {
3349                                 if (!ProcessKeyMessage(ref m)) {
3350                                         DefWndProc (ref m);
3351                                 }
3352                                 return;
3353                         }
3354
3355                         case Msg.WM_HELP: {
3356                                 Point   mouse_pos;
3357                                 if (m.LParam != IntPtr.Zero) {
3358                                         HELPINFO        hi;
3359
3360                                         hi = new HELPINFO();
3361
3362                                         hi = (HELPINFO) Marshal.PtrToStructure (m.LParam, typeof (HELPINFO));
3363                                         mouse_pos = new Point(hi.MousePos.x, hi.MousePos.y);
3364                                 } else {
3365                                         mouse_pos = Control.MousePosition;
3366                                 }
3367                                 OnHelpRequested(new HelpEventArgs(mouse_pos));
3368                                 m.Result = (IntPtr)1;
3369                                 return;
3370                         }
3371
3372                         case Msg.WM_KILLFOCUS: {
3373                                 OnLeave(EventArgs.Empty);
3374                                 if (CausesValidation) {
3375                                         CancelEventArgs e;
3376                                         e = new CancelEventArgs(false);
3377
3378                                         OnValidating(e);
3379
3380                                         if (e.Cancel) {
3381                                                 Focus();
3382                                                 return;
3383                                         }
3384
3385                                         OnValidated(EventArgs.Empty);
3386                                 }
3387
3388                                 this.has_focus = false;
3389                                 this.is_selected = false;
3390                                 OnLostFocus(EventArgs.Empty);
3391                                 return;
3392                         }
3393
3394                         case Msg.WM_SETFOCUS: {
3395                                 OnEnter(EventArgs.Empty);
3396                                 this.has_focus = true;
3397                                 OnGotFocus(EventArgs.Empty);
3398                                 return;
3399                         }
3400                                 
3401
3402                         case Msg.WM_SYSCOLORCHANGE: {
3403                                 ThemeEngine.Current.ResetDefaults();
3404                                 OnSystemColorsChanged(EventArgs.Empty);
3405                                 return;
3406                         }
3407                                 
3408
3409                         case Msg.WM_SETCURSOR: {
3410                                 if (cursor == null) {
3411                                         DefWndProc(ref m);
3412                                         return;
3413                                 }
3414
3415                                 XplatUI.SetCursor(window.Handle, cursor.handle);
3416                                 m.Result = (IntPtr)1;
3417                                 return;
3418                         }
3419
3420                         default:
3421                                 DefWndProc(ref m);      
3422                                 return;
3423                         }
3424                 }
3425                 #endregion      // Public Instance Methods
3426
3427                 #region OnXXX methods
3428                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3429                 protected virtual void OnBackColorChanged(EventArgs e) {
3430                         if (BackColorChanged!=null) BackColorChanged(this, e);
3431                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackColorChanged(e);
3432                 }
3433
3434                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3435                 protected virtual void OnBackgroundImageChanged(EventArgs e) {
3436                         if (BackgroundImageChanged!=null) BackgroundImageChanged(this, e);
3437                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackgroundImageChanged(e);
3438                 }
3439
3440                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3441                 protected virtual void OnBindingContextChanged(EventArgs e) {
3442                         if (BindingContextChanged!=null) BindingContextChanged(this, e);
3443                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBindingContextChanged(e);
3444                 }
3445
3446                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3447                 protected virtual void OnCausesValidationChanged(EventArgs e) {
3448                         if (CausesValidationChanged!=null) CausesValidationChanged(this, e);
3449                 }
3450
3451                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3452                 protected virtual void OnChangeUICues(UICuesEventArgs e) {
3453                         if (ChangeUICues!=null) ChangeUICues(this, e);
3454                 }
3455
3456                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3457                 protected virtual void OnClick(EventArgs e) {
3458                         if (Click!=null) Click(this, e);
3459                 }
3460
3461                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3462                 protected virtual void OnContextMenuChanged(EventArgs e) {
3463                         if (ContextMenuChanged!=null) ContextMenuChanged(this, e);
3464                 }
3465
3466                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3467                 protected virtual void OnControlAdded(ControlEventArgs e) {
3468                         if (ControlAdded!=null) ControlAdded(this, e);
3469                 }
3470
3471                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3472                 protected virtual void OnControlRemoved(ControlEventArgs e) {
3473                         if (ControlRemoved!=null) ControlRemoved(this, e);
3474                 }
3475
3476                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3477                 protected virtual void OnCreateControl() {
3478                         // Override me!
3479                 }
3480
3481                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3482                 protected virtual void OnCursorChanged(EventArgs e) {
3483                         if (CursorChanged!=null) CursorChanged(this, e);
3484                 }
3485
3486                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3487                 protected virtual void OnDockChanged(EventArgs e) {
3488                         if (DockChanged!=null) DockChanged(this, e);
3489                 }
3490
3491                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3492                 protected virtual void OnDoubleClick(EventArgs e) {
3493                         if (DoubleClick!=null) DoubleClick(this, e);
3494                 }
3495
3496                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3497                 protected virtual void OnDragDrop(DragEventArgs drgevent) {
3498                         if (DragDrop!=null) DragDrop(this, drgevent);
3499                 }
3500
3501                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3502                 protected virtual void OnDragEnter(DragEventArgs drgevent) {
3503                         if (DragEnter!=null) DragEnter(this, drgevent);
3504                 }
3505
3506                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3507                 protected virtual void OnDragLeave(EventArgs e) {
3508                         if (DragLeave!=null) DragLeave(this, e);
3509                 }
3510
3511                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3512                 protected virtual void OnDragOver(DragEventArgs drgevent) {
3513                         if (DragOver!=null) DragOver(this, drgevent);
3514                 }
3515
3516                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3517                 protected virtual void OnEnabledChanged(EventArgs e) {
3518                         if (EnabledChanged!=null) EnabledChanged(this, e);
3519                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentEnabledChanged(e);
3520                 }
3521
3522                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3523                 protected virtual void OnEnter(EventArgs e) {
3524                         if (Enter!=null) Enter(this, e);
3525                 }
3526
3527                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3528                 protected virtual void OnFontChanged(EventArgs e) {
3529                         if (FontChanged!=null) FontChanged(this, e);
3530                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentFontChanged(e);
3531                 }
3532
3533                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3534                 protected virtual void OnForeColorChanged(EventArgs e) {
3535                         if (ForeColorChanged!=null) ForeColorChanged(this, e);
3536                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentForeColorChanged(e);
3537                 }
3538
3539                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3540                 protected virtual void OnGiveFeedback(GiveFeedbackEventArgs gfbevent) {
3541                         if (GiveFeedback!=null) GiveFeedback(this, gfbevent);
3542                 }
3543                 
3544                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3545                 protected virtual void OnGotFocus(EventArgs e) {
3546                         if (GotFocus!=null) GotFocus(this, e);
3547                 }
3548
3549                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3550                 protected virtual void OnHandleCreated(EventArgs e) {
3551                         if (HandleCreated!=null) HandleCreated(this, e);
3552                 }
3553
3554                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3555                 protected virtual void OnHandleDestroyed(EventArgs e) {
3556                         if (HandleDestroyed!=null) HandleDestroyed(this, e);
3557                 }
3558
3559                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3560                 protected virtual void OnHelpRequested(HelpEventArgs hevent) {
3561                         if (HelpRequested!=null) HelpRequested(this, hevent);
3562                 }
3563
3564                 protected virtual void OnImeModeChanged(EventArgs e) {
3565                         if (ImeModeChanged!=null) ImeModeChanged(this, e);
3566                 }
3567
3568                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3569                 protected virtual void OnInvalidated(InvalidateEventArgs e) {
3570                         if (Invalidated!=null) Invalidated(this, e);
3571                 }
3572
3573                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3574                 protected virtual void OnKeyDown(KeyEventArgs e) {                      
3575                         if (KeyDown!=null) KeyDown(this, e);
3576                 }
3577
3578                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3579                 protected virtual void OnKeyPress(KeyPressEventArgs e) {
3580                         if (KeyPress!=null) KeyPress(this, e);
3581                 }
3582
3583                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3584                 protected virtual void OnKeyUp(KeyEventArgs e) {
3585                         if (KeyUp!=null) KeyUp(this, e);
3586                 }
3587
3588                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3589                 protected virtual void OnLayout(LayoutEventArgs levent) {
3590                         if (Layout!=null) Layout(this, levent);
3591                 }
3592
3593                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3594                 protected virtual void OnLeave(EventArgs e) {
3595                         if (Leave!=null) Leave(this, e);
3596                 }
3597
3598                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3599                 protected virtual void OnLocationChanged(EventArgs e) {
3600                         OnMove(e);
3601                         if (LocationChanged!=null) LocationChanged(this, e);
3602                         if (this is Form.FormParentWindow) {
3603                                 Form    form;
3604
3605                                 form = ((Form.FormParentWindow)this).owner;
3606
3607                                 if (form.LocationChanged != null) {
3608                                         form.LocationChanged(form, e);
3609                                 }
3610                         }
3611                 }
3612
3613                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3614                 protected virtual void OnLostFocus(EventArgs e) {
3615                         if (LostFocus!=null) LostFocus(this, e);
3616                 }
3617
3618                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3619                 protected virtual void OnMouseDown(MouseEventArgs e) {
3620                         if (MouseDown!=null) MouseDown(this, e);
3621                 }
3622
3623                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3624                 protected virtual void OnMouseEnter(EventArgs e) {
3625                         if (MouseEnter!=null) MouseEnter(this, e);
3626                 }
3627
3628                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3629                 protected virtual void OnMouseHover(EventArgs e) {
3630                         if (MouseHover!=null) MouseHover(this, e);
3631                 }
3632
3633                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3634                 protected virtual void OnMouseLeave(EventArgs e) {
3635                         if (MouseLeave!=null) MouseLeave(this, e);
3636                 }
3637
3638                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3639                 protected virtual void OnMouseMove(MouseEventArgs e) {                  
3640                         if (MouseMove!=null) MouseMove(this, e);
3641                 }
3642
3643                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3644                 protected virtual void OnMouseUp(MouseEventArgs e) {
3645                         if (MouseUp!=null) MouseUp(this, e);
3646                 }
3647
3648                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3649                 protected virtual void OnMouseWheel(MouseEventArgs e) {
3650                         if (MouseWheel!=null) MouseWheel(this, e);
3651                 }
3652
3653                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3654                 protected virtual void OnMove(EventArgs e) {
3655                         if (Move!=null) Move(this, e);
3656                 }
3657
3658                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3659                 protected virtual void OnNotifyMessage(Message m) {
3660                         // Override me!
3661                 }
3662
3663                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3664                 protected virtual void OnPaint(PaintEventArgs e) {
3665                         if (Paint!=null) Paint(this, e);
3666                 }
3667
3668                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3669                 protected virtual void OnPaintBackground(PaintEventArgs pevent) {
3670                         // Override me!
3671                 }
3672
3673                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3674                 protected virtual void OnParentBackColorChanged(EventArgs e) {
3675                         if (background_color.IsEmpty && background_image==null) {
3676                                 Invalidate();
3677                                 OnBackColorChanged(e);
3678                         }
3679                 }
3680
3681                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3682                 protected virtual void OnParentBackgroundImageChanged(EventArgs e) {
3683                         if (background_color.IsEmpty && background_image==null) {
3684                                 Invalidate();
3685                                 OnBackgroundImageChanged(e);
3686                         }
3687                 }
3688
3689                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3690                 protected virtual void OnParentBindingContextChanged(EventArgs e) {
3691                         if (binding_context==null) {
3692                                 binding_context=Parent.binding_context;
3693                                 OnBindingContextChanged(e);
3694                         }
3695                 }
3696
3697                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3698                 protected virtual void OnParentChanged(EventArgs e) {
3699                         if (ParentChanged!=null) ParentChanged(this, e);
3700                 }
3701
3702                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3703                 protected virtual void OnParentEnabledChanged(EventArgs e) {
3704                         if (is_enabled != Parent.is_enabled) {
3705                                 is_enabled=Parent.is_enabled;
3706                                 Invalidate();
3707                                 if (EnabledChanged != null) {
3708                                         EnabledChanged(this, e);
3709                                 }
3710                         }
3711                 }
3712
3713                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3714                 protected virtual void OnParentFontChanged(EventArgs e) {
3715                         if (font==null) {
3716                                 Invalidate();
3717                                 OnFontChanged(e);
3718                         }
3719                 }
3720
3721                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3722                 protected virtual void OnParentForeColorChanged(EventArgs e) {
3723                         if (foreground_color.IsEmpty) {
3724                                 Invalidate();
3725                                 OnForeColorChanged(e);
3726                         }
3727                 }
3728
3729                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3730                 protected virtual void OnParentRightToLeftChanged(EventArgs e) {
3731                         if (right_to_left==RightToLeft.Inherit) {
3732                                 Invalidate();
3733                                 OnRightToLeftChanged(e);
3734                         }
3735                 }
3736
3737                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3738                 protected virtual void OnParentVisibleChanged(EventArgs e) {
3739                         if (is_visible!=Parent.is_visible) {
3740                                 is_visible=false;
3741                                 Invalidate();
3742                                 OnVisibleChanged(e);
3743                         }
3744                 }
3745
3746                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3747                 protected virtual void OnQueryContinueDrag(QueryContinueDragEventArgs e) {
3748                         if (QueryContinueDrag!=null) QueryContinueDrag(this, e);
3749                 }
3750
3751                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3752                 protected virtual void OnResize(EventArgs e) {
3753                         if (Resize!=null) Resize(this, e);
3754
3755                         PerformLayout(this, "bounds");
3756
3757                         if (parent != null) {
3758                                 parent.PerformLayout();
3759                         }
3760                 }
3761
3762                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3763                 protected virtual void OnRightToLeftChanged(EventArgs e) {
3764                         if (RightToLeftChanged!=null) RightToLeftChanged(this, e);
3765                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentRightToLeftChanged(e);
3766                 }
3767
3768                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3769                 protected virtual void OnSizeChanged(EventArgs e) {
3770                         InvalidateBuffers ();
3771                         OnResize(e);
3772                         if (SizeChanged!=null) SizeChanged(this, e);
3773                 }
3774
3775                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3776                 protected virtual void OnStyleChanged(EventArgs e) {
3777                         if (StyleChanged!=null) StyleChanged(this, e);
3778                 }
3779
3780                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3781                 protected virtual void OnSystemColorsChanged(EventArgs e) {
3782                         if (SystemColorsChanged!=null) SystemColorsChanged(this, e);
3783                 }
3784
3785                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3786                 protected virtual void OnTabIndexChanged(EventArgs e) {
3787                         if (TabIndexChanged!=null) TabIndexChanged(this, e);
3788                 }
3789
3790                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3791                 protected virtual void OnTabStopChanged(EventArgs e) {
3792                         if (TabStopChanged!=null) TabStopChanged(this, e);
3793                 }
3794
3795                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3796                 protected virtual void OnTextChanged(EventArgs e) {
3797                         if (TextChanged!=null) TextChanged(this, e);
3798                 }
3799
3800                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3801                 protected virtual void OnValidated(EventArgs e) {
3802                         if (Validated!=null) Validated(this, e);
3803                 }
3804
3805                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3806                 protected virtual void OnValidating(System.ComponentModel.CancelEventArgs e) {
3807                         if (Validating!=null) Validating(this, e);
3808                 }
3809
3810                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3811                 protected virtual void OnVisibleChanged(EventArgs e) {
3812                         if (!is_visible) {
3813                                 if (dc_mem!=null) {
3814                                         dc_mem.Dispose ();
3815                                         bmp_mem=null;
3816                                 }
3817
3818                                 if (bmp_mem!=null) {
3819                                         bmp_mem.Dispose();
3820                                         bmp_mem=null;
3821                                 }
3822                         } else {
3823                                 if (!is_disposed) {
3824                                         if (!this.IsHandleCreated) {
3825                                                 this.CreateHandle();
3826                                         }
3827                                         PerformLayout();
3828                                 }
3829                         }
3830                         
3831                         if (VisibleChanged!=null) VisibleChanged(this, e);
3832 //hack start
3833                         if (this is Form.FormParentWindow) {
3834                                 Form    form;
3835
3836                                 form = ((Form.FormParentWindow)this).owner;
3837
3838                                 form.OnVisibleChanged(e);
3839                         }
3840 // hack end
3841                         // We need to tell our kids
3842                         for (int i=0; i<child_controls.Count; i++) {
3843                                 child_controls[i].OnParentVisibleChanged(e);
3844                         }
3845                 }
3846                 #endregion      // OnXXX methods
3847
3848                 #region Events
3849                 public event EventHandler               BackColorChanged;
3850                 public event EventHandler               BackgroundImageChanged;
3851                 public event EventHandler               BindingContextChanged;
3852                 public event EventHandler               CausesValidationChanged;
3853                 public event UICuesEventHandler         ChangeUICues;
3854                 public event EventHandler               Click;
3855                 public event EventHandler               ContextMenuChanged;
3856
3857                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3858                 [Browsable(false)]
3859                 public event ControlEventHandler        ControlAdded;
3860
3861                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3862                 [Browsable(false)]
3863                 public event ControlEventHandler        ControlRemoved;
3864
3865                 public event EventHandler               CursorChanged;
3866                 public event EventHandler               DockChanged;
3867                 public event EventHandler               DoubleClick;
3868                 public event DragEventHandler           DragDrop;
3869                 public event DragEventHandler           DragEnter;
3870                 public event EventHandler               DragLeave;
3871                 public event DragEventHandler           DragOver;
3872                 public event EventHandler               EnabledChanged;
3873                 public event EventHandler               Enter;
3874                 public event EventHandler               FontChanged;
3875                 public event EventHandler               ForeColorChanged;
3876                 public event GiveFeedbackEventHandler   GiveFeedback;
3877
3878                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3879                 [Browsable(false)]
3880                 public event EventHandler               GotFocus;
3881
3882                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3883                 [Browsable(false)]
3884                 public event EventHandler               HandleCreated;
3885
3886                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3887                 [Browsable(false)]
3888                 public event EventHandler               HandleDestroyed;
3889
3890                 public event HelpEventHandler           HelpRequested;
3891                 public event EventHandler               ImeModeChanged;
3892
3893                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3894                 [Browsable(false)]
3895                 public event InvalidateEventHandler     Invalidated;
3896
3897                 public event KeyEventHandler            KeyDown;
3898                 public event KeyPressEventHandler       KeyPress;
3899                 public event KeyEventHandler            KeyUp;
3900                 public event LayoutEventHandler         Layout;
3901                 public event EventHandler               Leave;
3902                 public event EventHandler               LocationChanged;
3903
3904                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3905                 [Browsable(false)]
3906                 public event EventHandler               LostFocus;
3907
3908                 public event MouseEventHandler          MouseDown;
3909                 public event EventHandler               MouseEnter;
3910                 public event EventHandler               MouseHover;
3911                 public event EventHandler               MouseLeave;
3912                 public event MouseEventHandler          MouseMove;
3913                 public event MouseEventHandler          MouseUp;
3914
3915                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3916                 [Browsable(false)]
3917                 public event MouseEventHandler          MouseWheel;
3918
3919                 public event EventHandler               Move;
3920                 public event PaintEventHandler          Paint;
3921                 public event EventHandler               ParentChanged;
3922                 public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp;
3923                 public event QueryContinueDragEventHandler      QueryContinueDrag;
3924                 public event EventHandler               Resize;
3925                 public event EventHandler               RightToLeftChanged;
3926                 public event EventHandler               SizeChanged;
3927                 public event EventHandler               StyleChanged;
3928                 public event EventHandler               SystemColorsChanged;
3929                 public event EventHandler               TabIndexChanged;
3930                 public event EventHandler               TabStopChanged;
3931                 public event EventHandler               TextChanged;
3932                 public event EventHandler               Validated;
3933                 public event CancelEventHandler         Validating;
3934                 public event EventHandler               VisibleChanged;
3935                 #endregion      // Events
3936         }
3937 }