Moved SendInput and related functions to the USER driver to avoid a
[wine] / dlls / user / focus.c
1 /*
2  * Focus and activation functions
3  *
4  * Copyright 1993 David Metcalfe
5  * Copyright 1995 Alex Korobka
6  * Copyright 1994, 2002 Alexandre Julliard
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winerror.h"
32 #include "win.h"
33 #include "user_private.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(win);
38
39
40 /*****************************************************************
41  *              set_focus_window
42  *
43  * Change the focus window, sending the WM_SETFOCUS and WM_KILLFOCUS messages
44  */
45 static HWND set_focus_window( HWND hwnd )
46 {
47     HWND previous = 0;
48     BOOL ret;
49
50     SERVER_START_REQ( set_focus_window )
51     {
52         req->handle = hwnd;
53         if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
54     }
55     SERVER_END_REQ;
56     if (!ret) return 0;
57     if (previous == hwnd) return previous;
58
59     if (previous)
60     {
61         SendMessageW( previous, WM_KILLFOCUS, (WPARAM)hwnd, 0 );
62         if (hwnd != GetFocus()) return previous;  /* changed by the message */
63     }
64     if (IsWindow(hwnd))
65     {
66         if (USER_Driver.pSetFocus) USER_Driver.pSetFocus(hwnd);
67         SendMessageW( hwnd, WM_SETFOCUS, (WPARAM)previous, 0 );
68     }
69     return previous;
70 }
71
72
73 /*******************************************************************
74  *              set_active_window
75  */
76 static BOOL set_active_window( HWND hwnd, HWND *prev, BOOL mouse, BOOL focus )
77 {
78     HWND previous = GetActiveWindow();
79     BOOL ret;
80     DWORD old_thread, new_thread;
81     CBTACTIVATESTRUCT cbt;
82
83     if (previous == hwnd)
84     {
85         if (prev) *prev = hwnd;
86         return TRUE;
87     }
88
89     /* call CBT hook chain */
90     cbt.fMouse     = mouse;
91     cbt.hWndActive = previous;
92     if (HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hwnd, (LPARAM)&cbt, TRUE )) return FALSE;
93
94     if (IsWindow(previous))
95     {
96         SendMessageW( previous, WM_NCACTIVATE, FALSE, (LPARAM)hwnd );
97         SendMessageW( previous, WM_ACTIVATE,
98                       MAKEWPARAM( WA_INACTIVE, IsIconic(previous) ), (LPARAM)hwnd );
99     }
100
101     SERVER_START_REQ( set_active_window )
102     {
103         req->handle = hwnd;
104         if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
105     }
106     SERVER_END_REQ;
107     if (!ret) return FALSE;
108     if (prev) *prev = previous;
109     if (previous == hwnd) return TRUE;
110
111     if (hwnd)
112     {
113         /* send palette messages */
114         if (SendMessageW( hwnd, WM_QUERYNEWPALETTE, 0, 0 ))
115             SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTEISCHANGING, (WPARAM)hwnd, 0,
116                                  SMTO_ABORTIFHUNG, 2000, NULL );
117
118         if (!GetPropA( hwnd, "__wine_x11_managed" ))
119             SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
120
121         if (!IsWindow(hwnd)) return FALSE;
122     }
123
124     old_thread = previous ? GetWindowThreadProcessId( previous, NULL ) : 0;
125     new_thread = hwnd ? GetWindowThreadProcessId( hwnd, NULL ) : 0;
126
127     if (old_thread != new_thread)
128     {
129         HWND *list, *phwnd;
130
131         if ((list = WIN_ListChildren( GetDesktopWindow() )))
132         {
133             if (old_thread)
134             {
135                 for (phwnd = list; *phwnd; phwnd++)
136                 {
137                     if (GetWindowThreadProcessId( *phwnd, NULL ) == old_thread)
138                         SendMessageW( *phwnd, WM_ACTIVATEAPP, 0, new_thread );
139                 }
140             }
141             if (new_thread)
142             {
143                 for (phwnd = list; *phwnd; phwnd++)
144                 {
145                     if (GetWindowThreadProcessId( *phwnd, NULL ) == new_thread)
146                         SendMessageW( *phwnd, WM_ACTIVATEAPP, 1, old_thread );
147                 }
148             }
149             HeapFree( GetProcessHeap(), 0, list );
150         }
151     }
152
153     if (IsWindow(hwnd))
154     {
155         SendMessageW( hwnd, WM_NCACTIVATE, (hwnd == GetForegroundWindow()), (LPARAM)previous );
156         SendMessageW( hwnd, WM_ACTIVATE,
157                       MAKEWPARAM( mouse ? WA_CLICKACTIVE : WA_ACTIVE, IsIconic(hwnd) ),
158                       (LPARAM)previous );
159     }
160
161     /* now change focus if necessary */
162     if (focus)
163     {
164         HWND curfocus = GetFocus();
165         if (!curfocus || !hwnd || GetAncestor( curfocus, GA_ROOT ) != hwnd)
166             set_focus_window( hwnd );
167     }
168
169     return TRUE;
170 }
171
172
173 /*******************************************************************
174  *              set_foreground_window
175  */
176 static BOOL set_foreground_window( HWND hwnd, BOOL mouse )
177 {
178     BOOL ret, send_msg_old = FALSE, send_msg_new = FALSE;
179     HWND previous = 0;
180
181     SERVER_START_REQ( set_foreground_window )
182     {
183         req->handle = hwnd;
184         if ((ret = !wine_server_call_err( req )))
185         {
186             previous = reply->previous;
187             send_msg_old = reply->send_msg_old;
188             send_msg_new = reply->send_msg_new;
189         }
190     }
191     SERVER_END_REQ;
192
193     if (ret)
194     {
195         if (send_msg_old)  /* old window belongs to other thread */
196             SendNotifyMessageW( previous, WM_WINE_SETACTIVEWINDOW, 0, 0 );
197         else if (send_msg_new)  /* old window belongs to us but new one to other thread */
198             ret = set_active_window( 0, NULL, mouse, TRUE );
199
200         if (send_msg_new)  /* new window belongs to other thread */
201             SendNotifyMessageW( hwnd, WM_WINE_SETACTIVEWINDOW, (WPARAM)hwnd, 0 );
202         else  /* new window belongs to us */
203             ret = set_active_window( hwnd, NULL, mouse, TRUE );
204     }
205     return ret;
206 }
207
208
209 /*******************************************************************
210  *              FOCUS_MouseActivate
211  *
212  * Activate a window as a result of a mouse click
213  */
214 BOOL FOCUS_MouseActivate( HWND hwnd )
215 {
216     return set_foreground_window( hwnd, TRUE );
217 }
218
219
220 /*******************************************************************
221  *              SetActiveWindow (USER32.@)
222  */
223 HWND WINAPI SetActiveWindow( HWND hwnd )
224 {
225     HWND prev;
226
227     TRACE( "%p\n", hwnd );
228
229     if (hwnd)
230     {
231         LONG style = GetWindowLongW( hwnd, GWL_STYLE );
232
233         if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD)
234             return GetActiveWindow();  /* Windows doesn't seem to return an error here */
235
236         hwnd = WIN_GetFullHandle( hwnd );
237     }
238
239     if (!set_active_window( hwnd, &prev, FALSE, TRUE )) return 0;
240     return prev;
241 }
242
243
244 /*****************************************************************
245  *              SetFocus  (USER32.@)
246  */
247 HWND WINAPI SetFocus( HWND hwnd )
248 {
249     HWND hwndTop = hwnd;
250     HWND previous = GetFocus();
251
252     TRACE( "%p prev %p\n", hwnd, previous );
253
254     if (hwnd)
255     {
256         /* Check if we can set the focus to this window */
257         hwnd = WIN_GetFullHandle( hwnd );
258         if (hwnd == previous) return previous;  /* nothing to do */
259         for (;;)
260         {
261             HWND parent;
262             LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
263             if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
264             parent = GetAncestor( hwndTop, GA_PARENT );
265             if (!parent || parent == GetDesktopWindow()) break;
266             hwndTop = parent;
267         }
268
269         /* call hooks */
270         if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)previous, TRUE )) return 0;
271
272         /* activate hwndTop if needed. */
273         if (hwndTop != GetActiveWindow())
274         {
275             if (!set_active_window( hwndTop, NULL, FALSE, FALSE )) return 0;
276             if (!IsWindow( hwnd )) return 0;  /* Abort if window destroyed */
277         }
278     }
279     else /* NULL hwnd passed in */
280     {
281         if (!previous) return 0;  /* nothing to do */
282         if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)previous, TRUE )) return 0;
283     }
284
285     /* change focus and send messages */
286     return set_focus_window( hwnd );
287 }
288
289
290 /*******************************************************************
291  *              SetForegroundWindow  (USER32.@)
292  */
293 BOOL WINAPI SetForegroundWindow( HWND hwnd )
294 {
295     TRACE( "%p\n", hwnd );
296     if (hwnd) hwnd = WIN_GetFullHandle( hwnd );
297     return set_foreground_window( hwnd, FALSE );
298 }
299
300
301 /*******************************************************************
302  *              GetActiveWindow  (USER32.@)
303  */
304 HWND WINAPI GetActiveWindow(void)
305 {
306     HWND ret = 0;
307
308     SERVER_START_REQ( get_thread_input )
309     {
310         req->tid = GetCurrentThreadId();
311         if (!wine_server_call_err( req )) ret = reply->active;
312     }
313     SERVER_END_REQ;
314     return ret;
315 }
316
317
318 /*****************************************************************
319  *              GetFocus  (USER32.@)
320  */
321 HWND WINAPI GetFocus(void)
322 {
323     HWND ret = 0;
324
325     SERVER_START_REQ( get_thread_input )
326     {
327         req->tid = GetCurrentThreadId();
328         if (!wine_server_call_err( req )) ret = reply->focus;
329     }
330     SERVER_END_REQ;
331     return ret;
332 }
333
334
335 /*******************************************************************
336  *              GetForegroundWindow  (USER32.@)
337  */
338 HWND WINAPI GetForegroundWindow(void)
339 {
340     HWND ret = 0;
341
342     SERVER_START_REQ( get_thread_input )
343     {
344         req->tid = 0;
345         if (!wine_server_call_err( req )) ret = reply->foreground;
346     }
347     SERVER_END_REQ;
348     return ret;
349 }
350
351
352 /***********************************************************************
353 *               SetShellWindowEx (USER32.@)
354 * hwndShell =    Progman[Program Manager]
355 *                |-> SHELLDLL_DefView
356 * hwndListView = |   |-> SysListView32
357 *                |   |   |-> tooltips_class32
358 *                |   |
359 *                |   |-> SysHeader32
360 *                |
361 *                |-> ProxyTarget
362 */
363 BOOL WINAPI SetShellWindowEx(HWND hwndShell, HWND hwndListView)
364 {
365     BOOL ret;
366
367     if (GetShellWindow())
368         return FALSE;
369
370     if (GetWindowLongW(hwndShell, GWL_EXSTYLE) & WS_EX_TOPMOST)
371         return FALSE;
372
373     if (hwndListView != hwndShell)
374         if (GetWindowLongW(hwndListView, GWL_EXSTYLE) & WS_EX_TOPMOST)
375             return FALSE;
376
377     if (hwndListView && hwndListView!=hwndShell)
378         SetWindowPos(hwndListView, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
379
380     SetWindowPos(hwndShell, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
381
382     SERVER_START_REQ(set_global_windows)
383     {
384         req->flags          = SET_GLOBAL_SHELL_WINDOWS;
385         req->shell_window   = hwndShell;
386         req->shell_listview = hwndListView;
387         ret = !wine_server_call_err(req);
388     }
389     SERVER_END_REQ;
390
391     return ret;
392 }
393
394
395 /*******************************************************************
396 *               SetShellWindow (USER32.@)
397 */
398 BOOL WINAPI SetShellWindow(HWND hwndShell)
399 {
400     return SetShellWindowEx(hwndShell, hwndShell);
401 }
402
403
404 /*******************************************************************
405 *               GetShellWindow (USER32.@)
406 */
407 HWND WINAPI GetShellWindow(void)
408 {
409     HWND hwndShell = 0;
410
411     SERVER_START_REQ(set_global_windows)
412     {
413         req->flags = 0;
414         if (!wine_server_call_err(req))
415             hwndShell = reply->old_shell_window;
416     }
417     SERVER_END_REQ;
418
419     return hwndShell;
420 }
421
422
423 /***********************************************************************
424  *              SetProgmanWindow (USER32.@)
425  */
426 HWND WINAPI SetProgmanWindow ( HWND hwnd )
427 {
428     SERVER_START_REQ(set_global_windows)
429     {
430         req->flags          = SET_GLOBAL_PROGMAN_WINDOW;
431         req->progman_window = hwnd;
432         if (wine_server_call_err( req )) hwnd = 0;
433     }
434     SERVER_END_REQ;
435     return hwnd;
436 }
437
438
439 /***********************************************************************
440  *              GetProgmanWindow (USER32.@)
441  */
442 HWND WINAPI GetProgmanWindow(void)
443 {
444     HWND ret = 0;
445
446     SERVER_START_REQ(set_global_windows)
447     {
448         req->flags = 0;
449         if (!wine_server_call_err(req)) ret = reply->old_progman_window;
450     }
451     SERVER_END_REQ;
452     return ret;
453 }
454
455
456 /***********************************************************************
457  *              SetTaskmanWindow (USER32.@)
458  * NOTES
459  *   hwnd = MSTaskSwWClass
460  *          |-> SysTabControl32
461  */
462 HWND WINAPI SetTaskmanWindow ( HWND hwnd )
463 {
464     SERVER_START_REQ(set_global_windows)
465     {
466         req->flags          = SET_GLOBAL_TASKMAN_WINDOW;
467         req->taskman_window = hwnd;
468         if (wine_server_call_err( req )) hwnd = 0;
469     }
470     SERVER_END_REQ;
471     return hwnd;
472 }
473
474 /***********************************************************************
475  *              GetTaskmanWindow (USER32.@)
476  */
477 HWND WINAPI GetTaskmanWindow(void)
478 {
479     HWND ret = 0;
480
481     SERVER_START_REQ(set_global_windows)
482     {
483         req->flags = 0;
484         if (!wine_server_call_err(req)) ret = reply->old_taskman_window;
485     }
486     SERVER_END_REQ;
487     return ret;
488 }