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