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