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
),
69 WndPrev
->state
&= ~WNDS_ACTIVEFRAME
;
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 ExFreePool(phwndTopLevel
);
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
),
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 ExFreePoolWithTag(Message
, TAG_USRMSG
);
349 ToggleFGActivate(PTHREADINFO pti
)
352 PPROCESSINFO ppi
= pti
->ppi
;
354 Ret
= !!(pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
);
357 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
360 Ret
= !!(ppi
->W32PF_flags
& W32PF_ALLOWFOREGROUNDACTIVATE
);
363 ppi
->W32PF_flags
&= ~W32PF_ALLOWFOREGROUNDACTIVATE
;
364 //ERR("ToggleFGActivate is %d\n",Ret);
369 IsAllowedFGActive(PTHREADINFO pti
, PWND Wnd
)
371 // Not allowed if one or more,,
372 if (!ToggleFGActivate(pti
) || // bits not set,
373 pti
->rpdesk
!= gpdeskInputDesktop
|| // not current Desktop,
374 pti
->MessageQueue
== gpqForeground
|| // if already the queue foreground,
375 IsFGLocked() || // foreground is locked,
376 Wnd
->ExStyle
& WS_EX_NOACTIVATE
) // or,,, does not become the foreground window when the user clicks it.
380 //ERR("IsAllowedFGActive is TRUE\n");
385 Can the system force foreground from one or more conditions.
388 CanForceFG(PPROCESSINFO ppi
)
391 ptiLastInput
->ppi
== ppi
||
393 gptiForeground
->ppi
== ppi
||
394 ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_SETFOREGROUNDALLOWED
) ||
395 gppiInputProvider
== ppi
||
398 //ERR("CanForceFG is FALSE\n");
404 The system restricts which processes can set the foreground window. A process
405 can set the foreground window only if one of the following conditions is true:
407 * The process is the foreground process.
408 * The process was started by the foreground process.
409 * The process received the last input event.
410 * There is no foreground process.
411 * The foreground process is being debugged.
412 * The foreground is not locked (see LockSetForegroundWindow).
413 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
414 * No menus are active.
418 co_IntSetForegroundAndFocusWindow(
420 _In_ BOOL MouseActivate
)
422 HWND hWnd
= Wnd
? UserHMGetHandle(Wnd
) : NULL
;
423 HWND hWndPrev
= NULL
;
424 PWND pWndPrev
= NULL
;
425 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
427 BOOL fgRet
= FALSE
, Ret
= FALSE
;
429 if (Wnd
) ASSERT_REFS_CO(Wnd
);
431 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
433 PrevForegroundQueue
= IntGetFocusMessageQueue(); // Use this active desktop.
434 pti
= PsGetCurrentThreadWin32Thread();
436 if (PrevForegroundQueue
)
437 { // Same Window Q as foreground just do active.
438 if (Wnd
&& Wnd
->head
.pti
->MessageQueue
== PrevForegroundQueue
)
440 //ERR("Same Window Q as foreground just do active.\n");
441 if (pti
->MessageQueue
== PrevForegroundQueue
)
442 { // Same WQ and TQ go active.
443 //ERR("Same WQ and TQ go active.\n");
444 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
446 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
447 { // Same WQ and it is active.
448 //ERR("Same WQ and it is active.\n");
452 { // Same WQ as FG but not the same TQ send active.
453 //ERR("Same WQ as FG but not the same TQ send active.\n");
454 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
460 hWndPrev
= PrevForegroundQueue
->spwndActive
? UserHMGetHandle(PrevForegroundQueue
->spwndActive
) : 0;
461 pWndPrev
= PrevForegroundQueue
->spwndActive
;
464 if ( (( !IsFGLocked() || pti
->ppi
== gppiInputProvider
) &&
465 ( CanForceFG(pti
->ppi
) || pti
->TIF_flags
& (TIF_SYSTEMTHREAD
|TIF_CSRSSTHREAD
|TIF_ALLOWFOREGROUNDACTIVATE
) )) ||
466 pti
->ppi
== ppiScrnSaver
470 //ToggleFGActivate(pti); // win.c line 2662 fail
473 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
474 gptiForeground
= Wnd
->head
.pti
;
475 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
479 IntSetFocusMessageQueue(NULL
);
480 gptiForeground
= NULL
;
481 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
485 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
486 other thread after we already changed the foreground window back to our own
490 FindRemoveAsyncMsg(Wnd
, 0); // Do this to fix test_SFW todos!
495 // Fix FG Bounce with regedit.
496 if (hWndPrev
!= hWnd
)
498 if (PrevForegroundQueue
&&
500 PrevForegroundQueue
->spwndActive
)
502 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
503 if (pti
->MessageQueue
== PrevForegroundQueue
)
505 //ERR("SFGW: TI same as Prev TI\n");
506 co_IntSetActiveWindow(NULL
, FALSE
, TRUE
, FALSE
);
510 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
511 // No real reason to wait here.
512 co_IntSendMessageNoWait(hWndPrev
, WM_ASYNC_SETACTIVEWINDOW
, 0, 0 );
517 if (!Wnd
) return FALSE
; // Always return false.
519 if (pti
->MessageQueue
== Wnd
->head
.pti
->MessageQueue
)
521 //ERR("Same PQ and WQ go active.\n");
522 Ret
= co_IntSetActiveWindow(Wnd
, MouseActivate
, TRUE
, FALSE
);
524 else if (Wnd
->head
.pti
->MessageQueue
->spwndActive
== Wnd
)
526 //ERR("Same Active and Wnd.\n");
531 //ERR("Activate Not same PQ and WQ and Wnd.\n");
532 co_IntSendMessage(hWnd
, WM_ASYNC_SETACTIVEWINDOW
, (WPARAM
)Wnd
, (LPARAM
)MouseActivate
);
539 co_IntSetActiveWindow(PWND Wnd OPTIONAL
, BOOL bMouse
, BOOL bFocus
, BOOL Async
)
542 PUSER_MESSAGE_QUEUE ThreadQueue
;
543 PWND pWndChg
, WndPrev
; // State changes.
547 CBTACTIVATESTRUCT cbt
;
548 //ERR("co_IntSetActiveWindow 1\n");
552 hWnd
= UserHMGetHandle(Wnd
);
553 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
554 if (Wnd
== UserGetDesktopWindow()) return FALSE
;
555 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
558 //ERR("co_IntSetActiveWindow 2\n");
559 pti
= PsGetCurrentThreadWin32Thread();
560 ThreadQueue
= pti
->MessageQueue
;
561 ASSERT(ThreadQueue
!= 0);
563 hWndPrev
= ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : NULL
;
565 pWndChg
= ThreadQueue
->spwndActive
; // Keep to notify of a preemptive switch.
569 BOOL Ret
, DoFG
, AllowFG
;
571 if (Wnd
->state
& WNDS_BEINGACTIVATED
) return TRUE
;
573 if (ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
575 if (IsAllowedFGActive(pti
, Wnd
))
581 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
584 AllowFG
= !pti
->cVisWindows
; // Nothing is visable.
585 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
587 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
589 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
590 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
591 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
592 if (!gpqForeground
|| gpqForeground
== ThreadQueue
)
600 if (pti
->TIF_flags
& TIF_ALLOWFOREGROUNDACTIVATE
|| pti
->cVisWindows
)
607 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
612 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
613 //ERR("co_IntSetActiveWindow 3c FG set\n");
614 Ret
= co_IntSetForegroundAndFocusWindow(Wnd
, bMouse
);
617 pti
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
621 pti
->TIF_flags
&= ~TIF_ALLOWFOREGROUNDACTIVATE
;
627 /* Call CBT hook chain */
629 cbt
.hWndActive
= hWndPrev
;
630 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
632 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
636 if ( ThreadQueue
->spwndActive
&& ThreadQueue
->spwndActive
->state
& WNDS_DESTROYED
)
637 ThreadQueue
->spwndActive
= NULL
;
639 ThreadQueue
->spwndActivePrev
= ThreadQueue
->spwndActive
;
641 WndPrev
= ThreadQueue
->spwndActive
; // Keep to save changing active.
645 if (ThreadQueue
== gpqForeground
) gpqForegroundPrev
= ThreadQueue
;
646 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev
), hWnd
)) return FALSE
;
649 WndPrev
= ThreadQueue
->spwndActive
; // Again keep to save changing active.
651 // While in calling message proc or hook:
652 // Fail if a preemptive switch was made, current active not made previous,
653 // focus window is dead or no longer the same thread queue.
654 if ( ThreadQueue
->spwndActivePrev
!= ThreadQueue
->spwndActive
||
655 pWndChg
!= WndPrev
||
656 (Wnd
&& !VerifyWnd(Wnd
)) ||
657 ThreadQueue
!= pti
->MessageQueue
)
659 ERR("SetActiveWindow: Summary ERROR, active state changed!\n");
663 if (!WndPrev
) ThreadQueue
->QF_flags
&= ~QF_FOCUSNULLSINCEACTIVE
;
665 if (Wnd
) Wnd
->state
|= WNDS_BEINGACTIVATED
;
667 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND
, Wnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
668 //// Breaks Atl-Esc/Tab via User32.
669 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
671 /* check if the specified window can be set in the input data of a given queue */
672 if ( !Wnd
|| ThreadQueue
== Wnd
->head
.pti
->MessageQueue
)
674 /* set the current thread active window */
675 ThreadQueue
->spwndActive
= Wnd
;
678 WndPrev
= VerifyWnd(ThreadQueue
->spwndActivePrev
); // Now should be set but verify it again.
680 InAAPM
= co_IntSendActivateMessages(WndPrev
, Wnd
, bMouse
, Async
);
682 /* now change focus if necessary */
683 //// Fixes CORE-6452 allows setting focus on window.
684 if (bFocus
&& !(ThreadQueue
->QF_flags
& QF_FOCUSNULLSINCEACTIVE
))
686 /* Do not change focus if the window is no longer active */
687 if (pti
->MessageQueue
->spwndActive
!= IntGetNonChildAncestor(pti
->MessageQueue
->spwndFocus
))
689 PWND pWndSend
= pti
->MessageQueue
->spwndActive
;
690 // Clear focus if the active window is minimized.
691 if (pWndSend
&& pti
->MessageQueue
->spwndActive
->style
& WS_MINIMIZE
) pWndSend
= NULL
;
692 // Send focus messages and if so, set the focus.
693 IntSendFocusMessages( pti
, pWndSend
);
699 pti
->TIF_flags
&= ~TIF_INACTIVATEAPPMSG
;
702 // FIXME: Used in the menu loop!!!
703 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
705 //ERR("co_IntSetActiveWindow Exit\n");
706 if (Wnd
) Wnd
->state
&= ~WNDS_BEINGACTIVATED
;
707 return (ThreadQueue
->spwndActive
== Wnd
);
711 co_IntMouseActivateWindow(PWND Wnd
)
713 TRACE("Mouse Active\n");
714 return co_IntSetForegroundAndFocusWindow(Wnd
, TRUE
);
718 UserSetActiveWindow(PWND Wnd
)
720 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
722 if (Wnd
) // Must have a window!
724 if ((Wnd
->style
& (WS_POPUP
|WS_CHILD
)) == WS_CHILD
) return FALSE
;
726 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
729 Yes your eye are not deceiving you~!
731 First part of wines Win.c test_SetActiveWindow:
733 flush_events( TRUE );
734 ShowWindow(hwnd, SW_HIDE);
737 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
739 Now Handle wines Msg.c test_SetActiveWindow( 0 )...
741 TRACE("USAW: Previous active window\n");
742 if ( gpqForegroundPrev
&&
743 gpqForegroundPrev
->spwndActivePrev
&&
744 (gpqForegroundPrev
->spwndActivePrev
->style
& (WS_VISIBLE
|WS_DISABLED
)) == WS_VISIBLE
&&
745 !(gpqForegroundPrev
->spwndActivePrev
->state2
& WNDS2_BOTTOMMOST
) &&
746 (Wnd
= VerifyWnd(gpqForegroundPrev
->spwndActivePrev
)) != NULL
)
748 TRACE("USAW:PAW hwnd %p\n",Wnd
?Wnd
->head
.h
:NULL
);
749 return co_IntSetActiveWindow(Wnd
, FALSE
, TRUE
, FALSE
);
752 // Activate anyone but the active window.
753 if ( pti
->MessageQueue
->spwndActive
&&
754 (Wnd
= VerifyWnd(pti
->MessageQueue
->spwndActive
)) != NULL
)
756 ERR("USAW:AOWM hwnd %p\n",Wnd
?Wnd
->head
.h
:NULL
);
757 if (!ActivateOtherWindowMin(Wnd
))
759 // Okay, now go find someone else to play with!
760 ERR("USAW: Going to WPAOW\n");
761 co_WinPosActivateOtherWindow(Wnd
);
766 TRACE("USAW: Nothing\n");
771 co_UserSetFocus(PWND Window
)
776 PUSER_MESSAGE_QUEUE ThreadQueue
;
779 ASSERT_REFS_CO(Window
);
781 pti
= PsGetCurrentThreadWin32Thread();
782 ThreadQueue
= pti
->MessageQueue
;
783 ASSERT(ThreadQueue
!= 0);
785 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window
? UserHMGetHandle(Window
) : 0, pti
);
787 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
791 if (hWndPrev
== UserHMGetHandle(Window
))
793 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0; /* Nothing to do */
796 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
798 ERR("SetFocus Must have the same Q!\n");
802 /* Check if we can set the focus to this window */
803 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720...
804 for (pwndTop
= Window
; pwndTop
; pwndTop
= pwndTop
->spwndParent
)
806 if (pwndTop
->style
& (WS_MINIMIZED
|WS_DISABLED
)) return 0;
807 if ((pwndTop
->style
& (WS_POPUP
|WS_CHILD
)) != WS_CHILD
) break;
808 if (pwndTop
->spwndParent
== NULL
) break;
811 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->head
.h
, (LPARAM
)hWndPrev
))
813 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
817 /* Activate pwndTop if needed. */
818 if (pwndTop
!= ThreadQueue
->spwndActive
)
820 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue(); // Keep it based on desktop.
821 if (ThreadQueue
!= ForegroundQueue
&& IsAllowedFGActive(pti
, pwndTop
)) // Rule 2 & 3.
823 //ERR("SetFocus: Set Foreground!\n");
824 if (!(pwndTop
->style
& WS_VISIBLE
))
826 pti
->ppi
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
;
828 if (!co_IntSetForegroundAndFocusWindow(pwndTop
, FALSE
))
830 ERR("SetFocus: Set Foreground and Focus Failed!\n");
835 /* Set Active when it is needed. */
836 if (pwndTop
!= ThreadQueue
->spwndActive
)
838 //ERR("SetFocus: Set Active!\n");
839 if (!co_IntSetActiveWindow(pwndTop
, FALSE
, FALSE
, FALSE
))
841 ERR("SetFocus: Set Active Failed!\n");
846 /* Abort if window destroyed */
847 if (Window
->state2
& WNDS2_INDESTROY
) return 0;
848 /* Do not change focus if the window is no longer active */
849 if (pwndTop
!= ThreadQueue
->spwndActive
)
851 ERR("SetFocus: Top window did not go active!\n");
856 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
857 hWndPrev
= ThreadQueue
->spwndFocus
? UserHMGetHandle(ThreadQueue
->spwndFocus
) : 0;
859 IntSendFocusMessages( pti
, Window
);
861 TRACE("Focus: %p -> %p\n", hWndPrev
, Window
->head
.h
);
863 else /* NULL hwnd passed in */
865 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
867 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
871 /* set the current thread focus window null */
872 IntSendFocusMessages( pti
, NULL
);
874 return hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0;
878 UserGetForegroundWindow(VOID
)
880 PUSER_MESSAGE_QUEUE ForegroundQueue
;
882 ForegroundQueue
= IntGetFocusMessageQueue();
883 return( ForegroundQueue
? (ForegroundQueue
->spwndActive
? UserHMGetHandle(ForegroundQueue
->spwndActive
) : 0) : 0);
886 HWND FASTCALL
UserGetActiveWindow(VOID
)
889 PUSER_MESSAGE_QUEUE ThreadQueue
;
891 pti
= PsGetCurrentThreadWin32Thread();
892 ThreadQueue
= pti
->MessageQueue
;
893 return( ThreadQueue
? (ThreadQueue
->spwndActive
? UserHMGetHandle(ThreadQueue
->spwndActive
) : 0) : 0);
900 PUSER_MESSAGE_QUEUE ThreadQueue
;
901 DECLARE_RETURN(HWND
);
903 TRACE("Enter IntGetCapture\n");
905 pti
= PsGetCurrentThreadWin32Thread();
906 ThreadQueue
= pti
->MessageQueue
;
907 RETURN( ThreadQueue
? (ThreadQueue
->spwndCapture
? UserHMGetHandle(ThreadQueue
->spwndCapture
) : 0) : 0);
910 TRACE("Leave IntGetCapture, ret=%p\n", _ret_
);
915 co_UserSetCapture(HWND hWnd
)
918 PUSER_MESSAGE_QUEUE ThreadQueue
;
919 PWND pWnd
, Window
= NULL
;
922 pti
= PsGetCurrentThreadWin32Thread();
923 ThreadQueue
= pti
->MessageQueue
;
925 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
)
928 if (hWnd
&& (Window
= UserGetWindowObject(hWnd
)))
930 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
932 ERR("Window Thread dos not match Current!\n");
937 hWndPrev
= MsqSetStateWindow(pti
, MSQ_STATE_CAPTURE
, hWnd
);
941 pWnd
= UserGetWindowObject(hWndPrev
);
943 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND
, pWnd
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
947 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART
, Window
, OBJID_WINDOW
, CHILDID_SELF
, WEF_SETBYWNDPTI
);
949 if (hWndPrev
&& hWndPrev
!= hWnd
)
951 if (ThreadQueue
->MenuOwner
&& Window
) ThreadQueue
->QF_flags
|= QF_CAPTURELOCKED
;
953 co_IntSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
955 ThreadQueue
->QF_flags
&= ~QF_CAPTURELOCKED
;
958 if (hWnd
== NULL
) // Release mode.
962 /* Also remove other windows if not capturing anymore */
963 MsqSetStateWindow(pti
, MSQ_STATE_MENUOWNER
, NULL
);
964 MsqSetStateWindow(pti
, MSQ_STATE_MOVESIZE
, NULL
);
966 /* Somebody may have missed some mouse movements */
970 mi
.dwFlags
= MOUSEEVENTF_MOVE
;
973 UserSendMouseInput(&mi
, FALSE
);
983 IntReleaseCapture(VOID
)
986 PUSER_MESSAGE_QUEUE ThreadQueue
;
988 pti
= PsGetCurrentThreadWin32Thread();
989 ThreadQueue
= pti
->MessageQueue
;
991 // Can not release inside WM_CAPTURECHANGED!!
992 if (ThreadQueue
->QF_flags
& QF_CAPTURELOCKED
) return FALSE
;
994 co_UserSetCapture(NULL
);
1003 co_IntSetForegroundWindow(PWND Window
)
1005 if (Window
) ASSERT_REFS_CO(Window
);
1007 return co_IntSetForegroundAndFocusWindow(Window
, FALSE
);
1014 co_IntSetForegroundWindowMouse(PWND Window
)
1016 if (Window
) ASSERT_REFS_CO(Window
);
1018 return co_IntSetForegroundAndFocusWindow(Window
, TRUE
);
1025 IntLockSetForegroundWindow(UINT uLockCode
)
1027 ULONG Err
= ERROR_ACCESS_DENIED
;
1028 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
1032 if ( CanForceFG(ppi
) && !gppiLockSFW
)
1039 if ( gppiLockSFW
== ppi
)
1046 Err
= ERROR_INVALID_PARAMETER
;
1048 EngSetLastError(Err
);
1056 IntAllowSetForegroundWindow(DWORD dwProcessId
)
1058 PPROCESSINFO ppi
, ppiCur
;
1059 PEPROCESS Process
= NULL
;
1062 if (dwProcessId
!= ASFW_ANY
)
1064 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE
)dwProcessId
, &Process
)))
1066 EngSetLastError(ERROR_INVALID_PARAMETER
);
1069 ppi
= PsGetProcessWin32Process(Process
);
1072 ObDereferenceObject(Process
);
1076 ppiCur
= PsGetCurrentProcessWin32Process();
1077 if (!CanForceFG(ppiCur
))
1079 if (Process
) ObDereferenceObject(Process
);
1080 EngSetLastError(ERROR_ACCESS_DENIED
);
1083 if (dwProcessId
== ASFW_ANY
)
1084 { // All processes will be enabled to set the foreground window.
1085 //ERR("ptiLastInput is CLEARED!!\n");
1086 ptiLastInput
= NULL
;
1089 { // Rule #3, last input event in force.
1090 ERR("ptiLastInput is SET!!\n");
1091 //ptiLastInput = ppi->ptiList; // See CORE-6384 & CORE-7030.
1092 ObDereferenceObject(Process
);
1101 NtUserGetForegroundWindow(VOID
)
1103 DECLARE_RETURN(HWND
);
1105 TRACE("Enter NtUserGetForegroundWindow\n");
1106 UserEnterExclusive();
1108 RETURN( UserGetForegroundWindow());
1111 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_
);
1117 NtUserSetActiveWindow(HWND hWnd
)
1119 USER_REFERENCE_ENTRY Ref
;
1122 DECLARE_RETURN(HWND
);
1124 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd
);
1125 UserEnterExclusive();
1130 if (!(Window
= UserGetWindowObject(hWnd
)))
1132 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd
);
1138 Window
->head
.pti
->MessageQueue
== gptiCurrent
->MessageQueue
)
1140 hWndPrev
= gptiCurrent
->MessageQueue
->spwndActive
? UserHMGetHandle(gptiCurrent
->MessageQueue
->spwndActive
) : NULL
;
1141 if (Window
) UserRefObjectCo(Window
, &Ref
);
1142 UserSetActiveWindow(Window
);
1143 if (Window
) UserDerefObjectCo(Window
);
1144 RETURN( hWndPrev
? (IntIsWindow(hWndPrev
) ? hWndPrev
: 0) : 0 );
1149 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_
);
1158 NtUserSetCapture(HWND hWnd
)
1160 DECLARE_RETURN(HWND
);
1162 TRACE("Enter NtUserSetCapture(%p)\n", hWnd
);
1163 UserEnterExclusive();
1165 RETURN( co_UserSetCapture(hWnd
));
1168 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_
);
1177 NtUserSetFocus(HWND hWnd
)
1180 USER_REFERENCE_ENTRY Ref
;
1181 DECLARE_RETURN(HWND
);
1184 TRACE("Enter NtUserSetFocus(%p)\n", hWnd
);
1185 UserEnterExclusive();
1189 if (!(Window
= UserGetWindowObject(hWnd
)))
1191 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd
);
1195 UserRefObjectCo(Window
, &Ref
);
1196 ret
= co_UserSetFocus(Window
);
1197 UserDerefObjectCo(Window
);
1203 RETURN( co_UserSetFocus(0));
1207 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_
);