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