Rename "SystemTime" to "t" (this is *not* SYSTEMTIME - avoid
[wine/wine-kai.git] / windows / focus.c
blob175d39da07ef0f9b904a9f2ff6d41ced86581101
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, 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, hFocusFrom, 0 );
47 /*****************************************************************
48 * SetFocus (USER.22)
50 HWND16 WINAPI SetFocus16( HWND16 hwnd )
52 return (HWND16)SetFocus( hwnd );
56 /*****************************************************************
57 * SetFocus (USER32.@)
59 HWND WINAPI SetFocus( HWND hwnd )
61 HWND hWndFocus = 0, hwndTop = hwnd;
62 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
63 BOOL bRet = 0;
65 /* Get the messageQ for the current thread */
66 if (!(pCurMsgQ = QUEUE_Current()))
68 WARN("\tCurrent message queue not found. Exiting!\n" );
69 return 0;
72 if (hwnd)
74 /* Check if we can set the focus to this window */
75 WND *wndPtr;
77 hwnd = WIN_GetFullHandle( hwnd );
78 for (;;)
80 HWND parent;
81 LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
82 if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
83 parent = GetAncestor( hwndTop, GA_PARENT );
84 if (!parent || parent == GetDesktopWindow()) break;
85 hwndTop = parent;
88 if (!(wndPtr = WIN_FindWndPtr( hwndTop ))) return 0;
90 /* Retrieve the message queue associated with this window */
91 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
92 WIN_ReleaseWndPtr( wndPtr );
93 if ( !pMsgQ )
95 WARN("\tMessage queue not found. Exiting!\n" );
96 return 0;
99 /* Make sure that message queue for the window we are setting focus to
100 * shares the same perQ data as the current threads message queue.
101 * In other words you can't set focus to a window owned by a different
102 * thread unless AttachThreadInput has been called previously.
103 * (see AttachThreadInput and SetFocus docs)
105 if ( pCurMsgQ->pQData != pMsgQ->pQData )
106 goto CLEANUP;
108 /* Get the current focus window from the perQ data */
109 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
111 if( hwnd == hWndFocus )
113 bRet = 1; /* Success */
114 goto CLEANUP; /* Nothing to do */
117 /* call hooks */
118 if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)hWndFocus) )
119 goto CLEANUP;
121 /* activate hwndTop if needed. */
122 if (hwndTop != GetActiveWindow())
124 if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
126 if (!IsWindow( hwnd )) goto CLEANUP; /* Abort if window destroyed */
129 /* Get the current focus window from the perQ data */
130 hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
132 /* Change focus and send messages */
133 FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
135 else /* NULL hwnd passed in */
137 if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
138 return 0;
140 /* Get the current focus from the perQ data of the current message Q */
141 hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
143 /* Change focus and send messages */
144 FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
147 bRet = 1; /* Success */
149 CLEANUP:
151 /* Unlock the queues before returning */
152 if ( pMsgQ )
153 QUEUE_Unlock( pMsgQ );
155 return bRet ? hWndFocus : 0;
159 /*****************************************************************
160 * GetFocus (USER.23)
162 HWND16 WINAPI GetFocus16(void)
164 return (HWND16)GetFocus();
168 /*****************************************************************
169 * GetFocus (USER32.@)
171 HWND WINAPI GetFocus(void)
173 MESSAGEQUEUE *pCurMsgQ = 0;
175 /* Get the messageQ for the current thread */
176 if (!(pCurMsgQ = QUEUE_Current()))
178 WARN("\tCurrent message queue not found. Exiting!\n" );
179 return 0;
182 /* Get the current focus from the perQ data of the current message Q */
183 return PERQDATA_GetFocusWnd( pCurMsgQ->pQData );