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