Release 951226
[wine/gsoc-2012-control.git] / windows / winpos.c
blob659cd2f963f786b1adc74fd7aee39593614be8fb
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995 Alex Korobka
6 */
8 #include "sysmetrics.h"
9 #include "selectors.h"
10 #include "user.h"
11 #include "win.h"
12 #include "event.h"
13 #include "hook.h"
14 #include "message.h"
15 #include "stackframe.h"
16 #include "winpos.h"
17 #include "nonclient.h"
18 #include "stddebug.h"
19 /* #define DEBUG_WIN */
20 #include "debug.h"
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 */
31 extern HANDLE hActiveQ_G; /* from message.c */
34 /***********************************************************************
35 * WINPOS_FindIconPos
37 * Find a suitable place for an iconic window.
38 * The new position is stored into wndPtr->ptIconPos.
40 void WINPOS_FindIconPos( HWND hwnd )
42 RECT rectParent;
43 short x, y, xspacing, yspacing;
44 WND * wndPtr = WIN_FindWndPtr( hwnd );
46 if (!wndPtr) return;
47 GetClientRect( wndPtr->hwndParent, &rectParent );
48 if ((wndPtr->ptIconPos.x >= rectParent.left) &&
49 (wndPtr->ptIconPos.x + SYSMETRICS_CXICON < rectParent.right) &&
50 (wndPtr->ptIconPos.y >= rectParent.top) &&
51 (wndPtr->ptIconPos.y + SYSMETRICS_CYICON < rectParent.bottom))
52 return; /* The icon already has a suitable position */
54 xspacing = yspacing = 70; /* FIXME: This should come from WIN.INI */
55 y = rectParent.bottom;
56 for (;;)
58 for (x = rectParent.left; x<=rectParent.right-xspacing; x += xspacing)
60 /* Check if another icon already occupies this spot */
61 HWND hwndChild = GetWindow( wndPtr->hwndParent, GW_CHILD );
62 while (hwndChild)
64 WND *childPtr = WIN_FindWndPtr( hwndChild );
65 if ((childPtr->dwStyle & WS_MINIMIZE) && (hwndChild != hwnd))
67 if ((childPtr->rectWindow.left < x + xspacing) &&
68 (childPtr->rectWindow.right >= x) &&
69 (childPtr->rectWindow.top <= y) &&
70 (childPtr->rectWindow.bottom > y - yspacing))
71 break; /* There's a window in there */
74 hwndChild = childPtr->hwndNext;
76 if (!hwndChild)
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 WORD ArrangeIconicWindows( HWND parent )
94 RECT rectParent;
95 HWND hwndChild;
96 short x, y, xspacing, yspacing;
98 GetClientRect( 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 * GetWindowRect (USER.32)
126 void GetWindowRect( HWND hwnd, LPRECT rect )
128 WND * wndPtr = WIN_FindWndPtr( hwnd );
129 if (!wndPtr) return;
131 *rect = wndPtr->rectWindow;
132 if (wndPtr->dwStyle & WS_CHILD)
133 MapWindowPoints( wndPtr->hwndParent, 0, (POINT *)rect, 2 );
137 /***********************************************************************
138 * GetClientRect (USER.33)
140 void GetClientRect( HWND hwnd, LPRECT rect )
142 WND * wndPtr = WIN_FindWndPtr( hwnd );
144 rect->left = rect->top = rect->right = rect->bottom = 0;
145 if (wndPtr)
147 rect->right = wndPtr->rectClient.right - wndPtr->rectClient.left;
148 rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
153 /*******************************************************************
154 * ClientToScreen (USER.28)
156 void ClientToScreen( HWND hwnd, LPPOINT lppnt )
158 MapWindowPoints( hwnd, 0, lppnt, 1 );
162 /*******************************************************************
163 * ScreenToClient (USER.29)
165 void ScreenToClient( HWND hwnd, LPPOINT lppnt )
167 MapWindowPoints( 0, hwnd, lppnt, 1 );
171 /*******************************************************************
172 * WindowFromPoint (USER.30)
174 HWND WindowFromPoint( POINT pt )
176 HWND hwndRet = 0;
177 HWND hwnd = GetDesktopWindow();
179 while(hwnd)
181 /* If point is in window, and window is visible, */
182 /* not disabled and not transparent, then explore */
183 /* its children. Otherwise, go to the next window. */
185 WND *wndPtr = WIN_FindWndPtr( hwnd );
186 if ((pt.x >= wndPtr->rectWindow.left) &&
187 (pt.x < wndPtr->rectWindow.right) &&
188 (pt.y >= wndPtr->rectWindow.top) &&
189 (pt.y < wndPtr->rectWindow.bottom) &&
190 !(wndPtr->dwStyle & WS_DISABLED) &&
191 (wndPtr->dwStyle & WS_VISIBLE) &&
192 !(wndPtr->dwExStyle & WS_EX_TRANSPARENT))
194 hwndRet = hwnd;
195 /* If window is minimized, ignore its children */
196 if (wndPtr->dwStyle & WS_MINIMIZE) break;
197 pt.x -= wndPtr->rectClient.left;
198 pt.y -= wndPtr->rectClient.top;
199 hwnd = wndPtr->hwndChild;
201 else hwnd = wndPtr->hwndNext;
203 return hwndRet;
207 /*******************************************************************
208 * ChildWindowFromPoint (USER.191)
210 HWND ChildWindowFromPoint( HWND hwndParent, POINT pt )
212 RECT rect;
213 HWND hwnd;
215 GetWindowRect( hwndParent, &rect );
216 if (!PtInRect( &rect, pt )) return 0;
217 hwnd = GetTopWindow( hwndParent );
218 while (hwnd)
220 GetWindowRect( hwnd, &rect );
221 if (PtInRect( &rect, pt )) return hwnd;
222 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
224 return hwndParent;
228 /*******************************************************************
229 * MapWindowPoints (USER.258)
231 void MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, WORD count )
233 WND * wndPtr;
234 POINT * curpt;
235 POINT origin = { 0, 0 };
236 WORD i;
238 /* Translate source window origin to screen coords */
239 while(hwndFrom)
241 if (!(wndPtr = WIN_FindWndPtr( hwndFrom )))
243 fprintf( stderr, "MapWindowPoints: bad hwndFrom = "NPFMT"\n",
244 hwndFrom);
245 return;
247 origin.x += wndPtr->rectClient.left;
248 origin.y += wndPtr->rectClient.top;
249 hwndFrom = (wndPtr->dwStyle & WS_CHILD) ? wndPtr->hwndParent : 0;
252 /* Translate origin to destination window coords */
253 while(hwndTo)
255 if (!(wndPtr = WIN_FindWndPtr( hwndTo )))
257 fprintf(stderr,"MapWindowPoints: bad hwndTo = "NPFMT"\n", hwndTo );
258 return;
260 origin.x -= wndPtr->rectClient.left;
261 origin.y -= wndPtr->rectClient.top;
262 hwndTo = (wndPtr->dwStyle & WS_CHILD) ? wndPtr->hwndParent : 0;
265 /* Translate points */
266 for (i = 0, curpt = lppt; i < count; i++, curpt++)
268 curpt->x += origin.x;
269 curpt->y += origin.y;
274 /***********************************************************************
275 * IsIconic (USER.31)
277 BOOL IsIconic(HWND hWnd)
279 WND * wndPtr = WIN_FindWndPtr(hWnd);
280 if (wndPtr == NULL) return FALSE;
281 return (wndPtr->dwStyle & WS_MINIMIZE) != 0;
285 /***********************************************************************
286 * IsZoomed (USER.272)
288 BOOL IsZoomed(HWND hWnd)
290 WND * wndPtr = WIN_FindWndPtr(hWnd);
291 if (wndPtr == NULL) return FALSE;
292 return (wndPtr->dwStyle & WS_MAXIMIZE) != 0;
296 /*******************************************************************
297 * GetActiveWindow (USER.60)
299 HWND GetActiveWindow()
301 return hwndActive;
305 /*******************************************************************
306 * SetActiveWindow (USER.59)
308 HWND SetActiveWindow( HWND hwnd )
310 HWND prev = hwndActive;
311 WND *wndPtr = WIN_FindWndPtr( hwnd );
313 if (!wndPtr || (wndPtr->dwStyle & WS_DISABLED) ||
314 !(wndPtr->dwStyle & WS_VISIBLE)) return 0;
316 WINPOS_SetActiveWindow( hwnd, 0, 0 );
317 return prev;
321 /***********************************************************************
322 * BringWindowToTop (USER.45)
324 BOOL BringWindowToTop( HWND hwnd )
326 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
330 /***********************************************************************
331 * MoveWindow (USER.56)
333 BOOL MoveWindow( HWND hwnd, short x, short y, short cx, short cy, BOOL repaint)
335 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
336 if (!repaint) flags |= SWP_NOREDRAW;
337 dprintf_win(stddeb, "MoveWindow: "NPFMT" %d,%d %dx%d %d\n",
338 hwnd, x, y, cx, cy, repaint );
339 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
343 /***********************************************************************
344 * ShowWindow (USER.42)
346 BOOL ShowWindow( HWND hwnd, int cmd )
348 WND * wndPtr = WIN_FindWndPtr( hwnd );
349 BOOL wasVisible;
350 POINT maxSize;
351 int swpflags = 0;
352 short x = 0, y = 0, cx = 0, cy = 0;
354 if (!wndPtr) return FALSE;
356 dprintf_win(stddeb,"ShowWindow: hwnd="NPFMT", cmd=%d\n", hwnd, cmd);
358 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
360 switch(cmd)
362 case SW_HIDE:
363 swpflags |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
364 SWP_NOACTIVATE | SWP_NOZORDER;
365 break;
367 case SW_SHOWMINNOACTIVE:
368 swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
369 /* fall through */
370 case SW_SHOWMINIMIZED:
371 swpflags |= SWP_SHOWWINDOW;
372 /* fall through */
373 case SW_MINIMIZE:
374 swpflags |= SWP_FRAMECHANGED;
375 if (!(wndPtr->dwStyle & WS_MINIMIZE))
377 if (wndPtr->dwStyle & WS_MAXIMIZE)
379 wndPtr->flags |= WIN_RESTORE_MAX;
380 wndPtr->dwStyle &= ~WS_MAXIMIZE;
382 else
384 wndPtr->flags &= ~WIN_RESTORE_MAX;
385 wndPtr->rectNormal = wndPtr->rectWindow;
387 wndPtr->dwStyle |= WS_MINIMIZE;
388 WINPOS_FindIconPos( hwnd );
389 x = wndPtr->ptIconPos.x;
390 y = wndPtr->ptIconPos.y;
391 cx = SYSMETRICS_CXICON;
392 cy = SYSMETRICS_CYICON;
394 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
395 break;
397 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE: */
398 swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
399 if (!(wndPtr->dwStyle & WS_MAXIMIZE))
401 /* Store the current position and find the maximized size */
402 if (!(wndPtr->dwStyle & WS_MINIMIZE))
403 wndPtr->rectNormal = wndPtr->rectWindow;
405 NC_GetMinMaxInfo( hwnd, &maxSize,
406 &wndPtr->ptMaxPos, NULL, NULL );
407 x = wndPtr->ptMaxPos.x;
408 y = wndPtr->ptMaxPos.y;
410 if( wndPtr->dwStyle & WS_MINIMIZE )
411 if( !SendMessage( hwnd, WM_QUERYOPEN, 0, 0L ) )
413 swpflags |= SWP_NOSIZE;
414 break;
417 cx = maxSize.x;
418 cy = maxSize.y;
419 wndPtr->dwStyle &= ~WS_MINIMIZE;
420 wndPtr->dwStyle |= WS_MAXIMIZE;
422 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
423 break;
425 case SW_SHOWNA:
426 swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
427 /* fall through */
428 case SW_SHOW:
429 swpflags |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
430 break;
432 case SW_SHOWNOACTIVATE:
433 swpflags |= SWP_NOZORDER;
434 if (GetActiveWindow()) swpflags |= SWP_NOACTIVATE;
435 /* fall through */
436 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
437 case SW_RESTORE:
438 swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
440 if (wndPtr->dwStyle & WS_MINIMIZE)
442 if( !SendMessage( hwnd, WM_QUERYOPEN, 0, 0L) )
444 swpflags |= SWP_NOSIZE;
445 break;
447 wndPtr->ptIconPos.x = wndPtr->rectWindow.left;
448 wndPtr->ptIconPos.y = wndPtr->rectWindow.top;
449 wndPtr->dwStyle &= ~WS_MINIMIZE;
450 if (wndPtr->flags & WIN_RESTORE_MAX)
452 /* Restore to maximized position */
453 NC_GetMinMaxInfo( hwnd, &maxSize, &wndPtr->ptMaxPos,
454 NULL, NULL );
455 x = wndPtr->ptMaxPos.x;
456 y = wndPtr->ptMaxPos.y;
457 cx = maxSize.x;
458 cy = maxSize.y;
459 wndPtr->dwStyle |= WS_MAXIMIZE;
461 else /* Restore to normal position */
463 x = wndPtr->rectNormal.left;
464 y = wndPtr->rectNormal.top;
465 cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
466 cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
469 else if (wndPtr->dwStyle & WS_MAXIMIZE)
471 wndPtr->ptMaxPos.x = wndPtr->rectWindow.left;
472 wndPtr->ptMaxPos.y = wndPtr->rectWindow.top;
473 wndPtr->dwStyle &= ~WS_MAXIMIZE;
474 x = wndPtr->rectNormal.left;
475 y = wndPtr->rectNormal.top;
476 cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
477 cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
479 else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
480 break;
483 SendMessage( hwnd, WM_SHOWWINDOW, (cmd != SW_HIDE), 0 );
484 SetWindowPos( hwnd, HWND_TOP, x, y, cx, cy, swpflags );
486 /* Send WM_SIZE and WM_MOVE messages if not already done */
487 if (!(wndPtr->flags & WIN_GOT_SIZEMSG))
489 int wParam = SIZE_RESTORED;
490 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
491 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
492 wndPtr->flags |= WIN_GOT_SIZEMSG;
493 SendMessage( hwnd, WM_SIZE, wParam,
494 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
495 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
496 SendMessage( hwnd, WM_MOVE, 0,
497 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
500 return wasVisible;
504 /***********************************************************************
505 * GetInternalWindowPos (USER.460)
507 WORD GetInternalWindowPos( HWND hwnd, LPRECT rectWnd, LPPOINT ptIcon )
509 WINDOWPLACEMENT wndpl;
510 if (!GetWindowPlacement( hwnd, &wndpl )) return 0;
511 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
512 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
513 return wndpl.showCmd;
517 /***********************************************************************
518 * SetInternalWindowPos (USER.461)
520 void SetInternalWindowPos( HWND hwnd, WORD showCmd, LPRECT rect, LPPOINT pt )
522 WINDOWPLACEMENT wndpl;
523 WND *wndPtr = WIN_FindWndPtr( hwnd );
525 wndpl.length = sizeof(wndpl);
526 wndpl.flags = (pt != NULL) ? WPF_SETMINPOSITION : 0;
527 wndpl.showCmd = showCmd;
528 if (pt) wndpl.ptMinPosition = *pt;
529 wndpl.rcNormalPosition = (rect != NULL) ? *rect : wndPtr->rectNormal;
530 wndpl.ptMaxPosition = wndPtr->ptMaxPos;
531 SetWindowPlacement( hwnd, &wndpl );
535 /***********************************************************************
536 * GetWindowPlacement (USER.370)
538 BOOL GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
540 WND *wndPtr = WIN_FindWndPtr( hwnd );
541 if (!wndPtr) return FALSE;
543 wndpl->length = sizeof(*wndpl);
544 wndpl->flags = 0;
545 wndpl->showCmd = IsZoomed(hwnd) ? SW_SHOWMAXIMIZED :
546 (IsIconic(hwnd) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL);
547 wndpl->ptMinPosition = wndPtr->ptIconPos;
548 wndpl->ptMaxPosition = wndPtr->ptMaxPos;
549 wndpl->rcNormalPosition = wndPtr->rectNormal;
550 return TRUE;
554 /***********************************************************************
555 * SetWindowPlacement (USER.371)
557 BOOL SetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
559 WND *wndPtr = WIN_FindWndPtr( hwnd );
560 if (!wndPtr) return FALSE;
562 if (wndpl->flags & WPF_SETMINPOSITION)
563 wndPtr->ptIconPos = wndpl->ptMinPosition;
564 if ((wndpl->flags & WPF_RESTORETOMAXIMIZED) &&
565 (wndpl->showCmd == SW_SHOWMINIMIZED)) wndPtr->flags |= WIN_RESTORE_MAX;
566 wndPtr->ptMaxPos = wndpl->ptMaxPosition;
567 wndPtr->rectNormal = wndpl->rcNormalPosition;
568 ShowWindow( hwnd, wndpl->showCmd );
569 return TRUE;
572 /*******************************************************************
573 * ACTIVATEAPP_callback
575 BOOL ACTIVATEAPP_callback(HWND hWnd, LPARAM lParam)
577 ACTIVATESTRUCT *lpActStruct = (ACTIVATESTRUCT*)lParam;
578 WND *wndPtr = WIN_FindWndPtr( hWnd );
580 if( !wndPtr || hWnd == GetDesktopWindow()) return 1;
582 if( MSG_GetQueueTask(wndPtr->hmemTaskQ) != lpActStruct->hTaskSendTo )
583 return 1;
585 SendMessage( hWnd, WM_ACTIVATEAPP, lpActStruct->wFlag,
586 (LPARAM)((lpActStruct->hWindowTask)?lpActStruct->hWindowTask:0));
587 return 1;
591 /*******************************************************************
592 * WINPOS_SetActiveWindow
594 * back-end to SetActiveWindow
596 BOOL WINPOS_SetActiveWindow( HWND hWnd, BOOL fMouse, BOOL fChangeFocus )
598 WND *wndPtr = WIN_FindWndPtr(hWnd);
599 WND *wndTemp = WIN_FindWndPtr(hwndActive);
600 CBTACTIVATESTRUCT cbtStruct = { fMouse , hwndActive };
601 FARPROC enumCallback = (FARPROC)GetWndProcEntry16("ActivateAppProc");
602 ACTIVATESTRUCT actStruct;
603 WORD wIconized=0,wRet= 0;
605 /* paranoid checks */
606 if( !hWnd || hWnd == GetDesktopWindow() || hWnd == hwndActive )
607 return 0;
609 if( GetTaskQueue(0) != wndPtr->hmemTaskQ )
610 return 0;
612 if( wndTemp )
613 wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
614 else
615 dprintf_win(stddeb,"WINPOS_ActivateWindow: no current active window.\n");
617 /* call CBT hook chain */
618 wRet = HOOK_CallHooks(WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd,
619 (LPARAM)MAKE_SEGPTR(&cbtStruct));
621 if( wRet ) return wRet;
623 /* set prev active wnd to current active wnd and send notification */
624 if( (hwndPrevActive = hwndActive) )
626 /* FIXME: need a Win32 translation for WINELIB32 */
627 if( !SendMessage(hwndPrevActive, WM_NCACTIVATE, 0, MAKELONG(hWnd,wIconized)) )
629 if (GetSysModalWindow() != hWnd) return 0;
630 /* disregard refusal if hWnd is sysmodal */
633 #ifdef WINELIB32
634 SendMessage( hwndActive, WM_ACTIVATE,
635 MAKEWPARAM( WA_INACTIVE, wIconized ),
636 (LPARAM)hWnd );
637 #else
638 SendMessage(hwndPrevActive, WM_ACTIVATE, WA_INACTIVE,
639 MAKELONG(hWnd,wIconized));
640 #endif
642 /* check if something happened during message processing */
643 if( hwndPrevActive != hwndActive ) return 0;
646 /* set active wnd */
647 hwndActive = hWnd;
649 /* send palette messages */
650 if( SendMessage( hWnd, WM_QUERYNEWPALETTE, 0, 0L) )
651 SendMessage((HWND)-1, WM_PALETTEISCHANGING, (WPARAM)hWnd, 0L );
653 /* if prev wnd is minimized redraw icon title
654 if( hwndPrevActive )
656 wndTemp = WIN_FindWndPtr( WIN_GetTopParent( hwndPrevActive ) );
657 if(wndTemp)
658 if(wndTemp->dwStyle & WS_MINIMIZE)
659 RedrawIconTitle(hwndPrevActive);
662 if (!(wndPtr->dwStyle & WS_CHILD) )
664 /* check Z-order and bring hWnd to the top */
665 wndTemp = WIN_FindWndPtr( GetDesktopWindow() );
667 for( ; wndTemp; wndTemp = WIN_FindWndPtr( wndTemp->hwndNext ))
668 if( wndTemp->dwStyle & WS_VISIBLE )
669 break;
671 if( wndTemp != wndPtr )
672 SetWindowPos(hWnd, HWND_TOP, 0,0,0,0,
673 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
676 if( !IsWindow(hWnd) ) return 0;
678 /* send WM_ACTIVATEAPP if necessary */
679 if( hActiveQ_G != wndPtr->hmemTaskQ )
681 HTASK hT = MSG_GetQueueTask( hActiveQ_G );
683 actStruct.wFlag = 0; /* deactivate */
684 actStruct.hWindowTask = MSG_GetQueueTask(wndPtr->hmemTaskQ);
685 actStruct.hTaskSendTo = hT;
687 /* send WM_ACTIVATEAPP to top-level windows
688 * that belong to the actStruct.hTaskSendTo task
690 EnumWindows( enumCallback , (LPARAM)&actStruct );
692 /* change active queue */
693 hActiveQ_G = wndPtr->hmemTaskQ;
695 actStruct.wFlag = 1; /* activate */
696 actStruct.hWindowTask = hT;
697 actStruct.hTaskSendTo = MSG_GetQueueTask( hActiveQ_G );
699 EnumWindows( enumCallback , (LPARAM)&actStruct );
701 if( !IsWindow(hWnd) ) return 0;
704 /* walk up to the first unowned window */
705 wndTemp = wndPtr;
707 while(wndTemp->hwndOwner)
709 wndTemp = WIN_FindWndPtr(wndTemp->hwndOwner);
710 if( !wndTemp)
712 /* there must be an unowned window in hierarchy */
713 dprintf_win(stddeb,"WINPOS_ActivateWindow: broken owner list\n");
714 wndTemp = wndPtr;
715 break;
718 /* and set last active owned popup */
719 wndTemp->hwndLastActive = hWnd;
721 wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
722 /* FIXME: Needs a Win32 translation for WINELIB32 */
723 SendMessage( hWnd, WM_NCACTIVATE, 1,
724 MAKELONG(hwndPrevActive,wIconized));
725 #ifdef WINELIB32
726 SendMessage( hWnd, WM_ACTIVATE,
727 MAKEWPARAM( (fMouse)?WA_CLICKACTIVE:WA_ACTIVE, wIconized),
728 (LPARAM)hwndPrevActive );
729 #else
730 SendMessage( hWnd, WM_ACTIVATE, (fMouse)? WA_CLICKACTIVE : WA_ACTIVE,
731 MAKELONG(hwndPrevActive,wIconized));
732 #endif
734 if( !IsWindow(hWnd) ) return 0;
736 /* change focus if possible */
737 if( fChangeFocus && GetFocus() )
738 if( WIN_GetTopParent(GetFocus()) != hwndActive )
739 FOCUS_SwitchFocus( GetFocus(),
740 (wndPtr->dwStyle & WS_MINIMIZE)? 0: hwndActive);
742 /* if active wnd is minimized redraw icon title
743 if( hwndActive )
745 wndPtr = WIN_FindWndPtr(hwndActive);
746 if(wndPtr->dwStyle & WS_MINIMIZE)
747 RedrawIconTitle(hwndActive);
750 return (hWnd == hwndActive);
754 /*******************************************************************
755 * WINPOS_ChangeActiveWindow
758 BOOL WINPOS_ChangeActiveWindow( HWND hWnd, BOOL mouseMsg )
760 WND *wndPtr = WIN_FindWndPtr(hWnd);
762 if( !wndPtr ) return FALSE;
764 /* minors are not allowed */
765 if( (wndPtr->dwStyle & WS_CHILD) && !( wndPtr->dwStyle & WS_POPUP))
766 return SendMessage(hWnd, WM_CHILDACTIVATE, 0, 0L);
768 if( hWnd == hwndActive ) return FALSE;
770 if( !WINPOS_SetActiveWindow(hWnd ,mouseMsg ,TRUE) )
771 return FALSE;
773 /* switch desktop queue to current active here */
774 if( wndPtr->hwndParent == GetDesktopWindow())
777 return TRUE;
781 /***********************************************************************
782 * WINPOS_SendNCCalcSize
784 * Send a WM_NCCALCSIZE message to a window.
785 * All parameters are read-only except newClientRect.
786 * oldWindowRect, oldClientRect and winpos must be non-NULL only
787 * when calcValidRect is TRUE.
789 LONG WINPOS_SendNCCalcSize( HWND hwnd, BOOL calcValidRect, RECT *newWindowRect,
790 RECT *oldWindowRect, RECT *oldClientRect,
791 WINDOWPOS *winpos, RECT *newClientRect )
793 NCCALCSIZE_PARAMS params;
794 LONG result;
796 params.rgrc[0] = *newWindowRect;
797 if (calcValidRect)
799 params.rgrc[1] = *oldWindowRect;
800 params.rgrc[2] = *oldClientRect;
801 params.lppos = winpos;
803 result = SendMessage( hwnd, WM_NCCALCSIZE, calcValidRect,
804 (LPARAM)MAKE_SEGPTR( &params ) );
805 dprintf_win(stddeb, "WINPOS_SendNCCalcSize: %d %d %d %d\n",
806 (int)params.rgrc[0].top, (int)params.rgrc[0].left,
807 (int)params.rgrc[0].bottom, (int)params.rgrc[0].right);
808 *newClientRect = params.rgrc[0];
809 return result;
813 /***********************************************************************
814 * WINPOS_HandleWindowPosChanging
816 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
818 LONG WINPOS_HandleWindowPosChanging( WINDOWPOS *winpos )
820 POINT maxSize;
821 WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
822 if (!wndPtr || (winpos->flags & SWP_NOSIZE)) return 0;
823 if ((wndPtr->dwStyle & WS_THICKFRAME) ||
824 ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
826 NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
827 winpos->cx = MIN( winpos->cx, maxSize.x );
828 winpos->cy = MIN( winpos->cy, maxSize.y );
830 return 0;
834 /***********************************************************************
835 * WINPOS_MoveWindowZOrder
837 * Move a window in Z order, invalidating everything that needs it.
838 * Only necessary for windows without associated X window.
840 static void WINPOS_MoveWindowZOrder( HWND hwnd, HWND hwndAfter )
842 BOOL movingUp;
843 HWND hwndCur;
844 WND *wndPtr = WIN_FindWndPtr( hwnd );
846 /* We have two possible cases:
847 * - The window is moving up: we have to invalidate all areas
848 * of the window that were covered by other windows
849 * - The window is moving down: we have to invalidate areas
850 * of other windows covered by this one.
853 if (hwndAfter == HWND_TOP)
855 movingUp = TRUE;
857 else if (hwndAfter == HWND_BOTTOM)
859 if (!wndPtr->hwndNext) return; /* Already at the bottom */
860 movingUp = FALSE;
862 else
864 if (wndPtr->hwndNext == hwndAfter) return; /* Already placed right */
866 /* Determine which window we encounter first in Z-order */
867 hwndCur = GetWindow( wndPtr->hwndParent, GW_CHILD );
868 while ((hwndCur != hwnd) && (hwndCur != hwndAfter))
869 hwndCur = GetWindow( hwndCur, GW_HWNDNEXT );
870 movingUp = (hwndCur == hwndAfter);
873 if (movingUp)
875 HWND hwndPrevAfter = wndPtr->hwndNext;
876 WIN_UnlinkWindow( hwnd );
877 WIN_LinkWindow( hwnd, hwndAfter );
878 hwndCur = wndPtr->hwndNext;
879 while (hwndCur != hwndPrevAfter)
881 WND *curPtr = WIN_FindWndPtr( hwndCur );
882 RECT rect = curPtr->rectWindow;
883 OffsetRect( &rect, -wndPtr->rectClient.left,
884 -wndPtr->rectClient.top );
885 RedrawWindow( hwnd, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
886 RDW_FRAME | RDW_ERASE );
887 hwndCur = curPtr->hwndNext;
890 else /* Moving down */
892 hwndCur = wndPtr->hwndNext;
893 WIN_UnlinkWindow( hwnd );
894 WIN_LinkWindow( hwnd, hwndAfter );
895 while (hwndCur != hwnd)
897 WND *curPtr = WIN_FindWndPtr( hwndCur );
898 RECT rect = wndPtr->rectWindow;
899 OffsetRect( &rect, -curPtr->rectClient.left,
900 -curPtr->rectClient.top );
901 RedrawWindow( hwndCur, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
902 RDW_FRAME | RDW_ERASE );
903 hwndCur = curPtr->hwndNext;
909 /***********************************************************************
910 * WINPOS_SetXWindowPos
912 * SetWindowPos() for an X window. Used by the real SetWindowPos().
914 static void WINPOS_SetXWindowPos( WINDOWPOS *winpos )
916 XWindowChanges winChanges;
917 int changeMask = 0;
918 WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
920 if (!(winpos->flags & SWP_NOSIZE))
922 winChanges.width = winpos->cx;
923 winChanges.height = winpos->cy;
924 changeMask |= CWWidth | CWHeight;
926 if (!(winpos->flags & SWP_NOMOVE))
928 winChanges.x = winpos->x;
929 winChanges.y = winpos->y;
930 changeMask |= CWX | CWY;
932 if (!(winpos->flags & SWP_NOZORDER))
934 if (winpos->hwndInsertAfter == HWND_TOP) winChanges.stack_mode = Above;
935 else winChanges.stack_mode = Below;
936 if ((winpos->hwndInsertAfter != HWND_TOP) &&
937 (winpos->hwndInsertAfter != HWND_BOTTOM))
939 WND * insertPtr = WIN_FindWndPtr( winpos->hwndInsertAfter );
940 winChanges.sibling = insertPtr->window;
941 changeMask |= CWSibling;
943 changeMask |= CWStackMode;
945 if (changeMask)
946 XConfigureWindow( display, wndPtr->window, changeMask, &winChanges );
950 /***********************************************************************
951 * SetWindowPos (USER.232)
953 BOOL SetWindowPos( HWND hwnd, HWND hwndInsertAfter, INT x, INT y,
954 INT cx, INT cy, WORD flags )
956 WINDOWPOS winpos;
957 WND *wndPtr;
958 RECT newWindowRect, newClientRect;
959 int result;
961 /* Check window handle */
963 if (hwnd == GetDesktopWindow()) return FALSE;
964 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
966 /* Check dimensions */
968 if (cx <= 0) cx = 1;
969 if (cy <= 0) cy = 1;
971 /* Check flags */
973 if (hwnd == hwndActive) flags |= SWP_NOACTIVATE; /* Already active */
974 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == cx) &&
975 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == cy))
976 flags |= SWP_NOSIZE; /* Already the right size */
977 if ((wndPtr->rectWindow.left == x) && (wndPtr->rectWindow.top == y))
978 flags |= SWP_NOMOVE; /* Already the right position */
980 /* Check hwndInsertAfter */
982 if (!(flags & (SWP_NOZORDER | SWP_NOACTIVATE)))
984 /* Ignore TOPMOST flags when activating a window */
985 /* _and_ moving it in Z order. */
986 if ((hwndInsertAfter == HWND_TOPMOST) ||
987 (hwndInsertAfter == HWND_NOTOPMOST))
988 hwndInsertAfter = HWND_TOP;
990 /* TOPMOST not supported yet */
991 if ((hwndInsertAfter == HWND_TOPMOST) ||
992 (hwndInsertAfter == HWND_NOTOPMOST)) hwndInsertAfter = HWND_TOP;
993 /* hwndInsertAfter must be a sibling of the window */
994 if ((hwndInsertAfter != HWND_TOP) && (hwndInsertAfter != HWND_BOTTOM) &&
995 (wndPtr->hwndParent != WIN_FindWndPtr(hwndInsertAfter)->hwndParent))
996 return FALSE;
998 /* Fill the WINDOWPOS structure */
1000 winpos.hwnd = hwnd;
1001 winpos.hwndInsertAfter = hwndInsertAfter;
1002 winpos.x = x;
1003 winpos.y = y;
1004 winpos.cx = cx;
1005 winpos.cy = cy;
1006 winpos.flags = flags;
1008 /* Send WM_WINDOWPOSCHANGING message */
1010 if (!(flags & SWP_NOSENDCHANGING))
1011 SendMessage( hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1013 /* Calculate new position and size */
1015 newWindowRect = wndPtr->rectWindow;
1016 newClientRect = wndPtr->rectClient;
1018 if (!(winpos.flags & SWP_NOSIZE))
1020 newWindowRect.right = newWindowRect.left + winpos.cx;
1021 newWindowRect.bottom = newWindowRect.top + winpos.cy;
1023 if (!(winpos.flags & SWP_NOMOVE))
1025 newWindowRect.left = winpos.x;
1026 newWindowRect.top = winpos.y;
1027 newWindowRect.right += winpos.x - wndPtr->rectWindow.left;
1028 newWindowRect.bottom += winpos.y - wndPtr->rectWindow.top;
1031 /* Reposition window in Z order */
1033 if (!(winpos.flags & SWP_NOZORDER))
1035 if (wndPtr->window)
1037 WIN_UnlinkWindow( winpos.hwnd );
1038 WIN_LinkWindow( winpos.hwnd, hwndInsertAfter );
1040 else WINPOS_MoveWindowZOrder( winpos.hwnd, hwndInsertAfter );
1043 /* Send WM_NCCALCSIZE message to get new client area */
1045 result = WINPOS_SendNCCalcSize( winpos.hwnd, TRUE, &newWindowRect,
1046 &wndPtr->rectWindow, &wndPtr->rectClient,
1047 &winpos, &newClientRect );
1048 /* .... Should handle result here */
1050 /* Perform the moving and resizing */
1052 if (wndPtr->window)
1054 WINPOS_SetXWindowPos( &winpos );
1055 wndPtr->rectWindow = newWindowRect;
1056 wndPtr->rectClient = newClientRect;
1058 else
1060 RECT oldWindowRect = wndPtr->rectWindow;
1062 wndPtr->rectWindow = newWindowRect;
1063 wndPtr->rectClient = newClientRect;
1065 if (!(flags & SWP_NOREDRAW) &&
1066 (!(flags & SWP_NOSIZE) || !(flags & SWP_NOMOVE) ||
1067 (!(flags & SWP_NOZORDER) && (hwndInsertAfter != HWND_TOP))))
1069 HRGN hrgn1 = CreateRectRgnIndirect( &oldWindowRect );
1070 HRGN hrgn2 = CreateRectRgnIndirect( &wndPtr->rectWindow );
1071 HRGN hrgn3 = CreateRectRgn( 0, 0, 0, 0 );
1072 CombineRgn( hrgn3, hrgn1, hrgn2, RGN_DIFF );
1073 RedrawWindow( wndPtr->hwndParent, NULL, hrgn3,
1074 RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1076 /* DCE_GetVisRgn should be called for old coordinates
1077 * and for new, then OffsetRgn and CombineRgn -
1078 * voila, a nice update region to use here - AK.
1080 if ((oldWindowRect.left != wndPtr->rectWindow.left) ||
1081 (oldWindowRect.top != wndPtr->rectWindow.top))
1083 RedrawWindow( winpos.hwnd, NULL, 0, RDW_INVALIDATE |
1084 RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1086 else
1087 if( CombineRgn( hrgn3, hrgn2, hrgn1, RGN_DIFF) != NULLREGION )
1088 RedrawWindow( winpos.hwnd, NULL, hrgn3, RDW_INVALIDATE |
1089 RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1091 DeleteObject( hrgn1 );
1092 DeleteObject( hrgn2 );
1093 DeleteObject( hrgn3 );
1097 if (flags & SWP_SHOWWINDOW)
1099 wndPtr->dwStyle |= WS_VISIBLE;
1100 if (wndPtr->window)
1102 XMapWindow( display, wndPtr->window );
1104 else
1106 if (!(flags & SWP_NOREDRAW))
1107 RedrawWindow( winpos.hwnd, NULL, 0,
1108 RDW_INVALIDATE | RDW_ALLCHILDREN |
1109 RDW_FRAME | RDW_ERASE );
1112 else if (flags & SWP_HIDEWINDOW)
1114 wndPtr->dwStyle &= ~WS_VISIBLE;
1115 if (wndPtr->window)
1117 XUnmapWindow( display, wndPtr->window );
1119 else
1121 if (!(flags & SWP_NOREDRAW))
1122 RedrawWindow( wndPtr->hwndParent, &wndPtr->rectWindow, 0,
1123 RDW_INVALIDATE | RDW_FRAME |
1124 RDW_ALLCHILDREN | RDW_ERASE );
1127 if ((winpos.hwnd == GetFocus()) || IsChild(winpos.hwnd, GetFocus()))
1128 SetFocus( GetParent(winpos.hwnd) ); /* Revert focus to parent */
1130 if (winpos.hwnd == hwndActive)
1132 /* Activate previously active window if possible */
1133 HWND newActive = hwndPrevActive;
1134 if (!IsWindow(newActive) || (newActive == winpos.hwnd))
1136 newActive = GetTopWindow( GetDesktopWindow() );
1137 if (newActive == winpos.hwnd) newActive = wndPtr->hwndNext;
1139 WINPOS_ChangeActiveWindow( newActive, FALSE );
1143 /* Activate the window */
1145 if (!(flags & SWP_NOACTIVATE))
1146 WINPOS_ChangeActiveWindow( winpos.hwnd, FALSE );
1148 /* Repaint the window */
1150 if (wndPtr->window) MSG_Synchronize(); /* Wait for all expose events */
1152 EVENT_DummyMotionNotify(); /* Simulate a mouse event to set the cursor */
1154 if ((flags & SWP_FRAMECHANGED) && !(flags & SWP_NOREDRAW))
1155 RedrawWindow( winpos.hwnd, NULL, 0,
1156 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE );
1157 if (!(flags & SWP_DEFERERASE))
1158 RedrawWindow( wndPtr->hwndParent, NULL, 0,
1159 RDW_ALLCHILDREN | RDW_ERASENOW );
1161 /* And last, send the WM_WINDOWPOSCHANGED message */
1163 winpos.flags |= SWP_NOMOVE; /* prevent looping.. window is already moved ??? (FIXME)*/
1165 if (!(winpos.flags & SWP_NOSENDCHANGING))
1166 SendMessage( winpos.hwnd, WM_WINDOWPOSCHANGED,
1167 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1169 return TRUE;
1173 /***********************************************************************
1174 * BeginDeferWindowPos (USER.259)
1176 HDWP BeginDeferWindowPos( INT count )
1178 HDWP handle;
1179 DWP *pDWP;
1181 if (count <= 0) return 0;
1182 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1183 if (!handle) return 0;
1184 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1185 pDWP->actualCount = 0;
1186 pDWP->suggestedCount = count;
1187 pDWP->valid = TRUE;
1188 pDWP->wMagic = DWP_MAGIC;
1189 pDWP->hwndParent = 0;
1190 return handle;
1194 /***********************************************************************
1195 * DeferWindowPos (USER.260)
1197 HDWP DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter, INT x, INT y,
1198 INT cx, INT cy, WORD flags )
1200 DWP *pDWP;
1201 int i;
1202 HDWP newhdwp = hdwp;
1203 HWND parent;
1205 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1206 if (!pDWP) return 0;
1208 /* All the windows of a DeferWindowPos() must have the same parent */
1210 parent = WIN_FindWndPtr( hwnd )->hwndParent;
1211 if (pDWP->actualCount == 0) pDWP->hwndParent = parent;
1212 else if (parent != pDWP->hwndParent)
1214 USER_HEAP_FREE( hdwp );
1215 return 0;
1218 for (i = 0; i < pDWP->actualCount; i++)
1220 if (pDWP->winPos[i].hwnd == hwnd)
1222 /* Merge with the other changes */
1223 if (!(flags & SWP_NOZORDER))
1225 pDWP->winPos[i].hwndInsertAfter = hwndAfter;
1227 if (!(flags & SWP_NOMOVE))
1229 pDWP->winPos[i].x = x;
1230 pDWP->winPos[i].y = y;
1232 if (!(flags & SWP_NOSIZE))
1234 pDWP->winPos[i].cx = cx;
1235 pDWP->winPos[i].cy = cy;
1237 pDWP->winPos[i].flags &= flags & (SWP_NOSIZE | SWP_NOMOVE |
1238 SWP_NOZORDER | SWP_NOREDRAW |
1239 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1240 SWP_NOOWNERZORDER);
1241 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1242 SWP_FRAMECHANGED);
1243 return hdwp;
1246 if (pDWP->actualCount >= pDWP->suggestedCount)
1248 newhdwp = USER_HEAP_REALLOC( hdwp,
1249 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1250 if (!newhdwp) return 0;
1251 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1252 pDWP->suggestedCount++;
1254 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1255 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1256 pDWP->winPos[pDWP->actualCount].x = x;
1257 pDWP->winPos[pDWP->actualCount].y = y;
1258 pDWP->winPos[pDWP->actualCount].cx = cx;
1259 pDWP->winPos[pDWP->actualCount].cy = cy;
1260 pDWP->winPos[pDWP->actualCount].flags = flags;
1261 pDWP->actualCount++;
1262 return newhdwp;
1266 /***********************************************************************
1267 * EndDeferWindowPos (USER.261)
1269 BOOL EndDeferWindowPos( HDWP hdwp )
1271 DWP *pDWP;
1272 WINDOWPOS *winpos;
1273 BOOL res = TRUE;
1274 int i;
1276 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1277 if (!pDWP) return FALSE;
1278 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1280 if (!(res = SetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1281 winpos->x, winpos->y, winpos->cx, winpos->cy,
1282 winpos->flags ))) break;
1284 USER_HEAP_FREE( hdwp );
1285 return res;
1289 /***********************************************************************
1290 * TileChildWindows (USER.199)
1292 void TileChildWindows( HWND parent, WORD action )
1294 printf("STUB TileChildWindows("NPFMT", %d)\n", parent, action);
1297 /***********************************************************************
1298 * CascageChildWindows (USER.198)
1300 void CascadeChildWindows( HWND parent, WORD action )
1302 printf("STUB CascadeChildWindows("NPFMT", %d)\n", parent, action);