Synchronize with trunk's revision r57659.
[reactos.git] / win32ss / user / ntuser / focus.c
1 /*
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
7 */
8
9 #include <win32k.h>
10 DBG_DEFAULT_CHANNEL(UserFocus);
11
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;
18
19 /*
20 Check locking of a process or one or more menus are active.
21 */
22 BOOL FASTCALL
23 IsFGLocked(VOID)
24 {
25 return (gppiLockSFW || guSFWLockCount);
26 }
27
28 HWND FASTCALL
29 IntGetCaptureWindow(VOID)
30 {
31 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
32 return ( ForegroundQueue ? (ForegroundQueue->spwndCapture ? UserHMGetHandle(ForegroundQueue->spwndCapture) : 0) : 0);
33 }
34
35 HWND FASTCALL
36 IntGetThreadFocusWindow(VOID)
37 {
38 PTHREADINFO pti;
39 PUSER_MESSAGE_QUEUE ThreadQueue;
40
41 pti = PsGetCurrentThreadWin32Thread();
42 ThreadQueue = pti->MessageQueue;
43 if (!ThreadQueue)
44 return NULL;
45 return ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0;
46 }
47
48 BOOL FASTCALL
49 co_IntSendDeactivateMessages(HWND hWndPrev, HWND hWnd)
50 {
51 USER_REFERENCE_ENTRY RefPrev;
52 PWND WndPrev;
53 BOOL Ret = TRUE;
54
55 if (hWndPrev && (WndPrev = ValidateHwndNoErr(hWndPrev)))
56 {
57 UserRefObjectCo(WndPrev, &RefPrev);
58
59 if (co_IntSendMessageNoWait(hWndPrev, WM_NCACTIVATE, FALSE, 0)) //(LPARAM)hWnd))
60 {
61 co_IntSendMessageNoWait(hWndPrev, WM_ACTIVATE,
62 MAKEWPARAM(WA_INACTIVE, WndPrev->style & WS_MINIMIZE),
63 (LPARAM)hWnd);
64
65 if (WndPrev)
66 WndPrev->state &= ~WNDS_ACTIVEFRAME;
67 }
68 else
69 {
70 ERR("Application is keeping itself Active to prevent the change!\n");
71 Ret = FALSE;
72 }
73
74 UserDerefObjectCo(WndPrev);
75 }
76 return Ret;
77 }
78
79 BOOL FASTCALL
80 co_IntMakeWindowActive(PWND Window)
81 {
82 PWND spwndOwner;
83 if (Window)
84 { // Set last active for window and it's owner.
85 spwndOwner = Window;
86 while (spwndOwner->spwndOwner)
87 {
88 spwndOwner = spwndOwner->spwndOwner;
89 }
90 spwndOwner->spwndLastActive = Window;
91 return TRUE;
92 }
93 ERR("MakeWindowActive Failed!\n");
94 return FALSE;
95 }
96
97 BOOL FASTCALL
98 co_IntSendActivateMessages(PWND WindowPrev, PWND Window, BOOL MouseActivate, BOOL Async)
99 {
100 USER_REFERENCE_ENTRY Ref, RefPrev;
101 HANDLE OldTID, NewTID;
102 PTHREADINFO pti, ptiOld, ptiNew;
103 BOOL InAAPM = FALSE;
104
105 if (Window)
106 {
107 pti = PsGetCurrentThreadWin32Thread();
108
109 UserRefObjectCo(Window, &Ref);
110
111 if (WindowPrev) UserRefObjectCo(WindowPrev, &RefPrev);
112
113 /* Send palette messages */
114 if (gpsi->PUSIFlags & PUSIF_PALETTEDISPLAY &&
115 co_IntPostOrSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0))
116 {
117 UserSendNotifyMessage( HWND_BROADCAST,
118 WM_PALETTEISCHANGING,
119 (WPARAM)UserHMGetHandle(Window),
120 0);
121 }
122 //// Fixes bug 7089.
123 if (!(Window->style & WS_CHILD))
124 {
125 PWND pwndTemp = co_GetDesktopWindow(Window)->spwndChild;
126
127 while (pwndTemp && !(pwndTemp->style & WS_VISIBLE)) pwndTemp = pwndTemp->spwndNext;
128
129 if (Window != pwndTemp || (WindowPrev && !IntIsWindowVisible(WindowPrev)))
130 {
131 if (!Async || pti->MessageQueue == gpqForeground)
132 {
133 UINT flags = SWP_NOSIZE | SWP_NOMOVE;
134 if (Window == pwndTemp) flags |= SWP_NOACTIVATE;
135 co_WinPosSetWindowPos(Window, HWND_TOP, 0, 0, 0, 0, flags);
136 }
137 }
138 }
139 ////
140 OldTID = WindowPrev ? IntGetWndThreadId(WindowPrev) : NULL;
141 NewTID = IntGetWndThreadId(Window);
142 ptiOld = WindowPrev ? WindowPrev->head.pti : NULL;
143 ptiNew = Window->head.pti;
144
145 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID);
146
147 if (!(pti->TIF_flags & TIF_INACTIVATEAPPMSG) &&
148 (!WindowPrev || OldTID != NewTID) )
149 {
150 PWND cWindow;
151 HWND *List, *phWnd;
152
153 List = IntWinListChildren(UserGetDesktopWindow());
154 if ( List )
155 {
156 if ( OldTID )
157 {
158 ptiOld->TIF_flags |= TIF_INACTIVATEAPPMSG;
159 ptiOld->pClientInfo->dwTIFlags = ptiOld->TIF_flags;
160
161 for (phWnd = List; *phWnd; ++phWnd)
162 {
163 cWindow = ValidateHwndNoErr(*phWnd);
164 if (cWindow && cWindow->head.pti == ptiOld)
165 { // FALSE if the window is being deactivated,
166 // ThreadId that owns the window being activated.
167 co_IntSendMessageNoWait(*phWnd, WM_ACTIVATEAPP, FALSE, (LPARAM)NewTID);
168 }
169 }
170 ptiOld->TIF_flags &= ~TIF_INACTIVATEAPPMSG;
171 ptiOld->pClientInfo->dwTIFlags = ptiOld->TIF_flags;
172 }
173 if ( NewTID )
174 { //// Prevents a resource crash due to reentrance!
175 InAAPM = TRUE;
176 pti->TIF_flags |= TIF_INACTIVATEAPPMSG;
177 pti->pClientInfo->dwTIFlags = pti->TIF_flags;
178 ////
179 for (phWnd = List; *phWnd; ++phWnd)
180 {
181 cWindow = ValidateHwndNoErr(*phWnd);
182 if (cWindow && cWindow->head.pti == ptiNew)
183 { // TRUE if the window is being activated,
184 // ThreadId that owns the window being deactivated.
185 co_IntSendMessageNoWait(*phWnd, WM_ACTIVATEAPP, TRUE, (LPARAM)OldTID);
186 }
187 }
188 }
189 ExFreePoolWithTag(List, USERTAG_WINDOWLIST);
190 }
191 }
192 if (WindowPrev)
193 UserDerefObjectCo(WindowPrev); // Now allow the previous window to die.
194
195 if (Window->state & WNDS_ACTIVEFRAME)
196 { // If already active frame do not allow NCPaint.
197 //ERR("SendActivateMessage Is Active Frame!\n");
198 Window->state |= WNDS_NONCPAINT;
199 }
200
201 if (Window->style & WS_MINIMIZE)
202 {
203 TRACE("Widow was minimized\n");
204 }
205
206 co_IntMakeWindowActive(Window);
207
208 UserDerefObjectCo(Window);
209
210 /* FIXME: IntIsWindow */
211 co_IntSendMessageNoWait( UserHMGetHandle(Window),
212 WM_NCACTIVATE,
213 (WPARAM)(Window == gpqForeground->spwndActive),
214 0); //(LPARAM)hWndPrev);
215
216 co_IntSendMessageNoWait( UserHMGetHandle(Window),
217 WM_ACTIVATE,
218 MAKEWPARAM(MouseActivate ? WA_CLICKACTIVE : WA_ACTIVE, Window->style & WS_MINIMIZE),
219 (LPARAM)(WindowPrev ? UserHMGetHandle(WindowPrev) : 0));
220
221 if (!Window->spwndOwner && !IntGetParent(Window))
222 {
223 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
224 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED, (WPARAM) UserHMGetHandle(Window), FALSE);
225 }
226
227 Window->state &= ~WNDS_NONCPAINT;
228
229 }
230 return InAAPM;
231 }
232
233 VOID FASTCALL
234 IntSendFocusMessages( PTHREADINFO pti, PWND pWnd)
235 {
236 PWND pWndPrev;
237 PUSER_MESSAGE_QUEUE ThreadQueue = pti->MessageQueue; // Queue can change...
238
239 ThreadQueue->QF_flags &= ~QF_FOCUSNULLSINCEACTIVE;
240 if (!pWnd && ThreadQueue->spwndActive)
241 {
242 ThreadQueue->QF_flags |= QF_FOCUSNULLSINCEACTIVE;
243 }
244
245 pWndPrev = ThreadQueue->spwndFocus;
246
247 /* check if the specified window can be set in the input data of a given queue */
248 if (!pWnd || ThreadQueue == pWnd->head.pti->MessageQueue)
249 /* set the current thread focus window */
250 ThreadQueue->spwndFocus = pWnd;
251
252 if (pWnd)
253 {
254 if (pWndPrev)
255 {
256 co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
257 }
258 if (ThreadQueue->spwndFocus == pWnd)
259 {
260 IntNotifyWinEvent(EVENT_OBJECT_FOCUS, pWnd, OBJID_CLIENT, CHILDID_SELF, 0);
261 co_IntPostOrSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)(pWndPrev ? UserHMGetHandle(pWndPrev) : NULL), 0);
262 }
263 }
264 else
265 {
266 if (pWndPrev)
267 {
268 IntNotifyWinEvent(EVENT_OBJECT_FOCUS, NULL, OBJID_CLIENT, CHILDID_SELF, 0);
269 co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
270 }
271 }
272 }
273
274 HWND FASTCALL
275 IntFindChildWindowToOwner(PWND Root, PWND Owner)
276 {
277 HWND Ret;
278 PWND Child, OwnerWnd;
279
280 for(Child = Root->spwndChild; Child; Child = Child->spwndNext)
281 {
282 OwnerWnd = Child->spwndOwner;
283 if(!OwnerWnd)
284 continue;
285
286 if(OwnerWnd == Owner)
287 {
288 Ret = Child->head.h;
289 return Ret;
290 }
291 }
292
293 return NULL;
294 }
295
296 VOID FASTCALL
297 FindRemoveAsyncMsg(PWND Wnd)
298 {
299 PUSER_MESSAGE_QUEUE MessageQueue;
300 PUSER_SENT_MESSAGE Message;
301 PLIST_ENTRY Entry;
302
303 if (!Wnd) return;
304
305 MessageQueue = Wnd->head.pti->MessageQueue;
306
307 if (!IsListEmpty(&MessageQueue->SentMessagesListHead))
308 {
309 // Scan sent queue messages to see if we received async messages.
310 Entry = MessageQueue->SentMessagesListHead.Flink;
311 Message = CONTAINING_RECORD(Entry, USER_SENT_MESSAGE, ListEntry);
312 do
313 {
314 if (Message->Msg.message == WM_ASYNC_SETACTIVEWINDOW &&
315 Message->Msg.hwnd == UserHMGetHandle(Wnd) &&
316 Message->Msg.wParam == 0 )
317 {
318 TRACE("ASYNC SAW: Found one in the Sent Msg Queue! %p\n", Message->Msg.hwnd);
319 RemoveEntryList(Entry); // Purge the entry.
320 }
321 Entry = Message->ListEntry.Flink;
322 Message = CONTAINING_RECORD(Entry, USER_SENT_MESSAGE, ListEntry);
323 }
324 while (Entry != &MessageQueue->SentMessagesListHead);
325 }
326 }
327
328 /*
329 Can the system force foreground from one or more conditions.
330 */
331 BOOL FASTCALL
332 CanForceFG(PPROCESSINFO ppi)
333 {
334 if (!ptiLastInput ||
335 ptiLastInput->ppi == ppi ||
336 !gptiForeground ||
337 gptiForeground->ppi == ppi ||
338 ppi->W32PF_flags & (W32PF_ALLOWFOREGROUNDACTIVATE | W32PF_SETFOREGROUNDALLOWED) ||
339 gppiInputProvider == ppi ||
340 !gpqForeground
341 ) return TRUE;
342 return FALSE;
343 }
344
345 /*
346 MSDN:
347 The system restricts which processes can set the foreground window. A process
348 can set the foreground window only if one of the following conditions is true:
349
350 * The process is the foreground process.
351 * The process was started by the foreground process.
352 * The process received the last input event.
353 * There is no foreground process.
354 * The foreground process is being debugged.
355 * The foreground is not locked (see LockSetForegroundWindow).
356 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
357 * No menus are active.
358 */
359
360 static BOOL FASTCALL
361 co_IntSetForegroundAndFocusWindow(PWND Wnd, BOOL MouseActivate)
362 {
363 HWND hWnd = UserHMGetHandle(Wnd);
364 HWND hWndPrev = NULL;
365 PUSER_MESSAGE_QUEUE PrevForegroundQueue;
366 PTHREADINFO pti;
367 BOOL fgRet = FALSE, Ret = FALSE;
368
369 ASSERT_REFS_CO(Wnd);
370
371 TRACE("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
372
373 PrevForegroundQueue = IntGetFocusMessageQueue(); // Use this active desktop.
374 pti = PsGetCurrentThreadWin32Thread();
375
376 if (PrevForegroundQueue)
377 { // Same Window Q as foreground just do active.
378 //ERR("Same Window Q as foreground just do active.\n");
379 if (Wnd && Wnd->head.pti->MessageQueue == PrevForegroundQueue)
380 {
381 if (pti->MessageQueue == PrevForegroundQueue)
382 { // Same WQ and TQ go active.
383 //ERR("Same WQ and TQ go active.\n");
384 Ret = co_IntSetActiveWindow(Wnd, NULL, MouseActivate, TRUE, FALSE);
385 }
386 else if (Wnd->head.pti->MessageQueue->spwndActive == Wnd)
387 { // Same WQ and it is active.
388 //ERR("Same WQ and it is active.\n");
389 Ret = TRUE;
390 }
391 else
392 { // Same WQ as FG but not the same TQ send active.
393 //ERR("Same WQ as FG but not the same TQ send active.\n");
394 co_IntSendMessageNoWait(hWnd, WM_ASYNC_SETACTIVEWINDOW, (WPARAM)Wnd, (LPARAM)MouseActivate );
395 Ret = TRUE;
396 }
397 return Ret;
398 }
399
400 hWndPrev = PrevForegroundQueue->spwndActive ? UserHMGetHandle(PrevForegroundQueue->spwndActive) : 0;
401 }
402
403 if ( (( !IsFGLocked() || pti->ppi == gppiInputProvider ) &&
404 ( CanForceFG(pti->ppi) || pti->TIF_flags & (TIF_SYSTEMTHREAD|TIF_CSRSSTHREAD|TIF_ALLOWFOREGROUNDACTIVATE) )) ||
405 pti->ppi == ppiScrnSaver
406 )
407 {
408 IntSetFocusMessageQueue(Wnd->head.pti->MessageQueue);
409 gptiForeground = Wnd->head.pti;
410 TRACE("Set Foreground pti 0x%p Q 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue);
411 /*
412 Henri Verbeet,
413 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
414 other thread after we already changed the foreground window back to our own
415 window.
416 */
417 FindRemoveAsyncMsg(Wnd); // Do this to fix test_SFW todos!
418 fgRet = TRUE;
419 }
420
421 // Fix FG Bounce with regedit.
422 if (hWndPrev != hWnd )
423 {
424 if (PrevForegroundQueue &&
425 fgRet &&
426 Wnd->head.pti->MessageQueue != PrevForegroundQueue &&
427 PrevForegroundQueue->spwndActive)
428 {
429 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
430 if (pti->MessageQueue == PrevForegroundQueue)
431 {
432 //ERR("SFGW: TI same as Prev TI\n");
433 co_IntSetActiveWindow(NULL, NULL, FALSE, TRUE, FALSE);
434 }
435 else
436 co_IntSendMessageNoWait(hWndPrev, WM_ASYNC_SETACTIVEWINDOW, 0, 0 );
437 }
438 }
439
440 if (pti->MessageQueue == Wnd->head.pti->MessageQueue)
441 {
442 Ret = co_IntSetActiveWindow(Wnd, NULL, MouseActivate, TRUE, FALSE);
443 }
444 else if (Wnd->head.pti->MessageQueue->spwndActive == Wnd)
445 {
446 Ret = TRUE;
447 }
448 else
449 {
450 co_IntSendMessageNoWait(hWnd, WM_ASYNC_SETACTIVEWINDOW, (WPARAM)Wnd, (LPARAM)MouseActivate );
451 Ret = TRUE;
452 }
453
454 return Ret && fgRet;
455 }
456
457 BOOL FASTCALL
458 co_IntMouseActivateWindow(PWND Wnd)
459 {
460 HWND Top;
461 PWND TopWindow;
462 USER_REFERENCE_ENTRY Ref;
463
464 ASSERT_REFS_CO(Wnd);
465
466 if (Wnd->style & WS_DISABLED)
467 {
468 BOOL Ret;
469 PWND TopWnd;
470 PWND DesktopWindow = UserGetDesktopWindow();
471 if (DesktopWindow)
472 {
473 Top = IntFindChildWindowToOwner(DesktopWindow, Wnd);
474 if ((TopWnd = ValidateHwndNoErr(Top)))
475 {
476 UserRefObjectCo(TopWnd, &Ref);
477 Ret = co_IntMouseActivateWindow(TopWnd);
478 UserDerefObjectCo(TopWnd);
479
480 return Ret;
481 }
482 }
483 return FALSE;
484 }
485
486 TopWindow = UserGetAncestor(Wnd, GA_ROOT);
487 //if (TopWindow) {ERR("MAW 2 pWnd %p hWnd %p\n",TopWindow,TopWindow->head.h);}
488 if (!TopWindow) return FALSE;
489
490 /* TMN: Check return value from this function? */
491 UserRefObjectCo(TopWindow, &Ref);
492 co_IntSetForegroundAndFocusWindow(TopWindow, TRUE);
493 UserDerefObjectCo(TopWindow);
494 return TRUE;
495 }
496
497 BOOL FASTCALL
498 co_IntSetActiveWindow(PWND Wnd OPTIONAL, HWND * Prev, BOOL bMouse, BOOL bFocus, BOOL Async)
499 {
500 PTHREADINFO pti;
501 PUSER_MESSAGE_QUEUE ThreadQueue;
502 PWND pWndChg, WndPrev; // State changes.
503 HWND hWndPrev;
504 HWND hWnd = 0;
505 BOOL InAAPM;
506 CBTACTIVATESTRUCT cbt;
507
508 if (Wnd)
509 {
510 ASSERT_REFS_CO(Wnd);
511 hWnd = UserHMGetHandle(Wnd);
512 if ((Wnd->style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
513 if (Wnd == UserGetDesktopWindow()) return FALSE;
514 }
515
516 pti = PsGetCurrentThreadWin32Thread();
517 ThreadQueue = pti->MessageQueue;
518 ASSERT(ThreadQueue != 0);
519
520 hWndPrev = ThreadQueue->spwndActive ? UserHMGetHandle(ThreadQueue->spwndActive) : NULL;
521 if (Prev) *Prev = hWndPrev;
522 if (hWndPrev == hWnd) return TRUE;
523
524 pWndChg = ThreadQueue->spwndActive; // Keep to notify of a preemptive switch.
525
526 if (Wnd)
527 {
528 if (ThreadQueue != Wnd->head.pti->MessageQueue)
529 {
530 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
531 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
532 if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
533 {
534 return co_IntSetForegroundAndFocusWindow(Wnd, bMouse);
535 }
536 }
537
538 if (Wnd->state & WNDS_BEINGACTIVATED) return TRUE;
539 }
540
541 /* Call CBT hook chain */
542 cbt.fMouse = bMouse;
543 cbt.hWndActive = hWndPrev;
544 if (co_HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd, (LPARAM)&cbt))
545 {
546 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
547 return FALSE;
548 }
549
550 if ( ThreadQueue->spwndActive && ThreadQueue->spwndActive->state & WNDS_DESTROYED )
551 ThreadQueue->spwndActive = NULL;
552 else
553 ThreadQueue->spwndActivePrev = ThreadQueue->spwndActive;
554
555 WndPrev = ThreadQueue->spwndActive; // Keep to save changing active.
556
557 if (WndPrev)
558 {
559 if (ThreadQueue == gpqForeground) gpqForegroundPrev = ThreadQueue;
560 if (!co_IntSendDeactivateMessages(hWndPrev, hWnd)) return FALSE;
561 }
562
563 // While in calling message proc or hook:
564 // Fail if a preemptive switch was made, current active not made previous,
565 // focus window is dead or no longer the same thread queue.
566 if ( ThreadQueue->spwndActivePrev != ThreadQueue->spwndActive ||
567 pWndChg != ThreadQueue->spwndActive ||
568 (Wnd && !VerifyWnd(Wnd)) ||
569 ThreadQueue != pti->MessageQueue )
570 {
571 ERR("SetActiveWindow: Summery ERROR, active state changed!\n");
572 return FALSE;
573 }
574
575 if (!WndPrev) ThreadQueue->QF_flags &= ~QF_FOCUSNULLSINCEACTIVE;
576
577 if (Wnd) Wnd->state |= WNDS_BEINGACTIVATED;
578
579 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND, Wnd, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI);
580
581 /* check if the specified window can be set in the input data of a given queue */
582 if ( !Wnd || ThreadQueue == Wnd->head.pti->MessageQueue)
583 {
584 /* set the current thread active window */
585 ThreadQueue->spwndActive = Wnd;
586 }
587
588 InAAPM = co_IntSendActivateMessages(WndPrev, Wnd, bMouse, Async);
589
590 /* now change focus if necessary */
591 if (bFocus && !(ThreadQueue->QF_flags & QF_FOCUSNULLSINCEACTIVE))
592 {
593 /* Do not change focus if the window is no longer active */
594 if (ThreadQueue->spwndActive == Wnd)
595 {
596 if (!ThreadQueue->spwndFocus ||
597 !Wnd ||
598 UserGetAncestor(ThreadQueue->spwndFocus, GA_ROOT) != Wnd)
599 {
600 co_UserSetFocus(Wnd);
601 }
602 }
603 }
604
605 if (InAAPM)
606 {
607 pti->TIF_flags &= ~TIF_INACTIVATEAPPMSG;
608 pti->pClientInfo->dwTIFlags = pti->TIF_flags;
609 }
610
611 // FIXME: Used in the menu loop!!!
612 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
613
614 if (Wnd) Wnd->state &= ~WNDS_BEINGACTIVATED;
615 return (ThreadQueue->spwndActive == Wnd);
616 }
617
618 HWND FASTCALL
619 co_UserSetFocus(PWND Window)
620 {
621 HWND hWndPrev = 0;
622 PWND pwndTop;
623 PTHREADINFO pti;
624 PUSER_MESSAGE_QUEUE ThreadQueue;
625
626 if (Window)
627 ASSERT_REFS_CO(Window);
628
629 pti = PsGetCurrentThreadWin32Thread();
630 ThreadQueue = pti->MessageQueue;
631 ASSERT(ThreadQueue != 0);
632
633 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window ? UserHMGetHandle(Window) : 0, pti );
634
635 hWndPrev = ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0;
636
637 if (Window != 0)
638 {
639 if (hWndPrev == UserHMGetHandle(Window))
640 {
641 return hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0; /* Nothing to do */
642 }
643
644 if (Window->head.pti->MessageQueue != ThreadQueue)
645 {
646 ERR("SetFocus Must have the same Q!\n");
647 return 0;
648 }
649
650 /* Check if we can set the focus to this window */
651 pwndTop = Window;
652 for (;;)
653 {
654 if (pwndTop->style & (WS_MINIMIZED|WS_DISABLED)) return 0;
655 if (!pwndTop->spwndParent || pwndTop->spwndParent == UserGetDesktopWindow())
656 {
657 if ((pwndTop->style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return 0;
658 break;
659 }
660 if (pwndTop->spwndParent == UserGetMessageWindow()) return 0;
661 pwndTop = pwndTop->spwndParent;
662 }
663
664 if (co_HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)Window->head.h, (LPARAM)hWndPrev))
665 {
666 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
667 return 0;
668 }
669
670 /* Activate pwndTop if needed. */
671 if (pwndTop != ThreadQueue->spwndActive)
672 {
673 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue(); // Keep it based on desktop.
674 if (ThreadQueue != ForegroundQueue) // HACK see rule 2 & 3.
675 {
676 if (!co_IntSetForegroundAndFocusWindow(pwndTop, FALSE))
677 {
678 ERR("SetFocus: Set Foreground and Focus Failed!\n");
679 return 0;
680 }
681 }
682
683 /* Set Active when it is needed. */
684 if (pwndTop != ThreadQueue->spwndActive)
685 {
686 if (!co_IntSetActiveWindow(pwndTop, NULL, FALSE, FALSE, FALSE))
687 {
688 ERR("SetFocus: Set Active Failed!\n");
689 return 0;
690 }
691 }
692
693 /* Abort if window destroyed */
694 if (Window->state2 & WNDS2_INDESTROY) return 0;
695 /* Do not change focus if the window is no longer active */
696 if (pwndTop != ThreadQueue->spwndActive)
697 {
698 ERR("SetFocus: Top window did not go active!\n");
699 return 0;
700 }
701 }
702
703 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
704 hWndPrev = ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0;
705
706 IntSendFocusMessages( pti, Window);
707
708 TRACE("Focus: %d -> %d\n", hWndPrev, Window->head.h);
709 }
710 else /* NULL hwnd passed in */
711 {
712 // if (!hWndPrev) return 0; /* nothing to do */
713
714 if (co_HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)0, (LPARAM)hWndPrev))
715 {
716 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
717 return 0;
718 }
719
720 /* set the current thread focus window null */
721 IntSendFocusMessages( pti, NULL);
722 }
723 return hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0;
724 }
725
726 HWND FASTCALL
727 UserGetForegroundWindow(VOID)
728 {
729 PUSER_MESSAGE_QUEUE ForegroundQueue;
730
731 ForegroundQueue = IntGetFocusMessageQueue();
732 return( ForegroundQueue ? (ForegroundQueue->spwndActive ? UserHMGetHandle(ForegroundQueue->spwndActive) : 0) : 0);
733 }
734
735 HWND FASTCALL UserGetActiveWindow(VOID)
736 {
737 PTHREADINFO pti;
738 PUSER_MESSAGE_QUEUE ThreadQueue;
739
740 pti = PsGetCurrentThreadWin32Thread();
741 ThreadQueue = pti->MessageQueue;
742 return( ThreadQueue ? (ThreadQueue->spwndActive ? UserHMGetHandle(ThreadQueue->spwndActive) : 0) : 0);
743 }
744
745 HWND APIENTRY
746 IntGetCapture(VOID)
747 {
748 PTHREADINFO pti;
749 PUSER_MESSAGE_QUEUE ThreadQueue;
750 DECLARE_RETURN(HWND);
751
752 TRACE("Enter IntGetCapture\n");
753
754 pti = PsGetCurrentThreadWin32Thread();
755 ThreadQueue = pti->MessageQueue;
756 RETURN( ThreadQueue ? (ThreadQueue->spwndCapture ? UserHMGetHandle(ThreadQueue->spwndCapture) : 0) : 0);
757
758 CLEANUP:
759 TRACE("Leave IntGetCapture, ret=%i\n",_ret_);
760 END_CLEANUP;
761 }
762
763 HWND FASTCALL
764 co_UserSetCapture(HWND hWnd)
765 {
766 PTHREADINFO pti;
767 PUSER_MESSAGE_QUEUE ThreadQueue;
768 PWND pWnd, Window = NULL;
769 HWND hWndPrev;
770
771 pti = PsGetCurrentThreadWin32Thread();
772 ThreadQueue = pti->MessageQueue;
773
774 if (ThreadQueue->QF_flags & QF_CAPTURELOCKED)
775 return NULL;
776
777 if (hWnd && (Window = UserGetWindowObject(hWnd)))
778 {
779 if (Window->head.pti->MessageQueue != ThreadQueue)
780 {
781 return NULL;
782 }
783 }
784
785 hWndPrev = MsqSetStateWindow(ThreadQueue, MSQ_STATE_CAPTURE, hWnd);
786
787 if (hWndPrev)
788 {
789 pWnd = UserGetWindowObject(hWndPrev);
790 if (pWnd)
791 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND, pWnd, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI);
792 }
793
794 if (Window)
795 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART, Window, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI);
796
797 if (hWndPrev && hWndPrev != hWnd)
798 {
799 if (ThreadQueue->MenuOwner && Window) ThreadQueue->QF_flags |= QF_CAPTURELOCKED;
800
801 co_IntPostOrSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
802
803 ThreadQueue->QF_flags &= ~QF_CAPTURELOCKED;
804 }
805
806 ThreadQueue->spwndCapture = Window;
807
808 if (hWnd == NULL) // Release mode.
809 {
810 MOUSEINPUT mi;
811 /// These are HACKS!
812 /* Also remove other windows if not capturing anymore */
813 MsqSetStateWindow(ThreadQueue, MSQ_STATE_MENUOWNER, NULL);
814 MsqSetStateWindow(ThreadQueue, MSQ_STATE_MOVESIZE, NULL);
815 ///
816 /* Somebody may have missed some mouse movements */
817 mi.dx = 0;
818 mi.dy = 0;
819 mi.mouseData = 0;
820 mi.dwFlags = MOUSEEVENTF_MOVE;
821 mi.time = 0;
822 mi.dwExtraInfo = 0;
823 UserSendMouseInput(&mi, FALSE);
824 }
825 return hWndPrev;
826 }
827
828 /*
829 API Call
830 */
831 BOOL
832 FASTCALL
833 IntReleaseCapture(VOID)
834 {
835 PTHREADINFO pti;
836 PUSER_MESSAGE_QUEUE ThreadQueue;
837
838 pti = PsGetCurrentThreadWin32Thread();
839 ThreadQueue = pti->MessageQueue;
840
841 // Can not release inside WM_CAPTURECHANGED!!
842 if (ThreadQueue->QF_flags & QF_CAPTURELOCKED) return FALSE;
843
844 co_UserSetCapture(NULL);
845
846 return TRUE;
847 }
848
849 /*
850 API Call
851 */
852 BOOL FASTCALL
853 co_IntSetForegroundWindow(PWND Window)
854 {
855 ASSERT_REFS_CO(Window);
856
857 return co_IntSetForegroundAndFocusWindow(Window, FALSE);
858 }
859
860 /*
861 API Call
862 */
863 BOOL FASTCALL
864 IntLockSetForegroundWindow(UINT uLockCode)
865 {
866 ULONG Err = ERROR_ACCESS_DENIED;
867 PPROCESSINFO ppi = PsGetCurrentProcessWin32Process();
868 switch (uLockCode)
869 {
870 case LSFW_LOCK:
871 if ( CanForceFG(ppi) && !gppiLockSFW )
872 {
873 gppiLockSFW = ppi;
874 return TRUE;
875 }
876 break;
877 case LSFW_UNLOCK:
878 if ( gppiLockSFW == ppi)
879 {
880 gppiLockSFW = NULL;
881 return TRUE;
882 }
883 break;
884 default:
885 Err = ERROR_INVALID_PARAMETER;
886 }
887 EngSetLastError(Err);
888 return FALSE;
889 }
890
891 /*
892 API Call
893 */
894 BOOL FASTCALL
895 IntAllowSetForegroundWindow(DWORD dwProcessId)
896 {
897 PPROCESSINFO ppi, ppiCur;
898 PEPROCESS Process = NULL;
899
900 ppi = NULL;
901 if (dwProcessId != ASFW_ANY)
902 {
903 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)dwProcessId, &Process)))
904 {
905 EngSetLastError(ERROR_INVALID_PARAMETER);
906 return FALSE;
907 }
908 ppi = PsGetProcessWin32Process(Process);
909 if (!ppi)
910 {
911 ObDereferenceObject(Process);
912 return FALSE;
913 }
914 }
915 ppiCur = PsGetCurrentProcessWin32Process();
916 if (!CanForceFG(ppiCur))
917 {
918 if (Process) ObDereferenceObject(Process);
919 EngSetLastError(ERROR_ACCESS_DENIED);
920 return FALSE;
921 }
922 if (dwProcessId == ASFW_ANY)
923 { // All processes will be enabled to set the foreground window.
924 ptiLastInput = NULL;
925 }
926 else
927 { // Rule #3, last input event in force.
928 ptiLastInput = ppi->ptiList;
929 ObDereferenceObject(Process);
930 }
931 return TRUE;
932 }
933
934 /*
935 * @implemented
936 */
937 HWND APIENTRY
938 NtUserGetForegroundWindow(VOID)
939 {
940 DECLARE_RETURN(HWND);
941
942 TRACE("Enter NtUserGetForegroundWindow\n");
943 UserEnterExclusive();
944
945 RETURN( UserGetForegroundWindow());
946
947 CLEANUP:
948 TRACE("Leave NtUserGetForegroundWindow, ret=%i\n",_ret_);
949 UserLeave();
950 END_CLEANUP;
951 }
952
953 HWND APIENTRY
954 NtUserSetActiveWindow(HWND hWnd)
955 {
956 USER_REFERENCE_ENTRY Ref;
957 HWND hWndPrev;
958 PWND Window;
959 DECLARE_RETURN(HWND);
960
961 TRACE("Enter NtUserSetActiveWindow(%x)\n", hWnd);
962 UserEnterExclusive();
963
964 Window = NULL;
965 if (hWnd)
966 {
967 if (!(Window = UserGetWindowObject(hWnd)))
968 {
969 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd);
970 RETURN( NULL);
971 }
972 }
973
974 if (!Window ||
975 Window->head.pti->MessageQueue == gptiCurrent->MessageQueue)
976 {
977 hWndPrev = gptiCurrent->MessageQueue->spwndActive ? UserHMGetHandle(gptiCurrent->MessageQueue->spwndActive) : NULL;
978 if (Window) UserRefObjectCo(Window, &Ref);
979 co_IntSetActiveWindow(Window, NULL, FALSE, TRUE, FALSE);
980 if (Window) UserDerefObjectCo(Window);
981 RETURN( hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0 );
982 }
983 RETURN( NULL);
984
985 CLEANUP:
986 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_);
987 UserLeave();
988 END_CLEANUP;
989 }
990
991 /*
992 * @implemented
993 */
994 HWND APIENTRY
995 NtUserSetCapture(HWND hWnd)
996 {
997 DECLARE_RETURN(HWND);
998
999 TRACE("Enter NtUserSetCapture(%x)\n", hWnd);
1000 UserEnterExclusive();
1001
1002 RETURN( co_UserSetCapture(hWnd));
1003
1004 CLEANUP:
1005 TRACE("Leave NtUserSetCapture, ret=%i\n",_ret_);
1006 UserLeave();
1007 END_CLEANUP;
1008 }
1009
1010 /*
1011 * @implemented
1012 */
1013 HWND APIENTRY
1014 NtUserSetFocus(HWND hWnd)
1015 {
1016 PWND Window;
1017 USER_REFERENCE_ENTRY Ref;
1018 DECLARE_RETURN(HWND);
1019 HWND ret;
1020
1021 TRACE("Enter NtUserSetFocus(%x)\n", hWnd);
1022 UserEnterExclusive();
1023
1024 if (hWnd)
1025 {
1026 if (!(Window = UserGetWindowObject(hWnd)))
1027 {
1028 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd);
1029 RETURN(NULL);
1030 }
1031
1032 UserRefObjectCo(Window, &Ref);
1033 ret = co_UserSetFocus(Window);
1034 UserDerefObjectCo(Window);
1035
1036 RETURN(ret);
1037 }
1038 else
1039 {
1040 RETURN( co_UserSetFocus(0));
1041 }
1042
1043 CLEANUP:
1044 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_);
1045 UserLeave();
1046 END_CLEANUP;
1047 }
1048
1049 /* EOF */