Fix a mismatch between format string and parameter.
[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 "task.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     WND *pFocusTo = WIN_FindWndPtr( hFocusTo );
31
32     PERQDATA_SetFocusWnd( pMsgQ->pQData, hFocusTo );
33     
34 #if 0
35     if (hFocusFrom) SendMessageA( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
36 #else
37     /* FIXME: must be SendMessage16() because 32A doesn't do
38      * intertask at this time */
39     if (hFocusFrom) SendMessage16( hFocusFrom, WM_KILLFOCUS, hFocusTo, 0 );
40 #endif
41
42     if( !pFocusTo || hFocusTo != PERQDATA_GetFocusWnd( pMsgQ->pQData ) )
43     {
44         WIN_ReleaseWndPtr(pFocusTo);
45         return;
46     }
47
48     /* According to API docs, the WM_SETFOCUS message is sent AFTER the window
49        has received the keyboard focus. */
50
51     pFocusTo->pDriver->pSetFocus(pFocusTo);
52
53     WIN_ReleaseWndPtr(pFocusTo);
54 #if 0
55     SendMessageA( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
56 #else
57     SendMessage16( hFocusTo, WM_SETFOCUS, hFocusFrom, 0 );
58 #endif
59 }
60
61
62 /*****************************************************************
63  *               SetFocus16   (USER.22)
64  */
65 HWND16 WINAPI SetFocus16( HWND16 hwnd )
66 {
67     return (HWND16)SetFocus( hwnd );
68 }
69
70
71 /*****************************************************************
72  *               SetFocus   (USER32.481)
73  */
74 HWND WINAPI SetFocus( HWND hwnd )
75 {
76     HWND hWndFocus = 0, hwndTop = hwnd;
77     WND *wndPtr = WIN_FindWndPtr( hwnd );
78     MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
79     BOOL16 bRet = 0;
80
81     /* Get the messageQ for the current thread */
82     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
83     {
84         WARN("\tCurrent message queue not found. Exiting!\n" );
85         goto CLEANUP;
86     }
87
88     if (wndPtr)
89     {
90           /* Check if we can set the focus to this window */
91
92         while ( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD  )
93         {
94             if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) )
95                  goto CLEANUP;
96             WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
97             if (!wndPtr) goto CLEANUP;
98             hwndTop = wndPtr->hwndSelf;
99         }
100
101         /* definitely at the top window now */
102         if ( wndPtr->dwStyle & ( WS_MINIMIZE | WS_DISABLED) ) goto CLEANUP;
103
104         /* Retrieve the message queue associated with this window */
105         pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
106         if ( !pMsgQ )
107         {
108             WARN("\tMessage queue not found. Exiting!\n" );
109             goto CLEANUP;
110         }
111
112         /* Make sure that message queue for the window we are setting focus to
113          * shares the same perQ data as the current threads message queue.
114          * In other words you can't set focus to a window owned by a different
115          * thread unless AttachThreadInput has been called previously.
116          * (see AttachThreadInput and SetFocus docs)
117          */
118         if ( pCurMsgQ->pQData != pMsgQ->pQData )
119             goto CLEANUP;
120
121         /* Get the current focus window from the perQ data */
122         hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
123         
124         if( hwnd == hWndFocus )
125         {
126             bRet = 1;      /* Success */
127             goto CLEANUP;  /* Nothing to do */
128         }
129         
130         /* call hooks */
131         if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, (WPARAM16)hwnd,
132                               (LPARAM)hWndFocus) )
133             goto CLEANUP;
134
135         /* activate hwndTop if needed. */
136         if (hwndTop != GetActiveWindow())
137         {
138             if (!WINPOS_SetActiveWindow(hwndTop, 0, 0)) goto CLEANUP;
139
140             if (!IsWindow( hwnd )) goto CLEANUP;  /* Abort if window destroyed */
141         }
142
143         /* Get the current focus window from the perQ data */
144         hWndFocus = PERQDATA_GetFocusWnd( pMsgQ->pQData );
145         
146         /* Change focus and send messages */
147         FOCUS_SwitchFocus( pMsgQ, hWndFocus, hwnd );
148     }
149     else /* NULL hwnd passed in */
150     {
151         if( HOOK_CallHooks16( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)hWndFocus ) )
152             goto CLEANUP;
153
154         /* Get the current focus from the perQ data of the current message Q */
155         hWndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
156
157       /* Change focus and send messages */
158         FOCUS_SwitchFocus( pCurMsgQ, hWndFocus, hwnd );
159     }
160
161     bRet = 1;      /* Success */
162     
163 CLEANUP:
164
165     /* Unlock the queues before returning */
166     if ( pMsgQ )
167         QUEUE_Unlock( pMsgQ );
168     if ( pCurMsgQ )
169         QUEUE_Unlock( pCurMsgQ );
170
171     WIN_ReleaseWndPtr(wndPtr);
172     return bRet ? hWndFocus : 0;
173 }
174
175
176 /*****************************************************************
177  *               GetFocus16   (USER.23)
178  */
179 HWND16 WINAPI GetFocus16(void)
180 {
181     return (HWND16)GetFocus();
182 }
183
184
185 /*****************************************************************
186  *               GetFocus   (USER32.240)
187  */
188 HWND WINAPI GetFocus(void)
189 {
190     MESSAGEQUEUE *pCurMsgQ = 0;
191     HWND hwndFocus = 0;
192
193     /* Get the messageQ for the current thread */
194     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
195     {
196         WARN("\tCurrent message queue not found. Exiting!\n" );
197         return 0;
198     }
199
200     /* Get the current focus from the perQ data of the current message Q */
201     hwndFocus = PERQDATA_GetFocusWnd( pCurMsgQ->pQData );
202
203     QUEUE_Unlock( pCurMsgQ );
204
205     return hwndFocus;
206 }