Release 960528
[wine/gsoc-2012-control.git] / windows / winpos.c
blob74a8942a820280d0e77356b3e2cbcb89333a8637
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995,1996 Alex Korobka
6 */
8 #include "sysmetrics.h"
9 #include "heap.h"
10 #include "module.h"
11 #include "user.h"
12 #include "win.h"
13 #include "hook.h"
14 #include "message.h"
15 #include "queue.h"
16 #include "stackframe.h"
17 #include "winpos.h"
18 #include "nonclient.h"
19 #include "stddebug.h"
20 /* #define DEBUG_WIN */
21 #include "debug.h"
23 #define SWP_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
25 /* ----- external functions ----- */
27 extern void FOCUS_SwitchFocus( HWND , HWND );
28 extern HRGN DCE_GetVisRgn( HWND, WORD );
29 extern HWND CARET_GetHwnd();
31 /* ----- internal variables ----- */
33 static HWND hwndActive = 0; /* Currently active window */
34 static HWND hwndPrevActive = 0; /* Previously active window */
36 /***********************************************************************
37 * WINPOS_FindIconPos
39 * Find a suitable place for an iconic window.
40 * The new position is stored into wndPtr->ptIconPos.
42 void WINPOS_FindIconPos( HWND hwnd )
44 RECT16 rectParent;
45 short x, y, xspacing, yspacing;
46 WND * wndPtr = WIN_FindWndPtr( hwnd );
48 if (!wndPtr || !wndPtr->parent) return;
49 GetClientRect16( wndPtr->parent->hwndSelf, &rectParent );
50 if ((wndPtr->ptIconPos.x >= rectParent.left) &&
51 (wndPtr->ptIconPos.x + SYSMETRICS_CXICON < rectParent.right) &&
52 (wndPtr->ptIconPos.y >= rectParent.top) &&
53 (wndPtr->ptIconPos.y + SYSMETRICS_CYICON < rectParent.bottom))
54 return; /* The icon already has a suitable position */
56 xspacing = yspacing = 70; /* FIXME: This should come from WIN.INI */
57 y = rectParent.bottom;
58 for (;;)
60 for (x = rectParent.left; x<=rectParent.right-xspacing; x += xspacing)
62 /* Check if another icon already occupies this spot */
63 WND *childPtr = wndPtr->parent->child;
64 while (childPtr)
66 if ((childPtr->dwStyle & WS_MINIMIZE) && (childPtr != wndPtr))
68 if ((childPtr->rectWindow.left < x + xspacing) &&
69 (childPtr->rectWindow.right >= x) &&
70 (childPtr->rectWindow.top <= y) &&
71 (childPtr->rectWindow.bottom > y - yspacing))
72 break; /* There's a window in there */
74 childPtr = childPtr->next;
76 if (!childPtr)
78 /* No window was found, so it's OK for us */
79 wndPtr->ptIconPos.x = x + (xspacing - SYSMETRICS_CXICON) / 2;
80 wndPtr->ptIconPos.y = y - (yspacing + SYSMETRICS_CYICON) / 2;
81 return;
84 y -= yspacing;
89 /***********************************************************************
90 * ArrangeIconicWindows (USER.170)
92 UINT ArrangeIconicWindows( HWND parent )
94 RECT16 rectParent;
95 HWND hwndChild;
96 INT x, y, xspacing, yspacing;
98 GetClientRect16( parent, &rectParent );
99 x = rectParent.left;
100 y = rectParent.bottom;
101 xspacing = yspacing = 70; /* FIXME: This should come from WIN.INI */
102 hwndChild = GetWindow( parent, GW_CHILD );
103 while (hwndChild)
105 if (IsIconic( hwndChild ))
107 SetWindowPos( hwndChild, 0, x + (xspacing - SYSMETRICS_CXICON) / 2,
108 y - (yspacing + SYSMETRICS_CYICON) / 2, 0, 0,
109 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
110 if (x <= rectParent.right - xspacing) x += xspacing;
111 else
113 x = rectParent.left;
114 y -= yspacing;
117 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
119 return yspacing;
123 /***********************************************************************
124 * GetWindowRect16 (USER.32)
126 void GetWindowRect16( HWND16 hwnd, LPRECT16 rect )
128 WND * wndPtr = WIN_FindWndPtr( hwnd );
129 if (!wndPtr) return;
131 *rect = wndPtr->rectWindow;
132 if (wndPtr->dwStyle & WS_CHILD)
133 MapWindowPoints16( wndPtr->parent->hwndSelf, 0, (POINT16 *)rect, 2 );
137 /***********************************************************************
138 * GetWindowRect32 (USER.32)
140 void GetWindowRect32( HWND32 hwnd, LPRECT32 rect )
142 WND * wndPtr = WIN_FindWndPtr( hwnd );
143 if (!wndPtr) return;
145 CONV_RECT16TO32( &wndPtr->rectWindow, rect );
146 if (wndPtr->dwStyle & WS_CHILD)
147 MapWindowPoints32( wndPtr->parent->hwndSelf, 0, (POINT32 *)rect, 2 );
151 /***********************************************************************
152 * GetClientRect16 (USER.33)
154 void GetClientRect16( HWND16 hwnd, LPRECT16 rect )
156 WND * wndPtr = WIN_FindWndPtr( hwnd );
158 rect->left = rect->top = rect->right = rect->bottom = 0;
159 if (wndPtr)
161 rect->right = wndPtr->rectClient.right - wndPtr->rectClient.left;
162 rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
167 /***********************************************************************
168 * GetClientRect32 (USER32.219)
170 void GetClientRect32( HWND32 hwnd, LPRECT32 rect )
172 WND * wndPtr = WIN_FindWndPtr( hwnd );
174 rect->left = rect->top = rect->right = rect->bottom = 0;
175 if (wndPtr)
177 rect->right = wndPtr->rectClient.right - wndPtr->rectClient.left;
178 rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
183 /*******************************************************************
184 * ClientToScreen16 (USER.28)
186 BOOL16 ClientToScreen16( HWND16 hwnd, LPPOINT16 lppnt )
188 MapWindowPoints16( hwnd, 0, lppnt, 1 );
189 return TRUE;
193 /*******************************************************************
194 * ClientToScreen32 (USER32.51)
196 BOOL32 ClientToScreen32( HWND32 hwnd, LPPOINT32 lppnt )
198 MapWindowPoints32( hwnd, 0, lppnt, 1 );
199 return TRUE;
203 /*******************************************************************
204 * ScreenToClient16 (USER.29)
206 void ScreenToClient16( HWND16 hwnd, LPPOINT16 lppnt )
208 MapWindowPoints16( 0, hwnd, lppnt, 1 );
212 /*******************************************************************
213 * ScreenToClient32 (USER32.446)
215 void ScreenToClient32( HWND32 hwnd, LPPOINT32 lppnt )
217 MapWindowPoints32( 0, hwnd, lppnt, 1 );
221 /***********************************************************************
222 * WINPOS_WindowFromPoint
224 * Find the window and hittest for a given point.
226 INT16 WINPOS_WindowFromPoint( POINT16 pt, WND **ppWnd )
228 WND *wndPtr;
229 INT16 hittest = HTERROR;
230 INT16 x, y;
232 *ppWnd = NULL;
233 x = pt.x;
234 y = pt.y;
235 wndPtr = WIN_GetDesktop()->child;
236 for (;;)
238 while (wndPtr)
240 /* If point is in window, and window is visible, and it */
241 /* is enabled (or it's a top-level window), then explore */
242 /* its children. Otherwise, go to the next window. */
244 if ((wndPtr->dwStyle & WS_VISIBLE) &&
245 (!(wndPtr->dwStyle & WS_DISABLED) ||
246 ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)) &&
247 (x >= wndPtr->rectWindow.left) &&
248 (x < wndPtr->rectWindow.right) &&
249 (y >= wndPtr->rectWindow.top) &&
250 (y < wndPtr->rectWindow.bottom))
252 *ppWnd = wndPtr; /* Got a suitable window */
254 /* If window is minimized or disabled, return at once */
255 if (wndPtr->dwStyle & WS_MINIMIZE) return HTCAPTION;
256 if (wndPtr->dwStyle & WS_DISABLED) return HTERROR;
258 /* If point is not in client area, ignore the children */
259 if ((x < wndPtr->rectClient.left) ||
260 (x >= wndPtr->rectClient.right) ||
261 (y < wndPtr->rectClient.top) ||
262 (y >= wndPtr->rectClient.bottom)) break;
264 x -= wndPtr->rectClient.left;
265 y -= wndPtr->rectClient.top;
266 wndPtr = wndPtr->child;
268 else wndPtr = wndPtr->next;
271 /* If nothing found, return the desktop window */
272 if (!*ppWnd)
274 *ppWnd = WIN_GetDesktop();
275 return HTCLIENT;
278 /* Send the WM_NCHITTEST message (only if to the same task) */
279 if ((*ppWnd)->hmemTaskQ != GetTaskQueue(0)) return HTCLIENT;
280 hittest = (INT)SendMessage16( (*ppWnd)->hwndSelf, WM_NCHITTEST, 0,
281 MAKELONG( pt.x, pt.y ) );
282 if (hittest != HTTRANSPARENT) return hittest; /* Found the window */
284 /* If no children found in last search, make point relative to parent*/
285 if (!wndPtr)
287 x += (*ppWnd)->rectClient.left;
288 y += (*ppWnd)->rectClient.top;
291 /* Restart the search from the next sibling */
292 wndPtr = (*ppWnd)->next;
293 *ppWnd = (*ppWnd)->parent;
298 /*******************************************************************
299 * WindowFromPoint16 (USER.30)
301 HWND16 WindowFromPoint16( POINT16 pt )
303 WND *pWnd;
304 WINPOS_WindowFromPoint( pt, &pWnd );
305 return pWnd->hwndSelf;
309 /*******************************************************************
310 * WindowFromPoint32 (USER32.581)
312 HWND32 WindowFromPoint32( POINT32 pt )
314 WND *pWnd;
315 POINT16 pt16;
316 CONV_POINT32TO16( &pt, &pt16 );
317 WINPOS_WindowFromPoint( pt16, &pWnd );
318 return (HWND32)pWnd->hwndSelf;
322 /*******************************************************************
323 * ChildWindowFromPoint16 (USER.191)
325 HWND16 ChildWindowFromPoint16( HWND16 hwndParent, POINT16 pt )
327 /* pt is in the client coordinates */
329 WND* wnd = WIN_FindWndPtr(hwndParent);
330 RECT16 rect;
332 if( !wnd ) return 0;
334 /* get client rect fast */
335 rect.top = rect.left = 0;
336 rect.right = wnd->rectClient.right - wnd->rectClient.left;
337 rect.bottom = wnd->rectClient.bottom - wnd->rectClient.top;
339 if (!PtInRect16( &rect, pt )) return 0;
341 wnd = wnd->child;
342 while ( wnd )
344 if (PtInRect16( &wnd->rectWindow, pt )) return wnd->hwndSelf;
345 wnd = wnd->next;
347 return hwndParent;
351 /*******************************************************************
352 * ChildWindowFromPoint32 (USER32.)
354 HWND32 ChildWindowFromPoint32( HWND32 hwndParent, POINT32 pt )
356 POINT16 pt16;
357 CONV_POINT32TO16( &pt, &pt16 );
358 return (HWND32)ChildWindowFromPoint16( hwndParent, pt16 );
362 /*******************************************************************
363 * WINPOS_GetWinOffset
365 * Calculate the offset between the origin of the two windows. Used
366 * to implement MapWindowPoints.
368 static void WINPOS_GetWinOffset( HWND32 hwndFrom, HWND32 hwndTo,
369 POINT32 *offset )
371 WND * wndPtr;
373 offset->x = offset->y = 0;
374 if (hwndFrom == hwndTo ) return;
376 /* Translate source window origin to screen coords */
377 if (hwndFrom)
379 if (!(wndPtr = WIN_FindWndPtr( hwndFrom )))
381 fprintf(stderr,"MapWindowPoints: bad hwndFrom = %04x\n",hwndFrom);
382 return;
384 while (wndPtr->parent)
386 offset->x += wndPtr->rectClient.left;
387 offset->y += wndPtr->rectClient.top;
388 wndPtr = wndPtr->parent;
392 /* Translate origin to destination window coords */
393 if (hwndTo)
395 if (!(wndPtr = WIN_FindWndPtr( hwndTo )))
397 fprintf(stderr,"MapWindowPoints: bad hwndTo = %04x\n", hwndTo );
398 return;
400 while (wndPtr->parent)
402 offset->x -= wndPtr->rectClient.left;
403 offset->y -= wndPtr->rectClient.top;
404 wndPtr = wndPtr->parent;
410 /*******************************************************************
411 * MapWindowPoints16 (USER.258)
413 void MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
414 LPPOINT16 lppt, UINT16 count )
416 POINT32 offset;
418 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
419 while (count--)
421 lppt->x += offset.x;
422 lppt->y += offset.y;
423 lppt++;
428 /*******************************************************************
429 * MapWindowPoints32 (USER32.385)
431 void MapWindowPoints32( HWND32 hwndFrom, HWND32 hwndTo,
432 LPPOINT32 lppt, UINT32 count )
434 POINT32 offset;
436 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
437 while (count--)
439 lppt->x += offset.x;
440 lppt->y += offset.y;
441 lppt++;
446 /***********************************************************************
447 * IsIconic (USER.31)
449 BOOL IsIconic(HWND hWnd)
451 WND * wndPtr = WIN_FindWndPtr(hWnd);
452 if (wndPtr == NULL) return FALSE;
453 return (wndPtr->dwStyle & WS_MINIMIZE) != 0;
457 /***********************************************************************
458 * IsZoomed (USER.272)
460 BOOL IsZoomed(HWND hWnd)
462 WND * wndPtr = WIN_FindWndPtr(hWnd);
463 if (wndPtr == NULL) return FALSE;
464 return (wndPtr->dwStyle & WS_MAXIMIZE) != 0;
468 /*******************************************************************
469 * GetActiveWindow (USER.60)
471 HWND GetActiveWindow()
473 return hwndActive;
477 /*******************************************************************
478 * SetActiveWindow (USER.59)
480 HWND SetActiveWindow( HWND hwnd )
482 HWND prev = hwndActive;
483 WND *wndPtr = WIN_FindWndPtr( hwnd );
485 if (!wndPtr || (wndPtr->dwStyle & WS_DISABLED) ||
486 !(wndPtr->dwStyle & WS_VISIBLE)) return 0;
488 WINPOS_SetActiveWindow( hwnd, 0, 0 );
489 return prev;
493 /***********************************************************************
494 * BringWindowToTop (USER.45)
496 BOOL BringWindowToTop( HWND hwnd )
498 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
502 /***********************************************************************
503 * MoveWindow (USER.56)
505 BOOL MoveWindow( HWND hwnd, short x, short y, short cx, short cy, BOOL repaint)
507 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
508 if (!repaint) flags |= SWP_NOREDRAW;
509 dprintf_win(stddeb, "MoveWindow: %04x %d,%d %dx%d %d\n",
510 hwnd, x, y, cx, cy, repaint );
511 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
515 /***********************************************************************
516 * ShowWindow (USER.42)
518 BOOL ShowWindow( HWND hwnd, int cmd )
520 WND * wndPtr = WIN_FindWndPtr( hwnd );
521 BOOL wasVisible;
522 POINT16 maxSize;
523 int swpflags = 0;
524 short x = 0, y = 0, cx = 0, cy = 0;
526 if (!wndPtr) return FALSE;
528 dprintf_win(stddeb,"ShowWindow: hwnd=%04x, cmd=%d\n", hwnd, cmd);
530 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
532 switch(cmd)
534 case SW_HIDE:
535 if (!wasVisible) return FALSE;
536 swpflags |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
537 SWP_NOACTIVATE | SWP_NOZORDER;
538 break;
540 case SW_SHOWMINNOACTIVE:
541 swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
542 /* fall through */
543 case SW_SHOWMINIMIZED:
544 swpflags |= SWP_SHOWWINDOW;
545 /* fall through */
546 case SW_MINIMIZE:
547 swpflags |= SWP_FRAMECHANGED;
548 if (!(wndPtr->dwStyle & WS_MINIMIZE))
550 if (wndPtr->dwStyle & WS_MAXIMIZE)
552 wndPtr->flags |= WIN_RESTORE_MAX;
553 wndPtr->dwStyle &= ~WS_MAXIMIZE;
555 else
557 wndPtr->flags &= ~WIN_RESTORE_MAX;
558 wndPtr->rectNormal = wndPtr->rectWindow;
560 wndPtr->dwStyle |= WS_MINIMIZE;
561 WINPOS_FindIconPos( hwnd );
562 x = wndPtr->ptIconPos.x;
563 y = wndPtr->ptIconPos.y;
564 cx = SYSMETRICS_CXICON;
565 cy = SYSMETRICS_CYICON;
567 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
568 break;
570 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE: */
571 swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
572 if (!(wndPtr->dwStyle & WS_MAXIMIZE))
574 /* Store the current position and find the maximized size */
575 if (!(wndPtr->dwStyle & WS_MINIMIZE))
576 wndPtr->rectNormal = wndPtr->rectWindow;
578 NC_GetMinMaxInfo( hwnd, &maxSize,
579 &wndPtr->ptMaxPos, NULL, NULL );
580 x = wndPtr->ptMaxPos.x;
581 y = wndPtr->ptMaxPos.y;
583 if( wndPtr->dwStyle & WS_MINIMIZE )
584 if( !SendMessage16( hwnd, WM_QUERYOPEN, 0, 0L ) )
586 swpflags |= SWP_NOSIZE;
587 break;
590 cx = maxSize.x;
591 cy = maxSize.y;
592 wndPtr->dwStyle &= ~WS_MINIMIZE;
593 wndPtr->dwStyle |= WS_MAXIMIZE;
595 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
596 break;
598 case SW_SHOWNA:
599 swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
600 /* fall through */
601 case SW_SHOW:
602 swpflags |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
603 break;
605 case SW_SHOWNOACTIVATE:
606 swpflags |= SWP_NOZORDER;
607 if (GetActiveWindow()) swpflags |= SWP_NOACTIVATE;
608 /* fall through */
609 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
610 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
611 case SW_RESTORE:
612 swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
614 if (wndPtr->dwStyle & WS_MINIMIZE)
616 if( !SendMessage16( hwnd, WM_QUERYOPEN, 0, 0L) )
618 swpflags |= SWP_NOSIZE;
619 break;
621 wndPtr->ptIconPos.x = wndPtr->rectWindow.left;
622 wndPtr->ptIconPos.y = wndPtr->rectWindow.top;
623 wndPtr->dwStyle &= ~WS_MINIMIZE;
624 if (wndPtr->flags & WIN_RESTORE_MAX)
626 /* Restore to maximized position */
627 NC_GetMinMaxInfo( hwnd, &maxSize, &wndPtr->ptMaxPos,
628 NULL, NULL );
629 x = wndPtr->ptMaxPos.x;
630 y = wndPtr->ptMaxPos.y;
631 cx = maxSize.x;
632 cy = maxSize.y;
633 wndPtr->dwStyle |= WS_MAXIMIZE;
635 else /* Restore to normal position */
637 x = wndPtr->rectNormal.left;
638 y = wndPtr->rectNormal.top;
639 cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
640 cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
643 else if (wndPtr->dwStyle & WS_MAXIMIZE)
645 wndPtr->ptMaxPos.x = wndPtr->rectWindow.left;
646 wndPtr->ptMaxPos.y = wndPtr->rectWindow.top;
647 wndPtr->dwStyle &= ~WS_MAXIMIZE;
648 x = wndPtr->rectNormal.left;
649 y = wndPtr->rectNormal.top;
650 cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
651 cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
653 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
654 break;
657 SendMessage16( hwnd, WM_SHOWWINDOW, (cmd != SW_HIDE), 0 );
658 SetWindowPos( hwnd, HWND_TOP, x, y, cx, cy, swpflags );
660 if (wndPtr->flags & WIN_NEED_SIZE)
662 int wParam = SIZE_RESTORED;
664 wndPtr->flags &= ~WIN_NEED_SIZE;
665 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
666 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
667 SendMessage16( hwnd, WM_SIZE, wParam,
668 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
669 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
670 SendMessage16( hwnd, WM_MOVE, 0,
671 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
674 if (hwnd == GetFocus())
676 SetFocus( (wndPtr->dwStyle & WS_CHILD)? wndPtr->parent->hwndSelf: 0 );
677 if (hwnd == CARET_GetHwnd()) DestroyCaret();
680 return wasVisible;
684 /***********************************************************************
685 * GetInternalWindowPos16 (USER.460)
687 UINT16 GetInternalWindowPos16( HWND16 hwnd, LPRECT16 rectWnd, LPPOINT16 ptIcon)
689 WINDOWPLACEMENT16 wndpl;
690 if (!GetWindowPlacement16( hwnd, &wndpl )) return 0;
691 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
692 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
693 return wndpl.showCmd;
697 /***********************************************************************
698 * GetInternalWindowPos32 (USER32.244)
700 UINT32 GetInternalWindowPos32( HWND32 hwnd, LPRECT32 rectWnd, LPPOINT32 ptIcon)
702 WINDOWPLACEMENT32 wndpl;
703 if (!GetWindowPlacement32( hwnd, &wndpl )) return 0;
704 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
705 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
706 return wndpl.showCmd;
710 /***********************************************************************
711 * SetInternalWindowPos16 (USER.461)
713 void SetInternalWindowPos16( HWND16 hwnd, UINT16 showCmd,
714 LPRECT16 rect, LPPOINT16 pt )
716 WINDOWPLACEMENT16 wndpl;
717 WND *wndPtr = WIN_FindWndPtr( hwnd );
719 wndpl.length = sizeof(wndpl);
720 wndpl.flags = (pt != NULL) ? WPF_SETMINPOSITION : 0;
721 wndpl.showCmd = showCmd;
722 if (pt) wndpl.ptMinPosition = *pt;
723 wndpl.rcNormalPosition = (rect != NULL) ? *rect : wndPtr->rectNormal;
724 wndpl.ptMaxPosition = wndPtr->ptMaxPos;
725 SetWindowPlacement16( hwnd, &wndpl );
729 /***********************************************************************
730 * SetInternalWindowPos32 (USER32.482)
732 void SetInternalWindowPos32( HWND32 hwnd, UINT32 showCmd,
733 LPRECT32 rect, LPPOINT32 pt )
735 WINDOWPLACEMENT32 wndpl;
736 WND *wndPtr = WIN_FindWndPtr( hwnd );
738 wndpl.length = sizeof(wndpl);
739 wndpl.flags = (pt != NULL) ? WPF_SETMINPOSITION : 0;
740 wndpl.showCmd = showCmd;
741 if (pt) wndpl.ptMinPosition = *pt;
742 if (rect) wndpl.rcNormalPosition = *rect;
743 else CONV_RECT16TO32( &wndPtr->rectNormal, &wndpl.rcNormalPosition );
744 CONV_POINT16TO32( &wndPtr->ptMaxPos, &wndpl.ptMaxPosition );
745 SetWindowPlacement32( hwnd, &wndpl );
749 /***********************************************************************
750 * GetWindowPlacement16 (USER.370)
752 BOOL16 GetWindowPlacement16( HWND16 hwnd, WINDOWPLACEMENT16 *wndpl )
754 WND *wndPtr = WIN_FindWndPtr( hwnd );
755 if (!wndPtr) return FALSE;
757 wndpl->length = sizeof(*wndpl);
758 wndpl->flags = 0;
759 wndpl->showCmd = IsZoomed(hwnd) ? SW_SHOWMAXIMIZED :
760 (IsIconic(hwnd) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL);
761 wndpl->ptMinPosition = wndPtr->ptIconPos;
762 wndpl->ptMaxPosition = wndPtr->ptMaxPos;
763 wndpl->rcNormalPosition = wndPtr->rectNormal;
764 return TRUE;
768 /***********************************************************************
769 * GetWindowPlacement32 (USER32.306)
771 BOOL32 GetWindowPlacement32( HWND32 hwnd, WINDOWPLACEMENT32 *wndpl )
773 WND *wndPtr = WIN_FindWndPtr( hwnd );
774 if (!wndPtr) return FALSE;
776 wndpl->length = sizeof(*wndpl);
777 wndpl->flags = 0;
778 wndpl->showCmd = IsZoomed(hwnd) ? SW_SHOWMAXIMIZED :
779 (IsIconic(hwnd) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL);
780 CONV_POINT16TO32( &wndPtr->ptIconPos, &wndpl->ptMinPosition );
781 CONV_POINT16TO32( &wndPtr->ptMaxPos, &wndpl->ptMaxPosition );
782 CONV_RECT16TO32( &wndPtr->rectNormal, &wndpl->rcNormalPosition );
783 return TRUE;
787 /***********************************************************************
788 * SetWindowPlacement16 (USER.371)
790 BOOL16 SetWindowPlacement16( HWND16 hwnd, const WINDOWPLACEMENT16 *wndpl )
792 WND *wndPtr = WIN_FindWndPtr( hwnd );
793 if (!wndPtr) return FALSE;
795 if (wndpl->flags & WPF_SETMINPOSITION)
796 wndPtr->ptIconPos = wndpl->ptMinPosition;
797 if ((wndpl->flags & WPF_RESTORETOMAXIMIZED) &&
798 (wndpl->showCmd == SW_SHOWMINIMIZED)) wndPtr->flags |= WIN_RESTORE_MAX;
799 wndPtr->ptMaxPos = wndpl->ptMaxPosition;
800 wndPtr->rectNormal = wndpl->rcNormalPosition;
801 ShowWindow( hwnd, wndpl->showCmd );
802 return TRUE;
806 /***********************************************************************
807 * SetWindowPlacement32 (USER32.518)
809 BOOL32 SetWindowPlacement32( HWND32 hwnd, const WINDOWPLACEMENT32 *wndpl )
811 WND *wndPtr = WIN_FindWndPtr( hwnd );
812 if (!wndPtr) return FALSE;
814 if (wndpl->flags & WPF_SETMINPOSITION)
815 CONV_POINT32TO16( &wndpl->ptMinPosition, &wndPtr->ptIconPos );
816 if ((wndpl->flags & WPF_RESTORETOMAXIMIZED) &&
817 (wndpl->showCmd == SW_SHOWMINIMIZED)) wndPtr->flags |= WIN_RESTORE_MAX;
818 CONV_POINT32TO16( &wndpl->ptMaxPosition, &wndPtr->ptMaxPos );
819 CONV_RECT32TO16( &wndpl->rcNormalPosition, &wndPtr->rectNormal );
820 ShowWindow( hwnd, wndpl->showCmd );
821 return TRUE;
825 /*******************************************************************
826 * ACTIVATEAPP_callback
828 BOOL ACTIVATEAPP_callback(HWND hWnd, LPARAM lParam)
830 ACTIVATESTRUCT *lpActStruct = (ACTIVATESTRUCT*)lParam;
832 if (GetWindowTask(hWnd) != lpActStruct->hTaskSendTo) return 1;
834 SendMessage16( hWnd, WM_ACTIVATEAPP, lpActStruct->wFlag,
835 (LPARAM)((lpActStruct->hWindowTask)?lpActStruct->hWindowTask:0));
836 return 1;
840 /*******************************************************************
841 * WINPOS_SetActiveWindow
843 * back-end to SetActiveWindow
845 BOOL WINPOS_SetActiveWindow( HWND hWnd, BOOL fMouse, BOOL fChangeFocus )
847 WND *wndPtr = WIN_FindWndPtr(hWnd);
848 WND *wndTemp = WIN_FindWndPtr(hwndActive);
849 CBTACTIVATESTRUCT *cbtStruct;
850 FARPROC enumCallback = MODULE_GetWndProcEntry16("ActivateAppProc");
851 ACTIVATESTRUCT actStruct;
852 WORD wIconized=0;
854 /* FIXME: When proper support for cooperative multitasking is in place
855 * hActiveQ will be global
858 HANDLE hActiveQ = 0;
860 /* paranoid checks */
861 if( !hWnd || hWnd == GetDesktopWindow() || hWnd == hwndActive )
862 return 0;
864 if( GetTaskQueue(0) != wndPtr->hmemTaskQ )
865 return 0;
867 if( wndTemp )
868 wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
869 else
870 dprintf_win(stddeb,"WINPOS_ActivateWindow: no current active window.\n");
872 /* call CBT hook chain */
873 if ((cbtStruct = SEGPTR_NEW(CBTACTIVATESTRUCT)))
875 LRESULT wRet;
876 cbtStruct->fMouse = fMouse;
877 cbtStruct->hWndActive = hwndActive;
878 wRet = HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd,
879 (LPARAM)SEGPTR_GET(cbtStruct) );
880 SEGPTR_FREE(cbtStruct);
881 if (wRet) return wRet;
884 /* set prev active wnd to current active wnd and send notification */
885 if ((hwndPrevActive = hwndActive) && IsWindow(hwndPrevActive))
887 if (!SendMessage16( hwndPrevActive, WM_NCACTIVATE, FALSE, 0 ))
889 if (GetSysModalWindow() != hWnd) return 0;
890 /* disregard refusal if hWnd is sysmodal */
893 SendMessage32A( hwndPrevActive, WM_ACTIVATE,
894 MAKEWPARAM( WA_INACTIVE, wIconized ),
895 (LPARAM)hWnd );
897 /* check if something happened during message processing */
898 if( hwndPrevActive != hwndActive ) return 0;
901 /* set active wnd */
902 hwndActive = hWnd;
904 /* send palette messages */
905 if( SendMessage16( hWnd, WM_QUERYNEWPALETTE, 0, 0L) )
906 SendMessage16((HWND16)-1, WM_PALETTEISCHANGING, (WPARAM)hWnd, 0L );
908 /* if prev wnd is minimized redraw icon title
909 if( hwndPrevActive )
911 wndTemp = WIN_FindWndPtr( WIN_GetTopParent( hwndPrevActive ) );
912 if(wndTemp)
913 if(wndTemp->dwStyle & WS_MINIMIZE)
914 RedrawIconTitle(hwndPrevActive);
918 /* managed windows will get ConfigureNotify event */
919 if (!(wndPtr->dwStyle & WS_CHILD) && !(wndPtr->flags & WIN_MANAGED))
921 /* check Z-order and bring hWnd to the top */
922 for (wndTemp = WIN_GetDesktop()->child; wndTemp; wndTemp = wndTemp->next)
923 if (wndTemp->dwStyle & WS_VISIBLE) break;
925 if( wndTemp != wndPtr )
926 SetWindowPos(hWnd, HWND_TOP, 0,0,0,0,
927 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
930 if( !IsWindow(hWnd) ) return 0;
932 if (hwndPrevActive)
934 wndTemp = WIN_FindWndPtr( hwndPrevActive );
935 if (wndTemp) hActiveQ = wndTemp->hmemTaskQ;
938 /* send WM_ACTIVATEAPP if necessary */
939 if (hActiveQ != wndPtr->hmemTaskQ)
941 HTASK hT = QUEUE_GetQueueTask( hActiveQ );
943 actStruct.wFlag = 0; /* deactivate */
944 actStruct.hWindowTask = QUEUE_GetQueueTask(wndPtr->hmemTaskQ);
945 actStruct.hTaskSendTo = hT;
947 /* send WM_ACTIVATEAPP to top-level windows
948 * that belong to the actStruct.hTaskSendTo task
950 EnumWindows( enumCallback , (LPARAM)&actStruct );
952 actStruct.wFlag = 1; /* activate */
953 actStruct.hWindowTask = hT;
954 actStruct.hTaskSendTo = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
956 EnumWindows( enumCallback , (LPARAM)&actStruct );
958 if( !IsWindow(hWnd) ) return 0;
961 /* walk up to the first unowned window */
962 wndTemp = wndPtr;
963 while (wndTemp->owner) wndTemp = wndTemp->owner;
964 /* and set last active owned popup */
965 wndTemp->hwndLastActive = hWnd;
967 wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
968 SendMessage16( hWnd, WM_NCACTIVATE, TRUE, 0 );
969 #ifdef WINELIB32
970 SendMessage32A( hWnd, WM_ACTIVATE,
971 MAKEWPARAM( (fMouse)?WA_CLICKACTIVE:WA_ACTIVE, wIconized),
972 (LPARAM)hwndPrevActive );
973 #else
974 SendMessage16( hWnd, WM_ACTIVATE, (fMouse)? WA_CLICKACTIVE : WA_ACTIVE,
975 MAKELONG(hwndPrevActive,wIconized));
976 #endif
978 if( !IsWindow(hWnd) ) return 0;
980 /* change focus if possible */
981 if( fChangeFocus && GetFocus() )
982 if( WIN_GetTopParent(GetFocus()) != hwndActive )
983 FOCUS_SwitchFocus( GetFocus(),
984 (wndPtr->dwStyle & WS_MINIMIZE)? 0: hwndActive);
986 /* if active wnd is minimized redraw icon title
987 if( hwndActive )
989 wndPtr = WIN_FindWndPtr(hwndActive);
990 if(wndPtr->dwStyle & WS_MINIMIZE)
991 RedrawIconTitle(hwndActive);
994 return (hWnd == hwndActive);
998 /*******************************************************************
999 * WINPOS_ChangeActiveWindow
1002 BOOL WINPOS_ChangeActiveWindow( HWND hWnd, BOOL mouseMsg )
1004 WND *wndPtr = WIN_FindWndPtr(hWnd);
1006 if( !wndPtr ) return FALSE;
1008 /* child windows get WM_CHILDACTIVATE message */
1009 if( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
1010 return SendMessage16(hWnd, WM_CHILDACTIVATE, 0, 0L);
1012 /* owned popups imply owner activation - not sure */
1013 if ((wndPtr->dwStyle & WS_POPUP) && wndPtr->owner &&
1014 !(wndPtr->owner->dwStyle & WS_DISABLED ))
1016 if (!(wndPtr = wndPtr->owner)) return FALSE;
1017 hWnd = wndPtr->hwndSelf;
1020 if( hWnd == hwndActive ) return FALSE;
1022 if( !WINPOS_SetActiveWindow(hWnd ,mouseMsg ,TRUE) )
1023 return FALSE;
1025 /* switch desktop queue to current active */
1026 if( wndPtr->parent == WIN_GetDesktop())
1027 WIN_GetDesktop()->hmemTaskQ = wndPtr->hmemTaskQ;
1029 return TRUE;
1033 /***********************************************************************
1034 * WINPOS_SendNCCalcSize
1036 * Send a WM_NCCALCSIZE message to a window.
1037 * All parameters are read-only except newClientRect.
1038 * oldWindowRect, oldClientRect and winpos must be non-NULL only
1039 * when calcValidRect is TRUE.
1041 LONG WINPOS_SendNCCalcSize( HWND hwnd, BOOL calcValidRect,
1042 RECT16 *newWindowRect, RECT16 *oldWindowRect,
1043 RECT16 *oldClientRect, SEGPTR winpos,
1044 RECT16 *newClientRect )
1046 NCCALCSIZE_PARAMS16 *params;
1047 LONG result;
1049 if (!(params = SEGPTR_NEW(NCCALCSIZE_PARAMS16))) return 0;
1050 params->rgrc[0] = *newWindowRect;
1051 if (calcValidRect)
1053 params->rgrc[1] = *oldWindowRect;
1054 params->rgrc[2] = *oldClientRect;
1055 params->lppos = winpos;
1057 result = SendMessage16( hwnd, WM_NCCALCSIZE, calcValidRect,
1058 (LPARAM)SEGPTR_GET( params ) );
1059 dprintf_win(stddeb, "WINPOS_SendNCCalcSize: %d %d %d %d\n",
1060 (int)params->rgrc[0].top, (int)params->rgrc[0].left,
1061 (int)params->rgrc[0].bottom, (int)params->rgrc[0].right);
1062 *newClientRect = params->rgrc[0];
1063 SEGPTR_FREE(params);
1064 return result;
1068 /***********************************************************************
1069 * WINPOS_HandleWindowPosChanging16
1071 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1073 LONG WINPOS_HandleWindowPosChanging16( WND *wndPtr, WINDOWPOS16 *winpos )
1075 POINT16 maxSize;
1076 if (winpos->flags & SWP_NOSIZE) return 0;
1077 if ((wndPtr->dwStyle & WS_THICKFRAME) ||
1078 ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
1080 NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
1081 winpos->cx = MIN( winpos->cx, maxSize.x );
1082 winpos->cy = MIN( winpos->cy, maxSize.y );
1084 return 0;
1088 /***********************************************************************
1089 * WINPOS_HandleWindowPosChanging32
1091 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1093 LONG WINPOS_HandleWindowPosChanging32( WND *wndPtr, WINDOWPOS32 *winpos )
1095 POINT16 maxSize;
1096 if (winpos->flags & SWP_NOSIZE) return 0;
1097 if ((wndPtr->dwStyle & WS_THICKFRAME) ||
1098 ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
1100 NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
1101 winpos->cx = MIN( winpos->cx, maxSize.x );
1102 winpos->cy = MIN( winpos->cy, maxSize.y );
1104 return 0;
1108 /***********************************************************************
1109 * WINPOS_MoveWindowZOrder
1111 * Move a window in Z order, invalidating everything that needs it.
1112 * Only necessary for windows without associated X window.
1114 static void WINPOS_MoveWindowZOrder( HWND hwnd, HWND hwndAfter )
1116 BOOL movingUp;
1117 WND *pWndAfter, *pWndCur, *wndPtr = WIN_FindWndPtr( hwnd );
1119 /* We have two possible cases:
1120 * - The window is moving up: we have to invalidate all areas
1121 * of the window that were covered by other windows
1122 * - The window is moving down: we have to invalidate areas
1123 * of other windows covered by this one.
1126 if (hwndAfter == HWND_TOP)
1128 movingUp = TRUE;
1130 else if (hwndAfter == HWND_BOTTOM)
1132 if (!wndPtr->next) return; /* Already at the bottom */
1133 movingUp = FALSE;
1135 else
1137 if (!(pWndAfter = WIN_FindWndPtr( hwndAfter ))) return;
1138 if (wndPtr->next == pWndAfter) return; /* Already placed right */
1140 /* Determine which window we encounter first in Z-order */
1141 pWndCur = wndPtr->parent->child;
1142 while ((pWndCur != wndPtr) && (pWndCur != pWndAfter))
1143 pWndCur = pWndCur->next;
1144 movingUp = (pWndCur == pWndAfter);
1147 if (movingUp)
1149 WND *pWndPrevAfter = wndPtr->next;
1150 WIN_UnlinkWindow( hwnd );
1151 WIN_LinkWindow( hwnd, hwndAfter );
1152 pWndCur = wndPtr->next;
1153 while (pWndCur != pWndPrevAfter)
1155 RECT16 rect = pWndCur->rectWindow;
1156 OffsetRect16( &rect, -wndPtr->rectClient.left,
1157 -wndPtr->rectClient.top );
1158 RedrawWindow16( hwnd, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
1159 RDW_FRAME | RDW_ERASE );
1160 pWndCur = pWndCur->next;
1163 else /* Moving down */
1165 pWndCur = wndPtr->next;
1166 WIN_UnlinkWindow( hwnd );
1167 WIN_LinkWindow( hwnd, hwndAfter );
1168 while (pWndCur != wndPtr)
1170 RECT16 rect = wndPtr->rectWindow;
1171 OffsetRect16( &rect, -pWndCur->rectClient.left,
1172 -pWndCur->rectClient.top );
1173 RedrawWindow16( pWndCur->hwndSelf, &rect, 0, RDW_INVALIDATE |
1174 RDW_ALLCHILDREN | RDW_FRAME | RDW_ERASE );
1175 pWndCur = pWndCur->next;
1180 /***********************************************************************
1181 * WINPOS_ReorderOwnedPopups
1183 * fix Z order taking into account owned popups -
1184 * basically we need to maintain them above owner window
1186 HWND WINPOS_ReorderOwnedPopups(HWND hwndInsertAfter, WND* wndPtr, WORD flags)
1188 WND* w = WIN_GetDesktop();
1190 w = w->child;
1192 /* if we are dealing with owned popup...
1194 if( wndPtr->dwStyle & WS_POPUP && wndPtr->owner && hwndInsertAfter != HWND_TOP )
1196 BOOL bFound = FALSE;
1197 HWND hwndLocalPrev = HWND_TOP;
1198 HWND hwndNewAfter = 0;
1200 while( w )
1202 if( !bFound && hwndInsertAfter == hwndLocalPrev )
1203 hwndInsertAfter = HWND_TOP;
1205 if( w->dwStyle & WS_POPUP && w->owner == wndPtr->owner )
1207 bFound = TRUE;
1209 if( hwndInsertAfter == HWND_TOP )
1211 hwndInsertAfter = hwndLocalPrev;
1212 break;
1214 hwndNewAfter = hwndLocalPrev;
1217 if( w == wndPtr->owner )
1219 /* basically HWND_BOTTOM */
1220 hwndInsertAfter = hwndLocalPrev;
1222 if( bFound )
1223 hwndInsertAfter = hwndNewAfter;
1224 break;
1227 if( w != wndPtr )
1228 hwndLocalPrev = w->hwndSelf;
1230 w = w->next;
1233 else
1234 /* or overlapped top-level window...
1236 if( !(wndPtr->dwStyle & WS_CHILD) )
1237 while( w )
1239 if( w == wndPtr ) break;
1241 if( w->dwStyle & WS_POPUP && w->owner == wndPtr )
1243 SetWindowPos(w->hwndSelf, hwndInsertAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1244 SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE);
1245 hwndInsertAfter = w->hwndSelf;
1247 w = w->next;
1250 return hwndInsertAfter;
1253 /***********************************************************************
1254 * WINPOS_SizeMoveClean
1256 * Make window look nice without excessive repainting
1258 * the pain:
1260 * visible regions are in window coordinates
1261 * update regions are in window client coordinates
1262 * client and window rectangles are in parent client coordinates
1264 static void WINPOS_SizeMoveClean(WND* Wnd, HRGN oldVisRgn, LPRECT16 lpOldWndRect, LPRECT16 lpOldClientRect, BOOL bNoCopy )
1266 HRGN newVisRgn = DCE_GetVisRgn(Wnd->hwndSelf, DCX_WINDOW | DCX_CLIPSIBLINGS );
1267 HRGN dirtyRgn = CreateRectRgn(0,0,0,0);
1268 int other, my;
1270 dprintf_win(stddeb,"cleaning up...new wnd=(%i %i-%i %i) old wnd=(%i %i-%i %i)\n\
1271 \t\tnew client=(%i %i-%i %i) old client=(%i %i-%i %i)\n",
1272 Wnd->rectWindow.left, Wnd->rectWindow.top, Wnd->rectWindow.right, Wnd->rectWindow.bottom,
1273 lpOldWndRect->left, lpOldWndRect->top, lpOldWndRect->right, lpOldWndRect->bottom,
1274 Wnd->rectClient.left,Wnd->rectClient.top,Wnd->rectClient.right,Wnd->rectClient.bottom,
1275 lpOldClientRect->left,lpOldClientRect->top,lpOldClientRect->right,lpOldClientRect->bottom);
1277 CombineRgn( dirtyRgn, newVisRgn, 0, RGN_COPY);
1279 if( !bNoCopy )
1281 HRGN hRgn = CreateRectRgn( lpOldClientRect->left - lpOldWndRect->left, lpOldClientRect->top - lpOldWndRect->top,
1282 lpOldClientRect->right - lpOldWndRect->left, lpOldClientRect->bottom - lpOldWndRect->top);
1283 CombineRgn( newVisRgn, newVisRgn, oldVisRgn, RGN_AND );
1284 CombineRgn( newVisRgn, newVisRgn, hRgn, RGN_AND );
1285 DeleteObject(hRgn);
1288 /* map regions to the parent client area */
1290 OffsetRgn(dirtyRgn, Wnd->rectWindow.left, Wnd->rectWindow.top);
1291 OffsetRgn(oldVisRgn, lpOldWndRect->left, lpOldWndRect->top);
1293 /* compute invalidated region outside Wnd - (in client coordinates of the parent window) */
1295 other = CombineRgn(dirtyRgn, oldVisRgn, dirtyRgn, RGN_DIFF);
1297 /* map visible region to the Wnd client area */
1299 OffsetRgn( newVisRgn, Wnd->rectWindow.left - Wnd->rectClient.left,
1300 Wnd->rectWindow.top - Wnd->rectClient.top );
1302 /* substract previously invalidated region from the Wnd visible region */
1304 my = (Wnd->hrgnUpdate > 1)? CombineRgn( newVisRgn, newVisRgn, Wnd->hrgnUpdate, RGN_DIFF)
1305 : COMPLEXREGION;
1307 if( bNoCopy ) /* invalidate Wnd visible region */
1309 if (my != NULLREGION) RedrawWindow32( Wnd->hwndSelf, NULL, newVisRgn, RDW_INVALIDATE |
1310 RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1312 else /* bitblt old client area */
1314 HDC hDC;
1315 int update;
1316 HRGN updateRgn;
1318 /* client rect */
1320 updateRgn = CreateRectRgn( 0,0, Wnd->rectClient.right - Wnd->rectClient.left,
1321 Wnd->rectClient.bottom - Wnd->rectClient.top );
1323 /* clip visible region with client rect */
1325 my = CombineRgn( newVisRgn, newVisRgn, updateRgn, RGN_AND );
1327 /* substract result from client rect to get region that won't be copied */
1329 update = CombineRgn( updateRgn, updateRgn, newVisRgn, RGN_DIFF );
1331 /* Blt valid bits using parent window DC */
1333 if( my != NULLREGION )
1335 int xfrom = lpOldClientRect->left;
1336 int yfrom = lpOldClientRect->top;
1337 int xto = Wnd->rectClient.left;
1338 int yto = Wnd->rectClient.top;
1340 /* check if we can skip copying */
1342 if( xfrom != xto || yfrom != yto )
1344 /* compute clipping region in parent client coordinates */
1346 OffsetRgn( newVisRgn, Wnd->rectClient.left, Wnd->rectClient.top);
1347 CombineRgn( oldVisRgn, oldVisRgn, newVisRgn, RGN_OR );
1349 hDC = GetDCEx( Wnd->parent->hwndSelf, oldVisRgn, DCX_INTERSECTRGN | DCX_CACHE | DCX_CLIPSIBLINGS);
1351 BitBlt(hDC, xto, yto, lpOldClientRect->right - lpOldClientRect->left + 1,
1352 lpOldClientRect->bottom - lpOldClientRect->top + 1,
1353 hDC, xfrom, yfrom, SRCCOPY );
1355 ReleaseDC( Wnd->parent->hwndSelf, hDC);
1359 if( update != NULLREGION )
1360 RedrawWindow32( Wnd->hwndSelf, NULL, updateRgn, RDW_INVALIDATE |
1361 RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1362 DeleteObject( updateRgn );
1365 /* erase uncovered areas */
1367 if( other != NULLREGION )
1368 RedrawWindow32( Wnd->parent->hwndSelf, NULL, dirtyRgn,
1369 RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1371 DeleteObject(dirtyRgn);
1372 DeleteObject(newVisRgn);
1375 /***********************************************************************
1376 * WINPOS_SetXWindowPos
1378 * SetWindowPos() for an X window. Used by the real SetWindowPos().
1380 static void WINPOS_SetXWindowPos( WINDOWPOS16 *winpos )
1382 XWindowChanges winChanges;
1383 int changeMask = 0;
1384 WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
1386 if (!(winpos->flags & SWP_NOSIZE))
1388 winChanges.width = winpos->cx;
1389 winChanges.height = winpos->cy;
1390 changeMask |= CWWidth | CWHeight;
1392 if (!(winpos->flags & SWP_NOMOVE))
1394 winChanges.x = winpos->x;
1395 winChanges.y = winpos->y;
1396 changeMask |= CWX | CWY;
1398 if (!(winpos->flags & SWP_NOZORDER))
1400 if (winpos->hwndInsertAfter == HWND_TOP) winChanges.stack_mode = Above;
1401 else winChanges.stack_mode = Below;
1402 if ((winpos->hwndInsertAfter != HWND_TOP) &&
1403 (winpos->hwndInsertAfter != HWND_BOTTOM))
1405 WND * insertPtr = WIN_FindWndPtr( winpos->hwndInsertAfter );
1406 winChanges.sibling = insertPtr->window;
1407 changeMask |= CWSibling;
1409 changeMask |= CWStackMode;
1411 if (changeMask)
1412 XConfigureWindow( display, wndPtr->window, changeMask, &winChanges );
1416 /***********************************************************************
1417 * SetWindowPos (USER.232)
1419 BOOL SetWindowPos( HWND hwnd, HWND hwndInsertAfter, INT x, INT y,
1420 INT cx, INT cy, WORD flags )
1422 WINDOWPOS16 winpos;
1423 WND * wndPtr;
1424 RECT16 newWindowRect, newClientRect;
1425 HRGN visRgn = 0;
1426 int result = 0;
1428 dprintf_win(stddeb,"SetWindowPos: hwnd %04x, flags %08x\n", hwnd, flags);
1430 /* Check window handle */
1432 if (hwnd == GetDesktopWindow()) return FALSE;
1433 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
1435 /* Check for windows that may not be resized
1436 FIXME: this should be done only for Windows 3.0 programs */
1437 if (flags ==(SWP_SHOWWINDOW) || flags ==(SWP_HIDEWINDOW ) )
1438 flags |= SWP_NOSIZE | SWP_NOMOVE;
1440 /* Check dimensions */
1442 if (cx <= 0) cx = 1;
1443 if (cy <= 0) cy = 1;
1445 /* Check flags */
1447 if (hwnd == hwndActive) flags |= SWP_NOACTIVATE; /* Already active */
1448 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == cx) &&
1449 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == cy))
1450 flags |= SWP_NOSIZE; /* Already the right size */
1451 if ((wndPtr->rectWindow.left == x) && (wndPtr->rectWindow.top == y))
1452 flags |= SWP_NOMOVE; /* Already the right position */
1454 /* Check hwndInsertAfter */
1456 if (!(flags & (SWP_NOZORDER | SWP_NOACTIVATE)))
1458 /* Ignore TOPMOST flags when activating a window */
1459 /* _and_ moving it in Z order. */
1460 if ((hwndInsertAfter == HWND_TOPMOST) ||
1461 (hwndInsertAfter == HWND_NOTOPMOST))
1462 hwndInsertAfter = HWND_TOP;
1464 /* TOPMOST not supported yet */
1465 if ((hwndInsertAfter == HWND_TOPMOST) ||
1466 (hwndInsertAfter == HWND_NOTOPMOST)) hwndInsertAfter = HWND_TOP;
1468 /* hwndInsertAfter must be a sibling of the window */
1469 if ((hwndInsertAfter != HWND_TOP) && (hwndInsertAfter != HWND_BOTTOM))
1471 WND* wnd = WIN_FindWndPtr(hwndInsertAfter);
1472 if( wnd->parent != wndPtr->parent ) return FALSE;
1473 if( wnd->next == wndPtr ) flags |= SWP_NOZORDER;
1475 else
1476 if (hwndInsertAfter == HWND_TOP)
1477 flags |= ( wndPtr->parent->child == wndPtr)? SWP_NOZORDER: 0;
1478 else /* HWND_BOTTOM */
1479 flags |= ( wndPtr->next )? 0: SWP_NOZORDER;
1481 /* Fill the WINDOWPOS structure */
1483 winpos.hwnd = hwnd;
1484 winpos.hwndInsertAfter = hwndInsertAfter;
1485 winpos.x = x;
1486 winpos.y = y;
1487 winpos.cx = cx;
1488 winpos.cy = cy;
1489 winpos.flags = flags;
1491 /* Send WM_WINDOWPOSCHANGING message */
1493 if (!(flags & SWP_NOSENDCHANGING))
1494 SendMessage16( hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1496 /* Calculate new position and size */
1498 newWindowRect = wndPtr->rectWindow;
1499 newClientRect = wndPtr->rectClient;
1501 if (!(winpos.flags & SWP_NOSIZE))
1503 newWindowRect.right = newWindowRect.left + winpos.cx;
1504 newWindowRect.bottom = newWindowRect.top + winpos.cy;
1506 if (!(winpos.flags & SWP_NOMOVE))
1508 newWindowRect.left = winpos.x;
1509 newWindowRect.top = winpos.y;
1510 newWindowRect.right += winpos.x - wndPtr->rectWindow.left;
1511 newWindowRect.bottom += winpos.y - wndPtr->rectWindow.top;
1513 OffsetRect16( &newClientRect, winpos.x - wndPtr->rectWindow.left,
1514 winpos.y - wndPtr->rectWindow.top );
1517 winpos.flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1519 /* Reposition window in Z order */
1521 if (!(winpos.flags & SWP_NOZORDER))
1523 /* reorder owned popups if hwnd is top-level window
1525 if( wndPtr->parent == WIN_GetDesktop() )
1526 hwndInsertAfter = WINPOS_ReorderOwnedPopups( hwndInsertAfter,
1527 wndPtr, flags );
1529 if (wndPtr->window)
1531 WIN_UnlinkWindow( winpos.hwnd );
1532 WIN_LinkWindow( winpos.hwnd, hwndInsertAfter );
1534 else WINPOS_MoveWindowZOrder( winpos.hwnd, hwndInsertAfter );
1537 if ( !wndPtr->window && !(flags & SWP_NOREDRAW) &&
1538 (!(flags & SWP_NOMOVE) || !(flags & SWP_NOSIZE) || (flags & SWP_FRAMECHANGED)) )
1539 visRgn = DCE_GetVisRgn(hwnd, DCX_WINDOW | DCX_CLIPSIBLINGS);
1542 /* Send WM_NCCALCSIZE message to get new client area */
1543 if( (flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1545 result = WINPOS_SendNCCalcSize( winpos.hwnd, TRUE, &newWindowRect,
1546 &wndPtr->rectWindow, &wndPtr->rectClient,
1547 MAKE_SEGPTR(&winpos), &newClientRect );
1549 /* FIXME: WVR_ALIGNxxx */
1551 if( newClientRect.left != wndPtr->rectClient.left ||
1552 newClientRect.top != wndPtr->rectClient.top )
1553 winpos.flags &= ~SWP_NOCLIENTMOVE;
1555 if( (newClientRect.right - newClientRect.left !=
1556 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
1557 (newClientRect.bottom - newClientRect.top !=
1558 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
1559 winpos.flags &= ~SWP_NOCLIENTSIZE;
1561 else
1562 if( !(flags & SWP_NOMOVE) && (newClientRect.left != wndPtr->rectClient.left ||
1563 newClientRect.top != wndPtr->rectClient.top) )
1564 winpos.flags &= ~SWP_NOCLIENTMOVE;
1566 /* Perform the moving and resizing */
1568 if (wndPtr->window)
1570 RECT16 oldWindowRect = wndPtr->rectWindow;
1571 RECT16 oldClientRect = wndPtr->rectClient;
1573 HWND bogusInsertAfter = winpos.hwndInsertAfter;
1575 winpos.hwndInsertAfter = hwndInsertAfter;
1576 WINPOS_SetXWindowPos( &winpos );
1578 wndPtr->rectWindow = newWindowRect;
1579 wndPtr->rectClient = newClientRect;
1580 winpos.hwndInsertAfter = bogusInsertAfter;
1582 /* FIXME: should do something like WINPOS_SizeMoveClean */
1584 if( (oldClientRect.left - oldWindowRect.left !=
1585 newClientRect.left - newWindowRect.left) ||
1586 (oldClientRect.top - oldWindowRect.top !=
1587 newClientRect.top - newWindowRect.top) )
1589 RedrawWindow32( wndPtr->hwndSelf, NULL, 0,
1590 RDW_ALLCHILDREN | RDW_FRAME | RDW_ERASE );
1591 else
1592 if( winpos.flags & SWP_FRAMECHANGED )
1594 WORD wErase = 0;
1595 RECT32 rect;
1597 if( oldClientRect.right > newClientRect.right )
1599 rect.left = newClientRect.right; rect.top = newClientRect.top;
1600 rect.right = oldClientRect.right; rect.bottom = newClientRect.bottom;
1601 wErase = 1;
1602 RedrawWindow32( wndPtr->hwndSelf, &rect, 0,
1603 RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN );
1605 if( oldClientRect.bottom > newClientRect.bottom )
1607 rect.left = newClientRect.left; rect.top = newClientRect.bottom;
1608 rect.right = (wErase)?oldClientRect.right:newClientRect.right;
1609 rect.bottom = oldClientRect.bottom;
1610 wErase = 1;
1611 RedrawWindow32( wndPtr->hwndSelf, &rect, 0,
1612 RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN );
1614 if( !wErase ) wndPtr->flags |= WIN_NEEDS_NCPAINT;
1617 else
1619 RECT16 oldWindowRect = wndPtr->rectWindow;
1620 RECT16 oldClientRect = wndPtr->rectClient;
1622 wndPtr->rectWindow = newWindowRect;
1623 wndPtr->rectClient = newClientRect;
1625 if( !(flags & SWP_NOREDRAW) )
1627 BOOL bNoCopy = (flags & SWP_NOCOPYBITS) ||
1628 (result >= WVR_HREDRAW && result < WVR_VALIDRECTS);
1630 if( (winpos.flags & SWP_NOPOSCHANGE) != SWP_NOPOSCHANGE )
1632 /* optimize cleanup by BitBlt'ing where possible */
1634 WINPOS_SizeMoveClean(wndPtr, visRgn, &oldWindowRect, &oldClientRect, bNoCopy);
1635 DeleteObject(visRgn);
1637 else
1638 if( winpos.flags & SWP_FRAMECHANGED )
1639 RedrawWindow32( winpos.hwnd, NULL, 0, RDW_NOCHILDREN | RDW_FRAME );
1642 DeleteObject(visRgn);
1645 if (flags & SWP_SHOWWINDOW)
1647 wndPtr->dwStyle |= WS_VISIBLE;
1648 if (wndPtr->window)
1650 XMapWindow( display, wndPtr->window );
1652 else
1654 if (!(flags & SWP_NOREDRAW))
1655 RedrawWindow32( winpos.hwnd, NULL, 0,
1656 RDW_INVALIDATE | RDW_ALLCHILDREN |
1657 RDW_FRAME | RDW_ERASE );
1660 else if (flags & SWP_HIDEWINDOW)
1662 wndPtr->dwStyle &= ~WS_VISIBLE;
1663 if (wndPtr->window)
1665 XUnmapWindow( display, wndPtr->window );
1667 else
1669 if (!(flags & SWP_NOREDRAW))
1670 RedrawWindow16( wndPtr->parent->hwndSelf, &wndPtr->rectWindow, 0,
1671 RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1674 if ((winpos.hwnd == GetFocus()) || IsChild(winpos.hwnd, GetFocus()))
1675 SetFocus( GetParent(winpos.hwnd) ); /* Revert focus to parent */
1677 if (winpos.hwnd == hwndActive)
1679 /* Activate previously active window if possible */
1680 HWND newActive = hwndPrevActive;
1681 if (!IsWindow(newActive) || (newActive == winpos.hwnd))
1683 newActive = GetTopWindow( GetDesktopWindow() );
1684 if (newActive == winpos.hwnd)
1685 newActive = wndPtr->next ? wndPtr->next->hwndSelf : 0;
1687 WINPOS_ChangeActiveWindow( newActive, FALSE );
1691 /* Activate the window */
1693 if (!(flags & SWP_NOACTIVATE))
1694 WINPOS_ChangeActiveWindow( winpos.hwnd, FALSE );
1696 /* Repaint the window */
1698 if (wndPtr->window) EVENT_Synchronize(); /* Wait for all expose events */
1700 EVENT_DummyMotionNotify(); /* Simulate a mouse event to set the cursor */
1702 if (!(flags & SWP_DEFERERASE))
1703 RedrawWindow32( wndPtr->parent->hwndSelf, NULL, 0,
1704 RDW_ALLCHILDREN | RDW_ERASENOW );
1706 /* And last, send the WM_WINDOWPOSCHANGED message */
1708 if (!(winpos.flags & SWP_NOSENDCHANGING))
1709 SendMessage16( winpos.hwnd, WM_WINDOWPOSCHANGED,
1710 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1712 return TRUE;
1716 /***********************************************************************
1717 * BeginDeferWindowPos (USER.259)
1719 HDWP16 BeginDeferWindowPos( INT count )
1721 HDWP16 handle;
1722 DWP *pDWP;
1724 if (count <= 0) return 0;
1725 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS16) );
1726 if (!handle) return 0;
1727 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1728 pDWP->actualCount = 0;
1729 pDWP->suggestedCount = count;
1730 pDWP->valid = TRUE;
1731 pDWP->wMagic = DWP_MAGIC;
1732 pDWP->hwndParent = 0;
1733 return handle;
1737 /***********************************************************************
1738 * DeferWindowPos (USER.260)
1740 HDWP16 DeferWindowPos( HDWP16 hdwp, HWND hwnd, HWND hwndAfter, INT x, INT y,
1741 INT cx, INT cy, UINT flags )
1743 DWP *pDWP;
1744 int i;
1745 HDWP16 newhdwp = hdwp;
1746 HWND parent;
1748 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1749 if (!pDWP) return 0;
1750 if (hwnd == GetDesktopWindow()) return 0;
1752 /* All the windows of a DeferWindowPos() must have the same parent */
1754 parent = WIN_FindWndPtr( hwnd )->parent->hwndSelf;
1755 if (pDWP->actualCount == 0) pDWP->hwndParent = parent;
1756 else if (parent != pDWP->hwndParent)
1758 USER_HEAP_FREE( hdwp );
1759 return 0;
1762 for (i = 0; i < pDWP->actualCount; i++)
1764 if (pDWP->winPos[i].hwnd == hwnd)
1766 /* Merge with the other changes */
1767 if (!(flags & SWP_NOZORDER))
1769 pDWP->winPos[i].hwndInsertAfter = hwndAfter;
1771 if (!(flags & SWP_NOMOVE))
1773 pDWP->winPos[i].x = x;
1774 pDWP->winPos[i].y = y;
1776 if (!(flags & SWP_NOSIZE))
1778 pDWP->winPos[i].cx = cx;
1779 pDWP->winPos[i].cy = cy;
1781 pDWP->winPos[i].flags &= flags & (SWP_NOSIZE | SWP_NOMOVE |
1782 SWP_NOZORDER | SWP_NOREDRAW |
1783 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1784 SWP_NOOWNERZORDER);
1785 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1786 SWP_FRAMECHANGED);
1787 return hdwp;
1790 if (pDWP->actualCount >= pDWP->suggestedCount)
1792 newhdwp = USER_HEAP_REALLOC( hdwp,
1793 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS16) );
1794 if (!newhdwp) return 0;
1795 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1796 pDWP->suggestedCount++;
1798 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1799 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1800 pDWP->winPos[pDWP->actualCount].x = x;
1801 pDWP->winPos[pDWP->actualCount].y = y;
1802 pDWP->winPos[pDWP->actualCount].cx = cx;
1803 pDWP->winPos[pDWP->actualCount].cy = cy;
1804 pDWP->winPos[pDWP->actualCount].flags = flags;
1805 pDWP->actualCount++;
1806 return newhdwp;
1810 /***********************************************************************
1811 * EndDeferWindowPos (USER.261)
1813 BOOL EndDeferWindowPos( HDWP16 hdwp )
1815 DWP *pDWP;
1816 WINDOWPOS16 *winpos;
1817 BOOL res = TRUE;
1818 int i;
1820 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1821 if (!pDWP) return FALSE;
1822 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1824 if (!(res = SetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1825 winpos->x, winpos->y, winpos->cx, winpos->cy,
1826 winpos->flags ))) break;
1828 USER_HEAP_FREE( hdwp );
1829 return res;
1833 /***********************************************************************
1834 * TileChildWindows (USER.199)
1836 void TileChildWindows( HWND parent, WORD action )
1838 printf("STUB TileChildWindows(%04x, %d)\n", parent, action);
1841 /***********************************************************************
1842 * CascageChildWindows (USER.198)
1844 void CascadeChildWindows( HWND parent, WORD action )
1846 printf("STUB CascadeChildWindows(%04x, %d)\n", parent, action);