Split OpenFile implementation in separate 16- and 32-bit versions, and
[wine.git] / windows / mdi.c
blob68450aa20333e299af6d45863effd6b4654f9433
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 "user.h"
99 #include "struct32.h"
100 #include "wine/debug.h"
101 #include "dlgs.h"
103 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
105 #define MDI_MAXTITLELENGTH 0xa1
107 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
109 /* "More Windows..." definitions */
110 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
111 option will appear under the Windows menu */
112 #define MDI_IDC_LISTBOX 100
113 #define IDS_MDI_MOREWINDOWS 13
115 #define MDIF_NEEDUPDATE 0x0001
117 typedef struct
119 UINT nActiveChildren;
120 HWND hwndActiveChild;
121 HWND *child; /* array of tracked children */
122 HMENU hFrameMenu;
123 HMENU hWindowMenu;
124 UINT idFirstChild;
125 LPWSTR frameTitle;
126 UINT nTotalCreated;
127 UINT mdiFlags;
128 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
129 } MDICLIENTINFO;
131 static HBITMAP hBmpClose = 0;
133 /* ----------------- declarations ----------------- */
134 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
135 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
136 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
137 static LONG MDI_ChildActivate( HWND, HWND );
138 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
140 static HWND MDI_MoreWindowsDialog(HWND);
141 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
142 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
144 /* -------- Miscellaneous service functions ----------
146 * MDI_GetChildByID
148 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
150 int i;
152 for (i = 0; ci->nActiveChildren; i++)
154 if (GetWindowLongW( ci->child[i], GWL_ID ) == id)
155 return ci->child[i];
157 return 0;
160 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
162 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
164 ci->mdiFlags |= MDIF_NEEDUPDATE;
165 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
167 ci->sbRecalc = recalc;
171 /*********************************************************************
172 * MDIClient class descriptor
174 const struct builtin_class_descr MDICLIENT_builtin_class =
176 "MDIClient", /* name */
177 0, /* style */
178 MDIClientWndProcA, /* procA */
179 MDIClientWndProcW, /* procW */
180 sizeof(MDICLIENTINFO), /* extra */
181 IDC_ARROW, /* cursor */
182 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
186 static MDICLIENTINFO *get_client_info( HWND client )
188 MDICLIENTINFO *ret = NULL;
189 WND *win = WIN_GetPtr( client );
190 if (win)
192 if (win == WND_OTHER_PROCESS)
194 ERR( "client %p belongs to other process\n", client );
195 return NULL;
197 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
198 else ret = (MDICLIENTINFO *)win->wExtra;
199 WIN_ReleasePtr( win );
201 return ret;
204 /**********************************************************************
205 * MDI_GetWindow
207 * returns "activateable" child different from the current or zero
209 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
210 DWORD dwStyleMask )
212 int i;
213 HWND *list;
214 HWND last = 0;
216 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
217 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
219 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
220 i = 0;
221 /* start from next after hWnd */
222 while (list[i] && list[i] != hWnd) i++;
223 if (list[i]) i++;
225 for ( ; list[i]; i++)
227 if (GetWindow( list[i], GW_OWNER )) continue;
228 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
229 last = list[i];
230 if (bNext) goto found;
232 /* now restart from the beginning */
233 for (i = 0; list[i] && list[i] != hWnd; i++)
235 if (GetWindow( list[i], GW_OWNER )) continue;
236 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
237 last = list[i];
238 if (bNext) goto found;
240 found:
241 HeapFree( GetProcessHeap(), 0, list );
242 return last;
245 /**********************************************************************
246 * MDI_CalcDefaultChildPos
248 * It seems that the default height is about 2/3 of the client rect
250 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta )
252 INT nstagger;
253 RECT rect;
254 INT spacing = GetSystemMetrics(SM_CYCAPTION) +
255 GetSystemMetrics(SM_CYFRAME) - 1;
257 if (total < 0)
259 MDICLIENTINFO *ci = get_client_info(hwndClient);
260 total = ci ? ci->nTotalCreated : 0;
263 GetClientRect( hwndClient, &rect );
264 if( rect.bottom - rect.top - delta >= spacing )
265 rect.bottom -= delta;
267 nstagger = (rect.bottom - rect.top)/(3 * spacing);
268 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
269 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
270 lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
273 /**********************************************************************
274 * MDISetMenu
276 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
277 HMENU hmenuWindow)
279 MDICLIENTINFO *ci;
280 HWND hwndFrame = GetParent(hwnd);
282 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
284 if (hmenuFrame && !IsMenu(hmenuFrame))
286 WARN("hmenuFrame is not a menu handle\n");
287 return 0L;
290 if (hmenuWindow && !IsMenu(hmenuWindow))
292 WARN("hmenuWindow is not a menu handle\n");
293 return 0L;
296 if (!(ci = get_client_info( hwnd ))) return 0;
298 if( IsZoomed(ci->hwndActiveChild) && hmenuFrame && hmenuFrame!= ci->hFrameMenu )
299 MDI_RestoreFrameMenu( hwndFrame, ci->hwndActiveChild );
301 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
303 /* delete menu items from ci->hWindowMenu
304 * and add them to hmenuWindow */
305 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
306 if( ci->hWindowMenu && ci->nActiveChildren )
308 UINT nActiveChildren_old = ci->nActiveChildren;
310 /* Remove all items from old Window menu */
311 ci->nActiveChildren = 0;
312 MDI_RefreshMenu(ci);
314 ci->hWindowMenu = hmenuWindow;
316 /* Add items to the new Window menu */
317 ci->nActiveChildren = nActiveChildren_old;
318 AppendMenuW(hmenuWindow, MF_SEPARATOR, 0, NULL);
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;
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, removes all existing
374 * menu items after it, and then adds visible MDI children.
376 separator_pos = 0;
377 count = GetMenuItemCount(ci->hWindowMenu);
378 for (i = 0; i < count; i++)
380 MENUITEMINFOW mii;
382 memset(&mii, 0, sizeof(mii));
383 mii.cbSize = sizeof(mii);
384 mii.fMask = MIIM_TYPE;
385 GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
387 if (mii.fType & MFT_SEPARATOR)
388 separator_pos = i;
391 for (i = separator_pos + 1; i < count; i++)
392 RemoveMenu(ci->hWindowMenu, separator_pos + 1, MF_BYPOSITION);
394 visible = 0;
395 for (i = 0; i < ci->nActiveChildren; i++)
397 UINT id = ci->idFirstChild + i;
399 if (visible == MDI_MOREWINDOWSLIMIT)
401 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
402 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
403 break;
406 if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
408 if (!visible && !separator_pos)
409 /* Visio expects that separator has id 0 */
410 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
412 visible++;
414 SetWindowLongW(ci->child[i], GWL_ID, id);
416 buf[0] = '&';
417 buf[1] = '0' + visible;
418 buf[2] = ' ';
419 InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
420 TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
421 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
423 if (ci->child[i] == ci->hwndActiveChild)
424 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
426 else
427 TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
430 return (LRESULT)ci->hFrameMenu;
434 /* ------------------ MDI child window functions ---------------------- */
436 /**********************************************************************
437 * MDI_ChildGetMinMaxInfo
439 * Note: The rule here is that client rect of the maximized MDI child
440 * is equal to the client rect of the MDI client window.
442 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
444 RECT rect;
446 GetClientRect( client, &rect );
447 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
448 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
450 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
451 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
453 lpMinMax->ptMaxPosition.x = rect.left;
454 lpMinMax->ptMaxPosition.y = rect.top;
456 TRACE("max rect (%ld,%ld - %ld, %ld)\n",
457 rect.left,rect.top,rect.right,rect.bottom);
460 /**********************************************************************
461 * MDI_SwitchActiveChild
463 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
464 * being activated
466 static void MDI_SwitchActiveChild( HWND clientHwnd, HWND childHwnd,
467 BOOL bNextWindow )
469 HWND hwndTo = 0;
470 HWND hwndPrev = 0;
471 MDICLIENTINFO *ci = get_client_info( clientHwnd );
473 hwndTo = MDI_GetWindow(ci, childHwnd, bNextWindow, 0);
474 hwndPrev = ci->hwndActiveChild;
476 TRACE("from %p, to %p\n",childHwnd,hwndTo);
478 if ( !hwndTo ) return; /* no window to switch to */
480 if ( hwndTo != hwndPrev )
482 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0,
483 SWP_NOMOVE | SWP_NOSIZE );
485 if( bNextWindow && hwndPrev )
486 SetWindowPos( hwndPrev, HWND_BOTTOM, 0, 0, 0, 0,
487 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE );
492 /**********************************************************************
493 * MDIDestroyChild
495 static LRESULT MDIDestroyChild( HWND parent, MDICLIENTINFO *ci,
496 HWND child, BOOL flagDestroy )
498 UINT i;
500 TRACE("# of managed children %u\n", ci->nActiveChildren);
502 if( child == ci->hwndActiveChild )
504 MDI_SwitchActiveChild(parent, child, TRUE);
506 if( child == ci->hwndActiveChild )
508 ShowWindow( child, SW_HIDE);
509 if( child == ci->hwndActiveChild && IsZoomed(ci->hwndActiveChild) )
511 HWND frame = GetParent(parent);
512 MDI_RestoreFrameMenu( frame, child );
513 MDI_UpdateFrameText( frame, parent, TRUE, NULL);
516 MDI_ChildActivate(parent, 0);
520 for (i = 0; i < ci->nActiveChildren; i++)
522 if (ci->child[i] == child)
524 HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
525 memcpy(new_child, ci->child, i * sizeof(HWND));
526 if (i + 1 < ci->nActiveChildren)
527 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
528 HeapFree(GetProcessHeap(), 0, ci->child);
529 ci->child = new_child;
531 ci->nActiveChildren--;
532 break;
536 if (flagDestroy)
538 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
539 DestroyWindow(child);
542 TRACE("child destroyed - %p\n", child);
543 return 0;
547 /**********************************************************************
548 * MDI_ChildActivate
550 * Note: hWndChild is NULL when last child is being destroyed
552 static LONG MDI_ChildActivate( HWND client, HWND child )
554 MDICLIENTINFO *clientInfo;
555 HWND prevActiveWnd;
556 BOOL isActiveFrameWnd;
558 if (child && (!IsWindowEnabled( child ))) return 0;
560 clientInfo = get_client_info( client );
562 TRACE("%p\n", child);
564 isActiveFrameWnd = (GetActiveWindow() == GetParent(client));
565 prevActiveWnd = clientInfo->hwndActiveChild;
567 /* deactivate prev. active child */
568 if(prevActiveWnd)
570 SetWindowLongW( prevActiveWnd, GWL_STYLE,
571 GetWindowLongW( prevActiveWnd, GWL_STYLE ) | WS_SYSMENU );
572 SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
573 SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
576 /* set appearance */
577 if (IsZoomed(clientInfo->hwndActiveChild) && clientInfo->hwndActiveChild != child)
579 INT cmd = SW_SHOWNORMAL;
581 if( child )
583 HMENU hSysMenu = GetSystemMenu(child, FALSE);
584 UINT state = 0;
585 if (hSysMenu)
586 state = GetMenuState(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
587 if (state != 0xFFFFFFFF && !(state & (MF_DISABLED | MF_GRAYED)))
589 SendMessageW(clientInfo->hwndActiveChild, WM_SYSCOMMAND, SC_RESTORE, 0);
590 cmd = SW_SHOWMAXIMIZED;
593 clientInfo->hwndActiveChild = child;
596 ShowWindow( clientInfo->hwndActiveChild, cmd );
599 clientInfo->hwndActiveChild = child;
601 /* check if we have any children left */
602 if( !child )
604 if( isActiveFrameWnd )
605 SetFocus( client );
606 return 0;
609 MDI_RefreshMenu(clientInfo);
611 /* bring active child to the top */
612 SetWindowPos( child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
614 if( isActiveFrameWnd )
616 SendMessageA( child, WM_NCACTIVATE, TRUE, 0L);
617 if( GetFocus() == client )
618 SendMessageA( client, WM_SETFOCUS, (WPARAM)client, 0L );
619 else
620 SetFocus( client );
622 SendMessageA( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
623 return TRUE;
626 /* -------------------- MDI client window functions ------------------- */
628 /**********************************************************************
629 * CreateMDIMenuBitmap
631 static HBITMAP CreateMDIMenuBitmap(void)
633 HDC hDCSrc = CreateCompatibleDC(0);
634 HDC hDCDest = CreateCompatibleDC(hDCSrc);
635 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
636 HBITMAP hbCopy;
637 HBITMAP hobjSrc, hobjDest;
639 hobjSrc = SelectObject(hDCSrc, hbClose);
640 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
641 hobjDest = SelectObject(hDCDest, hbCopy);
643 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
644 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
646 SelectObject(hDCSrc, hobjSrc);
647 DeleteObject(hbClose);
648 DeleteDC(hDCSrc);
650 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
652 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
653 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
655 SelectObject(hDCDest, hobjSrc );
656 SelectObject(hDCDest, hobjDest);
657 DeleteDC(hDCDest);
659 return hbCopy;
662 /**********************************************************************
663 * MDICascade
665 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
667 HWND *win_array;
668 BOOL has_icons = FALSE;
669 int i, total;
671 if (IsZoomed(ci->hwndActiveChild))
672 SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
674 if (ci->nActiveChildren == 0) return 0;
676 if (!(win_array = WIN_ListChildren( client ))) return 0;
678 /* remove all the windows we don't want */
679 for (i = total = 0; win_array[i]; i++)
681 if (!IsWindowVisible( win_array[i] )) continue;
682 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
683 if (IsIconic( win_array[i] ))
685 has_icons = TRUE;
686 continue;
688 win_array[total++] = win_array[i];
690 win_array[total] = 0;
692 if (total)
694 INT delta = 0, n = 0, i;
695 POINT pos[2];
696 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
698 /* walk the list (backwards) and move windows */
699 for (i = total - 1; i >= 0; i--)
701 TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
702 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
704 MDI_CalcDefaultChildPos(client, n++, pos, delta);
705 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
706 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
709 HeapFree( GetProcessHeap(), 0, win_array );
711 if (has_icons) ArrangeIconicWindows( client );
712 return 0;
715 /**********************************************************************
716 * MDITile
718 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
720 HWND *win_array;
721 int i, total;
722 BOOL has_icons = FALSE;
724 if (IsZoomed(ci->hwndActiveChild))
725 SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
727 if (ci->nActiveChildren == 0) return;
729 if (!(win_array = WIN_ListChildren( client ))) return;
731 /* remove all the windows we don't want */
732 for (i = total = 0; win_array[i]; i++)
734 if (!IsWindowVisible( win_array[i] )) continue;
735 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
736 if (IsIconic( win_array[i] ))
738 has_icons = TRUE;
739 continue;
741 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
742 win_array[total++] = win_array[i];
744 win_array[total] = 0;
746 TRACE("%u windows to tile\n", total);
748 if (total)
750 HWND *pWnd = win_array;
751 RECT rect;
752 int x, y, xsize, ysize;
753 int rows, columns, r, c, i;
755 GetClientRect(client,&rect);
756 rows = (int) sqrt((double)total);
757 columns = total / rows;
759 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
761 i = rows;
762 rows = columns; /* exchange r and c */
763 columns = i;
766 if (has_icons)
768 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
769 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
772 ysize = rect.bottom / rows;
773 xsize = rect.right / columns;
775 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
777 if (c == columns)
779 rows = total - i;
780 ysize = rect.bottom / rows;
783 y = 0;
784 for (r = 1; r <= rows && *pWnd; r++, i++)
786 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
787 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
788 y += ysize;
789 pWnd++;
791 x += xsize;
794 HeapFree( GetProcessHeap(), 0, win_array );
795 if (has_icons) ArrangeIconicWindows( client );
798 /* ----------------------- Frame window ---------------------------- */
801 /**********************************************************************
802 * MDI_AugmentFrameMenu
804 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
806 HMENU menu = GetMenu( frame );
807 HMENU hSysPopup = 0;
808 HBITMAP hSysMenuBitmap = 0;
809 INT nItems;
810 UINT iId;
812 TRACE("frame %p,child %p\n",frame,hChild);
814 if( !menu ) return 0;
816 /* if the system buttons already exist do not add them again */
817 nItems = GetMenuItemCount(menu) - 1;
818 iId = GetMenuItemID(menu,nItems) ;
819 if (iId == SC_RESTORE || iId == SC_CLOSE)
820 return 0;
822 /* create a copy of sysmenu popup and insert it into frame menu bar */
823 if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
824 return 0;
826 AppendMenuA(menu,MF_HELP | MF_BITMAP,
827 SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
828 AppendMenuA(menu,MF_HELP | MF_BITMAP,
829 SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
831 /* The close button is only present in Win 95 look */
832 if(TWEAK_WineLook > WIN31_LOOK)
834 AppendMenuA(menu,MF_HELP | MF_BITMAP,
835 SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
838 /* In Win 95 look, the system menu is replaced by the child icon */
840 if(TWEAK_WineLook > WIN31_LOOK)
842 HICON hIcon = (HICON)GetClassLongW(hChild, GCL_HICONSM);
843 if (!hIcon)
844 hIcon = (HICON)GetClassLongW(hChild, GCL_HICON);
845 if (!hIcon)
846 hIcon = LoadImageW(0, MAKEINTRESOURCEW(IDI_WINLOGO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
847 if (hIcon)
849 HDC hMemDC;
850 HBITMAP hBitmap, hOldBitmap;
851 HBRUSH hBrush;
852 HDC hdc = GetDC(hChild);
854 if (hdc)
856 int cx, cy;
857 cx = GetSystemMetrics(SM_CXSMICON);
858 cy = GetSystemMetrics(SM_CYSMICON);
859 hMemDC = CreateCompatibleDC(hdc);
860 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
861 hOldBitmap = SelectObject(hMemDC, hBitmap);
862 SetMapMode(hMemDC, MM_TEXT);
863 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
864 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
865 SelectObject (hMemDC, hOldBitmap);
866 DeleteObject(hBrush);
867 DeleteDC(hMemDC);
868 ReleaseDC(hChild, hdc);
869 hSysMenuBitmap = hBitmap;
873 else
874 hSysMenuBitmap = hBmpClose;
876 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
877 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
879 TRACE("not inserted\n");
880 DestroyMenu(hSysPopup);
881 return 0;
884 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
885 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
886 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
887 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
889 /* redraw menu */
890 DrawMenuBar(frame);
892 return 1;
895 /**********************************************************************
896 * MDI_RestoreFrameMenu
898 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
900 MENUITEMINFOW menuInfo;
901 HMENU menu = GetMenu( frame );
902 INT nItems = GetMenuItemCount(menu) - 1;
903 UINT iId = GetMenuItemID(menu,nItems) ;
905 TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
907 /* if there is no system buttons then nothing to do */
908 if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
909 return 0;
912 * Remove the system menu, If that menu is the icon of the window
913 * as it is in win95, we have to delete the bitmap.
915 memset(&menuInfo, 0, sizeof(menuInfo));
916 menuInfo.cbSize = sizeof(menuInfo);
917 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
919 GetMenuItemInfoW(menu,
921 TRUE,
922 &menuInfo);
924 RemoveMenu(menu,0,MF_BYPOSITION);
926 if ( (menuInfo.fType & MFT_BITMAP) &&
927 (LOWORD(menuInfo.dwTypeData)!=0) &&
928 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
930 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
933 if(TWEAK_WineLook > WIN31_LOOK)
935 /* close */
936 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
938 /* restore */
939 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
940 /* minimize */
941 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
943 DrawMenuBar(frame);
945 return 1;
949 /**********************************************************************
950 * MDI_UpdateFrameText
952 * used when child window is maximized/restored
954 * Note: lpTitle can be NULL
956 static void MDI_UpdateFrameText( HWND frame, HWND hClient,
957 BOOL repaint, LPCWSTR lpTitle )
959 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
960 MDICLIENTINFO *ci = get_client_info( hClient );
962 TRACE("repaint %i, frameText %s\n", repaint, debugstr_w(lpTitle));
964 if (!ci) return;
966 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
968 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
969 lpTitle = lpBuffer;
972 /* store new "default" title if lpTitle is not NULL */
973 if (lpTitle)
975 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
976 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
977 strcpyW( ci->frameTitle, lpTitle );
980 if (ci->frameTitle)
982 if (IsZoomed(ci->hwndActiveChild) && IsWindowVisible(ci->hwndActiveChild))
984 /* combine frame title and child title if possible */
986 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
987 static const WCHAR lpBracket2[] = {']',0};
988 int i_frame_text_length = strlenW(ci->frameTitle);
990 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
992 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
994 strcatW( lpBuffer, lpBracket );
995 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
996 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
997 strcatW( lpBuffer, lpBracket2 );
998 else
999 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1002 else
1004 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1007 else
1008 lpBuffer[0] = '\0';
1010 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1011 if( repaint )
1012 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1013 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1017 /* ----------------------------- Interface ---------------------------- */
1020 /**********************************************************************
1021 * MDIClientWndProc_common
1023 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1024 WPARAM wParam, LPARAM lParam, BOOL unicode )
1026 MDICLIENTINFO *ci;
1028 TRACE("%p %04x %08x %08lx\n", hwnd, message, wParam, lParam);
1030 if (!(ci = get_client_info( hwnd ))) return 0;
1032 switch (message)
1034 case WM_CREATE:
1036 /* Since we are using only cs->lpCreateParams, we can safely
1037 * cast to LPCREATESTRUCTA here */
1038 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1039 WND *wndPtr = WIN_GetPtr( hwnd );
1041 wndPtr->flags |= WIN_ISMDICLIENT;
1043 /* Translation layer doesn't know what's in the cs->lpCreateParams
1044 * so we have to keep track of what environment we're in. */
1046 if( wndPtr->flags & WIN_ISWIN32 )
1048 LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1049 ci->hWindowMenu = ccs->hWindowMenu;
1050 ci->idFirstChild = ccs->idFirstChild;
1052 else
1054 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1055 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1056 ci->idFirstChild = ccs->idFirstChild;
1058 WIN_ReleasePtr( wndPtr );
1060 ci->child = NULL;
1061 ci->nActiveChildren = 0;
1062 ci->nTotalCreated = 0;
1063 ci->frameTitle = NULL;
1064 ci->mdiFlags = 0;
1065 ci->hFrameMenu = GetMenu(cs->hwndParent);
1067 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1069 TRACE("Client created - hwnd = %p, idFirst = %u\n", hwnd, ci->idFirstChild );
1070 return 0;
1073 case WM_DESTROY:
1075 if( IsZoomed(ci->hwndActiveChild) )
1076 MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndActiveChild);
1078 ci->nActiveChildren = 0;
1079 MDI_RefreshMenu(ci);
1081 if (ci->child) HeapFree( GetProcessHeap(), 0, ci->child );
1082 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1084 return 0;
1087 case WM_MDIACTIVATE:
1089 HWND hwndActive;
1091 hwndActive = ci->hwndActiveChild;
1093 if( hwndActive != (HWND)wParam )
1094 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1095 return 0;
1098 case WM_MDICASCADE:
1099 return MDICascade(hwnd, ci);
1101 case WM_MDICREATE:
1102 if (lParam)
1104 if (unicode)
1106 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1107 return (LRESULT)CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1108 csW->szTitle, csW->style,
1109 csW->x, csW->y, csW->cx, csW->cy,
1110 hwnd, 0, csW->hOwner,
1111 (LPVOID)csW->lParam);
1113 else
1115 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1116 return (LRESULT)CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1117 csA->szTitle, csA->style,
1118 csA->x, csA->y, csA->cx, csA->cy,
1119 hwnd, 0, csA->hOwner,
1120 (LPVOID)csA->lParam);
1123 return 0;
1125 case WM_MDIDESTROY:
1126 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1128 case WM_MDIGETACTIVE:
1129 if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1130 return (LRESULT)ci->hwndActiveChild;
1132 case WM_MDIICONARRANGE:
1133 ci->mdiFlags |= MDIF_NEEDUPDATE;
1134 ArrangeIconicWindows( hwnd );
1135 ci->sbRecalc = SB_BOTH+1;
1136 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1137 return 0;
1139 case WM_MDIMAXIMIZE:
1140 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1141 return 0;
1143 case WM_MDINEXT: /* lParam != 0 means previous window */
1144 MDI_SwitchActiveChild( hwnd, WIN_GetFullHandle( (HWND)wParam ), !lParam );
1145 break;
1147 case WM_MDIRESTORE:
1148 SendMessageW( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1149 return 0;
1151 case WM_MDISETMENU:
1152 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1154 case WM_MDIREFRESHMENU:
1155 return MDI_RefreshMenu( ci );
1157 case WM_MDITILE:
1158 ci->mdiFlags |= MDIF_NEEDUPDATE;
1159 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1160 MDITile( hwnd, ci, wParam );
1161 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1162 return 0;
1164 case WM_VSCROLL:
1165 case WM_HSCROLL:
1166 ci->mdiFlags |= MDIF_NEEDUPDATE;
1167 ScrollChildren( hwnd, message, wParam, lParam );
1168 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1169 return 0;
1171 case WM_SETFOCUS:
1172 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1173 SetFocus( ci->hwndActiveChild );
1174 return 0;
1176 case WM_NCACTIVATE:
1177 if( ci->hwndActiveChild )
1178 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1179 break;
1181 case WM_PARENTNOTIFY:
1182 switch (LOWORD(wParam))
1184 case WM_CREATE:
1185 if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1187 ci->nTotalCreated++;
1188 ci->nActiveChildren++;
1190 if (!ci->child)
1191 ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1192 else
1193 ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1195 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1197 break;
1199 case WM_LBUTTONDOWN:
1201 HWND child;
1202 POINT pt;
1203 pt.x = (short)LOWORD(lParam);
1204 pt.y = (short)HIWORD(lParam);
1205 child = ChildWindowFromPoint(hwnd, pt);
1207 TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1209 if( child && child != hwnd && child != ci->hwndActiveChild )
1210 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1211 break;
1214 return 0;
1216 case WM_SIZE:
1217 if( IsWindow(ci->hwndActiveChild) && IsZoomed(ci->hwndActiveChild) &&
1218 IsWindowVisible(ci->hwndActiveChild) )
1220 RECT rect;
1222 rect.left = 0;
1223 rect.top = 0;
1224 rect.right = LOWORD(lParam);
1225 rect.bottom = HIWORD(lParam);
1227 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1228 0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1229 MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1230 rect.right - rect.left, rect.bottom - rect.top, 1);
1232 else
1233 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1235 break;
1237 case WM_MDICALCCHILDSCROLL:
1238 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1240 CalcChildScroll(hwnd, ci->sbRecalc-1);
1241 ci->sbRecalc = 0;
1242 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1244 return 0;
1246 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1247 DefWindowProcA( hwnd, message, wParam, lParam );
1250 /***********************************************************************
1251 * MDIClientWndProcA
1253 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1255 if (!IsWindow(hwnd)) return 0;
1256 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1259 /***********************************************************************
1260 * MDIClientWndProcW
1262 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1264 if (!IsWindow(hwnd)) return 0;
1265 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1268 /***********************************************************************
1269 * DefFrameProcA (USER32.@)
1271 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1272 UINT message, WPARAM wParam, LPARAM lParam)
1274 if (hwndMDIClient)
1276 switch (message)
1278 case WM_SETTEXT:
1280 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1281 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1282 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1283 MDI_UpdateFrameText(hwnd, hwndMDIClient, TRUE, text );
1284 HeapFree( GetProcessHeap(), 0, text );
1286 return 1; /* success. FIXME: check text length */
1288 case WM_COMMAND:
1289 case WM_NCACTIVATE:
1290 case WM_NEXTMENU:
1291 case WM_SETFOCUS:
1292 case WM_SIZE:
1293 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1296 return DefWindowProcA(hwnd, message, wParam, lParam);
1300 /***********************************************************************
1301 * DefFrameProcW (USER32.@)
1303 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1304 UINT message, WPARAM wParam, LPARAM lParam)
1306 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1308 TRACE("%p %p %04x %08x %08lx\n", hwnd, hwndMDIClient, message, wParam, lParam);
1310 if (ci)
1312 switch (message)
1314 case WM_COMMAND:
1316 WORD id = LOWORD(wParam);
1317 /* check for possible syscommands for maximized MDI child */
1318 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1320 if( (id - 0xf000) & 0xf00f ) break;
1321 if( !IsZoomed(ci->hwndActiveChild) ) break;
1322 switch( id )
1324 case SC_SIZE:
1325 case SC_MOVE:
1326 case SC_MINIMIZE:
1327 case SC_MAXIMIZE:
1328 case SC_NEXTWINDOW:
1329 case SC_PREVWINDOW:
1330 case SC_CLOSE:
1331 case SC_RESTORE:
1332 return SendMessageW( ci->hwndActiveChild, WM_SYSCOMMAND,
1333 wParam, lParam);
1336 else
1338 HWND childHwnd;
1339 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1340 /* User chose "More Windows..." */
1341 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1342 else
1343 /* User chose one of the windows listed in the "Windows" menu */
1344 childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1346 if( childHwnd )
1347 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1350 break;
1352 case WM_NCACTIVATE:
1353 SendMessageW(hwndMDIClient, message, wParam, lParam);
1354 break;
1356 case WM_SETTEXT:
1357 MDI_UpdateFrameText(hwnd, hwndMDIClient, TRUE, (LPWSTR)lParam );
1358 return 1; /* success. FIXME: check text length */
1360 case WM_SETFOCUS:
1361 SetFocus(hwndMDIClient);
1362 break;
1364 case WM_SIZE:
1365 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1366 break;
1368 case WM_NEXTMENU:
1370 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1372 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1374 /* control menu is between the frame system menu and
1375 * the first entry of menu bar */
1376 WND *wndPtr = WIN_GetPtr(hwnd);
1378 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1379 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1381 WIN_ReleasePtr(wndPtr);
1382 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1383 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1384 next_menu->hwndNext = ci->hwndActiveChild;
1386 WIN_ReleasePtr(wndPtr);
1388 return 0;
1393 return DefWindowProcW( hwnd, message, wParam, lParam );
1396 /***********************************************************************
1397 * DefMDIChildProcA (USER32.@)
1399 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1400 WPARAM wParam, LPARAM lParam )
1402 HWND client = GetParent(hwnd);
1403 MDICLIENTINFO *ci = get_client_info( client );
1405 TRACE("%p %04x %08x %08lx\n", hwnd, message, wParam, lParam);
1407 hwnd = WIN_GetFullHandle( hwnd );
1408 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1410 switch (message)
1412 case WM_SETTEXT:
1413 DefWindowProcA(hwnd, message, wParam, lParam);
1414 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1415 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1416 return 1; /* success. FIXME: check text length */
1418 case WM_GETMINMAXINFO:
1419 case WM_MENUCHAR:
1420 case WM_CLOSE:
1421 case WM_SETFOCUS:
1422 case WM_CHILDACTIVATE:
1423 case WM_SYSCOMMAND:
1424 case WM_SETVISIBLE:
1425 case WM_SIZE:
1426 case WM_NEXTMENU:
1427 case WM_SYSCHAR:
1428 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1430 return DefWindowProcA(hwnd, message, wParam, lParam);
1434 /***********************************************************************
1435 * DefMDIChildProcW (USER32.@)
1437 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1438 WPARAM wParam, LPARAM lParam )
1440 HWND client = GetParent(hwnd);
1441 MDICLIENTINFO *ci = get_client_info( client );
1443 TRACE("%p %04x %08x %08lx\n", hwnd, message, wParam, lParam);
1445 hwnd = WIN_GetFullHandle( hwnd );
1446 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1448 switch (message)
1450 case WM_SETTEXT:
1451 DefWindowProcW(hwnd, message, wParam, lParam);
1452 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1453 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1454 return 1; /* success. FIXME: check text length */
1456 case WM_GETMINMAXINFO:
1457 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1458 return 0;
1460 case WM_MENUCHAR:
1461 return 0x00010000; /* MDI children don't have menu bars */
1463 case WM_CLOSE:
1464 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1465 return 0;
1467 case WM_SETFOCUS:
1468 if (ci->hwndActiveChild != hwnd) MDI_ChildActivate( client, hwnd );
1469 break;
1471 case WM_CHILDACTIVATE:
1472 MDI_ChildActivate( client, hwnd );
1473 return 0;
1475 case WM_SYSCOMMAND:
1476 switch( wParam )
1478 case SC_MOVE:
1479 if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1480 return 0;
1481 break;
1482 case SC_RESTORE:
1483 case SC_MINIMIZE:
1484 SetWindowLongW( hwnd, GWL_STYLE,
1485 GetWindowLongW( hwnd, GWL_STYLE ) | WS_SYSMENU );
1486 break;
1487 case SC_MAXIMIZE:
1488 if (ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild))
1489 return SendMessageW( GetParent(client), message, wParam, lParam);
1490 SetWindowLongW( hwnd, GWL_STYLE,
1491 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_SYSMENU );
1492 break;
1493 case SC_NEXTWINDOW:
1494 SendMessageW( client, WM_MDINEXT, 0, 0);
1495 return 0;
1496 case SC_PREVWINDOW:
1497 SendMessageW( client, WM_MDINEXT, 0, 1);
1498 return 0;
1500 break;
1502 case WM_SETVISIBLE:
1503 if (IsZoomed(ci->hwndActiveChild)) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1504 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1505 break;
1507 case WM_SIZE:
1508 if (wParam == SIZE_MAXIMIZED && !IsWindowVisible(hwnd))
1509 wParam = SIZE_RESTORED;
1511 if( hwnd == ci->hwndActiveChild && wParam != SIZE_MAXIMIZED )
1513 MDI_RestoreFrameMenu( GetParent(client), hwnd );
1514 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1517 if( wParam == SIZE_MAXIMIZED )
1519 TRACE("maximizing child %p\n", hwnd );
1521 MDI_AugmentFrameMenu( GetParent(client), hwnd );
1522 MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1525 if( wParam == SIZE_MINIMIZED )
1527 HWND switchTo = MDI_GetWindow(ci, hwnd, TRUE, WS_MINIMIZE);
1529 if (switchTo) SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0);
1531 MDI_PostUpdate(client, ci, SB_BOTH+1);
1532 break;
1534 case WM_NEXTMENU:
1536 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1537 HWND parent = GetParent(client);
1539 if( wParam == VK_LEFT ) /* switch to frame system menu */
1541 WND *wndPtr = WIN_GetPtr( parent );
1542 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1543 WIN_ReleasePtr( wndPtr );
1545 if( wParam == VK_RIGHT ) /* to frame menu bar */
1547 next_menu->hmenuNext = GetMenu(parent);
1549 next_menu->hwndNext = parent;
1550 return 0;
1553 case WM_SYSCHAR:
1554 if (wParam == '-')
1556 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1557 return 0;
1559 break;
1561 return DefWindowProcW(hwnd, message, wParam, lParam);
1564 /**********************************************************************
1565 * CreateMDIWindowA (USER32.@) Creates a MDI child
1567 * RETURNS
1568 * Success: Handle to created window
1569 * Failure: NULL
1571 HWND WINAPI CreateMDIWindowA(
1572 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1573 LPCSTR lpWindowName, /* [in] Pointer to window name */
1574 DWORD dwStyle, /* [in] Window style */
1575 INT X, /* [in] Horizontal position of window */
1576 INT Y, /* [in] Vertical position of window */
1577 INT nWidth, /* [in] Width of window */
1578 INT nHeight, /* [in] Height of window */
1579 HWND hWndParent, /* [in] Handle to parent window */
1580 HINSTANCE hInstance, /* [in] Handle to application instance */
1581 LPARAM lParam) /* [in] Application-defined value */
1583 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1584 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1585 nWidth,nHeight,hWndParent,hInstance,lParam);
1587 return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1588 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1589 0, hInstance, (LPVOID)lParam);
1592 /***********************************************************************
1593 * CreateMDIWindowW (USER32.@) Creates a MDI child
1595 * RETURNS
1596 * Success: Handle to created window
1597 * Failure: NULL
1599 HWND WINAPI CreateMDIWindowW(
1600 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1601 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1602 DWORD dwStyle, /* [in] Window style */
1603 INT X, /* [in] Horizontal position of window */
1604 INT Y, /* [in] Vertical position of window */
1605 INT nWidth, /* [in] Width of window */
1606 INT nHeight, /* [in] Height of window */
1607 HWND hWndParent, /* [in] Handle to parent window */
1608 HINSTANCE hInstance, /* [in] Handle to application instance */
1609 LPARAM lParam) /* [in] Application-defined value */
1611 TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1612 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1613 nWidth, nHeight, hWndParent, hInstance, lParam);
1615 return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1616 dwStyle, X, Y, nWidth, nHeight, hWndParent,
1617 0, hInstance, (LPVOID)lParam);
1620 /**********************************************************************
1621 * TranslateMDISysAccel (USER32.@)
1623 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1625 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1627 MDICLIENTINFO *ci = get_client_info( hwndClient );
1628 WPARAM wParam = 0;
1630 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1632 /* translate if the Ctrl key is down and Alt not. */
1634 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1636 switch( msg->wParam )
1638 case VK_F6:
1639 case VK_TAB:
1640 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1641 break;
1642 case VK_F4:
1643 case VK_RBUTTON:
1644 wParam = SC_CLOSE;
1645 break;
1646 default:
1647 return 0;
1649 TRACE("wParam = %04x\n", wParam);
1650 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1651 return 1;
1654 return 0; /* failure */
1657 /***********************************************************************
1658 * CalcChildScroll (USER32.@)
1660 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1662 SCROLLINFO info;
1663 RECT childRect, clientRect;
1664 HWND *list;
1666 GetClientRect( hwnd, &clientRect );
1667 SetRectEmpty( &childRect );
1669 if ((list = WIN_ListChildren( hwnd )))
1671 int i;
1672 for (i = 0; list[i]; i++)
1674 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1675 if (style & WS_MAXIMIZE)
1677 HeapFree( GetProcessHeap(), 0, list );
1678 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1679 return;
1681 if (style & WS_VISIBLE)
1683 WND *pWnd = WIN_FindWndPtr( list[i] );
1684 UnionRect( &childRect, &pWnd->rectWindow, &childRect );
1685 WIN_ReleaseWndPtr( pWnd );
1688 HeapFree( GetProcessHeap(), 0, list );
1690 UnionRect( &childRect, &clientRect, &childRect );
1692 /* set common info values */
1693 info.cbSize = sizeof(info);
1694 info.fMask = SIF_POS | SIF_RANGE;
1696 /* set the specific */
1697 switch( scroll )
1699 case SB_BOTH:
1700 case SB_HORZ:
1701 info.nMin = childRect.left;
1702 info.nMax = childRect.right - clientRect.right;
1703 info.nPos = clientRect.left - childRect.left;
1704 SetScrollInfo(hwnd, scroll, &info, TRUE);
1705 if (scroll == SB_HORZ) break;
1706 /* fall through */
1707 case SB_VERT:
1708 info.nMin = childRect.top;
1709 info.nMax = childRect.bottom - clientRect.bottom;
1710 info.nPos = clientRect.top - childRect.top;
1711 SetScrollInfo(hwnd, scroll, &info, TRUE);
1712 break;
1717 /***********************************************************************
1718 * ScrollChildren (USER32.@)
1720 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1721 LPARAM lParam)
1723 INT newPos = -1;
1724 INT curPos, length, minPos, maxPos, shift;
1725 RECT rect;
1727 GetClientRect( hWnd, &rect );
1729 switch(uMsg)
1731 case WM_HSCROLL:
1732 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1733 curPos = GetScrollPos(hWnd,SB_HORZ);
1734 length = (rect.right - rect.left) / 2;
1735 shift = GetSystemMetrics(SM_CYHSCROLL);
1736 break;
1737 case WM_VSCROLL:
1738 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1739 curPos = GetScrollPos(hWnd,SB_VERT);
1740 length = (rect.bottom - rect.top) / 2;
1741 shift = GetSystemMetrics(SM_CXVSCROLL);
1742 break;
1743 default:
1744 return;
1747 switch( wParam )
1749 case SB_LINEUP:
1750 newPos = curPos - shift;
1751 break;
1752 case SB_LINEDOWN:
1753 newPos = curPos + shift;
1754 break;
1755 case SB_PAGEUP:
1756 newPos = curPos - length;
1757 break;
1758 case SB_PAGEDOWN:
1759 newPos = curPos + length;
1760 break;
1762 case SB_THUMBPOSITION:
1763 newPos = LOWORD(lParam);
1764 break;
1766 case SB_THUMBTRACK:
1767 return;
1769 case SB_TOP:
1770 newPos = minPos;
1771 break;
1772 case SB_BOTTOM:
1773 newPos = maxPos;
1774 break;
1775 case SB_ENDSCROLL:
1776 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1777 return;
1780 if( newPos > maxPos )
1781 newPos = maxPos;
1782 else
1783 if( newPos < minPos )
1784 newPos = minPos;
1786 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1788 if( uMsg == WM_VSCROLL )
1789 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1790 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1791 else
1792 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1793 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1797 /******************************************************************************
1798 * CascadeWindows (USER32.@) Cascades MDI child windows
1800 * RETURNS
1801 * Success: Number of cascaded windows.
1802 * Failure: 0
1804 WORD WINAPI
1805 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1806 UINT cKids, const HWND *lpKids)
1808 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1809 return 0;
1813 /******************************************************************************
1814 * TileWindows (USER32.@) Tiles MDI child windows
1816 * RETURNS
1817 * Success: Number of tiled windows.
1818 * Failure: 0
1820 WORD WINAPI
1821 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
1822 UINT cKids, const HWND *lpKids)
1824 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1825 return 0;
1828 /************************************************************************
1829 * "More Windows..." functionality
1832 /* MDI_MoreWindowsDlgProc
1834 * This function will process the messages sent to the "More Windows..."
1835 * dialog.
1836 * Return values: 0 = cancel pressed
1837 * HWND = ok pressed or double-click in the list...
1841 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1843 switch (iMsg)
1845 case WM_INITDIALOG:
1847 UINT widest = 0;
1848 UINT length;
1849 UINT i;
1850 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1851 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1853 for (i = 0; i < ci->nActiveChildren; i++)
1855 WCHAR buffer[MDI_MAXTITLELENGTH];
1857 if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1858 continue;
1859 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1860 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1861 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
1862 if (length > widest)
1863 widest = length;
1865 /* Make sure the horizontal scrollbar scrolls ok */
1866 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1868 /* Set the current selection */
1869 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1870 return TRUE;
1873 case WM_COMMAND:
1874 switch (LOWORD(wParam))
1876 default:
1877 if (HIWORD(wParam) != LBN_DBLCLK) break;
1878 /* fall through */
1879 case IDOK:
1881 /* windows are sorted by menu ID, so we must return the
1882 * window associated to the given id
1884 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1885 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1886 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1887 EndDialog(hDlg, res);
1888 return TRUE;
1890 case IDCANCEL:
1891 EndDialog(hDlg, 0);
1892 return TRUE;
1894 break;
1896 return FALSE;
1901 * MDI_MoreWindowsDialog
1903 * Prompts the user with a listbox containing the opened
1904 * documents. The user can then choose a windows and click
1905 * on OK to set the current window to the one selected, or
1906 * CANCEL to cancel. The function returns a handle to the
1907 * selected window.
1910 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1912 LPCVOID template;
1913 HRSRC hRes;
1914 HANDLE hDlgTmpl;
1916 hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1918 if (hRes == 0)
1919 return 0;
1921 hDlgTmpl = LoadResource(user32_module, hRes );
1923 if (hDlgTmpl == 0)
1924 return 0;
1926 template = LockResource( hDlgTmpl );
1928 if (template == 0)
1929 return 0;
1931 return (HWND) DialogBoxIndirectParamA(user32_module,
1932 (LPDLGTEMPLATEA) template,
1933 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);