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