4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
12 #include "wine/winuser16.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(win
);
23 /*****************************************************************
25 * pMsgQ is the queue whose perQData focus is to be modified
27 void FOCUS_SwitchFocus( MESSAGEQUEUE
*pMsgQ
, HWND hFocusFrom
, HWND hFocusTo
)
29 WND
*pFocusTo
= WIN_FindWndPtr( hFocusTo
);
31 PERQDATA_SetFocusWnd( pMsgQ
->pQData
, hFocusTo
);
34 if (hFocusFrom
) SendMessageA( hFocusFrom
, WM_KILLFOCUS
, hFocusTo
, 0 );
36 /* FIXME: must be SendMessage16() because 32A doesn't do
37 * intertask at this time */
38 if (hFocusFrom
) SendMessage16( hFocusFrom
, WM_KILLFOCUS
, hFocusTo
, 0 );
41 if( !pFocusTo
|| hFocusTo
!= PERQDATA_GetFocusWnd( pMsgQ
->pQData
) )
43 WIN_ReleaseWndPtr(pFocusTo
);
47 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
48 has received the keyboard focus. */
50 pFocusTo
->pDriver
->pSetFocus(pFocusTo
);
52 WIN_ReleaseWndPtr(pFocusTo
);
54 SendMessageA( hFocusTo
, WM_SETFOCUS
, hFocusFrom
, 0 );
56 SendMessage16( hFocusTo
, WM_SETFOCUS
, hFocusFrom
, 0 );
61 /*****************************************************************
64 HWND16 WINAPI
SetFocus16( HWND16 hwnd
)
66 return (HWND16
)SetFocus( hwnd
);
70 /*****************************************************************
73 HWND WINAPI
SetFocus( HWND hwnd
)
75 HWND hWndFocus
= 0, hwndTop
= hwnd
;
76 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
77 MESSAGEQUEUE
*pMsgQ
= 0, *pCurMsgQ
= 0;
80 /* Get the messageQ for the current thread */
81 if (!(pCurMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() )))
83 WARN("\tCurrent message queue not found. Exiting!\n" );
89 /* Check if we can set the focus to this window */
91 while ( (wndPtr
->dwStyle
& (WS_CHILD
| WS_POPUP
)) == WS_CHILD
)
93 if ( wndPtr
->dwStyle
& ( WS_MINIMIZE
| WS_DISABLED
) )
95 WIN_UpdateWndPtr(&wndPtr
,wndPtr
->parent
);
96 if (!wndPtr
) goto CLEANUP
;
97 hwndTop
= wndPtr
->hwndSelf
;
100 /* definitely at the top window now */
101 if ( wndPtr
->dwStyle
& ( WS_MINIMIZE
| WS_DISABLED
) ) goto CLEANUP
;
103 /* Retrieve the message queue associated with this window */
104 pMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( wndPtr
->hmemTaskQ
);
107 WARN("\tMessage queue not found. Exiting!\n" );
111 /* Make sure that message queue for the window we are setting focus to
112 * shares the same perQ data as the current threads message queue.
113 * In other words you can't set focus to a window owned by a different
114 * thread unless AttachThreadInput has been called previously.
115 * (see AttachThreadInput and SetFocus docs)
117 if ( pCurMsgQ
->pQData
!= pMsgQ
->pQData
)
120 /* Get the current focus window from the perQ data */
121 hWndFocus
= PERQDATA_GetFocusWnd( pMsgQ
->pQData
);
123 if( hwnd
== hWndFocus
)
125 bRet
= 1; /* Success */
126 goto CLEANUP
; /* Nothing to do */
130 if( HOOK_CallHooks16( WH_CBT
, HCBT_SETFOCUS
, (WPARAM16
)hwnd
,
134 /* activate hwndTop if needed. */
135 if (hwndTop
!= GetActiveWindow())
137 if (!WINPOS_SetActiveWindow(hwndTop
, 0, 0)) goto CLEANUP
;
139 if (!IsWindow( hwnd
)) goto CLEANUP
; /* Abort if window destroyed */
142 /* Get the current focus window from the perQ data */
143 hWndFocus
= PERQDATA_GetFocusWnd( pMsgQ
->pQData
);
145 /* Change focus and send messages */
146 FOCUS_SwitchFocus( pMsgQ
, hWndFocus
, hwnd
);
148 else /* NULL hwnd passed in */
150 if( HOOK_CallHooks16( WH_CBT
, HCBT_SETFOCUS
, 0, (LPARAM
)hWndFocus
) )
153 /* Get the current focus from the perQ data of the current message Q */
154 hWndFocus
= PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);
156 /* Change focus and send messages */
157 FOCUS_SwitchFocus( pCurMsgQ
, hWndFocus
, hwnd
);
160 bRet
= 1; /* Success */
164 /* Unlock the queues before returning */
166 QUEUE_Unlock( pMsgQ
);
168 QUEUE_Unlock( pCurMsgQ
);
170 WIN_ReleaseWndPtr(wndPtr
);
171 return bRet
? hWndFocus
: 0;
175 /*****************************************************************
178 HWND16 WINAPI
GetFocus16(void)
180 return (HWND16
)GetFocus();
184 /*****************************************************************
185 * GetFocus (USER32.@)
187 HWND WINAPI
GetFocus(void)
189 MESSAGEQUEUE
*pCurMsgQ
= 0;
192 /* Get the messageQ for the current thread */
193 if (!(pCurMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() )))
195 WARN("\tCurrent message queue not found. Exiting!\n" );
199 /* Get the current focus from the perQ data of the current message Q */
200 hwndFocus
= PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);
202 QUEUE_Unlock( pCurMsgQ
);