2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Focus functions
5 * FILE: subsystems/win32/win32k/ntuser/focus.c
6 * PROGRAMER: ReactOS Team
10 DBG_DEFAULT_CHANNEL(UserFocus
);
12 PUSER_MESSAGE_QUEUE gpqForeground
= NULL
;
13 PUSER_MESSAGE_QUEUE gpqForegroundPrev
= NULL
;
14 PTHREADINFO gptiForeground
= NULL
;
15 PPROCESSINFO gppiLockSFW
= NULL
;
16 ULONG guSFWLockCount
= 0; // Rule #8, No menus are active. So should be zero.
17 PTHREADINFO ptiLastInput
= NULL
;
20 Check locking of a process or one or more menus are active.
25 return (gppiLockSFW
|| guSFWLockCount
);
29 IntGetCaptureWindow(VOID
)
31 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue();
32 return ( ForegroundQueue
? (ForegroundQueue
->spwndCapture
? UserHMGetHandle(ForegroundQueue
->spwndCapture
) : 0) : 0);
36 IntGetThreadFocusWindow(VOID
)
39 PUSER_MESSAGE_QUEUE ThreadQueue
;
41 pti
= PsGetCurrentThreadWin32Thread();
42 ThreadQueue
= pti
->MessageQueue
;
45 return ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
49 co_IntSendDeactivateMessages(HWND hWndPrev
, HWND hWnd
)
51 USER_REFERENCE_ENTRY RefPrev
;
55 if (hWndPrev
&& (WndPrev
= ValidateHwndNoErr(hWndPrev
)))
57 UserRefObjectCo(WndPrev
, &RefPrev
);
59 if (co_IntSendMessageNoWait(hWndPrev
, WM_NCACTIVATE
, FALSE
, 0)) //(LPARAM)hWnd))
61 co_IntSendMessageNoWait(hWndPrev
, WM_ACTIVATE
,
62 MAKEWPARAM(WA_INACTIVE
, WndPrev
->style
& WS_MINIMIZE
),
66 WndPrev
->state
&= ~WNDS_ACTIVEFRAME
;
70 ERR("Application is keeping itself Active to prevent the change!\n");
74 UserDerefObjectCo(WndPrev
);
80 co_IntMakeWindowActive(PWND Window
)
83 if (VerifyWnd(Window
))
84 { // Set last active for window and it's owner.
86 while (spwndOwner
->spwndOwner
)
88 spwndOwner
= spwndOwner
->spwndOwner
;
90 spwndOwner
->spwndLastActive
= Window
;
93 ERR("MakeWindowActive Failed!\n");
98 co_IntSendActivateMessages(PWND WindowPrev
, PWND Window
, BOOL MouseActivate
, BOOL Async
)
100 USER_REFERENCE_ENTRY Ref
, RefPrev
;
101 HANDLE OldTID
, NewTID
;
102 PTHREADINFO pti
, ptiOld
, ptiNew
;
105 //ERR("SendActivateMessages\n");
107 pti
= PsGetCurrentThreadWin32Thread();
111 UserRefObjectCo(Window
, &Ref
);
113 if (WindowPrev
) UserRefObjectCo(WindowPrev
, &RefPrev
);
115 /* Send palette messages */
116 if (gpsi
->PUSIFlags
& PUSIF_PALETTEDISPLAY
&&
117 //co_IntPostOrSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0))
118 co_IntSendMessage(UserHMGetHandle(Window
), WM_QUERYNEWPALETTE
, 0, 0))
120 UserSendNotifyMessage( HWND_BROADCAST
,
121 WM_PALETTEISCHANGING
,
122 (WPARAM
)UserHMGetHandle(Window
),
125 //// Fixes CORE-6434.
126 if (!(Window
->style
& WS_CHILD
))
128 PWND pwndTemp
= co_GetDesktopWindow(Window
)->spwndChild
;
130 while (pwndTemp
&& !(pwndTemp
->style
& WS_VISIBLE
)) pwndTemp
= pwndTemp
->spwndNext
;
132 if (Window
!= pwndTemp
|| (WindowPrev
&& !IntIsWindowVisible(WindowPrev
)))
134 if (!Async
|| pti
->MessageQueue
== gpqForeground
)
136 UINT flags
= SWP_NOSIZE
| SWP_NOMOVE
;
137 if (Window
== pwndTemp
) flags
|= SWP_NOACTIVATE
;
138 //ERR("co_IntSendActivateMessages SetWindowPos! Async %d pti Q == FGQ %d\n",Async,pti->MessageQueue == gpqForeground);
139 co_WinPosSetWindowPos(Window
, HWND_TOP
, 0, 0, 0, 0, flags
);
144 //// CORE-1161 and CORE-6651
145 if (Window
->spwndPrev
)
147 HWND
*phwndTopLevel
, *phwndCurrent
;
148 PWND pwndCurrent
, pwndDesktop
;
150 pwndDesktop
= UserGetDesktopWindow();
151 if (Window
->spwndParent
== pwndDesktop
)
153 phwndTopLevel
= IntWinListChildren(pwndDesktop
);
154 phwndCurrent
= phwndTopLevel
;
157 pwndCurrent
= UserGetWindowObject(*phwndCurrent
);
159 if (pwndCurrent
&& pwndCurrent
->spwndOwner
== Window
)
161 co_WinPosSetWindowPos(pwndCurrent
, HWND_TOP
, 0, 0, 0, 0, SWP_NOSIZE
|SWP_NOMOVE
|SWP_NOACTIVATE
);
165 ExFreePool(phwndTopLevel
);
170 OldTID
= WindowPrev
? IntGetWndThreadId(WindowPrev
) : NULL
;
171 NewTID
= Window
? IntGetWndThreadId(Window
) : NULL
;
172 ptiOld
= WindowPrev
? WindowPrev
->head
.pti
: NULL
;
173 ptiNew
= Window
? Window
->head
.pti
: NULL
;
175 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID);
177 if (!(pti
->TIF_flags
& TIF_INACTIVATEAPPMSG
) &&
178 (!WindowPrev
|| OldTID
!= NewTID
) )
183 List
= IntWinListChildren(UserGetDesktopWindow());
188 ptiOld
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
189 // Note: Do not set pci flags, this does crash!
190 for (phWnd
= List
; *phWnd
; ++phWnd
)
192 cWindow
= ValidateHwndNoErr(*phWnd
);
193 if (cWindow
&& cWindow
->head
.pti
== ptiOld
)
194 { // FALSE if the window is being deactivated,
195 // ThreadId that owns the window being activated.
196 //ERR("SendActivateMessage Old\n");
197 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
200 ptiOld
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
203 { //// Prevents a resource crash due to reentrance!
205 pti
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
207 for (phWnd
= List
; *phWnd
; ++phWnd
)
209 cWindow
= ValidateHwndNoErr(*phWnd
);
210 if (cWindow
&& cWindow
->head
.pti
== ptiNew
)
211 { // TRUE if the window is being activated,
212 // ThreadId that owns the window being deactivated.
213 //ERR("SendActivateMessage New\n");
214 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
218 ExFreePoolWithTag(List
, USERTAG_WINDOWLIST
);
225 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
227 if (Window
->state
& WNDS_ACTIVEFRAME
)
228 { // If already active frame do not allow NCPaint.
229 //ERR("SendActivateMessage Is Active Frame!\n");
230 Window
->state
|= WNDS_NONCPAINT
;
233 if (Window
->style
& WS_MINIMIZE
)
235 TRACE("Widow was minimized\n");
238 co_IntMakeWindowActive(Window
);
240 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
242 (WPARAM
)(Window
== (gpqForeground
? gpqForeground
->spwndActive
: NULL
)),
243 0); //(LPARAM)hWndPrev);
245 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
247 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
, Window
->style
& WS_MINIMIZE
),
248 (LPARAM
)(WindowPrev
? UserHMGetHandle(WindowPrev
) : 0));
250 if (Window
->spwndParent
== UserGetDesktopWindow() &&
251 Window
->spwndOwner
== NULL
&&
252 (!(Window
->ExStyle
& WS_EX_TOOLWINDOW
) ||
253 (Window
->ExStyle
& WS_EX_APPWINDOW
)))
255 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
256 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (WPARAM
) UserHMGetHandle(Window
), FALSE
);
260 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, 0, FALSE
);
263 Window
->state
&= ~WNDS_NONCPAINT
;
265 UserDerefObjectCo(Window
);
268 OldTID
= WindowPrev
? IntGetWndThreadId(WindowPrev
) : NULL
;
269 NewTID
= Window
? IntGetWndThreadId(Window
) : NULL
;
270 ptiOld
= WindowPrev
? WindowPrev
->head
.pti
: NULL
;
271 ptiNew
= Window
? Window
->head
.pti
: NULL
;
273 //ERR("SendActivateMessage WindowPrev -> %x, Old -> %x, New -> %x\n", WindowPrev, OldTID, NewTID);
275 if (!(pti
->TIF_flags
& TIF_INACTIVATEAPPMSG
) &&
281 List
= IntWinListChildren(UserGetDesktopWindow());
286 ptiOld
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
287 // Note: Do not set pci flags, this does crash!
288 for (phWnd
= List
; *phWnd
; ++phWnd
)
290 cWindow
= ValidateHwndNoErr(*phWnd
);
291 if (cWindow
&& cWindow
->head
.pti
== ptiOld
)
292 { // FALSE if the window is being deactivated,
293 // ThreadId that owns the window being activated.
294 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
297 ptiOld
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
300 { //// Prevents a resource crash due to reentrance!
302 pti
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
304 for (phWnd
= List
; *phWnd
; ++phWnd
)
306 cWindow
= ValidateHwndNoErr(*phWnd
);
307 if (cWindow
&& cWindow
->head
.pti
== ptiNew
)
308 { // TRUE if the window is being activated,
309 // ThreadId that owns the window being deactivated.
310 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
314 ExFreePoolWithTag(List
, USERTAG_WINDOWLIST
);
321 IntSendFocusMessages( PTHREADINFO pti
, PWND pWnd
)
324 PUSER_MESSAGE_QUEUE ThreadQueue
= pti
->MessageQueue
; // Queue can change...
326 ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
327 if (!pWnd
&& ThreadQueue
->spwndActive
)
329 ThreadQueue
->QF_flags
|= QF_FOCUSNULLSINCEACTIVE
;
332 pWndPrev
= ThreadQueue
->spwndFocus
;
334 /* check if the specified window can be set in the input data of a given queue */
335 if (!pWnd
|| ThreadQueue
== pWnd
->head
.pti
->MessageQueue
)
336 /* set the current thread focus window */
337 ThreadQueue
->spwndFocus
= pWnd
;
343 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
344 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, (WPARAM
)UserHMGetHandle(pWnd
), 0);
346 if (ThreadQueue
->spwndFocus
== pWnd
)
348 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, pWnd
, OBJID_CLIENT
, CHILDID_SELF
, 0);
349 //co_IntPostOrSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)(pWndPrev ? UserHMGetHandle(pWndPrev) : NULL), 0);
350 co_IntSendMessage(UserHMGetHandle(pWnd
), WM_SETFOCUS
, (WPARAM
)(pWndPrev
? UserHMGetHandle(pWndPrev
) : NULL
), 0);
357 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, NULL
, OBJID_CLIENT
, CHILDID_SELF
, 0);
358 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
359 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, 0, 0);
365 FindRemoveAsyncMsg(PWND Wnd
, WPARAM wParam
)
368 PUSER_SENT_MESSAGE Message
;
375 if (!IsListEmpty(&pti
->SentMessagesListHead
))
377 // Scan sent queue messages to see if we received async messages.
378 Entry
= pti
->SentMessagesListHead
.Flink
;
379 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
382 if (IsListEmpty(Entry
)) return;
383 if (!Message
) return;
384 Entry
= Message
->ListEntry
.Flink
;
386 if (Message
->Msg
.message
== WM_ASYNC_SETACTIVEWINDOW
&&
387 Message
->Msg
.hwnd
== UserHMGetHandle(Wnd
) &&
388 Message
->Msg
.wParam
== wParam
)
390 ERR("ASYNC SAW: Found one in the Sent Msg Queue! %p Activate/Deactivate %d\n", Message
->Msg
.hwnd
,!!wParam
);
391 RemoveEntryList(&Message
->ListEntry
); // Purge the entry.
392 ExFreePoolWithTag(Message
, TAG_USRMSG
);
394 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
396 while (Entry
!= &pti
->SentMessagesListHead
);
401 ToggleFGActivate(PTHREADINFO pti
)
404 PPROCESSINFO ppi
= pti
->ppi
;
406 Ret
= !!(pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
);
409 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
412 Ret
= !!(ppi
->W32PF_flags
& W32PF_ALLOWFOREGROUNDACTIVATE
);
415 ppi
->W32PF_flags
&= ~W32PF_ALLOWFOREGROUNDACTIVATE
;
416 //ERR("ToggleFGActivate is %d\n",Ret);
421 IsAllowedFGActive(PTHREADINFO pti
, PWND Wnd
)
423 // Not allowed if one or more,,
424 if (!ToggleFGActivate(pti
) || // bits not set,
425 pti
->rpdesk
!= gpdeskInputDesktop
|| // not current Desktop,
426 pti
->MessageQueue
== gpqForeground
|| // if already the queue foreground,
427 IsFGLocked() || // foreground is locked,
428 Wnd
->ExStyle
& WS_EX_NOACTIVATE
) // or,,, does not become the foreground window when the user clicks it.
432 //ERR("IsAllowedFGActive is TRUE\n");
437 Can the system force foreground from one or more conditions.
440 CanForceFG(PPROCESSINFO ppi
)
443 ptiLastInput
->ppi
== ppi
||
445 gptiForeground
->ppi
== ppi
||
446 ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_SETFOREGROUNDALLOWED
) ||
447 gppiInputProvider
== ppi
||
450 //ERR("CanForceFG is FALSE\n");
456 The system restricts which processes can set the foreground window. A process
457 can set the foreground window only if one of the following conditions is true:
459 * The process is the foreground process.
460 * The process was started by the foreground process.
461 * The process received the last input event.
462 * There is no foreground process.
463 * The foreground process is being debugged.
464 * The foreground is not locked (see LockSetForegroundWindow).
465 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
466 * No menus are active.
470 co_IntSetForegroundAndFocusWindow(
472 _In_ BOOL MouseActivate
)
474 HWND hWnd
= Wnd
? UserHMGetHandle(Wnd
) : NULL
;
475 HWND hWndPrev
= NULL
;
476 PWND pWndPrev
= NULL
;
477 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
479 BOOL fgRet
= FALSE
, Ret
= FALSE
;
481 if (Wnd
) ASSERT_REFS_CO(Wnd
);
483 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
485 PrevForegroundQueue
= IntGetFocusMessageQueue(); // Use this active desktop.
486 pti
= PsGetCurrentThreadWin32Thread();
488 if (PrevForegroundQueue
)
489 { // Same Window Q as foreground just do active.
490 if (Wnd
&& Wnd
->head
.pti
->MessageQueue
== PrevForegroundQueue
)
492 //ERR("Same Window Q as foreground just do active.\n");
493 if (pti
->MessageQueue
== PrevForegroundQueue
)
494 { // Same WQ and TQ go active.
495 //ERR("Same WQ and TQ go active.\n");
496 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
498 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
499 { // Same WQ and it is active.
500 //ERR("Same WQ and it is active.\n");
504 { // Same WQ as FG but not the same TQ send active.
505 //ERR("Same WQ as FG but not the same TQ send active.\n");
506 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
512 hWndPrev
= PrevForegroundQueue
->spwndActive
? UserHMGetHandle(PrevForegroundQueue
->spwndActive
) : 0;
513 pWndPrev
= PrevForegroundQueue
->spwndActive
;
516 if ( (( !IsFGLocked() || pti
->ppi
== gppiInputProvider
) &&
517 ( CanForceFG(pti
->ppi
) || pti
->TIF_flags
& (TIF_SYSTEMTHREAD
|TIF_CSRSSTHREAD
|TIF_ALLOWFOREGROUNDACTIVATE
) )) ||
518 pti
->ppi
== ppiScrnSaver
522 //ToggleFGActivate(pti); // win.c line 2662 fail
525 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
526 gptiForeground
= Wnd
->head
.pti
;
527 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
531 IntSetFocusMessageQueue(NULL
);
532 gptiForeground
= NULL
;
533 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
537 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
538 other thread after we already changed the foreground window back to our own
542 FindRemoveAsyncMsg(Wnd
, 0); // Do this to fix test_SFW todos!
547 // Fix FG Bounce with regedit.
548 if (hWndPrev
!= hWnd
)
550 if (PrevForegroundQueue
&&
552 PrevForegroundQueue
->spwndActive
)
554 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
555 if (pti
->MessageQueue
== PrevForegroundQueue
)
557 //ERR("SFGW: TI same as Prev TI\n");
558 co_IntSetActiveWindow(NULL
, FALSE
, TRUE
, FALSE
);
562 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
563 // No real reason to wait here.
564 co_IntSendMessageNoWait(hWndPrev
, WM_ASYNC_SETACTIVEWINDOW
, 0, 0 );
569 if (!Wnd
) return FALSE
; // Always return false.
571 if (pti
->MessageQueue
== Wnd
->head
.pti
->MessageQueue
)
573 //ERR("Same PQ and WQ go active.\n");
574 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
576 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
578 //ERR("Same Active and Wnd.\n");
583 //ERR("Activate Not same PQ and WQ and Wnd.\n");
584 co_IntSendMessageNoWait(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
591 Revision 7888, activate modal dialog when clicking on a disabled window.
594 IntFindChildWindowToOwner(PWND Root
, PWND Owner
)
597 PWND Child
, OwnerWnd
;
599 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
601 OwnerWnd
= Child
->spwndOwner
;
605 if(OwnerWnd
== Owner
)
615 co_IntMouseActivateWindow(PWND Wnd
)
618 USER_REFERENCE_ENTRY Ref
;
621 if (Wnd
->style
& WS_DISABLED
)
625 PWND DesktopWindow
= UserGetDesktopWindow();
628 ERR("Window Diabled\n");
629 Top
= IntFindChildWindowToOwner(DesktopWindow
, Wnd
);
630 if ((TopWnd
= ValidateHwndNoErr(Top
)))
632 UserRefObjectCo(TopWnd
, &Ref
);
633 Ret
= co_IntMouseActivateWindow(TopWnd
);
634 UserDerefObjectCo(TopWnd
);
641 ERR("Mouse Active\n");
642 co_IntSetForegroundAndFocusWindow(Wnd
, TRUE
);
647 co_IntSetActiveWindow(PWND Wnd OPTIONAL
, BOOL bMouse
, BOOL bFocus
, BOOL Async
)
650 PUSER_MESSAGE_QUEUE ThreadQueue
;
651 PWND pWndChg
, WndPrev
; // State changes.
655 CBTACTIVATESTRUCT cbt
;
656 //ERR("co_IntSetActiveWindow 1\n");
660 hWnd
= UserHMGetHandle(Wnd
);
661 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
662 if (Wnd
== UserGetDesktopWindow()) return FALSE
;
663 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
666 //ERR("co_IntSetActiveWindow 2\n");
667 pti
= PsGetCurrentThreadWin32Thread();
668 ThreadQueue
= pti
->MessageQueue
;
669 ASSERT(ThreadQueue
!= 0);
671 hWndPrev
= ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : NULL
;
673 pWndChg
= ThreadQueue
->spwndActive
; // Keep to notify of a preemptive switch.
677 BOOL Ret
, DoFG
, AllowFG
;
679 if (Wnd
->state
& WNDS_BEINGACTIVATED
) return TRUE
;
681 if (ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
683 if (IsAllowedFGActive(pti
, Wnd
))
689 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
692 AllowFG
= !pti
->cVisWindows
; // Nothing is visable.
693 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
695 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
697 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
698 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
699 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
700 if (!gpqForeground
|| gpqForeground
== ThreadQueue
)
708 if (pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
|| pti
->cVisWindows
)
715 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
720 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
721 //ERR("co_IntSetActiveWindow 3c FG set\n");
722 Ret
= co_IntSetForegroundAndFocusWindow(Wnd
, bMouse
);
725 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
729 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
735 /* Call CBT hook chain */
737 cbt
.hWndActive
= hWndPrev
;
738 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
740 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
744 if ( ThreadQueue
->spwndActive
&& ThreadQueue
->spwndActive
->state
& WNDS_DESTROYED
)
745 ThreadQueue
->spwndActive
= NULL
;
747 ThreadQueue
->spwndActivePrev
= ThreadQueue
->spwndActive
;
749 WndPrev
= ThreadQueue
->spwndActive
; // Keep to save changing active.
753 if (ThreadQueue
== gpqForeground
) gpqForegroundPrev
= ThreadQueue
;
754 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev
), hWnd
)) return FALSE
;
757 WndPrev
= ThreadQueue
->spwndActive
; // Again keep to save changing active.
759 // While in calling message proc or hook:
760 // Fail if a preemptive switch was made, current active not made previous,
761 // focus window is dead or no longer the same thread queue.
762 if ( ThreadQueue
->spwndActivePrev
!= ThreadQueue
->spwndActive
||
763 pWndChg
!= WndPrev
||
764 (Wnd
&& !VerifyWnd(Wnd
)) ||
765 ThreadQueue
!= pti
->MessageQueue
)
767 ERR("SetActiveWindow: Summery ERROR, active state changed!\n");
771 if (!WndPrev
) ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
773 if (Wnd
) Wnd
->state
|= WNDS_BEINGACTIVATED
;
775 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND
, Wnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
776 //// Breaks Atl-Esc/Tab via User32.
777 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
779 /* check if the specified window can be set in the input data of a given queue */
780 if ( !Wnd
|| ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
782 /* set the current thread active window */
783 ThreadQueue
->spwndActive
= Wnd
;
786 WndPrev
= VerifyWnd(ThreadQueue
->spwndActivePrev
); // Now should be set but verify it again.
788 InAAPM
= co_IntSendActivateMessages(WndPrev
, Wnd
, bMouse
, Async
);
790 /* now change focus if necessary */
791 //// Fixes CORE-6452 allows setting focus on window.
792 if (bFocus
&& !(ThreadQueue
->QF_flags
& QF_FOCUSNULLSINCEACTIVE
))
794 /* Do not change focus if the window is no longer active */
795 if (pti
->MessageQueue
->spwndActive
!= IntGetNonChildAncestor(pti
->MessageQueue
->spwndFocus
))
797 PWND pWndSend
= pti
->MessageQueue
->spwndActive
;
798 // Clear focus if the active window is minimized.
799 if (pWndSend
&& pti
->MessageQueue
->spwndActive
->style
& WS_MINIMIZE
) pWndSend
= NULL
;
800 // Send focus messages and if so, set the focus.
801 IntSendFocusMessages( pti
, pWndSend
);
807 pti
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
810 // FIXME: Used in the menu loop!!!
811 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
813 //ERR("co_IntSetActiveWindow Exit\n");
814 if (Wnd
) Wnd
->state
&= ~WNDS_BEINGACTIVATED
;
815 return (ThreadQueue
->spwndActive
== Wnd
);
819 UserSetActiveWindow(PWND Wnd
)
821 if (Wnd
) // Must have a window!
823 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
825 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
828 Yes your eye are not deceiving you~!
830 First part of wines Win.c test_SetActiveWindow:
832 flush_events( TRUE );
833 ShowWindow(hwnd, SW_HIDE);
836 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
843 co_UserSetFocus(PWND Window
)
848 PUSER_MESSAGE_QUEUE ThreadQueue
;
851 ASSERT_REFS_CO(Window
);
853 pti
= PsGetCurrentThreadWin32Thread();
854 ThreadQueue
= pti
->MessageQueue
;
855 ASSERT(ThreadQueue
!= 0);
857 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window
? UserHMGetHandle(Window
) : 0, pti
);
859 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
863 if (hWndPrev
== UserHMGetHandle(Window
))
865 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0; /* Nothing to do */
868 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
870 ERR("SetFocus Must have the same Q!\n");
874 /* Check if we can set the focus to this window */
875 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720...
876 for (pwndTop
= Window
; pwndTop
; pwndTop
= pwndTop
->spwndParent
)
878 if (pwndTop
->style
& (WS_MINIMIZED
|WS_DISABLED
)) return 0;
879 if ((pwndTop
->style
& (WS_POPUP
|WS_CHILD
)) != WS_CHILD
) break;
882 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->head
.h
, (LPARAM
)hWndPrev
))
884 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
888 /* Activate pwndTop if needed. */
889 if (pwndTop
!= ThreadQueue
->spwndActive
)
891 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue(); // Keep it based on desktop.
892 if (ThreadQueue
!= ForegroundQueue
&& IsAllowedFGActive(pti
, pwndTop
)) // Rule 2 & 3.
894 //ERR("SetFocus: Set Foreground!\n");
895 if (!(pwndTop
->style
& WS_VISIBLE
))
897 pti
->ppi
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
;
899 if (!co_IntSetForegroundAndFocusWindow(pwndTop
, FALSE
))
901 ERR("SetFocus: Set Foreground and Focus Failed!\n");
906 /* Set Active when it is needed. */
907 if (pwndTop
!= ThreadQueue
->spwndActive
)
909 //ERR("SetFocus: Set Active!\n");
910 if (!co_IntSetActiveWindow(pwndTop
, FALSE
, FALSE
, FALSE
))
912 ERR("SetFocus: Set Active Failed!\n");
917 /* Abort if window destroyed */
918 if (Window
->state2
& WNDS2_INDESTROY
) return 0;
919 /* Do not change focus if the window is no longer active */
920 if (pwndTop
!= ThreadQueue
->spwndActive
)
922 ERR("SetFocus: Top window did not go active!\n");
927 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
928 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
930 IntSendFocusMessages( pti
, Window
);
932 TRACE("Focus: %p -> %p\n", hWndPrev
, Window
->head
.h
);
934 else /* NULL hwnd passed in */
936 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
938 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
942 /* set the current thread focus window null */
943 IntSendFocusMessages( pti
, NULL
);
945 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0;
949 UserGetForegroundWindow(VOID
)
951 PUSER_MESSAGE_QUEUE ForegroundQueue
;
953 ForegroundQueue
= IntGetFocusMessageQueue();
954 return( ForegroundQueue
? (ForegroundQueue
->spwndActive
? UserHMGetHandle(ForegroundQueue
->spwndActive
) : 0) : 0);
957 HWND FASTCALL
UserGetActiveWindow(VOID
)
960 PUSER_MESSAGE_QUEUE ThreadQueue
;
962 pti
= PsGetCurrentThreadWin32Thread();
963 ThreadQueue
= pti
->MessageQueue
;
964 return( ThreadQueue
? (ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : 0) : 0);
971 PUSER_MESSAGE_QUEUE ThreadQueue
;
972 DECLARE_RETURN(HWND
);
974 TRACE("Enter IntGetCapture\n");
976 pti
= PsGetCurrentThreadWin32Thread();
977 ThreadQueue
= pti
->MessageQueue
;
978 RETURN( ThreadQueue
? (ThreadQueue
->spwndCapture
? UserHMGetHandle(ThreadQueue
->spwndCapture
) : 0) : 0);
981 TRACE("Leave IntGetCapture, ret=%p\n", _ret_
);
986 co_UserSetCapture(HWND hWnd
)
989 PUSER_MESSAGE_QUEUE ThreadQueue
;
990 PWND pWnd
, Window
= NULL
;
993 pti
= PsGetCurrentThreadWin32Thread();
994 ThreadQueue
= pti
->MessageQueue
;
996 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
)
999 if (hWnd
&& (Window
= UserGetWindowObject(hWnd
)))
1001 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
1007 hWndPrev
= MsqSetStateWindow(pti
, MSQ_STATE_CAPTURE
, hWnd
);
1011 pWnd
= UserGetWindowObject(hWndPrev
);
1013 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND
, pWnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
1017 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART
, Window
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
1019 if (hWndPrev
&& hWndPrev
!= hWnd
)
1021 if (ThreadQueue
->MenuOwner
&& Window
) ThreadQueue
->QF_flags
|= QF_CAPTURELOCKED
;
1023 //co_IntPostOrSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
1024 co_IntSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
1026 ThreadQueue
->QF_flags
&= ~QF_CAPTURELOCKED
;
1029 ThreadQueue
->spwndCapture
= Window
;
1031 if (hWnd
== NULL
) // Release mode.
1034 /// These are HACKS!
1035 /* Also remove other windows if not capturing anymore */
1036 MsqSetStateWindow(pti
, MSQ_STATE_MENUOWNER
, NULL
);
1037 MsqSetStateWindow(pti
, MSQ_STATE_MOVESIZE
, NULL
);
1039 /* Somebody may have missed some mouse movements */
1043 mi
.dwFlags
= MOUSEEVENTF_MOVE
;
1046 UserSendMouseInput(&mi
, FALSE
);
1056 IntReleaseCapture(VOID
)
1059 PUSER_MESSAGE_QUEUE ThreadQueue
;
1061 pti
= PsGetCurrentThreadWin32Thread();
1062 ThreadQueue
= pti
->MessageQueue
;
1064 // Can not release inside WM_CAPTURECHANGED!!
1065 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
) return FALSE
;
1067 co_UserSetCapture(NULL
);
1076 co_IntSetForegroundWindow(PWND Window
)
1078 if (Window
) ASSERT_REFS_CO(Window
);
1080 return co_IntSetForegroundAndFocusWindow(Window
, FALSE
);
1087 co_IntSetForegroundWindowMouse(PWND Window
)
1089 if (Window
) ASSERT_REFS_CO(Window
);
1091 return co_IntSetForegroundAndFocusWindow(Window
, TRUE
);
1098 IntLockSetForegroundWindow(UINT uLockCode
)
1100 ULONG Err
= ERROR_ACCESS_DENIED
;
1101 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
1105 if ( CanForceFG(ppi
) && !gppiLockSFW
)
1112 if ( gppiLockSFW
== ppi
)
1119 Err
= ERROR_INVALID_PARAMETER
;
1121 EngSetLastError(Err
);
1129 IntAllowSetForegroundWindow(DWORD dwProcessId
)
1131 PPROCESSINFO ppi
, ppiCur
;
1132 PEPROCESS Process
= NULL
;
1135 if (dwProcessId
!= ASFW_ANY
)
1137 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE
)dwProcessId
, &Process
)))
1139 EngSetLastError(ERROR_INVALID_PARAMETER
);
1142 ppi
= PsGetProcessWin32Process(Process
);
1145 ObDereferenceObject(Process
);
1149 ppiCur
= PsGetCurrentProcessWin32Process();
1150 if (!CanForceFG(ppiCur
))
1152 if (Process
) ObDereferenceObject(Process
);
1153 EngSetLastError(ERROR_ACCESS_DENIED
);
1156 if (dwProcessId
== ASFW_ANY
)
1157 { // All processes will be enabled to set the foreground window.
1158 //ERR("ptiLastInput is CLEARED!!\n");
1159 ptiLastInput
= NULL
;
1162 { // Rule #3, last input event in force.
1163 ERR("ptiLastInput is SET!!\n");
1164 //ptiLastInput = ppi->ptiList; // See CORE-6384 & CORE-7030.
1165 ObDereferenceObject(Process
);
1174 NtUserGetForegroundWindow(VOID
)
1176 DECLARE_RETURN(HWND
);
1178 TRACE("Enter NtUserGetForegroundWindow\n");
1179 UserEnterExclusive();
1181 RETURN( UserGetForegroundWindow());
1184 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_
);
1190 NtUserSetActiveWindow(HWND hWnd
)
1192 USER_REFERENCE_ENTRY Ref
;
1195 DECLARE_RETURN(HWND
);
1197 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd
);
1198 UserEnterExclusive();
1203 if (!(Window
= UserGetWindowObject(hWnd
)))
1205 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd
);
1211 Window
->head
.pti
->MessageQueue
== gptiCurrent
->MessageQueue
)
1213 hWndPrev
= gptiCurrent
->MessageQueue
->spwndActive
? UserHMGetHandle(gptiCurrent
->MessageQueue
->spwndActive
) : NULL
;
1214 if (Window
) UserRefObjectCo(Window
, &Ref
);
1215 UserSetActiveWindow(Window
);
1216 if (Window
) UserDerefObjectCo(Window
);
1217 RETURN( hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0 );
1222 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_
);
1231 NtUserSetCapture(HWND hWnd
)
1233 DECLARE_RETURN(HWND
);
1235 TRACE("Enter NtUserSetCapture(%p)\n", hWnd
);
1236 UserEnterExclusive();
1238 RETURN( co_UserSetCapture(hWnd
));
1241 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_
);
1250 NtUserSetFocus(HWND hWnd
)
1253 USER_REFERENCE_ENTRY Ref
;
1254 DECLARE_RETURN(HWND
);
1257 TRACE("Enter NtUserSetFocus(%p)\n", hWnd
);
1258 UserEnterExclusive();
1262 if (!(Window
= UserGetWindowObject(hWnd
)))
1264 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd
);
1268 UserRefObjectCo(Window
, &Ref
);
1269 ret
= co_UserSetFocus(Window
);
1270 UserDerefObjectCo(Window
);
1276 RETURN( co_UserSetFocus(0));
1280 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_
);