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