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