comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / user32 / mdi.c
blob665610d73ca38308d26f097e22c3fa4180e4ba91
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 "wingdi.h"
94 #include "winuser.h"
95 #include "wownt32.h"
96 #include "wine/unicode.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 static const WCHAR mdiclientW[] = {'M','D','I','C','l','i','e','n','t',0};
186 const struct builtin_class_descr MDICLIENT_builtin_class =
188 mdiclientW, /* name */
189 0, /* style */
190 WINPROC_MDICLIENT, /* proc */
191 sizeof(MDICLIENTINFO), /* extra */
192 IDC_ARROW, /* cursor */
193 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
197 static MDICLIENTINFO *get_client_info( HWND client )
199 MDICLIENTINFO *ret = NULL;
200 WND *win = WIN_GetPtr( client );
201 if (win)
203 if (win == WND_OTHER_PROCESS || win == WND_DESKTOP)
205 if (IsWindow(client)) WARN( "client %p belongs to other process\n", client );
206 return NULL;
208 if (win->flags & WIN_ISMDICLIENT)
209 ret = (MDICLIENTINFO *)win->wExtra;
210 else
211 WARN( "%p is not an MDI client\n", client );
212 WIN_ReleasePtr( win );
214 return ret;
217 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
219 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
221 if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
222 if (hSysMenu)
224 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
225 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
226 return FALSE;
228 return TRUE;
231 /**********************************************************************
232 * MDI_GetWindow
234 * returns "activatable" child different from the current or zero
236 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
237 DWORD dwStyleMask )
239 int i;
240 HWND *list;
241 HWND last = 0;
243 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
244 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
246 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
247 i = 0;
248 /* start from next after hWnd */
249 while (list[i] && list[i] != hWnd) i++;
250 if (list[i]) i++;
252 for ( ; list[i]; i++)
254 if (GetWindow( list[i], GW_OWNER )) continue;
255 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
256 last = list[i];
257 if (bNext) goto found;
259 /* now restart from the beginning */
260 for (i = 0; list[i] && list[i] != hWnd; i++)
262 if (GetWindow( list[i], GW_OWNER )) continue;
263 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
264 last = list[i];
265 if (bNext) goto found;
267 found:
268 HeapFree( GetProcessHeap(), 0, list );
269 return last;
272 /**********************************************************************
273 * MDI_CalcDefaultChildPos
275 * It seems that the default height is about 2/3 of the client rect
277 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
279 INT nstagger;
280 RECT rect;
281 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
283 if (total < 0) /* we are called from CreateWindow */
285 MDICLIENTINFO *ci = get_client_info(hwndClient);
286 total = ci->nTotalCreated;
287 *id = ci->idFirstChild + ci->nActiveChildren;
288 TRACE("MDI child id %04x\n", *id);
291 GetClientRect( hwndClient, &rect );
292 if( rect.bottom - rect.top - delta >= spacing )
293 rect.bottom -= delta;
295 nstagger = (rect.bottom - rect.top)/(3 * spacing);
296 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
297 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
298 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
301 /**********************************************************************
302 * MDISetMenu
304 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
305 HMENU hmenuWindow)
307 MDICLIENTINFO *ci;
308 HWND hwndFrame = GetParent(hwnd);
310 TRACE("%p, frame menu %p, window menu %p\n", hwnd, hmenuFrame, hmenuWindow);
312 if (hmenuFrame && !IsMenu(hmenuFrame))
314 WARN("hmenuFrame is not a menu handle\n");
315 return 0L;
318 if (hmenuWindow && !IsMenu(hmenuWindow))
320 WARN("hmenuWindow is not a menu handle\n");
321 return 0L;
324 if (!(ci = get_client_info( hwnd ))) return 0;
326 TRACE("old frame menu %p, old window menu %p\n", ci->hFrameMenu, ci->hWindowMenu);
328 if (hmenuFrame)
330 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
332 if (ci->hwndChildMaximized)
333 MDI_RestoreFrameMenu( hwndFrame, ci->hwndChildMaximized );
336 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
338 /* delete menu items from ci->hWindowMenu
339 * and add them to hmenuWindow */
340 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
341 if( ci->hWindowMenu && ci->nActiveChildren )
343 UINT nActiveChildren_old = ci->nActiveChildren;
345 /* Remove all items from old Window menu */
346 ci->nActiveChildren = 0;
347 MDI_RefreshMenu(ci);
349 ci->hWindowMenu = hmenuWindow;
351 /* Add items to the new Window menu */
352 ci->nActiveChildren = nActiveChildren_old;
353 MDI_RefreshMenu(ci);
355 else
356 ci->hWindowMenu = hmenuWindow;
359 if (hmenuFrame)
361 SetMenu(hwndFrame, hmenuFrame);
362 if( hmenuFrame != ci->hFrameMenu )
364 HMENU oldFrameMenu = ci->hFrameMenu;
366 ci->hFrameMenu = hmenuFrame;
367 if (ci->hwndChildMaximized)
368 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
370 return (LRESULT)oldFrameMenu;
373 else
375 /* SetMenu() may already have been called, meaning that this window
376 * already has its menu. But they may have done a SetMenu() on
377 * an MDI window, and called MDISetMenu() after the fact, meaning
378 * that the "if" to this "else" wouldn't catch the need to
379 * augment the frame menu.
381 if( ci->hwndChildMaximized )
382 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
385 return 0;
388 /**********************************************************************
389 * MDIRefreshMenu
391 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
393 UINT i, count, visible, id;
394 WCHAR buf[MDI_MAXTITLELENGTH];
396 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
398 if (!ci->hWindowMenu)
399 return 0;
401 if (!IsMenu(ci->hWindowMenu))
403 WARN("Window menu handle %p is no longer valid\n", ci->hWindowMenu);
404 return 0;
407 /* Windows finds the last separator in the menu, and if after it
408 * there is a menu item with MDI magic ID removes all existing
409 * menu items after it, and then adds visible MDI children.
411 count = GetMenuItemCount(ci->hWindowMenu);
412 for (i = 0; i < count; i++)
414 MENUITEMINFOW mii;
416 memset(&mii, 0, sizeof(mii));
417 mii.cbSize = sizeof(mii);
418 mii.fMask = MIIM_TYPE;
419 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
421 if (mii.fType & MF_SEPARATOR)
423 /* Windows checks only ID of the menu item */
424 memset(&mii, 0, sizeof(mii));
425 mii.cbSize = sizeof(mii);
426 mii.fMask = MIIM_ID;
427 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
429 if (mii.wID == ci->idFirstChild)
431 TRACE("removing %u items including separator\n", count - i);
432 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
433 /* nothing */;
435 break;
442 visible = 0;
443 for (i = 0; i < ci->nActiveChildren; i++)
445 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
447 id = ci->idFirstChild + visible;
449 if (visible == MDI_MOREWINDOWSLIMIT)
451 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
452 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
453 break;
456 if (!visible)
457 /* Visio expects that separator has id 0 */
458 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
460 visible++;
462 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
464 buf[0] = '&';
465 buf[1] = '0' + visible;
466 buf[2] = ' ';
467 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
468 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
469 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
471 if (ci->child[i] == ci->hwndActiveChild)
472 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
474 else
475 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
478 return (LRESULT)ci->hFrameMenu;
482 /* ------------------ MDI child window functions ---------------------- */
484 /**********************************************************************
485 * MDI_ChildGetMinMaxInfo
487 * Note: The rule here is that client rect of the maximized MDI child
488 * is equal to the client rect of the MDI client window.
490 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
492 RECT rect;
494 GetClientRect( client, &rect );
495 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
496 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
498 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
499 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
501 lpMinMax->ptMaxPosition.x = rect.left;
502 lpMinMax->ptMaxPosition.y = rect.top;
504 TRACE("max rect %s\n", wine_dbgstr_rect(&rect));
507 /**********************************************************************
508 * MDI_SwitchActiveChild
510 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
511 * being activated
513 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
515 HWND hwndPrev;
517 hwndPrev = ci->hwndActiveChild;
519 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
521 if ( hwndTo != hwndPrev )
523 BOOL was_zoomed = IsZoomed(hwndPrev);
525 if (was_zoomed)
527 /* restore old MDI child */
528 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
529 ShowWindow( hwndPrev, SW_RESTORE );
530 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
532 /* activate new MDI child */
533 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
534 /* maximize new MDI child */
535 ShowWindow( hwndTo, SW_MAXIMIZE );
537 /* activate new MDI child */
538 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
543 /**********************************************************************
544 * MDIDestroyChild
546 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
547 HWND child, BOOL flagDestroy )
549 UINT i;
551 TRACE("# of managed children %u\n", ci->nActiveChildren);
553 if( child == ci->hwndActiveChild )
555 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
556 /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
557 if (flagDestroy && next)
558 MDI_SwitchActiveChild(ci, next, TRUE);
559 else
561 ShowWindow(child, SW_HIDE);
562 if (child == ci->hwndChildMaximized)
564 HWND frame = GetParent(client);
565 MDI_RestoreFrameMenu(frame, child);
566 ci->hwndChildMaximized = 0;
567 MDI_UpdateFrameText(frame, client, TRUE, NULL);
569 if (flagDestroy)
570 MDI_ChildActivate(client, 0);
574 for (i = 0; i < ci->nActiveChildren; i++)
576 if (ci->child[i] == child)
578 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
579 memcpy(new_child, ci->child, i * sizeof(HWND));
580 if (i + 1 < ci->nActiveChildren)
581 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
582 HeapFree(GetProcessHeap(), 0, ci->child);
583 ci->child = new_child;
585 ci->nActiveChildren--;
586 break;
590 if (flagDestroy)
592 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
593 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
594 DestroyWindow(child);
597 TRACE("child destroyed - %p\n", child);
598 return 0;
602 /**********************************************************************
603 * MDI_ChildActivate
605 * Called in response to WM_CHILDACTIVATE, or when last MDI child
606 * is being deactivated.
608 static LONG MDI_ChildActivate( HWND client, HWND child )
610 MDICLIENTINFO *clientInfo;
611 HWND prevActiveWnd, frame;
612 BOOL isActiveFrameWnd;
614 clientInfo = get_client_info( client );
616 if (clientInfo->hwndActiveChild == child) return 0;
618 TRACE("%p\n", child);
620 frame = GetParent(client);
621 isActiveFrameWnd = (GetActiveWindow() == frame);
622 prevActiveWnd = clientInfo->hwndActiveChild;
624 /* deactivate prev. active child */
625 if(prevActiveWnd)
627 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
628 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
631 MDI_SwitchActiveChild( clientInfo, child, FALSE );
632 clientInfo->hwndActiveChild = child;
634 MDI_RefreshMenu(clientInfo);
636 if( isActiveFrameWnd )
638 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
639 /* Let the client window manage focus for children, but if the focus
640 * is already on the client (for instance this is the 1st child) then
641 * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
642 * manually in this case.
644 if (SetFocus(client) == client)
645 SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
648 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
649 return TRUE;
652 /* -------------------- MDI client window functions ------------------- */
654 /**********************************************************************
655 * CreateMDIMenuBitmap
657 static HBITMAP CreateMDIMenuBitmap(void)
659 HDC hDCSrc = CreateCompatibleDC(0);
660 HDC hDCDest = CreateCompatibleDC(hDCSrc);
661 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
662 HBITMAP hbCopy;
663 HBITMAP hobjSrc, hobjDest;
665 hobjSrc = SelectObject(hDCSrc, hbClose);
666 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
667 hobjDest = SelectObject(hDCDest, hbCopy);
669 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
670 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
672 SelectObject(hDCSrc, hobjSrc);
673 DeleteObject(hbClose);
674 DeleteDC(hDCSrc);
676 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
678 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
679 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
681 SelectObject(hDCDest, hobjSrc );
682 SelectObject(hDCDest, hobjDest);
683 DeleteDC(hDCDest);
685 return hbCopy;
688 /**********************************************************************
689 * MDICascade
691 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
693 HWND *win_array;
694 BOOL has_icons = FALSE;
695 int i, total;
697 if (ci->hwndChildMaximized)
698 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
700 if (ci->nActiveChildren == 0) return 0;
702 if (!(win_array = WIN_ListChildren( client ))) return 0;
704 /* remove all the windows we don't want */
705 for (i = total = 0; win_array[i]; i++)
707 if (!IsWindowVisible( win_array[i] )) continue;
708 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
709 if (IsIconic( win_array[i] ))
711 has_icons = TRUE;
712 continue;
714 win_array[total++] = win_array[i];
716 win_array[total] = 0;
718 if (total)
720 INT delta = 0, n = 0, i;
721 POINT pos[2];
722 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
724 /* walk the list (backwards) and move windows */
725 for (i = total - 1; i >= 0; i--)
727 LONG style;
728 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
730 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
731 TRACE("move %p to (%d,%d) size [%d,%d]\n",
732 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
733 style = GetWindowLongW(win_array[i], GWL_STYLE);
735 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
736 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
737 posOptions);
740 HeapFree( GetProcessHeap(), 0, win_array );
742 if (has_icons) ArrangeIconicWindows( client );
743 return 0;
746 /**********************************************************************
747 * MDITile
749 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
751 HWND *win_array;
752 int i, total;
753 BOOL has_icons = FALSE;
755 if (ci->hwndChildMaximized)
756 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
758 if (ci->nActiveChildren == 0) return;
760 if (!(win_array = WIN_ListChildren( client ))) return;
762 /* remove all the windows we don't want */
763 for (i = total = 0; win_array[i]; i++)
765 if (!IsWindowVisible( win_array[i] )) continue;
766 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
767 if (IsIconic( win_array[i] ))
769 has_icons = TRUE;
770 continue;
772 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
773 win_array[total++] = win_array[i];
775 win_array[total] = 0;
777 TRACE("%u windows to tile\n", total);
779 if (total)
781 HWND *pWnd = win_array;
782 RECT rect;
783 int x, y, xsize, ysize;
784 int rows, columns, r, c, i;
786 GetClientRect(client,&rect);
787 rows = (int) sqrt((double)total);
788 columns = total / rows;
790 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
792 i = rows;
793 rows = columns; /* exchange r and c */
794 columns = i;
797 if (has_icons)
799 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
800 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
803 ysize = rect.bottom / rows;
804 xsize = rect.right / columns;
806 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
808 if (c == columns)
810 rows = total - i;
811 ysize = rect.bottom / rows;
814 y = 0;
815 for (r = 1; r <= rows && *pWnd; r++, i++)
817 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
818 LONG style = GetWindowLongW(win_array[i], GWL_STYLE);
819 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
821 SetWindowPos(*pWnd, 0, x, y, xsize, ysize, posOptions);
822 y += ysize;
823 pWnd++;
825 x += xsize;
828 HeapFree( GetProcessHeap(), 0, win_array );
829 if (has_icons) ArrangeIconicWindows( client );
832 /* ----------------------- Frame window ---------------------------- */
835 /**********************************************************************
836 * MDI_AugmentFrameMenu
838 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
840 HMENU menu = GetMenu( frame );
841 HMENU hSysPopup;
842 HBITMAP hSysMenuBitmap = 0;
843 HICON hIcon;
845 TRACE("frame %p,child %p\n",frame,hChild);
847 if (!menu) return FALSE;
849 /* create a copy of sysmenu popup and insert it into frame menu bar */
850 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
852 TRACE("child %p doesn't have a system menu\n", hChild);
853 return FALSE;
856 AppendMenuW(menu, MF_HELP | MF_BITMAP,
857 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
858 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
859 AppendMenuW(menu, MF_HELP | MF_BITMAP,
860 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
861 AppendMenuW(menu, MF_HELP | MF_BITMAP,
862 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
864 /* The system menu is replaced by the child icon */
865 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_SMALL, 0);
866 if (!hIcon)
867 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICONSM);
868 if (!hIcon)
869 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_BIG, 0);
870 if (!hIcon)
871 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICON);
872 if (!hIcon)
873 hIcon = LoadImageW(0, (LPWSTR)IDI_WINLOGO, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
874 GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
875 if (hIcon)
877 HDC hMemDC;
878 HBITMAP hBitmap, hOldBitmap;
879 HBRUSH hBrush;
880 HDC hdc = GetDC(hChild);
882 if (hdc)
884 int cx, cy;
885 cx = GetSystemMetrics(SM_CXSMICON);
886 cy = GetSystemMetrics(SM_CYSMICON);
887 hMemDC = CreateCompatibleDC(hdc);
888 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
889 hOldBitmap = SelectObject(hMemDC, hBitmap);
890 SetMapMode(hMemDC, MM_TEXT);
891 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
892 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
893 SelectObject (hMemDC, hOldBitmap);
894 DeleteObject(hBrush);
895 DeleteDC(hMemDC);
896 ReleaseDC(hChild, hdc);
897 hSysMenuBitmap = hBitmap;
901 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
902 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
904 TRACE("not inserted\n");
905 DestroyMenu(hSysPopup);
906 return FALSE;
909 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
910 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
911 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
912 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
914 /* redraw menu */
915 DrawMenuBar(frame);
917 return TRUE;
920 /**********************************************************************
921 * MDI_RestoreFrameMenu
923 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
925 MENUITEMINFOW menuInfo;
926 HMENU menu = GetMenu( frame );
927 INT nItems;
928 UINT iId;
930 TRACE("frame %p, child %p\n", frame, hChild);
932 if (!menu) return FALSE;
934 /* if there is no system buttons then nothing to do */
935 nItems = GetMenuItemCount(menu) - 1;
936 iId = GetMenuItemID(menu, nItems);
937 if ( !(iId == SC_RESTORE || iId == SC_CLOSE) )
938 return FALSE;
941 * Remove the system menu, If that menu is the icon of the window
942 * as it is in win95, we have to delete the bitmap.
944 memset(&menuInfo, 0, sizeof(menuInfo));
945 menuInfo.cbSize = sizeof(menuInfo);
946 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
948 GetMenuItemInfoW(menu,
950 TRUE,
951 &menuInfo);
953 RemoveMenu(menu,0,MF_BYPOSITION);
955 if ( (menuInfo.fType & MFT_BITMAP) &&
956 (LOWORD(menuInfo.dwTypeData)!=0) &&
957 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
959 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
962 /* close */
963 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
964 /* restore */
965 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
966 /* minimize */
967 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
969 DrawMenuBar(frame);
971 return TRUE;
975 /**********************************************************************
976 * MDI_UpdateFrameText
978 * used when child window is maximized/restored
980 * Note: lpTitle can be NULL
982 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
984 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
985 MDICLIENTINFO *ci = get_client_info( hClient );
987 TRACE("frameText %s\n", debugstr_w(lpTitle));
989 if (!ci) return;
991 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
993 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
994 lpTitle = lpBuffer;
997 /* store new "default" title if lpTitle is not NULL */
998 if (lpTitle)
1000 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1001 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
1002 strcpyW( ci->frameTitle, lpTitle );
1005 if (ci->frameTitle)
1007 if (ci->hwndChildMaximized)
1009 /* combine frame title and child title if possible */
1011 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
1012 static const WCHAR lpBracket2[] = {']',0};
1013 int i_frame_text_length = strlenW(ci->frameTitle);
1015 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1017 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1019 strcatW( lpBuffer, lpBracket );
1020 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
1021 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1022 strcatW( lpBuffer, lpBracket2 );
1023 else
1024 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1027 else
1029 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1032 else
1033 lpBuffer[0] = '\0';
1035 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1037 if (repaint)
1038 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1039 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1043 /* ----------------------------- Interface ---------------------------- */
1046 /**********************************************************************
1047 * MDIClientWndProc_common
1049 LRESULT MDIClientWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
1051 MDICLIENTINFO *ci;
1053 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1055 if (!(ci = get_client_info( hwnd )))
1057 if (message == WM_NCCREATE)
1059 WND *wndPtr = WIN_GetPtr( hwnd );
1060 wndPtr->flags |= WIN_ISMDICLIENT;
1061 WIN_ReleasePtr( wndPtr );
1063 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1064 DefWindowProcA( hwnd, message, wParam, lParam );
1067 switch (message)
1069 case WM_CREATE:
1071 /* Since we are using only cs->lpCreateParams, we can safely
1072 * cast to LPCREATESTRUCTA here */
1073 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1074 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1076 ci->hWindowMenu = ccs->hWindowMenu;
1077 ci->idFirstChild = ccs->idFirstChild;
1078 ci->hwndChildMaximized = 0;
1079 ci->child = NULL;
1080 ci->nActiveChildren = 0;
1081 ci->nTotalCreated = 0;
1082 ci->frameTitle = NULL;
1083 ci->mdiFlags = 0;
1084 ci->hFrameMenu = GetMenu(cs->hwndParent);
1086 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1088 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1089 hwnd, ci->hWindowMenu, ci->idFirstChild );
1090 return 0;
1093 case WM_DESTROY:
1095 if( ci->hwndChildMaximized )
1096 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1098 ci->nActiveChildren = 0;
1099 MDI_RefreshMenu(ci);
1101 HeapFree( GetProcessHeap(), 0, ci->child );
1102 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1104 return 0;
1107 case WM_MDIACTIVATE:
1109 if( ci->hwndActiveChild != (HWND)wParam )
1110 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1111 return 0;
1114 case WM_MDICASCADE:
1115 return MDICascade(hwnd, ci);
1117 case WM_MDICREATE:
1118 if (lParam)
1120 HWND child;
1122 if (unicode)
1124 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1125 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1126 csW->szTitle, csW->style,
1127 csW->x, csW->y, csW->cx, csW->cy,
1128 hwnd, 0, csW->hOwner,
1129 (LPVOID)csW->lParam);
1131 else
1133 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1134 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1135 csA->szTitle, csA->style,
1136 csA->x, csA->y, csA->cx, csA->cy,
1137 hwnd, 0, csA->hOwner,
1138 (LPVOID)csA->lParam);
1140 return (LRESULT)child;
1142 return 0;
1144 case WM_MDIDESTROY:
1145 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1147 case WM_MDIGETACTIVE:
1148 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1149 return (LRESULT)ci->hwndActiveChild;
1151 case WM_MDIICONARRANGE:
1152 ci->mdiFlags |= MDIF_NEEDUPDATE;
1153 ArrangeIconicWindows( hwnd );
1154 ci->sbRecalc = SB_BOTH+1;
1155 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1156 return 0;
1158 case WM_MDIMAXIMIZE:
1159 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1160 return 0;
1162 case WM_MDINEXT: /* lParam != 0 means previous window */
1164 HWND hwnd = wParam ? WIN_GetFullHandle((HWND)wParam) : ci->hwndActiveChild;
1165 HWND next = MDI_GetWindow( ci, hwnd, !lParam, 0 );
1166 MDI_SwitchActiveChild( ci, next, TRUE );
1167 if(!lParam)
1168 SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
1169 break;
1172 case WM_MDIRESTORE:
1173 ShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1174 return 0;
1176 case WM_MDISETMENU:
1177 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1179 case WM_MDIREFRESHMENU:
1180 return MDI_RefreshMenu( ci );
1182 case WM_MDITILE:
1183 ci->mdiFlags |= MDIF_NEEDUPDATE;
1184 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1185 MDITile( hwnd, ci, wParam );
1186 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1187 return 0;
1189 case WM_VSCROLL:
1190 case WM_HSCROLL:
1191 ci->mdiFlags |= MDIF_NEEDUPDATE;
1192 ScrollChildren( hwnd, message, wParam, lParam );
1193 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1194 return 0;
1196 case WM_SETFOCUS:
1197 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1198 SetFocus( ci->hwndActiveChild );
1199 return 0;
1201 case WM_NCACTIVATE:
1202 if( ci->hwndActiveChild )
1203 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1204 break;
1206 case WM_PARENTNOTIFY:
1207 switch (LOWORD(wParam))
1209 case WM_CREATE:
1210 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1212 ci->nTotalCreated++;
1213 ci->nActiveChildren++;
1215 if (!ci->child)
1216 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1217 else
1218 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1220 TRACE("Adding MDI child %p, # of children %d\n",
1221 (HWND)lParam, ci->nActiveChildren);
1223 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1225 break;
1227 case WM_LBUTTONDOWN:
1229 HWND child;
1230 POINT pt;
1231 pt.x = (short)LOWORD(lParam);
1232 pt.y = (short)HIWORD(lParam);
1233 child = ChildWindowFromPoint(hwnd, pt);
1235 TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1237 if( child && child != hwnd && child != ci->hwndActiveChild )
1238 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1239 break;
1242 case WM_DESTROY:
1243 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1245 return 0;
1247 case WM_SIZE:
1248 if( ci->hwndActiveChild && IsZoomed(ci->hwndActiveChild) )
1250 RECT rect;
1252 rect.left = 0;
1253 rect.top = 0;
1254 rect.right = LOWORD(lParam);
1255 rect.bottom = HIWORD(lParam);
1256 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1257 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1258 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1259 rect.right - rect.left, rect.bottom - rect.top, 1);
1261 else
1262 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1264 break;
1266 case WM_MDICALCCHILDSCROLL:
1267 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1269 CalcChildScroll(hwnd, ci->sbRecalc-1);
1270 ci->sbRecalc = 0;
1271 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1273 return 0;
1275 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1276 DefWindowProcA( hwnd, message, wParam, lParam );
1279 /***********************************************************************
1280 * DefFrameProcA (USER32.@)
1282 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1283 UINT message, WPARAM wParam, LPARAM lParam)
1285 if (hwndMDIClient)
1287 switch (message)
1289 case WM_SETTEXT:
1291 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1292 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1293 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1294 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1295 HeapFree( GetProcessHeap(), 0, text );
1297 return 1; /* success. FIXME: check text length */
1299 case WM_COMMAND:
1300 case WM_NCACTIVATE:
1301 case WM_NEXTMENU:
1302 case WM_SETFOCUS:
1303 case WM_SIZE:
1304 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1307 return DefWindowProcA(hwnd, message, wParam, lParam);
1311 /***********************************************************************
1312 * DefFrameProcW (USER32.@)
1314 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1315 UINT message, WPARAM wParam, LPARAM lParam)
1317 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1319 TRACE("%p %p %04x (%s) %08lx %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1321 if (ci)
1323 switch (message)
1325 case WM_COMMAND:
1327 WORD id = LOWORD(wParam);
1328 /* check for possible syscommands for maximized MDI child */
1329 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1331 if( (id - 0xf000) & 0xf00f ) break;
1332 if( !ci->hwndChildMaximized ) break;
1333 switch( id )
1335 case SC_CLOSE:
1336 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1337 case SC_SIZE:
1338 case SC_MOVE:
1339 case SC_MINIMIZE:
1340 case SC_MAXIMIZE:
1341 case SC_NEXTWINDOW:
1342 case SC_PREVWINDOW:
1343 case SC_RESTORE:
1344 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1345 wParam, lParam);
1348 else
1350 HWND childHwnd;
1351 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1352 /* User chose "More Windows..." */
1353 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1354 else
1355 /* User chose one of the windows listed in the "Windows" menu */
1356 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1358 if( childHwnd )
1359 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1362 break;
1364 case WM_NCACTIVATE:
1365 SendMessageW(hwndMDIClient, message, wParam, lParam);
1366 break;
1368 case WM_SETTEXT:
1369 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1370 return 1; /* success. FIXME: check text length */
1372 case WM_SETFOCUS:
1373 SetFocus(hwndMDIClient);
1374 break;
1376 case WM_SIZE:
1377 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1378 break;
1380 case WM_NEXTMENU:
1382 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1384 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1386 /* control menu is between the frame system menu and
1387 * the first entry of menu bar */
1388 WND *wndPtr = WIN_GetPtr(hwnd);
1390 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1391 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1393 WIN_ReleasePtr(wndPtr);
1394 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1395 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1396 next_menu->hwndNext = ci->hwndActiveChild;
1398 WIN_ReleasePtr(wndPtr);
1400 return 0;
1405 return DefWindowProcW( hwnd, message, wParam, lParam );
1408 /***********************************************************************
1409 * DefMDIChildProcA (USER32.@)
1411 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1412 WPARAM wParam, LPARAM lParam )
1414 HWND client = GetParent(hwnd);
1415 MDICLIENTINFO *ci = get_client_info( client );
1417 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1419 hwnd = WIN_GetFullHandle( hwnd );
1420 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1422 switch (message)
1424 case WM_SETTEXT:
1425 DefWindowProcA(hwnd, message, wParam, lParam);
1426 if( ci->hwndChildMaximized == hwnd )
1427 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1428 return 1; /* success. FIXME: check text length */
1430 case WM_GETMINMAXINFO:
1431 case WM_MENUCHAR:
1432 case WM_CLOSE:
1433 case WM_SETFOCUS:
1434 case WM_CHILDACTIVATE:
1435 case WM_SYSCOMMAND:
1436 case WM_SHOWWINDOW:
1437 case WM_SETVISIBLE:
1438 case WM_SIZE:
1439 case WM_NEXTMENU:
1440 case WM_SYSCHAR:
1441 case WM_DESTROY:
1442 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1444 return DefWindowProcA(hwnd, message, wParam, lParam);
1448 /***********************************************************************
1449 * DefMDIChildProcW (USER32.@)
1451 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1452 WPARAM wParam, LPARAM lParam )
1454 HWND client = GetParent(hwnd);
1455 MDICLIENTINFO *ci = get_client_info( client );
1457 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1459 hwnd = WIN_GetFullHandle( hwnd );
1460 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1462 switch (message)
1464 case WM_SETTEXT:
1465 DefWindowProcW(hwnd, message, wParam, lParam);
1466 if( ci->hwndChildMaximized == hwnd )
1467 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1468 return 1; /* success. FIXME: check text length */
1470 case WM_GETMINMAXINFO:
1471 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1472 return 0;
1474 case WM_MENUCHAR:
1475 return MAKELRESULT( 0, MNC_CLOSE ); /* MDI children don't have menu bars */
1477 case WM_CLOSE:
1478 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1479 return 0;
1481 case WM_SETFOCUS:
1482 if (ci->hwndActiveChild != hwnd)
1483 MDI_ChildActivate( client, hwnd );
1484 break;
1486 case WM_CHILDACTIVATE:
1487 if (IsWindowEnabled( hwnd ))
1488 MDI_ChildActivate( client, hwnd );
1489 return 0;
1491 case WM_SYSCOMMAND:
1492 switch (wParam & 0xfff0)
1494 case SC_MOVE:
1495 if( ci->hwndChildMaximized == hwnd )
1496 return 0;
1497 break;
1498 case SC_RESTORE:
1499 case SC_MINIMIZE:
1500 break;
1501 case SC_MAXIMIZE:
1502 if (ci->hwndChildMaximized == hwnd)
1503 return SendMessageW( GetParent(client), message, wParam, lParam);
1504 break;
1505 case SC_NEXTWINDOW:
1506 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 0);
1507 return 0;
1508 case SC_PREVWINDOW:
1509 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 1);
1510 return 0;
1512 break;
1514 case WM_SHOWWINDOW:
1515 case WM_SETVISIBLE:
1516 if (ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1517 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1518 break;
1520 case WM_SIZE:
1521 /* This is the only place where we switch to/from maximized state */
1522 /* do not change */
1523 TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1525 if( ci->hwndChildMaximized == hwnd && wParam != SIZE_MAXIMIZED )
1527 HWND frame;
1529 ci->hwndChildMaximized = 0;
1531 frame = GetParent(client);
1532 MDI_RestoreFrameMenu( frame, hwnd );
1533 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1536 if( wParam == SIZE_MAXIMIZED )
1538 HWND frame, hMaxChild = ci->hwndChildMaximized;
1540 if( hMaxChild == hwnd ) break;
1542 if( hMaxChild)
1544 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1546 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1547 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1549 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1552 TRACE("maximizing child %p\n", hwnd );
1554 /* keep track of the maximized window. */
1555 ci->hwndChildMaximized = hwnd; /* !!! */
1557 frame = GetParent(client);
1558 MDI_AugmentFrameMenu( frame, hwnd );
1559 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1562 if( wParam == SIZE_MINIMIZED )
1564 HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1566 if (!switchTo) switchTo = hwnd;
1567 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1570 MDI_PostUpdate(client, ci, SB_BOTH+1);
1571 break;
1573 case WM_NEXTMENU:
1575 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1576 HWND parent = GetParent(client);
1578 if( wParam == VK_LEFT ) /* switch to frame system menu */
1580 WND *wndPtr = WIN_GetPtr( parent );
1581 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1582 WIN_ReleasePtr( wndPtr );
1584 if( wParam == VK_RIGHT ) /* to frame menu bar */
1586 next_menu->hmenuNext = GetMenu(parent);
1588 next_menu->hwndNext = parent;
1589 return 0;
1592 case WM_SYSCHAR:
1593 if (wParam == '-')
1595 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, VK_SPACE);
1596 return 0;
1598 break;
1600 case WM_DESTROY:
1601 /* Remove itself from the Window menu */
1602 MDI_RefreshMenu(ci);
1603 break;
1605 return DefWindowProcW(hwnd, message, wParam, lParam);
1608 /**********************************************************************
1609 * CreateMDIWindowA (USER32.@) Creates a MDI child
1611 * RETURNS
1612 * Success: Handle to created window
1613 * Failure: NULL
1615 HWND WINAPI CreateMDIWindowA(
1616 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1617 LPCSTR lpWindowName, /* [in] Pointer to window name */
1618 DWORD dwStyle, /* [in] Window style */
1619 INT X, /* [in] Horizontal position of window */
1620 INT Y, /* [in] Vertical position of window */
1621 INT nWidth, /* [in] Width of window */
1622 INT nHeight, /* [in] Height of window */
1623 HWND hWndParent, /* [in] Handle to parent window */
1624 HINSTANCE hInstance, /* [in] Handle to application instance */
1625 LPARAM lParam) /* [in] Application-defined value */
1627 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1628 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1629 nWidth,nHeight,hWndParent,hInstance,lParam);
1631 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1632 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1633 0, hInstance, (LPVOID)lParam);
1636 /***********************************************************************
1637 * CreateMDIWindowW (USER32.@) Creates a MDI child
1639 * RETURNS
1640 * Success: Handle to created window
1641 * Failure: NULL
1643 HWND WINAPI CreateMDIWindowW(
1644 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1645 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1646 DWORD dwStyle, /* [in] Window style */
1647 INT X, /* [in] Horizontal position of window */
1648 INT Y, /* [in] Vertical position of window */
1649 INT nWidth, /* [in] Width of window */
1650 INT nHeight, /* [in] Height of window */
1651 HWND hWndParent, /* [in] Handle to parent window */
1652 HINSTANCE hInstance, /* [in] Handle to application instance */
1653 LPARAM lParam) /* [in] Application-defined value */
1655 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1656 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1657 nWidth, nHeight, hWndParent, hInstance, lParam);
1659 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1660 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1661 0, hInstance, (LPVOID)lParam);
1664 /**********************************************************************
1665 * TranslateMDISysAccel (USER32.@)
1667 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1669 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1671 MDICLIENTINFO *ci = get_client_info( hwndClient );
1672 WPARAM wParam = 0;
1674 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return FALSE;
1676 /* translate if the Ctrl key is down and Alt not. */
1678 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1680 switch( msg->wParam )
1682 case VK_F6:
1683 case VK_TAB:
1684 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1685 break;
1686 case VK_F4:
1687 case VK_RBUTTON:
1688 if (is_close_enabled(ci->hwndActiveChild, 0))
1690 wParam = SC_CLOSE;
1691 break;
1693 /* fall through */
1694 default:
1695 return FALSE;
1697 TRACE("wParam = %04lx\n", wParam);
1698 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, msg->wParam);
1699 return TRUE;
1702 return FALSE; /* failure */
1705 /***********************************************************************
1706 * CalcChildScroll (USER32.@)
1708 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1710 SCROLLINFO info;
1711 RECT childRect, clientRect;
1712 HWND *list;
1713 DWORD style;
1715 GetClientRect( hwnd, &clientRect );
1716 SetRectEmpty( &childRect );
1718 if ((list = WIN_ListChildren( hwnd )))
1720 int i;
1721 for (i = 0; list[i]; i++)
1723 style = GetWindowLongW( list[i], GWL_STYLE );
1724 if (style & WS_MAXIMIZE)
1726 HeapFree( GetProcessHeap(), 0, list );
1727 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1728 return;
1730 if (style & WS_VISIBLE)
1732 RECT rect;
1733 WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL );
1734 UnionRect( &childRect, &rect, &childRect );
1737 HeapFree( GetProcessHeap(), 0, list );
1739 UnionRect( &childRect, &clientRect, &childRect );
1741 /* set common info values */
1742 info.cbSize = sizeof(info);
1743 info.fMask = SIF_POS | SIF_RANGE;
1745 /* set the specific values and apply but only if window style allows */
1746 style = GetWindowLongW( hwnd, GWL_STYLE );
1747 switch( scroll )
1749 case SB_BOTH:
1750 case SB_HORZ:
1751 if (style & (WS_HSCROLL | WS_VSCROLL))
1753 info.nMin = childRect.left;
1754 info.nMax = childRect.right - clientRect.right;
1755 info.nPos = clientRect.left - childRect.left;
1756 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1758 if (scroll == SB_HORZ) break;
1759 /* fall through */
1760 case SB_VERT:
1761 if (style & (WS_HSCROLL | WS_VSCROLL))
1763 info.nMin = childRect.top;
1764 info.nMax = childRect.bottom - clientRect.bottom;
1765 info.nPos = clientRect.top - childRect.top;
1766 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1768 break;
1773 /***********************************************************************
1774 * ScrollChildren (USER32.@)
1776 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1777 LPARAM lParam)
1779 INT newPos = -1;
1780 INT curPos, length, minPos, maxPos, shift;
1781 RECT rect;
1783 GetClientRect( hWnd, &rect );
1785 switch(uMsg)
1787 case WM_HSCROLL:
1788 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1789 curPos = GetScrollPos(hWnd,SB_HORZ);
1790 length = (rect.right - rect.left) / 2;
1791 shift = GetSystemMetrics(SM_CYHSCROLL);
1792 break;
1793 case WM_VSCROLL:
1794 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1795 curPos = GetScrollPos(hWnd,SB_VERT);
1796 length = (rect.bottom - rect.top) / 2;
1797 shift = GetSystemMetrics(SM_CXVSCROLL);
1798 break;
1799 default:
1800 return;
1803 switch( wParam )
1805 case SB_LINEUP:
1806 newPos = curPos - shift;
1807 break;
1808 case SB_LINEDOWN:
1809 newPos = curPos + shift;
1810 break;
1811 case SB_PAGEUP:
1812 newPos = curPos - length;
1813 break;
1814 case SB_PAGEDOWN:
1815 newPos = curPos + length;
1816 break;
1818 case SB_THUMBPOSITION:
1819 newPos = LOWORD(lParam);
1820 break;
1822 case SB_THUMBTRACK:
1823 return;
1825 case SB_TOP:
1826 newPos = minPos;
1827 break;
1828 case SB_BOTTOM:
1829 newPos = maxPos;
1830 break;
1831 case SB_ENDSCROLL:
1832 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1833 return;
1836 if( newPos > maxPos )
1837 newPos = maxPos;
1838 else
1839 if( newPos < minPos )
1840 newPos = minPos;
1842 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1844 if( uMsg == WM_VSCROLL )
1845 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1846 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1847 else
1848 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1849 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1853 /******************************************************************************
1854 * CascadeWindows (USER32.@) Cascades MDI child windows
1856 * RETURNS
1857 * Success: Number of cascaded windows.
1858 * Failure: 0
1860 WORD WINAPI
1861 CascadeWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1862 UINT cKids, const HWND *lpKids)
1864 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1865 return 0;
1869 /***********************************************************************
1870 * CascadeChildWindows (USER32.@)
1872 WORD WINAPI CascadeChildWindows( HWND parent, UINT flags )
1874 return CascadeWindows( parent, flags, NULL, 0, NULL );
1878 /******************************************************************************
1879 * TileWindows (USER32.@) Tiles MDI child windows
1881 * RETURNS
1882 * Success: Number of tiled windows.
1883 * Failure: 0
1885 WORD WINAPI
1886 TileWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1887 UINT cKids, const HWND *lpKids)
1889 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1890 return 0;
1894 /***********************************************************************
1895 * TileChildWindows (USER32.@)
1897 WORD WINAPI TileChildWindows( HWND parent, UINT flags )
1899 return TileWindows( parent, flags, NULL, 0, NULL );
1903 /************************************************************************
1904 * "More Windows..." functionality
1907 /* MDI_MoreWindowsDlgProc
1909 * This function will process the messages sent to the "More Windows..."
1910 * dialog.
1911 * Return values: 0 = cancel pressed
1912 * HWND = ok pressed or double-click in the list...
1916 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1918 switch (iMsg)
1920 case WM_INITDIALOG:
1922 UINT widest = 0;
1923 UINT length;
1924 UINT i;
1925 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1926 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1928 for (i = 0; i < ci->nActiveChildren; i++)
1930 WCHAR buffer[MDI_MAXTITLELENGTH];
1932 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1933 continue;
1934 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1935 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1936 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1937 if (length > widest)
1938 widest = length;
1940 /* Make sure the horizontal scrollbar scrolls ok */
1941 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1943 /* Set the current selection */
1944 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1945 return TRUE;
1948 case WM_COMMAND:
1949 switch (LOWORD(wParam))
1951 default:
1952 if (HIWORD(wParam) != LBN_DBLCLK) break;
1953 /* fall through */
1954 case IDOK:
1956 /* windows are sorted by menu ID, so we must return the
1957 * window associated to the given id
1959 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1960 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1961 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1962 EndDialog(hDlg, res);
1963 return TRUE;
1965 case IDCANCEL:
1966 EndDialog(hDlg, 0);
1967 return TRUE;
1969 break;
1971 return FALSE;
1976 * MDI_MoreWindowsDialog
1978 * Prompts the user with a listbox containing the opened
1979 * documents. The user can then choose a windows and click
1980 * on OK to set the current window to the one selected, or
1981 * CANCEL to cancel. The function returns a handle to the
1982 * selected window.
1985 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1987 LPCVOID template;
1988 HRSRC hRes;
1989 HANDLE hDlgTmpl;
1991 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1993 if (hRes == 0)
1994 return 0;
1996 hDlgTmpl = LoadResource(user32_module, hRes );
1998 if (hDlgTmpl == 0)
1999 return 0;
2001 template = LockResource( hDlgTmpl );
2003 if (template == 0)
2004 return 0;
2006 return (HWND) DialogBoxIndirectParamA(user32_module, template, hwnd,
2007 MDI_MoreWindowsDlgProc, (LPARAM) hwnd);