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 Get capture window via foreground Queue.
32 IntGetCaptureWindow(VOID
)
34 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue();
35 return ( ForegroundQueue
? (ForegroundQueue
->spwndCapture
? UserHMGetHandle(ForegroundQueue
->spwndCapture
) : 0) : 0);
39 IntGetThreadFocusWindow(VOID
)
42 PUSER_MESSAGE_QUEUE ThreadQueue
;
44 pti
= PsGetCurrentThreadWin32Thread();
45 ThreadQueue
= pti
->MessageQueue
;
48 return ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
52 co_IntSendDeactivateMessages(HWND hWndPrev
, HWND hWnd
)
54 USER_REFERENCE_ENTRY RefPrev
;
58 if (hWndPrev
&& (WndPrev
= ValidateHwndNoErr(hWndPrev
)))
60 UserRefObjectCo(WndPrev
, &RefPrev
);
62 if (co_IntSendMessageNoWait(hWndPrev
, WM_NCACTIVATE
, FALSE
, 0)) //(LPARAM)hWnd))
64 co_IntSendMessageNoWait(hWndPrev
, WM_ACTIVATE
,
65 MAKEWPARAM(WA_INACTIVE
, (WndPrev
->style
& WS_MINIMIZE
) != 0),
69 WndPrev
->state
&= ~(WNDS_ACTIVEFRAME
|WNDS_HASCAPTION
);
73 ERR("Application is keeping itself Active to prevent the change!\n");
77 UserDerefObjectCo(WndPrev
);
83 co_IntMakeWindowActive(PWND Window
)
86 if (VerifyWnd(Window
))
87 { // Set last active for window and it's owner.
89 while (spwndOwner
->spwndOwner
)
91 spwndOwner
= spwndOwner
->spwndOwner
;
93 spwndOwner
->spwndLastActive
= Window
;
96 ERR("MakeWindowActive Failed!\n");
101 co_IntSendActivateMessages(PWND WindowPrev
, PWND Window
, BOOL MouseActivate
, BOOL Async
)
103 USER_REFERENCE_ENTRY Ref
, RefPrev
;
104 HANDLE OldTID
, NewTID
;
105 PTHREADINFO pti
, ptiOld
, ptiNew
;
108 //ERR("SendActivateMessages\n");
110 pti
= PsGetCurrentThreadWin32Thread();
114 UserRefObjectCo(Window
, &Ref
);
116 if (WindowPrev
) UserRefObjectCo(WindowPrev
, &RefPrev
);
118 /* Send palette messages */
119 if (gpsi
->PUSIFlags
& PUSIF_PALETTEDISPLAY
&&
120 //co_IntPostOrSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0))
121 co_IntSendMessage(UserHMGetHandle(Window
), WM_QUERYNEWPALETTE
, 0, 0))
123 UserSendNotifyMessage( HWND_BROADCAST
,
124 WM_PALETTEISCHANGING
,
125 (WPARAM
)UserHMGetHandle(Window
),
128 //// Fixes CORE-6434.
129 if (!(Window
->style
& WS_CHILD
))
131 PWND pwndTemp
= co_GetDesktopWindow(Window
)->spwndChild
;
133 while (pwndTemp
&& !(pwndTemp
->style
& WS_VISIBLE
)) pwndTemp
= pwndTemp
->spwndNext
;
135 if (Window
!= pwndTemp
|| (WindowPrev
&& !IntIsWindowVisible(WindowPrev
)))
137 if (!Async
|| pti
->MessageQueue
== gpqForeground
)
139 UINT flags
= SWP_NOSIZE
| SWP_NOMOVE
;
140 if (Window
== pwndTemp
) flags
|= SWP_NOACTIVATE
;
141 //ERR("co_IntSendActivateMessages SetWindowPos! Async %d pti Q == FGQ %d\n",Async,pti->MessageQueue == gpqForeground);
142 co_WinPosSetWindowPos(Window
, HWND_TOP
, 0, 0, 0, 0, flags
);
147 //// CORE-1161 and CORE-6651
148 if (Window
->spwndPrev
)
150 HWND
*phwndTopLevel
, *phwndCurrent
;
151 PWND pwndCurrent
, pwndDesktop
;
153 pwndDesktop
= co_GetDesktopWindow(Window
);//UserGetDesktopWindow();
154 if (Window
->spwndParent
== pwndDesktop
)
156 phwndTopLevel
= IntWinListChildren(pwndDesktop
);
157 phwndCurrent
= phwndTopLevel
;
160 pwndCurrent
= UserGetWindowObject(*phwndCurrent
);
162 if (pwndCurrent
&& pwndCurrent
->spwndOwner
== Window
)
164 co_WinPosSetWindowPos(pwndCurrent
, HWND_TOP
, 0, 0, 0, 0, SWP_NOSIZE
|SWP_NOMOVE
|SWP_NOACTIVATE
);
168 ExFreePoolWithTag(phwndTopLevel
, USERTAG_WINDOWLIST
);
174 OldTID
= WindowPrev
? IntGetWndThreadId(WindowPrev
) : NULL
;
175 NewTID
= Window
? IntGetWndThreadId(Window
) : NULL
;
176 ptiOld
= WindowPrev
? WindowPrev
->head
.pti
: NULL
;
177 ptiNew
= Window
? Window
->head
.pti
: NULL
;
179 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID);
181 if (!(pti
->TIF_flags
& TIF_INACTIVATEAPPMSG
) &&
187 List
= IntWinListChildren(UserGetDesktopWindow());
192 ptiOld
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
193 // Note: Do not set pci flags, this does crash!
194 for (phWnd
= List
; *phWnd
; ++phWnd
)
196 cWindow
= ValidateHwndNoErr(*phWnd
);
197 if (cWindow
&& cWindow
->head
.pti
== ptiOld
)
198 { // FALSE if the window is being deactivated,
199 // ThreadId that owns the window being activated.
200 //ERR("SendActivateMessage Old\n");
201 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
204 ptiOld
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
207 { //// Prevents a resource crash due to reentrance!
209 pti
->TIF_flags
|= TIF_INACTIVATEAPPMSG
;
211 for (phWnd
= List
; *phWnd
; ++phWnd
)
213 cWindow
= ValidateHwndNoErr(*phWnd
);
214 if (cWindow
&& cWindow
->head
.pti
== ptiNew
)
215 { // TRUE if the window is being activated,
216 // ThreadId that owns the window being deactivated.
217 //ERR("SendActivateMessage New\n");
218 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
222 ExFreePoolWithTag(List
, USERTAG_WINDOWLIST
);
229 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
231 if (Window
->state
& WNDS_ACTIVEFRAME
)
232 { // If already active frame do not allow NCPaint.
233 //ERR("SendActivateMessage Is Active Frame!\n");
234 Window
->state
|= WNDS_NONCPAINT
;
237 if (Window
->style
& WS_MINIMIZE
)
239 TRACE("Widow was minimized\n");
242 co_IntMakeWindowActive(Window
);
244 co_IntSendMessageNoWait( UserHMGetHandle(Window
),
246 (WPARAM
)(Window
== (gpqForeground
? gpqForeground
->spwndActive
: NULL
)),
247 0); //(LPARAM)hWndPrev);
249 co_IntSendMessage( UserHMGetHandle(Window
),
251 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
, (Window
->style
& WS_MINIMIZE
) != 0),
252 (LPARAM
)(WindowPrev
? UserHMGetHandle(WindowPrev
) : 0));
254 if (Window
->spwndParent
== UserGetDesktopWindow() &&
255 Window
->spwndOwner
== NULL
&&
256 (!(Window
->ExStyle
& WS_EX_TOOLWINDOW
) ||
257 (Window
->ExStyle
& WS_EX_APPWINDOW
)))
259 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
260 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (WPARAM
) UserHMGetHandle(Window
), FALSE
);
264 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, 0, FALSE
);
267 Window
->state
&= ~WNDS_NONCPAINT
;
269 UserDerefObjectCo(Window
);
275 IntSendFocusMessages( PTHREADINFO pti
, PWND pWnd
)
278 PUSER_MESSAGE_QUEUE ThreadQueue
= pti
->MessageQueue
; // Queue can change...
280 ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
281 if (!pWnd
&& ThreadQueue
->spwndActive
)
283 ThreadQueue
->QF_flags
|= QF_FOCUSNULLSINCEACTIVE
;
286 pWndPrev
= ThreadQueue
->spwndFocus
;
288 /* check if the specified window can be set in the input data of a given queue */
289 if (!pWnd
|| ThreadQueue
== pWnd
->head
.pti
->MessageQueue
)
290 /* set the current thread focus window */
291 ThreadQueue
->spwndFocus
= pWnd
;
297 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
298 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, (WPARAM
)UserHMGetHandle(pWnd
), 0);
300 if (ThreadQueue
->spwndFocus
== pWnd
)
302 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, pWnd
, OBJID_CLIENT
, CHILDID_SELF
, 0);
303 //co_IntPostOrSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)(pWndPrev ? UserHMGetHandle(pWndPrev) : NULL), 0);
304 co_IntSendMessage(UserHMGetHandle(pWnd
), WM_SETFOCUS
, (WPARAM
)(pWndPrev
? UserHMGetHandle(pWndPrev
) : NULL
), 0);
311 IntNotifyWinEvent(EVENT_OBJECT_FOCUS
, NULL
, OBJID_CLIENT
, CHILDID_SELF
, 0);
312 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
313 co_IntSendMessage(UserHMGetHandle(pWndPrev
), WM_KILLFOCUS
, 0, 0);
319 FindRemoveAsyncMsg(PWND Wnd
, WPARAM wParam
)
322 PUSER_SENT_MESSAGE Message
;
329 Entry
= pti
->SentMessagesListHead
.Flink
;
330 while (Entry
!= &pti
->SentMessagesListHead
)
332 // Scan sent queue messages to see if we received async messages.
333 Message
= CONTAINING_RECORD(Entry
, USER_SENT_MESSAGE
, ListEntry
);
334 Entry
= Entry
->Flink
;
336 if (Message
->Msg
.message
== WM_ASYNC_SETACTIVEWINDOW
&&
337 Message
->Msg
.hwnd
== UserHMGetHandle(Wnd
) &&
338 Message
->Msg
.wParam
== wParam
)
340 WARN("ASYNC SAW: Found one in the Sent Msg Queue! %p Activate/Deactivate %d\n", Message
->Msg
.hwnd
, !!wParam
);
341 RemoveEntryList(&Message
->ListEntry
); // Purge the entry.
342 ClearMsgBitsMask(pti
, Message
->QS_Flags
);
343 InsertTailList(&usmList
, &Message
->ListEntry
);
344 /* Notify the sender. */
345 if (Message
->pkCompletionEvent
!= NULL
)
347 KeSetEvent(Message
->pkCompletionEvent
, IO_NO_INCREMENT
, FALSE
);
349 FreeUserMessage(Message
);
355 ToggleFGActivate(PTHREADINFO pti
)
358 PPROCESSINFO ppi
= pti
->ppi
;
360 Ret
= !!(pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
);
363 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
366 Ret
= !!(ppi
->W32PF_flags
& W32PF_ALLOWFOREGROUNDACTIVATE
);
369 ppi
->W32PF_flags
&= ~W32PF_ALLOWFOREGROUNDACTIVATE
;
370 //ERR("ToggleFGActivate is %d\n",Ret);
375 IsAllowedFGActive(PTHREADINFO pti
, PWND Wnd
)
377 // Not allowed if one or more,,
378 if (!ToggleFGActivate(pti
) || // bits not set,
379 pti
->rpdesk
!= gpdeskInputDesktop
|| // not current Desktop,
380 pti
->MessageQueue
== gpqForeground
|| // if already the queue foreground,
381 IsFGLocked() || // foreground is locked,
382 Wnd
->ExStyle
& WS_EX_NOACTIVATE
) // or,,, does not become the foreground window when the user clicks it.
386 //ERR("IsAllowedFGActive is TRUE\n");
391 Can the system force foreground from one or more conditions.
394 CanForceFG(PPROCESSINFO ppi
)
397 ptiLastInput
->ppi
== ppi
||
399 gptiForeground
->ppi
== ppi
||
400 ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_SETFOREGROUNDALLOWED
) ||
401 gppiInputProvider
== ppi
||
404 //ERR("CanForceFG is FALSE\n");
410 The system restricts which processes can set the foreground window. A process
411 can set the foreground window only if one of the following conditions is true:
413 * The process is the foreground process.
414 * The process was started by the foreground process.
415 * The process received the last input event.
416 * There is no foreground process.
417 * The foreground process is being debugged.
418 * The foreground is not locked (see LockSetForegroundWindow).
419 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
420 * No menus are active.
424 co_IntSetForegroundAndFocusWindow(
426 _In_ BOOL MouseActivate
)
428 HWND hWnd
= Wnd
? UserHMGetHandle(Wnd
) : NULL
;
429 HWND hWndPrev
= NULL
;
430 PWND pWndPrev
= NULL
;
431 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
433 BOOL fgRet
= FALSE
, Ret
= FALSE
;
435 if (Wnd
) ASSERT_REFS_CO(Wnd
);
437 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
439 PrevForegroundQueue
= IntGetFocusMessageQueue(); // Use this active desktop.
440 pti
= PsGetCurrentThreadWin32Thread();
442 if (PrevForegroundQueue
)
443 { // Same Window Q as foreground just do active.
444 if (Wnd
&& Wnd
->head
.pti
->MessageQueue
== PrevForegroundQueue
)
446 //ERR("Same Window Q as foreground just do active.\n");
447 if (pti
->MessageQueue
== PrevForegroundQueue
)
448 { // Same WQ and TQ go active.
449 //ERR("Same WQ and TQ go active.\n");
450 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
452 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
453 { // Same WQ and it is active.
454 //ERR("Same WQ and it is active.\n");
458 { // Same WQ as FG but not the same TQ send active.
459 //ERR("Same WQ as FG but not the same TQ send active.\n");
460 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
466 hWndPrev
= PrevForegroundQueue
->spwndActive
? UserHMGetHandle(PrevForegroundQueue
->spwndActive
) : 0;
467 pWndPrev
= PrevForegroundQueue
->spwndActive
;
470 if ( (( !IsFGLocked() || pti
->ppi
== gppiInputProvider
) &&
471 ( CanForceFG(pti
->ppi
) || pti
->TIF_flags
& (TIF_SYSTEMTHREAD
|TIF_CSRSSTHREAD
|TIF_ALLOWFOREGROUNDACTIVATE
) )) ||
472 pti
->ppi
== ppiScrnSaver
476 //ToggleFGActivate(pti); // win.c line 2662 fail
479 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
480 gptiForeground
= Wnd
->head
.pti
;
481 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
485 IntSetFocusMessageQueue(NULL
);
486 gptiForeground
= NULL
;
487 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
491 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
492 other thread after we already changed the foreground window back to our own
496 FindRemoveAsyncMsg(Wnd
, 0); // Do this to fix test_SFW todos!
501 // Fix FG Bounce with regedit.
502 if (hWndPrev
!= hWnd
)
504 if (PrevForegroundQueue
&&
506 PrevForegroundQueue
->spwndActive
)
508 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
509 if (pti
->MessageQueue
== PrevForegroundQueue
)
511 //ERR("SFGW: TI same as Prev TI\n");
512 co_IntSetActiveWindow(NULL
, FALSE
, TRUE
, FALSE
);
516 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
517 // No real reason to wait here.
518 co_IntSendMessageNoWait(hWndPrev
, WM_ASYNC_SETACTIVEWINDOW
, 0, 0 );
523 if (!Wnd
) return FALSE
; // Always return false.
525 if (pti
->MessageQueue
== Wnd
->head
.pti
->MessageQueue
)
527 //ERR("Same PQ and WQ go active.\n");
528 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
530 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
532 //ERR("Same Active and Wnd.\n");
537 //ERR("Activate Not same PQ and WQ and Wnd.\n");
538 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
545 co_IntSetActiveWindow(PWND Wnd OPTIONAL
, BOOL bMouse
, BOOL bFocus
, BOOL Async
)
548 PUSER_MESSAGE_QUEUE ThreadQueue
;
549 PWND pWndChg
, WndPrev
; // State changes.
553 CBTACTIVATESTRUCT cbt
;
554 //ERR("co_IntSetActiveWindow 1\n");
558 hWnd
= UserHMGetHandle(Wnd
);
559 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
560 if (Wnd
== UserGetDesktopWindow()) return FALSE
;
561 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
564 //ERR("co_IntSetActiveWindow 2\n");
565 pti
= PsGetCurrentThreadWin32Thread();
566 ThreadQueue
= pti
->MessageQueue
;
567 ASSERT(ThreadQueue
!= 0);
569 hWndPrev
= ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : NULL
;
571 pWndChg
= ThreadQueue
->spwndActive
; // Keep to notify of a preemptive switch.
575 BOOL Ret
, DoFG
, AllowFG
;
577 if (Wnd
->state
& WNDS_BEINGACTIVATED
) return TRUE
;
579 if (ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
581 if (IsAllowedFGActive(pti
, Wnd
))
587 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
590 AllowFG
= !pti
->cVisWindows
; // Nothing is visable.
591 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
593 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
595 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
596 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
597 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
598 if (!gpqForeground
|| gpqForeground
== ThreadQueue
)
606 if (pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
|| pti
->cVisWindows
)
613 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
618 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
619 //ERR("co_IntSetActiveWindow 3c FG set\n");
620 Ret
= co_IntSetForegroundAndFocusWindow(Wnd
, bMouse
);
623 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
627 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
633 /* Call CBT hook chain */
635 cbt
.hWndActive
= hWndPrev
;
636 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
638 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
642 if ( ThreadQueue
->spwndActive
&& ThreadQueue
->spwndActive
->state
& WNDS_DESTROYED
)
643 ThreadQueue
->spwndActive
= NULL
;
645 ThreadQueue
->spwndActivePrev
= ThreadQueue
->spwndActive
;
647 WndPrev
= ThreadQueue
->spwndActive
; // Keep to save changing active.
651 if (ThreadQueue
== gpqForeground
) gpqForegroundPrev
= ThreadQueue
;
652 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev
), hWnd
)) return FALSE
;
655 WndPrev
= ThreadQueue
->spwndActive
; // Again keep to save changing active.
657 // While in calling message proc or hook:
658 // Fail if a preemptive switch was made, current active not made previous,
659 // focus window is dead or no longer the same thread queue.
660 if ( ThreadQueue
->spwndActivePrev
!= ThreadQueue
->spwndActive
||
661 pWndChg
!= WndPrev
||
662 (Wnd
&& !VerifyWnd(Wnd
)) ||
663 ThreadQueue
!= pti
->MessageQueue
)
665 ERR("SetActiveWindow: Summary ERROR, active state changed!\n");
669 if (!WndPrev
) ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
671 if (Wnd
) Wnd
->state
|= WNDS_BEINGACTIVATED
;
673 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND
, Wnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
674 //// Breaks Atl-Esc/Tab via User32.
675 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
677 /* check if the specified window can be set in the input data of a given queue */
678 if ( !Wnd
|| ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
680 /* set the current thread active window */
681 ThreadQueue
->spwndActive
= Wnd
;
684 WndPrev
= VerifyWnd(ThreadQueue
->spwndActivePrev
); // Now should be set but verify it again.
686 InAAPM
= co_IntSendActivateMessages(WndPrev
, Wnd
, bMouse
, Async
);
688 /* now change focus if necessary */
689 //// Fixes CORE-6452 allows setting focus on window.
690 if (bFocus
&& !(ThreadQueue
->QF_flags
& QF_FOCUSNULLSINCEACTIVE
))
692 /* Do not change focus if the window is no longer active */
693 if (pti
->MessageQueue
->spwndActive
!= IntGetNonChildAncestor(pti
->MessageQueue
->spwndFocus
))
695 PWND pWndSend
= pti
->MessageQueue
->spwndActive
;
696 // Clear focus if the active window is minimized.
697 if (pWndSend
&& pti
->MessageQueue
->spwndActive
->style
& WS_MINIMIZE
) pWndSend
= NULL
;
698 // Send focus messages and if so, set the focus.
699 IntSendFocusMessages( pti
, pWndSend
);
705 pti
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
708 // FIXME: Used in the menu loop!!!
709 ThreadQueue
->QF_flags
|= QF_ACTIVATIONCHANGE
;
711 //ERR("co_IntSetActiveWindow Exit\n");
712 if (Wnd
) Wnd
->state
&= ~WNDS_BEINGACTIVATED
;
713 return (ThreadQueue
->spwndActive
== Wnd
);
717 co_IntMouseActivateWindow(PWND Wnd
)
719 TRACE("Mouse Active\n");
720 return co_IntSetForegroundAndFocusWindow(Wnd
, TRUE
);
724 UserSetActiveWindow(PWND Wnd
)
726 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
728 if (Wnd
) // Must have a window!
730 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
732 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
735 Yes your eye are not deceiving you~!
737 First part of wines Win.c test_SetActiveWindow:
739 flush_events( TRUE );
740 ShowWindow(hwnd, SW_HIDE);
743 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
745 Now Handle wines Msg.c test_SetActiveWindow( 0 )...
747 TRACE("USAW: Previous active window\n");
748 if ( gpqForegroundPrev
&&
749 gpqForegroundPrev
->spwndActivePrev
&&
750 (gpqForegroundPrev
->spwndActivePrev
->style
& (WS_VISIBLE
|WS_DISABLED
)) == WS_VISIBLE
&&
751 !(gpqForegroundPrev
->spwndActivePrev
->state2
& WNDS2_BOTTOMMOST
) &&
752 (Wnd
= VerifyWnd(gpqForegroundPrev
->spwndActivePrev
)) != NULL
)
754 TRACE("USAW:PAW hwnd %p\n",Wnd
?Wnd
->head
.h
:NULL
);
755 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
758 // Activate anyone but the active window.
759 if ( pti
->MessageQueue
->spwndActive
&&
760 (Wnd
= VerifyWnd(pti
->MessageQueue
->spwndActive
)) != NULL
)
762 ERR("USAW:AOWM hwnd %p\n",Wnd
?Wnd
->head
.h
:NULL
);
763 if (!ActivateOtherWindowMin(Wnd
))
765 // Okay, now go find someone else to play with!
766 ERR("USAW: Going to WPAOW\n");
767 co_WinPosActivateOtherWindow(Wnd
);
772 TRACE("USAW: Nothing\n");
777 co_UserSetFocus(PWND Window
)
782 PUSER_MESSAGE_QUEUE ThreadQueue
;
785 ASSERT_REFS_CO(Window
);
787 pti
= PsGetCurrentThreadWin32Thread();
788 ThreadQueue
= pti
->MessageQueue
;
789 ASSERT(ThreadQueue
!= 0);
791 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window
? UserHMGetHandle(Window
) : 0, pti
);
793 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
797 if (hWndPrev
== UserHMGetHandle(Window
))
799 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0; /* Nothing to do */
802 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
804 ERR("SetFocus Must have the same Q!\n");
808 /* Check if we can set the focus to this window */
809 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720...
810 for (pwndTop
= Window
; pwndTop
; pwndTop
= pwndTop
->spwndParent
)
812 if (pwndTop
->style
& (WS_MINIMIZED
|WS_DISABLED
)) return 0;
813 if ((pwndTop
->style
& (WS_POPUP
|WS_CHILD
)) != WS_CHILD
) break;
814 if (pwndTop
->spwndParent
== NULL
) 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
)
938 ERR("Window Thread does not match Current!\n");
943 hWndPrev
= MsqSetStateWindow(pti
, MSQ_STATE_CAPTURE
, hWnd
);
947 pWnd
= UserGetWindowObject(hWndPrev
);
949 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND
, pWnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
953 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART
, Window
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
956 // Only send the message if we have a previous Window!
957 // Fix msg_menu tracking popup menu and win test_capture_4!!!!
961 if (ThreadQueue
->MenuOwner
&& Window
) ThreadQueue
->QF_flags
|= QF_CAPTURELOCKED
;
963 co_IntSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
965 ThreadQueue
->QF_flags
&= ~QF_CAPTURELOCKED
;
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_
);