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