gphoto2.ds: Set supported groups.
[wine.git] / dlls / user32 / mdi.c
blobea362585a67112505461abda936b923870df85b5
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 0x003f /* 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 LONG reserved;
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);
155 /* -------- Miscellaneous service functions ----------
157 * MDI_GetChildByID
159 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
161 int i;
163 for (i = 0; ci->nActiveChildren; i++)
165 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
166 return ci->child[i];
168 return 0;
171 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
173 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
175 ci->mdiFlags |= MDIF_NEEDUPDATE;
176 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
178 ci->sbRecalc = recalc;
182 /*********************************************************************
183 * MDIClient class descriptor
185 static const WCHAR mdiclientW[] = {'M','D','I','C','l','i','e','n','t',0};
186 const struct builtin_class_descr MDICLIENT_builtin_class =
188 mdiclientW, /* name */
189 0, /* style */
190 WINPROC_MDICLIENT, /* proc */
191 sizeof(MDICLIENTINFO), /* extra */
192 IDC_ARROW, /* cursor */
193 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
197 static MDICLIENTINFO *get_client_info( HWND client )
199 MDICLIENTINFO *ret = NULL;
200 WND *win = WIN_GetPtr( client );
201 if (win)
203 if (win == WND_OTHER_PROCESS || win == WND_DESKTOP)
205 if (IsWindow(client)) WARN( "client %p belongs to other process\n", client );
206 return NULL;
208 if (win->flags & WIN_ISMDICLIENT)
209 ret = (MDICLIENTINFO *)win->wExtra;
210 else
211 WARN( "%p is not an MDI client\n", client );
212 WIN_ReleasePtr( win );
214 return ret;
217 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
219 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
221 if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
222 if (hSysMenu)
224 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
225 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
226 return FALSE;
228 return TRUE;
231 /**********************************************************************
232 * MDI_GetWindow
234 * returns "activatable" child different from the current or zero
236 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
237 DWORD dwStyleMask )
239 int i;
240 HWND *list;
241 HWND last = 0;
243 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
244 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
246 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
247 i = 0;
248 /* start from next after hWnd */
249 while (list[i] && list[i] != hWnd) i++;
250 if (list[i]) i++;
252 for ( ; list[i]; i++)
254 if (GetWindow( list[i], GW_OWNER )) continue;
255 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
256 last = list[i];
257 if (bNext) goto found;
259 /* now restart from the beginning */
260 for (i = 0; list[i] && list[i] != hWnd; i++)
262 if (GetWindow( list[i], GW_OWNER )) continue;
263 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
264 last = list[i];
265 if (bNext) goto found;
267 found:
268 HeapFree( GetProcessHeap(), 0, list );
269 return last;
272 /**********************************************************************
273 * MDI_CalcDefaultChildPos
275 * It seems that the default height is about 2/3 of the client rect
277 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
279 INT nstagger;
280 RECT rect;
281 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
283 if (total < 0) /* we are called from CreateWindow */
285 MDICLIENTINFO *ci = get_client_info(hwndClient);
286 total = ci->nTotalCreated;
287 *id = ci->idFirstChild + ci->nActiveChildren;
288 TRACE("MDI child id %04x\n", *id);
291 GetClientRect( hwndClient, &rect );
292 if( rect.bottom - rect.top - delta >= spacing )
293 rect.bottom -= delta;
295 nstagger = (rect.bottom - rect.top)/(3 * spacing);
296 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
297 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
298 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
301 /**********************************************************************
302 * MDISetMenu
304 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
305 HMENU hmenuWindow)
307 MDICLIENTINFO *ci;
308 HWND hwndFrame = GetParent(hwnd);
310 TRACE("%p, frame menu %p, window menu %p\n", hwnd, hmenuFrame, hmenuWindow);
312 if (hmenuFrame && !IsMenu(hmenuFrame))
314 WARN("hmenuFrame is not a menu handle\n");
315 return 0L;
318 if (hmenuWindow && !IsMenu(hmenuWindow))
320 WARN("hmenuWindow is not a menu handle\n");
321 return 0L;
324 if (!(ci = get_client_info( hwnd ))) return 0;
326 TRACE("old frame menu %p, old window menu %p\n", ci->hFrameMenu, ci->hWindowMenu);
328 if (hmenuFrame)
330 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
332 if (ci->hwndChildMaximized)
333 MDI_RestoreFrameMenu( hwndFrame, ci->hwndChildMaximized );
336 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
338 /* delete menu items from ci->hWindowMenu
339 * and add them to hmenuWindow */
340 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
341 if( ci->hWindowMenu && ci->nActiveChildren )
343 UINT nActiveChildren_old = ci->nActiveChildren;
345 /* Remove all items from old Window menu */
346 ci->nActiveChildren = 0;
347 MDI_RefreshMenu(ci);
349 ci->hWindowMenu = hmenuWindow;
351 /* Add items to the new Window menu */
352 ci->nActiveChildren = nActiveChildren_old;
353 MDI_RefreshMenu(ci);
355 else
356 ci->hWindowMenu = hmenuWindow;
359 if (hmenuFrame)
361 SetMenu(hwndFrame, hmenuFrame);
362 if( hmenuFrame != ci->hFrameMenu )
364 HMENU oldFrameMenu = ci->hFrameMenu;
366 ci->hFrameMenu = hmenuFrame;
367 if (ci->hwndChildMaximized)
368 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
370 return (LRESULT)oldFrameMenu;
374 return 0;
377 /**********************************************************************
378 * MDIRefreshMenu
380 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
382 UINT i, count, visible, id;
383 WCHAR buf[MDI_MAXTITLELENGTH];
385 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
387 if (!ci->hWindowMenu)
388 return 0;
390 if (!IsMenu(ci->hWindowMenu))
392 WARN("Window menu handle %p is no longer valid\n", ci->hWindowMenu);
393 return 0;
396 /* Windows finds the last separator in the menu, and if after it
397 * there is a menu item with MDI magic ID removes all existing
398 * menu items after it, and then adds visible MDI children.
400 count = GetMenuItemCount(ci->hWindowMenu);
401 for (i = 0; i < count; i++)
403 MENUITEMINFOW mii;
405 memset(&mii, 0, sizeof(mii));
406 mii.cbSize = sizeof(mii);
407 mii.fMask = MIIM_TYPE;
408 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
410 if (mii.fType & MF_SEPARATOR)
412 /* Windows checks only ID of the menu item */
413 memset(&mii, 0, sizeof(mii));
414 mii.cbSize = sizeof(mii);
415 mii.fMask = MIIM_ID;
416 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
418 if (mii.wID == ci->idFirstChild)
420 TRACE("removing %u items including separator\n", count - i);
421 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
422 /* nothing */;
424 break;
431 visible = 0;
432 for (i = 0; i < ci->nActiveChildren; i++)
434 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
436 id = ci->idFirstChild + visible;
438 if (visible == MDI_MOREWINDOWSLIMIT)
440 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
441 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
442 break;
445 if (!visible)
446 /* Visio expects that separator has id 0 */
447 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
449 visible++;
451 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
453 buf[0] = '&';
454 buf[1] = '0' + visible;
455 buf[2] = ' ';
456 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
457 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
458 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
460 if (ci->child[i] == ci->hwndActiveChild)
461 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
463 else
464 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
467 return (LRESULT)ci->hFrameMenu;
471 /* ------------------ MDI child window functions ---------------------- */
473 /**********************************************************************
474 * MDI_ChildGetMinMaxInfo
476 * Note: The rule here is that client rect of the maximized MDI child
477 * is equal to the client rect of the MDI client window.
479 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
481 RECT rect;
483 GetClientRect( client, &rect );
484 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
485 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
487 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
488 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
490 lpMinMax->ptMaxPosition.x = rect.left;
491 lpMinMax->ptMaxPosition.y = rect.top;
493 TRACE("max rect %s\n", wine_dbgstr_rect(&rect));
496 /**********************************************************************
497 * MDI_SwitchActiveChild
499 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
500 * being activated
502 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
504 HWND hwndPrev;
506 hwndPrev = ci->hwndActiveChild;
508 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
510 if ( hwndTo != hwndPrev )
512 BOOL was_zoomed = IsZoomed(hwndPrev);
514 if (was_zoomed)
516 /* restore old MDI child */
517 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
518 ShowWindow( hwndPrev, SW_RESTORE );
519 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
521 /* activate new MDI child */
522 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
523 /* maximize new MDI child */
524 ShowWindow( hwndTo, SW_MAXIMIZE );
526 /* activate new MDI child */
527 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
532 /**********************************************************************
533 * MDIDestroyChild
535 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
536 HWND child, BOOL flagDestroy )
538 UINT i;
540 TRACE("# of managed children %u\n", ci->nActiveChildren);
542 if( child == ci->hwndActiveChild )
544 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
545 /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
546 if (flagDestroy && next)
547 MDI_SwitchActiveChild(ci, next, TRUE);
548 else
550 ShowWindow(child, SW_HIDE);
551 if (child == ci->hwndChildMaximized)
553 HWND frame = GetParent(client);
554 MDI_RestoreFrameMenu(frame, child);
555 ci->hwndChildMaximized = 0;
556 MDI_UpdateFrameText(frame, client, TRUE, NULL);
558 if (flagDestroy)
559 MDI_ChildActivate(client, 0);
563 for (i = 0; i < ci->nActiveChildren; i++)
565 if (ci->child[i] == child)
567 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
568 memcpy(new_child, ci->child, i * sizeof(HWND));
569 if (i + 1 < ci->nActiveChildren)
570 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
571 HeapFree(GetProcessHeap(), 0, ci->child);
572 ci->child = new_child;
574 ci->nActiveChildren--;
575 break;
579 if (flagDestroy)
581 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
582 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
583 DestroyWindow(child);
586 TRACE("child destroyed - %p\n", child);
587 return 0;
591 /**********************************************************************
592 * MDI_ChildActivate
594 * Called in response to WM_CHILDACTIVATE, or when last MDI child
595 * is being deactivated.
597 static LONG MDI_ChildActivate( HWND client, HWND child )
599 MDICLIENTINFO *clientInfo;
600 HWND prevActiveWnd, frame;
601 BOOL isActiveFrameWnd;
603 clientInfo = get_client_info( client );
605 if (clientInfo->hwndActiveChild == child) return 0;
607 TRACE("%p\n", child);
609 frame = GetParent(client);
610 isActiveFrameWnd = (GetActiveWindow() == frame);
611 prevActiveWnd = clientInfo->hwndActiveChild;
613 /* deactivate prev. active child */
614 if(prevActiveWnd)
616 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
617 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
620 MDI_SwitchActiveChild( clientInfo, child, FALSE );
621 clientInfo->hwndActiveChild = child;
623 MDI_RefreshMenu(clientInfo);
625 if( isActiveFrameWnd )
627 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
628 /* Let the client window manage focus for children, but if the focus
629 * is already on the client (for instance this is the 1st child) then
630 * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
631 * manually in this case.
633 if (SetFocus(client) == client)
634 SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
637 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
638 return TRUE;
641 /* -------------------- MDI client window functions ------------------- */
643 /**********************************************************************
644 * CreateMDIMenuBitmap
646 static HBITMAP CreateMDIMenuBitmap(void)
648 HDC hDCSrc = CreateCompatibleDC(0);
649 HDC hDCDest = CreateCompatibleDC(hDCSrc);
650 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
651 HBITMAP hbCopy;
652 HBITMAP hobjSrc, hobjDest;
654 hobjSrc = SelectObject(hDCSrc, hbClose);
655 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
656 hobjDest = SelectObject(hDCDest, hbCopy);
658 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
659 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
661 SelectObject(hDCSrc, hobjSrc);
662 DeleteObject(hbClose);
663 DeleteDC(hDCSrc);
665 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
667 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
668 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
670 SelectObject(hDCDest, hobjSrc );
671 SelectObject(hDCDest, hobjDest);
672 DeleteDC(hDCDest);
674 return hbCopy;
677 /**********************************************************************
678 * MDICascade
680 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
682 HWND *win_array;
683 BOOL has_icons = FALSE;
684 int i, total;
686 if (ci->hwndChildMaximized)
687 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
689 if (ci->nActiveChildren == 0) return 0;
691 if (!(win_array = WIN_ListChildren( client ))) return 0;
693 /* remove all the windows we don't want */
694 for (i = total = 0; win_array[i]; i++)
696 if (!IsWindowVisible( win_array[i] )) continue;
697 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
698 if (IsIconic( win_array[i] ))
700 has_icons = TRUE;
701 continue;
703 win_array[total++] = win_array[i];
705 win_array[total] = 0;
707 if (total)
709 INT delta = 0, n = 0, i;
710 POINT pos[2];
711 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
713 /* walk the list (backwards) and move windows */
714 for (i = total - 1; i >= 0; i--)
716 LONG style;
717 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
719 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
720 TRACE("move %p to (%d,%d) size [%d,%d]\n",
721 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
722 style = GetWindowLongW(win_array[i], GWL_STYLE);
724 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
725 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
726 posOptions);
729 HeapFree( GetProcessHeap(), 0, win_array );
731 if (has_icons) ArrangeIconicWindows( client );
732 return 0;
735 /**********************************************************************
736 * MDITile
738 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
740 HWND *win_array;
741 int i, total;
742 BOOL has_icons = FALSE;
744 if (ci->hwndChildMaximized)
745 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
747 if (ci->nActiveChildren == 0) return;
749 if (!(win_array = WIN_ListChildren( client ))) return;
751 /* remove all the windows we don't want */
752 for (i = total = 0; win_array[i]; i++)
754 if (!IsWindowVisible( win_array[i] )) continue;
755 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
756 if (IsIconic( win_array[i] ))
758 has_icons = TRUE;
759 continue;
761 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
762 win_array[total++] = win_array[i];
764 win_array[total] = 0;
766 TRACE("%u windows to tile\n", total);
768 if (total)
770 HWND *pWnd = win_array;
771 RECT rect;
772 int x, y, xsize, ysize;
773 int rows, columns, r, c, i;
775 GetClientRect(client,&rect);
776 rows = (int) sqrt((double)total);
777 columns = total / rows;
779 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
781 i = rows;
782 rows = columns; /* exchange r and c */
783 columns = i;
786 if (has_icons)
788 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
789 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
792 ysize = rect.bottom / rows;
793 xsize = rect.right / columns;
795 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
797 if (c == columns)
799 rows = total - i;
800 ysize = rect.bottom / rows;
803 y = 0;
804 for (r = 1; r <= rows && *pWnd; r++, i++)
806 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
807 LONG style = GetWindowLongW(win_array[i], GWL_STYLE);
808 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
810 SetWindowPos(*pWnd, 0, x, y, xsize, ysize, posOptions);
811 y += ysize;
812 pWnd++;
814 x += xsize;
817 HeapFree( GetProcessHeap(), 0, win_array );
818 if (has_icons) ArrangeIconicWindows( client );
821 /* ----------------------- Frame window ---------------------------- */
824 /**********************************************************************
825 * MDI_AugmentFrameMenu
827 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
829 HMENU menu = GetMenu( frame );
830 HMENU hSysPopup;
831 HBITMAP hSysMenuBitmap = 0;
832 HICON hIcon;
834 TRACE("frame %p,child %p\n",frame,hChild);
836 if (!menu) return FALSE;
838 /* create a copy of sysmenu popup and insert it into frame menu bar */
839 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
841 TRACE("child %p doesn't have a system menu\n", hChild);
842 return FALSE;
845 AppendMenuW(menu, MF_HELP | MF_BITMAP,
846 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
847 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
848 AppendMenuW(menu, MF_HELP | MF_BITMAP,
849 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
850 AppendMenuW(menu, MF_HELP | MF_BITMAP,
851 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
853 /* The system menu is replaced by the child icon */
854 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_SMALL, 0);
855 if (!hIcon)
856 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICONSM);
857 if (!hIcon)
858 hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_BIG, 0);
859 if (!hIcon)
860 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICON);
861 if (!hIcon)
862 hIcon = LoadImageW(0, (LPWSTR)IDI_WINLOGO, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
863 GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
864 if (hIcon)
866 HDC hMemDC;
867 HBITMAP hBitmap, hOldBitmap;
868 HBRUSH hBrush;
869 HDC hdc = GetDC(hChild);
871 if (hdc)
873 int cx, cy;
874 cx = GetSystemMetrics(SM_CXSMICON);
875 cy = GetSystemMetrics(SM_CYSMICON);
876 hMemDC = CreateCompatibleDC(hdc);
877 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
878 hOldBitmap = SelectObject(hMemDC, hBitmap);
879 SetMapMode(hMemDC, MM_TEXT);
880 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
881 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
882 SelectObject (hMemDC, hOldBitmap);
883 DeleteObject(hBrush);
884 DeleteDC(hMemDC);
885 ReleaseDC(hChild, hdc);
886 hSysMenuBitmap = hBitmap;
890 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
891 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
893 TRACE("not inserted\n");
894 DestroyMenu(hSysPopup);
895 return FALSE;
898 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
899 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
900 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
901 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
903 /* redraw menu */
904 DrawMenuBar(frame);
906 return TRUE;
909 /**********************************************************************
910 * MDI_RestoreFrameMenu
912 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
914 MENUITEMINFOW menuInfo;
915 HMENU menu = GetMenu( frame );
916 INT nItems;
917 UINT iId;
919 TRACE("frame %p, child %p\n", frame, hChild);
921 if (!menu) return FALSE;
923 /* if there is no system buttons then nothing to do */
924 nItems = GetMenuItemCount(menu) - 1;
925 iId = GetMenuItemID(menu, nItems);
926 if ( !(iId == SC_RESTORE || iId == SC_CLOSE) )
927 return FALSE;
930 * Remove the system menu, If that menu is the icon of the window
931 * as it is in win95, we have to delete the bitmap.
933 memset(&menuInfo, 0, sizeof(menuInfo));
934 menuInfo.cbSize = sizeof(menuInfo);
935 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
937 GetMenuItemInfoW(menu,
939 TRUE,
940 &menuInfo);
942 RemoveMenu(menu,0,MF_BYPOSITION);
944 if ( (menuInfo.fType & MFT_BITMAP) &&
945 (LOWORD(menuInfo.dwTypeData)!=0) &&
946 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
948 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
951 /* close */
952 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
953 /* restore */
954 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
955 /* minimize */
956 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
958 DrawMenuBar(frame);
960 return TRUE;
964 /**********************************************************************
965 * MDI_UpdateFrameText
967 * used when child window is maximized/restored
969 * Note: lpTitle can be NULL
971 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
973 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
974 MDICLIENTINFO *ci = get_client_info( hClient );
976 TRACE("frameText %s\n", debugstr_w(lpTitle));
978 if (!ci) return;
980 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
982 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
983 lpTitle = lpBuffer;
986 /* store new "default" title if lpTitle is not NULL */
987 if (lpTitle)
989 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
990 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
991 strcpyW( ci->frameTitle, lpTitle );
994 if (ci->frameTitle)
996 if (ci->hwndChildMaximized)
998 /* combine frame title and child title if possible */
1000 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
1001 static const WCHAR lpBracket2[] = {']',0};
1002 int i_frame_text_length = strlenW(ci->frameTitle);
1004 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1006 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1008 strcatW( lpBuffer, lpBracket );
1009 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
1010 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1011 strcatW( lpBuffer, lpBracket2 );
1012 else
1013 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1016 else
1018 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1021 else
1022 lpBuffer[0] = '\0';
1024 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1026 if (repaint)
1027 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1028 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1032 /* ----------------------------- Interface ---------------------------- */
1035 /**********************************************************************
1036 * MDIClientWndProc_common
1038 LRESULT MDIClientWndProc_common( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL unicode )
1040 MDICLIENTINFO *ci;
1042 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1044 if (!(ci = get_client_info( hwnd )))
1046 if (message == WM_NCCREATE) win_set_flags( hwnd, WIN_ISMDICLIENT, 0 );
1047 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1048 DefWindowProcA( hwnd, message, wParam, lParam );
1051 switch (message)
1053 case WM_CREATE:
1055 /* Since we are using only cs->lpCreateParams, we can safely
1056 * cast to LPCREATESTRUCTA here */
1057 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1058 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1060 ci->hWindowMenu = ccs->hWindowMenu;
1061 ci->idFirstChild = ccs->idFirstChild;
1062 ci->hwndChildMaximized = 0;
1063 ci->child = NULL;
1064 ci->nActiveChildren = 0;
1065 ci->nTotalCreated = 0;
1066 ci->frameTitle = NULL;
1067 ci->mdiFlags = 0;
1068 ci->hFrameMenu = GetMenu(cs->hwndParent);
1070 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1072 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1073 hwnd, ci->hWindowMenu, ci->idFirstChild );
1074 return 0;
1077 case WM_DESTROY:
1079 if( ci->hwndChildMaximized )
1080 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1082 ci->nActiveChildren = 0;
1083 MDI_RefreshMenu(ci);
1085 HeapFree( GetProcessHeap(), 0, ci->child );
1086 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1088 return 0;
1091 case WM_MDIACTIVATE:
1093 if( ci->hwndActiveChild != (HWND)wParam )
1094 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1095 return 0;
1098 case WM_MDICASCADE:
1099 return MDICascade(hwnd, ci);
1101 case WM_MDICREATE:
1102 if (lParam)
1104 HWND child;
1106 if (unicode)
1108 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1109 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1110 csW->szTitle, csW->style,
1111 csW->x, csW->y, csW->cx, csW->cy,
1112 hwnd, 0, csW->hOwner,
1113 (LPVOID)csW->lParam);
1115 else
1117 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1118 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1119 csA->szTitle, csA->style,
1120 csA->x, csA->y, csA->cx, csA->cy,
1121 hwnd, 0, csA->hOwner,
1122 (LPVOID)csA->lParam);
1124 return (LRESULT)child;
1126 return 0;
1128 case WM_MDIDESTROY:
1129 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1131 case WM_MDIGETACTIVE:
1132 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1133 return (LRESULT)ci->hwndActiveChild;
1135 case WM_MDIICONARRANGE:
1136 ci->mdiFlags |= MDIF_NEEDUPDATE;
1137 ArrangeIconicWindows( hwnd );
1138 ci->sbRecalc = SB_BOTH+1;
1139 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1140 return 0;
1142 case WM_MDIMAXIMIZE:
1143 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1144 return 0;
1146 case WM_MDINEXT: /* lParam != 0 means previous window */
1148 HWND hwnd = wParam ? WIN_GetFullHandle((HWND)wParam) : ci->hwndActiveChild;
1149 HWND next = MDI_GetWindow( ci, hwnd, !lParam, 0 );
1150 MDI_SwitchActiveChild( ci, next, TRUE );
1151 if(!lParam)
1152 SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
1153 break;
1156 case WM_MDIRESTORE:
1157 ShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1158 return 0;
1160 case WM_MDISETMENU:
1161 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1163 case WM_MDIREFRESHMENU:
1164 return MDI_RefreshMenu( ci );
1166 case WM_MDITILE:
1167 ci->mdiFlags |= MDIF_NEEDUPDATE;
1168 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1169 MDITile( hwnd, ci, wParam );
1170 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1171 return 0;
1173 case WM_VSCROLL:
1174 case WM_HSCROLL:
1175 ci->mdiFlags |= MDIF_NEEDUPDATE;
1176 ScrollChildren( hwnd, message, wParam, lParam );
1177 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1178 return 0;
1180 case WM_SETFOCUS:
1181 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1182 SetFocus( ci->hwndActiveChild );
1183 return 0;
1185 case WM_NCACTIVATE:
1186 if( ci->hwndActiveChild )
1187 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1188 break;
1190 case WM_PARENTNOTIFY:
1191 switch (LOWORD(wParam))
1193 case WM_CREATE:
1194 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1196 ci->nTotalCreated++;
1197 ci->nActiveChildren++;
1199 if (!ci->child)
1200 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1201 else
1202 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1204 TRACE("Adding MDI child %p, # of children %d\n",
1205 (HWND)lParam, ci->nActiveChildren);
1207 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1209 break;
1211 case WM_LBUTTONDOWN:
1213 HWND child;
1214 POINT pt;
1215 pt.x = (short)LOWORD(lParam);
1216 pt.y = (short)HIWORD(lParam);
1217 child = ChildWindowFromPoint(hwnd, pt);
1219 TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1221 if( child && child != hwnd && child != ci->hwndActiveChild )
1222 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1223 break;
1226 case WM_DESTROY:
1227 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1229 return 0;
1231 case WM_SIZE:
1232 if( ci->hwndActiveChild && IsZoomed(ci->hwndActiveChild) )
1234 RECT rect;
1236 SetRect(&rect, 0, 0, LOWORD(lParam), HIWORD(lParam));
1237 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1238 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1239 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1240 rect.right - rect.left, rect.bottom - rect.top, 1);
1242 else
1243 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1245 break;
1247 case WM_MDICALCCHILDSCROLL:
1248 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1250 CalcChildScroll(hwnd, ci->sbRecalc-1);
1251 ci->sbRecalc = 0;
1252 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1254 return 0;
1256 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1257 DefWindowProcA( hwnd, message, wParam, lParam );
1260 /***********************************************************************
1261 * DefFrameProcA (USER32.@)
1263 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1264 UINT message, WPARAM wParam, LPARAM lParam)
1266 if (hwndMDIClient)
1268 switch (message)
1270 case WM_SETTEXT:
1272 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1273 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1274 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1275 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1276 HeapFree( GetProcessHeap(), 0, text );
1278 return 1; /* success. FIXME: check text length */
1280 case WM_COMMAND:
1281 case WM_NCACTIVATE:
1282 case WM_NEXTMENU:
1283 case WM_SETFOCUS:
1284 case WM_SIZE:
1285 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1288 return DefWindowProcA(hwnd, message, wParam, lParam);
1292 /***********************************************************************
1293 * DefFrameProcW (USER32.@)
1295 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1296 UINT message, WPARAM wParam, LPARAM lParam)
1298 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1300 TRACE("%p %p %04x (%s) %08lx %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1302 if (ci)
1304 switch (message)
1306 case WM_COMMAND:
1308 WORD id = LOWORD(wParam);
1309 /* check for possible syscommands for maximized MDI child */
1310 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1312 if( (id - 0xf000) & 0xf00f ) break;
1313 if( !ci->hwndChildMaximized ) break;
1314 switch( id )
1316 case SC_CLOSE:
1317 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1318 case SC_SIZE:
1319 case SC_MOVE:
1320 case SC_MINIMIZE:
1321 case SC_MAXIMIZE:
1322 case SC_NEXTWINDOW:
1323 case SC_PREVWINDOW:
1324 case SC_RESTORE:
1325 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1326 wParam, lParam);
1329 else
1331 HWND childHwnd;
1332 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1333 /* User chose "More Windows..." */
1334 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1335 else
1336 /* User chose one of the windows listed in the "Windows" menu */
1337 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1339 if( childHwnd )
1340 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1343 break;
1345 case WM_NCACTIVATE:
1346 SendMessageW(hwndMDIClient, message, wParam, lParam);
1347 break;
1349 case WM_SETTEXT:
1350 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1351 return 1; /* success. FIXME: check text length */
1353 case WM_SETFOCUS:
1354 SetFocus(hwndMDIClient);
1355 break;
1357 case WM_SIZE:
1358 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1359 break;
1361 case WM_NEXTMENU:
1363 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1365 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1367 /* control menu is between the frame system menu and
1368 * the first entry of menu bar */
1369 WND *wndPtr = WIN_GetPtr(hwnd);
1371 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1372 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1374 WIN_ReleasePtr(wndPtr);
1375 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1376 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1377 next_menu->hwndNext = ci->hwndActiveChild;
1379 WIN_ReleasePtr(wndPtr);
1381 return 0;
1386 return DefWindowProcW( hwnd, message, wParam, lParam );
1389 /***********************************************************************
1390 * DefMDIChildProcA (USER32.@)
1392 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1393 WPARAM wParam, LPARAM lParam )
1395 HWND client = GetParent(hwnd);
1396 MDICLIENTINFO *ci = get_client_info( client );
1398 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1400 hwnd = WIN_GetFullHandle( hwnd );
1401 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1403 switch (message)
1405 case WM_SETTEXT:
1406 DefWindowProcA(hwnd, message, wParam, lParam);
1407 if( ci->hwndChildMaximized == hwnd )
1408 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1409 return 1; /* success. FIXME: check text length */
1411 case WM_GETMINMAXINFO:
1412 case WM_MENUCHAR:
1413 case WM_CLOSE:
1414 case WM_SETFOCUS:
1415 case WM_CHILDACTIVATE:
1416 case WM_SYSCOMMAND:
1417 case WM_SHOWWINDOW:
1418 case WM_SETVISIBLE:
1419 case WM_SIZE:
1420 case WM_NEXTMENU:
1421 case WM_SYSCHAR:
1422 case WM_DESTROY:
1423 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1425 return DefWindowProcA(hwnd, message, wParam, lParam);
1429 /***********************************************************************
1430 * DefMDIChildProcW (USER32.@)
1432 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1433 WPARAM wParam, LPARAM lParam )
1435 HWND client = GetParent(hwnd);
1436 MDICLIENTINFO *ci = get_client_info( client );
1438 TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1440 hwnd = WIN_GetFullHandle( hwnd );
1441 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1443 switch (message)
1445 case WM_SETTEXT:
1446 DefWindowProcW(hwnd, message, wParam, lParam);
1447 if( ci->hwndChildMaximized == hwnd )
1448 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1449 return 1; /* success. FIXME: check text length */
1451 case WM_GETMINMAXINFO:
1452 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1453 return 0;
1455 case WM_MENUCHAR:
1456 return MAKELRESULT( 0, MNC_CLOSE ); /* MDI children don't have menu bars */
1458 case WM_CLOSE:
1459 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1460 return 0;
1462 case WM_SETFOCUS:
1463 if (ci->hwndActiveChild != hwnd)
1464 MDI_ChildActivate( client, hwnd );
1465 break;
1467 case WM_CHILDACTIVATE:
1468 if (IsWindowEnabled( hwnd ))
1469 MDI_ChildActivate( client, hwnd );
1470 return 0;
1472 case WM_SYSCOMMAND:
1473 switch (wParam & 0xfff0)
1475 case SC_MOVE:
1476 if( ci->hwndChildMaximized == hwnd )
1477 return 0;
1478 break;
1479 case SC_RESTORE:
1480 case SC_MINIMIZE:
1481 break;
1482 case SC_MAXIMIZE:
1483 if (ci->hwndChildMaximized == hwnd)
1484 return SendMessageW( GetParent(client), message, wParam, lParam);
1485 break;
1486 case SC_NEXTWINDOW:
1487 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 0);
1488 return 0;
1489 case SC_PREVWINDOW:
1490 SendMessageW( client, WM_MDINEXT, (WPARAM)ci->hwndActiveChild, 1);
1491 return 0;
1493 break;
1495 case WM_SHOWWINDOW:
1496 case WM_SETVISIBLE:
1497 if (ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1498 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1499 break;
1501 case WM_SIZE:
1502 /* This is the only place where we switch to/from maximized state */
1503 /* do not change */
1504 TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1506 if( ci->hwndChildMaximized == hwnd && wParam != SIZE_MAXIMIZED )
1508 HWND frame;
1510 ci->hwndChildMaximized = 0;
1512 frame = GetParent(client);
1513 MDI_RestoreFrameMenu( frame, hwnd );
1514 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1517 if( wParam == SIZE_MAXIMIZED )
1519 HWND frame, hMaxChild = ci->hwndChildMaximized;
1521 if( hMaxChild == hwnd ) break;
1523 if( hMaxChild)
1525 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1527 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1528 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1530 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1533 TRACE("maximizing child %p\n", hwnd );
1535 /* keep track of the maximized window. */
1536 ci->hwndChildMaximized = hwnd; /* !!! */
1538 frame = GetParent(client);
1539 MDI_AugmentFrameMenu( frame, hwnd );
1540 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1543 if( wParam == SIZE_MINIMIZED )
1545 HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1547 if (!switchTo) switchTo = hwnd;
1548 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1551 MDI_PostUpdate(client, ci, SB_BOTH+1);
1552 break;
1554 case WM_NEXTMENU:
1556 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1557 HWND parent = GetParent(client);
1559 if( wParam == VK_LEFT ) /* switch to frame system menu */
1561 WND *wndPtr = WIN_GetPtr( parent );
1562 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1563 WIN_ReleasePtr( wndPtr );
1565 if( wParam == VK_RIGHT ) /* to frame menu bar */
1567 next_menu->hmenuNext = GetMenu(parent);
1569 next_menu->hwndNext = parent;
1570 return 0;
1573 case WM_SYSCHAR:
1574 if (wParam == '-')
1576 SendMessageW( hwnd, WM_SYSCOMMAND, SC_KEYMENU, VK_SPACE);
1577 return 0;
1579 break;
1581 case WM_DESTROY:
1582 /* Remove itself from the Window menu */
1583 MDI_RefreshMenu(ci);
1584 break;
1586 return DefWindowProcW(hwnd, message, wParam, lParam);
1589 /**********************************************************************
1590 * CreateMDIWindowA (USER32.@) Creates a MDI child
1592 * RETURNS
1593 * Success: Handle to created window
1594 * Failure: NULL
1596 HWND WINAPI CreateMDIWindowA(
1597 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1598 LPCSTR lpWindowName, /* [in] Pointer to window name */
1599 DWORD dwStyle, /* [in] Window style */
1600 INT X, /* [in] Horizontal position of window */
1601 INT Y, /* [in] Vertical position of window */
1602 INT nWidth, /* [in] Width of window */
1603 INT nHeight, /* [in] Height of window */
1604 HWND hWndParent, /* [in] Handle to parent window */
1605 HINSTANCE hInstance, /* [in] Handle to application instance */
1606 LPARAM lParam) /* [in] Application-defined value */
1608 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1609 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1610 nWidth,nHeight,hWndParent,hInstance,lParam);
1612 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1613 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1614 0, hInstance, (LPVOID)lParam);
1617 /***********************************************************************
1618 * CreateMDIWindowW (USER32.@) Creates a MDI child
1620 * RETURNS
1621 * Success: Handle to created window
1622 * Failure: NULL
1624 HWND WINAPI CreateMDIWindowW(
1625 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1626 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1627 DWORD dwStyle, /* [in] Window style */
1628 INT X, /* [in] Horizontal position of window */
1629 INT Y, /* [in] Vertical position of window */
1630 INT nWidth, /* [in] Width of window */
1631 INT nHeight, /* [in] Height of window */
1632 HWND hWndParent, /* [in] Handle to parent window */
1633 HINSTANCE hInstance, /* [in] Handle to application instance */
1634 LPARAM lParam) /* [in] Application-defined value */
1636 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1637 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1638 nWidth, nHeight, hWndParent, hInstance, lParam);
1640 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1641 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1642 0, hInstance, (LPVOID)lParam);
1645 /**********************************************************************
1646 * TranslateMDISysAccel (USER32.@)
1648 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1650 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1652 MDICLIENTINFO *ci = get_client_info( hwndClient );
1653 WPARAM wParam = 0;
1655 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return FALSE;
1657 /* translate if the Ctrl key is down and Alt not. */
1659 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1661 switch( msg->wParam )
1663 case VK_F6:
1664 case VK_TAB:
1665 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1666 break;
1667 case VK_F4:
1668 case VK_RBUTTON:
1669 if (is_close_enabled(ci->hwndActiveChild, 0))
1671 wParam = SC_CLOSE;
1672 break;
1674 /* fall through */
1675 default:
1676 return FALSE;
1678 TRACE("wParam = %04lx\n", wParam);
1679 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, msg->wParam);
1680 return TRUE;
1683 return FALSE; /* failure */
1686 /***********************************************************************
1687 * CalcChildScroll (USER32.@)
1689 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1691 SCROLLINFO info;
1692 RECT childRect, clientRect;
1693 HWND *list;
1694 DWORD style;
1696 GetClientRect( hwnd, &clientRect );
1697 SetRectEmpty( &childRect );
1699 if ((list = WIN_ListChildren( hwnd )))
1701 int i;
1702 for (i = 0; list[i]; i++)
1704 style = GetWindowLongW( list[i], GWL_STYLE );
1705 if (style & WS_MAXIMIZE)
1707 HeapFree( GetProcessHeap(), 0, list );
1708 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1709 return;
1711 if (style & WS_VISIBLE)
1713 RECT rect;
1714 WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL );
1715 UnionRect( &childRect, &rect, &childRect );
1718 HeapFree( GetProcessHeap(), 0, list );
1720 UnionRect( &childRect, &clientRect, &childRect );
1722 /* set common info values */
1723 info.cbSize = sizeof(info);
1724 info.fMask = SIF_POS | SIF_RANGE;
1726 /* set the specific values and apply but only if window style allows */
1727 style = GetWindowLongW( hwnd, GWL_STYLE );
1728 switch( scroll )
1730 case SB_BOTH:
1731 case SB_HORZ:
1732 if (style & (WS_HSCROLL | WS_VSCROLL))
1734 info.nMin = childRect.left;
1735 info.nMax = childRect.right - clientRect.right;
1736 info.nPos = clientRect.left - childRect.left;
1737 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1739 if (scroll == SB_HORZ) break;
1740 /* fall through */
1741 case SB_VERT:
1742 if (style & (WS_HSCROLL | WS_VSCROLL))
1744 info.nMin = childRect.top;
1745 info.nMax = childRect.bottom - clientRect.bottom;
1746 info.nPos = clientRect.top - childRect.top;
1747 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1749 break;
1754 /***********************************************************************
1755 * ScrollChildren (USER32.@)
1757 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1758 LPARAM lParam)
1760 INT newPos = -1;
1761 INT curPos, length, minPos, maxPos, shift;
1762 RECT rect;
1764 GetClientRect( hWnd, &rect );
1766 switch(uMsg)
1768 case WM_HSCROLL:
1769 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1770 curPos = GetScrollPos(hWnd,SB_HORZ);
1771 length = (rect.right - rect.left) / 2;
1772 shift = GetSystemMetrics(SM_CYHSCROLL);
1773 break;
1774 case WM_VSCROLL:
1775 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1776 curPos = GetScrollPos(hWnd,SB_VERT);
1777 length = (rect.bottom - rect.top) / 2;
1778 shift = GetSystemMetrics(SM_CXVSCROLL);
1779 break;
1780 default:
1781 return;
1784 switch( wParam )
1786 case SB_LINEUP:
1787 newPos = curPos - shift;
1788 break;
1789 case SB_LINEDOWN:
1790 newPos = curPos + shift;
1791 break;
1792 case SB_PAGEUP:
1793 newPos = curPos - length;
1794 break;
1795 case SB_PAGEDOWN:
1796 newPos = curPos + length;
1797 break;
1799 case SB_THUMBPOSITION:
1800 newPos = LOWORD(lParam);
1801 break;
1803 case SB_THUMBTRACK:
1804 return;
1806 case SB_TOP:
1807 newPos = minPos;
1808 break;
1809 case SB_BOTTOM:
1810 newPos = maxPos;
1811 break;
1812 case SB_ENDSCROLL:
1813 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1814 return;
1817 if( newPos > maxPos )
1818 newPos = maxPos;
1819 else
1820 if( newPos < minPos )
1821 newPos = minPos;
1823 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1825 if( uMsg == WM_VSCROLL )
1826 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1827 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1828 else
1829 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1830 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1834 /******************************************************************************
1835 * CascadeWindows (USER32.@) Cascades MDI child windows
1837 * RETURNS
1838 * Success: Number of cascaded windows.
1839 * Failure: 0
1841 WORD WINAPI
1842 CascadeWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1843 UINT cKids, const HWND *lpKids)
1845 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1846 return 0;
1850 /***********************************************************************
1851 * CascadeChildWindows (USER32.@)
1853 WORD WINAPI CascadeChildWindows( HWND parent, UINT flags )
1855 return CascadeWindows( parent, flags, NULL, 0, NULL );
1859 /******************************************************************************
1860 * TileWindows (USER32.@) Tiles MDI child windows
1862 * RETURNS
1863 * Success: Number of tiled windows.
1864 * Failure: 0
1866 WORD WINAPI
1867 TileWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1868 UINT cKids, const HWND *lpKids)
1870 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1871 return 0;
1875 /***********************************************************************
1876 * TileChildWindows (USER32.@)
1878 WORD WINAPI TileChildWindows( HWND parent, UINT flags )
1880 return TileWindows( parent, flags, NULL, 0, NULL );
1884 /************************************************************************
1885 * "More Windows..." functionality
1888 /* MDI_MoreWindowsDlgProc
1890 * This function will process the messages sent to the "More Windows..."
1891 * dialog.
1892 * Return values: 0 = cancel pressed
1893 * HWND = ok pressed or double-click in the list...
1897 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1899 switch (iMsg)
1901 case WM_INITDIALOG:
1903 UINT widest = 0;
1904 UINT length;
1905 UINT i;
1906 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1907 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1909 for (i = 0; i < ci->nActiveChildren; i++)
1911 WCHAR buffer[MDI_MAXTITLELENGTH];
1913 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1914 continue;
1915 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1916 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1917 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1918 if (length > widest)
1919 widest = length;
1921 /* Make sure the horizontal scrollbar scrolls ok */
1922 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1924 /* Set the current selection */
1925 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1926 return TRUE;
1929 case WM_COMMAND:
1930 switch (LOWORD(wParam))
1932 default:
1933 if (HIWORD(wParam) != LBN_DBLCLK) break;
1934 /* fall through */
1935 case IDOK:
1937 /* windows are sorted by menu ID, so we must return the
1938 * window associated to the given id
1940 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1941 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1942 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1943 EndDialog(hDlg, res);
1944 return TRUE;
1946 case IDCANCEL:
1947 EndDialog(hDlg, 0);
1948 return TRUE;
1950 break;
1952 return FALSE;
1957 * MDI_MoreWindowsDialog
1959 * Prompts the user with a listbox containing the opened
1960 * documents. The user can then choose a windows and click
1961 * on OK to set the current window to the one selected, or
1962 * CANCEL to cancel. The function returns a handle to the
1963 * selected window.
1966 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1968 LPCVOID template;
1969 HRSRC hRes;
1970 HANDLE hDlgTmpl;
1972 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1974 if (hRes == 0)
1975 return 0;
1977 hDlgTmpl = LoadResource(user32_module, hRes );
1979 if (hDlgTmpl == 0)
1980 return 0;
1982 template = LockResource( hDlgTmpl );
1984 if (template == 0)
1985 return 0;
1987 return (HWND) DialogBoxIndirectParamA(user32_module, template, hwnd,
1988 MDI_MoreWindowsDlgProc, (LPARAM) hwnd);