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