d3dcompiler: Allow D3DCompile2() to succeed with null output shader blob pointer.
[wine.git] / dlls / user32 / mdi.c
blobf579298544ac6a85db3d541c3964ce90a3ea28ac
1 /* MDI.C
3 * Copyright 1994, Bob Amstadt
4 * 1995,1996 Alex Korobka
6 * This file contains routines to support MDI (Multiple Document
7 * Interface) features .
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Notes: Fairly complete implementation.
24 * Also, Excel and WinWord do _not_ use MDI so if you're trying
25 * to fix them look elsewhere.
27 * Notes on how the "More Windows..." is implemented:
29 * When we have more than 9 opened windows, a "More Windows..."
30 * option appears in the "Windows" menu. Each child window has
31 * a WND* associated with it, accessible via the children list of
32 * the parent window. This WND* has a wIDmenu member, which reflects
33 * the position of the child in the window list. For example, with
34 * 9 child windows, we could have the following pattern:
38 * Name of the child window pWndChild->wIDmenu
39 * Doc1 5000
40 * Doc2 5001
41 * Doc3 5002
42 * Doc4 5003
43 * Doc5 5004
44 * Doc6 5005
45 * Doc7 5006
46 * Doc8 5007
47 * Doc9 5008
50 * The "Windows" menu, as the "More windows..." dialog, are constructed
51 * in this order. If we add a child, we would have the following list:
54 * Name of the child window pWndChild->wIDmenu
55 * Doc1 5000
56 * Doc2 5001
57 * Doc3 5002
58 * Doc4 5003
59 * Doc5 5004
60 * Doc6 5005
61 * Doc7 5006
62 * Doc8 5007
63 * Doc9 5008
64 * Doc10 5009
66 * But only 5000 to 5008 would be displayed in the "Windows" menu. We want
67 * the last created child to be in the menu, so we swap the last child with
68 * the 9th... Doc9 will be accessible via the "More Windows..." option.
70 * Doc1 5000
71 * Doc2 5001
72 * Doc3 5002
73 * Doc4 5003
74 * Doc5 5004
75 * Doc6 5005
76 * Doc7 5006
77 * Doc8 5007
78 * Doc9 5009
79 * Doc10 5008
83 #define OEMRESOURCE
85 #include <math.h>
86 #include "user_private.h"
87 #include "wownt32.h"
88 #include "controls.h"
89 #include "wine/debug.h"
91 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
93 #define MDI_MAXTITLELENGTH 0xa1
95 #define WM_MDICALCCHILDSCROLL 0x003f /* this is exactly what Windows uses */
97 /* "More Windows..." definitions */
98 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
99 option will appear under the Windows menu */
100 #define MDI_IDC_LISTBOX 100
101 #define IDS_MDI_MOREWINDOWS 13
103 #define MDIF_NEEDUPDATE 0x0001
105 typedef struct
107 /* At some points, particularly when switching MDI children, active and
108 * maximized MDI children may be not the same window, so we need to track
109 * them separately.
110 * The only place where we switch to/from maximized state is DefMDIChildProc
111 * WM_SIZE/SIZE_MAXIMIZED handler. We get that notification only after the
112 * ShowWindow(SW_SHOWMAXIMIZED) request, therefore window is guaranteed to
113 * be visible at the time we get the notification, and it's safe to assume
114 * that hwndChildMaximized is always visible.
115 * If the app plays games with WS_VISIBLE, WS_MAXIMIZE or any other window
116 * states it must keep coherency with USER32 on its own. This is true for
117 * Windows as well.
119 UINT nActiveChildren;
120 HWND hwndChildMaximized;
121 HWND hwndActiveChild;
122 HWND *child; /* array of tracked children */
123 HMENU hFrameMenu;
124 HMENU hWindowMenu;
125 UINT idFirstChild;
126 LPWSTR frameTitle;
127 UINT nTotalCreated;
128 UINT mdiFlags;
129 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
130 } MDICLIENTINFO;
132 static HBITMAP hBmpClose = 0;
134 /* ----------------- declarations ----------------- */
135 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
136 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
137 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
138 static LONG MDI_ChildActivate( HWND, HWND );
139 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
141 static HWND MDI_MoreWindowsDialog(HWND);
143 /* -------- Miscellaneous service functions ----------
145 * MDI_GetChildByID
147 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
149 int i;
151 for (i = 0; ci->nActiveChildren; i++)
153 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
154 return ci->child[i];
156 return 0;
159 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
161 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
163 ci->mdiFlags |= MDIF_NEEDUPDATE;
164 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
166 ci->sbRecalc = recalc;
170 static MDICLIENTINFO *get_client_info( HWND client )
172 return NtUserGetMDIClientInfo( client );
175 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
177 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
179 if (!hSysMenu) hSysMenu = NtUserGetSystemMenu( hwnd, FALSE );
180 if (hSysMenu)
182 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
183 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
184 return FALSE;
186 return TRUE;
189 /**********************************************************************
190 * MDI_GetWindow
192 * returns "activatable" child different from the current or zero
194 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
195 DWORD dwStyleMask )
197 int i;
198 HWND *list;
199 HWND last = 0;
201 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
202 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
204 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
205 i = 0;
206 /* start from next after hWnd */
207 while (list[i] && list[i] != hWnd) i++;
208 if (list[i]) i++;
210 for ( ; list[i]; i++)
212 if (GetWindow( list[i], GW_OWNER )) continue;
213 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
214 last = list[i];
215 if (bNext) goto found;
217 /* now restart from the beginning */
218 for (i = 0; list[i] && list[i] != hWnd; i++)
220 if (GetWindow( list[i], GW_OWNER )) continue;
221 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
222 last = list[i];
223 if (bNext) goto found;
225 found:
226 HeapFree( GetProcessHeap(), 0, list );
227 return last;
230 /**********************************************************************
231 * MDI_CalcDefaultChildPos
233 * It seems that the default height is about 2/3 of the client rect
235 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
237 INT nstagger;
238 RECT rect;
239 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
241 if (total < 0) /* we are called from CreateWindow */
243 MDICLIENTINFO *ci = get_client_info(hwndClient);
244 total = ci->nTotalCreated;
245 *id = ci->idFirstChild + ci->nActiveChildren;
246 TRACE("MDI child id %04x\n", *id);
249 GetClientRect( hwndClient, &rect );
250 if( rect.bottom - rect.top - delta >= spacing )
251 rect.bottom -= delta;
253 nstagger = (rect.bottom - rect.top)/(3 * spacing);
254 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
255 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
256 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
259 /**********************************************************************
260 * MDISetMenu
262 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
263 HMENU hmenuWindow)
265 MDICLIENTINFO *ci;
266 HWND hwndFrame = GetParent(hwnd);
268 TRACE("%p, frame menu %p, window menu %p\n", hwnd, hmenuFrame, hmenuWindow);
270 if (hmenuFrame && !IsMenu(hmenuFrame))
272 WARN("hmenuFrame is not a menu handle\n");
273 return 0L;
276 if (hmenuWindow && !IsMenu(hmenuWindow))
278 WARN("hmenuWindow is not a menu handle\n");
279 return 0L;
282 if (!(ci = get_client_info( hwnd ))) return 0;
284 TRACE("old frame menu %p, old window menu %p\n", ci->hFrameMenu, ci->hWindowMenu);
286 if (hmenuFrame)
288 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
290 if (ci->hwndChildMaximized)
291 MDI_RestoreFrameMenu( hwndFrame, ci->hwndChildMaximized );
294 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
296 /* delete menu items from ci->hWindowMenu
297 * and add them to hmenuWindow */
298 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
299 if( ci->hWindowMenu && ci->nActiveChildren )
301 UINT nActiveChildren_old = ci->nActiveChildren;
303 /* Remove all items from old Window menu */
304 ci->nActiveChildren = 0;
305 MDI_RefreshMenu(ci);
307 ci->hWindowMenu = hmenuWindow;
309 /* Add items to the new Window menu */
310 ci->nActiveChildren = nActiveChildren_old;
311 MDI_RefreshMenu(ci);
313 else
314 ci->hWindowMenu = hmenuWindow;
317 if (hmenuFrame)
319 NtUserSetMenu(hwndFrame, hmenuFrame);
320 if( hmenuFrame != ci->hFrameMenu )
322 HMENU oldFrameMenu = ci->hFrameMenu;
324 ci->hFrameMenu = hmenuFrame;
325 if (ci->hwndChildMaximized)
326 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
328 return (LRESULT)oldFrameMenu;
332 return 0;
335 /**********************************************************************
336 * MDIRefreshMenu
338 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
340 UINT i, count, visible, id;
341 WCHAR buf[MDI_MAXTITLELENGTH];
343 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
345 if (!ci->hWindowMenu)
346 return 0;
348 if (!IsMenu(ci->hWindowMenu))
350 WARN("Window menu handle %p is no longer valid\n", ci->hWindowMenu);
351 return 0;
354 /* Windows finds the last separator in the menu, and if after it
355 * there is a menu item with MDI magic ID removes all existing
356 * menu items after it, and then adds visible MDI children.
358 count = GetMenuItemCount(ci->hWindowMenu);
359 for (i = 0; i < count; i++)
361 MENUITEMINFOW mii;
363 memset(&mii, 0, sizeof(mii));
364 mii.cbSize = sizeof(mii);
365 mii.fMask = MIIM_TYPE;
366 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
368 if (mii.fType & MF_SEPARATOR)
370 /* Windows checks only ID of the menu item */
371 memset(&mii, 0, sizeof(mii));
372 mii.cbSize = sizeof(mii);
373 mii.fMask = MIIM_ID;
374 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
376 if (mii.wID == ci->idFirstChild)
378 TRACE("removing %u items including separator\n", count - i);
379 while (NtUserRemoveMenu( ci->hWindowMenu, i, MF_BYPOSITION ))
380 /* nothing */;
382 break;
389 visible = 0;
390 for (i = 0; i < ci->nActiveChildren; i++)
392 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
394 id = ci->idFirstChild + visible;
396 if (visible == MDI_MOREWINDOWSLIMIT)
398 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, ARRAY_SIZE(buf));
399 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
400 break;
403 if (!visible)
404 /* Visio expects that separator has id 0 */
405 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
407 visible++;
409 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
411 buf[0] = '&';
412 buf[1] = '0' + visible;
413 buf[2] = ' ';
414 NtUserInternalGetWindowText(ci->child[i], buf + 3, ARRAY_SIZE(buf) - 3);
415 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
416 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
418 if (ci->child[i] == ci->hwndActiveChild)
419 NtUserCheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
421 else
422 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
425 return (LRESULT)ci->hFrameMenu;
429 /* ------------------ MDI child window functions ---------------------- */
431 /**********************************************************************
432 * MDI_ChildGetMinMaxInfo
434 * Note: The rule here is that client rect of the maximized MDI child
435 * is equal to the client rect of the MDI client window.
437 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
439 RECT rect;
441 GetClientRect( client, &rect );
442 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
443 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
445 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
446 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
448 lpMinMax->ptMaxPosition.x = rect.left;
449 lpMinMax->ptMaxPosition.y = rect.top;
451 TRACE("max rect %s\n", wine_dbgstr_rect(&rect));
454 /**********************************************************************
455 * MDI_SwitchActiveChild
457 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
458 * being activated
460 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
462 HWND hwndPrev;
464 hwndPrev = ci->hwndActiveChild;
466 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
468 if ( hwndTo != hwndPrev )
470 BOOL was_zoomed = IsZoomed(hwndPrev);
472 if (was_zoomed)
474 /* restore old MDI child */
475 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
476 NtUserShowWindow( hwndPrev, SW_RESTORE );
477 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
479 /* activate new MDI child */
480 NtUserSetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
481 /* maximize new MDI child */
482 NtUserShowWindow( hwndTo, SW_MAXIMIZE );
484 /* activate new MDI child */
485 NtUserSetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0,
486 SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
491 /**********************************************************************
492 * MDIDestroyChild
494 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
495 HWND child, BOOL flagDestroy )
497 UINT i;
499 TRACE("# of managed children %u\n", ci->nActiveChildren);
501 if( child == ci->hwndActiveChild )
503 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
504 /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
505 if (flagDestroy && next)
506 MDI_SwitchActiveChild(ci, next, TRUE);
507 else
509 NtUserShowWindow( child, SW_HIDE );
510 if (child == ci->hwndChildMaximized)
512 HWND frame = GetParent(client);
513 MDI_RestoreFrameMenu(frame, child);
514 ci->hwndChildMaximized = 0;
515 MDI_UpdateFrameText(frame, client, TRUE, NULL);
517 if (flagDestroy)
518 MDI_ChildActivate(client, 0);
522 for (i = 0; i < ci->nActiveChildren; i++)
524 if (ci->child[i] == child)
526 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
527 memcpy(new_child, ci->child, i * sizeof(HWND));
528 if (i + 1 < ci->nActiveChildren)
529 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
530 HeapFree(GetProcessHeap(), 0, ci->child);
531 ci->child = new_child;
533 ci->nActiveChildren--;
534 break;
538 if (flagDestroy)
540 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
541 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
542 NtUserDestroyWindow(child);
545 TRACE("child destroyed - %p\n", child);
546 return 0;
550 /**********************************************************************
551 * MDI_ChildActivate
553 * Called in response to WM_CHILDACTIVATE, or when last MDI child
554 * is being deactivated.
556 static LONG MDI_ChildActivate( HWND client, HWND child )
558 MDICLIENTINFO *clientInfo;
559 HWND prevActiveWnd, frame;
560 BOOL isActiveFrameWnd;
562 clientInfo = get_client_info( client );
564 if (clientInfo->hwndActiveChild == child) return 0;
566 TRACE("%p\n", child);
568 frame = GetParent(client);
569 isActiveFrameWnd = (GetActiveWindow() == frame);
570 prevActiveWnd = clientInfo->hwndActiveChild;
572 /* deactivate prev. active child */
573 if(prevActiveWnd)
575 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
576 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
579 MDI_SwitchActiveChild( clientInfo, child, FALSE );
580 clientInfo->hwndActiveChild = child;
582 MDI_RefreshMenu(clientInfo);
584 if( isActiveFrameWnd )
586 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
587 /* Let the client window manage focus for children, but if the focus
588 * is already on the client (for instance this is the 1st child) then
589 * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
590 * manually in this case.
592 if (NtUserSetFocus(client) == client)
593 SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
596 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
597 return TRUE;
600 /* -------------------- MDI client window functions ------------------- */
602 /**********************************************************************
603 * CreateMDIMenuBitmap
605 static HBITMAP CreateMDIMenuBitmap(void)
607 HDC hDCSrc = CreateCompatibleDC(0);
608 HDC hDCDest = CreateCompatibleDC(hDCSrc);
609 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
610 HBITMAP hbCopy;
611 HBITMAP hobjSrc, hobjDest;
613 hobjSrc = SelectObject(hDCSrc, hbClose);
614 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
615 hobjDest = SelectObject(hDCDest, hbCopy);
617 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
618 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
620 SelectObject(hDCSrc, hobjSrc);
621 DeleteObject(hbClose);
622 DeleteDC(hDCSrc);
624 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
626 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
627 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
629 SelectObject(hDCDest, hobjSrc );
630 SelectObject(hDCDest, hobjDest);
631 DeleteDC(hDCDest);
633 return hbCopy;
636 /**********************************************************************
637 * MDICascade
639 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
641 HWND *win_array;
642 BOOL has_icons = FALSE;
643 int i, total;
645 if (ci->hwndChildMaximized)
646 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
648 if (ci->nActiveChildren == 0) return 0;
650 if (!(win_array = WIN_ListChildren( client ))) return 0;
652 /* remove all the windows we don't want */
653 for (i = total = 0; win_array[i]; i++)
655 if (!IsWindowVisible( win_array[i] )) continue;
656 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
657 if (IsIconic( win_array[i] ))
659 has_icons = TRUE;
660 continue;
662 win_array[total++] = win_array[i];
664 win_array[total] = 0;
666 if (total)
668 INT delta = 0, n = 0, i;
669 POINT pos[2];
670 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
672 /* walk the list (backwards) and move windows */
673 for (i = total - 1; i >= 0; i--)
675 LONG style;
676 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
678 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
679 TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
680 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
681 style = GetWindowLongW(win_array[i], GWL_STYLE);
683 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
684 NtUserSetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y, posOptions );
687 HeapFree( GetProcessHeap(), 0, win_array );
689 if (has_icons) ArrangeIconicWindows( client );
690 return 0;
693 /**********************************************************************
694 * MDITile
696 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
698 HWND *win_array;
699 int i, total;
700 BOOL has_icons = FALSE;
702 if (ci->hwndChildMaximized)
703 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
705 if (ci->nActiveChildren == 0) return;
707 if (!(win_array = WIN_ListChildren( client ))) return;
709 /* remove all the windows we don't want */
710 for (i = total = 0; win_array[i]; i++)
712 if (!IsWindowVisible( win_array[i] )) continue;
713 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
714 if (IsIconic( win_array[i] ))
716 has_icons = TRUE;
717 continue;
719 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
720 win_array[total++] = win_array[i];
722 win_array[total] = 0;
724 TRACE("%u windows to tile\n", total);
726 if (total)
728 HWND *pWnd = win_array;
729 RECT rect;
730 int x, y, xsize, ysize;
731 int rows, columns, r, c, i;
733 GetClientRect(client,&rect);
734 rows = (int) sqrt((double)total);
735 columns = total / rows;
737 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
739 i = rows;
740 rows = columns; /* exchange r and c */
741 columns = i;
744 if (has_icons)
746 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
747 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
750 ysize = rect.bottom / rows;
751 xsize = rect.right / columns;
753 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
755 if (c == columns)
757 rows = total - i;
758 ysize = rect.bottom / rows;
761 y = 0;
762 for (r = 1; r <= rows && *pWnd; r++, i++)
764 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
765 LONG style = GetWindowLongW(win_array[i], GWL_STYLE);
766 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
768 NtUserSetWindowPos( *pWnd, 0, x, y, xsize, ysize, posOptions );
769 y += ysize;
770 pWnd++;
772 x += xsize;
775 HeapFree( GetProcessHeap(), 0, win_array );
776 if (has_icons) ArrangeIconicWindows( client );
779 /* ----------------------- Frame window ---------------------------- */
782 /**********************************************************************
783 * MDI_AugmentFrameMenu
785 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
787 HMENU menu = GetMenu( frame );
788 HMENU hSysPopup;
789 HBITMAP hSysMenuBitmap = 0;
790 HICON hIcon;
792 TRACE("frame %p,child %p\n",frame,hChild);
794 if (!menu) return FALSE;
796 /* create a copy of sysmenu popup and insert it into frame menu bar */
797 if (!(hSysPopup = NtUserGetSystemMenu( hChild, FALSE )))
799 TRACE("child %p doesn't have a system menu\n", hChild);
800 return FALSE;
803 AppendMenuW(menu, MF_HELP | MF_BITMAP,
804 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
805 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
806 AppendMenuW(menu, MF_HELP | MF_BITMAP,
807 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
808 AppendMenuW(menu, MF_HELP | MF_BITMAP,
809 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
811 /* The system menu is replaced by the child icon */
812 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_SMALL, 0);
813 if (!hIcon)
814 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICONSM);
815 if (!hIcon)
816 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_BIG, 0);
817 if (!hIcon)
818 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICON);
819 if (!hIcon)
820 hIcon = LoadImageW(0, (LPWSTR)IDI_WINLOGO, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
821 GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR | LR_SHARED);
822 if (hIcon)
824 HDC hMemDC;
825 HBITMAP hBitmap, hOldBitmap;
826 HDC hdc = NtUserGetDC(hChild);
828 if (hdc)
830 int cx, cy;
831 cx = GetSystemMetrics(SM_CXSMICON);
832 cy = GetSystemMetrics(SM_CYSMICON);
833 hMemDC = CreateCompatibleDC(hdc);
834 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
835 hOldBitmap = SelectObject(hMemDC, hBitmap);
836 SetMapMode(hMemDC, MM_TEXT);
837 NtUserDrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, GetSysColorBrush(COLOR_MENU), DI_NORMAL);
838 SelectObject (hMemDC, hOldBitmap);
839 DeleteDC(hMemDC);
840 NtUserReleaseDC( hChild, hdc );
841 hSysMenuBitmap = hBitmap;
845 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
846 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
848 TRACE("not inserted\n");
849 NtUserDestroyMenu( hSysPopup );
850 return FALSE;
853 NtUserEnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
854 NtUserEnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
855 NtUserEnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
856 NtUserSetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
858 /* redraw menu */
859 DrawMenuBar(frame);
861 return TRUE;
864 /**********************************************************************
865 * MDI_RestoreFrameMenu
867 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
869 MENUITEMINFOW menuInfo;
870 HMENU menu = GetMenu( frame );
871 INT nItems;
872 UINT iId;
874 TRACE("frame %p, child %p\n", frame, hChild);
876 if (!menu) return FALSE;
878 /* if there is no system buttons then nothing to do */
879 nItems = GetMenuItemCount(menu) - 1;
880 iId = GetMenuItemID(menu, nItems);
881 if ( !(iId == SC_RESTORE || iId == SC_CLOSE) )
882 return FALSE;
885 * Remove the system menu, If that menu is the icon of the window
886 * as it is in win95, we have to delete the bitmap.
888 memset(&menuInfo, 0, sizeof(menuInfo));
889 menuInfo.cbSize = sizeof(menuInfo);
890 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
892 GetMenuItemInfoW(menu,
894 TRUE,
895 &menuInfo);
897 NtUserRemoveMenu( menu, 0, MF_BYPOSITION );
899 if ( (menuInfo.fType & MFT_BITMAP) &&
900 (LOWORD(menuInfo.dwTypeData)!=0) &&
901 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
903 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
906 /* close */
907 NtUserDeleteMenu( menu, SC_CLOSE, MF_BYCOMMAND );
908 /* restore */
909 NtUserDeleteMenu( menu, SC_RESTORE, MF_BYCOMMAND );
910 /* minimize */
911 NtUserDeleteMenu( menu, SC_MINIMIZE, MF_BYCOMMAND );
913 DrawMenuBar(frame);
915 return TRUE;
919 /**********************************************************************
920 * MDI_UpdateFrameText
922 * used when child window is maximized/restored
924 * Note: lpTitle can be NULL
926 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
928 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
929 MDICLIENTINFO *ci = get_client_info( hClient );
931 TRACE("frameText %s\n", debugstr_w(lpTitle));
933 if (!ci) return;
935 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
937 GetWindowTextW( frame, lpBuffer, ARRAY_SIZE( lpBuffer ));
938 lpTitle = lpBuffer;
941 /* store new "default" title if lpTitle is not NULL */
942 if (lpTitle)
944 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
945 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(lpTitle)+1)*sizeof(WCHAR))))
946 lstrcpyW( ci->frameTitle, lpTitle );
949 if (ci->frameTitle)
951 if (ci->hwndChildMaximized)
953 /* combine frame title and child title if possible */
954 int i_frame_text_length = lstrlenW(ci->frameTitle);
956 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
958 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
960 lstrcatW( lpBuffer, L" - [" );
961 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
962 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
963 lstrcatW( lpBuffer, L"]" );
964 else
965 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
968 else
970 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
973 else
974 lpBuffer[0] = '\0';
976 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
978 if (repaint)
979 NtUserSetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
980 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
984 /* ----------------------------- Interface ---------------------------- */
987 /**********************************************************************
988 * MDIClientWndProc_common
990 LRESULT MDIClientWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
992 MDICLIENTINFO *ci;
994 TRACE("%p %04x (%s) %08Ix %08Ix\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
996 if (!(ci = get_client_info( hwnd )))
998 if (message == WM_NCCREATE)
1000 if (!(ci = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ci) ))) return 0;
1001 NtUserSetMDIClientInfo( hwnd, ci );
1003 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1004 DefWindowProcA( hwnd, message, wParam, lParam );
1007 switch (message)
1009 case WM_CREATE:
1011 /* Since we are using only cs->lpCreateParams, we can safely
1012 * cast to LPCREATESTRUCTA here */
1013 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1014 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1016 ci->hWindowMenu = ccs->hWindowMenu;
1017 ci->idFirstChild = ccs->idFirstChild;
1018 ci->hwndChildMaximized = 0;
1019 ci->child = NULL;
1020 ci->nActiveChildren = 0;
1021 ci->nTotalCreated = 0;
1022 ci->frameTitle = NULL;
1023 ci->mdiFlags = 0;
1024 ci->hFrameMenu = GetMenu(cs->hwndParent);
1026 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1028 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1029 hwnd, ci->hWindowMenu, ci->idFirstChild );
1030 return 0;
1033 case WM_DESTROY:
1035 if( ci->hwndChildMaximized )
1036 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1038 ci->nActiveChildren = 0;
1039 MDI_RefreshMenu(ci);
1041 HeapFree( GetProcessHeap(), 0, ci->child );
1042 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1043 HeapFree( GetProcessHeap(), 0, ci );
1044 return 0;
1047 case WM_MDIACTIVATE:
1049 if( ci->hwndActiveChild != (HWND)wParam )
1050 NtUserSetWindowPos( (HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1051 return 0;
1054 case WM_MDICASCADE:
1055 return MDICascade(hwnd, ci);
1057 case WM_MDICREATE:
1058 if (lParam)
1060 HWND child;
1062 if (unicode)
1064 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1065 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1066 csW->szTitle, csW->style,
1067 csW->x, csW->y, csW->cx, csW->cy,
1068 hwnd, 0, csW->hOwner,
1069 (LPVOID)csW->lParam);
1071 else
1073 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1074 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1075 csA->szTitle, csA->style,
1076 csA->x, csA->y, csA->cx, csA->cy,
1077 hwnd, 0, csA->hOwner,
1078 (LPVOID)csA->lParam);
1080 return (LRESULT)child;
1082 return 0;
1084 case WM_MDIDESTROY:
1085 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1087 case WM_MDIGETACTIVE:
1088 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1089 return (LRESULT)ci->hwndActiveChild;
1091 case WM_MDIICONARRANGE:
1092 ci->mdiFlags |= MDIF_NEEDUPDATE;
1093 ArrangeIconicWindows( hwnd );
1094 ci->sbRecalc = SB_BOTH+1;
1095 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1096 return 0;
1098 case WM_MDIMAXIMIZE:
1099 NtUserShowWindow( (HWND)wParam, SW_MAXIMIZE );
1100 return 0;
1102 case WM_MDINEXT: /* lParam != 0 means previous window */
1104 HWND hwnd = wParam ? WIN_GetFullHandle((HWND)wParam) : ci->hwndActiveChild;
1105 HWND next = MDI_GetWindow( ci, hwnd, !lParam, 0 );
1106 MDI_SwitchActiveChild( ci, next, TRUE );
1107 if (!lParam)
1108 NtUserSetWindowPos( hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1109 break;
1112 case WM_MDIRESTORE:
1113 NtUserShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1114 return 0;
1116 case WM_MDISETMENU:
1117 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1119 case WM_MDIREFRESHMENU:
1120 return MDI_RefreshMenu( ci );
1122 case WM_MDITILE:
1123 ci->mdiFlags |= MDIF_NEEDUPDATE;
1124 NtUserShowScrollBar( hwnd, SB_BOTH, FALSE );
1125 MDITile( hwnd, ci, wParam );
1126 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1127 return 0;
1129 case WM_VSCROLL:
1130 case WM_HSCROLL:
1131 ci->mdiFlags |= MDIF_NEEDUPDATE;
1132 ScrollChildren( hwnd, message, wParam, lParam );
1133 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1134 return 0;
1136 case WM_SETFOCUS:
1137 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1138 NtUserSetFocus( ci->hwndActiveChild );
1139 return 0;
1141 case WM_NCACTIVATE:
1142 if( ci->hwndActiveChild )
1143 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1144 break;
1146 case WM_PARENTNOTIFY:
1147 switch (LOWORD(wParam))
1149 case WM_CREATE:
1150 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1152 ci->nTotalCreated++;
1153 ci->nActiveChildren++;
1155 if (!ci->child)
1156 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1157 else
1158 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1160 TRACE("Adding MDI child %p, # of children %d\n",
1161 (HWND)lParam, ci->nActiveChildren);
1163 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1165 break;
1167 case WM_LBUTTONDOWN:
1169 HWND child;
1170 POINT pt;
1171 pt.x = (short)LOWORD(lParam);
1172 pt.y = (short)HIWORD(lParam);
1173 child = ChildWindowFromPoint(hwnd, pt);
1175 TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1177 if (child && child != hwnd && child != ci->hwndActiveChild)
1178 NtUserSetWindowPos( child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1179 break;
1182 case WM_DESTROY:
1183 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1185 return 0;
1187 case WM_SIZE:
1188 if( ci->hwndActiveChild && IsZoomed(ci->hwndActiveChild) )
1190 RECT rect;
1192 SetRect(&rect, 0, 0, LOWORD(lParam), HIWORD(lParam));
1193 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1194 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1195 NtUserMoveWindow( ci->hwndActiveChild, rect.left, rect.top,
1196 rect.right - rect.left, rect.bottom - rect.top, 1 );
1198 else
1199 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1201 break;
1203 case WM_MDICALCCHILDSCROLL:
1204 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1206 CalcChildScroll(hwnd, ci->sbRecalc-1);
1207 ci->sbRecalc = 0;
1208 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1210 return 0;
1212 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1213 DefWindowProcA( hwnd, message, wParam, lParam );
1216 /***********************************************************************
1217 * DefFrameProcA (USER32.@)
1219 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1220 UINT message, WPARAM wParam, LPARAM lParam)
1222 if (hwndMDIClient)
1224 switch (message)
1226 case WM_SETTEXT:
1228 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1229 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1230 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1231 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1232 HeapFree( GetProcessHeap(), 0, text );
1234 return 1; /* success. FIXME: check text length */
1236 case WM_COMMAND:
1237 case WM_NCACTIVATE:
1238 case WM_NEXTMENU:
1239 case WM_SETFOCUS:
1240 case WM_SIZE:
1241 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1244 return DefWindowProcA(hwnd, message, wParam, lParam);
1248 /***********************************************************************
1249 * DefFrameProcW (USER32.@)
1251 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1252 UINT message, WPARAM wParam, LPARAM lParam)
1254 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1256 TRACE("%p %p %04x (%s) %08Ix %08Ix\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1258 if (ci)
1260 switch (message)
1262 case WM_COMMAND:
1264 WORD id = LOWORD(wParam);
1265 /* check for possible syscommands for maximized MDI child */
1266 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1268 if( (id - 0xf000) & 0xf00f ) break;
1269 if( !ci->hwndChildMaximized ) break;
1270 switch( id )
1272 case SC_CLOSE:
1273 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1274 case SC_SIZE:
1275 case SC_MOVE:
1276 case SC_MINIMIZE:
1277 case SC_MAXIMIZE:
1278 case SC_NEXTWINDOW:
1279 case SC_PREVWINDOW:
1280 case SC_RESTORE:
1281 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1282 wParam, lParam);
1285 else
1287 HWND childHwnd;
1288 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1289 /* User chose "More Windows..." */
1290 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1291 else
1292 /* User chose one of the windows listed in the "Windows" menu */
1293 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1295 if( childHwnd )
1296 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1299 break;
1301 case WM_NCACTIVATE:
1302 SendMessageW(hwndMDIClient, message, wParam, lParam);
1303 break;
1305 case WM_SETTEXT:
1306 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1307 return 1; /* success. FIXME: check text length */
1309 case WM_SETFOCUS:
1310 NtUserSetFocus( hwndMDIClient );
1311 break;
1313 case WM_SIZE:
1314 NtUserMoveWindow( hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE );
1315 break;
1317 case WM_NEXTMENU:
1319 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1321 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1323 /* control menu is between the frame system menu and
1324 * the first entry of menu bar */
1325 if ((wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1326 (wParam == VK_RIGHT && NtUserGetWindowSysSubMenu( hwnd ) == next_menu->hmenuIn))
1328 next_menu->hmenuNext = NtUserGetWindowSysSubMenu( ci->hwndActiveChild );
1329 next_menu->hwndNext = ci->hwndActiveChild;
1332 return 0;
1337 return DefWindowProcW( hwnd, message, wParam, lParam );
1340 /***********************************************************************
1341 * DefMDIChildProcA (USER32.@)
1343 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1344 WPARAM wParam, LPARAM lParam )
1346 HWND client = GetParent(hwnd);
1347 MDICLIENTINFO *ci = get_client_info( client );
1349 TRACE("%p %04x (%s) %08Ix %08Ix\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1351 hwnd = WIN_GetFullHandle( hwnd );
1352 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1354 switch (message)
1356 case WM_SETTEXT:
1357 DefWindowProcA(hwnd, message, wParam, lParam);
1358 if( ci->hwndChildMaximized == hwnd )
1359 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1360 return 1; /* success. FIXME: check text length */
1362 case WM_GETMINMAXINFO:
1363 case WM_MENUCHAR:
1364 case WM_CLOSE:
1365 case WM_SETFOCUS:
1366 case WM_CHILDACTIVATE:
1367 case WM_SYSCOMMAND:
1368 case WM_SHOWWINDOW:
1369 case WM_SETVISIBLE:
1370 case WM_SIZE:
1371 case WM_NEXTMENU:
1372 case WM_SYSCHAR:
1373 case WM_DESTROY:
1374 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1376 return DefWindowProcA(hwnd, message, wParam, lParam);
1380 /***********************************************************************
1381 * DefMDIChildProcW (USER32.@)
1383 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1384 WPARAM wParam, LPARAM lParam )
1386 HWND client = GetParent(hwnd);
1387 MDICLIENTINFO *ci = get_client_info( client );
1389 TRACE("%p %04x (%s) %08Ix %08Ix\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1391 hwnd = WIN_GetFullHandle( hwnd );
1392 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1394 switch (message)
1396 case WM_SETTEXT:
1397 DefWindowProcW(hwnd, message, wParam, lParam);
1398 if( ci->hwndChildMaximized == hwnd )
1399 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1400 return 1; /* success. FIXME: check text length */
1402 case WM_GETMINMAXINFO:
1403 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1404 return 0;
1406 case WM_MENUCHAR:
1407 return MAKELRESULT( 0, MNC_CLOSE ); /* MDI children don't have menu bars */
1409 case WM_CLOSE:
1410 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1411 return 0;
1413 case WM_SETFOCUS:
1414 if (ci->hwndActiveChild != hwnd)
1415 MDI_ChildActivate( client, hwnd );
1416 break;
1418 case WM_CHILDACTIVATE:
1419 if (IsWindowEnabled( hwnd ))
1420 MDI_ChildActivate( client, hwnd );
1421 return 0;
1423 case WM_SYSCOMMAND:
1424 switch (wParam & 0xfff0)
1426 case SC_MOVE:
1427 if( ci->hwndChildMaximized == hwnd )
1428 return 0;
1429 break;
1430 case SC_RESTORE:
1431 case SC_MINIMIZE:
1432 break;
1433 case SC_MAXIMIZE:
1434 if (ci->hwndChildMaximized == hwnd)
1435 return SendMessageW( GetParent(client), message, wParam, lParam);
1436 break;
1437 case SC_NEXTWINDOW:
1438 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 0);
1439 return 0;
1440 case SC_PREVWINDOW:
1441 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 1);
1442 return 0;
1444 break;
1446 case WM_SHOWWINDOW:
1447 case WM_SETVISIBLE:
1448 if (ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1449 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1450 break;
1452 case WM_SIZE:
1453 /* This is the only place where we switch to/from maximized state */
1454 /* do not change */
1455 TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1457 if( ci->hwndChildMaximized == hwnd && wParam != SIZE_MAXIMIZED )
1459 HWND frame;
1461 ci->hwndChildMaximized = 0;
1463 frame = GetParent(client);
1464 MDI_RestoreFrameMenu( frame, hwnd );
1465 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1468 if( wParam == SIZE_MAXIMIZED )
1470 HWND frame, hMaxChild = ci->hwndChildMaximized;
1472 if( hMaxChild == hwnd ) break;
1474 if( hMaxChild)
1476 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1478 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1479 NtUserShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1481 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1484 TRACE("maximizing child %p\n", hwnd );
1486 /* keep track of the maximized window. */
1487 ci->hwndChildMaximized = hwnd; /* !!! */
1489 frame = GetParent(client);
1490 MDI_AugmentFrameMenu( frame, hwnd );
1491 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1494 if( wParam == SIZE_MINIMIZED )
1496 HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1498 if (!switchTo) switchTo = hwnd;
1499 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1502 MDI_PostUpdate(client, ci, SB_BOTH+1);
1503 break;
1505 case WM_NEXTMENU:
1507 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1508 HWND parent = GetParent(client);
1510 if( wParam == VK_LEFT ) /* switch to frame system menu */
1512 next_menu->hmenuNext = NtUserGetWindowSysSubMenu( parent );
1514 if( wParam == VK_RIGHT ) /* to frame menu bar */
1516 next_menu->hmenuNext = GetMenu(parent);
1518 next_menu->hwndNext = parent;
1519 return 0;
1522 case WM_SYSCHAR:
1523 if (wParam == '-')
1525 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, VK_SPACE);
1526 return 0;
1528 break;
1530 case WM_DESTROY:
1531 /* Remove itself from the Window menu */
1532 MDI_RefreshMenu(ci);
1533 break;
1535 return DefWindowProcW(hwnd, message, wParam, lParam);
1538 /**********************************************************************
1539 * CreateMDIWindowA (USER32.@) Creates a MDI child
1541 * RETURNS
1542 * Success: Handle to created window
1543 * Failure: NULL
1545 HWND WINAPI CreateMDIWindowA(
1546 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1547 LPCSTR lpWindowName, /* [in] Pointer to window name */
1548 DWORD dwStyle, /* [in] Window style */
1549 INT X, /* [in] Horizontal position of window */
1550 INT Y, /* [in] Vertical position of window */
1551 INT nWidth, /* [in] Width of window */
1552 INT nHeight, /* [in] Height of window */
1553 HWND hWndParent, /* [in] Handle to parent window */
1554 HINSTANCE hInstance, /* [in] Handle to application instance */
1555 LPARAM lParam) /* [in] Application-defined value */
1557 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08Ix)\n",
1558 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1559 nWidth,nHeight,hWndParent,hInstance,lParam);
1561 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1562 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1563 0, hInstance, (LPVOID)lParam);
1566 /***********************************************************************
1567 * CreateMDIWindowW (USER32.@) Creates a MDI child
1569 * RETURNS
1570 * Success: Handle to created window
1571 * Failure: NULL
1573 HWND WINAPI CreateMDIWindowW(
1574 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1575 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1576 DWORD dwStyle, /* [in] Window style */
1577 INT X, /* [in] Horizontal position of window */
1578 INT Y, /* [in] Vertical position of window */
1579 INT nWidth, /* [in] Width of window */
1580 INT nHeight, /* [in] Height of window */
1581 HWND hWndParent, /* [in] Handle to parent window */
1582 HINSTANCE hInstance, /* [in] Handle to application instance */
1583 LPARAM lParam) /* [in] Application-defined value */
1585 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08Ix)\n",
1586 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1587 nWidth, nHeight, hWndParent, hInstance, lParam);
1589 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1590 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1591 0, hInstance, (LPVOID)lParam);
1594 /**********************************************************************
1595 * TranslateMDISysAccel (USER32.@)
1597 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1599 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1601 MDICLIENTINFO *ci = get_client_info( hwndClient );
1602 WPARAM wParam = 0;
1604 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return FALSE;
1606 /* translate if the Ctrl key is down and Alt not. */
1608 if( (NtUserGetKeyState(VK_CONTROL) & 0x8000) && !(NtUserGetKeyState(VK_MENU) & 0x8000))
1610 switch( msg->wParam )
1612 case VK_F6:
1613 case VK_TAB:
1614 wParam = ( NtUserGetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1615 break;
1616 case VK_F4:
1617 case VK_RBUTTON:
1618 if (is_close_enabled(ci->hwndActiveChild, 0))
1620 wParam = SC_CLOSE;
1621 break;
1623 /* fall through */
1624 default:
1625 return FALSE;
1627 TRACE("wParam = %04Ix\n", wParam);
1628 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, msg->wParam);
1629 return TRUE;
1632 return FALSE; /* failure */
1635 /***********************************************************************
1636 * CalcChildScroll (USER32.@)
1638 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1640 DPI_AWARENESS_CONTEXT context;
1641 SCROLLINFO info;
1642 RECT childRect, clientRect;
1643 HWND *list;
1644 DWORD style;
1646 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1648 GetClientRect( hwnd, &clientRect );
1649 SetRectEmpty( &childRect );
1651 if ((list = WIN_ListChildren( hwnd )))
1653 int i;
1654 for (i = 0; list[i]; i++)
1656 style = GetWindowLongW( list[i], GWL_STYLE );
1657 if (style & WS_MAXIMIZE)
1659 HeapFree( GetProcessHeap(), 0, list );
1660 NtUserShowScrollBar( hwnd, SB_BOTH, FALSE );
1661 return;
1663 if (style & WS_VISIBLE)
1665 RECT rect;
1666 NtUserGetChildRect( list[i], &rect );
1667 UnionRect( &childRect, &rect, &childRect );
1670 HeapFree( GetProcessHeap(), 0, list );
1672 UnionRect( &childRect, &clientRect, &childRect );
1674 /* set common info values */
1675 info.cbSize = sizeof(info);
1676 info.fMask = SIF_POS | SIF_RANGE;
1678 /* set the specific values and apply but only if window style allows */
1679 style = GetWindowLongW( hwnd, GWL_STYLE );
1680 switch( scroll )
1682 case SB_BOTH:
1683 case SB_HORZ:
1684 if (style & (WS_HSCROLL | WS_VSCROLL))
1686 info.nMin = childRect.left;
1687 info.nMax = childRect.right - clientRect.right;
1688 info.nPos = clientRect.left - childRect.left;
1689 NtUserSetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1691 if (scroll == SB_HORZ) break;
1692 /* fall through */
1693 case SB_VERT:
1694 if (style & (WS_HSCROLL | WS_VSCROLL))
1696 info.nMin = childRect.top;
1697 info.nMax = childRect.bottom - clientRect.bottom;
1698 info.nPos = clientRect.top - childRect.top;
1699 NtUserSetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1701 break;
1703 SetThreadDpiAwarenessContext( context );
1707 /***********************************************************************
1708 * ScrollChildren (USER32.@)
1710 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1711 LPARAM lParam)
1713 DPI_AWARENESS_CONTEXT context;
1714 INT newPos = -1;
1715 INT curPos, length, minPos, maxPos, shift;
1716 RECT rect;
1718 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hWnd ));
1719 GetClientRect( hWnd, &rect );
1721 switch(uMsg)
1723 case WM_HSCROLL:
1724 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1725 curPos = GetScrollPos(hWnd,SB_HORZ);
1726 length = (rect.right - rect.left) / 2;
1727 shift = GetSystemMetrics(SM_CYHSCROLL);
1728 break;
1729 case WM_VSCROLL:
1730 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1731 curPos = GetScrollPos(hWnd,SB_VERT);
1732 length = (rect.bottom - rect.top) / 2;
1733 shift = GetSystemMetrics(SM_CXVSCROLL);
1734 break;
1735 default:
1736 goto done;
1739 switch( wParam )
1741 case SB_LINEUP:
1742 newPos = curPos - shift;
1743 break;
1744 case SB_LINEDOWN:
1745 newPos = curPos + shift;
1746 break;
1747 case SB_PAGEUP:
1748 newPos = curPos - length;
1749 break;
1750 case SB_PAGEDOWN:
1751 newPos = curPos + length;
1752 break;
1754 case SB_THUMBPOSITION:
1755 newPos = LOWORD(lParam);
1756 break;
1758 case SB_THUMBTRACK:
1759 goto done;
1761 case SB_TOP:
1762 newPos = minPos;
1763 break;
1764 case SB_BOTTOM:
1765 newPos = maxPos;
1766 break;
1767 case SB_ENDSCROLL:
1768 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1769 goto done;
1772 if( newPos > maxPos )
1773 newPos = maxPos;
1774 else
1775 if( newPos < minPos )
1776 newPos = minPos;
1778 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1780 if( uMsg == WM_VSCROLL )
1781 NtUserScrollWindowEx( hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1782 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1783 else
1784 NtUserScrollWindowEx( hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1785 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1786 done:
1787 SetThreadDpiAwarenessContext( context );
1791 /******************************************************************************
1792 * CascadeWindows (USER32.@) Cascades MDI child windows
1794 * RETURNS
1795 * Success: Number of cascaded windows.
1796 * Failure: 0
1798 WORD WINAPI
1799 CascadeWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1800 UINT cKids, const HWND *lpKids)
1802 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1803 return 0;
1807 /***********************************************************************
1808 * CascadeChildWindows (USER32.@)
1810 WORD WINAPI CascadeChildWindows( HWND parent, UINT flags )
1812 return CascadeWindows( parent, flags, NULL, 0, NULL );
1816 /******************************************************************************
1817 * TileWindows (USER32.@) Tiles MDI child windows
1819 * RETURNS
1820 * Success: Number of tiled windows.
1821 * Failure: 0
1823 WORD WINAPI
1824 TileWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1825 UINT cKids, const HWND *lpKids)
1827 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1828 return 0;
1832 /***********************************************************************
1833 * TileChildWindows (USER32.@)
1835 WORD WINAPI TileChildWindows( HWND parent, UINT flags )
1837 return TileWindows( parent, flags, NULL, 0, NULL );
1841 /************************************************************************
1842 * "More Windows..." functionality
1845 /* MDI_MoreWindowsDlgProc
1847 * This function will process the messages sent to the "More Windows..."
1848 * dialog.
1849 * Return values: 0 = cancel pressed
1850 * HWND = ok pressed or double-click in the list...
1854 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1856 switch (iMsg)
1858 case WM_INITDIALOG:
1860 UINT widest = 0;
1861 UINT length;
1862 UINT i;
1863 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1864 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1866 for (i = 0; i < ci->nActiveChildren; i++)
1868 WCHAR buffer[MDI_MAXTITLELENGTH];
1870 if (!NtUserInternalGetWindowText(ci->child[i], buffer, ARRAY_SIZE(buffer)))
1871 continue;
1872 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1873 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1874 length = lstrlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1875 if (length > widest)
1876 widest = length;
1878 /* Make sure the horizontal scrollbar scrolls ok */
1879 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1881 /* Set the current selection */
1882 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1883 return TRUE;
1886 case WM_COMMAND:
1887 switch (LOWORD(wParam))
1889 default:
1890 if (HIWORD(wParam) != LBN_DBLCLK) break;
1891 /* fall through */
1892 case IDOK:
1894 /* windows are sorted by menu ID, so we must return the
1895 * window associated to the given id
1897 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1898 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1899 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1900 EndDialog(hDlg, res);
1901 return TRUE;
1903 case IDCANCEL:
1904 EndDialog(hDlg, 0);
1905 return TRUE;
1907 break;
1909 return FALSE;
1914 * MDI_MoreWindowsDialog
1916 * Prompts the user with a listbox containing the opened
1917 * documents. The user can then choose a windows and click
1918 * on OK to set the current window to the one selected, or
1919 * CANCEL to cancel. The function returns a handle to the
1920 * selected window.
1923 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1925 LPCVOID template;
1926 HRSRC hRes;
1927 HANDLE hDlgTmpl;
1929 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1931 if (hRes == 0)
1932 return 0;
1934 hDlgTmpl = LoadResource(user32_module, hRes );
1936 if (hDlgTmpl == 0)
1937 return 0;
1939 template = LockResource( hDlgTmpl );
1941 if (template == 0)
1942 return 0;
1944 return (HWND) DialogBoxIndirectParamA(user32_module, template, hwnd,
1945 MDI_MoreWindowsDlgProc, (LPARAM) hwnd);