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
) )
39 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
40 has received the keyboard focus. */
42 pFocusTo
->pDriver
->pSetFocus(pFocusTo
);
45 SendMessageA( hFocusTo
, WM_SETFOCUS
, hFocusFrom
, 0 );
47 SendMessage16( hFocusTo
, WM_SETFOCUS
, hFocusFrom
, 0 );
52 /*****************************************************************
53 * SetFocus16 (USER.22)
55 HWND16 WINAPI
SetFocus16( HWND16 hwnd
)
57 return (HWND16
)SetFocus( hwnd
);
61 /*****************************************************************
62 * SetFocus32 (USER32.481)
64 HWND WINAPI
SetFocus( HWND hwnd
)
66 HWND hWndFocus
= 0, hwndTop
= hwnd
;
67 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
68 MESSAGEQUEUE
*pMsgQ
= 0, *pCurMsgQ
= 0;
71 /* Get the messageQ for the current thread */
72 if (!(pCurMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() )))
74 WARN( win
, "\tCurrent message queue not found. Exiting!\n" );
80 /* Check if we can set the focus to this window */
82 while ( (wndPtr
->dwStyle
& (WS_CHILD
| WS_POPUP
)) == WS_CHILD
)
84 if ( wndPtr
->dwStyle
& ( WS_MINIMIZE
| WS_DISABLED
) )
86 if (!(wndPtr
= wndPtr
->parent
)) goto CLEANUP
;
87 hwndTop
= wndPtr
->hwndSelf
;
90 /* Retrieve the message queue associated with this window */
91 pMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( wndPtr
->hmemTaskQ
);
94 WARN( win
, "\tMessage queue not found. Exiting!\n" );
98 /* Make sure that message queue for the window we are setting focus to
99 * shares the same perQ data as the current threads message queue.
100 * In other words you can't set focus to a window owned by a different
101 * thread unless AttachThreadInput has been called previously.
102 * (see AttachThreadInput and SetFocus docs)
104 if ( pCurMsgQ
->pQData
!= pMsgQ
->pQData
)
107 /* Get the current focus window from the perQ data */
108 hWndFocus
= PERQDATA_GetFocusWnd( pMsgQ
->pQData
);
110 if( hwnd
== hWndFocus
)
113 goto CLEANUP
; // Nothing to do
117 if( HOOK_CallHooks16( WH_CBT
, HCBT_SETFOCUS
, (WPARAM16
)hwnd
,
121 /* activate hwndTop if needed. */
122 if (hwndTop
!= GetActiveWindow())
124 if (!WINPOS_SetActiveWindow(hwndTop
, 0, 0)) goto CLEANUP
;
126 if (!IsWindow( hwnd
)) goto CLEANUP
; /* Abort if window destroyed */
129 /* Get the current focus window from the perQ data */
130 hWndFocus
= PERQDATA_GetFocusWnd( pMsgQ
->pQData
);
132 /* Change focus and send messages */
133 FOCUS_SwitchFocus( pMsgQ
, hWndFocus
, hwnd
);
135 else /* NULL hwnd passed in */
137 if( HOOK_CallHooks16( WH_CBT
, HCBT_SETFOCUS
, 0, (LPARAM
)hWndFocus
) )
140 /* Get the current focus from the perQ data of the current message Q */
141 hWndFocus
= PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);
143 /* Change focus and send messages */
144 FOCUS_SwitchFocus( pCurMsgQ
, hWndFocus
, hwnd
);
151 /* Unlock the queues before returning */
153 QUEUE_Unlock( pMsgQ
);
155 QUEUE_Unlock( pCurMsgQ
);
157 return bRet
? hWndFocus
: 0;
161 /*****************************************************************
162 * GetFocus16 (USER.23)
164 HWND16 WINAPI
GetFocus16(void)
166 return (HWND16
)GetFocus();
170 /*****************************************************************
171 * GetFocus32 (USER32.240)
173 HWND WINAPI
GetFocus(void)
175 MESSAGEQUEUE
*pCurMsgQ
= 0;
178 /* Get the messageQ for the current thread */
179 if (!(pCurMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() )))
181 WARN( win
, "\tCurrent message queue not found. Exiting!\n" );
185 /* Get the current focus from the perQ data of the current message Q */
186 hwndFocus
= PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);
188 QUEUE_Unlock( pCurMsgQ
);