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