Implement SetupDiClassNameFromGuidExA.
[wine/multimedia.git] / windows / mdi.c
blobf54222d45af9b75f556f376de2e7f5586f41d3d4
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 "controls.h"
98 #include "message.h"
99 #include "user_private.h"
100 #include "wine/debug.h"
101 #include "dlgs.h"
103 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
105 #define MDI_MAXTITLELENGTH 0xa1
107 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
109 /* "More Windows..." definitions */
110 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
111 option will appear under the Windows menu */
112 #define MDI_IDC_LISTBOX 100
113 #define IDS_MDI_MOREWINDOWS 13
115 #define MDIF_NEEDUPDATE 0x0001
117 typedef struct
119 UINT nActiveChildren;
120 HWND hwndActiveChild;
121 HWND *child; /* array of tracked children */
122 HMENU hFrameMenu;
123 HMENU hWindowMenu;
124 UINT idFirstChild;
125 LPWSTR frameTitle;
126 UINT nTotalCreated;
127 UINT mdiFlags;
128 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
129 } MDICLIENTINFO;
131 static HBITMAP hBmpClose = 0;
133 /* ----------------- declarations ----------------- */
134 static void MDI_UpdateFrameText( HWND, HWND, LPCWSTR);
135 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
136 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
137 static LONG MDI_ChildActivate( HWND, HWND );
138 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
140 static HWND MDI_MoreWindowsDialog(HWND);
141 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
142 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
144 /* -------- Miscellaneous service functions ----------
146 * MDI_GetChildByID
148 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
150 int i;
152 for (i = 0; ci->nActiveChildren; i++)
154 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
155 return ci->child[i];
157 return 0;
160 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
162 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
164 ci->mdiFlags |= MDIF_NEEDUPDATE;
165 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
167 ci->sbRecalc = recalc;
171 /*********************************************************************
172 * MDIClient class descriptor
174 const struct builtin_class_descr MDICLIENT_builtin_class =
176 "MDIClient", /* name */
177 0, /* style */
178 MDIClientWndProcA, /* procA */
179 MDIClientWndProcW, /* procW */
180 sizeof(MDICLIENTINFO), /* extra */
181 IDC_ARROW, /* cursor */
182 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
186 static MDICLIENTINFO *get_client_info( HWND client )
188 MDICLIENTINFO *ret = NULL;
189 WND *win = WIN_GetPtr( client );
190 if (win)
192 if (win == WND_OTHER_PROCESS)
194 if (IsWindow(client)) ERR( "client %p belongs to other process\n", client );
195 return NULL;
197 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
198 else ret = (MDICLIENTINFO *)win->wExtra;
199 WIN_ReleasePtr( win );
201 return ret;
204 /**********************************************************************
205 * MDI_GetWindow
207 * returns "activateable" child different from the current or zero
209 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
210 DWORD dwStyleMask )
212 int i;
213 HWND *list;
214 HWND last = 0;
216 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
217 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
219 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
220 i = 0;
221 /* start from next after hWnd */
222 while (list[i] && list[i] != hWnd) i++;
223 if (list[i]) i++;
225 for ( ; list[i]; i++)
227 if (GetWindow( list[i], GW_OWNER )) continue;
228 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
229 last = list[i];
230 if (bNext) goto found;
232 /* now restart from the beginning */
233 for (i = 0; list[i] && list[i] != hWnd; i++)
235 if (GetWindow( list[i], GW_OWNER )) continue;
236 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
237 last = list[i];
238 if (bNext) goto found;
240 found:
241 HeapFree( GetProcessHeap(), 0, list );
242 return last;
245 /**********************************************************************
246 * MDI_CalcDefaultChildPos
248 * It seems that the default height is about 2/3 of the client rect
250 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
252 INT nstagger;
253 RECT rect;
254 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
256 if (total < 0) /* we are called from CreateWindow */
258 MDICLIENTINFO *ci = get_client_info(hwndClient);
259 total = ci ? ci->nTotalCreated : 0;
260 *id = ci->idFirstChild + ci->nActiveChildren;
261 TRACE("MDI child id %04x\n", *id);
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))
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) && (GetWindowLongW(ci->hwndActiveChild, GWL_STYLE) & WS_VISIBLE))
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, BOOL activate )
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 | (activate ? 0 : SWP_NOACTIVATE) );
515 /**********************************************************************
516 * MDIDestroyChild
518 static LRESULT MDIDestroyChild( HWND client, 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 if (next)
529 MDI_SwitchActiveChild(ci, next, TRUE);
530 else
532 ShowWindow(child, SW_HIDE);
533 if (IsZoomed(child))
535 MDI_RestoreFrameMenu(GetParent(client), child);
536 MDI_UpdateFrameText(GetParent(client), client, NULL);
538 MDI_ChildActivate(client, 0);
542 for (i = 0; i < ci->nActiveChildren; i++)
544 if (ci->child[i] == child)
546 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
547 memcpy(new_child, ci->child, i * sizeof(HWND));
548 if (i + 1 < ci->nActiveChildren)
549 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
550 HeapFree(GetProcessHeap(), 0, ci->child);
551 ci->child = new_child;
553 ci->nActiveChildren--;
554 break;
558 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
560 if (flagDestroy)
562 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
563 DestroyWindow(child);
566 TRACE("child destroyed - %p\n", child);
567 return 0;
571 /**********************************************************************
572 * MDI_ChildActivate
574 * Called in response to WM_CHILDACTIVATE, or when last MDI child
575 * is being deactivated.
577 static LONG MDI_ChildActivate( HWND client, HWND child )
579 MDICLIENTINFO *clientInfo;
580 HWND prevActiveWnd, frame;
581 BOOL isActiveFrameWnd;
583 clientInfo = get_client_info( client );
585 if (clientInfo->hwndActiveChild == child) return 0;
587 TRACE("%p\n", child);
589 frame = GetParent(client);
590 isActiveFrameWnd = (GetActiveWindow() == frame);
591 prevActiveWnd = clientInfo->hwndActiveChild;
593 /* deactivate prev. active child */
594 if(prevActiveWnd)
596 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
597 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
600 MDI_SwitchActiveChild( clientInfo, child, FALSE );
601 clientInfo->hwndActiveChild = child;
603 MDI_RefreshMenu(clientInfo);
605 if( isActiveFrameWnd )
607 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
608 SetFocus( client );
611 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
612 return TRUE;
615 /* -------------------- MDI client window functions ------------------- */
617 /**********************************************************************
618 * CreateMDIMenuBitmap
620 static HBITMAP CreateMDIMenuBitmap(void)
622 HDC hDCSrc = CreateCompatibleDC(0);
623 HDC hDCDest = CreateCompatibleDC(hDCSrc);
624 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
625 HBITMAP hbCopy;
626 HBITMAP hobjSrc, hobjDest;
628 hobjSrc = SelectObject(hDCSrc, hbClose);
629 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
630 hobjDest = SelectObject(hDCDest, hbCopy);
632 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
633 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
635 SelectObject(hDCSrc, hobjSrc);
636 DeleteObject(hbClose);
637 DeleteDC(hDCSrc);
639 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
641 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
642 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
644 SelectObject(hDCDest, hobjSrc );
645 SelectObject(hDCDest, hobjDest);
646 DeleteDC(hDCDest);
648 return hbCopy;
651 /**********************************************************************
652 * MDICascade
654 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
656 HWND *win_array;
657 BOOL has_icons = FALSE;
658 int i, total;
660 if (IsZoomed(ci->hwndActiveChild))
661 SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
663 if (ci->nActiveChildren == 0) return 0;
665 if (!(win_array = WIN_ListChildren( client ))) return 0;
667 /* remove all the windows we don't want */
668 for (i = total = 0; win_array[i]; i++)
670 if (!IsWindowVisible( win_array[i] )) continue;
671 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
672 if (IsIconic( win_array[i] ))
674 has_icons = TRUE;
675 continue;
677 win_array[total++] = win_array[i];
679 win_array[total] = 0;
681 if (total)
683 INT delta = 0, n = 0, i;
684 POINT pos[2];
685 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
687 /* walk the list (backwards) and move windows */
688 for (i = total - 1; i >= 0; i--)
690 TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
691 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
693 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
694 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
695 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
698 HeapFree( GetProcessHeap(), 0, win_array );
700 if (has_icons) ArrangeIconicWindows( client );
701 return 0;
704 /**********************************************************************
705 * MDITile
707 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
709 HWND *win_array;
710 int i, total;
711 BOOL has_icons = FALSE;
713 if (IsZoomed(ci->hwndActiveChild))
714 SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
716 if (ci->nActiveChildren == 0) return;
718 if (!(win_array = WIN_ListChildren( client ))) return;
720 /* remove all the windows we don't want */
721 for (i = total = 0; win_array[i]; i++)
723 if (!IsWindowVisible( win_array[i] )) continue;
724 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
725 if (IsIconic( win_array[i] ))
727 has_icons = TRUE;
728 continue;
730 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
731 win_array[total++] = win_array[i];
733 win_array[total] = 0;
735 TRACE("%u windows to tile\n", total);
737 if (total)
739 HWND *pWnd = win_array;
740 RECT rect;
741 int x, y, xsize, ysize;
742 int rows, columns, r, c, i;
744 GetClientRect(client,&rect);
745 rows = (int) sqrt((double)total);
746 columns = total / rows;
748 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
750 i = rows;
751 rows = columns; /* exchange r and c */
752 columns = i;
755 if (has_icons)
757 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
758 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
761 ysize = rect.bottom / rows;
762 xsize = rect.right / columns;
764 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
766 if (c == columns)
768 rows = total - i;
769 ysize = rect.bottom / rows;
772 y = 0;
773 for (r = 1; r <= rows && *pWnd; r++, i++)
775 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
776 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
777 y += ysize;
778 pWnd++;
780 x += xsize;
783 HeapFree( GetProcessHeap(), 0, win_array );
784 if (has_icons) ArrangeIconicWindows( client );
787 /* ----------------------- Frame window ---------------------------- */
790 /**********************************************************************
791 * MDI_AugmentFrameMenu
793 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
795 HMENU menu = GetMenu( frame );
796 HMENU hSysPopup = 0;
797 HBITMAP hSysMenuBitmap = 0;
798 INT nItems;
799 UINT iId;
800 HICON hIcon;
802 TRACE("frame %p,child %p\n",frame,hChild);
804 if( !menu ) return 0;
806 /* if the system buttons already exist do not add them again */
807 nItems = GetMenuItemCount(menu) - 1;
808 iId = GetMenuItemID(menu,nItems) ;
809 if (iId == SC_RESTORE || iId == SC_CLOSE)
810 return 0;
812 /* create a copy of sysmenu popup and insert it into frame menu bar */
813 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
814 return 0;
816 AppendMenuA(menu,MF_HELP | MF_BITMAP,
817 SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
818 AppendMenuA(menu,MF_HELP | MF_BITMAP,
819 SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
821 AppendMenuA(menu,MF_HELP | MF_BITMAP,
822 SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
824 /* The system menu is replaced by the child icon */
825 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICONSM);
826 if (!hIcon)
827 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICON);
828 if (!hIcon)
829 hIcon = LoadImageW(0, MAKEINTRESOURCEW(IDI_WINLOGO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
830 if (hIcon)
832 HDC hMemDC;
833 HBITMAP hBitmap, hOldBitmap;
834 HBRUSH hBrush;
835 HDC hdc = GetDC(hChild);
837 if (hdc)
839 int cx, cy;
840 cx = GetSystemMetrics(SM_CXSMICON);
841 cy = GetSystemMetrics(SM_CYSMICON);
842 hMemDC = CreateCompatibleDC(hdc);
843 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
844 hOldBitmap = SelectObject(hMemDC, hBitmap);
845 SetMapMode(hMemDC, MM_TEXT);
846 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
847 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
848 SelectObject (hMemDC, hOldBitmap);
849 DeleteObject(hBrush);
850 DeleteDC(hMemDC);
851 ReleaseDC(hChild, hdc);
852 hSysMenuBitmap = hBitmap;
856 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
857 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
859 TRACE("not inserted\n");
860 DestroyMenu(hSysPopup);
861 return 0;
864 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
865 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
866 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
867 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
869 /* redraw menu */
870 DrawMenuBar(frame);
872 return 1;
875 /**********************************************************************
876 * MDI_RestoreFrameMenu
878 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
880 MENUITEMINFOW menuInfo;
881 HMENU menu = GetMenu( frame );
882 INT nItems = GetMenuItemCount(menu) - 1;
883 UINT iId = GetMenuItemID(menu,nItems) ;
885 TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
887 if( !menu ) return 0;
889 /* if there is no system buttons then nothing to do */
890 if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
891 return 0;
894 * Remove the system menu, If that menu is the icon of the window
895 * as it is in win95, we have to delete the bitmap.
897 memset(&menuInfo, 0, sizeof(menuInfo));
898 menuInfo.cbSize = sizeof(menuInfo);
899 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
901 GetMenuItemInfoW(menu,
903 TRUE,
904 &menuInfo);
906 RemoveMenu(menu,0,MF_BYPOSITION);
908 if ( (menuInfo.fType & MFT_BITMAP) &&
909 (LOWORD(menuInfo.dwTypeData)!=0) &&
910 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
912 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
915 /* close */
916 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
917 /* restore */
918 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
919 /* minimize */
920 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
922 DrawMenuBar(frame);
924 return 1;
928 /**********************************************************************
929 * MDI_UpdateFrameText
931 * used when child window is maximized/restored
933 * Note: lpTitle can be NULL
935 static void MDI_UpdateFrameText( HWND frame, HWND hClient, LPCWSTR lpTitle )
937 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
938 MDICLIENTINFO *ci = get_client_info( hClient );
940 TRACE("frameText %s\n", debugstr_w(lpTitle));
942 if (!ci) return;
944 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
946 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
947 lpTitle = lpBuffer;
950 /* store new "default" title if lpTitle is not NULL */
951 if (lpTitle)
953 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
954 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
955 strcpyW( ci->frameTitle, lpTitle );
958 if (ci->frameTitle)
960 if (IsZoomed(ci->hwndActiveChild) && IsWindowVisible(ci->hwndActiveChild))
962 /* combine frame title and child title if possible */
964 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
965 static const WCHAR lpBracket2[] = {']',0};
966 int i_frame_text_length = strlenW(ci->frameTitle);
968 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
970 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
972 strcatW( lpBuffer, lpBracket );
973 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
974 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
975 strcatW( lpBuffer, lpBracket2 );
976 else
977 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
980 else
982 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
985 else
986 lpBuffer[0] = '\0';
988 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
992 /* ----------------------------- Interface ---------------------------- */
995 /**********************************************************************
996 * MDIClientWndProc_common
998 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
999 WPARAM wParam, LPARAM lParam, BOOL unicode )
1001 MDICLIENTINFO *ci;
1003 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1005 if (!(ci = get_client_info( hwnd ))) return 0;
1007 switch (message)
1009 case WM_CREATE:
1011 /* Since we are using only cs->lpCreateParams, we can safely
1012 * cast to LPCREATESTRUCTA here */
1013 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1014 WND *wndPtr = WIN_GetPtr( hwnd );
1016 wndPtr->flags |= WIN_ISMDICLIENT;
1018 /* Translation layer doesn't know what's in the cs->lpCreateParams
1019 * so we have to keep track of what environment we're in. */
1021 if( wndPtr->flags & WIN_ISWIN32 )
1023 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1024 ci->hWindowMenu = ccs->hWindowMenu;
1025 ci->idFirstChild = ccs->idFirstChild;
1027 else
1029 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1030 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1031 ci->idFirstChild = ccs->idFirstChild;
1033 WIN_ReleasePtr( wndPtr );
1035 ci->child = NULL;
1036 ci->nActiveChildren = 0;
1037 ci->nTotalCreated = 0;
1038 ci->frameTitle = NULL;
1039 ci->mdiFlags = 0;
1040 ci->hFrameMenu = GetMenu(cs->hwndParent);
1042 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1044 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1045 hwnd, ci->hWindowMenu, ci->idFirstChild );
1046 return 0;
1049 case WM_DESTROY:
1051 if( IsZoomed(ci->hwndActiveChild) )
1052 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndActiveChild);
1054 ci->nActiveChildren = 0;
1055 MDI_RefreshMenu(ci);
1057 HeapFree( GetProcessHeap(), 0, ci->child );
1058 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1060 return 0;
1063 case WM_MDIACTIVATE:
1065 MDI_SwitchActiveChild( ci, (HWND)wParam, TRUE );
1066 return 0;
1069 case WM_MDICASCADE:
1070 return MDICascade(hwnd, ci);
1072 case WM_MDICREATE:
1073 if (lParam)
1075 HWND child;
1077 if (unicode)
1079 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1080 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1081 csW->szTitle, csW->style,
1082 csW->x, csW->y, csW->cx, csW->cy,
1083 hwnd, 0, csW->hOwner,
1084 (LPVOID)csW->lParam);
1086 else
1088 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1089 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1090 csA->szTitle, csA->style,
1091 csA->x, csA->y, csA->cx, csA->cy,
1092 hwnd, 0, csA->hOwner,
1093 (LPVOID)csA->lParam);
1096 if (IsZoomed(ci->hwndActiveChild))
1098 MDI_AugmentFrameMenu(GetParent(hwnd), child);
1099 MDI_UpdateFrameText(GetParent(hwnd), hwnd, NULL);
1101 return (LRESULT)child;
1103 return 0;
1105 case WM_MDIDESTROY:
1106 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1108 case WM_MDIGETACTIVE:
1109 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1110 return (LRESULT)ci->hwndActiveChild;
1112 case WM_MDIICONARRANGE:
1113 ci->mdiFlags |= MDIF_NEEDUPDATE;
1114 ArrangeIconicWindows( hwnd );
1115 ci->sbRecalc = SB_BOTH+1;
1116 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1117 return 0;
1119 case WM_MDIMAXIMIZE:
1120 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1121 return 0;
1123 case WM_MDINEXT: /* lParam != 0 means previous window */
1125 HWND next = MDI_GetWindow( ci, WIN_GetFullHandle( (HWND)wParam ), !lParam, 0 );
1126 MDI_SwitchActiveChild( ci, next, TRUE );
1127 break;
1130 case WM_MDIRESTORE:
1131 SendMessageW( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1132 return 0;
1134 case WM_MDISETMENU:
1135 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1137 case WM_MDIREFRESHMENU:
1138 return MDI_RefreshMenu( ci );
1140 case WM_MDITILE:
1141 ci->mdiFlags |= MDIF_NEEDUPDATE;
1142 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1143 MDITile( hwnd, ci, wParam );
1144 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1145 return 0;
1147 case WM_VSCROLL:
1148 case WM_HSCROLL:
1149 ci->mdiFlags |= MDIF_NEEDUPDATE;
1150 ScrollChildren( hwnd, message, wParam, lParam );
1151 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1152 return 0;
1154 case WM_SETFOCUS:
1155 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1156 SetFocus( ci->hwndActiveChild );
1157 return 0;
1159 case WM_NCACTIVATE:
1160 if( ci->hwndActiveChild )
1161 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1162 break;
1164 case WM_PARENTNOTIFY:
1165 switch (LOWORD(wParam))
1167 case WM_CREATE:
1168 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1170 ci->nTotalCreated++;
1171 ci->nActiveChildren++;
1173 if (!ci->child)
1174 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1175 else
1176 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1178 TRACE("Adding MDI child %p, # of children %d\n",
1179 (HWND)lParam, ci->nActiveChildren);
1181 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1183 break;
1185 case WM_LBUTTONDOWN:
1187 HWND child;
1188 POINT pt;
1189 pt.x = (short)LOWORD(lParam);
1190 pt.y = (short)HIWORD(lParam);
1191 child = ChildWindowFromPoint(hwnd, pt);
1193 TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1195 if( child && child != hwnd && child != ci->hwndActiveChild )
1196 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1197 break;
1200 return 0;
1202 case WM_SIZE:
1203 if( IsWindow(ci->hwndActiveChild) && IsZoomed(ci->hwndActiveChild) &&
1204 (GetWindowLongW(ci->hwndActiveChild, GWL_STYLE) & WS_VISIBLE) )
1206 RECT rect;
1208 rect.left = 0;
1209 rect.top = 0;
1210 rect.right = LOWORD(lParam);
1211 rect.bottom = HIWORD(lParam);
1213 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1214 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1215 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1216 rect.right - rect.left, rect.bottom - rect.top, 1);
1218 else
1219 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1221 break;
1223 case WM_MDICALCCHILDSCROLL:
1224 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1226 CalcChildScroll(hwnd, ci->sbRecalc-1);
1227 ci->sbRecalc = 0;
1228 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1230 return 0;
1232 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1233 DefWindowProcA( hwnd, message, wParam, lParam );
1236 /***********************************************************************
1237 * MDIClientWndProcA
1239 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1241 if (!IsWindow(hwnd)) return 0;
1242 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1245 /***********************************************************************
1246 * MDIClientWndProcW
1248 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1250 if (!IsWindow(hwnd)) return 0;
1251 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1254 /***********************************************************************
1255 * DefFrameProcA (USER32.@)
1257 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1258 UINT message, WPARAM wParam, LPARAM lParam)
1260 if (hwndMDIClient)
1262 switch (message)
1264 case WM_SETTEXT:
1266 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1267 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1268 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1269 MDI_UpdateFrameText( hwnd, hwndMDIClient, text );
1270 HeapFree( GetProcessHeap(), 0, text );
1272 return 1; /* success. FIXME: check text length */
1274 case WM_COMMAND:
1275 case WM_NCACTIVATE:
1276 case WM_NEXTMENU:
1277 case WM_SETFOCUS:
1278 case WM_SIZE:
1279 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1282 return DefWindowProcA(hwnd, message, wParam, lParam);
1286 /***********************************************************************
1287 * DefFrameProcW (USER32.@)
1289 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1290 UINT message, WPARAM wParam, LPARAM lParam)
1292 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1294 TRACE("%p %p %04x (%s) %08x %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1296 if (ci)
1298 switch (message)
1300 case WM_COMMAND:
1302 WORD id = LOWORD(wParam);
1303 /* check for possible syscommands for maximized MDI child */
1304 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1306 if( (id - 0xf000) & 0xf00f ) break;
1307 if( !IsZoomed(ci->hwndActiveChild) ) break;
1308 switch( id )
1310 case SC_SIZE:
1311 case SC_MOVE:
1312 case SC_MINIMIZE:
1313 case SC_MAXIMIZE:
1314 case SC_NEXTWINDOW:
1315 case SC_PREVWINDOW:
1316 case SC_CLOSE:
1317 case SC_RESTORE:
1318 return SendMessageW( ci->hwndActiveChild, WM_SYSCOMMAND,
1319 wParam, lParam);
1322 else
1324 HWND childHwnd;
1325 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1326 /* User chose "More Windows..." */
1327 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1328 else
1329 /* User chose one of the windows listed in the "Windows" menu */
1330 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1332 if( childHwnd )
1333 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1336 break;
1338 case WM_NCACTIVATE:
1339 SendMessageW(hwndMDIClient, message, wParam, lParam);
1340 break;
1342 case WM_SETTEXT:
1343 MDI_UpdateFrameText( hwnd, hwndMDIClient, (LPWSTR)lParam );
1344 return 1; /* success. FIXME: check text length */
1346 case WM_SETFOCUS:
1347 SetFocus(hwndMDIClient);
1348 break;
1350 case WM_SIZE:
1351 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1352 break;
1354 case WM_NEXTMENU:
1356 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1358 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1360 /* control menu is between the frame system menu and
1361 * the first entry of menu bar */
1362 WND *wndPtr = WIN_GetPtr(hwnd);
1364 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1365 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1367 WIN_ReleasePtr(wndPtr);
1368 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1369 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1370 next_menu->hwndNext = ci->hwndActiveChild;
1372 WIN_ReleasePtr(wndPtr);
1374 return 0;
1379 return DefWindowProcW( hwnd, message, wParam, lParam );
1382 /***********************************************************************
1383 * DefMDIChildProcA (USER32.@)
1385 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1386 WPARAM wParam, LPARAM lParam )
1388 HWND client = GetParent(hwnd);
1389 MDICLIENTINFO *ci = get_client_info( client );
1391 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1393 hwnd = WIN_GetFullHandle( hwnd );
1394 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1396 switch (message)
1398 case WM_SETTEXT:
1399 DefWindowProcA(hwnd, message, wParam, lParam);
1400 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1401 MDI_UpdateFrameText( GetParent(client), client, NULL );
1402 return 1; /* success. FIXME: check text length */
1404 case WM_GETMINMAXINFO:
1405 case WM_MENUCHAR:
1406 case WM_CLOSE:
1407 case WM_SETFOCUS:
1408 case WM_CHILDACTIVATE:
1409 case WM_SYSCOMMAND:
1410 case WM_SHOWWINDOW:
1411 case WM_SETVISIBLE:
1412 case WM_SIZE:
1413 case WM_NEXTMENU:
1414 case WM_SYSCHAR:
1415 case WM_DESTROY:
1416 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1418 return DefWindowProcA(hwnd, message, wParam, lParam);
1422 /***********************************************************************
1423 * DefMDIChildProcW (USER32.@)
1425 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1426 WPARAM wParam, LPARAM lParam )
1428 HWND client = GetParent(hwnd);
1429 MDICLIENTINFO *ci = get_client_info( client );
1431 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1433 hwnd = WIN_GetFullHandle( hwnd );
1434 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1436 switch (message)
1438 case WM_SETTEXT:
1439 DefWindowProcW(hwnd, message, wParam, lParam);
1440 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1441 MDI_UpdateFrameText( GetParent(client), client, NULL );
1442 return 1; /* success. FIXME: check text length */
1444 case WM_GETMINMAXINFO:
1445 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1446 return 0;
1448 case WM_MENUCHAR:
1449 return 0x00010000; /* MDI children don't have menu bars */
1451 case WM_CLOSE:
1452 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1453 return 0;
1455 case WM_CHILDACTIVATE:
1456 MDI_ChildActivate( client, hwnd );
1457 return 0;
1459 case WM_SYSCOMMAND:
1460 switch( wParam )
1462 case SC_MOVE:
1463 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1464 return 0;
1465 break;
1466 case SC_RESTORE:
1467 case SC_MINIMIZE:
1468 break;
1469 case SC_MAXIMIZE:
1470 if (ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1471 return SendMessageW( GetParent(client), message, wParam, lParam);
1472 break;
1473 case SC_NEXTWINDOW:
1474 SendMessageW( client, WM_MDINEXT, 0, 0);
1475 return 0;
1476 case SC_PREVWINDOW:
1477 SendMessageW( client, WM_MDINEXT, 0, 1);
1478 return 0;
1480 break;
1482 case WM_SHOWWINDOW:
1483 case WM_SETVISIBLE:
1484 if (IsZoomed(ci->hwndActiveChild)) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1485 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1486 break;
1488 case WM_SIZE:
1489 if( hwnd == ci->hwndActiveChild )
1491 if( wParam == SIZE_MAXIMIZED )
1493 TRACE("maximizing child %p\n", hwnd );
1495 MDI_AugmentFrameMenu( GetParent(client), hwnd );
1497 else
1498 MDI_RestoreFrameMenu( GetParent(client), hwnd );
1501 MDI_UpdateFrameText( GetParent(client), client, NULL );
1502 MDI_RefreshMenu(ci);
1503 MDI_PostUpdate(client, ci, SB_BOTH+1);
1504 break;
1506 case WM_NEXTMENU:
1508 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1509 HWND parent = GetParent(client);
1511 if( wParam == VK_LEFT ) /* switch to frame system menu */
1513 WND *wndPtr = WIN_GetPtr( parent );
1514 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1515 WIN_ReleasePtr( wndPtr );
1517 if( wParam == VK_RIGHT ) /* to frame menu bar */
1519 next_menu->hmenuNext = GetMenu(parent);
1521 next_menu->hwndNext = parent;
1522 return 0;
1525 case WM_SYSCHAR:
1526 if (wParam == '-')
1528 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1529 return 0;
1531 break;
1533 case WM_DESTROY:
1534 /* Remove itself from the Window menu */
1535 MDI_RefreshMenu(ci);
1536 break;
1538 return DefWindowProcW(hwnd, message, wParam, lParam);
1541 /**********************************************************************
1542 * CreateMDIWindowA (USER32.@) Creates a MDI child
1544 * RETURNS
1545 * Success: Handle to created window
1546 * Failure: NULL
1548 HWND WINAPI CreateMDIWindowA(
1549 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1550 LPCSTR lpWindowName, /* [in] Pointer to window name */
1551 DWORD dwStyle, /* [in] Window style */
1552 INT X, /* [in] Horizontal position of window */
1553 INT Y, /* [in] Vertical position of window */
1554 INT nWidth, /* [in] Width of window */
1555 INT nHeight, /* [in] Height of window */
1556 HWND hWndParent, /* [in] Handle to parent window */
1557 HINSTANCE hInstance, /* [in] Handle to application instance */
1558 LPARAM lParam) /* [in] Application-defined value */
1560 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1561 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1562 nWidth,nHeight,hWndParent,hInstance,lParam);
1564 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1565 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1566 0, hInstance, (LPVOID)lParam);
1569 /***********************************************************************
1570 * CreateMDIWindowW (USER32.@) Creates a MDI child
1572 * RETURNS
1573 * Success: Handle to created window
1574 * Failure: NULL
1576 HWND WINAPI CreateMDIWindowW(
1577 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1578 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1579 DWORD dwStyle, /* [in] Window style */
1580 INT X, /* [in] Horizontal position of window */
1581 INT Y, /* [in] Vertical position of window */
1582 INT nWidth, /* [in] Width of window */
1583 INT nHeight, /* [in] Height of window */
1584 HWND hWndParent, /* [in] Handle to parent window */
1585 HINSTANCE hInstance, /* [in] Handle to application instance */
1586 LPARAM lParam) /* [in] Application-defined value */
1588 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1589 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1590 nWidth, nHeight, hWndParent, hInstance, lParam);
1592 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1593 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1594 0, hInstance, (LPVOID)lParam);
1597 /**********************************************************************
1598 * TranslateMDISysAccel (USER32.@)
1600 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1602 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1604 MDICLIENTINFO *ci = get_client_info( hwndClient );
1605 WPARAM wParam = 0;
1607 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1609 /* translate if the Ctrl key is down and Alt not. */
1611 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1613 switch( msg->wParam )
1615 case VK_F6:
1616 case VK_TAB:
1617 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1618 break;
1619 case VK_F4:
1620 case VK_RBUTTON:
1621 wParam = SC_CLOSE;
1622 break;
1623 default:
1624 return 0;
1626 TRACE("wParam = %04x\n", wParam);
1627 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1628 return 1;
1631 return 0; /* failure */
1634 /***********************************************************************
1635 * CalcChildScroll (USER32.@)
1637 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1639 SCROLLINFO info;
1640 RECT childRect, clientRect;
1641 HWND *list;
1643 GetClientRect( hwnd, &clientRect );
1644 SetRectEmpty( &childRect );
1646 if ((list = WIN_ListChildren( hwnd )))
1648 int i;
1649 for (i = 0; list[i]; i++)
1651 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1652 if (style & WS_MAXIMIZE)
1654 HeapFree( GetProcessHeap(), 0, list );
1655 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1656 return;
1658 if (style & WS_VISIBLE)
1660 RECT rect;
1661 GetWindowRect( list[i], &rect );
1662 UnionRect( &childRect, &rect, &childRect );
1665 HeapFree( GetProcessHeap(), 0, list );
1667 MapWindowPoints( 0, hwnd, (POINT *)&childRect, 2 );
1668 UnionRect( &childRect, &clientRect, &childRect );
1670 /* set common info values */
1671 info.cbSize = sizeof(info);
1672 info.fMask = SIF_POS | SIF_RANGE;
1674 /* set the specific */
1675 switch( scroll )
1677 case SB_BOTH:
1678 case SB_HORZ:
1679 info.nMin = childRect.left;
1680 info.nMax = childRect.right - clientRect.right;
1681 info.nPos = clientRect.left - childRect.left;
1682 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1683 if (scroll == SB_HORZ) break;
1684 /* fall through */
1685 case SB_VERT:
1686 info.nMin = childRect.top;
1687 info.nMax = childRect.bottom - clientRect.bottom;
1688 info.nPos = clientRect.top - childRect.top;
1689 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1690 break;
1695 /***********************************************************************
1696 * ScrollChildren (USER32.@)
1698 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1699 LPARAM lParam)
1701 INT newPos = -1;
1702 INT curPos, length, minPos, maxPos, shift;
1703 RECT rect;
1705 GetClientRect( hWnd, &rect );
1707 switch(uMsg)
1709 case WM_HSCROLL:
1710 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1711 curPos = GetScrollPos(hWnd,SB_HORZ);
1712 length = (rect.right - rect.left) / 2;
1713 shift = GetSystemMetrics(SM_CYHSCROLL);
1714 break;
1715 case WM_VSCROLL:
1716 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1717 curPos = GetScrollPos(hWnd,SB_VERT);
1718 length = (rect.bottom - rect.top) / 2;
1719 shift = GetSystemMetrics(SM_CXVSCROLL);
1720 break;
1721 default:
1722 return;
1725 switch( wParam )
1727 case SB_LINEUP:
1728 newPos = curPos - shift;
1729 break;
1730 case SB_LINEDOWN:
1731 newPos = curPos + shift;
1732 break;
1733 case SB_PAGEUP:
1734 newPos = curPos - length;
1735 break;
1736 case SB_PAGEDOWN:
1737 newPos = curPos + length;
1738 break;
1740 case SB_THUMBPOSITION:
1741 newPos = LOWORD(lParam);
1742 break;
1744 case SB_THUMBTRACK:
1745 return;
1747 case SB_TOP:
1748 newPos = minPos;
1749 break;
1750 case SB_BOTTOM:
1751 newPos = maxPos;
1752 break;
1753 case SB_ENDSCROLL:
1754 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1755 return;
1758 if( newPos > maxPos )
1759 newPos = maxPos;
1760 else
1761 if( newPos < minPos )
1762 newPos = minPos;
1764 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1766 if( uMsg == WM_VSCROLL )
1767 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1768 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1769 else
1770 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1771 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1775 /******************************************************************************
1776 * CascadeWindows (USER32.@) Cascades MDI child windows
1778 * RETURNS
1779 * Success: Number of cascaded windows.
1780 * Failure: 0
1782 WORD WINAPI
1783 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1784 UINT cKids, const HWND *lpKids)
1786 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1787 return 0;
1791 /******************************************************************************
1792 * TileWindows (USER32.@) Tiles MDI child windows
1794 * RETURNS
1795 * Success: Number of tiled windows.
1796 * Failure: 0
1798 WORD WINAPI
1799 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1800 UINT cKids, const HWND *lpKids)
1802 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1803 return 0;
1806 /************************************************************************
1807 * "More Windows..." functionality
1810 /* MDI_MoreWindowsDlgProc
1812 * This function will process the messages sent to the "More Windows..."
1813 * dialog.
1814 * Return values: 0 = cancel pressed
1815 * HWND = ok pressed or double-click in the list...
1819 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1821 switch (iMsg)
1823 case WM_INITDIALOG:
1825 UINT widest = 0;
1826 UINT length;
1827 UINT i;
1828 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1829 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1831 for (i = 0; i < ci->nActiveChildren; i++)
1833 WCHAR buffer[MDI_MAXTITLELENGTH];
1835 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1836 continue;
1837 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1838 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1839 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1840 if (length > widest)
1841 widest = length;
1843 /* Make sure the horizontal scrollbar scrolls ok */
1844 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1846 /* Set the current selection */
1847 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1848 return TRUE;
1851 case WM_COMMAND:
1852 switch (LOWORD(wParam))
1854 default:
1855 if (HIWORD(wParam) != LBN_DBLCLK) break;
1856 /* fall through */
1857 case IDOK:
1859 /* windows are sorted by menu ID, so we must return the
1860 * window associated to the given id
1862 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1863 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1864 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1865 EndDialog(hDlg, res);
1866 return TRUE;
1868 case IDCANCEL:
1869 EndDialog(hDlg, 0);
1870 return TRUE;
1872 break;
1874 return FALSE;
1879 * MDI_MoreWindowsDialog
1881 * Prompts the user with a listbox containing the opened
1882 * documents. The user can then choose a windows and click
1883 * on OK to set the current window to the one selected, or
1884 * CANCEL to cancel. The function returns a handle to the
1885 * selected window.
1888 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1890 LPCVOID template;
1891 HRSRC hRes;
1892 HANDLE hDlgTmpl;
1894 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1896 if (hRes == 0)
1897 return 0;
1899 hDlgTmpl = LoadResource(user32_module, hRes );
1901 if (hDlgTmpl == 0)
1902 return 0;
1904 template = LockResource( hDlgTmpl );
1906 if (template == 0)
1907 return 0;
1909 return (HWND) DialogBoxIndirectParamA(user32_module,
1910 (const DLGTEMPLATE*) template,
1911 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);