2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995,1996 Alex Korobka
8 #include "sysmetrics.h"
15 #include "stackframe.h"
17 #include "nonclient.h"
19 /* #define DEBUG_WIN */
22 /* ----- external functions ----- */
24 void FOCUS_SwitchFocus( HWND , HWND );
26 /* ----- internal variables ----- */
28 static HWND hwndActive = 0; /* Currently active window */
29 static HWND hwndPrevActive = 0; /* Previously active window */
32 /***********************************************************************
35 * Find a suitable place for an iconic window.
36 * The new position is stored into wndPtr->ptIconPos.
38 void WINPOS_FindIconPos( HWND hwnd )
41 short x, y, xspacing, yspacing;
42 WND * wndPtr = WIN_FindWndPtr( hwnd );
45 GetClientRect( wndPtr->hwndParent, &rectParent );
46 if ((wndPtr->ptIconPos.x >= rectParent.left) &&
47 (wndPtr->ptIconPos.x + SYSMETRICS_CXICON < rectParent.right) &&
48 (wndPtr->ptIconPos.y >= rectParent.top) &&
49 (wndPtr->ptIconPos.y + SYSMETRICS_CYICON < rectParent.bottom))
50 return; /* The icon already has a suitable position */
52 xspacing = yspacing = 70; /* FIXME: This should come from WIN.INI */
53 y = rectParent.bottom;
56 for (x = rectParent.left; x<=rectParent.right-xspacing; x += xspacing)
58 /* Check if another icon already occupies this spot */
59 HWND hwndChild = GetWindow( wndPtr->hwndParent, GW_CHILD );
62 WND *childPtr = WIN_FindWndPtr( hwndChild );
63 if ((childPtr->dwStyle & WS_MINIMIZE) && (hwndChild != hwnd))
65 if ((childPtr->rectWindow.left < x + xspacing) &&
66 (childPtr->rectWindow.right >= x) &&
67 (childPtr->rectWindow.top <= y) &&
68 (childPtr->rectWindow.bottom > y - yspacing))
69 break; /* There's a window in there */
72 hwndChild = childPtr->hwndNext;
76 /* No window was found, so it's OK for us */
77 wndPtr->ptIconPos.x = x + (xspacing - SYSMETRICS_CXICON) / 2;
78 wndPtr->ptIconPos.y = y - (yspacing + SYSMETRICS_CYICON) / 2;
87 /***********************************************************************
88 * ArrangeIconicWindows (USER.170)
90 UINT ArrangeIconicWindows( HWND parent )
94 INT x, y, xspacing, yspacing;
96 GetClientRect( parent, &rectParent );
98 y = rectParent.bottom;
99 xspacing = yspacing = 70; /* FIXME: This should come from WIN.INI */
100 hwndChild = GetWindow( parent, GW_CHILD );
103 if (IsIconic( hwndChild ))
105 SetWindowPos( hwndChild, 0, x + (xspacing - SYSMETRICS_CXICON) / 2,
106 y - (yspacing + SYSMETRICS_CYICON) / 2, 0, 0,
107 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
108 if (x <= rectParent.right - xspacing) x += xspacing;
115 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
121 /***********************************************************************
122 * GetWindowRect (USER.32)
124 void GetWindowRect( HWND hwnd, LPRECT rect )
126 WND * wndPtr = WIN_FindWndPtr( hwnd );
129 *rect = wndPtr->rectWindow;
130 if (wndPtr->dwStyle & WS_CHILD)
131 MapWindowPoints( wndPtr->hwndParent, 0, (POINT *)rect, 2 );
135 /***********************************************************************
136 * GetClientRect (USER.33)
138 void GetClientRect( HWND hwnd, LPRECT rect )
140 WND * wndPtr = WIN_FindWndPtr( hwnd );
142 rect->left = rect->top = rect->right = rect->bottom = 0;
145 rect->right = wndPtr->rectClient.right - wndPtr->rectClient.left;
146 rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
151 /*******************************************************************
152 * ClientToScreen (USER.28)
154 BOOL ClientToScreen( HWND hwnd, LPPOINT lppnt )
156 MapWindowPoints( hwnd, 0, lppnt, 1 );
161 /*******************************************************************
162 * ScreenToClient (USER.29)
164 void ScreenToClient( HWND hwnd, LPPOINT lppnt )
166 MapWindowPoints( 0, hwnd, lppnt, 1 );
170 /***********************************************************************
171 * WINPOS_WindowFromPoint
173 * Find the window and hittest for a given point.
175 INT WINPOS_WindowFromPoint( POINT pt, HWND *phwnd )
179 INT hittest = HTERROR;
185 hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
190 /* If point is in window, and window is visible, and it */
191 /* is enabled (or it's a top-level window), then explore */
192 /* its children. Otherwise, go to the next window. */
194 wndPtr = WIN_FindWndPtr( hwnd );
195 if ((wndPtr->dwStyle & WS_VISIBLE) &&
196 (!(wndPtr->dwStyle & WS_DISABLED) ||
197 ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)) &&
198 (x >= wndPtr->rectWindow.left) &&
199 (x < wndPtr->rectWindow.right) &&
200 (y >= wndPtr->rectWindow.top) &&
201 (y < wndPtr->rectWindow.bottom))
203 *phwnd = hwnd; /* Got a suitable window */
205 /* If window is minimized or disabled, return at once */
206 if (wndPtr->dwStyle & WS_MINIMIZE) return HTCAPTION;
207 if (wndPtr->dwStyle & WS_DISABLED) return HTERROR;
209 /* If point is not in client area, ignore the children */
210 if ((x < wndPtr->rectClient.left) ||
211 (x >= wndPtr->rectClient.right) ||
212 (y < wndPtr->rectClient.top) ||
213 (y >= wndPtr->rectClient.bottom)) break;
215 x -= wndPtr->rectClient.left;
216 y -= wndPtr->rectClient.top;
217 hwnd = wndPtr->hwndChild;
219 else hwnd = wndPtr->hwndNext;
222 /* If nothing found, return the desktop window */
225 *phwnd = GetDesktopWindow();
228 wndPtr = WIN_FindWndPtr( *phwnd );
230 /* Send the WM_NCHITTEST message (only if to the same task) */
231 if (wndPtr->hmemTaskQ != GetTaskQueue(0)) return HTCLIENT;
232 hittest = (INT)SendMessage( *phwnd, WM_NCHITTEST, 0,
233 MAKELONG( pt.x, pt.y ) );
234 if (hittest != HTTRANSPARENT) return hittest; /* Found the window */
236 /* If no children found in last search, make point relative to parent*/
239 x += wndPtr->rectClient.left;
240 y += wndPtr->rectClient.top;
243 /* Restart the search from the next sibling */
244 hwnd = wndPtr->hwndNext;
245 *phwnd = wndPtr->hwndParent;
250 /*******************************************************************
251 * WindowFromPoint (USER.30)
253 HWND WindowFromPoint( POINT pt )
256 WINPOS_WindowFromPoint( pt, &hwnd );
261 /*******************************************************************
262 * ChildWindowFromPoint (USER.191)
264 HWND ChildWindowFromPoint( HWND hwndParent, POINT pt )
269 GetWindowRect( hwndParent, &rect );
270 if (!PtInRect( &rect, pt )) return 0;
271 hwnd = GetTopWindow( hwndParent );
274 GetWindowRect( hwnd, &rect );
275 if (PtInRect( &rect, pt )) return hwnd;
276 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
282 /*******************************************************************
283 * MapWindowPoints (USER.258)
285 void MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, WORD count )
289 POINT origin = { 0, 0 };
292 if( hwndFrom == hwndTo ) return;
294 /* Translate source window origin to screen coords */
297 if (!(wndPtr = WIN_FindWndPtr( hwndFrom )))
299 fprintf( stderr, "MapWindowPoints: bad hwndFrom = "NPFMT"\n",
303 origin.x += wndPtr->rectClient.left;
304 origin.y += wndPtr->rectClient.top;
305 hwndFrom = (wndPtr->dwStyle & WS_CHILD) ? wndPtr->hwndParent : 0;
308 /* Translate origin to destination window coords */
311 if (!(wndPtr = WIN_FindWndPtr( hwndTo )))
313 fprintf(stderr,"MapWindowPoints: bad hwndTo = "NPFMT"\n", hwndTo );
316 origin.x -= wndPtr->rectClient.left;
317 origin.y -= wndPtr->rectClient.top;
318 hwndTo = (wndPtr->dwStyle & WS_CHILD) ? wndPtr->hwndParent : 0;
321 /* Translate points */
322 for (i = 0, curpt = lppt; i < count; i++, curpt++)
324 curpt->x += origin.x;
325 curpt->y += origin.y;
330 /***********************************************************************
333 BOOL IsIconic(HWND hWnd)
335 WND * wndPtr = WIN_FindWndPtr(hWnd);
336 if (wndPtr == NULL) return FALSE;
337 return (wndPtr->dwStyle & WS_MINIMIZE) != 0;
341 /***********************************************************************
342 * IsZoomed (USER.272)
344 BOOL IsZoomed(HWND hWnd)
346 WND * wndPtr = WIN_FindWndPtr(hWnd);
347 if (wndPtr == NULL) return FALSE;
348 return (wndPtr->dwStyle & WS_MAXIMIZE) != 0;
352 /*******************************************************************
353 * GetActiveWindow (USER.60)
355 HWND GetActiveWindow()
361 /*******************************************************************
362 * SetActiveWindow (USER.59)
364 HWND SetActiveWindow( HWND hwnd )
366 HWND prev = hwndActive;
367 WND *wndPtr = WIN_FindWndPtr( hwnd );
369 if (!wndPtr || (wndPtr->dwStyle & WS_DISABLED) ||
370 !(wndPtr->dwStyle & WS_VISIBLE)) return 0;
372 WINPOS_SetActiveWindow( hwnd, 0, 0 );
377 /***********************************************************************
378 * BringWindowToTop (USER.45)
380 BOOL BringWindowToTop( HWND hwnd )
382 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
386 /***********************************************************************
387 * MoveWindow (USER.56)
389 BOOL MoveWindow( HWND hwnd, short x, short y, short cx, short cy, BOOL repaint)
391 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
392 if (!repaint) flags |= SWP_NOREDRAW;
393 dprintf_win(stddeb, "MoveWindow: "NPFMT" %d,%d %dx%d %d\n",
394 hwnd, x, y, cx, cy, repaint );
395 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
399 /***********************************************************************
400 * ShowWindow (USER.42)
402 BOOL ShowWindow( HWND hwnd, int cmd )
404 WND * wndPtr = WIN_FindWndPtr( hwnd );
408 short x = 0, y = 0, cx = 0, cy = 0;
410 if (!wndPtr) return FALSE;
412 dprintf_win(stddeb,"ShowWindow: hwnd="NPFMT", cmd=%d\n", hwnd, cmd);
414 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
419 swpflags |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
420 SWP_NOACTIVATE | SWP_NOZORDER;
423 case SW_SHOWMINNOACTIVE:
424 swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
426 case SW_SHOWMINIMIZED:
427 swpflags |= SWP_SHOWWINDOW;
430 swpflags |= SWP_FRAMECHANGED;
431 if (!(wndPtr->dwStyle & WS_MINIMIZE))
433 if (wndPtr->dwStyle & WS_MAXIMIZE)
435 wndPtr->flags |= WIN_RESTORE_MAX;
436 wndPtr->dwStyle &= ~WS_MAXIMIZE;
440 wndPtr->flags &= ~WIN_RESTORE_MAX;
441 wndPtr->rectNormal = wndPtr->rectWindow;
443 wndPtr->dwStyle |= WS_MINIMIZE;
444 WINPOS_FindIconPos( hwnd );
445 x = wndPtr->ptIconPos.x;
446 y = wndPtr->ptIconPos.y;
447 cx = SYSMETRICS_CXICON;
448 cy = SYSMETRICS_CYICON;
450 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
453 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE: */
454 swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
455 if (!(wndPtr->dwStyle & WS_MAXIMIZE))
457 /* Store the current position and find the maximized size */
458 if (!(wndPtr->dwStyle & WS_MINIMIZE))
459 wndPtr->rectNormal = wndPtr->rectWindow;
461 NC_GetMinMaxInfo( hwnd, &maxSize,
462 &wndPtr->ptMaxPos, NULL, NULL );
463 x = wndPtr->ptMaxPos.x;
464 y = wndPtr->ptMaxPos.y;
466 if( wndPtr->dwStyle & WS_MINIMIZE )
467 if( !SendMessage( hwnd, WM_QUERYOPEN, 0, 0L ) )
469 swpflags |= SWP_NOSIZE;
475 wndPtr->dwStyle &= ~WS_MINIMIZE;
476 wndPtr->dwStyle |= WS_MAXIMIZE;
478 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
482 swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
485 swpflags |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
488 case SW_SHOWNOACTIVATE:
489 swpflags |= SWP_NOZORDER;
490 if (GetActiveWindow()) swpflags |= SWP_NOACTIVATE;
492 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
493 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
495 swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
497 if (wndPtr->dwStyle & WS_MINIMIZE)
499 if( !SendMessage( hwnd, WM_QUERYOPEN, 0, 0L) )
501 swpflags |= SWP_NOSIZE;
504 wndPtr->ptIconPos.x = wndPtr->rectWindow.left;
505 wndPtr->ptIconPos.y = wndPtr->rectWindow.top;
506 wndPtr->dwStyle &= ~WS_MINIMIZE;
507 if (wndPtr->flags & WIN_RESTORE_MAX)
509 /* Restore to maximized position */
510 NC_GetMinMaxInfo( hwnd, &maxSize, &wndPtr->ptMaxPos,
512 x = wndPtr->ptMaxPos.x;
513 y = wndPtr->ptMaxPos.y;
516 wndPtr->dwStyle |= WS_MAXIMIZE;
518 else /* Restore to normal position */
520 x = wndPtr->rectNormal.left;
521 y = wndPtr->rectNormal.top;
522 cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
523 cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
526 else if (wndPtr->dwStyle & WS_MAXIMIZE)
528 wndPtr->ptMaxPos.x = wndPtr->rectWindow.left;
529 wndPtr->ptMaxPos.y = wndPtr->rectWindow.top;
530 wndPtr->dwStyle &= ~WS_MAXIMIZE;
531 x = wndPtr->rectNormal.left;
532 y = wndPtr->rectNormal.top;
533 cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
534 cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
536 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
540 SendMessage( hwnd, WM_SHOWWINDOW, (cmd != SW_HIDE), 0 );
541 SetWindowPos( hwnd, HWND_TOP, x, y, cx, cy, swpflags );
543 /* Send WM_SIZE and WM_MOVE messages if not already done */
544 if (!(wndPtr->flags & WIN_GOT_SIZEMSG))
546 int wParam = SIZE_RESTORED;
547 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
548 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
549 wndPtr->flags |= WIN_GOT_SIZEMSG;
550 SendMessage( hwnd, WM_SIZE, wParam,
551 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
552 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
553 SendMessage( hwnd, WM_MOVE, 0,
554 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
561 /***********************************************************************
562 * GetInternalWindowPos (USER.460)
564 WORD GetInternalWindowPos( HWND hwnd, LPRECT rectWnd, LPPOINT ptIcon )
566 WINDOWPLACEMENT wndpl;
567 if (!GetWindowPlacement( hwnd, &wndpl )) return 0;
568 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
569 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
570 return wndpl.showCmd;
574 /***********************************************************************
575 * SetInternalWindowPos (USER.461)
577 void SetInternalWindowPos( HWND hwnd, WORD showCmd, LPRECT rect, LPPOINT pt )
579 WINDOWPLACEMENT wndpl;
580 WND *wndPtr = WIN_FindWndPtr( hwnd );
582 wndpl.length = sizeof(wndpl);
583 wndpl.flags = (pt != NULL) ? WPF_SETMINPOSITION : 0;
584 wndpl.showCmd = showCmd;
585 if (pt) wndpl.ptMinPosition = *pt;
586 wndpl.rcNormalPosition = (rect != NULL) ? *rect : wndPtr->rectNormal;
587 wndpl.ptMaxPosition = wndPtr->ptMaxPos;
588 SetWindowPlacement( hwnd, &wndpl );
592 /***********************************************************************
593 * GetWindowPlacement (USER.370)
595 BOOL GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
597 WND *wndPtr = WIN_FindWndPtr( hwnd );
598 if (!wndPtr) return FALSE;
600 wndpl->length = sizeof(*wndpl);
602 wndpl->showCmd = IsZoomed(hwnd) ? SW_SHOWMAXIMIZED :
603 (IsIconic(hwnd) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL);
604 wndpl->ptMinPosition = wndPtr->ptIconPos;
605 wndpl->ptMaxPosition = wndPtr->ptMaxPos;
606 wndpl->rcNormalPosition = wndPtr->rectNormal;
611 /***********************************************************************
612 * SetWindowPlacement (USER.371)
614 BOOL SetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
616 WND *wndPtr = WIN_FindWndPtr( hwnd );
617 if (!wndPtr) return FALSE;
619 if (wndpl->flags & WPF_SETMINPOSITION)
620 wndPtr->ptIconPos = wndpl->ptMinPosition;
621 if ((wndpl->flags & WPF_RESTORETOMAXIMIZED) &&
622 (wndpl->showCmd == SW_SHOWMINIMIZED)) wndPtr->flags |= WIN_RESTORE_MAX;
623 wndPtr->ptMaxPos = wndpl->ptMaxPosition;
624 wndPtr->rectNormal = wndpl->rcNormalPosition;
625 ShowWindow( hwnd, wndpl->showCmd );
629 /*******************************************************************
630 * ACTIVATEAPP_callback
632 BOOL ACTIVATEAPP_callback(HWND hWnd, LPARAM lParam)
634 ACTIVATESTRUCT *lpActStruct = (ACTIVATESTRUCT*)lParam;
635 WND *wndPtr = WIN_FindWndPtr( hWnd );
637 if( !wndPtr || hWnd == GetDesktopWindow()) return 1;
639 if( MSG_GetQueueTask(wndPtr->hmemTaskQ) != lpActStruct->hTaskSendTo )
642 SendMessage( hWnd, WM_ACTIVATEAPP, lpActStruct->wFlag,
643 (LPARAM)((lpActStruct->hWindowTask)?lpActStruct->hWindowTask:0));
648 /*******************************************************************
649 * WINPOS_SetActiveWindow
651 * back-end to SetActiveWindow
653 BOOL WINPOS_SetActiveWindow( HWND hWnd, BOOL fMouse, BOOL fChangeFocus )
655 WND *wndPtr = WIN_FindWndPtr(hWnd);
656 WND *wndTemp = WIN_FindWndPtr(hwndActive);
657 CBTACTIVATESTRUCT cbtStruct = { fMouse , hwndActive };
658 FARPROC enumCallback = (FARPROC)GetWndProcEntry16("ActivateAppProc");
659 ACTIVATESTRUCT actStruct;
660 WORD wIconized=0,wRet= 0;
663 /* paranoid checks */
664 if( !hWnd || hWnd == GetDesktopWindow() || hWnd == hwndActive )
667 if( GetTaskQueue(0) != wndPtr->hmemTaskQ )
671 wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
673 dprintf_win(stddeb,"WINPOS_ActivateWindow: no current active window.\n");
675 /* call CBT hook chain */
676 wRet = HOOK_CallHooks(WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd,
677 (LPARAM)MAKE_SEGPTR(&cbtStruct));
679 if( wRet ) return wRet;
681 /* set prev active wnd to current active wnd and send notification */
682 if( (hwndPrevActive = hwndActive) )
684 /* FIXME: need a Win32 translation for WINELIB32 */
685 if( !SendMessage(hwndPrevActive, WM_NCACTIVATE, 0, MAKELONG(hWnd,wIconized)) )
687 if (GetSysModalWindow() != hWnd) return 0;
688 /* disregard refusal if hWnd is sysmodal */
692 SendMessage( hwndActive, WM_ACTIVATE,
693 MAKEWPARAM( WA_INACTIVE, wIconized ),
696 SendMessage(hwndPrevActive, WM_ACTIVATE, WA_INACTIVE,
697 MAKELONG(hWnd,wIconized));
700 /* check if something happened during message processing */
701 if( hwndPrevActive != hwndActive ) return 0;
707 /* send palette messages */
708 if( SendMessage( hWnd, WM_QUERYNEWPALETTE, 0, 0L) )
709 SendMessage((HWND)-1, WM_PALETTEISCHANGING, (WPARAM)hWnd, 0L );
711 /* if prev wnd is minimized redraw icon title
714 wndTemp = WIN_FindWndPtr( WIN_GetTopParent( hwndPrevActive ) );
716 if(wndTemp->dwStyle & WS_MINIMIZE)
717 RedrawIconTitle(hwndPrevActive);
720 if (!(wndPtr->dwStyle & WS_CHILD))
722 /* check Z-order and bring hWnd to the top */
723 wndTemp = WIN_FindWndPtr( GetDesktopWindow() );
725 for( ; wndTemp; wndTemp = WIN_FindWndPtr( wndTemp->hwndNext ))
726 if( wndTemp->dwStyle & WS_VISIBLE )
729 if( wndTemp != wndPtr )
730 SetWindowPos(hWnd, HWND_TOP, 0,0,0,0,
731 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
734 if( !IsWindow(hWnd) ) return 0;
738 wndTemp = WIN_FindWndPtr( hwndPrevActive );
739 if (wndTemp) hActiveQ = wndTemp->hmemTaskQ;
742 /* send WM_ACTIVATEAPP if necessary */
743 if (hActiveQ != wndPtr->hmemTaskQ)
745 HTASK hT = MSG_GetQueueTask( hActiveQ );
747 actStruct.wFlag = 0; /* deactivate */
748 actStruct.hWindowTask = MSG_GetQueueTask(wndPtr->hmemTaskQ);
749 actStruct.hTaskSendTo = hT;
751 /* send WM_ACTIVATEAPP to top-level windows
752 * that belong to the actStruct.hTaskSendTo task
754 EnumWindows( enumCallback , (LPARAM)&actStruct );
756 actStruct.wFlag = 1; /* activate */
757 actStruct.hWindowTask = hT;
758 actStruct.hTaskSendTo = MSG_GetQueueTask( wndPtr->hmemTaskQ );
760 EnumWindows( enumCallback , (LPARAM)&actStruct );
762 if( !IsWindow(hWnd) ) return 0;
765 /* walk up to the first unowned window */
768 while(wndTemp->hwndOwner)
770 wndTemp = WIN_FindWndPtr(wndTemp->hwndOwner);
773 /* there must be an unowned window in hierarchy */
774 dprintf_win(stddeb,"WINPOS_ActivateWindow: broken owner list\n");
779 /* and set last active owned popup */
780 wndTemp->hwndLastActive = hWnd;
782 wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
783 /* FIXME: Needs a Win32 translation for WINELIB32 */
784 SendMessage( hWnd, WM_NCACTIVATE, 1,
785 MAKELONG(hwndPrevActive,wIconized));
787 SendMessage( hWnd, WM_ACTIVATE,
788 MAKEWPARAM( (fMouse)?WA_CLICKACTIVE:WA_ACTIVE, wIconized),
789 (LPARAM)hwndPrevActive );
791 SendMessage( hWnd, WM_ACTIVATE, (fMouse)? WA_CLICKACTIVE : WA_ACTIVE,
792 MAKELONG(hwndPrevActive,wIconized));
795 if( !IsWindow(hWnd) ) return 0;
797 /* change focus if possible */
798 if( fChangeFocus && GetFocus() )
799 if( WIN_GetTopParent(GetFocus()) != hwndActive )
800 FOCUS_SwitchFocus( GetFocus(),
801 (wndPtr->dwStyle & WS_MINIMIZE)? 0: hwndActive);
803 /* if active wnd is minimized redraw icon title
806 wndPtr = WIN_FindWndPtr(hwndActive);
807 if(wndPtr->dwStyle & WS_MINIMIZE)
808 RedrawIconTitle(hwndActive);
811 return (hWnd == hwndActive);
815 /*******************************************************************
816 * WINPOS_ChangeActiveWindow
819 BOOL WINPOS_ChangeActiveWindow( HWND hWnd, BOOL mouseMsg )
821 WND *wndPtr = WIN_FindWndPtr(hWnd);
823 if( !wndPtr ) return FALSE;
825 /* minors are not allowed */
826 if( (wndPtr->dwStyle & WS_CHILD) && !( wndPtr->dwStyle & WS_POPUP))
827 return SendMessage(hWnd, WM_CHILDACTIVATE, 0, 0L);
829 if( hWnd == hwndActive ) return FALSE;
831 if( !WINPOS_SetActiveWindow(hWnd ,mouseMsg ,TRUE) )
834 /* switch desktop queue to current active here */
835 if( wndPtr->hwndParent == GetDesktopWindow())
842 /***********************************************************************
843 * WINPOS_SendNCCalcSize
845 * Send a WM_NCCALCSIZE message to a window.
846 * All parameters are read-only except newClientRect.
847 * oldWindowRect, oldClientRect and winpos must be non-NULL only
848 * when calcValidRect is TRUE.
850 LONG WINPOS_SendNCCalcSize( HWND hwnd, BOOL calcValidRect, RECT *newWindowRect,
851 RECT *oldWindowRect, RECT *oldClientRect,
852 WINDOWPOS *winpos, RECT *newClientRect )
854 NCCALCSIZE_PARAMS params;
857 params.rgrc[0] = *newWindowRect;
860 params.rgrc[1] = *oldWindowRect;
861 params.rgrc[2] = *oldClientRect;
862 params.lppos = winpos;
864 result = SendMessage( hwnd, WM_NCCALCSIZE, calcValidRect,
865 (LPARAM)MAKE_SEGPTR( ¶ms ) );
866 dprintf_win(stddeb, "WINPOS_SendNCCalcSize: %d %d %d %d\n",
867 (int)params.rgrc[0].top, (int)params.rgrc[0].left,
868 (int)params.rgrc[0].bottom, (int)params.rgrc[0].right);
869 *newClientRect = params.rgrc[0];
874 /***********************************************************************
875 * WINPOS_HandleWindowPosChanging
877 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
879 LONG WINPOS_HandleWindowPosChanging( WINDOWPOS *winpos )
882 WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
883 if (!wndPtr || (winpos->flags & SWP_NOSIZE)) return 0;
884 if ((wndPtr->dwStyle & WS_THICKFRAME) ||
885 ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
887 NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
888 winpos->cx = MIN( winpos->cx, maxSize.x );
889 winpos->cy = MIN( winpos->cy, maxSize.y );
895 /***********************************************************************
896 * WINPOS_MoveWindowZOrder
898 * Move a window in Z order, invalidating everything that needs it.
899 * Only necessary for windows without associated X window.
901 static void WINPOS_MoveWindowZOrder( HWND hwnd, HWND hwndAfter )
905 WND *wndPtr = WIN_FindWndPtr( hwnd );
907 /* We have two possible cases:
908 * - The window is moving up: we have to invalidate all areas
909 * of the window that were covered by other windows
910 * - The window is moving down: we have to invalidate areas
911 * of other windows covered by this one.
914 if (hwndAfter == HWND_TOP)
918 else if (hwndAfter == HWND_BOTTOM)
920 if (!wndPtr->hwndNext) return; /* Already at the bottom */
925 if (wndPtr->hwndNext == hwndAfter) return; /* Already placed right */
927 /* Determine which window we encounter first in Z-order */
928 hwndCur = GetWindow( wndPtr->hwndParent, GW_CHILD );
929 while ((hwndCur != hwnd) && (hwndCur != hwndAfter))
930 hwndCur = GetWindow( hwndCur, GW_HWNDNEXT );
931 movingUp = (hwndCur == hwndAfter);
936 HWND hwndPrevAfter = wndPtr->hwndNext;
937 WIN_UnlinkWindow( hwnd );
938 WIN_LinkWindow( hwnd, hwndAfter );
939 hwndCur = wndPtr->hwndNext;
940 while (hwndCur != hwndPrevAfter)
942 WND *curPtr = WIN_FindWndPtr( hwndCur );
943 RECT rect = curPtr->rectWindow;
944 OffsetRect( &rect, -wndPtr->rectClient.left,
945 -wndPtr->rectClient.top );
946 RedrawWindow( hwnd, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
947 RDW_FRAME | RDW_ERASE );
948 hwndCur = curPtr->hwndNext;
951 else /* Moving down */
953 hwndCur = wndPtr->hwndNext;
954 WIN_UnlinkWindow( hwnd );
955 WIN_LinkWindow( hwnd, hwndAfter );
956 while (hwndCur != hwnd)
958 WND *curPtr = WIN_FindWndPtr( hwndCur );
959 RECT rect = wndPtr->rectWindow;
960 OffsetRect( &rect, -curPtr->rectClient.left,
961 -curPtr->rectClient.top );
962 RedrawWindow( hwndCur, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
963 RDW_FRAME | RDW_ERASE );
964 hwndCur = curPtr->hwndNext;
970 /***********************************************************************
971 * WINPOS_SetXWindowPos
973 * SetWindowPos() for an X window. Used by the real SetWindowPos().
975 static void WINPOS_SetXWindowPos( WINDOWPOS *winpos )
977 XWindowChanges winChanges;
979 WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
981 if (!(winpos->flags & SWP_NOSIZE))
983 winChanges.width = winpos->cx;
984 winChanges.height = winpos->cy;
985 changeMask |= CWWidth | CWHeight;
987 if (!(winpos->flags & SWP_NOMOVE))
989 winChanges.x = winpos->x;
990 winChanges.y = winpos->y;
991 changeMask |= CWX | CWY;
993 if (!(winpos->flags & SWP_NOZORDER))
995 if (winpos->hwndInsertAfter == HWND_TOP) winChanges.stack_mode = Above;
996 else winChanges.stack_mode = Below;
997 if ((winpos->hwndInsertAfter != HWND_TOP) &&
998 (winpos->hwndInsertAfter != HWND_BOTTOM))
1000 WND * insertPtr = WIN_FindWndPtr( winpos->hwndInsertAfter );
1001 winChanges.sibling = insertPtr->window;
1002 changeMask |= CWSibling;
1004 changeMask |= CWStackMode;
1007 XConfigureWindow( display, wndPtr->window, changeMask, &winChanges );
1011 /***********************************************************************
1012 * SetWindowPos (USER.232)
1014 BOOL SetWindowPos( HWND hwnd, HWND hwndInsertAfter, INT x, INT y,
1015 INT cx, INT cy, WORD flags )
1019 RECT newWindowRect, newClientRect;
1022 /* Check window handle */
1024 if (hwnd == GetDesktopWindow()) return FALSE;
1025 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
1027 /* Check for windows that may not be resized
1028 FIXME: this should be done only for Windows 3.0 programs */
1029 if (flags ==(SWP_SHOWWINDOW) || flags ==(SWP_HIDEWINDOW ) )
1030 flags |= SWP_NOSIZE | SWP_NOMOVE;
1032 /* Check dimensions */
1034 if (cx <= 0) cx = 1;
1035 if (cy <= 0) cy = 1;
1039 if (hwnd == hwndActive) flags |= SWP_NOACTIVATE; /* Already active */
1040 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == cx) &&
1041 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == cy))
1042 flags |= SWP_NOSIZE; /* Already the right size */
1043 if ((wndPtr->rectWindow.left == x) && (wndPtr->rectWindow.top == y))
1044 flags |= SWP_NOMOVE; /* Already the right position */
1046 /* Check hwndInsertAfter */
1048 if (!(flags & (SWP_NOZORDER | SWP_NOACTIVATE)))
1050 /* Ignore TOPMOST flags when activating a window */
1051 /* _and_ moving it in Z order. */
1052 if ((hwndInsertAfter == HWND_TOPMOST) ||
1053 (hwndInsertAfter == HWND_NOTOPMOST))
1054 hwndInsertAfter = HWND_TOP;
1056 /* TOPMOST not supported yet */
1057 if ((hwndInsertAfter == HWND_TOPMOST) ||
1058 (hwndInsertAfter == HWND_NOTOPMOST)) hwndInsertAfter = HWND_TOP;
1059 /* hwndInsertAfter must be a sibling of the window */
1060 if ((hwndInsertAfter != HWND_TOP) && (hwndInsertAfter != HWND_BOTTOM) &&
1061 (wndPtr->hwndParent != WIN_FindWndPtr(hwndInsertAfter)->hwndParent))
1064 /* Fill the WINDOWPOS structure */
1067 winpos.hwndInsertAfter = hwndInsertAfter;
1072 winpos.flags = flags;
1074 /* Send WM_WINDOWPOSCHANGING message */
1076 if (!(flags & SWP_NOSENDCHANGING))
1077 SendMessage( hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1079 /* Calculate new position and size */
1081 newWindowRect = wndPtr->rectWindow;
1082 newClientRect = wndPtr->rectClient;
1084 if (!(winpos.flags & SWP_NOSIZE))
1086 newWindowRect.right = newWindowRect.left + winpos.cx;
1087 newWindowRect.bottom = newWindowRect.top + winpos.cy;
1089 if (!(winpos.flags & SWP_NOMOVE))
1091 newWindowRect.left = winpos.x;
1092 newWindowRect.top = winpos.y;
1093 newWindowRect.right += winpos.x - wndPtr->rectWindow.left;
1094 newWindowRect.bottom += winpos.y - wndPtr->rectWindow.top;
1097 /* Reposition window in Z order */
1099 if (!(winpos.flags & SWP_NOZORDER))
1103 WIN_UnlinkWindow( winpos.hwnd );
1104 WIN_LinkWindow( winpos.hwnd, hwndInsertAfter );
1106 else WINPOS_MoveWindowZOrder( winpos.hwnd, hwndInsertAfter );
1109 /* Send WM_NCCALCSIZE message to get new client area */
1111 result = WINPOS_SendNCCalcSize( winpos.hwnd, TRUE, &newWindowRect,
1112 &wndPtr->rectWindow, &wndPtr->rectClient,
1113 &winpos, &newClientRect );
1114 /* FIXME: Should handle result here */
1116 /* Perform the moving and resizing */
1120 WINPOS_SetXWindowPos( &winpos );
1121 wndPtr->rectWindow = newWindowRect;
1122 wndPtr->rectClient = newClientRect;
1126 RECT oldWindowRect = wndPtr->rectWindow;
1128 wndPtr->rectWindow = newWindowRect;
1129 wndPtr->rectClient = newClientRect;
1131 if (!(flags & SWP_NOREDRAW) &&
1132 (!(flags & SWP_NOSIZE) || !(flags & SWP_NOMOVE) ||
1133 (!(flags & SWP_NOZORDER) && (hwndInsertAfter != HWND_TOP))))
1135 HRGN hrgn1 = CreateRectRgnIndirect( &oldWindowRect );
1136 HRGN hrgn2 = CreateRectRgnIndirect( &wndPtr->rectWindow );
1137 HRGN hrgn3 = CreateRectRgn( 0, 0, 0, 0 );
1138 CombineRgn( hrgn3, hrgn1, hrgn2, RGN_DIFF );
1139 RedrawWindow( wndPtr->hwndParent, NULL, hrgn3,
1140 RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1142 /* DCE_GetVisRgn should be called for old coordinates
1143 * and for new, then OffsetRgn and CombineRgn -
1144 * voila, a nice update region to use here - AK.
1146 if ((oldWindowRect.left != wndPtr->rectWindow.left) ||
1147 (oldWindowRect.top != wndPtr->rectWindow.top))
1149 RedrawWindow( winpos.hwnd, NULL, 0, RDW_INVALIDATE |
1150 RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1153 if( CombineRgn( hrgn3, hrgn2, hrgn1, RGN_DIFF) != NULLREGION )
1154 RedrawWindow( winpos.hwnd, NULL, hrgn3, RDW_INVALIDATE |
1155 RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1157 DeleteObject( hrgn1 );
1158 DeleteObject( hrgn2 );
1159 DeleteObject( hrgn3 );
1163 if (flags & SWP_SHOWWINDOW)
1165 wndPtr->dwStyle |= WS_VISIBLE;
1168 XMapWindow( display, wndPtr->window );
1172 if (!(flags & SWP_NOREDRAW))
1173 RedrawWindow( winpos.hwnd, NULL, 0,
1174 RDW_INVALIDATE | RDW_ALLCHILDREN |
1175 RDW_FRAME | RDW_ERASE );
1178 else if (flags & SWP_HIDEWINDOW)
1180 wndPtr->dwStyle &= ~WS_VISIBLE;
1183 XUnmapWindow( display, wndPtr->window );
1187 if (!(flags & SWP_NOREDRAW))
1188 RedrawWindow( wndPtr->hwndParent, &wndPtr->rectWindow, 0,
1189 RDW_INVALIDATE | RDW_FRAME |
1190 RDW_ALLCHILDREN | RDW_ERASE );
1193 if ((winpos.hwnd == GetFocus()) || IsChild(winpos.hwnd, GetFocus()))
1194 SetFocus( GetParent(winpos.hwnd) ); /* Revert focus to parent */
1196 if (winpos.hwnd == hwndActive)
1198 /* Activate previously active window if possible */
1199 HWND newActive = hwndPrevActive;
1200 if (!IsWindow(newActive) || (newActive == winpos.hwnd))
1202 newActive = GetTopWindow( GetDesktopWindow() );
1203 if (newActive == winpos.hwnd) newActive = wndPtr->hwndNext;
1205 WINPOS_ChangeActiveWindow( newActive, FALSE );
1209 /* Activate the window */
1211 if (!(flags & SWP_NOACTIVATE))
1212 WINPOS_ChangeActiveWindow( winpos.hwnd, FALSE );
1214 /* Repaint the window */
1216 if (wndPtr->window) MSG_Synchronize(); /* Wait for all expose events */
1218 EVENT_DummyMotionNotify(); /* Simulate a mouse event to set the cursor */
1220 if ((flags & SWP_FRAMECHANGED) && !(flags & SWP_NOREDRAW))
1221 RedrawWindow( winpos.hwnd, NULL, 0,
1222 RDW_ALLCHILDREN | /*FIXME: this should not be necessary*/
1223 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE );
1224 if (!(flags & SWP_DEFERERASE))
1225 RedrawWindow( wndPtr->hwndParent, NULL, 0,
1226 RDW_ALLCHILDREN | RDW_ERASENOW );
1228 /* And last, send the WM_WINDOWPOSCHANGED message */
1230 winpos.flags |= SWP_NOMOVE; /* prevent looping.. window is already moved ??? (FIXME)*/
1232 if (!(winpos.flags & SWP_NOSENDCHANGING))
1233 SendMessage( winpos.hwnd, WM_WINDOWPOSCHANGED,
1234 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1240 /***********************************************************************
1241 * BeginDeferWindowPos (USER.259)
1243 HDWP BeginDeferWindowPos( INT count )
1248 if (count <= 0) return 0;
1249 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1250 if (!handle) return 0;
1251 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1252 pDWP->actualCount = 0;
1253 pDWP->suggestedCount = count;
1255 pDWP->wMagic = DWP_MAGIC;
1256 pDWP->hwndParent = 0;
1261 /***********************************************************************
1262 * DeferWindowPos (USER.260)
1264 HDWP DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter, INT x, INT y,
1265 INT cx, INT cy, UINT flags )
1269 HDWP newhdwp = hdwp;
1272 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1273 if (!pDWP) return 0;
1275 /* All the windows of a DeferWindowPos() must have the same parent */
1277 parent = WIN_FindWndPtr( hwnd )->hwndParent;
1278 if (pDWP->actualCount == 0) pDWP->hwndParent = parent;
1279 else if (parent != pDWP->hwndParent)
1281 USER_HEAP_FREE( hdwp );
1285 for (i = 0; i < pDWP->actualCount; i++)
1287 if (pDWP->winPos[i].hwnd == hwnd)
1289 /* Merge with the other changes */
1290 if (!(flags & SWP_NOZORDER))
1292 pDWP->winPos[i].hwndInsertAfter = hwndAfter;
1294 if (!(flags & SWP_NOMOVE))
1296 pDWP->winPos[i].x = x;
1297 pDWP->winPos[i].y = y;
1299 if (!(flags & SWP_NOSIZE))
1301 pDWP->winPos[i].cx = cx;
1302 pDWP->winPos[i].cy = cy;
1304 pDWP->winPos[i].flags &= flags & (SWP_NOSIZE | SWP_NOMOVE |
1305 SWP_NOZORDER | SWP_NOREDRAW |
1306 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1308 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1313 if (pDWP->actualCount >= pDWP->suggestedCount)
1315 newhdwp = USER_HEAP_REALLOC( hdwp,
1316 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1317 if (!newhdwp) return 0;
1318 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1319 pDWP->suggestedCount++;
1321 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1322 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1323 pDWP->winPos[pDWP->actualCount].x = x;
1324 pDWP->winPos[pDWP->actualCount].y = y;
1325 pDWP->winPos[pDWP->actualCount].cx = cx;
1326 pDWP->winPos[pDWP->actualCount].cy = cy;
1327 pDWP->winPos[pDWP->actualCount].flags = flags;
1328 pDWP->actualCount++;
1333 /***********************************************************************
1334 * EndDeferWindowPos (USER.261)
1336 BOOL EndDeferWindowPos( HDWP hdwp )
1343 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1344 if (!pDWP) return FALSE;
1345 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1347 if (!(res = SetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1348 winpos->x, winpos->y, winpos->cx, winpos->cy,
1349 winpos->flags ))) break;
1351 USER_HEAP_FREE( hdwp );
1356 /***********************************************************************
1357 * TileChildWindows (USER.199)
1359 void TileChildWindows( HWND parent, WORD action )
1361 printf("STUB TileChildWindows("NPFMT", %d)\n", parent, action);
1364 /***********************************************************************
1365 * CascageChildWindows (USER.198)
1367 void CascadeChildWindows( HWND parent, WORD action )
1369 printf("STUB CascadeChildWindows("NPFMT", %d)\n", parent, action);