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.
93 #include "wine/unicode.h"
95 #include "nonclient.h"
99 #include "wine/debug.h"
102 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
104 #define MDI_MAXLISTLENGTH 0x40
105 #define MDI_MAXTITLELENGTH 0xa1
107 #define MDI_NOFRAMEREPAINT 0
108 #define MDI_REPAINTFRAMENOW 1
109 #define MDI_REPAINTFRAME 2
111 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
113 /* "More Windows..." definitions */
114 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
115 option will appear under the Windows menu */
116 #define MDI_IDC_LISTBOX 100
117 #define IDS_MDI_MOREWINDOWS 13
119 #define MDIF_NEEDUPDATE 0x0001
123 UINT nActiveChildren;
124 HWND hwndChildMaximized;
125 HWND hwndActiveChild;
131 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
134 static HBITMAP hBmpClose = 0;
136 /* ----------------- declarations ----------------- */
137 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
138 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
139 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
140 static LONG MDI_ChildActivate( HWND, HWND );
142 static HWND MDI_MoreWindowsDialog(HWND);
143 static void MDI_SwapMenuItems(HWND, UINT, UINT);
144 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
145 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
147 /* -------- Miscellaneous service functions ----------
151 static HWND MDI_GetChildByID(HWND hwnd, UINT id)
157 if (!(win_array = WIN_ListChildren( hwnd ))) return 0;
158 for (i = 0; win_array[i]; i++)
160 if (GetWindowLongA( win_array[i], GWL_ID ) == id) break;
163 HeapFree( GetProcessHeap(), 0, win_array );
167 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
169 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
171 ci->mdiFlags |= MDIF_NEEDUPDATE;
172 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
174 ci->sbRecalc = recalc;
178 /*********************************************************************
179 * MDIClient class descriptor
181 const struct builtin_class_descr MDICLIENT_builtin_class =
183 "MDIClient", /* name */
184 CS_GLOBALCLASS, /* style */
185 MDIClientWndProcA, /* procA */
186 MDIClientWndProcW, /* procW */
187 sizeof(MDICLIENTINFO), /* extra */
188 IDC_ARROWA, /* cursor */
189 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
193 static MDICLIENTINFO *get_client_info( HWND client )
195 MDICLIENTINFO *ret = NULL;
196 WND *win = WIN_GetPtr( client );
199 if (win == WND_OTHER_PROCESS)
201 ERR( "client %p belongs to other process\n", client );
204 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
205 else ret = (MDICLIENTINFO *)win->wExtra;
206 WIN_ReleasePtr( win );
211 /**********************************************************************
214 static void MDI_MenuModifyItem( HWND client, HWND hWndChild )
216 MDICLIENTINFO *clientInfo = get_client_info( client );
220 if (!clientInfo || !clientInfo->hWindowMenu) return;
222 id = GetWindowLongA( hWndChild, GWL_ID );
223 if (id >= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT) return;
225 buffer[1] = '1' + id - clientInfo->idFirstChild;
227 GetWindowTextW( hWndChild, buffer + 3, sizeof(buffer)/sizeof(WCHAR) - 3 );
229 n = GetMenuState(clientInfo->hWindowMenu, id, MF_BYCOMMAND);
230 ModifyMenuW(clientInfo->hWindowMenu, id, MF_BYCOMMAND | MF_STRING, id, buffer );
231 CheckMenuItem(clientInfo->hWindowMenu, id, n & MF_CHECKED);
234 /**********************************************************************
237 static BOOL MDI_MenuDeleteItem( HWND client, HWND hWndChild )
240 static const WCHAR format[] = {'&','%','d',' ',0};
241 MDICLIENTINFO *clientInfo = get_client_info( client );
244 if( !clientInfo->nActiveChildren || !clientInfo->hWindowMenu )
247 id = GetWindowLongA( hWndChild, GWL_ID );
248 DeleteMenu(clientInfo->hWindowMenu,id,MF_BYCOMMAND);
250 /* walk the rest of MDI children to prevent gaps in the id
251 * sequence and in the menu child list */
253 for( index = id+1; index <= clientInfo->nActiveChildren +
254 clientInfo->idFirstChild; index++ )
256 HWND hwnd = MDI_GetChildByID(client,index);
259 TRACE("no window for id=%i\n",index);
264 SetWindowLongW( hwnd, GWL_ID, GetWindowLongW( hwnd, GWL_ID ) - 1 );
266 n = wsprintfW(buffer, format ,index - clientInfo->idFirstChild);
267 GetWindowTextW( hwnd, buffer + n, sizeof(buffer)/sizeof(WCHAR) - n );
269 /* change menu if the current child is to be shown in the
272 if (index <= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT)
273 ModifyMenuW(clientInfo->hWindowMenu ,index ,MF_BYCOMMAND | MF_STRING,
274 index - 1 , buffer );
277 /* We must restore the "More Windows..." option if there are enough children
279 if (clientInfo->nActiveChildren - 1 > MDI_MOREWINDOWSLIMIT)
282 LoadStringW(GetModuleHandleA("USER32"), IDS_MDI_MOREWINDOWS, szTmp, sizeof(szTmp)/sizeof(szTmp[0]));
283 AppendMenuW(clientInfo->hWindowMenu, MF_STRING, clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT, szTmp);
288 /**********************************************************************
291 * returns "activateable" child different from the current or zero
293 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
300 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
301 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
303 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
305 /* start from next after hWnd */
306 while (list[i] && list[i] != hWnd) i++;
309 for ( ; list[i]; i++)
311 if (GetWindow( list[i], GW_OWNER )) continue;
312 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
314 if (bNext) goto found;
316 /* now restart from the beginning */
317 for (i = 0; list[i] && list[i] != hWnd; i++)
319 if (GetWindow( list[i], GW_OWNER )) continue;
320 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
322 if (bNext) goto found;
325 HeapFree( GetProcessHeap(), 0, list );
329 /**********************************************************************
330 * MDI_CalcDefaultChildPos
332 * It seems that the default height is about 2/3 of the client rect
334 static void MDI_CalcDefaultChildPos( HWND hwnd, WORD n, LPPOINT lpPos, INT delta)
338 INT spacing = GetSystemMetrics(SM_CYCAPTION) +
339 GetSystemMetrics(SM_CYFRAME) - 1;
341 GetClientRect( hwnd, &rect );
342 if( rect.bottom - rect.top - delta >= spacing )
343 rect.bottom -= delta;
345 nstagger = (rect.bottom - rect.top)/(3 * spacing);
346 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
347 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
348 lpPos[0].x = lpPos[0].y = spacing * (n%(nstagger+1));
351 /**********************************************************************
354 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
358 HWND hwndFrame = GetParent(hwnd);
359 HMENU oldFrameMenu = GetMenu(hwndFrame);
361 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
363 if (hmenuFrame && !IsMenu(hmenuFrame))
365 WARN("hmenuFrame is not a menu handle\n");
369 if (hmenuWindow && !IsMenu(hmenuWindow))
371 WARN("hmenuWindow is not a menu handle\n");
375 if (!(ci = get_client_info( hwnd ))) return 0;
377 if( ci->hwndChildMaximized && hmenuFrame && hmenuFrame!=oldFrameMenu )
378 MDI_RestoreFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
380 if( hmenuWindow && ci->hWindowMenu && hmenuWindow!=ci->hWindowMenu )
382 /* delete menu items from ci->hWindowMenu
383 * and add them to hmenuWindow */
385 INT i = GetMenuItemCount(ci->hWindowMenu) - 1;
386 INT pos = GetMenuItemCount(hmenuWindow) + 1;
388 AppendMenuA( hmenuWindow, MF_SEPARATOR, 0, NULL);
390 if( ci->nActiveChildren )
393 LPWSTR buffer = NULL;
395 INT nbWindowsMenuItems; /* num of documents shown + "More Windows..." if present */
397 if (ci->nActiveChildren <= MDI_MOREWINDOWSLIMIT)
398 nbWindowsMenuItems = ci->nActiveChildren;
400 nbWindowsMenuItems = MDI_MOREWINDOWSLIMIT + 1;
402 j = i - nbWindowsMenuItems + 1;
404 for( ; i >= j ; i-- )
406 memset(&mii, 0, sizeof(mii));
407 mii.cbSize = sizeof(mii);
408 mii.fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_ID | MIIM_STATE
409 | MIIM_SUBMENU | MIIM_TYPE | MIIM_BITMAP;
411 GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
412 if(mii.cch) { /* Menu is MFT_STRING */
413 mii.cch++; /* add room for '\0' */
414 buffer = HeapAlloc(GetProcessHeap(), 0,
415 mii.cch * sizeof(WCHAR));
416 mii.dwTypeData = buffer;
417 GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
419 DeleteMenu(ci->hWindowMenu, i, MF_BYPOSITION);
420 InsertMenuItemW(hmenuWindow, pos, TRUE, &mii);
422 HeapFree(GetProcessHeap(), 0, buffer);
428 /* remove separator */
429 DeleteMenu(ci->hWindowMenu, i, MF_BYPOSITION);
431 ci->hWindowMenu = hmenuWindow;
436 SetMenu(hwndFrame, hmenuFrame);
437 if( hmenuFrame!=oldFrameMenu )
439 if( ci->hwndChildMaximized )
440 MDI_AugmentFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
441 return (LRESULT)oldFrameMenu;
446 HMENU menu = GetMenu( GetParent(hwnd) );
447 INT nItems = GetMenuItemCount(menu) - 1;
448 UINT iId = GetMenuItemID(menu,nItems) ;
450 if( !(iId == SC_RESTORE || iId == SC_CLOSE) )
452 /* SetMenu() may already have been called, meaning that this window
453 * already has its menu. But they may have done a SetMenu() on
454 * an MDI window, and called MDISetMenu() after the fact, meaning
455 * that the "if" to this "else" wouldn't catch the need to
456 * augment the frame menu.
458 if( ci->hwndChildMaximized )
459 MDI_AugmentFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
465 /**********************************************************************
468 static LRESULT MDIRefreshMenu( HWND hwnd, HMENU hmenuFrame,
471 HWND hwndFrame = GetParent(hwnd);
472 HMENU oldFrameMenu = GetMenu(hwndFrame);
474 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
476 FIXME("partially function stub\n");
478 return (LRESULT)oldFrameMenu;
482 /* ------------------ MDI child window functions ---------------------- */
485 /**********************************************************************
488 static HWND MDICreateChild( HWND parent, MDICLIENTINFO *ci,
489 LPMDICREATESTRUCTA cs, BOOL unicode )
492 DWORD style = cs->style | (WS_CHILD | WS_CLIPSIBLINGS);
493 HWND hwnd, hwndMax = 0;
494 UINT wIDmenu = ci->idFirstChild + ci->nActiveChildren;
496 static const WCHAR lpstrDef[] = {'j','u','n','k','!',0};
498 TRACE("origin %i,%i - dim %i,%i, style %08lx\n",
499 cs->x, cs->y, cs->cx, cs->cy, cs->style);
500 /* calculate placement */
501 MDI_CalcDefaultChildPos(parent, ci->nTotalCreated++, pos, 0);
503 if (cs->cx == CW_USEDEFAULT || cs->cx == CW_USEDEFAULT16 || !cs->cx) cs->cx = pos[1].x;
504 if (cs->cy == CW_USEDEFAULT || cs->cy == CW_USEDEFAULT16 || !cs->cy) cs->cy = pos[1].y;
506 if (cs->x == CW_USEDEFAULT || cs->x == CW_USEDEFAULT16)
512 /* restore current maximized child */
513 if( (style & WS_VISIBLE) && ci->hwndChildMaximized )
515 TRACE("Restoring current maximized child %p\n", ci->hwndChildMaximized);
516 if( style & WS_MAXIMIZE )
517 SendMessageW(parent, WM_SETREDRAW, FALSE, 0L);
518 hwndMax = ci->hwndChildMaximized;
519 ShowWindow( hwndMax, SW_SHOWNOACTIVATE );
520 if( style & WS_MAXIMIZE )
521 SendMessageW(parent, WM_SETREDRAW, TRUE, 0L);
524 if (ci->nActiveChildren <= MDI_MOREWINDOWSLIMIT)
525 /* this menu is needed to set a check mark in MDI_ChildActivate */
526 if (ci->hWindowMenu != 0)
527 AppendMenuW(ci->hWindowMenu, MF_STRING, wIDmenu, lpstrDef);
529 ci->nActiveChildren++;
531 /* fix window style */
532 wndParent = WIN_FindWndPtr( parent );
533 if( !(wndParent->dwStyle & MDIS_ALLCHILDSTYLES) )
535 TRACE("MDIS_ALLCHILDSTYLES is missing, fixing window style\n");
536 style &= (WS_CHILD | WS_CLIPSIBLINGS | WS_MINIMIZE | WS_MAXIMIZE |
537 WS_CLIPCHILDREN | WS_DISABLED | WS_VSCROLL | WS_HSCROLL );
538 style |= (WS_VISIBLE | WS_OVERLAPPEDWINDOW);
541 if( wndParent->flags & WIN_ISWIN32 )
543 WIN_ReleaseWndPtr( wndParent );
546 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)cs;
547 hwnd = CreateWindowW( csW->szClass, csW->szTitle, style,
548 csW->x, csW->y, csW->cx, csW->cy, parent,
549 (HMENU)wIDmenu, csW->hOwner, csW );
552 hwnd = CreateWindowA( cs->szClass, cs->szTitle, style,
553 cs->x, cs->y, cs->cx, cs->cy, parent,
554 (HMENU)wIDmenu, cs->hOwner, cs );
558 MDICREATESTRUCT16 cs16;
559 SEGPTR title, cls, seg_cs16;
561 WIN_ReleaseWndPtr( wndParent );
562 STRUCT32_MDICREATESTRUCT32Ato16( cs, &cs16 );
563 cs16.szTitle = title = MapLS( cs->szTitle );
564 cs16.szClass = cls = MapLS( cs->szClass );
565 seg_cs16 = MapLS( &cs16 );
566 hwnd = WIN_Handle32( CreateWindow16( cs->szClass, cs->szTitle, style,
567 cs16.x, cs16.y, cs16.cx, cs16.cy,
568 HWND_16(parent), (HMENU16)wIDmenu,
569 cs16.hOwner, (LPVOID)seg_cs16 ));
575 /* MDI windows are WS_CHILD so they won't be activated by CreateWindow */
579 /* All MDI child windows have the WS_EX_MDICHILD style */
580 SetWindowLongW( hwnd, GWL_EXSTYLE, GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_MDICHILD );
582 /* If we have more than 9 windows, we must insert the new one at the
583 * 9th position in order to see it in the "Windows" menu
585 if (ci->nActiveChildren > MDI_MOREWINDOWSLIMIT)
586 MDI_SwapMenuItems( parent, GetWindowLongW( hwnd, GWL_ID ),
587 ci->idFirstChild + MDI_MOREWINDOWSLIMIT - 1);
589 MDI_MenuModifyItem(parent, hwnd);
591 /* Have we hit the "More Windows..." limit? If so, we must
592 * add a "More Windows..." option
594 if (ci->nActiveChildren == MDI_MOREWINDOWSLIMIT + 1)
597 LoadStringW(GetModuleHandleA("USER32"), IDS_MDI_MOREWINDOWS, szTmp, sizeof(szTmp)/sizeof(szTmp[0]));
599 ModifyMenuW(ci->hWindowMenu,
600 ci->idFirstChild + MDI_MOREWINDOWSLIMIT,
601 MF_BYCOMMAND | MF_STRING,
602 ci->idFirstChild + MDI_MOREWINDOWSLIMIT,
606 if( IsIconic(hwnd) && ci->hwndActiveChild )
608 TRACE("Minimizing created MDI child %p\n", hwnd);
609 ShowWindow( hwnd, SW_SHOWMINNOACTIVE );
613 /* WS_VISIBLE is clear if a) the MDI client has
614 * MDIS_ALLCHILDSTYLES style and 2) the flag is cleared in the
615 * MDICreateStruct. If so the created window is not shown nor
618 if (IsWindowVisible(hwnd)) ShowWindow(hwnd, SW_SHOW);
620 TRACE("created child - %p\n",hwnd);
624 ci->nActiveChildren--;
625 DeleteMenu(ci->hWindowMenu,wIDmenu,MF_BYCOMMAND);
626 if( IsWindow(hwndMax) )
627 ShowWindow(hwndMax, SW_SHOWMAXIMIZED);
633 /**********************************************************************
634 * MDI_ChildGetMinMaxInfo
636 * Note: The rule here is that client rect of the maximized MDI child
637 * is equal to the client rect of the MDI client window.
639 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
643 GetClientRect( client, &rect );
644 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
645 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
647 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
648 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
650 lpMinMax->ptMaxPosition.x = rect.left;
651 lpMinMax->ptMaxPosition.y = rect.top;
653 TRACE("max rect (%ld,%ld - %ld, %ld)\n",
654 rect.left,rect.top,rect.right,rect.bottom);
657 /**********************************************************************
658 * MDI_SwitchActiveChild
660 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
663 static void MDI_SwitchActiveChild( HWND clientHwnd, HWND childHwnd,
668 MDICLIENTINFO *ci = get_client_info( clientHwnd );
670 hwndTo = MDI_GetWindow(ci, childHwnd, bNextWindow, 0);
672 TRACE("from %p, to %p\n",childHwnd,hwndTo);
674 if ( !hwndTo ) return; /* no window to switch to */
676 hwndPrev = ci->hwndActiveChild;
678 if ( hwndTo != hwndPrev )
680 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0,
681 SWP_NOMOVE | SWP_NOSIZE );
683 if( bNextWindow && hwndPrev )
684 SetWindowPos( hwndPrev, HWND_BOTTOM, 0, 0, 0, 0,
685 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE );
690 /**********************************************************************
693 static LRESULT MDIDestroyChild( HWND parent, MDICLIENTINFO *ci,
694 HWND child, BOOL flagDestroy )
696 if( child == ci->hwndActiveChild )
698 MDI_SwitchActiveChild(parent, child, TRUE);
700 if( child == ci->hwndActiveChild )
702 ShowWindow( child, SW_HIDE);
703 if( child == ci->hwndChildMaximized )
705 HWND frame = GetParent(parent);
706 MDI_RestoreFrameMenu( frame, child );
707 ci->hwndChildMaximized = 0;
708 MDI_UpdateFrameText( frame, parent, TRUE, NULL);
711 MDI_ChildActivate(parent, 0);
715 MDI_MenuDeleteItem(parent, child);
717 ci->nActiveChildren--;
719 TRACE("child destroyed - %p\n",child);
723 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
724 DestroyWindow(child);
730 /**********************************************************************
733 * Note: hWndChild is NULL when last child is being destroyed
735 static LONG MDI_ChildActivate( HWND client, HWND child )
737 MDICLIENTINFO *clientInfo = get_client_info( client );
738 HWND prevActiveWnd = clientInfo->hwndActiveChild;
739 BOOL isActiveFrameWnd;
741 if (child && (!IsWindowEnabled( child ))) return 0;
743 /* Don't activate if it is already active. Might happen
744 since ShowWindow DOES activate MDI children */
745 if (clientInfo->hwndActiveChild == child) return 0;
747 TRACE("%p\n", child);
749 isActiveFrameWnd = (GetActiveWindow() == GetParent(client));
751 /* deactivate prev. active child */
754 SetWindowLongA( prevActiveWnd, GWL_STYLE,
755 GetWindowLongA( prevActiveWnd, GWL_STYLE ) | WS_SYSMENU );
756 SendMessageA( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
757 SendMessageA( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
758 /* uncheck menu item */
759 if( clientInfo->hWindowMenu )
761 UINT prevID = GetWindowLongA( prevActiveWnd, GWL_ID );
763 if (prevID - clientInfo->idFirstChild < MDI_MOREWINDOWSLIMIT)
764 CheckMenuItem( clientInfo->hWindowMenu, prevID, 0);
766 CheckMenuItem( clientInfo->hWindowMenu,
767 clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1, 0);
772 if (clientInfo->hwndChildMaximized && clientInfo->hwndChildMaximized != child)
774 INT cmd = SW_SHOWNORMAL;
778 UINT state = GetMenuState(GetSystemMenu(child, FALSE), SC_MAXIMIZE, MF_BYCOMMAND);
779 if (state != 0xFFFFFFFF && (state & (MF_DISABLED | MF_GRAYED)))
780 SendMessageW(clientInfo->hwndChildMaximized, WM_SYSCOMMAND, SC_RESTORE, 0);
782 cmd = SW_SHOWMAXIMIZED;
784 clientInfo->hwndActiveChild = child;
787 ShowWindow( clientInfo->hwndActiveChild, cmd );
790 clientInfo->hwndActiveChild = child;
792 /* check if we have any children left */
795 if( isActiveFrameWnd )
800 /* check menu item */
801 if( clientInfo->hWindowMenu )
803 UINT id = GetWindowLongA( child, GWL_ID );
804 /* The window to be activated must be displayed in the "Windows" menu */
805 if (id >= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT)
807 MDI_SwapMenuItems( GetParent(child),
808 id, clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1);
809 id = clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1;
810 MDI_MenuModifyItem( GetParent(child), child );
813 CheckMenuItem(clientInfo->hWindowMenu, id, MF_CHECKED);
815 /* bring active child to the top */
816 SetWindowPos( child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
818 if( isActiveFrameWnd )
820 SendMessageA( child, WM_NCACTIVATE, TRUE, 0L);
821 if( GetFocus() == client )
822 SendMessageA( client, WM_SETFOCUS, (WPARAM)client, 0L );
826 SendMessageA( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
830 /* -------------------- MDI client window functions ------------------- */
832 /**********************************************************************
833 * CreateMDIMenuBitmap
835 static HBITMAP CreateMDIMenuBitmap(void)
837 HDC hDCSrc = CreateCompatibleDC(0);
838 HDC hDCDest = CreateCompatibleDC(hDCSrc);
839 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
841 HBITMAP hobjSrc, hobjDest;
843 hobjSrc = SelectObject(hDCSrc, hbClose);
844 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
845 hobjDest = SelectObject(hDCDest, hbCopy);
847 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
848 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
850 SelectObject(hDCSrc, hobjSrc);
851 DeleteObject(hbClose);
854 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
856 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
857 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
859 SelectObject(hDCDest, hobjSrc );
860 SelectObject(hDCDest, hobjDest);
866 /**********************************************************************
869 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
872 BOOL has_icons = FALSE;
875 if (ci->hwndChildMaximized)
876 SendMessageA( client, WM_MDIRESTORE,
877 (WPARAM)ci->hwndChildMaximized, 0);
879 if (ci->nActiveChildren == 0) return 0;
881 if (!(win_array = WIN_ListChildren( client ))) return 0;
883 /* remove all the windows we don't want */
884 for (i = total = 0; win_array[i]; i++)
886 if (!IsWindowVisible( win_array[i] )) continue;
887 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
888 if (IsIconic( win_array[i] ))
893 win_array[total++] = win_array[i];
895 win_array[total] = 0;
899 INT delta = 0, n = 0, i;
901 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
903 /* walk the list (backwards) and move windows */
904 for (i = total - 1; i >= 0; i--)
906 TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
907 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
909 MDI_CalcDefaultChildPos(client, n++, pos, delta);
910 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
911 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
914 HeapFree( GetProcessHeap(), 0, win_array );
916 if (has_icons) ArrangeIconicWindows( client );
920 /**********************************************************************
923 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
927 BOOL has_icons = FALSE;
929 if (ci->hwndChildMaximized)
930 SendMessageA( client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
932 if (ci->nActiveChildren == 0) return;
934 if (!(win_array = WIN_ListChildren( client ))) return;
936 /* remove all the windows we don't want */
937 for (i = total = 0; win_array[i]; i++)
939 if (!IsWindowVisible( win_array[i] )) continue;
940 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
941 if (IsIconic( win_array[i] ))
946 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
947 win_array[total++] = win_array[i];
949 win_array[total] = 0;
951 TRACE("%u windows to tile\n", total);
955 HWND *pWnd = win_array;
957 int x, y, xsize, ysize;
958 int rows, columns, r, c, i;
960 GetClientRect(client,&rect);
961 rows = (int) sqrt((double)total);
962 columns = total / rows;
964 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
967 rows = columns; /* exchange r and c */
973 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
974 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
977 ysize = rect.bottom / rows;
978 xsize = rect.right / columns;
980 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
985 ysize = rect.bottom / rows;
989 for (r = 1; r <= rows && *pWnd; r++, i++)
991 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
992 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
999 HeapFree( GetProcessHeap(), 0, win_array );
1000 if (has_icons) ArrangeIconicWindows( client );
1003 /* ----------------------- Frame window ---------------------------- */
1006 /**********************************************************************
1007 * MDI_AugmentFrameMenu
1009 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
1011 HMENU menu = GetMenu( frame );
1012 WND* child = WIN_FindWndPtr(hChild);
1013 HMENU hSysPopup = 0;
1014 HBITMAP hSysMenuBitmap = 0;
1016 TRACE("frame %p,child %p\n",frame,hChild);
1018 if( !menu || !child->hSysMenu )
1020 WIN_ReleaseWndPtr(child);
1023 WIN_ReleaseWndPtr(child);
1025 /* create a copy of sysmenu popup and insert it into frame menu bar */
1027 if (!(hSysPopup = LoadMenuA(GetModuleHandleA("USER32"), "SYSMENU")))
1030 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1031 SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
1032 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1033 SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
1035 /* In Win 95 look, the system menu is replaced by the child icon */
1037 if(TWEAK_WineLook > WIN31_LOOK)
1039 HICON hIcon = (HICON)GetClassLongA(hChild, GCL_HICONSM);
1041 hIcon = (HICON)GetClassLongA(hChild, GCL_HICON);
1045 HBITMAP hBitmap, hOldBitmap;
1047 HDC hdc = GetDC(hChild);
1052 cx = GetSystemMetrics(SM_CXSMICON);
1053 cy = GetSystemMetrics(SM_CYSMICON);
1054 hMemDC = CreateCompatibleDC(hdc);
1055 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
1056 hOldBitmap = SelectObject(hMemDC, hBitmap);
1057 SetMapMode(hMemDC, MM_TEXT);
1058 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
1059 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
1060 SelectObject (hMemDC, hOldBitmap);
1061 DeleteObject(hBrush);
1063 ReleaseDC(hChild, hdc);
1064 hSysMenuBitmap = hBitmap;
1069 hSysMenuBitmap = hBmpClose;
1071 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
1072 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
1074 TRACE("not inserted\n");
1075 DestroyMenu(hSysPopup);
1079 /* The close button is only present in Win 95 look */
1080 if(TWEAK_WineLook > WIN31_LOOK)
1082 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1083 SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
1086 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
1087 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
1088 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
1089 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
1097 /**********************************************************************
1098 * MDI_RestoreFrameMenu
1100 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
1102 MENUITEMINFOW menuInfo;
1103 HMENU menu = GetMenu( frame );
1104 INT nItems = GetMenuItemCount(menu) - 1;
1105 UINT iId = GetMenuItemID(menu,nItems) ;
1107 TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
1109 if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
1113 * Remove the system menu, If that menu is the icon of the window
1114 * as it is in win95, we have to delete the bitmap.
1116 memset(&menuInfo, 0, sizeof(menuInfo));
1117 menuInfo.cbSize = sizeof(menuInfo);
1118 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
1120 GetMenuItemInfoW(menu,
1125 RemoveMenu(menu,0,MF_BYPOSITION);
1127 if ( (menuInfo.fType & MFT_BITMAP) &&
1128 (LOWORD(menuInfo.dwTypeData)!=0) &&
1129 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
1131 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
1134 if(TWEAK_WineLook > WIN31_LOOK)
1137 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1140 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1142 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1150 /**********************************************************************
1151 * MDI_UpdateFrameText
1153 * used when child window is maximized/restored
1155 * Note: lpTitle can be NULL
1157 static void MDI_UpdateFrameText( HWND frame, HWND hClient,
1158 BOOL repaint, LPCWSTR lpTitle )
1160 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
1161 MDICLIENTINFO *ci = get_client_info( hClient );
1163 TRACE("repaint %i, frameText %s\n", repaint, debugstr_w(lpTitle));
1167 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
1169 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
1173 /* store new "default" title if lpTitle is not NULL */
1176 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1177 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
1178 strcpyW( ci->frameTitle, lpTitle );
1183 if (ci->hwndChildMaximized)
1185 /* combine frame title and child title if possible */
1187 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
1188 static const WCHAR lpBracket2[] = {']',0};
1189 int i_frame_text_length = strlenW(ci->frameTitle);
1191 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1193 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1195 strcatW( lpBuffer, lpBracket );
1196 if (GetWindowTextW( ci->hwndChildMaximized, lpBuffer + i_frame_text_length + 4,
1197 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1198 strcatW( lpBuffer, lpBracket2 );
1200 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1205 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1211 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1212 if( repaint == MDI_REPAINTFRAME)
1213 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1214 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1218 /* ----------------------------- Interface ---------------------------- */
1221 /**********************************************************************
1222 * MDIClientWndProc_common
1224 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1225 WPARAM wParam, LPARAM lParam, BOOL unicode )
1229 if (!(ci = get_client_info( hwnd ))) return 0;
1236 /* Since we are using only cs->lpCreateParams, we can safely
1237 * cast to LPCREATESTRUCTA here */
1238 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1239 WND *wndPtr = WIN_GetPtr( hwnd );
1241 /* Translation layer doesn't know what's in the cs->lpCreateParams
1242 * so we have to keep track of what environment we're in. */
1244 if( wndPtr->flags & WIN_ISWIN32 )
1246 #define ccs ((LPCLIENTCREATESTRUCT)cs->lpCreateParams)
1247 ci->hWindowMenu = ccs->hWindowMenu;
1248 ci->idFirstChild = ccs->idFirstChild;
1253 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1254 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1255 ci->idFirstChild = ccs->idFirstChild;
1257 WIN_ReleasePtr( wndPtr );
1259 ci->hwndChildMaximized = 0;
1260 ci->nActiveChildren = 0;
1261 ci->nTotalCreated = 0;
1262 ci->frameTitle = NULL;
1264 SetWindowLongW( hwnd, GWL_STYLE, GetWindowLongW(hwnd,GWL_STYLE) | WS_CLIPCHILDREN );
1266 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1268 if (ci->hWindowMenu != 0)
1269 AppendMenuW( ci->hWindowMenu, MF_SEPARATOR, 0, NULL );
1271 GetClientRect( GetParent(hwnd), &rect);
1272 MoveWindow( hwnd, 0, 0, rect.right, rect.bottom, FALSE );
1274 MDI_UpdateFrameText( GetParent(hwnd), hwnd, MDI_NOFRAMEREPAINT, NULL);
1276 TRACE("Client created - hwnd = %p, idFirst = %u\n", hwnd, ci->idFirstChild );
1283 if( ci->hwndChildMaximized )
1284 MDI_RestoreFrameMenu( GetParent(hwnd), ci->hwndChildMaximized);
1285 if((ci->hWindowMenu != 0) &&
1286 (nItems = GetMenuItemCount(ci->hWindowMenu)) > 0)
1288 ci->idFirstChild = nItems - 1;
1289 ci->nActiveChildren++; /* to delete a separator */
1290 while( ci->nActiveChildren-- )
1291 DeleteMenu(ci->hWindowMenu,MF_BYPOSITION,ci->idFirstChild--);
1293 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1297 case WM_MDIACTIVATE:
1298 if( ci->hwndActiveChild != (HWND)wParam )
1299 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1303 return MDICascade(hwnd, ci);
1307 return (LRESULT)MDICreateChild( hwnd, ci, (MDICREATESTRUCTA *)lParam, unicode );
1311 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1313 case WM_MDIGETACTIVE:
1314 if (lParam) *(BOOL *)lParam = (ci->hwndChildMaximized != 0);
1315 return (LRESULT)ci->hwndActiveChild;
1317 case WM_MDIICONARRANGE:
1318 ci->mdiFlags |= MDIF_NEEDUPDATE;
1319 ArrangeIconicWindows( hwnd );
1320 ci->sbRecalc = SB_BOTH+1;
1321 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1324 case WM_MDIMAXIMIZE:
1325 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1328 case WM_MDINEXT: /* lParam != 0 means previous window */
1329 MDI_SwitchActiveChild( hwnd, WIN_GetFullHandle( (HWND)wParam ), !lParam );
1333 SendMessageW( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1337 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1339 case WM_MDIREFRESHMENU:
1340 return MDIRefreshMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1343 ci->mdiFlags |= MDIF_NEEDUPDATE;
1344 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1345 MDITile( hwnd, ci, wParam );
1346 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1351 ci->mdiFlags |= MDIF_NEEDUPDATE;
1352 ScrollChildren( hwnd, message, wParam, lParam );
1353 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1357 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1358 SetFocus( ci->hwndActiveChild );
1362 if( ci->hwndActiveChild )
1363 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1366 case WM_PARENTNOTIFY:
1367 if (LOWORD(wParam) == WM_LBUTTONDOWN)
1371 pt.x = SLOWORD(lParam);
1372 pt.y = SHIWORD(lParam);
1373 child = ChildWindowFromPoint(hwnd, pt);
1375 TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1377 if( child && child != hwnd && child != ci->hwndActiveChild )
1378 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1383 if( IsWindow(ci->hwndChildMaximized) )
1389 rect.right = LOWORD(lParam);
1390 rect.bottom = HIWORD(lParam);
1392 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndChildMaximized,GWL_STYLE),
1393 0, GetWindowLongA(ci->hwndChildMaximized,GWL_EXSTYLE) );
1394 MoveWindow(ci->hwndChildMaximized, rect.left, rect.top,
1395 rect.right - rect.left, rect.bottom - rect.top, 1);
1398 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1402 case WM_MDICALCCHILDSCROLL:
1403 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1405 CalcChildScroll(hwnd, ci->sbRecalc-1);
1407 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1411 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1412 DefWindowProcA( hwnd, message, wParam, lParam );
1415 /***********************************************************************
1418 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1420 if (!IsWindow(hwnd)) return 0;
1421 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1424 /***********************************************************************
1427 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1429 if (!IsWindow(hwnd)) return 0;
1430 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1433 /***********************************************************************
1434 * DefFrameProc (USER.445)
1436 LRESULT WINAPI DefFrameProc16( HWND16 hwnd, HWND16 hwndMDIClient,
1437 UINT16 message, WPARAM16 wParam, LPARAM lParam )
1442 lParam = (LPARAM)MapSL(lParam);
1448 return DefFrameProcA( WIN_Handle32(hwnd), WIN_Handle32(hwndMDIClient),
1449 message, wParam, lParam );
1453 MDINEXTMENU next_menu;
1454 DefFrameProcW( WIN_Handle32(hwnd), WIN_Handle32(hwndMDIClient),
1455 message, wParam, (LPARAM)&next_menu );
1456 return MAKELONG( HMENU_16(next_menu.hmenuNext), HWND_16(next_menu.hwndNext) );
1459 return DefWindowProc16(hwnd, message, wParam, lParam);
1464 /***********************************************************************
1465 * DefFrameProcA (USER32.@)
1467 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1468 UINT message, WPARAM wParam, LPARAM lParam)
1476 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1477 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1478 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1479 MDI_UpdateFrameText(hwnd, hwndMDIClient, MDI_REPAINTFRAME, text );
1480 HeapFree( GetProcessHeap(), 0, text );
1482 return 1; /* success. FIXME: check text length */
1489 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1492 return DefWindowProcA(hwnd, message, wParam, lParam);
1496 /***********************************************************************
1497 * DefFrameProcW (USER32.@)
1499 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1500 UINT message, WPARAM wParam, LPARAM lParam)
1502 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1510 WORD id = LOWORD(wParam);
1511 /* check for possible syscommands for maximized MDI child */
1512 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1514 if( (id - 0xf000) & 0xf00f ) break;
1515 if( !ci->hwndChildMaximized ) break;
1526 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1533 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1534 /* User chose "More Windows..." */
1535 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1537 /* User chose one of the windows listed in the "Windows" menu */
1538 childHwnd = MDI_GetChildByID(hwndMDIClient,id);
1541 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1547 SendMessageW(hwndMDIClient, message, wParam, lParam);
1551 MDI_UpdateFrameText(hwnd, hwndMDIClient, MDI_REPAINTFRAME, (LPWSTR)lParam );
1552 return 1; /* success. FIXME: check text length */
1555 SetFocus(hwndMDIClient);
1559 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1564 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1566 if (!IsIconic(hwnd) && ci->hwndActiveChild && !ci->hwndChildMaximized)
1568 /* control menu is between the frame system menu and
1569 * the first entry of menu bar */
1570 WND *wndPtr = WIN_GetPtr(hwnd);
1572 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1573 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1575 WIN_ReleasePtr(wndPtr);
1576 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1577 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1578 next_menu->hwndNext = ci->hwndActiveChild;
1580 WIN_ReleasePtr(wndPtr);
1587 return DefWindowProcW( hwnd, message, wParam, lParam );
1591 /***********************************************************************
1592 * DefMDIChildProc (USER.447)
1594 LRESULT WINAPI DefMDIChildProc16( HWND16 hwnd, UINT16 message,
1595 WPARAM16 wParam, LPARAM lParam )
1600 return DefMDIChildProcA( WIN_Handle32(hwnd), message, wParam, (LPARAM)MapSL(lParam) );
1604 case WM_CHILDACTIVATE:
1609 return DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, lParam );
1610 case WM_GETMINMAXINFO:
1612 MINMAXINFO16 *mmi16 = (MINMAXINFO16 *)MapSL(lParam);
1614 STRUCT32_MINMAXINFO16to32( mmi16, &mmi );
1615 DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, (LPARAM)&mmi );
1616 STRUCT32_MINMAXINFO32to16( &mmi, mmi16 );
1621 MDINEXTMENU next_menu;
1622 DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, (LPARAM)&next_menu );
1623 return MAKELONG( HMENU_16(next_menu.hmenuNext), HWND_16(next_menu.hwndNext) );
1626 return DefWindowProc16(hwnd, message, wParam, lParam);
1631 /***********************************************************************
1632 * DefMDIChildProcA (USER32.@)
1634 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1635 WPARAM wParam, LPARAM lParam )
1637 HWND client = GetParent(hwnd);
1638 MDICLIENTINFO *ci = get_client_info( client );
1640 hwnd = WIN_GetFullHandle( hwnd );
1641 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1646 DefWindowProcA(hwnd, message, wParam, lParam);
1647 MDI_MenuModifyItem( client, hwnd );
1648 if( ci->hwndChildMaximized == hwnd )
1649 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1650 return 1; /* success. FIXME: check text length */
1652 case WM_GETMINMAXINFO:
1656 case WM_CHILDACTIVATE:
1662 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1664 return DefWindowProcA(hwnd, message, wParam, lParam);
1668 /***********************************************************************
1669 * DefMDIChildProcW (USER32.@)
1671 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1672 WPARAM wParam, LPARAM lParam )
1674 HWND client = GetParent(hwnd);
1675 MDICLIENTINFO *ci = get_client_info( client );
1677 hwnd = WIN_GetFullHandle( hwnd );
1678 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1683 DefWindowProcW(hwnd, message, wParam, lParam);
1684 MDI_MenuModifyItem( client, hwnd );
1685 if( ci->hwndChildMaximized == hwnd )
1686 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1687 return 1; /* success. FIXME: check text length */
1689 case WM_GETMINMAXINFO:
1690 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1694 return 0x00010000; /* MDI children don't have menu bars */
1697 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1701 if (ci->hwndActiveChild != hwnd) MDI_ChildActivate( client, hwnd );
1704 case WM_CHILDACTIVATE:
1705 MDI_ChildActivate( client, hwnd );
1712 if( ci->hwndChildMaximized == hwnd) return 0;
1716 SetWindowLongW( hwnd, GWL_STYLE,
1717 GetWindowLongW( hwnd, GWL_STYLE ) | WS_SYSMENU );
1720 if (ci->hwndChildMaximized == hwnd)
1721 return SendMessageW( GetParent(client), message, wParam, lParam);
1722 SetWindowLongW( hwnd, GWL_STYLE,
1723 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_SYSMENU );
1726 SendMessageW( client, WM_MDINEXT, 0, 0);
1729 SendMessageW( client, WM_MDINEXT, 0, 1);
1735 if( ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1736 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1740 if( ci->hwndActiveChild == hwnd && wParam != SIZE_MAXIMIZED )
1742 ci->hwndChildMaximized = 0;
1743 MDI_RestoreFrameMenu( GetParent(client), hwnd );
1744 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1747 if( wParam == SIZE_MAXIMIZED )
1749 HWND hMaxChild = ci->hwndChildMaximized;
1751 if( hMaxChild == hwnd ) break;
1754 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1755 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1756 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1757 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1759 TRACE("maximizing child %p\n", hwnd );
1761 /* keep track of the maximized window. */
1762 ci->hwndChildMaximized = hwnd; /* !!! */
1764 /* The maximized window should also be the active window */
1765 MDI_ChildActivate( client, hwnd );
1766 MDI_AugmentFrameMenu( GetParent(client), hwnd );
1767 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1770 if( wParam == SIZE_MINIMIZED )
1772 HWND switchTo = MDI_GetWindow(ci, hwnd, TRUE, WS_MINIMIZE);
1774 if (switchTo) SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0);
1776 MDI_PostUpdate(client, ci, SB_BOTH+1);
1781 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1782 HWND parent = GetParent(client);
1784 if( wParam == VK_LEFT ) /* switch to frame system menu */
1786 WND *wndPtr = WIN_GetPtr( parent );
1787 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1788 WIN_ReleasePtr( wndPtr );
1790 if( wParam == VK_RIGHT ) /* to frame menu bar */
1792 next_menu->hmenuNext = GetMenu(parent);
1794 next_menu->hwndNext = parent;
1801 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1806 return DefWindowProcW(hwnd, message, wParam, lParam);
1809 /**********************************************************************
1810 * CreateMDIWindowA (USER32.@) Creates a MDI child
1813 * Success: Handle to created window
1816 HWND WINAPI CreateMDIWindowA(
1817 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1818 LPCSTR lpWindowName, /* [in] Pointer to window name */
1819 DWORD dwStyle, /* [in] Window style */
1820 INT X, /* [in] Horizontal position of window */
1821 INT Y, /* [in] Vertical position of window */
1822 INT nWidth, /* [in] Width of window */
1823 INT nHeight, /* [in] Height of window */
1824 HWND hWndParent, /* [in] Handle to parent window */
1825 HINSTANCE hInstance, /* [in] Handle to application instance */
1826 LPARAM lParam) /* [in] Application-defined value */
1828 MDICLIENTINFO *pCi = get_client_info( hWndParent );
1829 MDICREATESTRUCTA cs;
1831 TRACE("(%s,%s,%ld,%d,%d,%d,%d,%p,%p,%ld)\n",
1832 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1833 nWidth,nHeight,hWndParent,hInstance,lParam);
1837 ERR("bad hwnd for MDI-client: %p\n", hWndParent);
1840 cs.szClass=lpClassName;
1841 cs.szTitle=lpWindowName;
1842 cs.hOwner=hInstance;
1850 return MDICreateChild(hWndParent, pCi, &cs, FALSE);
1853 /***********************************************************************
1854 * CreateMDIWindowW (USER32.@) Creates a MDI child
1857 * Success: Handle to created window
1860 HWND WINAPI CreateMDIWindowW(
1861 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1862 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1863 DWORD dwStyle, /* [in] Window style */
1864 INT X, /* [in] Horizontal position of window */
1865 INT Y, /* [in] Vertical position of window */
1866 INT nWidth, /* [in] Width of window */
1867 INT nHeight, /* [in] Height of window */
1868 HWND hWndParent, /* [in] Handle to parent window */
1869 HINSTANCE hInstance, /* [in] Handle to application instance */
1870 LPARAM lParam) /* [in] Application-defined value */
1872 MDICLIENTINFO *pCi = get_client_info( hWndParent );
1873 MDICREATESTRUCTW cs;
1875 TRACE("(%s,%s,%ld,%d,%d,%d,%d,%p,%p,%ld)\n",
1876 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1877 nWidth, nHeight, hWndParent, hInstance, lParam);
1881 ERR("bad hwnd for MDI-client: %p\n", hWndParent);
1884 cs.szClass = lpClassName;
1885 cs.szTitle = lpWindowName;
1886 cs.hOwner = hInstance;
1894 return MDICreateChild(hWndParent, pCi, (MDICREATESTRUCTA *)&cs, TRUE);
1897 /**********************************************************************
1898 * TranslateMDISysAccel (USER32.@)
1900 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1902 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1904 MDICLIENTINFO *ci = get_client_info( hwndClient );
1907 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1909 /* translate if the Ctrl key is down and Alt not. */
1911 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1913 switch( msg->wParam )
1917 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1926 TRACE("wParam = %04x\n", wParam);
1927 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1931 return 0; /* failure */
1934 /***********************************************************************
1935 * CalcChildScroll (USER32.@)
1937 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1940 RECT childRect, clientRect;
1943 GetClientRect( hwnd, &clientRect );
1944 SetRectEmpty( &childRect );
1946 if ((list = WIN_ListChildren( hwnd )))
1949 for (i = 0; list[i]; i++)
1951 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1952 if (style & WS_MAXIMIZE)
1954 HeapFree( GetProcessHeap(), 0, list );
1955 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1958 if (style & WS_VISIBLE)
1960 WND *pWnd = WIN_FindWndPtr( list[i] );
1961 UnionRect( &childRect, &pWnd->rectWindow, &childRect );
1962 WIN_ReleaseWndPtr( pWnd );
1965 HeapFree( GetProcessHeap(), 0, list );
1967 UnionRect( &childRect, &clientRect, &childRect );
1969 /* set common info values */
1970 info.cbSize = sizeof(info);
1971 info.fMask = SIF_POS | SIF_RANGE;
1973 /* set the specific */
1978 info.nMin = childRect.left;
1979 info.nMax = childRect.right - clientRect.right;
1980 info.nPos = clientRect.left - childRect.left;
1981 SetScrollInfo(hwnd, scroll, &info, TRUE);
1982 if (scroll == SB_HORZ) break;
1985 info.nMin = childRect.top;
1986 info.nMax = childRect.bottom - clientRect.bottom;
1987 info.nPos = clientRect.top - childRect.top;
1988 SetScrollInfo(hwnd, scroll, &info, TRUE);
1994 /***********************************************************************
1995 * ScrollChildren (USER32.@)
1997 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
2001 INT curPos, length, minPos, maxPos, shift;
2004 GetClientRect( hWnd, &rect );
2009 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
2010 curPos = GetScrollPos(hWnd,SB_HORZ);
2011 length = (rect.right - rect.left) / 2;
2012 shift = GetSystemMetrics(SM_CYHSCROLL);
2015 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
2016 curPos = GetScrollPos(hWnd,SB_VERT);
2017 length = (rect.bottom - rect.top) / 2;
2018 shift = GetSystemMetrics(SM_CXVSCROLL);
2027 newPos = curPos - shift;
2030 newPos = curPos + shift;
2033 newPos = curPos - length;
2036 newPos = curPos + length;
2039 case SB_THUMBPOSITION:
2040 newPos = LOWORD(lParam);
2053 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
2057 if( newPos > maxPos )
2060 if( newPos < minPos )
2063 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
2065 if( uMsg == WM_VSCROLL )
2066 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
2067 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
2069 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
2070 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
2074 /******************************************************************************
2075 * CascadeWindows (USER32.@) Cascades MDI child windows
2078 * Success: Number of cascaded windows.
2082 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
2083 UINT cKids, const HWND *lpKids)
2085 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
2090 /******************************************************************************
2091 * TileWindows (USER32.@) Tiles MDI child windows
2094 * Success: Number of tiled windows.
2098 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
2099 UINT cKids, const HWND *lpKids)
2101 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
2105 /************************************************************************
2106 * "More Windows..." functionality
2109 /* MDI_MoreWindowsDlgProc
2111 * This function will process the messages sent to the "More Windows..."
2113 * Return values: 0 = cancel pressed
2114 * HWND = ok pressed or double-click in the list...
2118 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
2127 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
2128 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2129 HWND *list, *sorted_list;
2131 if (!(list = WIN_ListChildren( (HWND)lParam ))) return TRUE;
2132 if (!(sorted_list = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
2133 sizeof(HWND) * ci->nActiveChildren )))
2135 HeapFree( GetProcessHeap(), 0, list );
2139 /* Fill the list, sorted by id... */
2140 for (i = 0; list[i]; i++)
2142 UINT id = GetWindowLongW( list[i], GWL_ID ) - ci->idFirstChild;
2143 if (id < ci->nActiveChildren) sorted_list[id] = list[i];
2145 HeapFree( GetProcessHeap(), 0, list );
2147 for (i = 0; i < ci->nActiveChildren; i++)
2151 if (!GetWindowTextW( sorted_list[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
2153 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
2154 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)sorted_list[i] );
2155 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
2156 if (length > widest)
2159 /* Make sure the horizontal scrollbar scrolls ok */
2160 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
2162 /* Set the current selection */
2163 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
2168 switch (LOWORD(wParam))
2171 if (HIWORD(wParam) != LBN_DBLCLK) break;
2175 /* windows are sorted by menu ID, so we must return the
2176 * window associated to the given id
2178 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2179 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
2180 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
2181 EndDialog(hDlg, res);
2195 * MDI_MoreWindowsDialog
2197 * Prompts the user with a listbox containing the opened
2198 * documents. The user can then choose a windows and click
2199 * on OK to set the current window to the one selected, or
2200 * CANCEL to cancel. The function returns a handle to the
2204 static HWND MDI_MoreWindowsDialog(HWND hwnd)
2210 hRes = FindResourceA(GetModuleHandleA("USER32"), "MDI_MOREWINDOWS", RT_DIALOGA);
2215 hDlgTmpl = LoadResource(GetModuleHandleA("USER32"), hRes );
2220 template = LockResource( hDlgTmpl );
2225 return (HWND) DialogBoxIndirectParamA(GetModuleHandleA("USER32"),
2226 (LPDLGTEMPLATEA) template,
2227 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);
2234 * Will swap the menu IDs for the given 2 positions.
2235 * pos1 and pos2 are menu IDs
2240 static void MDI_SwapMenuItems(HWND parent, UINT pos1, UINT pos2)
2245 if (!(list = WIN_ListChildren( parent ))) return;
2246 for (i = 0; list[i]; i++)
2248 UINT id = GetWindowLongW( list[i], GWL_ID );
2249 if (id == pos1) SetWindowLongW( list[i], GWL_ID, pos2 );
2250 else if (id == pos2) SetWindowLongW( list[i], GWL_ID, pos1 );
2252 HeapFree( GetProcessHeap(), 0, list );