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