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