Disable DONT_RESOLVE_DLL_REFERENCES for builtin dlls.
[wine/multimedia.git] / windows / focus.c
blobd09c278e3cbb4164bd61c2501370b054d65f7bd0
1 /*
2 * Focus functions
4 * Copyright 1993 David Metcalfe
5 * 1994 Alexandre Julliard
6 * 1995 Alex Korobka
8 */
10 #include "windef.h"
11 #include "wingdi.h"
12 #include "wine/winuser16.h"
13 #include "win.h"
14 #include "winpos.h"
15 #include "hook.h"
16 #include "message.h"
17 #include "queue.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(win);
23 /*****************************************************************
24 * FOCUS_SwitchFocus
25 * pMsgQ is the queue whose perQData focus is to be modified
27 void FOCUS_SwitchFocus( MESSAGEQUEUE *pMsgQ, HWND hFocusFrom, HWND hFocusTo )
29 WND *pFocusTo = WIN_FindWndPtr( hFocusTo );
31 PERQDATA_SetFocusWnd( pMsgQ->pQData, hFocusTo );
33 #if 0
34 if (hFocusFrom) SendMessageA( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
35 #else
36 /* FIXME: must be SendMessage16() because 32A doesn't do
37 * intertask at this time */
38 if (hFocusFrom) SendMessage16( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
39 #endif
41 if( !pFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
43 WIN_ReleaseWndPtr(pFocusTo);
44 return;
47 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
48 has received the keyboard focus. */
50 pFocusTo->pDriver->pSetFocus(pFocusTo);
52 WIN_ReleaseWndPtr(pFocusTo);
53 #if 0
54 SendMessageA( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
55 #else
56 SendMessage16( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
57 #endif
61 /*****************************************************************
62 * SetFocus (USER.22)
64 HWND16 WINAPI SetFocus16( HWND16 hwnd )
66 return (HWND16)SetFocus( hwnd );
70 /*****************************************************************
71 * SetFocus (USER32.@)
73 HWND WINAPI SetFocus( HWND hwnd )
75 HWND hWndFocus = 0, hwndTop = hwnd;
76 WND *wndPtr = WIN_FindWndPtr( hwnd );
77 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
78 BOOL16 bRet = 0;
80 /* Get the messageQ for the current thread */
81 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
83 WARN("\tCurrent message queue not found. Exiting!\n" );
84 goto CLEANUP;
87 if (wndPtr)
89 /* Check if we can set the focus to this window */
91 while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
93 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
94 goto CLEANUP;
95 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
96 if (!wndPtr) goto CLEANUP;
97 hwndTop = wndPtr->hwndSelf;
100 /* definitely at the top window now */
101 if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) ) goto CLEANUP;
103 /* Retrieve the message queue associated with this window */
104 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
105 if ( !pMsgQ )
107 WARN("\tMessage queue not found. Exiting!\n" );
108 goto CLEANUP;
111 /* Make sure that message queue for the window we are setting focus to
112 * shares the same perQ data as the current threads message queue.
113 * In other words you can't set focus to a window owned by a different
114 * thread unless AttachThreadInput has been called previously.
115 * (see AttachThreadInput and SetFocus docs)
117 if ( pCurMsgQ->pQData != pMsgQ->pQData )
118 goto CLEANUP;
120 /* Get the current focus window from the perQ data */
121 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
123 if( hwnd == hWndFocus )
125 bRet = 1; /* Success */
126 goto CLEANUP; /* Nothing to do */
129 /* call hooks */
130 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
131 (LPARAM)hWndFocus) )
132 goto CLEANUP;
134 /* activate hwndTop if needed. */
135 if (hwndTop != GetActiveWindow())
137 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
139 if (!IsWindow( hwnd )) goto CLEANUP; /* Abort if window destroyed */
142 /* Get the current focus window from the perQ data */
143 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
145 /* Change focus and send messages */
146 FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
148 else /* NULL hwnd passed in */
150 if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
151 goto CLEANUP;
153 /* Get the current focus from the perQ data of the current message Q */
154 hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
156 /* Change focus and send messages */
157 FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
160 bRet = 1; /* Success */
162 CLEANUP:
164 /* Unlock the queues before returning */
165 if ( pMsgQ )
166 QUEUE_Unlock( pMsgQ );
167 if ( pCurMsgQ )
168 QUEUE_Unlock( pCurMsgQ );
170 WIN_ReleaseWndPtr(wndPtr);
171 return bRet ? hWndFocus : 0;
175 /*****************************************************************
176 * GetFocus (USER.23)
178 HWND16 WINAPI GetFocus16(void)
180 return (HWND16)GetFocus();
184 /*****************************************************************
185 * GetFocus (USER32.@)
187 HWND WINAPI GetFocus(void)
189 MESSAGEQUEUE *pCurMsgQ = 0;
190 HWND hwndFocus = 0;
192 /* Get the messageQ for the current thread */
193 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
195 WARN("\tCurrent message queue not found. Exiting!\n" );
196 return 0;
199 /* Get the current focus from the perQ data of the current message Q */
200 hwndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
202 QUEUE_Unlock( pCurMsgQ );
204 return hwndFocus;