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
;
107 pti
= PsGetCurrentThreadWin32Thread();
109 UserRefObjectCo(Window
, &Ref
);
111 if (WindowPrev
) UserRefObjectCo(WindowPrev
, &RefPrev
);
113 /* Send palette messages */
114 if (gpsi
->PUSIFlags
& PUSIF_PALETTEDISPLAY
&&
115 //co_IntPostOrSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0))
116 co_IntSendMessage(UserHMGetHandle(Window
), WM_QUERYNEWPALETTE
, 0, 0))
118 UserSendNotifyMessage( HWND_BROADCAST
,
119 WM_PALETTEISCHANGING
,
120 (WPARAM
)UserHMGetHandle(Window
),
124 if (!(Window
->style
& WS_CHILD
))
126 PWND pwndTemp
= co_GetDesktopWindow(Window
)->spwndChild
;
128 while (pwndTemp
&& !(pwndTemp
->style
& WS_VISIBLE
)) pwndTemp
= pwndTemp
->spwndNext
;
130 if (Window
!= pwndTemp
|| (WindowPrev
&& !IntIsWindowVisible(WindowPrev
)))
132 if (!Async
|| pti
->MessageQueue
== gpqForeground
)
134 UINT flags
= SWP_NOSIZE
| SWP_NOMOVE
;
135 if (Window
== pwndTemp
) flags
|= SWP_NOACTIVATE
;
136 //ERR("co_IntSendActivateMessages SetWindowPos! Async %d pti Q == FGQ %d\n",Async,pti->MessageQueue == gpqForeground);
137 co_WinPosSetWindowPos(Window
, HWND_TOP
, 0, 0, 0, 0, flags
);
142 //// CORE-1161 and CORE-6651
143 if (Window
->spwndPrev
)
145 HWND
*phwndTopLevel
, *phwndCurrent
;
146 PWND pwndCurrent
, pwndDesktop
;
148 pwndDesktop
= UserGetDesktopWindow();
149 if (Window
->spwndParent
== pwndDesktop
)
151 phwndTopLevel
= IntWinListChildren(pwndDesktop
);
152 phwndCurrent
= phwndTopLevel
;
155 pwndCurrent
= UserGetWindowObject(*phwndCurrent
);
157 if (pwndCurrent
&& pwndCurrent
->spwndOwner
== Window
)
159 co_WinPosSetWindowPos(pwndCurrent
, HWND_TOP
, 0, 0, 0, 0, SWP_NOSIZE
|SWP_NOMOVE
|SWP_NOACTIVATE
);
163 ExFreePool(phwndTopLevel
);
167 OldTID
= WindowPrev
? IntGetWndThreadId(WindowPrev
) : NULL
;
168 NewTID
= IntGetWndThreadId(Window
);
169 ptiOld
= WindowPrev
? WindowPrev
->head
.pti
: NULL
;
170 ptiNew
= Window
->head
.pti
;
172 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID);
174 if (!(pti
->TIF_flags
& TIF_INACTIVATEAPPMSG
) &&
175 (!WindowPrev
|| OldTID
!= NewTID
) )
180 List
= IntWinListChildren(UserGetDesktopWindow());
185 ptiOld
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
186 // Note: Do not set pci flags, this does crash!
187 for (phWnd
= List
; *phWnd
; ++phWnd
)
189 cWindow
= ValidateHwndNoErr(*phWnd
);
190 if (cWindow
&& cWindow
->head
.pti
== ptiOld
)
191 { // FALSE if the window is being deactivated,
192 // ThreadId that owns the window being activated.
193 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
196 ptiOld
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
199 { //// Prevents a resource crash due to reentrance!
201 pti
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
203 for (phWnd
= List
; *phWnd
; ++phWnd
)
205 cWindow
= ValidateHwndNoErr(*phWnd
);
206 if (cWindow
&& cWindow
->head
.pti
== ptiNew
)
207 { // TRUE if the window is being activated,
208 // ThreadId that owns the window being deactivated.
209 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
213 ExFreePoolWithTag(List
, USERTAG_WINDOWLIST
);
217 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
219 if (Window
->state
& WNDS_ACTIVEFRAME
)
220 { // If already active frame do not allow NCPaint.
221 //ERR("SendActivateMessage Is Active Frame!\n");
222 Window
->state
|= WNDS_NONCPAINT
;
225 if (Window
->style
& WS_MINIMIZE
)
227 TRACE("Widow was minimized\n");
230 co_IntMakeWindowActive(Window
);
232 /* FIXME: IntIsWindow */
234 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
236 (WPARAM
)(Window
== (gpqForeground
? gpqForeground
->spwndActive
: NULL
)),
237 0); //(LPARAM)hWndPrev);
239 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
241 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
, Window
->style
& WS_MINIMIZE
),
242 (LPARAM
)(WindowPrev
? UserHMGetHandle(WindowPrev
) : 0));
244 if (!Window
->spwndOwner
&& !IntGetParent(Window
))
246 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
247 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (WPARAM
) UserHMGetHandle(Window
), FALSE
);
250 Window
->state
&= ~WNDS_NONCPAINT
;
252 UserDerefObjectCo(Window
);
258 IntSendFocusMessages( PTHREADINFO pti
, PWND pWnd
)
261 PUSER_MESSAGE_QUEUE ThreadQueue
= pti
->MessageQueue
; // Queue can change...
263 ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
264 if (!pWnd
&& ThreadQueue
->spwndActive
)
266 ThreadQueue
->QF_flags
|= QF_FOCUSNULLSINCEACTIVE
;
269 pWndPrev
= ThreadQueue
->spwndFocus
;
271 /* check if the specified window can be set in the input data of a given queue */
272 if (!pWnd
|| ThreadQueue
== pWnd
->head
.pti
->MessageQueue
)
273 /* set the current thread focus window */
274 ThreadQueue
->spwndFocus
= pWnd
;
280 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
281 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, (WPARAM
)UserHMGetHandle(pWnd
), 0);
283 if (ThreadQueue
->spwndFocus
== pWnd
)
285 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, pWnd
, OBJID_CLIENT
, CHILDID_SELF
, 0);
286 //co_IntPostOrSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)(pWndPrev ? UserHMGetHandle(pWndPrev) : NULL), 0);
287 co_IntSendMessage(UserHMGetHandle(pWnd
), WM_SETFOCUS
, (WPARAM
)(pWndPrev
? UserHMGetHandle(pWndPrev
) : NULL
), 0);
294 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, NULL
, OBJID_CLIENT
, CHILDID_SELF
, 0);
295 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
296 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, 0, 0);
302 IntFindChildWindowToOwner(PWND Root
, PWND Owner
)
305 PWND Child
, OwnerWnd
;
307 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
309 OwnerWnd
= Child
->spwndOwner
;
313 if(OwnerWnd
== Owner
)
324 FindRemoveAsyncMsg(PWND Wnd
, WPARAM wParam
)
327 PUSER_SENT_MESSAGE Message
;
334 if (!IsListEmpty(&pti
->SentMessagesListHead
))
336 // Scan sent queue messages to see if we received async messages.
337 Entry
= pti
->SentMessagesListHead
.Flink
;
338 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
341 if (IsListEmpty(Entry
)) return;
342 if (!Message
) return;
343 Entry
= Message
->ListEntry
.Flink
;
345 if (Message
->Msg
.message
== WM_ASYNC_SETACTIVEWINDOW
&&
346 Message
->Msg
.hwnd
== UserHMGetHandle(Wnd
) &&
347 Message
->Msg
.wParam
== wParam
)
349 ERR("ASYNC SAW: Found one in the Sent Msg Queue! %p Activate/Deactivate %d\n", Message
->Msg
.hwnd
,!!wParam
);
350 RemoveEntryList(&Message
->ListEntry
); // Purge the entry.
351 ExFreePoolWithTag(Message
, TAG_USRMSG
);
353 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
355 while (Entry
!= &pti
->SentMessagesListHead
);
360 ToggleFGActivate(PTHREADINFO pti
)
363 PPROCESSINFO ppi
= pti
->ppi
;
365 Ret
= !!(pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
);
368 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
371 Ret
= !!(ppi
->W32PF_flags
& W32PF_ALLOWFOREGROUNDACTIVATE
);
374 ppi
->W32PF_flags
&= ~W32PF_ALLOWFOREGROUNDACTIVATE
;
375 //ERR("ToggleFGActivate is %d\n",Ret);
380 IsAllowedFGActive(PTHREADINFO pti
, PWND Wnd
)
382 // Not allowed if one or more,,
383 if (!ToggleFGActivate(pti
) || // bits not set,
384 pti
->rpdesk
!= gpdeskInputDesktop
|| // not current Desktop,
385 pti
->MessageQueue
== gpqForeground
|| // if already the queue foreground,
386 IsFGLocked() || // foreground is locked,
387 Wnd
->ExStyle
& WS_EX_NOACTIVATE
) // or,,, does not become the foreground window when the user clicks it.
391 //ERR("IsAllowedFGActive is TRUE\n");
396 Can the system force foreground from one or more conditions.
399 CanForceFG(PPROCESSINFO ppi
)
402 ptiLastInput
->ppi
== ppi
||
404 gptiForeground
->ppi
== ppi
||
405 ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_SETFOREGROUNDALLOWED
) ||
406 gppiInputProvider
== ppi
||
409 //ERR("CanForceFG is FALSE\n");
415 The system restricts which processes can set the foreground window. A process
416 can set the foreground window only if one of the following conditions is true:
418 * The process is the foreground process.
419 * The process was started by the foreground process.
420 * The process received the last input event.
421 * There is no foreground process.
422 * The foreground process is being debugged.
423 * The foreground is not locked (see LockSetForegroundWindow).
424 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
425 * No menus are active.
429 co_IntSetForegroundAndFocusWindow(
431 _In_ BOOL MouseActivate
)
433 HWND hWnd
= Wnd
? UserHMGetHandle(Wnd
) : NULL
;
434 HWND hWndPrev
= NULL
;
435 PWND pWndPrev
= NULL
;
436 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
438 BOOL fgRet
= FALSE
, Ret
= FALSE
;
440 if (Wnd
) ASSERT_REFS_CO(Wnd
);
442 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
444 PrevForegroundQueue
= IntGetFocusMessageQueue(); // Use this active desktop.
445 pti
= PsGetCurrentThreadWin32Thread();
447 if (PrevForegroundQueue
)
448 { // Same Window Q as foreground just do active.
449 if (Wnd
&& Wnd
->head
.pti
->MessageQueue
== PrevForegroundQueue
)
451 //ERR("Same Window Q as foreground just do active.\n");
452 if (pti
->MessageQueue
== PrevForegroundQueue
)
453 { // Same WQ and TQ go active.
454 //ERR("Same WQ and TQ go active.\n");
455 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
457 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
458 { // Same WQ and it is active.
459 //ERR("Same WQ and it is active.\n");
463 { // Same WQ as FG but not the same TQ send active.
464 //ERR("Same WQ as FG but not the same TQ send active.\n");
465 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
471 hWndPrev
= PrevForegroundQueue
->spwndActive
? UserHMGetHandle(PrevForegroundQueue
->spwndActive
) : 0;
472 pWndPrev
= PrevForegroundQueue
->spwndActive
;
475 if ( (( !IsFGLocked() || pti
->ppi
== gppiInputProvider
) &&
476 ( CanForceFG(pti
->ppi
) || pti
->TIF_flags
& (TIF_SYSTEMTHREAD
|TIF_CSRSSTHREAD
|TIF_ALLOWFOREGROUNDACTIVATE
) )) ||
477 pti
->ppi
== ppiScrnSaver
481 //ToggleFGActivate(pti); // win.c line 2662 fail
484 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
485 gptiForeground
= Wnd
->head
.pti
;
486 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
490 IntSetFocusMessageQueue(NULL
);
491 gptiForeground
= NULL
;
492 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
496 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
497 other thread after we already changed the foreground window back to our own
501 FindRemoveAsyncMsg(Wnd
, 0); // Do this to fix test_SFW todos!
506 // Fix FG Bounce with regedit.
507 if (hWndPrev
!= hWnd
)
509 if (PrevForegroundQueue
&&
511 PrevForegroundQueue
->spwndActive
)
513 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
514 if (pti
->MessageQueue
== PrevForegroundQueue
)
516 //ERR("SFGW: TI same as Prev TI\n");
517 co_IntSetActiveWindow(NULL
, FALSE
, TRUE
, FALSE
);
521 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
522 // No real reason to wait here.
523 co_IntSendMessageNoWait(hWndPrev
, WM_ASYNC_SETACTIVEWINDOW
, 0, 0 );
528 if (!Wnd
) return FALSE
; // Always return false.
530 if (pti
->MessageQueue
== Wnd
->head
.pti
->MessageQueue
)
532 //ERR("Same PQ and WQ go active.\n");
533 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
535 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
537 //ERR("Same Active and Wnd.\n");
542 //ERR("Activate Not same PQ and WQ and Wnd.\n");
543 co_IntSendMessageNoWait(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
550 co_IntMouseActivateWindow(PWND Wnd
)
554 USER_REFERENCE_ENTRY Ref
;
558 if (Wnd
->style
& WS_DISABLED
)
562 PWND DesktopWindow
= UserGetDesktopWindow();
565 Top
= IntFindChildWindowToOwner(DesktopWindow
, Wnd
);
566 if ((TopWnd
= ValidateHwndNoErr(Top
)))
568 UserRefObjectCo(TopWnd
, &Ref
);
569 Ret
= co_IntMouseActivateWindow(TopWnd
);
570 UserDerefObjectCo(TopWnd
);
578 TopWindow
= UserGetAncestor(Wnd
, GA_ROOT
);
579 //if (TopWindow) {ERR("MAW 2 pWnd %p hWnd %p\n",TopWindow,TopWindow->head.h);}
580 if (!TopWindow
) return FALSE
;
582 /* TMN: Check return value from this function? */
583 UserRefObjectCo(TopWindow
, &Ref
);
584 co_IntSetForegroundAndFocusWindow(TopWindow
, TRUE
);
585 UserDerefObjectCo(TopWindow
);
590 co_IntSetActiveWindow(PWND Wnd OPTIONAL
, BOOL bMouse
, BOOL bFocus
, BOOL Async
)
593 PUSER_MESSAGE_QUEUE ThreadQueue
;
594 PWND pWndChg
, WndPrev
; // State changes.
598 CBTACTIVATESTRUCT cbt
;
599 //ERR("co_IntSetActiveWindow 1\n");
603 hWnd
= UserHMGetHandle(Wnd
);
604 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
605 if (Wnd
== UserGetDesktopWindow()) return FALSE
;
606 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
609 //ERR("co_IntSetActiveWindow 2\n");
610 pti
= PsGetCurrentThreadWin32Thread();
611 ThreadQueue
= pti
->MessageQueue
;
612 ASSERT(ThreadQueue
!= 0);
614 hWndPrev
= ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : NULL
;
616 pWndChg
= ThreadQueue
->spwndActive
; // Keep to notify of a preemptive switch.
620 BOOL Ret
, DoFG
, AllowFG
;
622 if (Wnd
->state
& WNDS_BEINGACTIVATED
) return TRUE
;
624 if (ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
626 if (IsAllowedFGActive(pti
, Wnd
))
632 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
635 AllowFG
= !pti
->cVisWindows
; // Nothing is visable.
636 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
638 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
640 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
641 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
642 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
643 if (!gpqForeground
|| gpqForeground
== ThreadQueue
)
651 if (pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
|| pti
->cVisWindows
)
658 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
663 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
664 //ERR("co_IntSetActiveWindow 3c FG set\n");
665 Ret
= co_IntSetForegroundAndFocusWindow(Wnd
, bMouse
);
668 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
672 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
678 /* Call CBT hook chain */
680 cbt
.hWndActive
= hWndPrev
;
681 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
683 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
687 if ( ThreadQueue
->spwndActive
&& ThreadQueue
->spwndActive
->state
& WNDS_DESTROYED
)
688 ThreadQueue
->spwndActive
= NULL
;
690 ThreadQueue
->spwndActivePrev
= ThreadQueue
->spwndActive
;
692 WndPrev
= ThreadQueue
->spwndActive
; // Keep to save changing active.
696 if (ThreadQueue
== gpqForeground
) gpqForegroundPrev
= ThreadQueue
;
697 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev
), hWnd
)) return FALSE
;
700 WndPrev
= ThreadQueue
->spwndActive
; // Again keep to save changing active.
702 // While in calling message proc or hook:
703 // Fail if a preemptive switch was made, current active not made previous,
704 // focus window is dead or no longer the same thread queue.
705 if ( ThreadQueue
->spwndActivePrev
!= ThreadQueue
->spwndActive
||
706 pWndChg
!= WndPrev
||
707 (Wnd
&& !VerifyWnd(Wnd
)) ||
708 ThreadQueue
!= pti
->MessageQueue
)
710 ERR("SetActiveWindow: Summery ERROR, active state changed!\n");
714 if (!WndPrev
) ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
716 if (Wnd
) Wnd
->state
|= WNDS_BEINGACTIVATED
;
718 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND
, Wnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
719 //// Breaks Atl-Esc/Tab via User32.
720 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
722 /* check if the specified window can be set in the input data of a given queue */
723 if ( !Wnd
|| ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
725 /* set the current thread active window */
726 ThreadQueue
->spwndActive
= Wnd
;
729 WndPrev
= VerifyWnd(ThreadQueue
->spwndActivePrev
); // Now should be set but verify it again.
731 InAAPM
= co_IntSendActivateMessages(WndPrev
, Wnd
, bMouse
, Async
);
733 /* now change focus if necessary */
734 if (bFocus
&& !(ThreadQueue
->QF_flags
& QF_FOCUSNULLSINCEACTIVE
))
736 /* Do not change focus if the window is no longer active */
737 if (pti
->MessageQueue
->spwndActive
!= IntGetNonChildAncestor(pti
->MessageQueue
->spwndFocus
))
739 PWND pWndSend
= pti
->MessageQueue
->spwndActive
;
740 // Clear focus if the active window is minimized.
741 if (pWndSend
&& pti
->MessageQueue
->spwndActive
->style
& WS_MINIMIZE
) pWndSend
= NULL
;
742 // Send focus messages and if so, set the focus.
743 IntSendFocusMessages( pti
, pWndSend
);
749 pti
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
752 // FIXME: Used in the menu loop!!!
753 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
755 //ERR("co_IntSetActiveWindow Exit\n");
756 if (Wnd
) Wnd
->state
&= ~WNDS_BEINGACTIVATED
;
757 return (ThreadQueue
->spwndActive
== Wnd
);
761 UserSetActiveWindow(PWND Wnd
)
763 if (Wnd
) // Must have a window!
765 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
767 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
770 Yes your eye are not deceiving you~!
772 First part of wines Win.c test_SetActiveWindow:
774 flush_events( TRUE );
775 ShowWindow(hwnd, SW_HIDE);
778 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
785 co_UserSetFocus(PWND Window
)
790 PUSER_MESSAGE_QUEUE ThreadQueue
;
793 ASSERT_REFS_CO(Window
);
795 pti
= PsGetCurrentThreadWin32Thread();
796 ThreadQueue
= pti
->MessageQueue
;
797 ASSERT(ThreadQueue
!= 0);
799 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window
? UserHMGetHandle(Window
) : 0, pti
);
801 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
805 if (hWndPrev
== UserHMGetHandle(Window
))
807 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0; /* Nothing to do */
810 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
812 ERR("SetFocus Must have the same Q!\n");
816 /* Check if we can set the focus to this window */
817 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720...
818 for (pwndTop
= Window
; pwndTop
; pwndTop
= pwndTop
->spwndParent
)
820 if (pwndTop
->style
& (WS_MINIMIZED
|WS_DISABLED
)) return 0;
821 if ((pwndTop
->style
& (WS_POPUP
|WS_CHILD
)) != WS_CHILD
) break;
824 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->head
.h
, (LPARAM
)hWndPrev
))
826 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
830 /* Activate pwndTop if needed. */
831 if (pwndTop
!= ThreadQueue
->spwndActive
)
833 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue(); // Keep it based on desktop.
834 if (ThreadQueue
!= ForegroundQueue
&& IsAllowedFGActive(pti
, pwndTop
)) // Rule 2 & 3.
836 //ERR("SetFocus: Set Foreground!\n");
837 if (!(pwndTop
->style
& WS_VISIBLE
))
839 pti
->ppi
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
;
841 if (!co_IntSetForegroundAndFocusWindow(pwndTop
, FALSE
))
843 ERR("SetFocus: Set Foreground and Focus Failed!\n");
848 /* Set Active when it is needed. */
849 if (pwndTop
!= ThreadQueue
->spwndActive
)
851 //ERR("SetFocus: Set Active!\n");
852 if (!co_IntSetActiveWindow(pwndTop
, FALSE
, FALSE
, FALSE
))
854 ERR("SetFocus: Set Active Failed!\n");
859 /* Abort if window destroyed */
860 if (Window
->state2
& WNDS2_INDESTROY
) return 0;
861 /* Do not change focus if the window is no longer active */
862 if (pwndTop
!= ThreadQueue
->spwndActive
)
864 ERR("SetFocus: Top window did not go active!\n");
869 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
870 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
872 IntSendFocusMessages( pti
, Window
);
874 TRACE("Focus: %p -> %p\n", hWndPrev
, Window
->head
.h
);
876 else /* NULL hwnd passed in */
878 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
880 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
884 /* set the current thread focus window null */
885 IntSendFocusMessages( pti
, NULL
);
887 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0;
891 UserGetForegroundWindow(VOID
)
893 PUSER_MESSAGE_QUEUE ForegroundQueue
;
895 ForegroundQueue
= IntGetFocusMessageQueue();
896 return( ForegroundQueue
? (ForegroundQueue
->spwndActive
? UserHMGetHandle(ForegroundQueue
->spwndActive
) : 0) : 0);
899 HWND FASTCALL
UserGetActiveWindow(VOID
)
902 PUSER_MESSAGE_QUEUE ThreadQueue
;
904 pti
= PsGetCurrentThreadWin32Thread();
905 ThreadQueue
= pti
->MessageQueue
;
906 return( ThreadQueue
? (ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : 0) : 0);
913 PUSER_MESSAGE_QUEUE ThreadQueue
;
914 DECLARE_RETURN(HWND
);
916 TRACE("Enter IntGetCapture\n");
918 pti
= PsGetCurrentThreadWin32Thread();
919 ThreadQueue
= pti
->MessageQueue
;
920 RETURN( ThreadQueue
? (ThreadQueue
->spwndCapture
? UserHMGetHandle(ThreadQueue
->spwndCapture
) : 0) : 0);
923 TRACE("Leave IntGetCapture, ret=%p\n", _ret_
);
928 co_UserSetCapture(HWND hWnd
)
931 PUSER_MESSAGE_QUEUE ThreadQueue
;
932 PWND pWnd
, Window
= NULL
;
935 pti
= PsGetCurrentThreadWin32Thread();
936 ThreadQueue
= pti
->MessageQueue
;
938 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
)
941 if (hWnd
&& (Window
= UserGetWindowObject(hWnd
)))
943 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
949 hWndPrev
= MsqSetStateWindow(pti
, MSQ_STATE_CAPTURE
, hWnd
);
953 pWnd
= UserGetWindowObject(hWndPrev
);
955 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND
, pWnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
959 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART
, Window
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
961 if (hWndPrev
&& hWndPrev
!= hWnd
)
963 if (ThreadQueue
->MenuOwner
&& Window
) ThreadQueue
->QF_flags
|= QF_CAPTURELOCKED
;
965 //co_IntPostOrSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
966 co_IntSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
968 ThreadQueue
->QF_flags
&= ~QF_CAPTURELOCKED
;
971 ThreadQueue
->spwndCapture
= Window
;
973 if (hWnd
== NULL
) // Release mode.
977 /* Also remove other windows if not capturing anymore */
978 MsqSetStateWindow(pti
, MSQ_STATE_MENUOWNER
, NULL
);
979 MsqSetStateWindow(pti
, MSQ_STATE_MOVESIZE
, NULL
);
981 /* Somebody may have missed some mouse movements */
985 mi
.dwFlags
= MOUSEEVENTF_MOVE
;
988 UserSendMouseInput(&mi
, FALSE
);
998 IntReleaseCapture(VOID
)
1001 PUSER_MESSAGE_QUEUE ThreadQueue
;
1003 pti
= PsGetCurrentThreadWin32Thread();
1004 ThreadQueue
= pti
->MessageQueue
;
1006 // Can not release inside WM_CAPTURECHANGED!!
1007 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
) return FALSE
;
1009 co_UserSetCapture(NULL
);
1018 co_IntSetForegroundWindow(PWND Window
)
1020 if (Window
) ASSERT_REFS_CO(Window
);
1022 return co_IntSetForegroundAndFocusWindow(Window
, FALSE
);
1029 co_IntSetForegroundWindowMouse(PWND Window
)
1031 if (Window
) ASSERT_REFS_CO(Window
);
1033 return co_IntSetForegroundAndFocusWindow(Window
, TRUE
);
1040 IntLockSetForegroundWindow(UINT uLockCode
)
1042 ULONG Err
= ERROR_ACCESS_DENIED
;
1043 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
1047 if ( CanForceFG(ppi
) && !gppiLockSFW
)
1054 if ( gppiLockSFW
== ppi
)
1061 Err
= ERROR_INVALID_PARAMETER
;
1063 EngSetLastError(Err
);
1071 IntAllowSetForegroundWindow(DWORD dwProcessId
)
1073 PPROCESSINFO ppi
, ppiCur
;
1074 PEPROCESS Process
= NULL
;
1077 if (dwProcessId
!= ASFW_ANY
)
1079 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE
)dwProcessId
, &Process
)))
1081 EngSetLastError(ERROR_INVALID_PARAMETER
);
1084 ppi
= PsGetProcessWin32Process(Process
);
1087 ObDereferenceObject(Process
);
1091 ppiCur
= PsGetCurrentProcessWin32Process();
1092 if (!CanForceFG(ppiCur
))
1094 if (Process
) ObDereferenceObject(Process
);
1095 EngSetLastError(ERROR_ACCESS_DENIED
);
1098 if (dwProcessId
== ASFW_ANY
)
1099 { // All processes will be enabled to set the foreground window.
1100 //ERR("ptiLastInput is CLEARED!!\n");
1101 ptiLastInput
= NULL
;
1104 { // Rule #3, last input event in force.
1105 ERR("ptiLastInput is SET!!\n");
1106 //ptiLastInput = ppi->ptiList; // See CORE-6384 & CORE-7030.
1107 ObDereferenceObject(Process
);
1116 NtUserGetForegroundWindow(VOID
)
1118 DECLARE_RETURN(HWND
);
1120 TRACE("Enter NtUserGetForegroundWindow\n");
1121 UserEnterExclusive();
1123 RETURN( UserGetForegroundWindow());
1126 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_
);
1132 NtUserSetActiveWindow(HWND hWnd
)
1134 USER_REFERENCE_ENTRY Ref
;
1137 DECLARE_RETURN(HWND
);
1139 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd
);
1140 UserEnterExclusive();
1145 if (!(Window
= UserGetWindowObject(hWnd
)))
1147 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd
);
1153 Window
->head
.pti
->MessageQueue
== gptiCurrent
->MessageQueue
)
1155 hWndPrev
= gptiCurrent
->MessageQueue
->spwndActive
? UserHMGetHandle(gptiCurrent
->MessageQueue
->spwndActive
) : NULL
;
1156 if (Window
) UserRefObjectCo(Window
, &Ref
);
1157 UserSetActiveWindow(Window
);
1158 if (Window
) UserDerefObjectCo(Window
);
1159 RETURN( hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0 );
1164 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_
);
1173 NtUserSetCapture(HWND hWnd
)
1175 DECLARE_RETURN(HWND
);
1177 TRACE("Enter NtUserSetCapture(%p)\n", hWnd
);
1178 UserEnterExclusive();
1180 RETURN( co_UserSetCapture(hWnd
));
1183 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_
);
1192 NtUserSetFocus(HWND hWnd
)
1195 USER_REFERENCE_ENTRY Ref
;
1196 DECLARE_RETURN(HWND
);
1199 TRACE("Enter NtUserSetFocus(%p)\n", hWnd
);
1200 UserEnterExclusive();
1204 if (!(Window
= UserGetWindowObject(hWnd
)))
1206 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd
);
1210 UserRefObjectCo(Window
, &Ref
);
1211 ret
= co_UserSetFocus(Window
);
1212 UserDerefObjectCo(Window
);
1218 RETURN( co_UserSetFocus(0));
1222 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_
);