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