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))
117 UserSendNotifyMessage( HWND_BROADCAST
,
118 WM_PALETTEISCHANGING
,
119 (WPARAM
)UserHMGetHandle(Window
),
123 if (!(Window
->style
& WS_CHILD
))
125 PWND pwndTemp
= co_GetDesktopWindow(Window
)->spwndChild
;
127 while (pwndTemp
&& !(pwndTemp
->style
& WS_VISIBLE
)) pwndTemp
= pwndTemp
->spwndNext
;
129 if (Window
!= pwndTemp
|| (WindowPrev
&& !IntIsWindowVisible(WindowPrev
)))
131 if (!Async
|| pti
->MessageQueue
== gpqForeground
)
133 UINT flags
= SWP_NOSIZE
| SWP_NOMOVE
;
134 if (Window
== pwndTemp
) flags
|= SWP_NOACTIVATE
;
135 //ERR("co_IntSendActivateMessages SetWindowPos! Async %d pti Q == FGQ %d\n",Async,pti->MessageQueue == gpqForeground);
136 co_WinPosSetWindowPos(Window
, HWND_TOP
, 0, 0, 0, 0, flags
);
141 //// CORE-1161 and CORE-6651
142 if (Window
->spwndPrev
)
144 HWND
*phwndTopLevel
, *phwndCurrent
;
145 PWND pwndCurrent
, pwndDesktop
;
147 pwndDesktop
= UserGetDesktopWindow();
148 if (Window
->spwndParent
== pwndDesktop
)
150 phwndTopLevel
= IntWinListChildren(pwndDesktop
);
151 phwndCurrent
= phwndTopLevel
;
154 pwndCurrent
= UserGetWindowObject(*phwndCurrent
);
156 if (pwndCurrent
&& pwndCurrent
->spwndOwner
== Window
)
158 co_WinPosSetWindowPos(pwndCurrent
, HWND_TOP
, 0, 0, 0, 0, SWP_NOSIZE
|SWP_NOMOVE
|SWP_NOACTIVATE
);
162 ExFreePool(phwndTopLevel
);
166 OldTID
= WindowPrev
? IntGetWndThreadId(WindowPrev
) : NULL
;
167 NewTID
= IntGetWndThreadId(Window
);
168 ptiOld
= WindowPrev
? WindowPrev
->head
.pti
: NULL
;
169 ptiNew
= Window
->head
.pti
;
171 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID);
173 if (!(pti
->TIF_flags
& TIF_INACTIVATEAPPMSG
) &&
174 (!WindowPrev
|| OldTID
!= NewTID
) )
179 List
= IntWinListChildren(UserGetDesktopWindow());
184 ptiOld
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
185 // Note: Do not set pci flags, this does crash!
186 for (phWnd
= List
; *phWnd
; ++phWnd
)
188 cWindow
= ValidateHwndNoErr(*phWnd
);
189 if (cWindow
&& cWindow
->head
.pti
== ptiOld
)
190 { // FALSE if the window is being deactivated,
191 // ThreadId that owns the window being activated.
192 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
195 ptiOld
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
198 { //// Prevents a resource crash due to reentrance!
200 pti
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
202 for (phWnd
= List
; *phWnd
; ++phWnd
)
204 cWindow
= ValidateHwndNoErr(*phWnd
);
205 if (cWindow
&& cWindow
->head
.pti
== ptiNew
)
206 { // TRUE if the window is being activated,
207 // ThreadId that owns the window being deactivated.
208 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
212 ExFreePoolWithTag(List
, USERTAG_WINDOWLIST
);
216 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
218 if (Window
->state
& WNDS_ACTIVEFRAME
)
219 { // If already active frame do not allow NCPaint.
220 //ERR("SendActivateMessage Is Active Frame!\n");
221 Window
->state
|= WNDS_NONCPAINT
;
224 if (Window
->style
& WS_MINIMIZE
)
226 TRACE("Widow was minimized\n");
229 co_IntMakeWindowActive(Window
);
231 /* FIXME: IntIsWindow */
233 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
235 (WPARAM
)(Window
== (gpqForeground
? gpqForeground
->spwndActive
: NULL
)),
236 0); //(LPARAM)hWndPrev);
238 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
240 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
, Window
->style
& WS_MINIMIZE
),
241 (LPARAM
)(WindowPrev
? UserHMGetHandle(WindowPrev
) : 0));
243 if (!Window
->spwndOwner
&& !IntGetParent(Window
))
245 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
246 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (WPARAM
) UserHMGetHandle(Window
), FALSE
);
249 Window
->state
&= ~WNDS_NONCPAINT
;
251 UserDerefObjectCo(Window
);
257 IntSendFocusMessages( PTHREADINFO pti
, PWND pWnd
)
260 PUSER_MESSAGE_QUEUE ThreadQueue
= pti
->MessageQueue
; // Queue can change...
262 ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
263 if (!pWnd
&& ThreadQueue
->spwndActive
)
265 ThreadQueue
->QF_flags
|= QF_FOCUSNULLSINCEACTIVE
;
268 pWndPrev
= ThreadQueue
->spwndFocus
;
270 /* check if the specified window can be set in the input data of a given queue */
271 if (!pWnd
|| ThreadQueue
== pWnd
->head
.pti
->MessageQueue
)
272 /* set the current thread focus window */
273 ThreadQueue
->spwndFocus
= pWnd
;
279 co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, (WPARAM
)UserHMGetHandle(pWnd
), 0);
281 if (ThreadQueue
->spwndFocus
== pWnd
)
283 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, pWnd
, OBJID_CLIENT
, CHILDID_SELF
, 0);
284 co_IntPostOrSendMessage(UserHMGetHandle(pWnd
), WM_SETFOCUS
, (WPARAM
)(pWndPrev
? UserHMGetHandle(pWndPrev
) : NULL
), 0);
291 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, NULL
, OBJID_CLIENT
, CHILDID_SELF
, 0);
292 co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, 0, 0);
298 IntFindChildWindowToOwner(PWND Root
, PWND Owner
)
301 PWND Child
, OwnerWnd
;
303 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
305 OwnerWnd
= Child
->spwndOwner
;
309 if(OwnerWnd
== Owner
)
320 FindRemoveAsyncMsg(PWND Wnd
, WPARAM wParam
)
323 PUSER_SENT_MESSAGE Message
;
330 if (!IsListEmpty(&pti
->SentMessagesListHead
))
332 // Scan sent queue messages to see if we received async messages.
333 Entry
= pti
->SentMessagesListHead
.Flink
;
334 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
337 if (IsListEmpty(Entry
)) return;
338 if (!Message
) return;
339 Entry
= Message
->ListEntry
.Flink
;
341 if (Message
->Msg
.message
== WM_ASYNC_SETACTIVEWINDOW
&&
342 Message
->Msg
.hwnd
== UserHMGetHandle(Wnd
) &&
343 Message
->Msg
.wParam
== wParam
)
345 ERR("ASYNC SAW: Found one in the Sent Msg Queue! %p Activate/Deactivate %d\n", Message
->Msg
.hwnd
,!!wParam
);
346 RemoveEntryList(&Message
->ListEntry
); // Purge the entry.
347 ExFreePoolWithTag(Message
, TAG_USRMSG
);
349 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
351 while (Entry
!= &pti
->SentMessagesListHead
);
356 ToggleFGActivate(PTHREADINFO pti
)
359 PPROCESSINFO ppi
= pti
->ppi
;
361 Ret
= !!(pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
);
364 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
367 Ret
= !!(ppi
->W32PF_flags
& W32PF_ALLOWFOREGROUNDACTIVATE
);
370 ppi
->W32PF_flags
&= ~W32PF_ALLOWFOREGROUNDACTIVATE
;
371 //ERR("ToggleFGActivate is %d\n",Ret);
376 IsAllowedFGActive(PTHREADINFO pti
, PWND Wnd
)
378 // Not allowed if one or more,,
379 if (!ToggleFGActivate(pti
) || // bits not set,
380 pti
->rpdesk
!= gpdeskInputDesktop
|| // not current Desktop,
381 pti
->MessageQueue
== gpqForeground
|| // if already the queue foreground,
382 IsFGLocked() || // foreground is locked,
383 Wnd
->ExStyle
& WS_EX_NOACTIVATE
) // or,,, does not become the foreground window when the user clicks it.
387 //ERR("IsAllowedFGActive is TRUE\n");
392 Can the system force foreground from one or more conditions.
395 CanForceFG(PPROCESSINFO ppi
)
398 ptiLastInput
->ppi
== ppi
||
400 gptiForeground
->ppi
== ppi
||
401 ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_SETFOREGROUNDALLOWED
) ||
402 gppiInputProvider
== ppi
||
405 //ERR("CanForceFG is FALSE\n");
411 The system restricts which processes can set the foreground window. A process
412 can set the foreground window only if one of the following conditions is true:
414 * The process is the foreground process.
415 * The process was started by the foreground process.
416 * The process received the last input event.
417 * There is no foreground process.
418 * The foreground process is being debugged.
419 * The foreground is not locked (see LockSetForegroundWindow).
420 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
421 * No menus are active.
425 co_IntSetForegroundAndFocusWindow(
427 _In_ BOOL MouseActivate
)
429 HWND hWnd
= Wnd
? UserHMGetHandle(Wnd
) : NULL
;
430 HWND hWndPrev
= NULL
;
431 PWND pWndPrev
= NULL
;
432 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
434 BOOL fgRet
= FALSE
, Ret
= FALSE
;
436 if (Wnd
) ASSERT_REFS_CO(Wnd
);
438 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
440 PrevForegroundQueue
= IntGetFocusMessageQueue(); // Use this active desktop.
441 pti
= PsGetCurrentThreadWin32Thread();
443 if (PrevForegroundQueue
)
444 { // Same Window Q as foreground just do active.
445 if (Wnd
&& Wnd
->head
.pti
->MessageQueue
== PrevForegroundQueue
)
447 //ERR("Same Window Q as foreground just do active.\n");
448 if (pti
->MessageQueue
== PrevForegroundQueue
)
449 { // Same WQ and TQ go active.
450 //ERR("Same WQ and TQ go active.\n");
451 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
453 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
454 { // Same WQ and it is active.
455 //ERR("Same WQ and it is active.\n");
459 { // Same WQ as FG but not the same TQ send active.
460 //ERR("Same WQ as FG but not the same TQ send active.\n");
461 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
467 hWndPrev
= PrevForegroundQueue
->spwndActive
? UserHMGetHandle(PrevForegroundQueue
->spwndActive
) : 0;
468 pWndPrev
= PrevForegroundQueue
->spwndActive
;
471 if ( (( !IsFGLocked() || pti
->ppi
== gppiInputProvider
) &&
472 ( CanForceFG(pti
->ppi
) || pti
->TIF_flags
& (TIF_SYSTEMTHREAD
|TIF_CSRSSTHREAD
|TIF_ALLOWFOREGROUNDACTIVATE
) )) ||
473 pti
->ppi
== ppiScrnSaver
477 //ToggleFGActivate(pti); // win.c line 2662 fail
480 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
481 gptiForeground
= Wnd
->head
.pti
;
482 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
486 IntSetFocusMessageQueue(NULL
);
487 gptiForeground
= NULL
;
488 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
492 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
493 other thread after we already changed the foreground window back to our own
497 FindRemoveAsyncMsg(Wnd
, 0); // Do this to fix test_SFW todos!
502 // Fix FG Bounce with regedit.
503 if (hWndPrev
!= hWnd
)
505 if (PrevForegroundQueue
&&
507 PrevForegroundQueue
->spwndActive
)
509 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
510 if (pti
->MessageQueue
== PrevForegroundQueue
)
512 //ERR("SFGW: TI same as Prev TI\n");
513 co_IntSetActiveWindow(NULL
, FALSE
, TRUE
, FALSE
);
517 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
518 // No real reason to wait here.
519 co_IntSendMessageNoWait(hWndPrev
, WM_ASYNC_SETACTIVEWINDOW
, 0, 0 );
524 if (!Wnd
) return FALSE
; // Always return false.
526 if (pti
->MessageQueue
== Wnd
->head
.pti
->MessageQueue
)
528 //ERR("Same PQ and WQ go active.\n");
529 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
531 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
533 //ERR("Same Active and Wnd.\n");
538 //ERR("Activate Not same PQ and WQ and Wnd.\n");
539 co_IntSendMessageNoWait(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
546 co_IntMouseActivateWindow(PWND Wnd
)
550 USER_REFERENCE_ENTRY Ref
;
554 if (Wnd
->style
& WS_DISABLED
)
558 PWND DesktopWindow
= UserGetDesktopWindow();
561 Top
= IntFindChildWindowToOwner(DesktopWindow
, Wnd
);
562 if ((TopWnd
= ValidateHwndNoErr(Top
)))
564 UserRefObjectCo(TopWnd
, &Ref
);
565 Ret
= co_IntMouseActivateWindow(TopWnd
);
566 UserDerefObjectCo(TopWnd
);
574 TopWindow
= UserGetAncestor(Wnd
, GA_ROOT
);
575 //if (TopWindow) {ERR("MAW 2 pWnd %p hWnd %p\n",TopWindow,TopWindow->head.h);}
576 if (!TopWindow
) return FALSE
;
578 /* TMN: Check return value from this function? */
579 UserRefObjectCo(TopWindow
, &Ref
);
580 co_IntSetForegroundAndFocusWindow(TopWindow
, TRUE
);
581 UserDerefObjectCo(TopWindow
);
586 co_IntSetActiveWindow(PWND Wnd OPTIONAL
, BOOL bMouse
, BOOL bFocus
, BOOL Async
)
589 PUSER_MESSAGE_QUEUE ThreadQueue
;
590 PWND pWndChg
, WndPrev
; // State changes.
594 CBTACTIVATESTRUCT cbt
;
595 //ERR("co_IntSetActiveWindow 1\n");
599 hWnd
= UserHMGetHandle(Wnd
);
600 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
601 if (Wnd
== UserGetDesktopWindow()) return FALSE
;
602 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
605 //ERR("co_IntSetActiveWindow 2\n");
606 pti
= PsGetCurrentThreadWin32Thread();
607 ThreadQueue
= pti
->MessageQueue
;
608 ASSERT(ThreadQueue
!= 0);
610 hWndPrev
= ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : NULL
;
612 pWndChg
= ThreadQueue
->spwndActive
; // Keep to notify of a preemptive switch.
616 BOOL Ret
, DoFG
, AllowFG
;
618 if (Wnd
->state
& WNDS_BEINGACTIVATED
) return TRUE
;
620 if (ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
622 if (IsAllowedFGActive(pti
, Wnd
))
628 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
631 AllowFG
= !pti
->cVisWindows
; // Nothing is visable.
632 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
634 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
636 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
637 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
638 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
639 if (!gpqForeground
|| gpqForeground
== ThreadQueue
)
647 if (pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
|| pti
->cVisWindows
)
654 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
659 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
660 //ERR("co_IntSetActiveWindow 3c FG set\n");
661 Ret
= co_IntSetForegroundAndFocusWindow(Wnd
, bMouse
);
664 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
668 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
674 /* Call CBT hook chain */
676 cbt
.hWndActive
= hWndPrev
;
677 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
679 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
683 if ( ThreadQueue
->spwndActive
&& ThreadQueue
->spwndActive
->state
& WNDS_DESTROYED
)
684 ThreadQueue
->spwndActive
= NULL
;
686 ThreadQueue
->spwndActivePrev
= ThreadQueue
->spwndActive
;
688 WndPrev
= ThreadQueue
->spwndActive
; // Keep to save changing active.
692 if (ThreadQueue
== gpqForeground
) gpqForegroundPrev
= ThreadQueue
;
693 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev
), hWnd
)) return FALSE
;
696 WndPrev
= ThreadQueue
->spwndActive
; // Again keep to save changing active.
698 // While in calling message proc or hook:
699 // Fail if a preemptive switch was made, current active not made previous,
700 // focus window is dead or no longer the same thread queue.
701 if ( ThreadQueue
->spwndActivePrev
!= ThreadQueue
->spwndActive
||
702 pWndChg
!= WndPrev
||
703 (Wnd
&& !VerifyWnd(Wnd
)) ||
704 ThreadQueue
!= pti
->MessageQueue
)
706 ERR("SetActiveWindow: Summery ERROR, active state changed!\n");
710 if (!WndPrev
) ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
712 if (Wnd
) Wnd
->state
|= WNDS_BEINGACTIVATED
;
714 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND
, Wnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
715 //// Breaks Atl-Esc/Tab via User32.
716 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
718 /* check if the specified window can be set in the input data of a given queue */
719 if ( !Wnd
|| ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
721 /* set the current thread active window */
722 ThreadQueue
->spwndActive
= Wnd
;
725 WndPrev
= VerifyWnd(ThreadQueue
->spwndActivePrev
); // Now should be set but verify it again.
727 InAAPM
= co_IntSendActivateMessages(WndPrev
, Wnd
, bMouse
, Async
);
729 /* now change focus if necessary */
730 if (bFocus
&& !(ThreadQueue
->QF_flags
& QF_FOCUSNULLSINCEACTIVE
))
732 /* Do not change focus if the window is no longer active */
733 if (ThreadQueue
->spwndActive
== Wnd
)
735 if (!ThreadQueue
->spwndFocus
||
737 UserGetAncestor(ThreadQueue
->spwndFocus
, GA_ROOT
) != Wnd
)
739 co_UserSetFocus(Wnd
);
746 pti
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
749 // FIXME: Used in the menu loop!!!
750 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
752 //ERR("co_IntSetActiveWindow Exit\n");
753 if (Wnd
) Wnd
->state
&= ~WNDS_BEINGACTIVATED
;
754 return (ThreadQueue
->spwndActive
== Wnd
);
758 UserSetActiveWindow(PWND Wnd
)
760 if (Wnd
) // Must have a window!
762 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
764 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
767 Yes your eye are not deceiving you~!
769 First part of wines Win.c test_SetActiveWindow:
771 flush_events( TRUE );
772 ShowWindow(hwnd, SW_HIDE);
775 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
782 co_UserSetFocus(PWND Window
)
787 PUSER_MESSAGE_QUEUE ThreadQueue
;
790 ASSERT_REFS_CO(Window
);
792 pti
= PsGetCurrentThreadWin32Thread();
793 ThreadQueue
= pti
->MessageQueue
;
794 ASSERT(ThreadQueue
!= 0);
796 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window
? UserHMGetHandle(Window
) : 0, pti
);
798 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
802 if (hWndPrev
== UserHMGetHandle(Window
))
804 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0; /* Nothing to do */
807 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
809 ERR("SetFocus Must have the same Q!\n");
813 /* Check if we can set the focus to this window */
814 for (pwndTop
= Window
; pwndTop
!= NULL
; pwndTop
= pwndTop
->spwndParent
)
816 if (pwndTop
->style
& (WS_MINIMIZED
|WS_DISABLED
)) return 0;
817 if ((pwndTop
->style
& (WS_POPUP
|WS_CHILD
)) != WS_CHILD
) break;
820 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->head
.h
, (LPARAM
)hWndPrev
))
822 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
826 /* Activate pwndTop if needed. */
827 if (pwndTop
!= ThreadQueue
->spwndActive
)
829 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue(); // Keep it based on desktop.
830 if (ThreadQueue
!= ForegroundQueue
&& IsAllowedFGActive(pti
, pwndTop
)) // Rule 2 & 3.
832 //ERR("SetFocus: Set Foreground!\n");
833 if (!(pwndTop
->style
& WS_VISIBLE
))
835 pti
->ppi
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
;
837 if (!co_IntSetForegroundAndFocusWindow(pwndTop
, FALSE
))
839 ERR("SetFocus: Set Foreground and Focus Failed!\n");
844 /* Set Active when it is needed. */
845 if (pwndTop
!= ThreadQueue
->spwndActive
)
847 //ERR("SetFocus: Set Active!\n");
848 if (!co_IntSetActiveWindow(pwndTop
, FALSE
, FALSE
, FALSE
))
850 ERR("SetFocus: Set Active Failed!\n");
855 /* Abort if window destroyed */
856 if (Window
->state2
& WNDS2_INDESTROY
) return 0;
857 /* Do not change focus if the window is no longer active */
858 if (pwndTop
!= ThreadQueue
->spwndActive
)
860 ERR("SetFocus: Top window did not go active!\n");
865 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
866 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
868 IntSendFocusMessages( pti
, Window
);
870 TRACE("Focus: %p -> %p\n", hWndPrev
, Window
->head
.h
);
872 else /* NULL hwnd passed in */
874 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
876 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
880 /* set the current thread focus window null */
881 IntSendFocusMessages( pti
, NULL
);
883 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0;
887 UserGetForegroundWindow(VOID
)
889 PUSER_MESSAGE_QUEUE ForegroundQueue
;
891 ForegroundQueue
= IntGetFocusMessageQueue();
892 return( ForegroundQueue
? (ForegroundQueue
->spwndActive
? UserHMGetHandle(ForegroundQueue
->spwndActive
) : 0) : 0);
895 HWND FASTCALL
UserGetActiveWindow(VOID
)
898 PUSER_MESSAGE_QUEUE ThreadQueue
;
900 pti
= PsGetCurrentThreadWin32Thread();
901 ThreadQueue
= pti
->MessageQueue
;
902 return( ThreadQueue
? (ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : 0) : 0);
909 PUSER_MESSAGE_QUEUE ThreadQueue
;
910 DECLARE_RETURN(HWND
);
912 TRACE("Enter IntGetCapture\n");
914 pti
= PsGetCurrentThreadWin32Thread();
915 ThreadQueue
= pti
->MessageQueue
;
916 RETURN( ThreadQueue
? (ThreadQueue
->spwndCapture
? UserHMGetHandle(ThreadQueue
->spwndCapture
) : 0) : 0);
919 TRACE("Leave IntGetCapture, ret=%p\n", _ret_
);
924 co_UserSetCapture(HWND hWnd
)
927 PUSER_MESSAGE_QUEUE ThreadQueue
;
928 PWND pWnd
, Window
= NULL
;
931 pti
= PsGetCurrentThreadWin32Thread();
932 ThreadQueue
= pti
->MessageQueue
;
934 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
)
937 if (hWnd
&& (Window
= UserGetWindowObject(hWnd
)))
939 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
945 hWndPrev
= MsqSetStateWindow(pti
, MSQ_STATE_CAPTURE
, hWnd
);
949 pWnd
= UserGetWindowObject(hWndPrev
);
951 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND
, pWnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
955 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART
, Window
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
957 if (hWndPrev
&& hWndPrev
!= hWnd
)
959 if (ThreadQueue
->MenuOwner
&& Window
) ThreadQueue
->QF_flags
|= QF_CAPTURELOCKED
;
961 co_IntPostOrSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
963 ThreadQueue
->QF_flags
&= ~QF_CAPTURELOCKED
;
966 ThreadQueue
->spwndCapture
= Window
;
968 if (hWnd
== NULL
) // Release mode.
972 /* Also remove other windows if not capturing anymore */
973 MsqSetStateWindow(pti
, MSQ_STATE_MENUOWNER
, NULL
);
974 MsqSetStateWindow(pti
, MSQ_STATE_MOVESIZE
, NULL
);
976 /* Somebody may have missed some mouse movements */
980 mi
.dwFlags
= MOUSEEVENTF_MOVE
;
983 UserSendMouseInput(&mi
, FALSE
);
993 IntReleaseCapture(VOID
)
996 PUSER_MESSAGE_QUEUE ThreadQueue
;
998 pti
= PsGetCurrentThreadWin32Thread();
999 ThreadQueue
= pti
->MessageQueue
;
1001 // Can not release inside WM_CAPTURECHANGED!!
1002 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
) return FALSE
;
1004 co_UserSetCapture(NULL
);
1013 co_IntSetForegroundWindow(PWND Window
)
1015 if (Window
) ASSERT_REFS_CO(Window
);
1017 return co_IntSetForegroundAndFocusWindow(Window
, FALSE
);
1024 co_IntSetForegroundWindowMouse(PWND Window
)
1026 if (Window
) ASSERT_REFS_CO(Window
);
1028 return co_IntSetForegroundAndFocusWindow(Window
, TRUE
);
1035 IntLockSetForegroundWindow(UINT uLockCode
)
1037 ULONG Err
= ERROR_ACCESS_DENIED
;
1038 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
1042 if ( CanForceFG(ppi
) && !gppiLockSFW
)
1049 if ( gppiLockSFW
== ppi
)
1056 Err
= ERROR_INVALID_PARAMETER
;
1058 EngSetLastError(Err
);
1066 IntAllowSetForegroundWindow(DWORD dwProcessId
)
1068 PPROCESSINFO ppi
, ppiCur
;
1069 PEPROCESS Process
= NULL
;
1072 if (dwProcessId
!= ASFW_ANY
)
1074 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE
)dwProcessId
, &Process
)))
1076 EngSetLastError(ERROR_INVALID_PARAMETER
);
1079 ppi
= PsGetProcessWin32Process(Process
);
1082 ObDereferenceObject(Process
);
1086 ppiCur
= PsGetCurrentProcessWin32Process();
1087 if (!CanForceFG(ppiCur
))
1089 if (Process
) ObDereferenceObject(Process
);
1090 EngSetLastError(ERROR_ACCESS_DENIED
);
1093 if (dwProcessId
== ASFW_ANY
)
1094 { // All processes will be enabled to set the foreground window.
1095 //ERR("ptiLastInput is CLEARED!!\n");
1096 ptiLastInput
= NULL
;
1099 { // Rule #3, last input event in force.
1100 ERR("ptiLastInput is SET!!\n");
1101 //ptiLastInput = ppi->ptiList; // See CORE-6384 & CORE-7030.
1102 ObDereferenceObject(Process
);
1111 NtUserGetForegroundWindow(VOID
)
1113 DECLARE_RETURN(HWND
);
1115 TRACE("Enter NtUserGetForegroundWindow\n");
1116 UserEnterExclusive();
1118 RETURN( UserGetForegroundWindow());
1121 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_
);
1127 NtUserSetActiveWindow(HWND hWnd
)
1129 USER_REFERENCE_ENTRY Ref
;
1132 DECLARE_RETURN(HWND
);
1134 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd
);
1135 UserEnterExclusive();
1140 if (!(Window
= UserGetWindowObject(hWnd
)))
1142 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd
);
1148 Window
->head
.pti
->MessageQueue
== gptiCurrent
->MessageQueue
)
1150 hWndPrev
= gptiCurrent
->MessageQueue
->spwndActive
? UserHMGetHandle(gptiCurrent
->MessageQueue
->spwndActive
) : NULL
;
1151 if (Window
) UserRefObjectCo(Window
, &Ref
);
1152 UserSetActiveWindow(Window
);
1153 if (Window
) UserDerefObjectCo(Window
);
1154 RETURN( hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0 );
1159 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_
);
1168 NtUserSetCapture(HWND hWnd
)
1170 DECLARE_RETURN(HWND
);
1172 TRACE("Enter NtUserSetCapture(%p)\n", hWnd
);
1173 UserEnterExclusive();
1175 RETURN( co_UserSetCapture(hWnd
));
1178 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_
);
1187 NtUserSetFocus(HWND hWnd
)
1190 USER_REFERENCE_ENTRY Ref
;
1191 DECLARE_RETURN(HWND
);
1194 TRACE("Enter NtUserSetFocus(%p)\n", hWnd
);
1195 UserEnterExclusive();
1199 if (!(Window
= UserGetWindowObject(hWnd
)))
1201 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd
);
1205 UserRefObjectCo(Window
, &Ref
);
1206 ret
= co_UserSetFocus(Window
);
1207 UserDerefObjectCo(Window
);
1213 RETURN( co_UserSetFocus(0));
1217 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_
);