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