user: Win64 printf format warning fixes.
[wine/wine-kai.git] / dlls / user / mdi.c
blobbfeb2db64ec36c36d5dbe9019d7c5a0beb9ab920
1 /* MDI.C
3 * Copyright 1994, Bob Amstadt
4 * 1995,1996 Alex Korobka
6 * This file contains routines to support MDI (Multiple Document
7 * Interface) features .
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Notes: Fairly complete implementation.
24 * Also, Excel and WinWord do _not_ use MDI so if you're trying
25 * to fix them look elsewhere.
27 * Notes on how the "More Windows..." is implemented:
29 * When we have more than 9 opened windows, a "More Windows..."
30 * option appears in the "Windows" menu. Each child window has
31 * a WND* associated with it, 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 "user_private.h"
99 #include "wine/debug.h"
100 #include "dlgs.h"
102 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
104 #define MDI_MAXTITLELENGTH 0xa1
106 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
108 /* "More Windows..." definitions */
109 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
110 option will appear under the Windows menu */
111 #define MDI_IDC_LISTBOX 100
112 #define IDS_MDI_MOREWINDOWS 13
114 #define MDIF_NEEDUPDATE 0x0001
116 typedef struct
118 UINT nActiveChildren;
119 HWND hwndActiveChild;
120 HWND *child; /* array of tracked children */
121 HMENU hFrameMenu;
122 HMENU hWindowMenu;
123 UINT idFirstChild;
124 LPWSTR frameTitle;
125 UINT nTotalCreated;
126 UINT mdiFlags;
127 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
128 } MDICLIENTINFO;
130 static HBITMAP hBmpClose = 0;
132 /* ----------------- declarations ----------------- */
133 static void MDI_UpdateFrameText( HWND, HWND, LPCWSTR);
134 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
135 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
136 static LONG MDI_ChildActivate( HWND, HWND );
137 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
139 static HWND MDI_MoreWindowsDialog(HWND);
140 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
141 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
143 /* -------- Miscellaneous service functions ----------
145 * MDI_GetChildByID
147 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
149 int i;
151 for (i = 0; ci->nActiveChildren; i++)
153 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
154 return ci->child[i];
156 return 0;
159 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
161 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
163 ci->mdiFlags |= MDIF_NEEDUPDATE;
164 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
166 ci->sbRecalc = recalc;
170 /*********************************************************************
171 * MDIClient class descriptor
173 const struct builtin_class_descr MDICLIENT_builtin_class =
175 "MDIClient", /* name */
176 0, /* style */
177 MDIClientWndProcA, /* procA */
178 MDIClientWndProcW, /* procW */
179 sizeof(MDICLIENTINFO), /* extra */
180 IDC_ARROW, /* cursor */
181 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
185 static MDICLIENTINFO *get_client_info( HWND client )
187 MDICLIENTINFO *ret = NULL;
188 WND *win = WIN_GetPtr( client );
189 if (win)
191 if (win == WND_OTHER_PROCESS)
193 if (IsWindow(client)) ERR( "client %p belongs to other process\n", client );
194 return NULL;
196 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
197 else ret = (MDICLIENTINFO *)win->wExtra;
198 WIN_ReleasePtr( win );
200 return ret;
203 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
205 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
207 if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
208 if (hSysMenu)
210 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
211 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
212 return FALSE;
214 return TRUE;
217 /**********************************************************************
218 * MDI_GetWindow
220 * returns "activateable" child different from the current or zero
222 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
223 DWORD dwStyleMask )
225 int i;
226 HWND *list;
227 HWND last = 0;
229 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
230 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
232 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
233 i = 0;
234 /* start from next after hWnd */
235 while (list[i] && list[i] != hWnd) i++;
236 if (list[i]) i++;
238 for ( ; list[i]; i++)
240 if (GetWindow( list[i], GW_OWNER )) continue;
241 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
242 last = list[i];
243 if (bNext) goto found;
245 /* now restart from the beginning */
246 for (i = 0; list[i] && list[i] != hWnd; i++)
248 if (GetWindow( list[i], GW_OWNER )) continue;
249 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
250 last = list[i];
251 if (bNext) goto found;
253 found:
254 HeapFree( GetProcessHeap(), 0, list );
255 return last;
258 /**********************************************************************
259 * MDI_CalcDefaultChildPos
261 * It seems that the default height is about 2/3 of the client rect
263 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
265 INT nstagger;
266 RECT rect;
267 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
269 if (total < 0) /* we are called from CreateWindow */
271 MDICLIENTINFO *ci = get_client_info(hwndClient);
272 total = ci ? ci->nTotalCreated : 0;
273 *id = ci->idFirstChild + ci->nActiveChildren;
274 TRACE("MDI child id %04x\n", *id);
277 GetClientRect( hwndClient, &rect );
278 if( rect.bottom - rect.top - delta >= spacing )
279 rect.bottom -= delta;
281 nstagger = (rect.bottom - rect.top)/(3 * spacing);
282 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
283 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
284 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
287 /**********************************************************************
288 * MDISetMenu
290 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
291 HMENU hmenuWindow)
293 MDICLIENTINFO *ci;
294 HWND hwndFrame = GetParent(hwnd);
296 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
298 if (hmenuFrame && !IsMenu(hmenuFrame))
300 WARN("hmenuFrame is not a menu handle\n");
301 return 0L;
304 if (hmenuWindow && !IsMenu(hmenuWindow))
306 WARN("hmenuWindow is not a menu handle\n");
307 return 0L;
310 if (!(ci = get_client_info( hwnd ))) return 0;
312 if (hmenuFrame)
314 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
316 if (IsZoomed(ci->hwndActiveChild))
317 MDI_RestoreFrameMenu( hwndFrame, ci->hwndActiveChild );
320 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
322 /* delete menu items from ci->hWindowMenu
323 * and add them to hmenuWindow */
324 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
325 if( ci->hWindowMenu && ci->nActiveChildren )
327 UINT nActiveChildren_old = ci->nActiveChildren;
329 /* Remove all items from old Window menu */
330 ci->nActiveChildren = 0;
331 MDI_RefreshMenu(ci);
333 ci->hWindowMenu = hmenuWindow;
335 /* Add items to the new Window menu */
336 ci->nActiveChildren = nActiveChildren_old;
337 MDI_RefreshMenu(ci);
339 else
340 ci->hWindowMenu = hmenuWindow;
343 if (hmenuFrame)
345 SetMenu(hwndFrame, hmenuFrame);
346 if( hmenuFrame != ci->hFrameMenu )
348 HMENU oldFrameMenu = ci->hFrameMenu;
350 ci->hFrameMenu = hmenuFrame;
351 if (IsZoomed(ci->hwndActiveChild) && (GetWindowLongW(ci->hwndActiveChild, GWL_STYLE) & WS_VISIBLE))
352 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
354 return (LRESULT)oldFrameMenu;
357 else
359 /* SetMenu() may already have been called, meaning that this window
360 * already has its menu. But they may have done a SetMenu() on
361 * an MDI window, and called MDISetMenu() after the fact, meaning
362 * that the "if" to this "else" wouldn't catch the need to
363 * augment the frame menu.
365 if( IsZoomed(ci->hwndActiveChild) )
366 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
369 return 0;
372 /**********************************************************************
373 * MDIRefreshMenu
375 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
377 UINT i, count, visible, id;
378 WCHAR buf[MDI_MAXTITLELENGTH];
380 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
382 if (!ci->hWindowMenu)
383 return 0;
385 if (!IsMenu(ci->hWindowMenu))
387 WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
388 return 0;
391 /* Windows finds the last separator in the menu, and if after it
392 * there is a menu item with MDI magic ID removes all existing
393 * menu items after it, and then adds visible MDI children.
395 count = GetMenuItemCount(ci->hWindowMenu);
396 for (i = 0; i < count; i++)
398 MENUITEMINFOW mii;
400 memset(&mii, 0, sizeof(mii));
401 mii.cbSize = sizeof(mii);
402 mii.fMask = MIIM_TYPE;
403 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
405 if (mii.fType & MF_SEPARATOR)
407 /* Windows checks only ID of the menu item */
408 memset(&mii, 0, sizeof(mii));
409 mii.cbSize = sizeof(mii);
410 mii.fMask = MIIM_ID;
411 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
413 if (mii.wID == ci->idFirstChild)
415 TRACE("removing %u items including separator\n", count - i);
416 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
417 /* nothing */;
419 break;
426 visible = 0;
427 for (i = 0; i < ci->nActiveChildren; i++)
429 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
431 id = ci->idFirstChild + visible;
433 if (visible == MDI_MOREWINDOWSLIMIT)
435 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
436 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
437 break;
440 if (!visible)
441 /* Visio expects that separator has id 0 */
442 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
444 visible++;
446 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
448 buf[0] = '&';
449 buf[1] = '0' + visible;
450 buf[2] = ' ';
451 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
452 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
453 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
455 if (ci->child[i] == ci->hwndActiveChild)
456 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
458 else
459 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
462 return (LRESULT)ci->hFrameMenu;
466 /* ------------------ MDI child window functions ---------------------- */
468 /**********************************************************************
469 * MDI_ChildGetMinMaxInfo
471 * Note: The rule here is that client rect of the maximized MDI child
472 * is equal to the client rect of the MDI client window.
474 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
476 RECT rect;
478 GetClientRect( client, &rect );
479 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
480 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
482 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
483 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
485 lpMinMax->ptMaxPosition.x = rect.left;
486 lpMinMax->ptMaxPosition.y = rect.top;
488 TRACE("max rect (%d,%d - %d, %d)\n",
489 rect.left,rect.top,rect.right,rect.bottom);
492 /**********************************************************************
493 * MDI_SwitchActiveChild
495 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
496 * being activated
498 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
500 HWND hwndPrev;
502 hwndPrev = ci->hwndActiveChild;
504 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
506 if ( hwndTo != hwndPrev )
508 BOOL was_zoomed = IsZoomed(hwndPrev);
510 if (was_zoomed)
512 /* restore old MDI child */
513 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
514 ShowWindow( hwndPrev, SW_RESTORE );
515 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
517 /* activate new MDI child */
518 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
519 /* maximize new MDI child */
520 ShowWindow( hwndTo, SW_MAXIMIZE );
522 /* activate new MDI child */
523 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
528 /**********************************************************************
529 * MDIDestroyChild
531 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
532 HWND child, BOOL flagDestroy )
534 UINT i;
536 TRACE("# of managed children %u\n", ci->nActiveChildren);
538 if( child == ci->hwndActiveChild )
540 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
541 if (next)
542 MDI_SwitchActiveChild(ci, next, TRUE);
543 else
545 ShowWindow(child, SW_HIDE);
546 if (IsZoomed(child))
548 MDI_RestoreFrameMenu(GetParent(client), child);
549 MDI_UpdateFrameText(GetParent(client), client, NULL);
551 MDI_ChildActivate(client, 0);
555 for (i = 0; i < ci->nActiveChildren; i++)
557 if (ci->child[i] == child)
559 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
560 memcpy(new_child, ci->child, i * sizeof(HWND));
561 if (i + 1 < ci->nActiveChildren)
562 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
563 HeapFree(GetProcessHeap(), 0, ci->child);
564 ci->child = new_child;
566 ci->nActiveChildren--;
567 break;
571 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
573 if (flagDestroy)
575 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
576 DestroyWindow(child);
579 TRACE("child destroyed - %p\n", child);
580 return 0;
584 /**********************************************************************
585 * MDI_ChildActivate
587 * Called in response to WM_CHILDACTIVATE, or when last MDI child
588 * is being deactivated.
590 static LONG MDI_ChildActivate( HWND client, HWND child )
592 MDICLIENTINFO *clientInfo;
593 HWND prevActiveWnd, frame;
594 BOOL isActiveFrameWnd;
596 clientInfo = get_client_info( client );
598 if (clientInfo->hwndActiveChild == child) return 0;
600 TRACE("%p\n", child);
602 frame = GetParent(client);
603 isActiveFrameWnd = (GetActiveWindow() == frame);
604 prevActiveWnd = clientInfo->hwndActiveChild;
606 /* deactivate prev. active child */
607 if(prevActiveWnd)
609 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
610 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
613 MDI_SwitchActiveChild( clientInfo, child, FALSE );
614 clientInfo->hwndActiveChild = child;
616 MDI_RefreshMenu(clientInfo);
618 if( isActiveFrameWnd )
620 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
621 SetFocus( client );
624 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
625 return TRUE;
628 /* -------------------- MDI client window functions ------------------- */
630 /**********************************************************************
631 * CreateMDIMenuBitmap
633 static HBITMAP CreateMDIMenuBitmap(void)
635 HDC hDCSrc = CreateCompatibleDC(0);
636 HDC hDCDest = CreateCompatibleDC(hDCSrc);
637 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
638 HBITMAP hbCopy;
639 HBITMAP hobjSrc, hobjDest;
641 hobjSrc = SelectObject(hDCSrc, hbClose);
642 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
643 hobjDest = SelectObject(hDCDest, hbCopy);
645 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
646 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
648 SelectObject(hDCSrc, hobjSrc);
649 DeleteObject(hbClose);
650 DeleteDC(hDCSrc);
652 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
654 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
655 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
657 SelectObject(hDCDest, hobjSrc );
658 SelectObject(hDCDest, hobjDest);
659 DeleteDC(hDCDest);
661 return hbCopy;
664 /**********************************************************************
665 * MDICascade
667 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
669 HWND *win_array;
670 BOOL has_icons = FALSE;
671 int i, total;
673 if (IsZoomed(ci->hwndActiveChild))
674 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
676 if (ci->nActiveChildren == 0) return 0;
678 if (!(win_array = WIN_ListChildren( client ))) return 0;
680 /* remove all the windows we don't want */
681 for (i = total = 0; win_array[i]; i++)
683 if (!IsWindowVisible( win_array[i] )) continue;
684 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
685 if (IsIconic( win_array[i] ))
687 has_icons = TRUE;
688 continue;
690 win_array[total++] = win_array[i];
692 win_array[total] = 0;
694 if (total)
696 INT delta = 0, n = 0, i;
697 POINT pos[2];
698 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
700 /* walk the list (backwards) and move windows */
701 for (i = total - 1; i >= 0; i--)
703 TRACE("move %p to (%d,%d) size [%d,%d]\n",
704 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
706 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
707 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
708 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
711 HeapFree( GetProcessHeap(), 0, win_array );
713 if (has_icons) ArrangeIconicWindows( client );
714 return 0;
717 /**********************************************************************
718 * MDITile
720 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
722 HWND *win_array;
723 int i, total;
724 BOOL has_icons = FALSE;
726 if (IsZoomed(ci->hwndActiveChild))
727 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
729 if (ci->nActiveChildren == 0) return;
731 if (!(win_array = WIN_ListChildren( client ))) return;
733 /* remove all the windows we don't want */
734 for (i = total = 0; win_array[i]; i++)
736 if (!IsWindowVisible( win_array[i] )) continue;
737 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
738 if (IsIconic( win_array[i] ))
740 has_icons = TRUE;
741 continue;
743 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
744 win_array[total++] = win_array[i];
746 win_array[total] = 0;
748 TRACE("%u windows to tile\n", total);
750 if (total)
752 HWND *pWnd = win_array;
753 RECT rect;
754 int x, y, xsize, ysize;
755 int rows, columns, r, c, i;
757 GetClientRect(client,&rect);
758 rows = (int) sqrt((double)total);
759 columns = total / rows;
761 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
763 i = rows;
764 rows = columns; /* exchange r and c */
765 columns = i;
768 if (has_icons)
770 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
771 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
774 ysize = rect.bottom / rows;
775 xsize = rect.right / columns;
777 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
779 if (c == columns)
781 rows = total - i;
782 ysize = rect.bottom / rows;
785 y = 0;
786 for (r = 1; r <= rows && *pWnd; r++, i++)
788 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
789 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
790 y += ysize;
791 pWnd++;
793 x += xsize;
796 HeapFree( GetProcessHeap(), 0, win_array );
797 if (has_icons) ArrangeIconicWindows( client );
800 /* ----------------------- Frame window ---------------------------- */
803 /**********************************************************************
804 * MDI_AugmentFrameMenu
806 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
808 HMENU menu = GetMenu( frame );
809 HMENU hSysPopup = 0;
810 HBITMAP hSysMenuBitmap = 0;
811 INT nItems;
812 UINT iId;
813 HICON hIcon;
815 TRACE("frame %p,child %p\n",frame,hChild);
817 if( !menu ) return 0;
819 /* if the system buttons already exist do not add them again */
820 nItems = GetMenuItemCount(menu) - 1;
821 iId = GetMenuItemID(menu,nItems) ;
822 if (iId == SC_RESTORE || iId == SC_CLOSE)
823 return 0;
825 /* create a copy of sysmenu popup and insert it into frame menu bar */
826 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
827 return 0;
829 AppendMenuW(menu, MF_HELP | MF_BITMAP,
830 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
831 AppendMenuW(menu, MF_HELP | MF_BITMAP,
832 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
833 AppendMenuW(menu, MF_HELP | MF_BITMAP,
834 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
835 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
837 /* The system menu is replaced by the child icon */
838 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICONSM);
839 if (!hIcon)
840 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICON);
841 if (!hIcon)
842 hIcon = LoadImageW(0, MAKEINTRESOURCEW(IDI_WINLOGO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
843 if (hIcon)
845 HDC hMemDC;
846 HBITMAP hBitmap, hOldBitmap;
847 HBRUSH hBrush;
848 HDC hdc = GetDC(hChild);
850 if (hdc)
852 int cx, cy;
853 cx = GetSystemMetrics(SM_CXSMICON);
854 cy = GetSystemMetrics(SM_CYSMICON);
855 hMemDC = CreateCompatibleDC(hdc);
856 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
857 hOldBitmap = SelectObject(hMemDC, hBitmap);
858 SetMapMode(hMemDC, MM_TEXT);
859 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
860 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
861 SelectObject (hMemDC, hOldBitmap);
862 DeleteObject(hBrush);
863 DeleteDC(hMemDC);
864 ReleaseDC(hChild, hdc);
865 hSysMenuBitmap = hBitmap;
869 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
870 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
872 TRACE("not inserted\n");
873 DestroyMenu(hSysPopup);
874 return 0;
877 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
878 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
879 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
880 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
882 /* redraw menu */
883 DrawMenuBar(frame);
885 return 1;
888 /**********************************************************************
889 * MDI_RestoreFrameMenu
891 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
893 MENUITEMINFOW menuInfo;
894 HMENU menu = GetMenu( frame );
895 INT nItems = GetMenuItemCount(menu) - 1;
896 UINT iId = GetMenuItemID(menu,nItems) ;
898 TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
900 if( !menu ) return 0;
902 /* if there is no system buttons then nothing to do */
903 if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
904 return 0;
907 * Remove the system menu, If that menu is the icon of the window
908 * as it is in win95, we have to delete the bitmap.
910 memset(&menuInfo, 0, sizeof(menuInfo));
911 menuInfo.cbSize = sizeof(menuInfo);
912 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
914 GetMenuItemInfoW(menu,
916 TRUE,
917 &menuInfo);
919 RemoveMenu(menu,0,MF_BYPOSITION);
921 if ( (menuInfo.fType & MFT_BITMAP) &&
922 (LOWORD(menuInfo.dwTypeData)!=0) &&
923 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
925 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
928 /* close */
929 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
930 /* restore */
931 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
932 /* minimize */
933 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
935 DrawMenuBar(frame);
937 return 1;
941 /**********************************************************************
942 * MDI_UpdateFrameText
944 * used when child window is maximized/restored
946 * Note: lpTitle can be NULL
948 static void MDI_UpdateFrameText( HWND frame, HWND hClient, LPCWSTR lpTitle )
950 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
951 MDICLIENTINFO *ci = get_client_info( hClient );
953 TRACE("frameText %s\n", debugstr_w(lpTitle));
955 if (!ci) return;
957 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
959 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
960 lpTitle = lpBuffer;
963 /* store new "default" title if lpTitle is not NULL */
964 if (lpTitle)
966 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
967 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
968 strcpyW( ci->frameTitle, lpTitle );
971 if (ci->frameTitle)
973 if (IsZoomed(ci->hwndActiveChild) && IsWindowVisible(ci->hwndActiveChild))
975 /* combine frame title and child title if possible */
977 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
978 static const WCHAR lpBracket2[] = {']',0};
979 int i_frame_text_length = strlenW(ci->frameTitle);
981 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
983 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
985 strcatW( lpBuffer, lpBracket );
986 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
987 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
988 strcatW( lpBuffer, lpBracket2 );
989 else
990 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
993 else
995 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
998 else
999 lpBuffer[0] = '\0';
1001 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1005 /* ----------------------------- Interface ---------------------------- */
1008 /**********************************************************************
1009 * MDIClientWndProc_common
1011 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1012 WPARAM wParam, LPARAM lParam, BOOL unicode )
1014 MDICLIENTINFO *ci;
1016 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1018 if (!(ci = get_client_info( hwnd ))) return 0;
1020 switch (message)
1022 case WM_CREATE:
1024 /* Since we are using only cs->lpCreateParams, we can safely
1025 * cast to LPCREATESTRUCTA here */
1026 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1027 WND *wndPtr = WIN_GetPtr( hwnd );
1029 wndPtr->flags |= WIN_ISMDICLIENT;
1031 /* Translation layer doesn't know what's in the cs->lpCreateParams
1032 * so we have to keep track of what environment we're in. */
1034 if( wndPtr->flags & WIN_ISWIN32 )
1036 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1037 ci->hWindowMenu = ccs->hWindowMenu;
1038 ci->idFirstChild = ccs->idFirstChild;
1040 else
1042 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1043 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1044 ci->idFirstChild = ccs->idFirstChild;
1046 WIN_ReleasePtr( wndPtr );
1048 ci->child = NULL;
1049 ci->nActiveChildren = 0;
1050 ci->nTotalCreated = 0;
1051 ci->frameTitle = NULL;
1052 ci->mdiFlags = 0;
1053 ci->hFrameMenu = GetMenu(cs->hwndParent);
1055 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1057 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1058 hwnd, ci->hWindowMenu, ci->idFirstChild );
1059 return 0;
1062 case WM_DESTROY:
1064 if( IsZoomed(ci->hwndActiveChild) )
1065 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndActiveChild);
1067 ci->nActiveChildren = 0;
1068 MDI_RefreshMenu(ci);
1070 HeapFree( GetProcessHeap(), 0, ci->child );
1071 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1073 return 0;
1076 case WM_MDIACTIVATE:
1078 MDI_SwitchActiveChild( ci, (HWND)wParam, TRUE );
1079 return 0;
1082 case WM_MDICASCADE:
1083 return MDICascade(hwnd, ci);
1085 case WM_MDICREATE:
1086 if (lParam)
1088 HWND child;
1090 if (unicode)
1092 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1093 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1094 csW->szTitle, csW->style,
1095 csW->x, csW->y, csW->cx, csW->cy,
1096 hwnd, 0, csW->hOwner,
1097 (LPVOID)csW->lParam);
1099 else
1101 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1102 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1103 csA->szTitle, csA->style,
1104 csA->x, csA->y, csA->cx, csA->cy,
1105 hwnd, 0, csA->hOwner,
1106 (LPVOID)csA->lParam);
1109 if (IsZoomed(ci->hwndActiveChild))
1111 MDI_AugmentFrameMenu(GetParent(hwnd), child);
1112 MDI_UpdateFrameText(GetParent(hwnd), hwnd, NULL);
1114 return (LRESULT)child;
1116 return 0;
1118 case WM_MDIDESTROY:
1119 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1121 case WM_MDIGETACTIVE:
1122 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1123 return (LRESULT)ci->hwndActiveChild;
1125 case WM_MDIICONARRANGE:
1126 ci->mdiFlags |= MDIF_NEEDUPDATE;
1127 ArrangeIconicWindows( hwnd );
1128 ci->sbRecalc = SB_BOTH+1;
1129 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1130 return 0;
1132 case WM_MDIMAXIMIZE:
1133 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1134 return 0;
1136 case WM_MDINEXT: /* lParam != 0 means previous window */
1138 HWND next = MDI_GetWindow( ci, WIN_GetFullHandle( (HWND)wParam ), !lParam, 0 );
1139 MDI_SwitchActiveChild( ci, next, TRUE );
1140 break;
1143 case WM_MDIRESTORE:
1144 SendMessageW( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1145 return 0;
1147 case WM_MDISETMENU:
1148 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1150 case WM_MDIREFRESHMENU:
1151 return MDI_RefreshMenu( ci );
1153 case WM_MDITILE:
1154 ci->mdiFlags |= MDIF_NEEDUPDATE;
1155 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1156 MDITile( hwnd, ci, wParam );
1157 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1158 return 0;
1160 case WM_VSCROLL:
1161 case WM_HSCROLL:
1162 ci->mdiFlags |= MDIF_NEEDUPDATE;
1163 ScrollChildren( hwnd, message, wParam, lParam );
1164 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1165 return 0;
1167 case WM_SETFOCUS:
1168 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1169 SetFocus( ci->hwndActiveChild );
1170 return 0;
1172 case WM_NCACTIVATE:
1173 if( ci->hwndActiveChild )
1174 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1175 break;
1177 case WM_PARENTNOTIFY:
1178 switch (LOWORD(wParam))
1180 case WM_CREATE:
1181 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1183 ci->nTotalCreated++;
1184 ci->nActiveChildren++;
1186 if (!ci->child)
1187 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1188 else
1189 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1191 TRACE("Adding MDI child %p, # of children %d\n",
1192 (HWND)lParam, ci->nActiveChildren);
1194 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1196 break;
1198 case WM_LBUTTONDOWN:
1200 HWND child;
1201 POINT pt;
1202 pt.x = (short)LOWORD(lParam);
1203 pt.y = (short)HIWORD(lParam);
1204 child = ChildWindowFromPoint(hwnd, pt);
1206 TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1208 if( child && child != hwnd && child != ci->hwndActiveChild )
1209 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1210 break;
1213 return 0;
1215 case WM_SIZE:
1216 if( IsWindow(ci->hwndActiveChild) && IsZoomed(ci->hwndActiveChild) &&
1217 (GetWindowLongW(ci->hwndActiveChild, GWL_STYLE) & WS_VISIBLE) )
1219 RECT rect;
1221 rect.left = 0;
1222 rect.top = 0;
1223 rect.right = LOWORD(lParam);
1224 rect.bottom = HIWORD(lParam);
1226 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1227 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1228 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1229 rect.right - rect.left, rect.bottom - rect.top, 1);
1231 else
1232 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1234 break;
1236 case WM_MDICALCCHILDSCROLL:
1237 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1239 CalcChildScroll(hwnd, ci->sbRecalc-1);
1240 ci->sbRecalc = 0;
1241 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1243 return 0;
1245 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1246 DefWindowProcA( hwnd, message, wParam, lParam );
1249 /***********************************************************************
1250 * MDIClientWndProcA
1252 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1254 if (!IsWindow(hwnd)) return 0;
1255 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1258 /***********************************************************************
1259 * MDIClientWndProcW
1261 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1263 if (!IsWindow(hwnd)) return 0;
1264 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1267 /***********************************************************************
1268 * DefFrameProcA (USER32.@)
1270 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1271 UINT message, WPARAM wParam, LPARAM lParam)
1273 if (hwndMDIClient)
1275 switch (message)
1277 case WM_SETTEXT:
1279 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1280 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1281 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1282 MDI_UpdateFrameText( hwnd, hwndMDIClient, text );
1283 HeapFree( GetProcessHeap(), 0, text );
1285 return 1; /* success. FIXME: check text length */
1287 case WM_COMMAND:
1288 case WM_NCACTIVATE:
1289 case WM_NEXTMENU:
1290 case WM_SETFOCUS:
1291 case WM_SIZE:
1292 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1295 return DefWindowProcA(hwnd, message, wParam, lParam);
1299 /***********************************************************************
1300 * DefFrameProcW (USER32.@)
1302 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1303 UINT message, WPARAM wParam, LPARAM lParam)
1305 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1307 TRACE("%p %p %04x (%s) %08x %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1309 if (ci)
1311 switch (message)
1313 case WM_COMMAND:
1315 WORD id = LOWORD(wParam);
1316 /* check for possible syscommands for maximized MDI child */
1317 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1319 if( (id - 0xf000) & 0xf00f ) break;
1320 if( !IsZoomed(ci->hwndActiveChild) ) break;
1321 switch( id )
1323 case SC_CLOSE:
1324 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1325 case SC_SIZE:
1326 case SC_MOVE:
1327 case SC_MINIMIZE:
1328 case SC_MAXIMIZE:
1329 case SC_NEXTWINDOW:
1330 case SC_PREVWINDOW:
1331 case SC_RESTORE:
1332 return SendMessageW( ci->hwndActiveChild, WM_SYSCOMMAND,
1333 wParam, lParam);
1336 else
1338 HWND childHwnd;
1339 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1340 /* User chose "More Windows..." */
1341 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1342 else
1343 /* User chose one of the windows listed in the "Windows" menu */
1344 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1346 if( childHwnd )
1347 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1350 break;
1352 case WM_NCACTIVATE:
1353 SendMessageW(hwndMDIClient, message, wParam, lParam);
1354 break;
1356 case WM_SETTEXT:
1357 MDI_UpdateFrameText( hwnd, hwndMDIClient, (LPWSTR)lParam );
1358 return 1; /* success. FIXME: check text length */
1360 case WM_SETFOCUS:
1361 SetFocus(hwndMDIClient);
1362 break;
1364 case WM_SIZE:
1365 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1366 break;
1368 case WM_NEXTMENU:
1370 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1372 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1374 /* control menu is between the frame system menu and
1375 * the first entry of menu bar */
1376 WND *wndPtr = WIN_GetPtr(hwnd);
1378 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1379 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1381 WIN_ReleasePtr(wndPtr);
1382 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1383 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1384 next_menu->hwndNext = ci->hwndActiveChild;
1386 WIN_ReleasePtr(wndPtr);
1388 return 0;
1393 return DefWindowProcW( hwnd, message, wParam, lParam );
1396 /***********************************************************************
1397 * DefMDIChildProcA (USER32.@)
1399 LRESULT WINAPI DefMDIChildProcA( 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 DefWindowProcA( hwnd, message, wParam, lParam );
1410 switch (message)
1412 case WM_SETTEXT:
1413 DefWindowProcA(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 case WM_MENUCHAR:
1420 case WM_CLOSE:
1421 case WM_SETFOCUS:
1422 case WM_CHILDACTIVATE:
1423 case WM_SYSCOMMAND:
1424 case WM_SHOWWINDOW:
1425 case WM_SETVISIBLE:
1426 case WM_SIZE:
1427 case WM_NEXTMENU:
1428 case WM_SYSCHAR:
1429 case WM_DESTROY:
1430 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1432 return DefWindowProcA(hwnd, message, wParam, lParam);
1436 /***********************************************************************
1437 * DefMDIChildProcW (USER32.@)
1439 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1440 WPARAM wParam, LPARAM lParam )
1442 HWND client = GetParent(hwnd);
1443 MDICLIENTINFO *ci = get_client_info( client );
1445 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1447 hwnd = WIN_GetFullHandle( hwnd );
1448 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1450 switch (message)
1452 case WM_SETTEXT:
1453 DefWindowProcW(hwnd, message, wParam, lParam);
1454 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1455 MDI_UpdateFrameText( GetParent(client), client, NULL );
1456 return 1; /* success. FIXME: check text length */
1458 case WM_GETMINMAXINFO:
1459 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1460 return 0;
1462 case WM_MENUCHAR:
1463 return 0x00010000; /* MDI children don't have menu bars */
1465 case WM_CLOSE:
1466 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1467 return 0;
1469 case WM_CHILDACTIVATE:
1470 MDI_ChildActivate( client, hwnd );
1471 return 0;
1473 case WM_SYSCOMMAND:
1474 switch( wParam )
1476 case SC_MOVE:
1477 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1478 return 0;
1479 break;
1480 case SC_RESTORE:
1481 case SC_MINIMIZE:
1482 break;
1483 case SC_MAXIMIZE:
1484 if (ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1485 return SendMessageW( GetParent(client), message, wParam, lParam);
1486 break;
1487 case SC_NEXTWINDOW:
1488 SendMessageW( client, WM_MDINEXT, 0, 0);
1489 return 0;
1490 case SC_PREVWINDOW:
1491 SendMessageW( client, WM_MDINEXT, 0, 1);
1492 return 0;
1494 break;
1496 case WM_SHOWWINDOW:
1497 case WM_SETVISIBLE:
1498 if (IsZoomed(ci->hwndActiveChild)) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1499 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1500 break;
1502 case WM_SIZE:
1503 if( hwnd == ci->hwndActiveChild )
1505 if( wParam == SIZE_MAXIMIZED )
1507 TRACE("maximizing child %p\n", hwnd );
1509 MDI_AugmentFrameMenu( GetParent(client), hwnd );
1511 else
1512 MDI_RestoreFrameMenu( GetParent(client), hwnd );
1515 MDI_UpdateFrameText( GetParent(client), client, NULL );
1516 MDI_RefreshMenu(ci);
1517 MDI_PostUpdate(client, ci, SB_BOTH+1);
1518 break;
1520 case WM_NEXTMENU:
1522 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1523 HWND parent = GetParent(client);
1525 if( wParam == VK_LEFT ) /* switch to frame system menu */
1527 WND *wndPtr = WIN_GetPtr( parent );
1528 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1529 WIN_ReleasePtr( wndPtr );
1531 if( wParam == VK_RIGHT ) /* to frame menu bar */
1533 next_menu->hmenuNext = GetMenu(parent);
1535 next_menu->hwndNext = parent;
1536 return 0;
1539 case WM_SYSCHAR:
1540 if (wParam == '-')
1542 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1543 return 0;
1545 break;
1547 case WM_DESTROY:
1548 /* Remove itself from the Window menu */
1549 MDI_RefreshMenu(ci);
1550 break;
1552 return DefWindowProcW(hwnd, message, wParam, lParam);
1555 /**********************************************************************
1556 * CreateMDIWindowA (USER32.@) Creates a MDI child
1558 * RETURNS
1559 * Success: Handle to created window
1560 * Failure: NULL
1562 HWND WINAPI CreateMDIWindowA(
1563 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1564 LPCSTR lpWindowName, /* [in] Pointer to window name */
1565 DWORD dwStyle, /* [in] Window style */
1566 INT X, /* [in] Horizontal position of window */
1567 INT Y, /* [in] Vertical position of window */
1568 INT nWidth, /* [in] Width of window */
1569 INT nHeight, /* [in] Height of window */
1570 HWND hWndParent, /* [in] Handle to parent window */
1571 HINSTANCE hInstance, /* [in] Handle to application instance */
1572 LPARAM lParam) /* [in] Application-defined value */
1574 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1575 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1576 nWidth,nHeight,hWndParent,hInstance,lParam);
1578 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1579 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1580 0, hInstance, (LPVOID)lParam);
1583 /***********************************************************************
1584 * CreateMDIWindowW (USER32.@) Creates a MDI child
1586 * RETURNS
1587 * Success: Handle to created window
1588 * Failure: NULL
1590 HWND WINAPI CreateMDIWindowW(
1591 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1592 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1593 DWORD dwStyle, /* [in] Window style */
1594 INT X, /* [in] Horizontal position of window */
1595 INT Y, /* [in] Vertical position of window */
1596 INT nWidth, /* [in] Width of window */
1597 INT nHeight, /* [in] Height of window */
1598 HWND hWndParent, /* [in] Handle to parent window */
1599 HINSTANCE hInstance, /* [in] Handle to application instance */
1600 LPARAM lParam) /* [in] Application-defined value */
1602 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1603 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1604 nWidth, nHeight, hWndParent, hInstance, lParam);
1606 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1607 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1608 0, hInstance, (LPVOID)lParam);
1611 /**********************************************************************
1612 * TranslateMDISysAccel (USER32.@)
1614 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1616 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1618 MDICLIENTINFO *ci = get_client_info( hwndClient );
1619 WPARAM wParam = 0;
1621 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1623 /* translate if the Ctrl key is down and Alt not. */
1625 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1627 switch( msg->wParam )
1629 case VK_F6:
1630 case VK_TAB:
1631 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1632 break;
1633 case VK_F4:
1634 case VK_RBUTTON:
1635 if (is_close_enabled(ci->hwndActiveChild, 0))
1637 wParam = SC_CLOSE;
1638 break;
1640 /* fall through */
1641 default:
1642 return 0;
1644 TRACE("wParam = %04x\n", wParam);
1645 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1646 return 1;
1649 return 0; /* failure */
1652 /***********************************************************************
1653 * CalcChildScroll (USER32.@)
1655 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1657 SCROLLINFO info;
1658 RECT childRect, clientRect;
1659 HWND *list;
1661 GetClientRect( hwnd, &clientRect );
1662 SetRectEmpty( &childRect );
1664 if ((list = WIN_ListChildren( hwnd )))
1666 int i;
1667 for (i = 0; list[i]; i++)
1669 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1670 if (style & WS_MAXIMIZE)
1672 HeapFree( GetProcessHeap(), 0, list );
1673 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1674 return;
1676 if (style & WS_VISIBLE)
1678 RECT rect;
1679 GetWindowRect( list[i], &rect );
1680 UnionRect( &childRect, &rect, &childRect );
1683 HeapFree( GetProcessHeap(), 0, list );
1685 MapWindowPoints( 0, hwnd, (POINT *)&childRect, 2 );
1686 UnionRect( &childRect, &clientRect, &childRect );
1688 /* set common info values */
1689 info.cbSize = sizeof(info);
1690 info.fMask = SIF_POS | SIF_RANGE;
1692 /* set the specific */
1693 switch( scroll )
1695 case SB_BOTH:
1696 case SB_HORZ:
1697 info.nMin = childRect.left;
1698 info.nMax = childRect.right - clientRect.right;
1699 info.nPos = clientRect.left - childRect.left;
1700 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1701 if (scroll == SB_HORZ) break;
1702 /* fall through */
1703 case SB_VERT:
1704 info.nMin = childRect.top;
1705 info.nMax = childRect.bottom - clientRect.bottom;
1706 info.nPos = clientRect.top - childRect.top;
1707 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1708 break;
1713 /***********************************************************************
1714 * ScrollChildren (USER32.@)
1716 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1717 LPARAM lParam)
1719 INT newPos = -1;
1720 INT curPos, length, minPos, maxPos, shift;
1721 RECT rect;
1723 GetClientRect( hWnd, &rect );
1725 switch(uMsg)
1727 case WM_HSCROLL:
1728 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1729 curPos = GetScrollPos(hWnd,SB_HORZ);
1730 length = (rect.right - rect.left) / 2;
1731 shift = GetSystemMetrics(SM_CYHSCROLL);
1732 break;
1733 case WM_VSCROLL:
1734 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1735 curPos = GetScrollPos(hWnd,SB_VERT);
1736 length = (rect.bottom - rect.top) / 2;
1737 shift = GetSystemMetrics(SM_CXVSCROLL);
1738 break;
1739 default:
1740 return;
1743 switch( wParam )
1745 case SB_LINEUP:
1746 newPos = curPos - shift;
1747 break;
1748 case SB_LINEDOWN:
1749 newPos = curPos + shift;
1750 break;
1751 case SB_PAGEUP:
1752 newPos = curPos - length;
1753 break;
1754 case SB_PAGEDOWN:
1755 newPos = curPos + length;
1756 break;
1758 case SB_THUMBPOSITION:
1759 newPos = LOWORD(lParam);
1760 break;
1762 case SB_THUMBTRACK:
1763 return;
1765 case SB_TOP:
1766 newPos = minPos;
1767 break;
1768 case SB_BOTTOM:
1769 newPos = maxPos;
1770 break;
1771 case SB_ENDSCROLL:
1772 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1773 return;
1776 if( newPos > maxPos )
1777 newPos = maxPos;
1778 else
1779 if( newPos < minPos )
1780 newPos = minPos;
1782 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1784 if( uMsg == WM_VSCROLL )
1785 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1786 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1787 else
1788 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1789 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1793 /******************************************************************************
1794 * CascadeWindows (USER32.@) Cascades MDI child windows
1796 * RETURNS
1797 * Success: Number of cascaded windows.
1798 * Failure: 0
1800 WORD WINAPI
1801 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1802 UINT cKids, const HWND *lpKids)
1804 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1805 return 0;
1809 /******************************************************************************
1810 * TileWindows (USER32.@) Tiles MDI child windows
1812 * RETURNS
1813 * Success: Number of tiled windows.
1814 * Failure: 0
1816 WORD WINAPI
1817 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1818 UINT cKids, const HWND *lpKids)
1820 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1821 return 0;
1824 /************************************************************************
1825 * "More Windows..." functionality
1828 /* MDI_MoreWindowsDlgProc
1830 * This function will process the messages sent to the "More Windows..."
1831 * dialog.
1832 * Return values: 0 = cancel pressed
1833 * HWND = ok pressed or double-click in the list...
1837 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1839 switch (iMsg)
1841 case WM_INITDIALOG:
1843 UINT widest = 0;
1844 UINT length;
1845 UINT i;
1846 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1847 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1849 for (i = 0; i < ci->nActiveChildren; i++)
1851 WCHAR buffer[MDI_MAXTITLELENGTH];
1853 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1854 continue;
1855 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1856 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1857 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1858 if (length > widest)
1859 widest = length;
1861 /* Make sure the horizontal scrollbar scrolls ok */
1862 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1864 /* Set the current selection */
1865 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1866 return TRUE;
1869 case WM_COMMAND:
1870 switch (LOWORD(wParam))
1872 default:
1873 if (HIWORD(wParam) != LBN_DBLCLK) break;
1874 /* fall through */
1875 case IDOK:
1877 /* windows are sorted by menu ID, so we must return the
1878 * window associated to the given id
1880 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1881 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1882 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1883 EndDialog(hDlg, res);
1884 return TRUE;
1886 case IDCANCEL:
1887 EndDialog(hDlg, 0);
1888 return TRUE;
1890 break;
1892 return FALSE;
1897 * MDI_MoreWindowsDialog
1899 * Prompts the user with a listbox containing the opened
1900 * documents. The user can then choose a windows and click
1901 * on OK to set the current window to the one selected, or
1902 * CANCEL to cancel. The function returns a handle to the
1903 * selected window.
1906 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1908 LPCVOID template;
1909 HRSRC hRes;
1910 HANDLE hDlgTmpl;
1912 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1914 if (hRes == 0)
1915 return 0;
1917 hDlgTmpl = LoadResource(user32_module, hRes );
1919 if (hDlgTmpl == 0)
1920 return 0;
1922 template = LockResource( hDlgTmpl );
1924 if (template == 0)
1925 return 0;
1927 return (HWND) DialogBoxIndirectParamA(user32_module,
1928 (const DLGTEMPLATE*) template,
1929 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);