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_IntPostOrSendMessage(hWndPrev
, WM_NCACTIVATE
, FALSE
, 0);
57 co_IntPostOrSendMessage(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
);
115 if (OldTID
!= NewTID
)
117 List
= IntWinListChildren(UserGetWindowObject(IntGetDesktopWindow()));
120 for (phWnd
= List
; *phWnd
; ++phWnd
)
122 cWindow
= UserGetWindowObject(*phWnd
);
123 if (cWindow
&& (IntGetWndThreadId(cWindow
) == OldTID
))
124 { // FALSE if the window is being deactivated,
125 // ThreadId that owns the window being activated.
126 co_IntPostOrSendMessage(*phWnd
, WM_ACTIVATEAPP
, FALSE
, (LPARAM
)NewTID
);
129 for (phWnd
= List
; *phWnd
; ++phWnd
)
131 cWindow
= UserGetWindowObject(*phWnd
);
132 if (cWindow
&& (IntGetWndThreadId(cWindow
) == NewTID
))
133 { // TRUE if the window is being activated,
134 // ThreadId that owns the window being deactivated.
135 co_IntPostOrSendMessage(*phWnd
, WM_ACTIVATEAPP
, TRUE
, (LPARAM
)OldTID
);
141 UserDerefObjectCo(WindowPrev
); // Now allow the previous window to die.
144 UserDerefObjectCo(Window
);
146 /* FIXME: IntIsWindow */
148 co_IntPostOrSendMessage(hWnd
, WM_NCACTIVATE
, (WPARAM
)(hWnd
== UserGetForegroundWindow()), 0);
149 /* FIXME: WA_CLICKACTIVE */
150 co_IntPostOrSendMessage(hWnd
, WM_ACTIVATE
,
151 MAKEWPARAM(MouseActivate
? WA_CLICKACTIVE
: WA_ACTIVE
,
152 UserGetWindowLong(hWnd
, GWL_STYLE
, FALSE
) & WS_MINIMIZE
),
158 co_IntSendKillFocusMessages(HWND hWndPrev
, HWND hWnd
)
162 co_IntPostOrSendMessage(hWndPrev
, WM_KILLFOCUS
, (WPARAM
)hWnd
, 0);
167 co_IntSendSetFocusMessages(HWND hWndPrev
, HWND hWnd
)
171 co_IntPostOrSendMessage(hWnd
, WM_SETFOCUS
, (WPARAM
)hWndPrev
, 0);
176 IntFindChildWindowToOwner(PWINDOW_OBJECT Root
, PWINDOW_OBJECT Owner
)
179 PWINDOW_OBJECT Child
, OwnerWnd
;
181 for(Child
= Root
->spwndChild
; Child
; Child
= Child
->spwndNext
)
183 OwnerWnd
= UserGetWindowObject(Child
->hOwner
);
187 if(OwnerWnd
== Owner
)
198 co_IntSetForegroundAndFocusWindow(PWINDOW_OBJECT Window
, PWINDOW_OBJECT FocusWindow
, BOOL MouseActivate
)
200 HWND hWnd
= Window
->hSelf
;
201 HWND hWndPrev
= NULL
;
202 HWND hWndFocus
= FocusWindow
->hSelf
;
203 HWND hWndFocusPrev
= NULL
;
204 PUSER_MESSAGE_QUEUE PrevForegroundQueue
;
207 ASSERT_REFS_CO(Window
);
209 DPRINT("IntSetForegroundAndFocusWindow(%x, %x, %s)\n", hWnd
, hWndFocus
, MouseActivate
? "TRUE" : "FALSE");
213 if ((Wnd
->style
& (WS_CHILD
| WS_POPUP
)) == WS_CHILD
)
215 DPRINT("Failed - Child\n");
219 if (0 == (Wnd
->style
& WS_VISIBLE
) &&
220 Window
->pti
->pEThread
->ThreadsProcess
!= CsrProcess
)
222 DPRINT("Failed - Invisible\n");
226 PrevForegroundQueue
= IntGetFocusMessageQueue();
227 if (PrevForegroundQueue
!= 0)
229 hWndPrev
= PrevForegroundQueue
->ActiveWindow
;
230 hWndFocusPrev
= PrevForegroundQueue
->FocusWindow
;
233 if (hWndPrev
== hWnd
)
235 DPRINT("Failed - Same\n");
239 /* FIXME: Call hooks. */
241 co_IntSendDeactivateMessages(hWndPrev
, hWnd
);
242 co_IntSendKillFocusMessages(hWndFocusPrev
, hWndFocus
);
244 IntSetFocusMessageQueue(Window
->pti
->MessageQueue
);
245 if (Window
->pti
->MessageQueue
)
247 Window
->pti
->MessageQueue
->ActiveWindow
= hWnd
;
250 if (FocusWindow
->pti
->MessageQueue
)
252 FocusWindow
->pti
->MessageQueue
->FocusWindow
= hWndFocus
;
255 if (PrevForegroundQueue
!= Window
->pti
->MessageQueue
)
257 /* FIXME: Send WM_ACTIVATEAPP to all thread windows. */
260 co_IntSendSetFocusMessages(hWndFocusPrev
, hWndFocus
);
261 co_IntSendActivateMessages(hWndPrev
, hWnd
, MouseActivate
);
267 co_IntSetForegroundWindow(PWINDOW_OBJECT Window
)//FIXME: can Window be NULL??
269 /*if (Window)*/ ASSERT_REFS_CO(Window
);
271 return co_IntSetForegroundAndFocusWindow(Window
, Window
, FALSE
);
275 co_IntMouseActivateWindow(PWINDOW_OBJECT Window
)
278 PWINDOW_OBJECT TopWindow
;
279 USER_REFERENCE_ENTRY Ref
;
282 ASSERT_REFS_CO(Window
);
285 if(Wnd
->style
& WS_DISABLED
)
288 PWINDOW_OBJECT TopWnd
;
289 PWINDOW_OBJECT DesktopWindow
= UserGetWindowObject(IntGetDesktopWindow());
292 Top
= IntFindChildWindowToOwner(DesktopWindow
, Window
);
293 if((TopWnd
= UserGetWindowObject(Top
)))
295 UserRefObjectCo(TopWnd
, &Ref
);
296 Ret
= co_IntMouseActivateWindow(TopWnd
);
297 UserDerefObjectCo(TopWnd
);
306 TopWindow
= UserGetAncestor(Window
, GA_ROOT
);
307 if (!TopWindow
) return FALSE
;
309 /* TMN: Check return valud from this function? */
310 UserRefObjectCo(TopWindow
, &Ref
);
312 co_IntSetForegroundAndFocusWindow(TopWindow
, Window
, TRUE
);
314 UserDerefObjectCo(TopWindow
);
320 co_IntSetActiveWindow(PWINDOW_OBJECT Window OPTIONAL
)
323 PUSER_MESSAGE_QUEUE ThreadQueue
;
327 CBTACTIVATESTRUCT cbt
;
330 ASSERT_REFS_CO(Window
);
332 pti
= PsGetCurrentThreadWin32Thread();
333 ThreadQueue
= pti
->MessageQueue
;
334 ASSERT(ThreadQueue
!= 0);
339 if ((!(Wnd
->style
& WS_VISIBLE
) &&
340 Window
->pti
->pEThread
->ThreadsProcess
!= CsrProcess
) ||
341 (Wnd
->style
& (WS_POPUP
| WS_CHILD
)) == WS_CHILD
)
343 return ThreadQueue
? 0 : ThreadQueue
->ActiveWindow
;
345 hWnd
= Window
->hSelf
;
348 hWndPrev
= ThreadQueue
->ActiveWindow
;
349 if (hWndPrev
== hWnd
)
354 /* call CBT hook chain */
356 cbt
.hWndActive
= hWndPrev
;
357 if (co_HOOK_CallHooks( WH_CBT
, HCBT_ACTIVATE
, (WPARAM
)hWnd
, (LPARAM
)&cbt
))
360 ThreadQueue
->ActiveWindow
= hWnd
;
362 co_IntSendDeactivateMessages(hWndPrev
, hWnd
);
363 co_IntSendActivateMessages(hWndPrev
, hWnd
, FALSE
);
366 /* return IntIsWindow(hWndPrev) ? hWndPrev : 0;*/
372 co_IntSetFocusWindow(PWINDOW_OBJECT Window OPTIONAL
)
376 PUSER_MESSAGE_QUEUE ThreadQueue
;
379 ASSERT_REFS_CO(Window
);
381 pti
= PsGetCurrentThreadWin32Thread();
382 ThreadQueue
= pti
->MessageQueue
;
383 ASSERT(ThreadQueue
!= 0);
385 hWndPrev
= ThreadQueue
->FocusWindow
;
389 if (hWndPrev
== Window
->hSelf
)
394 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)Window
->hSelf
, (LPARAM
)hWndPrev
))
397 ThreadQueue
->FocusWindow
= Window
->hSelf
;
399 co_IntSendKillFocusMessages(hWndPrev
, Window
->hSelf
);
400 co_IntSendSetFocusMessages(hWndPrev
, Window
->hSelf
);
404 ThreadQueue
->FocusWindow
= 0;
406 if (co_HOOK_CallHooks( WH_CBT
, HCBT_SETFOCUS
, (WPARAM
)0, (LPARAM
)hWndPrev
))
409 co_IntSendKillFocusMessages(hWndPrev
, 0);
419 UserGetForegroundWindow(VOID
)
421 PUSER_MESSAGE_QUEUE ForegroundQueue
;
423 ForegroundQueue
= IntGetFocusMessageQueue();
424 return( ForegroundQueue
!= NULL
? ForegroundQueue
->ActiveWindow
: 0);
432 NtUserGetForegroundWindow(VOID
)
434 DECLARE_RETURN(HWND
);
436 DPRINT("Enter NtUserGetForegroundWindow\n");
437 UserEnterExclusive();
439 RETURN( UserGetForegroundWindow());
442 DPRINT("Leave NtUserGetForegroundWindow, ret=%i\n",_ret_
);
448 HWND FASTCALL
UserGetActiveWindow(VOID
)
451 PUSER_MESSAGE_QUEUE ThreadQueue
;
453 pti
= PsGetCurrentThreadWin32Thread();
454 ThreadQueue
= pti
->MessageQueue
;
455 return( ThreadQueue
? ThreadQueue
->ActiveWindow
: 0);
460 NtUserSetActiveWindow(HWND hWnd
)
462 USER_REFERENCE_ENTRY Ref
;
463 DECLARE_RETURN(HWND
);
465 DPRINT("Enter NtUserSetActiveWindow(%x)\n", hWnd
);
466 UserEnterExclusive();
470 PWINDOW_OBJECT Window
;
472 PUSER_MESSAGE_QUEUE ThreadQueue
;
475 if (!(Window
= UserGetWindowObject(hWnd
)))
480 pti
= PsGetCurrentThreadWin32Thread();
481 ThreadQueue
= pti
->MessageQueue
;
483 if (Window
->pti
->MessageQueue
!= ThreadQueue
)
485 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
489 UserRefObjectCo(Window
, &Ref
);
490 hWndPrev
= co_IntSetActiveWindow(Window
);
491 UserDerefObjectCo(Window
);
497 RETURN( co_IntSetActiveWindow(0));
501 DPRINT("Leave NtUserSetActiveWindow, ret=%i\n",_ret_
);
513 PUSER_MESSAGE_QUEUE ThreadQueue
;
514 DECLARE_RETURN(HWND
);
516 DPRINT("Enter IntGetCapture\n");
518 pti
= PsGetCurrentThreadWin32Thread();
519 ThreadQueue
= pti
->MessageQueue
;
520 RETURN( ThreadQueue
? ThreadQueue
->CaptureWindow
: 0);
523 DPRINT("Leave IntGetCapture, ret=%i\n",_ret_
);
531 NtUserSetCapture(HWND hWnd
)
534 PUSER_MESSAGE_QUEUE ThreadQueue
;
535 PWINDOW_OBJECT Window
;
537 DECLARE_RETURN(HWND
);
539 DPRINT("Enter NtUserSetCapture(%x)\n", hWnd
);
540 UserEnterExclusive();
542 pti
= PsGetCurrentThreadWin32Thread();
543 ThreadQueue
= pti
->MessageQueue
;
545 if((Window
= UserGetWindowObject(hWnd
)))
547 if(Window
->pti
->MessageQueue
!= ThreadQueue
)
553 hWndPrev
= MsqSetStateWindow(ThreadQueue
, MSQ_STATE_CAPTURE
, hWnd
);
555 /* also remove other windows if not capturing anymore */
558 MsqSetStateWindow(ThreadQueue
, MSQ_STATE_MENUOWNER
, NULL
);
559 MsqSetStateWindow(ThreadQueue
, MSQ_STATE_MOVESIZE
, NULL
);
562 co_IntPostOrSendMessage(hWndPrev
, WM_CAPTURECHANGED
, 0, (LPARAM
)hWnd
);
563 ThreadQueue
->CaptureWindow
= hWnd
;
568 DPRINT("Leave NtUserSetCapture, ret=%i\n",_ret_
);
575 HWND FASTCALL
co_UserSetFocus(PWINDOW_OBJECT Window OPTIONAL
)
580 PUSER_MESSAGE_QUEUE ThreadQueue
;
582 PWINDOW_OBJECT TopWnd
;
583 USER_REFERENCE_ENTRY Ref
;
586 ASSERT_REFS_CO(Window
);
588 pti
= PsGetCurrentThreadWin32Thread();
589 ThreadQueue
= pti
->MessageQueue
;
592 if (Wnd
->style
& (WS_MINIMIZE
| WS_DISABLED
))
594 return( (ThreadQueue
? ThreadQueue
->FocusWindow
: 0));
597 if (Window
->pti
->MessageQueue
!= ThreadQueue
)
599 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
603 TopWnd
= UserGetAncestor(Window
, GA_ROOT
);
604 if (TopWnd
&& TopWnd
->hSelf
!= UserGetActiveWindow())
606 // PWINDOW_OBJECT WndTops = UserGetWindowObject(hWndTop);
607 UserRefObjectCo(TopWnd
, &Ref
);
608 co_IntSetActiveWindow(TopWnd
);
609 UserDerefObjectCo(TopWnd
);
612 hWndPrev
= co_IntSetFocusWindow(Window
);
618 return( co_IntSetFocusWindow(NULL
));
628 NtUserSetFocus(HWND hWnd
)
630 PWINDOW_OBJECT Window
;
631 USER_REFERENCE_ENTRY Ref
;
632 DECLARE_RETURN(HWND
);
635 DPRINT("Enter NtUserSetFocus(%x)\n", hWnd
);
636 UserEnterExclusive();
640 if (!(Window
= UserGetWindowObject(hWnd
)))
645 UserRefObjectCo(Window
, &Ref
);
646 ret
= co_UserSetFocus(Window
);
647 UserDerefObjectCo(Window
);
653 RETURN( co_UserSetFocus(0));
657 DPRINT("Leave NtUserSetFocus, ret=%i\n",_ret_
);