4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
18 /*****************************************************************
20 * pMsgQ is the queue whose perQData focus is to be modified
22 void FOCUS_SwitchFocus( MESSAGEQUEUE *pMsgQ, HWND hFocusFrom, HWND hFocusTo )
24 WND *pFocusTo = WIN_FindWndPtr( hFocusTo );
26 PERQDATA_SetFocusWnd( pMsgQ->pQData, hFocusTo );
29 if (hFocusFrom) SendMessageA( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
31 /* FIXME: must be SendMessage16() because 32A doesn't do
32 * intertask at this time */
33 if (hFocusFrom) SendMessage16( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
36 if( !pFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
38 WIN_ReleaseWndPtr(pFocusTo);
42 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
43 has received the keyboard focus. */
45 pFocusTo->pDriver->pSetFocus(pFocusTo);
47 WIN_ReleaseWndPtr(pFocusTo);
49 SendMessageA( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
51 SendMessage16( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
56 /*****************************************************************
57 * SetFocus16 (USER.22)
59 HWND16 WINAPI SetFocus16( HWND16 hwnd )
61 return (HWND16)SetFocus( hwnd );
65 /*****************************************************************
66 * SetFocus32 (USER32.481)
68 HWND WINAPI SetFocus( HWND hwnd )
70 HWND hWndFocus = 0, hwndTop = hwnd;
71 WND *wndPtr = WIN_FindWndPtr( hwnd );
72 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
75 /* Get the messageQ for the current thread */
76 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
78 WARN( win, "\tCurrent message queue not found. Exiting!\n" );
84 /* Check if we can set the focus to this window */
86 while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
88 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
90 if (!(wndPtr = wndPtr->parent)) goto CLEANUP;
91 hwndTop = wndPtr->hwndSelf;
94 /* Retrieve the message queue associated with this window */
95 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
98 WARN( win, "\tMessage queue not found. Exiting!\n" );
102 /* Make sure that message queue for the window we are setting focus to
103 * shares the same perQ data as the current threads message queue.
104 * In other words you can't set focus to a window owned by a different
105 * thread unless AttachThreadInput has been called previously.
106 * (see AttachThreadInput and SetFocus docs)
108 if ( pCurMsgQ->pQData != pMsgQ->pQData )
111 /* Get the current focus window from the perQ data */
112 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
114 if( hwnd == hWndFocus )
117 goto CLEANUP; // Nothing to do
121 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
125 /* activate hwndTop if needed. */
126 if (hwndTop != GetActiveWindow())
128 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
130 if (!IsWindow( hwnd )) goto CLEANUP; /* Abort if window destroyed */
133 /* Get the current focus window from the perQ data */
134 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
136 /* Change focus and send messages */
137 FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
139 else /* NULL hwnd passed in */
141 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
144 /* Get the current focus from the perQ data of the current message Q */
145 hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
147 /* Change focus and send messages */
148 FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
155 /* Unlock the queues before returning */
157 QUEUE_Unlock( pMsgQ );
159 QUEUE_Unlock( pCurMsgQ );
161 WIN_ReleaseWndPtr(wndPtr);
162 return bRet ? hWndFocus : 0;
166 /*****************************************************************
167 * GetFocus16 (USER.23)
169 HWND16 WINAPI GetFocus16(void)
171 return (HWND16)GetFocus();
175 /*****************************************************************
176 * GetFocus32 (USER32.240)
178 HWND WINAPI GetFocus(void)
180 MESSAGEQUEUE *pCurMsgQ = 0;
183 /* Get the messageQ for the current thread */
184 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
186 WARN( win, "\tCurrent message queue not found. Exiting!\n" );
190 /* Get the current focus from the perQ data of the current message Q */
191 hwndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
193 QUEUE_Unlock( pCurMsgQ );