Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / user32 / focus.c
blobefc8b8f5b82f2995f44baa86e2450e55e6a0f9d3
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 "winerror.h"
32 #include "win.h"
33 #include "user_private.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(win);
40 /*****************************************************************
41 * set_focus_window
43 * Change the focus window, sending the WM_SETFOCUS and WM_KILLFOCUS messages
45 static HWND set_focus_window( HWND hwnd )
47 HWND previous = 0;
48 BOOL ret;
50 SERVER_START_REQ( set_focus_window )
52 req->handle = hwnd;
53 if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
55 SERVER_END_REQ;
56 if (!ret) return 0;
57 if (previous == hwnd) return previous;
59 if (previous)
61 SendMessageW( previous, WM_KILLFOCUS, (WPARAM)hwnd, 0 );
62 if (hwnd != GetFocus()) return previous; /* changed by the message */
64 if (IsWindow(hwnd))
66 USER_Driver->pSetFocus(hwnd);
67 SendMessageW( hwnd, WM_SETFOCUS, (WPARAM)previous, 0 );
69 return previous;
73 /*******************************************************************
74 * set_active_window
76 static BOOL set_active_window( HWND hwnd, HWND *prev, BOOL mouse, BOOL focus )
78 HWND previous = GetActiveWindow();
79 BOOL ret;
80 DWORD old_thread, new_thread;
81 CBTACTIVATESTRUCT cbt;
83 if (previous == hwnd)
85 if (prev) *prev = hwnd;
86 return TRUE;
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;
94 if (IsWindow(previous))
96 SendMessageW( previous, WM_NCACTIVATE, FALSE, (LPARAM)hwnd );
97 SendMessageW( previous, WM_ACTIVATE,
98 MAKEWPARAM( WA_INACTIVE, IsIconic(previous) ), (LPARAM)hwnd );
101 SERVER_START_REQ( set_active_window )
103 req->handle = hwnd;
104 if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
106 SERVER_END_REQ;
107 if (!ret) return FALSE;
108 if (prev) *prev = previous;
109 if (previous == hwnd) return TRUE;
111 if (hwnd)
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 );
118 if (!GetPropA( hwnd, "__wine_x11_managed" ))
119 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
121 if (!IsWindow(hwnd)) return FALSE;
124 old_thread = previous ? GetWindowThreadProcessId( previous, NULL ) : 0;
125 new_thread = hwnd ? GetWindowThreadProcessId( hwnd, NULL ) : 0;
127 if (old_thread != new_thread)
129 HWND *list, *phwnd;
131 if ((list = WIN_ListChildren( GetDesktopWindow() )))
133 if (old_thread)
135 for (phwnd = list; *phwnd; phwnd++)
137 if (GetWindowThreadProcessId( *phwnd, NULL ) == old_thread)
138 SendMessageW( *phwnd, WM_ACTIVATEAPP, 0, new_thread );
141 if (new_thread)
143 for (phwnd = list; *phwnd; phwnd++)
145 if (GetWindowThreadProcessId( *phwnd, NULL ) == new_thread)
146 SendMessageW( *phwnd, WM_ACTIVATEAPP, 1, old_thread );
149 HeapFree( GetProcessHeap(), 0, list );
153 if (IsWindow(hwnd))
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 );
161 /* now change focus if necessary */
162 if (focus)
164 GUITHREADINFO info;
166 GetGUIThreadInfo( GetCurrentThreadId(), &info );
167 /* Do not change focus if the window is no more active */
168 if (hwnd == info.hwndActive)
170 if (!info.hwndFocus || !hwnd || GetAncestor( info.hwndFocus, GA_ROOT ) != hwnd)
171 set_focus_window( hwnd );
175 return TRUE;
179 /*******************************************************************
180 * set_foreground_window
182 static BOOL set_foreground_window( HWND hwnd, BOOL mouse )
184 BOOL ret, send_msg_old = FALSE, send_msg_new = FALSE;
185 HWND previous = 0;
187 SERVER_START_REQ( set_foreground_window )
189 req->handle = hwnd;
190 if ((ret = !wine_server_call_err( req )))
192 previous = reply->previous;
193 send_msg_old = reply->send_msg_old;
194 send_msg_new = reply->send_msg_new;
197 SERVER_END_REQ;
199 if (ret)
201 if (send_msg_old) /* old window belongs to other thread */
202 SendNotifyMessageW( previous, WM_WINE_SETACTIVEWINDOW, 0, 0 );
203 else if (send_msg_new) /* old window belongs to us but new one to other thread */
204 ret = set_active_window( 0, NULL, mouse, TRUE );
206 if (send_msg_new) /* new window belongs to other thread */
207 SendNotifyMessageW( hwnd, WM_WINE_SETACTIVEWINDOW, (WPARAM)hwnd, 0 );
208 else /* new window belongs to us */
209 ret = set_active_window( hwnd, NULL, mouse, TRUE );
211 return ret;
215 /*******************************************************************
216 * FOCUS_MouseActivate
218 * Activate a window as a result of a mouse click
220 BOOL FOCUS_MouseActivate( HWND hwnd )
222 return set_foreground_window( hwnd, TRUE );
226 /*******************************************************************
227 * SetActiveWindow (USER32.@)
229 HWND WINAPI SetActiveWindow( HWND hwnd )
231 HWND prev;
233 TRACE( "%p\n", hwnd );
235 if (hwnd)
237 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
239 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD)
240 return GetActiveWindow(); /* Windows doesn't seem to return an error here */
242 hwnd = WIN_GetFullHandle( hwnd );
245 if (!set_active_window( hwnd, &prev, FALSE, TRUE )) return 0;
246 return prev;
250 /*****************************************************************
251 * SetFocus (USER32.@)
253 HWND WINAPI SetFocus( HWND hwnd )
255 HWND hwndTop = hwnd;
256 HWND previous = GetFocus();
258 TRACE( "%p prev %p\n", hwnd, previous );
260 if (hwnd)
262 /* Check if we can set the focus to this window */
263 hwnd = WIN_GetFullHandle( hwnd );
264 if (hwnd == previous) return previous; /* nothing to do */
265 for (;;)
267 HWND parent;
268 LONG style = GetWindowLongW( hwndTop, GWL_STYLE );
269 if (style & (WS_MINIMIZE | WS_DISABLED)) return 0;
270 parent = GetAncestor( hwndTop, GA_PARENT );
271 if (!parent || parent == GetDesktopWindow()) break;
272 hwndTop = parent;
275 /* call hooks */
276 if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)hwnd, (LPARAM)previous, TRUE )) return 0;
278 /* activate hwndTop if needed. */
279 if (hwndTop != GetActiveWindow())
281 if (!set_active_window( hwndTop, NULL, FALSE, FALSE )) return 0;
282 if (!IsWindow( hwnd )) return 0; /* Abort if window destroyed */
285 else /* NULL hwnd passed in */
287 if (!previous) return 0; /* nothing to do */
288 if (HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, 0, (LPARAM)previous, TRUE )) return 0;
291 /* change focus and send messages */
292 return set_focus_window( hwnd );
296 /*******************************************************************
297 * SetForegroundWindow (USER32.@)
299 BOOL WINAPI SetForegroundWindow( HWND hwnd )
301 TRACE( "%p\n", hwnd );
303 hwnd = WIN_GetFullHandle( hwnd );
304 return set_foreground_window( hwnd, FALSE );
308 /*******************************************************************
309 * GetActiveWindow (USER32.@)
311 HWND WINAPI GetActiveWindow(void)
313 HWND ret = 0;
315 SERVER_START_REQ( get_thread_input )
317 req->tid = GetCurrentThreadId();
318 if (!wine_server_call_err( req )) ret = reply->active;
320 SERVER_END_REQ;
321 return ret;
325 /*****************************************************************
326 * GetFocus (USER32.@)
328 HWND WINAPI GetFocus(void)
330 HWND ret = 0;
332 SERVER_START_REQ( get_thread_input )
334 req->tid = GetCurrentThreadId();
335 if (!wine_server_call_err( req )) ret = reply->focus;
337 SERVER_END_REQ;
338 return ret;
342 /*******************************************************************
343 * GetForegroundWindow (USER32.@)
345 HWND WINAPI GetForegroundWindow(void)
347 HWND ret = 0;
349 SERVER_START_REQ( get_thread_input )
351 req->tid = 0;
352 if (!wine_server_call_err( req )) ret = reply->foreground;
354 SERVER_END_REQ;
355 return ret;
359 /***********************************************************************
360 * SetShellWindowEx (USER32.@)
361 * hwndShell = Progman[Program Manager]
362 * |-> SHELLDLL_DefView
363 * hwndListView = | |-> SysListView32
364 * | | |-> tooltips_class32
365 * | |
366 * | |-> SysHeader32
368 * |-> ProxyTarget
370 BOOL WINAPI SetShellWindowEx(HWND hwndShell, HWND hwndListView)
372 BOOL ret;
374 if (GetShellWindow())
375 return FALSE;
377 if (GetWindowLongW(hwndShell, GWL_EXSTYLE) & WS_EX_TOPMOST)
378 return FALSE;
380 if (hwndListView != hwndShell)
381 if (GetWindowLongW(hwndListView, GWL_EXSTYLE) & WS_EX_TOPMOST)
382 return FALSE;
384 if (hwndListView && hwndListView!=hwndShell)
385 SetWindowPos(hwndListView, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
387 SetWindowPos(hwndShell, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
389 SERVER_START_REQ(set_global_windows)
391 req->flags = SET_GLOBAL_SHELL_WINDOWS;
392 req->shell_window = hwndShell;
393 req->shell_listview = hwndListView;
394 ret = !wine_server_call_err(req);
396 SERVER_END_REQ;
398 return ret;
402 /*******************************************************************
403 * SetShellWindow (USER32.@)
405 BOOL WINAPI SetShellWindow(HWND hwndShell)
407 return SetShellWindowEx(hwndShell, hwndShell);
411 /*******************************************************************
412 * GetShellWindow (USER32.@)
414 HWND WINAPI GetShellWindow(void)
416 HWND hwndShell = 0;
418 SERVER_START_REQ(set_global_windows)
420 req->flags = 0;
421 if (!wine_server_call_err(req))
422 hwndShell = reply->old_shell_window;
424 SERVER_END_REQ;
426 return hwndShell;
430 /***********************************************************************
431 * SetProgmanWindow (USER32.@)
433 HWND WINAPI SetProgmanWindow ( HWND hwnd )
435 SERVER_START_REQ(set_global_windows)
437 req->flags = SET_GLOBAL_PROGMAN_WINDOW;
438 req->progman_window = hwnd;
439 if (wine_server_call_err( req )) hwnd = 0;
441 SERVER_END_REQ;
442 return hwnd;
446 /***********************************************************************
447 * GetProgmanWindow (USER32.@)
449 HWND WINAPI GetProgmanWindow(void)
451 HWND ret = 0;
453 SERVER_START_REQ(set_global_windows)
455 req->flags = 0;
456 if (!wine_server_call_err(req)) ret = reply->old_progman_window;
458 SERVER_END_REQ;
459 return ret;
463 /***********************************************************************
464 * SetTaskmanWindow (USER32.@)
465 * NOTES
466 * hwnd = MSTaskSwWClass
467 * |-> SysTabControl32
469 HWND WINAPI SetTaskmanWindow ( HWND hwnd )
471 SERVER_START_REQ(set_global_windows)
473 req->flags = SET_GLOBAL_TASKMAN_WINDOW;
474 req->taskman_window = hwnd;
475 if (wine_server_call_err( req )) hwnd = 0;
477 SERVER_END_REQ;
478 return hwnd;
481 /***********************************************************************
482 * GetTaskmanWindow (USER32.@)
484 HWND WINAPI GetTaskmanWindow(void)
486 HWND ret = 0;
488 SERVER_START_REQ(set_global_windows)
490 req->flags = 0;
491 if (!wine_server_call_err(req)) ret = reply->old_taskman_window;
493 SERVER_END_REQ;
494 return ret;