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