- Forgot a cast
[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                 private 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) {
2105                                 XplatUI.SetFocus(window.Handle);
2106                         }
2107                         return true;
2108                 }
2109
2110                 public Control GetChildAtPoint(Point pt) {
2111                         // Microsoft's version of this function doesn't seem to work, so I can't check
2112                         // if we only consider children or also grandchildren, etc.
2113                         // I'm gonna say 'children only'
2114                         for (int i=0; i<child_controls.Count; i++) {
2115                                 if (child_controls[i].Bounds.Contains(pt)) {
2116                                         return child_controls[i];
2117                                 }
2118                         }
2119                         return null;
2120                 }
2121
2122                 public IContainerControl GetContainerControl() {
2123                         Control current = this;
2124
2125                         while (current!=null) {
2126                                 if ((current is IContainerControl) && ((current.control_style & ControlStyles.ContainerControl)!=0)) {
2127                                         return (IContainerControl)current;
2128                                 }
2129                                 current = current.parent;
2130                         }
2131                         return null;
2132                 }
2133
2134                 public Control GetNextControl(Control ctl, bool forward) {
2135                         // If we're not a container we don't play
2136                         if (!(this is IContainerControl) && !this.GetStyle(ControlStyles.ContainerControl)) {
2137                                 return null;
2138                         }
2139
2140                         // If ctl is not contained by this, we start at the first child of this
2141                         if (!this.Contains(ctl)) {
2142                                 ctl = null;
2143                         }
2144
2145                         // Search through our controls, starting at ctl, stepping into children as we encounter them
2146                         // try to find the control with the tabindex closest to our own, or, if we're looking into
2147                         // child controls, the one with the smallest tabindex
2148                         if (forward) {
2149                                 return FindControlForward(this, ctl);
2150                         }
2151                         return FindControlBackward(this, ctl);
2152                 }
2153
2154                 public void Hide() {
2155                         this.Visible = false;
2156                 }
2157
2158                 public void Invalidate() {
2159                         Invalidate(ClientRectangle, false);
2160                 }
2161
2162                 public void Invalidate(bool invalidateChildren) {
2163                         Invalidate(ClientRectangle, invalidateChildren);
2164                 }
2165
2166                 public void Invalidate(System.Drawing.Rectangle rc) {
2167                         Invalidate(rc, false);
2168                 }
2169
2170                 public void Invalidate(System.Drawing.Rectangle rc, bool invalidateChildren) {
2171                         if (!IsHandleCreated || !Visible) {
2172                                 return;
2173                         }
2174
2175                         NotifyInvalidate(rc);
2176
2177                         XplatUI.Invalidate(Handle, rc, !GetStyle (ControlStyles.AllPaintingInWmPaint));
2178
2179                         if (invalidateChildren) {
2180                                 for (int i=0; i<child_controls.Count; i++) child_controls[i].Invalidate();
2181                         }
2182                         OnInvalidated(new InvalidateEventArgs(rc));
2183                 }
2184
2185                 public void Invalidate(System.Drawing.Region region) {
2186                         Invalidate(region, false);
2187                 }
2188
2189                 [MonoTODO("Figure out if GetRegionScans is usable")]
2190                 public void Invalidate(System.Drawing.Region region, bool invalidateChildren) {
2191                         throw new NotImplementedException();
2192
2193                         // FIXME - should use the GetRegionScans function of the region to invalidate each area
2194                         //if (invalidateChildren) {
2195                         //      for (int i=0; i<child_controls.Count; i++) child_controls[i].Invalidate();
2196                         //}
2197                 }
2198
2199                 public object Invoke (Delegate method) {
2200                         return Invoke(method, null);
2201                 }
2202
2203                 public object Invoke (Delegate method, object[] args) {
2204                         IAsyncResult result = BeginInvoke (method, args);
2205                         return EndInvoke(result);
2206                 }
2207
2208                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2209                 public void PerformLayout() {
2210                         PerformLayout(null, null);
2211                 }
2212
2213                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2214                 public void PerformLayout(Control affectedControl, string affectedProperty) {
2215                         LayoutEventArgs levent = new LayoutEventArgs(affectedControl, affectedProperty);
2216
2217                         if (layout_suspended>0) {
2218                                 layout_pending = true;
2219                                 return;
2220                         }
2221
2222                         layout_pending = false;
2223
2224                         // Prevent us from getting messed up
2225                         layout_suspended++;
2226
2227                         // Perform all Dock and Anchor calculations
2228                         try {
2229                                 Control         child;
2230                                 AnchorStyles    anchor;
2231                                 Rectangle       space;
2232                                 int             diff_width;
2233                                 int             diff_height;
2234
2235                                 space=this.DisplayRectangle;
2236                                 if (prev_size != Size.Empty) {
2237                                         diff_width = space.Width - prev_size.Width;
2238                                         diff_height = space.Height - prev_size.Height;
2239                                 } else {
2240                                         diff_width = 0;
2241                                         diff_height = 0;
2242                                 }
2243
2244                                 // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
2245                                 for (int i = child_controls.Count - 1; i >= 0; i--) {
2246                                         child=child_controls[i];
2247                                         switch (child.Dock) {
2248                                         case DockStyle.None: {
2249                                                 // Do nothing
2250                                                 break;
2251                                         }
2252
2253                                         case DockStyle.Left: {
2254                                                 child.SetBounds(space.Left, space.Y, child.Width, space.Height);
2255                                                 space.X+=child.Width;
2256                                                 space.Width-=child.Width;
2257                                                 break;
2258                                         }
2259
2260                                         case DockStyle.Top: {
2261                                                 child.SetBounds(space.Left, space.Y, space.Width, child.Height);
2262                                                 space.Y+=child.Height;
2263                                                 space.Height-=child.Height;
2264                                                 break;
2265                                         }
2266                                 
2267                                         case DockStyle.Right: {
2268                                                 child.SetBounds(space.Right-child.Width, space.Y, child.Width, space.Height);
2269                                                 space.Width-=child.Width;
2270                                                 break;
2271                                         }
2272
2273                                         case DockStyle.Bottom: {
2274                                                 child.SetBounds(space.Left, space.Bottom-child.Height, space.Width, child.Height);
2275                                                 space.Height-=child.Height;
2276                                                 break;
2277                                         }
2278
2279                                         case DockStyle.Fill: {
2280                                                 child.SetBounds(space.Left, space.Top, space.Width, space.Height);
2281                                                 space.Width=0;
2282                                                 space.Height=0;
2283                                                 break;
2284                                         }
2285                                         }
2286                                 }
2287
2288                                 space=this.DisplayRectangle;
2289
2290                                 // Deal with anchoring
2291                                 for (int i=0; i < child_controls.Count; i++) {
2292                                         int left;
2293                                         int top;
2294                                         int width;
2295                                         int height;
2296
2297                                         child=child_controls[i];
2298                                         anchor=child.Anchor;
2299
2300                                         left=child.Left;
2301                                         top=child.Top;
2302                                         width=child.Width;
2303                                         height=child.Height;
2304
2305                                         // If the control is docked we don't need to do anything
2306                                         if (child.Dock != DockStyle.None) {
2307                                                 continue;
2308                                         }
2309
2310                                         if ((anchor & AnchorStyles.Left) !=0 ) {
2311                                                 if ((anchor & AnchorStyles.Right) != 0) {
2312                                                         // Anchoring to left and right
2313                                                         width=width+diff_width;
2314                                                 } else {
2315                                                         ; // nothing to do
2316                                                 }
2317                                         } else if ((anchor & AnchorStyles.Right) != 0) {
2318                                                 left+=diff_width;
2319                                         } else {
2320                                                 left+=diff_width/2;
2321                                         }
2322
2323                                         if ((anchor & AnchorStyles.Top) !=0 ) {
2324                                                 if ((anchor & AnchorStyles.Bottom) != 0) {
2325                                                         height+=diff_height;
2326                                                 } else {
2327                                                         ; // nothing to do
2328                                                 }
2329                                         } else if ((anchor & AnchorStyles.Bottom) != 0) {
2330                                                 top+=diff_height;
2331                                         } else {
2332                                                 top+=diff_height/2;
2333                                         }
2334
2335                                         // Sanity
2336                                         if (width < 0) {
2337                                                 width=0;
2338                                         }
2339
2340                                         if (height < 0) {
2341                                                 height=0;
2342                                         }
2343
2344                                         child.SetBounds(left, top, width, height);
2345                                 }
2346
2347                                 // Let everyone know
2348                                 OnLayout(levent);
2349                         }
2350
2351                                 // Need to make sure we decremend layout_suspended
2352                         finally {
2353                                 layout_suspended--;
2354                         }
2355                 }
2356
2357                 public Point PointToClient (Point p) {
2358                         int x = p.X;
2359                         int y = p.Y;
2360
2361                         XplatUI.ScreenToClient (Handle, ref x, ref y);
2362
2363                         return new Point (x, y);
2364                 }
2365
2366                 public Point PointToScreen(Point p) {
2367                         int x = p.X;
2368                         int y = p.Y;
2369
2370                         XplatUI.ClientToScreen(Handle, ref x, ref y);
2371
2372                         return new Point(x, y);
2373                 }
2374
2375                 public virtual bool PreProcessMessage(ref Message msg) {
2376                         Keys key_data;
2377
2378                         if (msg.Msg == (int)Msg.WM_KEYDOWN) {
2379                                 key_data = (Keys)msg.WParam.ToInt32();
2380                                 if (!ProcessCmdKey(ref msg, key_data)) {
2381                                         if (IsInputKey(key_data)) {
2382                                                 return false;
2383                                         }
2384
2385                                         return ProcessDialogKey(key_data);
2386                                 }
2387
2388                                 return true;
2389                         } else if (msg.Msg == (int)Msg.WM_CHAR) {
2390                                 if (IsInputChar((char)msg.WParam)) {
2391                                         return false;
2392                                 }
2393
2394                                 return ProcessDialogChar((char)msg.WParam);
2395                         }
2396
2397                         return false;
2398                 }
2399
2400                 public Rectangle RectangleToClient(Rectangle r) {
2401                         return new Rectangle(PointToClient(r.Location), r.Size);
2402                 }
2403
2404                 public Rectangle RectangleToScreen(Rectangle r) {
2405                         return new Rectangle(PointToScreen(r.Location), r.Size);
2406                 }
2407
2408                 public virtual void Refresh() {                 
2409                         if (IsHandleCreated == true) {
2410                                 XplatUI.RefreshWindow(window.Handle);
2411                         }
2412                 }
2413
2414                 [EditorBrowsable(EditorBrowsableState.Never)]
2415                 public virtual void ResetBackColor() {
2416                         background_color = Color.Empty;
2417                 }
2418
2419 #if haveDataBindings
2420                 [EditorBrowsable(EditorBrowsableState.Never)]
2421                 [MonoTODO]
2422                 public void ResetBindings() {
2423                         // Do something
2424                 }
2425 #endif
2426
2427                 [EditorBrowsable(EditorBrowsableState.Never)]
2428                 public virtual void ResetCursor() {
2429                         cursor = null;
2430                 }
2431
2432                 [EditorBrowsable(EditorBrowsableState.Never)]
2433                 public virtual void ResetFont() {
2434                         font = null;
2435                 }
2436
2437                 [EditorBrowsable(EditorBrowsableState.Never)]
2438                 public virtual void ResetForeColor() {
2439                         foreground_color = Color.Empty;
2440                 }
2441
2442                 [EditorBrowsable(EditorBrowsableState.Never)]
2443                 public void ResetImeMode() {
2444                         ime_mode = DefaultImeMode;
2445                 }
2446
2447                 [EditorBrowsable(EditorBrowsableState.Never)]
2448                 public virtual void ResetRightToLeft() {
2449                         right_to_left = RightToLeft.Inherit;
2450                 }
2451
2452                 public virtual void ResetText() {
2453                         text = null;
2454                 }
2455
2456                 public void ResumeLayout() {
2457                         ResumeLayout (true);
2458                 }
2459
2460                 public void ResumeLayout(bool performLayout) {
2461                         layout_suspended--;
2462                         
2463                         if (layout_suspended > 0) {
2464                                 return;
2465                         }
2466
2467                         if (performLayout || layout_pending) {
2468                                 PerformLayout();
2469                         }
2470                 }
2471
2472                 public void Scale(float ratio) {
2473                         ScaleCore(ratio, ratio);
2474                 }
2475
2476                 public void Scale(float dx, float dy) {
2477                         ScaleCore(dx, dy);
2478                 }
2479
2480                 public void Select() {
2481                         Select(false, false);
2482                 }
2483
2484                 public bool SelectNextControl(Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) {
2485                         Control c;
2486                                 
2487                         c = ctl;
2488                         do {
2489                                 c = GetNextControl(c, forward);
2490                                 if (c == null) {
2491                                         if (wrap) {
2492                                                 wrap = false;
2493                                                 continue;
2494                                         }
2495                                         break;
2496                                 }
2497
2498                                 if (c.CanSelect && ((c.parent == this) || nested) && (c.tab_stop || !tabStopOnly)) {
2499                                         Select(c);
2500                                         return true;
2501                                 }
2502                         } while (c != ctl);     // If we wrap back to ourselves we stop
2503
2504                         return false;
2505                 }
2506
2507                 public void SendToBack() {
2508                         if ((parent != null) && (parent.child_controls[parent.child_controls.Count-1]!=this)) {
2509                                 if (parent.child_controls.Contains(this)) {
2510                                         parent.child_controls.SetChildIndex(this, parent.child_controls.Count);
2511                                 }
2512                         }
2513
2514                         XplatUI.SetZOrder(this.window.Handle, IntPtr.Zero, false, true);
2515                         if (parent != null) {
2516                                 parent.Refresh();
2517                         }
2518                 }
2519
2520                 public void SetBounds(int x, int y, int width, int height) {
2521                         SetBoundsCore(x, y, width, height, BoundsSpecified.All);
2522                 }
2523
2524                 public void SetBounds(int x, int y, int width, int height, BoundsSpecified specified) {
2525                         SetBoundsCore(x, y, width, height, specified);
2526                 }
2527
2528                 public void Show() {
2529                         if (!IsHandleCreated) {
2530                                 this.CreateHandle();
2531                         }
2532
2533                         this.Visible=true;
2534                 }
2535
2536                 public void SuspendLayout() {
2537                         layout_suspended++;
2538                 }
2539
2540                 public void Update() {
2541                         XplatUI.UpdateWindow(window.Handle);
2542                 }
2543                 #endregion      // Public Instance Methods
2544
2545                 #region Protected Instance Methods
2546                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2547                 [MonoTODO("Implement this and tie it into Control.ControlAccessibleObject.NotifyClients")]
2548                 protected void AccessibilityNotifyClients(AccessibleEvents accEvent, int childID) {
2549                         throw new NotImplementedException();
2550                 }
2551
2552                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2553                 protected virtual AccessibleObject CreateAccessibilityInstance() {
2554                         return new Control.ControlAccessibleObject(this);
2555                 }
2556
2557                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2558                 protected virtual ControlCollection CreateControlsInstance() {
2559                         return new ControlCollection(this);
2560                 }
2561
2562                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2563                 protected virtual void CreateHandle() {
2564                         if (IsDisposed) {
2565                                 throw new ObjectDisposedException(Name);
2566                         }
2567
2568                         if (IsHandleCreated) {
2569                                 return;
2570                         }
2571
2572                         if (window==null) {
2573                                 window = new ControlNativeWindow(this);
2574                                 window.CreateHandle(CreateParams);
2575                         }
2576
2577                         if (window.Handle!=IntPtr.Zero) {
2578                                 if (!controls.Contains(window.Handle)) {
2579                                         controls.Add(this);
2580                                 }
2581
2582                                 creator_thread = Thread.CurrentThread;
2583
2584                                 XplatUI.SetWindowBackground(window.Handle, this.BackColor);
2585
2586                                 OnHandleCreated(EventArgs.Empty);
2587                         }
2588                 }
2589
2590                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2591                 protected virtual void DefWndProc(ref Message m) {
2592                         window.DefWndProc(ref m);
2593                 }
2594
2595                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2596                 protected virtual void DestroyHandle() {
2597                         if (IsHandleCreated) {
2598                                 if (Handle != IntPtr.Zero) {
2599                                         controls.Remove(Handle);
2600                                 }
2601
2602                                 if (window != null) {
2603                                         window.DestroyHandle();
2604                                 }
2605                         }
2606                 }
2607
2608                 protected bool GetStyle(ControlStyles flag) {
2609                         return (control_style & flag) != 0;
2610                 }
2611
2612                 protected bool GetTopLevel() {
2613                         return is_toplevel;
2614                 }
2615
2616                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2617                 protected virtual void InitLayout() {
2618                         if (parent != null) {
2619                                 parent.PerformLayout(this, "parent");
2620                         }
2621                 }
2622
2623                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2624                 protected void InvokeGotFocus(Control toInvoke, EventArgs e) {
2625                         toInvoke.OnGotFocus(e);
2626                 }
2627
2628                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2629                 protected void InvokeLostFocus(Control toInvoke, EventArgs e) {
2630                         toInvoke.OnLostFocus(e);
2631                 }
2632
2633                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2634                 protected void InvokeOnClick(Control toInvoke, EventArgs e) {
2635                         toInvoke.OnClick(e);
2636                 }
2637
2638                 protected void InvokePaint(Control toInvoke, PaintEventArgs e) {
2639                         toInvoke.OnPaint(e);
2640                 }
2641
2642                 protected void InvokePaintBackground(Control toInvoke, PaintEventArgs e) {
2643                         toInvoke.OnPaintBackground(e);
2644                 }
2645
2646                 protected virtual bool IsInputChar (char charCode) {
2647                         if (parent != null) {
2648                                 return parent.IsInputChar(charCode);
2649                         }
2650
2651                         return true;
2652                 }
2653
2654                 protected virtual bool IsInputKey (Keys keyData) {
2655                         // Doc says this one calls IsInputChar; not sure what to do with that
2656                         return false;
2657                 }
2658
2659                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2660                 protected virtual void NotifyInvalidate(Rectangle invalidatedArea) {
2661                         // override me?
2662                 }
2663
2664                 protected virtual bool ProcessCmdKey(ref Message msg, Keys keyData) {
2665                         if ((context_menu != null) && context_menu.ProcessCmdKey(ref msg, keyData)) {
2666                                 return true;
2667                         }
2668
2669                         if (parent != null) {
2670                                 return parent.ProcessCmdKey(ref msg, keyData);
2671                         }
2672
2673                         return false;
2674                 }
2675
2676                 protected virtual bool ProcessDialogChar(char charCode) {
2677                         if (parent != null) {
2678                                 return parent.ProcessDialogChar (charCode);
2679                         }
2680
2681                         return false;
2682                 }
2683
2684                 protected virtual bool ProcessDialogKey (Keys keyData) {
2685                         if (parent != null) {
2686                                 return parent.ProcessDialogKey (keyData);
2687                         }
2688
2689                         return false;
2690                 }
2691
2692                 protected virtual bool ProcessKeyEventArgs (ref Message msg)
2693                 {
2694                         KeyEventArgs            key_event;
2695
2696                         PreProcessMessage(ref msg);
2697
2698                         switch (msg.Msg) {
2699                                 case (int)Msg.WM_KEYDOWN: {
2700                                         key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
2701                                         OnKeyDown (key_event);
2702                                         return key_event.Handled;
2703                                 }
2704                                 case (int)Msg.WM_KEYUP: {
2705                                         key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
2706                                         OnKeyUp (key_event);
2707                                         return key_event.Handled;
2708                                 }
2709
2710                                 case (int)Msg.WM_CHAR: {
2711                                         KeyPressEventArgs       key_press_event;
2712
2713                                         key_press_event = new KeyPressEventArgs((char)msg.WParam);
2714                                         OnKeyPress(key_press_event);
2715                                         return key_press_event.Handled;
2716                                 }
2717
2718                                 default: {
2719                                         break;
2720                                 }
2721                         }
2722
2723                         return false;
2724                 }
2725
2726                 protected internal virtual bool ProcessKeyMessage(ref Message msg) {
2727                         if (parent != null) {
2728                                 if (parent.ProcessKeyPreview(ref msg)) {
2729                                         return true;
2730                                 }
2731                         }
2732
2733                         return ProcessKeyEventArgs(ref msg);
2734                 }
2735
2736                 protected virtual bool ProcessKeyPreview(ref Message msg) {
2737                         if (parent != null) {
2738                                 return parent.ProcessKeyPreview(ref msg);
2739                         }
2740
2741                         return false;
2742                 }
2743
2744                 protected virtual bool ProcessMnemonic(char charCode) {
2745                         // override me
2746                         return false;
2747                 }
2748
2749                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2750                 protected void RaiseDragEvent(object key, DragEventArgs e) {
2751                         // MS Internal
2752                 }
2753
2754                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2755                 protected void RaiseKeyEvent(object key, KeyEventArgs e) {
2756                         // MS Internal
2757                 }
2758
2759                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2760                 protected void RaiseMouseEvent(object key, MouseEventArgs e) {
2761                         // MS Internal
2762                 }
2763
2764                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2765                 protected void RaisePaintEvent(object key, PaintEventArgs e) {
2766                         // MS Internal
2767                 }
2768
2769                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2770                 protected void RecreateHandle() {
2771                         IEnumerator child = child_controls.GetEnumerator();
2772
2773                         is_recreating=true;
2774
2775                         if (IsHandleCreated) {
2776                                 DestroyHandle();
2777                                 CreateHandle();
2778
2779                                 // FIXME ZOrder?
2780
2781                                 while (child.MoveNext()) {
2782                                         ((Control)child.Current).RecreateHandle();
2783                                 }
2784                         } else {
2785                                 CreateHandle();
2786                         }
2787
2788                         is_recreating = false;
2789                 }
2790
2791                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2792                 protected void ResetMouseEventArgs() {
2793                         // MS Internal
2794                 }
2795
2796                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2797                 protected ContentAlignment RtlTranslateAlignment(ContentAlignment align) {
2798                         if (right_to_left == RightToLeft.No) {
2799                                 return align;
2800                         }
2801
2802                         switch (align) {
2803                                 case ContentAlignment.TopLeft: {
2804                                         return ContentAlignment.TopRight;
2805                                 }
2806
2807                                 case ContentAlignment.TopRight: {
2808                                         return ContentAlignment.TopLeft;
2809                                 }
2810
2811                                 case ContentAlignment.MiddleLeft: {
2812                                         return ContentAlignment.MiddleRight;
2813                                 }
2814
2815                                 case ContentAlignment.MiddleRight: {
2816                                         return ContentAlignment.MiddleLeft;
2817                                 }
2818
2819                                 case ContentAlignment.BottomLeft: {
2820                                         return ContentAlignment.BottomRight;
2821                                 }
2822
2823                                 case ContentAlignment.BottomRight: {
2824                                         return ContentAlignment.BottomLeft;
2825                                 }
2826
2827                                 default: {
2828                                         // if it's center it doesn't change
2829                                         return align;
2830                                 }
2831                         }
2832                 }
2833
2834                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2835                 protected HorizontalAlignment RtlTranslateAlignment(HorizontalAlignment align) {
2836                         if ((right_to_left == RightToLeft.No) || (align == HorizontalAlignment.Center)) {
2837                                 return align;
2838                         }
2839
2840                         if (align == HorizontalAlignment.Left) {
2841                                 return HorizontalAlignment.Right;
2842                         }
2843
2844                         // align must be HorizontalAlignment.Right
2845                         return HorizontalAlignment.Left;
2846                 }
2847
2848                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2849                 protected LeftRightAlignment RtlTranslateAlignment(LeftRightAlignment align) {
2850                         if (right_to_left == RightToLeft.No) {
2851                                 return align;
2852                         }
2853
2854                         if (align == LeftRightAlignment.Left) {
2855                                 return LeftRightAlignment.Right;
2856                         }
2857
2858                         // align must be LeftRightAlignment.Right;
2859                         return LeftRightAlignment.Left;
2860                 }
2861
2862                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2863                 protected ContentAlignment RtlTranslateContent(ContentAlignment align) {
2864                         return RtlTranslateAlignment(align);
2865                 }
2866
2867                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2868                 protected HorizontalAlignment RtlTranslateHorizontal(HorizontalAlignment align) {
2869                         return RtlTranslateAlignment(align);
2870                 }
2871
2872                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2873                 protected LeftRightAlignment RtlTranslateLeftRight(LeftRightAlignment align) {
2874                         return RtlTranslateAlignment(align);
2875                 }
2876
2877                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2878                 protected virtual void ScaleCore(float dx, float dy) {
2879                         Point   location;
2880                         Size    size;
2881
2882                         SuspendLayout();
2883
2884                         location = new Point((int)(Left * dx), (int)(Top * dy));
2885                         size = this.ClientSize;
2886                         
2887
2888                         if (!GetStyle(ControlStyles.FixedWidth)) {
2889                                 size.Width = (int)(size.Width * dx);
2890                         }
2891
2892                         if (!GetStyle(ControlStyles.FixedHeight)) {
2893                                 size.Height = (int)(size.Height * dy);
2894                         }
2895
2896                         Location = location;
2897                         ClientSize = size;
2898
2899                         /* Now scale our children */
2900                         for (int i=0; i < child_controls.Count; i++) {
2901                                 child_controls[i].Scale(dx, dy);
2902                         }
2903
2904                         ResumeLayout();
2905                 }
2906
2907                 protected virtual void Select(bool directed, bool forward) {
2908                         int     index;
2909                         bool    result;
2910
2911                         if (!directed) {
2912                                 // Select this control
2913                                 Select(this);
2914                                 return;
2915                         }
2916
2917                         if (parent == null) {
2918                                 return;
2919                         }
2920
2921                         index = parent.child_controls.IndexOf(this);
2922                         result = false;
2923
2924                         do {
2925                                 if (forward) {
2926                                         if ((index+1) < parent.child_controls.Count) {
2927                                                 index++;
2928                                         } else {
2929                                                 index = 0;
2930                                         }
2931                                 } else {
2932                                         if (index>0) {
2933                                                 index++;
2934                                         } else {
2935                                                 index = parent.child_controls.Count-1;
2936                                         }
2937                                 }
2938                                 result = Select(parent.child_controls[index]);
2939                         } while (!result && parent.child_controls[index] != this);
2940                 }
2941
2942                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2943                 protected virtual void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
2944                         // SetBoundsCore updates the Win32 control itself. UpdateBounds updates the controls variables and fires events, I'm guessing - pdb
2945                         if ((specified & BoundsSpecified.X) != BoundsSpecified.X) {
2946                                 x = Left;
2947                         }
2948
2949                         if ((specified & BoundsSpecified.Y) != BoundsSpecified.Y) {
2950                                 y = Top;
2951                         }
2952
2953                         if ((specified & BoundsSpecified.Width)!= BoundsSpecified.Width) {
2954                                 width = Width;
2955                         }
2956
2957                         if ((specified & BoundsSpecified.Height) != BoundsSpecified.Height) {
2958                                 height = Height;
2959                         }
2960
2961                         if (IsHandleCreated) {
2962                                 XplatUI.SetWindowPos(Handle, x, y, width, height);
2963                         }
2964                         UpdateBounds(x, y, width, height);
2965                 }
2966
2967                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2968                 protected virtual void SetClientSizeCore(int x, int y) {
2969                         // Calculate the actual window size from the client size (it usually stays the same or grows)
2970                         Rectangle       ClientRect;
2971                         Rectangle       WindowRect;
2972                         CreateParams    cp;
2973
2974                         ClientRect = new Rectangle(0, 0, x, y);
2975                         cp = this.CreateParams;
2976
2977                         if (XplatUI.CalculateWindowRect(Handle, ref ClientRect, cp.Style, false, out WindowRect)==false) {
2978                                 return;
2979                         }
2980
2981                         SetBoundsCore(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
2982                 }
2983
2984                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2985                 protected void SetStyle(ControlStyles flag, bool value) {
2986                         if (value) {
2987                                 control_style |= flag;
2988                         } else {
2989                                 control_style &= ~flag;
2990                         }
2991                 }
2992
2993                 protected void SetTopLevel(bool value) {
2994                         if ((GetTopLevel() != value) && (parent != null)) {
2995                                 throw new Exception();
2996                         }
2997
2998                         if (this is Form) {
2999                                 if (value == true) {
3000                                         if (!Visible) {
3001                                                 Visible = true;
3002                                         }
3003                                 } else {
3004                                         if (Visible) {
3005                                                 Visible = false;
3006                                         }
3007                                 }
3008                         }
3009                         is_toplevel = value;
3010                 }
3011
3012                 protected virtual void SetVisibleCore(bool value) {
3013                         if (value!=is_visible) {
3014                                 is_visible=value;
3015                                 XplatUI.SetVisible(Handle, value);
3016                                 OnVisibleChanged(EventArgs.Empty);
3017                                 if (!is_visible) {
3018                                         if (dc_mem != null) {
3019                                                 dc_mem.Dispose();
3020                                                 dc_mem = null;
3021                                         }
3022
3023                                         if (bmp_mem != null) {
3024                                                 bmp_mem.Dispose();
3025                                                 bmp_mem = null;
3026                                         }
3027                                 } else {
3028                                         this.CreateBuffers(bounds.Width, bounds.Height);
3029                                 }
3030
3031                                 // FIXME - deal with focus
3032
3033                                 if (parent != null) {
3034                                         parent.PerformLayout(this, "visible");
3035                                 } else {
3036                                         PerformLayout(this, "visible");
3037                                 }
3038                         }
3039                 }
3040         
3041                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3042                 protected void UpdateBounds() {
3043                         int     x;
3044                         int     y;
3045                         int     width;
3046                         int     height;
3047                         int     client_width;
3048                         int     client_height;
3049
3050                         if (!IsHandleCreated) {
3051                                 CreateHandle();
3052                         }
3053
3054                         XplatUI.GetWindowPos(this.Handle, out x, out y, out width, out height, out client_width, out client_height);
3055                         UpdateBounds(x, y, width, height, client_width, client_height);
3056                 }
3057
3058                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3059                 protected void UpdateBounds(int x, int y, int width, int height) {
3060                         // UpdateBounds only seems to set our sizes and fire events but not update the GUI window to match
3061                         bool    moved   = false;
3062                         bool    resized = false;
3063
3064                         int     client_x_diff = this.bounds.Width-this.client_size.Width;
3065                         int     client_y_diff = this.bounds.Height-this.client_size.Height;
3066
3067                         // Needed to generate required notifications
3068                         if ((this.bounds.X!=x) || (this.bounds.Y!=y)) {
3069                                 moved=true;
3070                         }
3071
3072                         if ((this.Bounds.Width!=width) || (this.Bounds.Height!=height)) {
3073                                 resized=true;
3074                         }
3075
3076                         bounds.X=x;
3077                         bounds.Y=y;
3078                         bounds.Width=width;
3079                         bounds.Height=height;
3080
3081                         // Update client rectangle as well
3082                         if (this.layout_suspended==0) {
3083                                 prev_size.Width=client_size.Width;
3084                                 prev_size.Height=client_size.Height;
3085                         }
3086
3087                         client_size.Width=width-client_x_diff;
3088                         client_size.Height=height-client_y_diff;
3089
3090                         if (moved) {
3091                                 OnLocationChanged(EventArgs.Empty);
3092                         }
3093
3094                         if (resized) {
3095                                 OnSizeChanged(EventArgs.Empty);
3096                         }
3097                 }
3098
3099                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3100                 protected void UpdateBounds(int x, int y, int width, int height, int clientWidth, int clientHeight) {
3101                         UpdateBounds(x, y, width, height);
3102
3103                         this.client_size.Width=clientWidth;
3104                         this.client_size.Height=clientHeight;
3105                 }
3106
3107                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3108                 protected void UpdateStyles() {
3109                         if (!IsHandleCreated) {
3110                                 return;
3111                         }
3112
3113                         XplatUI.SetWindowStyle(window.Handle, CreateParams);
3114                 }
3115
3116                 protected void UpdateZOrder() {
3117                         int     children;
3118 #if not
3119                         Control ctl;
3120
3121                         if (parent == null) {
3122                                 return;
3123                         }
3124
3125                         ctl = parent;
3126
3127                         children = ctl.child_controls.Count;
3128                         for (int i = 1; i < children; i++ ) {
3129                                 XplatUI.SetZOrder(ctl.child_controls[i].window.Handle, ctl.child_controls[i-1].window.Handle, false, false); 
3130                         }
3131 #else
3132                         children = child_controls.Count;
3133                         for (int i = 1; i < children; i++ ) {
3134                                 XplatUI.SetZOrder(child_controls[i].window.Handle, child_controls[i-1].window.Handle, false, false); 
3135                         }
3136 #endif
3137                 }
3138
3139                 protected virtual void WndProc(ref Message m) {
3140 #if debug
3141                         Console.WriteLine("Received message {0}", m);
3142 #endif
3143                         if ((this.control_style & ControlStyles.EnableNotifyMessage) != 0) {
3144                                 OnNotifyMessage(m);
3145                         }
3146
3147                         switch((Msg)m.Msg) {
3148                         case Msg.WM_WINDOWPOSCHANGED: {
3149                                 if (Visible) {
3150                                         UpdateBounds();
3151                                         if (GetStyle(ControlStyles.ResizeRedraw)) {
3152                                                 Invalidate();
3153                                         }
3154                                 }
3155                                 return;
3156                         }
3157
3158                         case Msg.WM_PAINT: {                            
3159                                 PaintEventArgs  paint_event;
3160
3161                                 paint_event = XplatUI.PaintEventStart(Handle);
3162                                 OnPaint(paint_event);
3163                                 XplatUI.PaintEventEnd(Handle);
3164                                 DefWndProc(ref m);      
3165                                 return;
3166                         }
3167                                 
3168                         case Msg.WM_ERASEBKGND: {
3169                                 if (GetStyle (ControlStyles.UserPaint)) {
3170                                         if (!GetStyle(ControlStyles.AllPaintingInWmPaint)) {
3171                                                 PaintEventArgs eraseEventArgs = new PaintEventArgs (m.WParam == IntPtr.Zero ? Graphics.FromHwnd (m.HWnd) :
3172                                                                 Graphics.FromHdc (m.WParam), new Rectangle (new Point (0,0),Size));
3173                                                 OnPaintBackground (eraseEventArgs);
3174                                         }
3175                                         m.Result = (IntPtr)1;
3176                                 } else {
3177                                         m.Result = IntPtr.Zero;
3178                                         DefWndProc (ref m);     
3179                                 }                                       
3180                                         
3181                                 return;
3182                         }
3183
3184                         case Msg.WM_LBUTTONUP: {
3185                                 HandleClick(mouse_clicks);
3186                                 OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Left, 
3187                                         mouse_clicks, 
3188                                         LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3189                                         0));
3190                                 if (mouse_clicks > 1) {
3191                                         mouse_clicks = 1;
3192                                 }
3193                                 return;
3194                         }
3195                                 
3196                         case Msg.WM_LBUTTONDOWN: {
3197                                 if (CanSelect && !is_selected) {
3198                                         Select(this);
3199                                 }
3200                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3201                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3202                                         0));
3203                                         
3204                                 return;
3205                         }
3206
3207                         case Msg.WM_LBUTTONDBLCLK: {
3208                                 mouse_clicks++;
3209                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3210                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3211                                         0));
3212
3213                                 return;
3214                         }
3215
3216                         case Msg.WM_MBUTTONUP: {
3217                                 HandleClick(mouse_clicks);
3218                                 OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Middle, 
3219                                         mouse_clicks, 
3220                                         LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3221                                         0));
3222                                 if (mouse_clicks > 1) {
3223                                         mouse_clicks = 1;
3224                                 }
3225                                 return;
3226                         }
3227                                 
3228                         case Msg.WM_MBUTTONDOWN: {                                      
3229                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3230                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3231                                         0));
3232                                         
3233                                 return;
3234                         }
3235
3236                         case Msg.WM_MBUTTONDBLCLK: {
3237                                 mouse_clicks++;
3238                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3239                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3240                                         0));
3241                                 return;
3242                         }
3243
3244                         case Msg.WM_RBUTTONUP: {
3245                                 HandleClick(mouse_clicks);
3246                                 OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Right, 
3247                                         mouse_clicks, 
3248                                         LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3249                                         0));
3250                                 if (mouse_clicks > 1) {
3251                                         mouse_clicks = 1;
3252                                 }
3253                                 return;
3254                         }
3255                                 
3256                         case Msg.WM_RBUTTONDOWN: {                                      
3257                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3258                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3259                                         0));
3260                                 return;
3261                         }
3262
3263                         case Msg.WM_RBUTTONDBLCLK: {
3264                                 mouse_clicks++;
3265                                 OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3266                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3267                                         0));
3268                                 return;
3269                         }
3270
3271                         case Msg.WM_MOUSEWHEEL: {                               
3272
3273                                 OnMouseWheel (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3274                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3275                                         HighOrder(m.WParam.ToInt32())));
3276                                 return;
3277                         }
3278
3279                                 
3280                         case Msg.WM_MOUSEMOVE: {                                        
3281                                 OnMouseMove  (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3282                                         mouse_clicks, 
3283                                         LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3284                                         0));
3285                                 return;
3286                         }
3287
3288                         case Msg.WM_MOUSE_ENTER: {
3289                                 if (is_entered) {
3290                                         return;
3291                                 }
3292                                 is_entered = true;
3293                                 OnMouseEnter(EventArgs.Empty);
3294                                 return;
3295                         }
3296
3297                         case Msg.WM_MOUSE_LEAVE: {
3298                                 is_entered=false;
3299                                 OnMouseLeave(EventArgs.Empty);
3300                                 return;
3301                         }
3302
3303                         case Msg.WM_MOUSEHOVER: {
3304                                 OnMouseHover(EventArgs.Empty);
3305                                 return;
3306                         }
3307                         
3308                         case Msg.WM_KEYDOWN: {
3309                                 if (!ProcessKeyMessage(ref m)) {
3310                                         DefWndProc (ref m);
3311                                 }
3312                                 return;
3313                         }
3314
3315                         case Msg.WM_KEYUP: {
3316                                 if (!ProcessKeyMessage(ref m)) {
3317                                         DefWndProc (ref m);
3318                                 }
3319                                 return;
3320                         }               
3321
3322                         case Msg.WM_CHAR: {
3323                                 if (!ProcessKeyMessage(ref m)) {
3324                                         DefWndProc (ref m);
3325                                 }
3326                                 return;
3327                         }
3328
3329                         case Msg.WM_HELP: {
3330                                 Point   mouse_pos;
3331                                 if (m.LParam != IntPtr.Zero) {
3332                                         HELPINFO        hi;
3333
3334                                         hi = new HELPINFO();
3335
3336                                         hi = (HELPINFO) Marshal.PtrToStructure (m.LParam, typeof (HELPINFO));
3337                                         mouse_pos = new Point(hi.MousePos.x, hi.MousePos.y);
3338                                 } else {
3339                                         mouse_pos = Control.MousePosition;
3340                                 }
3341                                 OnHelpRequested(new HelpEventArgs(mouse_pos));
3342                                 m.Result = (IntPtr)1;
3343                                 return;
3344                         }
3345
3346                         case Msg.WM_KILLFOCUS: {
3347                                 OnLeave(EventArgs.Empty);
3348                                 if (CausesValidation) {
3349                                         CancelEventArgs e;
3350                                         e = new CancelEventArgs(false);
3351
3352                                         OnValidating(e);
3353
3354                                         if (e.Cancel) {
3355                                                 Focus();
3356                                                 return;
3357                                         }
3358
3359                                         OnValidated(EventArgs.Empty);
3360                                 }
3361
3362                                 this.has_focus = false;
3363                                 OnLostFocus(EventArgs.Empty);
3364                                 return;
3365                         }
3366
3367                         case Msg.WM_SETFOCUS: {
3368                                 OnEnter(EventArgs.Empty);
3369                                 this.has_focus = true;
3370                                 OnGotFocus(EventArgs.Empty);
3371                                 return;
3372                         }
3373                                 
3374
3375                         case Msg.WM_SYSCOLORCHANGE: {
3376                                 ThemeEngine.Current.ResetDefaults();
3377                                 OnSystemColorsChanged(EventArgs.Empty);
3378                                 return;
3379                         }
3380                                 
3381
3382                         case Msg.WM_SETCURSOR: {
3383                                 if (cursor == null) {
3384                                         DefWndProc(ref m);
3385                                         return;
3386                                 }
3387
3388                                 XplatUI.SetCursor(window.Handle, cursor.handle);
3389                                 m.Result = (IntPtr)1;
3390                                 return;
3391                         }
3392
3393                         default:
3394                                 DefWndProc(ref m);      
3395                                 return;
3396                         }
3397                 }
3398                 #endregion      // Public Instance Methods
3399
3400                 #region OnXXX methods
3401                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3402                 protected virtual void OnBackColorChanged(EventArgs e) {
3403                         if (BackColorChanged!=null) BackColorChanged(this, e);
3404                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackColorChanged(e);
3405                 }
3406
3407                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3408                 protected virtual void OnBackgroundImageChanged(EventArgs e) {
3409                         if (BackgroundImageChanged!=null) BackgroundImageChanged(this, e);
3410                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackgroundImageChanged(e);
3411                 }
3412
3413                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3414                 protected virtual void OnBindingContextChanged(EventArgs e) {
3415                         if (BindingContextChanged!=null) BindingContextChanged(this, e);
3416                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBindingContextChanged(e);
3417                 }
3418
3419                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3420                 protected virtual void OnCausesValidationChanged(EventArgs e) {
3421                         if (CausesValidationChanged!=null) CausesValidationChanged(this, e);
3422                 }
3423
3424                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3425                 protected virtual void OnChangeUICues(UICuesEventArgs e) {
3426                         if (ChangeUICues!=null) ChangeUICues(this, e);
3427                 }
3428
3429                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3430                 protected virtual void OnClick(EventArgs e) {
3431                         if (Click!=null) Click(this, e);
3432                 }
3433
3434                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3435                 protected virtual void OnContextMenuChanged(EventArgs e) {
3436                         if (ContextMenuChanged!=null) ContextMenuChanged(this, e);
3437                 }
3438
3439                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3440                 protected virtual void OnControlAdded(ControlEventArgs e) {
3441                         if (ControlAdded!=null) ControlAdded(this, e);
3442                 }
3443
3444                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3445                 protected virtual void OnControlRemoved(ControlEventArgs e) {
3446                         if (ControlRemoved!=null) ControlRemoved(this, e);
3447                 }
3448
3449                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3450                 protected virtual void OnCreateControl() {
3451                         // Override me!
3452                 }
3453
3454                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3455                 protected virtual void OnCursorChanged(EventArgs e) {
3456                         if (CursorChanged!=null) CursorChanged(this, e);
3457                 }
3458
3459                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3460                 protected virtual void OnDockChanged(EventArgs e) {
3461                         if (DockChanged!=null) DockChanged(this, e);
3462                 }
3463
3464                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3465                 protected virtual void OnDoubleClick(EventArgs e) {
3466                         if (DoubleClick!=null) DoubleClick(this, e);
3467                 }
3468
3469                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3470                 protected virtual void OnDragDrop(DragEventArgs drgevent) {
3471                         if (DragDrop!=null) DragDrop(this, drgevent);
3472                 }
3473
3474                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3475                 protected virtual void OnDragEnter(DragEventArgs drgevent) {
3476                         if (DragEnter!=null) DragEnter(this, drgevent);
3477                 }
3478
3479                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3480                 protected virtual void OnDragLeave(EventArgs e) {
3481                         if (DragLeave!=null) DragLeave(this, e);
3482                 }
3483
3484                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3485                 protected virtual void OnDragOver(DragEventArgs drgevent) {
3486                         if (DragOver!=null) DragOver(this, drgevent);
3487                 }
3488
3489                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3490                 protected virtual void OnEnabledChanged(EventArgs e) {
3491                         if (EnabledChanged!=null) EnabledChanged(this, e);
3492                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentEnabledChanged(e);
3493                 }
3494
3495                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3496                 protected virtual void OnEnter(EventArgs e) {
3497                         if (Enter!=null) Enter(this, e);
3498                 }
3499
3500                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3501                 protected virtual void OnFontChanged(EventArgs e) {
3502                         if (FontChanged!=null) FontChanged(this, e);
3503                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentFontChanged(e);
3504                 }
3505
3506                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3507                 protected virtual void OnForeColorChanged(EventArgs e) {
3508                         if (ForeColorChanged!=null) ForeColorChanged(this, e);
3509                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentForeColorChanged(e);
3510                 }
3511
3512                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3513                 protected virtual void OnGiveFeedback(GiveFeedbackEventArgs gfbevent) {
3514                         if (GiveFeedback!=null) GiveFeedback(this, gfbevent);
3515                 }
3516                 
3517                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3518                 protected virtual void OnGotFocus(EventArgs e) {
3519                         if (GotFocus!=null) GotFocus(this, e);
3520                 }
3521
3522                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3523                 protected virtual void OnHandleCreated(EventArgs e) {
3524                         if (HandleCreated!=null) HandleCreated(this, e);
3525                 }
3526
3527                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3528                 protected virtual void OnHandleDestroyed(EventArgs e) {
3529                         if (HandleDestroyed!=null) HandleDestroyed(this, e);
3530                 }
3531
3532                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3533                 protected virtual void OnHelpRequested(HelpEventArgs hevent) {
3534                         if (HelpRequested!=null) HelpRequested(this, hevent);
3535                 }
3536
3537                 protected virtual void OnImeModeChanged(EventArgs e) {
3538                         if (ImeModeChanged!=null) ImeModeChanged(this, e);
3539                 }
3540
3541                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3542                 protected virtual void OnInvalidated(InvalidateEventArgs e) {
3543                         if (Invalidated!=null) Invalidated(this, e);
3544                 }
3545
3546                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3547                 protected virtual void OnKeyDown(KeyEventArgs e) {                      
3548                         if (KeyDown!=null) KeyDown(this, e);
3549                 }
3550
3551                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3552                 protected virtual void OnKeyPress(KeyPressEventArgs e) {
3553                         if (KeyPress!=null) KeyPress(this, e);
3554                 }
3555
3556                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3557                 protected virtual void OnKeyUp(KeyEventArgs e) {
3558                         if (KeyUp!=null) KeyUp(this, e);
3559                 }
3560
3561                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3562                 protected virtual void OnLayout(LayoutEventArgs levent) {
3563                         if (Layout!=null) Layout(this, levent);
3564                 }
3565
3566                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3567                 protected virtual void OnLeave(EventArgs e) {
3568                         if (Leave!=null) Leave(this, e);
3569                 }
3570
3571                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3572                 protected virtual void OnLocationChanged(EventArgs e) {
3573                         OnMove(e);
3574                         if (LocationChanged!=null) LocationChanged(this, e);
3575                 }
3576
3577                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3578                 protected virtual void OnLostFocus(EventArgs e) {
3579                         if (LostFocus!=null) LostFocus(this, e);
3580                 }
3581
3582                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3583                 protected virtual void OnMouseDown(MouseEventArgs e) {
3584                         if (MouseDown!=null) MouseDown(this, e);
3585                 }
3586
3587                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3588                 protected virtual void OnMouseEnter(EventArgs e) {
3589                         if (MouseEnter!=null) MouseEnter(this, e);
3590                 }
3591
3592                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3593                 protected virtual void OnMouseHover(EventArgs e) {
3594                         if (MouseHover!=null) MouseHover(this, e);
3595                 }
3596
3597                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3598                 protected virtual void OnMouseLeave(EventArgs e) {
3599                         if (MouseLeave!=null) MouseLeave(this, e);
3600                 }
3601
3602                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3603                 protected virtual void OnMouseMove(MouseEventArgs e) {                  
3604                         if (MouseMove!=null) MouseMove(this, e);
3605                 }
3606
3607                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3608                 protected virtual void OnMouseUp(MouseEventArgs e) {
3609                         if (MouseUp!=null) MouseUp(this, e);
3610                 }
3611
3612                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3613                 protected virtual void OnMouseWheel(MouseEventArgs e) {
3614                         if (MouseWheel!=null) MouseWheel(this, e);
3615                 }
3616
3617                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3618                 protected virtual void OnMove(EventArgs e) {
3619                         if (Move!=null) Move(this, e);
3620                 }
3621
3622                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3623                 protected virtual void OnNotifyMessage(Message m) {
3624                         // Override me!
3625                 }
3626
3627                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3628                 protected virtual void OnPaint(PaintEventArgs e) {
3629                         if (Paint!=null) Paint(this, e);
3630                 }
3631
3632                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3633                 protected virtual void OnPaintBackground(PaintEventArgs pevent) {
3634                         // Override me!
3635                 }
3636
3637                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3638                 protected virtual void OnParentBackColorChanged(EventArgs e) {
3639                         if (background_color.IsEmpty && background_image==null) {
3640                                 Invalidate();
3641                                 OnBackColorChanged(e);
3642                         }
3643                 }
3644
3645                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3646                 protected virtual void OnParentBackgroundImageChanged(EventArgs e) {
3647                         if (background_color.IsEmpty && background_image==null) {
3648                                 Invalidate();
3649                                 OnBackgroundImageChanged(e);
3650                         }
3651                 }
3652
3653                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3654                 protected virtual void OnParentBindingContextChanged(EventArgs e) {
3655                         if (binding_context==null) {
3656                                 binding_context=Parent.binding_context;
3657                                 OnBindingContextChanged(e);
3658                         }
3659                 }
3660
3661                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3662                 protected virtual void OnParentChanged(EventArgs e) {
3663                         if (ParentChanged!=null) ParentChanged(this, e);
3664                 }
3665
3666                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3667                 protected virtual void OnParentEnabledChanged(EventArgs e) {
3668                         if (is_enabled != Parent.is_enabled) {
3669                                 is_enabled=Parent.is_enabled;
3670                                 Invalidate();
3671                                 if (EnabledChanged != null) {
3672                                         EnabledChanged(this, e);
3673                                 }
3674                         }
3675                 }
3676
3677                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3678                 protected virtual void OnParentFontChanged(EventArgs e) {
3679                         if (font==null) {
3680                                 Invalidate();
3681                                 OnFontChanged(e);
3682                         }
3683                 }
3684
3685                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3686                 protected virtual void OnParentForeColorChanged(EventArgs e) {
3687                         if (foreground_color.IsEmpty) {
3688                                 Invalidate();
3689                                 OnForeColorChanged(e);
3690                         }
3691                 }
3692
3693                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3694                 protected virtual void OnParentRightToLeftChanged(EventArgs e) {
3695                         if (right_to_left==RightToLeft.Inherit) {
3696                                 Invalidate();
3697                                 OnRightToLeftChanged(e);
3698                         }
3699                 }
3700
3701                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3702                 protected virtual void OnParentVisibleChanged(EventArgs e) {
3703                         if (is_visible!=Parent.is_visible) {
3704                                 is_visible=false;
3705                                 Invalidate();
3706                                 OnVisibleChanged(e);
3707                         }
3708                 }
3709
3710                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3711                 protected virtual void OnQueryContinueDrag(QueryContinueDragEventArgs e) {
3712                         if (QueryContinueDrag!=null) QueryContinueDrag(this, e);
3713                 }
3714
3715                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3716                 protected virtual void OnResize(EventArgs e) {
3717                         if (Resize!=null) Resize(this, e);
3718
3719                         PerformLayout(this, "bounds");
3720
3721                         if (parent != null) {
3722                                 parent.PerformLayout();
3723                         }
3724                 }
3725
3726                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3727                 protected virtual void OnRightToLeftChanged(EventArgs e) {
3728                         if (RightToLeftChanged!=null) RightToLeftChanged(this, e);
3729                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentRightToLeftChanged(e);
3730                 }
3731
3732                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3733                 protected virtual void OnSizeChanged(EventArgs e) {
3734                         InvalidateBuffers ();
3735                         OnResize(e);
3736                         if (SizeChanged!=null) SizeChanged(this, e);
3737                 }
3738
3739                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3740                 protected virtual void OnStyleChanged(EventArgs e) {
3741                         if (StyleChanged!=null) StyleChanged(this, e);
3742                 }
3743
3744                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3745                 protected virtual void OnSystemColorsChanged(EventArgs e) {
3746                         if (SystemColorsChanged!=null) SystemColorsChanged(this, e);
3747                 }
3748
3749                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3750                 protected virtual void OnTabIndexChanged(EventArgs e) {
3751                         if (TabIndexChanged!=null) TabIndexChanged(this, e);
3752                 }
3753
3754                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3755                 protected virtual void OnTabStopChanged(EventArgs e) {
3756                         if (TabStopChanged!=null) TabStopChanged(this, e);
3757                 }
3758
3759                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3760                 protected virtual void OnTextChanged(EventArgs e) {
3761                         if (TextChanged!=null) TextChanged(this, e);
3762                 }
3763
3764                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3765                 protected virtual void OnValidated(EventArgs e) {
3766                         if (Validated!=null) Validated(this, e);
3767                 }
3768
3769                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3770                 protected virtual void OnValidating(System.ComponentModel.CancelEventArgs e) {
3771                         if (Validating!=null) Validating(this, e);
3772                 }
3773
3774                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3775                 protected virtual void OnVisibleChanged(EventArgs e) {
3776                         if (!is_visible) {
3777                                 if (dc_mem!=null) {
3778                                         dc_mem.Dispose ();
3779                                         bmp_mem=null;
3780                                 }
3781
3782                                 if (bmp_mem!=null) {
3783                                         bmp_mem.Dispose();
3784                                         bmp_mem=null;
3785                                 }
3786                         } else {
3787                                 if (!is_disposed) {
3788                                         if (!this.IsHandleCreated) {
3789                                                 this.CreateHandle();
3790                                         }
3791                                         PerformLayout();
3792                                 }
3793                         }
3794                         
3795                         if (VisibleChanged!=null) VisibleChanged(this, e);
3796
3797                         // We need to tell our kids
3798                         for (int i=0; i<child_controls.Count; i++) {
3799                                 child_controls[i].OnParentVisibleChanged(e);
3800                         }
3801                 }
3802                 #endregion      // OnXXX methods
3803
3804                 #region Events
3805                 public event EventHandler               BackColorChanged;
3806                 public event EventHandler               BackgroundImageChanged;
3807                 public event EventHandler               BindingContextChanged;
3808                 public event EventHandler               CausesValidationChanged;
3809                 public event UICuesEventHandler         ChangeUICues;
3810                 public event EventHandler               Click;
3811                 public event EventHandler               ContextMenuChanged;
3812
3813                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3814                 [Browsable(false)]
3815                 public event ControlEventHandler        ControlAdded;
3816
3817                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3818                 [Browsable(false)]
3819                 public event ControlEventHandler        ControlRemoved;
3820
3821                 public event EventHandler               CursorChanged;
3822                 public event EventHandler               DockChanged;
3823                 public event EventHandler               DoubleClick;
3824                 public event DragEventHandler           DragDrop;
3825                 public event DragEventHandler           DragEnter;
3826                 public event EventHandler               DragLeave;
3827                 public event DragEventHandler           DragOver;
3828                 public event EventHandler               EnabledChanged;
3829                 public event EventHandler               Enter;
3830                 public event EventHandler               FontChanged;
3831                 public event EventHandler               ForeColorChanged;
3832                 public event GiveFeedbackEventHandler   GiveFeedback;
3833
3834                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3835                 [Browsable(false)]
3836                 public event EventHandler               GotFocus;
3837
3838                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3839                 [Browsable(false)]
3840                 public event EventHandler               HandleCreated;
3841
3842                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3843                 [Browsable(false)]
3844                 public event EventHandler               HandleDestroyed;
3845
3846                 public event HelpEventHandler           HelpRequested;
3847                 public event EventHandler               ImeModeChanged;
3848
3849                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3850                 [Browsable(false)]
3851                 public event InvalidateEventHandler     Invalidated;
3852
3853                 public event KeyEventHandler            KeyDown;
3854                 public event KeyPressEventHandler       KeyPress;
3855                 public event KeyEventHandler            KeyUp;
3856                 public event LayoutEventHandler         Layout;
3857                 public event EventHandler               Leave;
3858                 public event EventHandler               LocationChanged;
3859
3860                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3861                 [Browsable(false)]
3862                 public event EventHandler               LostFocus;
3863
3864                 public event MouseEventHandler          MouseDown;
3865                 public event EventHandler               MouseEnter;
3866                 public event EventHandler               MouseHover;
3867                 public event EventHandler               MouseLeave;
3868                 public event MouseEventHandler          MouseMove;
3869                 public event MouseEventHandler          MouseUp;
3870
3871                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3872                 [Browsable(false)]
3873                 public event MouseEventHandler          MouseWheel;
3874
3875                 public event EventHandler               Move;
3876                 public event PaintEventHandler          Paint;
3877                 public event EventHandler               ParentChanged;
3878                 public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp;
3879                 public event QueryContinueDragEventHandler      QueryContinueDrag;
3880                 public event EventHandler               Resize;
3881                 public event EventHandler               RightToLeftChanged;
3882                 public event EventHandler               SizeChanged;
3883                 public event EventHandler               StyleChanged;
3884                 public event EventHandler               SystemColorsChanged;
3885                 public event EventHandler               TabIndexChanged;
3886                 public event EventHandler               TabStopChanged;
3887                 public event EventHandler               TextChanged;
3888                 public event EventHandler               Validated;
3889                 public event CancelEventHandler         Validating;
3890                 public event EventHandler               VisibleChanged;
3891                 #endregion      // Events
3892         }
3893 }