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 co_IntSendMessageNoWait(hWndPrev
, WM_NCACTIVATE
, FALSE
, 0);
57 co_IntSendMessageNoWait(hWndPrev
, WM_ACTIVATE
,
58 MAKEWPARAM(WA_INACTIVE
, UserGetWindowLong(hWndPrev
, GWL_STYLE
, FALSE
) & WS_MINIMIZE
),
64 co_IntSendActivateMessages(HWND hWndPrev
, HWND hWnd
, BOOL MouseActivate
)
66 USER_REFERENCE_ENTRY Ref
, RefPrev
;
67 PWINDOW_OBJECT Window
, WindowPrev
= NULL
;
69 if ((Window
= UserGetWindowObject(hWnd
)))
71 UserRefObjectCo(Window
, &Ref
);
73 WindowPrev
= UserGetWindowObject(hWndPrev
);
75 if (WindowPrev
) UserRefObjectCo(WindowPrev
, &RefPrev
);
77 /* Send palette messages */
78 if (co_IntPostOrSendMessage(hWnd
, WM_QUERYNEWPALETTE
, 0, 0))
80 UserPostMessage( HWND_BROADCAST
,
86 if (UserGetWindow(hWnd
, GW_HWNDPREV
) != NULL
)
87 co_WinPosSetWindowPos(Window
, HWND_TOP
, 0, 0, 0, 0,
88 SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
| SWP_NOSENDCHANGING
);
90 if (!IntGetOwner(Window
) && !IntGetParent(Window
))
92 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED
, (LPARAM
) hWnd
);
96 { // Set last active for window and it's owner.
97 Window
->Wnd
->hWndLastActive
= hWnd
;
98 if (Window
->Wnd
->spwndOwner
)
99 Window
->Wnd
->spwndOwner
->hWndLastActive
= hWnd
;
100 Window
->Wnd
->state
|= WNDS_ACTIVEFRAME
;
103 if (WindowPrev
&& WindowPrev
->Wnd
)
104 WindowPrev
->Wnd
->state
&= ~WNDS_ACTIVEFRAME
;
106 if (Window
&& WindowPrev
)
108 PWINDOW_OBJECT cWindow
;
110 HANDLE OldTID
= IntGetWndThreadId(WindowPrev
);
111 HANDLE NewTID
= IntGetWndThreadId(Window
);
113 DPRINT("SendActiveMessage Old -> %x, New -> %x\n", OldTID
, NewTID
);
114 if (Window
->Wnd
->style
& WS_MINIMIZE
)
116 DPRINT("Widow was minimized\n");
119 if (OldTID
!= NewTID
)
121 List
= IntWinListChildren(UserGetWindowObject(IntGetDesktopWindow()));
124 for (phWnd
= List
; *phWnd
; ++phWnd
)
126 cWindow
= UserGetWindowObject(*phWnd
);
128 if (cWindow
&& (IntGetWndThreadId(cWindow
) == OldTID
))
129 { // FALSE if the window is being deactivated,
130 // ThreadId that owns the window being activated.
131 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
134 for (phWnd
= List
; *phWnd
; ++phWnd
)
136 cWindow
= UserGetWindowObject(*phWnd
);
137 if (cWindow
&& (IntGetWndThreadId(cWindow
) == NewTID
))
138 { // TRUE if the window is being activated,
139 // ThreadId that owns the window being deactivated.
140 co_IntSendMessageNoWait(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
146 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
149 UserDerefObjectCo(Window
);
151 /* FIXME: IntIsWindow */
152 co_IntSendMessageNoWait(hWnd
, WM_NCACTIVATE
, (WPARAM
)(hWnd
== UserGetForegroundWindow()), 0);
153 /* FIXME: WA_CLICKACTIVE */
154 co_IntSendMessageNoWait(hWnd
, WM_ACTIVATE
,
155 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
,
156 UserGetWindowLong(hWnd
, GWL_STYLE
, FALSE
) & WS_MINIMIZE
),
162 co_IntSendKillFocusMessages(HWND hWndPrev
, HWND hWnd
)
166 co_IntPostOrSendMessage(hWndPrev
, WM_KILLFOCUS
, (WPARAM
)hWnd
, 0);
171 co_IntSendSetFocusMessages(HWND hWndPrev
, HWND hWnd
)
175 co_IntPostOrSendMessage(hWnd
, WM_SETFOCUS
, (WPARAM
)hWndPrev
, 0);
180 IntFindChildWindowToOwner(PWINDOW_OBJECT Root
, PWINDOW_OBJECT Owner
)
183 PWINDOW_OBJECT Child
, OwnerWnd
;
185 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
187 OwnerWnd
= UserGetWindowObject(Child
->hOwner
);
191 if(OwnerWnd
== Owner
)
202 co_IntSetForegroundAndFocusWindow(PWINDOW_OBJECT Window
, PWINDOW_OBJECT FocusWindow
, BOOL MouseActivate
)
204 HWND hWnd
= Window
->hSelf
;
205 HWND hWndPrev
= NULL
;
206 HWND hWndFocus
= FocusWindow
->hSelf
;
207 HWND hWndFocusPrev
= NULL
;
208 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
211 ASSERT_REFS_CO(Window
);
213 DPRINT("IntSetForegroundAndFocusWindow(%x, %x, %s)\n", hWnd
, hWndFocus
, MouseActivate
? "TRUE" : "FALSE");
217 if ((Wnd
->style
& (WS_CHILD
| WS_POPUP
)) == WS_CHILD
)
219 DPRINT("Failed - Child\n");
223 if (0 == (Wnd
->style
& WS_VISIBLE
) &&
224 Window
->pti
->pEThread
->ThreadsProcess
!= CsrProcess
)
226 DPRINT("Failed - Invisible\n");
230 PrevForegroundQueue
= IntGetFocusMessageQueue();
231 if (PrevForegroundQueue
!= 0)
233 hWndPrev
= PrevForegroundQueue
->ActiveWindow
;
234 hWndFocusPrev
= PrevForegroundQueue
->FocusWindow
;
237 if (hWndPrev
== hWnd
)
239 DPRINT("Failed - Same\n");
243 /* FIXME: Call hooks. */
245 co_IntSendDeactivateMessages(hWndPrev
, hWnd
);
246 co_IntSendKillFocusMessages(hWndFocusPrev
, hWndFocus
);
249 IntSetFocusMessageQueue(Window
->pti
->MessageQueue
);
251 if (Window
->pti
->MessageQueue
)
253 Window
->pti
->MessageQueue
->ActiveWindow
= hWnd
;
256 if (FocusWindow
->pti
->MessageQueue
)
258 FocusWindow
->pti
->MessageQueue
->FocusWindow
= hWndFocus
;
261 if (PrevForegroundQueue
!= Window
->pti
->MessageQueue
)
263 /* FIXME: Send WM_ACTIVATEAPP to all thread windows. */
266 co_IntSendSetFocusMessages(hWndFocusPrev
, hWndFocus
);
267 co_IntSendActivateMessages(hWndPrev
, hWnd
, MouseActivate
);
273 co_IntSetForegroundWindow(PWINDOW_OBJECT Window
)//FIXME: can Window be NULL??
275 /*if (Window)*/ ASSERT_REFS_CO(Window
);
277 return co_IntSetForegroundAndFocusWindow(Window
, Window
, FALSE
);
281 co_IntMouseActivateWindow(PWINDOW_OBJECT Window
)
284 PWINDOW_OBJECT TopWindow
;
285 USER_REFERENCE_ENTRY Ref
;
288 ASSERT_REFS_CO(Window
);
291 if(Wnd
->style
& WS_DISABLED
)
294 PWINDOW_OBJECT TopWnd
;
295 PWINDOW_OBJECT DesktopWindow
= UserGetWindowObject(IntGetDesktopWindow());
298 Top
= IntFindChildWindowToOwner(DesktopWindow
, Window
);
299 if((TopWnd
= UserGetWindowObject(Top
)))
301 UserRefObjectCo(TopWnd
, &Ref
);
302 Ret
= co_IntMouseActivateWindow(TopWnd
);
303 UserDerefObjectCo(TopWnd
);
312 TopWindow
= UserGetAncestor(Window
, GA_ROOT
);
313 if (!TopWindow
) return FALSE
;
315 /* TMN: Check return valud from this function? */
316 UserRefObjectCo(TopWindow
, &Ref
);
318 co_IntSetForegroundAndFocusWindow(TopWindow
, Window
, TRUE
);
320 UserDerefObjectCo(TopWindow
);
326 co_IntSetActiveWindow(PWINDOW_OBJECT Window OPTIONAL
)
329 PUSER_MESSAGE_QUEUE ThreadQueue
;
333 CBTACTIVATESTRUCT cbt
;
336 ASSERT_REFS_CO(Window
);
338 pti
= PsGetCurrentThreadWin32Thread();
339 ThreadQueue
= pti
->MessageQueue
;
340 ASSERT(ThreadQueue
!= 0);
345 if ((!(Wnd
->style
& WS_VISIBLE
) &&
346 Window
->pti
->pEThread
->ThreadsProcess
!= CsrProcess
) ||
347 (Wnd
->style
& (WS_POPUP
| WS_CHILD
)) == WS_CHILD
)
349 return ThreadQueue
? 0 : ThreadQueue
->ActiveWindow
;
351 hWnd
= Window
->hSelf
;
354 hWndPrev
= ThreadQueue
->ActiveWindow
;
355 if (hWndPrev
== hWnd
)
360 /* call CBT hook chain */
362 cbt
.hWndActive
= hWndPrev
;
363 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
366 ThreadQueue
->ActiveWindow
= hWnd
;
368 co_IntSendDeactivateMessages(hWndPrev
, hWnd
);
369 co_IntSendActivateMessages(hWndPrev
, hWnd
, FALSE
);
372 /* return IntIsWindow(hWndPrev) ? hWndPrev : 0;*/
378 co_IntSetFocusWindow(PWINDOW_OBJECT Window OPTIONAL
)
382 PUSER_MESSAGE_QUEUE ThreadQueue
;
385 ASSERT_REFS_CO(Window
);
387 pti
= PsGetCurrentThreadWin32Thread();
388 ThreadQueue
= pti
->MessageQueue
;
389 ASSERT(ThreadQueue
!= 0);
391 hWndPrev
= ThreadQueue
->FocusWindow
;
395 if (hWndPrev
== Window
->hSelf
)
400 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->hSelf
, (LPARAM
)hWndPrev
))
403 ThreadQueue
->FocusWindow
= Window
->hSelf
;
405 co_IntSendKillFocusMessages(hWndPrev
, Window
->hSelf
);
406 co_IntSendSetFocusMessages(hWndPrev
, Window
->hSelf
);
410 ThreadQueue
->FocusWindow
= 0;
412 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
415 co_IntSendKillFocusMessages(hWndPrev
, 0);
425 UserGetForegroundWindow(VOID
)
427 PUSER_MESSAGE_QUEUE ForegroundQueue
;
429 ForegroundQueue
= IntGetFocusMessageQueue();
430 return( ForegroundQueue
!= NULL
? ForegroundQueue
->ActiveWindow
: 0);
438 NtUserGetForegroundWindow(VOID
)
440 DECLARE_RETURN(HWND
);
442 DPRINT("Enter NtUserGetForegroundWindow\n");
443 UserEnterExclusive();
445 RETURN( UserGetForegroundWindow());
448 DPRINT("Leave NtUserGetForegroundWindow, ret=%i\n",_ret_
);
454 HWND FASTCALL
UserGetActiveWindow(VOID
)
457 PUSER_MESSAGE_QUEUE ThreadQueue
;
459 pti
= PsGetCurrentThreadWin32Thread();
460 ThreadQueue
= pti
->MessageQueue
;
461 return( ThreadQueue
? ThreadQueue
->ActiveWindow
: 0);
466 NtUserSetActiveWindow(HWND hWnd
)
468 USER_REFERENCE_ENTRY Ref
;
469 DECLARE_RETURN(HWND
);
471 DPRINT("Enter NtUserSetActiveWindow(%x)\n", hWnd
);
472 UserEnterExclusive();
476 PWINDOW_OBJECT Window
;
478 PUSER_MESSAGE_QUEUE ThreadQueue
;
481 if (!(Window
= UserGetWindowObject(hWnd
)))
486 pti
= PsGetCurrentThreadWin32Thread();
487 ThreadQueue
= pti
->MessageQueue
;
489 if (Window
->pti
->MessageQueue
!= ThreadQueue
)
491 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
495 UserRefObjectCo(Window
, &Ref
);
496 hWndPrev
= co_IntSetActiveWindow(Window
);
497 UserDerefObjectCo(Window
);
503 RETURN( co_IntSetActiveWindow(0));
507 DPRINT("Leave NtUserSetActiveWindow, ret=%i\n",_ret_
);
519 PUSER_MESSAGE_QUEUE ThreadQueue
;
520 DECLARE_RETURN(HWND
);
522 DPRINT("Enter IntGetCapture\n");
524 pti
= PsGetCurrentThreadWin32Thread();
525 ThreadQueue
= pti
->MessageQueue
;
526 RETURN( ThreadQueue
? ThreadQueue
->CaptureWindow
: 0);
529 DPRINT("Leave IntGetCapture, ret=%i\n",_ret_
);
537 NtUserSetCapture(HWND hWnd
)
540 PUSER_MESSAGE_QUEUE ThreadQueue
;
541 PWINDOW_OBJECT Window
;
543 DECLARE_RETURN(HWND
);
545 DPRINT("Enter NtUserSetCapture(%x)\n", hWnd
);
546 UserEnterExclusive();
548 pti
= PsGetCurrentThreadWin32Thread();
549 ThreadQueue
= pti
->MessageQueue
;
551 if((Window
= UserGetWindowObject(hWnd
)))
553 if(Window
->pti
->MessageQueue
!= ThreadQueue
)
559 hWndPrev
= MsqSetStateWindow(ThreadQueue
, MSQ_STATE_CAPTURE
, hWnd
);
561 /* also remove other windows if not capturing anymore */
564 MsqSetStateWindow(ThreadQueue
, MSQ_STATE_MENUOWNER
, NULL
);
565 MsqSetStateWindow(ThreadQueue
, MSQ_STATE_MOVESIZE
, NULL
);
568 co_IntPostOrSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
569 ThreadQueue
->CaptureWindow
= hWnd
;
574 DPRINT("Leave NtUserSetCapture, ret=%i\n",_ret_
);
581 HWND FASTCALL
co_UserSetFocus(PWINDOW_OBJECT Window OPTIONAL
)
586 PUSER_MESSAGE_QUEUE ThreadQueue
;
588 PWINDOW_OBJECT TopWnd
;
589 USER_REFERENCE_ENTRY Ref
;
592 ASSERT_REFS_CO(Window
);
594 pti
= PsGetCurrentThreadWin32Thread();
595 ThreadQueue
= pti
->MessageQueue
;
598 if (Wnd
->style
& (WS_MINIMIZE
| WS_DISABLED
))
600 return( (ThreadQueue
? ThreadQueue
->FocusWindow
: 0));
603 if (Window
->pti
->MessageQueue
!= ThreadQueue
)
605 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
609 TopWnd
= UserGetAncestor(Window
, GA_ROOT
);
610 if (TopWnd
&& TopWnd
->hSelf
!= UserGetActiveWindow())
612 // PWINDOW_OBJECT WndTops = UserGetWindowObject(hWndTop);
613 UserRefObjectCo(TopWnd
, &Ref
);
614 co_IntSetActiveWindow(TopWnd
);
615 UserDerefObjectCo(TopWnd
);
618 hWndPrev
= co_IntSetFocusWindow(Window
);
624 return( co_IntSetFocusWindow(NULL
));
634 NtUserSetFocus(HWND hWnd
)
636 PWINDOW_OBJECT Window
;
637 USER_REFERENCE_ENTRY Ref
;
638 DECLARE_RETURN(HWND
);
641 DPRINT("Enter NtUserSetFocus(%x)\n", hWnd
);
642 UserEnterExclusive();
646 if (!(Window
= UserGetWindowObject(hWnd
)))
651 UserRefObjectCo(Window
, &Ref
);
652 ret
= co_UserSetFocus(Window
);
653 UserDerefObjectCo(Window
);
659 RETURN( co_UserSetFocus(0));
663 DPRINT("Leave NtUserSetFocus, ret=%i\n",_ret_
);