2 * ReactOS Win32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 IntGetCaptureWindow(VOID
)
29 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue();
30 return ForegroundQueue
!= NULL
? ForegroundQueue
->CaptureWindow
: 0;
34 IntGetFocusWindow(VOID
)
36 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue();
37 return ForegroundQueue
!= NULL
? ForegroundQueue
->FocusWindow
: 0;
41 IntGetThreadFocusWindow(VOID
)
44 PUSER_MESSAGE_QUEUE ThreadQueue
;
46 pti
= PsGetCurrentThreadWin32Thread();
47 ThreadQueue
= pti
->MessageQueue
;
48 return ThreadQueue
!= NULL
? ThreadQueue
->FocusWindow
: 0;
52 co_IntSendDeactivateMessages(HWND hWndPrev
, HWND hWnd
)
54 PWINDOW_OBJECT WndPrev
;
56 if (hWndPrev
&& (WndPrev
= UserGetWindowObject(hWndPrev
)))
58 co_IntSendMessageNoWait(hWndPrev
, WM_NCACTIVATE
, FALSE
, 0);
59 co_IntSendMessageNoWait(hWndPrev
, WM_ACTIVATE
,
60 MAKEWPARAM(WA_INACTIVE
, WndPrev
->Wnd
->style
& WS_MINIMIZE
),
66 co_IntSendActivateMessages(HWND hWndPrev
, HWND hWnd
, BOOL MouseActivate
)
68 USER_REFERENCE_ENTRY Ref
, RefPrev
;
69 PWINDOW_OBJECT Window
, WindowPrev
= NULL
;
71 if ((Window
= UserGetWindowObject(hWnd
)))
73 UserRefObjectCo(Window
, &Ref
);
75 WindowPrev
= UserGetWindowObject(hWndPrev
);
77 if (WindowPrev
) UserRefObjectCo(WindowPrev
, &RefPrev
);
79 /* Send palette messages */
80 if (co_IntPostOrSendMessage(hWnd
, WM_QUERYNEWPALETTE
, 0, 0))
82 UserPostMessage( HWND_BROADCAST
,
88 if (Window
->spwndPrev
!= NULL
)
89 co_WinPosSetWindowPos(Window
, HWND_TOP
, 0, 0, 0, 0,
90 SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
| SWP_NOSENDCHANGING
);
92 if (!Window
->spwndOwner
&& !IntGetParent(Window
))
94 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (LPARAM
) hWnd
);
98 { // Set last active for window and it's owner.
99 Window
->Wnd
->hWndLastActive
= hWnd
;
100 if (Window
->Wnd
->spwndOwner
)
101 Window
->Wnd
->spwndOwner
->hWndLastActive
= hWnd
;
102 Window
->Wnd
->state
|= WNDS_ACTIVEFRAME
;
105 if (WindowPrev
&& WindowPrev
->Wnd
)
106 WindowPrev
->Wnd
->state
&= ~WNDS_ACTIVEFRAME
;
108 if (Window
&& WindowPrev
)
110 PWINDOW_OBJECT cWindow
;
112 HANDLE OldTID
= IntGetWndThreadId(WindowPrev
);
113 HANDLE NewTID
= IntGetWndThreadId(Window
);
115 DPRINT("SendActiveMessage Old -> %x, New -> %x\n", OldTID
, NewTID
);
116 if (Window
->Wnd
->style
& WS_MINIMIZE
)
118 DPRINT("Widow was minimized\n");
121 if (OldTID
!= NewTID
)
123 List
= IntWinListChildren(UserGetWindowObject(IntGetDesktopWindow()));
126 for (phWnd
= List
; *phWnd
; ++phWnd
)
128 cWindow
= UserGetWindowObject(*phWnd
);
130 if (cWindow
&& (IntGetWndThreadId(cWindow
) == OldTID
))
131 { // FALSE if the window is being deactivated,
132 // ThreadId that owns the window being activated.
133 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
136 for (phWnd
= List
; *phWnd
; ++phWnd
)
138 cWindow
= UserGetWindowObject(*phWnd
);
139 if (cWindow
&& (IntGetWndThreadId(cWindow
) == NewTID
))
140 { // TRUE if the window is being activated,
141 // ThreadId that owns the window being deactivated.
142 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
148 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
151 UserDerefObjectCo(Window
);
153 /* FIXME: IntIsWindow */
154 co_IntSendMessageNoWait(hWnd
, WM_NCACTIVATE
, (WPARAM
)(hWnd
== UserGetForegroundWindow()), 0);
155 /* FIXME: WA_CLICKACTIVE */
156 co_IntSendMessageNoWait(hWnd
, WM_ACTIVATE
,
157 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
,
158 Window
->Wnd
->style
& WS_MINIMIZE
),
164 co_IntSendKillFocusMessages(HWND hWndPrev
, HWND hWnd
)
168 co_IntPostOrSendMessage(hWndPrev
, WM_KILLFOCUS
, (WPARAM
)hWnd
, 0);
173 co_IntSendSetFocusMessages(HWND hWndPrev
, HWND hWnd
)
177 co_IntPostOrSendMessage(hWnd
, WM_SETFOCUS
, (WPARAM
)hWndPrev
, 0);
182 IntFindChildWindowToOwner(PWINDOW_OBJECT Root
, PWINDOW_OBJECT Owner
)
185 PWINDOW_OBJECT Child
, OwnerWnd
;
187 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
189 OwnerWnd
= Child
->spwndOwner
;
193 if(OwnerWnd
== Owner
)
204 co_IntSetForegroundAndFocusWindow(PWINDOW_OBJECT Window
, PWINDOW_OBJECT FocusWindow
, BOOL MouseActivate
)
206 HWND hWnd
= Window
->hSelf
;
207 HWND hWndPrev
= NULL
;
208 HWND hWndFocus
= FocusWindow
->hSelf
;
209 HWND hWndFocusPrev
= NULL
;
210 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
213 ASSERT_REFS_CO(Window
);
215 DPRINT("IntSetForegroundAndFocusWindow(%x, %x, %s)\n", hWnd
, hWndFocus
, MouseActivate
? "TRUE" : "FALSE");
219 if ((Wnd
->style
& (WS_CHILD
| WS_POPUP
)) == WS_CHILD
)
221 DPRINT("Failed - Child\n");
225 if (0 == (Wnd
->style
& WS_VISIBLE
) &&
226 Window
->pti
->pEThread
->ThreadsProcess
!= CsrProcess
)
228 DPRINT("Failed - Invisible\n");
232 PrevForegroundQueue
= IntGetFocusMessageQueue();
233 if (PrevForegroundQueue
!= 0)
235 hWndPrev
= PrevForegroundQueue
->ActiveWindow
;
236 hWndFocusPrev
= PrevForegroundQueue
->FocusWindow
;
239 if (hWndPrev
== hWnd
)
241 DPRINT("Failed - Same\n");
245 /* FIXME: Call hooks. */
247 co_IntSendDeactivateMessages(hWndPrev
, hWnd
);
248 co_IntSendKillFocusMessages(hWndFocusPrev
, hWndFocus
);
251 IntSetFocusMessageQueue(Window
->pti
->MessageQueue
);
253 if (Window
->pti
->MessageQueue
)
255 Window
->pti
->MessageQueue
->ActiveWindow
= hWnd
;
258 if (FocusWindow
->pti
->MessageQueue
)
260 FocusWindow
->pti
->MessageQueue
->FocusWindow
= hWndFocus
;
263 if (PrevForegroundQueue
!= Window
->pti
->MessageQueue
)
265 /* FIXME: Send WM_ACTIVATEAPP to all thread windows. */
268 co_IntSendSetFocusMessages(hWndFocusPrev
, hWndFocus
);
269 co_IntSendActivateMessages(hWndPrev
, hWnd
, MouseActivate
);
275 co_IntSetForegroundWindow(PWINDOW_OBJECT Window
)//FIXME: can Window be NULL??
277 /*if (Window)*/ ASSERT_REFS_CO(Window
);
279 return co_IntSetForegroundAndFocusWindow(Window
, Window
, FALSE
);
283 co_IntMouseActivateWindow(PWINDOW_OBJECT Window
)
286 PWINDOW_OBJECT TopWindow
;
287 USER_REFERENCE_ENTRY Ref
;
290 ASSERT_REFS_CO(Window
);
293 if(Wnd
->style
& WS_DISABLED
)
296 PWINDOW_OBJECT TopWnd
;
297 PWINDOW_OBJECT DesktopWindow
= UserGetWindowObject(IntGetDesktopWindow());
300 Top
= IntFindChildWindowToOwner(DesktopWindow
, Window
);
301 if((TopWnd
= UserGetWindowObject(Top
)))
303 UserRefObjectCo(TopWnd
, &Ref
);
304 Ret
= co_IntMouseActivateWindow(TopWnd
);
305 UserDerefObjectCo(TopWnd
);
314 TopWindow
= UserGetAncestor(Window
, GA_ROOT
);
315 if (!TopWindow
) return FALSE
;
317 /* TMN: Check return valud from this function? */
318 UserRefObjectCo(TopWindow
, &Ref
);
320 co_IntSetForegroundAndFocusWindow(TopWindow
, Window
, TRUE
);
322 UserDerefObjectCo(TopWindow
);
328 co_IntSetActiveWindow(PWINDOW_OBJECT Window OPTIONAL
)
331 PUSER_MESSAGE_QUEUE ThreadQueue
;
335 CBTACTIVATESTRUCT cbt
;
338 ASSERT_REFS_CO(Window
);
340 pti
= PsGetCurrentThreadWin32Thread();
341 ThreadQueue
= pti
->MessageQueue
;
342 ASSERT(ThreadQueue
!= 0);
347 if ((!(Wnd
->style
& WS_VISIBLE
) &&
348 Window
->pti
->pEThread
->ThreadsProcess
!= CsrProcess
) ||
349 (Wnd
->style
& (WS_POPUP
| WS_CHILD
)) == WS_CHILD
)
351 return ThreadQueue
? 0 : ThreadQueue
->ActiveWindow
;
353 hWnd
= Window
->hSelf
;
356 hWndPrev
= ThreadQueue
->ActiveWindow
;
357 if (hWndPrev
== hWnd
)
362 /* call CBT hook chain */
364 cbt
.hWndActive
= hWndPrev
;
365 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
368 ThreadQueue
->ActiveWindow
= hWnd
;
370 co_IntSendDeactivateMessages(hWndPrev
, hWnd
);
371 co_IntSendActivateMessages(hWndPrev
, hWnd
, FALSE
);
374 /* return IntIsWindow(hWndPrev) ? hWndPrev : 0;*/
380 co_IntSetFocusWindow(PWINDOW_OBJECT Window OPTIONAL
)
384 PUSER_MESSAGE_QUEUE ThreadQueue
;
387 ASSERT_REFS_CO(Window
);
389 pti
= PsGetCurrentThreadWin32Thread();
390 ThreadQueue
= pti
->MessageQueue
;
391 ASSERT(ThreadQueue
!= 0);
393 hWndPrev
= ThreadQueue
->FocusWindow
;
397 if (hWndPrev
== Window
->hSelf
)
402 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->hSelf
, (LPARAM
)hWndPrev
))
405 ThreadQueue
->FocusWindow
= Window
->hSelf
;
407 co_IntSendKillFocusMessages(hWndPrev
, Window
->hSelf
);
408 co_IntSendSetFocusMessages(hWndPrev
, Window
->hSelf
);
412 ThreadQueue
->FocusWindow
= 0;
414 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
417 co_IntSendKillFocusMessages(hWndPrev
, 0);
427 UserGetForegroundWindow(VOID
)
429 PUSER_MESSAGE_QUEUE ForegroundQueue
;
431 ForegroundQueue
= IntGetFocusMessageQueue();
432 return( ForegroundQueue
!= NULL
? ForegroundQueue
->ActiveWindow
: 0);
440 NtUserGetForegroundWindow(VOID
)
442 DECLARE_RETURN(HWND
);
444 DPRINT("Enter NtUserGetForegroundWindow\n");
445 UserEnterExclusive();
447 RETURN( UserGetForegroundWindow());
450 DPRINT("Leave NtUserGetForegroundWindow, ret=%i\n",_ret_
);
456 HWND FASTCALL
UserGetActiveWindow(VOID
)
459 PUSER_MESSAGE_QUEUE ThreadQueue
;
461 pti
= PsGetCurrentThreadWin32Thread();
462 ThreadQueue
= pti
->MessageQueue
;
463 return( ThreadQueue
? ThreadQueue
->ActiveWindow
: 0);
468 NtUserSetActiveWindow(HWND hWnd
)
470 USER_REFERENCE_ENTRY Ref
;
471 DECLARE_RETURN(HWND
);
473 DPRINT("Enter NtUserSetActiveWindow(%x)\n", hWnd
);
474 UserEnterExclusive();
478 PWINDOW_OBJECT Window
;
480 PUSER_MESSAGE_QUEUE ThreadQueue
;
483 if (!(Window
= UserGetWindowObject(hWnd
)))
488 pti
= PsGetCurrentThreadWin32Thread();
489 ThreadQueue
= pti
->MessageQueue
;
491 if (Window
->pti
->MessageQueue
!= ThreadQueue
)
493 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
497 UserRefObjectCo(Window
, &Ref
);
498 hWndPrev
= co_IntSetActiveWindow(Window
);
499 UserDerefObjectCo(Window
);
505 RETURN( co_IntSetActiveWindow(0));
509 DPRINT("Leave NtUserSetActiveWindow, ret=%i\n",_ret_
);
521 PUSER_MESSAGE_QUEUE ThreadQueue
;
522 DECLARE_RETURN(HWND
);
524 DPRINT("Enter IntGetCapture\n");
526 pti
= PsGetCurrentThreadWin32Thread();
527 ThreadQueue
= pti
->MessageQueue
;
528 RETURN( ThreadQueue
? ThreadQueue
->CaptureWindow
: 0);
531 DPRINT("Leave IntGetCapture, ret=%i\n",_ret_
);
539 NtUserSetCapture(HWND hWnd
)
542 PUSER_MESSAGE_QUEUE ThreadQueue
;
543 PWINDOW_OBJECT Window
;
545 DECLARE_RETURN(HWND
);
547 DPRINT("Enter NtUserSetCapture(%x)\n", hWnd
);
548 UserEnterExclusive();
550 pti
= PsGetCurrentThreadWin32Thread();
551 ThreadQueue
= pti
->MessageQueue
;
553 if((Window
= UserGetWindowObject(hWnd
)))
555 if(Window
->pti
->MessageQueue
!= ThreadQueue
)
561 hWndPrev
= MsqSetStateWindow(ThreadQueue
, MSQ_STATE_CAPTURE
, hWnd
);
563 /* also remove other windows if not capturing anymore */
566 MsqSetStateWindow(ThreadQueue
, MSQ_STATE_MENUOWNER
, NULL
);
567 MsqSetStateWindow(ThreadQueue
, MSQ_STATE_MOVESIZE
, NULL
);
570 co_IntPostOrSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
571 ThreadQueue
->CaptureWindow
= hWnd
;
576 DPRINT("Leave NtUserSetCapture, ret=%i\n",_ret_
);
583 HWND FASTCALL
co_UserSetFocus(PWINDOW_OBJECT Window OPTIONAL
)
588 PUSER_MESSAGE_QUEUE ThreadQueue
;
590 PWINDOW_OBJECT TopWnd
;
591 USER_REFERENCE_ENTRY Ref
;
594 ASSERT_REFS_CO(Window
);
596 pti
= PsGetCurrentThreadWin32Thread();
597 ThreadQueue
= pti
->MessageQueue
;
600 if (Wnd
->style
& (WS_MINIMIZE
| WS_DISABLED
))
602 return( (ThreadQueue
? ThreadQueue
->FocusWindow
: 0));
605 if (Window
->pti
->MessageQueue
!= ThreadQueue
)
607 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
611 TopWnd
= UserGetAncestor(Window
, GA_ROOT
);
612 if (TopWnd
&& TopWnd
->hSelf
!= UserGetActiveWindow())
614 // PWINDOW_OBJECT WndTops = UserGetWindowObject(hWndTop);
615 UserRefObjectCo(TopWnd
, &Ref
);
616 co_IntSetActiveWindow(TopWnd
);
617 UserDerefObjectCo(TopWnd
);
620 hWndPrev
= co_IntSetFocusWindow(Window
);
626 return( co_IntSetFocusWindow(NULL
));
636 NtUserSetFocus(HWND hWnd
)
638 PWINDOW_OBJECT Window
;
639 USER_REFERENCE_ENTRY Ref
;
640 DECLARE_RETURN(HWND
);
643 DPRINT("Enter NtUserSetFocus(%x)\n", hWnd
);
644 UserEnterExclusive();
648 if (!(Window
= UserGetWindowObject(hWnd
)))
653 UserRefObjectCo(Window
, &Ref
);
654 ret
= co_UserSetFocus(Window
);
655 UserDerefObjectCo(Window
);
661 RETURN( co_UserSetFocus(0));
665 DPRINT("Leave NtUserSetFocus, ret=%i\n",_ret_
);