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 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
91 if (!wndPtr) goto CLEANUP;
92 hwndTop = wndPtr->hwndSelf;
95 /* Retrieve the message queue associated with this window */
96 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
99 WARN( win, "\tMessage queue not found. Exiting!\n" );
103 /* Make sure that message queue for the window we are setting focus to
104 * shares the same perQ data as the current threads message queue.
105 * In other words you can't set focus to a window owned by a different
106 * thread unless AttachThreadInput has been called previously.
107 * (see AttachThreadInput and SetFocus docs)
109 if ( pCurMsgQ->pQData != pMsgQ->pQData )
112 /* Get the current focus window from the perQ data */
113 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
115 if( hwnd == hWndFocus )
118 goto CLEANUP; // Nothing to do
122 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
126 /* activate hwndTop if needed. */
127 if (hwndTop != GetActiveWindow())
129 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
131 if (!IsWindow( hwnd )) goto CLEANUP; /* Abort if window destroyed */
134 /* Get the current focus window from the perQ data */
135 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
137 /* Change focus and send messages */
138 FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
140 else /* NULL hwnd passed in */
142 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
145 /* Get the current focus from the perQ data of the current message Q */
146 hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
148 /* Change focus and send messages */
149 FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
156 /* Unlock the queues before returning */
158 QUEUE_Unlock( pMsgQ );
160 QUEUE_Unlock( pCurMsgQ );
162 WIN_ReleaseWndPtr(wndPtr);
163 return bRet ? hWndFocus : 0;
167 /*****************************************************************
168 * GetFocus16 (USER.23)
170 HWND16 WINAPI GetFocus16(void)
172 return (HWND16)GetFocus();
176 /*****************************************************************
177 * GetFocus32 (USER32.240)
179 HWND WINAPI GetFocus(void)
181 MESSAGEQUEUE *pCurMsgQ = 0;
184 /* Get the messageQ for the current thread */
185 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
187 WARN( win, "\tCurrent message queue not found. Exiting!\n" );
191 /* Get the current focus from the perQ data of the current message Q */
192 hwndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
194 QUEUE_Unlock( pCurMsgQ );