4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
12 #include "wine/winuser16.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(win);
24 /*****************************************************************
26 * pMsgQ is the queue whose perQData focus is to be modified
28 void FOCUS_SwitchFocus( MESSAGEQUEUE *pMsgQ, HWND hFocusFrom, HWND hFocusTo )
30 PERQDATA_SetFocusWnd( pMsgQ->pQData, hFocusTo );
32 if (hFocusFrom) SendMessageA( hFocusFrom, WM_KILLFOCUS, (WPARAM)hFocusTo, 0 );
34 if( !hFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
39 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
40 has received the keyboard focus. */
41 if (USER_Driver.pSetFocus) USER_Driver.pSetFocus(hFocusTo);
43 SendMessageA( hFocusTo, WM_SETFOCUS, (WPARAM)hFocusFrom, 0 );
47 /*****************************************************************
50 HWND WINAPI SetFocus( HWND hwnd )
52 HWND hWndFocus = 0, hwndTop = hwnd;
53 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
56 /* Get the messageQ for the current thread */
57 if (!(pCurMsgQ = QUEUE_Current()))
59 WARN("\tCurrent message queue not found. Exiting!\n" );
65 /* Check if we can set the focus to this window */
68 hwnd = WIN_GetFullHandle( hwnd );
72 LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
73 if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
74 parent = GetAncestor( hwndTop, GA_PARENT );
75 if (!parent || parent == GetDesktopWindow()) break;
79 if (!(wndPtr = WIN_FindWndPtr( hwndTop ))) return 0;
81 /* Retrieve the message queue associated with this window */
82 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
83 WIN_ReleaseWndPtr( wndPtr );
86 WARN("\tMessage queue not found. Exiting!\n" );
90 /* Make sure that message queue for the window we are setting focus to
91 * shares the same perQ data as the current threads message queue.
92 * In other words you can't set focus to a window owned by a different
93 * thread unless AttachThreadInput has been called previously.
94 * (see AttachThreadInput and SetFocus docs)
96 if ( pCurMsgQ->pQData != pMsgQ->pQData )
99 /* Get the current focus window from the perQ data */
100 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
102 if( hwnd == hWndFocus )
104 bRet = 1; /* Success */
105 goto CLEANUP; /* Nothing to do */
109 if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)hWndFocus) )
112 /* activate hwndTop if needed. */
113 if (hwndTop != GetActiveWindow())
115 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
117 if (!IsWindow( hwnd )) goto CLEANUP; /* Abort if window destroyed */
120 /* Get the current focus window from the perQ data */
121 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
123 /* Change focus and send messages */
124 FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
126 else /* NULL hwnd passed in */
128 if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
131 /* Get the current focus from the perQ data of the current message Q */
132 hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
134 /* Change focus and send messages */
135 FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
138 bRet = 1; /* Success */
142 /* Unlock the queues before returning */
144 QUEUE_Unlock( pMsgQ );
146 return bRet ? hWndFocus : 0;
150 /*****************************************************************
151 * GetFocus (USER32.@)
153 HWND WINAPI GetFocus(void)
155 MESSAGEQUEUE *pCurMsgQ = 0;
157 /* Get the messageQ for the current thread */
158 if (!(pCurMsgQ = QUEUE_Current()))
160 WARN("\tCurrent message queue not found. Exiting!\n" );
164 /* Get the current focus from the perQ data of the current message Q */
165 return PERQDATA_GetFocusWnd( pCurMsgQ->pQData );