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 WND
*pFocusTo
= WIN_FindWndPtr( hFocusTo
);
32 PERQDATA_SetFocusWnd( pMsgQ
->pQData
, hFocusTo
);
35 if (hFocusFrom
) SendMessageA( hFocusFrom
, WM_KILLFOCUS
, hFocusTo
, 0 );
37 /* FIXME: must be SendMessage16() because 32A doesn't do
38 * intertask at this time */
39 if (hFocusFrom
) SendMessage16( hFocusFrom
, WM_KILLFOCUS
, hFocusTo
, 0 );
42 if( !pFocusTo
|| hFocusTo
!= PERQDATA_GetFocusWnd( pMsgQ
->pQData
) )
44 WIN_ReleaseWndPtr(pFocusTo
);
48 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
49 has received the keyboard focus. */
51 pFocusTo
->pDriver
->pSetFocus(pFocusTo
);
53 WIN_ReleaseWndPtr(pFocusTo
);
55 SendMessageA( hFocusTo
, WM_SETFOCUS
, hFocusFrom
, 0 );
57 SendMessage16( hFocusTo
, WM_SETFOCUS
, hFocusFrom
, 0 );
62 /*****************************************************************
63 * SetFocus16 (USER.22)
65 HWND16 WINAPI
SetFocus16( HWND16 hwnd
)
67 return (HWND16
)SetFocus( hwnd
);
71 /*****************************************************************
72 * SetFocus (USER32.481)
74 HWND WINAPI
SetFocus( HWND hwnd
)
76 HWND hWndFocus
= 0, hwndTop
= hwnd
;
77 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
78 MESSAGEQUEUE
*pMsgQ
= 0, *pCurMsgQ
= 0;
81 /* Get the messageQ for the current thread */
82 if (!(pCurMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() )))
84 WARN("\tCurrent message queue not found. Exiting!\n" );
90 /* Check if we can set the focus to this window */
92 while ( (wndPtr
->dwStyle
& (WS_CHILD
| WS_POPUP
)) == WS_CHILD
)
94 if ( wndPtr
->dwStyle
& ( WS_MINIMIZE
| WS_DISABLED
) )
96 WIN_UpdateWndPtr(&wndPtr
,wndPtr
->parent
);
97 if (!wndPtr
) goto CLEANUP
;
98 hwndTop
= wndPtr
->hwndSelf
;
101 /* definitely at the top window now */
102 if ( wndPtr
->dwStyle
& ( WS_MINIMIZE
| WS_DISABLED
) ) goto CLEANUP
;
104 /* Retrieve the message queue associated with this window */
105 pMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( wndPtr
->hmemTaskQ
);
108 WARN("\tMessage queue not found. Exiting!\n" );
112 /* Make sure that message queue for the window we are setting focus to
113 * shares the same perQ data as the current threads message queue.
114 * In other words you can't set focus to a window owned by a different
115 * thread unless AttachThreadInput has been called previously.
116 * (see AttachThreadInput and SetFocus docs)
118 if ( pCurMsgQ
->pQData
!= pMsgQ
->pQData
)
121 /* Get the current focus window from the perQ data */
122 hWndFocus
= PERQDATA_GetFocusWnd( pMsgQ
->pQData
);
124 if( hwnd
== hWndFocus
)
126 bRet
= 1; /* Success */
127 goto CLEANUP
; /* Nothing to do */
131 if( HOOK_CallHooks16( WH_CBT
, HCBT_SETFOCUS
, (WPARAM16
)hwnd
,
135 /* activate hwndTop if needed. */
136 if (hwndTop
!= GetActiveWindow())
138 if (!WINPOS_SetActiveWindow(hwndTop
, 0, 0)) goto CLEANUP
;
140 if (!IsWindow( hwnd
)) goto CLEANUP
; /* Abort if window destroyed */
143 /* Get the current focus window from the perQ data */
144 hWndFocus
= PERQDATA_GetFocusWnd( pMsgQ
->pQData
);
146 /* Change focus and send messages */
147 FOCUS_SwitchFocus( pMsgQ
, hWndFocus
, hwnd
);
149 else /* NULL hwnd passed in */
151 if( HOOK_CallHooks16( WH_CBT
, HCBT_SETFOCUS
, 0, (LPARAM
)hWndFocus
) )
154 /* Get the current focus from the perQ data of the current message Q */
155 hWndFocus
= PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);
157 /* Change focus and send messages */
158 FOCUS_SwitchFocus( pCurMsgQ
, hWndFocus
, hwnd
);
161 bRet
= 1; /* Success */
165 /* Unlock the queues before returning */
167 QUEUE_Unlock( pMsgQ
);
169 QUEUE_Unlock( pCurMsgQ
);
171 WIN_ReleaseWndPtr(wndPtr
);
172 return bRet
? hWndFocus
: 0;
176 /*****************************************************************
177 * GetFocus16 (USER.23)
179 HWND16 WINAPI
GetFocus16(void)
181 return (HWND16
)GetFocus();
185 /*****************************************************************
186 * GetFocus (USER32.240)
188 HWND WINAPI
GetFocus(void)
190 MESSAGEQUEUE
*pCurMsgQ
= 0;
193 /* Get the messageQ for the current thread */
194 if (!(pCurMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() )))
196 WARN("\tCurrent message queue not found. Exiting!\n" );
200 /* Get the current focus from the perQ data of the current message Q */
201 hwndFocus
= PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);
203 QUEUE_Unlock( pCurMsgQ
);