Assorted spelling fixes.
[wine/dcerpc.git] / windows / mdi.c
blob75bbeacb22a44f30c0ae143f4a1a0d0d9d6815e8
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Notes: Fairly complete implementation.
24 * Also, Excel and WinWord do _not_ use MDI so if you're trying
25 * to fix them look elsewhere.
27 * Notes on how the "More Windows..." is implemented:
29 * When we have more than 9 opened windows, a "More Windows..."
30 * option appears in the "Windows" menu. Each child window has
31 * a WND* associated with it, accesible via the children list of
32 * the parent window. This WND* has a wIDmenu member, which reflects
33 * the position of the child in the window list. For example, with
34 * 9 child windows, we could have the following pattern:
38 * Name of the child window pWndChild->wIDmenu
39 * Doc1 5000
40 * Doc2 5001
41 * Doc3 5002
42 * Doc4 5003
43 * Doc5 5004
44 * Doc6 5005
45 * Doc7 5006
46 * Doc8 5007
47 * Doc9 5008
50 * The "Windows" menu, as the "More windows..." dialog, are constructed
51 * in this order. If we add a child, we would have the following list:
54 * Name of the child window pWndChild->wIDmenu
55 * Doc1 5000
56 * Doc2 5001
57 * Doc3 5002
58 * Doc4 5003
59 * Doc5 5004
60 * Doc6 5005
61 * Doc7 5006
62 * Doc8 5007
63 * Doc9 5008
64 * Doc10 5009
66 * But only 5000 to 5008 would be displayed in the "Windows" menu. We want
67 * the last created child to be in the menu, so we swap the last child with
68 * the 9th... Doc9 will be accessible via the "More Windows..." option.
70 * Doc1 5000
71 * Doc2 5001
72 * Doc3 5002
73 * Doc4 5003
74 * Doc5 5004
75 * Doc6 5005
76 * Doc7 5006
77 * Doc8 5007
78 * Doc9 5009
79 * Doc10 5008
83 #include <stdlib.h>
84 #include <stdarg.h>
85 #include <stdio.h>
86 #include <string.h>
87 #include <math.h>
89 #include "windef.h"
90 #include "winbase.h"
91 #include "wingdi.h"
92 #include "winuser.h"
93 #include "wownt32.h"
94 #include "wine/unicode.h"
95 #include "win.h"
96 #include "nonclient.h"
97 #include "controls.h"
98 #include "message.h"
99 #include "user.h"
100 #include "struct32.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 UINT nActiveChildren;
121 HWND hwndActiveChild;
122 HWND *child; /* array of tracked children */
123 HMENU hFrameMenu;
124 HMENU hWindowMenu;
125 UINT idFirstChild;
126 LPWSTR frameTitle;
127 UINT nTotalCreated;
128 UINT mdiFlags;
129 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
130 } MDICLIENTINFO;
132 static HBITMAP hBmpClose = 0;
134 /* ----------------- declarations ----------------- */
135 static void MDI_UpdateFrameText( HWND, HWND, LPCWSTR);
136 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
137 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
138 static LONG MDI_ChildActivate( HWND, HWND );
139 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
141 static HWND MDI_MoreWindowsDialog(HWND);
142 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
143 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
145 /* -------- Miscellaneous service functions ----------
147 * MDI_GetChildByID
149 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
151 int i;
153 for (i = 0; ci->nActiveChildren; i++)
155 if (GetWindowLongW( ci->child[i], GWL_ID ) == id)
156 return ci->child[i];
158 return 0;
161 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
163 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
165 ci->mdiFlags |= MDIF_NEEDUPDATE;
166 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
168 ci->sbRecalc = recalc;
172 /*********************************************************************
173 * MDIClient class descriptor
175 const struct builtin_class_descr MDICLIENT_builtin_class =
177 "MDIClient", /* name */
178 0, /* style */
179 MDIClientWndProcA, /* procA */
180 MDIClientWndProcW, /* procW */
181 sizeof(MDICLIENTINFO), /* extra */
182 IDC_ARROW, /* cursor */
183 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
187 static MDICLIENTINFO *get_client_info( HWND client )
189 MDICLIENTINFO *ret = NULL;
190 WND *win = WIN_GetPtr( client );
191 if (win)
193 if (win == WND_OTHER_PROCESS)
195 ERR( "client %p belongs to other process\n", client );
196 return NULL;
198 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
199 else ret = (MDICLIENTINFO *)win->wExtra;
200 WIN_ReleasePtr( win );
202 return ret;
205 /**********************************************************************
206 * MDI_GetWindow
208 * returns "activateable" child different from the current or zero
210 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
211 DWORD dwStyleMask )
213 int i;
214 HWND *list;
215 HWND last = 0;
217 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
218 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
220 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
221 i = 0;
222 /* start from next after hWnd */
223 while (list[i] && list[i] != hWnd) i++;
224 if (list[i]) i++;
226 for ( ; list[i]; i++)
228 if (GetWindow( list[i], GW_OWNER )) continue;
229 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
230 last = list[i];
231 if (bNext) goto found;
233 /* now restart from the beginning */
234 for (i = 0; list[i] && list[i] != hWnd; i++)
236 if (GetWindow( list[i], GW_OWNER )) continue;
237 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
238 last = list[i];
239 if (bNext) goto found;
241 found:
242 HeapFree( GetProcessHeap(), 0, list );
243 return last;
246 /**********************************************************************
247 * MDI_CalcDefaultChildPos
249 * It seems that the default height is about 2/3 of the client rect
251 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta )
253 INT nstagger;
254 RECT rect;
255 INT spacing = GetSystemMetrics(SM_CYCAPTION) +
256 GetSystemMetrics(SM_CYFRAME) - 1;
258 if (total < 0)
260 MDICLIENTINFO *ci = get_client_info(hwndClient);
261 total = ci ? ci->nTotalCreated : 0;
264 GetClientRect( hwndClient, &rect );
265 if( rect.bottom - rect.top - delta >= spacing )
266 rect.bottom -= delta;
268 nstagger = (rect.bottom - rect.top)/(3 * spacing);
269 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
270 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
271 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
274 /**********************************************************************
275 * MDISetMenu
277 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
278 HMENU hmenuWindow)
280 MDICLIENTINFO *ci;
281 HWND hwndFrame = GetParent(hwnd);
283 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
285 if (hmenuFrame && !IsMenu(hmenuFrame))
287 WARN("hmenuFrame is not a menu handle\n");
288 return 0L;
291 if (hmenuWindow && !IsMenu(hmenuWindow))
293 WARN("hmenuWindow is not a menu handle\n");
294 return 0L;
297 if (!(ci = get_client_info( hwnd ))) return 0;
299 if( IsZoomed(ci->hwndActiveChild) && hmenuFrame && hmenuFrame!= ci->hFrameMenu )
300 MDI_RestoreFrameMenu( hwndFrame, ci->hwndActiveChild );
302 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
304 /* delete menu items from ci->hWindowMenu
305 * and add them to hmenuWindow */
306 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
307 if( ci->hWindowMenu && ci->nActiveChildren )
309 UINT nActiveChildren_old = ci->nActiveChildren;
311 /* Remove all items from old Window menu */
312 ci->nActiveChildren = 0;
313 MDI_RefreshMenu(ci);
315 ci->hWindowMenu = hmenuWindow;
317 /* Add items to the new Window menu */
318 ci->nActiveChildren = nActiveChildren_old;
319 MDI_RefreshMenu(ci);
321 else
322 ci->hWindowMenu = hmenuWindow;
325 if (hmenuFrame)
327 SetMenu(hwndFrame, hmenuFrame);
328 if( hmenuFrame != ci->hFrameMenu )
330 HMENU oldFrameMenu = ci->hFrameMenu;
332 ci->hFrameMenu = hmenuFrame;
333 if( IsZoomed(ci->hwndActiveChild) )
334 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
336 return (LRESULT)oldFrameMenu;
339 else
341 /* SetMenu() may already have been called, meaning that this window
342 * already has its menu. But they may have done a SetMenu() on
343 * an MDI window, and called MDISetMenu() after the fact, meaning
344 * that the "if" to this "else" wouldn't catch the need to
345 * augment the frame menu.
347 if( IsZoomed(ci->hwndActiveChild) )
348 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
351 return 0;
354 /**********************************************************************
355 * MDIRefreshMenu
357 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
359 UINT i, count, visible, separator_pos, id;
360 WCHAR buf[MDI_MAXTITLELENGTH];
362 TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
364 if (!ci->hWindowMenu)
365 return 0;
367 if (!IsMenu(ci->hWindowMenu))
369 WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
370 return 0;
373 /* Windows finds the last separator in the menu, and if after it
374 * there is a menu item with MDI magic ID removes all existing
375 * menu items after it, and then adds visible MDI children.
377 id = (UINT)-1;
378 separator_pos = 0;
379 count = GetMenuItemCount(ci->hWindowMenu);
380 for (i = 0; i < count; i++)
382 MENUITEMINFOW mii;
384 memset(&mii, 0, sizeof(mii));
385 mii.cbSize = sizeof(mii);
386 mii.fMask = MIIM_TYPE;
387 if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
389 if (mii.fType & MF_SEPARATOR)
391 separator_pos = i;
393 /* Windows checks only ID of the menu item */
394 memset(&mii, 0, sizeof(mii));
395 mii.cbSize = sizeof(mii);
396 mii.fMask = MIIM_ID;
397 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
398 id = mii.wID;
403 if (separator_pos && id == ci->idFirstChild)
405 for (i = separator_pos; i < count; i++)
406 RemoveMenu(ci->hWindowMenu, separator_pos, MF_BYPOSITION);
409 visible = 0;
410 for (i = 0; i < ci->nActiveChildren; i++)
412 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
414 id = ci->idFirstChild + visible;
416 if (visible == MDI_MOREWINDOWSLIMIT)
418 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
419 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
420 break;
423 if (!visible)
424 /* Visio expects that separator has id 0 */
425 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
427 visible++;
429 SetWindowLongW(ci->child[i], GWL_ID, id);
431 buf[0] = '&';
432 buf[1] = '0' + visible;
433 buf[2] = ' ';
434 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
435 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
436 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
438 if (ci->child[i] == ci->hwndActiveChild)
439 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
441 else
442 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
445 return (LRESULT)ci->hFrameMenu;
449 /* ------------------ MDI child window functions ---------------------- */
451 /**********************************************************************
452 * MDI_ChildGetMinMaxInfo
454 * Note: The rule here is that client rect of the maximized MDI child
455 * is equal to the client rect of the MDI client window.
457 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
459 RECT rect;
461 GetClientRect( client, &rect );
462 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
463 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
465 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
466 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
468 lpMinMax->ptMaxPosition.x = rect.left;
469 lpMinMax->ptMaxPosition.y = rect.top;
471 TRACE("max rect (%ld,%ld - %ld, %ld)\n",
472 rect.left,rect.top,rect.right,rect.bottom);
475 /**********************************************************************
476 * MDI_SwitchActiveChild
478 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
479 * being activated
481 static void MDI_SwitchActiveChild( HWND clientHwnd, HWND childHwnd,
482 BOOL bNextWindow )
484 HWND hwndTo = 0;
485 HWND hwndPrev = 0;
486 MDICLIENTINFO *ci = get_client_info( clientHwnd );
488 hwndTo = MDI_GetWindow(ci, childHwnd, bNextWindow, 0);
489 hwndPrev = ci->hwndActiveChild;
491 TRACE("from %p, to %p\n",childHwnd,hwndTo);
493 if ( !hwndTo )
494 MDI_ChildActivate( clientHwnd, 0 );
495 else if ( hwndTo != hwndPrev )
496 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0,
497 SWP_NOMOVE | SWP_NOSIZE );
501 /**********************************************************************
502 * MDIDestroyChild
504 static LRESULT MDIDestroyChild( HWND parent, MDICLIENTINFO *ci,
505 HWND child, BOOL flagDestroy )
507 UINT i;
509 TRACE("# of managed children %u\n", ci->nActiveChildren);
511 if( child == ci->hwndActiveChild )
513 MDI_SwitchActiveChild(parent, child, TRUE);
514 ShowWindow(child, SW_HIDE);
517 for (i = 0; i < ci->nActiveChildren; i++)
519 if (ci->child[i] == child)
521 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
522 memcpy(new_child, ci->child, i * sizeof(HWND));
523 if (i + 1 < ci->nActiveChildren)
524 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
525 HeapFree(GetProcessHeap(), 0, ci->child);
526 ci->child = new_child;
528 ci->nActiveChildren--;
529 break;
533 if (flagDestroy)
535 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
536 DestroyWindow(child);
539 TRACE("child destroyed - %p\n", child);
540 return 0;
544 /**********************************************************************
545 * MDI_ChildActivate
547 * Note: hWndChild is NULL when last child is being destroyed
549 static LONG MDI_ChildActivate( HWND client, HWND child )
551 MDICLIENTINFO *clientInfo;
552 HWND prevActiveWnd, frame;
553 BOOL isActiveFrameWnd;
555 if (child && (!IsWindowEnabled( child ))) return 0;
557 clientInfo = get_client_info( client );
559 TRACE("%p\n", child);
561 frame = GetParent(client);
562 isActiveFrameWnd = (GetActiveWindow() == frame);
563 prevActiveWnd = clientInfo->hwndActiveChild;
565 if (prevActiveWnd == child) return 0;
567 /* deactivate prev. active child */
568 if(prevActiveWnd)
570 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
571 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
574 clientInfo->hwndActiveChild = child;
576 /* set appearance */
577 if (IsZoomed(prevActiveWnd) && prevActiveWnd != child)
579 if( child )
581 INT cmd = SW_SHOWNORMAL;
582 HMENU hSysMenu = GetSystemMenu(child, FALSE);
583 UINT state = 0;
584 if (hSysMenu)
585 state = GetMenuState(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
586 if (state != 0xFFFFFFFF && !(state & (MF_DISABLED | MF_GRAYED)))
587 cmd = SW_SHOWMAXIMIZED;
589 ShowWindow( child, cmd );
591 else
592 MDI_RestoreFrameMenu(frame, child);
595 MDI_UpdateFrameText(frame, client, NULL);
597 /* check if we have any children left */
598 if( !child )
600 if( isActiveFrameWnd )
601 SetFocus( client );
602 return 0;
605 MDI_RefreshMenu(clientInfo);
607 if( isActiveFrameWnd )
608 SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
610 SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
611 return TRUE;
614 /* -------------------- MDI client window functions ------------------- */
616 /**********************************************************************
617 * CreateMDIMenuBitmap
619 static HBITMAP CreateMDIMenuBitmap(void)
621 HDC hDCSrc = CreateCompatibleDC(0);
622 HDC hDCDest = CreateCompatibleDC(hDCSrc);
623 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
624 HBITMAP hbCopy;
625 HBITMAP hobjSrc, hobjDest;
627 hobjSrc = SelectObject(hDCSrc, hbClose);
628 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
629 hobjDest = SelectObject(hDCDest, hbCopy);
631 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
632 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
634 SelectObject(hDCSrc, hobjSrc);
635 DeleteObject(hbClose);
636 DeleteDC(hDCSrc);
638 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
640 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
641 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
643 SelectObject(hDCDest, hobjSrc );
644 SelectObject(hDCDest, hobjDest);
645 DeleteDC(hDCDest);
647 return hbCopy;
650 /**********************************************************************
651 * MDICascade
653 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
655 HWND *win_array;
656 BOOL has_icons = FALSE;
657 int i, total;
659 if (IsZoomed(ci->hwndActiveChild))
660 SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
662 if (ci->nActiveChildren == 0) return 0;
664 if (!(win_array = WIN_ListChildren( client ))) return 0;
666 /* remove all the windows we don't want */
667 for (i = total = 0; win_array[i]; i++)
669 if (!IsWindowVisible( win_array[i] )) continue;
670 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
671 if (IsIconic( win_array[i] ))
673 has_icons = TRUE;
674 continue;
676 win_array[total++] = win_array[i];
678 win_array[total] = 0;
680 if (total)
682 INT delta = 0, n = 0, i;
683 POINT pos[2];
684 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
686 /* walk the list (backwards) and move windows */
687 for (i = total - 1; i >= 0; i--)
689 TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
690 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
692 MDI_CalcDefaultChildPos(client, n++, pos, delta);
693 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
694 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
697 HeapFree( GetProcessHeap(), 0, win_array );
699 if (has_icons) ArrangeIconicWindows( client );
700 return 0;
703 /**********************************************************************
704 * MDITile
706 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
708 HWND *win_array;
709 int i, total;
710 BOOL has_icons = FALSE;
712 if (IsZoomed(ci->hwndActiveChild))
713 SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
715 if (ci->nActiveChildren == 0) return;
717 if (!(win_array = WIN_ListChildren( client ))) return;
719 /* remove all the windows we don't want */
720 for (i = total = 0; win_array[i]; i++)
722 if (!IsWindowVisible( win_array[i] )) continue;
723 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
724 if (IsIconic( win_array[i] ))
726 has_icons = TRUE;
727 continue;
729 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
730 win_array[total++] = win_array[i];
732 win_array[total] = 0;
734 TRACE("%u windows to tile\n", total);
736 if (total)
738 HWND *pWnd = win_array;
739 RECT rect;
740 int x, y, xsize, ysize;
741 int rows, columns, r, c, i;
743 GetClientRect(client,&rect);
744 rows = (int) sqrt((double)total);
745 columns = total / rows;
747 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
749 i = rows;
750 rows = columns; /* exchange r and c */
751 columns = i;
754 if (has_icons)
756 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
757 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
760 ysize = rect.bottom / rows;
761 xsize = rect.right / columns;
763 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
765 if (c == columns)
767 rows = total - i;
768 ysize = rect.bottom / rows;
771 y = 0;
772 for (r = 1; r <= rows && *pWnd; r++, i++)
774 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
775 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
776 y += ysize;
777 pWnd++;
779 x += xsize;
782 HeapFree( GetProcessHeap(), 0, win_array );
783 if (has_icons) ArrangeIconicWindows( client );
786 /* ----------------------- Frame window ---------------------------- */
789 /**********************************************************************
790 * MDI_AugmentFrameMenu
792 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
794 HMENU menu = GetMenu( frame );
795 HMENU hSysPopup = 0;
796 HBITMAP hSysMenuBitmap = 0;
797 INT nItems;
798 UINT iId;
799 HICON hIcon;
801 TRACE("frame %p,child %p\n",frame,hChild);
803 if( !menu ) return 0;
805 /* if the system buttons already exist do not add them again */
806 nItems = GetMenuItemCount(menu) - 1;
807 iId = GetMenuItemID(menu,nItems) ;
808 if (iId == SC_RESTORE || iId == SC_CLOSE)
809 return 0;
811 /* create a copy of sysmenu popup and insert it into frame menu bar */
812 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
813 return 0;
815 AppendMenuA(menu,MF_HELP | MF_BITMAP,
816 SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
817 AppendMenuA(menu,MF_HELP | MF_BITMAP,
818 SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
820 AppendMenuA(menu,MF_HELP | MF_BITMAP,
821 SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
823 /* The system menu is replaced by the child icon */
824 hIcon = (HICON)GetClassLongW(hChild, GCL_HICONSM);
825 if (!hIcon)
826 hIcon = (HICON)GetClassLongW(hChild, GCL_HICON);
827 if (!hIcon)
828 hIcon = LoadImageW(0, MAKEINTRESOURCEW(IDI_WINLOGO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
829 if (hIcon)
831 HDC hMemDC;
832 HBITMAP hBitmap, hOldBitmap;
833 HBRUSH hBrush;
834 HDC hdc = GetDC(hChild);
836 if (hdc)
838 int cx, cy;
839 cx = GetSystemMetrics(SM_CXSMICON);
840 cy = GetSystemMetrics(SM_CYSMICON);
841 hMemDC = CreateCompatibleDC(hdc);
842 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
843 hOldBitmap = SelectObject(hMemDC, hBitmap);
844 SetMapMode(hMemDC, MM_TEXT);
845 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
846 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
847 SelectObject (hMemDC, hOldBitmap);
848 DeleteObject(hBrush);
849 DeleteDC(hMemDC);
850 ReleaseDC(hChild, hdc);
851 hSysMenuBitmap = hBitmap;
855 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
856 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
858 TRACE("not inserted\n");
859 DestroyMenu(hSysPopup);
860 return 0;
863 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
864 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
865 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
866 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
868 /* redraw menu */
869 DrawMenuBar(frame);
871 return 1;
874 /**********************************************************************
875 * MDI_RestoreFrameMenu
877 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
879 MENUITEMINFOW menuInfo;
880 HMENU menu = GetMenu( frame );
881 INT nItems = GetMenuItemCount(menu) - 1;
882 UINT iId = GetMenuItemID(menu,nItems) ;
884 TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
886 /* if there is no system buttons then nothing to do */
887 if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
888 return 0;
891 * Remove the system menu, If that menu is the icon of the window
892 * as it is in win95, we have to delete the bitmap.
894 memset(&menuInfo, 0, sizeof(menuInfo));
895 menuInfo.cbSize = sizeof(menuInfo);
896 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
898 GetMenuItemInfoW(menu,
900 TRUE,
901 &menuInfo);
903 RemoveMenu(menu,0,MF_BYPOSITION);
905 if ( (menuInfo.fType & MFT_BITMAP) &&
906 (LOWORD(menuInfo.dwTypeData)!=0) &&
907 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
909 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
912 /* close */
913 DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
914 /* restore */
915 DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
916 /* minimize */
917 DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
919 DrawMenuBar(frame);
921 return 1;
925 /**********************************************************************
926 * MDI_UpdateFrameText
928 * used when child window is maximized/restored
930 * Note: lpTitle can be NULL
932 static void MDI_UpdateFrameText( HWND frame, HWND hClient, LPCWSTR lpTitle )
934 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
935 MDICLIENTINFO *ci = get_client_info( hClient );
937 TRACE("frameText %s\n", debugstr_w(lpTitle));
939 if (!ci) return;
941 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
943 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
944 lpTitle = lpBuffer;
947 /* store new "default" title if lpTitle is not NULL */
948 if (lpTitle)
950 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
951 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
952 strcpyW( ci->frameTitle, lpTitle );
955 if (ci->frameTitle)
957 if (IsZoomed(ci->hwndActiveChild) && IsWindowVisible(ci->hwndActiveChild))
959 /* combine frame title and child title if possible */
961 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
962 static const WCHAR lpBracket2[] = {']',0};
963 int i_frame_text_length = strlenW(ci->frameTitle);
965 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
967 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
969 strcatW( lpBuffer, lpBracket );
970 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
971 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
972 strcatW( lpBuffer, lpBracket2 );
973 else
974 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
977 else
979 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
982 else
983 lpBuffer[0] = '\0';
985 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
989 /* ----------------------------- Interface ---------------------------- */
992 /**********************************************************************
993 * MDIClientWndProc_common
995 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
996 WPARAM wParam, LPARAM lParam, BOOL unicode )
998 MDICLIENTINFO *ci;
1000 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1002 if (!(ci = get_client_info( hwnd ))) return 0;
1004 switch (message)
1006 case WM_CREATE:
1008 /* Since we are using only cs->lpCreateParams, we can safely
1009 * cast to LPCREATESTRUCTA here */
1010 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1011 WND *wndPtr = WIN_GetPtr( hwnd );
1013 wndPtr->flags |= WIN_ISMDICLIENT;
1015 /* Translation layer doesn't know what's in the cs->lpCreateParams
1016 * so we have to keep track of what environment we're in. */
1018 if( wndPtr->flags & WIN_ISWIN32 )
1020 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1021 ci->hWindowMenu = ccs->hWindowMenu;
1022 ci->idFirstChild = ccs->idFirstChild;
1024 else
1026 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1027 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1028 ci->idFirstChild = ccs->idFirstChild;
1030 WIN_ReleasePtr( wndPtr );
1032 ci->child = NULL;
1033 ci->nActiveChildren = 0;
1034 ci->nTotalCreated = 0;
1035 ci->frameTitle = NULL;
1036 ci->mdiFlags = 0;
1037 ci->hFrameMenu = GetMenu(cs->hwndParent);
1039 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1041 TRACE("Client created - hwnd = %p, idFirst = %u\n", hwnd, ci->idFirstChild );
1042 return 0;
1045 case WM_DESTROY:
1047 if( IsZoomed(ci->hwndActiveChild) )
1048 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndActiveChild);
1050 ci->nActiveChildren = 0;
1051 MDI_RefreshMenu(ci);
1053 if (ci->child) HeapFree( GetProcessHeap(), 0, ci->child );
1054 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1056 return 0;
1059 case WM_MDIACTIVATE:
1061 if( ci->hwndActiveChild != (HWND)wParam )
1062 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1063 return 0;
1066 case WM_MDICASCADE:
1067 return MDICascade(hwnd, ci);
1069 case WM_MDICREATE:
1070 if (lParam)
1072 if (unicode)
1074 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1075 return (LRESULT)CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1076 csW->szTitle, csW->style,
1077 csW->x, csW->y, csW->cx, csW->cy,
1078 hwnd, 0, csW->hOwner,
1079 (LPVOID)csW->lParam);
1081 else
1083 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1084 return (LRESULT)CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1085 csA->szTitle, csA->style,
1086 csA->x, csA->y, csA->cx, csA->cy,
1087 hwnd, 0, csA->hOwner,
1088 (LPVOID)csA->lParam);
1091 return 0;
1093 case WM_MDIDESTROY:
1094 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1096 case WM_MDIGETACTIVE:
1097 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1098 return (LRESULT)ci->hwndActiveChild;
1100 case WM_MDIICONARRANGE:
1101 ci->mdiFlags |= MDIF_NEEDUPDATE;
1102 ArrangeIconicWindows( hwnd );
1103 ci->sbRecalc = SB_BOTH+1;
1104 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1105 return 0;
1107 case WM_MDIMAXIMIZE:
1108 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1109 return 0;
1111 case WM_MDINEXT: /* lParam != 0 means previous window */
1112 MDI_SwitchActiveChild( hwnd, WIN_GetFullHandle( (HWND)wParam ), !lParam );
1113 break;
1115 case WM_MDIRESTORE:
1116 SendMessageW( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1117 return 0;
1119 case WM_MDISETMENU:
1120 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1122 case WM_MDIREFRESHMENU:
1123 return MDI_RefreshMenu( ci );
1125 case WM_MDITILE:
1126 ci->mdiFlags |= MDIF_NEEDUPDATE;
1127 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1128 MDITile( hwnd, ci, wParam );
1129 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1130 return 0;
1132 case WM_VSCROLL:
1133 case WM_HSCROLL:
1134 ci->mdiFlags |= MDIF_NEEDUPDATE;
1135 ScrollChildren( hwnd, message, wParam, lParam );
1136 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1137 return 0;
1139 case WM_SETFOCUS:
1140 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1141 SetFocus( ci->hwndActiveChild );
1142 return 0;
1144 case WM_NCACTIVATE:
1145 if( ci->hwndActiveChild )
1146 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1147 break;
1149 case WM_PARENTNOTIFY:
1150 switch (LOWORD(wParam))
1152 case WM_CREATE:
1153 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1155 ci->nTotalCreated++;
1156 ci->nActiveChildren++;
1158 if (!ci->child)
1159 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1160 else
1161 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1163 TRACE("Adding MDI child %p, # of children %d\n",
1164 (HWND)lParam, ci->nActiveChildren);
1166 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1168 break;
1170 case WM_LBUTTONDOWN:
1172 HWND child;
1173 POINT pt;
1174 pt.x = (short)LOWORD(lParam);
1175 pt.y = (short)HIWORD(lParam);
1176 child = ChildWindowFromPoint(hwnd, pt);
1178 TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1180 if( child && child != hwnd && child != ci->hwndActiveChild )
1181 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1182 break;
1185 return 0;
1187 case WM_SIZE:
1188 if( IsWindow(ci->hwndActiveChild) && IsZoomed(ci->hwndActiveChild) &&
1189 IsWindowVisible(ci->hwndActiveChild) )
1191 RECT rect;
1193 rect.left = 0;
1194 rect.top = 0;
1195 rect.right = LOWORD(lParam);
1196 rect.bottom = HIWORD(lParam);
1198 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1199 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1200 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1201 rect.right - rect.left, rect.bottom - rect.top, 1);
1203 else
1204 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1206 break;
1208 case WM_MDICALCCHILDSCROLL:
1209 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1211 CalcChildScroll(hwnd, ci->sbRecalc-1);
1212 ci->sbRecalc = 0;
1213 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1215 return 0;
1217 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1218 DefWindowProcA( hwnd, message, wParam, lParam );
1221 /***********************************************************************
1222 * MDIClientWndProcA
1224 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1226 if (!IsWindow(hwnd)) return 0;
1227 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1230 /***********************************************************************
1231 * MDIClientWndProcW
1233 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1235 if (!IsWindow(hwnd)) return 0;
1236 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1239 /***********************************************************************
1240 * DefFrameProcA (USER32.@)
1242 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1243 UINT message, WPARAM wParam, LPARAM lParam)
1245 if (hwndMDIClient)
1247 switch (message)
1249 case WM_SETTEXT:
1251 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1252 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1253 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1254 MDI_UpdateFrameText( hwnd, hwndMDIClient, text );
1255 HeapFree( GetProcessHeap(), 0, text );
1257 return 1; /* success. FIXME: check text length */
1259 case WM_COMMAND:
1260 case WM_NCACTIVATE:
1261 case WM_NEXTMENU:
1262 case WM_SETFOCUS:
1263 case WM_SIZE:
1264 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1267 return DefWindowProcA(hwnd, message, wParam, lParam);
1271 /***********************************************************************
1272 * DefFrameProcW (USER32.@)
1274 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1275 UINT message, WPARAM wParam, LPARAM lParam)
1277 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1279 TRACE("%p %p %04x (%s) %08x %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1281 if (ci)
1283 switch (message)
1285 case WM_COMMAND:
1287 WORD id = LOWORD(wParam);
1288 /* check for possible syscommands for maximized MDI child */
1289 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1291 if( (id - 0xf000) & 0xf00f ) break;
1292 if( !IsZoomed(ci->hwndActiveChild) ) break;
1293 switch( id )
1295 case SC_SIZE:
1296 case SC_MOVE:
1297 case SC_MINIMIZE:
1298 case SC_MAXIMIZE:
1299 case SC_NEXTWINDOW:
1300 case SC_PREVWINDOW:
1301 case SC_CLOSE:
1302 case SC_RESTORE:
1303 return SendMessageW( ci->hwndActiveChild, WM_SYSCOMMAND,
1304 wParam, lParam);
1307 else
1309 HWND childHwnd;
1310 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1311 /* User chose "More Windows..." */
1312 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1313 else
1314 /* User chose one of the windows listed in the "Windows" menu */
1315 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1317 if( childHwnd )
1318 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1321 break;
1323 case WM_NCACTIVATE:
1324 SendMessageW(hwndMDIClient, message, wParam, lParam);
1325 break;
1327 case WM_SETTEXT:
1328 MDI_UpdateFrameText( hwnd, hwndMDIClient, (LPWSTR)lParam );
1329 return 1; /* success. FIXME: check text length */
1331 case WM_SETFOCUS:
1332 SetFocus(hwndMDIClient);
1333 break;
1335 case WM_SIZE:
1336 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1337 break;
1339 case WM_NEXTMENU:
1341 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1343 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1345 /* control menu is between the frame system menu and
1346 * the first entry of menu bar */
1347 WND *wndPtr = WIN_GetPtr(hwnd);
1349 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1350 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1352 WIN_ReleasePtr(wndPtr);
1353 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1354 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1355 next_menu->hwndNext = ci->hwndActiveChild;
1357 WIN_ReleasePtr(wndPtr);
1359 return 0;
1364 return DefWindowProcW( hwnd, message, wParam, lParam );
1367 /***********************************************************************
1368 * DefMDIChildProcA (USER32.@)
1370 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1371 WPARAM wParam, LPARAM lParam )
1373 HWND client = GetParent(hwnd);
1374 MDICLIENTINFO *ci = get_client_info( client );
1376 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1378 hwnd = WIN_GetFullHandle( hwnd );
1379 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1381 switch (message)
1383 case WM_SETTEXT:
1384 DefWindowProcA(hwnd, message, wParam, lParam);
1385 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1386 MDI_UpdateFrameText( GetParent(client), client, NULL );
1387 return 1; /* success. FIXME: check text length */
1389 case WM_GETMINMAXINFO:
1390 case WM_MENUCHAR:
1391 case WM_CLOSE:
1392 case WM_SETFOCUS:
1393 case WM_CHILDACTIVATE:
1394 case WM_SYSCOMMAND:
1395 case WM_SHOWWINDOW:
1396 case WM_SETVISIBLE:
1397 case WM_SIZE:
1398 case WM_NEXTMENU:
1399 case WM_SYSCHAR:
1400 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1402 return DefWindowProcA(hwnd, message, wParam, lParam);
1406 /***********************************************************************
1407 * DefMDIChildProcW (USER32.@)
1409 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1410 WPARAM wParam, LPARAM lParam )
1412 HWND client = GetParent(hwnd);
1413 MDICLIENTINFO *ci = get_client_info( client );
1415 TRACE("%p %04x (%s) %08x %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1417 hwnd = WIN_GetFullHandle( hwnd );
1418 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1420 switch (message)
1422 case WM_SETTEXT:
1423 DefWindowProcW(hwnd, message, wParam, lParam);
1424 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1425 MDI_UpdateFrameText( GetParent(client), client, NULL );
1426 return 1; /* success. FIXME: check text length */
1428 case WM_GETMINMAXINFO:
1429 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1430 return 0;
1432 case WM_MENUCHAR:
1433 return 0x00010000; /* MDI children don't have menu bars */
1435 case WM_CLOSE:
1436 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1437 return 0;
1439 case WM_CHILDACTIVATE:
1440 MDI_ChildActivate( client, hwnd );
1441 return 0;
1443 case WM_SYSCOMMAND:
1444 switch( wParam )
1446 case SC_MOVE:
1447 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1448 return 0;
1449 break;
1450 case SC_RESTORE:
1451 case SC_MINIMIZE:
1452 break;
1453 case SC_MAXIMIZE:
1454 if (ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1455 return SendMessageW( GetParent(client), message, wParam, lParam);
1456 break;
1457 case SC_NEXTWINDOW:
1458 SendMessageW( client, WM_MDINEXT, 0, 0);
1459 return 0;
1460 case SC_PREVWINDOW:
1461 SendMessageW( client, WM_MDINEXT, 0, 1);
1462 return 0;
1464 break;
1466 case WM_SHOWWINDOW:
1467 case WM_SETVISIBLE:
1468 if (IsZoomed(ci->hwndActiveChild)) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1469 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1470 break;
1472 case WM_SIZE:
1473 if (wParam == SIZE_MAXIMIZED && !IsWindowVisible(hwnd))
1474 wParam = SIZE_RESTORED;
1476 if( hwnd == ci->hwndActiveChild && wParam != SIZE_MAXIMIZED )
1478 MDI_RestoreFrameMenu( GetParent(client), hwnd );
1479 MDI_UpdateFrameText( GetParent(client), client, NULL );
1482 if( wParam == SIZE_MAXIMIZED )
1484 TRACE("maximizing child %p\n", hwnd );
1486 MDI_AugmentFrameMenu( GetParent(client), hwnd );
1487 MDI_UpdateFrameText( GetParent(client), client, NULL );
1490 if( wParam == SIZE_MINIMIZED )
1492 HWND switchTo = MDI_GetWindow(ci, hwnd, TRUE, WS_MINIMIZE);
1494 if (switchTo) SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0);
1496 MDI_PostUpdate(client, ci, SB_BOTH+1);
1497 break;
1499 case WM_NEXTMENU:
1501 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1502 HWND parent = GetParent(client);
1504 if( wParam == VK_LEFT ) /* switch to frame system menu */
1506 WND *wndPtr = WIN_GetPtr( parent );
1507 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1508 WIN_ReleasePtr( wndPtr );
1510 if( wParam == VK_RIGHT ) /* to frame menu bar */
1512 next_menu->hmenuNext = GetMenu(parent);
1514 next_menu->hwndNext = parent;
1515 return 0;
1518 case WM_SYSCHAR:
1519 if (wParam == '-')
1521 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1522 return 0;
1524 break;
1526 return DefWindowProcW(hwnd, message, wParam, lParam);
1529 /**********************************************************************
1530 * CreateMDIWindowA (USER32.@) Creates a MDI child
1532 * RETURNS
1533 * Success: Handle to created window
1534 * Failure: NULL
1536 HWND WINAPI CreateMDIWindowA(
1537 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1538 LPCSTR lpWindowName, /* [in] Pointer to window name */
1539 DWORD dwStyle, /* [in] Window style */
1540 INT X, /* [in] Horizontal position of window */
1541 INT Y, /* [in] Vertical position of window */
1542 INT nWidth, /* [in] Width of window */
1543 INT nHeight, /* [in] Height of window */
1544 HWND hWndParent, /* [in] Handle to parent window */
1545 HINSTANCE hInstance, /* [in] Handle to application instance */
1546 LPARAM lParam) /* [in] Application-defined value */
1548 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1549 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1550 nWidth,nHeight,hWndParent,hInstance,lParam);
1552 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1553 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1554 0, hInstance, (LPVOID)lParam);
1557 /***********************************************************************
1558 * CreateMDIWindowW (USER32.@) Creates a MDI child
1560 * RETURNS
1561 * Success: Handle to created window
1562 * Failure: NULL
1564 HWND WINAPI CreateMDIWindowW(
1565 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1566 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1567 DWORD dwStyle, /* [in] Window style */
1568 INT X, /* [in] Horizontal position of window */
1569 INT Y, /* [in] Vertical position of window */
1570 INT nWidth, /* [in] Width of window */
1571 INT nHeight, /* [in] Height of window */
1572 HWND hWndParent, /* [in] Handle to parent window */
1573 HINSTANCE hInstance, /* [in] Handle to application instance */
1574 LPARAM lParam) /* [in] Application-defined value */
1576 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1577 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1578 nWidth, nHeight, hWndParent, hInstance, lParam);
1580 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1581 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1582 0, hInstance, (LPVOID)lParam);
1585 /**********************************************************************
1586 * TranslateMDISysAccel (USER32.@)
1588 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1590 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1592 MDICLIENTINFO *ci = get_client_info( hwndClient );
1593 WPARAM wParam = 0;
1595 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1597 /* translate if the Ctrl key is down and Alt not. */
1599 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1601 switch( msg->wParam )
1603 case VK_F6:
1604 case VK_TAB:
1605 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1606 break;
1607 case VK_F4:
1608 case VK_RBUTTON:
1609 wParam = SC_CLOSE;
1610 break;
1611 default:
1612 return 0;
1614 TRACE("wParam = %04x\n", wParam);
1615 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1616 return 1;
1619 return 0; /* failure */
1622 /***********************************************************************
1623 * CalcChildScroll (USER32.@)
1625 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1627 SCROLLINFO info;
1628 RECT childRect, clientRect;
1629 HWND *list;
1631 GetClientRect( hwnd, &clientRect );
1632 SetRectEmpty( &childRect );
1634 if ((list = WIN_ListChildren( hwnd )))
1636 int i;
1637 for (i = 0; list[i]; i++)
1639 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1640 if (style & WS_MAXIMIZE)
1642 HeapFree( GetProcessHeap(), 0, list );
1643 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1644 return;
1646 if (style & WS_VISIBLE)
1648 WND *pWnd = WIN_FindWndPtr( list[i] );
1649 UnionRect( &childRect, &pWnd->rectWindow, &childRect );
1650 WIN_ReleaseWndPtr( pWnd );
1653 HeapFree( GetProcessHeap(), 0, list );
1655 UnionRect( &childRect, &clientRect, &childRect );
1657 /* set common info values */
1658 info.cbSize = sizeof(info);
1659 info.fMask = SIF_POS | SIF_RANGE;
1661 /* set the specific */
1662 switch( scroll )
1664 case SB_BOTH:
1665 case SB_HORZ:
1666 info.nMin = childRect.left;
1667 info.nMax = childRect.right - clientRect.right;
1668 info.nPos = clientRect.left - childRect.left;
1669 SetScrollInfo(hwnd, scroll, &info, TRUE);
1670 if (scroll == SB_HORZ) break;
1671 /* fall through */
1672 case SB_VERT:
1673 info.nMin = childRect.top;
1674 info.nMax = childRect.bottom - clientRect.bottom;
1675 info.nPos = clientRect.top - childRect.top;
1676 SetScrollInfo(hwnd, scroll, &info, TRUE);
1677 break;
1682 /***********************************************************************
1683 * ScrollChildren (USER32.@)
1685 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1686 LPARAM lParam)
1688 INT newPos = -1;
1689 INT curPos, length, minPos, maxPos, shift;
1690 RECT rect;
1692 GetClientRect( hWnd, &rect );
1694 switch(uMsg)
1696 case WM_HSCROLL:
1697 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1698 curPos = GetScrollPos(hWnd,SB_HORZ);
1699 length = (rect.right - rect.left) / 2;
1700 shift = GetSystemMetrics(SM_CYHSCROLL);
1701 break;
1702 case WM_VSCROLL:
1703 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1704 curPos = GetScrollPos(hWnd,SB_VERT);
1705 length = (rect.bottom - rect.top) / 2;
1706 shift = GetSystemMetrics(SM_CXVSCROLL);
1707 break;
1708 default:
1709 return;
1712 switch( wParam )
1714 case SB_LINEUP:
1715 newPos = curPos - shift;
1716 break;
1717 case SB_LINEDOWN:
1718 newPos = curPos + shift;
1719 break;
1720 case SB_PAGEUP:
1721 newPos = curPos - length;
1722 break;
1723 case SB_PAGEDOWN:
1724 newPos = curPos + length;
1725 break;
1727 case SB_THUMBPOSITION:
1728 newPos = LOWORD(lParam);
1729 break;
1731 case SB_THUMBTRACK:
1732 return;
1734 case SB_TOP:
1735 newPos = minPos;
1736 break;
1737 case SB_BOTTOM:
1738 newPos = maxPos;
1739 break;
1740 case SB_ENDSCROLL:
1741 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1742 return;
1745 if( newPos > maxPos )
1746 newPos = maxPos;
1747 else
1748 if( newPos < minPos )
1749 newPos = minPos;
1751 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1753 if( uMsg == WM_VSCROLL )
1754 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1755 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1756 else
1757 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1758 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1762 /******************************************************************************
1763 * CascadeWindows (USER32.@) Cascades MDI child windows
1765 * RETURNS
1766 * Success: Number of cascaded windows.
1767 * Failure: 0
1769 WORD WINAPI
1770 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1771 UINT cKids, const HWND *lpKids)
1773 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1774 return 0;
1778 /******************************************************************************
1779 * TileWindows (USER32.@) Tiles MDI child windows
1781 * RETURNS
1782 * Success: Number of tiled windows.
1783 * Failure: 0
1785 WORD WINAPI
1786 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1787 UINT cKids, const HWND *lpKids)
1789 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1790 return 0;
1793 /************************************************************************
1794 * "More Windows..." functionality
1797 /* MDI_MoreWindowsDlgProc
1799 * This function will process the messages sent to the "More Windows..."
1800 * dialog.
1801 * Return values: 0 = cancel pressed
1802 * HWND = ok pressed or double-click in the list...
1806 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1808 switch (iMsg)
1810 case WM_INITDIALOG:
1812 UINT widest = 0;
1813 UINT length;
1814 UINT i;
1815 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1816 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1818 for (i = 0; i < ci->nActiveChildren; i++)
1820 WCHAR buffer[MDI_MAXTITLELENGTH];
1822 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1823 continue;
1824 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1825 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1826 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1827 if (length > widest)
1828 widest = length;
1830 /* Make sure the horizontal scrollbar scrolls ok */
1831 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1833 /* Set the current selection */
1834 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1835 return TRUE;
1838 case WM_COMMAND:
1839 switch (LOWORD(wParam))
1841 default:
1842 if (HIWORD(wParam) != LBN_DBLCLK) break;
1843 /* fall through */
1844 case IDOK:
1846 /* windows are sorted by menu ID, so we must return the
1847 * window associated to the given id
1849 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1850 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1851 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1852 EndDialog(hDlg, res);
1853 return TRUE;
1855 case IDCANCEL:
1856 EndDialog(hDlg, 0);
1857 return TRUE;
1859 break;
1861 return FALSE;
1866 * MDI_MoreWindowsDialog
1868 * Prompts the user with a listbox containing the opened
1869 * documents. The user can then choose a windows and click
1870 * on OK to set the current window to the one selected, or
1871 * CANCEL to cancel. The function returns a handle to the
1872 * selected window.
1875 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1877 LPCVOID template;
1878 HRSRC hRes;
1879 HANDLE hDlgTmpl;
1881 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1883 if (hRes == 0)
1884 return 0;
1886 hDlgTmpl = LoadResource(user32_module, hRes );
1888 if (hDlgTmpl == 0)
1889 return 0;
1891 template = LockResource( hDlgTmpl );
1893 if (template == 0)
1894 return 0;
1896 return (HWND) DialogBoxIndirectParamA(user32_module,
1897 (LPDLGTEMPLATEA) template,
1898 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);