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