[WIN32K:NTUSER]
[reactos.git] / reactos / 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 (VerifyWnd(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 //ERR("SendActivateMessages\n");
106
107 pti = PsGetCurrentThreadWin32Thread();
108
109 if (Window)
110 {
111 UserRefObjectCo(Window, &Ref);
112
113 if (WindowPrev) UserRefObjectCo(WindowPrev, &RefPrev);
114
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))
119 {
120 UserSendNotifyMessage( HWND_BROADCAST,
121 WM_PALETTEISCHANGING,
122 (WPARAM)UserHMGetHandle(Window),
123 0);
124 }
125 //// Fixes CORE-6434.
126 if (!(Window->style & WS_CHILD))
127 {
128 PWND pwndTemp = co_GetDesktopWindow(Window)->spwndChild;
129
130 while (pwndTemp && !(pwndTemp->style & WS_VISIBLE)) pwndTemp = pwndTemp->spwndNext;
131
132 if (Window != pwndTemp || (WindowPrev && !IntIsWindowVisible(WindowPrev)))
133 {
134 if (!Async || pti->MessageQueue == gpqForeground)
135 {
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);
140 }
141 }
142 }
143 ////
144 //// CORE-1161 and CORE-6651
145 if (Window->spwndPrev)
146 {
147 HWND *phwndTopLevel, *phwndCurrent;
148 PWND pwndCurrent, pwndDesktop;
149
150 pwndDesktop = UserGetDesktopWindow();
151 if (Window->spwndParent == pwndDesktop )
152 {
153 phwndTopLevel = IntWinListChildren(pwndDesktop);
154 phwndCurrent = phwndTopLevel;
155 while(*phwndCurrent)
156 {
157 pwndCurrent = UserGetWindowObject(*phwndCurrent);
158
159 if (pwndCurrent && pwndCurrent->spwndOwner == Window )
160 {
161 co_WinPosSetWindowPos(pwndCurrent, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
162 }
163 phwndCurrent++;
164 }
165 ExFreePool(phwndTopLevel);
166 }
167 }
168 }
169 ////
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;
174
175 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID);
176
177 if (!(pti->TIF_flags & TIF_INACTIVATEAPPMSG) &&
178 (!WindowPrev || OldTID != NewTID) )
179 {
180 PWND cWindow;
181 HWND *List, *phWnd;
182
183 List = IntWinListChildren(UserGetDesktopWindow());
184 if ( List )
185 {
186 if ( OldTID )
187 {
188 ptiOld->TIF_flags |= TIF_INACTIVATEAPPMSG;
189 // Note: Do not set pci flags, this does crash!
190 for (phWnd = List; *phWnd; ++phWnd)
191 {
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);
198 }
199 }
200 ptiOld->TIF_flags &= ~TIF_INACTIVATEAPPMSG;
201 }
202 if ( NewTID )
203 { //// Prevents a resource crash due to reentrance!
204 InAAPM = TRUE;
205 pti->TIF_flags |= TIF_INACTIVATEAPPMSG;
206 ////
207 for (phWnd = List; *phWnd; ++phWnd)
208 {
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);
215 }
216 }
217 }
218 ExFreePoolWithTag(List, USERTAG_WINDOWLIST);
219 }
220 }
221
222 if (Window)
223 {
224 if (WindowPrev)
225 UserDerefObjectCo(WindowPrev); // Now allow the previous window to die.
226
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;
231 }
232
233 if (Window->style & WS_MINIMIZE)
234 {
235 TRACE("Widow was minimized\n");
236 }
237
238 co_IntMakeWindowActive(Window);
239
240 co_IntSendMessageNoWait( UserHMGetHandle(Window),
241 WM_NCACTIVATE,
242 (WPARAM)(Window == (gpqForeground ? gpqForeground->spwndActive : NULL)),
243 0); //(LPARAM)hWndPrev);
244
245 co_IntSendMessageNoWait( UserHMGetHandle(Window),
246 WM_ACTIVATE,
247 MAKEWPARAM(MouseActivate ? WA_CLICKACTIVE : WA_ACTIVE, Window->style & WS_MINIMIZE),
248 (LPARAM)(WindowPrev ? UserHMGetHandle(WindowPrev) : 0));
249
250 if (!Window->spwndOwner && !IntGetParent(Window))
251 {
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);
254 }
255
256 Window->state &= ~WNDS_NONCPAINT;
257
258 UserDerefObjectCo(Window);
259 }
260 return InAAPM;
261 }
262
263 VOID FASTCALL
264 IntSendFocusMessages( PTHREADINFO pti, PWND pWnd)
265 {
266 PWND pWndPrev;
267 PUSER_MESSAGE_QUEUE ThreadQueue = pti->MessageQueue; // Queue can change...
268
269 ThreadQueue->QF_flags &= ~QF_FOCUSNULLSINCEACTIVE;
270 if (!pWnd && ThreadQueue->spwndActive)
271 {
272 ThreadQueue->QF_flags |= QF_FOCUSNULLSINCEACTIVE;
273 }
274
275 pWndPrev = ThreadQueue->spwndFocus;
276
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;
281
282 if (pWnd)
283 {
284 if (pWndPrev)
285 {
286 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
287 co_IntSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
288 }
289 if (ThreadQueue->spwndFocus == pWnd)
290 {
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);
294 }
295 }
296 else
297 {
298 if (pWndPrev)
299 {
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);
303 }
304 }
305 }
306
307 VOID FASTCALL
308 FindRemoveAsyncMsg(PWND Wnd, WPARAM wParam)
309 {
310 PTHREADINFO pti;
311 PUSER_SENT_MESSAGE Message;
312 PLIST_ENTRY Entry;
313
314 if (!Wnd) return;
315
316 pti = Wnd->head.pti;
317
318 Entry = pti->SentMessagesListHead.Flink;
319 while (Entry != &pti->SentMessagesListHead)
320 {
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;
324
325 if (Message->Msg.message == WM_ASYNC_SETACTIVEWINDOW &&
326 Message->Msg.hwnd == UserHMGetHandle(Wnd) &&
327 Message->Msg.wParam == wParam)
328 {
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);
333 }
334 }
335 }
336
337 BOOL FASTCALL
338 ToggleFGActivate(PTHREADINFO pti)
339 {
340 BOOL Ret;
341 PPROCESSINFO ppi = pti->ppi;
342
343 Ret = !!(pti->TIF_flags & TIF_ALLOWFOREGROUNDACTIVATE);
344 if (Ret)
345 {
346 pti->TIF_flags &= ~TIF_ALLOWFOREGROUNDACTIVATE;
347 }
348 else
349 Ret = !!(ppi->W32PF_flags & W32PF_ALLOWFOREGROUNDACTIVATE);
350
351 if (Ret)
352 ppi->W32PF_flags &= ~W32PF_ALLOWFOREGROUNDACTIVATE;
353 //ERR("ToggleFGActivate is %d\n",Ret);
354 return Ret;
355 }
356
357 BOOL FASTCALL
358 IsAllowedFGActive(PTHREADINFO pti, PWND Wnd)
359 {
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.
366 {
367 return FALSE;
368 }
369 //ERR("IsAllowedFGActive is TRUE\n");
370 return TRUE;
371 }
372
373 /*
374 Can the system force foreground from one or more conditions.
375 */
376 BOOL FASTCALL
377 CanForceFG(PPROCESSINFO ppi)
378 {
379 if (!ptiLastInput ||
380 ptiLastInput->ppi == ppi ||
381 !gptiForeground ||
382 gptiForeground->ppi == ppi ||
383 ppi->W32PF_flags & (W32PF_ALLOWFOREGROUNDACTIVATE | W32PF_SETFOREGROUNDALLOWED) ||
384 gppiInputProvider == ppi ||
385 !gpqForeground
386 ) return TRUE;
387 //ERR("CanForceFG is FALSE\n");
388 return FALSE;
389 }
390
391 /*
392 MSDN:
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:
395
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.
404 */
405 static
406 BOOL FASTCALL
407 co_IntSetForegroundAndFocusWindow(
408 _In_ PWND Wnd,
409 _In_ BOOL MouseActivate)
410 {
411 HWND hWnd = Wnd ? UserHMGetHandle(Wnd) : NULL;
412 HWND hWndPrev = NULL;
413 PWND pWndPrev = NULL;
414 PUSER_MESSAGE_QUEUE PrevForegroundQueue;
415 PTHREADINFO pti;
416 BOOL fgRet = FALSE, Ret = FALSE;
417
418 if (Wnd) ASSERT_REFS_CO(Wnd);
419
420 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
421
422 PrevForegroundQueue = IntGetFocusMessageQueue(); // Use this active desktop.
423 pti = PsGetCurrentThreadWin32Thread();
424
425 if (PrevForegroundQueue)
426 { // Same Window Q as foreground just do active.
427 if (Wnd && Wnd->head.pti->MessageQueue == PrevForegroundQueue)
428 {
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);
434 }
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");
438 Ret = TRUE;
439 }
440 else
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 );
444 Ret = TRUE;
445 }
446 return Ret;
447 }
448
449 hWndPrev = PrevForegroundQueue->spwndActive ? UserHMGetHandle(PrevForegroundQueue->spwndActive) : 0;
450 pWndPrev = PrevForegroundQueue->spwndActive;
451 }
452
453 if ( (( !IsFGLocked() || pti->ppi == gppiInputProvider ) &&
454 ( CanForceFG(pti->ppi) || pti->TIF_flags & (TIF_SYSTEMTHREAD|TIF_CSRSSTHREAD|TIF_ALLOWFOREGROUNDACTIVATE) )) ||
455 pti->ppi == ppiScrnSaver
456 )
457 {
458
459 //ToggleFGActivate(pti); // win.c line 2662 fail
460 if (Wnd)
461 {
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);
465 }
466 else
467 {
468 IntSetFocusMessageQueue(NULL);
469 gptiForeground = NULL;
470 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
471 }
472 /*
473 Henri Verbeet,
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
476 window.
477 */
478 //ERR("SFAFW: 1\n");
479 FindRemoveAsyncMsg(Wnd, 0); // Do this to fix test_SFW todos!
480
481 fgRet = TRUE;
482 }
483
484 // Fix FG Bounce with regedit.
485 if (hWndPrev != hWnd )
486 {
487 if (PrevForegroundQueue &&
488 fgRet &&
489 PrevForegroundQueue->spwndActive)
490 {
491 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
492 if (pti->MessageQueue == PrevForegroundQueue)
493 {
494 //ERR("SFGW: TI same as Prev TI\n");
495 co_IntSetActiveWindow(NULL, FALSE, TRUE, FALSE);
496 }
497 else if (pWndPrev)
498 {
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 );
502 }
503 }
504 }
505
506 if (!Wnd) return FALSE; // Always return false.
507
508 if (pti->MessageQueue == Wnd->head.pti->MessageQueue)
509 {
510 //ERR("Same PQ and WQ go active.\n");
511 Ret = co_IntSetActiveWindow(Wnd, MouseActivate, TRUE, FALSE);
512 }
513 else if (Wnd->head.pti->MessageQueue->spwndActive == Wnd)
514 {
515 //ERR("Same Active and Wnd.\n");
516 Ret = TRUE;
517 }
518 else
519 {
520 //ERR("Activate Not same PQ and WQ and Wnd.\n");
521 co_IntSendMessageNoWait(hWnd, WM_ASYNC_SETACTIVEWINDOW, (WPARAM)Wnd, (LPARAM)MouseActivate );
522 Ret = TRUE;
523 }
524 return Ret && fgRet;
525 }
526
527 /*
528 Revision 7888, activate modal dialog when clicking on a disabled window.
529 */
530 HWND FASTCALL
531 IntFindChildWindowToOwner(PWND Root, PWND Owner)
532 {
533 HWND Ret;
534 PWND Child, OwnerWnd;
535
536 for(Child = Root->spwndChild; Child; Child = Child->spwndNext)
537 {
538 OwnerWnd = Child->spwndOwner;
539 if(!OwnerWnd)
540 continue;
541
542 if(OwnerWnd == Owner)
543 {
544 Ret = Child->head.h;
545 return Ret;
546 }
547 }
548 return NULL;
549 }
550
551 BOOL FASTCALL
552 co_IntMouseActivateWindow(PWND Wnd)
553 {
554 HWND Top;
555 USER_REFERENCE_ENTRY Ref;
556 ASSERT_REFS_CO(Wnd);
557
558 if (Wnd->style & WS_DISABLED)
559 {
560 BOOL Ret;
561 PWND TopWnd;
562 PWND DesktopWindow = UserGetDesktopWindow();
563 if (DesktopWindow)
564 {
565 ERR("Window Diabled\n");
566 Top = IntFindChildWindowToOwner(DesktopWindow, Wnd);
567 if ((TopWnd = ValidateHwndNoErr(Top)))
568 {
569 UserRefObjectCo(TopWnd, &Ref);
570 Ret = co_IntMouseActivateWindow(TopWnd);
571 UserDerefObjectCo(TopWnd);
572
573 return Ret;
574 }
575 }
576 return FALSE;
577 }
578 ERR("Mouse Active\n");
579 co_IntSetForegroundAndFocusWindow(Wnd, TRUE);
580 return TRUE;
581 }
582
583 BOOL FASTCALL
584 co_IntSetActiveWindow(PWND Wnd OPTIONAL, BOOL bMouse, BOOL bFocus, BOOL Async)
585 {
586 PTHREADINFO pti;
587 PUSER_MESSAGE_QUEUE ThreadQueue;
588 PWND pWndChg, WndPrev; // State changes.
589 HWND hWndPrev;
590 HWND hWnd = 0;
591 BOOL InAAPM;
592 CBTACTIVATESTRUCT cbt;
593 //ERR("co_IntSetActiveWindow 1\n");
594 if (Wnd)
595 {
596 ASSERT_REFS_CO(Wnd);
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);
601 }
602
603 //ERR("co_IntSetActiveWindow 2\n");
604 pti = PsGetCurrentThreadWin32Thread();
605 ThreadQueue = pti->MessageQueue;
606 ASSERT(ThreadQueue != 0);
607
608 hWndPrev = ThreadQueue->spwndActive ? UserHMGetHandle(ThreadQueue->spwndActive) : NULL;
609
610 pWndChg = ThreadQueue->spwndActive; // Keep to notify of a preemptive switch.
611
612 while (Wnd)
613 {
614 BOOL Ret, DoFG, AllowFG;
615
616 if (Wnd->state & WNDS_BEINGACTIVATED) return TRUE;
617
618 if (ThreadQueue == Wnd->head.pti->MessageQueue)
619 {
620 if (IsAllowedFGActive(pti, Wnd))
621 {
622 DoFG = TRUE;
623 }
624 else
625 {
626 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
627 break;
628 }
629 AllowFG = !pti->cVisWindows; // Nothing is visable.
630 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
631 }
632 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
633 {
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)
638 {
639 DoFG = TRUE;
640 }
641 else
642 DoFG = FALSE;
643 if (DoFG)
644 {
645 if (pti->TIF_flags & TIF_ALLOWFOREGROUNDACTIVATE || pti->cVisWindows)
646 AllowFG = TRUE;
647 else
648 AllowFG = FALSE;
649 }
650 else
651 AllowFG = FALSE;
652 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
653 }
654 Ret = FALSE;
655 if (DoFG)
656 {
657 pti->TIF_flags |= TIF_ALLOWFOREGROUNDACTIVATE;
658 //ERR("co_IntSetActiveWindow 3c FG set\n");
659 Ret = co_IntSetForegroundAndFocusWindow(Wnd, bMouse);
660 if (AllowFG)
661 {
662 pti->TIF_flags |= TIF_ALLOWFOREGROUNDACTIVATE;
663 }
664 else
665 {
666 pti->TIF_flags &= ~TIF_ALLOWFOREGROUNDACTIVATE;
667 }
668 }
669 return Ret;
670 }
671
672 /* Call CBT hook chain */
673 cbt.fMouse = bMouse;
674 cbt.hWndActive = hWndPrev;
675 if (co_HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd, (LPARAM)&cbt))
676 {
677 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
678 return FALSE;
679 }
680
681 if ( ThreadQueue->spwndActive && ThreadQueue->spwndActive->state & WNDS_DESTROYED )
682 ThreadQueue->spwndActive = NULL;
683 else
684 ThreadQueue->spwndActivePrev = ThreadQueue->spwndActive;
685
686 WndPrev = ThreadQueue->spwndActive; // Keep to save changing active.
687
688 if (WndPrev)
689 {
690 if (ThreadQueue == gpqForeground) gpqForegroundPrev = ThreadQueue;
691 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev), hWnd)) return FALSE;
692 }
693
694 WndPrev = ThreadQueue->spwndActive; // Again keep to save changing active.
695
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 )
703 {
704 ERR("SetActiveWindow: Summery ERROR, active state changed!\n");
705 return FALSE;
706 }
707
708 if (!WndPrev) ThreadQueue->QF_flags &= ~QF_FOCUSNULLSINCEACTIVE;
709
710 if (Wnd) Wnd->state |= WNDS_BEINGACTIVATED;
711
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.
715
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)
718 {
719 /* set the current thread active window */
720 ThreadQueue->spwndActive = Wnd;
721 }
722
723 WndPrev = VerifyWnd(ThreadQueue->spwndActivePrev); // Now should be set but verify it again.
724
725 InAAPM = co_IntSendActivateMessages(WndPrev, Wnd, bMouse, Async);
726
727 /* now change focus if necessary */
728 //// Fixes CORE-6452 allows setting focus on window.
729 if (bFocus && !(ThreadQueue->QF_flags & QF_FOCUSNULLSINCEACTIVE))
730 {
731 /* Do not change focus if the window is no longer active */
732 if (pti->MessageQueue->spwndActive != IntGetNonChildAncestor(pti->MessageQueue->spwndFocus))
733 {
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);
739 }
740 }
741 ////
742 if (InAAPM)
743 {
744 pti->TIF_flags &= ~TIF_INACTIVATEAPPMSG;
745 }
746
747 // FIXME: Used in the menu loop!!!
748 //ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
749
750 //ERR("co_IntSetActiveWindow Exit\n");
751 if (Wnd) Wnd->state &= ~WNDS_BEINGACTIVATED;
752 return (ThreadQueue->spwndActive == Wnd);
753 }
754
755 BOOL FASTCALL
756 UserSetActiveWindow(PWND Wnd)
757 {
758 if (Wnd) // Must have a window!
759 {
760 if ((Wnd->style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
761
762 return co_IntSetActiveWindow(Wnd, FALSE, TRUE, FALSE);
763 }
764 /*
765 Yes your eye are not deceiving you~!
766
767 First part of wines Win.c test_SetActiveWindow:
768
769 flush_events( TRUE );
770 ShowWindow(hwnd, SW_HIDE);
771 SetFocus(0);
772 SetActiveWindow(0);
773 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
774
775 */
776 return FALSE;
777 }
778
779 HWND FASTCALL
780 co_UserSetFocus(PWND Window)
781 {
782 HWND hWndPrev = 0;
783 PWND pwndTop;
784 PTHREADINFO pti;
785 PUSER_MESSAGE_QUEUE ThreadQueue;
786
787 if (Window)
788 ASSERT_REFS_CO(Window);
789
790 pti = PsGetCurrentThreadWin32Thread();
791 ThreadQueue = pti->MessageQueue;
792 ASSERT(ThreadQueue != 0);
793
794 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window ? UserHMGetHandle(Window) : 0, pti );
795
796 hWndPrev = ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0;
797
798 if (Window != 0)
799 {
800 if (hWndPrev == UserHMGetHandle(Window))
801 {
802 return hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0; /* Nothing to do */
803 }
804
805 if (Window->head.pti->MessageQueue != ThreadQueue)
806 {
807 ERR("SetFocus Must have the same Q!\n");
808 return 0;
809 }
810
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)
814 {
815 if (pwndTop->style & (WS_MINIMIZED|WS_DISABLED)) return 0;
816 if ((pwndTop->style & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
817 }
818 ////
819 if (co_HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)Window->head.h, (LPARAM)hWndPrev))
820 {
821 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
822 return 0;
823 }
824
825 /* Activate pwndTop if needed. */
826 if (pwndTop != ThreadQueue->spwndActive)
827 {
828 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue(); // Keep it based on desktop.
829 if (ThreadQueue != ForegroundQueue && IsAllowedFGActive(pti, pwndTop)) // Rule 2 & 3.
830 {
831 //ERR("SetFocus: Set Foreground!\n");
832 if (!(pwndTop->style & WS_VISIBLE))
833 {
834 pti->ppi->W32PF_flags |= W32PF_ALLOWFOREGROUNDACTIVATE;
835 }
836 if (!co_IntSetForegroundAndFocusWindow(pwndTop, FALSE))
837 {
838 ERR("SetFocus: Set Foreground and Focus Failed!\n");
839 return 0;
840 }
841 }
842
843 /* Set Active when it is needed. */
844 if (pwndTop != ThreadQueue->spwndActive)
845 {
846 //ERR("SetFocus: Set Active!\n");
847 if (!co_IntSetActiveWindow(pwndTop, FALSE, FALSE, FALSE))
848 {
849 ERR("SetFocus: Set Active Failed!\n");
850 return 0;
851 }
852 }
853
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)
858 {
859 ERR("SetFocus: Top window did not go active!\n");
860 return 0;
861 }
862 }
863
864 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
865 hWndPrev = ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0;
866
867 IntSendFocusMessages( pti, Window);
868
869 TRACE("Focus: %p -> %p\n", hWndPrev, Window->head.h);
870 }
871 else /* NULL hwnd passed in */
872 {
873 if (co_HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)0, (LPARAM)hWndPrev))
874 {
875 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
876 return 0;
877 }
878
879 /* set the current thread focus window null */
880 IntSendFocusMessages( pti, NULL);
881 }
882 return hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0;
883 }
884
885 HWND FASTCALL
886 UserGetForegroundWindow(VOID)
887 {
888 PUSER_MESSAGE_QUEUE ForegroundQueue;
889
890 ForegroundQueue = IntGetFocusMessageQueue();
891 return( ForegroundQueue ? (ForegroundQueue->spwndActive ? UserHMGetHandle(ForegroundQueue->spwndActive) : 0) : 0);
892 }
893
894 HWND FASTCALL UserGetActiveWindow(VOID)
895 {
896 PTHREADINFO pti;
897 PUSER_MESSAGE_QUEUE ThreadQueue;
898
899 pti = PsGetCurrentThreadWin32Thread();
900 ThreadQueue = pti->MessageQueue;
901 return( ThreadQueue ? (ThreadQueue->spwndActive ? UserHMGetHandle(ThreadQueue->spwndActive) : 0) : 0);
902 }
903
904 HWND APIENTRY
905 IntGetCapture(VOID)
906 {
907 PTHREADINFO pti;
908 PUSER_MESSAGE_QUEUE ThreadQueue;
909 DECLARE_RETURN(HWND);
910
911 TRACE("Enter IntGetCapture\n");
912
913 pti = PsGetCurrentThreadWin32Thread();
914 ThreadQueue = pti->MessageQueue;
915 RETURN( ThreadQueue ? (ThreadQueue->spwndCapture ? UserHMGetHandle(ThreadQueue->spwndCapture) : 0) : 0);
916
917 CLEANUP:
918 TRACE("Leave IntGetCapture, ret=%p\n", _ret_);
919 END_CLEANUP;
920 }
921
922 HWND FASTCALL
923 co_UserSetCapture(HWND hWnd)
924 {
925 PTHREADINFO pti;
926 PUSER_MESSAGE_QUEUE ThreadQueue;
927 PWND pWnd, Window = NULL;
928 HWND hWndPrev;
929
930 pti = PsGetCurrentThreadWin32Thread();
931 ThreadQueue = pti->MessageQueue;
932
933 if (ThreadQueue->QF_flags & QF_CAPTURELOCKED)
934 return NULL;
935
936 if (hWnd && (Window = UserGetWindowObject(hWnd)))
937 {
938 if (Window->head.pti->MessageQueue != ThreadQueue)
939 {
940 return NULL;
941 }
942 }
943
944 hWndPrev = MsqSetStateWindow(pti, MSQ_STATE_CAPTURE, hWnd);
945
946 if (hWndPrev)
947 {
948 pWnd = UserGetWindowObject(hWndPrev);
949 if (pWnd)
950 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND, pWnd, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI);
951 }
952
953 if (Window)
954 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART, Window, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI);
955
956 if (hWndPrev && hWndPrev != hWnd)
957 {
958 if (ThreadQueue->MenuOwner && Window) ThreadQueue->QF_flags |= QF_CAPTURELOCKED;
959
960 //co_IntPostOrSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
961 co_IntSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
962
963 ThreadQueue->QF_flags &= ~QF_CAPTURELOCKED;
964 }
965
966 ThreadQueue->spwndCapture = Window;
967
968 if (hWnd == NULL) // Release mode.
969 {
970 MOUSEINPUT mi;
971 /// These are HACKS!
972 /* Also remove other windows if not capturing anymore */
973 MsqSetStateWindow(pti, MSQ_STATE_MENUOWNER, NULL);
974 MsqSetStateWindow(pti, MSQ_STATE_MOVESIZE, NULL);
975 ///
976 /* Somebody may have missed some mouse movements */
977 mi.dx = 0;
978 mi.dy = 0;
979 mi.mouseData = 0;
980 mi.dwFlags = MOUSEEVENTF_MOVE;
981 mi.time = 0;
982 mi.dwExtraInfo = 0;
983 UserSendMouseInput(&mi, FALSE);
984 }
985 return hWndPrev;
986 }
987
988 /*
989 API Call
990 */
991 BOOL
992 FASTCALL
993 IntReleaseCapture(VOID)
994 {
995 PTHREADINFO pti;
996 PUSER_MESSAGE_QUEUE ThreadQueue;
997
998 pti = PsGetCurrentThreadWin32Thread();
999 ThreadQueue = pti->MessageQueue;
1000
1001 // Can not release inside WM_CAPTURECHANGED!!
1002 if (ThreadQueue->QF_flags & QF_CAPTURELOCKED) return FALSE;
1003
1004 co_UserSetCapture(NULL);
1005
1006 return TRUE;
1007 }
1008
1009 /*
1010 API Call
1011 */
1012 BOOL FASTCALL
1013 co_IntSetForegroundWindow(PWND Window)
1014 {
1015 if (Window) ASSERT_REFS_CO(Window);
1016
1017 return co_IntSetForegroundAndFocusWindow(Window, FALSE);
1018 }
1019
1020 /*
1021 API Call
1022 */
1023 BOOL FASTCALL
1024 co_IntSetForegroundWindowMouse(PWND Window)
1025 {
1026 if (Window) ASSERT_REFS_CO(Window);
1027
1028 return co_IntSetForegroundAndFocusWindow(Window, TRUE);
1029 }
1030
1031 /*
1032 API Call
1033 */
1034 BOOL FASTCALL
1035 IntLockSetForegroundWindow(UINT uLockCode)
1036 {
1037 ULONG Err = ERROR_ACCESS_DENIED;
1038 PPROCESSINFO ppi = PsGetCurrentProcessWin32Process();
1039 switch (uLockCode)
1040 {
1041 case LSFW_LOCK:
1042 if ( CanForceFG(ppi) && !gppiLockSFW )
1043 {
1044 gppiLockSFW = ppi;
1045 return TRUE;
1046 }
1047 break;
1048 case LSFW_UNLOCK:
1049 if ( gppiLockSFW == ppi)
1050 {
1051 gppiLockSFW = NULL;
1052 return TRUE;
1053 }
1054 break;
1055 default:
1056 Err = ERROR_INVALID_PARAMETER;
1057 }
1058 EngSetLastError(Err);
1059 return FALSE;
1060 }
1061
1062 /*
1063 API Call
1064 */
1065 BOOL FASTCALL
1066 IntAllowSetForegroundWindow(DWORD dwProcessId)
1067 {
1068 PPROCESSINFO ppi, ppiCur;
1069 PEPROCESS Process = NULL;
1070
1071 ppi = NULL;
1072 if (dwProcessId != ASFW_ANY)
1073 {
1074 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)dwProcessId, &Process)))
1075 {
1076 EngSetLastError(ERROR_INVALID_PARAMETER);
1077 return FALSE;
1078 }
1079 ppi = PsGetProcessWin32Process(Process);
1080 if (!ppi)
1081 {
1082 ObDereferenceObject(Process);
1083 return FALSE;
1084 }
1085 }
1086 ppiCur = PsGetCurrentProcessWin32Process();
1087 if (!CanForceFG(ppiCur))
1088 {
1089 if (Process) ObDereferenceObject(Process);
1090 EngSetLastError(ERROR_ACCESS_DENIED);
1091 return FALSE;
1092 }
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;
1097 }
1098 else
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);
1103 }
1104 return TRUE;
1105 }
1106
1107 /*
1108 * @implemented
1109 */
1110 HWND APIENTRY
1111 NtUserGetForegroundWindow(VOID)
1112 {
1113 DECLARE_RETURN(HWND);
1114
1115 TRACE("Enter NtUserGetForegroundWindow\n");
1116 UserEnterExclusive();
1117
1118 RETURN( UserGetForegroundWindow());
1119
1120 CLEANUP:
1121 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_);
1122 UserLeave();
1123 END_CLEANUP;
1124 }
1125
1126 HWND APIENTRY
1127 NtUserSetActiveWindow(HWND hWnd)
1128 {
1129 USER_REFERENCE_ENTRY Ref;
1130 HWND hWndPrev;
1131 PWND Window;
1132 DECLARE_RETURN(HWND);
1133
1134 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd);
1135 UserEnterExclusive();
1136
1137 Window = NULL;
1138 if (hWnd)
1139 {
1140 if (!(Window = UserGetWindowObject(hWnd)))
1141 {
1142 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd);
1143 RETURN( NULL);
1144 }
1145 }
1146
1147 if (!Window ||
1148 Window->head.pti->MessageQueue == gptiCurrent->MessageQueue)
1149 {
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 );
1155 }
1156 RETURN( NULL);
1157
1158 CLEANUP:
1159 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_);
1160 UserLeave();
1161 END_CLEANUP;
1162 }
1163
1164 /*
1165 * @implemented
1166 */
1167 HWND APIENTRY
1168 NtUserSetCapture(HWND hWnd)
1169 {
1170 DECLARE_RETURN(HWND);
1171
1172 TRACE("Enter NtUserSetCapture(%p)\n", hWnd);
1173 UserEnterExclusive();
1174
1175 RETURN( co_UserSetCapture(hWnd));
1176
1177 CLEANUP:
1178 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_);
1179 UserLeave();
1180 END_CLEANUP;
1181 }
1182
1183 /*
1184 * @implemented
1185 */
1186 HWND APIENTRY
1187 NtUserSetFocus(HWND hWnd)
1188 {
1189 PWND Window;
1190 USER_REFERENCE_ENTRY Ref;
1191 DECLARE_RETURN(HWND);
1192 HWND ret;
1193
1194 TRACE("Enter NtUserSetFocus(%p)\n", hWnd);
1195 UserEnterExclusive();
1196
1197 if (hWnd)
1198 {
1199 if (!(Window = UserGetWindowObject(hWnd)))
1200 {
1201 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd);
1202 RETURN(NULL);
1203 }
1204
1205 UserRefObjectCo(Window, &Ref);
1206 ret = co_UserSetFocus(Window);
1207 UserDerefObjectCo(Window);
1208
1209 RETURN(ret);
1210 }
1211 else
1212 {
1213 RETURN( co_UserSetFocus(0));
1214 }
1215
1216 CLEANUP:
1217 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_);
1218 UserLeave();
1219 END_CLEANUP;
1220 }
1221
1222 /* EOF */