Moved debug output up before calling the backend.
[wine/wine-kai.git] / windows / mdi.c
blob8661587f627d6512e68ae01fcb35f3430f2cbc61
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_MAXLISTLENGTH 0x40
106 #define MDI_MAXTITLELENGTH 0xa1
108 #define MDI_NOFRAMEREPAINT 0
109 #define MDI_REPAINTFRAMENOW 1
110 #define MDI_REPAINTFRAME 2
112 #define WM_MDICALCCHILDSCROLL 0x10ac /* this is exactly what Windows uses */
114 /* "More Windows..." definitions */
115 #define MDI_MOREWINDOWSLIMIT 9 /* after this number of windows, a "More Windows..."
116 option will appear under the Windows menu */
117 #define MDI_IDC_LISTBOX 100
118 #define IDS_MDI_MOREWINDOWS 13
120 #define MDIF_NEEDUPDATE 0x0001
122 typedef struct
124 UINT nActiveChildren;
125 HWND hwndChildMaximized;
126 HWND hwndActiveChild;
127 HMENU hWindowMenu;
128 UINT idFirstChild;
129 LPWSTR frameTitle;
130 UINT nTotalCreated;
131 UINT mdiFlags;
132 UINT sbRecalc; /* SB_xxx flags for scrollbar fixup */
133 } MDICLIENTINFO;
135 static HBITMAP hBmpClose = 0;
137 /* ----------------- declarations ----------------- */
138 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
139 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
140 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
141 static LONG MDI_ChildActivate( HWND, HWND );
143 static HWND MDI_MoreWindowsDialog(HWND);
144 static void MDI_SwapMenuItems(HWND, UINT, UINT);
145 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
146 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
148 /* -------- Miscellaneous service functions ----------
150 * MDI_GetChildByID
152 static HWND MDI_GetChildByID(HWND hwnd, UINT id)
154 HWND ret;
155 HWND *win_array;
156 int i;
158 if (!(win_array = WIN_ListChildren( hwnd ))) return 0;
159 for (i = 0; win_array[i]; i++)
161 if (GetWindowLongA( win_array[i], GWL_ID ) == id) break;
163 ret = win_array[i];
164 HeapFree( GetProcessHeap(), 0, win_array );
165 return ret;
168 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
170 if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
172 ci->mdiFlags |= MDIF_NEEDUPDATE;
173 PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
175 ci->sbRecalc = recalc;
179 /*********************************************************************
180 * MDIClient class descriptor
182 const struct builtin_class_descr MDICLIENT_builtin_class =
184 "MDIClient", /* name */
185 CS_GLOBALCLASS, /* style */
186 MDIClientWndProcA, /* procA */
187 MDIClientWndProcW, /* procW */
188 sizeof(MDICLIENTINFO), /* extra */
189 IDC_ARROW, /* cursor */
190 (HBRUSH)(COLOR_APPWORKSPACE+1) /* brush */
194 static MDICLIENTINFO *get_client_info( HWND client )
196 MDICLIENTINFO *ret = NULL;
197 WND *win = WIN_GetPtr( client );
198 if (win)
200 if (win == WND_OTHER_PROCESS)
202 ERR( "client %p belongs to other process\n", client );
203 return NULL;
205 if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
206 else ret = (MDICLIENTINFO *)win->wExtra;
207 WIN_ReleasePtr( win );
209 return ret;
212 /**********************************************************************
213 * MDI_MenuModifyItem
215 static void MDI_MenuModifyItem( HWND client, HWND hWndChild )
217 MDICLIENTINFO *clientInfo = get_client_info( client );
218 WCHAR buffer[128];
219 UINT n, id;
221 if (!clientInfo || !clientInfo->hWindowMenu) return;
223 id = GetWindowLongA( hWndChild, GWL_ID );
224 if (id >= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT) return;
225 buffer[0] = '&';
226 buffer[1] = '1' + id - clientInfo->idFirstChild;
227 buffer[2] = ' ';
228 GetWindowTextW( hWndChild, buffer + 3, sizeof(buffer)/sizeof(WCHAR) - 3 );
230 n = GetMenuState(clientInfo->hWindowMenu, id, MF_BYCOMMAND);
231 ModifyMenuW(clientInfo->hWindowMenu, id, MF_BYCOMMAND | MF_STRING, id, buffer );
232 CheckMenuItem(clientInfo->hWindowMenu, id, n & MF_CHECKED);
235 /**********************************************************************
236 * MDI_MenuDeleteItem
238 static BOOL MDI_MenuDeleteItem( HWND client, HWND hWndChild )
240 WCHAR buffer[128];
241 static const WCHAR format[] = {'&','%','d',' ',0};
242 MDICLIENTINFO *clientInfo = get_client_info( client );
243 UINT index = 0,id,n;
245 if( !clientInfo->nActiveChildren || !clientInfo->hWindowMenu )
246 return FALSE;
248 id = GetWindowLongA( hWndChild, GWL_ID );
249 DeleteMenu(clientInfo->hWindowMenu,id,MF_BYCOMMAND);
251 /* walk the rest of MDI children to prevent gaps in the id
252 * sequence and in the menu child list */
254 for( index = id+1; index <= clientInfo->nActiveChildren +
255 clientInfo->idFirstChild; index++ )
257 HWND hwnd = MDI_GetChildByID(client,index);
258 if (!hwnd)
260 TRACE("no window for id=%i\n",index);
261 continue;
264 /* set correct id */
265 SetWindowLongW( hwnd, GWL_ID, GetWindowLongW( hwnd, GWL_ID ) - 1 );
267 n = wsprintfW(buffer, format ,index - clientInfo->idFirstChild);
268 GetWindowTextW( hwnd, buffer + n, sizeof(buffer)/sizeof(WCHAR) - n );
270 /* change menu if the current child is to be shown in the
271 * "Windows" menu
273 if (index <= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT)
274 ModifyMenuW(clientInfo->hWindowMenu ,index ,MF_BYCOMMAND | MF_STRING,
275 index - 1 , buffer );
278 /* We must restore the "More Windows..." option if there are enough children
280 if (clientInfo->nActiveChildren - 1 > MDI_MOREWINDOWSLIMIT)
282 WCHAR szTmp[50];
283 LoadStringW(GetModuleHandleA("USER32"), IDS_MDI_MOREWINDOWS, szTmp, sizeof(szTmp)/sizeof(szTmp[0]));
284 AppendMenuW(clientInfo->hWindowMenu, MF_STRING, clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT, szTmp);
286 return TRUE;
289 /**********************************************************************
290 * MDI_GetWindow
292 * returns "activateable" child different from the current or zero
294 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
295 DWORD dwStyleMask )
297 int i;
298 HWND *list;
299 HWND last = 0;
301 dwStyleMask |= WS_DISABLED | WS_VISIBLE;
302 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
304 if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
305 i = 0;
306 /* start from next after hWnd */
307 while (list[i] && list[i] != hWnd) i++;
308 if (list[i]) i++;
310 for ( ; list[i]; i++)
312 if (GetWindow( list[i], GW_OWNER )) continue;
313 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
314 last = list[i];
315 if (bNext) goto found;
317 /* now restart from the beginning */
318 for (i = 0; list[i] && list[i] != hWnd; i++)
320 if (GetWindow( list[i], GW_OWNER )) continue;
321 if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
322 last = list[i];
323 if (bNext) goto found;
325 found:
326 HeapFree( GetProcessHeap(), 0, list );
327 return last;
330 /**********************************************************************
331 * MDI_CalcDefaultChildPos
333 * It seems that the default height is about 2/3 of the client rect
335 static void MDI_CalcDefaultChildPos( HWND hwnd, WORD n, LPPOINT lpPos, INT delta)
337 INT nstagger;
338 RECT rect;
339 INT spacing = GetSystemMetrics(SM_CYCAPTION) +
340 GetSystemMetrics(SM_CYFRAME) - 1;
342 GetClientRect( hwnd, &rect );
343 if( rect.bottom - rect.top - delta >= spacing )
344 rect.bottom -= delta;
346 nstagger = (rect.bottom - rect.top)/(3 * spacing);
347 lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
348 lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
349 lpPos[0].x = lpPos[0].y = spacing * (n%(nstagger+1));
352 /**********************************************************************
353 * MDISetMenu
355 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
356 HMENU hmenuWindow)
358 MDICLIENTINFO *ci;
359 HWND hwndFrame = GetParent(hwnd);
360 HMENU oldFrameMenu = GetMenu(hwndFrame);
362 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
364 if (hmenuFrame && !IsMenu(hmenuFrame))
366 WARN("hmenuFrame is not a menu handle\n");
367 return 0L;
370 if (hmenuWindow && !IsMenu(hmenuWindow))
372 WARN("hmenuWindow is not a menu handle\n");
373 return 0L;
376 if (!(ci = get_client_info( hwnd ))) return 0;
378 if( ci->hwndChildMaximized && hmenuFrame && hmenuFrame!=oldFrameMenu )
379 MDI_RestoreFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
381 if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
383 /* delete menu items from ci->hWindowMenu
384 * and add them to hmenuWindow */
385 /* Agent newsreader calls this function with ci->hWindowMenu == NULL */
386 if( ci->hWindowMenu && ci->nActiveChildren )
388 INT j;
389 LPWSTR buffer = NULL;
390 MENUITEMINFOW mii;
391 INT nbWindowsMenuItems; /* num of documents shown + "More Windows..." if present */
392 INT i = GetMenuItemCount(ci->hWindowMenu) - 1;
393 INT pos = GetMenuItemCount(hmenuWindow) + 1;
395 AppendMenuA( hmenuWindow, MF_SEPARATOR, 0, NULL);
397 if (ci->nActiveChildren <= MDI_MOREWINDOWSLIMIT)
398 nbWindowsMenuItems = ci->nActiveChildren;
399 else
400 nbWindowsMenuItems = MDI_MOREWINDOWSLIMIT + 1;
402 j = i - nbWindowsMenuItems + 1;
404 for( ; i >= j ; i-- )
406 memset(&mii, 0, sizeof(mii));
407 mii.cbSize = sizeof(mii);
408 mii.fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_ID | MIIM_STATE
409 | MIIM_SUBMENU | MIIM_TYPE | MIIM_BITMAP;
411 GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
412 if(mii.cch) { /* Menu is MFT_STRING */
413 mii.cch++; /* add room for '\0' */
414 buffer = HeapAlloc(GetProcessHeap(), 0,
415 mii.cch * sizeof(WCHAR));
416 mii.dwTypeData = buffer;
417 GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
419 DeleteMenu(ci->hWindowMenu, i, MF_BYPOSITION);
420 InsertMenuItemW(hmenuWindow, pos, TRUE, &mii);
421 if(buffer) {
422 HeapFree(GetProcessHeap(), 0, buffer);
423 buffer = NULL;
426 /* remove separator */
427 DeleteMenu(ci->hWindowMenu, i, MF_BYPOSITION);
429 ci->hWindowMenu = hmenuWindow;
432 if (hmenuFrame)
434 SetMenu(hwndFrame, hmenuFrame);
435 if( hmenuFrame!=oldFrameMenu )
437 if( ci->hwndChildMaximized )
438 MDI_AugmentFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
439 return (LRESULT)oldFrameMenu;
442 else
444 HMENU menu = GetMenu( GetParent(hwnd) );
445 INT nItems = GetMenuItemCount(menu) - 1;
446 UINT iId = GetMenuItemID(menu,nItems) ;
448 if( !(iId == SC_RESTORE || iId == SC_CLOSE) )
450 /* SetMenu() may already have been called, meaning that this window
451 * already has its menu. But they may have done a SetMenu() on
452 * an MDI window, and called MDISetMenu() after the fact, meaning
453 * that the "if" to this "else" wouldn't catch the need to
454 * augment the frame menu.
456 if( ci->hwndChildMaximized )
457 MDI_AugmentFrameMenu( GetParent(hwnd), ci->hwndChildMaximized );
460 return 0;
463 /**********************************************************************
464 * MDIRefreshMenu
466 static LRESULT MDIRefreshMenu( HWND hwnd, HMENU hmenuFrame,
467 HMENU hmenuWindow)
469 HWND hwndFrame = GetParent(hwnd);
470 HMENU oldFrameMenu = GetMenu(hwndFrame);
472 TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
474 FIXME("partially function stub\n");
476 return (LRESULT)oldFrameMenu;
480 /* ------------------ MDI child window functions ---------------------- */
483 /**********************************************************************
484 * MDICreateChild
486 static HWND MDICreateChild( HWND parent, MDICLIENTINFO *ci,
487 LPMDICREATESTRUCTA cs, BOOL unicode )
489 POINT pos[2];
490 DWORD style = cs->style | (WS_CHILD | WS_CLIPSIBLINGS);
491 HWND hwnd, hwndMax = 0;
492 UINT wIDmenu = ci->idFirstChild + ci->nActiveChildren;
493 WND *wndParent;
494 static const WCHAR lpstrDef[] = {'j','u','n','k','!',0};
496 TRACE("origin %i,%i - dim %i,%i, style %08lx\n",
497 cs->x, cs->y, cs->cx, cs->cy, cs->style);
498 /* calculate placement */
499 MDI_CalcDefaultChildPos(parent, ci->nTotalCreated++, pos, 0);
501 if (cs->cx == CW_USEDEFAULT || cs->cx == CW_USEDEFAULT16 || !cs->cx) cs->cx = pos[1].x;
502 if (cs->cy == CW_USEDEFAULT || cs->cy == CW_USEDEFAULT16 || !cs->cy) cs->cy = pos[1].y;
504 if (cs->x == CW_USEDEFAULT || cs->x == CW_USEDEFAULT16)
506 cs->x = pos[0].x;
507 cs->y = pos[0].y;
510 /* restore current maximized child */
511 if( (style & WS_VISIBLE) && ci->hwndChildMaximized )
513 TRACE("Restoring current maximized child %p\n", ci->hwndChildMaximized);
514 if( style & WS_MAXIMIZE )
515 SendMessageW(parent, WM_SETREDRAW, FALSE, 0L);
516 hwndMax = ci->hwndChildMaximized;
517 ShowWindow( hwndMax, SW_SHOWNOACTIVATE );
518 if( style & WS_MAXIMIZE )
519 SendMessageW(parent, WM_SETREDRAW, TRUE, 0L);
522 if (ci->nActiveChildren <= MDI_MOREWINDOWSLIMIT)
523 /* this menu is needed to set a check mark in MDI_ChildActivate */
524 if (ci->hWindowMenu != 0)
525 AppendMenuW(ci->hWindowMenu, MF_STRING, wIDmenu, lpstrDef);
527 ci->nActiveChildren++;
529 /* fix window style */
530 wndParent = WIN_FindWndPtr( parent );
531 if( !(wndParent->dwStyle & MDIS_ALLCHILDSTYLES) )
533 TRACE("MDIS_ALLCHILDSTYLES is missing, fixing window style\n");
534 style &= (WS_CHILD | WS_CLIPSIBLINGS | WS_MINIMIZE | WS_MAXIMIZE |
535 WS_CLIPCHILDREN | WS_DISABLED | WS_VSCROLL | WS_HSCROLL );
536 style |= (WS_VISIBLE | WS_OVERLAPPEDWINDOW);
539 if( wndParent->flags & WIN_ISWIN32 )
541 WIN_ReleaseWndPtr( wndParent );
542 if(unicode)
544 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)cs;
545 hwnd = CreateWindowW( csW->szClass, csW->szTitle, style,
546 csW->x, csW->y, csW->cx, csW->cy, parent,
547 (HMENU)wIDmenu, csW->hOwner, csW );
549 else
550 hwnd = CreateWindowA( cs->szClass, cs->szTitle, style,
551 cs->x, cs->y, cs->cx, cs->cy, parent,
552 (HMENU)wIDmenu, cs->hOwner, cs );
554 else
556 MDICREATESTRUCT16 cs16;
557 SEGPTR title, cls, seg_cs16;
559 WIN_ReleaseWndPtr( wndParent );
560 STRUCT32_MDICREATESTRUCT32Ato16( cs, &cs16 );
561 cs16.szTitle = title = MapLS( cs->szTitle );
562 cs16.szClass = cls = MapLS( cs->szClass );
563 seg_cs16 = MapLS( &cs16 );
564 hwnd = WIN_Handle32( CreateWindow16( cs->szClass, cs->szTitle, style,
565 cs16.x, cs16.y, cs16.cx, cs16.cy,
566 HWND_16(parent), (HMENU16)wIDmenu,
567 cs16.hOwner, (LPVOID)seg_cs16 ));
568 UnMapLS( seg_cs16 );
569 UnMapLS( title );
570 UnMapLS( cls );
573 /* MDI windows are WS_CHILD so they won't be activated by CreateWindow */
575 if (hwnd)
577 /* All MDI child windows have the WS_EX_MDICHILD style */
578 SetWindowLongW( hwnd, GWL_EXSTYLE, GetWindowLongW( hwnd, GWL_EXSTYLE ) | WS_EX_MDICHILD );
580 /* If we have more than 9 windows, we must insert the new one at the
581 * 9th position in order to see it in the "Windows" menu
583 if (ci->nActiveChildren > MDI_MOREWINDOWSLIMIT)
584 MDI_SwapMenuItems( parent, GetWindowLongW( hwnd, GWL_ID ),
585 ci->idFirstChild + MDI_MOREWINDOWSLIMIT - 1);
587 MDI_MenuModifyItem(parent, hwnd);
589 /* Have we hit the "More Windows..." limit? If so, we must
590 * add a "More Windows..." option
592 if (ci->nActiveChildren == MDI_MOREWINDOWSLIMIT + 1)
594 WCHAR szTmp[50];
595 LoadStringW(GetModuleHandleA("USER32"), IDS_MDI_MOREWINDOWS, szTmp, sizeof(szTmp)/sizeof(szTmp[0]));
597 ModifyMenuW(ci->hWindowMenu,
598 ci->idFirstChild + MDI_MOREWINDOWSLIMIT,
599 MF_BYCOMMAND | MF_STRING,
600 ci->idFirstChild + MDI_MOREWINDOWSLIMIT,
601 szTmp);
604 if( IsIconic(hwnd) && ci->hwndActiveChild )
606 TRACE("Minimizing created MDI child %p\n", hwnd);
607 ShowWindow( hwnd, SW_SHOWMINNOACTIVE );
609 else
611 /* WS_VISIBLE is clear if a) the MDI client has
612 * MDIS_ALLCHILDSTYLES style and 2) the flag is cleared in the
613 * MDICreateStruct. If so the created window is not shown nor
614 * activated.
616 if (IsWindowVisible(hwnd)) ShowWindow(hwnd, SW_SHOW);
618 TRACE("created child - %p\n",hwnd);
620 else
622 ci->nActiveChildren--;
623 DeleteMenu(ci->hWindowMenu,wIDmenu,MF_BYCOMMAND);
624 if( IsWindow(hwndMax) )
625 ShowWindow(hwndMax, SW_SHOWMAXIMIZED);
628 return hwnd;
631 /**********************************************************************
632 * MDI_ChildGetMinMaxInfo
634 * Note: The rule here is that client rect of the maximized MDI child
635 * is equal to the client rect of the MDI client window.
637 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
639 RECT rect;
641 GetClientRect( client, &rect );
642 AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
643 0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
645 lpMinMax->ptMaxSize.x = rect.right -= rect.left;
646 lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
648 lpMinMax->ptMaxPosition.x = rect.left;
649 lpMinMax->ptMaxPosition.y = rect.top;
651 TRACE("max rect (%ld,%ld - %ld, %ld)\n",
652 rect.left,rect.top,rect.right,rect.bottom);
655 /**********************************************************************
656 * MDI_SwitchActiveChild
658 * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
659 * being activated
661 static void MDI_SwitchActiveChild( HWND clientHwnd, HWND childHwnd,
662 BOOL bNextWindow )
664 HWND hwndTo = 0;
665 HWND hwndPrev = 0;
666 MDICLIENTINFO *ci = get_client_info( clientHwnd );
668 hwndTo = MDI_GetWindow(ci, childHwnd, bNextWindow, 0);
670 TRACE("from %p, to %p\n",childHwnd,hwndTo);
672 if ( !hwndTo ) return; /* no window to switch to */
674 hwndPrev = ci->hwndActiveChild;
676 if ( hwndTo != hwndPrev )
678 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0,
679 SWP_NOMOVE | SWP_NOSIZE );
681 if( bNextWindow && hwndPrev )
682 SetWindowPos( hwndPrev, HWND_BOTTOM, 0, 0, 0, 0,
683 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE );
688 /**********************************************************************
689 * MDIDestroyChild
691 static LRESULT MDIDestroyChild( HWND parent, MDICLIENTINFO *ci,
692 HWND child, BOOL flagDestroy )
694 if( child == ci->hwndActiveChild )
696 MDI_SwitchActiveChild(parent, child, TRUE);
698 if( child == ci->hwndActiveChild )
700 ShowWindow( child, SW_HIDE);
701 if( child == ci->hwndChildMaximized )
703 HWND frame = GetParent(parent);
704 MDI_RestoreFrameMenu( frame, child );
705 ci->hwndChildMaximized = 0;
706 MDI_UpdateFrameText( frame, parent, TRUE, NULL);
709 MDI_ChildActivate(parent, 0);
713 MDI_MenuDeleteItem(parent, child);
715 ci->nActiveChildren--;
717 TRACE("child destroyed - %p\n",child);
719 if (flagDestroy)
721 MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
722 DestroyWindow(child);
724 return 0;
728 /**********************************************************************
729 * MDI_ChildActivate
731 * Note: hWndChild is NULL when last child is being destroyed
733 static LONG MDI_ChildActivate( HWND client, HWND child )
735 MDICLIENTINFO *clientInfo = get_client_info( client );
736 HWND prevActiveWnd = clientInfo->hwndActiveChild;
737 BOOL isActiveFrameWnd;
739 if (child && (!IsWindowEnabled( child ))) return 0;
741 /* Don't activate if it is already active. Might happen
742 since ShowWindow DOES activate MDI children */
743 if (clientInfo->hwndActiveChild == child) return 0;
745 TRACE("%p\n", child);
747 isActiveFrameWnd = (GetActiveWindow() == GetParent(client));
749 /* deactivate prev. active child */
750 if(prevActiveWnd)
752 SetWindowLongA( prevActiveWnd, GWL_STYLE,
753 GetWindowLongA( prevActiveWnd, GWL_STYLE ) | WS_SYSMENU );
754 SendMessageA( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
755 SendMessageA( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
756 /* uncheck menu item */
757 if( clientInfo->hWindowMenu )
759 UINT prevID = GetWindowLongA( prevActiveWnd, GWL_ID );
761 if (prevID - clientInfo->idFirstChild < MDI_MOREWINDOWSLIMIT)
762 CheckMenuItem( clientInfo->hWindowMenu, prevID, 0);
763 else
764 CheckMenuItem( clientInfo->hWindowMenu,
765 clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1, 0);
769 /* set appearance */
770 if (clientInfo->hwndChildMaximized && clientInfo->hwndChildMaximized != child)
772 INT cmd = SW_SHOWNORMAL;
774 if( child )
776 UINT state = GetMenuState(GetSystemMenu(child, FALSE), SC_MAXIMIZE, MF_BYCOMMAND);
777 if (state != 0xFFFFFFFF && (state & (MF_DISABLED | MF_GRAYED)))
778 SendMessageW(clientInfo->hwndChildMaximized, WM_SYSCOMMAND, SC_RESTORE, 0);
779 else
780 cmd = SW_SHOWMAXIMIZED;
782 clientInfo->hwndActiveChild = child;
785 ShowWindow( clientInfo->hwndActiveChild, cmd );
788 clientInfo->hwndActiveChild = child;
790 /* check if we have any children left */
791 if( !child )
793 if( isActiveFrameWnd )
794 SetFocus( client );
795 return 0;
798 /* check menu item */
799 if( clientInfo->hWindowMenu )
801 UINT id = GetWindowLongA( child, GWL_ID );
802 /* The window to be activated must be displayed in the "Windows" menu */
803 if (id >= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT)
805 MDI_SwapMenuItems( GetParent(child),
806 id, clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1);
807 id = clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1;
808 MDI_MenuModifyItem( GetParent(child), child );
811 CheckMenuItem(clientInfo->hWindowMenu, id, MF_CHECKED);
813 /* bring active child to the top */
814 SetWindowPos( child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
816 if( isActiveFrameWnd )
818 SendMessageA( child, WM_NCACTIVATE, TRUE, 0L);
819 if( GetFocus() == client )
820 SendMessageA( client, WM_SETFOCUS, (WPARAM)client, 0L );
821 else
822 SetFocus( client );
824 SendMessageA( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
825 return TRUE;
828 /* -------------------- MDI client window functions ------------------- */
830 /**********************************************************************
831 * CreateMDIMenuBitmap
833 static HBITMAP CreateMDIMenuBitmap(void)
835 HDC hDCSrc = CreateCompatibleDC(0);
836 HDC hDCDest = CreateCompatibleDC(hDCSrc);
837 HBITMAP hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
838 HBITMAP hbCopy;
839 HBITMAP hobjSrc, hobjDest;
841 hobjSrc = SelectObject(hDCSrc, hbClose);
842 hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
843 hobjDest = SelectObject(hDCDest, hbCopy);
845 BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
846 hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
848 SelectObject(hDCSrc, hobjSrc);
849 DeleteObject(hbClose);
850 DeleteDC(hDCSrc);
852 hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
854 MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
855 LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
857 SelectObject(hDCDest, hobjSrc );
858 SelectObject(hDCDest, hobjDest);
859 DeleteDC(hDCDest);
861 return hbCopy;
864 /**********************************************************************
865 * MDICascade
867 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
869 HWND *win_array;
870 BOOL has_icons = FALSE;
871 int i, total;
873 if (ci->hwndChildMaximized)
874 SendMessageA( client, WM_MDIRESTORE,
875 (WPARAM)ci->hwndChildMaximized, 0);
877 if (ci->nActiveChildren == 0) return 0;
879 if (!(win_array = WIN_ListChildren( client ))) return 0;
881 /* remove all the windows we don't want */
882 for (i = total = 0; win_array[i]; i++)
884 if (!IsWindowVisible( win_array[i] )) continue;
885 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
886 if (IsIconic( win_array[i] ))
888 has_icons = TRUE;
889 continue;
891 win_array[total++] = win_array[i];
893 win_array[total] = 0;
895 if (total)
897 INT delta = 0, n = 0, i;
898 POINT pos[2];
899 if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
901 /* walk the list (backwards) and move windows */
902 for (i = total - 1; i >= 0; i--)
904 TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
905 win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
907 MDI_CalcDefaultChildPos(client, n++, pos, delta);
908 SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
909 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
912 HeapFree( GetProcessHeap(), 0, win_array );
914 if (has_icons) ArrangeIconicWindows( client );
915 return 0;
918 /**********************************************************************
919 * MDITile
921 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
923 HWND *win_array;
924 int i, total;
925 BOOL has_icons = FALSE;
927 if (ci->hwndChildMaximized)
928 SendMessageA( client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
930 if (ci->nActiveChildren == 0) return;
932 if (!(win_array = WIN_ListChildren( client ))) return;
934 /* remove all the windows we don't want */
935 for (i = total = 0; win_array[i]; i++)
937 if (!IsWindowVisible( win_array[i] )) continue;
938 if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
939 if (IsIconic( win_array[i] ))
941 has_icons = TRUE;
942 continue;
944 if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
945 win_array[total++] = win_array[i];
947 win_array[total] = 0;
949 TRACE("%u windows to tile\n", total);
951 if (total)
953 HWND *pWnd = win_array;
954 RECT rect;
955 int x, y, xsize, ysize;
956 int rows, columns, r, c, i;
958 GetClientRect(client,&rect);
959 rows = (int) sqrt((double)total);
960 columns = total / rows;
962 if( wParam & MDITILE_HORIZONTAL ) /* version >= 3.1 */
964 i = rows;
965 rows = columns; /* exchange r and c */
966 columns = i;
969 if (has_icons)
971 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
972 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
975 ysize = rect.bottom / rows;
976 xsize = rect.right / columns;
978 for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
980 if (c == columns)
982 rows = total - i;
983 ysize = rect.bottom / rows;
986 y = 0;
987 for (r = 1; r <= rows && *pWnd; r++, i++)
989 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
990 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
991 y += ysize;
992 pWnd++;
994 x += xsize;
997 HeapFree( GetProcessHeap(), 0, win_array );
998 if (has_icons) ArrangeIconicWindows( client );
1001 /* ----------------------- Frame window ---------------------------- */
1004 /**********************************************************************
1005 * MDI_AugmentFrameMenu
1007 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
1009 HMENU menu = GetMenu( frame );
1010 WND* child = WIN_FindWndPtr(hChild);
1011 HMENU hSysPopup = 0;
1012 HBITMAP hSysMenuBitmap = 0;
1014 TRACE("frame %p,child %p\n",frame,hChild);
1016 if( !menu || !child->hSysMenu )
1018 WIN_ReleaseWndPtr(child);
1019 return 0;
1021 WIN_ReleaseWndPtr(child);
1023 /* create a copy of sysmenu popup and insert it into frame menu bar */
1025 if (!(hSysPopup = LoadMenuA(GetModuleHandleA("USER32"), "SYSMENU")))
1026 return 0;
1028 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1029 SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
1030 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1031 SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
1033 /* In Win 95 look, the system menu is replaced by the child icon */
1035 if(TWEAK_WineLook > WIN31_LOOK)
1037 HICON hIcon = (HICON)GetClassLongA(hChild, GCL_HICONSM);
1038 if (!hIcon)
1039 hIcon = (HICON)GetClassLongA(hChild, GCL_HICON);
1040 if (hIcon)
1042 HDC hMemDC;
1043 HBITMAP hBitmap, hOldBitmap;
1044 HBRUSH hBrush;
1045 HDC hdc = GetDC(hChild);
1047 if (hdc)
1049 int cx, cy;
1050 cx = GetSystemMetrics(SM_CXSMICON);
1051 cy = GetSystemMetrics(SM_CYSMICON);
1052 hMemDC = CreateCompatibleDC(hdc);
1053 hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
1054 hOldBitmap = SelectObject(hMemDC, hBitmap);
1055 SetMapMode(hMemDC, MM_TEXT);
1056 hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
1057 DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
1058 SelectObject (hMemDC, hOldBitmap);
1059 DeleteObject(hBrush);
1060 DeleteDC(hMemDC);
1061 ReleaseDC(hChild, hdc);
1062 hSysMenuBitmap = hBitmap;
1066 else
1067 hSysMenuBitmap = hBmpClose;
1069 if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
1070 (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
1072 TRACE("not inserted\n");
1073 DestroyMenu(hSysPopup);
1074 return 0;
1077 /* The close button is only present in Win 95 look */
1078 if(TWEAK_WineLook > WIN31_LOOK)
1080 AppendMenuA(menu,MF_HELP | MF_BITMAP,
1081 SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
1084 EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
1085 EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
1086 EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
1087 SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
1089 /* redraw menu */
1090 DrawMenuBar(frame);
1092 return 1;
1095 /**********************************************************************
1096 * MDI_RestoreFrameMenu
1098 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
1100 MENUITEMINFOW menuInfo;
1101 HMENU menu = GetMenu( frame );
1102 INT nItems = GetMenuItemCount(menu) - 1;
1103 UINT iId = GetMenuItemID(menu,nItems) ;
1105 TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
1107 if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
1108 return 0;
1111 * Remove the system menu, If that menu is the icon of the window
1112 * as it is in win95, we have to delete the bitmap.
1114 memset(&menuInfo, 0, sizeof(menuInfo));
1115 menuInfo.cbSize = sizeof(menuInfo);
1116 menuInfo.fMask = MIIM_DATA | MIIM_TYPE;
1118 GetMenuItemInfoW(menu,
1120 TRUE,
1121 &menuInfo);
1123 RemoveMenu(menu,0,MF_BYPOSITION);
1125 if ( (menuInfo.fType & MFT_BITMAP) &&
1126 (LOWORD(menuInfo.dwTypeData)!=0) &&
1127 (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
1129 DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
1132 if(TWEAK_WineLook > WIN31_LOOK)
1134 /* close */
1135 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1137 /* restore */
1138 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1139 /* minimize */
1140 DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
1142 DrawMenuBar(frame);
1144 return 1;
1148 /**********************************************************************
1149 * MDI_UpdateFrameText
1151 * used when child window is maximized/restored
1153 * Note: lpTitle can be NULL
1155 static void MDI_UpdateFrameText( HWND frame, HWND hClient,
1156 BOOL repaint, LPCWSTR lpTitle )
1158 WCHAR lpBuffer[MDI_MAXTITLELENGTH+1];
1159 MDICLIENTINFO *ci = get_client_info( hClient );
1161 TRACE("repaint %i, frameText %s\n", repaint, debugstr_w(lpTitle));
1163 if (!ci) return;
1165 if (!lpTitle && !ci->frameTitle) /* first time around, get title from the frame window */
1167 GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
1168 lpTitle = lpBuffer;
1171 /* store new "default" title if lpTitle is not NULL */
1172 if (lpTitle)
1174 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1175 if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
1176 strcpyW( ci->frameTitle, lpTitle );
1179 if (ci->frameTitle)
1181 if (ci->hwndChildMaximized)
1183 /* combine frame title and child title if possible */
1185 static const WCHAR lpBracket[] = {' ','-',' ','[',0};
1186 static const WCHAR lpBracket2[] = {']',0};
1187 int i_frame_text_length = strlenW(ci->frameTitle);
1189 lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1191 if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1193 strcatW( lpBuffer, lpBracket );
1194 if (GetWindowTextW( ci->hwndChildMaximized, lpBuffer + i_frame_text_length + 4,
1195 MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1196 strcatW( lpBuffer, lpBracket2 );
1197 else
1198 lpBuffer[i_frame_text_length] = 0; /* remove bracket */
1201 else
1203 lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1206 else
1207 lpBuffer[0] = '\0';
1209 DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1210 if( repaint == MDI_REPAINTFRAME)
1211 SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1212 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1216 /* ----------------------------- Interface ---------------------------- */
1219 /**********************************************************************
1220 * MDIClientWndProc_common
1222 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1223 WPARAM wParam, LPARAM lParam, BOOL unicode )
1225 MDICLIENTINFO *ci;
1227 if (!(ci = get_client_info( hwnd ))) return 0;
1229 switch (message)
1231 case WM_CREATE:
1233 RECT rect;
1234 /* Since we are using only cs->lpCreateParams, we can safely
1235 * cast to LPCREATESTRUCTA here */
1236 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1237 WND *wndPtr = WIN_GetPtr( hwnd );
1239 /* Translation layer doesn't know what's in the cs->lpCreateParams
1240 * so we have to keep track of what environment we're in. */
1242 if( wndPtr->flags & WIN_ISWIN32 )
1244 #define ccs ((LPCLIENTCREATESTRUCT)cs->lpCreateParams)
1245 ci->hWindowMenu = ccs->hWindowMenu;
1246 ci->idFirstChild = ccs->idFirstChild;
1247 #undef ccs
1249 else
1251 LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1252 ci->hWindowMenu = HMENU_32(ccs->hWindowMenu);
1253 ci->idFirstChild = ccs->idFirstChild;
1255 WIN_ReleasePtr( wndPtr );
1257 ci->hwndChildMaximized = 0;
1258 ci->nActiveChildren = 0;
1259 ci->nTotalCreated = 0;
1260 ci->frameTitle = NULL;
1261 ci->mdiFlags = 0;
1262 SetWindowLongW( hwnd, GWL_STYLE, GetWindowLongW(hwnd,GWL_STYLE) | WS_CLIPCHILDREN );
1264 if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1266 if (ci->hWindowMenu != 0)
1267 AppendMenuW( ci->hWindowMenu, MF_SEPARATOR, 0, NULL );
1269 GetClientRect( GetParent(hwnd), &rect);
1270 MoveWindow( hwnd, 0, 0, rect.right, rect.bottom, FALSE );
1272 MDI_UpdateFrameText( GetParent(hwnd), hwnd, MDI_NOFRAMEREPAINT, NULL);
1274 TRACE("Client created - hwnd = %p, idFirst = %u\n", hwnd, ci->idFirstChild );
1275 return 0;
1278 case WM_DESTROY:
1280 INT nItems;
1281 if( ci->hwndChildMaximized )
1282 MDI_RestoreFrameMenu( GetParent(hwnd), ci->hwndChildMaximized);
1283 if((ci->hWindowMenu != 0) &&
1284 (nItems = GetMenuItemCount(ci->hWindowMenu)) > 0)
1286 ci->idFirstChild = nItems - 1;
1287 ci->nActiveChildren++; /* to delete a separator */
1288 while( ci->nActiveChildren-- )
1289 DeleteMenu(ci->hWindowMenu,MF_BYPOSITION,ci->idFirstChild--);
1291 if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1292 return 0;
1295 case WM_MDIACTIVATE:
1296 if( ci->hwndActiveChild != (HWND)wParam )
1297 SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1298 return 0;
1300 case WM_MDICASCADE:
1301 return MDICascade(hwnd, ci);
1303 case WM_MDICREATE:
1304 if (lParam)
1305 return (LRESULT)MDICreateChild( hwnd, ci, (MDICREATESTRUCTA *)lParam, unicode );
1306 return 0;
1308 case WM_MDIDESTROY:
1309 return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1311 case WM_MDIGETACTIVE:
1312 if (lParam) *(BOOL *)lParam = (ci->hwndChildMaximized != 0);
1313 return (LRESULT)ci->hwndActiveChild;
1315 case WM_MDIICONARRANGE:
1316 ci->mdiFlags |= MDIF_NEEDUPDATE;
1317 ArrangeIconicWindows( hwnd );
1318 ci->sbRecalc = SB_BOTH+1;
1319 SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1320 return 0;
1322 case WM_MDIMAXIMIZE:
1323 ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1324 return 0;
1326 case WM_MDINEXT: /* lParam != 0 means previous window */
1327 MDI_SwitchActiveChild( hwnd, WIN_GetFullHandle( (HWND)wParam ), !lParam );
1328 break;
1330 case WM_MDIRESTORE:
1331 SendMessageW( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1332 return 0;
1334 case WM_MDISETMENU:
1335 return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1337 case WM_MDIREFRESHMENU:
1338 return MDIRefreshMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1340 case WM_MDITILE:
1341 ci->mdiFlags |= MDIF_NEEDUPDATE;
1342 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1343 MDITile( hwnd, ci, wParam );
1344 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1345 return 0;
1347 case WM_VSCROLL:
1348 case WM_HSCROLL:
1349 ci->mdiFlags |= MDIF_NEEDUPDATE;
1350 ScrollChildren( hwnd, message, wParam, lParam );
1351 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1352 return 0;
1354 case WM_SETFOCUS:
1355 if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1356 SetFocus( ci->hwndActiveChild );
1357 return 0;
1359 case WM_NCACTIVATE:
1360 if( ci->hwndActiveChild )
1361 SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1362 break;
1364 case WM_PARENTNOTIFY:
1365 if (LOWORD(wParam) == WM_LBUTTONDOWN)
1367 HWND child;
1368 POINT pt;
1369 pt.x = (short)LOWORD(lParam);
1370 pt.y = (short)HIWORD(lParam);
1371 child = ChildWindowFromPoint(hwnd, pt);
1373 TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1375 if( child && child != hwnd && child != ci->hwndActiveChild )
1376 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1378 return 0;
1380 case WM_SIZE:
1381 if( IsWindow(ci->hwndChildMaximized) )
1383 RECT rect;
1385 rect.left = 0;
1386 rect.top = 0;
1387 rect.right = LOWORD(lParam);
1388 rect.bottom = HIWORD(lParam);
1390 AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndChildMaximized,GWL_STYLE),
1391 0, GetWindowLongA(ci->hwndChildMaximized,GWL_EXSTYLE) );
1392 MoveWindow(ci->hwndChildMaximized, rect.left, rect.top,
1393 rect.right - rect.left, rect.bottom - rect.top, 1);
1395 else
1396 MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1398 break;
1400 case WM_MDICALCCHILDSCROLL:
1401 if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1403 CalcChildScroll(hwnd, ci->sbRecalc-1);
1404 ci->sbRecalc = 0;
1405 ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1407 return 0;
1409 return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1410 DefWindowProcA( hwnd, message, wParam, lParam );
1413 /***********************************************************************
1414 * MDIClientWndProcA
1416 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1418 if (!IsWindow(hwnd)) return 0;
1419 return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1422 /***********************************************************************
1423 * MDIClientWndProcW
1425 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1427 if (!IsWindow(hwnd)) return 0;
1428 return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1431 /***********************************************************************
1432 * DefFrameProc (USER.445)
1434 LRESULT WINAPI DefFrameProc16( HWND16 hwnd, HWND16 hwndMDIClient,
1435 UINT16 message, WPARAM16 wParam, LPARAM lParam )
1437 switch (message)
1439 case WM_SETTEXT:
1440 lParam = (LPARAM)MapSL(lParam);
1441 /* fall through */
1442 case WM_COMMAND:
1443 case WM_NCACTIVATE:
1444 case WM_SETFOCUS:
1445 case WM_SIZE:
1446 return DefFrameProcA( WIN_Handle32(hwnd), WIN_Handle32(hwndMDIClient),
1447 message, wParam, lParam );
1449 case WM_NEXTMENU:
1451 MDINEXTMENU next_menu;
1452 DefFrameProcW( WIN_Handle32(hwnd), WIN_Handle32(hwndMDIClient),
1453 message, wParam, (LPARAM)&next_menu );
1454 return MAKELONG( HMENU_16(next_menu.hmenuNext), HWND_16(next_menu.hwndNext) );
1456 default:
1457 return DefWindowProc16(hwnd, message, wParam, lParam);
1462 /***********************************************************************
1463 * DefFrameProcA (USER32.@)
1465 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1466 UINT message, WPARAM wParam, LPARAM lParam)
1468 if (hwndMDIClient)
1470 switch (message)
1472 case WM_SETTEXT:
1474 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1475 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1476 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1477 MDI_UpdateFrameText(hwnd, hwndMDIClient, MDI_REPAINTFRAME, text );
1478 HeapFree( GetProcessHeap(), 0, text );
1480 return 1; /* success. FIXME: check text length */
1482 case WM_COMMAND:
1483 case WM_NCACTIVATE:
1484 case WM_NEXTMENU:
1485 case WM_SETFOCUS:
1486 case WM_SIZE:
1487 return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1490 return DefWindowProcA(hwnd, message, wParam, lParam);
1494 /***********************************************************************
1495 * DefFrameProcW (USER32.@)
1497 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1498 UINT message, WPARAM wParam, LPARAM lParam)
1500 MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1502 if (ci)
1504 switch (message)
1506 case WM_COMMAND:
1508 WORD id = LOWORD(wParam);
1509 /* check for possible syscommands for maximized MDI child */
1510 if (id < ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1512 if( (id - 0xf000) & 0xf00f ) break;
1513 if( !ci->hwndChildMaximized ) break;
1514 switch( id )
1516 case SC_SIZE:
1517 case SC_MOVE:
1518 case SC_MINIMIZE:
1519 case SC_MAXIMIZE:
1520 case SC_NEXTWINDOW:
1521 case SC_PREVWINDOW:
1522 case SC_CLOSE:
1523 case SC_RESTORE:
1524 return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1525 wParam, lParam);
1528 else
1530 HWND childHwnd;
1531 if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1532 /* User chose "More Windows..." */
1533 childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1534 else
1535 /* User chose one of the windows listed in the "Windows" menu */
1536 childHwnd = MDI_GetChildByID(hwndMDIClient,id);
1538 if( childHwnd )
1539 SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1542 break;
1544 case WM_NCACTIVATE:
1545 SendMessageW(hwndMDIClient, message, wParam, lParam);
1546 break;
1548 case WM_SETTEXT:
1549 MDI_UpdateFrameText(hwnd, hwndMDIClient, MDI_REPAINTFRAME, (LPWSTR)lParam );
1550 return 1; /* success. FIXME: check text length */
1552 case WM_SETFOCUS:
1553 SetFocus(hwndMDIClient);
1554 break;
1556 case WM_SIZE:
1557 MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1558 break;
1560 case WM_NEXTMENU:
1562 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1564 if (!IsIconic(hwnd) && ci->hwndActiveChild && !ci->hwndChildMaximized)
1566 /* control menu is between the frame system menu and
1567 * the first entry of menu bar */
1568 WND *wndPtr = WIN_GetPtr(hwnd);
1570 if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1571 (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1573 WIN_ReleasePtr(wndPtr);
1574 wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1575 next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1576 next_menu->hwndNext = ci->hwndActiveChild;
1578 WIN_ReleasePtr(wndPtr);
1580 return 0;
1585 return DefWindowProcW( hwnd, message, wParam, lParam );
1589 /***********************************************************************
1590 * DefMDIChildProc (USER.447)
1592 LRESULT WINAPI DefMDIChildProc16( HWND16 hwnd, UINT16 message,
1593 WPARAM16 wParam, LPARAM lParam )
1595 switch (message)
1597 case WM_SETTEXT:
1598 return DefMDIChildProcA( WIN_Handle32(hwnd), message, wParam, (LPARAM)MapSL(lParam) );
1599 case WM_MENUCHAR:
1600 case WM_CLOSE:
1601 case WM_SETFOCUS:
1602 case WM_CHILDACTIVATE:
1603 case WM_SYSCOMMAND:
1604 case WM_SETVISIBLE:
1605 case WM_SIZE:
1606 case WM_SYSCHAR:
1607 return DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, lParam );
1608 case WM_GETMINMAXINFO:
1610 MINMAXINFO16 *mmi16 = (MINMAXINFO16 *)MapSL(lParam);
1611 MINMAXINFO mmi;
1612 STRUCT32_MINMAXINFO16to32( mmi16, &mmi );
1613 DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, (LPARAM)&mmi );
1614 STRUCT32_MINMAXINFO32to16( &mmi, mmi16 );
1615 return 0;
1617 case WM_NEXTMENU:
1619 MDINEXTMENU next_menu;
1620 DefMDIChildProcW( WIN_Handle32(hwnd), message, wParam, (LPARAM)&next_menu );
1621 return MAKELONG( HMENU_16(next_menu.hmenuNext), HWND_16(next_menu.hwndNext) );
1623 default:
1624 return DefWindowProc16(hwnd, message, wParam, lParam);
1629 /***********************************************************************
1630 * DefMDIChildProcA (USER32.@)
1632 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1633 WPARAM wParam, LPARAM lParam )
1635 HWND client = GetParent(hwnd);
1636 MDICLIENTINFO *ci = get_client_info( client );
1638 hwnd = WIN_GetFullHandle( hwnd );
1639 if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1641 switch (message)
1643 case WM_SETTEXT:
1644 DefWindowProcA(hwnd, message, wParam, lParam);
1645 MDI_MenuModifyItem( client, hwnd );
1646 if( ci->hwndChildMaximized == hwnd )
1647 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1648 return 1; /* success. FIXME: check text length */
1650 case WM_GETMINMAXINFO:
1651 case WM_MENUCHAR:
1652 case WM_CLOSE:
1653 case WM_SETFOCUS:
1654 case WM_CHILDACTIVATE:
1655 case WM_SYSCOMMAND:
1656 case WM_SETVISIBLE:
1657 case WM_SIZE:
1658 case WM_NEXTMENU:
1659 case WM_SYSCHAR:
1660 return DefMDIChildProcW( hwnd, message, wParam, lParam );
1662 return DefWindowProcA(hwnd, message, wParam, lParam);
1666 /***********************************************************************
1667 * DefMDIChildProcW (USER32.@)
1669 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1670 WPARAM wParam, LPARAM lParam )
1672 HWND client = GetParent(hwnd);
1673 MDICLIENTINFO *ci = get_client_info( client );
1675 hwnd = WIN_GetFullHandle( hwnd );
1676 if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1678 switch (message)
1680 case WM_SETTEXT:
1681 DefWindowProcW(hwnd, message, wParam, lParam);
1682 MDI_MenuModifyItem( client, hwnd );
1683 if( ci->hwndChildMaximized == hwnd )
1684 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1685 return 1; /* success. FIXME: check text length */
1687 case WM_GETMINMAXINFO:
1688 MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1689 return 0;
1691 case WM_MENUCHAR:
1692 return 0x00010000; /* MDI children don't have menu bars */
1694 case WM_CLOSE:
1695 SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1696 return 0;
1698 case WM_SETFOCUS:
1699 if (ci->hwndActiveChild != hwnd) MDI_ChildActivate( client, hwnd );
1700 break;
1702 case WM_CHILDACTIVATE:
1703 MDI_ChildActivate( client, hwnd );
1704 return 0;
1706 case WM_SYSCOMMAND:
1707 switch( wParam )
1709 case SC_MOVE:
1710 if( ci->hwndChildMaximized == hwnd) return 0;
1711 break;
1712 case SC_RESTORE:
1713 case SC_MINIMIZE:
1714 SetWindowLongW( hwnd, GWL_STYLE,
1715 GetWindowLongW( hwnd, GWL_STYLE ) | WS_SYSMENU );
1716 break;
1717 case SC_MAXIMIZE:
1718 if (ci->hwndChildMaximized == hwnd)
1719 return SendMessageW( GetParent(client), message, wParam, lParam);
1720 SetWindowLongW( hwnd, GWL_STYLE,
1721 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_SYSMENU );
1722 break;
1723 case SC_NEXTWINDOW:
1724 SendMessageW( client, WM_MDINEXT, 0, 0);
1725 return 0;
1726 case SC_PREVWINDOW:
1727 SendMessageW( client, WM_MDINEXT, 0, 1);
1728 return 0;
1730 break;
1732 case WM_SETVISIBLE:
1733 if( ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1734 else MDI_PostUpdate(client, ci, SB_BOTH+1);
1735 break;
1737 case WM_SIZE:
1738 if( ci->hwndActiveChild == hwnd && wParam != SIZE_MAXIMIZED )
1740 ci->hwndChildMaximized = 0;
1741 MDI_RestoreFrameMenu( GetParent(client), hwnd );
1742 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1745 if( wParam == SIZE_MAXIMIZED )
1747 HWND hMaxChild = ci->hwndChildMaximized;
1749 if( hMaxChild == hwnd ) break;
1750 if( hMaxChild)
1752 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1753 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1754 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1755 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1757 TRACE("maximizing child %p\n", hwnd );
1759 /* keep track of the maximized window. */
1760 ci->hwndChildMaximized = hwnd; /* !!! */
1762 /* The maximized window should also be the active window */
1763 MDI_ChildActivate( client, hwnd );
1764 MDI_AugmentFrameMenu( GetParent(client), hwnd );
1765 MDI_UpdateFrameText( GetParent(client), client, MDI_REPAINTFRAME, NULL );
1768 if( wParam == SIZE_MINIMIZED )
1770 HWND switchTo = MDI_GetWindow(ci, hwnd, TRUE, WS_MINIMIZE);
1772 if (switchTo) SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0);
1774 MDI_PostUpdate(client, ci, SB_BOTH+1);
1775 break;
1777 case WM_NEXTMENU:
1779 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1780 HWND parent = GetParent(client);
1782 if( wParam == VK_LEFT ) /* switch to frame system menu */
1784 WND *wndPtr = WIN_GetPtr( parent );
1785 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1786 WIN_ReleasePtr( wndPtr );
1788 if( wParam == VK_RIGHT ) /* to frame menu bar */
1790 next_menu->hmenuNext = GetMenu(parent);
1792 next_menu->hwndNext = parent;
1793 return 0;
1796 case WM_SYSCHAR:
1797 if (wParam == '-')
1799 SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1800 return 0;
1802 break;
1804 return DefWindowProcW(hwnd, message, wParam, lParam);
1807 /**********************************************************************
1808 * CreateMDIWindowA (USER32.@) Creates a MDI child
1810 * RETURNS
1811 * Success: Handle to created window
1812 * Failure: NULL
1814 HWND WINAPI CreateMDIWindowA(
1815 LPCSTR lpClassName, /* [in] Pointer to registered child class name */
1816 LPCSTR lpWindowName, /* [in] Pointer to window name */
1817 DWORD dwStyle, /* [in] Window style */
1818 INT X, /* [in] Horizontal position of window */
1819 INT Y, /* [in] Vertical position of window */
1820 INT nWidth, /* [in] Width of window */
1821 INT nHeight, /* [in] Height of window */
1822 HWND hWndParent, /* [in] Handle to parent window */
1823 HINSTANCE hInstance, /* [in] Handle to application instance */
1824 LPARAM lParam) /* [in] Application-defined value */
1826 MDICLIENTINFO *pCi = get_client_info( hWndParent );
1827 MDICREATESTRUCTA cs;
1829 TRACE("(%s,%s,%ld,%d,%d,%d,%d,%p,%p,%ld)\n",
1830 debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1831 nWidth,nHeight,hWndParent,hInstance,lParam);
1833 if (!pCi)
1835 ERR("bad hwnd for MDI-client: %p\n", hWndParent);
1836 return 0;
1838 cs.szClass=lpClassName;
1839 cs.szTitle=lpWindowName;
1840 cs.hOwner=hInstance;
1841 cs.x=X;
1842 cs.y=Y;
1843 cs.cx=nWidth;
1844 cs.cy=nHeight;
1845 cs.style=dwStyle;
1846 cs.lParam=lParam;
1848 return MDICreateChild(hWndParent, pCi, &cs, FALSE);
1851 /***********************************************************************
1852 * CreateMDIWindowW (USER32.@) Creates a MDI child
1854 * RETURNS
1855 * Success: Handle to created window
1856 * Failure: NULL
1858 HWND WINAPI CreateMDIWindowW(
1859 LPCWSTR lpClassName, /* [in] Pointer to registered child class name */
1860 LPCWSTR lpWindowName, /* [in] Pointer to window name */
1861 DWORD dwStyle, /* [in] Window style */
1862 INT X, /* [in] Horizontal position of window */
1863 INT Y, /* [in] Vertical position of window */
1864 INT nWidth, /* [in] Width of window */
1865 INT nHeight, /* [in] Height of window */
1866 HWND hWndParent, /* [in] Handle to parent window */
1867 HINSTANCE hInstance, /* [in] Handle to application instance */
1868 LPARAM lParam) /* [in] Application-defined value */
1870 MDICLIENTINFO *pCi = get_client_info( hWndParent );
1871 MDICREATESTRUCTW cs;
1873 TRACE("(%s,%s,%ld,%d,%d,%d,%d,%p,%p,%ld)\n",
1874 debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1875 nWidth, nHeight, hWndParent, hInstance, lParam);
1877 if (!pCi)
1879 ERR("bad hwnd for MDI-client: %p\n", hWndParent);
1880 return 0;
1882 cs.szClass = lpClassName;
1883 cs.szTitle = lpWindowName;
1884 cs.hOwner = hInstance;
1885 cs.x = X;
1886 cs.y = Y;
1887 cs.cx = nWidth;
1888 cs.cy = nHeight;
1889 cs.style = dwStyle;
1890 cs.lParam = lParam;
1892 return MDICreateChild(hWndParent, pCi, (MDICREATESTRUCTA *)&cs, TRUE);
1895 /**********************************************************************
1896 * TranslateMDISysAccel (USER32.@)
1898 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1900 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1902 MDICLIENTINFO *ci = get_client_info( hwndClient );
1903 WPARAM wParam = 0;
1905 if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1907 /* translate if the Ctrl key is down and Alt not. */
1909 if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1911 switch( msg->wParam )
1913 case VK_F6:
1914 case VK_TAB:
1915 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1916 break;
1917 case VK_F4:
1918 case VK_RBUTTON:
1919 wParam = SC_CLOSE;
1920 break;
1921 default:
1922 return 0;
1924 TRACE("wParam = %04x\n", wParam);
1925 SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1926 return 1;
1929 return 0; /* failure */
1932 /***********************************************************************
1933 * CalcChildScroll (USER32.@)
1935 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1937 SCROLLINFO info;
1938 RECT childRect, clientRect;
1939 HWND *list;
1941 GetClientRect( hwnd, &clientRect );
1942 SetRectEmpty( &childRect );
1944 if ((list = WIN_ListChildren( hwnd )))
1946 int i;
1947 for (i = 0; list[i]; i++)
1949 DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1950 if (style & WS_MAXIMIZE)
1952 HeapFree( GetProcessHeap(), 0, list );
1953 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1954 return;
1956 if (style & WS_VISIBLE)
1958 WND *pWnd = WIN_FindWndPtr( list[i] );
1959 UnionRect( &childRect, &pWnd->rectWindow, &childRect );
1960 WIN_ReleaseWndPtr( pWnd );
1963 HeapFree( GetProcessHeap(), 0, list );
1965 UnionRect( &childRect, &clientRect, &childRect );
1967 /* set common info values */
1968 info.cbSize = sizeof(info);
1969 info.fMask = SIF_POS | SIF_RANGE;
1971 /* set the specific */
1972 switch( scroll )
1974 case SB_BOTH:
1975 case SB_HORZ:
1976 info.nMin = childRect.left;
1977 info.nMax = childRect.right - clientRect.right;
1978 info.nPos = clientRect.left - childRect.left;
1979 SetScrollInfo(hwnd, scroll, &info, TRUE);
1980 if (scroll == SB_HORZ) break;
1981 /* fall through */
1982 case SB_VERT:
1983 info.nMin = childRect.top;
1984 info.nMax = childRect.bottom - clientRect.bottom;
1985 info.nPos = clientRect.top - childRect.top;
1986 SetScrollInfo(hwnd, scroll, &info, TRUE);
1987 break;
1992 /***********************************************************************
1993 * ScrollChildren (USER32.@)
1995 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1996 LPARAM lParam)
1998 INT newPos = -1;
1999 INT curPos, length, minPos, maxPos, shift;
2000 RECT rect;
2002 GetClientRect( hWnd, &rect );
2004 switch(uMsg)
2006 case WM_HSCROLL:
2007 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
2008 curPos = GetScrollPos(hWnd,SB_HORZ);
2009 length = (rect.right - rect.left) / 2;
2010 shift = GetSystemMetrics(SM_CYHSCROLL);
2011 break;
2012 case WM_VSCROLL:
2013 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
2014 curPos = GetScrollPos(hWnd,SB_VERT);
2015 length = (rect.bottom - rect.top) / 2;
2016 shift = GetSystemMetrics(SM_CXVSCROLL);
2017 break;
2018 default:
2019 return;
2022 switch( wParam )
2024 case SB_LINEUP:
2025 newPos = curPos - shift;
2026 break;
2027 case SB_LINEDOWN:
2028 newPos = curPos + shift;
2029 break;
2030 case SB_PAGEUP:
2031 newPos = curPos - length;
2032 break;
2033 case SB_PAGEDOWN:
2034 newPos = curPos + length;
2035 break;
2037 case SB_THUMBPOSITION:
2038 newPos = LOWORD(lParam);
2039 break;
2041 case SB_THUMBTRACK:
2042 return;
2044 case SB_TOP:
2045 newPos = minPos;
2046 break;
2047 case SB_BOTTOM:
2048 newPos = maxPos;
2049 break;
2050 case SB_ENDSCROLL:
2051 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
2052 return;
2055 if( newPos > maxPos )
2056 newPos = maxPos;
2057 else
2058 if( newPos < minPos )
2059 newPos = minPos;
2061 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
2063 if( uMsg == WM_VSCROLL )
2064 ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
2065 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
2066 else
2067 ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
2068 SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
2072 /******************************************************************************
2073 * CascadeWindows (USER32.@) Cascades MDI child windows
2075 * RETURNS
2076 * Success: Number of cascaded windows.
2077 * Failure: 0
2079 WORD WINAPI
2080 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
2081 UINT cKids, const HWND *lpKids)
2083 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
2084 return 0;
2088 /******************************************************************************
2089 * TileWindows (USER32.@) Tiles MDI child windows
2091 * RETURNS
2092 * Success: Number of tiled windows.
2093 * Failure: 0
2095 WORD WINAPI
2096 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
2097 UINT cKids, const HWND *lpKids)
2099 FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
2100 return 0;
2103 /************************************************************************
2104 * "More Windows..." functionality
2107 /* MDI_MoreWindowsDlgProc
2109 * This function will process the messages sent to the "More Windows..."
2110 * dialog.
2111 * Return values: 0 = cancel pressed
2112 * HWND = ok pressed or double-click in the list...
2116 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
2118 switch (iMsg)
2120 case WM_INITDIALOG:
2122 UINT widest = 0;
2123 UINT length;
2124 UINT i;
2125 MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
2126 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2127 HWND *list, *sorted_list;
2129 if (!(list = WIN_ListChildren( (HWND)lParam ))) return TRUE;
2130 if (!(sorted_list = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
2131 sizeof(HWND) * ci->nActiveChildren )))
2133 HeapFree( GetProcessHeap(), 0, list );
2134 return FALSE;
2137 /* Fill the list, sorted by id... */
2138 for (i = 0; list[i]; i++)
2140 UINT id = GetWindowLongW( list[i], GWL_ID ) - ci->idFirstChild;
2141 if (id < ci->nActiveChildren) sorted_list[id] = list[i];
2143 HeapFree( GetProcessHeap(), 0, list );
2145 for (i = 0; i < ci->nActiveChildren; i++)
2147 WCHAR buffer[128];
2149 if (!GetWindowTextW( sorted_list[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
2150 continue;
2151 SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
2152 SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)sorted_list[i] );
2153 length = strlenW(buffer); /* FIXME: should use GetTextExtentPoint */
2154 if (length > widest)
2155 widest = length;
2157 /* Make sure the horizontal scrollbar scrolls ok */
2158 SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
2160 /* Set the current selection */
2161 SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
2162 return TRUE;
2165 case WM_COMMAND:
2166 switch (LOWORD(wParam))
2168 default:
2169 if (HIWORD(wParam) != LBN_DBLCLK) break;
2170 /* fall through */
2171 case IDOK:
2173 /* windows are sorted by menu ID, so we must return the
2174 * window associated to the given id
2176 HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2177 UINT index = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
2178 LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
2179 EndDialog(hDlg, res);
2180 return TRUE;
2182 case IDCANCEL:
2183 EndDialog(hDlg, 0);
2184 return TRUE;
2186 break;
2188 return FALSE;
2193 * MDI_MoreWindowsDialog
2195 * Prompts the user with a listbox containing the opened
2196 * documents. The user can then choose a windows and click
2197 * on OK to set the current window to the one selected, or
2198 * CANCEL to cancel. The function returns a handle to the
2199 * selected window.
2202 static HWND MDI_MoreWindowsDialog(HWND hwnd)
2204 LPCVOID template;
2205 HRSRC hRes;
2206 HANDLE hDlgTmpl;
2208 hRes = FindResourceA(GetModuleHandleA("USER32"), "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
2210 if (hRes == 0)
2211 return 0;
2213 hDlgTmpl = LoadResource(GetModuleHandleA("USER32"), hRes );
2215 if (hDlgTmpl == 0)
2216 return 0;
2218 template = LockResource( hDlgTmpl );
2220 if (template == 0)
2221 return 0;
2223 return (HWND) DialogBoxIndirectParamA(GetModuleHandleA("USER32"),
2224 (LPDLGTEMPLATEA) template,
2225 hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);
2230 * MDI_SwapMenuItems
2232 * Will swap the menu IDs for the given 2 positions.
2233 * pos1 and pos2 are menu IDs
2238 static void MDI_SwapMenuItems(HWND parent, UINT pos1, UINT pos2)
2240 HWND *list;
2241 int i;
2243 if (!(list = WIN_ListChildren( parent ))) return;
2244 for (i = 0; list[i]; i++)
2246 UINT id = GetWindowLongW( list[i], GWL_ID );
2247 if (id == pos1) SetWindowLongW( list[i], GWL_ID, pos2 );
2248 else if (id == pos2) SetWindowLongW( list[i], GWL_ID, pos1 );
2250 HeapFree( GetProcessHeap(), 0, list );