gdiplus: Add TRACE(..) for font calls.
[wine/multimedia.git] / dlls / user32 / focus.c
blob3253a870d4c3c21527fd68de2152ec615b2765a8
1 /*
2 * Focus and activation functions
4 * Copyright 1993 David Metcalfe
5 * Copyright 1995 Alex Korobka
6 * Copyright 1994, 2002 Alexandre Julliard
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
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"
36 WINE_DEFAULT_DEBUG_CHANNEL(win);
39 /*****************************************************************
40 * set_focus_window
42 * Change the focus window, sending the WM_SETFOCUS and WM_KILLFOCUS messages
44 static HWND set_focus_window( HWND hwnd )
46 HWND previous = 0;
47 BOOL ret;
49 SERVER_START_REQ( set_focus_window )
51 req->handle = hwnd;
52 if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
54 SERVER_END_REQ;
55 if (!ret) return 0;
56 if (previous == hwnd) return previous;
58 if (previous)
60 SendMessageW( previous, WM_KILLFOCUS, (WPARAM)hwnd, 0 );
61 if (hwnd != GetFocus()) return previous; /* changed by the message */
63 if (IsWindow(hwnd))
65 USER_Driver->pSetFocus(hwnd);
66 SendMessageW( hwnd, WM_SETFOCUS, (WPARAM)previous, 0 );
68 return previous;
72 /*******************************************************************
73 * set_active_window
75 static BOOL set_active_window( HWND hwnd, HWND *prev, BOOL mouse, BOOL focus )
77 HWND previous = GetActiveWindow();
78 BOOL ret;
79 DWORD old_thread, new_thread;
80 CBTACTIVATESTRUCT cbt;
82 if (previous == hwnd)
84 if (prev) *prev = hwnd;
85 return TRUE;
88 /* call CBT hook chain */
89 cbt.fMouse = mouse;
90 cbt.hWndActive = previous;
91 if (HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hwnd, (LPARAM)&cbt, TRUE )) return FALSE;
93 if (IsWindow(previous))
95 SendMessageW( previous, WM_NCACTIVATE, FALSE, (LPARAM)hwnd );
96 SendMessageW( previous, WM_ACTIVATE,
97 MAKEWPARAM( WA_INACTIVE, IsIconic(previous) ), (LPARAM)hwnd );
100 SERVER_START_REQ( set_active_window )
102 req->handle = hwnd;
103 if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
105 SERVER_END_REQ;
106 if (!ret) return FALSE;
107 if (prev) *prev = previous;
108 if (previous == hwnd) return TRUE;
110 if (hwnd)
112 /* send palette messages */
113 if (SendMessageW( hwnd, WM_QUERYNEWPALETTE, 0, 0 ))
114 SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTEISCHANGING, (WPARAM)hwnd, 0,
115 SMTO_ABORTIFHUNG, 2000, NULL );
116 if (!IsWindow(hwnd)) return FALSE;
119 old_thread = previous ? GetWindowThreadProcessId( previous, NULL ) : 0;
120 new_thread = hwnd ? GetWindowThreadProcessId( hwnd, NULL ) : 0;
122 if (old_thread != new_thread)
124 HWND *list, *phwnd;
126 if ((list = WIN_ListChildren( GetDesktopWindow() )))
128 if (old_thread)
130 for (phwnd = list; *phwnd; phwnd++)
132 if (GetWindowThreadProcessId( *phwnd, NULL ) == old_thread)
133 SendMessageW( *phwnd, WM_ACTIVATEAPP, 0, new_thread );
136 if (new_thread)
138 for (phwnd = list; *phwnd; phwnd++)
140 if (GetWindowThreadProcessId( *phwnd, NULL ) == new_thread)
141 SendMessageW( *phwnd, WM_ACTIVATEAPP, 1, old_thread );
144 HeapFree( GetProcessHeap(), 0, list );
148 if (IsWindow(hwnd))
150 SendMessageW( hwnd, WM_NCACTIVATE, (hwnd == GetForegroundWindow()), (LPARAM)previous );
151 SendMessageW( hwnd, WM_ACTIVATE,
152 MAKEWPARAM( mouse ? WA_CLICKACTIVE : WA_ACTIVE, IsIconic(hwnd) ),
153 (LPARAM)previous );
156 /* now change focus if necessary */
157 if (focus)
159 GUITHREADINFO info;
161 GetGUIThreadInfo( GetCurrentThreadId(), &info );
162 /* Do not change focus if the window is no more active */
163 if (hwnd == info.hwndActive)
165 if (!info.hwndFocus || !hwnd || GetAncestor( info.hwndFocus, GA_ROOT ) != hwnd)
166 set_focus_window( hwnd );
170 return TRUE;
174 /*******************************************************************
175 * set_foreground_window
177 static BOOL set_foreground_window( HWND hwnd, BOOL mouse )
179 BOOL ret, send_msg_old = FALSE, send_msg_new = FALSE;
180 HWND previous = 0;
182 SERVER_START_REQ( set_foreground_window )
184 req->handle = hwnd;
185 if ((ret = !wine_server_call_err( req )))
187 previous = reply->previous;
188 send_msg_old = reply->send_msg_old;
189 send_msg_new = reply->send_msg_new;
192 SERVER_END_REQ;
194 if (ret)
196 if (send_msg_old) /* old window belongs to other thread */
197 SendNotifyMessageW( previous, WM_WINE_SETACTIVEWINDOW, 0, 0 );
198 else if (send_msg_new) /* old window belongs to us but new one to other thread */
199 ret = set_active_window( 0, NULL, mouse, TRUE );
201 if (send_msg_new) /* new window belongs to other thread */
202 SendNotifyMessageW( hwnd, WM_WINE_SETACTIVEWINDOW, (WPARAM)hwnd, 0 );
203 else /* new window belongs to us */
204 ret = set_active_window( hwnd, NULL, mouse, TRUE );
206 return ret;
210 /*******************************************************************
211 * FOCUS_MouseActivate
213 * Activate a window as a result of a mouse click
215 BOOL FOCUS_MouseActivate( HWND hwnd )
217 return set_foreground_window( hwnd, TRUE );
221 /*******************************************************************
222 * SetActiveWindow (USER32.@)
224 HWND WINAPI SetActiveWindow( HWND hwnd )
226 HWND prev;
228 TRACE( "%p\n", hwnd );
230 if (hwnd)
232 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
234 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD)
235 return GetActiveWindow(); /* Windows doesn't seem to return an error here */
237 hwnd = WIN_GetFullHandle( hwnd );
240 if (!set_active_window( hwnd, &prev, FALSE, TRUE )) return 0;
241 return prev;
245 /*****************************************************************
246 * SetFocus (USER32.@)
248 HWND WINAPI SetFocus( HWND hwnd )
250 HWND hwndTop = hwnd;
251 HWND previous = GetFocus();
253 TRACE( "%p prev %p\n", hwnd, previous );
255 if (hwnd)
257 /* Check if we can set the focus to this window */
258 hwnd = WIN_GetFullHandle( hwnd );
259 if (hwnd == previous) return previous; /* nothing to do */
260 for (;;)
262 HWND parent;
263 LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
264 if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
265 parent = GetAncestor( hwndTop, GA_PARENT );
266 if (!parent || parent == GetDesktopWindow()) break;
267 if (parent == get_hwnd_message_parent()) return 0;
268 hwndTop = parent;
271 /* call hooks */
272 if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)previous, TRUE )) return 0;
274 /* activate hwndTop if needed. */
275 if (hwndTop != GetActiveWindow())
277 if (!set_active_window( hwndTop, NULL, FALSE, FALSE )) return 0;
278 if (!IsWindow( hwnd )) return 0; /* Abort if window destroyed */
281 else /* NULL hwnd passed in */
283 if (!previous) return 0; /* nothing to do */
284 if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)previous, TRUE )) return 0;
287 /* change focus and send messages */
288 return set_focus_window( hwnd );
292 /*******************************************************************
293 * SetForegroundWindow (USER32.@)
295 BOOL WINAPI SetForegroundWindow( HWND hwnd )
297 TRACE( "%p\n", hwnd );
299 hwnd = WIN_GetFullHandle( hwnd );
300 return set_foreground_window( hwnd, FALSE );
304 /*******************************************************************
305 * GetActiveWindow (USER32.@)
307 HWND WINAPI GetActiveWindow(void)
309 HWND ret = 0;
311 SERVER_START_REQ( get_thread_input )
313 req->tid = GetCurrentThreadId();
314 if (!wine_server_call_err( req )) ret = reply->active;
316 SERVER_END_REQ;
317 return ret;
321 /*****************************************************************
322 * GetFocus (USER32.@)
324 HWND WINAPI GetFocus(void)
326 HWND ret = 0;
328 SERVER_START_REQ( get_thread_input )
330 req->tid = GetCurrentThreadId();
331 if (!wine_server_call_err( req )) ret = reply->focus;
333 SERVER_END_REQ;
334 return ret;
338 /*******************************************************************
339 * GetForegroundWindow (USER32.@)
341 HWND WINAPI GetForegroundWindow(void)
343 HWND ret = 0;
345 SERVER_START_REQ( get_thread_input )
347 req->tid = 0;
348 if (!wine_server_call_err( req )) ret = reply->foreground;
350 SERVER_END_REQ;
351 return ret;
355 /***********************************************************************
356 * SetShellWindowEx (USER32.@)
357 * hwndShell = Progman[Program Manager]
358 * |-> SHELLDLL_DefView
359 * hwndListView = | |-> SysListView32
360 * | | |-> tooltips_class32
361 * | |
362 * | |-> SysHeader32
364 * |-> ProxyTarget
366 BOOL WINAPI SetShellWindowEx(HWND hwndShell, HWND hwndListView)
368 BOOL ret;
370 if (GetShellWindow())
371 return FALSE;
373 if (GetWindowLongW(hwndShell, GWL_EXSTYLE) & WS_EX_TOPMOST)
374 return FALSE;
376 if (hwndListView != hwndShell)
377 if (GetWindowLongW(hwndListView, GWL_EXSTYLE) & WS_EX_TOPMOST)
378 return FALSE;
380 if (hwndListView && hwndListView!=hwndShell)
381 SetWindowPos(hwndListView, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
383 SetWindowPos(hwndShell, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
385 SERVER_START_REQ(set_global_windows)
387 req->flags = SET_GLOBAL_SHELL_WINDOWS;
388 req->shell_window = hwndShell;
389 req->shell_listview = hwndListView;
390 ret = !wine_server_call_err(req);
392 SERVER_END_REQ;
394 return ret;
398 /*******************************************************************
399 * SetShellWindow (USER32.@)
401 BOOL WINAPI SetShellWindow(HWND hwndShell)
403 return SetShellWindowEx(hwndShell, hwndShell);
407 /*******************************************************************
408 * GetShellWindow (USER32.@)
410 HWND WINAPI GetShellWindow(void)
412 HWND hwndShell = 0;
414 SERVER_START_REQ(set_global_windows)
416 req->flags = 0;
417 if (!wine_server_call_err(req))
418 hwndShell = reply->old_shell_window;
420 SERVER_END_REQ;
422 return hwndShell;
426 /***********************************************************************
427 * SetProgmanWindow (USER32.@)
429 HWND WINAPI SetProgmanWindow ( HWND hwnd )
431 SERVER_START_REQ(set_global_windows)
433 req->flags = SET_GLOBAL_PROGMAN_WINDOW;
434 req->progman_window = hwnd;
435 if (wine_server_call_err( req )) hwnd = 0;
437 SERVER_END_REQ;
438 return hwnd;
442 /***********************************************************************
443 * GetProgmanWindow (USER32.@)
445 HWND WINAPI GetProgmanWindow(void)
447 HWND ret = 0;
449 SERVER_START_REQ(set_global_windows)
451 req->flags = 0;
452 if (!wine_server_call_err(req)) ret = reply->old_progman_window;
454 SERVER_END_REQ;
455 return ret;
459 /***********************************************************************
460 * SetTaskmanWindow (USER32.@)
461 * NOTES
462 * hwnd = MSTaskSwWClass
463 * |-> SysTabControl32
465 HWND WINAPI SetTaskmanWindow ( HWND hwnd )
467 SERVER_START_REQ(set_global_windows)
469 req->flags = SET_GLOBAL_TASKMAN_WINDOW;
470 req->taskman_window = hwnd;
471 if (wine_server_call_err( req )) hwnd = 0;
473 SERVER_END_REQ;
474 return hwnd;
477 /***********************************************************************
478 * GetTaskmanWindow (USER32.@)
480 HWND WINAPI GetTaskmanWindow(void)
482 HWND ret = 0;
484 SERVER_START_REQ(set_global_windows)
486 req->flags = 0;
487 if (!wine_server_call_err(req)) ret = reply->old_taskman_window;
489 SERVER_END_REQ;
490 return ret;