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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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, accesible 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
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
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.
94 #include "wine/unicode.h"
96 #include "nonclient.h"
100 #include "wine/debug.h"
103 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
105 #define MDI_MAXLISTLENGTH 0x40
106 #define MDI_MAXTITLELENGTH 0xa1
108 #define MDI_NOFRAMEREPAINT 0
109 #define MDI_REPAINTFRAMENOW 1
110 #define MDI_REPAINTFRAME 2
112 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
114 /* "More Windows..." definitions */
115 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
116 option will appear under the Windows menu */
117 #define MDI_IDC_LISTBOX 100
118 #define IDS_MDI_MOREWINDOWS 13
120 #define MDIF_NEEDUPDATE 0x0001
124 UINT nActiveChildren;
125 HWND hwndChildMaximized;
126 HWND hwndActiveChild;
132 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
135 static HBITMAP hBmpClose = 0;
137 /* ----------------- declarations ----------------- */
138 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
139 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
140 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
141 static LONG MDI_ChildActivate( HWND, HWND );
143 static HWND MDI_MoreWindowsDialog(HWND);
144 static void MDI_SwapMenuItems(HWND, UINT, UINT);
145 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
146 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
148 /* -------- Miscellaneous service functions ----------
152 static HWND MDI_GetChildByID(HWND hwnd, UINT id)
158 if (!(win_array = WIN_ListChildren( hwnd ))) return 0;
159 for (i = 0; win_array[i]; i++)
161 if (GetWindowLongA( win_array[i], GWL_ID ) == id) break;
164 HeapFree( GetProcessHeap(), 0, win_array );
168 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
170 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
172 ci->mdiFlags |= MDIF_NEEDUPDATE;
173 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
175 ci->sbRecalc = recalc;
179 /*********************************************************************
180 * MDIClient class descriptor
182 const struct builtin_class_descr MDICLIENT_builtin_class =
184 "MDIClient", /* name */
185 CS_GLOBALCLASS, /* style */
186 MDIClientWndProcA, /* procA */
187 MDIClientWndProcW, /* procW */
188 sizeof(MDICLIENTINFO), /* extra */
189 IDC_ARROW, /* cursor */
190 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
194 static MDICLIENTINFO *get_client_info( HWND client )
196 MDICLIENTINFO *ret = NULL;
197 WND *win = WIN_GetPtr( client );
200 if (win == WND_OTHER_PROCESS)
202 ERR( "client %p belongs to other process\n", client );
205 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
206 else ret = (MDICLIENTINFO *)win->wExtra;
207 WIN_ReleasePtr( win );
212 /**********************************************************************
215 static void MDI_MenuModifyItem( HWND client, HWND hWndChild )
217 MDICLIENTINFO *clientInfo = get_client_info( client );
221 if (!clientInfo || !clientInfo->hWindowMenu) return;
223 id = GetWindowLongA( hWndChild, GWL_ID );
224 if (id >= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT) return;
226 buffer[1] = '1' + id - clientInfo->idFirstChild;
228 GetWindowTextW( hWndChild, buffer + 3, sizeof(buffer)/sizeof(WCHAR) - 3 );
230 n = GetMenuState(clientInfo->hWindowMenu, id, MF_BYCOMMAND);
231 ModifyMenuW(clientInfo->hWindowMenu, id, MF_BYCOMMAND | MF_STRING, id, buffer );
232 CheckMenuItem(clientInfo->hWindowMenu, id, n & MF_CHECKED);
235 /**********************************************************************
238 static BOOL MDI_MenuDeleteItem( HWND client, HWND hWndChild )
241 static const WCHAR format[] = {'&','%','d',' ',0};
242 MDICLIENTINFO *clientInfo = get_client_info( client );
245 if( !clientInfo->nActiveChildren || !clientInfo->hWindowMenu )
248 id = GetWindowLongA( hWndChild, GWL_ID );
249 DeleteMenu(clientInfo->hWindowMenu,id,MF_BYCOMMAND);
251 /* walk the rest of MDI children to prevent gaps in the id
252 * sequence and in the menu child list */
254 for( index = id+1; index <= clientInfo->nActiveChildren +
255 clientInfo->idFirstChild; index++ )
257 HWND hwnd = MDI_GetChildByID(client,index);
260 TRACE("no window for id=%i\n",index);
265 SetWindowLongW( hwnd, GWL_ID, GetWindowLongW( hwnd, GWL_ID ) - 1 );
267 n = wsprintfW(buffer, format ,index - clientInfo->idFirstChild);
268 GetWindowTextW( hwnd, buffer + n, sizeof(buffer)/sizeof(WCHAR) - n );
270 /* change menu if the current child is to be shown in the
273 if (index <= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT)
274 ModifyMenuW(clientInfo->hWindowMenu ,index ,MF_BYCOMMAND | MF_STRING,
275 index - 1 , buffer );
278 /* We must restore the "More Windows..." option if there are enough children
280 if (clientInfo->nActiveChildren - 1 > MDI_MOREWINDOWSLIMIT)
283 LoadStringW(GetModuleHandleA("USER32"), IDS_MDI_MOREWINDOWS, szTmp, sizeof(szTmp)/sizeof(szTmp[0]));
284 AppendMenuW(clientInfo->hWindowMenu, MF_STRING, clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT, szTmp);
289 /**********************************************************************
292 * returns "activateable" child different from the current or zero
294 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
301 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
302 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
304 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
306 /* start from next after hWnd */
307 while (list[i] && list[i] != hWnd) i++;
310 for ( ; list[i]; i++)
312 if (GetWindow( list[i], GW_OWNER )) continue;
313 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
315 if (bNext) goto found;
317 /* now restart from the beginning */
318 for (i = 0; list[i] && list[i] != hWnd; i++)
320 if (GetWindow( list[i], GW_OWNER )) continue;
321 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
323 if (bNext) goto found;
326 HeapFree( GetProcessHeap(), 0, list );
330 /**********************************************************************
331 * MDI_CalcDefaultChildPos
333 * It seems that the default height is about 2/3 of the client rect
335 static void MDI_CalcDefaultChildPos( HWND hwnd, WORD n, LPPOINT lpPos, INT delta)
339 INT spacing = GetSystemMetrics(SM_CYCAPTION) +
340 GetSystemMetrics(SM_CYFRAME) - 1;
342 GetClientRect( hwnd, &rect );
343 if( rect.bottom - rect.top - delta >= spacing )
344 rect.bottom -= delta;
346 nstagger = (rect.bottom - rect.top)/(3 * spacing);
347 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
348 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
349 lpPos[0].x = lpPos[0].y = spacing * (n%(nstagger+1));
352 /**********************************************************************
355 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
359 HWND hwndFrame = GetParent(hwnd);
360 HMENU oldFrameMenu = GetMenu(hwndFrame);
362 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
364 if (hmenuFrame && !IsMenu(hmenuFrame))
366 WARN("hmenuFrame is not a menu handle\n");
370 if (hmenuWindow && !IsMenu(hmenuWindow))
372 WARN("hmenuWindow is not a menu handle\n");
376 if (!(ci = get_client_info( hwnd ))) return 0;
378 if( ci->hwndChildMaximized && hmenuFrame && hmenuFrame!=oldFrameMenu )
379 MDI_RestoreFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
381 if( hmenuWindow && ci->hWindowMenu && hmenuWindow!=ci->hWindowMenu )
383 /* delete menu items from ci->hWindowMenu
384 * and add them to hmenuWindow */
386 INT i = GetMenuItemCount(ci->hWindowMenu) - 1;
387 INT pos = GetMenuItemCount(hmenuWindow) + 1;
389 AppendMenuA( hmenuWindow, MF_SEPARATOR, 0, NULL);
391 if( ci->nActiveChildren )
394 LPWSTR buffer = NULL;
396 INT nbWindowsMenuItems; /* num of documents shown + "More Windows..." if present */
398 if (ci->nActiveChildren <= MDI_MOREWINDOWSLIMIT)
399 nbWindowsMenuItems = ci->nActiveChildren;
401 nbWindowsMenuItems = MDI_MOREWINDOWSLIMIT + 1;
403 j = i - nbWindowsMenuItems + 1;
405 for( ; i >= j ; i-- )
407 memset(&mii, 0, sizeof(mii));
408 mii.cbSize = sizeof(mii);
409 mii.fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_ID | MIIM_STATE
410 | MIIM_SUBMENU | MIIM_TYPE | MIIM_BITMAP;
412 GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
413 if(mii.cch) { /* Menu is MFT_STRING */
414 mii.cch++; /* add room for '\0' */
415 buffer = HeapAlloc(GetProcessHeap(), 0,
416 mii.cch * sizeof(WCHAR));
417 mii.dwTypeData = buffer;
418 GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
420 DeleteMenu(ci->hWindowMenu, i, MF_BYPOSITION);
421 InsertMenuItemW(hmenuWindow, pos, TRUE, &mii);
423 HeapFree(GetProcessHeap(), 0, buffer);
429 /* remove separator */
430 DeleteMenu(ci->hWindowMenu, i, MF_BYPOSITION);
432 ci->hWindowMenu = hmenuWindow;
437 SetMenu(hwndFrame, hmenuFrame);
438 if( hmenuFrame!=oldFrameMenu )
440 if( ci->hwndChildMaximized )
441 MDI_AugmentFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
442 return (LRESULT)oldFrameMenu;
447 HMENU menu = GetMenu( GetParent(hwnd) );
448 INT nItems = GetMenuItemCount(menu) - 1;
449 UINT iId = GetMenuItemID(menu,nItems) ;
451 if( !(iId == SC_RESTORE || iId == SC_CLOSE) )
453 /* SetMenu() may already have been called, meaning that this window
454 * already has its menu. But they may have done a SetMenu() on
455 * an MDI window, and called MDISetMenu() after the fact, meaning
456 * that the "if" to this "else" wouldn't catch the need to
457 * augment the frame menu.
459 if( ci->hwndChildMaximized )
460 MDI_AugmentFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
466 /**********************************************************************
469 static LRESULT MDIRefreshMenu( HWND hwnd, HMENU hmenuFrame,
472 HWND hwndFrame = GetParent(hwnd);
473 HMENU oldFrameMenu = GetMenu(hwndFrame);
475 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
477 FIXME("partially function stub\n");
479 return (LRESULT)oldFrameMenu;
483 /* ------------------ MDI child window functions ---------------------- */
486 /**********************************************************************
489 static HWND MDICreateChild( HWND parent, MDICLIENTINFO *ci,
490 LPMDICREATESTRUCTA cs, BOOL unicode )
493 DWORD style = cs->style | (WS_CHILD | WS_CLIPSIBLINGS);
494 HWND hwnd, hwndMax = 0;
495 UINT wIDmenu = ci->idFirstChild + ci->nActiveChildren;
497 static const WCHAR lpstrDef[] = {'j','u','n','k','!',0};
499 TRACE("origin %i,%i - dim %i,%i, style %08lx\n",
500 cs->x, cs->y, cs->cx, cs->cy, cs->style);
501 /* calculate placement */
502 MDI_CalcDefaultChildPos(parent, ci->nTotalCreated++, pos, 0);
504 if (cs->cx == CW_USEDEFAULT || cs->cx == CW_USEDEFAULT16 || !cs->cx) cs->cx = pos[1].x;
505 if (cs->cy == CW_USEDEFAULT || cs->cy == CW_USEDEFAULT16 || !cs->cy) cs->cy = pos[1].y;
507 if (cs->x == CW_USEDEFAULT || cs->x == CW_USEDEFAULT16)
513 /* restore current maximized child */
514 if( (style & WS_VISIBLE) && ci->hwndChildMaximized )
516 TRACE("Restoring current maximized child %p\n", ci->hwndChildMaximized);
517 if( style & WS_MAXIMIZE )
518 SendMessageW(parent, WM_SETREDRAW, FALSE, 0L);
519 hwndMax = ci->hwndChildMaximized;
520 ShowWindow( hwndMax, SW_SHOWNOACTIVATE );
521 if( style & WS_MAXIMIZE )
522 SendMessageW(parent, WM_SETREDRAW, TRUE, 0L);
525 if (ci->nActiveChildren <= MDI_MOREWINDOWSLIMIT)
526 /* this menu is needed to set a check mark in MDI_ChildActivate */
527 if (ci->hWindowMenu != 0)
528 AppendMenuW(ci->hWindowMenu, MF_STRING, wIDmenu, lpstrDef);
530 ci->nActiveChildren++;
532 /* fix window style */
533 wndParent = WIN_FindWndPtr( parent );
534 if( !(wndParent->dwStyle & MDIS_ALLCHILDSTYLES) )
536 TRACE("MDIS_ALLCHILDSTYLES is missing, fixing window style\n");
537 style &= (WS_CHILD | WS_CLIPSIBLINGS | WS_MINIMIZE | WS_MAXIMIZE |
538 WS_CLIPCHILDREN | WS_DISABLED | WS_VSCROLL | WS_HSCROLL );
539 style |= (WS_VISIBLE | WS_OVERLAPPEDWINDOW);
542 if( wndParent->flags & WIN_ISWIN32 )
544 WIN_ReleaseWndPtr( wndParent );
547 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)cs;
548 hwnd = CreateWindowW( csW->szClass, csW->szTitle, style,
549 csW->x, csW->y, csW->cx, csW->cy, parent,
550 (HMENU)wIDmenu, csW->hOwner, csW );
553 hwnd = CreateWindowA( cs->szClass, cs->szTitle, style,
554 cs->x, cs->y, cs->cx, cs->cy, parent,
555 (HMENU)wIDmenu, cs->hOwner, cs );
559 MDICREATESTRUCT16 cs16;
560 SEGPTR title, cls, seg_cs16;
562 WIN_ReleaseWndPtr( wndParent );
563 STRUCT32_MDICREATESTRUCT32Ato16( cs, &cs16 );
564 cs16.szTitle = title = MapLS( cs->szTitle );
565 cs16.szClass = cls = MapLS( cs->szClass );
566 seg_cs16 = MapLS( &cs16 );
567 hwnd = WIN_Handle32( CreateWindow16( cs->szClass, cs->szTitle, style,
568 cs16.x, cs16.y, cs16.cx, cs16.cy,
569 HWND_16(parent), (HMENU16)wIDmenu,
570 cs16.hOwner, (LPVOID)seg_cs16 ));
576 /* MDI windows are WS_CHILD so they won't be activated by CreateWindow */
580 /* All MDI child windows have the WS_EX_MDICHILD style */
581 SetWindowLongW( hwnd, GWL_EXSTYLE, GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_MDICHILD );
583 /* If we have more than 9 windows, we must insert the new one at the
584 * 9th position in order to see it in the "Windows" menu
586 if (ci->nActiveChildren > MDI_MOREWINDOWSLIMIT)
587 MDI_SwapMenuItems( parent, GetWindowLongW( hwnd, GWL_ID ),
588 ci->idFirstChild + MDI_MOREWINDOWSLIMIT - 1);
590 MDI_MenuModifyItem(parent, hwnd);
592 /* Have we hit the "More Windows..." limit? If so, we must
593 * add a "More Windows..." option
595 if (ci->nActiveChildren == MDI_MOREWINDOWSLIMIT + 1)
598 LoadStringW(GetModuleHandleA("USER32"), IDS_MDI_MOREWINDOWS, szTmp, sizeof(szTmp)/sizeof(szTmp[0]));
600 ModifyMenuW(ci->hWindowMenu,
601 ci->idFirstChild + MDI_MOREWINDOWSLIMIT,
602 MF_BYCOMMAND | MF_STRING,
603 ci->idFirstChild + MDI_MOREWINDOWSLIMIT,
607 if( IsIconic(hwnd) && ci->hwndActiveChild )
609 TRACE("Minimizing created MDI child %p\n", hwnd);
610 ShowWindow( hwnd, SW_SHOWMINNOACTIVE );
614 /* WS_VISIBLE is clear if a) the MDI client has
615 * MDIS_ALLCHILDSTYLES style and 2) the flag is cleared in the
616 * MDICreateStruct. If so the created window is not shown nor
619 if (IsWindowVisible(hwnd)) ShowWindow(hwnd, SW_SHOW);
621 TRACE("created child - %p\n",hwnd);
625 ci->nActiveChildren--;
626 DeleteMenu(ci->hWindowMenu,wIDmenu,MF_BYCOMMAND);
627 if( IsWindow(hwndMax) )
628 ShowWindow(hwndMax, SW_SHOWMAXIMIZED);
634 /**********************************************************************
635 * MDI_ChildGetMinMaxInfo
637 * Note: The rule here is that client rect of the maximized MDI child
638 * is equal to the client rect of the MDI client window.
640 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
644 GetClientRect( client, &rect );
645 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
646 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
648 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
649 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
651 lpMinMax->ptMaxPosition.x = rect.left;
652 lpMinMax->ptMaxPosition.y = rect.top;
654 TRACE("max rect (%ld,%ld - %ld, %ld)\n",
655 rect.left,rect.top,rect.right,rect.bottom);
658 /**********************************************************************
659 * MDI_SwitchActiveChild
661 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
664 static void MDI_SwitchActiveChild( HWND clientHwnd, HWND childHwnd,
669 MDICLIENTINFO *ci = get_client_info( clientHwnd );
671 hwndTo = MDI_GetWindow(ci, childHwnd, bNextWindow, 0);
673 TRACE("from %p, to %p\n",childHwnd,hwndTo);
675 if ( !hwndTo ) return; /* no window to switch to */
677 hwndPrev = ci->hwndActiveChild;
679 if ( hwndTo != hwndPrev )
681 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0,
682 SWP_NOMOVE | SWP_NOSIZE );
684 if( bNextWindow && hwndPrev )
685 SetWindowPos( hwndPrev, HWND_BOTTOM, 0, 0, 0, 0,
686 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE );
691 /**********************************************************************
694 static LRESULT MDIDestroyChild( HWND parent, MDICLIENTINFO *ci,
695 HWND child, BOOL flagDestroy )
697 if( child == ci->hwndActiveChild )
699 MDI_SwitchActiveChild(parent, child, TRUE);
701 if( child == ci->hwndActiveChild )
703 ShowWindow( child, SW_HIDE);
704 if( child == ci->hwndChildMaximized )
706 HWND frame = GetParent(parent);
707 MDI_RestoreFrameMenu( frame, child );
708 ci->hwndChildMaximized = 0;
709 MDI_UpdateFrameText( frame, parent, TRUE, NULL);
712 MDI_ChildActivate(parent, 0);
716 MDI_MenuDeleteItem(parent, child);
718 ci->nActiveChildren--;
720 TRACE("child destroyed - %p\n",child);
724 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
725 DestroyWindow(child);
731 /**********************************************************************
734 * Note: hWndChild is NULL when last child is being destroyed
736 static LONG MDI_ChildActivate( HWND client, HWND child )
738 MDICLIENTINFO *clientInfo = get_client_info( client );
739 HWND prevActiveWnd = clientInfo->hwndActiveChild;
740 BOOL isActiveFrameWnd;
742 if (child && (!IsWindowEnabled( child ))) return 0;
744 /* Don't activate if it is already active. Might happen
745 since ShowWindow DOES activate MDI children */
746 if (clientInfo->hwndActiveChild == child) return 0;
748 TRACE("%p\n", child);
750 isActiveFrameWnd = (GetActiveWindow() == GetParent(client));
752 /* deactivate prev. active child */
755 SetWindowLongA( prevActiveWnd, GWL_STYLE,
756 GetWindowLongA( prevActiveWnd, GWL_STYLE ) | WS_SYSMENU );
757 SendMessageA( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
758 SendMessageA( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
759 /* uncheck menu item */
760 if( clientInfo->hWindowMenu )
762 UINT prevID = GetWindowLongA( prevActiveWnd, GWL_ID );
764 if (prevID - clientInfo->idFirstChild < MDI_MOREWINDOWSLIMIT)
765 CheckMenuItem( clientInfo->hWindowMenu, prevID, 0);
767 CheckMenuItem( clientInfo->hWindowMenu,
768 clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1, 0);
773 if (clientInfo->hwndChildMaximized && clientInfo->hwndChildMaximized != child)
775 INT cmd = SW_SHOWNORMAL;
779 UINT state = GetMenuState(GetSystemMenu(child, FALSE), SC_MAXIMIZE, MF_BYCOMMAND);
780 if (state != 0xFFFFFFFF && (state & (MF_DISABLED | MF_GRAYED)))
781 SendMessageW(clientInfo->hwndChildMaximized, WM_SYSCOMMAND, SC_RESTORE, 0);
783 cmd = SW_SHOWMAXIMIZED;
785 clientInfo->hwndActiveChild = child;
788 ShowWindow( clientInfo->hwndActiveChild, cmd );
791 clientInfo->hwndActiveChild = child;
793 /* check if we have any children left */
796 if( isActiveFrameWnd )
801 /* check menu item */
802 if( clientInfo->hWindowMenu )
804 UINT id = GetWindowLongA( child, GWL_ID );
805 /* The window to be activated must be displayed in the "Windows" menu */
806 if (id >= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT)
808 MDI_SwapMenuItems( GetParent(child),
809 id, clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1);
810 id = clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1;
811 MDI_MenuModifyItem( GetParent(child), child );
814 CheckMenuItem(clientInfo->hWindowMenu, id, MF_CHECKED);
816 /* bring active child to the top */
817 SetWindowPos( child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
819 if( isActiveFrameWnd )
821 SendMessageA( child, WM_NCACTIVATE, TRUE, 0L);
822 if( GetFocus() == client )
823 SendMessageA( client, WM_SETFOCUS, (WPARAM)client, 0L );
827 SendMessageA( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
831 /* -------------------- MDI client window functions ------------------- */
833 /**********************************************************************
834 * CreateMDIMenuBitmap
836 static HBITMAP CreateMDIMenuBitmap(void)
838 HDC hDCSrc = CreateCompatibleDC(0);
839 HDC hDCDest = CreateCompatibleDC(hDCSrc);
840 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
842 HBITMAP hobjSrc, hobjDest;
844 hobjSrc = SelectObject(hDCSrc, hbClose);
845 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
846 hobjDest = SelectObject(hDCDest, hbCopy);
848 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
849 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
851 SelectObject(hDCSrc, hobjSrc);
852 DeleteObject(hbClose);
855 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
857 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
858 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
860 SelectObject(hDCDest, hobjSrc );
861 SelectObject(hDCDest, hobjDest);
867 /**********************************************************************
870 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
873 BOOL has_icons = FALSE;
876 if (ci->hwndChildMaximized)
877 SendMessageA( client, WM_MDIRESTORE,
878 (WPARAM)ci->hwndChildMaximized, 0);
880 if (ci->nActiveChildren == 0) return 0;
882 if (!(win_array = WIN_ListChildren( client ))) return 0;
884 /* remove all the windows we don't want */
885 for (i = total = 0; win_array[i]; i++)
887 if (!IsWindowVisible( win_array[i] )) continue;
888 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
889 if (IsIconic( win_array[i] ))
894 win_array[total++] = win_array[i];
896 win_array[total] = 0;
900 INT delta = 0, n = 0, i;
902 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
904 /* walk the list (backwards) and move windows */
905 for (i = total - 1; i >= 0; i--)
907 TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
908 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
910 MDI_CalcDefaultChildPos(client, n++, pos, delta);
911 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
912 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
915 HeapFree( GetProcessHeap(), 0, win_array );
917 if (has_icons) ArrangeIconicWindows( client );
921 /**********************************************************************
924 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
928 BOOL has_icons = FALSE;
930 if (ci->hwndChildMaximized)
931 SendMessageA( client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
933 if (ci->nActiveChildren == 0) return;
935 if (!(win_array = WIN_ListChildren( client ))) return;
937 /* remove all the windows we don't want */
938 for (i = total = 0; win_array[i]; i++)
940 if (!IsWindowVisible( win_array[i] )) continue;
941 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
942 if (IsIconic( win_array[i] ))
947 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
948 win_array[total++] = win_array[i];
950 win_array[total] = 0;
952 TRACE("%u windows to tile\n", total);
956 HWND *pWnd = win_array;
958 int x, y, xsize, ysize;
959 int rows, columns, r, c, i;
961 GetClientRect(client,&rect);
962 rows = (int) sqrt((double)total);
963 columns = total / rows;
965 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
968 rows = columns; /* exchange r and c */
974 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
975 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
978 ysize = rect.bottom / rows;
979 xsize = rect.right / columns;
981 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
986 ysize = rect.bottom / rows;
990 for (r = 1; r <= rows && *pWnd; r++, i++)
992 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
993 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
1000 HeapFree( GetProcessHeap(), 0, win_array );
1001 if (has_icons) ArrangeIconicWindows( client );
1004 /* ----------------------- Frame window ---------------------------- */
1007 /**********************************************************************
1008 * MDI_AugmentFrameMenu
1010 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
1012 HMENU menu = GetMenu( frame );
1013 WND* child = WIN_FindWndPtr(hChild);
1014 HMENU hSysPopup = 0;
1015 HBITMAP hSysMenuBitmap = 0;
1017 TRACE("frame %p,child %p\n",frame,hChild);
1019 if( !menu || !child->hSysMenu )
1021 WIN_ReleaseWndPtr(child);
1024 WIN_ReleaseWndPtr(child);
1026 /* create a copy of sysmenu popup and insert it into frame menu bar */
1028 if (!(hSysPopup = LoadMenuA(GetModuleHandleA("USER32"), "SYSMENU")))
1031 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1032 SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
1033 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1034 SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
1036 /* In Win 95 look, the system menu is replaced by the child icon */
1038 if(TWEAK_WineLook > WIN31_LOOK)
1040 HICON hIcon = (HICON)GetClassLongA(hChild, GCL_HICONSM);
1042 hIcon = (HICON)GetClassLongA(hChild, GCL_HICON);
1046 HBITMAP hBitmap, hOldBitmap;
1048 HDC hdc = GetDC(hChild);
1053 cx = GetSystemMetrics(SM_CXSMICON);
1054 cy = GetSystemMetrics(SM_CYSMICON);
1055 hMemDC = CreateCompatibleDC(hdc);
1056 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
1057 hOldBitmap = SelectObject(hMemDC, hBitmap);
1058 SetMapMode(hMemDC, MM_TEXT);
1059 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
1060 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
1061 SelectObject (hMemDC, hOldBitmap);
1062 DeleteObject(hBrush);
1064 ReleaseDC(hChild, hdc);
1065 hSysMenuBitmap = hBitmap;
1070 hSysMenuBitmap = hBmpClose;
1072 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
1073 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
1075 TRACE("not inserted\n");
1076 DestroyMenu(hSysPopup);
1080 /* The close button is only present in Win 95 look */
1081 if(TWEAK_WineLook > WIN31_LOOK)
1083 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1084 SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
1087 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
1088 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
1089 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
1090 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
1098 /**********************************************************************
1099 * MDI_RestoreFrameMenu
1101 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
1103 MENUITEMINFOW menuInfo;
1104 HMENU menu = GetMenu( frame );
1105 INT nItems = GetMenuItemCount(menu) - 1;
1106 UINT iId = GetMenuItemID(menu,nItems) ;
1108 TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
1110 if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
1114 * Remove the system menu, If that menu is the icon of the window
1115 * as it is in win95, we have to delete the bitmap.
1117 memset(&menuInfo, 0, sizeof(menuInfo));
1118 menuInfo.cbSize = sizeof(menuInfo);
1119 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
1121 GetMenuItemInfoW(menu,
1126 RemoveMenu(menu,0,MF_BYPOSITION);
1128 if ( (menuInfo.fType & MFT_BITMAP) &&
1129 (LOWORD(menuInfo.dwTypeData)!=0) &&
1130 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
1132 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
1135 if(TWEAK_WineLook > WIN31_LOOK)
1138 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1141 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1143 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1151 /**********************************************************************
1152 * MDI_UpdateFrameText
1154 * used when child window is maximized/restored
1156 * Note: lpTitle can be NULL
1158 static void MDI_UpdateFrameText( HWND frame, HWND hClient,
1159 BOOL repaint, LPCWSTR lpTitle )
1161 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
1162 MDICLIENTINFO *ci = get_client_info( hClient );
1164 TRACE("repaint %i, frameText %s\n", repaint, debugstr_w(lpTitle));
1168 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
1170 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
1174 /* store new "default" title if lpTitle is not NULL */
1177 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1178 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
1179 strcpyW( ci->frameTitle, lpTitle );
1184 if (ci->hwndChildMaximized)
1186 /* combine frame title and child title if possible */
1188 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
1189 static const WCHAR lpBracket2[] = {']',0};
1190 int i_frame_text_length = strlenW(ci->frameTitle);
1192 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1194 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1196 strcatW( lpBuffer, lpBracket );
1197 if (GetWindowTextW( ci->hwndChildMaximized, lpBuffer + i_frame_text_length + 4,
1198 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1199 strcatW( lpBuffer, lpBracket2 );
1201 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1206 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1212 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1213 if( repaint == MDI_REPAINTFRAME)
1214 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1215 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1219 /* ----------------------------- Interface ---------------------------- */
1222 /**********************************************************************
1223 * MDIClientWndProc_common
1225 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1226 WPARAM wParam, LPARAM lParam, BOOL unicode )
1230 if (!(ci = get_client_info( hwnd ))) return 0;
1237 /* Since we are using only cs->lpCreateParams, we can safely
1238 * cast to LPCREATESTRUCTA here */
1239 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1240 WND *wndPtr = WIN_GetPtr( hwnd );
1242 /* Translation layer doesn't know what's in the cs->lpCreateParams
1243 * so we have to keep track of what environment we're in. */
1245 if( wndPtr->flags & WIN_ISWIN32 )
1247 #define ccs ((LPCLIENTCREATESTRUCT)cs->lpCreateParams)
1248 ci->hWindowMenu = ccs->hWindowMenu;
1249 ci->idFirstChild = ccs->idFirstChild;
1254 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1255 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1256 ci->idFirstChild = ccs->idFirstChild;
1258 WIN_ReleasePtr( wndPtr );
1260 ci->hwndChildMaximized = 0;
1261 ci->nActiveChildren = 0;
1262 ci->nTotalCreated = 0;
1263 ci->frameTitle = NULL;
1265 SetWindowLongW( hwnd, GWL_STYLE, GetWindowLongW(hwnd,GWL_STYLE) | WS_CLIPCHILDREN );
1267 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1269 if (ci->hWindowMenu != 0)
1270 AppendMenuW( ci->hWindowMenu, MF_SEPARATOR, 0, NULL );
1272 GetClientRect( GetParent(hwnd), &rect);
1273 MoveWindow( hwnd, 0, 0, rect.right, rect.bottom, FALSE );
1275 MDI_UpdateFrameText( GetParent(hwnd), hwnd, MDI_NOFRAMEREPAINT, NULL);
1277 TRACE("Client created - hwnd = %p, idFirst = %u\n", hwnd, ci->idFirstChild );
1284 if( ci->hwndChildMaximized )
1285 MDI_RestoreFrameMenu( GetParent(hwnd), ci->hwndChildMaximized);
1286 if((ci->hWindowMenu != 0) &&
1287 (nItems = GetMenuItemCount(ci->hWindowMenu)) > 0)
1289 ci->idFirstChild = nItems - 1;
1290 ci->nActiveChildren++; /* to delete a separator */
1291 while( ci->nActiveChildren-- )
1292 DeleteMenu(ci->hWindowMenu,MF_BYPOSITION,ci->idFirstChild--);
1294 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1298 case WM_MDIACTIVATE:
1299 if( ci->hwndActiveChild != (HWND)wParam )
1300 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1304 return MDICascade(hwnd, ci);
1308 return (LRESULT)MDICreateChild( hwnd, ci, (MDICREATESTRUCTA *)lParam, unicode );
1312 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1314 case WM_MDIGETACTIVE:
1315 if (lParam) *(BOOL *)lParam = (ci->hwndChildMaximized != 0);
1316 return (LRESULT)ci->hwndActiveChild;
1318 case WM_MDIICONARRANGE:
1319 ci->mdiFlags |= MDIF_NEEDUPDATE;
1320 ArrangeIconicWindows( hwnd );
1321 ci->sbRecalc = SB_BOTH+1;
1322 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1325 case WM_MDIMAXIMIZE:
1326 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1329 case WM_MDINEXT: /* lParam != 0 means previous window */
1330 MDI_SwitchActiveChild( hwnd, WIN_GetFullHandle( (HWND)wParam ), !lParam );
1334 SendMessageW( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1338 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1340 case WM_MDIREFRESHMENU:
1341 return MDIRefreshMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1344 ci->mdiFlags |= MDIF_NEEDUPDATE;
1345 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1346 MDITile( hwnd, ci, wParam );
1347 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1352 ci->mdiFlags |= MDIF_NEEDUPDATE;
1353 ScrollChildren( hwnd, message, wParam, lParam );
1354 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1358 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1359 SetFocus( ci->hwndActiveChild );
1363 if( ci->hwndActiveChild )
1364 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1367 case WM_PARENTNOTIFY:
1368 if (LOWORD(wParam) == WM_LBUTTONDOWN)
1372 pt.x = SLOWORD(lParam);
1373 pt.y = SHIWORD(lParam);
1374 child = ChildWindowFromPoint(hwnd, pt);
1376 TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1378 if( child && child != hwnd && child != ci->hwndActiveChild )
1379 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1384 if( IsWindow(ci->hwndChildMaximized) )
1390 rect.right = LOWORD(lParam);
1391 rect.bottom = HIWORD(lParam);
1393 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndChildMaximized,GWL_STYLE),
1394 0, GetWindowLongA(ci->hwndChildMaximized,GWL_EXSTYLE) );
1395 MoveWindow(ci->hwndChildMaximized, rect.left, rect.top,
1396 rect.right - rect.left, rect.bottom - rect.top, 1);
1399 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1403 case WM_MDICALCCHILDSCROLL:
1404 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1406 CalcChildScroll(hwnd, ci->sbRecalc-1);
1408 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1412 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1413 DefWindowProcA( hwnd, message, wParam, lParam );
1416 /***********************************************************************
1419 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1421 if (!IsWindow(hwnd)) return 0;
1422 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1425 /***********************************************************************
1428 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1430 if (!IsWindow(hwnd)) return 0;
1431 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1434 /***********************************************************************
1435 * DefFrameProc (USER.445)
1437 LRESULT WINAPI DefFrameProc16( HWND16 hwnd, HWND16 hwndMDIClient,
1438 UINT16 message, WPARAM16 wParam, LPARAM lParam )
1443 lParam = (LPARAM)MapSL(lParam);
1449 return DefFrameProcA( WIN_Handle32(hwnd), WIN_Handle32(hwndMDIClient),
1450 message, wParam, lParam );
1454 MDINEXTMENU next_menu;
1455 DefFrameProcW( WIN_Handle32(hwnd), WIN_Handle32(hwndMDIClient),
1456 message, wParam, (LPARAM)&next_menu );
1457 return MAKELONG( HMENU_16(next_menu.hmenuNext), HWND_16(next_menu.hwndNext) );
1460 return DefWindowProc16(hwnd, message, wParam, lParam);
1465 /***********************************************************************
1466 * DefFrameProcA (USER32.@)
1468 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1469 UINT message, WPARAM wParam, LPARAM lParam)
1477 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1478 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1479 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1480 MDI_UpdateFrameText(hwnd, hwndMDIClient, MDI_REPAINTFRAME, text );
1481 HeapFree( GetProcessHeap(), 0, text );
1483 return 1; /* success. FIXME: check text length */
1490 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1493 return DefWindowProcA(hwnd, message, wParam, lParam);
1497 /***********************************************************************
1498 * DefFrameProcW (USER32.@)
1500 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1501 UINT message, WPARAM wParam, LPARAM lParam)
1503 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1511 WORD id = LOWORD(wParam);
1512 /* check for possible syscommands for maximized MDI child */
1513 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1515 if( (id - 0xf000) & 0xf00f ) break;
1516 if( !ci->hwndChildMaximized ) break;
1527 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1534 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1535 /* User chose "More Windows..." */
1536 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1538 /* User chose one of the windows listed in the "Windows" menu */
1539 childHwnd = MDI_GetChildByID(hwndMDIClient,id);
1542 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1548 SendMessageW(hwndMDIClient, message, wParam, lParam);
1552 MDI_UpdateFrameText(hwnd, hwndMDIClient, MDI_REPAINTFRAME, (LPWSTR)lParam );
1553 return 1; /* success. FIXME: check text length */
1556 SetFocus(hwndMDIClient);
1560 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1565 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1567 if (!IsIconic(hwnd) && ci->hwndActiveChild && !ci->hwndChildMaximized)
1569 /* control menu is between the frame system menu and
1570 * the first entry of menu bar */
1571 WND *wndPtr = WIN_GetPtr(hwnd);
1573 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1574 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1576 WIN_ReleasePtr(wndPtr);
1577 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1578 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1579 next_menu->hwndNext = ci->hwndActiveChild;
1581 WIN_ReleasePtr(wndPtr);
1588 return DefWindowProcW( hwnd, message, wParam, lParam );
1592 /***********************************************************************
1593 * DefMDIChildProc (USER.447)
1595 LRESULT WINAPI DefMDIChildProc16( HWND16 hwnd, UINT16 message,
1596 WPARAM16 wParam, LPARAM lParam )
1601 return DefMDIChildProcA( WIN_Handle32(hwnd), message, wParam, (LPARAM)MapSL(lParam) );
1605 case WM_CHILDACTIVATE:
1610 return DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, lParam );
1611 case WM_GETMINMAXINFO:
1613 MINMAXINFO16 *mmi16 = (MINMAXINFO16 *)MapSL(lParam);
1615 STRUCT32_MINMAXINFO16to32( mmi16, &mmi );
1616 DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, (LPARAM)&mmi );
1617 STRUCT32_MINMAXINFO32to16( &mmi, mmi16 );
1622 MDINEXTMENU next_menu;
1623 DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, (LPARAM)&next_menu );
1624 return MAKELONG( HMENU_16(next_menu.hmenuNext), HWND_16(next_menu.hwndNext) );
1627 return DefWindowProc16(hwnd, message, wParam, lParam);
1632 /***********************************************************************
1633 * DefMDIChildProcA (USER32.@)
1635 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1636 WPARAM wParam, LPARAM lParam )
1638 HWND client = GetParent(hwnd);
1639 MDICLIENTINFO *ci = get_client_info( client );
1641 hwnd = WIN_GetFullHandle( hwnd );
1642 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1647 DefWindowProcA(hwnd, message, wParam, lParam);
1648 MDI_MenuModifyItem( client, hwnd );
1649 if( ci->hwndChildMaximized == hwnd )
1650 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1651 return 1; /* success. FIXME: check text length */
1653 case WM_GETMINMAXINFO:
1657 case WM_CHILDACTIVATE:
1663 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1665 return DefWindowProcA(hwnd, message, wParam, lParam);
1669 /***********************************************************************
1670 * DefMDIChildProcW (USER32.@)
1672 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1673 WPARAM wParam, LPARAM lParam )
1675 HWND client = GetParent(hwnd);
1676 MDICLIENTINFO *ci = get_client_info( client );
1678 hwnd = WIN_GetFullHandle( hwnd );
1679 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1684 DefWindowProcW(hwnd, message, wParam, lParam);
1685 MDI_MenuModifyItem( client, hwnd );
1686 if( ci->hwndChildMaximized == hwnd )
1687 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1688 return 1; /* success. FIXME: check text length */
1690 case WM_GETMINMAXINFO:
1691 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1695 return 0x00010000; /* MDI children don't have menu bars */
1698 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1702 if (ci->hwndActiveChild != hwnd) MDI_ChildActivate( client, hwnd );
1705 case WM_CHILDACTIVATE:
1706 MDI_ChildActivate( client, hwnd );
1713 if( ci->hwndChildMaximized == hwnd) return 0;
1717 SetWindowLongW( hwnd, GWL_STYLE,
1718 GetWindowLongW( hwnd, GWL_STYLE ) | WS_SYSMENU );
1721 if (ci->hwndChildMaximized == hwnd)
1722 return SendMessageW( GetParent(client), message, wParam, lParam);
1723 SetWindowLongW( hwnd, GWL_STYLE,
1724 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_SYSMENU );
1727 SendMessageW( client, WM_MDINEXT, 0, 0);
1730 SendMessageW( client, WM_MDINEXT, 0, 1);
1736 if( ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1737 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1741 if( ci->hwndActiveChild == hwnd && wParam != SIZE_MAXIMIZED )
1743 ci->hwndChildMaximized = 0;
1744 MDI_RestoreFrameMenu( GetParent(client), hwnd );
1745 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1748 if( wParam == SIZE_MAXIMIZED )
1750 HWND hMaxChild = ci->hwndChildMaximized;
1752 if( hMaxChild == hwnd ) break;
1755 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1756 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1757 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1758 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1760 TRACE("maximizing child %p\n", hwnd );
1762 /* keep track of the maximized window. */
1763 ci->hwndChildMaximized = hwnd; /* !!! */
1765 /* The maximized window should also be the active window */
1766 MDI_ChildActivate( client, hwnd );
1767 MDI_AugmentFrameMenu( GetParent(client), hwnd );
1768 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1771 if( wParam == SIZE_MINIMIZED )
1773 HWND switchTo = MDI_GetWindow(ci, hwnd, TRUE, WS_MINIMIZE);
1775 if (switchTo) SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0);
1777 MDI_PostUpdate(client, ci, SB_BOTH+1);
1782 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1783 HWND parent = GetParent(client);
1785 if( wParam == VK_LEFT ) /* switch to frame system menu */
1787 WND *wndPtr = WIN_GetPtr( parent );
1788 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1789 WIN_ReleasePtr( wndPtr );
1791 if( wParam == VK_RIGHT ) /* to frame menu bar */
1793 next_menu->hmenuNext = GetMenu(parent);
1795 next_menu->hwndNext = parent;
1802 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1807 return DefWindowProcW(hwnd, message, wParam, lParam);
1810 /**********************************************************************
1811 * CreateMDIWindowA (USER32.@) Creates a MDI child
1814 * Success: Handle to created window
1817 HWND WINAPI CreateMDIWindowA(
1818 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1819 LPCSTR lpWindowName, /* [in] Pointer to window name */
1820 DWORD dwStyle, /* [in] Window style */
1821 INT X, /* [in] Horizontal position of window */
1822 INT Y, /* [in] Vertical position of window */
1823 INT nWidth, /* [in] Width of window */
1824 INT nHeight, /* [in] Height of window */
1825 HWND hWndParent, /* [in] Handle to parent window */
1826 HINSTANCE hInstance, /* [in] Handle to application instance */
1827 LPARAM lParam) /* [in] Application-defined value */
1829 MDICLIENTINFO *pCi = get_client_info( hWndParent );
1830 MDICREATESTRUCTA cs;
1832 TRACE("(%s,%s,%ld,%d,%d,%d,%d,%p,%p,%ld)\n",
1833 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1834 nWidth,nHeight,hWndParent,hInstance,lParam);
1838 ERR("bad hwnd for MDI-client: %p\n", hWndParent);
1841 cs.szClass=lpClassName;
1842 cs.szTitle=lpWindowName;
1843 cs.hOwner=hInstance;
1851 return MDICreateChild(hWndParent, pCi, &cs, FALSE);
1854 /***********************************************************************
1855 * CreateMDIWindowW (USER32.@) Creates a MDI child
1858 * Success: Handle to created window
1861 HWND WINAPI CreateMDIWindowW(
1862 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1863 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1864 DWORD dwStyle, /* [in] Window style */
1865 INT X, /* [in] Horizontal position of window */
1866 INT Y, /* [in] Vertical position of window */
1867 INT nWidth, /* [in] Width of window */
1868 INT nHeight, /* [in] Height of window */
1869 HWND hWndParent, /* [in] Handle to parent window */
1870 HINSTANCE hInstance, /* [in] Handle to application instance */
1871 LPARAM lParam) /* [in] Application-defined value */
1873 MDICLIENTINFO *pCi = get_client_info( hWndParent );
1874 MDICREATESTRUCTW cs;
1876 TRACE("(%s,%s,%ld,%d,%d,%d,%d,%p,%p,%ld)\n",
1877 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1878 nWidth, nHeight, hWndParent, hInstance, lParam);
1882 ERR("bad hwnd for MDI-client: %p\n", hWndParent);
1885 cs.szClass = lpClassName;
1886 cs.szTitle = lpWindowName;
1887 cs.hOwner = hInstance;
1895 return MDICreateChild(hWndParent, pCi, (MDICREATESTRUCTA *)&cs, TRUE);
1898 /**********************************************************************
1899 * TranslateMDISysAccel (USER32.@)
1901 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1903 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1905 MDICLIENTINFO *ci = get_client_info( hwndClient );
1908 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1910 /* translate if the Ctrl key is down and Alt not. */
1912 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1914 switch( msg->wParam )
1918 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1927 TRACE("wParam = %04x\n", wParam);
1928 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1932 return 0; /* failure */
1935 /***********************************************************************
1936 * CalcChildScroll (USER32.@)
1938 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1941 RECT childRect, clientRect;
1944 GetClientRect( hwnd, &clientRect );
1945 SetRectEmpty( &childRect );
1947 if ((list = WIN_ListChildren( hwnd )))
1950 for (i = 0; list[i]; i++)
1952 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1953 if (style & WS_MAXIMIZE)
1955 HeapFree( GetProcessHeap(), 0, list );
1956 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1959 if (style & WS_VISIBLE)
1961 WND *pWnd = WIN_FindWndPtr( list[i] );
1962 UnionRect( &childRect, &pWnd->rectWindow, &childRect );
1963 WIN_ReleaseWndPtr( pWnd );
1966 HeapFree( GetProcessHeap(), 0, list );
1968 UnionRect( &childRect, &clientRect, &childRect );
1970 /* set common info values */
1971 info.cbSize = sizeof(info);
1972 info.fMask = SIF_POS | SIF_RANGE;
1974 /* set the specific */
1979 info.nMin = childRect.left;
1980 info.nMax = childRect.right - clientRect.right;
1981 info.nPos = clientRect.left - childRect.left;
1982 SetScrollInfo(hwnd, scroll, &info, TRUE);
1983 if (scroll == SB_HORZ) break;
1986 info.nMin = childRect.top;
1987 info.nMax = childRect.bottom - clientRect.bottom;
1988 info.nPos = clientRect.top - childRect.top;
1989 SetScrollInfo(hwnd, scroll, &info, TRUE);
1995 /***********************************************************************
1996 * ScrollChildren (USER32.@)
1998 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
2002 INT curPos, length, minPos, maxPos, shift;
2005 GetClientRect( hWnd, &rect );
2010 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
2011 curPos = GetScrollPos(hWnd,SB_HORZ);
2012 length = (rect.right - rect.left) / 2;
2013 shift = GetSystemMetrics(SM_CYHSCROLL);
2016 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
2017 curPos = GetScrollPos(hWnd,SB_VERT);
2018 length = (rect.bottom - rect.top) / 2;
2019 shift = GetSystemMetrics(SM_CXVSCROLL);
2028 newPos = curPos - shift;
2031 newPos = curPos + shift;
2034 newPos = curPos - length;
2037 newPos = curPos + length;
2040 case SB_THUMBPOSITION:
2041 newPos = LOWORD(lParam);
2054 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
2058 if( newPos > maxPos )
2061 if( newPos < minPos )
2064 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
2066 if( uMsg == WM_VSCROLL )
2067 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
2068 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
2070 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
2071 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
2075 /******************************************************************************
2076 * CascadeWindows (USER32.@) Cascades MDI child windows
2079 * Success: Number of cascaded windows.
2083 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
2084 UINT cKids, const HWND *lpKids)
2086 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
2091 /******************************************************************************
2092 * TileWindows (USER32.@) Tiles MDI child windows
2095 * Success: Number of tiled windows.
2099 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
2100 UINT cKids, const HWND *lpKids)
2102 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
2106 /************************************************************************
2107 * "More Windows..." functionality
2110 /* MDI_MoreWindowsDlgProc
2112 * This function will process the messages sent to the "More Windows..."
2114 * Return values: 0 = cancel pressed
2115 * HWND = ok pressed or double-click in the list...
2119 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
2128 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
2129 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2130 HWND *list, *sorted_list;
2132 if (!(list = WIN_ListChildren( (HWND)lParam ))) return TRUE;
2133 if (!(sorted_list = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
2134 sizeof(HWND) * ci->nActiveChildren )))
2136 HeapFree( GetProcessHeap(), 0, list );
2140 /* Fill the list, sorted by id... */
2141 for (i = 0; list[i]; i++)
2143 UINT id = GetWindowLongW( list[i], GWL_ID ) - ci->idFirstChild;
2144 if (id < ci->nActiveChildren) sorted_list[id] = list[i];
2146 HeapFree( GetProcessHeap(), 0, list );
2148 for (i = 0; i < ci->nActiveChildren; i++)
2152 if (!GetWindowTextW( sorted_list[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
2154 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
2155 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)sorted_list[i] );
2156 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
2157 if (length > widest)
2160 /* Make sure the horizontal scrollbar scrolls ok */
2161 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
2163 /* Set the current selection */
2164 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
2169 switch (LOWORD(wParam))
2172 if (HIWORD(wParam) != LBN_DBLCLK) break;
2176 /* windows are sorted by menu ID, so we must return the
2177 * window associated to the given id
2179 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2180 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
2181 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
2182 EndDialog(hDlg, res);
2196 * MDI_MoreWindowsDialog
2198 * Prompts the user with a listbox containing the opened
2199 * documents. The user can then choose a windows and click
2200 * on OK to set the current window to the one selected, or
2201 * CANCEL to cancel. The function returns a handle to the
2205 static HWND MDI_MoreWindowsDialog(HWND hwnd)
2211 hRes = FindResourceA(GetModuleHandleA("USER32"), "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
2216 hDlgTmpl = LoadResource(GetModuleHandleA("USER32"), hRes );
2221 template = LockResource( hDlgTmpl );
2226 return (HWND) DialogBoxIndirectParamA(GetModuleHandleA("USER32"),
2227 (LPDLGTEMPLATEA) template,
2228 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);
2235 * Will swap the menu IDs for the given 2 positions.
2236 * pos1 and pos2 are menu IDs
2241 static void MDI_SwapMenuItems(HWND parent, UINT pos1, UINT pos2)
2246 if (!(list = WIN_ListChildren( parent ))) return;
2247 for (i = 0; list[i]; i++)
2249 UINT id = GetWindowLongW( list[i], GWL_ID );
2250 if (id == pos1) SetWindowLongW( list[i], GWL_ID, pos2 );
2251 else if (id == pos2) SetWindowLongW( list[i], GWL_ID, pos1 );
2253 HeapFree( GetProcessHeap(), 0, list );