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