2 // System.Windows.Forms.Control.cs
5 // stubbed out by Jaak Simm (jaaksimm@firm.ee)
6 // Dennis Hayes (dennish@rayetk.com)
7 // WINELib implementation started by John Sohn (jsohn@columbus.rr.com)
8 // Alexandre Pigolkine (pigolkine@gmx.de)
9 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
11 // (C) Ximian, Inc., 2002
14 using System.ComponentModel;
16 using System.Collections;
17 using System.Threading;
19 using System.Runtime.InteropServices;
20 using System.Collections.Specialized;
22 namespace System.Windows.Forms {
25 /// Defines the base class for controls, which are components with
26 /// visual representation.
29 public class Control : Component , ISynchronizeInvoke, IWin32Window {
32 // Helper NativeWindow class to dispatch messages back
33 // to the Control class
34 protected class ControlNativeWindow : NativeWindow {
36 private Control control;
38 public ControlNativeWindow (Control control) : base() {
39 this.control = control;
42 protected override void WndProc (ref Message m) {
43 //Console.WriteLine ("Control WndProc Message HWnd {0}, Msg {1}", m.HWnd, m.Msg);
44 // Do not call default WndProc here
45 // let the control decide what to do
46 // base.WndProc (ref m);
47 control.WndProc (ref m);
51 // FIXME: not sure if dervied classes should have access
52 protected ControlNativeWindow window;
53 private ControlCollection childControls;
54 private Control parent;
55 static private Hashtable controlsCollection = new Hashtable ();
56 static private NativeWindow parkingWindow = null;
59 // it seems these are stored in case the window is not created,
60 // corresponding properties (below) need to check if window
61 // is created or not and react accordingly
62 string accessibleDefaultActionDescription;
63 string accessibleDescription;
64 string accessibleName;
65 AccessibleRole accessibleRole;
69 Image backgroundImage;
70 //BindingContext bindingContext;
74 bool causesValidation;
75 //ContextMenu contextMenu;
82 // Point location; // using bounds to store location
85 RightToLeft rightToLeft;
89 protected ControlStyles controlStyles_;
95 private static readonly int LAYOUT_SUSPENDED = BitVector32.CreateMask();
96 private static readonly int LAYOUT_PENDING = BitVector32.CreateMask( LAYOUT_SUSPENDED );
97 private static readonly int DISPOSED = BitVector32.CreateMask( LAYOUT_PENDING );
98 private static readonly int RECREATING_HANDLE= BitVector32.CreateMask( DISPOSED );
99 private static readonly int CREATED = BitVector32.CreateMask( RECREATING_HANDLE );
102 protected bool mouseIsInside_;
104 // BeginInvoke() etc. helpers
105 static int InvokeMessage = Win32.RegisterWindowMessage("mono_control_invoke_helper");
107 // CHECKME: This variable is used to determine whether current thread
108 // was used to create Control Handle. It take some space but saves a call
109 // to unmanaged code in ISynchronizeInvoke.IsInvokeRequired.
110 private int CreatorThreadId_ = 0;
112 private Queue InvokeQueue_ = new Queue();
114 internal class ControlInvokeHelper : IAsyncResult {
115 private Delegate Method_ = null;
116 private object[] MethodArgs_ = null;
117 private object MethodResult_ = null;
118 private ManualResetEvent AsyncWaitHandle_ = new ManualResetEvent(false);
119 private bool CompletedSynchronously_ = false;
120 private bool IsCompleted_ = false;
122 public ControlInvokeHelper( Delegate method, object[] args) {
127 // IAsyncResult interface
128 object IAsyncResult.AsyncState {
130 if( MethodArgs_ != null && MethodArgs_.Length != 0) {
131 return MethodArgs_[MethodArgs_.Length - 1];
137 WaitHandle IAsyncResult.AsyncWaitHandle {
139 return AsyncWaitHandle_;
143 bool IAsyncResult.CompletedSynchronously {
145 return CompletedSynchronously_;
149 bool IAsyncResult.IsCompleted {
155 internal bool CompletedSynchronously {
157 CompletedSynchronously_ = value;
161 internal object MethodResult {
163 return MethodResult_;
167 internal void ExecuteMethod() {
168 object result = Method_.DynamicInvoke(MethodArgs_);
170 MethodResult_ = result;
173 AsyncWaitHandle_.Set();
177 // --- Constructors ---
179 //Compact Framework //only Control()
182 childControls = CreateControlsInstance ();
184 statuses = new BitVector32();
186 accessibleDefaultActionDescription = null;
187 accessibleDescription = null;
188 accessibleName = null;
189 accessibleRole = AccessibleRole.Default;
191 anchor = AnchorStyles.Top | AnchorStyles.Left;
192 backColor = Control.DefaultBackColor;
193 backgroundImage = null;
194 bounds = new Rectangle();
195 oldBounds = new Rectangle();
196 // bindingContext = null;
197 causesValidation = true;
198 // contextMenu = null;
199 dock = DockStyle.None;
201 // font = Control.DefaultFont;
202 foreColor = Control.DefaultForeColor;
203 imeMode = ImeMode.Inherit;
204 isAccessible = false;
205 // location = new Point (0,0); should be from OS
208 rightToLeft = RightToLeft.Inherit;
214 oldBounds.Width = bounds.Width = DefaultSize.Width;
215 oldBounds.Height = bounds.Height= DefaultSize.Height;
216 clientWidth = DefaultSize.Width;
217 clientHeight = DefaultSize.Height;
219 mouseIsInside_ = false;
220 controlStyles_ = ControlStyles.Selectable | ControlStyles.StandardClick | ControlStyles.StandardDoubleClick;
221 // Do not create Handle here, only in CreateHandle
222 // CreateHandle();//sets window handle. FIXME: No it does not
225 // according to docs, the constructors do not create
227 public Control (string text) : this()
230 // SetWindowTextA (Handle, text);
233 public Control (Control parent, string text) : this (text)
236 // SetParent (Handle, parent.Handle);
239 public Control (string text, int left, int top,
240 int width, int height) : this(text)
246 //SetWindowPos (Handle, (IntPtr) 0, left, top,
247 // width, height, 0);
250 public Control (Control parent,string text,int left, int top,
251 int width,int height) : this (parent, text)
257 // SetWindowPos (Handle, (IntPtr) 0, left, top,
258 // width, height, 0);
261 // for internal use only, create a control class
262 // for an existing, created HWND
263 private Control (IntPtr existingHandle)
265 window = (ControlNativeWindow) NativeWindow.FromHandle (
269 // --- Properties ---
270 // Properties only supporting .NET framework, not stubbed out:
271 // - protected bool RenderRightToLeft {get;}
272 // - public IWindowTarget WindowTarget {get; set;}
274 //public AccessibleObject AccessibilityObject {
276 // throw new NotImplementedException ();
280 public string AccessibleDefaultActionDescription {
282 return accessibleDefaultActionDescription;
285 accessibleDefaultActionDescription = value;
289 public string AccessibleDescription {
291 return accessibleDescription;
294 accessibleDescription=value;
298 public string AccessibleName {
300 return accessibleName;
303 accessibleName=value;
307 public AccessibleRole AccessibleRole {
309 return accessibleRole;
312 accessibleRole=value;
316 public virtual bool AllowDrop {
325 public virtual AnchorStyles Anchor {
330 if ( anchor != value ) {
332 if ( anchor != ( AnchorStyles.Left | AnchorStyles.Top ) )
333 Dock = DockStyle.None;
339 public virtual Color BackColor {
344 if( backColor != value) {
346 OnBackColorChanged(new EventArgs());
351 public virtual Image BackgroundImage {
353 return backgroundImage;
356 backgroundImage = value;
357 // FIXME: force redraw
362 public virtual BindingContext BindingContext {
364 //return bindingContext;
365 throw new NotImplementedException ();
368 //bindingContext=value;
369 throw new NotImplementedException ();
381 public Rectangle Bounds {
386 SetBounds(value.Left, value.Top, value.Width, value.Height);
390 public bool CanFocus {
392 if (IsHandleCreated && Visible && Enabled)
399 public bool CanSelect {
401 // if (ControlStyles.Selectable &&
402 // isContainedInAnotherControl &&
403 // parentIsVisiable && isVisialbe &&
404 // parentIsEnabled && isEnabled) {
409 throw new NotImplementedException ();
414 public bool Capture {
416 if (IsHandleCreated) {
417 IntPtr captured = Win32.GetCapture ();
418 if (Handle == captured)
424 if (IsHandleCreated) {
426 Win32.SetCapture (Handle);
428 IntPtr captured = Win32.GetCapture ();
430 // if this window is in capture state
432 if (Handle == captured)
433 Win32.ReleaseCapture ();
439 public bool CausesValidation {
441 return causesValidation;
444 causesValidation=value;
449 public Rectangle ClientRectangle {
451 return new Rectangle ( 0, 0, ClientSize.Width, ClientSize.Height );
458 public Size ClientSize {
460 return new Size ( clientWidth, clientHeight);
463 SetClientSizeCore ( value.Width, value.Height );
468 public string CompanyName {
470 //Better than throwing an execption
471 return "Company Name";
475 internal void SetClientSize(Size value, int styleIfNoWindow, bool menuIfNoWindow) {
476 RECT rc = new RECT();
477 rc.left = rc.top = 0;
478 rc.right = value.Width;
479 rc.bottom = value.Height;
481 if( IsHandleCreated ){
482 int style = Win32.GetWindowLong( Handle, GetWindowLongFlag.GWL_STYLE).ToInt32();
484 if( (style & (int)WindowStyles.WS_CHILD) == 0 ){
485 menuExists = Win32.GetMenu(Handle) != IntPtr.Zero ? 1 : 0;
487 Win32.AdjustWindowRect( ref rc, style, menuExists);
488 Win32.SetWindowPos( Handle, SetWindowPosZOrder.HWND_TOP, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
489 SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOZORDER);
492 Win32.AdjustWindowRect( ref rc, styleIfNoWindow, menuIfNoWindow ? 1 : 0);
494 Size = new Size(rc.right - rc.left, rc.bottom - rc.top);
497 public bool ContainsFocus {
499 if (IsHandleCreated) {
500 IntPtr focusedWindow = Win32.GetFocus();
501 if (focusedWindow == Handle)
510 public virtual ContextMenu ContextMenu {
512 //return contextMenu;
513 throw new NotImplementedException ();
517 throw new NotImplementedException ();
521 public ControlCollection Controls {
522 get { return childControls; }
525 public bool Created {
526 get { return statuses[ CREATED ]; }
529 protected virtual CreateParams CreateParams {
531 CreateParams createParams = new CreateParams ();
532 createParams.Caption = Text;
533 createParams.X = Left;
534 createParams.Y = Top;
535 createParams.Width = Width;
536 createParams.Height = Height;
537 createParams.ClassStyle = 0;
538 createParams.ExStyle = 0;
539 createParams.Param = 0;
542 createParams.Parent = parent.Handle;
544 createParams.Parent = ParkingWindowHandle;
546 createParams.Style = (int) WindowStyles.WS_OVERLAPPEDWINDOW;
552 internal protected IntPtr ControlRealWndProc = IntPtr.Zero;
553 internal protected bool SubClassWndProc_ = false;
555 // This function lets Windows or/and default Windows control process message
556 // Classes have to call it if they do not handle message in WndProc or
557 // default handling is needed.
558 protected void CallControlWndProc( ref Message msg) {
559 if( ControlRealWndProc != IntPtr.Zero) {
560 msg.Result = (IntPtr)Win32.CallWindowProc(ControlRealWndProc, msg.HWnd, (int)msg.Msg, msg.WParam.ToInt32(), msg.LParam.ToInt32());
567 // Subclass only native Windows controls. Those classes have to set SubClassWndProc_ to true in contructor
568 private void SubclassWindow() {
569 if( IsHandleCreated && SubClassWndProc_) {
570 ControlRealWndProc = Win32.SetWindowLong( Handle, GetWindowLongFlag.GWL_WNDPROC, NativeWindow.GetWindowProc());
574 private void UnsubclassWindow() {
575 if( IsHandleCreated) {
576 Win32.SetWindowLong( Handle, GetWindowLongFlag.GWL_WNDPROC, ControlRealWndProc.ToInt32());
580 protected virtual void OnWmCommand (ref Message m) {
581 if( m.LParam.ToInt32() != 0) {
582 if( m.LParam != Handle) {
583 // Control notification
584 System.Console.WriteLine("Control notification Code {0} Id = Hwnd {1}", m.HiWordWParam, m.LParam.ToInt32());
585 Control.ReflectMessage(m.LParam, ref m);
588 // Unhandled Control reflection
589 // Derived class didn't handle WM_COMMAND or called base.WndProc in WM_COMMAND handler
590 // CHECKME: Shall we notify user in debug build, throw an exception or just ignore this case ?
596 public virtual Cursor Cursor {
598 throw new NotImplementedException ();
601 throw new NotImplementedException ();
607 // waiting for BindingContext; should be stubbed now
608 public ControlBindingsCollection DataBindings {
610 throw new NotImplementedException ();
614 public static Color DefaultBackColor {
616 // FIXME: use GetSystemMetrics?
617 return SystemColors.Control;
618 //throw new NotImplementedException ();
623 // FIXME: use GetSystemMetrics?
624 public static Font DefaultFont {
625 // FIXME: get current system font from GenericSansSerif
626 // call ArgumentException not called
628 // throw new NotImplementedException ();
629 // return (FontFamily.GenericSansSerif);
630 return Font.FromHfont(Win32.GetStockObject(GSO_.DEFAULT_GUI_FONT));
634 public static Color DefaultForeColor {
636 return SystemColors.ControlText;
640 protected virtual ImeMode DefaultImeMode {
642 return ImeMode.Inherit;
646 protected virtual Size DefaultSize {
648 //Default label size, this should be correct.
649 return new Size(100,23);
653 public virtual Rectangle DisplayRectangle {
655 return ClientRectangle;
660 public bool Disposing {
662 throw new NotImplementedException ();
666 public virtual DockStyle Dock {
671 if ( dock != value ) {
673 if ( dock != DockStyle.None )
674 Anchor = ( AnchorStyles.Left | AnchorStyles.Top );
675 OnDockChanged ( EventArgs.Empty );
681 public virtual bool Enabled {
684 //return Win32.IsWindowEnabled (Handle);
687 if( enabled != value) {
688 Win32.EnableWindow (Handle, value);
690 // FIXME: Disable/enable all children here
697 public virtual bool Focused {
699 return ContainsFocus;
704 public virtual Font Font {
707 if( result == null) {
708 if( Parent != null) {
709 result = Parent.Font;
711 if( result == null) {
712 result = Control.DefaultFont;
719 if( IsHandleCreated) {
720 Win32.SendMessage(Handle, Msg.WM_SETFONT, Font.ToHfont().ToInt32(), 1);
726 protected int FontHeight {
728 throw new NotImplementedException ();
731 throw new NotImplementedException ();
736 public virtual Color ForeColor {
745 public bool HasChildren {
747 if (childControls.Count >0)
756 return bounds.Height;
759 SetBounds(bounds.X, bounds.Y, bounds.Width, value, BoundsSpecified.Height);
763 public ImeMode ImeMode {
773 public bool IsAccessible {
777 } // default is false
783 public bool IsDisposed {
784 get { return statuses [ DISPOSED ]; }
787 public bool IsHandleCreated {
789 return window != null && window.Handle != IntPtr.Zero;
790 //if (Handle != (IntPtr) 0)
802 SetBounds(value, bounds.Y, bounds.Width, bounds.Height, BoundsSpecified.X);
807 public Point Location {
810 return new Point (Top, Left);
813 SetBounds(value.X, value.Y, bounds.Width, bounds.Height, BoundsSpecified.Location);
818 public static Keys ModifierKeys {
820 throw new NotImplementedException ();
826 public static MouseButtons MouseButtons {
828 // FIXME: use GetAsycKeyState?
829 throw new NotImplementedException ();
834 public static Point MousePosition {
836 POINT point = new POINT();
837 Win32.GetCursorPos (ref point);
838 return new Point ( (int) point.x, (int) point.y);
853 public Control Parent {
856 //IntPtr parent = GetParent (Handle);
857 //return FromHandle (parent);
860 if( parent != value) {
861 Console.WriteLine ("setting parent");
864 Console.WriteLine ("add ourself to the parents control");
865 // add ourself to the parents control
866 if ( !parent.Controls.Contains ( this ) )
867 parent.Controls.Add (this);
869 // FIXME: Is this logic correct ?
870 if( BackColor == DefaultBackColor) {
871 if( parent.BackColor != BackColor)
872 OnParentBackColorChanged(new EventArgs());
875 Console.WriteLine ("SetParent");
876 if (IsHandleCreated) {
877 Console.WriteLine ("Handle created");
878 Win32.SetParent (Handle, value.Handle);
881 else if( parent.IsHandleCreated){
882 // CHECKME: Now control is responsible for creating his window
883 // when added to Form, may be things must be reversed.
891 private static IntPtr ParkingWindowHandle {
893 if ( parkingWindow == null )
894 parkingWindow = new NativeWindow ( );
896 if ( parkingWindow.Handle == IntPtr.Zero ) {
897 CreateParams pars = new CreateParams ( );
898 pars.ClassName = "mono_native_window";
899 pars.Style = (int) WindowStyles.WS_OVERLAPPED;
900 parkingWindow.CreateHandle ( pars );
903 return parkingWindow.Handle;
908 public string ProductName {
911 return "Product Name";
916 public string ProductVersion {
919 return "Product Version";
924 public bool RecreatingHandle {
926 return statuses [ RECREATING_HANDLE ] ;
930 public Region Region {
941 protected bool ResizeRedraw {
943 throw new NotImplementedException ();
946 throw new NotImplementedException ();
958 public virtual RightToLeft RightToLeft {
969 protected virtual bool ShowFocusCues {
971 throw new NotImplementedException ();
976 protected bool ShowKeyboardCues {
978 throw new NotImplementedException ();
983 public override ISite Site {
985 throw new NotImplementedException ();
988 throw new NotImplementedException ();
995 return new Size(Width, Height);
998 SetBounds(bounds.X, bounds.Y, value.Width, value.Height, BoundsSpecified.Size);
1002 internal int tabindex;//for debug/test only. remove
1004 public int TabIndex {
1013 public bool TabStop {
1034 public virtual string Text {
1036 // CHECKME: if we really need to provide back current text of real window
1037 // or just our copy in text member.
1038 if (IsHandleCreated) {
1039 int len = Win32.GetWindowTextLengthA (Handle);
1040 // FIXME: len is doubled due to some strange behaviour.(of GetWindowText function ?)
1041 // instead of 10 characters we can get only 9, even if sb.Capacity is 10.
1042 StringBuilder sb = new StringBuilder(len * 2 /*Win32.GetWindowTextLengthA (Handle)*/);
1043 Win32.GetWindowText (Handle, sb, sb.Capacity);
1044 return sb.ToString();
1053 if (IsHandleCreated)
1054 Win32.SetWindowTextA (Handle, value);
1064 SetBounds(bounds.X, value, bounds.Width, bounds.Height, BoundsSpecified.Y);
1069 public Control TopLevelControl {
1071 throw new NotImplementedException ();
1076 public bool Visible {
1091 return bounds.Width;
1094 SetBounds(bounds.X, bounds.Y, value, bounds.Height, BoundsSpecified.Width);
1099 /// internal .NET framework supporting methods, not stubbed out:
1100 /// - protected virtual void NotifyInvalidate(Rectangle invalidatedArea)
1101 /// - protected void RaiseDragEvent(object key,DragEventArgs e);
1102 /// - protected void RaiseKeyEvent(object key,KeyEventArgs e);
1103 /// - protected void RaiseMouseEvent(object key,MouseEventArgs e);
1104 /// - protected void RaisePaintEvent(object key,PaintEventArgs e);
1105 /// - protected void ResetMouseEventArgs();
1108 protected void AccessibilityNotifyClients (
1109 AccessibleEvents accEvent,int childID)
1111 throw new NotImplementedException ();
1116 public void BringToFront ()
1121 public bool Contains (Control ctl)
1123 return childControls.Contains (ctl);
1126 public void CreateControl ()
1130 statuses [ CREATED ] = true;
1134 protected virtual AccessibleObject CreateAccessibilityInstance() {
1135 throw new NotImplementedException ();
1138 protected virtual ControlCollection CreateControlsInstance ()
1140 return new ControlCollection (this);
1145 public Graphics CreateGraphics ()
1147 return Graphics.FromHwnd(Handle);
1150 protected virtual void CreateHandle ()
1153 throw new ObjectDisposedException ( Name );
1155 if( IsHandleCreated )
1159 window = new ControlNativeWindow (this);
1161 CreateParams createParams = CreateParams;
1163 createParams.Style |= (int)WindowStyles.WS_DISABLED;
1165 window.CreateHandle (createParams);
1167 if( window.Handle != IntPtr.Zero) {
1168 if( !controlsCollection.Contains( window.Handle ) )
1169 controlsCollection.Add( window.Handle, this );
1173 CreatorThreadId_ = Win32.GetCurrentThreadId();
1175 OnHandleCreated ( EventArgs.Empty );
1179 protected virtual void DefWndProc (ref Message m)
1181 window.DefWndProc(ref m);
1184 protected virtual void DestroyHandle ()
1186 if ( IsHandleCreated ) {
1187 if( Handle != IntPtr.Zero) {
1188 controlsCollection.Remove(Handle);
1190 if( window != null) {
1191 window.DestroyHandle ();
1196 protected override void Dispose (bool disposing)
1198 statuses [ DISPOSED ] = true;
1200 base.Dispose(disposing);
1204 public DragDropEffects DoDragDrop (
1205 object data, DragDropEffects allowedEffects)
1207 throw new NotImplementedException ();
1210 //public object EndInvoke(IAsyncResult asyncResult):
1211 //look under ISynchronizeInvoke methods
1214 public Form FindForm ()
1216 throw new NotImplementedException ();
1220 public bool Focus ()
1222 if (Win32.SetFocus (Handle) != (IntPtr) 0)
1228 public static Control FromChildHandle (IntPtr handle)
1230 Control control = null;
1231 IntPtr controlHwnd = handle;
1232 while( controlHwnd != IntPtr.Zero) {
1233 control = controlsCollection[controlHwnd] as Control;
1234 if( control != null) break;
1235 controlHwnd = Win32.GetParent(controlHwnd);
1240 public static Control FromHandle (IntPtr handle)
1242 // FIXME: Here we have to check, whether control already exists
1243 //Control control = new Control (handle);
1244 Control control = controlsCollection[handle] as Control;
1249 public Control GetChildAtPoint (Point pt)
1251 throw new NotImplementedException ();
1255 //public IContainerControl GetContainerControl ()
1257 // throw new NotImplementedException ();
1261 public Control GetNextControl (Control ctl, bool forward)
1263 throw new NotImplementedException ();
1267 protected bool GetStyle (ControlStyles flag)
1269 return (controlStyles_ & flag) != 0;
1273 protected bool GetTopLevel ()
1275 throw new NotImplementedException ();
1281 if (IsHandleCreated)
1282 Win32.ShowWindow (Handle, ShowWindowStyles.SW_HIDE);
1286 protected virtual void InitLayout ()
1292 public void Invalidate ()
1294 if (IsHandleCreated) {
1295 Win32.InvalidateRect(Handle, IntPtr.Zero, 1);
1300 public void Invalidate (bool invalidateChildren)
1305 // tries to find appropriate owner for modal form
1306 internal static Control getOwnerWindow ( Control skip )
1308 // temporary solution
1309 IEnumerator cw = controlsCollection.GetEnumerator ( );
1311 while ( cw.MoveNext ( ) ) {
1312 Control c = ( (DictionaryEntry) cw.Current).Value as Control;
1313 if ( c != null && c != skip ) {
1314 IntPtr parent = Win32.GetParent ( c.Handle );
1315 IntPtr owner = Win32.GetWindow ( c.Handle, (uint) GetWindowConstants.GW_OWNER );
1316 if ( parent == IntPtr.Zero && owner == IntPtr.Zero )
1324 public void Invalidate (Rectangle rc)
1326 if (IsHandleCreated) {
1327 RECT rect = new RECT();
1328 rect.left = rc.Left;
1330 rect.right = rc.Right;
1331 rect.bottom = rc.Bottom;
1332 Win32.InvalidateRect (Handle, ref rect, true);
1337 public void Invalidate(Region region)
1343 public void Invalidate (Rectangle rc, bool invalidateChildren)
1349 public void Invalidate(Region region,bool invalidateChildren)
1355 protected void InvokeGotFocus (Control toInvoke, EventArgs e)
1361 protected void InvokeLostFocus (Control toInvoke, EventArgs e)
1367 protected void InvokeOnClick (Control toInvoke, EventArgs e)
1373 protected void InvokePaint (Control c, PaintEventArgs e)
1379 protected void InvokePaintBackground (
1380 Control c,PaintEventArgs e)
1386 protected virtual bool IsInputChar (char charCode)
1388 throw new NotImplementedException ();
1392 protected virtual bool IsInputKey (Keys keyData)
1394 throw new NotImplementedException ();
1398 public static bool IsMnemonic (char charCode,string text)
1400 throw new NotImplementedException ();
1403 // methods used with events:
1404 protected virtual void OnBackColorChanged (EventArgs e)
1406 if (BackColorChanged != null)
1407 BackColorChanged (this, e);
1409 foreach( Control ctl in Controls) {
1410 ctl.OnParentBackColorChanged(e);
1414 protected virtual void OnBackgroundImageChanged (EventArgs e)
1416 if (BackgroundImageChanged != null)
1417 BackgroundImageChanged (this, e);
1420 protected virtual void OnBindingContextChanged (EventArgs e)
1422 if (BindingContextChanged != null)
1423 BindingContextChanged (this, e);
1426 protected virtual void OnCausesValidationChanged (EventArgs e)
1428 if (CausesValidationChanged != null)
1429 CausesValidationChanged (this, e);
1432 protected virtual void OnChangeUICues(UICuesEventArgs e)
1434 if (ChangeUICues != null)
1435 ChangeUICues (this, e);
1439 protected virtual void OnClick (EventArgs e)
1446 protected virtual void OnContextMenuChanged (EventArgs e)
1448 if (ContextMenuChanged != null)
1449 ContextMenuChanged (this, e);
1452 protected virtual void OnControlAdded (ControlEventArgs e)
1454 if (ControlAdded != null)
1455 ControlAdded (this, e);
1458 protected virtual void OnControlRemoved (ControlEventArgs e)
1460 if (ControlRemoved != null)
1461 ControlRemoved (this, e);
1464 protected virtual void OnCreateControl ()
1467 // create all child windows
1468 IEnumerator cw = childControls.GetEnumerator();
1470 while (cw.MoveNext()) {
1471 Console.WriteLine ("Adding Control");
1472 Control control = (Control) cw.Current;
1473 control.CreateControl ();
1478 protected virtual void OnCursorChanged (EventArgs e)
1480 if (CursorChanged != null)
1481 CursorChanged (this, e);
1484 protected virtual void OnDockChanged (EventArgs e)
1486 PerformLayout ( this, "Dock" );
1487 if (DockChanged != null)
1488 DockChanged (this, e);
1491 protected virtual void OnDoubleClick (EventArgs e)
1493 if (DoubleClick != null)
1494 DoubleClick (this, e);
1497 protected virtual void OnDragDrop (DragEventArgs e)
1499 if (DragDrop != null)
1503 protected virtual void OnDragEnter (DragEventArgs e)
1505 if (DragEnter != null)
1506 DragEnter (this, e);
1509 protected virtual void OnDragLeave (EventArgs e)
1511 if (DragLeave != null)
1512 DragLeave (this, e);
1515 protected virtual void OnDragOver (DragEventArgs e)
1517 if (DragOver != null)
1522 protected virtual void OnEnabledChanged (EventArgs e)
1524 if (EnabledChanged != null)
1525 EnabledChanged (this, e);
1528 protected virtual void OnEnter (EventArgs e)
1534 protected virtual void OnFontChanged (EventArgs e)
1536 if (FontChanged != null)
1537 FontChanged (this, e);
1540 protected virtual void OnForeColorChanged (EventArgs e)
1542 if (ForeColorChanged != null)
1543 ForeColorChanged (this, e);
1546 protected virtual void OnGiveFeedback (GiveFeedbackEventArgs e)
1548 if (GiveFeedback != null)
1549 GiveFeedback (this, e);
1553 protected virtual void OnGotFocus (EventArgs e)
1555 if (GotFocus != null)
1559 protected virtual void OnHandleCreated (EventArgs e)
1561 Console.WriteLine ("OnHandleCreated");
1563 //if( font != null) {
1564 // Win32.SendMessage( Handle, Msg.WM_SETFONT, font.ToHfont().ToInt32(), 0);
1566 Win32.SendMessage( Handle, Msg.WM_SETFONT, Font.ToHfont().ToInt32(), 0);
1567 Win32.SetWindowText( Handle, text);
1569 if (HandleCreated != null)
1570 HandleCreated (this, e);
1574 protected virtual void OnHandleDestroyed (EventArgs e)
1576 if( Handle != IntPtr.Zero) {
1577 controlsCollection.Remove(Handle);
1580 if (HandleDestroyed != null) {
1581 HandleDestroyed (this, e);
1585 protected virtual void OnHelpRequested (HelpEventArgs e)
1587 if (HelpRequested != null)
1588 HelpRequested (this, e);
1591 protected virtual void OnImeModeChanged (EventArgs e)
1593 if (ImeModeChanged != null)
1594 ImeModeChanged (this, e);
1597 protected virtual void OnInvalidated (InvalidateEventArgs e)
1599 if (Invalidated != null)
1600 Invalidated (this, e);
1604 protected virtual void OnKeyDown (KeyEventArgs e)
1606 if (KeyDown != null)
1611 protected virtual void OnKeyPress (KeyPressEventArgs e)
1613 if (KeyPress != null)
1618 protected virtual void OnKeyUp (KeyEventArgs e)
1625 protected virtual void OnLayout (LayoutEventArgs e)
1627 DoDockAndAnchorLayout ( e );
1632 protected virtual void OnLeave (EventArgs e)
1638 protected virtual void OnLocationChanged (EventArgs e)
1640 PerformLayout ( this, "Location" );
1641 if (LocationChanged != null)
1642 LocationChanged (this, e);
1646 protected virtual void OnLostFocus (EventArgs e)
1648 if (LostFocus != null)
1649 LostFocus (this, e);
1653 protected virtual void OnMouseDown (MouseEventArgs e)
1655 if (MouseDown != null)
1656 MouseDown (this, e);
1659 protected virtual void OnMouseEnter (EventArgs e)
1661 //System.Console.WriteLine("OnMouseEnter");
1662 if (MouseEnter != null)
1663 MouseEnter (this, e);
1666 protected virtual void OnMouseHover (EventArgs e)
1668 if (MouseHover != null)
1669 MouseHover (this, e);
1672 protected virtual void OnMouseLeave (EventArgs e)
1674 //System.Console.WriteLine("OnMouseLeave");
1676 mouseIsInside_ = false;
1677 if (MouseLeave != null)
1678 MouseLeave (this, e);
1682 protected virtual void OnMouseMove (MouseEventArgs e)
1684 // If enter and mouse pressed - do not process
1685 if( ((e.Button & MouseButtons.Left) != 0) && !mouseIsInside_) return;
1687 if( !mouseIsInside_) {
1688 TRACKMOUSEEVENT tme = new TRACKMOUSEEVENT();
1691 tme.dwFlags = (int)TrackerEventFlags.TME_LEAVE;
1692 tme.dwHoverTime = 0;
1694 bool result = Win32.TrackMouseEvent(ref tme);
1696 System.Console.WriteLine("{0}",Win32.FormatMessage(Win32.GetLastError()));
1700 POINT pt = new POINT();
1703 Win32.ClientToScreen(Handle, ref pt);
1704 IntPtr wndUnderMouse = Win32.WindowFromPoint(pt);
1706 if( wndUnderMouse != Handle) {
1707 // we are outside of the window
1708 if( mouseIsInside_) {
1709 OnMouseLeave(new EventArgs());
1710 mouseIsInside_ = false;
1714 if( !mouseIsInside_) {
1715 mouseIsInside_ = true;
1716 OnMouseEnter(new EventArgs());
1719 if (MouseMove != null)
1720 MouseMove (this, e);
1724 protected virtual void OnMouseUp (MouseEventArgs e)
1726 if (MouseUp != null)
1730 protected virtual void OnMouseWheel (MouseEventArgs e)
1732 if (MouseWheel != null)
1733 MouseWheel (this, e);
1736 protected virtual void OnMove (EventArgs e)
1742 protected virtual void OnNotifyMessage (Message m)
1748 protected virtual void OnPaint (PaintEventArgs e)
1755 protected virtual void OnPaintBackground (PaintEventArgs e)
1757 if( GetStyle(ControlStyles.UserPaint)) {
1758 Brush br = new SolidBrush(BackColor);
1759 e.Graphics.FillRectangle(br, e.ClipRectangle);
1764 protected virtual void OnParentBackColorChanged (EventArgs e)
1766 BackColor = Parent.BackColor;
1767 // FIXME: setting BackColor fires the BackColorChanged event,
1768 // so we do not need to call this here
1770 if (BackColorChanged != null)
1771 BackColorChanged (this, e);
1775 protected virtual void OnParentBackgroundImageChanged (
1778 if (BackgroundImageChanged != null)
1779 BackgroundImageChanged (this, e);
1782 protected virtual void OnParentBindingContextChanged (
1785 if (BindingContextChanged != null)
1786 BindingContextChanged (this, e);
1790 protected virtual void OnParentChanged (EventArgs e)
1792 if (ParentChanged != null)
1793 ParentChanged (this, e);
1796 protected virtual void OnParentEnabledChanged (EventArgs e)
1798 if (EnabledChanged != null)
1799 EnabledChanged (this, e);
1802 protected virtual void OnParentFontChanged (EventArgs e)
1804 if (FontChanged != null)
1805 FontChanged (this, e);
1808 protected virtual void OnParentForeColorChanged (EventArgs e)
1810 if (ForeColorChanged != null)
1811 ForeColorChanged (this, e);
1814 protected virtual void OnParentRightToLeftChanged (
1817 if (RightToLeftChanged != null)
1818 RightToLeftChanged (this, e);
1821 protected virtual void OnParentVisibleChanged (EventArgs e)
1823 if (VisibleChanged != null)
1824 VisibleChanged (this, e);
1827 protected virtual void OnQueryContinueDrag (
1828 QueryContinueDragEventArgs e)
1830 if (QueryContinueDrag != null)
1831 QueryContinueDrag (this, e);
1835 protected virtual void OnResize (EventArgs e)
1843 protected virtual void OnRightToLeftChanged (EventArgs e)
1845 if (RightToLeftChanged != null)
1846 RightToLeftChanged (this, e);
1849 protected virtual void OnSizeChanged (EventArgs e)
1851 PerformLayout ( this, "Size" );
1852 if (SizeChanged != null)
1853 SizeChanged (this, e);
1856 protected virtual void OnStyleChanged (EventArgs e)
1858 if (StyleChanged != null)
1859 StyleChanged (this, e);
1862 protected virtual void OnSystemColorsChanged (EventArgs e)
1864 if (SystemColorsChanged != null)
1865 SystemColorsChanged (this, e);
1868 protected virtual void OnTabIndexChanged (EventArgs e)
1870 if (TabIndexChanged != null)
1871 TabIndexChanged (this, e);
1874 protected virtual void OnTabStopChanged (EventArgs e)
1876 if (TabStopChanged != null)
1877 TabStopChanged (this, e);
1881 protected virtual void OnTextChanged (EventArgs e)
1883 if (TextChanged != null)
1884 TextChanged (this, e);
1887 //[MonoTODO] // this doesn't seem to be documented
1888 // protected virtual void OnTextAlignChanged (EventArgs e) {
1889 // TextAlignChanged (this, e);
1892 protected virtual void OnValidated (EventArgs e)
1894 if (Validated != null)
1895 Validated (this, e);
1899 // CancelEventArgs not ready
1900 //protected virtual void OnValidating(CancelEventArgs e)
1902 // throw new NotImplementedException ();
1906 protected virtual void OnVisibleChanged (EventArgs e)
1908 if (VisibleChanged != null)
1909 VisibleChanged (this, e);
1913 // --- end of methods for events ---
1917 public void PerformLayout ()
1919 PerformLayout ( null, null );
1923 public void PerformLayout (Control affectedControl,
1924 string affectedProperty)
1926 if ( !statuses [ LAYOUT_SUSPENDED ] )
1927 OnLayout ( new LayoutEventArgs ( affectedControl, affectedProperty ) );
1929 statuses [ LAYOUT_PENDING ] = true;
1934 public Point PointToClient (Point p)
1936 throw new NotImplementedException ();
1941 public Point PointToScreen (Point p)
1943 throw new NotImplementedException ();
1947 public virtual bool PreProcessMessage (ref Message msg)
1949 throw new NotImplementedException ();
1953 protected virtual bool ProcessCmdKey (ref Message msg,
1956 throw new NotImplementedException ();
1960 protected virtual bool ProcessDialogChar (char charCode)
1962 throw new NotImplementedException ();
1966 protected virtual bool ProcessDialogKey (Keys keyData)
1968 throw new NotImplementedException ();
1972 protected virtual bool ProcessKeyEventArgs (ref Message m)
1974 throw new NotImplementedException ();
1978 protected internal virtual bool ProcessKeyMessage (
1981 throw new NotImplementedException ();
1985 protected virtual bool ProcessKeyPreview (ref Message m)
1987 throw new NotImplementedException ();
1991 protected virtual bool ProcessMnemonic (char charCode)
1993 throw new NotImplementedException ();
1996 // used when properties/values of Control
1997 // are big enough to warrant recreating the HWND
1998 protected void RecreateHandle()
2000 statuses [ RECREATING_HANDLE ] = true;
2001 if( IsHandleCreated) {
2005 IEnumerator cw = childControls.GetEnumerator();
2006 while ( cw.MoveNext() )
2007 (( Control )cw.Current).RecreateHandle ( );
2010 statuses [ RECREATING_HANDLE ] = false;
2015 public Rectangle RectangleToClient (Rectangle r)
2017 // FIXME: What to return if Handle is not created yet ?
2018 RECT rect = new RECT();
2021 rect.right = r.Right;
2022 rect.bottom = r.Bottom;
2023 Win32.ScreenToClient(Handle,ref rect);
2024 return new Rectangle( rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
2029 public Rectangle RectangleToScreen (Rectangle r)
2031 // FIXME: What to return if Handle is not created yet ?
2032 RECT rect = new RECT();
2035 rect.right = r.Right;
2036 rect.bottom = r.Bottom;
2037 Win32.ClientToScreen(Handle,ref rect);
2038 return new Rectangle( rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
2042 protected static bool ReflectMessage (IntPtr hWnd, ref Message m) {
2043 bool result = false;
2044 Control cntrl = Control.FromHandle( hWnd);
2045 if( cntrl != null) {
2046 cntrl.WndProc(ref m);
2053 public virtual void Refresh ()
2055 //RECT rect = (RECT) null;
2056 //InvalidateRect (Handle, ref rect, true);
2057 Win32.UpdateWindow (Handle);
2061 public virtual void ResetBackColor ()
2067 public void ResetBindings ()
2073 public virtual void ResetFont ()
2079 public virtual void ResetForeColor ()
2085 public void ResetImeMode ()
2091 public virtual void ResetRightToLeft ()
2097 public virtual void ResetText ()
2103 public void ResumeLayout ()
2105 statuses [ LAYOUT_SUSPENDED ] = false;
2106 if ( statuses [ LAYOUT_PENDING ] ) {
2108 statuses [ LAYOUT_PENDING ] = false;
2113 public void ResumeLayout (bool performLayout)
2115 statuses [ LAYOUT_SUSPENDED ] = false;
2116 if ( performLayout && statuses [ LAYOUT_PENDING ] ) {
2118 statuses [ LAYOUT_PENDING ] = false;
2123 protected ContentAlignment RtlTranslateAlignment (
2124 ContentAlignment align)
2126 throw new NotImplementedException ();
2130 protected HorizontalAlignment RtlTranslateAlignment (
2131 HorizontalAlignment align)
2133 throw new NotImplementedException ();
2137 protected LeftRightAlignment RtlTranslateAlignment (
2138 LeftRightAlignment align)
2140 throw new NotImplementedException ();
2144 protected ContentAlignment RtlTranslateContent (
2145 ContentAlignment align)
2147 throw new NotImplementedException ();
2151 protected HorizontalAlignment RtlTranslateHorizontal (
2152 HorizontalAlignment align)
2154 throw new NotImplementedException ();
2158 protected LeftRightAlignment RtlTranslateLeftRight (
2159 LeftRightAlignment align)
2161 throw new NotImplementedException ();
2165 public void Scale (float ratio)
2167 Scale ( ratio, ratio );
2171 public void Scale (float dx,float dy)
2174 ScaleCore ( dx, dy );
2175 IEnumerator cw = childControls.GetEnumerator();
2176 while ( cw.MoveNext() ) {
2177 Control control = (Control) cw.Current;
2178 control.Scale ( dx, dy );
2184 protected virtual void ScaleCore (float dx, float dy)
2186 Location = new Point ( (int) (Left * dx), (int) (Top * dy) );
2187 ClientSize = new Size ((int) ( ClientSize.Width * dx ),
2188 (int) ( ClientSize.Height * dy) );
2192 public void Select ()
2198 protected virtual void Select (bool directed,bool forward)
2204 public bool SelectNextControl (Control ctl, bool forward,
2206 bool nested, bool wrap)
2208 throw new NotImplementedException ();
2213 public void SendToBack ()
2219 public void SetBounds (int x, int y, int width, int height)
2221 SetBounds(x, y, width, height, BoundsSpecified.All);
2225 public void SetBounds (int x, int y, int width, int height, BoundsSpecified specified)
2227 SetBoundsCore( x, y, width, height, specified);
2231 protected virtual void SetBoundsCore ( int x, int y, int width, int height, BoundsSpecified specified)
2233 if( (specified & BoundsSpecified.X) == 0) x = Left;
2234 if( (specified & BoundsSpecified.Y) == 0) y = Top;
2235 if( (specified & BoundsSpecified.Width) == 0) width = Width;
2236 if( (specified & BoundsSpecified.Height) == 0) height = Height;
2238 if( IsHandleCreated ) {
2239 SetWindowPosFlags flags = SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_DRAWFRAME;
2240 Win32.SetWindowPos( Handle, SetWindowPosZOrder.HWND_NOTOPMOST, x, y, width, height, flags);
2243 UpdateBounds( x, y, width, height );
2246 protected virtual bool MenuPresent {
2247 get { return false; }
2251 protected virtual void SetClientSizeCore (int x, int y)
2253 RECT rc = new RECT();
2257 CreateParams pars = CreateParams;
2258 Win32.AdjustWindowRectEx ( ref rc, pars.Style, MenuPresent ? 1 : 0, pars.ExStyle );
2260 Size = new Size( rc.right - rc.left, rc.bottom - rc.top );
2264 protected void SetStyle (ControlStyles flag, bool value)
2267 controlStyles_ |= flag;
2270 controlStyles_ &= ~flag;
2274 protected void SetTopLevel (bool value)
2277 // FIXME: verify on whether this is supposed
2278 // to activate/deactive the window
2279 Win32.SetWindowPos (Handle,
2280 SetWindowPosZOrder.HWND_NOTOPMOST,
2283 // FIXME: this does not make sense but
2284 // the docs say the window is hidden
2285 Win32.ShowWindow (Handle, ShowWindowStyles.SW_HIDE);
2289 protected virtual void SetVisibleCore (bool value)
2297 if (IsHandleCreated)
2298 Win32.ShowWindow (Handle, ShowWindowStyles.SW_SHOW);
2301 public void SuspendLayout ()
2303 statuses [ LAYOUT_SUSPENDED ] = true;
2307 public void Update ()
2309 Win32.UpdateWindow (Handle);
2313 protected void UpdateBounds ()
2314 { // update control bounds with current size and position
2316 // currently, this function is called in responce to
2317 // window events, so I assume that all window handles
2319 RECT rect = new RECT ( );
2320 Win32.GetWindowRect ( Handle, ref rect );
2322 IntPtr parent = Win32.GetParent ( Handle );
2323 if ( parent != IntPtr.Zero ) {
2324 Win32.ScreenToClient( parent, ref rect );
2327 UpdateBounds ( rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top );
2331 protected void UpdateBounds (int x, int y, int width, int height)
2333 int clWidth = width;
2334 int clHeight = height;
2336 CreateParams pars = CreateParams;
2337 // this check should be removed when all controls will use base
2338 // implementation of CreateParams
2339 if ( pars != null ) {
2340 RECT rc = new RECT();
2344 Win32.AdjustWindowRectEx ( ref rc, pars.Style, MenuPresent ? 1 : 0, pars.ExStyle );
2346 clWidth -= ( ( rc.right - rc.left ) - clWidth );
2347 clHeight -= ( ( rc.bottom - rc.top ) - clHeight);
2350 UpdateBounds ( x , y, width, height, clWidth, clHeight );
2354 protected void UpdateBounds (
2355 int x, int y, int width, int height, int clientWidth,
2358 oldBounds.X = bounds.X;
2359 oldBounds.Y = bounds.Y;
2360 oldBounds.Width = bounds.Width;
2361 oldBounds.Height = bounds.Height;
2363 bool bLocationChanged = ( bounds.X != x ) || ( bounds.Y != y );
2367 bool bSizeChanged = ( bounds.Width != width ) || ( bounds.Height != height );
2368 bounds.Width = width;
2369 bounds.Height = height;
2371 this.clientWidth = clientWidth;
2372 this.clientHeight = clientHeight;
2374 if ( bLocationChanged )
2375 OnLocationChanged ( EventArgs.Empty );
2377 OnSizeChanged ( EventArgs.Empty );
2381 protected void UpdateStyles ()
2387 protected void UpdateZOrder ()
2393 internal MouseEventArgs Msg2MouseEventArgs( ref Message msg) {
2394 MouseButtons mb = MouseButtons.None;
2395 KeyStatusFlags keyIndicator = (KeyStatusFlags)msg.WParam.ToInt32();
2396 if( (keyIndicator & KeyStatusFlags.MK_LBUTTON) != 0) {
2397 mb |= MouseButtons.Left;
2399 if( (keyIndicator & KeyStatusFlags.MK_RBUTTON) != 0) {
2400 mb |= MouseButtons.Right;
2402 if( (keyIndicator & KeyStatusFlags.MK_MBUTTON) != 0) {
2403 mb |= MouseButtons.Middle;
2405 if( (keyIndicator & KeyStatusFlags.MK_XBUTTON1) != 0) {
2406 mb |= MouseButtons.XButton1;
2408 if( (keyIndicator & KeyStatusFlags.MK_XBUTTON2) != 0) {
2409 mb |= MouseButtons.XButton2;
2412 return new MouseEventArgs( mb, (mb != MouseButtons.None) ? 1: 0, msg.LoWordLParam, msg.HiWordLParam, 0);
2415 // WndProc - calls appriate On... function for the give
2418 // These On... functions do not appear to be called by
2421 // background color/image handled by WinForms
2422 // OnBackColorChanged
2423 // OnBackgroundImageChanged
2424 // OnForeColorChanged
2425 // OnPaintBackground
2427 // controls are added/removed by WinForms
2432 // OnBindingContextChanged
2433 // OnCausesValidationChanged
2435 // OnContextMenuChanged
2436 // OnRightToLeftChanged
2441 // OnTextAlignChanged
2444 // OnTabIndexChanged
2446 // OnLocationChanged
2448 // FIXME: may be one of the WM_IME_ messages
2451 // InvalidateRect is called by no Invalidate message exists
2454 // these messages ARE not called by WNDPROC according to docs
2455 // OnParentBackColorChanged
2456 // OnParentBackgroundImageChanged
2457 // OnParentBindingContextChanged
2459 // OnParentEnabledChanged
2460 // OnParentFontChanged
2461 // OnParentForeColorChanged
2462 // OnParentRightToLeftChanged
2463 // OnParentVisibleChanged
2465 protected virtual void WndProc(ref Message m)
2467 EventArgs eventArgs = new EventArgs ();
2468 // FIXME: paintEventArgs is not being created properly
2469 // FIXME: Graphics does not have a public constructor, you must get one from .NET
2470 //PaintEventArgs paintEventArgs = new PaintEventArgs (
2471 // new Graphics(), new Rectangle());
2473 if( (uint)m.Msg == Control.InvokeMessage) {
2474 ControlInvokeHelper helper = null;
2475 lock( InvokeQueue_.SyncRoot) {
2476 if( InvokeQueue_.Count > 0) {
2477 helper = (ControlInvokeHelper)InvokeQueue_.Dequeue();
2480 if( helper != null) {
2481 helper.ExecuteMethod();
2485 else if( m.Msg == Msg.WM_COMMAND) {
2487 m.Result = (IntPtr)1;
2488 OnWmCommand (ref m);
2489 if( m.Result != IntPtr.Zero) {
2490 CallControlWndProc (ref m);
2497 Console.WriteLine ("WM_CREATE");
2498 OnHandleCreated (eventArgs);
2500 case Msg.WM_LBUTTONDBLCLK:
2501 OnDoubleClick (eventArgs);
2502 CallControlWndProc(ref m);
2508 // OnQueryContinueDrag
2510 OnEnabledChanged (eventArgs);
2511 CallControlWndProc(ref m);
2513 case Msg.WM_SETFOCUS:
2514 OnEnter (eventArgs);
2515 OnGotFocus (eventArgs);
2516 CallControlWndProc(ref m);
2518 case Msg.WM_FONTCHANGE:
2519 OnFontChanged (eventArgs);
2520 CallControlWndProc(ref m);
2522 case Msg.WM_DESTROY:
2523 OnHandleDestroyed (eventArgs);
2524 CallControlWndProc(ref m);
2528 //OnHelpRequested (eventArgs);
2529 CallControlWndProc(ref m);
2531 case Msg.WM_KEYDOWN:
2533 // OnKeyDown (eventArgs);
2534 CallControlWndProc(ref m);
2538 // OnKeyPress (eventArgs);
2539 CallControlWndProc(ref m);
2543 OnKeyUp ( new KeyEventArgs ( (Keys)m.WParam.ToInt32() ) );
2544 CallControlWndProc(ref m);
2546 case Msg.WM_KILLFOCUS:
2547 OnLeave (eventArgs);
2548 OnLostFocus (eventArgs);
2549 CallControlWndProc(ref m);
2551 case Msg.WM_MOUSEACTIVATE:
2552 //OnMouseEnter (eventArgs);
2553 CallControlWndProc(ref m);
2555 case Msg.WM_MOUSEHOVER: // called by TrackMouseEvent
2556 OnMouseHover (eventArgs);
2557 CallControlWndProc(ref m);
2559 case Msg.WM_MOUSELEAVE: // called by TrackMouseEvent
2560 OnMouseLeave (eventArgs);
2561 CallControlWndProc(ref m);
2563 case Msg.WM_MOUSEMOVE:
2565 OnMouseMove (Msg2MouseEventArgs(ref m));
2566 CallControlWndProc(ref m);
2568 case Msg.WM_LBUTTONDOWN:
2570 //OnMouseDown (eventArgs);
2571 CallControlWndProc(ref m);
2573 case Msg.WM_LBUTTONUP:
2575 //OnMouseUp (eventArgs);
2576 CallControlWndProc(ref m);
2578 case Msg.WM_MOUSEWHEEL:
2580 //OnMouseWheel (eventArgs);
2581 CallControlWndProc(ref m);
2586 CallControlWndProc(ref m);
2589 NMHDR nmhdr = (NMHDR)Marshal.PtrToStructure ( m.LParam,
2591 if( !Control.ReflectMessage( nmhdr.hwndFrom, ref m ))
2592 CallControlWndProc(ref m);
2594 // FIXME: get NM_CLICKED msg from pnmh
2595 // OnClick (eventArgs);
2596 //OnNotifyMessage (eventArgs);
2598 case Msg.WM_ERASEBKGND:
2599 if( GetStyle(ControlStyles.UserPaint)){
2600 if( !GetStyle(ControlStyles.AllPaintingInWmPaint)) {
2601 PaintEventArgs eraseEventArgs = new PaintEventArgs( Graphics.FromHdc(m.WParam), new Rectangle(new Point(0,0),Size));
2602 OnPaintBackground(eraseEventArgs);
2603 eraseEventArgs.Dispose();
2605 m.Result = (IntPtr)1;
2608 CallControlWndProc(ref m);
2612 if( !GetStyle(ControlStyles.UserPaint)) {
2613 CallControlWndProc(ref m);
2616 PAINTSTRUCT ps = new PAINTSTRUCT();
2617 IntPtr hdc = Win32.BeginPaint( Handle, ref ps);
2618 Rectangle rc = new Rectangle();
2619 rc.X = ps.rcPaint.left;
2620 rc.Y = ps.rcPaint.top;
2621 rc.Width = ps.rcPaint.right - ps.rcPaint.left;
2622 rc.Height = ps.rcPaint.bottom - ps.rcPaint.top;
2623 PaintEventArgs paintEventArgs = new PaintEventArgs( Graphics.FromHdc(hdc), rc);
2624 if( GetStyle(ControlStyles.AllPaintingInWmPaint)) {
2625 OnPaintBackground(paintEventArgs);
2627 OnPaint (paintEventArgs);
2628 paintEventArgs.Dispose();
2629 Win32.EndPaint(Handle, ref ps);
2633 OnResize (eventArgs);
2635 if( GetStyle(ControlStyles.ResizeRedraw)) {
2638 CallControlWndProc(ref m);
2640 case Msg.WM_WINDOWPOSCHANGED:
2642 CallControlWndProc(ref m);
2644 case Msg.WM_STYLECHANGED:
2645 OnStyleChanged (eventArgs);
2646 CallControlWndProc(ref m);
2648 case Msg.WM_SYSCOLORCHANGE:
2649 OnSystemColorsChanged (eventArgs);
2650 CallControlWndProc(ref m);
2652 case Msg.WM_SETTEXT:
2653 OnTextChanged (eventArgs);
2654 CallControlWndProc(ref m);
2656 case Msg.WM_SETFONT:
2657 //OnTextChanged (eventArgs);
2658 CallControlWndProc(ref m);
2660 case Msg.WM_SHOWWINDOW:
2661 OnVisibleChanged (eventArgs);
2662 CallControlWndProc(ref m);
2664 case Msg.WM_CTLCOLORLISTBOX:
2665 Win32.SetTextColor( m.WParam, Win32.RGB(ForeColor));
2666 //Win32.SetBkColor( m.WParam, 0x00FF00);
2667 //m.Result = Win32.GetStockObject(GSO_.LTGRAY_BRUSH);
2669 case Msg.WM_MEASUREITEM:
2670 ReflectMessage( m.WParam, ref m);
2672 case Msg.WM_DRAWITEM:
2673 Control.ReflectMessage( m.WParam, ref m);
2675 case Msg.WM_HSCROLL:
2676 case Msg.WM_VSCROLL:
2677 if(!Control.ReflectMessage( m.LParam, ref m )) {
2678 CallControlWndProc(ref m);
2682 CallControlWndProc(ref m);
2684 if( ControlRealWndProc != IntPtr.Zero) {
2685 CallControlWndProc(ref m);
2695 private void DoDockAndAnchorLayout ( LayoutEventArgs e ) {
2696 Rectangle area = DisplayRectangle;
2698 for ( int i = childControls.Count - 1; i >= 0; i-- ) {
2699 Control control = childControls[i];
2701 switch ( control.Dock ) {
2702 case DockStyle.Bottom:
2703 control.SetBounds ( area.Left, area.Bottom - control.Height,
2704 area.Width, control.Height );
2705 area.Height -= control.Height;
2708 control.SetBounds ( area.Left, area.Y, area.Width, control.Height );
2709 area.Y += control.Height;
2710 area.Height -= control.Height;
2712 case DockStyle.Right:
2713 control.SetBounds ( area.Right - control.Width, area.Top,
2714 control.Width, area.Height );
2715 area.Width -= control.Width;
2717 case DockStyle.Left:
2718 control.SetBounds ( area.Left, area.Y, control.Width, area.Height );
2719 area.X += control.Width;
2720 area.Width -= control.Width;
2725 for ( int i = childControls.Count - 1; i >= 0; i-- ) {
2726 Control control = childControls[i];
2728 if ( control.Dock == DockStyle.Fill ) {
2729 control.SetBounds ( area.X, area.Y, area.Width, area.Height );
2734 /// --- Control: events ---
2735 public event EventHandler BackColorChanged;
2736 public event EventHandler BackgroundImageChanged;
2737 public event EventHandler BindingContextChanged;
2738 public event EventHandler CausesValidationChanged;
2739 public event UICuesEventHandler ChangeUICues;
2742 public event EventHandler Click;
2744 public event EventHandler ContextMenuChanged;
2745 public event ControlEventHandler ControlAdded;
2746 public event ControlEventHandler ControlRemoved;
2747 public event EventHandler CursorChanged;
2748 public event EventHandler DockChanged;
2749 public event EventHandler DoubleClick;
2750 public event DragEventHandler DragDrop;
2751 public event DragEventHandler DragEnter;
2752 public event EventHandler DragLeave;
2753 public event DragEventHandler DragOver;
2756 public event EventHandler EnabledChanged;
2758 public event EventHandler Enter;
2759 public event EventHandler FontChanged;
2760 public event EventHandler ForeColorChanged;
2761 public event GiveFeedbackEventHandler GiveFeedback;
2764 public event EventHandler GotFocus;
2766 public event EventHandler HandleCreated;
2767 public event EventHandler HandleDestroyed;
2768 public event HelpEventHandler HelpRequested;
2769 public event EventHandler ImeModeChanged;
2770 public event InvalidateEventHandler Invalidated;
2773 public event KeyEventHandler KeyDown;
2776 public event KeyPressEventHandler KeyPress;
2779 public event KeyEventHandler KeyUp;
2781 public event LayoutEventHandler Layout;
2782 public event EventHandler Leave;
2783 public event EventHandler LocationChanged;
2786 public event EventHandler LostFocus;
2789 public event MouseEventHandler MouseDown;
2791 public event EventHandler MouseEnter;
2792 public event EventHandler MouseHover;
2793 public event EventHandler MouseLeave;
2796 public event MouseEventHandler MouseMove;
2799 public event MouseEventHandler MouseUp;
2801 public event MouseEventHandler MouseWheel;
2802 public event EventHandler Move;
2805 public event PaintEventHandler Paint;
2808 public event EventHandler ParentChanged;
2810 public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp;
2811 public event QueryContinueDragEventHandler QueryContinueDrag;
2814 public event EventHandler Resize;
2816 public event EventHandler RightToLeftChanged;
2817 public event EventHandler SizeChanged;
2818 public event EventHandler StyleChanged;
2819 public event EventHandler SystemColorsChanged;
2820 public event EventHandler TabIndexChanged;
2821 public event EventHandler TabStopChanged;
2824 public event EventHandler TextChanged;
2826 public event EventHandler Validated;
2828 // CancelEventHandler not yet defined
2829 //public event CancelEventHandler Validating {
2831 public event EventHandler VisibleChanged;
2833 /// --- IWin32Window properties
2834 public IntPtr Handle {
2836 // If the handle has not yet been created,
2837 // referencing this property will force the
2838 // handle to be created. ( MSDN )
2840 if ( !IsHandleCreated )
2843 return window.Handle;
2847 /// --- ISynchronizeInvoke properties ---
2849 public bool InvokeRequired {
2851 return CreatorThreadId_ != Win32.GetCurrentThreadId();
2855 private IAsyncResult DoInvoke( Delegate method, object[] args) {
2856 IAsyncResult result = null;
2857 ControlInvokeHelper helper = new ControlInvokeHelper(method, args);
2858 if( InvokeRequired) {
2860 lock( InvokeQueue_.SyncRoot) {
2861 InvokeQueue_.Enqueue(helper);
2863 Win32.PostMessage(Handle, Control.InvokeMessage, 0, 0);
2868 helper.CompletedSynchronously = true;
2869 helper.ExecuteMethod();
2875 /// --- ISynchronizeInvoke methods ---
2877 public IAsyncResult BeginInvoke (Delegate method)
2879 return DoInvoke( method, null);
2883 public IAsyncResult BeginInvoke (Delegate method, object[] args)
2885 return DoInvoke( method, args);
2889 public object EndInvoke (IAsyncResult asyncResult)
2891 object result = null;
2892 ControlInvokeHelper helper = asyncResult as ControlInvokeHelper;
2893 if( helper != null) {
2894 if( !asyncResult.CompletedSynchronously) {
2895 asyncResult.AsyncWaitHandle.WaitOne();
2897 result = helper.MethodResult;
2904 public object Invoke (Delegate method)
2906 return Invoke( method, null);
2910 public object Invoke (Delegate method, object[] args)
2912 IAsyncResult result = BeginInvoke(method, args);
2913 return EndInvoke(result);
2916 /// sub-class: Control.ControlAccessibleObject
2918 /// Provides information about a control that can be used by an accessibility application.
2920 public class ControlAccessibleObject : AccessibleObject {
2921 // AccessibleObject not ready to be base class
2922 /// --- ControlAccessibleObject.constructor ---
2924 public ControlAccessibleObject (Control ownerControl)
2926 throw new NotImplementedException ();
2930 /// --- ControlAccessibleObject Properties ---
2932 public override string DefaultAction {
2935 return base.DefaultAction;
2940 public override string Description {
2943 return base.Description;
2948 public IntPtr Handle {
2950 throw new NotImplementedException ();
2958 public override string Help {
2966 public override string KeyboardShortcut {
2969 return base.KeyboardShortcut;
2974 public override string Name {
2986 public Control Owner {
2988 throw new NotImplementedException ();
2993 public override AccessibleRole Role {
3000 /// --- ControlAccessibleObject Methods ---
3002 public override int GetHelpTopic(out string fileName)
3005 return base.GetHelpTopic(out fileName);
3009 public void NotifyClients (AccessibleEvents accEvent)
3015 public void NotifyClients (AccessibleEvents accEvent,
3022 public override string ToString ()
3025 return base.ToString();
3029 /// sub-class: Control.ControlCollection
3031 /// Represents a collection of Control objects
3033 public class ControlCollection : IList, ICollection, IEnumerable, ICloneable {
3035 private ArrayList collection = new ArrayList ();
3036 protected Control owner;
3038 /// --- ControlCollection.constructor ---
3039 public ControlCollection (Control owner)
3044 /// --- ControlCollection Properties ---
3047 return collection.Count;
3051 public bool IsReadOnly {
3053 return collection.IsReadOnly;
3057 public virtual Control this [int index] {
3059 return (Control) collection[index];
3063 public virtual void Add (Control value)
3065 if( !Contains(value)) {
3066 collection.Add (value);
3067 value.Parent = owner;
3071 public virtual void AddRange (Control[] controls)
3073 for(int i = 0; i < controls.Length; i++) {
3078 public virtual void Clear ()
3080 collection.Clear ();
3083 public bool Contains (Control control)
3085 return collection.Contains (control);
3088 public void CopyTo (Array dest,int index)
3090 collection.CopyTo (dest, index);
3094 public override bool Equals (object obj)
3097 return base.Equals(obj);
3101 public int GetChildIndex (Control child)
3103 throw new NotImplementedException ();
3106 public IEnumerator GetEnumerator ()
3108 return collection.GetEnumerator ();
3112 public override int GetHashCode ()
3115 return base.GetHashCode();
3118 public int IndexOf (Control control)
3120 return collection.IndexOf (control);
3123 public virtual void Remove (Control value)
3125 collection.Remove (value);
3128 public void RemoveAt (int index)
3130 Remove ( this [ index ] );
3131 // have to give a chance to handle this situation in derived class
3132 //collection.RemoveAt (index);
3136 public void SetChildIndex (Control child,int newIndex)
3141 /// --- ControlCollection.IClonable methods ---
3143 object ICloneable.Clone ()
3145 throw new NotImplementedException ();
3148 /// --- ControlCollection.IList properties ---
3149 bool IList.IsFixedSize {
3151 return collection.IsFixedSize;
3155 object IList.this [int index] {
3157 return collection[index];
3160 collection[index] = value;
3164 object ICollection.SyncRoot {
3166 return collection.SyncRoot;
3170 bool ICollection.IsSynchronized {
3172 return collection.IsSynchronized;
3176 /// --- ControlCollection.IList methods ---
3177 int IList.Add (object control)
3179 return collection.Add (control);
3182 bool IList.Contains (object control)
3184 return collection.Contains (control);
3187 int IList.IndexOf (object control)
3189 return collection.IndexOf (control);
3192 void IList.Insert (int index,object value)
3194 collection.Insert (index, value);
3197 void IList.Remove (object control)
3199 collection.Remove (control);
3201 } // --- end of Control.ControlCollection ---