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
;
104 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
);
169 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
171 if (Window
->state
& WNDS_ACTIVEFRAME
)
172 { // If already active frame do not allow NCPaint.
173 //ERR("SendActivateMessage Is Active Frame!\n");
174 Window
->state
|= WNDS_NONCPAINT
;
177 if (Window
->style
& WS_MINIMIZE
)
179 TRACE("Widow was minimized\n");
182 co_IntMakeWindowActive(Window
);
184 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
186 (WPARAM
)(Window
== (gpqForeground
? gpqForeground
->spwndActive
: NULL
)),
187 0); //(LPARAM)hWndPrev);
189 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
191 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
, Window
->style
& WS_MINIMIZE
),
192 (LPARAM
)(WindowPrev
? UserHMGetHandle(WindowPrev
) : 0));
194 if (Window
->spwndParent
== UserGetDesktopWindow() &&
195 Window
->spwndOwner
== NULL
&&
196 (!(Window
->ExStyle
& WS_EX_TOOLWINDOW
) ||
197 (Window
->ExStyle
& WS_EX_APPWINDOW
)))
199 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
200 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (WPARAM
) UserHMGetHandle(Window
), FALSE
);
204 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, 0, FALSE
);
207 Window
->state
&= ~WNDS_NONCPAINT
;
209 UserDerefObjectCo(Window
);
212 OldTID
= WindowPrev
? IntGetWndThreadId(WindowPrev
) : NULL
;
213 NewTID
= Window
? IntGetWndThreadId(Window
) : NULL
;
214 ptiOld
= WindowPrev
? WindowPrev
->head
.pti
: NULL
;
215 ptiNew
= Window
? Window
->head
.pti
: NULL
;
217 //ERR("SendActivateMessage WindowPrev -> %x, Old -> %x, New -> %x\n", WindowPrev, OldTID, NewTID);
219 if (!(pti
->TIF_flags
& TIF_INACTIVATEAPPMSG
) &&
225 List
= IntWinListChildren(UserGetDesktopWindow());
230 ptiOld
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
231 // Note: Do not set pci flags, this does crash!
232 for (phWnd
= List
; *phWnd
; ++phWnd
)
234 cWindow
= ValidateHwndNoErr(*phWnd
);
235 if (cWindow
&& cWindow
->head
.pti
== ptiOld
)
236 { // FALSE if the window is being deactivated,
237 // ThreadId that owns the window being activated.
238 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
241 ptiOld
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
244 { //// Prevents a resource crash due to reentrance!
246 pti
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
248 for (phWnd
= List
; *phWnd
; ++phWnd
)
250 cWindow
= ValidateHwndNoErr(*phWnd
);
251 if (cWindow
&& cWindow
->head
.pti
== ptiNew
)
252 { // TRUE if the window is being activated,
253 // ThreadId that owns the window being deactivated.
254 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
258 ExFreePoolWithTag(List
, USERTAG_WINDOWLIST
);
265 IntSendFocusMessages( PTHREADINFO pti
, PWND pWnd
)
268 PUSER_MESSAGE_QUEUE ThreadQueue
= pti
->MessageQueue
; // Queue can change...
270 ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
271 if (!pWnd
&& ThreadQueue
->spwndActive
)
273 ThreadQueue
->QF_flags
|= QF_FOCUSNULLSINCEACTIVE
;
276 pWndPrev
= ThreadQueue
->spwndFocus
;
278 /* check if the specified window can be set in the input data of a given queue */
279 if (!pWnd
|| ThreadQueue
== pWnd
->head
.pti
->MessageQueue
)
280 /* set the current thread focus window */
281 ThreadQueue
->spwndFocus
= pWnd
;
287 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
288 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, (WPARAM
)UserHMGetHandle(pWnd
), 0);
290 if (ThreadQueue
->spwndFocus
== pWnd
)
292 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, pWnd
, OBJID_CLIENT
, CHILDID_SELF
, 0);
293 //co_IntPostOrSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)(pWndPrev ? UserHMGetHandle(pWndPrev) : NULL), 0);
294 co_IntSendMessage(UserHMGetHandle(pWnd
), WM_SETFOCUS
, (WPARAM
)(pWndPrev
? UserHMGetHandle(pWndPrev
) : NULL
), 0);
301 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, NULL
, OBJID_CLIENT
, CHILDID_SELF
, 0);
302 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
303 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, 0, 0);
309 FindRemoveAsyncMsg(PWND Wnd
, WPARAM wParam
)
312 PUSER_SENT_MESSAGE Message
;
319 if (!IsListEmpty(&pti
->SentMessagesListHead
))
321 // Scan sent queue messages to see if we received async messages.
322 Entry
= pti
->SentMessagesListHead
.Flink
;
323 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
326 if (IsListEmpty(Entry
)) return;
327 if (!Message
) return;
328 Entry
= Message
->ListEntry
.Flink
;
330 if (Message
->Msg
.message
== WM_ASYNC_SETACTIVEWINDOW
&&
331 Message
->Msg
.hwnd
== UserHMGetHandle(Wnd
) &&
332 Message
->Msg
.wParam
== wParam
)
334 ERR("ASYNC SAW: Found one in the Sent Msg Queue! %p Activate/Deactivate %d\n", Message
->Msg
.hwnd
,!!wParam
);
335 RemoveEntryList(&Message
->ListEntry
); // Purge the entry.
336 ExFreePoolWithTag(Message
, TAG_USRMSG
);
338 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
340 while (Entry
!= &pti
->SentMessagesListHead
);
345 ToggleFGActivate(PTHREADINFO pti
)
348 PPROCESSINFO ppi
= pti
->ppi
;
350 Ret
= !!(pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
);
353 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
356 Ret
= !!(ppi
->W32PF_flags
& W32PF_ALLOWFOREGROUNDACTIVATE
);
359 ppi
->W32PF_flags
&= ~W32PF_ALLOWFOREGROUNDACTIVATE
;
360 //ERR("ToggleFGActivate is %d\n",Ret);
365 IsAllowedFGActive(PTHREADINFO pti
, PWND Wnd
)
367 // Not allowed if one or more,,
368 if (!ToggleFGActivate(pti
) || // bits not set,
369 pti
->rpdesk
!= gpdeskInputDesktop
|| // not current Desktop,
370 pti
->MessageQueue
== gpqForeground
|| // if already the queue foreground,
371 IsFGLocked() || // foreground is locked,
372 Wnd
->ExStyle
& WS_EX_NOACTIVATE
) // or,,, does not become the foreground window when the user clicks it.
376 //ERR("IsAllowedFGActive is TRUE\n");
381 Can the system force foreground from one or more conditions.
384 CanForceFG(PPROCESSINFO ppi
)
387 ptiLastInput
->ppi
== ppi
||
389 gptiForeground
->ppi
== ppi
||
390 ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_SETFOREGROUNDALLOWED
) ||
391 gppiInputProvider
== ppi
||
394 //ERR("CanForceFG is FALSE\n");
400 The system restricts which processes can set the foreground window. A process
401 can set the foreground window only if one of the following conditions is true:
403 * The process is the foreground process.
404 * The process was started by the foreground process.
405 * The process received the last input event.
406 * There is no foreground process.
407 * The foreground process is being debugged.
408 * The foreground is not locked (see LockSetForegroundWindow).
409 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
410 * No menus are active.
414 co_IntSetForegroundAndFocusWindow(
416 _In_ BOOL MouseActivate
)
418 HWND hWnd
= Wnd
? UserHMGetHandle(Wnd
) : NULL
;
419 HWND hWndPrev
= NULL
;
420 PWND pWndPrev
= NULL
;
421 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
423 BOOL fgRet
= FALSE
, Ret
= FALSE
;
425 if (Wnd
) ASSERT_REFS_CO(Wnd
);
427 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
429 PrevForegroundQueue
= IntGetFocusMessageQueue(); // Use this active desktop.
430 pti
= PsGetCurrentThreadWin32Thread();
432 if (PrevForegroundQueue
)
433 { // Same Window Q as foreground just do active.
434 if (Wnd
&& Wnd
->head
.pti
->MessageQueue
== PrevForegroundQueue
)
436 //ERR("Same Window Q as foreground just do active.\n");
437 if (pti
->MessageQueue
== PrevForegroundQueue
)
438 { // Same WQ and TQ go active.
439 //ERR("Same WQ and TQ go active.\n");
440 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
442 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
443 { // Same WQ and it is active.
444 //ERR("Same WQ and it is active.\n");
448 { // Same WQ as FG but not the same TQ send active.
449 //ERR("Same WQ as FG but not the same TQ send active.\n");
450 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
456 hWndPrev
= PrevForegroundQueue
->spwndActive
? UserHMGetHandle(PrevForegroundQueue
->spwndActive
) : 0;
457 pWndPrev
= PrevForegroundQueue
->spwndActive
;
460 if ( (( !IsFGLocked() || pti
->ppi
== gppiInputProvider
) &&
461 ( CanForceFG(pti
->ppi
) || pti
->TIF_flags
& (TIF_SYSTEMTHREAD
|TIF_CSRSSTHREAD
|TIF_ALLOWFOREGROUNDACTIVATE
) )) ||
462 pti
->ppi
== ppiScrnSaver
466 //ToggleFGActivate(pti); // win.c line 2662 fail
469 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
470 gptiForeground
= Wnd
->head
.pti
;
471 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
475 IntSetFocusMessageQueue(NULL
);
476 gptiForeground
= NULL
;
477 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
481 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
482 other thread after we already changed the foreground window back to our own
486 FindRemoveAsyncMsg(Wnd
, 0); // Do this to fix test_SFW todos!
491 // Fix FG Bounce with regedit.
492 if (hWndPrev
!= hWnd
)
494 if (PrevForegroundQueue
&&
496 PrevForegroundQueue
->spwndActive
)
498 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
499 if (pti
->MessageQueue
== PrevForegroundQueue
)
501 //ERR("SFGW: TI same as Prev TI\n");
502 co_IntSetActiveWindow(NULL
, FALSE
, TRUE
, FALSE
);
506 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
507 // No real reason to wait here.
508 co_IntSendMessageNoWait(hWndPrev
, WM_ASYNC_SETACTIVEWINDOW
, 0, 0 );
513 if (!Wnd
) return FALSE
; // Always return false.
515 if (pti
->MessageQueue
== Wnd
->head
.pti
->MessageQueue
)
517 //ERR("Same PQ and WQ go active.\n");
518 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
520 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
522 //ERR("Same Active and Wnd.\n");
527 //ERR("Activate Not same PQ and WQ and Wnd.\n");
528 co_IntSendMessageNoWait(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
535 Revision 7888, activate modal dialog when clicking on a disabled window.
538 IntFindChildWindowToOwner(PWND Root
, PWND Owner
)
541 PWND Child
, OwnerWnd
;
543 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
545 OwnerWnd
= Child
->spwndOwner
;
549 if(OwnerWnd
== Owner
)
559 co_IntMouseActivateWindow(PWND Wnd
)
562 USER_REFERENCE_ENTRY Ref
;
565 if (Wnd
->style
& WS_DISABLED
)
569 PWND DesktopWindow
= UserGetDesktopWindow();
572 ERR("Window Diabled\n");
573 Top
= IntFindChildWindowToOwner(DesktopWindow
, Wnd
);
574 if ((TopWnd
= ValidateHwndNoErr(Top
)))
576 UserRefObjectCo(TopWnd
, &Ref
);
577 Ret
= co_IntMouseActivateWindow(TopWnd
);
578 UserDerefObjectCo(TopWnd
);
585 ERR("Mouse Active\n");
586 co_IntSetForegroundAndFocusWindow(Wnd
, TRUE
);
591 co_IntSetActiveWindow(PWND Wnd OPTIONAL
, BOOL bMouse
, BOOL bFocus
, BOOL Async
)
594 PUSER_MESSAGE_QUEUE ThreadQueue
;
595 PWND pWndChg
, WndPrev
; // State changes.
599 CBTACTIVATESTRUCT cbt
;
600 //ERR("co_IntSetActiveWindow 1\n");
604 hWnd
= UserHMGetHandle(Wnd
);
605 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
606 if (Wnd
== UserGetDesktopWindow()) return FALSE
;
607 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
610 //ERR("co_IntSetActiveWindow 2\n");
611 pti
= PsGetCurrentThreadWin32Thread();
612 ThreadQueue
= pti
->MessageQueue
;
613 ASSERT(ThreadQueue
!= 0);
615 hWndPrev
= ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : NULL
;
617 pWndChg
= ThreadQueue
->spwndActive
; // Keep to notify of a preemptive switch.
621 BOOL Ret
, DoFG
, AllowFG
;
623 if (Wnd
->state
& WNDS_BEINGACTIVATED
) return TRUE
;
625 if (ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
627 if (IsAllowedFGActive(pti
, Wnd
))
633 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
636 AllowFG
= !pti
->cVisWindows
; // Nothing is visable.
637 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
639 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
641 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
642 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
643 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
644 if (!gpqForeground
|| gpqForeground
== ThreadQueue
)
652 if (pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
|| pti
->cVisWindows
)
659 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
664 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
665 //ERR("co_IntSetActiveWindow 3c FG set\n");
666 Ret
= co_IntSetForegroundAndFocusWindow(Wnd
, bMouse
);
669 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
673 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
679 /* Call CBT hook chain */
681 cbt
.hWndActive
= hWndPrev
;
682 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
684 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
688 if ( ThreadQueue
->spwndActive
&& ThreadQueue
->spwndActive
->state
& WNDS_DESTROYED
)
689 ThreadQueue
->spwndActive
= NULL
;
691 ThreadQueue
->spwndActivePrev
= ThreadQueue
->spwndActive
;
693 WndPrev
= ThreadQueue
->spwndActive
; // Keep to save changing active.
697 if (ThreadQueue
== gpqForeground
) gpqForegroundPrev
= ThreadQueue
;
698 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev
), hWnd
)) return FALSE
;
701 WndPrev
= ThreadQueue
->spwndActive
; // Again keep to save changing active.
703 // While in calling message proc or hook:
704 // Fail if a preemptive switch was made, current active not made previous,
705 // focus window is dead or no longer the same thread queue.
706 if ( ThreadQueue
->spwndActivePrev
!= ThreadQueue
->spwndActive
||
707 pWndChg
!= WndPrev
||
708 (Wnd
&& !VerifyWnd(Wnd
)) ||
709 ThreadQueue
!= pti
->MessageQueue
)
711 ERR("SetActiveWindow: Summery ERROR, active state changed!\n");
715 if (!WndPrev
) ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
717 if (Wnd
) Wnd
->state
|= WNDS_BEINGACTIVATED
;
719 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND
, Wnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
720 //// Breaks Atl-Esc/Tab via User32.
721 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
723 /* check if the specified window can be set in the input data of a given queue */
724 if ( !Wnd
|| ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
726 /* set the current thread active window */
727 ThreadQueue
->spwndActive
= Wnd
;
730 WndPrev
= VerifyWnd(ThreadQueue
->spwndActivePrev
); // Now should be set but verify it again.
732 InAAPM
= co_IntSendActivateMessages(WndPrev
, Wnd
, bMouse
, Async
);
734 /* now change focus if necessary */
735 //// Fixes CORE-6452 allows setting focus on window.
736 if (bFocus
&& !(ThreadQueue
->QF_flags
& QF_FOCUSNULLSINCEACTIVE
))
738 /* Do not change focus if the window is no longer active */
739 if (pti
->MessageQueue
->spwndActive
!= IntGetNonChildAncestor(pti
->MessageQueue
->spwndFocus
))
741 PWND pWndSend
= pti
->MessageQueue
->spwndActive
;
742 // Clear focus if the active window is minimized.
743 if (pWndSend
&& pti
->MessageQueue
->spwndActive
->style
& WS_MINIMIZE
) pWndSend
= NULL
;
744 // Send focus messages and if so, set the focus.
745 IntSendFocusMessages( pti
, pWndSend
);
751 pti
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
754 // FIXME: Used in the menu loop!!!
755 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
757 //ERR("co_IntSetActiveWindow Exit\n");
758 if (Wnd
) Wnd
->state
&= ~WNDS_BEINGACTIVATED
;
759 return (ThreadQueue
->spwndActive
== Wnd
);
763 UserSetActiveWindow(PWND Wnd
)
765 if (Wnd
) // Must have a window!
767 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
769 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
772 Yes your eye are not deceiving you~!
774 First part of wines Win.c test_SetActiveWindow:
776 flush_events( TRUE );
777 ShowWindow(hwnd, SW_HIDE);
780 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
787 co_UserSetFocus(PWND Window
)
792 PUSER_MESSAGE_QUEUE ThreadQueue
;
795 ASSERT_REFS_CO(Window
);
797 pti
= PsGetCurrentThreadWin32Thread();
798 ThreadQueue
= pti
->MessageQueue
;
799 ASSERT(ThreadQueue
!= 0);
801 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window
? UserHMGetHandle(Window
) : 0, pti
);
803 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
807 if (hWndPrev
== UserHMGetHandle(Window
))
809 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0; /* Nothing to do */
812 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
814 ERR("SetFocus Must have the same Q!\n");
818 /* Check if we can set the focus to this window */
819 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720...
820 for (pwndTop
= Window
; pwndTop
; pwndTop
= pwndTop
->spwndParent
)
822 if (pwndTop
->style
& (WS_MINIMIZED
|WS_DISABLED
)) return 0;
823 if ((pwndTop
->style
& (WS_POPUP
|WS_CHILD
)) != WS_CHILD
) break;
826 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->head
.h
, (LPARAM
)hWndPrev
))
828 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
832 /* Activate pwndTop if needed. */
833 if (pwndTop
!= ThreadQueue
->spwndActive
)
835 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue(); // Keep it based on desktop.
836 if (ThreadQueue
!= ForegroundQueue
&& IsAllowedFGActive(pti
, pwndTop
)) // Rule 2 & 3.
838 //ERR("SetFocus: Set Foreground!\n");
839 if (!(pwndTop
->style
& WS_VISIBLE
))
841 pti
->ppi
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
;
843 if (!co_IntSetForegroundAndFocusWindow(pwndTop
, FALSE
))
845 ERR("SetFocus: Set Foreground and Focus Failed!\n");
850 /* Set Active when it is needed. */
851 if (pwndTop
!= ThreadQueue
->spwndActive
)
853 //ERR("SetFocus: Set Active!\n");
854 if (!co_IntSetActiveWindow(pwndTop
, FALSE
, FALSE
, FALSE
))
856 ERR("SetFocus: Set Active Failed!\n");
861 /* Abort if window destroyed */
862 if (Window
->state2
& WNDS2_INDESTROY
) return 0;
863 /* Do not change focus if the window is no longer active */
864 if (pwndTop
!= ThreadQueue
->spwndActive
)
866 ERR("SetFocus: Top window did not go active!\n");
871 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
872 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
874 IntSendFocusMessages( pti
, Window
);
876 TRACE("Focus: %p -> %p\n", hWndPrev
, Window
->head
.h
);
878 else /* NULL hwnd passed in */
880 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
882 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
886 /* set the current thread focus window null */
887 IntSendFocusMessages( pti
, NULL
);
889 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0;
893 UserGetForegroundWindow(VOID
)
895 PUSER_MESSAGE_QUEUE ForegroundQueue
;
897 ForegroundQueue
= IntGetFocusMessageQueue();
898 return( ForegroundQueue
? (ForegroundQueue
->spwndActive
? UserHMGetHandle(ForegroundQueue
->spwndActive
) : 0) : 0);
901 HWND FASTCALL
UserGetActiveWindow(VOID
)
904 PUSER_MESSAGE_QUEUE ThreadQueue
;
906 pti
= PsGetCurrentThreadWin32Thread();
907 ThreadQueue
= pti
->MessageQueue
;
908 return( ThreadQueue
? (ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : 0) : 0);
915 PUSER_MESSAGE_QUEUE ThreadQueue
;
916 DECLARE_RETURN(HWND
);
918 TRACE("Enter IntGetCapture\n");
920 pti
= PsGetCurrentThreadWin32Thread();
921 ThreadQueue
= pti
->MessageQueue
;
922 RETURN( ThreadQueue
? (ThreadQueue
->spwndCapture
? UserHMGetHandle(ThreadQueue
->spwndCapture
) : 0) : 0);
925 TRACE("Leave IntGetCapture, ret=%p\n", _ret_
);
930 co_UserSetCapture(HWND hWnd
)
933 PUSER_MESSAGE_QUEUE ThreadQueue
;
934 PWND pWnd
, Window
= NULL
;
937 pti
= PsGetCurrentThreadWin32Thread();
938 ThreadQueue
= pti
->MessageQueue
;
940 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
)
943 if (hWnd
&& (Window
= UserGetWindowObject(hWnd
)))
945 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
951 hWndPrev
= MsqSetStateWindow(pti
, MSQ_STATE_CAPTURE
, hWnd
);
955 pWnd
= UserGetWindowObject(hWndPrev
);
957 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND
, pWnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
961 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART
, Window
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
963 if (hWndPrev
&& hWndPrev
!= hWnd
)
965 if (ThreadQueue
->MenuOwner
&& Window
) ThreadQueue
->QF_flags
|= QF_CAPTURELOCKED
;
967 //co_IntPostOrSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
968 co_IntSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
970 ThreadQueue
->QF_flags
&= ~QF_CAPTURELOCKED
;
973 ThreadQueue
->spwndCapture
= Window
;
975 if (hWnd
== NULL
) // Release mode.
979 /* Also remove other windows if not capturing anymore */
980 MsqSetStateWindow(pti
, MSQ_STATE_MENUOWNER
, NULL
);
981 MsqSetStateWindow(pti
, MSQ_STATE_MOVESIZE
, NULL
);
983 /* Somebody may have missed some mouse movements */
987 mi
.dwFlags
= MOUSEEVENTF_MOVE
;
990 UserSendMouseInput(&mi
, FALSE
);
1000 IntReleaseCapture(VOID
)
1003 PUSER_MESSAGE_QUEUE ThreadQueue
;
1005 pti
= PsGetCurrentThreadWin32Thread();
1006 ThreadQueue
= pti
->MessageQueue
;
1008 // Can not release inside WM_CAPTURECHANGED!!
1009 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
) return FALSE
;
1011 co_UserSetCapture(NULL
);
1020 co_IntSetForegroundWindow(PWND Window
)
1022 if (Window
) ASSERT_REFS_CO(Window
);
1024 return co_IntSetForegroundAndFocusWindow(Window
, FALSE
);
1031 co_IntSetForegroundWindowMouse(PWND Window
)
1033 if (Window
) ASSERT_REFS_CO(Window
);
1035 return co_IntSetForegroundAndFocusWindow(Window
, TRUE
);
1042 IntLockSetForegroundWindow(UINT uLockCode
)
1044 ULONG Err
= ERROR_ACCESS_DENIED
;
1045 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
1049 if ( CanForceFG(ppi
) && !gppiLockSFW
)
1056 if ( gppiLockSFW
== ppi
)
1063 Err
= ERROR_INVALID_PARAMETER
;
1065 EngSetLastError(Err
);
1073 IntAllowSetForegroundWindow(DWORD dwProcessId
)
1075 PPROCESSINFO ppi
, ppiCur
;
1076 PEPROCESS Process
= NULL
;
1079 if (dwProcessId
!= ASFW_ANY
)
1081 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE
)dwProcessId
, &Process
)))
1083 EngSetLastError(ERROR_INVALID_PARAMETER
);
1086 ppi
= PsGetProcessWin32Process(Process
);
1089 ObDereferenceObject(Process
);
1093 ppiCur
= PsGetCurrentProcessWin32Process();
1094 if (!CanForceFG(ppiCur
))
1096 if (Process
) ObDereferenceObject(Process
);
1097 EngSetLastError(ERROR_ACCESS_DENIED
);
1100 if (dwProcessId
== ASFW_ANY
)
1101 { // All processes will be enabled to set the foreground window.
1102 //ERR("ptiLastInput is CLEARED!!\n");
1103 ptiLastInput
= NULL
;
1106 { // Rule #3, last input event in force.
1107 ERR("ptiLastInput is SET!!\n");
1108 //ptiLastInput = ppi->ptiList; // See CORE-6384 & CORE-7030.
1109 ObDereferenceObject(Process
);
1118 NtUserGetForegroundWindow(VOID
)
1120 DECLARE_RETURN(HWND
);
1122 TRACE("Enter NtUserGetForegroundWindow\n");
1123 UserEnterExclusive();
1125 RETURN( UserGetForegroundWindow());
1128 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_
);
1134 NtUserSetActiveWindow(HWND hWnd
)
1136 USER_REFERENCE_ENTRY Ref
;
1139 DECLARE_RETURN(HWND
);
1141 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd
);
1142 UserEnterExclusive();
1147 if (!(Window
= UserGetWindowObject(hWnd
)))
1149 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd
);
1155 Window
->head
.pti
->MessageQueue
== gptiCurrent
->MessageQueue
)
1157 hWndPrev
= gptiCurrent
->MessageQueue
->spwndActive
? UserHMGetHandle(gptiCurrent
->MessageQueue
->spwndActive
) : NULL
;
1158 if (Window
) UserRefObjectCo(Window
, &Ref
);
1159 UserSetActiveWindow(Window
);
1160 if (Window
) UserDerefObjectCo(Window
);
1161 RETURN( hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0 );
1166 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_
);
1175 NtUserSetCapture(HWND hWnd
)
1177 DECLARE_RETURN(HWND
);
1179 TRACE("Enter NtUserSetCapture(%p)\n", hWnd
);
1180 UserEnterExclusive();
1182 RETURN( co_UserSetCapture(hWnd
));
1185 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_
);
1194 NtUserSetFocus(HWND hWnd
)
1197 USER_REFERENCE_ENTRY Ref
;
1198 DECLARE_RETURN(HWND
);
1201 TRACE("Enter NtUserSetFocus(%p)\n", hWnd
);
1202 UserEnterExclusive();
1206 if (!(Window
= UserGetWindowObject(hWnd
)))
1208 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd
);
1212 UserRefObjectCo(Window
, &Ref
);
1213 ret
= co_UserSetFocus(Window
);
1214 UserDerefObjectCo(Window
);
1220 RETURN( co_UserSetFocus(0));
1224 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_
);