wined3d: Validate (2D) texture dimensions in texture_init().
[wine.git] / dlls / user32 / mdi.c
blobbda2b002bab5a2b39e30dffa8c8a212f55a27771
1 /* MDI.C
3 * Copyright 1994, Bob Amstadt
4 * 1995,1996 Alex Korobka
6 * This file contains routines to support MDI (Multiple Document
7 * Interface) features .
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Notes: Fairly complete implementation.
24 * Also, Excel and WinWord do _not_ use MDI so if you're trying
25 * to fix them look elsewhere.
27 * Notes on how the "More Windows..." is implemented:
29 * When we have more than 9 opened windows, a "More Windows..."
30 * option appears in the "Windows" menu. Each child window has
31 * a WND* associated with it, accessible via the children list of
32 * the parent window. This WND* has a wIDmenu member, which reflects
33 * the position of the child in the window list. For example, with
34 * 9 child windows, we could have the following pattern:
38 * Name of the child window pWndChild->wIDmenu
39 * Doc1 5000
40 * Doc2 5001
41 * Doc3 5002
42 * Doc4 5003
43 * Doc5 5004
44 * Doc6 5005
45 * Doc7 5006
46 * Doc8 5007
47 * Doc9 5008
50 * The "Windows" menu, as the "More windows..." dialog, are constructed
51 * in this order. If we add a child, we would have the following list:
54 * Name of the child window pWndChild->wIDmenu
55 * Doc1 5000
56 * Doc2 5001
57 * Doc3 5002
58 * Doc4 5003
59 * Doc5 5004
60 * Doc6 5005
61 * Doc7 5006
62 * Doc8 5007
63 * Doc9 5008
64 * Doc10 5009
66 * But only 5000 to 5008 would be displayed in the "Windows" menu. We want
67 * the last created child to be in the menu, so we swap the last child with
68 * the 9th... Doc9 will be accessible via the "More Windows..." option.
70 * Doc1 5000
71 * Doc2 5001
72 * Doc3 5002
73 * Doc4 5003
74 * Doc5 5004
75 * Doc6 5005
76 * Doc7 5006
77 * Doc8 5007
78 * Doc9 5009
79 * Doc10 5008
83 #include <stdlib.h>
84 #include <stdarg.h>
85 #include <stdio.h>
86 #include <string.h>
87 #include <math.h>
89 #define OEMRESOURCE
91 #include "windef.h"
92 #include "winbase.h"
93 #include "wingdi.h"
94 #include "winuser.h"
95 #include "wownt32.h"
96 #include "wine/unicode.h"
97 #include "win.h"
98 #include "controls.h"
99 #include "user_private.h"
100 #include "wine/debug.h"
102 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
104 #define MDI_MAXTITLELENGTH 0xa1
106 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
108 /* "More Windows..." definitions */
109 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
110 option will appear under the Windows menu */
111 #define MDI_IDC_LISTBOX 100
112 #define IDS_MDI_MOREWINDOWS 13
114 #define MDIF_NEEDUPDATE 0x0001
116 typedef struct
118 /* At some points, particularly when switching MDI children, active and
119 * maximized MDI children may be not the same window, so we need to track
120 * them separately.
121 * The only place where we switch to/from maximized state is DefMDIChildProc
122 * WM_SIZE/SIZE_MAXIMIZED handler. We get that notification only after the
123 * ShowWindow(SW_SHOWMAXIMIZED) request, therefore window is guaranteed to
124 * be visible at the time we get the notification, and it's safe to assume
125 * that hwndChildMaximized is always visible.
126 * If the app plays games with WS_VISIBLE, WS_MAXIMIZE or any other window
127 * states it must keep coherency with USER32 on its own. This is true for
128 * Windows as well.
130 LONG reserved;
131 UINT nActiveChildren;
132 HWND hwndChildMaximized;
133 HWND hwndActiveChild;
134 HWND *child; /* array of tracked children */
135 HMENU hFrameMenu;
136 HMENU hWindowMenu;
137 UINT idFirstChild;
138 LPWSTR frameTitle;
139 UINT nTotalCreated;
140 UINT mdiFlags;
141 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
142 } MDICLIENTINFO;
144 static HBITMAP hBmpClose = 0;
146 /* ----------------- declarations ----------------- */
147 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
148 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
149 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
150 static LONG MDI_ChildActivate( HWND, HWND );
151 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
153 static HWND MDI_MoreWindowsDialog(HWND);
155 /* -------- Miscellaneous service functions ----------
157 * MDI_GetChildByID
159 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
161 int i;
163 for (i = 0; ci->nActiveChildren; i++)
165 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
166 return ci->child[i];
168 return 0;
171 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
173 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
175 ci->mdiFlags |= MDIF_NEEDUPDATE;
176 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
178 ci->sbRecalc = recalc;
182 /*********************************************************************
183 * MDIClient class descriptor
185 static const WCHAR mdiclientW[] = {'M','D','I','C','l','i','e','n','t',0};
186 const struct builtin_class_descr MDICLIENT_builtin_class =
188 mdiclientW, /* name */
189 0, /* style */
190 WINPROC_MDICLIENT, /* proc */
191 sizeof(MDICLIENTINFO), /* extra */
192 IDC_ARROW, /* cursor */
193 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
197 static MDICLIENTINFO *get_client_info( HWND client )
199 MDICLIENTINFO *ret = NULL;
200 WND *win = WIN_GetPtr( client );
201 if (win)
203 if (win == WND_OTHER_PROCESS || win == WND_DESKTOP)
205 if (IsWindow(client)) WARN( "client %p belongs to other process\n", client );
206 return NULL;
208 if (win->flags & WIN_ISMDICLIENT)
209 ret = (MDICLIENTINFO *)win->wExtra;
210 else
211 WARN( "%p is not an MDI client\n", client );
212 WIN_ReleasePtr( win );
214 return ret;
217 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
219 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
221 if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
222 if (hSysMenu)
224 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
225 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
226 return FALSE;
228 return TRUE;
231 /**********************************************************************
232 * MDI_GetWindow
234 * returns "activatable" child different from the current or zero
236 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
237 DWORD dwStyleMask )
239 int i;
240 HWND *list;
241 HWND last = 0;
243 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
244 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
246 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
247 i = 0;
248 /* start from next after hWnd */
249 while (list[i] && list[i] != hWnd) i++;
250 if (list[i]) i++;
252 for ( ; list[i]; i++)
254 if (GetWindow( list[i], GW_OWNER )) continue;
255 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
256 last = list[i];
257 if (bNext) goto found;
259 /* now restart from the beginning */
260 for (i = 0; list[i] && list[i] != hWnd; i++)
262 if (GetWindow( list[i], GW_OWNER )) continue;
263 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
264 last = list[i];
265 if (bNext) goto found;
267 found:
268 HeapFree( GetProcessHeap(), 0, list );
269 return last;
272 /**********************************************************************
273 * MDI_CalcDefaultChildPos
275 * It seems that the default height is about 2/3 of the client rect
277 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
279 INT nstagger;
280 RECT rect;
281 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
283 if (total < 0) /* we are called from CreateWindow */
285 MDICLIENTINFO *ci = get_client_info(hwndClient);
286 total = ci->nTotalCreated;
287 *id = ci->idFirstChild + ci->nActiveChildren;
288 TRACE("MDI child id %04x\n", *id);
291 GetClientRect( hwndClient, &rect );
292 if( rect.bottom - rect.top - delta >= spacing )
293 rect.bottom -= delta;
295 nstagger = (rect.bottom - rect.top)/(3 * spacing);
296 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
297 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
298 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
301 /**********************************************************************
302 * MDISetMenu
304 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
305 HMENU hmenuWindow)
307 MDICLIENTINFO *ci;
308 HWND hwndFrame = GetParent(hwnd);
310 TRACE("%p, frame menu %p, window menu %p\n", hwnd, hmenuFrame, hmenuWindow);
312 if (hmenuFrame && !IsMenu(hmenuFrame))
314 WARN("hmenuFrame is not a menu handle\n");
315 return 0L;
318 if (hmenuWindow && !IsMenu(hmenuWindow))
320 WARN("hmenuWindow is not a menu handle\n");
321 return 0L;
324 if (!(ci = get_client_info( hwnd ))) return 0;
326 TRACE("old frame menu %p, old window menu %p\n", ci->hFrameMenu, ci->hWindowMenu);
328 if (hmenuFrame)
330 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
332 if (ci->hwndChildMaximized)
333 MDI_RestoreFrameMenu( hwndFrame, ci->hwndChildMaximized );
336 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
338 /* delete menu items from ci->hWindowMenu
339 * and add them to hmenuWindow */
340 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
341 if( ci->hWindowMenu && ci->nActiveChildren )
343 UINT nActiveChildren_old = ci->nActiveChildren;
345 /* Remove all items from old Window menu */
346 ci->nActiveChildren = 0;
347 MDI_RefreshMenu(ci);
349 ci->hWindowMenu = hmenuWindow;
351 /* Add items to the new Window menu */
352 ci->nActiveChildren = nActiveChildren_old;
353 MDI_RefreshMenu(ci);
355 else
356 ci->hWindowMenu = hmenuWindow;
359 if (hmenuFrame)
361 SetMenu(hwndFrame, hmenuFrame);
362 if( hmenuFrame != ci->hFrameMenu )
364 HMENU oldFrameMenu = ci->hFrameMenu;
366 ci->hFrameMenu = hmenuFrame;
367 if (ci->hwndChildMaximized)
368 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
370 return (LRESULT)oldFrameMenu;
373 else
375 /* SetMenu() may already have been called, meaning that this window
376 * already has its menu. But they may have done a SetMenu() on
377 * an MDI window, and called MDISetMenu() after the fact, meaning
378 * that the "if" to this "else" wouldn't catch the need to
379 * augment the frame menu.
381 if( ci->hwndChildMaximized )
382 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
385 return 0;
388 /**********************************************************************
389 * MDIRefreshMenu
391 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
393 UINT i, count, visible, id;
394 WCHAR buf[MDI_MAXTITLELENGTH];
396 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
398 if (!ci->hWindowMenu)
399 return 0;
401 if (!IsMenu(ci->hWindowMenu))
403 WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
404 return 0;
407 /* Windows finds the last separator in the menu, and if after it
408 * there is a menu item with MDI magic ID removes all existing
409 * menu items after it, and then adds visible MDI children.
411 count = GetMenuItemCount(ci->hWindowMenu);
412 for (i = 0; i < count; i++)
414 MENUITEMINFOW mii;
416 memset(&mii, 0, sizeof(mii));
417 mii.cbSize = sizeof(mii);
418 mii.fMask = MIIM_TYPE;
419 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
421 if (mii.fType & MF_SEPARATOR)
423 /* Windows checks only ID of the menu item */
424 memset(&mii, 0, sizeof(mii));
425 mii.cbSize = sizeof(mii);
426 mii.fMask = MIIM_ID;
427 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
429 if (mii.wID == ci->idFirstChild)
431 TRACE("removing %u items including separator\n", count - i);
432 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
433 /* nothing */;
435 break;
442 visible = 0;
443 for (i = 0; i < ci->nActiveChildren; i++)
445 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
447 id = ci->idFirstChild + visible;
449 if (visible == MDI_MOREWINDOWSLIMIT)
451 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
452 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
453 break;
456 if (!visible)
457 /* Visio expects that separator has id 0 */
458 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
460 visible++;
462 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
464 buf[0] = '&';
465 buf[1] = '0' + visible;
466 buf[2] = ' ';
467 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
468 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
469 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
471 if (ci->child[i] == ci->hwndActiveChild)
472 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
474 else
475 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
478 return (LRESULT)ci->hFrameMenu;
482 /* ------------------ MDI child window functions ---------------------- */
484 /**********************************************************************
485 * MDI_ChildGetMinMaxInfo
487 * Note: The rule here is that client rect of the maximized MDI child
488 * is equal to the client rect of the MDI client window.
490 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
492 RECT rect;
494 GetClientRect( client, &rect );
495 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
496 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
498 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
499 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
501 lpMinMax->ptMaxPosition.x = rect.left;
502 lpMinMax->ptMaxPosition.y = rect.top;
504 TRACE("max rect (%d,%d - %d, %d)\n",
505 rect.left,rect.top,rect.right,rect.bottom);
508 /**********************************************************************
509 * MDI_SwitchActiveChild
511 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
512 * being activated
514 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
516 HWND hwndPrev;
518 hwndPrev = ci->hwndActiveChild;
520 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
522 if ( hwndTo != hwndPrev )
524 BOOL was_zoomed = IsZoomed(hwndPrev);
526 if (was_zoomed)
528 /* restore old MDI child */
529 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
530 ShowWindow( hwndPrev, SW_RESTORE );
531 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
533 /* activate new MDI child */
534 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
535 /* maximize new MDI child */
536 ShowWindow( hwndTo, SW_MAXIMIZE );
538 /* activate new MDI child */
539 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
544 /**********************************************************************
545 * MDIDestroyChild
547 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
548 HWND child, BOOL flagDestroy )
550 UINT i;
552 TRACE("# of managed children %u\n", ci->nActiveChildren);
554 if( child == ci->hwndActiveChild )
556 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
557 /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
558 if (flagDestroy && next)
559 MDI_SwitchActiveChild(ci, next, TRUE);
560 else
562 ShowWindow(child, SW_HIDE);
563 if (child == ci->hwndChildMaximized)
565 HWND frame = GetParent(client);
566 MDI_RestoreFrameMenu(frame, child);
567 ci->hwndChildMaximized = 0;
568 MDI_UpdateFrameText(frame, client, TRUE, NULL);
570 if (flagDestroy)
571 MDI_ChildActivate(client, 0);
575 for (i = 0; i < ci->nActiveChildren; i++)
577 if (ci->child[i] == child)
579 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
580 memcpy(new_child, ci->child, i * sizeof(HWND));
581 if (i + 1 < ci->nActiveChildren)
582 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
583 HeapFree(GetProcessHeap(), 0, ci->child);
584 ci->child = new_child;
586 ci->nActiveChildren--;
587 break;
591 if (flagDestroy)
593 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
594 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
595 DestroyWindow(child);
598 TRACE("child destroyed - %p\n", child);
599 return 0;
603 /**********************************************************************
604 * MDI_ChildActivate
606 * Called in response to WM_CHILDACTIVATE, or when last MDI child
607 * is being deactivated.
609 static LONG MDI_ChildActivate( HWND client, HWND child )
611 MDICLIENTINFO *clientInfo;
612 HWND prevActiveWnd, frame;
613 BOOL isActiveFrameWnd;
615 clientInfo = get_client_info( client );
617 if (clientInfo->hwndActiveChild == child) return 0;
619 TRACE("%p\n", child);
621 frame = GetParent(client);
622 isActiveFrameWnd = (GetActiveWindow() == frame);
623 prevActiveWnd = clientInfo->hwndActiveChild;
625 /* deactivate prev. active child */
626 if(prevActiveWnd)
628 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
629 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
632 MDI_SwitchActiveChild( clientInfo, child, FALSE );
633 clientInfo->hwndActiveChild = child;
635 MDI_RefreshMenu(clientInfo);
637 if( isActiveFrameWnd )
639 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
640 /* Let the client window manage focus for children, but if the focus
641 * is already on the client (for instance this is the 1st child) then
642 * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
643 * manually in this case.
645 if (SetFocus(client) == client)
646 SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
649 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
650 return TRUE;
653 /* -------------------- MDI client window functions ------------------- */
655 /**********************************************************************
656 * CreateMDIMenuBitmap
658 static HBITMAP CreateMDIMenuBitmap(void)
660 HDC hDCSrc = CreateCompatibleDC(0);
661 HDC hDCDest = CreateCompatibleDC(hDCSrc);
662 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
663 HBITMAP hbCopy;
664 HBITMAP hobjSrc, hobjDest;
666 hobjSrc = SelectObject(hDCSrc, hbClose);
667 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
668 hobjDest = SelectObject(hDCDest, hbCopy);
670 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
671 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
673 SelectObject(hDCSrc, hobjSrc);
674 DeleteObject(hbClose);
675 DeleteDC(hDCSrc);
677 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
679 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
680 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
682 SelectObject(hDCDest, hobjSrc );
683 SelectObject(hDCDest, hobjDest);
684 DeleteDC(hDCDest);
686 return hbCopy;
689 /**********************************************************************
690 * MDICascade
692 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
694 HWND *win_array;
695 BOOL has_icons = FALSE;
696 int i, total;
698 if (ci->hwndChildMaximized)
699 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
701 if (ci->nActiveChildren == 0) return 0;
703 if (!(win_array = WIN_ListChildren( client ))) return 0;
705 /* remove all the windows we don't want */
706 for (i = total = 0; win_array[i]; i++)
708 if (!IsWindowVisible( win_array[i] )) continue;
709 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
710 if (IsIconic( win_array[i] ))
712 has_icons = TRUE;
713 continue;
715 win_array[total++] = win_array[i];
717 win_array[total] = 0;
719 if (total)
721 INT delta = 0, n = 0, i;
722 POINT pos[2];
723 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
725 /* walk the list (backwards) and move windows */
726 for (i = total - 1; i >= 0; i--)
728 LONG style;
729 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
731 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
732 TRACE("move %p to (%d,%d) size [%d,%d]\n",
733 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
734 style = GetWindowLongW(win_array[i], GWL_STYLE);
736 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
737 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
738 posOptions);
741 HeapFree( GetProcessHeap(), 0, win_array );
743 if (has_icons) ArrangeIconicWindows( client );
744 return 0;
747 /**********************************************************************
748 * MDITile
750 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
752 HWND *win_array;
753 int i, total;
754 BOOL has_icons = FALSE;
756 if (ci->hwndChildMaximized)
757 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
759 if (ci->nActiveChildren == 0) return;
761 if (!(win_array = WIN_ListChildren( client ))) return;
763 /* remove all the windows we don't want */
764 for (i = total = 0; win_array[i]; i++)
766 if (!IsWindowVisible( win_array[i] )) continue;
767 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
768 if (IsIconic( win_array[i] ))
770 has_icons = TRUE;
771 continue;
773 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
774 win_array[total++] = win_array[i];
776 win_array[total] = 0;
778 TRACE("%u windows to tile\n", total);
780 if (total)
782 HWND *pWnd = win_array;
783 RECT rect;
784 int x, y, xsize, ysize;
785 int rows, columns, r, c, i;
787 GetClientRect(client,&rect);
788 rows = (int) sqrt((double)total);
789 columns = total / rows;
791 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
793 i = rows;
794 rows = columns; /* exchange r and c */
795 columns = i;
798 if (has_icons)
800 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
801 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
804 ysize = rect.bottom / rows;
805 xsize = rect.right / columns;
807 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
809 if (c == columns)
811 rows = total - i;
812 ysize = rect.bottom / rows;
815 y = 0;
816 for (r = 1; r <= rows && *pWnd; r++, i++)
818 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
819 LONG style = GetWindowLongW(win_array[i], GWL_STYLE);
820 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
822 SetWindowPos(*pWnd, 0, x, y, xsize, ysize, posOptions);
823 y += ysize;
824 pWnd++;
826 x += xsize;
829 HeapFree( GetProcessHeap(), 0, win_array );
830 if (has_icons) ArrangeIconicWindows( client );
833 /* ----------------------- Frame window ---------------------------- */
836 /**********************************************************************
837 * MDI_AugmentFrameMenu
839 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
841 HMENU menu = GetMenu( frame );
842 HMENU hSysPopup;
843 HBITMAP hSysMenuBitmap = 0;
844 HICON hIcon;
846 TRACE("frame %p,child %p\n",frame,hChild);
848 if (!menu) return FALSE;
850 /* create a copy of sysmenu popup and insert it into frame menu bar */
851 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
853 TRACE("child %p doesn't have a system menu\n", hChild);
854 return FALSE;
857 AppendMenuW(menu, MF_HELP | MF_BITMAP,
858 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
859 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
860 AppendMenuW(menu, MF_HELP | MF_BITMAP,
861 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
862 AppendMenuW(menu, MF_HELP | MF_BITMAP,
863 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
865 /* The system menu is replaced by the child icon */
866 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_SMALL, 0);
867 if (!hIcon)
868 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICONSM);
869 if (!hIcon)
870 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_BIG, 0);
871 if (!hIcon)
872 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICON);
873 if (!hIcon)
874 hIcon = LoadImageW(0, (LPWSTR)IDI_WINLOGO, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
875 GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
876 if (hIcon)
878 HDC hMemDC;
879 HBITMAP hBitmap, hOldBitmap;
880 HBRUSH hBrush;
881 HDC hdc = GetDC(hChild);
883 if (hdc)
885 int cx, cy;
886 cx = GetSystemMetrics(SM_CXSMICON);
887 cy = GetSystemMetrics(SM_CYSMICON);
888 hMemDC = CreateCompatibleDC(hdc);
889 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
890 hOldBitmap = SelectObject(hMemDC, hBitmap);
891 SetMapMode(hMemDC, MM_TEXT);
892 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
893 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
894 SelectObject (hMemDC, hOldBitmap);
895 DeleteObject(hBrush);
896 DeleteDC(hMemDC);
897 ReleaseDC(hChild, hdc);
898 hSysMenuBitmap = hBitmap;
902 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
903 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
905 TRACE("not inserted\n");
906 DestroyMenu(hSysPopup);
907 return FALSE;
910 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
911 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
912 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
913 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
915 /* redraw menu */
916 DrawMenuBar(frame);
918 return TRUE;
921 /**********************************************************************
922 * MDI_RestoreFrameMenu
924 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
926 MENUITEMINFOW menuInfo;
927 HMENU menu = GetMenu( frame );
928 INT nItems;
929 UINT iId;
931 TRACE("frame %p, child %p\n", frame, hChild);
933 if (!menu) return FALSE;
935 /* if there is no system buttons then nothing to do */
936 nItems = GetMenuItemCount(menu) - 1;
937 iId = GetMenuItemID(menu, nItems);
938 if ( !(iId == SC_RESTORE || iId == SC_CLOSE) )
939 return FALSE;
942 * Remove the system menu, If that menu is the icon of the window
943 * as it is in win95, we have to delete the bitmap.
945 memset(&menuInfo, 0, sizeof(menuInfo));
946 menuInfo.cbSize = sizeof(menuInfo);
947 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
949 GetMenuItemInfoW(menu,
951 TRUE,
952 &menuInfo);
954 RemoveMenu(menu,0,MF_BYPOSITION);
956 if ( (menuInfo.fType & MFT_BITMAP) &&
957 (LOWORD(menuInfo.dwTypeData)!=0) &&
958 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
960 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
963 /* close */
964 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
965 /* restore */
966 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
967 /* minimize */
968 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
970 DrawMenuBar(frame);
972 return TRUE;
976 /**********************************************************************
977 * MDI_UpdateFrameText
979 * used when child window is maximized/restored
981 * Note: lpTitle can be NULL
983 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
985 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
986 MDICLIENTINFO *ci = get_client_info( hClient );
988 TRACE("frameText %s\n", debugstr_w(lpTitle));
990 if (!ci) return;
992 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
994 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
995 lpTitle = lpBuffer;
998 /* store new "default" title if lpTitle is not NULL */
999 if (lpTitle)
1001 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1002 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
1003 strcpyW( ci->frameTitle, lpTitle );
1006 if (ci->frameTitle)
1008 if (ci->hwndChildMaximized)
1010 /* combine frame title and child title if possible */
1012 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
1013 static const WCHAR lpBracket2[] = {']',0};
1014 int i_frame_text_length = strlenW(ci->frameTitle);
1016 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1018 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1020 strcatW( lpBuffer, lpBracket );
1021 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
1022 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1023 strcatW( lpBuffer, lpBracket2 );
1024 else
1025 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1028 else
1030 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1033 else
1034 lpBuffer[0] = '\0';
1036 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1038 if (repaint)
1039 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1040 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1044 /* ----------------------------- Interface ---------------------------- */
1047 /**********************************************************************
1048 * MDIClientWndProc_common
1050 LRESULT MDIClientWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
1052 MDICLIENTINFO *ci;
1054 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1056 if (!(ci = get_client_info( hwnd )))
1058 if (message == WM_NCCREATE)
1060 WND *wndPtr = WIN_GetPtr( hwnd );
1061 wndPtr->flags |= WIN_ISMDICLIENT;
1062 WIN_ReleasePtr( wndPtr );
1064 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1065 DefWindowProcA( hwnd, message, wParam, lParam );
1068 switch (message)
1070 case WM_CREATE:
1072 /* Since we are using only cs->lpCreateParams, we can safely
1073 * cast to LPCREATESTRUCTA here */
1074 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1075 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1077 ci->hWindowMenu = ccs->hWindowMenu;
1078 ci->idFirstChild = ccs->idFirstChild;
1079 ci->hwndChildMaximized = 0;
1080 ci->child = NULL;
1081 ci->nActiveChildren = 0;
1082 ci->nTotalCreated = 0;
1083 ci->frameTitle = NULL;
1084 ci->mdiFlags = 0;
1085 ci->hFrameMenu = GetMenu(cs->hwndParent);
1087 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1089 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1090 hwnd, ci->hWindowMenu, ci->idFirstChild );
1091 return 0;
1094 case WM_DESTROY:
1096 if( ci->hwndChildMaximized )
1097 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1099 ci->nActiveChildren = 0;
1100 MDI_RefreshMenu(ci);
1102 HeapFree( GetProcessHeap(), 0, ci->child );
1103 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1105 return 0;
1108 case WM_MDIACTIVATE:
1110 if( ci->hwndActiveChild != (HWND)wParam )
1111 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1112 return 0;
1115 case WM_MDICASCADE:
1116 return MDICascade(hwnd, ci);
1118 case WM_MDICREATE:
1119 if (lParam)
1121 HWND child;
1123 if (unicode)
1125 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1126 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1127 csW->szTitle, csW->style,
1128 csW->x, csW->y, csW->cx, csW->cy,
1129 hwnd, 0, csW->hOwner,
1130 (LPVOID)csW->lParam);
1132 else
1134 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1135 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1136 csA->szTitle, csA->style,
1137 csA->x, csA->y, csA->cx, csA->cy,
1138 hwnd, 0, csA->hOwner,
1139 (LPVOID)csA->lParam);
1141 return (LRESULT)child;
1143 return 0;
1145 case WM_MDIDESTROY:
1146 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1148 case WM_MDIGETACTIVE:
1149 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1150 return (LRESULT)ci->hwndActiveChild;
1152 case WM_MDIICONARRANGE:
1153 ci->mdiFlags |= MDIF_NEEDUPDATE;
1154 ArrangeIconicWindows( hwnd );
1155 ci->sbRecalc = SB_BOTH+1;
1156 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1157 return 0;
1159 case WM_MDIMAXIMIZE:
1160 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1161 return 0;
1163 case WM_MDINEXT: /* lParam != 0 means previous window */
1165 HWND hwnd = wParam ? WIN_GetFullHandle((HWND)wParam) : ci->hwndActiveChild;
1166 HWND next = MDI_GetWindow( ci, hwnd, !lParam, 0 );
1167 MDI_SwitchActiveChild( ci, next, TRUE );
1168 if(!lParam)
1169 SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
1170 break;
1173 case WM_MDIRESTORE:
1174 ShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1175 return 0;
1177 case WM_MDISETMENU:
1178 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1180 case WM_MDIREFRESHMENU:
1181 return MDI_RefreshMenu( ci );
1183 case WM_MDITILE:
1184 ci->mdiFlags |= MDIF_NEEDUPDATE;
1185 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1186 MDITile( hwnd, ci, wParam );
1187 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1188 return 0;
1190 case WM_VSCROLL:
1191 case WM_HSCROLL:
1192 ci->mdiFlags |= MDIF_NEEDUPDATE;
1193 ScrollChildren( hwnd, message, wParam, lParam );
1194 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1195 return 0;
1197 case WM_SETFOCUS:
1198 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1199 SetFocus( ci->hwndActiveChild );
1200 return 0;
1202 case WM_NCACTIVATE:
1203 if( ci->hwndActiveChild )
1204 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1205 break;
1207 case WM_PARENTNOTIFY:
1208 switch (LOWORD(wParam))
1210 case WM_CREATE:
1211 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1213 ci->nTotalCreated++;
1214 ci->nActiveChildren++;
1216 if (!ci->child)
1217 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1218 else
1219 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1221 TRACE("Adding MDI child %p, # of children %d\n",
1222 (HWND)lParam, ci->nActiveChildren);
1224 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1226 break;
1228 case WM_LBUTTONDOWN:
1230 HWND child;
1231 POINT pt;
1232 pt.x = (short)LOWORD(lParam);
1233 pt.y = (short)HIWORD(lParam);
1234 child = ChildWindowFromPoint(hwnd, pt);
1236 TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1238 if( child && child != hwnd && child != ci->hwndActiveChild )
1239 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1240 break;
1243 case WM_DESTROY:
1244 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1246 return 0;
1248 case WM_SIZE:
1249 if( ci->hwndActiveChild && IsZoomed(ci->hwndActiveChild) )
1251 RECT rect;
1253 rect.left = 0;
1254 rect.top = 0;
1255 rect.right = LOWORD(lParam);
1256 rect.bottom = HIWORD(lParam);
1257 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1258 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1259 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1260 rect.right - rect.left, rect.bottom - rect.top, 1);
1262 else
1263 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1265 break;
1267 case WM_MDICALCCHILDSCROLL:
1268 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1270 CalcChildScroll(hwnd, ci->sbRecalc-1);
1271 ci->sbRecalc = 0;
1272 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1274 return 0;
1276 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1277 DefWindowProcA( hwnd, message, wParam, lParam );
1280 /***********************************************************************
1281 * DefFrameProcA (USER32.@)
1283 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1284 UINT message, WPARAM wParam, LPARAM lParam)
1286 if (hwndMDIClient)
1288 switch (message)
1290 case WM_SETTEXT:
1292 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1293 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1294 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1295 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1296 HeapFree( GetProcessHeap(), 0, text );
1298 return 1; /* success. FIXME: check text length */
1300 case WM_COMMAND:
1301 case WM_NCACTIVATE:
1302 case WM_NEXTMENU:
1303 case WM_SETFOCUS:
1304 case WM_SIZE:
1305 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1308 return DefWindowProcA(hwnd, message, wParam, lParam);
1312 /***********************************************************************
1313 * DefFrameProcW (USER32.@)
1315 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1316 UINT message, WPARAM wParam, LPARAM lParam)
1318 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1320 TRACE("%p %p %04x (%s) %08lx %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1322 if (ci)
1324 switch (message)
1326 case WM_COMMAND:
1328 WORD id = LOWORD(wParam);
1329 /* check for possible syscommands for maximized MDI child */
1330 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1332 if( (id - 0xf000) & 0xf00f ) break;
1333 if( !ci->hwndChildMaximized ) break;
1334 switch( id )
1336 case SC_CLOSE:
1337 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1338 case SC_SIZE:
1339 case SC_MOVE:
1340 case SC_MINIMIZE:
1341 case SC_MAXIMIZE:
1342 case SC_NEXTWINDOW:
1343 case SC_PREVWINDOW:
1344 case SC_RESTORE:
1345 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1346 wParam, lParam);
1349 else
1351 HWND childHwnd;
1352 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1353 /* User chose "More Windows..." */
1354 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1355 else
1356 /* User chose one of the windows listed in the "Windows" menu */
1357 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1359 if( childHwnd )
1360 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1363 break;
1365 case WM_NCACTIVATE:
1366 SendMessageW(hwndMDIClient, message, wParam, lParam);
1367 break;
1369 case WM_SETTEXT:
1370 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1371 return 1; /* success. FIXME: check text length */
1373 case WM_SETFOCUS:
1374 SetFocus(hwndMDIClient);
1375 break;
1377 case WM_SIZE:
1378 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1379 break;
1381 case WM_NEXTMENU:
1383 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1385 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1387 /* control menu is between the frame system menu and
1388 * the first entry of menu bar */
1389 WND *wndPtr = WIN_GetPtr(hwnd);
1391 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1392 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1394 WIN_ReleasePtr(wndPtr);
1395 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1396 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1397 next_menu->hwndNext = ci->hwndActiveChild;
1399 WIN_ReleasePtr(wndPtr);
1401 return 0;
1406 return DefWindowProcW( hwnd, message, wParam, lParam );
1409 /***********************************************************************
1410 * DefMDIChildProcA (USER32.@)
1412 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1413 WPARAM wParam, LPARAM lParam )
1415 HWND client = GetParent(hwnd);
1416 MDICLIENTINFO *ci = get_client_info( client );
1418 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1420 hwnd = WIN_GetFullHandle( hwnd );
1421 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1423 switch (message)
1425 case WM_SETTEXT:
1426 DefWindowProcA(hwnd, message, wParam, lParam);
1427 if( ci->hwndChildMaximized == hwnd )
1428 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1429 return 1; /* success. FIXME: check text length */
1431 case WM_GETMINMAXINFO:
1432 case WM_MENUCHAR:
1433 case WM_CLOSE:
1434 case WM_SETFOCUS:
1435 case WM_CHILDACTIVATE:
1436 case WM_SYSCOMMAND:
1437 case WM_SHOWWINDOW:
1438 case WM_SETVISIBLE:
1439 case WM_SIZE:
1440 case WM_NEXTMENU:
1441 case WM_SYSCHAR:
1442 case WM_DESTROY:
1443 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1445 return DefWindowProcA(hwnd, message, wParam, lParam);
1449 /***********************************************************************
1450 * DefMDIChildProcW (USER32.@)
1452 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1453 WPARAM wParam, LPARAM lParam )
1455 HWND client = GetParent(hwnd);
1456 MDICLIENTINFO *ci = get_client_info( client );
1458 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1460 hwnd = WIN_GetFullHandle( hwnd );
1461 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1463 switch (message)
1465 case WM_SETTEXT:
1466 DefWindowProcW(hwnd, message, wParam, lParam);
1467 if( ci->hwndChildMaximized == hwnd )
1468 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1469 return 1; /* success. FIXME: check text length */
1471 case WM_GETMINMAXINFO:
1472 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1473 return 0;
1475 case WM_MENUCHAR:
1476 return MAKELRESULT( 0, MNC_CLOSE ); /* MDI children don't have menu bars */
1478 case WM_CLOSE:
1479 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1480 return 0;
1482 case WM_SETFOCUS:
1483 if (ci->hwndActiveChild != hwnd)
1484 MDI_ChildActivate( client, hwnd );
1485 break;
1487 case WM_CHILDACTIVATE:
1488 if (IsWindowEnabled( hwnd ))
1489 MDI_ChildActivate( client, hwnd );
1490 return 0;
1492 case WM_SYSCOMMAND:
1493 switch (wParam & 0xfff0)
1495 case SC_MOVE:
1496 if( ci->hwndChildMaximized == hwnd )
1497 return 0;
1498 break;
1499 case SC_RESTORE:
1500 case SC_MINIMIZE:
1501 break;
1502 case SC_MAXIMIZE:
1503 if (ci->hwndChildMaximized == hwnd)
1504 return SendMessageW( GetParent(client), message, wParam, lParam);
1505 break;
1506 case SC_NEXTWINDOW:
1507 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 0);
1508 return 0;
1509 case SC_PREVWINDOW:
1510 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 1);
1511 return 0;
1513 break;
1515 case WM_SHOWWINDOW:
1516 case WM_SETVISIBLE:
1517 if (ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1518 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1519 break;
1521 case WM_SIZE:
1522 /* This is the only place where we switch to/from maximized state */
1523 /* do not change */
1524 TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1526 if( ci->hwndChildMaximized == hwnd && wParam != SIZE_MAXIMIZED )
1528 HWND frame;
1530 ci->hwndChildMaximized = 0;
1532 frame = GetParent(client);
1533 MDI_RestoreFrameMenu( frame, hwnd );
1534 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1537 if( wParam == SIZE_MAXIMIZED )
1539 HWND frame, hMaxChild = ci->hwndChildMaximized;
1541 if( hMaxChild == hwnd ) break;
1543 if( hMaxChild)
1545 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1547 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1548 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1550 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1553 TRACE("maximizing child %p\n", hwnd );
1555 /* keep track of the maximized window. */
1556 ci->hwndChildMaximized = hwnd; /* !!! */
1558 frame = GetParent(client);
1559 MDI_AugmentFrameMenu( frame, hwnd );
1560 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1563 if( wParam == SIZE_MINIMIZED )
1565 HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1567 if (!switchTo) switchTo = hwnd;
1568 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1571 MDI_PostUpdate(client, ci, SB_BOTH+1);
1572 break;
1574 case WM_NEXTMENU:
1576 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1577 HWND parent = GetParent(client);
1579 if( wParam == VK_LEFT ) /* switch to frame system menu */
1581 WND *wndPtr = WIN_GetPtr( parent );
1582 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1583 WIN_ReleasePtr( wndPtr );
1585 if( wParam == VK_RIGHT ) /* to frame menu bar */
1587 next_menu->hmenuNext = GetMenu(parent);
1589 next_menu->hwndNext = parent;
1590 return 0;
1593 case WM_SYSCHAR:
1594 if (wParam == '-')
1596 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, VK_SPACE);
1597 return 0;
1599 break;
1601 case WM_DESTROY:
1602 /* Remove itself from the Window menu */
1603 MDI_RefreshMenu(ci);
1604 break;
1606 return DefWindowProcW(hwnd, message, wParam, lParam);
1609 /**********************************************************************
1610 * CreateMDIWindowA (USER32.@) Creates a MDI child
1612 * RETURNS
1613 * Success: Handle to created window
1614 * Failure: NULL
1616 HWND WINAPI CreateMDIWindowA(
1617 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1618 LPCSTR lpWindowName, /* [in] Pointer to window name */
1619 DWORD dwStyle, /* [in] Window style */
1620 INT X, /* [in] Horizontal position of window */
1621 INT Y, /* [in] Vertical position of window */
1622 INT nWidth, /* [in] Width of window */
1623 INT nHeight, /* [in] Height of window */
1624 HWND hWndParent, /* [in] Handle to parent window */
1625 HINSTANCE hInstance, /* [in] Handle to application instance */
1626 LPARAM lParam) /* [in] Application-defined value */
1628 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1629 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1630 nWidth,nHeight,hWndParent,hInstance,lParam);
1632 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1633 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1634 0, hInstance, (LPVOID)lParam);
1637 /***********************************************************************
1638 * CreateMDIWindowW (USER32.@) Creates a MDI child
1640 * RETURNS
1641 * Success: Handle to created window
1642 * Failure: NULL
1644 HWND WINAPI CreateMDIWindowW(
1645 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1646 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1647 DWORD dwStyle, /* [in] Window style */
1648 INT X, /* [in] Horizontal position of window */
1649 INT Y, /* [in] Vertical position of window */
1650 INT nWidth, /* [in] Width of window */
1651 INT nHeight, /* [in] Height of window */
1652 HWND hWndParent, /* [in] Handle to parent window */
1653 HINSTANCE hInstance, /* [in] Handle to application instance */
1654 LPARAM lParam) /* [in] Application-defined value */
1656 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1657 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1658 nWidth, nHeight, hWndParent, hInstance, lParam);
1660 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1661 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1662 0, hInstance, (LPVOID)lParam);
1665 /**********************************************************************
1666 * TranslateMDISysAccel (USER32.@)
1668 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1670 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1672 MDICLIENTINFO *ci = get_client_info( hwndClient );
1673 WPARAM wParam = 0;
1675 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return FALSE;
1677 /* translate if the Ctrl key is down and Alt not. */
1679 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1681 switch( msg->wParam )
1683 case VK_F6:
1684 case VK_TAB:
1685 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1686 break;
1687 case VK_F4:
1688 case VK_RBUTTON:
1689 if (is_close_enabled(ci->hwndActiveChild, 0))
1691 wParam = SC_CLOSE;
1692 break;
1694 /* fall through */
1695 default:
1696 return FALSE;
1698 TRACE("wParam = %04lx\n", wParam);
1699 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, msg->wParam);
1700 return TRUE;
1703 return FALSE; /* failure */
1706 /***********************************************************************
1707 * CalcChildScroll (USER32.@)
1709 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1711 SCROLLINFO info;
1712 RECT childRect, clientRect;
1713 HWND *list;
1714 DWORD style;
1716 GetClientRect( hwnd, &clientRect );
1717 SetRectEmpty( &childRect );
1719 if ((list = WIN_ListChildren( hwnd )))
1721 int i;
1722 for (i = 0; list[i]; i++)
1724 style = GetWindowLongW( list[i], GWL_STYLE );
1725 if (style & WS_MAXIMIZE)
1727 HeapFree( GetProcessHeap(), 0, list );
1728 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1729 return;
1731 if (style & WS_VISIBLE)
1733 RECT rect;
1734 WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL );
1735 UnionRect( &childRect, &rect, &childRect );
1738 HeapFree( GetProcessHeap(), 0, list );
1740 UnionRect( &childRect, &clientRect, &childRect );
1742 /* set common info values */
1743 info.cbSize = sizeof(info);
1744 info.fMask = SIF_POS | SIF_RANGE;
1746 /* set the specific values and apply but only if window style allows */
1747 style = GetWindowLongW( hwnd, GWL_STYLE );
1748 switch( scroll )
1750 case SB_BOTH:
1751 case SB_HORZ:
1752 if (style & (WS_HSCROLL | WS_VSCROLL))
1754 info.nMin = childRect.left;
1755 info.nMax = childRect.right - clientRect.right;
1756 info.nPos = clientRect.left - childRect.left;
1757 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1759 if (scroll == SB_HORZ) break;
1760 /* fall through */
1761 case SB_VERT:
1762 if (style & (WS_HSCROLL | WS_VSCROLL))
1764 info.nMin = childRect.top;
1765 info.nMax = childRect.bottom - clientRect.bottom;
1766 info.nPos = clientRect.top - childRect.top;
1767 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1769 break;
1774 /***********************************************************************
1775 * ScrollChildren (USER32.@)
1777 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1778 LPARAM lParam)
1780 INT newPos = -1;
1781 INT curPos, length, minPos, maxPos, shift;
1782 RECT rect;
1784 GetClientRect( hWnd, &rect );
1786 switch(uMsg)
1788 case WM_HSCROLL:
1789 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1790 curPos = GetScrollPos(hWnd,SB_HORZ);
1791 length = (rect.right - rect.left) / 2;
1792 shift = GetSystemMetrics(SM_CYHSCROLL);
1793 break;
1794 case WM_VSCROLL:
1795 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1796 curPos = GetScrollPos(hWnd,SB_VERT);
1797 length = (rect.bottom - rect.top) / 2;
1798 shift = GetSystemMetrics(SM_CXVSCROLL);
1799 break;
1800 default:
1801 return;
1804 switch( wParam )
1806 case SB_LINEUP:
1807 newPos = curPos - shift;
1808 break;
1809 case SB_LINEDOWN:
1810 newPos = curPos + shift;
1811 break;
1812 case SB_PAGEUP:
1813 newPos = curPos - length;
1814 break;
1815 case SB_PAGEDOWN:
1816 newPos = curPos + length;
1817 break;
1819 case SB_THUMBPOSITION:
1820 newPos = LOWORD(lParam);
1821 break;
1823 case SB_THUMBTRACK:
1824 return;
1826 case SB_TOP:
1827 newPos = minPos;
1828 break;
1829 case SB_BOTTOM:
1830 newPos = maxPos;
1831 break;
1832 case SB_ENDSCROLL:
1833 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1834 return;
1837 if( newPos > maxPos )
1838 newPos = maxPos;
1839 else
1840 if( newPos < minPos )
1841 newPos = minPos;
1843 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1845 if( uMsg == WM_VSCROLL )
1846 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1847 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1848 else
1849 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1850 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1854 /******************************************************************************
1855 * CascadeWindows (USER32.@) Cascades MDI child windows
1857 * RETURNS
1858 * Success: Number of cascaded windows.
1859 * Failure: 0
1861 WORD WINAPI
1862 CascadeWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1863 UINT cKids, const HWND *lpKids)
1865 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1866 return 0;
1870 /***********************************************************************
1871 * CascadeChildWindows (USER32.@)
1873 WORD WINAPI CascadeChildWindows( HWND parent, UINT flags )
1875 return CascadeWindows( parent, flags, NULL, 0, NULL );
1879 /******************************************************************************
1880 * TileWindows (USER32.@) Tiles MDI child windows
1882 * RETURNS
1883 * Success: Number of tiled windows.
1884 * Failure: 0
1886 WORD WINAPI
1887 TileWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1888 UINT cKids, const HWND *lpKids)
1890 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1891 return 0;
1895 /***********************************************************************
1896 * TileChildWindows (USER32.@)
1898 WORD WINAPI TileChildWindows( HWND parent, UINT flags )
1900 return TileWindows( parent, flags, NULL, 0, NULL );
1904 /************************************************************************
1905 * "More Windows..." functionality
1908 /* MDI_MoreWindowsDlgProc
1910 * This function will process the messages sent to the "More Windows..."
1911 * dialog.
1912 * Return values: 0 = cancel pressed
1913 * HWND = ok pressed or double-click in the list...
1917 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1919 switch (iMsg)
1921 case WM_INITDIALOG:
1923 UINT widest = 0;
1924 UINT length;
1925 UINT i;
1926 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1927 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1929 for (i = 0; i < ci->nActiveChildren; i++)
1931 WCHAR buffer[MDI_MAXTITLELENGTH];
1933 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1934 continue;
1935 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1936 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1937 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1938 if (length > widest)
1939 widest = length;
1941 /* Make sure the horizontal scrollbar scrolls ok */
1942 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1944 /* Set the current selection */
1945 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1946 return TRUE;
1949 case WM_COMMAND:
1950 switch (LOWORD(wParam))
1952 default:
1953 if (HIWORD(wParam) != LBN_DBLCLK) break;
1954 /* fall through */
1955 case IDOK:
1957 /* windows are sorted by menu ID, so we must return the
1958 * window associated to the given id
1960 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1961 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1962 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1963 EndDialog(hDlg, res);
1964 return TRUE;
1966 case IDCANCEL:
1967 EndDialog(hDlg, 0);
1968 return TRUE;
1970 break;
1972 return FALSE;
1977 * MDI_MoreWindowsDialog
1979 * Prompts the user with a listbox containing the opened
1980 * documents. The user can then choose a windows and click
1981 * on OK to set the current window to the one selected, or
1982 * CANCEL to cancel. The function returns a handle to the
1983 * selected window.
1986 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1988 LPCVOID template;
1989 HRSRC hRes;
1990 HANDLE hDlgTmpl;
1992 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1994 if (hRes == 0)
1995 return 0;
1997 hDlgTmpl = LoadResource(user32_module, hRes );
1999 if (hDlgTmpl == 0)
2000 return 0;
2002 template = LockResource( hDlgTmpl );
2004 if (template == 0)
2005 return 0;
2007 return (HWND) DialogBoxIndirectParamA(user32_module, template, hwnd,
2008 MDI_MoreWindowsDlgProc, (LPARAM) hwnd);