Move the name directive from the .spec file to the Makefile.
[wine/gsoc_dplay.git] / windows / focus.c
blob61898dae5dbab8fcef48edcfc1ba5e3a771422b8
1 /*
2 * Focus functions
4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
6 * 1995 Alex Korobka
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
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "wine/winuser16.h"
26 #include "win.h"
27 #include "winpos.h"
28 #include "hook.h"
29 #include "message.h"
30 #include "queue.h"
31 #include "user.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(win);
37 /*****************************************************************
38 * FOCUS_SwitchFocus
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)
46 SendNotifyMessageA( hFocusFrom, WM_KILLFOCUS, (WPARAM)hFocusTo, 0 );
48 if( !hFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
50 return;
53 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
54 has received the keyboard focus. */
55 if (USER_Driver.pSetFocus) USER_Driver.pSetFocus(hFocusTo);
57 SendMessageA( hFocusTo, WM_SETFOCUS, (WPARAM)hFocusFrom, 0 );
61 /*****************************************************************
62 * SetFocus (USER32.@)
64 HWND WINAPI SetFocus( HWND hwnd )
66 HWND hWndFocus = 0, hwndTop = hwnd;
67 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
68 BOOL bRet = 0;
70 /* Get the messageQ for the current thread */
71 if (!(pCurMsgQ = QUEUE_Current()))
73 WARN("\tCurrent message queue not found. Exiting!\n" );
74 return 0;
77 if (hwnd)
79 /* Check if we can set the focus to this window */
80 WND *wndPtr;
82 hwnd = WIN_GetFullHandle( hwnd );
83 for (;;)
85 HWND parent;
86 LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
87 if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
88 parent = GetAncestor( hwndTop, GA_PARENT );
89 if (!parent || parent == GetDesktopWindow()) break;
90 hwndTop = parent;
93 if (!(wndPtr = WIN_FindWndPtr( hwndTop ))) return 0;
95 /* Retrieve the message queue associated with this window */
96 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
97 WIN_ReleaseWndPtr( wndPtr );
98 if ( !pMsgQ )
100 WARN("\tMessage queue not found. Exiting!\n" );
101 return 0;
104 /* Make sure that message queue for the window we are setting focus to
105 * shares the same perQ data as the current threads message queue.
106 * In other words you can't set focus to a window owned by a different
107 * thread unless AttachThreadInput has been called previously.
108 * (see AttachThreadInput and SetFocus docs)
110 if ( pCurMsgQ->pQData != pMsgQ->pQData )
111 goto CLEANUP;
113 /* Get the current focus window from the perQ data */
114 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
116 if( hwnd == hWndFocus )
118 bRet = 1; /* Success */
119 goto CLEANUP; /* Nothing to do */
122 /* call hooks */
123 if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)hWndFocus) )
124 goto CLEANUP;
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_CallHooksA( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
143 return 0;
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 );
152 bRet = 1; /* Success */
154 CLEANUP:
156 /* Unlock the queues before returning */
157 if ( pMsgQ )
158 QUEUE_Unlock( pMsgQ );
160 return bRet ? hWndFocus : 0;
164 /*****************************************************************
165 * GetFocus (USER32.@)
167 HWND WINAPI GetFocus(void)
169 MESSAGEQUEUE *pCurMsgQ = 0;
171 /* Get the messageQ for the current thread */
172 if (!(pCurMsgQ = QUEUE_Current()))
174 WARN("\tCurrent message queue not found. Exiting!\n" );
175 return 0;
178 /* Get the current focus from the perQ data of the current message Q */
179 return PERQDATA_GetFocusWnd( pCurMsgQ->pQData );