2005-06-05 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                                 try {
1919                                         create_params.Caption = Text;
1920                                 }
1921                                 catch {
1922                                         create_params.Caption = text;
1923                                 }
1924
1925                                 try {
1926                                         create_params.X = Left;
1927                                 }
1928                                 catch {
1929                                         create_params.X = this.bounds.X;
1930                                 }
1931
1932                                 try {
1933                                         create_params.Y = Top;
1934                                 }
1935                                 catch {
1936                                         create_params.Y = this.bounds.Y;
1937                                 }
1938
1939                                 try {
1940                                         create_params.Y = Width;
1941                                 }
1942                                 catch {
1943                                         create_params.Y = this.bounds.Width;
1944                                 }
1945
1946                                 try {
1947                                         create_params.Y = Height;
1948                                 }
1949                                 catch {
1950                                         create_params.Y = this.bounds.Height;
1951                                 }
1952
1953
1954                                 create_params.ClassName = XplatUI.DefaultClassName;
1955                                 create_params.ClassStyle = 0;
1956                                 create_params.ExStyle = 0;
1957                                 create_params.Param = 0;
1958
1959                                 if (allow_drop) {
1960                                         create_params.ExStyle |= (int)WindowStyles.WS_EX_ACCEPTFILES;
1961                                 }
1962
1963                                 if (parent!=null) {
1964                                         create_params.Parent = parent.Handle;
1965                                 }
1966
1967                                 create_params.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
1968
1969                                 if (is_visible) {
1970                                         create_params.Style |= (int)WindowStyles.WS_VISIBLE;
1971                                 }
1972
1973                                 return create_params;
1974                         }
1975                 }
1976
1977                 protected virtual ImeMode DefaultImeMode {
1978                         get {
1979                                 return ImeMode.Inherit;
1980                         }
1981                 }
1982
1983                 protected virtual Size DefaultSize {
1984                         get {
1985                                 return new Size(100, 23);
1986                         }
1987                 }
1988
1989                 protected int FontHeight {
1990                         get {
1991                                 return Font.Height;
1992                         }
1993
1994                         set {
1995                                 ;; // Nothing to do
1996                         }
1997                 }
1998
1999                 protected bool RenderRightToLeft {
2000                         get {
2001                                 return (this.right_to_left == RightToLeft.Yes);
2002                         }
2003                 }
2004
2005                 protected bool ResizeRedraw {
2006                         get {
2007                                 return GetStyle(ControlStyles.ResizeRedraw);
2008                         }
2009
2010                         set {
2011                                 SetStyle(ControlStyles.ResizeRedraw, value);
2012                         }
2013                 }
2014
2015                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2016                 [Browsable(false)]
2017                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2018                 protected virtual bool ShowFocusCues {
2019                         get {
2020                                 return true;
2021                         }
2022                 }
2023
2024                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2025                 [Browsable(false)]
2026                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2027                 protected bool ShowKeyboardCues {
2028                         get {
2029                                 return true;
2030                         }
2031                 }
2032                 #endregion      // Protected Instance Properties
2033
2034                 #region Public Static Methods
2035                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2036                 public static Control FromChildHandle(IntPtr handle) {
2037                         IEnumerator control = Control.controls.GetEnumerator();
2038
2039                         while (control.MoveNext()) {
2040                                 if (((Control)control.Current).window.Handle == handle) {
2041                                         // Found it
2042                                         if (((Control)control.Current).Parent != null) {
2043                                                 return ((Control)control.Current).Parent;
2044                                         }
2045                                 }
2046                         }
2047                         return null;
2048                 }
2049
2050                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2051                 public static Control FromHandle(IntPtr handle) {
2052                         IEnumerator control = Control.controls.GetEnumerator();
2053
2054                         while (control.MoveNext()) {
2055                                 if (((Control)control.Current).window.Handle == handle) {
2056                                         // Found it
2057                                         return ((Control)control.Current);
2058                                 }
2059                         }
2060                         return null;
2061                 }
2062
2063                 public static bool IsMnemonic(char charCode, string text) {
2064                         int amp;                        
2065
2066                         amp = text.IndexOf('&');
2067
2068                         if (amp != -1) {
2069                                 if (amp + 1 < text.Length) {
2070                                         if (text[amp + 1] != '&') {
2071                                                 if (Char.ToUpper(charCode) == Char.ToUpper(text.ToCharArray(amp + 1, 1)[0])) {
2072                                                         return true;
2073                                                 }       
2074                                         }
2075                                 }
2076                         }
2077                         return false;
2078                 }
2079                 #endregion
2080
2081                 #region Protected Static Methods
2082                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2083                 protected static bool ReflectMessage(IntPtr hWnd, ref Message m) {
2084                         Control c;
2085
2086                         c = Control.FromHandle(hWnd);
2087
2088                         if (c != null) {
2089                                 c.WndProc(ref m);
2090                                 return true;
2091                         }
2092                         return false;
2093                 }
2094                 #endregion
2095
2096                 #region Public Instance Methods
2097                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2098                 public IAsyncResult BeginInvoke(Delegate method) {
2099                         return BeginInvokeInternal(method, null);
2100                 }
2101
2102                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2103                 public IAsyncResult BeginInvoke (Delegate method, object[] args) {
2104                         return BeginInvokeInternal (method, args);
2105                 }
2106
2107                 public void BringToFront() {
2108                         if ((parent != null) && (parent.child_controls[0]!=this)) {
2109                                 if (parent.child_controls.Contains(this)) {
2110                                         parent.child_controls.SetChildIndex(this, 0);
2111                                 }
2112                         }
2113
2114                         XplatUI.SetZOrder(this.window.Handle, IntPtr.Zero, true, false);
2115
2116                         if (parent != null) {
2117                                 parent.Refresh();
2118                         }
2119                 }
2120
2121                 public bool Contains(Control ctl) {
2122                         while (ctl != null) {
2123                                 ctl = ctl.parent;
2124                                 if (ctl == this) {
2125                                         return true;
2126                                 }
2127                         }
2128                         return false;
2129                 }
2130
2131                 public void CreateControl() {
2132
2133                         if (!IsHandleCreated) {
2134                                 CreateHandle();
2135                         }
2136
2137                         if (!create_handled) {
2138                                 create_handled = true;
2139                                 OnCreateControl();
2140                         }
2141
2142                         for (int i=0; i<child_controls.Count; i++) {
2143                                 child_controls[i].CreateControl();
2144                         }
2145                 }
2146
2147                 public Graphics CreateGraphics() {
2148                         if (!IsHandleCreated) {
2149                                 this.CreateControl();
2150                         }
2151                         return Graphics.FromHwnd(this.window.Handle);
2152                 }
2153
2154                 public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects) {
2155                         return XplatUI.StartDrag(this.window.Handle, data, allowedEffects);
2156                 }
2157
2158                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2159                 public object EndInvoke (IAsyncResult async_result) {
2160                         AsyncMethodResult result = (AsyncMethodResult) async_result;
2161                         return result.EndInvoke ();
2162                 }
2163
2164                 public Form FindForm() {
2165                         Control c;
2166
2167                         c = this;
2168                         while (c != null) {
2169                                 if (c is Form) {
2170                                         return (Form)c;
2171                                 }
2172                                 c = c.Parent;
2173                         }
2174                         return null;
2175                 }
2176
2177                 public bool Focus() {
2178                         if (IsHandleCreated && !has_focus) {
2179                                 has_focus = true;
2180                                 XplatUI.SetFocus(window.Handle);
2181                         }
2182                         return true;
2183                 }
2184
2185                 public Control GetChildAtPoint(Point pt) {
2186                         // Microsoft's version of this function doesn't seem to work, so I can't check
2187                         // if we only consider children or also grandchildren, etc.
2188                         // I'm gonna say 'children only'
2189                         for (int i=0; i<child_controls.Count; i++) {
2190                                 if (child_controls[i].Bounds.Contains(pt)) {
2191                                         return child_controls[i];
2192                                 }
2193                         }
2194                         return null;
2195                 }
2196
2197                 public IContainerControl GetContainerControl() {
2198                         Control current = this;
2199
2200                         while (current!=null) {
2201                                 if ((current is IContainerControl) && ((current.control_style & ControlStyles.ContainerControl)!=0)) {
2202                                         return (IContainerControl)current;
2203                                 }
2204                                 current = current.parent;
2205                         }
2206                         return null;
2207                 }
2208
2209                 public Control GetNextControl(Control ctl, bool forward) {
2210                         // If we're not a container we don't play
2211                         if (!(this is IContainerControl) && !this.GetStyle(ControlStyles.ContainerControl)) {
2212                                 return null;
2213                         }
2214
2215                         // If ctl is not contained by this, we start at the first child of this
2216                         if (!this.Contains(ctl)) {
2217                                 ctl = null;
2218                         }
2219
2220                         // Search through our controls, starting at ctl, stepping into children as we encounter them
2221                         // try to find the control with the tabindex closest to our own, or, if we're looking into
2222                         // child controls, the one with the smallest tabindex
2223                         if (forward) {
2224                                 return FindControlForward(this, ctl);
2225                         }
2226                         return FindControlBackward(this, ctl);
2227                 }
2228
2229                 public void Hide() {
2230                         this.Visible = false;
2231                 }
2232
2233                 public void Invalidate() {
2234                         Invalidate(ClientRectangle, false);
2235                 }
2236
2237                 public void Invalidate(bool invalidateChildren) {
2238                         Invalidate(ClientRectangle, invalidateChildren);
2239                 }
2240
2241                 public void Invalidate(System.Drawing.Rectangle rc) {
2242                         Invalidate(rc, false);
2243                 }
2244
2245                 public void Invalidate(System.Drawing.Rectangle rc, bool invalidateChildren) {
2246                         if (!IsHandleCreated || !Visible) {
2247                                 return;
2248                         }
2249
2250                         NotifyInvalidate(rc);
2251
2252                         XplatUI.Invalidate(Handle, rc, false);
2253
2254                         if (invalidateChildren) {
2255                                 for (int i=0; i<child_controls.Count; i++) child_controls[i].Invalidate();
2256                         }
2257                         OnInvalidated(new InvalidateEventArgs(rc));
2258                 }
2259
2260                 public void Invalidate(System.Drawing.Region region) {
2261                         Invalidate(region, false);
2262                 }
2263
2264                 [MonoTODO("Figure out if GetRegionScans is usable")]
2265                 public void Invalidate(System.Drawing.Region region, bool invalidateChildren) {
2266                         throw new NotImplementedException();
2267
2268                         // FIXME - should use the GetRegionScans function of the region to invalidate each area
2269                         //if (invalidateChildren) {
2270                         //      for (int i=0; i<child_controls.Count; i++) child_controls[i].Invalidate();
2271                         //}
2272                 }
2273
2274                 public object Invoke (Delegate method) {
2275                         return Invoke(method, null);
2276                 }
2277
2278                 public object Invoke (Delegate method, object[] args) {
2279                         if (!this.InvokeRequired) {
2280                                 return method.DynamicInvoke(args);
2281                         }
2282
2283                         IAsyncResult result = BeginInvoke (method, args);
2284                         return EndInvoke(result);
2285                 }
2286
2287                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2288                 public void PerformLayout() {
2289                         PerformLayout(null, null);
2290                 }
2291
2292                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2293                 public void PerformLayout(Control affectedControl, string affectedProperty) {
2294                         LayoutEventArgs levent = new LayoutEventArgs(affectedControl, affectedProperty);
2295
2296                         if (layout_suspended>0) {
2297                                 layout_pending = true;
2298                                 return;
2299                         }
2300
2301                         layout_pending = false;
2302
2303                         // Prevent us from getting messed up
2304                         layout_suspended++;
2305
2306                         // Perform all Dock and Anchor calculations
2307                         try {
2308                                 Control         child;
2309                                 AnchorStyles    anchor;
2310                                 Rectangle       space;
2311                                 int             diff_width;
2312                                 int             diff_height;
2313
2314                                 space=this.DisplayRectangle;
2315                                 if (prev_size != Size.Empty) {
2316                                         diff_width = space.Width - prev_size.Width;
2317                                         diff_height = space.Height - prev_size.Height;
2318                                 } else {
2319                                         diff_width = 0;
2320                                         diff_height = 0;
2321                                 }
2322
2323                                 // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
2324                                 for (int i = child_controls.Count - 1; i >= 0; i--) {
2325                                         child=child_controls[i];
2326                                         switch (child.Dock) {
2327                                                 case DockStyle.None: {
2328                                                         // Do nothing
2329                                                         break;
2330                                                 }
2331
2332                                                 case DockStyle.Left: {
2333                                                         child.SetBounds(space.Left, space.Y, child.Width, space.Height);
2334                                                         space.X+=child.Width;
2335                                                         space.Width-=child.Width;
2336                                                         break;
2337                                                 }
2338
2339                                                 case DockStyle.Top: {
2340                                                         child.SetBounds(space.Left, space.Y, space.Width, child.Height);
2341                                                         space.Y+=child.Height;
2342                                                         space.Height-=child.Height;
2343                                                         break;
2344                                                 }
2345                                         
2346                                                 case DockStyle.Right: {
2347                                                         child.SetBounds(space.Right-child.Width, space.Y, child.Width, space.Height);
2348                                                         space.Width-=child.Width;
2349                                                         break;
2350                                                 }
2351
2352                                                 case DockStyle.Bottom: {
2353                                                         child.SetBounds(space.Left, space.Bottom-child.Height, space.Width, child.Height);
2354                                                         space.Height-=child.Height;
2355                                                         break;
2356                                                 }
2357                                         }
2358                                 }
2359
2360                                 for (int i = child_controls.Count - 1; i >= 0; i--) {
2361                                         child=child_controls[i];
2362
2363                                         if (child.Dock == DockStyle.Fill) {
2364                                                 child.SetBounds(space.Left, space.Top, space.Width, space.Height);
2365                                                 space.Width=0;
2366                                                 space.Height=0;
2367                                         }
2368                                 }
2369
2370                                 space=this.DisplayRectangle;
2371
2372                                 // Deal with anchoring
2373                                 for (int i=0; i < child_controls.Count; i++) {
2374                                         int left;
2375                                         int top;
2376                                         int width;
2377                                         int height;
2378
2379                                         child=child_controls[i];
2380                                         anchor=child.Anchor;
2381
2382                                         left=child.Left;
2383                                         top=child.Top;
2384                                         width=child.Width;
2385                                         height=child.Height;
2386
2387                                         // If the control is docked we don't need to do anything
2388                                         if (child.Dock != DockStyle.None) {
2389                                                 continue;
2390                                         }
2391
2392                                         if ((anchor & AnchorStyles.Left) !=0 ) {
2393                                                 if ((anchor & AnchorStyles.Right) != 0) {
2394                                                         // Anchoring to left and right
2395                                                         width=width+diff_width;
2396                                                 } else {
2397                                                         ; // nothing to do
2398                                                 }
2399                                         } else if ((anchor & AnchorStyles.Right) != 0) {
2400                                                 left+=diff_width;
2401                                         } else {
2402                                                 left+=diff_width/2;
2403                                         }
2404
2405                                         if ((anchor & AnchorStyles.Top) !=0 ) {
2406                                                 if ((anchor & AnchorStyles.Bottom) != 0) {
2407                                                         height+=diff_height;
2408                                                 } else {
2409                                                         ; // nothing to do
2410                                                 }
2411                                         } else if ((anchor & AnchorStyles.Bottom) != 0) {
2412                                                 top+=diff_height;
2413                                         } else {
2414                                                 top+=diff_height/2;
2415                                         }
2416
2417                                         // Sanity
2418                                         if (width < 0) {
2419                                                 width=0;
2420                                         }
2421
2422                                         if (height < 0) {
2423                                                 height=0;
2424                                         }
2425
2426                                         child.SetBounds(left, top, width, height);
2427                                 }
2428
2429                                 // Let everyone know
2430                                 OnLayout(levent);
2431                         }
2432
2433                                 // Need to make sure we decremend layout_suspended
2434                         finally {
2435                                 layout_suspended--;
2436                         }
2437                 }
2438
2439                 public Point PointToClient (Point p) {
2440                         int x = p.X;
2441                         int y = p.Y;
2442
2443                         XplatUI.ScreenToClient (Handle, ref x, ref y);
2444
2445                         return new Point (x, y);
2446                 }
2447
2448                 public Point PointToScreen(Point p) {
2449                         int x = p.X;
2450                         int y = p.Y;
2451
2452                         XplatUI.ClientToScreen(Handle, ref x, ref y);
2453
2454                         return new Point(x, y);
2455                 }
2456
2457                 public virtual bool PreProcessMessage(ref Message msg) {
2458                         Keys key_data;
2459
2460                         if ((msg.Msg == (int)Msg.WM_KEYDOWN) || (msg.Msg == (int)Msg.WM_SYSKEYDOWN)) {
2461                                 key_data = (Keys)msg.WParam.ToInt32();
2462                                 if (!ProcessCmdKey(ref msg, key_data)) {
2463                                         if (IsInputKey(key_data)) {
2464                                                 return true;
2465                                         }
2466
2467                                         return ProcessDialogKey(key_data);
2468                                 }
2469
2470                                 return true;
2471                         } else if (msg.Msg == (int)Msg.WM_CHAR) {
2472                                 if (IsInputChar((char)msg.WParam)) {
2473                                         return true;
2474                                 }
2475                         } else if (msg.Msg == (int)Msg.WM_SYSCHAR) {
2476                                 if (IsInputChar((char)msg.WParam)) {
2477                                         return true;
2478                                 }
2479                                 return ProcessDialogChar((char)msg.WParam);
2480                         }
2481                         return false;
2482                 }
2483
2484                 public Rectangle RectangleToClient(Rectangle r) {
2485                         return new Rectangle(PointToClient(r.Location), r.Size);
2486                 }
2487
2488                 public Rectangle RectangleToScreen(Rectangle r) {
2489                         return new Rectangle(PointToScreen(r.Location), r.Size);
2490                 }
2491
2492                 public virtual void Refresh() {                 
2493                         if (IsHandleCreated == true) {
2494                                 Invalidate();
2495                                 XplatUI.UpdateWindow(window.Handle);
2496                         }
2497                 }
2498
2499                 [EditorBrowsable(EditorBrowsableState.Never)]
2500                 public virtual void ResetBackColor() {
2501                         background_color = Color.Empty;
2502                 }
2503
2504 #if haveDataBindings
2505                 [EditorBrowsable(EditorBrowsableState.Never)]
2506                 [MonoTODO]
2507                 public void ResetBindings() {
2508                         // Do something
2509                 }
2510 #endif
2511
2512                 [EditorBrowsable(EditorBrowsableState.Never)]
2513                 public virtual void ResetCursor() {
2514                         cursor = null;
2515                 }
2516
2517                 [EditorBrowsable(EditorBrowsableState.Never)]
2518                 public virtual void ResetFont() {
2519                         font = null;
2520                 }
2521
2522                 [EditorBrowsable(EditorBrowsableState.Never)]
2523                 public virtual void ResetForeColor() {
2524                         foreground_color = Color.Empty;
2525                 }
2526
2527                 [EditorBrowsable(EditorBrowsableState.Never)]
2528                 public void ResetImeMode() {
2529                         ime_mode = DefaultImeMode;
2530                 }
2531
2532                 [EditorBrowsable(EditorBrowsableState.Never)]
2533                 public virtual void ResetRightToLeft() {
2534                         right_to_left = RightToLeft.Inherit;
2535                 }
2536
2537                 public virtual void ResetText() {
2538                         text = null;
2539                 }
2540
2541                 public void ResumeLayout() {
2542                         ResumeLayout (true);
2543                 }
2544
2545                 public void ResumeLayout(bool performLayout) {
2546                         layout_suspended--;
2547                         
2548                         if (layout_suspended > 0) {
2549                                 return;
2550                         }
2551
2552                         if (performLayout || layout_pending) {
2553                                 PerformLayout();
2554                         }
2555                 }
2556
2557                 public void Scale(float ratio) {
2558                         ScaleCore(ratio, ratio);
2559                 }
2560
2561                 public void Scale(float dx, float dy) {
2562                         ScaleCore(dx, dy);
2563                 }
2564
2565                 public void Select() {
2566                         Select(false, false);
2567                 }
2568
2569                 public bool SelectNextControl(Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) {
2570                         Control c;
2571                                 
2572                         c = ctl;
2573                         do {
2574                                 c = GetNextControl(c, forward);
2575                                 if (c == null) {
2576                                         if (wrap) {
2577                                                 wrap = false;
2578                                                 continue;
2579                                         }
2580                                         break;
2581                                 }
2582
2583                                 if (c.CanSelect && ((c.parent == ctl.parent) || nested) && (c.tab_stop || !tabStopOnly)) {
2584                                         Select(c);
2585                                         return true;
2586                                 }
2587                         } while (c != ctl);     // If we wrap back to ourselves we stop
2588
2589                         return false;
2590                 }
2591
2592                 public void SendToBack() {
2593                         if ((parent != null) && (parent.child_controls[parent.child_controls.Count-1]!=this)) {
2594                                 if (parent.child_controls.Contains(this)) {
2595                                         parent.child_controls.SetChildIndex(this, parent.child_controls.Count);
2596                                 }
2597                         }
2598
2599                         XplatUI.SetZOrder(this.window.Handle, IntPtr.Zero, false, true);
2600                         if (parent != null) {
2601                                 parent.Refresh();
2602                         }
2603                 }
2604
2605                 public void SetBounds(int x, int y, int width, int height) {
2606                         SetBoundsCore(x, y, width, height, BoundsSpecified.All);
2607                 }
2608
2609                 public void SetBounds(int x, int y, int width, int height, BoundsSpecified specified) {
2610                         SetBoundsCore(x, y, width, height, specified);
2611                 }
2612
2613                 public void Show() {
2614                         if (!IsHandleCreated) {
2615                                 this.CreateControl();
2616                         }
2617
2618                         this.Visible=true;
2619                 }
2620
2621                 public void SuspendLayout() {
2622                         layout_suspended++;
2623                 }
2624
2625                 public void Update() {
2626                         needs_redraw = true;
2627                         XplatUI.UpdateWindow(window.Handle);
2628                 }
2629                 #endregion      // Public Instance Methods
2630
2631                 #region Protected Instance Methods
2632                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2633                 [MonoTODO("Implement this and tie it into Control.ControlAccessibleObject.NotifyClients")]
2634                 protected void AccessibilityNotifyClients(AccessibleEvents accEvent, int childID) {
2635                         throw new NotImplementedException();
2636                 }
2637
2638                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2639                 protected virtual AccessibleObject CreateAccessibilityInstance() {
2640                         return new Control.ControlAccessibleObject(this);
2641                 }
2642
2643                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2644                 protected virtual ControlCollection CreateControlsInstance() {
2645                         return new ControlCollection(this);
2646                 }
2647
2648                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2649                 protected virtual void CreateHandle() {
2650                         if (IsDisposed) {
2651                                 throw new ObjectDisposedException(Name);
2652                         }
2653
2654                         if (IsHandleCreated) {
2655                                 return;
2656                         }
2657
2658                         window.CreateHandle(CreateParams);
2659
2660                         // Find out where the window manager placed us
2661                         UpdateBounds();
2662                         UpdateStyles();
2663
2664                         if (window.Handle!=IntPtr.Zero) {
2665                                 if (!controls.Contains(window.Handle)) {
2666                                         controls.Add(this);
2667                                 }
2668
2669                                 creator_thread = Thread.CurrentThread;
2670
2671                                 XplatUI.SetWindowBackground(window.Handle, this.BackColor);
2672
2673                                 OnHandleCreated(EventArgs.Empty);
2674                         }
2675                 }
2676
2677                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2678                 protected virtual void DefWndProc(ref Message m) {
2679                         window.DefWndProc(ref m);
2680                 }
2681
2682                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2683                 protected virtual void DestroyHandle() {
2684                         if (IsHandleCreated) {
2685                                 // Destroy our children before we destroy ourselves, to prevent them from
2686                                 // being implictly (without us knowing) destroyed
2687                                 for (int i=0; i < child_controls.Count; i++) {
2688                                         child_controls[i].DestroyHandle();
2689                                 }
2690
2691
2692                                 if (window != null) {
2693                                         window.DestroyHandle();
2694                                 }
2695                                 OnHandleDestroyed(EventArgs.Empty);
2696                         }
2697                 }
2698
2699                 protected bool GetStyle(ControlStyles flag) {
2700                         return (control_style & flag) != 0;
2701                 }
2702
2703                 protected bool GetTopLevel() {
2704                         return is_toplevel;
2705                 }
2706
2707                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2708                 protected virtual void InitLayout() {
2709                         if (parent != null) {
2710                                 parent.PerformLayout(this, "parent");
2711                         }
2712                 }
2713
2714                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2715                 protected void InvokeGotFocus(Control toInvoke, EventArgs e) {
2716                         toInvoke.OnGotFocus(e);
2717                 }
2718
2719                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2720                 protected void InvokeLostFocus(Control toInvoke, EventArgs e) {
2721                         toInvoke.OnLostFocus(e);
2722                 }
2723
2724                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2725                 protected void InvokeOnClick(Control toInvoke, EventArgs e) {
2726                         toInvoke.OnClick(e);
2727                 }
2728
2729                 protected void InvokePaint(Control toInvoke, PaintEventArgs e) {
2730                         toInvoke.OnPaint(e);
2731                 }
2732
2733                 protected void InvokePaintBackground(Control toInvoke, PaintEventArgs e) {
2734                         toInvoke.OnPaintBackground(e);
2735                 }
2736
2737                 protected virtual bool IsInputChar (char charCode) {
2738                         if (parent != null) {
2739                                 return parent.IsInputChar(charCode);
2740                         }
2741
2742                         return false;
2743                 }
2744
2745                 protected virtual bool IsInputKey (Keys keyData) {
2746                         // Doc says this one calls IsInputChar; not sure what to do with that
2747                         return false;
2748                 }
2749
2750                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2751                 protected virtual void NotifyInvalidate(Rectangle invalidatedArea) {
2752                         // override me?
2753                 }
2754
2755                 protected virtual bool ProcessCmdKey(ref Message msg, Keys keyData) {
2756                         if ((context_menu != null) && context_menu.ProcessCmdKey(ref msg, keyData)) {
2757                                 return true;
2758                         }
2759
2760                         if (parent != null) {
2761                                 return parent.ProcessCmdKey(ref msg, keyData);
2762                         }
2763
2764                         return false;
2765                 }
2766
2767                 protected virtual bool ProcessDialogChar(char charCode) {
2768                         if (parent != null) {
2769                                 return parent.ProcessDialogChar (charCode);
2770                         }
2771
2772                         return false;
2773                 }
2774
2775                 protected virtual bool ProcessDialogKey (Keys keyData) {
2776                         if (parent != null) {
2777                                 return parent.ProcessDialogKey (keyData);
2778                         }
2779
2780                         return false;
2781                 }
2782
2783                 protected virtual bool ProcessKeyEventArgs (ref Message msg)
2784                 {
2785                         KeyEventArgs            key_event;
2786
2787                         switch (msg.Msg) {
2788                                 case (int)Msg.WM_KEYDOWN: {
2789                                         key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
2790                                         OnKeyDown (key_event);
2791                                         return key_event.Handled;
2792                                 }
2793                                 case (int)Msg.WM_KEYUP: {
2794                                         key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
2795                                         OnKeyUp (key_event);
2796                                         return key_event.Handled;
2797                                 }
2798
2799                                 case (int)Msg.WM_CHAR: {
2800                                         KeyPressEventArgs       key_press_event;
2801
2802                                         key_press_event = new KeyPressEventArgs((char)msg.WParam);
2803                                         OnKeyPress(key_press_event);
2804                                         return key_press_event.Handled;
2805                                 }
2806
2807                                 default: {
2808                                         break;
2809                                 }
2810                         }
2811
2812                         return false;
2813                 }
2814
2815                 protected internal virtual bool ProcessKeyMessage(ref Message msg) {
2816                         if (parent != null) {
2817                                 if (parent.ProcessKeyPreview(ref msg)) {
2818                                         return true;
2819                                 }
2820                         }
2821
2822                         return false;
2823                 }
2824
2825                 protected virtual bool ProcessKeyPreview(ref Message msg) {
2826                         if (parent != null) {
2827                                 return parent.ProcessKeyPreview(ref msg);
2828                         }
2829
2830                         return false;
2831                 }
2832
2833                 protected virtual bool ProcessMnemonic(char charCode) {
2834                         // override me
2835                         return false;
2836                 }
2837
2838                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2839                 protected void RaiseDragEvent(object key, DragEventArgs e) {
2840                         // MS Internal
2841                 }
2842
2843                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2844                 protected void RaiseKeyEvent(object key, KeyEventArgs e) {
2845                         // MS Internal
2846                 }
2847
2848                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2849                 protected void RaiseMouseEvent(object key, MouseEventArgs e) {
2850                         // MS Internal
2851                 }
2852
2853                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2854                 protected void RaisePaintEvent(object key, PaintEventArgs e) {
2855                         // MS Internal
2856                 }
2857
2858                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2859                 protected void RecreateHandle() {
2860                         IEnumerator child = child_controls.GetEnumerator();
2861
2862                         is_recreating=true;
2863
2864                         if (IsHandleCreated) {
2865                                 DestroyHandle();
2866                                 CreateHandle();
2867
2868                                 // FIXME ZOrder?
2869
2870                                 while (child.MoveNext()) {
2871                                         ((Control)child.Current).RecreateHandle();
2872                                 }
2873                         } else {
2874                                 CreateControl();
2875                         }
2876
2877                         is_recreating = false;
2878                 }
2879
2880                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2881                 protected void ResetMouseEventArgs() {
2882                         // MS Internal
2883                 }
2884
2885                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2886                 protected ContentAlignment RtlTranslateAlignment(ContentAlignment align) {
2887                         if (right_to_left == RightToLeft.No) {
2888                                 return align;
2889                         }
2890
2891                         switch (align) {
2892                                 case ContentAlignment.TopLeft: {
2893                                         return ContentAlignment.TopRight;
2894                                 }
2895
2896                                 case ContentAlignment.TopRight: {
2897                                         return ContentAlignment.TopLeft;
2898                                 }
2899
2900                                 case ContentAlignment.MiddleLeft: {
2901                                         return ContentAlignment.MiddleRight;
2902                                 }
2903
2904                                 case ContentAlignment.MiddleRight: {
2905                                         return ContentAlignment.MiddleLeft;
2906                                 }
2907
2908                                 case ContentAlignment.BottomLeft: {
2909                                         return ContentAlignment.BottomRight;
2910                                 }
2911
2912                                 case ContentAlignment.BottomRight: {
2913                                         return ContentAlignment.BottomLeft;
2914                                 }
2915
2916                                 default: {
2917                                         // if it's center it doesn't change
2918                                         return align;
2919                                 }
2920                         }
2921                 }
2922
2923                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2924                 protected HorizontalAlignment RtlTranslateAlignment(HorizontalAlignment align) {
2925                         if ((right_to_left == RightToLeft.No) || (align == HorizontalAlignment.Center)) {
2926                                 return align;
2927                         }
2928
2929                         if (align == HorizontalAlignment.Left) {
2930                                 return HorizontalAlignment.Right;
2931                         }
2932
2933                         // align must be HorizontalAlignment.Right
2934                         return HorizontalAlignment.Left;
2935                 }
2936
2937                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2938                 protected LeftRightAlignment RtlTranslateAlignment(LeftRightAlignment align) {
2939                         if (right_to_left == RightToLeft.No) {
2940                                 return align;
2941                         }
2942
2943                         if (align == LeftRightAlignment.Left) {
2944                                 return LeftRightAlignment.Right;
2945                         }
2946
2947                         // align must be LeftRightAlignment.Right;
2948                         return LeftRightAlignment.Left;
2949                 }
2950
2951                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2952                 protected ContentAlignment RtlTranslateContent(ContentAlignment align) {
2953                         return RtlTranslateAlignment(align);
2954                 }
2955
2956                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2957                 protected HorizontalAlignment RtlTranslateHorizontal(HorizontalAlignment align) {
2958                         return RtlTranslateAlignment(align);
2959                 }
2960
2961                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2962                 protected LeftRightAlignment RtlTranslateLeftRight(LeftRightAlignment align) {
2963                         return RtlTranslateAlignment(align);
2964                 }
2965
2966                 [EditorBrowsable(EditorBrowsableState.Advanced)]
2967                 protected virtual void ScaleCore(float dx, float dy) {
2968                         Point   location;
2969                         Size    size;
2970
2971                         SuspendLayout();
2972
2973                         location = new Point((int)(Left * dx), (int)(Top * dy));
2974                         size = this.ClientSize;
2975                         
2976
2977                         if (!GetStyle(ControlStyles.FixedWidth)) {
2978                                 size.Width = (int)(size.Width * dx);
2979                         }
2980
2981                         if (!GetStyle(ControlStyles.FixedHeight)) {
2982                                 size.Height = (int)(size.Height * dy);
2983                         }
2984
2985                         Location = location;
2986                         ClientSize = size;
2987
2988                         /* Now scale our children */
2989                         for (int i=0; i < child_controls.Count; i++) {
2990                                 child_controls[i].Scale(dx, dy);
2991                         }
2992
2993                         ResumeLayout();
2994                 }
2995
2996                 protected virtual void Select(bool directed, bool forward) {
2997                         int     index;
2998                         bool    result;
2999
3000                         if (!directed) {
3001                                 // Select this control
3002                                 Select(this);
3003                                 return;
3004                         }
3005
3006                         if (parent == null) {
3007                                 return;
3008                         }
3009
3010                         // FIXME - this thing is doing the wrong stuff, needs to be similar to SelectNextControl
3011
3012                         index = parent.child_controls.IndexOf(this);
3013                         result = false;
3014
3015                         do {
3016                                 if (forward) {
3017                                         if ((index+1) < parent.child_controls.Count) {
3018                                                 index++;
3019                                         } else {
3020                                                 index = 0;
3021                                         }
3022                                 } else {
3023                                         if (index>0) {
3024                                                 index++;
3025                                         } else {
3026                                                 index = parent.child_controls.Count-1;
3027                                         }
3028                                 }
3029                                 result = Select(parent.child_controls[index]);
3030                         } while (!result && parent.child_controls[index] != this);
3031                 }
3032
3033                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3034                 protected virtual void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
3035                         // SetBoundsCore updates the Win32 control itself. UpdateBounds updates the controls variables and fires events, I'm guessing - pdb
3036                         if ((specified & BoundsSpecified.X) != BoundsSpecified.X) {
3037                                 x = Left;
3038                         }
3039
3040                         if ((specified & BoundsSpecified.Y) != BoundsSpecified.Y) {
3041                                 y = Top;
3042                         }
3043
3044                         if ((specified & BoundsSpecified.Width) != BoundsSpecified.Width) {
3045                                 width = Width;
3046                         }
3047
3048                         if ((specified & BoundsSpecified.Height) != BoundsSpecified.Height) {
3049                                 height = Height;
3050                         }
3051
3052                         if (IsHandleCreated) {
3053                                 XplatUI.SetWindowPos(Handle, x, y, width, height);
3054                         }
3055                         UpdateBounds(x, y, width, height);
3056                 }
3057
3058                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3059                 protected virtual void SetClientSizeCore(int x, int y) {
3060                         // Calculate the actual window size from the client size (it usually stays the same or grows)
3061                         Rectangle       ClientRect;
3062                         Rectangle       WindowRect;
3063                         CreateParams    cp;
3064
3065                         ClientRect = new Rectangle(0, 0, x, y);
3066                         cp = this.CreateParams;
3067
3068                         if (XplatUI.CalculateWindowRect(Handle, ref ClientRect, cp.Style, cp.ExStyle, IntPtr.Zero, out WindowRect)==false) {
3069                                 return;
3070                         }
3071
3072                         SetBoundsCore(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
3073                 }
3074
3075                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3076                 protected void SetStyle(ControlStyles flag, bool value) {
3077                         if (value) {
3078                                 control_style |= flag;
3079                         } else {
3080                                 control_style &= ~flag;
3081                         }
3082                         OnStyleChanged(EventArgs.Empty);
3083                 }
3084
3085                 protected void SetTopLevel(bool value) {
3086                         if ((GetTopLevel() != value) && (parent != null)) {
3087                                 throw new Exception();
3088                         }
3089
3090                         if (this is Form) {
3091                                 if (value == true) {
3092                                         if (!Visible) {
3093                                                 Visible = true;
3094                                         }
3095                                 } else {
3096                                         if (Visible) {
3097                                                 Visible = false;
3098                                         }
3099                                 }
3100                         }
3101                         is_toplevel = value;
3102                 }
3103
3104                 protected virtual void SetVisibleCore(bool value) {
3105                         if (value!=is_visible) {
3106                                 is_visible=value;
3107                                 XplatUI.SetVisible(Handle, value);
3108                                 // Explicitly move Toplevel windows to where we want them;
3109                                 // apparently moving unmapped toplevel windows doesn't work
3110                                 if (is_visible && (this is Form)) {
3111                                         XplatUI.SetWindowPos(window.Handle, bounds.X, bounds.Y, bounds.Width, bounds.Height);
3112                                 }
3113                                 OnVisibleChanged(EventArgs.Empty);
3114
3115                                 if (!is_visible) {
3116                                         if (dc_mem != null) {
3117                                                 dc_mem.Dispose();
3118                                                 dc_mem = null;
3119                                         }
3120
3121                                         if (bmp_mem != null) {
3122                                                 bmp_mem.Dispose();
3123                                                 bmp_mem = null;
3124                                         }
3125                                 } else {
3126                                         this.CreateBuffers(bounds.Width, bounds.Height);
3127
3128                                         CreateControl();
3129                                 }
3130
3131                                 if (value == false && parent != null) {
3132                                         Control container;
3133
3134                                         // Need to start at parent, GetContainerControl might return ourselves if we're a container
3135                                         container = (Control)parent.GetContainerControl();
3136                                         if (container != null) {
3137                                                 container.SelectNextControl(this, true, true, true, true);
3138                                         }
3139                                 }
3140
3141                                 if (parent != null) {
3142                                         parent.PerformLayout(this, "visible");
3143                                 } else {
3144                                         PerformLayout(this, "visible");
3145                                 }
3146                         }
3147                 }
3148         
3149                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3150                 protected void UpdateBounds() {
3151                         int     x;
3152                         int     y;
3153                         int     width;
3154                         int     height;
3155                         int     client_width;
3156                         int     client_height;
3157
3158                         if (!IsHandleCreated) {
3159                                 CreateHandle();
3160                         }
3161
3162                         XplatUI.GetWindowPos(this.Handle, this is Form, out x, out y, out width, out height, out client_width, out client_height);
3163
3164                         UpdateBounds(x, y, width, height, client_width, client_height);
3165                 }
3166
3167                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3168                 protected void UpdateBounds(int x, int y, int width, int height) {
3169                         // UpdateBounds only seems to set our sizes and fire events but not update the GUI window to match
3170                         bool    moved   = false;
3171                         bool    resized = false;
3172
3173                         int     client_x_diff = this.bounds.Width-this.client_size.Width;
3174                         int     client_y_diff = this.bounds.Height-this.client_size.Height;
3175
3176                         // Needed to generate required notifications
3177                         if ((this.bounds.X!=x) || (this.bounds.Y!=y)) {
3178                                 moved=true;
3179                         }
3180
3181                         if ((this.Bounds.Width!=width) || (this.Bounds.Height!=height)) {
3182                                 resized=true;
3183                         }
3184
3185                         bounds.X=x;
3186                         bounds.Y=y;
3187                         bounds.Width=width;
3188                         bounds.Height=height;
3189
3190                         // Update client rectangle as well
3191                         if (this.layout_suspended==0) {
3192                                 prev_size.Width=client_size.Width;
3193                                 prev_size.Height=client_size.Height;
3194                         }
3195
3196                         client_size.Width=width-client_x_diff;
3197                         client_size.Height=height-client_y_diff;
3198
3199                         if (moved) {
3200                                 OnLocationChanged(EventArgs.Empty);
3201                         }
3202
3203                         if (resized) {
3204                                 OnSizeChanged(EventArgs.Empty);
3205                         }
3206                 }
3207
3208                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3209                 protected void UpdateBounds(int x, int y, int width, int height, int clientWidth, int clientHeight) {
3210                         UpdateBounds(x, y, width, height);
3211
3212                         this.client_size.Width=clientWidth;
3213                         this.client_size.Height=clientHeight;
3214                 }
3215
3216                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3217                 protected void UpdateStyles() {
3218                         if (!IsHandleCreated) {
3219                                 return;
3220                         }
3221
3222                         XplatUI.SetWindowStyle(window.Handle, CreateParams);
3223                 }
3224
3225                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3226                 protected void UpdateZOrder() {
3227                         int     children;
3228 #if not
3229                         Control ctl;
3230
3231                         if (parent == null) {
3232                                 return;
3233                         }
3234
3235                         ctl = parent;
3236
3237                         children = ctl.child_controls.Count;
3238                         for (int i = 1; i < children; i++ ) {
3239                                 XplatUI.SetZOrder(ctl.child_controls[i].window.Handle, ctl.child_controls[i-1].window.Handle, false, false); 
3240                         }
3241 #else
3242                         children = child_controls.Count;
3243                         for (int i = 1; i < children; i++ ) {
3244                                 XplatUI.SetZOrder(child_controls[i].window.Handle, child_controls[i-1].window.Handle, false, false); 
3245                         }
3246 #endif
3247                 }
3248
3249                 protected virtual void WndProc(ref Message m) {
3250 #if debug
3251                         Console.WriteLine("Control received message {0}", (Msg)m.Msg);
3252 #endif
3253                         if ((this.control_style & ControlStyles.EnableNotifyMessage) != 0) {
3254                                 OnNotifyMessage(m);
3255                         }
3256
3257                         switch((Msg)m.Msg) {
3258                                 case Msg.WM_WINDOWPOSCHANGED: {
3259                                         if (Visible) {
3260                                                 UpdateBounds();
3261                                                 if (GetStyle(ControlStyles.ResizeRedraw)) {
3262                                                         Invalidate();
3263                                                 }
3264                                         }
3265                                         return;
3266                                 }
3267
3268                                 case Msg.WM_PAINT: {                            
3269                                         PaintEventArgs  paint_event;
3270
3271                                         paint_event = XplatUI.PaintEventStart(Handle);
3272
3273                                         if (!needs_redraw) {
3274                                                 // Just blit the previous image
3275                                                 paint_event.Graphics.DrawImage (ImageBuffer, paint_event.ClipRectangle, paint_event.ClipRectangle, GraphicsUnit.Pixel);
3276                                                 XplatUI.PaintEventEnd(Handle);
3277                                                 return;
3278                                         }
3279
3280                                         Graphics dc = null;
3281                                         if ((control_style & ControlStyles.DoubleBuffer) != 0) {
3282                                                 dc = paint_event.SetGraphics (DeviceContext);
3283                                         }
3284
3285                                         if ((control_style & (ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint)) != (ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint)) {
3286                                                 OnPaintBackground(paint_event);
3287                                         }
3288
3289                                         OnPaint(paint_event);
3290
3291                                         if ((control_style & ControlStyles.DoubleBuffer) != 0) {
3292                                                 dc.DrawImage (ImageBuffer, paint_event.ClipRectangle, paint_event.ClipRectangle, GraphicsUnit.Pixel);
3293                                                 paint_event.SetGraphics (dc);
3294                                                 needs_redraw = false;
3295                                         }
3296
3297                                         XplatUI.PaintEventEnd(Handle);
3298
3299                                         if (!GetStyle(ControlStyles.UserPaint)) {
3300                                                 DefWndProc(ref m);
3301                                         }
3302                                         
3303                                         return;
3304                                 }
3305                                         
3306                                 case Msg.WM_ERASEBKGND: {
3307                                         if (GetStyle (ControlStyles.UserPaint)) {
3308                                                 if (!GetStyle(ControlStyles.AllPaintingInWmPaint)) {
3309                                                         PaintEventArgs eraseEventArgs = new PaintEventArgs (m.WParam == IntPtr.Zero ? Graphics.FromHwnd (m.HWnd) :
3310                                                                         Graphics.FromHdc (m.WParam), new Rectangle (new Point (0,0),Size));
3311                                                         OnPaintBackground (eraseEventArgs);
3312                                                 }
3313                                         } else {
3314                                                 XplatUI.EraseWindowBackground(m.HWnd, m.WParam);
3315                                         }
3316                                         // The DefWndProc will never have to handle this, we don't ever set hbr on the window
3317                                         m.Result = (IntPtr)1;
3318                                         return;
3319                                 }
3320
3321                                 case Msg.WM_LBUTTONUP: {
3322                                         HandleClick(mouse_clicks);
3323                                         OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Left, 
3324                                                 mouse_clicks, 
3325                                                 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3326                                                 0));
3327                                         if (mouse_clicks > 1) {
3328                                                 mouse_clicks = 1;
3329                                         }
3330                                         return;
3331                                 }
3332                                         
3333                                 case Msg.WM_LBUTTONDOWN: {
3334                                         if (CanSelect && !is_selected) {
3335                                                 Select(this);
3336                                         }
3337                                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3338                                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3339                                                 0));
3340                                                 
3341                                         return;
3342                                 }
3343
3344                                 case Msg.WM_LBUTTONDBLCLK: {
3345                                         mouse_clicks++;
3346                                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3347                                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3348                                                 0));
3349
3350                                         return;
3351                                 }
3352
3353                                 case Msg.WM_MBUTTONUP: {
3354                                         HandleClick(mouse_clicks);
3355                                         OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Middle, 
3356                                                 mouse_clicks, 
3357                                                 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3358                                                 0));
3359                                         if (mouse_clicks > 1) {
3360                                                 mouse_clicks = 1;
3361                                         }
3362                                         return;
3363                                 }
3364                                         
3365                                 case Msg.WM_MBUTTONDOWN: {                                      
3366                                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3367                                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3368                                                 0));
3369                                                 
3370                                         return;
3371                                 }
3372
3373                                 case Msg.WM_MBUTTONDBLCLK: {
3374                                         mouse_clicks++;
3375                                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3376                                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3377                                                 0));
3378                                         return;
3379                                 }
3380
3381                                 case Msg.WM_RBUTTONUP: {
3382                                         if (context_menu != null) {
3383                                                 context_menu.Show(this, new Point(LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ())));
3384                                         }
3385
3386                                         HandleClick(mouse_clicks);
3387                                         OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Right, 
3388                                                 mouse_clicks, 
3389                                                 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3390                                                 0));
3391                                         if (mouse_clicks > 1) {
3392                                                 mouse_clicks = 1;
3393                                         }
3394                                         return;
3395                                 }
3396                                         
3397                                 case Msg.WM_RBUTTONDOWN: {                                      
3398                                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3399                                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3400                                                 0));
3401                                         return;
3402                                 }
3403
3404                                 case Msg.WM_RBUTTONDBLCLK: {
3405                                         mouse_clicks++;
3406                                         OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3407                                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3408                                                 0));
3409                                         return;
3410                                 }
3411
3412                                 case Msg.WM_MOUSEWHEEL: {                               
3413
3414                                         OnMouseWheel (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3415                                                 mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3416                                                 HighOrder(m.WParam.ToInt32())));
3417                                         return;
3418                                 }
3419
3420                                         
3421                                 case Msg.WM_MOUSEMOVE: {                                        
3422                                         OnMouseMove  (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
3423                                                 mouse_clicks, 
3424                                                 LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 
3425                                                 0));
3426                                         return;
3427                                 }
3428
3429                                 case Msg.WM_MOUSE_ENTER: {
3430                                         if (is_entered) {
3431                                                 return;
3432                                         }
3433                                         is_entered = true;
3434                                         OnMouseEnter(EventArgs.Empty);
3435                                         return;
3436                                 }
3437
3438                                 case Msg.WM_MOUSE_LEAVE: {
3439                                         is_entered=false;
3440                                         OnMouseLeave(EventArgs.Empty);
3441                                         return;
3442                                 }
3443
3444                                 case Msg.WM_MOUSEHOVER: {
3445                                         OnMouseHover(EventArgs.Empty);
3446                                         return;
3447                                 }
3448
3449                                 case Msg.WM_SYSKEYDOWN:
3450                                 case Msg.WM_KEYDOWN:
3451                                 case Msg.WM_SYSKEYUP:
3452                                 case Msg.WM_KEYUP:
3453                                 case Msg.WM_SYSCHAR:
3454                                 case Msg.WM_CHAR: {
3455                                         if (ProcessKeyEventArgs(ref m)) {
3456                                                 return;
3457                                         }
3458
3459                                         if (PreProcessMessage(ref m)) {
3460                                                 return;
3461                                         }
3462
3463                                         if (ProcessKeyMessage(ref m)) {
3464                                                 return;
3465                                         }
3466                                         DefWndProc (ref m);
3467                                         return;
3468                                 }
3469
3470                                 case Msg.WM_HELP: {
3471                                         Point   mouse_pos;
3472                                         if (m.LParam != IntPtr.Zero) {
3473                                                 HELPINFO        hi;
3474
3475                                                 hi = new HELPINFO();
3476
3477                                                 hi = (HELPINFO) Marshal.PtrToStructure (m.LParam, typeof (HELPINFO));
3478                                                 mouse_pos = new Point(hi.MousePos.x, hi.MousePos.y);
3479                                         } else {
3480                                                 mouse_pos = Control.MousePosition;
3481                                         }
3482                                         OnHelpRequested(new HelpEventArgs(mouse_pos));
3483                                         m.Result = (IntPtr)1;
3484                                         return;
3485                                 }
3486
3487                                 case Msg.WM_KILLFOCUS: {
3488                                         OnLeave(EventArgs.Empty);
3489                                         if (CausesValidation) {
3490                                                 CancelEventArgs e;
3491                                                 e = new CancelEventArgs(false);
3492
3493                                                 OnValidating(e);
3494
3495                                                 if (e.Cancel) {
3496                                                         Focus();
3497                                                         return;
3498                                                 }
3499
3500                                                 OnValidated(EventArgs.Empty);
3501                                         }
3502
3503                                         this.has_focus = false;
3504                                         this.is_selected = false;
3505                                         OnLostFocus(EventArgs.Empty);
3506                                         return;
3507                                 }
3508
3509                                 case Msg.WM_SETFOCUS: {
3510                                         OnEnter(EventArgs.Empty);
3511                                         this.has_focus = true;
3512                                         OnGotFocus(EventArgs.Empty);
3513                                         return;
3514                                 }
3515                                         
3516
3517                                 case Msg.WM_SYSCOLORCHANGE: {
3518                                         ThemeEngine.Current.ResetDefaults();
3519                                         OnSystemColorsChanged(EventArgs.Empty);
3520                                         return;
3521                                 }
3522                                         
3523
3524                                 case Msg.WM_SETCURSOR: {
3525                                         if (cursor == null) {
3526                                                 DefWndProc(ref m);
3527                                                 return;
3528                                         }
3529
3530                                         XplatUI.SetCursor(window.Handle, cursor.handle);
3531                                         m.Result = (IntPtr)1;
3532                                         return;
3533                                 }
3534
3535                                 default: {
3536                                         DefWndProc(ref m);      
3537                                         return;
3538                                 }
3539                         }
3540                 }
3541                 #endregion      // Public Instance Methods
3542
3543                 #region OnXXX methods
3544                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3545                 protected virtual void OnBackColorChanged(EventArgs e) {
3546                         if (BackColorChanged!=null) BackColorChanged(this, e);
3547                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackColorChanged(e);
3548                 }
3549
3550                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3551                 protected virtual void OnBackgroundImageChanged(EventArgs e) {
3552                         if (BackgroundImageChanged!=null) BackgroundImageChanged(this, e);
3553                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackgroundImageChanged(e);
3554                 }
3555
3556                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3557                 protected virtual void OnBindingContextChanged(EventArgs e) {
3558                         CheckDataBindings ();
3559                         if (BindingContextChanged!=null) {
3560                                 BindingContextChanged(this, e);
3561                         }
3562                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBindingContextChanged(e);
3563                 }
3564
3565                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3566                 protected virtual void OnCausesValidationChanged(EventArgs e) {
3567                         if (CausesValidationChanged!=null) CausesValidationChanged(this, e);
3568                 }
3569
3570                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3571                 protected virtual void OnChangeUICues(UICuesEventArgs e) {
3572                         if (ChangeUICues!=null) ChangeUICues(this, e);
3573                 }
3574
3575                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3576                 protected virtual void OnClick(EventArgs e) {
3577                         if (Click!=null) Click(this, e);
3578                 }
3579
3580                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3581                 protected virtual void OnContextMenuChanged(EventArgs e) {
3582                         if (ContextMenuChanged!=null) ContextMenuChanged(this, e);
3583                 }
3584
3585                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3586                 protected virtual void OnControlAdded(ControlEventArgs e) {
3587                         if (ControlAdded!=null) ControlAdded(this, e);
3588                 }
3589
3590                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3591                 protected virtual void OnControlRemoved(ControlEventArgs e) {
3592                         if (ControlRemoved!=null) ControlRemoved(this, e);
3593                 }
3594
3595                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3596                 protected virtual void OnCreateControl() {
3597                         // Override me!
3598                 }
3599
3600                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3601                 protected virtual void OnCursorChanged(EventArgs e) {
3602                         if (CursorChanged!=null) CursorChanged(this, e);
3603                 }
3604
3605                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3606                 protected virtual void OnDockChanged(EventArgs e) {
3607                         if (DockChanged!=null) DockChanged(this, e);
3608                 }
3609
3610                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3611                 protected virtual void OnDoubleClick(EventArgs e) {
3612                         if (DoubleClick!=null) DoubleClick(this, e);
3613                 }
3614
3615                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3616                 protected virtual void OnDragDrop(DragEventArgs drgevent) {
3617                         if (DragDrop!=null) DragDrop(this, drgevent);
3618                 }
3619
3620                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3621                 protected virtual void OnDragEnter(DragEventArgs drgevent) {
3622                         if (DragEnter!=null) DragEnter(this, drgevent);
3623                 }
3624
3625                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3626                 protected virtual void OnDragLeave(EventArgs e) {
3627                         if (DragLeave!=null) DragLeave(this, e);
3628                 }
3629
3630                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3631                 protected virtual void OnDragOver(DragEventArgs drgevent) {
3632                         if (DragOver!=null) DragOver(this, drgevent);
3633                 }
3634
3635                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3636                 protected virtual void OnEnabledChanged(EventArgs e) {
3637                         if (EnabledChanged!=null) EnabledChanged(this, e);
3638                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentEnabledChanged(e);
3639                 }
3640
3641                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3642                 protected virtual void OnEnter(EventArgs e) {
3643                         if (Enter!=null) Enter(this, e);
3644                 }
3645
3646                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3647                 protected virtual void OnFontChanged(EventArgs e) {
3648                         if (FontChanged!=null) FontChanged(this, e);
3649                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentFontChanged(e);
3650                 }
3651
3652                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3653                 protected virtual void OnForeColorChanged(EventArgs e) {
3654                         if (ForeColorChanged!=null) ForeColorChanged(this, e);
3655                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentForeColorChanged(e);
3656                 }
3657
3658                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3659                 protected virtual void OnGiveFeedback(GiveFeedbackEventArgs gfbevent) {
3660                         if (GiveFeedback!=null) GiveFeedback(this, gfbevent);
3661                 }
3662                 
3663                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3664                 protected virtual void OnGotFocus(EventArgs e) {
3665                         if (GotFocus!=null) GotFocus(this, e);
3666                 }
3667
3668                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3669                 protected virtual void OnHandleCreated(EventArgs e) {
3670                         if (HandleCreated!=null) HandleCreated(this, e);
3671                 }
3672
3673                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3674                 protected virtual void OnHandleDestroyed(EventArgs e) {
3675                         if (HandleDestroyed!=null) HandleDestroyed(this, e);
3676                 }
3677
3678                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3679                 protected virtual void OnHelpRequested(HelpEventArgs hevent) {
3680                         if (HelpRequested!=null) HelpRequested(this, hevent);
3681                 }
3682
3683                 protected virtual void OnImeModeChanged(EventArgs e) {
3684                         if (ImeModeChanged!=null) ImeModeChanged(this, e);
3685                 }
3686
3687                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3688                 protected virtual void OnInvalidated(InvalidateEventArgs e) {
3689                         needs_redraw = true;
3690                         if (Invalidated!=null) Invalidated(this, e);
3691                 }
3692
3693                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3694                 protected virtual void OnKeyDown(KeyEventArgs e) {                      
3695                         if (KeyDown!=null) KeyDown(this, e);
3696                 }
3697
3698                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3699                 protected virtual void OnKeyPress(KeyPressEventArgs e) {
3700                         if (KeyPress!=null) KeyPress(this, e);
3701                 }
3702
3703                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3704                 protected virtual void OnKeyUp(KeyEventArgs e) {
3705                         if (KeyUp!=null) KeyUp(this, e);
3706                 }
3707
3708                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3709                 protected virtual void OnLayout(LayoutEventArgs levent) {
3710                         if (Layout!=null) Layout(this, levent);
3711                 }
3712
3713                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3714                 protected virtual void OnLeave(EventArgs e) {
3715                         if (Leave!=null) Leave(this, e);
3716                 }
3717
3718                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3719                 protected virtual void OnLocationChanged(EventArgs e) {
3720                         OnMove(e);
3721                         if (LocationChanged!=null) LocationChanged(this, e);
3722                 }
3723
3724                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3725                 protected virtual void OnLostFocus(EventArgs e) {
3726                         if (LostFocus!=null) LostFocus(this, e);
3727                 }
3728
3729                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3730                 protected virtual void OnMouseDown(MouseEventArgs e) {
3731                         if (MouseDown!=null) MouseDown(this, e);
3732                 }
3733
3734                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3735                 protected virtual void OnMouseEnter(EventArgs e) {
3736                         if (MouseEnter!=null) MouseEnter(this, e);
3737                 }
3738
3739                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3740                 protected virtual void OnMouseHover(EventArgs e) {
3741                         if (MouseHover!=null) MouseHover(this, e);
3742                 }
3743
3744                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3745                 protected virtual void OnMouseLeave(EventArgs e) {
3746                         if (MouseLeave!=null) MouseLeave(this, e);
3747                 }
3748
3749                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3750                 protected virtual void OnMouseMove(MouseEventArgs e) {                  
3751                         if (MouseMove!=null) MouseMove(this, e);
3752                 }
3753
3754                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3755                 protected virtual void OnMouseUp(MouseEventArgs e) {
3756                         if (MouseUp!=null) MouseUp(this, e);
3757                 }
3758
3759                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3760                 protected virtual void OnMouseWheel(MouseEventArgs e) {
3761                         if (MouseWheel!=null) MouseWheel(this, e);
3762                 }
3763
3764                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3765                 protected virtual void OnMove(EventArgs e) {
3766                         if (Move!=null) Move(this, e);
3767                 }
3768
3769                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3770                 protected virtual void OnNotifyMessage(Message m) {
3771                         // Override me!
3772                 }
3773
3774                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3775                 protected virtual void OnPaint(PaintEventArgs e) {
3776                         if (Paint!=null) Paint(this, e);
3777                 }
3778
3779                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3780                 protected virtual void OnPaintBackground(PaintEventArgs pevent) {
3781                         PaintControlBackground (pevent);
3782                 }
3783
3784                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3785                 protected virtual void OnParentBackColorChanged(EventArgs e) {
3786                         if (background_color.IsEmpty && background_image==null) {
3787                                 Invalidate();
3788                                 OnBackColorChanged(e);
3789                         }
3790                 }
3791
3792                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3793                 protected virtual void OnParentBackgroundImageChanged(EventArgs e) {
3794                         if (background_color.IsEmpty && background_image==null) {
3795                                 Invalidate();
3796                                 OnBackgroundImageChanged(e);
3797                         }
3798                 }
3799
3800                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3801                 protected virtual void OnParentBindingContextChanged(EventArgs e) {
3802                         if (binding_context==null) {
3803                                 binding_context=Parent.binding_context;
3804                                 OnBindingContextChanged(e);
3805                         }
3806                 }
3807
3808                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3809                 protected virtual void OnParentChanged(EventArgs e) {
3810                         if (ParentChanged!=null) ParentChanged(this, e);
3811                 }
3812
3813                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3814                 protected virtual void OnParentEnabledChanged(EventArgs e) {
3815                         if (is_enabled != Parent.is_enabled) {
3816                                 is_enabled=Parent.is_enabled;
3817                                 Invalidate();
3818                                 if (EnabledChanged != null) {
3819                                         EnabledChanged(this, e);
3820                                 }
3821                         }
3822                 }
3823
3824                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3825                 protected virtual void OnParentFontChanged(EventArgs e) {
3826                         if (font==null) {
3827                                 Invalidate();
3828                                 OnFontChanged(e);
3829                         }
3830                 }
3831
3832                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3833                 protected virtual void OnParentForeColorChanged(EventArgs e) {
3834                         if (foreground_color.IsEmpty) {
3835                                 Invalidate();
3836                                 OnForeColorChanged(e);
3837                         }
3838                 }
3839
3840                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3841                 protected virtual void OnParentRightToLeftChanged(EventArgs e) {
3842                         if (right_to_left==RightToLeft.Inherit) {
3843                                 Invalidate();
3844                                 OnRightToLeftChanged(e);
3845                         }
3846                 }
3847
3848                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3849                 protected virtual void OnParentVisibleChanged(EventArgs e) {
3850                         if (is_visible) {
3851                                 OnVisibleChanged(e);
3852                         }
3853                 }
3854
3855                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3856                 protected virtual void OnQueryContinueDrag(QueryContinueDragEventArgs e) {
3857                         if (QueryContinueDrag!=null) QueryContinueDrag(this, e);
3858                 }
3859
3860                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3861                 protected virtual void OnResize(EventArgs e) {
3862                         if (Resize!=null) Resize(this, e);
3863
3864                         PerformLayout(this, "bounds");
3865
3866                         if (parent != null) {
3867                                 parent.PerformLayout();
3868                         }
3869                 }
3870
3871                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3872                 protected virtual void OnRightToLeftChanged(EventArgs e) {
3873                         if (RightToLeftChanged!=null) RightToLeftChanged(this, e);
3874                         for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentRightToLeftChanged(e);
3875                 }
3876
3877                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3878                 protected virtual void OnSizeChanged(EventArgs e) {
3879                         InvalidateBuffers ();
3880                         OnResize(e);
3881                         if (SizeChanged!=null) SizeChanged(this, e);
3882                 }
3883
3884                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3885                 protected virtual void OnStyleChanged(EventArgs e) {
3886                         if (StyleChanged!=null) StyleChanged(this, e);
3887                 }
3888
3889                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3890                 protected virtual void OnSystemColorsChanged(EventArgs e) {
3891                         if (SystemColorsChanged!=null) SystemColorsChanged(this, e);
3892                 }
3893
3894                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3895                 protected virtual void OnTabIndexChanged(EventArgs e) {
3896                         if (TabIndexChanged!=null) TabIndexChanged(this, e);
3897                 }
3898
3899                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3900                 protected virtual void OnTabStopChanged(EventArgs e) {
3901                         if (TabStopChanged!=null) TabStopChanged(this, e);
3902                 }
3903
3904                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3905                 protected virtual void OnTextChanged(EventArgs e) {
3906                         if (TextChanged!=null) TextChanged(this, e);
3907                 }
3908
3909                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3910                 protected virtual void OnValidated(EventArgs e) {
3911                         if (Validated!=null) Validated(this, e);
3912                 }
3913
3914                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3915                 protected virtual void OnValidating(System.ComponentModel.CancelEventArgs e) {
3916                         if (Validating!=null) Validating(this, e);
3917                 }
3918
3919                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3920                 protected virtual void OnVisibleChanged(EventArgs e) {
3921                         if (!is_visible) {
3922                                 if (dc_mem!=null) {
3923                                         dc_mem.Dispose ();
3924                                         dc_mem=null;
3925                                 }
3926
3927                                 if (bmp_mem!=null) {
3928                                         bmp_mem.Dispose();
3929                                         bmp_mem=null;
3930                                 }
3931                         } else {
3932                                 if (!is_disposed) {
3933                                         if (!this.IsHandleCreated) {
3934                                                 this.CreateControl();
3935                                         }
3936                                         PerformLayout();
3937                                 }
3938                         }
3939                         
3940                         if (VisibleChanged!=null) VisibleChanged(this, e);
3941
3942                         // We need to tell our kids
3943                         for (int i=0; i<child_controls.Count; i++) {
3944                                 child_controls[i].OnParentVisibleChanged(e);
3945                         }
3946                 }
3947                 #endregion      // OnXXX methods
3948
3949                 #region Events
3950                 public event EventHandler               BackColorChanged;
3951                 public event EventHandler               BackgroundImageChanged;
3952                 public event EventHandler               BindingContextChanged;
3953                 public event EventHandler               CausesValidationChanged;
3954                 public event UICuesEventHandler         ChangeUICues;
3955                 public event EventHandler               Click;
3956                 public event EventHandler               ContextMenuChanged;
3957
3958                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3959                 [Browsable(false)]
3960                 public event ControlEventHandler        ControlAdded;
3961
3962                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3963                 [Browsable(false)]
3964                 public event ControlEventHandler        ControlRemoved;
3965
3966                 public event EventHandler               CursorChanged;
3967                 public event EventHandler               DockChanged;
3968                 public event EventHandler               DoubleClick;
3969                 public event DragEventHandler           DragDrop;
3970                 public event DragEventHandler           DragEnter;
3971                 public event EventHandler               DragLeave;
3972                 public event DragEventHandler           DragOver;
3973                 public event EventHandler               EnabledChanged;
3974                 public event EventHandler               Enter;
3975                 public event EventHandler               FontChanged;
3976                 public event EventHandler               ForeColorChanged;
3977                 public event GiveFeedbackEventHandler   GiveFeedback;
3978
3979                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3980                 [Browsable(false)]
3981                 public event EventHandler               GotFocus;
3982
3983                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3984                 [Browsable(false)]
3985                 public event EventHandler               HandleCreated;
3986
3987                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3988                 [Browsable(false)]
3989                 public event EventHandler               HandleDestroyed;
3990
3991                 public event HelpEventHandler           HelpRequested;
3992                 public event EventHandler               ImeModeChanged;
3993
3994                 [EditorBrowsable(EditorBrowsableState.Advanced)]
3995                 [Browsable(false)]
3996                 public event InvalidateEventHandler     Invalidated;
3997
3998                 public event KeyEventHandler            KeyDown;
3999                 public event KeyPressEventHandler       KeyPress;
4000                 public event KeyEventHandler            KeyUp;
4001                 public event LayoutEventHandler         Layout;
4002                 public event EventHandler               Leave;
4003                 public event EventHandler               LocationChanged;
4004
4005                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4006                 [Browsable(false)]
4007                 public event EventHandler               LostFocus;
4008
4009                 public event MouseEventHandler          MouseDown;
4010                 public event EventHandler               MouseEnter;
4011                 public event EventHandler               MouseHover;
4012                 public event EventHandler               MouseLeave;
4013                 public event MouseEventHandler          MouseMove;
4014                 public event MouseEventHandler          MouseUp;
4015
4016                 [EditorBrowsable(EditorBrowsableState.Advanced)]
4017                 [Browsable(false)]
4018                 public event MouseEventHandler          MouseWheel;
4019
4020                 public event EventHandler               Move;
4021                 public event PaintEventHandler          Paint;
4022                 public event EventHandler               ParentChanged;
4023                 public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp;
4024                 public event QueryContinueDragEventHandler      QueryContinueDrag;
4025                 public event EventHandler               Resize;
4026                 public event EventHandler               RightToLeftChanged;
4027                 public event EventHandler               SizeChanged;
4028                 public event EventHandler               StyleChanged;
4029                 public event EventHandler               SystemColorsChanged;
4030                 public event EventHandler               TabIndexChanged;
4031                 public event EventHandler               TabStopChanged;
4032                 public event EventHandler               TextChanged;
4033                 public event EventHandler               Validated;
4034                 public event CancelEventHandler         Validating;
4035                 public event EventHandler               VisibleChanged;
4036                 #endregion      // Events
4037         }
4038 }