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