Fixed a typo.
[wine/multimedia.git] / windows / focus.c
blob09cd85ee360f8c2d71be389af8169bcb10e69ba6
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 "user.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(win);
24 /*****************************************************************
25 * FOCUS_SwitchFocus
26 * pMsgQ is the queue whose perQData focus is to be modified
28 void FOCUS_SwitchFocus( MESSAGEQUEUE *pMsgQ, HWND hFocusFrom, HWND hFocusTo )
30 PERQDATA_SetFocusWnd( pMsgQ->pQData, hFocusTo );
32 if (hFocusFrom) SendMessageA( hFocusFrom, WM_KILLFOCUS, (WPARAM)hFocusTo, 0 );
34 if( !hFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
36 return;
39 /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
40 has received the keyboard focus. */
41 if (USER_Driver.pSetFocus) USER_Driver.pSetFocus(hFocusTo);
43 SendMessageA( hFocusTo, WM_SETFOCUS, (WPARAM)hFocusFrom, 0 );
47 /*****************************************************************
48 * SetFocus (USER32.@)
50 HWND WINAPI SetFocus( HWND hwnd )
52 HWND hWndFocus = 0, hwndTop = hwnd;
53 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
54 BOOL bRet = 0;
56 /* Get the messageQ for the current thread */
57 if (!(pCurMsgQ = QUEUE_Current()))
59 WARN("\tCurrent message queue not found. Exiting!\n" );
60 return 0;
63 if (hwnd)
65 /* Check if we can set the focus to this window */
66 WND *wndPtr;
68 hwnd = WIN_GetFullHandle( hwnd );
69 for (;;)
71 HWND parent;
72 LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
73 if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
74 parent = GetAncestor( hwndTop, GA_PARENT );
75 if (!parent || parent == GetDesktopWindow()) break;
76 hwndTop = parent;
79 if (!(wndPtr = WIN_FindWndPtr( hwndTop ))) return 0;
81 /* Retrieve the message queue associated with this window */
82 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
83 WIN_ReleaseWndPtr( wndPtr );
84 if ( !pMsgQ )
86 WARN("\tMessage queue not found. Exiting!\n" );
87 return 0;
90 /* Make sure that message queue for the window we are setting focus to
91 * shares the same perQ data as the current threads message queue.
92 * In other words you can't set focus to a window owned by a different
93 * thread unless AttachThreadInput has been called previously.
94 * (see AttachThreadInput and SetFocus docs)
96 if ( pCurMsgQ->pQData != pMsgQ->pQData )
97 goto CLEANUP;
99 /* Get the current focus window from the perQ data */
100 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
102 if( hwnd == hWndFocus )
104 bRet = 1; /* Success */
105 goto CLEANUP; /* Nothing to do */
108 /* call hooks */
109 if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)hWndFocus) )
110 goto CLEANUP;
112 /* activate hwndTop if needed. */
113 if (hwndTop != GetActiveWindow())
115 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
117 if (!IsWindow( hwnd )) goto CLEANUP; /* Abort if window destroyed */
120 /* Get the current focus window from the perQ data */
121 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
123 /* Change focus and send messages */
124 FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
126 else /* NULL hwnd passed in */
128 if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
129 return 0;
131 /* Get the current focus from the perQ data of the current message Q */
132 hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
134 /* Change focus and send messages */
135 FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
138 bRet = 1; /* Success */
140 CLEANUP:
142 /* Unlock the queues before returning */
143 if ( pMsgQ )
144 QUEUE_Unlock( pMsgQ );
146 return bRet ? hWndFocus : 0;
150 /*****************************************************************
151 * GetFocus (USER32.@)
153 HWND WINAPI GetFocus(void)
155 MESSAGEQUEUE *pCurMsgQ = 0;
157 /* Get the messageQ for the current thread */
158 if (!(pCurMsgQ = QUEUE_Current()))
160 WARN("\tCurrent message queue not found. Exiting!\n" );
161 return 0;
164 /* Get the current focus from the perQ data of the current message Q */
165 return PERQDATA_GetFocusWnd( pCurMsgQ->pQData );