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