jscript: Add Map.prototype.forEach implementation.
[wine.git] / dlls / user32 / mdi.c
blobaf39e5781ed8b47c3433f48a80711f9eb99a421f
1 /* MDI.C
3 * Copyright 1994, Bob Amstadt
4 * 1995,1996 Alex Korobka
6 * This file contains routines to support MDI (Multiple Document
7 * Interface) features .
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Notes: Fairly complete implementation.
24 * Also, Excel and WinWord do _not_ use MDI so if you're trying
25 * to fix them look elsewhere.
27 * Notes on how the "More Windows..." is implemented:
29 * When we have more than 9 opened windows, a "More Windows..."
30 * option appears in the "Windows" menu. Each child window has
31 * a WND* associated with it, accessible via the children list of
32 * the parent window. This WND* has a wIDmenu member, which reflects
33 * the position of the child in the window list. For example, with
34 * 9 child windows, we could have the following pattern:
38 * Name of the child window pWndChild->wIDmenu
39 * Doc1 5000
40 * Doc2 5001
41 * Doc3 5002
42 * Doc4 5003
43 * Doc5 5004
44 * Doc6 5005
45 * Doc7 5006
46 * Doc8 5007
47 * Doc9 5008
50 * The "Windows" menu, as the "More windows..." dialog, are constructed
51 * in this order. If we add a child, we would have the following list:
54 * Name of the child window pWndChild->wIDmenu
55 * Doc1 5000
56 * Doc2 5001
57 * Doc3 5002
58 * Doc4 5003
59 * Doc5 5004
60 * Doc6 5005
61 * Doc7 5006
62 * Doc8 5007
63 * Doc9 5008
64 * Doc10 5009
66 * But only 5000 to 5008 would be displayed in the "Windows" menu. We want
67 * the last created child to be in the menu, so we swap the last child with
68 * the 9th... Doc9 will be accessible via the "More Windows..." option.
70 * Doc1 5000
71 * Doc2 5001
72 * Doc3 5002
73 * Doc4 5003
74 * Doc5 5004
75 * Doc6 5005
76 * Doc7 5006
77 * Doc8 5007
78 * Doc9 5009
79 * Doc10 5008
83 #include <stdlib.h>
84 #include <stdarg.h>
85 #include <stdio.h>
86 #include <string.h>
87 #include <math.h>
89 #define OEMRESOURCE
91 #include "windef.h"
92 #include "winbase.h"
93 #include "winnls.h"
94 #include "wingdi.h"
95 #include "winuser.h"
96 #include "wownt32.h"
97 #include "win.h"
98 #include "controls.h"
99 #include "user_private.h"
100 #include "wine/debug.h"
102 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
104 #define MDI_MAXTITLELENGTH 0xa1
106 #define WM_MDICALCCHILDSCROLL 0x003f /* this is exactly what Windows uses */
108 /* "More Windows..." definitions */
109 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
110 option will appear under the Windows menu */
111 #define MDI_IDC_LISTBOX 100
112 #define IDS_MDI_MOREWINDOWS 13
114 #define MDIF_NEEDUPDATE 0x0001
116 typedef struct
118 /* At some points, particularly when switching MDI children, active and
119 * maximized MDI children may be not the same window, so we need to track
120 * them separately.
121 * The only place where we switch to/from maximized state is DefMDIChildProc
122 * WM_SIZE/SIZE_MAXIMIZED handler. We get that notification only after the
123 * ShowWindow(SW_SHOWMAXIMIZED) request, therefore window is guaranteed to
124 * be visible at the time we get the notification, and it's safe to assume
125 * that hwndChildMaximized is always visible.
126 * If the app plays games with WS_VISIBLE, WS_MAXIMIZE or any other window
127 * states it must keep coherency with USER32 on its own. This is true for
128 * Windows as well.
130 LONG reserved;
131 UINT nActiveChildren;
132 HWND hwndChildMaximized;
133 HWND hwndActiveChild;
134 HWND *child; /* array of tracked children */
135 HMENU hFrameMenu;
136 HMENU hWindowMenu;
137 UINT idFirstChild;
138 LPWSTR frameTitle;
139 UINT nTotalCreated;
140 UINT mdiFlags;
141 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
142 } MDICLIENTINFO;
144 static HBITMAP hBmpClose = 0;
146 /* ----------------- declarations ----------------- */
147 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
148 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
149 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
150 static LONG MDI_ChildActivate( HWND, HWND );
151 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
153 static HWND MDI_MoreWindowsDialog(HWND);
155 /* -------- Miscellaneous service functions ----------
157 * MDI_GetChildByID
159 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
161 int i;
163 for (i = 0; ci->nActiveChildren; i++)
165 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
166 return ci->child[i];
168 return 0;
171 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
173 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
175 ci->mdiFlags |= MDIF_NEEDUPDATE;
176 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
178 ci->sbRecalc = recalc;
182 /*********************************************************************
183 * MDIClient class descriptor
185 const struct builtin_class_descr MDICLIENT_builtin_class =
187 L"MDIClient", /* name */
188 0, /* style */
189 WINPROC_MDICLIENT, /* proc */
190 sizeof(MDICLIENTINFO), /* extra */
191 IDC_ARROW, /* cursor */
192 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
196 static MDICLIENTINFO *get_client_info( HWND client )
198 MDICLIENTINFO *ret = NULL;
199 WND *win = WIN_GetPtr( client );
200 if (win)
202 if (win == WND_OTHER_PROCESS || win == WND_DESKTOP)
204 if (IsWindow(client)) WARN( "client %p belongs to other process\n", client );
205 return NULL;
207 if (win->flags & WIN_ISMDICLIENT)
208 ret = (MDICLIENTINFO *)win->wExtra;
209 else
210 WARN( "%p is not an MDI client\n", client );
211 WIN_ReleasePtr( win );
213 return ret;
216 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
218 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
220 if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
221 if (hSysMenu)
223 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
224 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
225 return FALSE;
227 return TRUE;
230 /**********************************************************************
231 * MDI_GetWindow
233 * returns "activatable" child different from the current or zero
235 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
236 DWORD dwStyleMask )
238 int i;
239 HWND *list;
240 HWND last = 0;
242 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
243 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
245 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
246 i = 0;
247 /* start from next after hWnd */
248 while (list[i] && list[i] != hWnd) i++;
249 if (list[i]) i++;
251 for ( ; list[i]; i++)
253 if (GetWindow( list[i], GW_OWNER )) continue;
254 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
255 last = list[i];
256 if (bNext) goto found;
258 /* now restart from the beginning */
259 for (i = 0; list[i] && list[i] != hWnd; i++)
261 if (GetWindow( list[i], GW_OWNER )) continue;
262 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
263 last = list[i];
264 if (bNext) goto found;
266 found:
267 HeapFree( GetProcessHeap(), 0, list );
268 return last;
271 /**********************************************************************
272 * MDI_CalcDefaultChildPos
274 * It seems that the default height is about 2/3 of the client rect
276 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
278 INT nstagger;
279 RECT rect;
280 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
282 if (total < 0) /* we are called from CreateWindow */
284 MDICLIENTINFO *ci = get_client_info(hwndClient);
285 total = ci->nTotalCreated;
286 *id = ci->idFirstChild + ci->nActiveChildren;
287 TRACE("MDI child id %04x\n", *id);
290 GetClientRect( hwndClient, &rect );
291 if( rect.bottom - rect.top - delta >= spacing )
292 rect.bottom -= delta;
294 nstagger = (rect.bottom - rect.top)/(3 * spacing);
295 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
296 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
297 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
300 /**********************************************************************
301 * MDISetMenu
303 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
304 HMENU hmenuWindow)
306 MDICLIENTINFO *ci;
307 HWND hwndFrame = GetParent(hwnd);
309 TRACE("%p, frame menu %p, window menu %p\n", hwnd, hmenuFrame, hmenuWindow);
311 if (hmenuFrame && !IsMenu(hmenuFrame))
313 WARN("hmenuFrame is not a menu handle\n");
314 return 0L;
317 if (hmenuWindow && !IsMenu(hmenuWindow))
319 WARN("hmenuWindow is not a menu handle\n");
320 return 0L;
323 if (!(ci = get_client_info( hwnd ))) return 0;
325 TRACE("old frame menu %p, old window menu %p\n", ci->hFrameMenu, ci->hWindowMenu);
327 if (hmenuFrame)
329 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
331 if (ci->hwndChildMaximized)
332 MDI_RestoreFrameMenu( hwndFrame, ci->hwndChildMaximized );
335 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
337 /* delete menu items from ci->hWindowMenu
338 * and add them to hmenuWindow */
339 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
340 if( ci->hWindowMenu && ci->nActiveChildren )
342 UINT nActiveChildren_old = ci->nActiveChildren;
344 /* Remove all items from old Window menu */
345 ci->nActiveChildren = 0;
346 MDI_RefreshMenu(ci);
348 ci->hWindowMenu = hmenuWindow;
350 /* Add items to the new Window menu */
351 ci->nActiveChildren = nActiveChildren_old;
352 MDI_RefreshMenu(ci);
354 else
355 ci->hWindowMenu = hmenuWindow;
358 if (hmenuFrame)
360 SetMenu(hwndFrame, hmenuFrame);
361 if( hmenuFrame != ci->hFrameMenu )
363 HMENU oldFrameMenu = ci->hFrameMenu;
365 ci->hFrameMenu = hmenuFrame;
366 if (ci->hwndChildMaximized)
367 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
369 return (LRESULT)oldFrameMenu;
373 return 0;
376 /**********************************************************************
377 * MDIRefreshMenu
379 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
381 UINT i, count, visible, id;
382 WCHAR buf[MDI_MAXTITLELENGTH];
384 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
386 if (!ci->hWindowMenu)
387 return 0;
389 if (!IsMenu(ci->hWindowMenu))
391 WARN("Window menu handle %p is no longer valid\n", ci->hWindowMenu);
392 return 0;
395 /* Windows finds the last separator in the menu, and if after it
396 * there is a menu item with MDI magic ID removes all existing
397 * menu items after it, and then adds visible MDI children.
399 count = GetMenuItemCount(ci->hWindowMenu);
400 for (i = 0; i < count; i++)
402 MENUITEMINFOW mii;
404 memset(&mii, 0, sizeof(mii));
405 mii.cbSize = sizeof(mii);
406 mii.fMask = MIIM_TYPE;
407 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
409 if (mii.fType & MF_SEPARATOR)
411 /* Windows checks only ID of the menu item */
412 memset(&mii, 0, sizeof(mii));
413 mii.cbSize = sizeof(mii);
414 mii.fMask = MIIM_ID;
415 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
417 if (mii.wID == ci->idFirstChild)
419 TRACE("removing %u items including separator\n", count - i);
420 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
421 /* nothing */;
423 break;
430 visible = 0;
431 for (i = 0; i < ci->nActiveChildren; i++)
433 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
435 id = ci->idFirstChild + visible;
437 if (visible == MDI_MOREWINDOWSLIMIT)
439 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, ARRAY_SIZE(buf));
440 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
441 break;
444 if (!visible)
445 /* Visio expects that separator has id 0 */
446 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
448 visible++;
450 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
452 buf[0] = '&';
453 buf[1] = '0' + visible;
454 buf[2] = ' ';
455 InternalGetWindowText(ci->child[i], buf + 3, ARRAY_SIZE(buf) - 3);
456 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
457 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
459 if (ci->child[i] == ci->hwndActiveChild)
460 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
462 else
463 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
466 return (LRESULT)ci->hFrameMenu;
470 /* ------------------ MDI child window functions ---------------------- */
472 /**********************************************************************
473 * MDI_ChildGetMinMaxInfo
475 * Note: The rule here is that client rect of the maximized MDI child
476 * is equal to the client rect of the MDI client window.
478 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
480 RECT rect;
482 GetClientRect( client, &rect );
483 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
484 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
486 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
487 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
489 lpMinMax->ptMaxPosition.x = rect.left;
490 lpMinMax->ptMaxPosition.y = rect.top;
492 TRACE("max rect %s\n", wine_dbgstr_rect(&rect));
495 /**********************************************************************
496 * MDI_SwitchActiveChild
498 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
499 * being activated
501 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
503 HWND hwndPrev;
505 hwndPrev = ci->hwndActiveChild;
507 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
509 if ( hwndTo != hwndPrev )
511 BOOL was_zoomed = IsZoomed(hwndPrev);
513 if (was_zoomed)
515 /* restore old MDI child */
516 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
517 ShowWindow( hwndPrev, SW_RESTORE );
518 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
520 /* activate new MDI child */
521 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
522 /* maximize new MDI child */
523 ShowWindow( hwndTo, SW_MAXIMIZE );
525 /* activate new MDI child */
526 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
531 /**********************************************************************
532 * MDIDestroyChild
534 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
535 HWND child, BOOL flagDestroy )
537 UINT i;
539 TRACE("# of managed children %u\n", ci->nActiveChildren);
541 if( child == ci->hwndActiveChild )
543 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
544 /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
545 if (flagDestroy && next)
546 MDI_SwitchActiveChild(ci, next, TRUE);
547 else
549 ShowWindow(child, SW_HIDE);
550 if (child == ci->hwndChildMaximized)
552 HWND frame = GetParent(client);
553 MDI_RestoreFrameMenu(frame, child);
554 ci->hwndChildMaximized = 0;
555 MDI_UpdateFrameText(frame, client, TRUE, NULL);
557 if (flagDestroy)
558 MDI_ChildActivate(client, 0);
562 for (i = 0; i < ci->nActiveChildren; i++)
564 if (ci->child[i] == child)
566 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
567 memcpy(new_child, ci->child, i * sizeof(HWND));
568 if (i + 1 < ci->nActiveChildren)
569 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
570 HeapFree(GetProcessHeap(), 0, ci->child);
571 ci->child = new_child;
573 ci->nActiveChildren--;
574 break;
578 if (flagDestroy)
580 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
581 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
582 DestroyWindow(child);
585 TRACE("child destroyed - %p\n", child);
586 return 0;
590 /**********************************************************************
591 * MDI_ChildActivate
593 * Called in response to WM_CHILDACTIVATE, or when last MDI child
594 * is being deactivated.
596 static LONG MDI_ChildActivate( HWND client, HWND child )
598 MDICLIENTINFO *clientInfo;
599 HWND prevActiveWnd, frame;
600 BOOL isActiveFrameWnd;
602 clientInfo = get_client_info( client );
604 if (clientInfo->hwndActiveChild == child) return 0;
606 TRACE("%p\n", child);
608 frame = GetParent(client);
609 isActiveFrameWnd = (GetActiveWindow() == frame);
610 prevActiveWnd = clientInfo->hwndActiveChild;
612 /* deactivate prev. active child */
613 if(prevActiveWnd)
615 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
616 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
619 MDI_SwitchActiveChild( clientInfo, child, FALSE );
620 clientInfo->hwndActiveChild = child;
622 MDI_RefreshMenu(clientInfo);
624 if( isActiveFrameWnd )
626 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
627 /* Let the client window manage focus for children, but if the focus
628 * is already on the client (for instance this is the 1st child) then
629 * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
630 * manually in this case.
632 if (SetFocus(client) == client)
633 SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
636 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
637 return TRUE;
640 /* -------------------- MDI client window functions ------------------- */
642 /**********************************************************************
643 * CreateMDIMenuBitmap
645 static HBITMAP CreateMDIMenuBitmap(void)
647 HDC hDCSrc = CreateCompatibleDC(0);
648 HDC hDCDest = CreateCompatibleDC(hDCSrc);
649 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
650 HBITMAP hbCopy;
651 HBITMAP hobjSrc, hobjDest;
653 hobjSrc = SelectObject(hDCSrc, hbClose);
654 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
655 hobjDest = SelectObject(hDCDest, hbCopy);
657 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
658 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
660 SelectObject(hDCSrc, hobjSrc);
661 DeleteObject(hbClose);
662 DeleteDC(hDCSrc);
664 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
666 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
667 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
669 SelectObject(hDCDest, hobjSrc );
670 SelectObject(hDCDest, hobjDest);
671 DeleteDC(hDCDest);
673 return hbCopy;
676 /**********************************************************************
677 * MDICascade
679 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
681 HWND *win_array;
682 BOOL has_icons = FALSE;
683 int i, total;
685 if (ci->hwndChildMaximized)
686 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
688 if (ci->nActiveChildren == 0) return 0;
690 if (!(win_array = WIN_ListChildren( client ))) return 0;
692 /* remove all the windows we don't want */
693 for (i = total = 0; win_array[i]; i++)
695 if (!IsWindowVisible( win_array[i] )) continue;
696 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
697 if (IsIconic( win_array[i] ))
699 has_icons = TRUE;
700 continue;
702 win_array[total++] = win_array[i];
704 win_array[total] = 0;
706 if (total)
708 INT delta = 0, n = 0, i;
709 POINT pos[2];
710 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
712 /* walk the list (backwards) and move windows */
713 for (i = total - 1; i >= 0; i--)
715 LONG style;
716 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
718 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
719 TRACE("move %p to (%d,%d) size [%d,%d]\n",
720 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
721 style = GetWindowLongW(win_array[i], GWL_STYLE);
723 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
724 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
725 posOptions);
728 HeapFree( GetProcessHeap(), 0, win_array );
730 if (has_icons) ArrangeIconicWindows( client );
731 return 0;
734 /**********************************************************************
735 * MDITile
737 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
739 HWND *win_array;
740 int i, total;
741 BOOL has_icons = FALSE;
743 if (ci->hwndChildMaximized)
744 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
746 if (ci->nActiveChildren == 0) return;
748 if (!(win_array = WIN_ListChildren( client ))) return;
750 /* remove all the windows we don't want */
751 for (i = total = 0; win_array[i]; i++)
753 if (!IsWindowVisible( win_array[i] )) continue;
754 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
755 if (IsIconic( win_array[i] ))
757 has_icons = TRUE;
758 continue;
760 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
761 win_array[total++] = win_array[i];
763 win_array[total] = 0;
765 TRACE("%u windows to tile\n", total);
767 if (total)
769 HWND *pWnd = win_array;
770 RECT rect;
771 int x, y, xsize, ysize;
772 int rows, columns, r, c, i;
774 GetClientRect(client,&rect);
775 rows = (int) sqrt((double)total);
776 columns = total / rows;
778 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
780 i = rows;
781 rows = columns; /* exchange r and c */
782 columns = i;
785 if (has_icons)
787 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
788 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
791 ysize = rect.bottom / rows;
792 xsize = rect.right / columns;
794 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
796 if (c == columns)
798 rows = total - i;
799 ysize = rect.bottom / rows;
802 y = 0;
803 for (r = 1; r <= rows && *pWnd; r++, i++)
805 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
806 LONG style = GetWindowLongW(win_array[i], GWL_STYLE);
807 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
809 SetWindowPos(*pWnd, 0, x, y, xsize, ysize, posOptions);
810 y += ysize;
811 pWnd++;
813 x += xsize;
816 HeapFree( GetProcessHeap(), 0, win_array );
817 if (has_icons) ArrangeIconicWindows( client );
820 /* ----------------------- Frame window ---------------------------- */
823 /**********************************************************************
824 * MDI_AugmentFrameMenu
826 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
828 HMENU menu = GetMenu( frame );
829 HMENU hSysPopup;
830 HBITMAP hSysMenuBitmap = 0;
831 HICON hIcon;
833 TRACE("frame %p,child %p\n",frame,hChild);
835 if (!menu) return FALSE;
837 /* create a copy of sysmenu popup and insert it into frame menu bar */
838 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
840 TRACE("child %p doesn't have a system menu\n", hChild);
841 return FALSE;
844 AppendMenuW(menu, MF_HELP | MF_BITMAP,
845 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
846 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
847 AppendMenuW(menu, MF_HELP | MF_BITMAP,
848 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
849 AppendMenuW(menu, MF_HELP | MF_BITMAP,
850 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
852 /* The system menu is replaced by the child icon */
853 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_SMALL, 0);
854 if (!hIcon)
855 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICONSM);
856 if (!hIcon)
857 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_BIG, 0);
858 if (!hIcon)
859 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICON);
860 if (!hIcon)
861 hIcon = LoadImageW(0, (LPWSTR)IDI_WINLOGO, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
862 GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
863 if (hIcon)
865 HDC hMemDC;
866 HBITMAP hBitmap, hOldBitmap;
867 HDC hdc = GetDC(hChild);
869 if (hdc)
871 int cx, cy;
872 cx = GetSystemMetrics(SM_CXSMICON);
873 cy = GetSystemMetrics(SM_CYSMICON);
874 hMemDC = CreateCompatibleDC(hdc);
875 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
876 hOldBitmap = SelectObject(hMemDC, hBitmap);
877 SetMapMode(hMemDC, MM_TEXT);
878 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, GetSysColorBrush(COLOR_MENU), DI_NORMAL);
879 SelectObject (hMemDC, hOldBitmap);
880 DeleteDC(hMemDC);
881 ReleaseDC(hChild, hdc);
882 hSysMenuBitmap = hBitmap;
886 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
887 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
889 TRACE("not inserted\n");
890 DestroyMenu(hSysPopup);
891 return FALSE;
894 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
895 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
896 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
897 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
899 /* redraw menu */
900 DrawMenuBar(frame);
902 return TRUE;
905 /**********************************************************************
906 * MDI_RestoreFrameMenu
908 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
910 MENUITEMINFOW menuInfo;
911 HMENU menu = GetMenu( frame );
912 INT nItems;
913 UINT iId;
915 TRACE("frame %p, child %p\n", frame, hChild);
917 if (!menu) return FALSE;
919 /* if there is no system buttons then nothing to do */
920 nItems = GetMenuItemCount(menu) - 1;
921 iId = GetMenuItemID(menu, nItems);
922 if ( !(iId == SC_RESTORE || iId == SC_CLOSE) )
923 return FALSE;
926 * Remove the system menu, If that menu is the icon of the window
927 * as it is in win95, we have to delete the bitmap.
929 memset(&menuInfo, 0, sizeof(menuInfo));
930 menuInfo.cbSize = sizeof(menuInfo);
931 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
933 GetMenuItemInfoW(menu,
935 TRUE,
936 &menuInfo);
938 RemoveMenu(menu,0,MF_BYPOSITION);
940 if ( (menuInfo.fType & MFT_BITMAP) &&
941 (LOWORD(menuInfo.dwTypeData)!=0) &&
942 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
944 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
947 /* close */
948 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
949 /* restore */
950 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
951 /* minimize */
952 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
954 DrawMenuBar(frame);
956 return TRUE;
960 /**********************************************************************
961 * MDI_UpdateFrameText
963 * used when child window is maximized/restored
965 * Note: lpTitle can be NULL
967 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
969 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
970 MDICLIENTINFO *ci = get_client_info( hClient );
972 TRACE("frameText %s\n", debugstr_w(lpTitle));
974 if (!ci) return;
976 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
978 GetWindowTextW( frame, lpBuffer, ARRAY_SIZE( lpBuffer ));
979 lpTitle = lpBuffer;
982 /* store new "default" title if lpTitle is not NULL */
983 if (lpTitle)
985 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
986 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(lpTitle)+1)*sizeof(WCHAR))))
987 lstrcpyW( ci->frameTitle, lpTitle );
990 if (ci->frameTitle)
992 if (ci->hwndChildMaximized)
994 /* combine frame title and child title if possible */
995 int i_frame_text_length = lstrlenW(ci->frameTitle);
997 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
999 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1001 lstrcatW( lpBuffer, L" - [" );
1002 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
1003 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1004 lstrcatW( lpBuffer, L"]" );
1005 else
1006 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1009 else
1011 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1014 else
1015 lpBuffer[0] = '\0';
1017 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1019 if (repaint)
1020 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1021 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1025 /* ----------------------------- Interface ---------------------------- */
1028 /**********************************************************************
1029 * MDIClientWndProc_common
1031 LRESULT MDIClientWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
1033 MDICLIENTINFO *ci;
1035 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1037 if (!(ci = get_client_info( hwnd )))
1039 if (message == WM_NCCREATE) win_set_flags( hwnd, WIN_ISMDICLIENT, 0 );
1040 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1041 DefWindowProcA( hwnd, message, wParam, lParam );
1044 switch (message)
1046 case WM_CREATE:
1048 /* Since we are using only cs->lpCreateParams, we can safely
1049 * cast to LPCREATESTRUCTA here */
1050 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1051 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1053 ci->hWindowMenu = ccs->hWindowMenu;
1054 ci->idFirstChild = ccs->idFirstChild;
1055 ci->hwndChildMaximized = 0;
1056 ci->child = NULL;
1057 ci->nActiveChildren = 0;
1058 ci->nTotalCreated = 0;
1059 ci->frameTitle = NULL;
1060 ci->mdiFlags = 0;
1061 ci->hFrameMenu = GetMenu(cs->hwndParent);
1063 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1065 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1066 hwnd, ci->hWindowMenu, ci->idFirstChild );
1067 return 0;
1070 case WM_DESTROY:
1072 if( ci->hwndChildMaximized )
1073 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1075 ci->nActiveChildren = 0;
1076 MDI_RefreshMenu(ci);
1078 HeapFree( GetProcessHeap(), 0, ci->child );
1079 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1081 return 0;
1084 case WM_MDIACTIVATE:
1086 if( ci->hwndActiveChild != (HWND)wParam )
1087 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1088 return 0;
1091 case WM_MDICASCADE:
1092 return MDICascade(hwnd, ci);
1094 case WM_MDICREATE:
1095 if (lParam)
1097 HWND child;
1099 if (unicode)
1101 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1102 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1103 csW->szTitle, csW->style,
1104 csW->x, csW->y, csW->cx, csW->cy,
1105 hwnd, 0, csW->hOwner,
1106 (LPVOID)csW->lParam);
1108 else
1110 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1111 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1112 csA->szTitle, csA->style,
1113 csA->x, csA->y, csA->cx, csA->cy,
1114 hwnd, 0, csA->hOwner,
1115 (LPVOID)csA->lParam);
1117 return (LRESULT)child;
1119 return 0;
1121 case WM_MDIDESTROY:
1122 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1124 case WM_MDIGETACTIVE:
1125 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1126 return (LRESULT)ci->hwndActiveChild;
1128 case WM_MDIICONARRANGE:
1129 ci->mdiFlags |= MDIF_NEEDUPDATE;
1130 ArrangeIconicWindows( hwnd );
1131 ci->sbRecalc = SB_BOTH+1;
1132 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1133 return 0;
1135 case WM_MDIMAXIMIZE:
1136 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1137 return 0;
1139 case WM_MDINEXT: /* lParam != 0 means previous window */
1141 HWND hwnd = wParam ? WIN_GetFullHandle((HWND)wParam) : ci->hwndActiveChild;
1142 HWND next = MDI_GetWindow( ci, hwnd, !lParam, 0 );
1143 MDI_SwitchActiveChild( ci, next, TRUE );
1144 if(!lParam)
1145 SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
1146 break;
1149 case WM_MDIRESTORE:
1150 ShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1151 return 0;
1153 case WM_MDISETMENU:
1154 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1156 case WM_MDIREFRESHMENU:
1157 return MDI_RefreshMenu( ci );
1159 case WM_MDITILE:
1160 ci->mdiFlags |= MDIF_NEEDUPDATE;
1161 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1162 MDITile( hwnd, ci, wParam );
1163 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1164 return 0;
1166 case WM_VSCROLL:
1167 case WM_HSCROLL:
1168 ci->mdiFlags |= MDIF_NEEDUPDATE;
1169 ScrollChildren( hwnd, message, wParam, lParam );
1170 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1171 return 0;
1173 case WM_SETFOCUS:
1174 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1175 SetFocus( ci->hwndActiveChild );
1176 return 0;
1178 case WM_NCACTIVATE:
1179 if( ci->hwndActiveChild )
1180 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1181 break;
1183 case WM_PARENTNOTIFY:
1184 switch (LOWORD(wParam))
1186 case WM_CREATE:
1187 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1189 ci->nTotalCreated++;
1190 ci->nActiveChildren++;
1192 if (!ci->child)
1193 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1194 else
1195 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1197 TRACE("Adding MDI child %p, # of children %d\n",
1198 (HWND)lParam, ci->nActiveChildren);
1200 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1202 break;
1204 case WM_LBUTTONDOWN:
1206 HWND child;
1207 POINT pt;
1208 pt.x = (short)LOWORD(lParam);
1209 pt.y = (short)HIWORD(lParam);
1210 child = ChildWindowFromPoint(hwnd, pt);
1212 TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1214 if( child && child != hwnd && child != ci->hwndActiveChild )
1215 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1216 break;
1219 case WM_DESTROY:
1220 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1222 return 0;
1224 case WM_SIZE:
1225 if( ci->hwndActiveChild && IsZoomed(ci->hwndActiveChild) )
1227 RECT rect;
1229 SetRect(&rect, 0, 0, LOWORD(lParam), HIWORD(lParam));
1230 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1231 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1232 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1233 rect.right - rect.left, rect.bottom - rect.top, 1);
1235 else
1236 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1238 break;
1240 case WM_MDICALCCHILDSCROLL:
1241 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1243 CalcChildScroll(hwnd, ci->sbRecalc-1);
1244 ci->sbRecalc = 0;
1245 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1247 return 0;
1249 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1250 DefWindowProcA( hwnd, message, wParam, lParam );
1253 /***********************************************************************
1254 * DefFrameProcA (USER32.@)
1256 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1257 UINT message, WPARAM wParam, LPARAM lParam)
1259 if (hwndMDIClient)
1261 switch (message)
1263 case WM_SETTEXT:
1265 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1266 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1267 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1268 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1269 HeapFree( GetProcessHeap(), 0, text );
1271 return 1; /* success. FIXME: check text length */
1273 case WM_COMMAND:
1274 case WM_NCACTIVATE:
1275 case WM_NEXTMENU:
1276 case WM_SETFOCUS:
1277 case WM_SIZE:
1278 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1281 return DefWindowProcA(hwnd, message, wParam, lParam);
1285 /***********************************************************************
1286 * DefFrameProcW (USER32.@)
1288 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1289 UINT message, WPARAM wParam, LPARAM lParam)
1291 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1293 TRACE("%p %p %04x (%s) %08lx %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1295 if (ci)
1297 switch (message)
1299 case WM_COMMAND:
1301 WORD id = LOWORD(wParam);
1302 /* check for possible syscommands for maximized MDI child */
1303 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1305 if( (id - 0xf000) & 0xf00f ) break;
1306 if( !ci->hwndChildMaximized ) break;
1307 switch( id )
1309 case SC_CLOSE:
1310 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1311 case SC_SIZE:
1312 case SC_MOVE:
1313 case SC_MINIMIZE:
1314 case SC_MAXIMIZE:
1315 case SC_NEXTWINDOW:
1316 case SC_PREVWINDOW:
1317 case SC_RESTORE:
1318 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1319 wParam, lParam);
1322 else
1324 HWND childHwnd;
1325 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1326 /* User chose "More Windows..." */
1327 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1328 else
1329 /* User chose one of the windows listed in the "Windows" menu */
1330 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1332 if( childHwnd )
1333 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1336 break;
1338 case WM_NCACTIVATE:
1339 SendMessageW(hwndMDIClient, message, wParam, lParam);
1340 break;
1342 case WM_SETTEXT:
1343 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1344 return 1; /* success. FIXME: check text length */
1346 case WM_SETFOCUS:
1347 SetFocus(hwndMDIClient);
1348 break;
1350 case WM_SIZE:
1351 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1352 break;
1354 case WM_NEXTMENU:
1356 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1358 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1360 /* control menu is between the frame system menu and
1361 * the first entry of menu bar */
1362 WND *wndPtr = WIN_GetPtr(hwnd);
1364 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1365 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1367 WIN_ReleasePtr(wndPtr);
1368 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1369 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1370 next_menu->hwndNext = ci->hwndActiveChild;
1372 WIN_ReleasePtr(wndPtr);
1374 return 0;
1379 return DefWindowProcW( hwnd, message, wParam, lParam );
1382 /***********************************************************************
1383 * DefMDIChildProcA (USER32.@)
1385 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1386 WPARAM wParam, LPARAM lParam )
1388 HWND client = GetParent(hwnd);
1389 MDICLIENTINFO *ci = get_client_info( client );
1391 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1393 hwnd = WIN_GetFullHandle( hwnd );
1394 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1396 switch (message)
1398 case WM_SETTEXT:
1399 DefWindowProcA(hwnd, message, wParam, lParam);
1400 if( ci->hwndChildMaximized == hwnd )
1401 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1402 return 1; /* success. FIXME: check text length */
1404 case WM_GETMINMAXINFO:
1405 case WM_MENUCHAR:
1406 case WM_CLOSE:
1407 case WM_SETFOCUS:
1408 case WM_CHILDACTIVATE:
1409 case WM_SYSCOMMAND:
1410 case WM_SHOWWINDOW:
1411 case WM_SETVISIBLE:
1412 case WM_SIZE:
1413 case WM_NEXTMENU:
1414 case WM_SYSCHAR:
1415 case WM_DESTROY:
1416 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1418 return DefWindowProcA(hwnd, message, wParam, lParam);
1422 /***********************************************************************
1423 * DefMDIChildProcW (USER32.@)
1425 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1426 WPARAM wParam, LPARAM lParam )
1428 HWND client = GetParent(hwnd);
1429 MDICLIENTINFO *ci = get_client_info( client );
1431 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1433 hwnd = WIN_GetFullHandle( hwnd );
1434 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1436 switch (message)
1438 case WM_SETTEXT:
1439 DefWindowProcW(hwnd, message, wParam, lParam);
1440 if( ci->hwndChildMaximized == hwnd )
1441 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1442 return 1; /* success. FIXME: check text length */
1444 case WM_GETMINMAXINFO:
1445 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1446 return 0;
1448 case WM_MENUCHAR:
1449 return MAKELRESULT( 0, MNC_CLOSE ); /* MDI children don't have menu bars */
1451 case WM_CLOSE:
1452 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1453 return 0;
1455 case WM_SETFOCUS:
1456 if (ci->hwndActiveChild != hwnd)
1457 MDI_ChildActivate( client, hwnd );
1458 break;
1460 case WM_CHILDACTIVATE:
1461 if (IsWindowEnabled( hwnd ))
1462 MDI_ChildActivate( client, hwnd );
1463 return 0;
1465 case WM_SYSCOMMAND:
1466 switch (wParam & 0xfff0)
1468 case SC_MOVE:
1469 if( ci->hwndChildMaximized == hwnd )
1470 return 0;
1471 break;
1472 case SC_RESTORE:
1473 case SC_MINIMIZE:
1474 break;
1475 case SC_MAXIMIZE:
1476 if (ci->hwndChildMaximized == hwnd)
1477 return SendMessageW( GetParent(client), message, wParam, lParam);
1478 break;
1479 case SC_NEXTWINDOW:
1480 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 0);
1481 return 0;
1482 case SC_PREVWINDOW:
1483 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 1);
1484 return 0;
1486 break;
1488 case WM_SHOWWINDOW:
1489 case WM_SETVISIBLE:
1490 if (ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1491 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1492 break;
1494 case WM_SIZE:
1495 /* This is the only place where we switch to/from maximized state */
1496 /* do not change */
1497 TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1499 if( ci->hwndChildMaximized == hwnd && wParam != SIZE_MAXIMIZED )
1501 HWND frame;
1503 ci->hwndChildMaximized = 0;
1505 frame = GetParent(client);
1506 MDI_RestoreFrameMenu( frame, hwnd );
1507 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1510 if( wParam == SIZE_MAXIMIZED )
1512 HWND frame, hMaxChild = ci->hwndChildMaximized;
1514 if( hMaxChild == hwnd ) break;
1516 if( hMaxChild)
1518 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1520 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1521 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1523 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1526 TRACE("maximizing child %p\n", hwnd );
1528 /* keep track of the maximized window. */
1529 ci->hwndChildMaximized = hwnd; /* !!! */
1531 frame = GetParent(client);
1532 MDI_AugmentFrameMenu( frame, hwnd );
1533 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1536 if( wParam == SIZE_MINIMIZED )
1538 HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1540 if (!switchTo) switchTo = hwnd;
1541 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1544 MDI_PostUpdate(client, ci, SB_BOTH+1);
1545 break;
1547 case WM_NEXTMENU:
1549 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1550 HWND parent = GetParent(client);
1552 if( wParam == VK_LEFT ) /* switch to frame system menu */
1554 WND *wndPtr = WIN_GetPtr( parent );
1555 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1556 WIN_ReleasePtr( wndPtr );
1558 if( wParam == VK_RIGHT ) /* to frame menu bar */
1560 next_menu->hmenuNext = GetMenu(parent);
1562 next_menu->hwndNext = parent;
1563 return 0;
1566 case WM_SYSCHAR:
1567 if (wParam == '-')
1569 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, VK_SPACE);
1570 return 0;
1572 break;
1574 case WM_DESTROY:
1575 /* Remove itself from the Window menu */
1576 MDI_RefreshMenu(ci);
1577 break;
1579 return DefWindowProcW(hwnd, message, wParam, lParam);
1582 /**********************************************************************
1583 * CreateMDIWindowA (USER32.@) Creates a MDI child
1585 * RETURNS
1586 * Success: Handle to created window
1587 * Failure: NULL
1589 HWND WINAPI CreateMDIWindowA(
1590 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1591 LPCSTR lpWindowName, /* [in] Pointer to window name */
1592 DWORD dwStyle, /* [in] Window style */
1593 INT X, /* [in] Horizontal position of window */
1594 INT Y, /* [in] Vertical position of window */
1595 INT nWidth, /* [in] Width of window */
1596 INT nHeight, /* [in] Height of window */
1597 HWND hWndParent, /* [in] Handle to parent window */
1598 HINSTANCE hInstance, /* [in] Handle to application instance */
1599 LPARAM lParam) /* [in] Application-defined value */
1601 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1602 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1603 nWidth,nHeight,hWndParent,hInstance,lParam);
1605 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1606 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1607 0, hInstance, (LPVOID)lParam);
1610 /***********************************************************************
1611 * CreateMDIWindowW (USER32.@) Creates a MDI child
1613 * RETURNS
1614 * Success: Handle to created window
1615 * Failure: NULL
1617 HWND WINAPI CreateMDIWindowW(
1618 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1619 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1620 DWORD dwStyle, /* [in] Window style */
1621 INT X, /* [in] Horizontal position of window */
1622 INT Y, /* [in] Vertical position of window */
1623 INT nWidth, /* [in] Width of window */
1624 INT nHeight, /* [in] Height of window */
1625 HWND hWndParent, /* [in] Handle to parent window */
1626 HINSTANCE hInstance, /* [in] Handle to application instance */
1627 LPARAM lParam) /* [in] Application-defined value */
1629 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1630 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1631 nWidth, nHeight, hWndParent, hInstance, lParam);
1633 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1634 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1635 0, hInstance, (LPVOID)lParam);
1638 /**********************************************************************
1639 * TranslateMDISysAccel (USER32.@)
1641 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1643 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1645 MDICLIENTINFO *ci = get_client_info( hwndClient );
1646 WPARAM wParam = 0;
1648 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return FALSE;
1650 /* translate if the Ctrl key is down and Alt not. */
1652 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1654 switch( msg->wParam )
1656 case VK_F6:
1657 case VK_TAB:
1658 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1659 break;
1660 case VK_F4:
1661 case VK_RBUTTON:
1662 if (is_close_enabled(ci->hwndActiveChild, 0))
1664 wParam = SC_CLOSE;
1665 break;
1667 /* fall through */
1668 default:
1669 return FALSE;
1671 TRACE("wParam = %04lx\n", wParam);
1672 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, msg->wParam);
1673 return TRUE;
1676 return FALSE; /* failure */
1679 /***********************************************************************
1680 * CalcChildScroll (USER32.@)
1682 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1684 DPI_AWARENESS_CONTEXT context;
1685 SCROLLINFO info;
1686 RECT childRect, clientRect;
1687 HWND *list;
1688 DWORD style;
1690 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1692 GetClientRect( hwnd, &clientRect );
1693 SetRectEmpty( &childRect );
1695 if ((list = WIN_ListChildren( hwnd )))
1697 int i;
1698 for (i = 0; list[i]; i++)
1700 style = GetWindowLongW( list[i], GWL_STYLE );
1701 if (style & WS_MAXIMIZE)
1703 HeapFree( GetProcessHeap(), 0, list );
1704 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1705 return;
1707 if (style & WS_VISIBLE)
1709 RECT rect;
1710 WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL );
1711 UnionRect( &childRect, &rect, &childRect );
1714 HeapFree( GetProcessHeap(), 0, list );
1716 UnionRect( &childRect, &clientRect, &childRect );
1718 /* set common info values */
1719 info.cbSize = sizeof(info);
1720 info.fMask = SIF_POS | SIF_RANGE;
1722 /* set the specific values and apply but only if window style allows */
1723 style = GetWindowLongW( hwnd, GWL_STYLE );
1724 switch( scroll )
1726 case SB_BOTH:
1727 case SB_HORZ:
1728 if (style & (WS_HSCROLL | WS_VSCROLL))
1730 info.nMin = childRect.left;
1731 info.nMax = childRect.right - clientRect.right;
1732 info.nPos = clientRect.left - childRect.left;
1733 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1735 if (scroll == SB_HORZ) break;
1736 /* fall through */
1737 case SB_VERT:
1738 if (style & (WS_HSCROLL | WS_VSCROLL))
1740 info.nMin = childRect.top;
1741 info.nMax = childRect.bottom - clientRect.bottom;
1742 info.nPos = clientRect.top - childRect.top;
1743 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1745 break;
1747 SetThreadDpiAwarenessContext( context );
1751 /***********************************************************************
1752 * ScrollChildren (USER32.@)
1754 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1755 LPARAM lParam)
1757 DPI_AWARENESS_CONTEXT context;
1758 INT newPos = -1;
1759 INT curPos, length, minPos, maxPos, shift;
1760 RECT rect;
1762 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hWnd ));
1763 GetClientRect( hWnd, &rect );
1765 switch(uMsg)
1767 case WM_HSCROLL:
1768 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1769 curPos = GetScrollPos(hWnd,SB_HORZ);
1770 length = (rect.right - rect.left) / 2;
1771 shift = GetSystemMetrics(SM_CYHSCROLL);
1772 break;
1773 case WM_VSCROLL:
1774 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1775 curPos = GetScrollPos(hWnd,SB_VERT);
1776 length = (rect.bottom - rect.top) / 2;
1777 shift = GetSystemMetrics(SM_CXVSCROLL);
1778 break;
1779 default:
1780 goto done;
1783 switch( wParam )
1785 case SB_LINEUP:
1786 newPos = curPos - shift;
1787 break;
1788 case SB_LINEDOWN:
1789 newPos = curPos + shift;
1790 break;
1791 case SB_PAGEUP:
1792 newPos = curPos - length;
1793 break;
1794 case SB_PAGEDOWN:
1795 newPos = curPos + length;
1796 break;
1798 case SB_THUMBPOSITION:
1799 newPos = LOWORD(lParam);
1800 break;
1802 case SB_THUMBTRACK:
1803 goto done;
1805 case SB_TOP:
1806 newPos = minPos;
1807 break;
1808 case SB_BOTTOM:
1809 newPos = maxPos;
1810 break;
1811 case SB_ENDSCROLL:
1812 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1813 goto done;
1816 if( newPos > maxPos )
1817 newPos = maxPos;
1818 else
1819 if( newPos < minPos )
1820 newPos = minPos;
1822 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1824 if( uMsg == WM_VSCROLL )
1825 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1826 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1827 else
1828 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1829 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1830 done:
1831 SetThreadDpiAwarenessContext( context );
1835 /******************************************************************************
1836 * CascadeWindows (USER32.@) Cascades MDI child windows
1838 * RETURNS
1839 * Success: Number of cascaded windows.
1840 * Failure: 0
1842 WORD WINAPI
1843 CascadeWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1844 UINT cKids, const HWND *lpKids)
1846 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1847 return 0;
1851 /***********************************************************************
1852 * CascadeChildWindows (USER32.@)
1854 WORD WINAPI CascadeChildWindows( HWND parent, UINT flags )
1856 return CascadeWindows( parent, flags, NULL, 0, NULL );
1860 /******************************************************************************
1861 * TileWindows (USER32.@) Tiles MDI child windows
1863 * RETURNS
1864 * Success: Number of tiled windows.
1865 * Failure: 0
1867 WORD WINAPI
1868 TileWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1869 UINT cKids, const HWND *lpKids)
1871 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1872 return 0;
1876 /***********************************************************************
1877 * TileChildWindows (USER32.@)
1879 WORD WINAPI TileChildWindows( HWND parent, UINT flags )
1881 return TileWindows( parent, flags, NULL, 0, NULL );
1885 /************************************************************************
1886 * "More Windows..." functionality
1889 /* MDI_MoreWindowsDlgProc
1891 * This function will process the messages sent to the "More Windows..."
1892 * dialog.
1893 * Return values: 0 = cancel pressed
1894 * HWND = ok pressed or double-click in the list...
1898 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1900 switch (iMsg)
1902 case WM_INITDIALOG:
1904 UINT widest = 0;
1905 UINT length;
1906 UINT i;
1907 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1908 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1910 for (i = 0; i < ci->nActiveChildren; i++)
1912 WCHAR buffer[MDI_MAXTITLELENGTH];
1914 if (!InternalGetWindowText(ci->child[i], buffer, ARRAY_SIZE(buffer)))
1915 continue;
1916 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1917 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1918 length = lstrlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1919 if (length > widest)
1920 widest = length;
1922 /* Make sure the horizontal scrollbar scrolls ok */
1923 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1925 /* Set the current selection */
1926 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1927 return TRUE;
1930 case WM_COMMAND:
1931 switch (LOWORD(wParam))
1933 default:
1934 if (HIWORD(wParam) != LBN_DBLCLK) break;
1935 /* fall through */
1936 case IDOK:
1938 /* windows are sorted by menu ID, so we must return the
1939 * window associated to the given id
1941 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1942 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1943 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1944 EndDialog(hDlg, res);
1945 return TRUE;
1947 case IDCANCEL:
1948 EndDialog(hDlg, 0);
1949 return TRUE;
1951 break;
1953 return FALSE;
1958 * MDI_MoreWindowsDialog
1960 * Prompts the user with a listbox containing the opened
1961 * documents. The user can then choose a windows and click
1962 * on OK to set the current window to the one selected, or
1963 * CANCEL to cancel. The function returns a handle to the
1964 * selected window.
1967 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1969 LPCVOID template;
1970 HRSRC hRes;
1971 HANDLE hDlgTmpl;
1973 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1975 if (hRes == 0)
1976 return 0;
1978 hDlgTmpl = LoadResource(user32_module, hRes );
1980 if (hDlgTmpl == 0)
1981 return 0;
1983 template = LockResource( hDlgTmpl );
1985 if (template == 0)
1986 return 0;
1988 return (HWND) DialogBoxIndirectParamA(user32_module, template, hwnd,
1989 MDI_MoreWindowsDlgProc, (LPARAM) hwnd);