dwrite: Store pair kerning range attribute.
[wine.git] / dlls / user32 / mdi.c
blob11d56797d2bfe0d844b8c0998456bb658502d331
1 /* MDI.C
3 * Copyright 1994, Bob Amstadt
4 * 1995,1996 Alex Korobka
6 * This file contains routines to support MDI (Multiple Document
7 * Interface) features .
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Notes: Fairly complete implementation.
24 * Also, Excel and WinWord do _not_ use MDI so if you're trying
25 * to fix them look elsewhere.
27 * Notes on how the "More Windows..." is implemented:
29 * When we have more than 9 opened windows, a "More Windows..."
30 * option appears in the "Windows" menu. Each child window has
31 * a WND* associated with it, accessible via the children list of
32 * the parent window. This WND* has a wIDmenu member, which reflects
33 * the position of the child in the window list. For example, with
34 * 9 child windows, we could have the following pattern:
38 * Name of the child window pWndChild->wIDmenu
39 * Doc1 5000
40 * Doc2 5001
41 * Doc3 5002
42 * Doc4 5003
43 * Doc5 5004
44 * Doc6 5005
45 * Doc7 5006
46 * Doc8 5007
47 * Doc9 5008
50 * The "Windows" menu, as the "More windows..." dialog, are constructed
51 * in this order. If we add a child, we would have the following list:
54 * Name of the child window pWndChild->wIDmenu
55 * Doc1 5000
56 * Doc2 5001
57 * Doc3 5002
58 * Doc4 5003
59 * Doc5 5004
60 * Doc6 5005
61 * Doc7 5006
62 * Doc8 5007
63 * Doc9 5008
64 * Doc10 5009
66 * But only 5000 to 5008 would be displayed in the "Windows" menu. We want
67 * the last created child to be in the menu, so we swap the last child with
68 * the 9th... Doc9 will be accessible via the "More Windows..." option.
70 * Doc1 5000
71 * Doc2 5001
72 * Doc3 5002
73 * Doc4 5003
74 * Doc5 5004
75 * Doc6 5005
76 * Doc7 5006
77 * Doc8 5007
78 * Doc9 5009
79 * Doc10 5008
83 #include <stdlib.h>
84 #include <stdarg.h>
85 #include <stdio.h>
86 #include <string.h>
87 #include <math.h>
89 #define OEMRESOURCE
91 #include "windef.h"
92 #include "winbase.h"
93 #include "wingdi.h"
94 #include "winuser.h"
95 #include "wownt32.h"
96 #include "wine/unicode.h"
97 #include "win.h"
98 #include "controls.h"
99 #include "user_private.h"
100 #include "wine/debug.h"
102 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
104 #define MDI_MAXTITLELENGTH 0xa1
106 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
108 /* "More Windows..." definitions */
109 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
110 option will appear under the Windows menu */
111 #define MDI_IDC_LISTBOX 100
112 #define IDS_MDI_MOREWINDOWS 13
114 #define MDIF_NEEDUPDATE 0x0001
116 typedef struct
118 /* At some points, particularly when switching MDI children, active and
119 * maximized MDI children may be not the same window, so we need to track
120 * them separately.
121 * The only place where we switch to/from maximized state is DefMDIChildProc
122 * WM_SIZE/SIZE_MAXIMIZED handler. We get that notification only after the
123 * ShowWindow(SW_SHOWMAXIMIZED) request, therefore window is guaranteed to
124 * be visible at the time we get the notification, and it's safe to assume
125 * that hwndChildMaximized is always visible.
126 * If the app plays games with WS_VISIBLE, WS_MAXIMIZE or any other window
127 * states it must keep coherency with USER32 on its own. This is true for
128 * Windows as well.
130 UINT nActiveChildren;
131 HWND hwndChildMaximized;
132 HWND hwndActiveChild;
133 HWND *child; /* array of tracked children */
134 HMENU hFrameMenu;
135 HMENU hWindowMenu;
136 UINT idFirstChild;
137 LPWSTR frameTitle;
138 UINT nTotalCreated;
139 UINT mdiFlags;
140 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
141 } MDICLIENTINFO;
143 static HBITMAP hBmpClose = 0;
145 /* ----------------- declarations ----------------- */
146 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
147 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
148 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
149 static LONG MDI_ChildActivate( HWND, HWND );
150 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
152 static HWND MDI_MoreWindowsDialog(HWND);
154 /* -------- Miscellaneous service functions ----------
156 * MDI_GetChildByID
158 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
160 int i;
162 for (i = 0; ci->nActiveChildren; i++)
164 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
165 return ci->child[i];
167 return 0;
170 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
172 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
174 ci->mdiFlags |= MDIF_NEEDUPDATE;
175 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
177 ci->sbRecalc = recalc;
181 /*********************************************************************
182 * MDIClient class descriptor
184 static const WCHAR mdiclientW[] = {'M','D','I','C','l','i','e','n','t',0};
185 const struct builtin_class_descr MDICLIENT_builtin_class =
187 mdiclientW, /* name */
188 0, /* style */
189 WINPROC_MDICLIENT, /* proc */
190 sizeof(MDICLIENTINFO), /* extra */
191 IDC_ARROW, /* cursor */
192 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
196 static MDICLIENTINFO *get_client_info( HWND client )
198 MDICLIENTINFO *ret = NULL;
199 WND *win = WIN_GetPtr( client );
200 if (win)
202 if (win == WND_OTHER_PROCESS || win == WND_DESKTOP)
204 if (IsWindow(client)) WARN( "client %p belongs to other process\n", client );
205 return NULL;
207 if (win->flags & WIN_ISMDICLIENT)
208 ret = (MDICLIENTINFO *)win->wExtra;
209 else
210 WARN( "%p is not an MDI client\n", client );
211 WIN_ReleasePtr( win );
213 return ret;
216 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
218 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
220 if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
221 if (hSysMenu)
223 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
224 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
225 return FALSE;
227 return TRUE;
230 /**********************************************************************
231 * MDI_GetWindow
233 * returns "activatable" child different from the current or zero
235 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
236 DWORD dwStyleMask )
238 int i;
239 HWND *list;
240 HWND last = 0;
242 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
243 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
245 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
246 i = 0;
247 /* start from next after hWnd */
248 while (list[i] && list[i] != hWnd) i++;
249 if (list[i]) i++;
251 for ( ; list[i]; i++)
253 if (GetWindow( list[i], GW_OWNER )) continue;
254 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
255 last = list[i];
256 if (bNext) goto found;
258 /* now restart from the beginning */
259 for (i = 0; list[i] && list[i] != hWnd; i++)
261 if (GetWindow( list[i], GW_OWNER )) continue;
262 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
263 last = list[i];
264 if (bNext) goto found;
266 found:
267 HeapFree( GetProcessHeap(), 0, list );
268 return last;
271 /**********************************************************************
272 * MDI_CalcDefaultChildPos
274 * It seems that the default height is about 2/3 of the client rect
276 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
278 INT nstagger;
279 RECT rect;
280 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
282 if (total < 0) /* we are called from CreateWindow */
284 MDICLIENTINFO *ci = get_client_info(hwndClient);
285 total = ci->nTotalCreated;
286 *id = ci->idFirstChild + ci->nActiveChildren;
287 TRACE("MDI child id %04x\n", *id);
290 GetClientRect( hwndClient, &rect );
291 if( rect.bottom - rect.top - delta >= spacing )
292 rect.bottom -= delta;
294 nstagger = (rect.bottom - rect.top)/(3 * spacing);
295 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
296 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
297 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
300 /**********************************************************************
301 * MDISetMenu
303 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
304 HMENU hmenuWindow)
306 MDICLIENTINFO *ci;
307 HWND hwndFrame = GetParent(hwnd);
309 TRACE("%p, frame menu %p, window menu %p\n", hwnd, hmenuFrame, hmenuWindow);
311 if (hmenuFrame && !IsMenu(hmenuFrame))
313 WARN("hmenuFrame is not a menu handle\n");
314 return 0L;
317 if (hmenuWindow && !IsMenu(hmenuWindow))
319 WARN("hmenuWindow is not a menu handle\n");
320 return 0L;
323 if (!(ci = get_client_info( hwnd ))) return 0;
325 TRACE("old frame menu %p, old window menu %p\n", ci->hFrameMenu, ci->hWindowMenu);
327 if (hmenuFrame)
329 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
331 if (ci->hwndChildMaximized)
332 MDI_RestoreFrameMenu( hwndFrame, ci->hwndChildMaximized );
335 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
337 /* delete menu items from ci->hWindowMenu
338 * and add them to hmenuWindow */
339 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
340 if( ci->hWindowMenu && ci->nActiveChildren )
342 UINT nActiveChildren_old = ci->nActiveChildren;
344 /* Remove all items from old Window menu */
345 ci->nActiveChildren = 0;
346 MDI_RefreshMenu(ci);
348 ci->hWindowMenu = hmenuWindow;
350 /* Add items to the new Window menu */
351 ci->nActiveChildren = nActiveChildren_old;
352 MDI_RefreshMenu(ci);
354 else
355 ci->hWindowMenu = hmenuWindow;
358 if (hmenuFrame)
360 SetMenu(hwndFrame, hmenuFrame);
361 if( hmenuFrame != ci->hFrameMenu )
363 HMENU oldFrameMenu = ci->hFrameMenu;
365 ci->hFrameMenu = hmenuFrame;
366 if (ci->hwndChildMaximized)
367 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
369 return (LRESULT)oldFrameMenu;
372 else
374 /* SetMenu() may already have been called, meaning that this window
375 * already has its menu. But they may have done a SetMenu() on
376 * an MDI window, and called MDISetMenu() after the fact, meaning
377 * that the "if" to this "else" wouldn't catch the need to
378 * augment the frame menu.
380 if( ci->hwndChildMaximized )
381 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
384 return 0;
387 /**********************************************************************
388 * MDIRefreshMenu
390 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
392 UINT i, count, visible, id;
393 WCHAR buf[MDI_MAXTITLELENGTH];
395 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
397 if (!ci->hWindowMenu)
398 return 0;
400 if (!IsMenu(ci->hWindowMenu))
402 WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
403 return 0;
406 /* Windows finds the last separator in the menu, and if after it
407 * there is a menu item with MDI magic ID removes all existing
408 * menu items after it, and then adds visible MDI children.
410 count = GetMenuItemCount(ci->hWindowMenu);
411 for (i = 0; i < count; i++)
413 MENUITEMINFOW mii;
415 memset(&mii, 0, sizeof(mii));
416 mii.cbSize = sizeof(mii);
417 mii.fMask = MIIM_TYPE;
418 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
420 if (mii.fType & MF_SEPARATOR)
422 /* Windows checks only ID of the menu item */
423 memset(&mii, 0, sizeof(mii));
424 mii.cbSize = sizeof(mii);
425 mii.fMask = MIIM_ID;
426 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
428 if (mii.wID == ci->idFirstChild)
430 TRACE("removing %u items including separator\n", count - i);
431 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
432 /* nothing */;
434 break;
441 visible = 0;
442 for (i = 0; i < ci->nActiveChildren; i++)
444 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
446 id = ci->idFirstChild + visible;
448 if (visible == MDI_MOREWINDOWSLIMIT)
450 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
451 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
452 break;
455 if (!visible)
456 /* Visio expects that separator has id 0 */
457 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
459 visible++;
461 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
463 buf[0] = '&';
464 buf[1] = '0' + visible;
465 buf[2] = ' ';
466 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
467 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
468 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
470 if (ci->child[i] == ci->hwndActiveChild)
471 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
473 else
474 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
477 return (LRESULT)ci->hFrameMenu;
481 /* ------------------ MDI child window functions ---------------------- */
483 /**********************************************************************
484 * MDI_ChildGetMinMaxInfo
486 * Note: The rule here is that client rect of the maximized MDI child
487 * is equal to the client rect of the MDI client window.
489 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
491 RECT rect;
493 GetClientRect( client, &rect );
494 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
495 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
497 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
498 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
500 lpMinMax->ptMaxPosition.x = rect.left;
501 lpMinMax->ptMaxPosition.y = rect.top;
503 TRACE("max rect (%d,%d - %d, %d)\n",
504 rect.left,rect.top,rect.right,rect.bottom);
507 /**********************************************************************
508 * MDI_SwitchActiveChild
510 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
511 * being activated
513 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
515 HWND hwndPrev;
517 hwndPrev = ci->hwndActiveChild;
519 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
521 if ( hwndTo != hwndPrev )
523 BOOL was_zoomed = IsZoomed(hwndPrev);
525 if (was_zoomed)
527 /* restore old MDI child */
528 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
529 ShowWindow( hwndPrev, SW_RESTORE );
530 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
532 /* activate new MDI child */
533 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
534 /* maximize new MDI child */
535 ShowWindow( hwndTo, SW_MAXIMIZE );
537 /* activate new MDI child */
538 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
543 /**********************************************************************
544 * MDIDestroyChild
546 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
547 HWND child, BOOL flagDestroy )
549 UINT i;
551 TRACE("# of managed children %u\n", ci->nActiveChildren);
553 if( child == ci->hwndActiveChild )
555 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
556 /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
557 if (flagDestroy && next)
558 MDI_SwitchActiveChild(ci, next, TRUE);
559 else
561 ShowWindow(child, SW_HIDE);
562 if (child == ci->hwndChildMaximized)
564 HWND frame = GetParent(client);
565 MDI_RestoreFrameMenu(frame, child);
566 ci->hwndChildMaximized = 0;
567 MDI_UpdateFrameText(frame, client, TRUE, NULL);
569 if (flagDestroy)
570 MDI_ChildActivate(client, 0);
574 for (i = 0; i < ci->nActiveChildren; i++)
576 if (ci->child[i] == child)
578 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
579 memcpy(new_child, ci->child, i * sizeof(HWND));
580 if (i + 1 < ci->nActiveChildren)
581 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
582 HeapFree(GetProcessHeap(), 0, ci->child);
583 ci->child = new_child;
585 ci->nActiveChildren--;
586 break;
590 if (flagDestroy)
592 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
593 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
594 DestroyWindow(child);
597 TRACE("child destroyed - %p\n", child);
598 return 0;
602 /**********************************************************************
603 * MDI_ChildActivate
605 * Called in response to WM_CHILDACTIVATE, or when last MDI child
606 * is being deactivated.
608 static LONG MDI_ChildActivate( HWND client, HWND child )
610 MDICLIENTINFO *clientInfo;
611 HWND prevActiveWnd, frame;
612 BOOL isActiveFrameWnd;
614 clientInfo = get_client_info( client );
616 if (clientInfo->hwndActiveChild == child) return 0;
618 TRACE("%p\n", child);
620 frame = GetParent(client);
621 isActiveFrameWnd = (GetActiveWindow() == frame);
622 prevActiveWnd = clientInfo->hwndActiveChild;
624 /* deactivate prev. active child */
625 if(prevActiveWnd)
627 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
628 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
631 MDI_SwitchActiveChild( clientInfo, child, FALSE );
632 clientInfo->hwndActiveChild = child;
634 MDI_RefreshMenu(clientInfo);
636 if( isActiveFrameWnd )
638 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
639 /* Let the client window manage focus for children, but if the focus
640 * is already on the client (for instance this is the 1st child) then
641 * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
642 * manually in this case.
644 if (SetFocus(client) == client)
645 SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
648 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
649 return TRUE;
652 /* -------------------- MDI client window functions ------------------- */
654 /**********************************************************************
655 * CreateMDIMenuBitmap
657 static HBITMAP CreateMDIMenuBitmap(void)
659 HDC hDCSrc = CreateCompatibleDC(0);
660 HDC hDCDest = CreateCompatibleDC(hDCSrc);
661 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
662 HBITMAP hbCopy;
663 HBITMAP hobjSrc, hobjDest;
665 hobjSrc = SelectObject(hDCSrc, hbClose);
666 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
667 hobjDest = SelectObject(hDCDest, hbCopy);
669 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
670 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
672 SelectObject(hDCSrc, hobjSrc);
673 DeleteObject(hbClose);
674 DeleteDC(hDCSrc);
676 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
678 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
679 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
681 SelectObject(hDCDest, hobjSrc );
682 SelectObject(hDCDest, hobjDest);
683 DeleteDC(hDCDest);
685 return hbCopy;
688 /**********************************************************************
689 * MDICascade
691 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
693 HWND *win_array;
694 BOOL has_icons = FALSE;
695 int i, total;
697 if (ci->hwndChildMaximized)
698 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
700 if (ci->nActiveChildren == 0) return 0;
702 if (!(win_array = WIN_ListChildren( client ))) return 0;
704 /* remove all the windows we don't want */
705 for (i = total = 0; win_array[i]; i++)
707 if (!IsWindowVisible( win_array[i] )) continue;
708 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
709 if (IsIconic( win_array[i] ))
711 has_icons = TRUE;
712 continue;
714 win_array[total++] = win_array[i];
716 win_array[total] = 0;
718 if (total)
720 INT delta = 0, n = 0, i;
721 POINT pos[2];
722 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
724 /* walk the list (backwards) and move windows */
725 for (i = total - 1; i >= 0; i--)
727 LONG style;
728 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
730 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
731 TRACE("move %p to (%d,%d) size [%d,%d]\n",
732 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
733 style = GetWindowLongW(win_array[i], GWL_STYLE);
735 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
736 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
737 posOptions);
740 HeapFree( GetProcessHeap(), 0, win_array );
742 if (has_icons) ArrangeIconicWindows( client );
743 return 0;
746 /**********************************************************************
747 * MDITile
749 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
751 HWND *win_array;
752 int i, total;
753 BOOL has_icons = FALSE;
755 if (ci->hwndChildMaximized)
756 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
758 if (ci->nActiveChildren == 0) return;
760 if (!(win_array = WIN_ListChildren( client ))) return;
762 /* remove all the windows we don't want */
763 for (i = total = 0; win_array[i]; i++)
765 if (!IsWindowVisible( win_array[i] )) continue;
766 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
767 if (IsIconic( win_array[i] ))
769 has_icons = TRUE;
770 continue;
772 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
773 win_array[total++] = win_array[i];
775 win_array[total] = 0;
777 TRACE("%u windows to tile\n", total);
779 if (total)
781 HWND *pWnd = win_array;
782 RECT rect;
783 int x, y, xsize, ysize;
784 int rows, columns, r, c, i;
786 GetClientRect(client,&rect);
787 rows = (int) sqrt((double)total);
788 columns = total / rows;
790 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
792 i = rows;
793 rows = columns; /* exchange r and c */
794 columns = i;
797 if (has_icons)
799 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
800 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
803 ysize = rect.bottom / rows;
804 xsize = rect.right / columns;
806 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
808 if (c == columns)
810 rows = total - i;
811 ysize = rect.bottom / rows;
814 y = 0;
815 for (r = 1; r <= rows && *pWnd; r++, i++)
817 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
818 LONG style = GetWindowLongW(win_array[i], GWL_STYLE);
819 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
821 SetWindowPos(*pWnd, 0, x, y, xsize, ysize, posOptions);
822 y += ysize;
823 pWnd++;
825 x += xsize;
828 HeapFree( GetProcessHeap(), 0, win_array );
829 if (has_icons) ArrangeIconicWindows( client );
832 /* ----------------------- Frame window ---------------------------- */
835 /**********************************************************************
836 * MDI_AugmentFrameMenu
838 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
840 HMENU menu = GetMenu( frame );
841 HMENU hSysPopup = 0;
842 HBITMAP hSysMenuBitmap = 0;
843 HICON hIcon;
845 TRACE("frame %p,child %p\n",frame,hChild);
847 if (!menu) return FALSE;
849 /* create a copy of sysmenu popup and insert it into frame menu bar */
850 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
852 TRACE("child %p doesn't have a system menu\n", hChild);
853 return FALSE;
856 AppendMenuW(menu, MF_HELP | MF_BITMAP,
857 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
858 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
859 AppendMenuW(menu, MF_HELP | MF_BITMAP,
860 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
861 AppendMenuW(menu, MF_HELP | MF_BITMAP,
862 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
864 /* The system menu is replaced by the child icon */
865 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_SMALL, 0);
866 if (!hIcon)
867 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_BIG, 0);
868 if (!hIcon)
869 hIcon = LoadImageW(0, (LPWSTR)IDI_WINLOGO, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
870 GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
871 if (hIcon)
873 HDC hMemDC;
874 HBITMAP hBitmap, hOldBitmap;
875 HBRUSH hBrush;
876 HDC hdc = GetDC(hChild);
878 if (hdc)
880 int cx, cy;
881 cx = GetSystemMetrics(SM_CXSMICON);
882 cy = GetSystemMetrics(SM_CYSMICON);
883 hMemDC = CreateCompatibleDC(hdc);
884 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
885 hOldBitmap = SelectObject(hMemDC, hBitmap);
886 SetMapMode(hMemDC, MM_TEXT);
887 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
888 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
889 SelectObject (hMemDC, hOldBitmap);
890 DeleteObject(hBrush);
891 DeleteDC(hMemDC);
892 ReleaseDC(hChild, hdc);
893 hSysMenuBitmap = hBitmap;
897 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
898 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
900 TRACE("not inserted\n");
901 DestroyMenu(hSysPopup);
902 return FALSE;
905 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
906 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
907 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
908 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
910 /* redraw menu */
911 DrawMenuBar(frame);
913 return TRUE;
916 /**********************************************************************
917 * MDI_RestoreFrameMenu
919 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
921 MENUITEMINFOW menuInfo;
922 HMENU menu = GetMenu( frame );
923 INT nItems;
924 UINT iId;
926 TRACE("frame %p, child %p\n", frame, hChild);
928 if (!menu) return FALSE;
930 /* if there is no system buttons then nothing to do */
931 nItems = GetMenuItemCount(menu) - 1;
932 iId = GetMenuItemID(menu, nItems);
933 if ( !(iId == SC_RESTORE || iId == SC_CLOSE) )
934 return FALSE;
937 * Remove the system menu, If that menu is the icon of the window
938 * as it is in win95, we have to delete the bitmap.
940 memset(&menuInfo, 0, sizeof(menuInfo));
941 menuInfo.cbSize = sizeof(menuInfo);
942 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
944 GetMenuItemInfoW(menu,
946 TRUE,
947 &menuInfo);
949 RemoveMenu(menu,0,MF_BYPOSITION);
951 if ( (menuInfo.fType & MFT_BITMAP) &&
952 (LOWORD(menuInfo.dwTypeData)!=0) &&
953 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
955 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
958 /* close */
959 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
960 /* restore */
961 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
962 /* minimize */
963 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
965 DrawMenuBar(frame);
967 return TRUE;
971 /**********************************************************************
972 * MDI_UpdateFrameText
974 * used when child window is maximized/restored
976 * Note: lpTitle can be NULL
978 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
980 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
981 MDICLIENTINFO *ci = get_client_info( hClient );
983 TRACE("frameText %s\n", debugstr_w(lpTitle));
985 if (!ci) return;
987 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
989 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
990 lpTitle = lpBuffer;
993 /* store new "default" title if lpTitle is not NULL */
994 if (lpTitle)
996 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
997 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
998 strcpyW( ci->frameTitle, lpTitle );
1001 if (ci->frameTitle)
1003 if (ci->hwndChildMaximized)
1005 /* combine frame title and child title if possible */
1007 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
1008 static const WCHAR lpBracket2[] = {']',0};
1009 int i_frame_text_length = strlenW(ci->frameTitle);
1011 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1013 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1015 strcatW( lpBuffer, lpBracket );
1016 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
1017 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1018 strcatW( lpBuffer, lpBracket2 );
1019 else
1020 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1023 else
1025 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1028 else
1029 lpBuffer[0] = '\0';
1031 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1033 if (repaint)
1034 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1035 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1039 /* ----------------------------- Interface ---------------------------- */
1042 /**********************************************************************
1043 * MDIClientWndProc_common
1045 LRESULT MDIClientWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
1047 MDICLIENTINFO *ci;
1049 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1051 if (!(ci = get_client_info( hwnd )))
1053 if (message == WM_NCCREATE)
1055 WND *wndPtr = WIN_GetPtr( hwnd );
1056 wndPtr->flags |= WIN_ISMDICLIENT;
1057 WIN_ReleasePtr( wndPtr );
1059 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1060 DefWindowProcA( hwnd, message, wParam, lParam );
1063 switch (message)
1065 case WM_CREATE:
1067 /* Since we are using only cs->lpCreateParams, we can safely
1068 * cast to LPCREATESTRUCTA here */
1069 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1070 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1072 ci->hWindowMenu = ccs->hWindowMenu;
1073 ci->idFirstChild = ccs->idFirstChild;
1074 ci->hwndChildMaximized = 0;
1075 ci->child = NULL;
1076 ci->nActiveChildren = 0;
1077 ci->nTotalCreated = 0;
1078 ci->frameTitle = NULL;
1079 ci->mdiFlags = 0;
1080 ci->hFrameMenu = GetMenu(cs->hwndParent);
1082 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1084 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1085 hwnd, ci->hWindowMenu, ci->idFirstChild );
1086 return 0;
1089 case WM_DESTROY:
1091 if( ci->hwndChildMaximized )
1092 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1094 ci->nActiveChildren = 0;
1095 MDI_RefreshMenu(ci);
1097 HeapFree( GetProcessHeap(), 0, ci->child );
1098 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1100 return 0;
1103 case WM_MDIACTIVATE:
1105 if( ci->hwndActiveChild != (HWND)wParam )
1106 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1107 return 0;
1110 case WM_MDICASCADE:
1111 return MDICascade(hwnd, ci);
1113 case WM_MDICREATE:
1114 if (lParam)
1116 HWND child;
1118 if (unicode)
1120 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1121 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1122 csW->szTitle, csW->style,
1123 csW->x, csW->y, csW->cx, csW->cy,
1124 hwnd, 0, csW->hOwner,
1125 (LPVOID)csW->lParam);
1127 else
1129 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1130 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1131 csA->szTitle, csA->style,
1132 csA->x, csA->y, csA->cx, csA->cy,
1133 hwnd, 0, csA->hOwner,
1134 (LPVOID)csA->lParam);
1136 return (LRESULT)child;
1138 return 0;
1140 case WM_MDIDESTROY:
1141 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1143 case WM_MDIGETACTIVE:
1144 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1145 return (LRESULT)ci->hwndActiveChild;
1147 case WM_MDIICONARRANGE:
1148 ci->mdiFlags |= MDIF_NEEDUPDATE;
1149 ArrangeIconicWindows( hwnd );
1150 ci->sbRecalc = SB_BOTH+1;
1151 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1152 return 0;
1154 case WM_MDIMAXIMIZE:
1155 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1156 return 0;
1158 case WM_MDINEXT: /* lParam != 0 means previous window */
1160 HWND hwnd = wParam ? WIN_GetFullHandle((HWND)wParam) : ci->hwndActiveChild;
1161 HWND next = MDI_GetWindow( ci, hwnd, !lParam, 0 );
1162 MDI_SwitchActiveChild( ci, next, TRUE );
1163 if(!lParam)
1164 SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
1165 break;
1168 case WM_MDIRESTORE:
1169 ShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1170 return 0;
1172 case WM_MDISETMENU:
1173 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1175 case WM_MDIREFRESHMENU:
1176 return MDI_RefreshMenu( ci );
1178 case WM_MDITILE:
1179 ci->mdiFlags |= MDIF_NEEDUPDATE;
1180 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1181 MDITile( hwnd, ci, wParam );
1182 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1183 return 0;
1185 case WM_VSCROLL:
1186 case WM_HSCROLL:
1187 ci->mdiFlags |= MDIF_NEEDUPDATE;
1188 ScrollChildren( hwnd, message, wParam, lParam );
1189 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1190 return 0;
1192 case WM_SETFOCUS:
1193 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1194 SetFocus( ci->hwndActiveChild );
1195 return 0;
1197 case WM_NCACTIVATE:
1198 if( ci->hwndActiveChild )
1199 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1200 break;
1202 case WM_PARENTNOTIFY:
1203 switch (LOWORD(wParam))
1205 case WM_CREATE:
1206 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1208 ci->nTotalCreated++;
1209 ci->nActiveChildren++;
1211 if (!ci->child)
1212 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1213 else
1214 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1216 TRACE("Adding MDI child %p, # of children %d\n",
1217 (HWND)lParam, ci->nActiveChildren);
1219 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1221 break;
1223 case WM_LBUTTONDOWN:
1225 HWND child;
1226 POINT pt;
1227 pt.x = (short)LOWORD(lParam);
1228 pt.y = (short)HIWORD(lParam);
1229 child = ChildWindowFromPoint(hwnd, pt);
1231 TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1233 if( child && child != hwnd && child != ci->hwndActiveChild )
1234 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1235 break;
1238 case WM_DESTROY:
1239 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1241 return 0;
1243 case WM_SIZE:
1244 if( ci->hwndActiveChild && IsZoomed(ci->hwndActiveChild) )
1246 RECT rect;
1248 rect.left = 0;
1249 rect.top = 0;
1250 rect.right = LOWORD(lParam);
1251 rect.bottom = HIWORD(lParam);
1252 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1253 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1254 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1255 rect.right - rect.left, rect.bottom - rect.top, 1);
1257 else
1258 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1260 break;
1262 case WM_MDICALCCHILDSCROLL:
1263 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1265 CalcChildScroll(hwnd, ci->sbRecalc-1);
1266 ci->sbRecalc = 0;
1267 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1269 return 0;
1271 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1272 DefWindowProcA( hwnd, message, wParam, lParam );
1275 /***********************************************************************
1276 * DefFrameProcA (USER32.@)
1278 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1279 UINT message, WPARAM wParam, LPARAM lParam)
1281 if (hwndMDIClient)
1283 switch (message)
1285 case WM_SETTEXT:
1287 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1288 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1289 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1290 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1291 HeapFree( GetProcessHeap(), 0, text );
1293 return 1; /* success. FIXME: check text length */
1295 case WM_COMMAND:
1296 case WM_NCACTIVATE:
1297 case WM_NEXTMENU:
1298 case WM_SETFOCUS:
1299 case WM_SIZE:
1300 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1303 return DefWindowProcA(hwnd, message, wParam, lParam);
1307 /***********************************************************************
1308 * DefFrameProcW (USER32.@)
1310 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1311 UINT message, WPARAM wParam, LPARAM lParam)
1313 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1315 TRACE("%p %p %04x (%s) %08lx %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1317 if (ci)
1319 switch (message)
1321 case WM_COMMAND:
1323 WORD id = LOWORD(wParam);
1324 /* check for possible syscommands for maximized MDI child */
1325 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1327 if( (id - 0xf000) & 0xf00f ) break;
1328 if( !ci->hwndChildMaximized ) break;
1329 switch( id )
1331 case SC_CLOSE:
1332 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1333 case SC_SIZE:
1334 case SC_MOVE:
1335 case SC_MINIMIZE:
1336 case SC_MAXIMIZE:
1337 case SC_NEXTWINDOW:
1338 case SC_PREVWINDOW:
1339 case SC_RESTORE:
1340 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1341 wParam, lParam);
1344 else
1346 HWND childHwnd;
1347 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1348 /* User chose "More Windows..." */
1349 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1350 else
1351 /* User chose one of the windows listed in the "Windows" menu */
1352 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1354 if( childHwnd )
1355 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1358 break;
1360 case WM_NCACTIVATE:
1361 SendMessageW(hwndMDIClient, message, wParam, lParam);
1362 break;
1364 case WM_SETTEXT:
1365 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1366 return 1; /* success. FIXME: check text length */
1368 case WM_SETFOCUS:
1369 SetFocus(hwndMDIClient);
1370 break;
1372 case WM_SIZE:
1373 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1374 break;
1376 case WM_NEXTMENU:
1378 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1380 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1382 /* control menu is between the frame system menu and
1383 * the first entry of menu bar */
1384 WND *wndPtr = WIN_GetPtr(hwnd);
1386 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1387 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1389 WIN_ReleasePtr(wndPtr);
1390 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1391 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1392 next_menu->hwndNext = ci->hwndActiveChild;
1394 WIN_ReleasePtr(wndPtr);
1396 return 0;
1401 return DefWindowProcW( hwnd, message, wParam, lParam );
1404 /***********************************************************************
1405 * DefMDIChildProcA (USER32.@)
1407 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1408 WPARAM wParam, LPARAM lParam )
1410 HWND client = GetParent(hwnd);
1411 MDICLIENTINFO *ci = get_client_info( client );
1413 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1415 hwnd = WIN_GetFullHandle( hwnd );
1416 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1418 switch (message)
1420 case WM_SETTEXT:
1421 DefWindowProcA(hwnd, message, wParam, lParam);
1422 if( ci->hwndChildMaximized == hwnd )
1423 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1424 return 1; /* success. FIXME: check text length */
1426 case WM_GETMINMAXINFO:
1427 case WM_MENUCHAR:
1428 case WM_CLOSE:
1429 case WM_SETFOCUS:
1430 case WM_CHILDACTIVATE:
1431 case WM_SYSCOMMAND:
1432 case WM_SHOWWINDOW:
1433 case WM_SETVISIBLE:
1434 case WM_SIZE:
1435 case WM_NEXTMENU:
1436 case WM_SYSCHAR:
1437 case WM_DESTROY:
1438 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1440 return DefWindowProcA(hwnd, message, wParam, lParam);
1444 /***********************************************************************
1445 * DefMDIChildProcW (USER32.@)
1447 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1448 WPARAM wParam, LPARAM lParam )
1450 HWND client = GetParent(hwnd);
1451 MDICLIENTINFO *ci = get_client_info( client );
1453 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1455 hwnd = WIN_GetFullHandle( hwnd );
1456 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1458 switch (message)
1460 case WM_SETTEXT:
1461 DefWindowProcW(hwnd, message, wParam, lParam);
1462 if( ci->hwndChildMaximized == hwnd )
1463 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1464 return 1; /* success. FIXME: check text length */
1466 case WM_GETMINMAXINFO:
1467 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1468 return 0;
1470 case WM_MENUCHAR:
1471 return MAKELRESULT( 0, MNC_CLOSE ); /* MDI children don't have menu bars */
1473 case WM_CLOSE:
1474 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1475 return 0;
1477 case WM_SETFOCUS:
1478 if (ci->hwndActiveChild != hwnd)
1479 MDI_ChildActivate( client, hwnd );
1480 break;
1482 case WM_CHILDACTIVATE:
1483 MDI_ChildActivate( client, hwnd );
1484 return 0;
1486 case WM_SYSCOMMAND:
1487 switch (wParam & 0xfff0)
1489 case SC_MOVE:
1490 if( ci->hwndChildMaximized == hwnd )
1491 return 0;
1492 break;
1493 case SC_RESTORE:
1494 case SC_MINIMIZE:
1495 break;
1496 case SC_MAXIMIZE:
1497 if (ci->hwndChildMaximized == hwnd)
1498 return SendMessageW( GetParent(client), message, wParam, lParam);
1499 break;
1500 case SC_NEXTWINDOW:
1501 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 0);
1502 return 0;
1503 case SC_PREVWINDOW:
1504 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 1);
1505 return 0;
1507 break;
1509 case WM_SHOWWINDOW:
1510 case WM_SETVISIBLE:
1511 if (ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1512 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1513 break;
1515 case WM_SIZE:
1516 /* This is the only place where we switch to/from maximized state */
1517 /* do not change */
1518 TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1520 if( ci->hwndChildMaximized == hwnd && wParam != SIZE_MAXIMIZED )
1522 HWND frame;
1524 ci->hwndChildMaximized = 0;
1526 frame = GetParent(client);
1527 MDI_RestoreFrameMenu( frame, hwnd );
1528 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1531 if( wParam == SIZE_MAXIMIZED )
1533 HWND frame, hMaxChild = ci->hwndChildMaximized;
1535 if( hMaxChild == hwnd ) break;
1537 if( hMaxChild)
1539 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1541 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1542 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1544 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1547 TRACE("maximizing child %p\n", hwnd );
1549 /* keep track of the maximized window. */
1550 ci->hwndChildMaximized = hwnd; /* !!! */
1552 frame = GetParent(client);
1553 MDI_AugmentFrameMenu( frame, hwnd );
1554 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1557 if( wParam == SIZE_MINIMIZED )
1559 HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1561 if (!switchTo) switchTo = hwnd;
1562 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1565 MDI_PostUpdate(client, ci, SB_BOTH+1);
1566 break;
1568 case WM_NEXTMENU:
1570 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1571 HWND parent = GetParent(client);
1573 if( wParam == VK_LEFT ) /* switch to frame system menu */
1575 WND *wndPtr = WIN_GetPtr( parent );
1576 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1577 WIN_ReleasePtr( wndPtr );
1579 if( wParam == VK_RIGHT ) /* to frame menu bar */
1581 next_menu->hmenuNext = GetMenu(parent);
1583 next_menu->hwndNext = parent;
1584 return 0;
1587 case WM_SYSCHAR:
1588 if (wParam == '-')
1590 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, VK_SPACE);
1591 return 0;
1593 break;
1595 case WM_DESTROY:
1596 /* Remove itself from the Window menu */
1597 MDI_RefreshMenu(ci);
1598 break;
1600 return DefWindowProcW(hwnd, message, wParam, lParam);
1603 /**********************************************************************
1604 * CreateMDIWindowA (USER32.@) Creates a MDI child
1606 * RETURNS
1607 * Success: Handle to created window
1608 * Failure: NULL
1610 HWND WINAPI CreateMDIWindowA(
1611 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1612 LPCSTR lpWindowName, /* [in] Pointer to window name */
1613 DWORD dwStyle, /* [in] Window style */
1614 INT X, /* [in] Horizontal position of window */
1615 INT Y, /* [in] Vertical position of window */
1616 INT nWidth, /* [in] Width of window */
1617 INT nHeight, /* [in] Height of window */
1618 HWND hWndParent, /* [in] Handle to parent window */
1619 HINSTANCE hInstance, /* [in] Handle to application instance */
1620 LPARAM lParam) /* [in] Application-defined value */
1622 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1623 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1624 nWidth,nHeight,hWndParent,hInstance,lParam);
1626 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1627 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1628 0, hInstance, (LPVOID)lParam);
1631 /***********************************************************************
1632 * CreateMDIWindowW (USER32.@) Creates a MDI child
1634 * RETURNS
1635 * Success: Handle to created window
1636 * Failure: NULL
1638 HWND WINAPI CreateMDIWindowW(
1639 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1640 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1641 DWORD dwStyle, /* [in] Window style */
1642 INT X, /* [in] Horizontal position of window */
1643 INT Y, /* [in] Vertical position of window */
1644 INT nWidth, /* [in] Width of window */
1645 INT nHeight, /* [in] Height of window */
1646 HWND hWndParent, /* [in] Handle to parent window */
1647 HINSTANCE hInstance, /* [in] Handle to application instance */
1648 LPARAM lParam) /* [in] Application-defined value */
1650 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1651 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1652 nWidth, nHeight, hWndParent, hInstance, lParam);
1654 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1655 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1656 0, hInstance, (LPVOID)lParam);
1659 /**********************************************************************
1660 * TranslateMDISysAccel (USER32.@)
1662 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1664 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1666 MDICLIENTINFO *ci = get_client_info( hwndClient );
1667 WPARAM wParam = 0;
1669 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return FALSE;
1671 /* translate if the Ctrl key is down and Alt not. */
1673 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1675 switch( msg->wParam )
1677 case VK_F6:
1678 case VK_TAB:
1679 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1680 break;
1681 case VK_F4:
1682 case VK_RBUTTON:
1683 if (is_close_enabled(ci->hwndActiveChild, 0))
1685 wParam = SC_CLOSE;
1686 break;
1688 /* fall through */
1689 default:
1690 return FALSE;
1692 TRACE("wParam = %04lx\n", wParam);
1693 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, msg->wParam);
1694 return TRUE;
1697 return FALSE; /* failure */
1700 /***********************************************************************
1701 * CalcChildScroll (USER32.@)
1703 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1705 SCROLLINFO info;
1706 RECT childRect, clientRect;
1707 HWND *list;
1709 GetClientRect( hwnd, &clientRect );
1710 SetRectEmpty( &childRect );
1712 if ((list = WIN_ListChildren( hwnd )))
1714 int i;
1715 for (i = 0; list[i]; i++)
1717 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1718 if (style & WS_MAXIMIZE)
1720 HeapFree( GetProcessHeap(), 0, list );
1721 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1722 return;
1724 if (style & WS_VISIBLE)
1726 RECT rect;
1727 WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL );
1728 UnionRect( &childRect, &rect, &childRect );
1731 HeapFree( GetProcessHeap(), 0, list );
1733 UnionRect( &childRect, &clientRect, &childRect );
1735 /* set common info values */
1736 info.cbSize = sizeof(info);
1737 info.fMask = SIF_POS | SIF_RANGE;
1739 /* set the specific */
1740 switch( scroll )
1742 case SB_BOTH:
1743 case SB_HORZ:
1744 info.nMin = childRect.left;
1745 info.nMax = childRect.right - clientRect.right;
1746 info.nPos = clientRect.left - childRect.left;
1747 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1748 if (scroll == SB_HORZ) break;
1749 /* fall through */
1750 case SB_VERT:
1751 info.nMin = childRect.top;
1752 info.nMax = childRect.bottom - clientRect.bottom;
1753 info.nPos = clientRect.top - childRect.top;
1754 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1755 break;
1760 /***********************************************************************
1761 * ScrollChildren (USER32.@)
1763 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1764 LPARAM lParam)
1766 INT newPos = -1;
1767 INT curPos, length, minPos, maxPos, shift;
1768 RECT rect;
1770 GetClientRect( hWnd, &rect );
1772 switch(uMsg)
1774 case WM_HSCROLL:
1775 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1776 curPos = GetScrollPos(hWnd,SB_HORZ);
1777 length = (rect.right - rect.left) / 2;
1778 shift = GetSystemMetrics(SM_CYHSCROLL);
1779 break;
1780 case WM_VSCROLL:
1781 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1782 curPos = GetScrollPos(hWnd,SB_VERT);
1783 length = (rect.bottom - rect.top) / 2;
1784 shift = GetSystemMetrics(SM_CXVSCROLL);
1785 break;
1786 default:
1787 return;
1790 switch( wParam )
1792 case SB_LINEUP:
1793 newPos = curPos - shift;
1794 break;
1795 case SB_LINEDOWN:
1796 newPos = curPos + shift;
1797 break;
1798 case SB_PAGEUP:
1799 newPos = curPos - length;
1800 break;
1801 case SB_PAGEDOWN:
1802 newPos = curPos + length;
1803 break;
1805 case SB_THUMBPOSITION:
1806 newPos = LOWORD(lParam);
1807 break;
1809 case SB_THUMBTRACK:
1810 return;
1812 case SB_TOP:
1813 newPos = minPos;
1814 break;
1815 case SB_BOTTOM:
1816 newPos = maxPos;
1817 break;
1818 case SB_ENDSCROLL:
1819 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1820 return;
1823 if( newPos > maxPos )
1824 newPos = maxPos;
1825 else
1826 if( newPos < minPos )
1827 newPos = minPos;
1829 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1831 if( uMsg == WM_VSCROLL )
1832 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1833 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1834 else
1835 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1836 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1840 /******************************************************************************
1841 * CascadeWindows (USER32.@) Cascades MDI child windows
1843 * RETURNS
1844 * Success: Number of cascaded windows.
1845 * Failure: 0
1847 WORD WINAPI
1848 CascadeWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1849 UINT cKids, const HWND *lpKids)
1851 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1852 return 0;
1856 /***********************************************************************
1857 * CascadeChildWindows (USER32.@)
1859 WORD WINAPI CascadeChildWindows( HWND parent, UINT flags )
1861 return CascadeWindows( parent, flags, NULL, 0, NULL );
1865 /******************************************************************************
1866 * TileWindows (USER32.@) Tiles MDI child windows
1868 * RETURNS
1869 * Success: Number of tiled windows.
1870 * Failure: 0
1872 WORD WINAPI
1873 TileWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1874 UINT cKids, const HWND *lpKids)
1876 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1877 return 0;
1881 /***********************************************************************
1882 * TileChildWindows (USER32.@)
1884 WORD WINAPI TileChildWindows( HWND parent, UINT flags )
1886 return TileWindows( parent, flags, NULL, 0, NULL );
1890 /************************************************************************
1891 * "More Windows..." functionality
1894 /* MDI_MoreWindowsDlgProc
1896 * This function will process the messages sent to the "More Windows..."
1897 * dialog.
1898 * Return values: 0 = cancel pressed
1899 * HWND = ok pressed or double-click in the list...
1903 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1905 switch (iMsg)
1907 case WM_INITDIALOG:
1909 UINT widest = 0;
1910 UINT length;
1911 UINT i;
1912 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1913 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1915 for (i = 0; i < ci->nActiveChildren; i++)
1917 WCHAR buffer[MDI_MAXTITLELENGTH];
1919 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1920 continue;
1921 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1922 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1923 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1924 if (length > widest)
1925 widest = length;
1927 /* Make sure the horizontal scrollbar scrolls ok */
1928 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1930 /* Set the current selection */
1931 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1932 return TRUE;
1935 case WM_COMMAND:
1936 switch (LOWORD(wParam))
1938 default:
1939 if (HIWORD(wParam) != LBN_DBLCLK) break;
1940 /* fall through */
1941 case IDOK:
1943 /* windows are sorted by menu ID, so we must return the
1944 * window associated to the given id
1946 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1947 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1948 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1949 EndDialog(hDlg, res);
1950 return TRUE;
1952 case IDCANCEL:
1953 EndDialog(hDlg, 0);
1954 return TRUE;
1956 break;
1958 return FALSE;
1963 * MDI_MoreWindowsDialog
1965 * Prompts the user with a listbox containing the opened
1966 * documents. The user can then choose a windows and click
1967 * on OK to set the current window to the one selected, or
1968 * CANCEL to cancel. The function returns a handle to the
1969 * selected window.
1972 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1974 LPCVOID template;
1975 HRSRC hRes;
1976 HANDLE hDlgTmpl;
1978 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1980 if (hRes == 0)
1981 return 0;
1983 hDlgTmpl = LoadResource(user32_module, hRes );
1985 if (hDlgTmpl == 0)
1986 return 0;
1988 template = LockResource( hDlgTmpl );
1990 if (template == 0)
1991 return 0;
1993 return (HWND) DialogBoxIndirectParamA(user32_module, template, hwnd,
1994 MDI_MoreWindowsDlgProc, (LPARAM) hwnd);