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