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
)
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
->style
& WS_MINIMIZE
),
66 co_IntSendActivateMessages(HWND hWndPrev
, HWND hWnd
, BOOL MouseActivate
)
68 USER_REFERENCE_ENTRY Ref
, RefPrev
;
69 PWND 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
->hWndLastActive
= hWnd
;
100 if (Window
->spwndOwner
)
101 Window
->spwndOwner
->hWndLastActive
= hWnd
;
102 Window
->state
|= WNDS_ACTIVEFRAME
;
106 WindowPrev
->state
&= ~WNDS_ACTIVEFRAME
;
108 if (Window
&& WindowPrev
)
112 HANDLE OldTID
= IntGetWndThreadId(WindowPrev
);
113 HANDLE NewTID
= IntGetWndThreadId(Window
);
115 DPRINT("SendActiveMessage Old -> %x, New -> %x\n", OldTID
, NewTID
);
116 if (Window
->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
->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(PWND Root
, PWND Owner
)
185 PWND Child
, OwnerWnd
;
187 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
189 OwnerWnd
= Child
->spwndOwner
;
193 if(OwnerWnd
== Owner
)
204 co_IntSetForegroundAndFocusWindow(PWND Wnd
, PWND FocusWindow
, BOOL MouseActivate
)
206 HWND hWnd
= Wnd
->head
.h
;
207 HWND hWndPrev
= NULL
;
208 HWND hWndFocus
= FocusWindow
->head
.h
;
209 HWND hWndFocusPrev
= NULL
;
210 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
214 DPRINT("IntSetForegroundAndFocusWindow(%x, %x, %s)\n", hWnd
, hWndFocus
, MouseActivate
? "TRUE" : "FALSE");
216 if ((Wnd
->style
& (WS_CHILD
| WS_POPUP
)) == WS_CHILD
)
218 DPRINT("Failed - Child\n");
222 if (0 == (Wnd
->style
& WS_VISIBLE
) &&
223 Wnd
->head
.pti
->pEThread
->ThreadsProcess
!= CsrProcess
)
225 DPRINT("Failed - Invisible\n");
229 PrevForegroundQueue
= IntGetFocusMessageQueue();
230 if (PrevForegroundQueue
!= 0)
232 hWndPrev
= PrevForegroundQueue
->ActiveWindow
;
233 hWndFocusPrev
= PrevForegroundQueue
->FocusWindow
;
236 if (hWndPrev
== hWnd
)
238 DPRINT("Failed - Same\n");
242 /* FIXME: Call hooks. */
244 co_IntSendDeactivateMessages(hWndPrev
, hWnd
);
245 co_IntSendKillFocusMessages(hWndFocusPrev
, hWndFocus
);
248 IntSetFocusMessageQueue(Wnd
->head
.pti
->MessageQueue
);
250 if (Wnd
->head
.pti
->MessageQueue
)
252 Wnd
->head
.pti
->MessageQueue
->ActiveWindow
= hWnd
;
255 if (FocusWindow
->head
.pti
->MessageQueue
)
257 FocusWindow
->head
.pti
->MessageQueue
->FocusWindow
= hWndFocus
;
260 if (PrevForegroundQueue
!= Wnd
->head
.pti
->MessageQueue
)
262 /* FIXME: Send WM_ACTIVATEAPP to all thread windows. */
265 co_IntSendSetFocusMessages(hWndFocusPrev
, hWndFocus
);
266 co_IntSendActivateMessages(hWndPrev
, hWnd
, MouseActivate
);
272 co_IntSetForegroundWindow(PWND Window
)//FIXME: can Window be NULL??
274 /*if (Window)*/ ASSERT_REFS_CO(Window
);
276 return co_IntSetForegroundAndFocusWindow(Window
, Window
, FALSE
);
280 co_IntMouseActivateWindow(PWND Wnd
)
284 USER_REFERENCE_ENTRY Ref
;
288 if(Wnd
->style
& WS_DISABLED
)
292 PWND DesktopWindow
= UserGetWindowObject(IntGetDesktopWindow());
295 Top
= IntFindChildWindowToOwner(DesktopWindow
, Wnd
);
296 if((TopWnd
= UserGetWindowObject(Top
)))
298 UserRefObjectCo(TopWnd
, &Ref
);
299 Ret
= co_IntMouseActivateWindow(TopWnd
);
300 UserDerefObjectCo(TopWnd
);
309 TopWindow
= UserGetAncestor(Wnd
, GA_ROOT
);
310 if (!TopWindow
) return FALSE
;
312 /* TMN: Check return valud from this function? */
313 UserRefObjectCo(TopWindow
, &Ref
);
315 co_IntSetForegroundAndFocusWindow(TopWindow
, Wnd
, TRUE
);
317 UserDerefObjectCo(TopWindow
);
323 co_IntSetActiveWindow(PWND Wnd OPTIONAL
)
326 PUSER_MESSAGE_QUEUE ThreadQueue
;
329 CBTACTIVATESTRUCT cbt
;
334 pti
= PsGetCurrentThreadWin32Thread();
335 ThreadQueue
= pti
->MessageQueue
;
336 ASSERT(ThreadQueue
!= 0);
340 if ((!(Wnd
->style
& WS_VISIBLE
) &&
341 Wnd
->head
.pti
->pEThread
->ThreadsProcess
!= CsrProcess
) ||
342 (Wnd
->style
& (WS_POPUP
| WS_CHILD
)) == WS_CHILD
)
344 return ThreadQueue
? 0 : ThreadQueue
->ActiveWindow
;
349 hWndPrev
= ThreadQueue
->ActiveWindow
;
350 if (hWndPrev
== hWnd
)
355 /* call CBT hook chain */
357 cbt
.hWndActive
= hWndPrev
;
358 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
361 ThreadQueue
->ActiveWindow
= hWnd
;
363 co_IntSendDeactivateMessages(hWndPrev
, hWnd
);
364 co_IntSendActivateMessages(hWndPrev
, hWnd
, FALSE
);
367 /* return IntIsWindow(hWndPrev) ? hWndPrev : 0;*/
373 co_IntSetFocusWindow(PWND Window OPTIONAL
)
377 PUSER_MESSAGE_QUEUE ThreadQueue
;
380 ASSERT_REFS_CO(Window
);
382 pti
= PsGetCurrentThreadWin32Thread();
383 ThreadQueue
= pti
->MessageQueue
;
384 ASSERT(ThreadQueue
!= 0);
386 hWndPrev
= ThreadQueue
->FocusWindow
;
390 if (hWndPrev
== Window
->head
.h
)
395 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->head
.h
, (LPARAM
)hWndPrev
))
398 ThreadQueue
->FocusWindow
= Window
->head
.h
;
400 co_IntSendKillFocusMessages(hWndPrev
, Window
->head
.h
);
401 co_IntSendSetFocusMessages(hWndPrev
, Window
->head
.h
);
405 ThreadQueue
->FocusWindow
= 0;
407 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
410 co_IntSendKillFocusMessages(hWndPrev
, 0);
420 UserGetForegroundWindow(VOID
)
422 PUSER_MESSAGE_QUEUE ForegroundQueue
;
424 ForegroundQueue
= IntGetFocusMessageQueue();
425 return( ForegroundQueue
!= NULL
? ForegroundQueue
->ActiveWindow
: 0);
433 NtUserGetForegroundWindow(VOID
)
435 DECLARE_RETURN(HWND
);
437 DPRINT("Enter NtUserGetForegroundWindow\n");
438 UserEnterExclusive();
440 RETURN( UserGetForegroundWindow());
443 DPRINT("Leave NtUserGetForegroundWindow, ret=%i\n",_ret_
);
449 HWND FASTCALL
UserGetActiveWindow(VOID
)
452 PUSER_MESSAGE_QUEUE ThreadQueue
;
454 pti
= PsGetCurrentThreadWin32Thread();
455 ThreadQueue
= pti
->MessageQueue
;
456 return( ThreadQueue
? ThreadQueue
->ActiveWindow
: 0);
461 NtUserSetActiveWindow(HWND hWnd
)
463 USER_REFERENCE_ENTRY Ref
;
464 DECLARE_RETURN(HWND
);
466 DPRINT("Enter NtUserSetActiveWindow(%x)\n", hWnd
);
467 UserEnterExclusive();
473 PUSER_MESSAGE_QUEUE ThreadQueue
;
476 if (!(Window
= UserGetWindowObject(hWnd
)))
481 pti
= PsGetCurrentThreadWin32Thread();
482 ThreadQueue
= pti
->MessageQueue
;
484 if (Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
486 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
490 UserRefObjectCo(Window
, &Ref
);
491 hWndPrev
= co_IntSetActiveWindow(Window
);
492 UserDerefObjectCo(Window
);
498 RETURN( co_IntSetActiveWindow(0));
502 DPRINT("Leave NtUserSetActiveWindow, ret=%i\n",_ret_
);
514 PUSER_MESSAGE_QUEUE ThreadQueue
;
515 DECLARE_RETURN(HWND
);
517 DPRINT("Enter IntGetCapture\n");
519 pti
= PsGetCurrentThreadWin32Thread();
520 ThreadQueue
= pti
->MessageQueue
;
521 RETURN( ThreadQueue
? ThreadQueue
->CaptureWindow
: 0);
524 DPRINT("Leave IntGetCapture, ret=%i\n",_ret_
);
532 NtUserSetCapture(HWND hWnd
)
535 PUSER_MESSAGE_QUEUE ThreadQueue
;
538 DECLARE_RETURN(HWND
);
540 DPRINT("Enter NtUserSetCapture(%x)\n", hWnd
);
541 UserEnterExclusive();
543 pti
= PsGetCurrentThreadWin32Thread();
544 ThreadQueue
= pti
->MessageQueue
;
546 if((Window
= UserGetWindowObject(hWnd
)))
548 if(Window
->head
.pti
->MessageQueue
!= ThreadQueue
)
554 hWndPrev
= MsqSetStateWindow(ThreadQueue
, MSQ_STATE_CAPTURE
, hWnd
);
556 /* also remove other windows if not capturing anymore */
559 MsqSetStateWindow(ThreadQueue
, MSQ_STATE_MENUOWNER
, NULL
);
560 MsqSetStateWindow(ThreadQueue
, MSQ_STATE_MOVESIZE
, NULL
);
563 co_IntPostOrSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
564 ThreadQueue
->CaptureWindow
= hWnd
;
569 DPRINT("Leave NtUserSetCapture, ret=%i\n",_ret_
);
576 HWND FASTCALL
co_UserSetFocus(PWND Wnd OPTIONAL
)
581 PUSER_MESSAGE_QUEUE ThreadQueue
;
584 USER_REFERENCE_ENTRY Ref
;
588 pti
= PsGetCurrentThreadWin32Thread();
589 ThreadQueue
= pti
->MessageQueue
;
591 if (Wnd
->style
& (WS_MINIMIZE
| WS_DISABLED
))
593 return( (ThreadQueue
? ThreadQueue
->FocusWindow
: 0));
596 if (Wnd
->head
.pti
->MessageQueue
!= ThreadQueue
)
598 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
602 TopWnd
= UserGetAncestor(Wnd
, GA_ROOT
);
603 if (TopWnd
&& TopWnd
->head
.h
!= UserGetActiveWindow())
605 // PWND WndTops = UserGetWindowObject(hWndTop);
606 UserRefObjectCo(TopWnd
, &Ref
);
607 co_IntSetActiveWindow(TopWnd
);
608 UserDerefObjectCo(TopWnd
);
611 hWndPrev
= co_IntSetFocusWindow(Wnd
);
617 return( co_IntSetFocusWindow(NULL
));
627 NtUserSetFocus(HWND hWnd
)
630 USER_REFERENCE_ENTRY Ref
;
631 DECLARE_RETURN(HWND
);
634 DPRINT("Enter NtUserSetFocus(%x)\n", hWnd
);
635 UserEnterExclusive();
639 if (!(Window
= UserGetWindowObject(hWnd
)))
644 UserRefObjectCo(Window
, &Ref
);
645 ret
= co_UserSetFocus(Window
);
646 UserDerefObjectCo(Window
);
652 RETURN( co_UserSetFocus(0));
656 DPRINT("Leave NtUserSetFocus, ret=%i\n",_ret_
);