4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/winuser16.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(win
);
37 /*****************************************************************
39 * pMsgQ is the queue whose perQData focus is to be modified
41 void FOCUS_SwitchFocus( MESSAGEQUEUE
*pMsgQ
, HWND hFocusFrom
, HWND hFocusTo
)
43 PERQDATA_SetFocusWnd( pMsgQ
->pQData
, hFocusTo
);
45 if (hFocusFrom
) SendMessageA( hFocusFrom
, WM_KILLFOCUS
, (WPARAM
)hFocusTo
, 0 );
47 if( !hFocusTo
|| hFocusTo
!= PERQDATA_GetFocusWnd( pMsgQ
->pQData
) )
52 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
53 has received the keyboard focus. */
54 if (USER_Driver
.pSetFocus
) USER_Driver
.pSetFocus(hFocusTo
);
56 SendMessageA( hFocusTo
, WM_SETFOCUS
, (WPARAM
)hFocusFrom
, 0 );
60 /*****************************************************************
63 HWND WINAPI
SetFocus( HWND hwnd
)
65 HWND hWndFocus
= 0, hwndTop
= hwnd
;
66 MESSAGEQUEUE
*pMsgQ
= 0, *pCurMsgQ
= 0;
69 /* Get the messageQ for the current thread */
70 if (!(pCurMsgQ
= QUEUE_Current()))
72 WARN("\tCurrent message queue not found. Exiting!\n" );
78 /* Check if we can set the focus to this window */
81 hwnd
= WIN_GetFullHandle( hwnd
);
85 LONG style
= GetWindowLongW( hwndTop
, GWL_STYLE
);
86 if (style
& (WS_MINIMIZE
| WS_DISABLED
)) return 0;
87 parent
= GetAncestor( hwndTop
, GA_PARENT
);
88 if (!parent
|| parent
== GetDesktopWindow()) break;
92 if (!(wndPtr
= WIN_FindWndPtr( hwndTop
))) return 0;
94 /* Retrieve the message queue associated with this window */
95 pMsgQ
= (MESSAGEQUEUE
*)QUEUE_Lock( wndPtr
->hmemTaskQ
);
96 WIN_ReleaseWndPtr( wndPtr
);
99 WARN("\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
)
117 bRet
= 1; /* Success */
118 goto CLEANUP
; /* Nothing to do */
122 if( HOOK_CallHooksA( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)hwnd
, (LPARAM
)hWndFocus
) )
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_CallHooksA( 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
);
151 bRet
= 1; /* Success */
155 /* Unlock the queues before returning */
157 QUEUE_Unlock( pMsgQ
);
159 return bRet
? hWndFocus
: 0;
163 /*****************************************************************
164 * GetFocus (USER32.@)
166 HWND WINAPI
GetFocus(void)
168 MESSAGEQUEUE
*pCurMsgQ
= 0;
170 /* Get the messageQ for the current thread */
171 if (!(pCurMsgQ
= QUEUE_Current()))
173 WARN("\tCurrent message queue not found. Exiting!\n" );
177 /* Get the current focus from the perQ data of the current message Q */
178 return PERQDATA_GetFocusWnd( pCurMsgQ
->pQData
);