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
),
123 //// Fixes CORE-6434.
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 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
234 (WPARAM
)(Window
== (gpqForeground
? gpqForeground
->spwndActive
: NULL
)),
235 0); //(LPARAM)hWndPrev);
237 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
239 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
, Window
->style
& WS_MINIMIZE
),
240 (LPARAM
)(WindowPrev
? UserHMGetHandle(WindowPrev
) : 0));
242 if (!Window
->spwndOwner
&& !IntGetParent(Window
))
244 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
245 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (WPARAM
) UserHMGetHandle(Window
), FALSE
);
248 Window
->state
&= ~WNDS_NONCPAINT
;
250 UserDerefObjectCo(Window
);
256 IntSendFocusMessages( PTHREADINFO pti
, PWND pWnd
)
259 PUSER_MESSAGE_QUEUE ThreadQueue
= pti
->MessageQueue
; // Queue can change...
261 ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
262 if (!pWnd
&& ThreadQueue
->spwndActive
)
264 ThreadQueue
->QF_flags
|= QF_FOCUSNULLSINCEACTIVE
;
267 pWndPrev
= ThreadQueue
->spwndFocus
;
269 /* check if the specified window can be set in the input data of a given queue */
270 if (!pWnd
|| ThreadQueue
== pWnd
->head
.pti
->MessageQueue
)
271 /* set the current thread focus window */
272 ThreadQueue
->spwndFocus
= pWnd
;
278 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
279 co_IntSendMessage(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);
285 co_IntSendMessage(UserHMGetHandle(pWnd
), WM_SETFOCUS
, (WPARAM
)(pWndPrev
? UserHMGetHandle(pWndPrev
) : NULL
), 0);
292 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, NULL
, OBJID_CLIENT
, CHILDID_SELF
, 0);
293 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
294 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, 0, 0);
300 FindRemoveAsyncMsg(PWND Wnd
, WPARAM wParam
)
303 PUSER_SENT_MESSAGE Message
;
310 if (!IsListEmpty(&pti
->SentMessagesListHead
))
312 // Scan sent queue messages to see if we received async messages.
313 Entry
= pti
->SentMessagesListHead
.Flink
;
314 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
317 if (IsListEmpty(Entry
)) return;
318 if (!Message
) return;
319 Entry
= Message
->ListEntry
.Flink
;
321 if (Message
->Msg
.message
== WM_ASYNC_SETACTIVEWINDOW
&&
322 Message
->Msg
.hwnd
== UserHMGetHandle(Wnd
) &&
323 Message
->Msg
.wParam
== wParam
)
325 ERR("ASYNC SAW: Found one in the Sent Msg Queue! %p Activate/Deactivate %d\n", Message
->Msg
.hwnd
,!!wParam
);
326 RemoveEntryList(&Message
->ListEntry
); // Purge the entry.
327 ExFreePoolWithTag(Message
, TAG_USRMSG
);
329 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
331 while (Entry
!= &pti
->SentMessagesListHead
);
336 ToggleFGActivate(PTHREADINFO pti
)
339 PPROCESSINFO ppi
= pti
->ppi
;
341 Ret
= !!(pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
);
344 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
347 Ret
= !!(ppi
->W32PF_flags
& W32PF_ALLOWFOREGROUNDACTIVATE
);
350 ppi
->W32PF_flags
&= ~W32PF_ALLOWFOREGROUNDACTIVATE
;
351 //ERR("ToggleFGActivate is %d\n",Ret);
356 IsAllowedFGActive(PTHREADINFO pti
, PWND Wnd
)
358 // Not allowed if one or more,,
359 if (!ToggleFGActivate(pti
) || // bits not set,
360 pti
->rpdesk
!= gpdeskInputDesktop
|| // not current Desktop,
361 pti
->MessageQueue
== gpqForeground
|| // if already the queue foreground,
362 IsFGLocked() || // foreground is locked,
363 Wnd
->ExStyle
& WS_EX_NOACTIVATE
) // or,,, does not become the foreground window when the user clicks it.
367 //ERR("IsAllowedFGActive is TRUE\n");
372 Can the system force foreground from one or more conditions.
375 CanForceFG(PPROCESSINFO ppi
)
378 ptiLastInput
->ppi
== ppi
||
380 gptiForeground
->ppi
== ppi
||
381 ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_SETFOREGROUNDALLOWED
) ||
382 gppiInputProvider
== ppi
||
385 //ERR("CanForceFG is FALSE\n");
391 The system restricts which processes can set the foreground window. A process
392 can set the foreground window only if one of the following conditions is true:
394 * The process is the foreground process.
395 * The process was started by the foreground process.
396 * The process received the last input event.
397 * There is no foreground process.
398 * The foreground process is being debugged.
399 * The foreground is not locked (see LockSetForegroundWindow).
400 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
401 * No menus are active.
405 co_IntSetForegroundAndFocusWindow(
407 _In_ BOOL MouseActivate
)
409 HWND hWnd
= Wnd
? UserHMGetHandle(Wnd
) : NULL
;
410 HWND hWndPrev
= NULL
;
411 PWND pWndPrev
= NULL
;
412 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
414 BOOL fgRet
= FALSE
, Ret
= FALSE
;
416 if (Wnd
) ASSERT_REFS_CO(Wnd
);
418 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
420 PrevForegroundQueue
= IntGetFocusMessageQueue(); // Use this active desktop.
421 pti
= PsGetCurrentThreadWin32Thread();
423 if (PrevForegroundQueue
)
424 { // Same Window Q as foreground just do active.
425 if (Wnd
&& Wnd
->head
.pti
->MessageQueue
== PrevForegroundQueue
)
427 //ERR("Same Window Q as foreground just do active.\n");
428 if (pti
->MessageQueue
== PrevForegroundQueue
)
429 { // Same WQ and TQ go active.
430 //ERR("Same WQ and TQ go active.\n");
431 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
433 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
434 { // Same WQ and it is active.
435 //ERR("Same WQ and it is active.\n");
439 { // Same WQ as FG but not the same TQ send active.
440 //ERR("Same WQ as FG but not the same TQ send active.\n");
441 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
447 hWndPrev
= PrevForegroundQueue
->spwndActive
? UserHMGetHandle(PrevForegroundQueue
->spwndActive
) : 0;
448 pWndPrev
= PrevForegroundQueue
->spwndActive
;
451 if ( (( !IsFGLocked() || pti
->ppi
== gppiInputProvider
) &&
452 ( CanForceFG(pti
->ppi
) || pti
->TIF_flags
& (TIF_SYSTEMTHREAD
|TIF_CSRSSTHREAD
|TIF_ALLOWFOREGROUNDACTIVATE
) )) ||
453 pti
->ppi
== ppiScrnSaver
457 //ToggleFGActivate(pti); // win.c line 2662 fail
460 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
461 gptiForeground
= Wnd
->head
.pti
;
462 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
466 IntSetFocusMessageQueue(NULL
);
467 gptiForeground
= NULL
;
468 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
472 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
473 other thread after we already changed the foreground window back to our own
477 FindRemoveAsyncMsg(Wnd
, 0); // Do this to fix test_SFW todos!
482 // Fix FG Bounce with regedit.
483 if (hWndPrev
!= hWnd
)
485 if (PrevForegroundQueue
&&
487 PrevForegroundQueue
->spwndActive
)
489 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
490 if (pti
->MessageQueue
== PrevForegroundQueue
)
492 //ERR("SFGW: TI same as Prev TI\n");
493 co_IntSetActiveWindow(NULL
, FALSE
, TRUE
, FALSE
);
497 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
498 // No real reason to wait here.
499 co_IntSendMessageNoWait(hWndPrev
, WM_ASYNC_SETACTIVEWINDOW
, 0, 0 );
504 if (!Wnd
) return FALSE
; // Always return false.
506 if (pti
->MessageQueue
== Wnd
->head
.pti
->MessageQueue
)
508 //ERR("Same PQ and WQ go active.\n");
509 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
511 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
513 //ERR("Same Active and Wnd.\n");
518 //ERR("Activate Not same PQ and WQ and Wnd.\n");
519 co_IntSendMessageNoWait(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
526 Revision 7888, activate modal dialog when clicking on a disabled window.
529 IntFindChildWindowToOwner(PWND Root
, PWND Owner
)
532 PWND Child
, OwnerWnd
;
534 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
536 OwnerWnd
= Child
->spwndOwner
;
540 if(OwnerWnd
== Owner
)
550 co_IntMouseActivateWindow(PWND Wnd
)
553 USER_REFERENCE_ENTRY Ref
;
556 if (Wnd
->style
& WS_DISABLED
)
560 PWND DesktopWindow
= UserGetDesktopWindow();
563 ERR("Window Diabled\n");
564 Top
= IntFindChildWindowToOwner(DesktopWindow
, Wnd
);
565 if ((TopWnd
= ValidateHwndNoErr(Top
)))
567 UserRefObjectCo(TopWnd
, &Ref
);
568 Ret
= co_IntMouseActivateWindow(TopWnd
);
569 UserDerefObjectCo(TopWnd
);
576 ERR("Mouse Active\n");
577 co_IntSetForegroundAndFocusWindow(Wnd
, TRUE
);
582 co_IntSetActiveWindow(PWND Wnd OPTIONAL
, BOOL bMouse
, BOOL bFocus
, BOOL Async
)
585 PUSER_MESSAGE_QUEUE ThreadQueue
;
586 PWND pWndChg
, WndPrev
; // State changes.
590 CBTACTIVATESTRUCT cbt
;
591 //ERR("co_IntSetActiveWindow 1\n");
595 hWnd
= UserHMGetHandle(Wnd
);
596 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
597 if (Wnd
== UserGetDesktopWindow()) return FALSE
;
598 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
601 //ERR("co_IntSetActiveWindow 2\n");
602 pti
= PsGetCurrentThreadWin32Thread();
603 ThreadQueue
= pti
->MessageQueue
;
604 ASSERT(ThreadQueue
!= 0);
606 hWndPrev
= ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : NULL
;
608 pWndChg
= ThreadQueue
->spwndActive
; // Keep to notify of a preemptive switch.
612 BOOL Ret
, DoFG
, AllowFG
;
614 if (Wnd
->state
& WNDS_BEINGACTIVATED
) return TRUE
;
616 if (ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
618 if (IsAllowedFGActive(pti
, Wnd
))
624 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
627 AllowFG
= !pti
->cVisWindows
; // Nothing is visable.
628 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
630 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
632 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
633 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
634 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
635 if (!gpqForeground
|| gpqForeground
== ThreadQueue
)
643 if (pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
|| pti
->cVisWindows
)
650 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
655 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
656 //ERR("co_IntSetActiveWindow 3c FG set\n");
657 Ret
= co_IntSetForegroundAndFocusWindow(Wnd
, bMouse
);
660 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
664 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
670 /* Call CBT hook chain */
672 cbt
.hWndActive
= hWndPrev
;
673 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
675 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
679 if ( ThreadQueue
->spwndActive
&& ThreadQueue
->spwndActive
->state
& WNDS_DESTROYED
)
680 ThreadQueue
->spwndActive
= NULL
;
682 ThreadQueue
->spwndActivePrev
= ThreadQueue
->spwndActive
;
684 WndPrev
= ThreadQueue
->spwndActive
; // Keep to save changing active.
688 if (ThreadQueue
== gpqForeground
) gpqForegroundPrev
= ThreadQueue
;
689 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev
), hWnd
)) return FALSE
;
692 WndPrev
= ThreadQueue
->spwndActive
; // Again keep to save changing active.
694 // While in calling message proc or hook:
695 // Fail if a preemptive switch was made, current active not made previous,
696 // focus window is dead or no longer the same thread queue.
697 if ( ThreadQueue
->spwndActivePrev
!= ThreadQueue
->spwndActive
||
698 pWndChg
!= WndPrev
||
699 (Wnd
&& !VerifyWnd(Wnd
)) ||
700 ThreadQueue
!= pti
->MessageQueue
)
702 ERR("SetActiveWindow: Summery ERROR, active state changed!\n");
706 if (!WndPrev
) ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
708 if (Wnd
) Wnd
->state
|= WNDS_BEINGACTIVATED
;
710 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND
, Wnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
711 //// Breaks Atl-Esc/Tab via User32.
712 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
714 /* check if the specified window can be set in the input data of a given queue */
715 if ( !Wnd
|| ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
717 /* set the current thread active window */
718 ThreadQueue
->spwndActive
= Wnd
;
721 WndPrev
= VerifyWnd(ThreadQueue
->spwndActivePrev
); // Now should be set but verify it again.
723 InAAPM
= co_IntSendActivateMessages(WndPrev
, Wnd
, bMouse
, Async
);
725 /* now change focus if necessary */
726 //// Fixes CORE-6452 allows setting focus on window.
727 if (bFocus
&& !(ThreadQueue
->QF_flags
& QF_FOCUSNULLSINCEACTIVE
))
729 /* Do not change focus if the window is no longer active */
730 if (pti
->MessageQueue
->spwndActive
!= IntGetNonChildAncestor(pti
->MessageQueue
->spwndFocus
))
732 PWND pWndSend
= pti
->MessageQueue
->spwndActive
;
733 // Clear focus if the active window is minimized.
734 if (pWndSend
&& pti
->MessageQueue
->spwndActive
->style
& WS_MINIMIZE
) pWndSend
= NULL
;
735 // Send focus messages and if so, set the focus.
736 IntSendFocusMessages( pti
, pWndSend
);
742 pti
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
745 // FIXME: Used in the menu loop!!!
746 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
748 //ERR("co_IntSetActiveWindow Exit\n");
749 if (Wnd
) Wnd
->state
&= ~WNDS_BEINGACTIVATED
;
750 return (ThreadQueue
->spwndActive
== Wnd
);
754 UserSetActiveWindow(PWND Wnd
)
756 if (Wnd
) // Must have a window!
758 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
760 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
763 Yes your eye are not deceiving you~!
765 First part of wines Win.c test_SetActiveWindow:
767 flush_events( TRUE );
768 ShowWindow(hwnd, SW_HIDE);
771 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
778 co_UserSetFocus(PWND Window
)
783 PUSER_MESSAGE_QUEUE ThreadQueue
;
786 ASSERT_REFS_CO(Window
);
788 pti
= PsGetCurrentThreadWin32Thread();
789 ThreadQueue
= pti
->MessageQueue
;
790 ASSERT(ThreadQueue
!= 0);
792 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window
? UserHMGetHandle(Window
) : 0, pti
);
794 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
798 if (hWndPrev
== UserHMGetHandle(Window
))
800 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0; /* Nothing to do */
803 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
805 ERR("SetFocus Must have the same Q!\n");
809 /* Check if we can set the focus to this window */
810 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720...
811 for (pwndTop
= Window
; pwndTop
; pwndTop
= pwndTop
->spwndParent
)
813 if (pwndTop
->style
& (WS_MINIMIZED
|WS_DISABLED
)) return 0;
814 if ((pwndTop
->style
& (WS_POPUP
|WS_CHILD
)) != WS_CHILD
) break;
817 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->head
.h
, (LPARAM
)hWndPrev
))
819 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
823 /* Activate pwndTop if needed. */
824 if (pwndTop
!= ThreadQueue
->spwndActive
)
826 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue(); // Keep it based on desktop.
827 if (ThreadQueue
!= ForegroundQueue
&& IsAllowedFGActive(pti
, pwndTop
)) // Rule 2 & 3.
829 //ERR("SetFocus: Set Foreground!\n");
830 if (!(pwndTop
->style
& WS_VISIBLE
))
832 pti
->ppi
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
;
834 if (!co_IntSetForegroundAndFocusWindow(pwndTop
, FALSE
))
836 ERR("SetFocus: Set Foreground and Focus Failed!\n");
841 /* Set Active when it is needed. */
842 if (pwndTop
!= ThreadQueue
->spwndActive
)
844 //ERR("SetFocus: Set Active!\n");
845 if (!co_IntSetActiveWindow(pwndTop
, FALSE
, FALSE
, FALSE
))
847 ERR("SetFocus: Set Active Failed!\n");
852 /* Abort if window destroyed */
853 if (Window
->state2
& WNDS2_INDESTROY
) return 0;
854 /* Do not change focus if the window is no longer active */
855 if (pwndTop
!= ThreadQueue
->spwndActive
)
857 ERR("SetFocus: Top window did not go active!\n");
862 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
863 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
865 IntSendFocusMessages( pti
, Window
);
867 TRACE("Focus: %p -> %p\n", hWndPrev
, Window
->head
.h
);
869 else /* NULL hwnd passed in */
871 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
873 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
877 /* set the current thread focus window null */
878 IntSendFocusMessages( pti
, NULL
);
880 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0;
884 UserGetForegroundWindow(VOID
)
886 PUSER_MESSAGE_QUEUE ForegroundQueue
;
888 ForegroundQueue
= IntGetFocusMessageQueue();
889 return( ForegroundQueue
? (ForegroundQueue
->spwndActive
? UserHMGetHandle(ForegroundQueue
->spwndActive
) : 0) : 0);
892 HWND FASTCALL
UserGetActiveWindow(VOID
)
895 PUSER_MESSAGE_QUEUE ThreadQueue
;
897 pti
= PsGetCurrentThreadWin32Thread();
898 ThreadQueue
= pti
->MessageQueue
;
899 return( ThreadQueue
? (ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : 0) : 0);
906 PUSER_MESSAGE_QUEUE ThreadQueue
;
907 DECLARE_RETURN(HWND
);
909 TRACE("Enter IntGetCapture\n");
911 pti
= PsGetCurrentThreadWin32Thread();
912 ThreadQueue
= pti
->MessageQueue
;
913 RETURN( ThreadQueue
? (ThreadQueue
->spwndCapture
? UserHMGetHandle(ThreadQueue
->spwndCapture
) : 0) : 0);
916 TRACE("Leave IntGetCapture, ret=%p\n", _ret_
);
921 co_UserSetCapture(HWND hWnd
)
924 PUSER_MESSAGE_QUEUE ThreadQueue
;
925 PWND pWnd
, Window
= NULL
;
928 pti
= PsGetCurrentThreadWin32Thread();
929 ThreadQueue
= pti
->MessageQueue
;
931 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
)
934 if (hWnd
&& (Window
= UserGetWindowObject(hWnd
)))
936 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
942 hWndPrev
= MsqSetStateWindow(pti
, MSQ_STATE_CAPTURE
, hWnd
);
946 pWnd
= UserGetWindowObject(hWndPrev
);
948 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND
, pWnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
952 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART
, Window
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
954 if (hWndPrev
&& hWndPrev
!= hWnd
)
956 if (ThreadQueue
->MenuOwner
&& Window
) ThreadQueue
->QF_flags
|= QF_CAPTURELOCKED
;
958 //co_IntPostOrSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
959 co_IntSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
961 ThreadQueue
->QF_flags
&= ~QF_CAPTURELOCKED
;
964 ThreadQueue
->spwndCapture
= Window
;
966 if (hWnd
== NULL
) // Release mode.
970 /* Also remove other windows if not capturing anymore */
971 MsqSetStateWindow(pti
, MSQ_STATE_MENUOWNER
, NULL
);
972 MsqSetStateWindow(pti
, MSQ_STATE_MOVESIZE
, NULL
);
974 /* Somebody may have missed some mouse movements */
978 mi
.dwFlags
= MOUSEEVENTF_MOVE
;
981 UserSendMouseInput(&mi
, FALSE
);
991 IntReleaseCapture(VOID
)
994 PUSER_MESSAGE_QUEUE ThreadQueue
;
996 pti
= PsGetCurrentThreadWin32Thread();
997 ThreadQueue
= pti
->MessageQueue
;
999 // Can not release inside WM_CAPTURECHANGED!!
1000 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
) return FALSE
;
1002 co_UserSetCapture(NULL
);
1011 co_IntSetForegroundWindow(PWND Window
)
1013 if (Window
) ASSERT_REFS_CO(Window
);
1015 return co_IntSetForegroundAndFocusWindow(Window
, FALSE
);
1022 co_IntSetForegroundWindowMouse(PWND Window
)
1024 if (Window
) ASSERT_REFS_CO(Window
);
1026 return co_IntSetForegroundAndFocusWindow(Window
, TRUE
);
1033 IntLockSetForegroundWindow(UINT uLockCode
)
1035 ULONG Err
= ERROR_ACCESS_DENIED
;
1036 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
1040 if ( CanForceFG(ppi
) && !gppiLockSFW
)
1047 if ( gppiLockSFW
== ppi
)
1054 Err
= ERROR_INVALID_PARAMETER
;
1056 EngSetLastError(Err
);
1064 IntAllowSetForegroundWindow(DWORD dwProcessId
)
1066 PPROCESSINFO ppi
, ppiCur
;
1067 PEPROCESS Process
= NULL
;
1070 if (dwProcessId
!= ASFW_ANY
)
1072 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE
)dwProcessId
, &Process
)))
1074 EngSetLastError(ERROR_INVALID_PARAMETER
);
1077 ppi
= PsGetProcessWin32Process(Process
);
1080 ObDereferenceObject(Process
);
1084 ppiCur
= PsGetCurrentProcessWin32Process();
1085 if (!CanForceFG(ppiCur
))
1087 if (Process
) ObDereferenceObject(Process
);
1088 EngSetLastError(ERROR_ACCESS_DENIED
);
1091 if (dwProcessId
== ASFW_ANY
)
1092 { // All processes will be enabled to set the foreground window.
1093 //ERR("ptiLastInput is CLEARED!!\n");
1094 ptiLastInput
= NULL
;
1097 { // Rule #3, last input event in force.
1098 ERR("ptiLastInput is SET!!\n");
1099 //ptiLastInput = ppi->ptiList; // See CORE-6384 & CORE-7030.
1100 ObDereferenceObject(Process
);
1109 NtUserGetForegroundWindow(VOID
)
1111 DECLARE_RETURN(HWND
);
1113 TRACE("Enter NtUserGetForegroundWindow\n");
1114 UserEnterExclusive();
1116 RETURN( UserGetForegroundWindow());
1119 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_
);
1125 NtUserSetActiveWindow(HWND hWnd
)
1127 USER_REFERENCE_ENTRY Ref
;
1130 DECLARE_RETURN(HWND
);
1132 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd
);
1133 UserEnterExclusive();
1138 if (!(Window
= UserGetWindowObject(hWnd
)))
1140 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd
);
1146 Window
->head
.pti
->MessageQueue
== gptiCurrent
->MessageQueue
)
1148 hWndPrev
= gptiCurrent
->MessageQueue
->spwndActive
? UserHMGetHandle(gptiCurrent
->MessageQueue
->spwndActive
) : NULL
;
1149 if (Window
) UserRefObjectCo(Window
, &Ref
);
1150 UserSetActiveWindow(Window
);
1151 if (Window
) UserDerefObjectCo(Window
);
1152 RETURN( hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0 );
1157 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_
);
1166 NtUserSetCapture(HWND hWnd
)
1168 DECLARE_RETURN(HWND
);
1170 TRACE("Enter NtUserSetCapture(%p)\n", hWnd
);
1171 UserEnterExclusive();
1173 RETURN( co_UserSetCapture(hWnd
));
1176 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_
);
1185 NtUserSetFocus(HWND hWnd
)
1188 USER_REFERENCE_ENTRY Ref
;
1189 DECLARE_RETURN(HWND
);
1192 TRACE("Enter NtUserSetFocus(%p)\n", hWnd
);
1193 UserEnterExclusive();
1197 if (!(Window
= UserGetWindowObject(hWnd
)))
1199 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd
);
1203 UserRefObjectCo(Window
, &Ref
);
1204 ret
= co_UserSetFocus(Window
);
1205 UserDerefObjectCo(Window
);
1211 RETURN( co_UserSetFocus(0));
1215 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_
);