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