2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Focus functions
5 * FILE: subsystems/win32/win32k/ntuser/focus.c
6 * PROGRAMER: ReactOS Team
10 DBG_DEFAULT_CHANNEL(UserFocus
);
12 PUSER_MESSAGE_QUEUE gpqForeground
= NULL
;
13 PUSER_MESSAGE_QUEUE gpqForegroundPrev
= NULL
;
14 PTHREADINFO gptiForeground
= NULL
;
15 PPROCESSINFO gppiLockSFW
= NULL
;
16 ULONG guSFWLockCount
= 0; // Rule #8, No menus are active. So should be zero.
17 PTHREADINFO ptiLastInput
= NULL
;
20 Check locking of a process or one or more menus are active.
25 return (gppiLockSFW
|| guSFWLockCount
);
29 IntGetCaptureWindow(VOID
)
31 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue();
32 return ( ForegroundQueue
? (ForegroundQueue
->spwndCapture
? UserHMGetHandle(ForegroundQueue
->spwndCapture
) : 0) : 0);
36 IntGetThreadFocusWindow(VOID
)
39 PUSER_MESSAGE_QUEUE ThreadQueue
;
41 pti
= PsGetCurrentThreadWin32Thread();
42 ThreadQueue
= pti
->MessageQueue
;
45 return ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
49 co_IntSendDeactivateMessages(HWND hWndPrev
, HWND hWnd
)
51 USER_REFERENCE_ENTRY RefPrev
;
55 if (hWndPrev
&& (WndPrev
= ValidateHwndNoErr(hWndPrev
)))
57 UserRefObjectCo(WndPrev
, &RefPrev
);
59 if (co_IntSendMessageNoWait(hWndPrev
, WM_NCACTIVATE
, FALSE
, 0)) //(LPARAM)hWnd))
61 co_IntSendMessageNoWait(hWndPrev
, WM_ACTIVATE
,
62 MAKEWPARAM(WA_INACTIVE
, WndPrev
->style
& WS_MINIMIZE
),
66 WndPrev
->state
&= ~WNDS_ACTIVEFRAME
;
70 ERR("Application is keeping itself Active to prevent the change!\n");
74 UserDerefObjectCo(WndPrev
);
80 co_IntMakeWindowActive(PWND Window
)
83 if (VerifyWnd(Window
))
84 { // Set last active for window and it's owner.
86 while (spwndOwner
->spwndOwner
)
88 spwndOwner
= spwndOwner
->spwndOwner
;
90 spwndOwner
->spwndLastActive
= Window
;
93 ERR("MakeWindowActive Failed!\n");
98 co_IntSendActivateMessages(PWND WindowPrev
, PWND Window
, BOOL MouseActivate
, BOOL Async
)
100 USER_REFERENCE_ENTRY Ref
, RefPrev
;
101 HANDLE OldTID
, NewTID
;
102 PTHREADINFO pti
, ptiOld
, ptiNew
;
105 //ERR("SendActivateMessages\n");
107 pti
= PsGetCurrentThreadWin32Thread();
111 UserRefObjectCo(Window
, &Ref
);
113 if (WindowPrev
) UserRefObjectCo(WindowPrev
, &RefPrev
);
115 /* Send palette messages */
116 if (gpsi
->PUSIFlags
& PUSIF_PALETTEDISPLAY
&&
117 //co_IntPostOrSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0))
118 co_IntSendMessage(UserHMGetHandle(Window
), WM_QUERYNEWPALETTE
, 0, 0))
120 UserSendNotifyMessage( HWND_BROADCAST
,
121 WM_PALETTEISCHANGING
,
122 (WPARAM
)UserHMGetHandle(Window
),
125 //// Fixes CORE-6434.
126 if (!(Window
->style
& WS_CHILD
))
128 PWND pwndTemp
= co_GetDesktopWindow(Window
)->spwndChild
;
130 while (pwndTemp
&& !(pwndTemp
->style
& WS_VISIBLE
)) pwndTemp
= pwndTemp
->spwndNext
;
132 if (Window
!= pwndTemp
|| (WindowPrev
&& !IntIsWindowVisible(WindowPrev
)))
134 if (!Async
|| pti
->MessageQueue
== gpqForeground
)
136 UINT flags
= SWP_NOSIZE
| SWP_NOMOVE
;
137 if (Window
== pwndTemp
) flags
|= SWP_NOACTIVATE
;
138 //ERR("co_IntSendActivateMessages SetWindowPos! Async %d pti Q == FGQ %d\n",Async,pti->MessageQueue == gpqForeground);
139 co_WinPosSetWindowPos(Window
, HWND_TOP
, 0, 0, 0, 0, flags
);
144 //// CORE-1161 and CORE-6651
145 if (Window
->spwndPrev
)
147 HWND
*phwndTopLevel
, *phwndCurrent
;
148 PWND pwndCurrent
, pwndDesktop
;
150 pwndDesktop
= UserGetDesktopWindow();
151 if (Window
->spwndParent
== pwndDesktop
)
153 phwndTopLevel
= IntWinListChildren(pwndDesktop
);
154 phwndCurrent
= phwndTopLevel
;
157 pwndCurrent
= UserGetWindowObject(*phwndCurrent
);
159 if (pwndCurrent
&& pwndCurrent
->spwndOwner
== Window
)
161 co_WinPosSetWindowPos(pwndCurrent
, HWND_TOP
, 0, 0, 0, 0, SWP_NOSIZE
|SWP_NOMOVE
|SWP_NOACTIVATE
);
165 ExFreePool(phwndTopLevel
);
170 OldTID
= WindowPrev
? IntGetWndThreadId(WindowPrev
) : NULL
;
171 NewTID
= Window
? IntGetWndThreadId(Window
) : NULL
;
172 ptiOld
= WindowPrev
? WindowPrev
->head
.pti
: NULL
;
173 ptiNew
= Window
? Window
->head
.pti
: NULL
;
175 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID);
177 if (!(pti
->TIF_flags
& TIF_INACTIVATEAPPMSG
) &&
178 (!WindowPrev
|| OldTID
!= NewTID
) )
183 List
= IntWinListChildren(UserGetDesktopWindow());
188 ptiOld
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
189 // Note: Do not set pci flags, this does crash!
190 for (phWnd
= List
; *phWnd
; ++phWnd
)
192 cWindow
= ValidateHwndNoErr(*phWnd
);
193 if (cWindow
&& cWindow
->head
.pti
== ptiOld
)
194 { // FALSE if the window is being deactivated,
195 // ThreadId that owns the window being activated.
196 //ERR("SendActivateMessage Old\n");
197 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
200 ptiOld
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
203 { //// Prevents a resource crash due to reentrance!
205 pti
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
207 for (phWnd
= List
; *phWnd
; ++phWnd
)
209 cWindow
= ValidateHwndNoErr(*phWnd
);
210 if (cWindow
&& cWindow
->head
.pti
== ptiNew
)
211 { // TRUE if the window is being activated,
212 // ThreadId that owns the window being deactivated.
213 //ERR("SendActivateMessage New\n");
214 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
218 ExFreePoolWithTag(List
, USERTAG_WINDOWLIST
);
225 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
227 if (Window
->state
& WNDS_ACTIVEFRAME
)
228 { // If already active frame do not allow NCPaint.
229 //ERR("SendActivateMessage Is Active Frame!\n");
230 Window
->state
|= WNDS_NONCPAINT
;
233 if (Window
->style
& WS_MINIMIZE
)
235 TRACE("Widow was minimized\n");
238 co_IntMakeWindowActive(Window
);
240 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
242 (WPARAM
)(Window
== (gpqForeground
? gpqForeground
->spwndActive
: NULL
)),
243 0); //(LPARAM)hWndPrev);
245 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
247 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
, Window
->style
& WS_MINIMIZE
),
248 (LPARAM
)(WindowPrev
? UserHMGetHandle(WindowPrev
) : 0));
250 if (!Window
->spwndOwner
&& !IntGetParent(Window
))
252 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
253 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (WPARAM
) UserHMGetHandle(Window
), FALSE
);
256 Window
->state
&= ~WNDS_NONCPAINT
;
258 UserDerefObjectCo(Window
);
264 IntSendFocusMessages( PTHREADINFO pti
, PWND pWnd
)
267 PUSER_MESSAGE_QUEUE ThreadQueue
= pti
->MessageQueue
; // Queue can change...
269 ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
270 if (!pWnd
&& ThreadQueue
->spwndActive
)
272 ThreadQueue
->QF_flags
|= QF_FOCUSNULLSINCEACTIVE
;
275 pWndPrev
= ThreadQueue
->spwndFocus
;
277 /* check if the specified window can be set in the input data of a given queue */
278 if (!pWnd
|| ThreadQueue
== pWnd
->head
.pti
->MessageQueue
)
279 /* set the current thread focus window */
280 ThreadQueue
->spwndFocus
= pWnd
;
286 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
287 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, (WPARAM
)UserHMGetHandle(pWnd
), 0);
289 if (ThreadQueue
->spwndFocus
== pWnd
)
291 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, pWnd
, OBJID_CLIENT
, CHILDID_SELF
, 0);
292 //co_IntPostOrSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)(pWndPrev ? UserHMGetHandle(pWndPrev) : NULL), 0);
293 co_IntSendMessage(UserHMGetHandle(pWnd
), WM_SETFOCUS
, (WPARAM
)(pWndPrev
? UserHMGetHandle(pWndPrev
) : NULL
), 0);
300 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, NULL
, OBJID_CLIENT
, CHILDID_SELF
, 0);
301 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
302 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, 0, 0);
308 FindRemoveAsyncMsg(PWND Wnd
, WPARAM wParam
)
311 PUSER_SENT_MESSAGE Message
;
318 Entry
= pti
->SentMessagesListHead
.Flink
;
319 while (Entry
!= &pti
->SentMessagesListHead
)
321 // Scan sent queue messages to see if we received async messages.
322 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
323 Entry
= Entry
->Flink
;
325 if (Message
->Msg
.message
== WM_ASYNC_SETACTIVEWINDOW
&&
326 Message
->Msg
.hwnd
== UserHMGetHandle(Wnd
) &&
327 Message
->Msg
.wParam
== wParam
)
329 ERR("ASYNC SAW: Found one in the Sent Msg Queue! %p Activate/Deactivate %d\n", Message
->Msg
.hwnd
, !!wParam
);
330 RemoveEntryList(&Message
->ListEntry
); // Purge the entry.
331 ClearMsgBitsMask(pti
, Message
->QS_Flags
);
332 ExFreePoolWithTag(Message
, TAG_USRMSG
);
338 ToggleFGActivate(PTHREADINFO pti
)
341 PPROCESSINFO ppi
= pti
->ppi
;
343 Ret
= !!(pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
);
346 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
349 Ret
= !!(ppi
->W32PF_flags
& W32PF_ALLOWFOREGROUNDACTIVATE
);
352 ppi
->W32PF_flags
&= ~W32PF_ALLOWFOREGROUNDACTIVATE
;
353 //ERR("ToggleFGActivate is %d\n",Ret);
358 IsAllowedFGActive(PTHREADINFO pti
, PWND Wnd
)
360 // Not allowed if one or more,,
361 if (!ToggleFGActivate(pti
) || // bits not set,
362 pti
->rpdesk
!= gpdeskInputDesktop
|| // not current Desktop,
363 pti
->MessageQueue
== gpqForeground
|| // if already the queue foreground,
364 IsFGLocked() || // foreground is locked,
365 Wnd
->ExStyle
& WS_EX_NOACTIVATE
) // or,,, does not become the foreground window when the user clicks it.
369 //ERR("IsAllowedFGActive is TRUE\n");
374 Can the system force foreground from one or more conditions.
377 CanForceFG(PPROCESSINFO ppi
)
380 ptiLastInput
->ppi
== ppi
||
382 gptiForeground
->ppi
== ppi
||
383 ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_SETFOREGROUNDALLOWED
) ||
384 gppiInputProvider
== ppi
||
387 //ERR("CanForceFG is FALSE\n");
393 The system restricts which processes can set the foreground window. A process
394 can set the foreground window only if one of the following conditions is true:
396 * The process is the foreground process.
397 * The process was started by the foreground process.
398 * The process received the last input event.
399 * There is no foreground process.
400 * The foreground process is being debugged.
401 * The foreground is not locked (see LockSetForegroundWindow).
402 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
403 * No menus are active.
407 co_IntSetForegroundAndFocusWindow(
409 _In_ BOOL MouseActivate
)
411 HWND hWnd
= Wnd
? UserHMGetHandle(Wnd
) : NULL
;
412 HWND hWndPrev
= NULL
;
413 PWND pWndPrev
= NULL
;
414 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
416 BOOL fgRet
= FALSE
, Ret
= FALSE
;
418 if (Wnd
) ASSERT_REFS_CO(Wnd
);
420 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
422 PrevForegroundQueue
= IntGetFocusMessageQueue(); // Use this active desktop.
423 pti
= PsGetCurrentThreadWin32Thread();
425 if (PrevForegroundQueue
)
426 { // Same Window Q as foreground just do active.
427 if (Wnd
&& Wnd
->head
.pti
->MessageQueue
== PrevForegroundQueue
)
429 //ERR("Same Window Q as foreground just do active.\n");
430 if (pti
->MessageQueue
== PrevForegroundQueue
)
431 { // Same WQ and TQ go active.
432 //ERR("Same WQ and TQ go active.\n");
433 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
435 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
436 { // Same WQ and it is active.
437 //ERR("Same WQ and it is active.\n");
441 { // Same WQ as FG but not the same TQ send active.
442 //ERR("Same WQ as FG but not the same TQ send active.\n");
443 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
449 hWndPrev
= PrevForegroundQueue
->spwndActive
? UserHMGetHandle(PrevForegroundQueue
->spwndActive
) : 0;
450 pWndPrev
= PrevForegroundQueue
->spwndActive
;
453 if ( (( !IsFGLocked() || pti
->ppi
== gppiInputProvider
) &&
454 ( CanForceFG(pti
->ppi
) || pti
->TIF_flags
& (TIF_SYSTEMTHREAD
|TIF_CSRSSTHREAD
|TIF_ALLOWFOREGROUNDACTIVATE
) )) ||
455 pti
->ppi
== ppiScrnSaver
459 //ToggleFGActivate(pti); // win.c line 2662 fail
462 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
463 gptiForeground
= Wnd
->head
.pti
;
464 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
468 IntSetFocusMessageQueue(NULL
);
469 gptiForeground
= NULL
;
470 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
474 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
475 other thread after we already changed the foreground window back to our own
479 FindRemoveAsyncMsg(Wnd
, 0); // Do this to fix test_SFW todos!
484 // Fix FG Bounce with regedit.
485 if (hWndPrev
!= hWnd
)
487 if (PrevForegroundQueue
&&
489 PrevForegroundQueue
->spwndActive
)
491 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
492 if (pti
->MessageQueue
== PrevForegroundQueue
)
494 //ERR("SFGW: TI same as Prev TI\n");
495 co_IntSetActiveWindow(NULL
, FALSE
, TRUE
, FALSE
);
499 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
500 // No real reason to wait here.
501 co_IntSendMessageNoWait(hWndPrev
, WM_ASYNC_SETACTIVEWINDOW
, 0, 0 );
506 if (!Wnd
) return FALSE
; // Always return false.
508 if (pti
->MessageQueue
== Wnd
->head
.pti
->MessageQueue
)
510 //ERR("Same PQ and WQ go active.\n");
511 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
513 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
515 //ERR("Same Active and Wnd.\n");
520 //ERR("Activate Not same PQ and WQ and Wnd.\n");
521 co_IntSendMessageNoWait(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
528 Revision 7888, activate modal dialog when clicking on a disabled window.
531 IntFindChildWindowToOwner(PWND Root
, PWND Owner
)
534 PWND Child
, OwnerWnd
;
536 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
538 OwnerWnd
= Child
->spwndOwner
;
542 if(OwnerWnd
== Owner
)
552 co_IntMouseActivateWindow(PWND Wnd
)
555 USER_REFERENCE_ENTRY Ref
;
558 if (Wnd
->style
& WS_DISABLED
)
562 PWND DesktopWindow
= UserGetDesktopWindow();
565 ERR("Window Diabled\n");
566 Top
= IntFindChildWindowToOwner(DesktopWindow
, Wnd
);
567 if ((TopWnd
= ValidateHwndNoErr(Top
)))
569 UserRefObjectCo(TopWnd
, &Ref
);
570 Ret
= co_IntMouseActivateWindow(TopWnd
);
571 UserDerefObjectCo(TopWnd
);
578 ERR("Mouse Active\n");
579 co_IntSetForegroundAndFocusWindow(Wnd
, TRUE
);
584 co_IntSetActiveWindow(PWND Wnd OPTIONAL
, BOOL bMouse
, BOOL bFocus
, BOOL Async
)
587 PUSER_MESSAGE_QUEUE ThreadQueue
;
588 PWND pWndChg
, WndPrev
; // State changes.
592 CBTACTIVATESTRUCT cbt
;
593 //ERR("co_IntSetActiveWindow 1\n");
597 hWnd
= UserHMGetHandle(Wnd
);
598 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
599 if (Wnd
== UserGetDesktopWindow()) return FALSE
;
600 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
603 //ERR("co_IntSetActiveWindow 2\n");
604 pti
= PsGetCurrentThreadWin32Thread();
605 ThreadQueue
= pti
->MessageQueue
;
606 ASSERT(ThreadQueue
!= 0);
608 hWndPrev
= ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : NULL
;
610 pWndChg
= ThreadQueue
->spwndActive
; // Keep to notify of a preemptive switch.
614 BOOL Ret
, DoFG
, AllowFG
;
616 if (Wnd
->state
& WNDS_BEINGACTIVATED
) return TRUE
;
618 if (ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
620 if (IsAllowedFGActive(pti
, Wnd
))
626 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
629 AllowFG
= !pti
->cVisWindows
; // Nothing is visable.
630 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
632 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
634 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
635 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
636 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
637 if (!gpqForeground
|| gpqForeground
== ThreadQueue
)
645 if (pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
|| pti
->cVisWindows
)
652 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
657 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
658 //ERR("co_IntSetActiveWindow 3c FG set\n");
659 Ret
= co_IntSetForegroundAndFocusWindow(Wnd
, bMouse
);
662 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
666 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
672 /* Call CBT hook chain */
674 cbt
.hWndActive
= hWndPrev
;
675 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
677 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
681 if ( ThreadQueue
->spwndActive
&& ThreadQueue
->spwndActive
->state
& WNDS_DESTROYED
)
682 ThreadQueue
->spwndActive
= NULL
;
684 ThreadQueue
->spwndActivePrev
= ThreadQueue
->spwndActive
;
686 WndPrev
= ThreadQueue
->spwndActive
; // Keep to save changing active.
690 if (ThreadQueue
== gpqForeground
) gpqForegroundPrev
= ThreadQueue
;
691 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev
), hWnd
)) return FALSE
;
694 WndPrev
= ThreadQueue
->spwndActive
; // Again keep to save changing active.
696 // While in calling message proc or hook:
697 // Fail if a preemptive switch was made, current active not made previous,
698 // focus window is dead or no longer the same thread queue.
699 if ( ThreadQueue
->spwndActivePrev
!= ThreadQueue
->spwndActive
||
700 pWndChg
!= WndPrev
||
701 (Wnd
&& !VerifyWnd(Wnd
)) ||
702 ThreadQueue
!= pti
->MessageQueue
)
704 ERR("SetActiveWindow: Summery ERROR, active state changed!\n");
708 if (!WndPrev
) ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
710 if (Wnd
) Wnd
->state
|= WNDS_BEINGACTIVATED
;
712 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND
, Wnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
713 //// Breaks Atl-Esc/Tab via User32.
714 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
716 /* check if the specified window can be set in the input data of a given queue */
717 if ( !Wnd
|| ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
719 /* set the current thread active window */
720 ThreadQueue
->spwndActive
= Wnd
;
723 WndPrev
= VerifyWnd(ThreadQueue
->spwndActivePrev
); // Now should be set but verify it again.
725 InAAPM
= co_IntSendActivateMessages(WndPrev
, Wnd
, bMouse
, Async
);
727 /* now change focus if necessary */
728 //// Fixes CORE-6452 allows setting focus on window.
729 if (bFocus
&& !(ThreadQueue
->QF_flags
& QF_FOCUSNULLSINCEACTIVE
))
731 /* Do not change focus if the window is no longer active */
732 if (pti
->MessageQueue
->spwndActive
!= IntGetNonChildAncestor(pti
->MessageQueue
->spwndFocus
))
734 PWND pWndSend
= pti
->MessageQueue
->spwndActive
;
735 // Clear focus if the active window is minimized.
736 if (pWndSend
&& pti
->MessageQueue
->spwndActive
->style
& WS_MINIMIZE
) pWndSend
= NULL
;
737 // Send focus messages and if so, set the focus.
738 IntSendFocusMessages( pti
, pWndSend
);
744 pti
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
747 // FIXME: Used in the menu loop!!!
748 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
750 //ERR("co_IntSetActiveWindow Exit\n");
751 if (Wnd
) Wnd
->state
&= ~WNDS_BEINGACTIVATED
;
752 return (ThreadQueue
->spwndActive
== Wnd
);
756 UserSetActiveWindow(PWND Wnd
)
758 if (Wnd
) // Must have a window!
760 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
762 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
765 Yes your eye are not deceiving you~!
767 First part of wines Win.c test_SetActiveWindow:
769 flush_events( TRUE );
770 ShowWindow(hwnd, SW_HIDE);
773 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
780 co_UserSetFocus(PWND Window
)
785 PUSER_MESSAGE_QUEUE ThreadQueue
;
788 ASSERT_REFS_CO(Window
);
790 pti
= PsGetCurrentThreadWin32Thread();
791 ThreadQueue
= pti
->MessageQueue
;
792 ASSERT(ThreadQueue
!= 0);
794 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window
? UserHMGetHandle(Window
) : 0, pti
);
796 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
800 if (hWndPrev
== UserHMGetHandle(Window
))
802 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0; /* Nothing to do */
805 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
807 ERR("SetFocus Must have the same Q!\n");
811 /* Check if we can set the focus to this window */
812 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720...
813 for (pwndTop
= Window
; pwndTop
; pwndTop
= pwndTop
->spwndParent
)
815 if (pwndTop
->style
& (WS_MINIMIZED
|WS_DISABLED
)) return 0;
816 if ((pwndTop
->style
& (WS_POPUP
|WS_CHILD
)) != WS_CHILD
) break;
819 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->head
.h
, (LPARAM
)hWndPrev
))
821 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
825 /* Activate pwndTop if needed. */
826 if (pwndTop
!= ThreadQueue
->spwndActive
)
828 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue(); // Keep it based on desktop.
829 if (ThreadQueue
!= ForegroundQueue
&& IsAllowedFGActive(pti
, pwndTop
)) // Rule 2 & 3.
831 //ERR("SetFocus: Set Foreground!\n");
832 if (!(pwndTop
->style
& WS_VISIBLE
))
834 pti
->ppi
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
;
836 if (!co_IntSetForegroundAndFocusWindow(pwndTop
, FALSE
))
838 ERR("SetFocus: Set Foreground and Focus Failed!\n");
843 /* Set Active when it is needed. */
844 if (pwndTop
!= ThreadQueue
->spwndActive
)
846 //ERR("SetFocus: Set Active!\n");
847 if (!co_IntSetActiveWindow(pwndTop
, FALSE
, FALSE
, FALSE
))
849 ERR("SetFocus: Set Active Failed!\n");
854 /* Abort if window destroyed */
855 if (Window
->state2
& WNDS2_INDESTROY
) return 0;
856 /* Do not change focus if the window is no longer active */
857 if (pwndTop
!= ThreadQueue
->spwndActive
)
859 ERR("SetFocus: Top window did not go active!\n");
864 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
865 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
867 IntSendFocusMessages( pti
, Window
);
869 TRACE("Focus: %p -> %p\n", hWndPrev
, Window
->head
.h
);
871 else /* NULL hwnd passed in */
873 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
875 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
879 /* set the current thread focus window null */
880 IntSendFocusMessages( pti
, NULL
);
882 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0;
886 UserGetForegroundWindow(VOID
)
888 PUSER_MESSAGE_QUEUE ForegroundQueue
;
890 ForegroundQueue
= IntGetFocusMessageQueue();
891 return( ForegroundQueue
? (ForegroundQueue
->spwndActive
? UserHMGetHandle(ForegroundQueue
->spwndActive
) : 0) : 0);
894 HWND FASTCALL
UserGetActiveWindow(VOID
)
897 PUSER_MESSAGE_QUEUE ThreadQueue
;
899 pti
= PsGetCurrentThreadWin32Thread();
900 ThreadQueue
= pti
->MessageQueue
;
901 return( ThreadQueue
? (ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : 0) : 0);
908 PUSER_MESSAGE_QUEUE ThreadQueue
;
909 DECLARE_RETURN(HWND
);
911 TRACE("Enter IntGetCapture\n");
913 pti
= PsGetCurrentThreadWin32Thread();
914 ThreadQueue
= pti
->MessageQueue
;
915 RETURN( ThreadQueue
? (ThreadQueue
->spwndCapture
? UserHMGetHandle(ThreadQueue
->spwndCapture
) : 0) : 0);
918 TRACE("Leave IntGetCapture, ret=%p\n", _ret_
);
923 co_UserSetCapture(HWND hWnd
)
926 PUSER_MESSAGE_QUEUE ThreadQueue
;
927 PWND pWnd
, Window
= NULL
;
930 pti
= PsGetCurrentThreadWin32Thread();
931 ThreadQueue
= pti
->MessageQueue
;
933 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
)
936 if (hWnd
&& (Window
= UserGetWindowObject(hWnd
)))
938 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
944 hWndPrev
= MsqSetStateWindow(pti
, MSQ_STATE_CAPTURE
, hWnd
);
948 pWnd
= UserGetWindowObject(hWndPrev
);
950 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND
, pWnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
954 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART
, Window
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
956 if (hWndPrev
&& hWndPrev
!= hWnd
)
958 if (ThreadQueue
->MenuOwner
&& Window
) ThreadQueue
->QF_flags
|= QF_CAPTURELOCKED
;
960 //co_IntPostOrSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
961 co_IntSendMessage(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_
);