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