Make WINEDLLOVERRIDES also match for *dll.
[wine/multimedia.git] / windows / mdi.c
blobc1bdb1141eba45f080a91f622d20f33e0ab6c40c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Notes: Fairly complete implementation.
24 * Also, Excel and WinWord do _not_ use MDI so if you're trying
25 * to fix them look elsewhere.
27 * Notes on how the "More Windows..." is implemented:
29 * When we have more than 9 opened windows, a "More Windows..."
30 * option appears in the "Windows" menu. Each child window has
31 * a WND* associated with it, accesible via the children list of
32 * the parent window. This WND* has a wIDmenu member, which reflects
33 * the position of the child in the window list. For example, with
34 * 9 child windows, we could have the following pattern:
38 * Name of the child window pWndChild->wIDmenu
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 #include "windef.h"
90 #include "winbase.h"
91 #include "wingdi.h"
92 #include "winuser.h"
93 #include "wownt32.h"
94 #include "wine/winuser16.h"
95 #include "wine/unicode.h"
96 #include "win.h"
97 #include "nonclient.h"
98 #include "controls.h"
99 #include "message.h"
100 #include "user.h"
101 #include "wine/debug.h"
102 #include "dlgs.h"
104 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
106 #define MDI_MAXTITLELENGTH 0xa1
108 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
110 /* "More Windows..." definitions */
111 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
112 option will appear under the Windows menu */
113 #define MDI_IDC_LISTBOX 100
114 #define IDS_MDI_MOREWINDOWS 13
116 #define MDIF_NEEDUPDATE 0x0001
118 typedef struct
120 UINT nActiveChildren;
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, 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);
142 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
143 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
145 /* -------- Miscellaneous service functions ----------
147 * MDI_GetChildByID
149 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
151 int i;
153 for (i = 0; ci->nActiveChildren; i++)
155 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
156 return ci->child[i];
158 return 0;
161 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
163 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
165 ci->mdiFlags |= MDIF_NEEDUPDATE;
166 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
168 ci->sbRecalc = recalc;
172 /*********************************************************************
173 * MDIClient class descriptor
175 const struct builtin_class_descr MDICLIENT_builtin_class =
177 "MDIClient", /* name */
178 0, /* style */
179 MDIClientWndProcA, /* procA */
180 MDIClientWndProcW, /* procW */
181 sizeof(MDICLIENTINFO), /* extra */
182 IDC_ARROW, /* cursor */
183 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
187 static MDICLIENTINFO *get_client_info( HWND client )
189 MDICLIENTINFO *ret = NULL;
190 WND *win = WIN_GetPtr( client );
191 if (win)
193 if (win == WND_OTHER_PROCESS)
195 ERR( "client %p belongs to other process\n", client );
196 return NULL;
198 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
199 else ret = (MDICLIENTINFO *)win->wExtra;
200 WIN_ReleasePtr( win );
202 return ret;
205 /**********************************************************************
206 * MDI_GetWindow
208 * returns "activateable" child different from the current or zero
210 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
211 DWORD dwStyleMask )
213 int i;
214 HWND *list;
215 HWND last = 0;
217 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
218 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
220 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
221 i = 0;
222 /* start from next after hWnd */
223 while (list[i] && list[i] != hWnd) i++;
224 if (list[i]) i++;
226 for ( ; list[i]; i++)
228 if (GetWindow( list[i], GW_OWNER )) continue;
229 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
230 last = list[i];
231 if (bNext) goto found;
233 /* now restart from the beginning */
234 for (i = 0; list[i] && list[i] != hWnd; i++)
236 if (GetWindow( list[i], GW_OWNER )) continue;
237 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
238 last = list[i];
239 if (bNext) goto found;
241 found:
242 HeapFree( GetProcessHeap(), 0, list );
243 return last;
246 /**********************************************************************
247 * MDI_CalcDefaultChildPos
249 * It seems that the default height is about 2/3 of the client rect
251 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta )
253 INT nstagger;
254 RECT rect;
255 INT spacing = GetSystemMetrics(SM_CYCAPTION) +
256 GetSystemMetrics(SM_CYFRAME) - 1;
258 if (total < 0)
260 MDICLIENTINFO *ci = get_client_info(hwndClient);
261 total = ci ? ci->nTotalCreated : 0;
264 GetClientRect( hwndClient, &rect );
265 if( rect.bottom - rect.top - delta >= spacing )
266 rect.bottom -= delta;
268 nstagger = (rect.bottom - rect.top)/(3 * spacing);
269 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
270 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
271 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
274 /**********************************************************************
275 * MDISetMenu
277 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
278 HMENU hmenuWindow)
280 MDICLIENTINFO *ci;
281 HWND hwndFrame = GetParent(hwnd);
283 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
285 if (hmenuFrame && !IsMenu(hmenuFrame))
287 WARN("hmenuFrame is not a menu handle\n");
288 return 0L;
291 if (hmenuWindow && !IsMenu(hmenuWindow))
293 WARN("hmenuWindow is not a menu handle\n");
294 return 0L;
297 if (!(ci = get_client_info( hwnd ))) return 0;
299 if (hmenuFrame)
301 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
303 if( IsZoomed(ci->hwndActiveChild) && hmenuFrame != ci->hFrameMenu )
304 MDI_RestoreFrameMenu( hwndFrame, ci->hwndActiveChild );
307 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
309 /* delete menu items from ci->hWindowMenu
310 * and add them to hmenuWindow */
311 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
312 if( ci->hWindowMenu && ci->nActiveChildren )
314 UINT nActiveChildren_old = ci->nActiveChildren;
316 /* Remove all items from old Window menu */
317 ci->nActiveChildren = 0;
318 MDI_RefreshMenu(ci);
320 ci->hWindowMenu = hmenuWindow;
322 /* Add items to the new Window menu */
323 ci->nActiveChildren = nActiveChildren_old;
324 MDI_RefreshMenu(ci);
326 else
327 ci->hWindowMenu = hmenuWindow;
330 if (hmenuFrame)
332 SetMenu(hwndFrame, hmenuFrame);
333 if( hmenuFrame != ci->hFrameMenu )
335 HMENU oldFrameMenu = ci->hFrameMenu;
337 ci->hFrameMenu = hmenuFrame;
338 if( IsZoomed(ci->hwndActiveChild) )
339 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
341 return (LRESULT)oldFrameMenu;
344 else
346 /* SetMenu() may already have been called, meaning that this window
347 * already has its menu. But they may have done a SetMenu() on
348 * an MDI window, and called MDISetMenu() after the fact, meaning
349 * that the "if" to this "else" wouldn't catch the need to
350 * augment the frame menu.
352 if( IsZoomed(ci->hwndActiveChild) )
353 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
356 return 0;
359 /**********************************************************************
360 * MDIRefreshMenu
362 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
364 UINT i, count, visible, id;
365 WCHAR buf[MDI_MAXTITLELENGTH];
367 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
369 if (!ci->hWindowMenu)
370 return 0;
372 if (!IsMenu(ci->hWindowMenu))
374 WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
375 return 0;
378 /* Windows finds the last separator in the menu, and if after it
379 * there is a menu item with MDI magic ID removes all existing
380 * menu items after it, and then adds visible MDI children.
382 count = GetMenuItemCount(ci->hWindowMenu);
383 for (i = 0; i < count; i++)
385 MENUITEMINFOW mii;
387 memset(&mii, 0, sizeof(mii));
388 mii.cbSize = sizeof(mii);
389 mii.fMask = MIIM_TYPE;
390 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
392 if (mii.fType & MF_SEPARATOR)
394 /* Windows checks only ID of the menu item */
395 memset(&mii, 0, sizeof(mii));
396 mii.cbSize = sizeof(mii);
397 mii.fMask = MIIM_ID;
398 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
400 if (mii.wID == ci->idFirstChild)
402 TRACE("removing %u items including separator\n", count - i);
403 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
404 /* nothing */;
406 break;
413 visible = 0;
414 for (i = 0; i < ci->nActiveChildren; i++)
416 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
418 id = ci->idFirstChild + visible;
420 if (visible == MDI_MOREWINDOWSLIMIT)
422 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
423 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
424 break;
427 if (!visible)
428 /* Visio expects that separator has id 0 */
429 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
431 visible++;
433 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
435 buf[0] = '&';
436 buf[1] = '0' + visible;
437 buf[2] = ' ';
438 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
439 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
440 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
442 if (ci->child[i] == ci->hwndActiveChild)
443 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
445 else
446 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
449 return (LRESULT)ci->hFrameMenu;
453 /* ------------------ MDI child window functions ---------------------- */
455 /**********************************************************************
456 * MDI_ChildGetMinMaxInfo
458 * Note: The rule here is that client rect of the maximized MDI child
459 * is equal to the client rect of the MDI client window.
461 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
463 RECT rect;
465 GetClientRect( client, &rect );
466 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
467 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
469 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
470 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
472 lpMinMax->ptMaxPosition.x = rect.left;
473 lpMinMax->ptMaxPosition.y = rect.top;
475 TRACE("max rect (%ld,%ld - %ld, %ld)\n",
476 rect.left,rect.top,rect.right,rect.bottom);
479 /**********************************************************************
480 * MDI_SwitchActiveChild
482 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
483 * being activated
485 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo )
487 HWND hwndPrev;
489 hwndPrev = ci->hwndActiveChild;
491 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
493 if ( hwndTo != hwndPrev )
495 BOOL was_zoomed = IsZoomed(hwndPrev);
497 if (was_zoomed)
499 /* restore old MDI child */
500 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
501 ShowWindow( hwndPrev, SW_RESTORE );
502 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
504 /* activate new MDI child */
505 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
506 /* maximize new MDI child */
507 ShowWindow( hwndTo, SW_MAXIMIZE );
509 /* activate new MDI child */
510 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
515 /**********************************************************************
516 * MDIDestroyChild
518 static LRESULT MDIDestroyChild( HWND parent, MDICLIENTINFO *ci,
519 HWND child, BOOL flagDestroy )
521 UINT i;
523 TRACE("# of managed children %u\n", ci->nActiveChildren);
525 if( child == ci->hwndActiveChild )
527 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
528 MDI_SwitchActiveChild(ci, next);
531 for (i = 0; i < ci->nActiveChildren; i++)
533 if (ci->child[i] == child)
535 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
536 memcpy(new_child, ci->child, i * sizeof(HWND));
537 if (i + 1 < ci->nActiveChildren)
538 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
539 HeapFree(GetProcessHeap(), 0, ci->child);
540 ci->child = new_child;
542 ci->nActiveChildren--;
543 break;
547 if (flagDestroy)
549 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
550 DestroyWindow(child);
553 TRACE("child destroyed - %p\n", child);
554 return 0;
558 /**********************************************************************
559 * MDI_ChildActivate
561 * Called in response to WM_CHILDACTIVATE
563 static LONG MDI_ChildActivate( HWND client, HWND child )
565 MDICLIENTINFO *clientInfo;
566 HWND prevActiveWnd, frame;
567 BOOL isActiveFrameWnd;
569 clientInfo = get_client_info( client );
571 if (clientInfo->hwndActiveChild == child) return 0;
573 TRACE("%p\n", child);
575 frame = GetParent(client);
576 isActiveFrameWnd = (GetActiveWindow() == frame);
577 prevActiveWnd = clientInfo->hwndActiveChild;
579 /* deactivate prev. active child */
580 if(prevActiveWnd)
582 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
583 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
586 clientInfo->hwndActiveChild = child;
588 MDI_RefreshMenu(clientInfo);
590 if( isActiveFrameWnd )
592 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
593 SetFocus(child);
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 (IsZoomed(ci->hwndActiveChild))
646 SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 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 TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
676 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
678 MDI_CalcDefaultChildPos(client, n++, pos, delta);
679 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
680 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
683 HeapFree( GetProcessHeap(), 0, win_array );
685 if (has_icons) ArrangeIconicWindows( client );
686 return 0;
689 /**********************************************************************
690 * MDITile
692 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
694 HWND *win_array;
695 int i, total;
696 BOOL has_icons = FALSE;
698 if (IsZoomed(ci->hwndActiveChild))
699 SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
701 if (ci->nActiveChildren == 0) return;
703 if (!(win_array = WIN_ListChildren( client ))) return;
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 (icon titles) */
710 if (IsIconic( win_array[i] ))
712 has_icons = TRUE;
713 continue;
715 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
716 win_array[total++] = win_array[i];
718 win_array[total] = 0;
720 TRACE("%u windows to tile\n", total);
722 if (total)
724 HWND *pWnd = win_array;
725 RECT rect;
726 int x, y, xsize, ysize;
727 int rows, columns, r, c, i;
729 GetClientRect(client,&rect);
730 rows = (int) sqrt((double)total);
731 columns = total / rows;
733 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
735 i = rows;
736 rows = columns; /* exchange r and c */
737 columns = i;
740 if (has_icons)
742 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
743 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
746 ysize = rect.bottom / rows;
747 xsize = rect.right / columns;
749 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
751 if (c == columns)
753 rows = total - i;
754 ysize = rect.bottom / rows;
757 y = 0;
758 for (r = 1; r <= rows && *pWnd; r++, i++)
760 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
761 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
762 y += ysize;
763 pWnd++;
765 x += xsize;
768 HeapFree( GetProcessHeap(), 0, win_array );
769 if (has_icons) ArrangeIconicWindows( client );
772 /* ----------------------- Frame window ---------------------------- */
775 /**********************************************************************
776 * MDI_AugmentFrameMenu
778 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
780 HMENU menu = GetMenu( frame );
781 HMENU hSysPopup = 0;
782 HBITMAP hSysMenuBitmap = 0;
783 INT nItems;
784 UINT iId;
785 HICON hIcon;
787 TRACE("frame %p,child %p\n",frame,hChild);
789 if( !menu ) return 0;
791 /* if the system buttons already exist do not add them again */
792 nItems = GetMenuItemCount(menu) - 1;
793 iId = GetMenuItemID(menu,nItems) ;
794 if (iId == SC_RESTORE || iId == SC_CLOSE)
795 return 0;
797 /* create a copy of sysmenu popup and insert it into frame menu bar */
798 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
799 return 0;
801 AppendMenuA(menu,MF_HELP | MF_BITMAP,
802 SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
803 AppendMenuA(menu,MF_HELP | MF_BITMAP,
804 SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
806 AppendMenuA(menu,MF_HELP | MF_BITMAP,
807 SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
809 /* The system menu is replaced by the child icon */
810 hIcon = (HICON)GetClassLongW(hChild, GCL_HICONSM);
811 if (!hIcon)
812 hIcon = (HICON)GetClassLongW(hChild, GCL_HICON);
813 if (!hIcon)
814 hIcon = LoadImageW(0, MAKEINTRESOURCEW(IDI_WINLOGO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
815 if (hIcon)
817 HDC hMemDC;
818 HBITMAP hBitmap, hOldBitmap;
819 HBRUSH hBrush;
820 HDC hdc = GetDC(hChild);
822 if (hdc)
824 int cx, cy;
825 cx = GetSystemMetrics(SM_CXSMICON);
826 cy = GetSystemMetrics(SM_CYSMICON);
827 hMemDC = CreateCompatibleDC(hdc);
828 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
829 hOldBitmap = SelectObject(hMemDC, hBitmap);
830 SetMapMode(hMemDC, MM_TEXT);
831 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
832 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
833 SelectObject (hMemDC, hOldBitmap);
834 DeleteObject(hBrush);
835 DeleteDC(hMemDC);
836 ReleaseDC(hChild, hdc);
837 hSysMenuBitmap = hBitmap;
841 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
842 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
844 TRACE("not inserted\n");
845 DestroyMenu(hSysPopup);
846 return 0;
849 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
850 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
851 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
852 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
854 /* redraw menu */
855 DrawMenuBar(frame);
857 return 1;
860 /**********************************************************************
861 * MDI_RestoreFrameMenu
863 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
865 MENUITEMINFOW menuInfo;
866 HMENU menu = GetMenu( frame );
867 INT nItems = GetMenuItemCount(menu) - 1;
868 UINT iId = GetMenuItemID(menu,nItems) ;
870 TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
872 /* if there is no system buttons then nothing to do */
873 if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
874 return 0;
877 * Remove the system menu, If that menu is the icon of the window
878 * as it is in win95, we have to delete the bitmap.
880 memset(&menuInfo, 0, sizeof(menuInfo));
881 menuInfo.cbSize = sizeof(menuInfo);
882 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
884 GetMenuItemInfoW(menu,
886 TRUE,
887 &menuInfo);
889 RemoveMenu(menu,0,MF_BYPOSITION);
891 if ( (menuInfo.fType & MFT_BITMAP) &&
892 (LOWORD(menuInfo.dwTypeData)!=0) &&
893 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
895 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
898 /* close */
899 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
900 /* restore */
901 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
902 /* minimize */
903 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
905 DrawMenuBar(frame);
907 return 1;
911 /**********************************************************************
912 * MDI_UpdateFrameText
914 * used when child window is maximized/restored
916 * Note: lpTitle can be NULL
918 static void MDI_UpdateFrameText( HWND frame, HWND hClient, LPCWSTR lpTitle )
920 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
921 MDICLIENTINFO *ci = get_client_info( hClient );
923 TRACE("frameText %s\n", debugstr_w(lpTitle));
925 if (!ci) return;
927 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
929 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
930 lpTitle = lpBuffer;
933 /* store new "default" title if lpTitle is not NULL */
934 if (lpTitle)
936 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
937 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
938 strcpyW( ci->frameTitle, lpTitle );
941 if (ci->frameTitle)
943 if (IsZoomed(ci->hwndActiveChild) && IsWindowVisible(ci->hwndActiveChild))
945 /* combine frame title and child title if possible */
947 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
948 static const WCHAR lpBracket2[] = {']',0};
949 int i_frame_text_length = strlenW(ci->frameTitle);
951 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
953 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
955 strcatW( lpBuffer, lpBracket );
956 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
957 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
958 strcatW( lpBuffer, lpBracket2 );
959 else
960 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
963 else
965 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
968 else
969 lpBuffer[0] = '\0';
971 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
975 /* ----------------------------- Interface ---------------------------- */
978 /**********************************************************************
979 * MDIClientWndProc_common
981 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
982 WPARAM wParam, LPARAM lParam, BOOL unicode )
984 MDICLIENTINFO *ci;
986 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
988 if (!(ci = get_client_info( hwnd ))) return 0;
990 switch (message)
992 case WM_CREATE:
994 /* Since we are using only cs->lpCreateParams, we can safely
995 * cast to LPCREATESTRUCTA here */
996 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
997 WND *wndPtr = WIN_GetPtr( hwnd );
999 wndPtr->flags |= WIN_ISMDICLIENT;
1001 /* Translation layer doesn't know what's in the cs->lpCreateParams
1002 * so we have to keep track of what environment we're in. */
1004 if( wndPtr->flags & WIN_ISWIN32 )
1006 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1007 ci->hWindowMenu = ccs->hWindowMenu;
1008 ci->idFirstChild = ccs->idFirstChild;
1010 else
1012 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1013 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1014 ci->idFirstChild = ccs->idFirstChild;
1016 WIN_ReleasePtr( wndPtr );
1018 ci->child = NULL;
1019 ci->nActiveChildren = 0;
1020 ci->nTotalCreated = 0;
1021 ci->frameTitle = NULL;
1022 ci->mdiFlags = 0;
1023 ci->hFrameMenu = GetMenu(cs->hwndParent);
1025 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1027 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %u\n",
1028 hwnd, ci->hWindowMenu, ci->idFirstChild );
1029 return 0;
1032 case WM_DESTROY:
1034 if( IsZoomed(ci->hwndActiveChild) )
1035 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndActiveChild);
1037 ci->nActiveChildren = 0;
1038 MDI_RefreshMenu(ci);
1040 if (ci->child) HeapFree( GetProcessHeap(), 0, ci->child );
1041 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1043 return 0;
1046 case WM_MDIACTIVATE:
1048 MDI_SwitchActiveChild( ci, (HWND)wParam );
1049 return 0;
1052 case WM_MDICASCADE:
1053 return MDICascade(hwnd, ci);
1055 case WM_MDICREATE:
1056 if (lParam)
1058 if (unicode)
1060 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1061 return (LRESULT)CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1062 csW->szTitle, csW->style,
1063 csW->x, csW->y, csW->cx, csW->cy,
1064 hwnd, 0, csW->hOwner,
1065 (LPVOID)csW->lParam);
1067 else
1069 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1070 return (LRESULT)CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1071 csA->szTitle, csA->style,
1072 csA->x, csA->y, csA->cx, csA->cy,
1073 hwnd, 0, csA->hOwner,
1074 (LPVOID)csA->lParam);
1077 return 0;
1079 case WM_MDIDESTROY:
1080 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1082 case WM_MDIGETACTIVE:
1083 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1084 return (LRESULT)ci->hwndActiveChild;
1086 case WM_MDIICONARRANGE:
1087 ci->mdiFlags |= MDIF_NEEDUPDATE;
1088 ArrangeIconicWindows( hwnd );
1089 ci->sbRecalc = SB_BOTH+1;
1090 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1091 return 0;
1093 case WM_MDIMAXIMIZE:
1094 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1095 return 0;
1097 case WM_MDINEXT: /* lParam != 0 means previous window */
1099 HWND next = MDI_GetWindow( ci, WIN_GetFullHandle( (HWND)wParam ), !lParam, 0 );
1100 MDI_SwitchActiveChild( ci, next );
1101 break;
1104 case WM_MDIRESTORE:
1105 SendMessageW( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1106 return 0;
1108 case WM_MDISETMENU:
1109 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1111 case WM_MDIREFRESHMENU:
1112 return MDI_RefreshMenu( ci );
1114 case WM_MDITILE:
1115 ci->mdiFlags |= MDIF_NEEDUPDATE;
1116 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1117 MDITile( hwnd, ci, wParam );
1118 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1119 return 0;
1121 case WM_VSCROLL:
1122 case WM_HSCROLL:
1123 ci->mdiFlags |= MDIF_NEEDUPDATE;
1124 ScrollChildren( hwnd, message, wParam, lParam );
1125 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1126 return 0;
1128 case WM_SETFOCUS:
1129 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1130 SetFocus( ci->hwndActiveChild );
1131 return 0;
1133 case WM_NCACTIVATE:
1134 if( ci->hwndActiveChild )
1135 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1136 break;
1138 case WM_PARENTNOTIFY:
1139 switch (LOWORD(wParam))
1141 case WM_CREATE:
1142 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1144 ci->nTotalCreated++;
1145 ci->nActiveChildren++;
1147 if (!ci->child)
1148 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1149 else
1150 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1152 TRACE("Adding MDI child %p, # of children %d\n",
1153 (HWND)lParam, ci->nActiveChildren);
1155 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1157 break;
1159 case WM_LBUTTONDOWN:
1161 HWND child;
1162 POINT pt;
1163 pt.x = (short)LOWORD(lParam);
1164 pt.y = (short)HIWORD(lParam);
1165 child = ChildWindowFromPoint(hwnd, pt);
1167 TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1169 if( child && child != hwnd && child != ci->hwndActiveChild )
1170 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1171 break;
1174 return 0;
1176 case WM_SIZE:
1177 if( IsWindow(ci->hwndActiveChild) && IsZoomed(ci->hwndActiveChild) &&
1178 IsWindowVisible(ci->hwndActiveChild) )
1180 RECT rect;
1182 rect.left = 0;
1183 rect.top = 0;
1184 rect.right = LOWORD(lParam);
1185 rect.bottom = HIWORD(lParam);
1187 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1188 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1189 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1190 rect.right - rect.left, rect.bottom - rect.top, 1);
1192 else
1193 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1195 break;
1197 case WM_MDICALCCHILDSCROLL:
1198 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1200 CalcChildScroll(hwnd, ci->sbRecalc-1);
1201 ci->sbRecalc = 0;
1202 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1204 return 0;
1206 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1207 DefWindowProcA( hwnd, message, wParam, lParam );
1210 /***********************************************************************
1211 * MDIClientWndProcA
1213 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1215 if (!IsWindow(hwnd)) return 0;
1216 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1219 /***********************************************************************
1220 * MDIClientWndProcW
1222 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1224 if (!IsWindow(hwnd)) return 0;
1225 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1228 /***********************************************************************
1229 * DefFrameProcA (USER32.@)
1231 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1232 UINT message, WPARAM wParam, LPARAM lParam)
1234 if (hwndMDIClient)
1236 switch (message)
1238 case WM_SETTEXT:
1240 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1241 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1242 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1243 MDI_UpdateFrameText( hwnd, hwndMDIClient, text );
1244 HeapFree( GetProcessHeap(), 0, text );
1246 return 1; /* success. FIXME: check text length */
1248 case WM_COMMAND:
1249 case WM_NCACTIVATE:
1250 case WM_NEXTMENU:
1251 case WM_SETFOCUS:
1252 case WM_SIZE:
1253 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1256 return DefWindowProcA(hwnd, message, wParam, lParam);
1260 /***********************************************************************
1261 * DefFrameProcW (USER32.@)
1263 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1264 UINT message, WPARAM wParam, LPARAM lParam)
1266 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1268 TRACE("%p %p %04x (%s) %08x %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1270 if (ci)
1272 switch (message)
1274 case WM_COMMAND:
1276 WORD id = LOWORD(wParam);
1277 /* check for possible syscommands for maximized MDI child */
1278 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1280 if( (id - 0xf000) & 0xf00f ) break;
1281 if( !IsZoomed(ci->hwndActiveChild) ) break;
1282 switch( id )
1284 case SC_SIZE:
1285 case SC_MOVE:
1286 case SC_MINIMIZE:
1287 case SC_MAXIMIZE:
1288 case SC_NEXTWINDOW:
1289 case SC_PREVWINDOW:
1290 case SC_CLOSE:
1291 case SC_RESTORE:
1292 return SendMessageW( ci->hwndActiveChild, WM_SYSCOMMAND,
1293 wParam, lParam);
1296 else
1298 HWND childHwnd;
1299 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1300 /* User chose "More Windows..." */
1301 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1302 else
1303 /* User chose one of the windows listed in the "Windows" menu */
1304 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1306 if( childHwnd )
1307 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1310 break;
1312 case WM_NCACTIVATE:
1313 SendMessageW(hwndMDIClient, message, wParam, lParam);
1314 break;
1316 case WM_SETTEXT:
1317 MDI_UpdateFrameText( hwnd, hwndMDIClient, (LPWSTR)lParam );
1318 return 1; /* success. FIXME: check text length */
1320 case WM_SETFOCUS:
1321 SetFocus(hwndMDIClient);
1322 break;
1324 case WM_SIZE:
1325 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1326 break;
1328 case WM_NEXTMENU:
1330 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1332 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1334 /* control menu is between the frame system menu and
1335 * the first entry of menu bar */
1336 WND *wndPtr = WIN_GetPtr(hwnd);
1338 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1339 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1341 WIN_ReleasePtr(wndPtr);
1342 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1343 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1344 next_menu->hwndNext = ci->hwndActiveChild;
1346 WIN_ReleasePtr(wndPtr);
1348 return 0;
1353 return DefWindowProcW( hwnd, message, wParam, lParam );
1356 /***********************************************************************
1357 * DefMDIChildProcA (USER32.@)
1359 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1360 WPARAM wParam, LPARAM lParam )
1362 HWND client = GetParent(hwnd);
1363 MDICLIENTINFO *ci = get_client_info( client );
1365 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1367 hwnd = WIN_GetFullHandle( hwnd );
1368 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1370 switch (message)
1372 case WM_SETTEXT:
1373 DefWindowProcA(hwnd, message, wParam, lParam);
1374 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1375 MDI_UpdateFrameText( GetParent(client), client, NULL );
1376 return 1; /* success. FIXME: check text length */
1378 case WM_GETMINMAXINFO:
1379 case WM_MENUCHAR:
1380 case WM_CLOSE:
1381 case WM_SETFOCUS:
1382 case WM_CHILDACTIVATE:
1383 case WM_SYSCOMMAND:
1384 case WM_SHOWWINDOW:
1385 case WM_SETVISIBLE:
1386 case WM_SIZE:
1387 case WM_NEXTMENU:
1388 case WM_SYSCHAR:
1389 case WM_DESTROY:
1390 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1392 return DefWindowProcA(hwnd, message, wParam, lParam);
1396 /***********************************************************************
1397 * DefMDIChildProcW (USER32.@)
1399 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1400 WPARAM wParam, LPARAM lParam )
1402 HWND client = GetParent(hwnd);
1403 MDICLIENTINFO *ci = get_client_info( client );
1405 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1407 hwnd = WIN_GetFullHandle( hwnd );
1408 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1410 switch (message)
1412 case WM_SETTEXT:
1413 DefWindowProcW(hwnd, message, wParam, lParam);
1414 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1415 MDI_UpdateFrameText( GetParent(client), client, NULL );
1416 return 1; /* success. FIXME: check text length */
1418 case WM_GETMINMAXINFO:
1419 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1420 return 0;
1422 case WM_MENUCHAR:
1423 return 0x00010000; /* MDI children don't have menu bars */
1425 case WM_CLOSE:
1426 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1427 return 0;
1429 case WM_CHILDACTIVATE:
1430 MDI_ChildActivate( client, hwnd );
1431 return 0;
1433 case WM_SYSCOMMAND:
1434 switch( wParam )
1436 case SC_MOVE:
1437 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1438 return 0;
1439 break;
1440 case SC_RESTORE:
1441 case SC_MINIMIZE:
1442 break;
1443 case SC_MAXIMIZE:
1444 if (ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1445 return SendMessageW( GetParent(client), message, wParam, lParam);
1446 break;
1447 case SC_NEXTWINDOW:
1448 SendMessageW( client, WM_MDINEXT, 0, 0);
1449 return 0;
1450 case SC_PREVWINDOW:
1451 SendMessageW( client, WM_MDINEXT, 0, 1);
1452 return 0;
1454 break;
1456 case WM_SHOWWINDOW:
1457 case WM_SETVISIBLE:
1458 if (IsZoomed(ci->hwndActiveChild)) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1459 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1460 break;
1462 case WM_SIZE:
1463 if( hwnd == ci->hwndActiveChild )
1465 if( wParam == SIZE_MAXIMIZED )
1467 TRACE("maximizing child %p\n", hwnd );
1469 MDI_AugmentFrameMenu( GetParent(client), hwnd );
1471 else
1472 MDI_RestoreFrameMenu( GetParent(client), hwnd );
1475 MDI_UpdateFrameText( GetParent(client), client, NULL );
1476 MDI_RefreshMenu(ci);
1477 MDI_PostUpdate(client, ci, SB_BOTH+1);
1478 break;
1480 case WM_NEXTMENU:
1482 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1483 HWND parent = GetParent(client);
1485 if( wParam == VK_LEFT ) /* switch to frame system menu */
1487 WND *wndPtr = WIN_GetPtr( parent );
1488 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1489 WIN_ReleasePtr( wndPtr );
1491 if( wParam == VK_RIGHT ) /* to frame menu bar */
1493 next_menu->hmenuNext = GetMenu(parent);
1495 next_menu->hwndNext = parent;
1496 return 0;
1499 case WM_SYSCHAR:
1500 if (wParam == '-')
1502 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1503 return 0;
1505 break;
1507 case WM_DESTROY:
1508 /* Remove itself from the Window menu */
1509 MDI_RefreshMenu(ci);
1510 break;
1512 return DefWindowProcW(hwnd, message, wParam, lParam);
1515 /**********************************************************************
1516 * CreateMDIWindowA (USER32.@) Creates a MDI child
1518 * RETURNS
1519 * Success: Handle to created window
1520 * Failure: NULL
1522 HWND WINAPI CreateMDIWindowA(
1523 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1524 LPCSTR lpWindowName, /* [in] Pointer to window name */
1525 DWORD dwStyle, /* [in] Window style */
1526 INT X, /* [in] Horizontal position of window */
1527 INT Y, /* [in] Vertical position of window */
1528 INT nWidth, /* [in] Width of window */
1529 INT nHeight, /* [in] Height of window */
1530 HWND hWndParent, /* [in] Handle to parent window */
1531 HINSTANCE hInstance, /* [in] Handle to application instance */
1532 LPARAM lParam) /* [in] Application-defined value */
1534 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1535 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1536 nWidth,nHeight,hWndParent,hInstance,lParam);
1538 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1539 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1540 0, hInstance, (LPVOID)lParam);
1543 /***********************************************************************
1544 * CreateMDIWindowW (USER32.@) Creates a MDI child
1546 * RETURNS
1547 * Success: Handle to created window
1548 * Failure: NULL
1550 HWND WINAPI CreateMDIWindowW(
1551 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1552 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1553 DWORD dwStyle, /* [in] Window style */
1554 INT X, /* [in] Horizontal position of window */
1555 INT Y, /* [in] Vertical position of window */
1556 INT nWidth, /* [in] Width of window */
1557 INT nHeight, /* [in] Height of window */
1558 HWND hWndParent, /* [in] Handle to parent window */
1559 HINSTANCE hInstance, /* [in] Handle to application instance */
1560 LPARAM lParam) /* [in] Application-defined value */
1562 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1563 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1564 nWidth, nHeight, hWndParent, hInstance, lParam);
1566 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1567 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1568 0, hInstance, (LPVOID)lParam);
1571 /**********************************************************************
1572 * TranslateMDISysAccel (USER32.@)
1574 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1576 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1578 MDICLIENTINFO *ci = get_client_info( hwndClient );
1579 WPARAM wParam = 0;
1581 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1583 /* translate if the Ctrl key is down and Alt not. */
1585 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1587 switch( msg->wParam )
1589 case VK_F6:
1590 case VK_TAB:
1591 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1592 break;
1593 case VK_F4:
1594 case VK_RBUTTON:
1595 wParam = SC_CLOSE;
1596 break;
1597 default:
1598 return 0;
1600 TRACE("wParam = %04x\n", wParam);
1601 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1602 return 1;
1605 return 0; /* failure */
1608 /***********************************************************************
1609 * CalcChildScroll (USER32.@)
1611 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1613 SCROLLINFO info;
1614 RECT childRect, clientRect;
1615 HWND *list;
1617 GetClientRect( hwnd, &clientRect );
1618 SetRectEmpty( &childRect );
1620 if ((list = WIN_ListChildren( hwnd )))
1622 int i;
1623 for (i = 0; list[i]; i++)
1625 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1626 if (style & WS_MAXIMIZE)
1628 HeapFree( GetProcessHeap(), 0, list );
1629 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1630 return;
1632 if (style & WS_VISIBLE)
1634 WND *pWnd = WIN_FindWndPtr( list[i] );
1635 UnionRect( &childRect, &pWnd->rectWindow, &childRect );
1636 WIN_ReleaseWndPtr( pWnd );
1639 HeapFree( GetProcessHeap(), 0, list );
1641 UnionRect( &childRect, &clientRect, &childRect );
1643 /* set common info values */
1644 info.cbSize = sizeof(info);
1645 info.fMask = SIF_POS | SIF_RANGE;
1647 /* set the specific */
1648 switch( scroll )
1650 case SB_BOTH:
1651 case SB_HORZ:
1652 info.nMin = childRect.left;
1653 info.nMax = childRect.right - clientRect.right;
1654 info.nPos = clientRect.left - childRect.left;
1655 SetScrollInfo(hwnd, scroll, &info, TRUE);
1656 if (scroll == SB_HORZ) break;
1657 /* fall through */
1658 case SB_VERT:
1659 info.nMin = childRect.top;
1660 info.nMax = childRect.bottom - clientRect.bottom;
1661 info.nPos = clientRect.top - childRect.top;
1662 SetScrollInfo(hwnd, scroll, &info, TRUE);
1663 break;
1668 /***********************************************************************
1669 * ScrollChildren (USER32.@)
1671 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1672 LPARAM lParam)
1674 INT newPos = -1;
1675 INT curPos, length, minPos, maxPos, shift;
1676 RECT rect;
1678 GetClientRect( hWnd, &rect );
1680 switch(uMsg)
1682 case WM_HSCROLL:
1683 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1684 curPos = GetScrollPos(hWnd,SB_HORZ);
1685 length = (rect.right - rect.left) / 2;
1686 shift = GetSystemMetrics(SM_CYHSCROLL);
1687 break;
1688 case WM_VSCROLL:
1689 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1690 curPos = GetScrollPos(hWnd,SB_VERT);
1691 length = (rect.bottom - rect.top) / 2;
1692 shift = GetSystemMetrics(SM_CXVSCROLL);
1693 break;
1694 default:
1695 return;
1698 switch( wParam )
1700 case SB_LINEUP:
1701 newPos = curPos - shift;
1702 break;
1703 case SB_LINEDOWN:
1704 newPos = curPos + shift;
1705 break;
1706 case SB_PAGEUP:
1707 newPos = curPos - length;
1708 break;
1709 case SB_PAGEDOWN:
1710 newPos = curPos + length;
1711 break;
1713 case SB_THUMBPOSITION:
1714 newPos = LOWORD(lParam);
1715 break;
1717 case SB_THUMBTRACK:
1718 return;
1720 case SB_TOP:
1721 newPos = minPos;
1722 break;
1723 case SB_BOTTOM:
1724 newPos = maxPos;
1725 break;
1726 case SB_ENDSCROLL:
1727 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1728 return;
1731 if( newPos > maxPos )
1732 newPos = maxPos;
1733 else
1734 if( newPos < minPos )
1735 newPos = minPos;
1737 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1739 if( uMsg == WM_VSCROLL )
1740 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1741 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1742 else
1743 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1744 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1748 /******************************************************************************
1749 * CascadeWindows (USER32.@) Cascades MDI child windows
1751 * RETURNS
1752 * Success: Number of cascaded windows.
1753 * Failure: 0
1755 WORD WINAPI
1756 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1757 UINT cKids, const HWND *lpKids)
1759 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1760 return 0;
1764 /******************************************************************************
1765 * TileWindows (USER32.@) Tiles MDI child windows
1767 * RETURNS
1768 * Success: Number of tiled windows.
1769 * Failure: 0
1771 WORD WINAPI
1772 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1773 UINT cKids, const HWND *lpKids)
1775 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1776 return 0;
1779 /************************************************************************
1780 * "More Windows..." functionality
1783 /* MDI_MoreWindowsDlgProc
1785 * This function will process the messages sent to the "More Windows..."
1786 * dialog.
1787 * Return values: 0 = cancel pressed
1788 * HWND = ok pressed or double-click in the list...
1792 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1794 switch (iMsg)
1796 case WM_INITDIALOG:
1798 UINT widest = 0;
1799 UINT length;
1800 UINT i;
1801 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1802 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1804 for (i = 0; i < ci->nActiveChildren; i++)
1806 WCHAR buffer[MDI_MAXTITLELENGTH];
1808 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1809 continue;
1810 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1811 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1812 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1813 if (length > widest)
1814 widest = length;
1816 /* Make sure the horizontal scrollbar scrolls ok */
1817 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1819 /* Set the current selection */
1820 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1821 return TRUE;
1824 case WM_COMMAND:
1825 switch (LOWORD(wParam))
1827 default:
1828 if (HIWORD(wParam) != LBN_DBLCLK) break;
1829 /* fall through */
1830 case IDOK:
1832 /* windows are sorted by menu ID, so we must return the
1833 * window associated to the given id
1835 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1836 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1837 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1838 EndDialog(hDlg, res);
1839 return TRUE;
1841 case IDCANCEL:
1842 EndDialog(hDlg, 0);
1843 return TRUE;
1845 break;
1847 return FALSE;
1852 * MDI_MoreWindowsDialog
1854 * Prompts the user with a listbox containing the opened
1855 * documents. The user can then choose a windows and click
1856 * on OK to set the current window to the one selected, or
1857 * CANCEL to cancel. The function returns a handle to the
1858 * selected window.
1861 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1863 LPCVOID template;
1864 HRSRC hRes;
1865 HANDLE hDlgTmpl;
1867 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1869 if (hRes == 0)
1870 return 0;
1872 hDlgTmpl = LoadResource(user32_module, hRes );
1874 if (hDlgTmpl == 0)
1875 return 0;
1877 template = LockResource( hDlgTmpl );
1879 if (template == 0)
1880 return 0;
1882 return (HWND) DialogBoxIndirectParamA(user32_module,
1883 (LPDLGTEMPLATEA) template,
1884 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);