Added an unknown VxD error code.
[wine] / windows / focus.c
1 /*
2  * Focus functions
3  *
4  * Copyright 1993 David Metcalfe
5  *           1994 Alexandre Julliard
6  *           1995 Alex Korobka
7  *
8  */
9
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"
20
21 DEFAULT_DEBUG_CHANNEL(win);
22
23
24 /*****************************************************************
25  *               FOCUS_SwitchFocus 
26  * pMsgQ is the queue whose perQData focus is to be modified
27  */
28 void FOCUS_SwitchFocus( MESSAGEQUEUE *pMsgQ, HWND hFocusFrom, HWND hFocusTo )
29 {
30     PERQDATA_SetFocusWnd( pMsgQ->pQData, hFocusTo );
31
32     if (hFocusFrom) SendMessageA( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
33
34     if( !hFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
35     {
36         return;
37     }
38
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);
42
43     SendMessageA( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
44 }
45
46
47 /*****************************************************************
48  *              SetFocus (USER.22)
49  */
50 HWND16 WINAPI SetFocus16( HWND16 hwnd )
51 {
52     return (HWND16)SetFocus( hwnd );
53 }
54
55
56 /*****************************************************************
57  *              SetFocus (USER32.@)
58  */
59 HWND WINAPI SetFocus( HWND hwnd )
60 {
61     HWND hWndFocus = 0, hwndTop = hwnd;
62     WND *wndPtr = WIN_FindWndPtr( hwnd );
63     MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
64     BOOL16 bRet = 0;
65
66     /* Get the messageQ for the current thread */
67     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
68     {
69         WARN("\tCurrent message queue not found. Exiting!\n" );
70         goto CLEANUP;
71     }
72
73     if (wndPtr)
74     {
75           /* Check if we can set the focus to this window */
76
77         while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD  )
78         {
79             if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
80                  goto CLEANUP;
81             WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
82             if (!wndPtr) goto CLEANUP;
83             hwndTop = wndPtr->hwndSelf;
84         }
85
86         /* definitely at the top window now */
87         if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) ) goto CLEANUP;
88
89         /* Retrieve the message queue associated with this window */
90         pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
91         if ( !pMsgQ )
92         {
93             WARN("\tMessage queue not found. Exiting!\n" );
94             goto CLEANUP;
95         }
96
97         /* Make sure that message queue for the window we are setting focus to
98          * shares the same perQ data as the current threads message queue.
99          * In other words you can't set focus to a window owned by a different
100          * thread unless AttachThreadInput has been called previously.
101          * (see AttachThreadInput and SetFocus docs)
102          */
103         if ( pCurMsgQ->pQData != pMsgQ->pQData )
104             goto CLEANUP;
105
106         /* Get the current focus window from the perQ data */
107         hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
108         
109         if( hwnd == hWndFocus )
110         {
111             bRet = 1;      /* Success */
112             goto CLEANUP;  /* Nothing to do */
113         }
114         
115         /* call hooks */
116         if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)hWndFocus) )
117             goto CLEANUP;
118
119         /* activate hwndTop if needed. */
120         if (hwndTop != GetActiveWindow())
121         {
122             if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
123
124             if (!IsWindow( hwnd )) goto CLEANUP;  /* Abort if window destroyed */
125         }
126
127         /* Get the current focus window from the perQ data */
128         hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
129         
130         /* Change focus and send messages */
131         FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
132     }
133     else /* NULL hwnd passed in */
134     {
135         if( HOOK_CallHooksA( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
136             goto CLEANUP;
137
138         /* Get the current focus from the perQ data of the current message Q */
139         hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
140
141       /* Change focus and send messages */
142         FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
143     }
144
145     bRet = 1;      /* Success */
146     
147 CLEANUP:
148
149     /* Unlock the queues before returning */
150     if ( pMsgQ )
151         QUEUE_Unlock( pMsgQ );
152     if ( pCurMsgQ )
153         QUEUE_Unlock( pCurMsgQ );
154
155     WIN_ReleaseWndPtr(wndPtr);
156     return bRet ? hWndFocus : 0;
157 }
158
159
160 /*****************************************************************
161  *              GetFocus (USER.23)
162  */
163 HWND16 WINAPI GetFocus16(void)
164 {
165     return (HWND16)GetFocus();
166 }
167
168
169 /*****************************************************************
170  *              GetFocus (USER32.@)
171  */
172 HWND WINAPI GetFocus(void)
173 {
174     MESSAGEQUEUE *pCurMsgQ = 0;
175     HWND hwndFocus = 0;
176
177     /* Get the messageQ for the current thread */
178     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
179     {
180         WARN("\tCurrent message queue not found. Exiting!\n" );
181         return 0;
182     }
183
184     /* Get the current focus from the perQ data of the current message Q */
185     hwndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
186
187     QUEUE_Unlock( pCurMsgQ );
188
189     return hwndFocus;
190 }