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