winhelp: Do not confuse caller application with bogus error when we really succeeded.
[wine/wine64.git] / dlls / user32 / mdi.c
blobbad5519285da488351c2bfb05ab5f4678898e058
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 #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/winuser16.h"
97 #include "wine/unicode.h"
98 #include "win.h"
99 #include "controls.h"
100 #include "user_private.h"
101 #include "wine/debug.h"
103 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
105 #define MDI_MAXTITLELENGTH 0xa1
107 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
109 /* "More Windows..." definitions */
110 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
111 option will appear under the Windows menu */
112 #define MDI_IDC_LISTBOX 100
113 #define IDS_MDI_MOREWINDOWS 13
115 #define MDIF_NEEDUPDATE 0x0001
117 typedef struct
119 /* At some points, particularly when switching MDI children, active and
120 * maximized MDI children may be not the same window, so we need to track
121 * them separately.
122 * The only place where we switch to/from maximized state is DefMDIChildProc
123 * WM_SIZE/SIZE_MAXIMIZED handler. We get that notification only after the
124 * ShowWindow(SW_SHOWMAXIMIZED) request, therefore window is guaranteed to
125 * be visible at the time we get the notification, and it's safe to assume
126 * that hwndChildMaximized is always visible.
127 * If the app plays games with WS_VISIBLE, WS_MAXIMIZE or any other window
128 * states it must keep coherency with USER32 on its own. This is true for
129 * Windows as well.
131 UINT nActiveChildren;
132 HWND hwndChildMaximized;
133 HWND hwndActiveChild;
134 HWND *child; /* array of tracked children */
135 HMENU hFrameMenu;
136 HMENU hWindowMenu;
137 UINT idFirstChild;
138 LPWSTR frameTitle;
139 UINT nTotalCreated;
140 UINT mdiFlags;
141 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
142 } MDICLIENTINFO;
144 static HBITMAP hBmpClose = 0;
146 /* ----------------- declarations ----------------- */
147 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
148 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
149 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
150 static LONG MDI_ChildActivate( HWND, HWND );
151 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
153 static HWND MDI_MoreWindowsDialog(HWND);
154 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
155 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
157 /* -------- Miscellaneous service functions ----------
159 * MDI_GetChildByID
161 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
163 int i;
165 for (i = 0; ci->nActiveChildren; i++)
167 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
168 return ci->child[i];
170 return 0;
173 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
175 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
177 ci->mdiFlags |= MDIF_NEEDUPDATE;
178 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
180 ci->sbRecalc = recalc;
184 /*********************************************************************
185 * MDIClient class descriptor
187 static const WCHAR mdiclientW[] = {'M','D','I','C','l','i','e','n','t',0};
188 const struct builtin_class_descr MDICLIENT_builtin_class =
190 mdiclientW, /* name */
191 0, /* style */
192 MDIClientWndProcA, /* procA */
193 MDIClientWndProcW, /* procW */
194 sizeof(MDICLIENTINFO), /* extra */
195 IDC_ARROW, /* cursor */
196 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
200 static MDICLIENTINFO *get_client_info( HWND client )
202 MDICLIENTINFO *ret = NULL;
203 WND *win = WIN_GetPtr( client );
204 if (win)
206 if (win == WND_OTHER_PROCESS || win == WND_DESKTOP)
208 if (IsWindow(client)) WARN( "client %p belongs to other process\n", client );
209 return NULL;
211 if (win->flags & WIN_ISMDICLIENT)
212 ret = (MDICLIENTINFO *)win->wExtra;
213 else
214 WARN( "%p is not an MDI client\n", client );
215 WIN_ReleasePtr( win );
217 return ret;
220 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
222 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
224 if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
225 if (hSysMenu)
227 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
228 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
229 return FALSE;
231 return TRUE;
234 /**********************************************************************
235 * MDI_GetWindow
237 * returns "activatable" child different from the current or zero
239 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
240 DWORD dwStyleMask )
242 int i;
243 HWND *list;
244 HWND last = 0;
246 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
247 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
249 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
250 i = 0;
251 /* start from next after hWnd */
252 while (list[i] && list[i] != hWnd) i++;
253 if (list[i]) i++;
255 for ( ; list[i]; i++)
257 if (GetWindow( list[i], GW_OWNER )) continue;
258 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
259 last = list[i];
260 if (bNext) goto found;
262 /* now restart from the beginning */
263 for (i = 0; list[i] && list[i] != hWnd; i++)
265 if (GetWindow( list[i], GW_OWNER )) continue;
266 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
267 last = list[i];
268 if (bNext) goto found;
270 found:
271 HeapFree( GetProcessHeap(), 0, list );
272 return last;
275 /**********************************************************************
276 * MDI_CalcDefaultChildPos
278 * It seems that the default height is about 2/3 of the client rect
280 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
282 INT nstagger;
283 RECT rect;
284 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
286 if (total < 0) /* we are called from CreateWindow */
288 MDICLIENTINFO *ci = get_client_info(hwndClient);
289 total = ci ? ci->nTotalCreated : 0;
290 *id = ci->idFirstChild + ci->nActiveChildren;
291 TRACE("MDI child id %04x\n", *id);
294 GetClientRect( hwndClient, &rect );
295 if( rect.bottom - rect.top - delta >= spacing )
296 rect.bottom -= delta;
298 nstagger = (rect.bottom - rect.top)/(3 * spacing);
299 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
300 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
301 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
304 /**********************************************************************
305 * MDISetMenu
307 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
308 HMENU hmenuWindow)
310 MDICLIENTINFO *ci;
311 HWND hwndFrame = GetParent(hwnd);
313 TRACE("%p, frame menu %p, window menu %p\n", hwnd, hmenuFrame, hmenuWindow);
315 if (hmenuFrame && !IsMenu(hmenuFrame))
317 WARN("hmenuFrame is not a menu handle\n");
318 return 0L;
321 if (hmenuWindow && !IsMenu(hmenuWindow))
323 WARN("hmenuWindow is not a menu handle\n");
324 return 0L;
327 if (!(ci = get_client_info( hwnd ))) return 0;
329 TRACE("old frame menu %p, old window menu %p\n", ci->hFrameMenu, ci->hWindowMenu);
331 if (hmenuFrame)
333 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
335 if (ci->hwndChildMaximized)
336 MDI_RestoreFrameMenu( hwndFrame, ci->hwndChildMaximized );
339 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
341 /* delete menu items from ci->hWindowMenu
342 * and add them to hmenuWindow */
343 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
344 if( ci->hWindowMenu && ci->nActiveChildren )
346 UINT nActiveChildren_old = ci->nActiveChildren;
348 /* Remove all items from old Window menu */
349 ci->nActiveChildren = 0;
350 MDI_RefreshMenu(ci);
352 ci->hWindowMenu = hmenuWindow;
354 /* Add items to the new Window menu */
355 ci->nActiveChildren = nActiveChildren_old;
356 MDI_RefreshMenu(ci);
358 else
359 ci->hWindowMenu = hmenuWindow;
362 if (hmenuFrame)
364 SetMenu(hwndFrame, hmenuFrame);
365 if( hmenuFrame != ci->hFrameMenu )
367 HMENU oldFrameMenu = ci->hFrameMenu;
369 ci->hFrameMenu = hmenuFrame;
370 if (ci->hwndChildMaximized)
371 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
373 return (LRESULT)oldFrameMenu;
376 else
378 /* SetMenu() may already have been called, meaning that this window
379 * already has its menu. But they may have done a SetMenu() on
380 * an MDI window, and called MDISetMenu() after the fact, meaning
381 * that the "if" to this "else" wouldn't catch the need to
382 * augment the frame menu.
384 if( ci->hwndChildMaximized )
385 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
388 return 0;
391 /**********************************************************************
392 * MDIRefreshMenu
394 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
396 UINT i, count, visible, id;
397 WCHAR buf[MDI_MAXTITLELENGTH];
399 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
401 if (!ci->hWindowMenu)
402 return 0;
404 if (!IsMenu(ci->hWindowMenu))
406 WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
407 return 0;
410 /* Windows finds the last separator in the menu, and if after it
411 * there is a menu item with MDI magic ID removes all existing
412 * menu items after it, and then adds visible MDI children.
414 count = GetMenuItemCount(ci->hWindowMenu);
415 for (i = 0; i < count; i++)
417 MENUITEMINFOW mii;
419 memset(&mii, 0, sizeof(mii));
420 mii.cbSize = sizeof(mii);
421 mii.fMask = MIIM_TYPE;
422 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
424 if (mii.fType & MF_SEPARATOR)
426 /* Windows checks only ID of the menu item */
427 memset(&mii, 0, sizeof(mii));
428 mii.cbSize = sizeof(mii);
429 mii.fMask = MIIM_ID;
430 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
432 if (mii.wID == ci->idFirstChild)
434 TRACE("removing %u items including separator\n", count - i);
435 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
436 /* nothing */;
438 break;
445 visible = 0;
446 for (i = 0; i < ci->nActiveChildren; i++)
448 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
450 id = ci->idFirstChild + visible;
452 if (visible == MDI_MOREWINDOWSLIMIT)
454 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
455 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
456 break;
459 if (!visible)
460 /* Visio expects that separator has id 0 */
461 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
463 visible++;
465 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
467 buf[0] = '&';
468 buf[1] = '0' + visible;
469 buf[2] = ' ';
470 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
471 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
472 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
474 if (ci->child[i] == ci->hwndActiveChild)
475 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
477 else
478 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
481 return (LRESULT)ci->hFrameMenu;
485 /* ------------------ MDI child window functions ---------------------- */
487 /**********************************************************************
488 * MDI_ChildGetMinMaxInfo
490 * Note: The rule here is that client rect of the maximized MDI child
491 * is equal to the client rect of the MDI client window.
493 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
495 RECT rect;
497 GetClientRect( client, &rect );
498 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
499 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
501 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
502 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
504 lpMinMax->ptMaxPosition.x = rect.left;
505 lpMinMax->ptMaxPosition.y = rect.top;
507 TRACE("max rect (%d,%d - %d, %d)\n",
508 rect.left,rect.top,rect.right,rect.bottom);
511 /**********************************************************************
512 * MDI_SwitchActiveChild
514 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
515 * being activated
517 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
519 HWND hwndPrev;
521 hwndPrev = ci->hwndActiveChild;
523 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
525 if ( hwndTo != hwndPrev )
527 BOOL was_zoomed = IsZoomed(hwndPrev);
529 if (was_zoomed)
531 /* restore old MDI child */
532 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
533 ShowWindow( hwndPrev, SW_RESTORE );
534 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
536 /* activate new MDI child */
537 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
538 /* maximize new MDI child */
539 ShowWindow( hwndTo, SW_MAXIMIZE );
541 /* activate new MDI child */
542 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
547 /**********************************************************************
548 * MDIDestroyChild
550 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
551 HWND child, BOOL flagDestroy )
553 UINT i;
555 TRACE("# of managed children %u\n", ci->nActiveChildren);
557 if( child == ci->hwndActiveChild )
559 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
560 /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
561 if (flagDestroy && next)
562 MDI_SwitchActiveChild(ci, next, TRUE);
563 else
565 ShowWindow(child, SW_HIDE);
566 if (child == ci->hwndChildMaximized)
568 HWND frame = GetParent(client);
569 MDI_RestoreFrameMenu(frame, child);
570 ci->hwndChildMaximized = 0;
571 MDI_UpdateFrameText(frame, client, TRUE, NULL);
573 if (flagDestroy) ci->hwndActiveChild = 0;
577 for (i = 0; i < ci->nActiveChildren; i++)
579 if (ci->child[i] == child)
581 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
582 memcpy(new_child, ci->child, i * sizeof(HWND));
583 if (i + 1 < ci->nActiveChildren)
584 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
585 HeapFree(GetProcessHeap(), 0, ci->child);
586 ci->child = new_child;
588 ci->nActiveChildren--;
589 break;
593 if (flagDestroy)
595 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
596 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
597 DestroyWindow(child);
600 TRACE("child destroyed - %p\n", child);
601 return 0;
605 /**********************************************************************
606 * MDI_ChildActivate
608 * Called in response to WM_CHILDACTIVATE, or when last MDI child
609 * is being deactivated.
611 static LONG MDI_ChildActivate( HWND client, HWND child )
613 MDICLIENTINFO *clientInfo;
614 HWND prevActiveWnd, frame;
615 BOOL isActiveFrameWnd;
617 clientInfo = get_client_info( client );
619 if (clientInfo->hwndActiveChild == child) return 0;
621 TRACE("%p\n", child);
623 frame = GetParent(client);
624 isActiveFrameWnd = (GetActiveWindow() == frame);
625 prevActiveWnd = clientInfo->hwndActiveChild;
627 /* deactivate prev. active child */
628 if(prevActiveWnd)
630 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
631 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
634 MDI_SwitchActiveChild( clientInfo, child, FALSE );
635 clientInfo->hwndActiveChild = child;
637 MDI_RefreshMenu(clientInfo);
639 if( isActiveFrameWnd )
641 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
642 /* Let the client window manage focus for children, but if the focus
643 * is already on the client (for instance this is the 1st child) then
644 * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
645 * manually in this case.
647 if (SetFocus(client) == client)
648 SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
651 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
652 return TRUE;
655 /* -------------------- MDI client window functions ------------------- */
657 /**********************************************************************
658 * CreateMDIMenuBitmap
660 static HBITMAP CreateMDIMenuBitmap(void)
662 HDC hDCSrc = CreateCompatibleDC(0);
663 HDC hDCDest = CreateCompatibleDC(hDCSrc);
664 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
665 HBITMAP hbCopy;
666 HBITMAP hobjSrc, hobjDest;
668 hobjSrc = SelectObject(hDCSrc, hbClose);
669 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
670 hobjDest = SelectObject(hDCDest, hbCopy);
672 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
673 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
675 SelectObject(hDCSrc, hobjSrc);
676 DeleteObject(hbClose);
677 DeleteDC(hDCSrc);
679 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
681 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
682 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
684 SelectObject(hDCDest, hobjSrc );
685 SelectObject(hDCDest, hobjDest);
686 DeleteDC(hDCDest);
688 return hbCopy;
691 /**********************************************************************
692 * MDICascade
694 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
696 HWND *win_array;
697 BOOL has_icons = FALSE;
698 int i, total;
700 if (ci->hwndChildMaximized)
701 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
703 if (ci->nActiveChildren == 0) return 0;
705 if (!(win_array = WIN_ListChildren( client ))) return 0;
707 /* remove all the windows we don't want */
708 for (i = total = 0; win_array[i]; i++)
710 if (!IsWindowVisible( win_array[i] )) continue;
711 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
712 if (IsIconic( win_array[i] ))
714 has_icons = TRUE;
715 continue;
717 win_array[total++] = win_array[i];
719 win_array[total] = 0;
721 if (total)
723 INT delta = 0, n = 0, i;
724 POINT pos[2];
725 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
727 /* walk the list (backwards) and move windows */
728 for (i = total - 1; i >= 0; i--)
730 LONG style;
731 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
733 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
734 TRACE("move %p to (%d,%d) size [%d,%d]\n",
735 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
736 style = GetWindowLongW(win_array[i], GWL_STYLE);
738 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
739 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
740 posOptions);
743 HeapFree( GetProcessHeap(), 0, win_array );
745 if (has_icons) ArrangeIconicWindows( client );
746 return 0;
749 /**********************************************************************
750 * MDITile
752 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
754 HWND *win_array;
755 int i, total;
756 BOOL has_icons = FALSE;
758 if (ci->hwndChildMaximized)
759 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
761 if (ci->nActiveChildren == 0) return;
763 if (!(win_array = WIN_ListChildren( client ))) return;
765 /* remove all the windows we don't want */
766 for (i = total = 0; win_array[i]; i++)
768 if (!IsWindowVisible( win_array[i] )) continue;
769 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
770 if (IsIconic( win_array[i] ))
772 has_icons = TRUE;
773 continue;
775 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
776 win_array[total++] = win_array[i];
778 win_array[total] = 0;
780 TRACE("%u windows to tile\n", total);
782 if (total)
784 HWND *pWnd = win_array;
785 RECT rect;
786 int x, y, xsize, ysize;
787 int rows, columns, r, c, i;
789 GetClientRect(client,&rect);
790 rows = (int) sqrt((double)total);
791 columns = total / rows;
793 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
795 i = rows;
796 rows = columns; /* exchange r and c */
797 columns = i;
800 if (has_icons)
802 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
803 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
806 ysize = rect.bottom / rows;
807 xsize = rect.right / columns;
809 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
811 if (c == columns)
813 rows = total - i;
814 ysize = rect.bottom / rows;
817 y = 0;
818 for (r = 1; r <= rows && *pWnd; r++, i++)
820 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
821 LONG style = GetWindowLongW(win_array[i], GWL_STYLE);
822 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
824 SetWindowPos(*pWnd, 0, x, y, xsize, ysize, posOptions);
825 y += ysize;
826 pWnd++;
828 x += xsize;
831 HeapFree( GetProcessHeap(), 0, win_array );
832 if (has_icons) ArrangeIconicWindows( client );
835 /* ----------------------- Frame window ---------------------------- */
838 /**********************************************************************
839 * MDI_AugmentFrameMenu
841 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
843 HMENU menu = GetMenu( frame );
844 HMENU hSysPopup = 0;
845 HBITMAP hSysMenuBitmap = 0;
846 HICON hIcon;
848 TRACE("frame %p,child %p\n",frame,hChild);
850 if( !menu ) return 0;
852 /* create a copy of sysmenu popup and insert it into frame menu bar */
853 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
855 TRACE("child %p doesn't have a system menu\n", hChild);
856 return 0;
859 AppendMenuW(menu, MF_HELP | MF_BITMAP,
860 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
861 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
862 AppendMenuW(menu, MF_HELP | MF_BITMAP,
863 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
864 AppendMenuW(menu, MF_HELP | MF_BITMAP,
865 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
867 /* The system menu is replaced by the child icon */
868 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_SMALL, 0);
869 if (!hIcon)
870 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_BIG, 0);
871 if (!hIcon)
872 hIcon = LoadImageW(0, MAKEINTRESOURCEW(IDI_WINLOGO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
873 if (hIcon)
875 HDC hMemDC;
876 HBITMAP hBitmap, hOldBitmap;
877 HBRUSH hBrush;
878 HDC hdc = GetDC(hChild);
880 if (hdc)
882 int cx, cy;
883 cx = GetSystemMetrics(SM_CXSMICON);
884 cy = GetSystemMetrics(SM_CYSMICON);
885 hMemDC = CreateCompatibleDC(hdc);
886 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
887 hOldBitmap = SelectObject(hMemDC, hBitmap);
888 SetMapMode(hMemDC, MM_TEXT);
889 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
890 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
891 SelectObject (hMemDC, hOldBitmap);
892 DeleteObject(hBrush);
893 DeleteDC(hMemDC);
894 ReleaseDC(hChild, hdc);
895 hSysMenuBitmap = hBitmap;
899 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
900 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
902 TRACE("not inserted\n");
903 DestroyMenu(hSysPopup);
904 return 0;
907 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
908 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
909 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
910 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
912 /* redraw menu */
913 DrawMenuBar(frame);
915 return 1;
918 /**********************************************************************
919 * MDI_RestoreFrameMenu
921 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
923 MENUITEMINFOW menuInfo;
924 HMENU menu = GetMenu( frame );
925 INT nItems;
926 UINT iId;
928 TRACE("frame %p, child %p\n", frame, hChild);
930 if( !menu ) return 0;
932 /* if there is no system buttons then nothing to do */
933 nItems = GetMenuItemCount(menu) - 1;
934 iId = GetMenuItemID(menu, nItems);
935 if ( !(iId == SC_RESTORE || iId == SC_CLOSE) )
936 return 0;
939 * Remove the system menu, If that menu is the icon of the window
940 * as it is in win95, we have to delete the bitmap.
942 memset(&menuInfo, 0, sizeof(menuInfo));
943 menuInfo.cbSize = sizeof(menuInfo);
944 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
946 GetMenuItemInfoW(menu,
948 TRUE,
949 &menuInfo);
951 RemoveMenu(menu,0,MF_BYPOSITION);
953 if ( (menuInfo.fType & MFT_BITMAP) &&
954 (LOWORD(menuInfo.dwTypeData)!=0) &&
955 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
957 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
960 /* close */
961 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
962 /* restore */
963 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
964 /* minimize */
965 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
967 DrawMenuBar(frame);
969 return 1;
973 /**********************************************************************
974 * MDI_UpdateFrameText
976 * used when child window is maximized/restored
978 * Note: lpTitle can be NULL
980 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
982 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
983 MDICLIENTINFO *ci = get_client_info( hClient );
985 TRACE("frameText %s\n", debugstr_w(lpTitle));
987 if (!ci) return;
989 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
991 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
992 lpTitle = lpBuffer;
995 /* store new "default" title if lpTitle is not NULL */
996 if (lpTitle)
998 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
999 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
1000 strcpyW( ci->frameTitle, lpTitle );
1003 if (ci->frameTitle)
1005 if (ci->hwndChildMaximized)
1007 /* combine frame title and child title if possible */
1009 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
1010 static const WCHAR lpBracket2[] = {']',0};
1011 int i_frame_text_length = strlenW(ci->frameTitle);
1013 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1015 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1017 strcatW( lpBuffer, lpBracket );
1018 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
1019 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1020 strcatW( lpBuffer, lpBracket2 );
1021 else
1022 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1025 else
1027 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1030 else
1031 lpBuffer[0] = '\0';
1033 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1035 if (repaint)
1036 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1037 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1041 /* ----------------------------- Interface ---------------------------- */
1044 /**********************************************************************
1045 * MDIClientWndProc_common
1047 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1048 WPARAM wParam, LPARAM lParam, BOOL unicode )
1050 MDICLIENTINFO *ci;
1052 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1054 if (!(ci = get_client_info( hwnd )))
1056 if (message == WM_NCCREATE)
1058 WND *wndPtr = WIN_GetPtr( hwnd );
1059 wndPtr->flags |= WIN_ISMDICLIENT;
1060 WIN_ReleasePtr( wndPtr );
1062 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1063 DefWindowProcA( hwnd, message, wParam, lParam );
1066 switch (message)
1068 case WM_CREATE:
1070 /* Since we are using only cs->lpCreateParams, we can safely
1071 * cast to LPCREATESTRUCTA here */
1072 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1073 WND *wndPtr = WIN_GetPtr( hwnd );
1075 /* Translation layer doesn't know what's in the cs->lpCreateParams
1076 * so we have to keep track of what environment we're in. */
1078 if( wndPtr->flags & WIN_ISWIN32 )
1080 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1081 ci->hWindowMenu = ccs->hWindowMenu;
1082 ci->idFirstChild = ccs->idFirstChild;
1084 else
1086 LPCLIENTCREATESTRUCT16 ccs = MapSL(PtrToUlong(cs->lpCreateParams));
1087 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1088 ci->idFirstChild = ccs->idFirstChild;
1090 WIN_ReleasePtr( wndPtr );
1092 ci->hwndChildMaximized = 0;
1093 ci->child = NULL;
1094 ci->nActiveChildren = 0;
1095 ci->nTotalCreated = 0;
1096 ci->frameTitle = NULL;
1097 ci->mdiFlags = 0;
1098 ci->hFrameMenu = GetMenu(cs->hwndParent);
1100 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1102 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1103 hwnd, ci->hWindowMenu, ci->idFirstChild );
1104 return 0;
1107 case WM_DESTROY:
1109 if( ci->hwndChildMaximized )
1110 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1112 ci->nActiveChildren = 0;
1113 MDI_RefreshMenu(ci);
1115 HeapFree( GetProcessHeap(), 0, ci->child );
1116 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1118 return 0;
1121 case WM_MDIACTIVATE:
1123 if( ci->hwndActiveChild != (HWND)wParam )
1124 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1125 return 0;
1128 case WM_MDICASCADE:
1129 return MDICascade(hwnd, ci);
1131 case WM_MDICREATE:
1132 if (lParam)
1134 HWND child;
1136 if (unicode)
1138 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1139 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1140 csW->szTitle, csW->style,
1141 csW->x, csW->y, csW->cx, csW->cy,
1142 hwnd, 0, csW->hOwner,
1143 (LPVOID)csW->lParam);
1145 else
1147 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1148 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1149 csA->szTitle, csA->style,
1150 csA->x, csA->y, csA->cx, csA->cy,
1151 hwnd, 0, csA->hOwner,
1152 (LPVOID)csA->lParam);
1154 return (LRESULT)child;
1156 return 0;
1158 case WM_MDIDESTROY:
1159 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1161 case WM_MDIGETACTIVE:
1162 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1163 return (LRESULT)ci->hwndActiveChild;
1165 case WM_MDIICONARRANGE:
1166 ci->mdiFlags |= MDIF_NEEDUPDATE;
1167 ArrangeIconicWindows( hwnd );
1168 ci->sbRecalc = SB_BOTH+1;
1169 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1170 return 0;
1172 case WM_MDIMAXIMIZE:
1173 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1174 return 0;
1176 case WM_MDINEXT: /* lParam != 0 means previous window */
1178 HWND next = MDI_GetWindow( ci, WIN_GetFullHandle( (HWND)wParam ), !lParam, 0 );
1179 MDI_SwitchActiveChild( ci, next, TRUE );
1180 break;
1183 case WM_MDIRESTORE:
1184 ShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1185 return 0;
1187 case WM_MDISETMENU:
1188 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1190 case WM_MDIREFRESHMENU:
1191 return MDI_RefreshMenu( ci );
1193 case WM_MDITILE:
1194 ci->mdiFlags |= MDIF_NEEDUPDATE;
1195 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1196 MDITile( hwnd, ci, wParam );
1197 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1198 return 0;
1200 case WM_VSCROLL:
1201 case WM_HSCROLL:
1202 ci->mdiFlags |= MDIF_NEEDUPDATE;
1203 ScrollChildren( hwnd, message, wParam, lParam );
1204 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1205 return 0;
1207 case WM_SETFOCUS:
1208 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1209 SetFocus( ci->hwndActiveChild );
1210 return 0;
1212 case WM_NCACTIVATE:
1213 if( ci->hwndActiveChild )
1214 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1215 break;
1217 case WM_PARENTNOTIFY:
1218 switch (LOWORD(wParam))
1220 case WM_CREATE:
1221 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1223 ci->nTotalCreated++;
1224 ci->nActiveChildren++;
1226 if (!ci->child)
1227 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1228 else
1229 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1231 TRACE("Adding MDI child %p, # of children %d\n",
1232 (HWND)lParam, ci->nActiveChildren);
1234 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1236 break;
1238 case WM_LBUTTONDOWN:
1240 HWND child;
1241 POINT pt;
1242 pt.x = (short)LOWORD(lParam);
1243 pt.y = (short)HIWORD(lParam);
1244 child = ChildWindowFromPoint(hwnd, pt);
1246 TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1248 if( child && child != hwnd && child != ci->hwndActiveChild )
1249 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1250 break;
1253 case WM_DESTROY:
1254 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1256 return 0;
1258 case WM_SIZE:
1259 if( ci->hwndChildMaximized )
1261 RECT rect;
1263 rect.left = 0;
1264 rect.top = 0;
1265 rect.right = LOWORD(lParam);
1266 rect.bottom = HIWORD(lParam);
1268 AdjustWindowRectEx( &rect, GetWindowLongA(ci->hwndChildMaximized, GWL_STYLE),
1269 0, GetWindowLongA(ci->hwndChildMaximized, GWL_EXSTYLE) );
1270 MoveWindow( ci->hwndChildMaximized, rect.left, rect.top,
1271 rect.right - rect.left, rect.bottom - rect.top, 1);
1273 else
1274 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1276 break;
1278 case WM_MDICALCCHILDSCROLL:
1279 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1281 CalcChildScroll(hwnd, ci->sbRecalc-1);
1282 ci->sbRecalc = 0;
1283 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1285 return 0;
1287 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1288 DefWindowProcA( hwnd, message, wParam, lParam );
1291 /***********************************************************************
1292 * MDIClientWndProcA
1294 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1296 if (!IsWindow(hwnd)) return 0;
1297 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1300 /***********************************************************************
1301 * MDIClientWndProcW
1303 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1305 if (!IsWindow(hwnd)) return 0;
1306 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1309 /***********************************************************************
1310 * DefFrameProcA (USER32.@)
1312 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1313 UINT message, WPARAM wParam, LPARAM lParam)
1315 if (hwndMDIClient)
1317 switch (message)
1319 case WM_SETTEXT:
1321 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1322 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1323 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1324 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1325 HeapFree( GetProcessHeap(), 0, text );
1327 return 1; /* success. FIXME: check text length */
1329 case WM_COMMAND:
1330 case WM_NCACTIVATE:
1331 case WM_NEXTMENU:
1332 case WM_SETFOCUS:
1333 case WM_SIZE:
1334 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1337 return DefWindowProcA(hwnd, message, wParam, lParam);
1341 /***********************************************************************
1342 * DefFrameProcW (USER32.@)
1344 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1345 UINT message, WPARAM wParam, LPARAM lParam)
1347 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1349 TRACE("%p %p %04x (%s) %08lx %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1351 if (ci)
1353 switch (message)
1355 case WM_COMMAND:
1357 WORD id = LOWORD(wParam);
1358 /* check for possible syscommands for maximized MDI child */
1359 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1361 if( (id - 0xf000) & 0xf00f ) break;
1362 if( !ci->hwndChildMaximized ) break;
1363 switch( id )
1365 case SC_CLOSE:
1366 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1367 case SC_SIZE:
1368 case SC_MOVE:
1369 case SC_MINIMIZE:
1370 case SC_MAXIMIZE:
1371 case SC_NEXTWINDOW:
1372 case SC_PREVWINDOW:
1373 case SC_RESTORE:
1374 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1375 wParam, lParam);
1378 else
1380 HWND childHwnd;
1381 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1382 /* User chose "More Windows..." */
1383 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1384 else
1385 /* User chose one of the windows listed in the "Windows" menu */
1386 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1388 if( childHwnd )
1389 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1392 break;
1394 case WM_NCACTIVATE:
1395 SendMessageW(hwndMDIClient, message, wParam, lParam);
1396 break;
1398 case WM_SETTEXT:
1399 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1400 return 1; /* success. FIXME: check text length */
1402 case WM_SETFOCUS:
1403 SetFocus(hwndMDIClient);
1404 break;
1406 case WM_SIZE:
1407 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1408 break;
1410 case WM_NEXTMENU:
1412 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1414 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1416 /* control menu is between the frame system menu and
1417 * the first entry of menu bar */
1418 WND *wndPtr = WIN_GetPtr(hwnd);
1420 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1421 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1423 WIN_ReleasePtr(wndPtr);
1424 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1425 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1426 next_menu->hwndNext = ci->hwndActiveChild;
1428 WIN_ReleasePtr(wndPtr);
1430 return 0;
1435 return DefWindowProcW( hwnd, message, wParam, lParam );
1438 /***********************************************************************
1439 * DefMDIChildProcA (USER32.@)
1441 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1442 WPARAM wParam, LPARAM lParam )
1444 HWND client = GetParent(hwnd);
1445 MDICLIENTINFO *ci = get_client_info( client );
1447 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1449 hwnd = WIN_GetFullHandle( hwnd );
1450 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1452 switch (message)
1454 case WM_SETTEXT:
1455 DefWindowProcA(hwnd, message, wParam, lParam);
1456 if( ci->hwndChildMaximized == hwnd )
1457 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1458 return 1; /* success. FIXME: check text length */
1460 case WM_GETMINMAXINFO:
1461 case WM_MENUCHAR:
1462 case WM_CLOSE:
1463 case WM_SETFOCUS:
1464 case WM_CHILDACTIVATE:
1465 case WM_SYSCOMMAND:
1466 case WM_SHOWWINDOW:
1467 case WM_SETVISIBLE:
1468 case WM_SIZE:
1469 case WM_NEXTMENU:
1470 case WM_SYSCHAR:
1471 case WM_DESTROY:
1472 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1474 return DefWindowProcA(hwnd, message, wParam, lParam);
1478 /***********************************************************************
1479 * DefMDIChildProcW (USER32.@)
1481 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1482 WPARAM wParam, LPARAM lParam )
1484 HWND client = GetParent(hwnd);
1485 MDICLIENTINFO *ci = get_client_info( client );
1487 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1489 hwnd = WIN_GetFullHandle( hwnd );
1490 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1492 switch (message)
1494 case WM_SETTEXT:
1495 DefWindowProcW(hwnd, message, wParam, lParam);
1496 if( ci->hwndChildMaximized == hwnd )
1497 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1498 return 1; /* success. FIXME: check text length */
1500 case WM_GETMINMAXINFO:
1501 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1502 return 0;
1504 case WM_MENUCHAR:
1505 return 0x00010000; /* MDI children don't have menu bars */
1507 case WM_CLOSE:
1508 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1509 return 0;
1511 case WM_SETFOCUS:
1512 if (ci->hwndActiveChild != hwnd)
1513 MDI_ChildActivate( client, hwnd );
1514 break;
1516 case WM_CHILDACTIVATE:
1517 MDI_ChildActivate( client, hwnd );
1518 return 0;
1520 case WM_SYSCOMMAND:
1521 switch (wParam & 0xfff0)
1523 case SC_MOVE:
1524 if( ci->hwndChildMaximized == hwnd )
1525 return 0;
1526 break;
1527 case SC_RESTORE:
1528 case SC_MINIMIZE:
1529 break;
1530 case SC_MAXIMIZE:
1531 if (ci->hwndChildMaximized == hwnd)
1532 return SendMessageW( GetParent(client), message, wParam, lParam);
1533 break;
1534 case SC_NEXTWINDOW:
1535 SendMessageW( client, WM_MDINEXT, 0, 0);
1536 return 0;
1537 case SC_PREVWINDOW:
1538 SendMessageW( client, WM_MDINEXT, 0, 1);
1539 return 0;
1541 break;
1543 case WM_SHOWWINDOW:
1544 case WM_SETVISIBLE:
1545 if (ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1546 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1547 break;
1549 case WM_SIZE:
1550 /* This is the only place where we switch to/from maximized state */
1551 /* do not change */
1552 TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1554 if( ci->hwndChildMaximized == hwnd && wParam != SIZE_MAXIMIZED )
1556 HWND frame;
1558 ci->hwndChildMaximized = 0;
1560 frame = GetParent(client);
1561 MDI_RestoreFrameMenu( frame, hwnd );
1562 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1565 if( wParam == SIZE_MAXIMIZED )
1567 HWND frame, hMaxChild = ci->hwndChildMaximized;
1569 if( hMaxChild == hwnd ) break;
1571 if( hMaxChild)
1573 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1575 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1576 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1578 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1581 TRACE("maximizing child %p\n", hwnd );
1583 /* keep track of the maximized window. */
1584 ci->hwndChildMaximized = hwnd; /* !!! */
1586 frame = GetParent(client);
1587 MDI_AugmentFrameMenu( frame, hwnd );
1588 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1591 if( wParam == SIZE_MINIMIZED )
1593 HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1595 if (!switchTo) switchTo = hwnd;
1596 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1599 MDI_PostUpdate(client, ci, SB_BOTH+1);
1600 break;
1602 case WM_NEXTMENU:
1604 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1605 HWND parent = GetParent(client);
1607 if( wParam == VK_LEFT ) /* switch to frame system menu */
1609 WND *wndPtr = WIN_GetPtr( parent );
1610 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1611 WIN_ReleasePtr( wndPtr );
1613 if( wParam == VK_RIGHT ) /* to frame menu bar */
1615 next_menu->hmenuNext = GetMenu(parent);
1617 next_menu->hwndNext = parent;
1618 return 0;
1621 case WM_SYSCHAR:
1622 if (wParam == '-')
1624 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1625 return 0;
1627 break;
1629 case WM_DESTROY:
1630 /* Remove itself from the Window menu */
1631 MDI_RefreshMenu(ci);
1632 break;
1634 return DefWindowProcW(hwnd, message, wParam, lParam);
1637 /**********************************************************************
1638 * CreateMDIWindowA (USER32.@) Creates a MDI child
1640 * RETURNS
1641 * Success: Handle to created window
1642 * Failure: NULL
1644 HWND WINAPI CreateMDIWindowA(
1645 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1646 LPCSTR lpWindowName, /* [in] Pointer to window name */
1647 DWORD dwStyle, /* [in] Window style */
1648 INT X, /* [in] Horizontal position of window */
1649 INT Y, /* [in] Vertical position of window */
1650 INT nWidth, /* [in] Width of window */
1651 INT nHeight, /* [in] Height of window */
1652 HWND hWndParent, /* [in] Handle to parent window */
1653 HINSTANCE hInstance, /* [in] Handle to application instance */
1654 LPARAM lParam) /* [in] Application-defined value */
1656 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1657 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1658 nWidth,nHeight,hWndParent,hInstance,lParam);
1660 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1661 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1662 0, hInstance, (LPVOID)lParam);
1665 /***********************************************************************
1666 * CreateMDIWindowW (USER32.@) Creates a MDI child
1668 * RETURNS
1669 * Success: Handle to created window
1670 * Failure: NULL
1672 HWND WINAPI CreateMDIWindowW(
1673 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1674 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1675 DWORD dwStyle, /* [in] Window style */
1676 INT X, /* [in] Horizontal position of window */
1677 INT Y, /* [in] Vertical position of window */
1678 INT nWidth, /* [in] Width of window */
1679 INT nHeight, /* [in] Height of window */
1680 HWND hWndParent, /* [in] Handle to parent window */
1681 HINSTANCE hInstance, /* [in] Handle to application instance */
1682 LPARAM lParam) /* [in] Application-defined value */
1684 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1685 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1686 nWidth, nHeight, hWndParent, hInstance, lParam);
1688 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1689 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1690 0, hInstance, (LPVOID)lParam);
1693 /**********************************************************************
1694 * TranslateMDISysAccel (USER32.@)
1696 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1698 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1700 MDICLIENTINFO *ci = get_client_info( hwndClient );
1701 WPARAM wParam = 0;
1703 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1705 /* translate if the Ctrl key is down and Alt not. */
1707 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1709 switch( msg->wParam )
1711 case VK_F6:
1712 case VK_TAB:
1713 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1714 break;
1715 case VK_F4:
1716 case VK_RBUTTON:
1717 if (is_close_enabled(ci->hwndActiveChild, 0))
1719 wParam = SC_CLOSE;
1720 break;
1722 /* fall through */
1723 default:
1724 return 0;
1726 TRACE("wParam = %04lx\n", wParam);
1727 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1728 return 1;
1731 return 0; /* failure */
1734 /***********************************************************************
1735 * CalcChildScroll (USER32.@)
1737 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1739 SCROLLINFO info;
1740 RECT childRect, clientRect;
1741 HWND *list;
1743 GetClientRect( hwnd, &clientRect );
1744 SetRectEmpty( &childRect );
1746 if ((list = WIN_ListChildren( hwnd )))
1748 int i;
1749 for (i = 0; list[i]; i++)
1751 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1752 if (style & WS_MAXIMIZE)
1754 HeapFree( GetProcessHeap(), 0, list );
1755 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1756 return;
1758 if (style & WS_VISIBLE)
1760 RECT rect;
1761 GetWindowRect( list[i], &rect );
1762 UnionRect( &childRect, &rect, &childRect );
1765 HeapFree( GetProcessHeap(), 0, list );
1767 MapWindowPoints( 0, hwnd, (POINT *)&childRect, 2 );
1768 UnionRect( &childRect, &clientRect, &childRect );
1770 /* set common info values */
1771 info.cbSize = sizeof(info);
1772 info.fMask = SIF_POS | SIF_RANGE;
1774 /* set the specific */
1775 switch( scroll )
1777 case SB_BOTH:
1778 case SB_HORZ:
1779 info.nMin = childRect.left;
1780 info.nMax = childRect.right - clientRect.right;
1781 info.nPos = clientRect.left - childRect.left;
1782 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1783 if (scroll == SB_HORZ) break;
1784 /* fall through */
1785 case SB_VERT:
1786 info.nMin = childRect.top;
1787 info.nMax = childRect.bottom - clientRect.bottom;
1788 info.nPos = clientRect.top - childRect.top;
1789 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1790 break;
1795 /***********************************************************************
1796 * ScrollChildren (USER32.@)
1798 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1799 LPARAM lParam)
1801 INT newPos = -1;
1802 INT curPos, length, minPos, maxPos, shift;
1803 RECT rect;
1805 GetClientRect( hWnd, &rect );
1807 switch(uMsg)
1809 case WM_HSCROLL:
1810 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1811 curPos = GetScrollPos(hWnd,SB_HORZ);
1812 length = (rect.right - rect.left) / 2;
1813 shift = GetSystemMetrics(SM_CYHSCROLL);
1814 break;
1815 case WM_VSCROLL:
1816 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1817 curPos = GetScrollPos(hWnd,SB_VERT);
1818 length = (rect.bottom - rect.top) / 2;
1819 shift = GetSystemMetrics(SM_CXVSCROLL);
1820 break;
1821 default:
1822 return;
1825 switch( wParam )
1827 case SB_LINEUP:
1828 newPos = curPos - shift;
1829 break;
1830 case SB_LINEDOWN:
1831 newPos = curPos + shift;
1832 break;
1833 case SB_PAGEUP:
1834 newPos = curPos - length;
1835 break;
1836 case SB_PAGEDOWN:
1837 newPos = curPos + length;
1838 break;
1840 case SB_THUMBPOSITION:
1841 newPos = LOWORD(lParam);
1842 break;
1844 case SB_THUMBTRACK:
1845 return;
1847 case SB_TOP:
1848 newPos = minPos;
1849 break;
1850 case SB_BOTTOM:
1851 newPos = maxPos;
1852 break;
1853 case SB_ENDSCROLL:
1854 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1855 return;
1858 if( newPos > maxPos )
1859 newPos = maxPos;
1860 else
1861 if( newPos < minPos )
1862 newPos = minPos;
1864 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1866 if( uMsg == WM_VSCROLL )
1867 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1868 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1869 else
1870 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1871 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1875 /******************************************************************************
1876 * CascadeWindows (USER32.@) Cascades MDI child windows
1878 * RETURNS
1879 * Success: Number of cascaded windows.
1880 * Failure: 0
1882 WORD WINAPI
1883 CascadeWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1884 UINT cKids, const HWND *lpKids)
1886 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1887 return 0;
1891 /******************************************************************************
1892 * TileWindows (USER32.@) Tiles MDI child windows
1894 * RETURNS
1895 * Success: Number of tiled windows.
1896 * Failure: 0
1898 WORD WINAPI
1899 TileWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1900 UINT cKids, const HWND *lpKids)
1902 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1903 return 0;
1906 /************************************************************************
1907 * "More Windows..." functionality
1910 /* MDI_MoreWindowsDlgProc
1912 * This function will process the messages sent to the "More Windows..."
1913 * dialog.
1914 * Return values: 0 = cancel pressed
1915 * HWND = ok pressed or double-click in the list...
1919 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1921 switch (iMsg)
1923 case WM_INITDIALOG:
1925 UINT widest = 0;
1926 UINT length;
1927 UINT i;
1928 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1929 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1931 for (i = 0; i < ci->nActiveChildren; i++)
1933 WCHAR buffer[MDI_MAXTITLELENGTH];
1935 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1936 continue;
1937 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1938 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1939 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1940 if (length > widest)
1941 widest = length;
1943 /* Make sure the horizontal scrollbar scrolls ok */
1944 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1946 /* Set the current selection */
1947 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1948 return TRUE;
1951 case WM_COMMAND:
1952 switch (LOWORD(wParam))
1954 default:
1955 if (HIWORD(wParam) != LBN_DBLCLK) break;
1956 /* fall through */
1957 case IDOK:
1959 /* windows are sorted by menu ID, so we must return the
1960 * window associated to the given id
1962 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1963 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1964 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1965 EndDialog(hDlg, res);
1966 return TRUE;
1968 case IDCANCEL:
1969 EndDialog(hDlg, 0);
1970 return TRUE;
1972 break;
1974 return FALSE;
1979 * MDI_MoreWindowsDialog
1981 * Prompts the user with a listbox containing the opened
1982 * documents. The user can then choose a windows and click
1983 * on OK to set the current window to the one selected, or
1984 * CANCEL to cancel. The function returns a handle to the
1985 * selected window.
1988 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1990 LPCVOID template;
1991 HRSRC hRes;
1992 HANDLE hDlgTmpl;
1994 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1996 if (hRes == 0)
1997 return 0;
1999 hDlgTmpl = LoadResource(user32_module, hRes );
2001 if (hDlgTmpl == 0)
2002 return 0;
2004 template = LockResource( hDlgTmpl );
2006 if (template == 0)
2007 return 0;
2009 return (HWND) DialogBoxIndirectParamA(user32_module,
2010 (const DLGTEMPLATE*) template,
2011 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);