odbccp32: Remove A->W cross-call.
[wine/testsucceed.git] / dlls / user32 / mdi.c
blob4f6844c3f4fe1cde225411b20f780d751a2305e7
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"
102 #include "dlgs.h"
104 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
106 #define MDI_MAXTITLELENGTH 0xa1
108 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
110 /* "More Windows..." definitions */
111 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
112 option will appear under the Windows menu */
113 #define MDI_IDC_LISTBOX 100
114 #define IDS_MDI_MOREWINDOWS 13
116 #define MDIF_NEEDUPDATE 0x0001
118 typedef struct
120 /* At some points, particularly when switching MDI children, active and
121 * maximized MDI children may be not the same window, so we need to track
122 * them separately.
124 UINT nActiveChildren;
125 HWND hwndChildMaximized;
126 HWND hwndActiveChild;
127 HWND *child; /* array of tracked children */
128 HMENU hFrameMenu;
129 HMENU hWindowMenu;
130 UINT idFirstChild;
131 LPWSTR frameTitle;
132 UINT nTotalCreated;
133 UINT mdiFlags;
134 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
135 } MDICLIENTINFO;
137 static HBITMAP hBmpClose = 0;
139 /* ----------------- declarations ----------------- */
140 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
141 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
142 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
143 static LONG MDI_ChildActivate( HWND, HWND );
144 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
146 static HWND MDI_MoreWindowsDialog(HWND);
147 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
148 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
150 /* -------- Miscellaneous service functions ----------
152 * MDI_GetChildByID
154 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
156 int i;
158 for (i = 0; ci->nActiveChildren; i++)
160 if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
161 return ci->child[i];
163 return 0;
166 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
168 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
170 ci->mdiFlags |= MDIF_NEEDUPDATE;
171 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
173 ci->sbRecalc = recalc;
177 /*********************************************************************
178 * MDIClient class descriptor
180 const struct builtin_class_descr MDICLIENT_builtin_class =
182 "MDIClient", /* name */
183 0, /* style */
184 MDIClientWndProcA, /* procA */
185 MDIClientWndProcW, /* procW */
186 sizeof(MDICLIENTINFO), /* extra */
187 IDC_ARROW, /* cursor */
188 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
192 static MDICLIENTINFO *get_client_info( HWND client )
194 MDICLIENTINFO *ret = NULL;
195 WND *win = WIN_GetPtr( client );
196 if (win)
198 if (win == WND_OTHER_PROCESS)
200 if (IsWindow(client)) ERR( "client %p belongs to other process\n", client );
201 return NULL;
203 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
204 else ret = (MDICLIENTINFO *)win->wExtra;
205 WIN_ReleasePtr( win );
207 return ret;
210 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
212 if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
214 if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
215 if (hSysMenu)
217 UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
218 if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
219 return FALSE;
221 return TRUE;
224 /**********************************************************************
225 * MDI_GetWindow
227 * returns "activatable" child different from the current or zero
229 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
230 DWORD dwStyleMask )
232 int i;
233 HWND *list;
234 HWND last = 0;
236 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
237 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
239 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
240 i = 0;
241 /* start from next after hWnd */
242 while (list[i] && list[i] != hWnd) i++;
243 if (list[i]) i++;
245 for ( ; list[i]; i++)
247 if (GetWindow( list[i], GW_OWNER )) continue;
248 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
249 last = list[i];
250 if (bNext) goto found;
252 /* now restart from the beginning */
253 for (i = 0; list[i] && list[i] != hWnd; i++)
255 if (GetWindow( list[i], GW_OWNER )) continue;
256 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
257 last = list[i];
258 if (bNext) goto found;
260 found:
261 HeapFree( GetProcessHeap(), 0, list );
262 return last;
265 /**********************************************************************
266 * MDI_CalcDefaultChildPos
268 * It seems that the default height is about 2/3 of the client rect
270 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
272 INT nstagger;
273 RECT rect;
274 INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
276 if (total < 0) /* we are called from CreateWindow */
278 MDICLIENTINFO *ci = get_client_info(hwndClient);
279 total = ci ? ci->nTotalCreated : 0;
280 *id = ci->idFirstChild + ci->nActiveChildren;
281 TRACE("MDI child id %04x\n", *id);
284 GetClientRect( hwndClient, &rect );
285 if( rect.bottom - rect.top - delta >= spacing )
286 rect.bottom -= delta;
288 nstagger = (rect.bottom - rect.top)/(3 * spacing);
289 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
290 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
291 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
294 /**********************************************************************
295 * MDISetMenu
297 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
298 HMENU hmenuWindow)
300 MDICLIENTINFO *ci;
301 HWND hwndFrame = GetParent(hwnd);
303 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
305 if (hmenuFrame && !IsMenu(hmenuFrame))
307 WARN("hmenuFrame is not a menu handle\n");
308 return 0L;
311 if (hmenuWindow && !IsMenu(hmenuWindow))
313 WARN("hmenuWindow is not a menu handle\n");
314 return 0L;
317 if (!(ci = get_client_info( hwnd ))) return 0;
319 if (hmenuFrame)
321 if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
323 if (IsZoomed(ci->hwndActiveChild))
324 MDI_RestoreFrameMenu( hwndFrame, ci->hwndActiveChild );
327 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
329 /* delete menu items from ci->hWindowMenu
330 * and add them to hmenuWindow */
331 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
332 if( ci->hWindowMenu && ci->nActiveChildren )
334 UINT nActiveChildren_old = ci->nActiveChildren;
336 /* Remove all items from old Window menu */
337 ci->nActiveChildren = 0;
338 MDI_RefreshMenu(ci);
340 ci->hWindowMenu = hmenuWindow;
342 /* Add items to the new Window menu */
343 ci->nActiveChildren = nActiveChildren_old;
344 MDI_RefreshMenu(ci);
346 else
347 ci->hWindowMenu = hmenuWindow;
350 if (hmenuFrame)
352 SetMenu(hwndFrame, hmenuFrame);
353 if( hmenuFrame != ci->hFrameMenu )
355 HMENU oldFrameMenu = ci->hFrameMenu;
357 ci->hFrameMenu = hmenuFrame;
358 if (IsZoomed(ci->hwndActiveChild) && (GetWindowLongW(ci->hwndActiveChild, GWL_STYLE) & WS_VISIBLE))
359 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
361 return (LRESULT)oldFrameMenu;
364 else
366 /* SetMenu() may already have been called, meaning that this window
367 * already has its menu. But they may have done a SetMenu() on
368 * an MDI window, and called MDISetMenu() after the fact, meaning
369 * that the "if" to this "else" wouldn't catch the need to
370 * augment the frame menu.
372 if( IsZoomed(ci->hwndActiveChild) )
373 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
376 return 0;
379 /**********************************************************************
380 * MDIRefreshMenu
382 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
384 UINT i, count, visible, id;
385 WCHAR buf[MDI_MAXTITLELENGTH];
387 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
389 if (!ci->hWindowMenu)
390 return 0;
392 if (!IsMenu(ci->hWindowMenu))
394 WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
395 return 0;
398 /* Windows finds the last separator in the menu, and if after it
399 * there is a menu item with MDI magic ID removes all existing
400 * menu items after it, and then adds visible MDI children.
402 count = GetMenuItemCount(ci->hWindowMenu);
403 for (i = 0; i < count; i++)
405 MENUITEMINFOW mii;
407 memset(&mii, 0, sizeof(mii));
408 mii.cbSize = sizeof(mii);
409 mii.fMask = MIIM_TYPE;
410 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
412 if (mii.fType & MF_SEPARATOR)
414 /* Windows checks only ID of the menu item */
415 memset(&mii, 0, sizeof(mii));
416 mii.cbSize = sizeof(mii);
417 mii.fMask = MIIM_ID;
418 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
420 if (mii.wID == ci->idFirstChild)
422 TRACE("removing %u items including separator\n", count - i);
423 while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
424 /* nothing */;
426 break;
433 visible = 0;
434 for (i = 0; i < ci->nActiveChildren; i++)
436 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
438 id = ci->idFirstChild + visible;
440 if (visible == MDI_MOREWINDOWSLIMIT)
442 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
443 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
444 break;
447 if (!visible)
448 /* Visio expects that separator has id 0 */
449 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
451 visible++;
453 SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
455 buf[0] = '&';
456 buf[1] = '0' + visible;
457 buf[2] = ' ';
458 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
459 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
460 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
462 if (ci->child[i] == ci->hwndActiveChild)
463 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
465 else
466 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
469 return (LRESULT)ci->hFrameMenu;
473 /* ------------------ MDI child window functions ---------------------- */
475 /**********************************************************************
476 * MDI_ChildGetMinMaxInfo
478 * Note: The rule here is that client rect of the maximized MDI child
479 * is equal to the client rect of the MDI client window.
481 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
483 RECT rect;
485 GetClientRect( client, &rect );
486 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
487 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
489 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
490 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
492 lpMinMax->ptMaxPosition.x = rect.left;
493 lpMinMax->ptMaxPosition.y = rect.top;
495 TRACE("max rect (%d,%d - %d, %d)\n",
496 rect.left,rect.top,rect.right,rect.bottom);
499 /**********************************************************************
500 * MDI_SwitchActiveChild
502 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
503 * being activated
505 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
507 HWND hwndPrev;
509 hwndPrev = ci->hwndActiveChild;
511 TRACE("from %p, to %p\n", hwndPrev, hwndTo);
513 if ( hwndTo != hwndPrev )
515 BOOL was_zoomed = IsZoomed(hwndPrev);
517 if (was_zoomed)
519 /* restore old MDI child */
520 SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
521 ShowWindow( hwndPrev, SW_RESTORE );
522 SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
524 /* activate new MDI child */
525 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
526 /* maximize new MDI child */
527 ShowWindow( hwndTo, SW_MAXIMIZE );
529 /* activate new MDI child */
530 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
535 /**********************************************************************
536 * MDIDestroyChild
538 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
539 HWND child, BOOL flagDestroy )
541 UINT i;
543 TRACE("# of managed children %u\n", ci->nActiveChildren);
545 if( child == ci->hwndActiveChild )
547 HWND next = MDI_GetWindow(ci, child, TRUE, 0);
548 /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
549 if (flagDestroy && next)
550 MDI_SwitchActiveChild(ci, next, TRUE);
551 else
553 ShowWindow(child, SW_HIDE);
554 if (child == ci->hwndChildMaximized)
556 HWND frame = GetParent(client);
557 MDI_RestoreFrameMenu(frame, child);
558 ci->hwndChildMaximized = 0;
559 MDI_UpdateFrameText(frame, client, TRUE, NULL);
561 if (flagDestroy) ci->hwndActiveChild = 0;
565 for (i = 0; i < ci->nActiveChildren; i++)
567 if (ci->child[i] == child)
569 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
570 memcpy(new_child, ci->child, i * sizeof(HWND));
571 if (i + 1 < ci->nActiveChildren)
572 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
573 HeapFree(GetProcessHeap(), 0, ci->child);
574 ci->child = new_child;
576 ci->nActiveChildren--;
577 break;
581 if (flagDestroy)
583 SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
584 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
585 DestroyWindow(child);
588 TRACE("child destroyed - %p\n", child);
589 return 0;
593 /**********************************************************************
594 * MDI_ChildActivate
596 * Called in response to WM_CHILDACTIVATE, or when last MDI child
597 * is being deactivated.
599 static LONG MDI_ChildActivate( HWND client, HWND child )
601 MDICLIENTINFO *clientInfo;
602 HWND prevActiveWnd, frame;
603 BOOL isActiveFrameWnd;
605 clientInfo = get_client_info( client );
607 if (clientInfo->hwndActiveChild == child) return 0;
609 TRACE("%p\n", child);
611 frame = GetParent(client);
612 isActiveFrameWnd = (GetActiveWindow() == frame);
613 prevActiveWnd = clientInfo->hwndActiveChild;
615 /* deactivate prev. active child */
616 if(prevActiveWnd)
618 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
619 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
622 MDI_SwitchActiveChild( clientInfo, child, FALSE );
623 clientInfo->hwndActiveChild = child;
625 MDI_RefreshMenu(clientInfo);
627 if( isActiveFrameWnd )
629 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
630 /* Let the client window manage focus for children, but if the focus
631 * is already on the client (for instance this is the 1st child) then
632 * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
633 * manually in this case.
635 if (SetFocus(client) == client)
636 SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
639 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
640 return TRUE;
643 /* -------------------- MDI client window functions ------------------- */
645 /**********************************************************************
646 * CreateMDIMenuBitmap
648 static HBITMAP CreateMDIMenuBitmap(void)
650 HDC hDCSrc = CreateCompatibleDC(0);
651 HDC hDCDest = CreateCompatibleDC(hDCSrc);
652 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
653 HBITMAP hbCopy;
654 HBITMAP hobjSrc, hobjDest;
656 hobjSrc = SelectObject(hDCSrc, hbClose);
657 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
658 hobjDest = SelectObject(hDCDest, hbCopy);
660 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
661 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
663 SelectObject(hDCSrc, hobjSrc);
664 DeleteObject(hbClose);
665 DeleteDC(hDCSrc);
667 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
669 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
670 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
672 SelectObject(hDCDest, hobjSrc );
673 SelectObject(hDCDest, hobjDest);
674 DeleteDC(hDCDest);
676 return hbCopy;
679 /**********************************************************************
680 * MDICascade
682 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
684 HWND *win_array;
685 BOOL has_icons = FALSE;
686 int i, total;
688 if (IsZoomed(ci->hwndActiveChild))
689 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
691 if (ci->nActiveChildren == 0) return 0;
693 if (!(win_array = WIN_ListChildren( client ))) return 0;
695 /* remove all the windows we don't want */
696 for (i = total = 0; win_array[i]; i++)
698 if (!IsWindowVisible( win_array[i] )) continue;
699 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
700 if (IsIconic( win_array[i] ))
702 has_icons = TRUE;
703 continue;
705 win_array[total++] = win_array[i];
707 win_array[total] = 0;
709 if (total)
711 INT delta = 0, n = 0, i;
712 POINT pos[2];
713 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
715 /* walk the list (backwards) and move windows */
716 for (i = total - 1; i >= 0; i--)
718 TRACE("move %p to (%d,%d) size [%d,%d]\n",
719 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
721 MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
722 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
723 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
726 HeapFree( GetProcessHeap(), 0, win_array );
728 if (has_icons) ArrangeIconicWindows( client );
729 return 0;
732 /**********************************************************************
733 * MDITile
735 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
737 HWND *win_array;
738 int i, total;
739 BOOL has_icons = FALSE;
741 if (IsZoomed(ci->hwndActiveChild))
742 SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
744 if (ci->nActiveChildren == 0) return;
746 if (!(win_array = WIN_ListChildren( client ))) return;
748 /* remove all the windows we don't want */
749 for (i = total = 0; win_array[i]; i++)
751 if (!IsWindowVisible( win_array[i] )) continue;
752 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
753 if (IsIconic( win_array[i] ))
755 has_icons = TRUE;
756 continue;
758 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
759 win_array[total++] = win_array[i];
761 win_array[total] = 0;
763 TRACE("%u windows to tile\n", total);
765 if (total)
767 HWND *pWnd = win_array;
768 RECT rect;
769 int x, y, xsize, ysize;
770 int rows, columns, r, c, i;
772 GetClientRect(client,&rect);
773 rows = (int) sqrt((double)total);
774 columns = total / rows;
776 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
778 i = rows;
779 rows = columns; /* exchange r and c */
780 columns = i;
783 if (has_icons)
785 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
786 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
789 ysize = rect.bottom / rows;
790 xsize = rect.right / columns;
792 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
794 if (c == columns)
796 rows = total - i;
797 ysize = rect.bottom / rows;
800 y = 0;
801 for (r = 1; r <= rows && *pWnd; r++, i++)
803 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
804 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
805 y += ysize;
806 pWnd++;
808 x += xsize;
811 HeapFree( GetProcessHeap(), 0, win_array );
812 if (has_icons) ArrangeIconicWindows( client );
815 /* ----------------------- Frame window ---------------------------- */
818 /**********************************************************************
819 * MDI_AugmentFrameMenu
821 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
823 HMENU menu = GetMenu( frame );
824 HMENU hSysPopup = 0;
825 HBITMAP hSysMenuBitmap = 0;
826 HICON hIcon;
828 TRACE("frame %p,child %p\n",frame,hChild);
830 if( !menu ) return 0;
832 /* create a copy of sysmenu popup and insert it into frame menu bar */
833 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
835 TRACE("child %p doesn't have a system menu\n", hChild);
836 return 0;
839 AppendMenuW(menu, MF_HELP | MF_BITMAP,
840 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
841 AppendMenuW(menu, MF_HELP | MF_BITMAP,
842 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
843 AppendMenuW(menu, MF_HELP | MF_BITMAP,
844 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
845 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
847 /* The system menu is replaced by the child icon */
848 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICONSM);
849 if (!hIcon)
850 hIcon = (HICON)GetClassLongPtrW(hChild, GCLP_HICON);
851 if (!hIcon)
852 hIcon = LoadImageW(0, MAKEINTRESOURCEW(IDI_WINLOGO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
853 if (hIcon)
855 HDC hMemDC;
856 HBITMAP hBitmap, hOldBitmap;
857 HBRUSH hBrush;
858 HDC hdc = GetDC(hChild);
860 if (hdc)
862 int cx, cy;
863 cx = GetSystemMetrics(SM_CXSMICON);
864 cy = GetSystemMetrics(SM_CYSMICON);
865 hMemDC = CreateCompatibleDC(hdc);
866 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
867 hOldBitmap = SelectObject(hMemDC, hBitmap);
868 SetMapMode(hMemDC, MM_TEXT);
869 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
870 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
871 SelectObject (hMemDC, hOldBitmap);
872 DeleteObject(hBrush);
873 DeleteDC(hMemDC);
874 ReleaseDC(hChild, hdc);
875 hSysMenuBitmap = hBitmap;
879 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
880 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
882 TRACE("not inserted\n");
883 DestroyMenu(hSysPopup);
884 return 0;
887 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
888 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
889 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
890 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
892 /* redraw menu */
893 DrawMenuBar(frame);
895 return 1;
898 /**********************************************************************
899 * MDI_RestoreFrameMenu
901 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
903 MENUITEMINFOW menuInfo;
904 HMENU menu = GetMenu( frame );
906 TRACE("frame %p, child %p\n", frame, hChild);
908 if( !menu ) return 0;
911 * Remove the system menu, If that menu is the icon of the window
912 * as it is in win95, we have to delete the bitmap.
914 memset(&menuInfo, 0, sizeof(menuInfo));
915 menuInfo.cbSize = sizeof(menuInfo);
916 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
918 GetMenuItemInfoW(menu,
920 TRUE,
921 &menuInfo);
923 RemoveMenu(menu,0,MF_BYPOSITION);
925 if ( (menuInfo.fType & MFT_BITMAP) &&
926 (LOWORD(menuInfo.dwTypeData)!=0) &&
927 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
929 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
932 /* close */
933 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
934 /* restore */
935 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
936 /* minimize */
937 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
939 DrawMenuBar(frame);
941 return 1;
945 /**********************************************************************
946 * MDI_UpdateFrameText
948 * used when child window is maximized/restored
950 * Note: lpTitle can be NULL
952 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
954 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
955 MDICLIENTINFO *ci = get_client_info( hClient );
957 TRACE("frameText %s\n", debugstr_w(lpTitle));
959 if (!ci) return;
961 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
963 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
964 lpTitle = lpBuffer;
967 /* store new "default" title if lpTitle is not NULL */
968 if (lpTitle)
970 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
971 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
972 strcpyW( ci->frameTitle, lpTitle );
975 if (ci->frameTitle)
977 if (IsZoomed(ci->hwndActiveChild) && IsWindowVisible(ci->hwndActiveChild))
979 /* combine frame title and child title if possible */
981 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
982 static const WCHAR lpBracket2[] = {']',0};
983 int i_frame_text_length = strlenW(ci->frameTitle);
985 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
987 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
989 strcatW( lpBuffer, lpBracket );
990 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
991 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
992 strcatW( lpBuffer, lpBracket2 );
993 else
994 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
997 else
999 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1002 else
1003 lpBuffer[0] = '\0';
1005 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1007 if (repaint)
1008 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1009 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1013 /* ----------------------------- Interface ---------------------------- */
1016 /**********************************************************************
1017 * MDIClientWndProc_common
1019 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1020 WPARAM wParam, LPARAM lParam, BOOL unicode )
1022 MDICLIENTINFO *ci;
1024 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1026 if (!(ci = get_client_info( hwnd ))) return 0;
1028 switch (message)
1030 case WM_CREATE:
1032 /* Since we are using only cs->lpCreateParams, we can safely
1033 * cast to LPCREATESTRUCTA here */
1034 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1035 WND *wndPtr = WIN_GetPtr( hwnd );
1037 wndPtr->flags |= WIN_ISMDICLIENT;
1039 /* Translation layer doesn't know what's in the cs->lpCreateParams
1040 * so we have to keep track of what environment we're in. */
1042 if( wndPtr->flags & WIN_ISWIN32 )
1044 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1045 ci->hWindowMenu = ccs->hWindowMenu;
1046 ci->idFirstChild = ccs->idFirstChild;
1048 else
1050 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1051 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1052 ci->idFirstChild = ccs->idFirstChild;
1054 WIN_ReleasePtr( wndPtr );
1056 ci->hwndChildMaximized = 0;
1057 ci->child = NULL;
1058 ci->nActiveChildren = 0;
1059 ci->nTotalCreated = 0;
1060 ci->frameTitle = NULL;
1061 ci->mdiFlags = 0;
1062 ci->hFrameMenu = GetMenu(cs->hwndParent);
1064 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1066 TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1067 hwnd, ci->hWindowMenu, ci->idFirstChild );
1068 return 0;
1071 case WM_DESTROY:
1073 if( ci->hwndChildMaximized )
1074 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1076 ci->nActiveChildren = 0;
1077 MDI_RefreshMenu(ci);
1079 HeapFree( GetProcessHeap(), 0, ci->child );
1080 HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1082 return 0;
1085 case WM_MDIACTIVATE:
1087 if( ci->hwndActiveChild != (HWND)wParam )
1088 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1089 return 0;
1092 case WM_MDICASCADE:
1093 return MDICascade(hwnd, ci);
1095 case WM_MDICREATE:
1096 if (lParam)
1098 HWND child;
1100 if (unicode)
1102 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1103 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1104 csW->szTitle, csW->style,
1105 csW->x, csW->y, csW->cx, csW->cy,
1106 hwnd, 0, csW->hOwner,
1107 (LPVOID)csW->lParam);
1109 else
1111 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1112 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1113 csA->szTitle, csA->style,
1114 csA->x, csA->y, csA->cx, csA->cy,
1115 hwnd, 0, csA->hOwner,
1116 (LPVOID)csA->lParam);
1119 if (IsZoomed(ci->hwndActiveChild))
1121 MDI_AugmentFrameMenu(GetParent(hwnd), child);
1122 MDI_UpdateFrameText(GetParent(hwnd), hwnd, TRUE, NULL);
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 next = MDI_GetWindow( ci, WIN_GetFullHandle( (HWND)wParam ), !lParam, 0 );
1149 MDI_SwitchActiveChild( ci, next, TRUE );
1150 break;
1153 case WM_MDIRESTORE:
1154 ShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1155 return 0;
1157 case WM_MDISETMENU:
1158 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1160 case WM_MDIREFRESHMENU:
1161 return MDI_RefreshMenu( ci );
1163 case WM_MDITILE:
1164 ci->mdiFlags |= MDIF_NEEDUPDATE;
1165 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1166 MDITile( hwnd, ci, wParam );
1167 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1168 return 0;
1170 case WM_VSCROLL:
1171 case WM_HSCROLL:
1172 ci->mdiFlags |= MDIF_NEEDUPDATE;
1173 ScrollChildren( hwnd, message, wParam, lParam );
1174 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1175 return 0;
1177 case WM_SETFOCUS:
1178 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1179 SetFocus( ci->hwndActiveChild );
1180 return 0;
1182 case WM_NCACTIVATE:
1183 if( ci->hwndActiveChild )
1184 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1185 break;
1187 case WM_PARENTNOTIFY:
1188 switch (LOWORD(wParam))
1190 case WM_CREATE:
1191 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1193 ci->nTotalCreated++;
1194 ci->nActiveChildren++;
1196 if (!ci->child)
1197 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1198 else
1199 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1201 TRACE("Adding MDI child %p, # of children %d\n",
1202 (HWND)lParam, ci->nActiveChildren);
1204 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1206 break;
1208 case WM_LBUTTONDOWN:
1210 HWND child;
1211 POINT pt;
1212 pt.x = (short)LOWORD(lParam);
1213 pt.y = (short)HIWORD(lParam);
1214 child = ChildWindowFromPoint(hwnd, pt);
1216 TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1218 if( child && child != hwnd && child != ci->hwndActiveChild )
1219 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1220 break;
1223 case WM_DESTROY:
1224 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1226 return 0;
1228 case WM_SIZE:
1229 if( IsWindow(ci->hwndActiveChild) && IsZoomed(ci->hwndActiveChild) &&
1230 (GetWindowLongW(ci->hwndActiveChild, GWL_STYLE) & WS_VISIBLE) )
1232 RECT rect;
1234 rect.left = 0;
1235 rect.top = 0;
1236 rect.right = LOWORD(lParam);
1237 rect.bottom = HIWORD(lParam);
1239 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1240 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1241 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1242 rect.right - rect.left, rect.bottom - rect.top, 1);
1244 else
1245 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1247 break;
1249 case WM_MDICALCCHILDSCROLL:
1250 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1252 CalcChildScroll(hwnd, ci->sbRecalc-1);
1253 ci->sbRecalc = 0;
1254 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1256 return 0;
1258 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1259 DefWindowProcA( hwnd, message, wParam, lParam );
1262 /***********************************************************************
1263 * MDIClientWndProcA
1265 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1267 if (!IsWindow(hwnd)) return 0;
1268 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1271 /***********************************************************************
1272 * MDIClientWndProcW
1274 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1276 if (!IsWindow(hwnd)) return 0;
1277 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1280 /***********************************************************************
1281 * DefFrameProcA (USER32.@)
1283 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1284 UINT message, WPARAM wParam, LPARAM lParam)
1286 if (hwndMDIClient)
1288 switch (message)
1290 case WM_SETTEXT:
1292 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1293 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1294 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1295 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1296 HeapFree( GetProcessHeap(), 0, text );
1298 return 1; /* success. FIXME: check text length */
1300 case WM_COMMAND:
1301 case WM_NCACTIVATE:
1302 case WM_NEXTMENU:
1303 case WM_SETFOCUS:
1304 case WM_SIZE:
1305 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1308 return DefWindowProcA(hwnd, message, wParam, lParam);
1312 /***********************************************************************
1313 * DefFrameProcW (USER32.@)
1315 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1316 UINT message, WPARAM wParam, LPARAM lParam)
1318 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1320 TRACE("%p %p %04x (%s) %08x %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1322 if (ci)
1324 switch (message)
1326 case WM_COMMAND:
1328 WORD id = LOWORD(wParam);
1329 /* check for possible syscommands for maximized MDI child */
1330 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1332 if( (id - 0xf000) & 0xf00f ) break;
1333 if( !ci->hwndChildMaximized ) break;
1334 switch( id )
1336 case SC_CLOSE:
1337 if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1338 case SC_SIZE:
1339 case SC_MOVE:
1340 case SC_MINIMIZE:
1341 case SC_MAXIMIZE:
1342 case SC_NEXTWINDOW:
1343 case SC_PREVWINDOW:
1344 case SC_RESTORE:
1345 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1346 wParam, lParam);
1349 else
1351 HWND childHwnd;
1352 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1353 /* User chose "More Windows..." */
1354 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1355 else
1356 /* User chose one of the windows listed in the "Windows" menu */
1357 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1359 if( childHwnd )
1360 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1363 break;
1365 case WM_NCACTIVATE:
1366 SendMessageW(hwndMDIClient, message, wParam, lParam);
1367 break;
1369 case WM_SETTEXT:
1370 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1371 return 1; /* success. FIXME: check text length */
1373 case WM_SETFOCUS:
1374 SetFocus(hwndMDIClient);
1375 break;
1377 case WM_SIZE:
1378 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1379 break;
1381 case WM_NEXTMENU:
1383 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1385 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1387 /* control menu is between the frame system menu and
1388 * the first entry of menu bar */
1389 WND *wndPtr = WIN_GetPtr(hwnd);
1391 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1392 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1394 WIN_ReleasePtr(wndPtr);
1395 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1396 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1397 next_menu->hwndNext = ci->hwndActiveChild;
1399 WIN_ReleasePtr(wndPtr);
1401 return 0;
1406 return DefWindowProcW( hwnd, message, wParam, lParam );
1409 /***********************************************************************
1410 * DefMDIChildProcA (USER32.@)
1412 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1413 WPARAM wParam, LPARAM lParam )
1415 HWND client = GetParent(hwnd);
1416 MDICLIENTINFO *ci = get_client_info( client );
1418 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1420 hwnd = WIN_GetFullHandle( hwnd );
1421 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1423 switch (message)
1425 case WM_SETTEXT:
1426 DefWindowProcA(hwnd, message, wParam, lParam);
1427 if( ci->hwndChildMaximized == hwnd )
1428 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1429 return 1; /* success. FIXME: check text length */
1431 case WM_GETMINMAXINFO:
1432 case WM_MENUCHAR:
1433 case WM_CLOSE:
1434 case WM_SETFOCUS:
1435 case WM_CHILDACTIVATE:
1436 case WM_SYSCOMMAND:
1437 case WM_SHOWWINDOW:
1438 case WM_SETVISIBLE:
1439 case WM_SIZE:
1440 case WM_NEXTMENU:
1441 case WM_SYSCHAR:
1442 case WM_DESTROY:
1443 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1445 return DefWindowProcA(hwnd, message, wParam, lParam);
1449 /***********************************************************************
1450 * DefMDIChildProcW (USER32.@)
1452 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1453 WPARAM wParam, LPARAM lParam )
1455 HWND client = GetParent(hwnd);
1456 MDICLIENTINFO *ci = get_client_info( client );
1458 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1460 hwnd = WIN_GetFullHandle( hwnd );
1461 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1463 switch (message)
1465 case WM_SETTEXT:
1466 DefWindowProcW(hwnd, message, wParam, lParam);
1467 if( ci->hwndChildMaximized == hwnd )
1468 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1469 return 1; /* success. FIXME: check text length */
1471 case WM_GETMINMAXINFO:
1472 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1473 return 0;
1475 case WM_MENUCHAR:
1476 return 0x00010000; /* MDI children don't have menu bars */
1478 case WM_CLOSE:
1479 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1480 return 0;
1482 case WM_CHILDACTIVATE:
1483 MDI_ChildActivate( client, hwnd );
1484 return 0;
1486 case WM_SYSCOMMAND:
1487 switch( wParam )
1489 case SC_MOVE:
1490 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1491 return 0;
1492 break;
1493 case SC_RESTORE:
1494 case SC_MINIMIZE:
1495 break;
1496 case SC_MAXIMIZE:
1497 if (ci->hwndChildMaximized == hwnd)
1498 return SendMessageW( GetParent(client), message, wParam, lParam);
1499 break;
1500 case SC_NEXTWINDOW:
1501 SendMessageW( client, WM_MDINEXT, 0, 0);
1502 return 0;
1503 case SC_PREVWINDOW:
1504 SendMessageW( client, WM_MDINEXT, 0, 1);
1505 return 0;
1507 break;
1509 case WM_SHOWWINDOW:
1510 case WM_SETVISIBLE:
1511 if (IsZoomed(ci->hwndActiveChild)) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1512 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1513 break;
1515 case WM_SIZE:
1516 /* do not change */
1517 TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1519 if( ci->hwndActiveChild == hwnd && wParam != SIZE_MAXIMIZED )
1521 HWND frame;
1523 ci->hwndChildMaximized = 0;
1525 frame = GetParent(client);
1526 MDI_RestoreFrameMenu( frame, hwnd );
1527 MDI_UpdateFrameText( frame, client, FALSE, NULL );
1530 if( wParam == SIZE_MAXIMIZED )
1532 HWND frame, hMaxChild = ci->hwndChildMaximized;
1534 if( hMaxChild == hwnd ) break;
1536 if( hMaxChild)
1538 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1540 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1541 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1543 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1546 TRACE("maximizing child %p\n", hwnd );
1548 /* keep track of the maximized window. */
1549 ci->hwndChildMaximized = hwnd; /* !!! */
1551 frame = GetParent(client);
1552 MDI_AugmentFrameMenu( frame, hwnd );
1553 MDI_UpdateFrameText( frame, client, TRUE, NULL );
1556 if( wParam == SIZE_MINIMIZED )
1558 HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1560 if( switchTo )
1561 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1564 MDI_PostUpdate(client, ci, SB_BOTH+1);
1565 break;
1567 case WM_NEXTMENU:
1569 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1570 HWND parent = GetParent(client);
1572 if( wParam == VK_LEFT ) /* switch to frame system menu */
1574 WND *wndPtr = WIN_GetPtr( parent );
1575 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1576 WIN_ReleasePtr( wndPtr );
1578 if( wParam == VK_RIGHT ) /* to frame menu bar */
1580 next_menu->hmenuNext = GetMenu(parent);
1582 next_menu->hwndNext = parent;
1583 return 0;
1586 case WM_SYSCHAR:
1587 if (wParam == '-')
1589 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1590 return 0;
1592 break;
1594 case WM_DESTROY:
1595 /* Remove itself from the Window menu */
1596 MDI_RefreshMenu(ci);
1597 break;
1599 return DefWindowProcW(hwnd, message, wParam, lParam);
1602 /**********************************************************************
1603 * CreateMDIWindowA (USER32.@) Creates a MDI child
1605 * RETURNS
1606 * Success: Handle to created window
1607 * Failure: NULL
1609 HWND WINAPI CreateMDIWindowA(
1610 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1611 LPCSTR lpWindowName, /* [in] Pointer to window name */
1612 DWORD dwStyle, /* [in] Window style */
1613 INT X, /* [in] Horizontal position of window */
1614 INT Y, /* [in] Vertical position of window */
1615 INT nWidth, /* [in] Width of window */
1616 INT nHeight, /* [in] Height of window */
1617 HWND hWndParent, /* [in] Handle to parent window */
1618 HINSTANCE hInstance, /* [in] Handle to application instance */
1619 LPARAM lParam) /* [in] Application-defined value */
1621 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1622 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1623 nWidth,nHeight,hWndParent,hInstance,lParam);
1625 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1626 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1627 0, hInstance, (LPVOID)lParam);
1630 /***********************************************************************
1631 * CreateMDIWindowW (USER32.@) Creates a MDI child
1633 * RETURNS
1634 * Success: Handle to created window
1635 * Failure: NULL
1637 HWND WINAPI CreateMDIWindowW(
1638 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1639 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1640 DWORD dwStyle, /* [in] Window style */
1641 INT X, /* [in] Horizontal position of window */
1642 INT Y, /* [in] Vertical position of window */
1643 INT nWidth, /* [in] Width of window */
1644 INT nHeight, /* [in] Height of window */
1645 HWND hWndParent, /* [in] Handle to parent window */
1646 HINSTANCE hInstance, /* [in] Handle to application instance */
1647 LPARAM lParam) /* [in] Application-defined value */
1649 TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1650 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1651 nWidth, nHeight, hWndParent, hInstance, lParam);
1653 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1654 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1655 0, hInstance, (LPVOID)lParam);
1658 /**********************************************************************
1659 * TranslateMDISysAccel (USER32.@)
1661 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1663 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1665 MDICLIENTINFO *ci = get_client_info( hwndClient );
1666 WPARAM wParam = 0;
1668 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1670 /* translate if the Ctrl key is down and Alt not. */
1672 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1674 switch( msg->wParam )
1676 case VK_F6:
1677 case VK_TAB:
1678 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1679 break;
1680 case VK_F4:
1681 case VK_RBUTTON:
1682 if (is_close_enabled(ci->hwndActiveChild, 0))
1684 wParam = SC_CLOSE;
1685 break;
1687 /* fall through */
1688 default:
1689 return 0;
1691 TRACE("wParam = %04x\n", wParam);
1692 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1693 return 1;
1696 return 0; /* failure */
1699 /***********************************************************************
1700 * CalcChildScroll (USER32.@)
1702 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1704 SCROLLINFO info;
1705 RECT childRect, clientRect;
1706 HWND *list;
1708 GetClientRect( hwnd, &clientRect );
1709 SetRectEmpty( &childRect );
1711 if ((list = WIN_ListChildren( hwnd )))
1713 int i;
1714 for (i = 0; list[i]; i++)
1716 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1717 if (style & WS_MAXIMIZE)
1719 HeapFree( GetProcessHeap(), 0, list );
1720 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1721 return;
1723 if (style & WS_VISIBLE)
1725 RECT rect;
1726 GetWindowRect( list[i], &rect );
1727 UnionRect( &childRect, &rect, &childRect );
1730 HeapFree( GetProcessHeap(), 0, list );
1732 MapWindowPoints( 0, hwnd, (POINT *)&childRect, 2 );
1733 UnionRect( &childRect, &clientRect, &childRect );
1735 /* set common info values */
1736 info.cbSize = sizeof(info);
1737 info.fMask = SIF_POS | SIF_RANGE;
1739 /* set the specific */
1740 switch( scroll )
1742 case SB_BOTH:
1743 case SB_HORZ:
1744 info.nMin = childRect.left;
1745 info.nMax = childRect.right - clientRect.right;
1746 info.nPos = clientRect.left - childRect.left;
1747 SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1748 if (scroll == SB_HORZ) break;
1749 /* fall through */
1750 case SB_VERT:
1751 info.nMin = childRect.top;
1752 info.nMax = childRect.bottom - clientRect.bottom;
1753 info.nPos = clientRect.top - childRect.top;
1754 SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1755 break;
1760 /***********************************************************************
1761 * ScrollChildren (USER32.@)
1763 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1764 LPARAM lParam)
1766 INT newPos = -1;
1767 INT curPos, length, minPos, maxPos, shift;
1768 RECT rect;
1770 GetClientRect( hWnd, &rect );
1772 switch(uMsg)
1774 case WM_HSCROLL:
1775 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1776 curPos = GetScrollPos(hWnd,SB_HORZ);
1777 length = (rect.right - rect.left) / 2;
1778 shift = GetSystemMetrics(SM_CYHSCROLL);
1779 break;
1780 case WM_VSCROLL:
1781 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1782 curPos = GetScrollPos(hWnd,SB_VERT);
1783 length = (rect.bottom - rect.top) / 2;
1784 shift = GetSystemMetrics(SM_CXVSCROLL);
1785 break;
1786 default:
1787 return;
1790 switch( wParam )
1792 case SB_LINEUP:
1793 newPos = curPos - shift;
1794 break;
1795 case SB_LINEDOWN:
1796 newPos = curPos + shift;
1797 break;
1798 case SB_PAGEUP:
1799 newPos = curPos - length;
1800 break;
1801 case SB_PAGEDOWN:
1802 newPos = curPos + length;
1803 break;
1805 case SB_THUMBPOSITION:
1806 newPos = LOWORD(lParam);
1807 break;
1809 case SB_THUMBTRACK:
1810 return;
1812 case SB_TOP:
1813 newPos = minPos;
1814 break;
1815 case SB_BOTTOM:
1816 newPos = maxPos;
1817 break;
1818 case SB_ENDSCROLL:
1819 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1820 return;
1823 if( newPos > maxPos )
1824 newPos = maxPos;
1825 else
1826 if( newPos < minPos )
1827 newPos = minPos;
1829 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1831 if( uMsg == WM_VSCROLL )
1832 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1833 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1834 else
1835 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1836 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1840 /******************************************************************************
1841 * CascadeWindows (USER32.@) Cascades MDI child windows
1843 * RETURNS
1844 * Success: Number of cascaded windows.
1845 * Failure: 0
1847 WORD WINAPI
1848 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1849 UINT cKids, const HWND *lpKids)
1851 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1852 return 0;
1856 /******************************************************************************
1857 * TileWindows (USER32.@) Tiles MDI child windows
1859 * RETURNS
1860 * Success: Number of tiled windows.
1861 * Failure: 0
1863 WORD WINAPI
1864 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1865 UINT cKids, const HWND *lpKids)
1867 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1868 return 0;
1871 /************************************************************************
1872 * "More Windows..." functionality
1875 /* MDI_MoreWindowsDlgProc
1877 * This function will process the messages sent to the "More Windows..."
1878 * dialog.
1879 * Return values: 0 = cancel pressed
1880 * HWND = ok pressed or double-click in the list...
1884 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1886 switch (iMsg)
1888 case WM_INITDIALOG:
1890 UINT widest = 0;
1891 UINT length;
1892 UINT i;
1893 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1894 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1896 for (i = 0; i < ci->nActiveChildren; i++)
1898 WCHAR buffer[MDI_MAXTITLELENGTH];
1900 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1901 continue;
1902 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1903 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1904 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1905 if (length > widest)
1906 widest = length;
1908 /* Make sure the horizontal scrollbar scrolls ok */
1909 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1911 /* Set the current selection */
1912 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1913 return TRUE;
1916 case WM_COMMAND:
1917 switch (LOWORD(wParam))
1919 default:
1920 if (HIWORD(wParam) != LBN_DBLCLK) break;
1921 /* fall through */
1922 case IDOK:
1924 /* windows are sorted by menu ID, so we must return the
1925 * window associated to the given id
1927 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1928 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1929 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1930 EndDialog(hDlg, res);
1931 return TRUE;
1933 case IDCANCEL:
1934 EndDialog(hDlg, 0);
1935 return TRUE;
1937 break;
1939 return FALSE;
1944 * MDI_MoreWindowsDialog
1946 * Prompts the user with a listbox containing the opened
1947 * documents. The user can then choose a windows and click
1948 * on OK to set the current window to the one selected, or
1949 * CANCEL to cancel. The function returns a handle to the
1950 * selected window.
1953 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1955 LPCVOID template;
1956 HRSRC hRes;
1957 HANDLE hDlgTmpl;
1959 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1961 if (hRes == 0)
1962 return 0;
1964 hDlgTmpl = LoadResource(user32_module, hRes );
1966 if (hDlgTmpl == 0)
1967 return 0;
1969 template = LockResource( hDlgTmpl );
1971 if (template == 0)
1972 return 0;
1974 return (HWND) DialogBoxIndirectParamA(user32_module,
1975 (const DLGTEMPLATE*) template,
1976 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);