d3d10: Return the read value from read_dword().
[wine.git] / dlls / user32 / focus.c
bloba20151ad5df63cc785df4fe5bb4f1d0baf7c4965
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 <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "win.h"
30 #include "imm.h"
31 #include "user_private.h"
32 #include "wine/server.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(win);
38 /*****************************************************************
39 * set_focus_window
41 * Change the focus window, sending the WM_SETFOCUS and WM_KILLFOCUS messages
43 static HWND set_focus_window( HWND hwnd )
45 HWND previous = 0, ime_default;
46 BOOL ret;
48 SERVER_START_REQ( set_focus_window )
50 req->handle = wine_server_user_handle( hwnd );
51 if ((ret = !wine_server_call_err( req )))
52 previous = wine_server_ptr_handle( 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 );
62 ime_default = ImmGetDefaultIMEWnd( previous );
63 if (ime_default)
64 SendMessageW( ime_default, WM_IME_INTERNAL, IME_INTERNAL_DEACTIVATE, (LPARAM)previous );
66 if (hwnd != GetFocus()) return previous; /* changed by the message */
68 if (IsWindow(hwnd))
70 USER_Driver->pSetFocus(hwnd);
72 ime_default = ImmGetDefaultIMEWnd( hwnd );
73 if (ime_default)
74 SendMessageW( ime_default, WM_IME_INTERNAL, IME_INTERNAL_ACTIVATE, (LPARAM)hwnd );
76 if (previous)
77 NotifyWinEvent( EVENT_OBJECT_FOCUS, hwnd, OBJID_CLIENT, 0 );
79 SendMessageW( hwnd, WM_SETFOCUS, (WPARAM)previous, 0 );
81 return previous;
85 /*******************************************************************
86 * set_active_window
88 static BOOL set_active_window( HWND hwnd, HWND *prev, BOOL mouse, BOOL focus )
90 HWND previous = GetActiveWindow();
91 BOOL ret;
92 DWORD old_thread, new_thread;
93 CBTACTIVATESTRUCT cbt;
95 if (previous == hwnd)
97 if (prev) *prev = hwnd;
98 return TRUE;
101 /* call CBT hook chain */
102 cbt.fMouse = mouse;
103 cbt.hWndActive = previous;
104 if (HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hwnd, (LPARAM)&cbt, TRUE )) return FALSE;
106 if (IsWindow(previous))
108 SendMessageW( previous, WM_NCACTIVATE, FALSE, (LPARAM)hwnd );
109 SendMessageW( previous, WM_ACTIVATE,
110 MAKEWPARAM( WA_INACTIVE, IsIconic(previous) ), (LPARAM)hwnd );
113 SERVER_START_REQ( set_active_window )
115 req->handle = wine_server_user_handle( hwnd );
116 if ((ret = !wine_server_call_err( req )))
117 previous = wine_server_ptr_handle( reply->previous );
119 SERVER_END_REQ;
120 if (!ret) return FALSE;
121 if (prev) *prev = previous;
122 if (previous == hwnd) return TRUE;
124 if (hwnd)
126 /* send palette messages */
127 if (SendMessageW( hwnd, WM_QUERYNEWPALETTE, 0, 0 ))
128 SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTEISCHANGING, (WPARAM)hwnd, 0,
129 SMTO_ABORTIFHUNG, 2000, NULL );
130 if (!IsWindow(hwnd)) return FALSE;
133 old_thread = previous ? GetWindowThreadProcessId( previous, NULL ) : 0;
134 new_thread = hwnd ? GetWindowThreadProcessId( hwnd, NULL ) : 0;
136 if (old_thread != new_thread)
138 HWND *list, *phwnd;
140 if ((list = WIN_ListChildren( GetDesktopWindow() )))
142 if (old_thread)
144 for (phwnd = list; *phwnd; phwnd++)
146 if (GetWindowThreadProcessId( *phwnd, NULL ) == old_thread)
147 SendMessageW( *phwnd, WM_ACTIVATEAPP, 0, new_thread );
150 if (new_thread)
152 for (phwnd = list; *phwnd; phwnd++)
154 if (GetWindowThreadProcessId( *phwnd, NULL ) == new_thread)
155 SendMessageW( *phwnd, WM_ACTIVATEAPP, 1, old_thread );
158 HeapFree( GetProcessHeap(), 0, list );
162 if (IsWindow(hwnd))
164 SendMessageW( hwnd, WM_NCACTIVATE, (hwnd == GetForegroundWindow()), (LPARAM)previous );
165 SendMessageW( hwnd, WM_ACTIVATE,
166 MAKEWPARAM( mouse ? WA_CLICKACTIVE : WA_ACTIVE, IsIconic(hwnd) ),
167 (LPARAM)previous );
168 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
169 PostMessageW( GetDesktopWindow(), WM_PARENTNOTIFY, WM_NCACTIVATE, (LPARAM)hwnd );
172 /* now change focus if necessary */
173 if (focus)
175 GUITHREADINFO info;
177 info.cbSize = sizeof(info);
178 GetGUIThreadInfo( GetCurrentThreadId(), &info );
179 /* Do not change focus if the window is no more active */
180 if (hwnd == info.hwndActive)
182 if (!info.hwndFocus || !hwnd || GetAncestor( info.hwndFocus, GA_ROOT ) != hwnd)
183 set_focus_window( hwnd );
187 return TRUE;
191 /*******************************************************************
192 * set_foreground_window
194 static BOOL set_foreground_window( HWND hwnd, BOOL mouse )
196 BOOL ret, send_msg_old = FALSE, send_msg_new = FALSE;
197 HWND previous = 0;
199 SERVER_START_REQ( set_foreground_window )
201 req->handle = wine_server_user_handle( hwnd );
202 if ((ret = !wine_server_call_err( req )))
204 previous = wine_server_ptr_handle( reply->previous );
205 send_msg_old = reply->send_msg_old;
206 send_msg_new = reply->send_msg_new;
209 SERVER_END_REQ;
211 if (ret && previous != hwnd)
213 if (send_msg_old) /* old window belongs to other thread */
214 SendNotifyMessageW( previous, WM_WINE_SETACTIVEWINDOW, 0, 0 );
215 else if (send_msg_new) /* old window belongs to us but new one to other thread */
216 ret = set_active_window( 0, NULL, mouse, TRUE );
218 if (send_msg_new) /* new window belongs to other thread */
219 SendNotifyMessageW( hwnd, WM_WINE_SETACTIVEWINDOW, (WPARAM)hwnd, 0 );
220 else /* new window belongs to us */
221 ret = set_active_window( hwnd, NULL, mouse, TRUE );
223 return ret;
227 /*******************************************************************
228 * FOCUS_MouseActivate
230 * Activate a window as a result of a mouse click
232 BOOL FOCUS_MouseActivate( HWND hwnd )
234 return set_foreground_window( hwnd, TRUE );
238 /*******************************************************************
239 * SetActiveWindow (USER32.@)
241 HWND WINAPI SetActiveWindow( HWND hwnd )
243 HWND prev;
245 TRACE( "%p\n", hwnd );
247 if (hwnd)
249 LONG style;
251 hwnd = WIN_GetFullHandle( hwnd );
252 if (!IsWindow( hwnd ))
254 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
255 return 0;
258 style = GetWindowLongW( hwnd, GWL_STYLE );
259 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD)
260 return GetActiveWindow(); /* Windows doesn't seem to return an error here */
263 if (!set_active_window( hwnd, &prev, FALSE, TRUE )) return 0;
264 return prev;
268 /*****************************************************************
269 * SetFocus (USER32.@)
271 HWND WINAPI SetFocus( HWND hwnd )
273 HWND hwndTop = hwnd;
274 HWND previous = GetFocus();
276 TRACE( "%p prev %p\n", hwnd, previous );
278 if (hwnd)
280 /* Check if we can set the focus to this window */
281 hwnd = WIN_GetFullHandle( hwnd );
282 if (!IsWindow( hwnd ))
284 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
285 return 0;
287 if (hwnd == previous) return previous; /* nothing to do */
288 for (;;)
290 HWND parent;
291 LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
292 if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
293 if (!(style & WS_CHILD)) break;
294 parent = GetAncestor( hwndTop, GA_PARENT );
295 if (!parent || parent == GetDesktopWindow())
297 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return 0;
298 break;
300 if (parent == get_hwnd_message_parent()) return 0;
301 hwndTop = parent;
304 /* call hooks */
305 if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)previous, TRUE )) return 0;
307 /* activate hwndTop if needed. */
308 if (hwndTop != GetActiveWindow())
310 if (!set_active_window( hwndTop, NULL, FALSE, FALSE )) return 0;
311 if (!IsWindow( hwnd )) return 0; /* Abort if window destroyed */
313 /* Do not change focus if the window is no longer active */
314 if (hwndTop != GetActiveWindow()) return 0;
317 else /* NULL hwnd passed in */
319 if (!previous) return 0; /* nothing to do */
320 if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)previous, TRUE )) return 0;
323 /* change focus and send messages */
324 return set_focus_window( hwnd );
328 /*******************************************************************
329 * SetForegroundWindow (USER32.@)
331 BOOL WINAPI SetForegroundWindow( HWND hwnd )
333 TRACE( "%p\n", hwnd );
335 hwnd = WIN_GetFullHandle( hwnd );
336 return set_foreground_window( hwnd, FALSE );
340 /*******************************************************************
341 * GetActiveWindow (USER32.@)
343 HWND WINAPI GetActiveWindow(void)
345 HWND ret = 0;
347 SERVER_START_REQ( get_thread_input )
349 req->tid = GetCurrentThreadId();
350 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->active );
352 SERVER_END_REQ;
353 return ret;
357 /*****************************************************************
358 * GetFocus (USER32.@)
360 HWND WINAPI GetFocus(void)
362 HWND ret = 0;
364 SERVER_START_REQ( get_thread_input )
366 req->tid = GetCurrentThreadId();
367 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->focus );
369 SERVER_END_REQ;
370 return ret;
374 /*******************************************************************
375 * GetForegroundWindow (USER32.@)
377 HWND WINAPI GetForegroundWindow(void)
379 HWND ret = 0;
381 SERVER_START_REQ( get_thread_input )
383 req->tid = 0;
384 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->foreground );
386 SERVER_END_REQ;
387 return ret;
391 /***********************************************************************
392 * SetShellWindowEx (USER32.@)
393 * hwndShell = Progman[Program Manager]
394 * |-> SHELLDLL_DefView
395 * hwndListView = | |-> SysListView32
396 * | | |-> tooltips_class32
397 * | |
398 * | |-> SysHeader32
400 * |-> ProxyTarget
402 BOOL WINAPI SetShellWindowEx(HWND hwndShell, HWND hwndListView)
404 BOOL ret;
406 if (GetShellWindow())
407 return FALSE;
409 if (GetWindowLongW(hwndShell, GWL_EXSTYLE) & WS_EX_TOPMOST)
410 return FALSE;
412 if (hwndListView != hwndShell)
413 if (GetWindowLongW(hwndListView, GWL_EXSTYLE) & WS_EX_TOPMOST)
414 return FALSE;
416 if (hwndListView && hwndListView!=hwndShell)
417 SetWindowPos(hwndListView, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
419 SetWindowPos(hwndShell, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
421 SERVER_START_REQ(set_global_windows)
423 req->flags = SET_GLOBAL_SHELL_WINDOWS;
424 req->shell_window = wine_server_user_handle( hwndShell );
425 req->shell_listview = wine_server_user_handle( hwndListView );
426 ret = !wine_server_call_err(req);
428 SERVER_END_REQ;
430 return ret;
434 /*******************************************************************
435 * SetShellWindow (USER32.@)
437 BOOL WINAPI SetShellWindow(HWND hwndShell)
439 return SetShellWindowEx(hwndShell, hwndShell);
443 /*******************************************************************
444 * GetShellWindow (USER32.@)
446 HWND WINAPI GetShellWindow(void)
448 HWND hwndShell = 0;
450 SERVER_START_REQ(set_global_windows)
452 req->flags = 0;
453 if (!wine_server_call_err(req))
454 hwndShell = wine_server_ptr_handle( reply->old_shell_window );
456 SERVER_END_REQ;
458 return hwndShell;
462 /***********************************************************************
463 * SetProgmanWindow (USER32.@)
465 HWND WINAPI SetProgmanWindow ( HWND hwnd )
467 SERVER_START_REQ(set_global_windows)
469 req->flags = SET_GLOBAL_PROGMAN_WINDOW;
470 req->progman_window = wine_server_user_handle( hwnd );
471 if (wine_server_call_err( req )) hwnd = 0;
473 SERVER_END_REQ;
474 return hwnd;
478 /***********************************************************************
479 * GetProgmanWindow (USER32.@)
481 HWND WINAPI GetProgmanWindow(void)
483 HWND ret = 0;
485 SERVER_START_REQ(set_global_windows)
487 req->flags = 0;
488 if (!wine_server_call_err(req))
489 ret = wine_server_ptr_handle( reply->old_progman_window );
491 SERVER_END_REQ;
492 return ret;
496 /***********************************************************************
497 * SetTaskmanWindow (USER32.@)
498 * NOTES
499 * hwnd = MSTaskSwWClass
500 * |-> SysTabControl32
502 HWND WINAPI SetTaskmanWindow ( HWND hwnd )
504 SERVER_START_REQ(set_global_windows)
506 req->flags = SET_GLOBAL_TASKMAN_WINDOW;
507 req->taskman_window = wine_server_user_handle( hwnd );
508 if (wine_server_call_err( req )) hwnd = 0;
510 SERVER_END_REQ;
511 return hwnd;
514 /***********************************************************************
515 * GetTaskmanWindow (USER32.@)
517 HWND WINAPI GetTaskmanWindow(void)
519 HWND ret = 0;
521 SERVER_START_REQ(set_global_windows)
523 req->flags = 0;
524 if (!wine_server_call_err(req))
525 ret = wine_server_ptr_handle( reply->old_taskman_window );
527 SERVER_END_REQ;
528 return ret;