user32: Load PaddedBorderWidth from registry too (Coverity).
[wine] / dlls / user32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "win.h"
32 #include "user_private.h"
33 #include "wine/server.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(win);
37
38
39 /*****************************************************************
40  *              set_focus_window
41  *
42  * Change the focus window, sending the WM_SETFOCUS and WM_KILLFOCUS messages
43  */
44 static HWND set_focus_window( HWND hwnd )
45 {
46     HWND previous = 0;
47     BOOL ret;
48
49     SERVER_START_REQ( set_focus_window )
50     {
51         req->handle = wine_server_user_handle( hwnd );
52         if ((ret = !wine_server_call_err( req )))
53             previous = wine_server_ptr_handle( 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         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 = wine_server_user_handle( hwnd );
104         if ((ret = !wine_server_call_err( req )))
105             previous = wine_server_ptr_handle( reply->previous );
106     }
107     SERVER_END_REQ;
108     if (!ret) return FALSE;
109     if (prev) *prev = previous;
110     if (previous == hwnd) return TRUE;
111
112     if (hwnd)
113     {
114         /* send palette messages */
115         if (SendMessageW( hwnd, WM_QUERYNEWPALETTE, 0, 0 ))
116             SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTEISCHANGING, (WPARAM)hwnd, 0,
117                                  SMTO_ABORTIFHUNG, 2000, NULL );
118         if (!IsWindow(hwnd)) return FALSE;
119     }
120
121     old_thread = previous ? GetWindowThreadProcessId( previous, NULL ) : 0;
122     new_thread = hwnd ? GetWindowThreadProcessId( hwnd, NULL ) : 0;
123
124     if (old_thread != new_thread)
125     {
126         HWND *list, *phwnd;
127
128         if ((list = WIN_ListChildren( GetDesktopWindow() )))
129         {
130             if (old_thread)
131             {
132                 for (phwnd = list; *phwnd; phwnd++)
133                 {
134                     if (GetWindowThreadProcessId( *phwnd, NULL ) == old_thread)
135                         SendMessageW( *phwnd, WM_ACTIVATEAPP, 0, new_thread );
136                 }
137             }
138             if (new_thread)
139             {
140                 for (phwnd = list; *phwnd; phwnd++)
141                 {
142                     if (GetWindowThreadProcessId( *phwnd, NULL ) == new_thread)
143                         SendMessageW( *phwnd, WM_ACTIVATEAPP, 1, old_thread );
144                 }
145             }
146             HeapFree( GetProcessHeap(), 0, list );
147         }
148     }
149
150     if (IsWindow(hwnd))
151     {
152         SendMessageW( hwnd, WM_NCACTIVATE, (hwnd == GetForegroundWindow()), (LPARAM)previous );
153         SendMessageW( hwnd, WM_ACTIVATE,
154                       MAKEWPARAM( mouse ? WA_CLICKACTIVE : WA_ACTIVE, IsIconic(hwnd) ),
155                       (LPARAM)previous );
156     }
157
158     /* now change focus if necessary */
159     if (focus)
160     {
161         GUITHREADINFO info;
162
163         info.cbSize = sizeof(info);
164         GetGUIThreadInfo( GetCurrentThreadId(), &info );
165         /* Do not change focus if the window is no more active */
166         if (hwnd == info.hwndActive)
167         {
168             if (!info.hwndFocus || !hwnd || GetAncestor( info.hwndFocus, GA_ROOT ) != hwnd)
169                 set_focus_window( hwnd );
170         }
171     }
172
173     return TRUE;
174 }
175
176
177 /*******************************************************************
178  *              set_foreground_window
179  */
180 static BOOL set_foreground_window( HWND hwnd, BOOL mouse )
181 {
182     BOOL ret, send_msg_old = FALSE, send_msg_new = FALSE;
183     HWND previous = 0;
184
185     SERVER_START_REQ( set_foreground_window )
186     {
187         req->handle = wine_server_user_handle( hwnd );
188         if ((ret = !wine_server_call_err( req )))
189         {
190             previous = wine_server_ptr_handle( reply->previous );
191             send_msg_old = reply->send_msg_old;
192             send_msg_new = reply->send_msg_new;
193         }
194     }
195     SERVER_END_REQ;
196
197     if (ret && previous != hwnd)
198     {
199         if (send_msg_old)  /* old window belongs to other thread */
200             SendNotifyMessageW( previous, WM_WINE_SETACTIVEWINDOW, 0, 0 );
201         else if (send_msg_new)  /* old window belongs to us but new one to other thread */
202             ret = set_active_window( 0, NULL, mouse, TRUE );
203
204         if (send_msg_new)  /* new window belongs to other thread */
205             SendNotifyMessageW( hwnd, WM_WINE_SETACTIVEWINDOW, (WPARAM)hwnd, 0 );
206         else  /* new window belongs to us */
207             ret = set_active_window( hwnd, NULL, mouse, TRUE );
208     }
209     return ret;
210 }
211
212
213 /*******************************************************************
214  *              FOCUS_MouseActivate
215  *
216  * Activate a window as a result of a mouse click
217  */
218 BOOL FOCUS_MouseActivate( HWND hwnd )
219 {
220     return set_foreground_window( hwnd, TRUE );
221 }
222
223
224 /*******************************************************************
225  *              SetActiveWindow (USER32.@)
226  */
227 HWND WINAPI SetActiveWindow( HWND hwnd )
228 {
229     HWND prev;
230
231     TRACE( "%p\n", hwnd );
232
233     if (hwnd)
234     {
235         LONG style = GetWindowLongW( hwnd, GWL_STYLE );
236
237         if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD)
238             return GetActiveWindow();  /* Windows doesn't seem to return an error here */
239
240         hwnd = WIN_GetFullHandle( hwnd );
241     }
242
243     if (!set_active_window( hwnd, &prev, FALSE, TRUE )) return 0;
244     return prev;
245 }
246
247
248 /*****************************************************************
249  *              SetFocus  (USER32.@)
250  */
251 HWND WINAPI SetFocus( HWND hwnd )
252 {
253     HWND hwndTop = hwnd;
254     HWND previous = GetFocus();
255
256     TRACE( "%p prev %p\n", hwnd, previous );
257
258     if (hwnd)
259     {
260         /* Check if we can set the focus to this window */
261         hwnd = WIN_GetFullHandle( hwnd );
262         if (hwnd == previous) return previous;  /* nothing to do */
263         for (;;)
264         {
265             HWND parent;
266             LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
267             if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
268             parent = GetAncestor( hwndTop, GA_PARENT );
269             if (!parent || parent == GetDesktopWindow()) break;
270             if (parent == get_hwnd_message_parent()) return 0;
271             hwndTop = parent;
272         }
273
274         /* call hooks */
275         if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)previous, TRUE )) return 0;
276
277         /* activate hwndTop if needed. */
278         if (hwndTop != GetActiveWindow())
279         {
280             if (!set_active_window( hwndTop, NULL, FALSE, FALSE )) return 0;
281             if (!IsWindow( hwnd )) return 0;  /* Abort if window destroyed */
282
283             /* Do not change focus if the window is no longer active */
284             if (hwndTop != GetActiveWindow()) return 0;
285         }
286     }
287     else /* NULL hwnd passed in */
288     {
289         if (!previous) return 0;  /* nothing to do */
290         if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)previous, TRUE )) return 0;
291     }
292
293     /* change focus and send messages */
294     return set_focus_window( hwnd );
295 }
296
297
298 /*******************************************************************
299  *              SetForegroundWindow  (USER32.@)
300  */
301 BOOL WINAPI SetForegroundWindow( HWND hwnd )
302 {
303     TRACE( "%p\n", hwnd );
304
305     hwnd = WIN_GetFullHandle( hwnd );
306     return set_foreground_window( hwnd, FALSE );
307 }
308
309
310 /*******************************************************************
311  *              GetActiveWindow  (USER32.@)
312  */
313 HWND WINAPI GetActiveWindow(void)
314 {
315     HWND ret = 0;
316
317     SERVER_START_REQ( get_thread_input )
318     {
319         req->tid = GetCurrentThreadId();
320         if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->active );
321     }
322     SERVER_END_REQ;
323     return ret;
324 }
325
326
327 /*****************************************************************
328  *              GetFocus  (USER32.@)
329  */
330 HWND WINAPI GetFocus(void)
331 {
332     HWND ret = 0;
333
334     SERVER_START_REQ( get_thread_input )
335     {
336         req->tid = GetCurrentThreadId();
337         if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->focus );
338     }
339     SERVER_END_REQ;
340     return ret;
341 }
342
343
344 /*******************************************************************
345  *              GetForegroundWindow  (USER32.@)
346  */
347 HWND WINAPI GetForegroundWindow(void)
348 {
349     HWND ret = 0;
350
351     SERVER_START_REQ( get_thread_input )
352     {
353         req->tid = 0;
354         if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->foreground );
355     }
356     SERVER_END_REQ;
357     return ret;
358 }
359
360
361 /***********************************************************************
362 *               SetShellWindowEx (USER32.@)
363 * hwndShell =    Progman[Program Manager]
364 *                |-> SHELLDLL_DefView
365 * hwndListView = |   |-> SysListView32
366 *                |   |   |-> tooltips_class32
367 *                |   |
368 *                |   |-> SysHeader32
369 *                |
370 *                |-> ProxyTarget
371 */
372 BOOL WINAPI SetShellWindowEx(HWND hwndShell, HWND hwndListView)
373 {
374     BOOL ret;
375
376     if (GetShellWindow())
377         return FALSE;
378
379     if (GetWindowLongW(hwndShell, GWL_EXSTYLE) & WS_EX_TOPMOST)
380         return FALSE;
381
382     if (hwndListView != hwndShell)
383         if (GetWindowLongW(hwndListView, GWL_EXSTYLE) & WS_EX_TOPMOST)
384             return FALSE;
385
386     if (hwndListView && hwndListView!=hwndShell)
387         SetWindowPos(hwndListView, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
388
389     SetWindowPos(hwndShell, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
390
391     SERVER_START_REQ(set_global_windows)
392     {
393         req->flags          = SET_GLOBAL_SHELL_WINDOWS;
394         req->shell_window   = wine_server_user_handle( hwndShell );
395         req->shell_listview = wine_server_user_handle( hwndListView );
396         ret = !wine_server_call_err(req);
397     }
398     SERVER_END_REQ;
399
400     return ret;
401 }
402
403
404 /*******************************************************************
405 *               SetShellWindow (USER32.@)
406 */
407 BOOL WINAPI SetShellWindow(HWND hwndShell)
408 {
409     return SetShellWindowEx(hwndShell, hwndShell);
410 }
411
412
413 /*******************************************************************
414 *               GetShellWindow (USER32.@)
415 */
416 HWND WINAPI GetShellWindow(void)
417 {
418     HWND hwndShell = 0;
419
420     SERVER_START_REQ(set_global_windows)
421     {
422         req->flags = 0;
423         if (!wine_server_call_err(req))
424             hwndShell = wine_server_ptr_handle( reply->old_shell_window );
425     }
426     SERVER_END_REQ;
427
428     return hwndShell;
429 }
430
431
432 /***********************************************************************
433  *              SetProgmanWindow (USER32.@)
434  */
435 HWND WINAPI SetProgmanWindow ( HWND hwnd )
436 {
437     SERVER_START_REQ(set_global_windows)
438     {
439         req->flags          = SET_GLOBAL_PROGMAN_WINDOW;
440         req->progman_window = wine_server_user_handle( hwnd );
441         if (wine_server_call_err( req )) hwnd = 0;
442     }
443     SERVER_END_REQ;
444     return hwnd;
445 }
446
447
448 /***********************************************************************
449  *              GetProgmanWindow (USER32.@)
450  */
451 HWND WINAPI GetProgmanWindow(void)
452 {
453     HWND ret = 0;
454
455     SERVER_START_REQ(set_global_windows)
456     {
457         req->flags = 0;
458         if (!wine_server_call_err(req))
459             ret = wine_server_ptr_handle( reply->old_progman_window );
460     }
461     SERVER_END_REQ;
462     return ret;
463 }
464
465
466 /***********************************************************************
467  *              SetTaskmanWindow (USER32.@)
468  * NOTES
469  *   hwnd = MSTaskSwWClass
470  *          |-> SysTabControl32
471  */
472 HWND WINAPI SetTaskmanWindow ( HWND hwnd )
473 {
474     SERVER_START_REQ(set_global_windows)
475     {
476         req->flags          = SET_GLOBAL_TASKMAN_WINDOW;
477         req->taskman_window = wine_server_user_handle( hwnd );
478         if (wine_server_call_err( req )) hwnd = 0;
479     }
480     SERVER_END_REQ;
481     return hwnd;
482 }
483
484 /***********************************************************************
485  *              GetTaskmanWindow (USER32.@)
486  */
487 HWND WINAPI GetTaskmanWindow(void)
488 {
489     HWND ret = 0;
490
491     SERVER_START_REQ(set_global_windows)
492     {
493         req->flags = 0;
494         if (!wine_server_call_err(req))
495             ret = wine_server_ptr_handle( reply->old_taskman_window );
496     }
497     SERVER_END_REQ;
498     return ret;
499 }