comctl32/tests: We can now store binary files in the repository.
[wine] / dlls / user32 / mdi.c
1 /* MDI.C
2  *
3  * Copyright 1994, Bob Amstadt
4  *           1995,1996 Alex Korobka
5  *
6  * This file contains routines to support MDI (Multiple Document
7  * Interface) features .
8  *
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.
13  *
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.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  *
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.
26  *
27  * Notes on how the "More Windows..." is implemented:
28  *
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:
35  *
36  *
37  *
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
48  *
49  *
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:
52  *
53  *
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
65  *
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.
69  *
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
80  *
81  */
82
83 #include <stdlib.h>
84 #include <stdarg.h>
85 #include <stdio.h>
86 #include <string.h>
87 #include <math.h>
88
89 #define OEMRESOURCE
90
91 #include "windef.h"
92 #include "winbase.h"
93 #include "wingdi.h"
94 #include "winuser.h"
95 #include "wownt32.h"
96 #include "wine/winuser16.h"
97 #include "wine/unicode.h"
98 #include "win.h"
99 #include "controls.h"
100 #include "user_private.h"
101 #include "wine/debug.h"
102
103 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
104
105 #define MDI_MAXTITLELENGTH      0xa1
106
107 #define WM_MDICALCCHILDSCROLL   0x10ac /* this is exactly what Windows uses */
108
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
114
115 #define MDIF_NEEDUPDATE         0x0001
116
117 typedef struct
118 {
119     /* At some points, particularly when switching MDI children, active and
120      * maximized MDI children may be not the same window, so we need to track
121      * them separately.
122      * The only place where we switch to/from maximized state is DefMDIChildProc
123      * WM_SIZE/SIZE_MAXIMIZED handler. We get that notification only after the
124      * ShowWindow(SW_SHOWMAXIMIZED) request, therefore window is guaranteed to
125      * be visible at the time we get the notification, and it's safe to assume
126      * that hwndChildMaximized is always visible.
127      * If the app plays games with WS_VISIBLE, WS_MAXIMIZE or any other window
128      * states it must keep coherency with USER32 on its own. This is true for
129      * Windows as well.
130      */
131     UINT      nActiveChildren;
132     HWND      hwndChildMaximized;
133     HWND      hwndActiveChild;
134     HWND      *child; /* array of tracked children */
135     HMENU     hFrameMenu;
136     HMENU     hWindowMenu;
137     UINT      idFirstChild;
138     LPWSTR    frameTitle;
139     UINT      nTotalCreated;
140     UINT      mdiFlags;
141     UINT      sbRecalc;   /* SB_xxx flags for scrollbar fixup */
142 } MDICLIENTINFO;
143
144 static HBITMAP hBmpClose   = 0;
145
146 /* ----------------- declarations ----------------- */
147 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
148 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
149 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
150 static LONG MDI_ChildActivate( HWND, HWND );
151 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
152
153 static HWND MDI_MoreWindowsDialog(HWND);
154 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
155 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
156
157 /* -------- Miscellaneous service functions ----------
158  *
159  *                      MDI_GetChildByID
160  */
161 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
162 {
163     int i;
164
165     for (i = 0; ci->nActiveChildren; i++)
166     {
167         if (GetWindowLongPtrW( ci->child[i], GWLP_ID ) == id)
168             return ci->child[i];
169     }
170     return 0;
171 }
172
173 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
174 {
175     if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
176     {
177         ci->mdiFlags |= MDIF_NEEDUPDATE;
178         PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
179     }
180     ci->sbRecalc = recalc;
181 }
182
183
184 /*********************************************************************
185  * MDIClient class descriptor
186  */
187 const struct builtin_class_descr MDICLIENT_builtin_class =
188 {
189     "MDIClient",            /* name */
190     0,                      /* style */
191     MDIClientWndProcA,      /* procA */
192     MDIClientWndProcW,      /* procW */
193     sizeof(MDICLIENTINFO),  /* extra */
194     IDC_ARROW,              /* cursor */
195     (HBRUSH)(COLOR_APPWORKSPACE+1)    /* brush */
196 };
197
198
199 static MDICLIENTINFO *get_client_info( HWND client )
200 {
201     MDICLIENTINFO *ret = NULL;
202     WND *win = WIN_GetPtr( client );
203     if (win)
204     {
205         if (win == WND_OTHER_PROCESS || win == WND_DESKTOP)
206         {
207             if (IsWindow(client)) WARN( "client %p belongs to other process\n", client );
208             return NULL;
209         }
210         if (win->flags & WIN_ISMDICLIENT)
211             ret = (MDICLIENTINFO *)win->wExtra;
212         else
213             WARN( "%p is not an MDI client\n", client );
214         WIN_ReleasePtr( win );
215     }
216     return ret;
217 }
218
219 static BOOL is_close_enabled(HWND hwnd, HMENU hSysMenu)
220 {
221     if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return FALSE;
222
223     if (!hSysMenu) hSysMenu = GetSystemMenu(hwnd, FALSE);
224     if (hSysMenu)
225     {
226         UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
227         if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
228             return FALSE;
229     }
230     return TRUE;
231 }
232
233 /**********************************************************************
234  *                      MDI_GetWindow
235  *
236  * returns "activatable" child different from the current or zero
237  */
238 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
239                             DWORD dwStyleMask )
240 {
241     int i;
242     HWND *list;
243     HWND last = 0;
244
245     dwStyleMask |= WS_DISABLED | WS_VISIBLE;
246     if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
247
248     if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
249     i = 0;
250     /* start from next after hWnd */
251     while (list[i] && list[i] != hWnd) i++;
252     if (list[i]) i++;
253
254     for ( ; list[i]; i++)
255     {
256         if (GetWindow( list[i], GW_OWNER )) continue;
257         if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
258         last = list[i];
259         if (bNext) goto found;
260     }
261     /* now restart from the beginning */
262     for (i = 0; list[i] && list[i] != hWnd; i++)
263     {
264         if (GetWindow( list[i], GW_OWNER )) continue;
265         if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
266         last = list[i];
267         if (bNext) goto found;
268     }
269  found:
270     HeapFree( GetProcessHeap(), 0, list );
271     return last;
272 }
273
274 /**********************************************************************
275  *                      MDI_CalcDefaultChildPos
276  *
277  *  It seems that the default height is about 2/3 of the client rect
278  */
279 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta, UINT *id )
280 {
281     INT  nstagger;
282     RECT rect;
283     INT spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) - 1;
284
285     if (total < 0) /* we are called from CreateWindow */
286     {
287         MDICLIENTINFO *ci = get_client_info(hwndClient);
288         total = ci ? ci->nTotalCreated : 0;
289         *id = ci->idFirstChild + ci->nActiveChildren;
290         TRACE("MDI child id %04x\n", *id);
291     }
292
293     GetClientRect( hwndClient, &rect );
294     if( rect.bottom - rect.top - delta >= spacing )
295         rect.bottom -= delta;
296
297     nstagger = (rect.bottom - rect.top)/(3 * spacing);
298     lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
299     lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
300     lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
301 }
302
303 /**********************************************************************
304  *            MDISetMenu
305  */
306 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
307                            HMENU hmenuWindow)
308 {
309     MDICLIENTINFO *ci;
310     HWND hwndFrame = GetParent(hwnd);
311
312     TRACE("%p, frame menu %p, window menu %p\n", hwnd, hmenuFrame, hmenuWindow);
313
314     if (hmenuFrame && !IsMenu(hmenuFrame))
315     {
316         WARN("hmenuFrame is not a menu handle\n");
317         return 0L;
318     }
319
320     if (hmenuWindow && !IsMenu(hmenuWindow))
321     {
322         WARN("hmenuWindow is not a menu handle\n");
323         return 0L;
324     }
325
326     if (!(ci = get_client_info( hwnd ))) return 0;
327
328     TRACE("old frame menu %p, old window menu %p\n", ci->hFrameMenu, ci->hWindowMenu);
329
330     if (hmenuFrame)
331     {
332         if (hmenuFrame == ci->hFrameMenu) return (LRESULT)hmenuFrame;
333
334         if (ci->hwndChildMaximized)
335             MDI_RestoreFrameMenu( hwndFrame, ci->hwndChildMaximized );
336     }
337
338     if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
339     {
340         /* delete menu items from ci->hWindowMenu
341          * and add them to hmenuWindow */
342         /* Agent newsreader calls this function with  ci->hWindowMenu == NULL */
343         if( ci->hWindowMenu && ci->nActiveChildren )
344         {
345             UINT nActiveChildren_old = ci->nActiveChildren;
346
347             /* Remove all items from old Window menu */
348             ci->nActiveChildren = 0;
349             MDI_RefreshMenu(ci);
350
351             ci->hWindowMenu = hmenuWindow;
352
353             /* Add items to the new Window menu */
354             ci->nActiveChildren = nActiveChildren_old;
355             MDI_RefreshMenu(ci);
356         }
357         else
358             ci->hWindowMenu = hmenuWindow;
359     }
360
361     if (hmenuFrame)
362     {
363         SetMenu(hwndFrame, hmenuFrame);
364         if( hmenuFrame != ci->hFrameMenu )
365         {
366             HMENU oldFrameMenu = ci->hFrameMenu;
367
368             ci->hFrameMenu = hmenuFrame;
369             if (ci->hwndChildMaximized)
370                 MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
371
372             return (LRESULT)oldFrameMenu;
373         }
374     }
375     else
376     {
377         /* SetMenu() may already have been called, meaning that this window
378          * already has its menu. But they may have done a SetMenu() on
379          * an MDI window, and called MDISetMenu() after the fact, meaning
380          * that the "if" to this "else" wouldn't catch the need to
381          * augment the frame menu.
382          */
383         if( ci->hwndChildMaximized )
384             MDI_AugmentFrameMenu( hwndFrame, ci->hwndChildMaximized );
385     }
386
387     return 0;
388 }
389
390 /**********************************************************************
391  *            MDIRefreshMenu
392  */
393 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
394 {
395     UINT i, count, visible, id;
396     WCHAR buf[MDI_MAXTITLELENGTH];
397
398     TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
399
400     if (!ci->hWindowMenu)
401         return 0;
402
403     if (!IsMenu(ci->hWindowMenu))
404     {
405         WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
406         return 0;
407     }
408
409     /* Windows finds the last separator in the menu, and if after it
410      * there is a menu item with MDI magic ID removes all existing
411      * menu items after it, and then adds visible MDI children.
412      */
413     count = GetMenuItemCount(ci->hWindowMenu);
414     for (i = 0; i < count; i++)
415     {
416         MENUITEMINFOW mii;
417
418         memset(&mii, 0, sizeof(mii));
419         mii.cbSize = sizeof(mii);
420         mii.fMask  = MIIM_TYPE;
421         if (GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii))
422         {
423             if (mii.fType & MF_SEPARATOR)
424             {
425                 /* Windows checks only ID of the menu item */
426                 memset(&mii, 0, sizeof(mii));
427                 mii.cbSize = sizeof(mii);
428                 mii.fMask  = MIIM_ID;
429                 if (GetMenuItemInfoW(ci->hWindowMenu, i + 1, TRUE, &mii))
430                 {
431                     if (mii.wID == ci->idFirstChild)
432                     {
433                         TRACE("removing %u items including separator\n", count - i);
434                         while (RemoveMenu(ci->hWindowMenu, i, MF_BYPOSITION))
435                             /* nothing */;
436
437                         break;
438                     }
439                 }
440             }
441         }
442     }
443
444     visible = 0;
445     for (i = 0; i < ci->nActiveChildren; i++)
446     {
447         if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
448         {
449             id = ci->idFirstChild + visible;
450
451             if (visible == MDI_MOREWINDOWSLIMIT)
452             {
453                 LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
454                 AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
455                 break;
456             }
457
458             if (!visible)
459                 /* Visio expects that separator has id 0 */
460                 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
461
462             visible++;
463
464             SetWindowLongPtrW(ci->child[i], GWLP_ID, id);
465
466             buf[0] = '&';
467             buf[1] = '0' + visible;
468             buf[2] = ' ';
469             InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
470             TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
471             AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
472
473             if (ci->child[i] == ci->hwndActiveChild)
474                 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
475         }
476         else
477             TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
478     }
479
480     return (LRESULT)ci->hFrameMenu;
481 }
482
483
484 /* ------------------ MDI child window functions ---------------------- */
485
486 /**********************************************************************
487  *                      MDI_ChildGetMinMaxInfo
488  *
489  * Note: The rule here is that client rect of the maximized MDI child
490  *       is equal to the client rect of the MDI client window.
491  */
492 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
493 {
494     RECT rect;
495
496     GetClientRect( client, &rect );
497     AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
498                         0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
499
500     lpMinMax->ptMaxSize.x = rect.right -= rect.left;
501     lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
502
503     lpMinMax->ptMaxPosition.x = rect.left;
504     lpMinMax->ptMaxPosition.y = rect.top;
505
506     TRACE("max rect (%d,%d - %d, %d)\n",
507                         rect.left,rect.top,rect.right,rect.bottom);
508 }
509
510 /**********************************************************************
511  *                      MDI_SwitchActiveChild
512  *
513  * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
514  *       being activated
515  */
516 static void MDI_SwitchActiveChild( MDICLIENTINFO *ci, HWND hwndTo, BOOL activate )
517 {
518     HWND hwndPrev;
519
520     hwndPrev = ci->hwndActiveChild;
521
522     TRACE("from %p, to %p\n", hwndPrev, hwndTo);
523
524     if ( hwndTo != hwndPrev )
525     {
526         BOOL was_zoomed = IsZoomed(hwndPrev);
527
528         if (was_zoomed)
529         {
530             /* restore old MDI child */
531             SendMessageW( hwndPrev, WM_SETREDRAW, FALSE, 0 );
532             ShowWindow( hwndPrev, SW_RESTORE );
533             SendMessageW( hwndPrev, WM_SETREDRAW, TRUE, 0 );
534
535             /* activate new MDI child */
536             SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
537             /* maximize new MDI child */
538             ShowWindow( hwndTo, SW_MAXIMIZE );
539         }
540         /* activate new MDI child */
541         SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | (activate ? 0 : SWP_NOACTIVATE) );
542     }
543 }
544
545
546 /**********************************************************************
547  *                                      MDIDestroyChild
548  */
549 static LRESULT MDIDestroyChild( HWND client, MDICLIENTINFO *ci,
550                                 HWND child, BOOL flagDestroy )
551 {
552     UINT i;
553
554     TRACE("# of managed children %u\n", ci->nActiveChildren);
555
556     if( child == ci->hwndActiveChild )
557     {
558         HWND next = MDI_GetWindow(ci, child, TRUE, 0);
559         /* flagDestroy == 0 means we were called from WM_PARENTNOTIFY handler */
560         if (flagDestroy && next)
561             MDI_SwitchActiveChild(ci, next, TRUE);
562         else
563         {
564             ShowWindow(child, SW_HIDE);
565             if (child == ci->hwndChildMaximized)
566             {
567                 HWND frame = GetParent(client);
568                 MDI_RestoreFrameMenu(frame, child);
569                 ci->hwndChildMaximized = 0;
570                 MDI_UpdateFrameText(frame, client, TRUE, NULL);
571             }
572             if (flagDestroy) ci->hwndActiveChild = 0;
573         }
574     }
575
576     for (i = 0; i < ci->nActiveChildren; i++)
577     {
578         if (ci->child[i] == child)
579         {
580             HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
581             memcpy(new_child, ci->child, i * sizeof(HWND));
582             if (i + 1 < ci->nActiveChildren)
583                 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
584             HeapFree(GetProcessHeap(), 0, ci->child);
585             ci->child = new_child;
586
587             ci->nActiveChildren--;
588             break;
589         }
590     }
591
592     if (flagDestroy)
593     {
594         SendMessageW(client, WM_MDIREFRESHMENU, 0, 0);
595         MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
596         DestroyWindow(child);
597     }
598
599     TRACE("child destroyed - %p\n", child);
600     return 0;
601 }
602
603
604 /**********************************************************************
605  *                                      MDI_ChildActivate
606  *
607  * Called in response to WM_CHILDACTIVATE, or when last MDI child
608  * is being deactivated.
609  */
610 static LONG MDI_ChildActivate( HWND client, HWND child )
611 {
612     MDICLIENTINFO *clientInfo;
613     HWND prevActiveWnd, frame;
614     BOOL isActiveFrameWnd;
615
616     clientInfo = get_client_info( client );
617
618     if (clientInfo->hwndActiveChild == child) return 0;
619
620     TRACE("%p\n", child);
621
622     frame = GetParent(client);
623     isActiveFrameWnd = (GetActiveWindow() == frame);
624     prevActiveWnd = clientInfo->hwndActiveChild;
625
626     /* deactivate prev. active child */
627     if(prevActiveWnd)
628     {
629         SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
630         SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
631     }
632
633     MDI_SwitchActiveChild( clientInfo, child, FALSE );
634     clientInfo->hwndActiveChild = child;
635
636     MDI_RefreshMenu(clientInfo);
637
638     if( isActiveFrameWnd )
639     {
640         SendMessageW( child, WM_NCACTIVATE, TRUE, 0L);
641         /* Let the client window manage focus for children, but if the focus
642          * is already on the client (for instance this is the 1st child) then
643          * SetFocus won't work. It appears that Windows sends WM_SETFOCUS
644          * manually in this case.
645          */
646         if (SetFocus(client) == client)
647             SendMessageW( client, WM_SETFOCUS, (WPARAM)client, 0 );
648     }
649
650     SendMessageW( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
651     return TRUE;
652 }
653
654 /* -------------------- MDI client window functions ------------------- */
655
656 /**********************************************************************
657  *                              CreateMDIMenuBitmap
658  */
659 static HBITMAP CreateMDIMenuBitmap(void)
660 {
661  HDC            hDCSrc  = CreateCompatibleDC(0);
662  HDC            hDCDest = CreateCompatibleDC(hDCSrc);
663  HBITMAP        hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
664  HBITMAP        hbCopy;
665  HBITMAP        hobjSrc, hobjDest;
666
667  hobjSrc = SelectObject(hDCSrc, hbClose);
668  hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
669  hobjDest = SelectObject(hDCDest, hbCopy);
670
671  BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
672           hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
673
674  SelectObject(hDCSrc, hobjSrc);
675  DeleteObject(hbClose);
676  DeleteDC(hDCSrc);
677
678  hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
679
680  MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
681  LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
682
683  SelectObject(hDCDest, hobjSrc );
684  SelectObject(hDCDest, hobjDest);
685  DeleteDC(hDCDest);
686
687  return hbCopy;
688 }
689
690 /**********************************************************************
691  *                              MDICascade
692  */
693 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
694 {
695     HWND *win_array;
696     BOOL has_icons = FALSE;
697     int i, total;
698
699     if (ci->hwndChildMaximized)
700         SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
701
702     if (ci->nActiveChildren == 0) return 0;
703
704     if (!(win_array = WIN_ListChildren( client ))) return 0;
705
706     /* remove all the windows we don't want */
707     for (i = total = 0; win_array[i]; i++)
708     {
709         if (!IsWindowVisible( win_array[i] )) continue;
710         if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
711         if (IsIconic( win_array[i] ))
712         {
713             has_icons = TRUE;
714             continue;
715         }
716         win_array[total++] = win_array[i];
717     }
718     win_array[total] = 0;
719
720     if (total)
721     {
722         INT delta = 0, n = 0, i;
723         POINT pos[2];
724         if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
725
726         /* walk the list (backwards) and move windows */
727         for (i = total - 1; i >= 0; i--)
728         {
729             LONG style;
730             LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
731
732             MDI_CalcDefaultChildPos(client, n++, pos, delta, NULL);
733             TRACE("move %p to (%d,%d) size [%d,%d]\n",
734                   win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
735             style = GetWindowLongW(win_array[i], GWL_STYLE);
736
737             if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
738             SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
739                            posOptions);
740         }
741     }
742     HeapFree( GetProcessHeap(), 0, win_array );
743
744     if (has_icons) ArrangeIconicWindows( client );
745     return 0;
746 }
747
748 /**********************************************************************
749  *                                      MDITile
750  */
751 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
752 {
753     HWND *win_array;
754     int i, total;
755     BOOL has_icons = FALSE;
756
757     if (ci->hwndChildMaximized)
758         SendMessageW(client, WM_MDIRESTORE, (WPARAM)ci->hwndChildMaximized, 0);
759
760     if (ci->nActiveChildren == 0) return;
761
762     if (!(win_array = WIN_ListChildren( client ))) return;
763
764     /* remove all the windows we don't want */
765     for (i = total = 0; win_array[i]; i++)
766     {
767         if (!IsWindowVisible( win_array[i] )) continue;
768         if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
769         if (IsIconic( win_array[i] ))
770         {
771             has_icons = TRUE;
772             continue;
773         }
774         if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
775         win_array[total++] = win_array[i];
776     }
777     win_array[total] = 0;
778
779     TRACE("%u windows to tile\n", total);
780
781     if (total)
782     {
783         HWND *pWnd = win_array;
784         RECT rect;
785         int x, y, xsize, ysize;
786         int rows, columns, r, c, i;
787
788         GetClientRect(client,&rect);
789         rows    = (int) sqrt((double)total);
790         columns = total / rows;
791
792         if( wParam & MDITILE_HORIZONTAL )  /* version >= 3.1 */
793         {
794             i = rows;
795             rows = columns;  /* exchange r and c */
796             columns = i;
797         }
798
799         if (has_icons)
800         {
801             y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
802             rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
803         }
804
805         ysize   = rect.bottom / rows;
806         xsize   = rect.right  / columns;
807
808         for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
809         {
810             if (c == columns)
811             {
812                 rows  = total - i;
813                 ysize = rect.bottom / rows;
814             }
815
816             y = 0;
817             for (r = 1; r <= rows && *pWnd; r++, i++)
818             {
819                 LONG posOptions = SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER;
820                 LONG style = GetWindowLongW(win_array[i], GWL_STYLE);
821                 if (!(style & WS_SIZEBOX)) posOptions |= SWP_NOSIZE;
822
823                 SetWindowPos(*pWnd, 0, x, y, xsize, ysize, posOptions);
824                 y += ysize;
825                 pWnd++;
826             }
827             x += xsize;
828         }
829     }
830     HeapFree( GetProcessHeap(), 0, win_array );
831     if (has_icons) ArrangeIconicWindows( client );
832 }
833
834 /* ----------------------- Frame window ---------------------------- */
835
836
837 /**********************************************************************
838  *                                      MDI_AugmentFrameMenu
839  */
840 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
841 {
842     HMENU menu = GetMenu( frame );
843     HMENU       hSysPopup = 0;
844     HBITMAP hSysMenuBitmap = 0;
845     HICON hIcon;
846
847     TRACE("frame %p,child %p\n",frame,hChild);
848
849     if( !menu ) return 0;
850
851     /* create a copy of sysmenu popup and insert it into frame menu bar */
852     if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
853     {
854         TRACE("child %p doesn't have a system menu\n", hChild);
855         return 0;
856     }
857
858     AppendMenuW(menu, MF_HELP | MF_BITMAP,
859                 SC_CLOSE, is_close_enabled(hChild, hSysPopup) ?
860                 (LPCWSTR)HBMMENU_MBAR_CLOSE : (LPCWSTR)HBMMENU_MBAR_CLOSE_D );
861     AppendMenuW(menu, MF_HELP | MF_BITMAP,
862                 SC_RESTORE, (LPCWSTR)HBMMENU_MBAR_RESTORE );
863     AppendMenuW(menu, MF_HELP | MF_BITMAP,
864                 SC_MINIMIZE, (LPCWSTR)HBMMENU_MBAR_MINIMIZE ) ;
865
866     /* The system menu is replaced by the child icon */
867     hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_SMALL, 0);
868     if (!hIcon)
869         hIcon = (HICON)SendMessageW(hChild, WM_GETICON, ICON_BIG, 0);
870     if (!hIcon)
871         hIcon = LoadImageW(0, MAKEINTRESOURCEW(IDI_WINLOGO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
872     if (hIcon)
873     {
874       HDC hMemDC;
875       HBITMAP hBitmap, hOldBitmap;
876       HBRUSH hBrush;
877       HDC hdc = GetDC(hChild);
878
879       if (hdc)
880       {
881         int cx, cy;
882         cx = GetSystemMetrics(SM_CXSMICON);
883         cy = GetSystemMetrics(SM_CYSMICON);
884         hMemDC = CreateCompatibleDC(hdc);
885         hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
886         hOldBitmap = SelectObject(hMemDC, hBitmap);
887         SetMapMode(hMemDC, MM_TEXT);
888         hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
889         DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
890         SelectObject (hMemDC, hOldBitmap);
891         DeleteObject(hBrush);
892         DeleteDC(hMemDC);
893         ReleaseDC(hChild, hdc);
894         hSysMenuBitmap = hBitmap;
895       }
896     }
897
898     if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
899                      (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
900     {
901         TRACE("not inserted\n");
902         DestroyMenu(hSysPopup);
903         return 0;
904     }
905
906     EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
907     EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
908     EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
909     SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
910
911     /* redraw menu */
912     DrawMenuBar(frame);
913
914     return 1;
915 }
916
917 /**********************************************************************
918  *                                      MDI_RestoreFrameMenu
919  */
920 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
921 {
922     MENUITEMINFOW menuInfo;
923     HMENU menu = GetMenu( frame );
924     INT nItems;
925     UINT iId;
926
927     TRACE("frame %p, child %p\n", frame, hChild);
928
929     if( !menu ) return 0;
930
931     /* if there is no system buttons then nothing to do */
932     nItems = GetMenuItemCount(menu) - 1;
933     iId = GetMenuItemID(menu, nItems);
934     if ( !(iId == SC_RESTORE || iId == SC_CLOSE) )
935         return 0;
936
937     /*
938      * Remove the system menu, If that menu is the icon of the window
939      * as it is in win95, we have to delete the bitmap.
940      */
941     memset(&menuInfo, 0, sizeof(menuInfo));
942     menuInfo.cbSize = sizeof(menuInfo);
943     menuInfo.fMask  = MIIM_DATA | MIIM_TYPE;
944
945     GetMenuItemInfoW(menu,
946                      0,
947                      TRUE,
948                      &menuInfo);
949
950     RemoveMenu(menu,0,MF_BYPOSITION);
951
952     if ( (menuInfo.fType & MFT_BITMAP)           &&
953          (LOWORD(menuInfo.dwTypeData)!=0)        &&
954          (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
955     {
956         DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
957     }
958
959     /* close */
960     DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
961     /* restore */
962     DeleteMenu(menu, SC_RESTORE, MF_BYCOMMAND);
963     /* minimize */
964     DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
965
966     DrawMenuBar(frame);
967
968     return 1;
969 }
970
971
972 /**********************************************************************
973  *                                      MDI_UpdateFrameText
974  *
975  * used when child window is maximized/restored
976  *
977  * Note: lpTitle can be NULL
978  */
979 static void MDI_UpdateFrameText( HWND frame, HWND hClient, BOOL repaint, LPCWSTR lpTitle )
980 {
981     WCHAR   lpBuffer[MDI_MAXTITLELENGTH+1];
982     MDICLIENTINFO *ci = get_client_info( hClient );
983
984     TRACE("frameText %s\n", debugstr_w(lpTitle));
985
986     if (!ci) return;
987
988     if (!lpTitle && !ci->frameTitle)  /* first time around, get title from the frame window */
989     {
990         GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
991         lpTitle = lpBuffer;
992     }
993
994     /* store new "default" title if lpTitle is not NULL */
995     if (lpTitle)
996     {
997         HeapFree( GetProcessHeap(), 0, ci->frameTitle );
998         if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
999             strcpyW( ci->frameTitle, lpTitle );
1000     }
1001
1002     if (ci->frameTitle)
1003     {
1004         if (ci->hwndChildMaximized)
1005         {
1006             /* combine frame title and child title if possible */
1007
1008             static const WCHAR lpBracket[]  = {' ','-',' ','[',0};
1009             static const WCHAR lpBracket2[]  = {']',0};
1010             int i_frame_text_length = strlenW(ci->frameTitle);
1011
1012             lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1013
1014             if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1015             {
1016                 strcatW( lpBuffer, lpBracket );
1017                 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
1018                                     MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1019                     strcatW( lpBuffer, lpBracket2 );
1020                 else
1021                     lpBuffer[i_frame_text_length] = 0;  /* remove bracket */
1022             }
1023         }
1024         else
1025         {
1026             lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1027         }
1028     }
1029     else
1030         lpBuffer[0] = '\0';
1031
1032     DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1033
1034     if (repaint)
1035         SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1036                       SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1037 }
1038
1039
1040 /* ----------------------------- Interface ---------------------------- */
1041
1042
1043 /**********************************************************************
1044  *              MDIClientWndProc_common
1045  */
1046 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1047                                         WPARAM wParam, LPARAM lParam, BOOL unicode )
1048 {
1049     MDICLIENTINFO *ci;
1050
1051     TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1052
1053     if (!(ci = get_client_info( hwnd )))
1054     {
1055         if (message == WM_NCCREATE)
1056         {
1057             WND *wndPtr = WIN_GetPtr( hwnd );
1058             wndPtr->flags |= WIN_ISMDICLIENT;
1059             WIN_ReleasePtr( wndPtr );
1060         }
1061         return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1062                          DefWindowProcA( hwnd, message, wParam, lParam );
1063     }
1064
1065     switch (message)
1066     {
1067       case WM_CREATE:
1068       {
1069           /* Since we are using only cs->lpCreateParams, we can safely
1070            * cast to LPCREATESTRUCTA here */
1071           LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1072           WND *wndPtr = WIN_GetPtr( hwnd );
1073
1074         /* Translation layer doesn't know what's in the cs->lpCreateParams
1075          * so we have to keep track of what environment we're in. */
1076
1077         if( wndPtr->flags & WIN_ISWIN32 )
1078         {
1079             LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1080             ci->hWindowMenu     = ccs->hWindowMenu;
1081             ci->idFirstChild    = ccs->idFirstChild;
1082         }
1083         else
1084         {
1085             LPCLIENTCREATESTRUCT16 ccs = MapSL(PtrToUlong(cs->lpCreateParams));
1086             ci->hWindowMenu     = HMENU_32(ccs->hWindowMenu);
1087             ci->idFirstChild    = ccs->idFirstChild;
1088         }
1089         WIN_ReleasePtr( wndPtr );
1090
1091         ci->hwndChildMaximized  = 0;
1092         ci->child = NULL;
1093         ci->nActiveChildren     = 0;
1094         ci->nTotalCreated       = 0;
1095         ci->frameTitle          = NULL;
1096         ci->mdiFlags            = 0;
1097         ci->hFrameMenu = GetMenu(cs->hwndParent);
1098
1099         if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1100
1101         TRACE("Client created: hwnd %p, Window menu %p, idFirst = %04x\n",
1102               hwnd, ci->hWindowMenu, ci->idFirstChild );
1103         return 0;
1104       }
1105
1106       case WM_DESTROY:
1107       {
1108           if( ci->hwndChildMaximized )
1109               MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndChildMaximized);
1110
1111           ci->nActiveChildren = 0;
1112           MDI_RefreshMenu(ci);
1113
1114           HeapFree( GetProcessHeap(), 0, ci->child );
1115           HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1116
1117           return 0;
1118       }
1119
1120       case WM_MDIACTIVATE:
1121       {
1122         if( ci->hwndActiveChild != (HWND)wParam )
1123             SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1124         return 0;
1125       }
1126
1127       case WM_MDICASCADE:
1128         return MDICascade(hwnd, ci);
1129
1130       case WM_MDICREATE:
1131         if (lParam)
1132         {
1133             HWND child;
1134
1135             if (unicode)
1136             {
1137                 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1138                 child = CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1139                                             csW->szTitle, csW->style,
1140                                             csW->x, csW->y, csW->cx, csW->cy,
1141                                             hwnd, 0, csW->hOwner,
1142                                             (LPVOID)csW->lParam);
1143             }
1144             else
1145             {
1146                 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1147                 child = CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1148                                             csA->szTitle, csA->style,
1149                                             csA->x, csA->y, csA->cx, csA->cy,
1150                                             hwnd, 0, csA->hOwner,
1151                                             (LPVOID)csA->lParam);
1152             }
1153             return (LRESULT)child;
1154         }
1155         return 0;
1156
1157       case WM_MDIDESTROY:
1158           return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1159
1160       case WM_MDIGETACTIVE:
1161           if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1162           return (LRESULT)ci->hwndActiveChild;
1163
1164       case WM_MDIICONARRANGE:
1165         ci->mdiFlags |= MDIF_NEEDUPDATE;
1166         ArrangeIconicWindows( hwnd );
1167         ci->sbRecalc = SB_BOTH+1;
1168         SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1169         return 0;
1170
1171       case WM_MDIMAXIMIZE:
1172         ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1173         return 0;
1174
1175       case WM_MDINEXT: /* lParam != 0 means previous window */
1176       {
1177         HWND next = MDI_GetWindow( ci, WIN_GetFullHandle( (HWND)wParam ), !lParam, 0 );
1178         MDI_SwitchActiveChild( ci, next, TRUE );
1179         break;
1180       }
1181
1182       case WM_MDIRESTORE:
1183         ShowWindow( (HWND)wParam, SW_SHOWNORMAL );
1184         return 0;
1185
1186       case WM_MDISETMENU:
1187           return MDISetMenu( hwnd, (HMENU)wParam, (HMENU)lParam );
1188
1189       case WM_MDIREFRESHMENU:
1190           return MDI_RefreshMenu( ci );
1191
1192       case WM_MDITILE:
1193         ci->mdiFlags |= MDIF_NEEDUPDATE;
1194         ShowScrollBar( hwnd, SB_BOTH, FALSE );
1195         MDITile( hwnd, ci, wParam );
1196         ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1197         return 0;
1198
1199       case WM_VSCROLL:
1200       case WM_HSCROLL:
1201         ci->mdiFlags |= MDIF_NEEDUPDATE;
1202         ScrollChildren( hwnd, message, wParam, lParam );
1203         ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1204         return 0;
1205
1206       case WM_SETFOCUS:
1207           if (ci->hwndActiveChild && !IsIconic( ci->hwndActiveChild ))
1208               SetFocus( ci->hwndActiveChild );
1209           return 0;
1210
1211       case WM_NCACTIVATE:
1212         if( ci->hwndActiveChild )
1213             SendMessageW(ci->hwndActiveChild, message, wParam, lParam);
1214         break;
1215
1216       case WM_PARENTNOTIFY:
1217         switch (LOWORD(wParam))
1218         {
1219         case WM_CREATE:
1220             if (GetWindowLongW((HWND)lParam, GWL_EXSTYLE) & WS_EX_MDICHILD)
1221             {
1222                 ci->nTotalCreated++;
1223                 ci->nActiveChildren++;
1224
1225                 if (!ci->child)
1226                     ci->child = HeapAlloc(GetProcessHeap(), 0, sizeof(HWND));
1227                 else
1228                     ci->child = HeapReAlloc(GetProcessHeap(), 0, ci->child, sizeof(HWND) * ci->nActiveChildren);
1229
1230                 TRACE("Adding MDI child %p, # of children %d\n",
1231                       (HWND)lParam, ci->nActiveChildren);
1232
1233                 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1234             }
1235             break;
1236
1237         case WM_LBUTTONDOWN:
1238             {
1239             HWND child;
1240             POINT pt;
1241             pt.x = (short)LOWORD(lParam);
1242             pt.y = (short)HIWORD(lParam);
1243             child = ChildWindowFromPoint(hwnd, pt);
1244
1245             TRACE("notification from %p (%i,%i)\n",child,pt.x,pt.y);
1246
1247             if( child && child != hwnd && child != ci->hwndActiveChild )
1248                 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1249             break;
1250             }
1251
1252         case WM_DESTROY:
1253             return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)lParam ), FALSE );
1254         }
1255         return 0;
1256
1257       case WM_SIZE:
1258         if( ci->hwndChildMaximized )
1259         {
1260             RECT        rect;
1261
1262             rect.left = 0;
1263             rect.top = 0;
1264             rect.right = LOWORD(lParam);
1265             rect.bottom = HIWORD(lParam);
1266
1267             AdjustWindowRectEx( &rect, GetWindowLongA(ci->hwndChildMaximized, GWL_STYLE),
1268                                0, GetWindowLongA(ci->hwndChildMaximized, GWL_EXSTYLE) );
1269             MoveWindow( ci->hwndChildMaximized, rect.left, rect.top,
1270                          rect.right - rect.left, rect.bottom - rect.top, 1);
1271         }
1272         else
1273             MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1274
1275         break;
1276
1277       case WM_MDICALCCHILDSCROLL:
1278         if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1279         {
1280             CalcChildScroll(hwnd, ci->sbRecalc-1);
1281             ci->sbRecalc = 0;
1282             ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1283         }
1284         return 0;
1285     }
1286     return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1287                      DefWindowProcA( hwnd, message, wParam, lParam );
1288 }
1289
1290 /***********************************************************************
1291  *              MDIClientWndProcA
1292  */
1293 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1294 {
1295     if (!IsWindow(hwnd)) return 0;
1296     return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1297 }
1298
1299 /***********************************************************************
1300  *              MDIClientWndProcW
1301  */
1302 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1303 {
1304     if (!IsWindow(hwnd)) return 0;
1305     return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1306 }
1307
1308 /***********************************************************************
1309  *              DefFrameProcA (USER32.@)
1310  */
1311 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1312                                 UINT message, WPARAM wParam, LPARAM lParam)
1313 {
1314     if (hwndMDIClient)
1315     {
1316         switch (message)
1317         {
1318         case WM_SETTEXT:
1319             {
1320                 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1321                 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1322                 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1323                 MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, text );
1324                 HeapFree( GetProcessHeap(), 0, text );
1325             }
1326             return 1; /* success. FIXME: check text length */
1327
1328         case WM_COMMAND:
1329         case WM_NCACTIVATE:
1330         case WM_NEXTMENU:
1331         case WM_SETFOCUS:
1332         case WM_SIZE:
1333             return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1334         }
1335     }
1336     return DefWindowProcA(hwnd, message, wParam, lParam);
1337 }
1338
1339
1340 /***********************************************************************
1341  *              DefFrameProcW (USER32.@)
1342  */
1343 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1344                                 UINT message, WPARAM wParam, LPARAM lParam)
1345 {
1346     MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1347
1348     TRACE("%p %p %04x (%s) %08lx %08lx\n", hwnd, hwndMDIClient, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1349
1350     if (ci)
1351     {
1352         switch (message)
1353         {
1354         case WM_COMMAND:
1355             {
1356                 WORD id = LOWORD(wParam);
1357                 /* check for possible syscommands for maximized MDI child */
1358                 if (id <  ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1359                 {
1360                     if( (id - 0xf000) & 0xf00f ) break;
1361                     if( !ci->hwndChildMaximized ) break;
1362                     switch( id )
1363                     {
1364                     case SC_CLOSE:
1365                         if (!is_close_enabled(ci->hwndActiveChild, 0)) break;
1366                     case SC_SIZE:
1367                     case SC_MOVE:
1368                     case SC_MINIMIZE:
1369                     case SC_MAXIMIZE:
1370                     case SC_NEXTWINDOW:
1371                     case SC_PREVWINDOW:
1372                     case SC_RESTORE:
1373                         return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1374                                              wParam, lParam);
1375                     }
1376                 }
1377                 else
1378                 {
1379                     HWND childHwnd;
1380                     if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1381                         /* User chose "More Windows..." */
1382                         childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1383                     else
1384                         /* User chose one of the windows listed in the "Windows" menu */
1385                         childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1386
1387                     if( childHwnd )
1388                         SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1389                 }
1390             }
1391             break;
1392
1393         case WM_NCACTIVATE:
1394             SendMessageW(hwndMDIClient, message, wParam, lParam);
1395             break;
1396
1397         case WM_SETTEXT:
1398             MDI_UpdateFrameText( hwnd, hwndMDIClient, FALSE, (LPWSTR)lParam );
1399             return 1; /* success. FIXME: check text length */
1400
1401         case WM_SETFOCUS:
1402             SetFocus(hwndMDIClient);
1403             break;
1404
1405         case WM_SIZE:
1406             MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1407             break;
1408
1409         case WM_NEXTMENU:
1410             {
1411                 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1412
1413                 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1414                 {
1415                     /* control menu is between the frame system menu and
1416                      * the first entry of menu bar */
1417                     WND *wndPtr = WIN_GetPtr(hwnd);
1418
1419                     if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1420                         (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1421                     {
1422                         WIN_ReleasePtr(wndPtr);
1423                         wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1424                         next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1425                         next_menu->hwndNext = ci->hwndActiveChild;
1426                     }
1427                     WIN_ReleasePtr(wndPtr);
1428                 }
1429                 return 0;
1430             }
1431         }
1432     }
1433
1434     return DefWindowProcW( hwnd, message, wParam, lParam );
1435 }
1436
1437 /***********************************************************************
1438  *              DefMDIChildProcA (USER32.@)
1439  */
1440 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1441                                    WPARAM wParam, LPARAM lParam )
1442 {
1443     HWND client = GetParent(hwnd);
1444     MDICLIENTINFO *ci = get_client_info( client );
1445
1446     TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1447
1448     hwnd = WIN_GetFullHandle( hwnd );
1449     if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1450
1451     switch (message)
1452     {
1453     case WM_SETTEXT:
1454         DefWindowProcA(hwnd, message, wParam, lParam);
1455         if( ci->hwndChildMaximized == hwnd )
1456             MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1457         return 1; /* success. FIXME: check text length */
1458
1459     case WM_GETMINMAXINFO:
1460     case WM_MENUCHAR:
1461     case WM_CLOSE:
1462     case WM_SETFOCUS:
1463     case WM_CHILDACTIVATE:
1464     case WM_SYSCOMMAND:
1465     case WM_SHOWWINDOW:
1466     case WM_SETVISIBLE:
1467     case WM_SIZE:
1468     case WM_NEXTMENU:
1469     case WM_SYSCHAR:
1470     case WM_DESTROY:
1471         return DefMDIChildProcW( hwnd, message, wParam, lParam );
1472     }
1473     return DefWindowProcA(hwnd, message, wParam, lParam);
1474 }
1475
1476
1477 /***********************************************************************
1478  *              DefMDIChildProcW (USER32.@)
1479  */
1480 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1481                                    WPARAM wParam, LPARAM lParam )
1482 {
1483     HWND client = GetParent(hwnd);
1484     MDICLIENTINFO *ci = get_client_info( client );
1485
1486     TRACE("%p %04x (%s) %08lx %08lx\n", hwnd, message, SPY_GetMsgName(message, hwnd), wParam, lParam);
1487
1488     hwnd = WIN_GetFullHandle( hwnd );
1489     if (!ci) return DefWindowProcW( hwnd, message, wParam, lParam );
1490
1491     switch (message)
1492     {
1493     case WM_SETTEXT:
1494         DefWindowProcW(hwnd, message, wParam, lParam);
1495         if( ci->hwndChildMaximized == hwnd )
1496             MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1497         return 1; /* success. FIXME: check text length */
1498
1499     case WM_GETMINMAXINFO:
1500         MDI_ChildGetMinMaxInfo( client, hwnd, (MINMAXINFO *)lParam );
1501         return 0;
1502
1503     case WM_MENUCHAR:
1504         return 0x00010000; /* MDI children don't have menu bars */
1505
1506     case WM_CLOSE:
1507         SendMessageW( client, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1508         return 0;
1509
1510     case WM_SETFOCUS:
1511         if (ci->hwndActiveChild != hwnd)
1512             MDI_ChildActivate( client, hwnd );
1513         break;
1514
1515     case WM_CHILDACTIVATE:
1516         MDI_ChildActivate( client, hwnd );
1517         return 0;
1518
1519     case WM_SYSCOMMAND:
1520         switch (wParam & 0xfff0)
1521         {
1522         case SC_MOVE:
1523             if( ci->hwndChildMaximized == hwnd )
1524                 return 0;
1525             break;
1526         case SC_RESTORE:
1527         case SC_MINIMIZE:
1528             break;
1529         case SC_MAXIMIZE:
1530             if (ci->hwndChildMaximized == hwnd)
1531                 return SendMessageW( GetParent(client), message, wParam, lParam);
1532             break;
1533         case SC_NEXTWINDOW:
1534             SendMessageW( client, WM_MDINEXT, 0, 0);
1535             return 0;
1536         case SC_PREVWINDOW:
1537             SendMessageW( client, WM_MDINEXT, 0, 1);
1538             return 0;
1539         }
1540         break;
1541
1542     case WM_SHOWWINDOW:
1543     case WM_SETVISIBLE:
1544         if (ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1545         else MDI_PostUpdate(client, ci, SB_BOTH+1);
1546         break;
1547
1548     case WM_SIZE:
1549         /* This is the only place where we switch to/from maximized state */
1550         /* do not change */
1551         TRACE("current active %p, maximized %p\n", ci->hwndActiveChild, ci->hwndChildMaximized);
1552
1553         if( ci->hwndChildMaximized == hwnd && wParam != SIZE_MAXIMIZED )
1554         {
1555             HWND frame;
1556
1557             ci->hwndChildMaximized = 0;
1558
1559             frame = GetParent(client);
1560             MDI_RestoreFrameMenu( frame, hwnd );
1561             MDI_UpdateFrameText( frame, client, TRUE, NULL );
1562         }
1563
1564         if( wParam == SIZE_MAXIMIZED )
1565         {
1566             HWND frame, hMaxChild = ci->hwndChildMaximized;
1567
1568             if( hMaxChild == hwnd ) break;
1569
1570             if( hMaxChild)
1571             {
1572                 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1573
1574                 MDI_RestoreFrameMenu( GetParent(client), hMaxChild );
1575                 ShowWindow( hMaxChild, SW_SHOWNOACTIVATE );
1576
1577                 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1578             }
1579
1580             TRACE("maximizing child %p\n", hwnd );
1581
1582             /* keep track of the maximized window. */
1583             ci->hwndChildMaximized = hwnd; /* !!! */
1584
1585             frame = GetParent(client);
1586             MDI_AugmentFrameMenu( frame, hwnd );
1587             MDI_UpdateFrameText( frame, client, TRUE, NULL );
1588         }
1589
1590         if( wParam == SIZE_MINIMIZED )
1591         {
1592             HWND switchTo = MDI_GetWindow( ci, hwnd, TRUE, WS_MINIMIZE );
1593
1594             if (!switchTo) switchTo = hwnd;
1595             SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0 );
1596         }
1597
1598         MDI_PostUpdate(client, ci, SB_BOTH+1);
1599         break;
1600
1601     case WM_NEXTMENU:
1602         {
1603             MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1604             HWND parent = GetParent(client);
1605
1606             if( wParam == VK_LEFT )  /* switch to frame system menu */
1607             {
1608                 WND *wndPtr = WIN_GetPtr( parent );
1609                 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1610                 WIN_ReleasePtr( wndPtr );
1611             }
1612             if( wParam == VK_RIGHT )  /* to frame menu bar */
1613             {
1614                 next_menu->hmenuNext = GetMenu(parent);
1615             }
1616             next_menu->hwndNext = parent;
1617             return 0;
1618         }
1619
1620     case WM_SYSCHAR:
1621         if (wParam == '-')
1622         {
1623             SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1624             return 0;
1625         }
1626         break;
1627
1628     case WM_DESTROY:
1629         /* Remove itself from the Window menu */
1630         MDI_RefreshMenu(ci);
1631         break;
1632     }
1633     return DefWindowProcW(hwnd, message, wParam, lParam);
1634 }
1635
1636 /**********************************************************************
1637  *              CreateMDIWindowA (USER32.@) Creates a MDI child
1638  *
1639  * RETURNS
1640  *    Success: Handle to created window
1641  *    Failure: NULL
1642  */
1643 HWND WINAPI CreateMDIWindowA(
1644     LPCSTR lpClassName,    /* [in] Pointer to registered child class name */
1645     LPCSTR lpWindowName,   /* [in] Pointer to window name */
1646     DWORD dwStyle,         /* [in] Window style */
1647     INT X,               /* [in] Horizontal position of window */
1648     INT Y,               /* [in] Vertical position of window */
1649     INT nWidth,          /* [in] Width of window */
1650     INT nHeight,         /* [in] Height of window */
1651     HWND hWndParent,     /* [in] Handle to parent window */
1652     HINSTANCE hInstance, /* [in] Handle to application instance */
1653     LPARAM lParam)         /* [in] Application-defined value */
1654 {
1655     TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1656           debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1657           nWidth,nHeight,hWndParent,hInstance,lParam);
1658
1659     return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1660                            dwStyle, X, Y, nWidth, nHeight, hWndParent,
1661                            0, hInstance, (LPVOID)lParam);
1662 }
1663
1664 /***********************************************************************
1665  *              CreateMDIWindowW (USER32.@) Creates a MDI child
1666  *
1667  * RETURNS
1668  *    Success: Handle to created window
1669  *    Failure: NULL
1670  */
1671 HWND WINAPI CreateMDIWindowW(
1672     LPCWSTR lpClassName,    /* [in] Pointer to registered child class name */
1673     LPCWSTR lpWindowName,   /* [in] Pointer to window name */
1674     DWORD dwStyle,         /* [in] Window style */
1675     INT X,               /* [in] Horizontal position of window */
1676     INT Y,               /* [in] Vertical position of window */
1677     INT nWidth,          /* [in] Width of window */
1678     INT nHeight,         /* [in] Height of window */
1679     HWND hWndParent,     /* [in] Handle to parent window */
1680     HINSTANCE hInstance, /* [in] Handle to application instance */
1681     LPARAM lParam)         /* [in] Application-defined value */
1682 {
1683     TRACE("(%s,%s,%08x,%d,%d,%d,%d,%p,%p,%08lx)\n",
1684           debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1685           nWidth, nHeight, hWndParent, hInstance, lParam);
1686
1687     return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1688                            dwStyle, X, Y, nWidth, nHeight, hWndParent,
1689                            0, hInstance, (LPVOID)lParam);
1690 }
1691
1692 /**********************************************************************
1693  *              TranslateMDISysAccel (USER32.@)
1694  */
1695 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1696 {
1697     if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1698     {
1699         MDICLIENTINFO *ci = get_client_info( hwndClient );
1700         WPARAM wParam = 0;
1701
1702         if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1703
1704         /* translate if the Ctrl key is down and Alt not. */
1705
1706         if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1707         {
1708             switch( msg->wParam )
1709             {
1710             case VK_F6:
1711             case VK_TAB:
1712                 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1713                 break;
1714             case VK_F4:
1715             case VK_RBUTTON:
1716                 if (is_close_enabled(ci->hwndActiveChild, 0))
1717                 {
1718                     wParam = SC_CLOSE;
1719                     break;
1720                 }
1721                 /* fall through */
1722             default:
1723                 return 0;
1724             }
1725             TRACE("wParam = %04lx\n", wParam);
1726             SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1727             return 1;
1728         }
1729     }
1730     return 0; /* failure */
1731 }
1732
1733 /***********************************************************************
1734  *              CalcChildScroll (USER32.@)
1735  */
1736 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1737 {
1738     SCROLLINFO info;
1739     RECT childRect, clientRect;
1740     HWND *list;
1741
1742     GetClientRect( hwnd, &clientRect );
1743     SetRectEmpty( &childRect );
1744
1745     if ((list = WIN_ListChildren( hwnd )))
1746     {
1747         int i;
1748         for (i = 0; list[i]; i++)
1749         {
1750             DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1751             if (style & WS_MAXIMIZE)
1752             {
1753                 HeapFree( GetProcessHeap(), 0, list );
1754                 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1755                 return;
1756             }
1757             if (style & WS_VISIBLE)
1758             {
1759                 RECT rect;
1760                 GetWindowRect( list[i], &rect );
1761                 UnionRect( &childRect, &rect, &childRect );
1762             }
1763         }
1764         HeapFree( GetProcessHeap(), 0, list );
1765     }
1766     MapWindowPoints( 0, hwnd, (POINT *)&childRect, 2 );
1767     UnionRect( &childRect, &clientRect, &childRect );
1768
1769     /* set common info values */
1770     info.cbSize = sizeof(info);
1771     info.fMask = SIF_POS | SIF_RANGE;
1772
1773     /* set the specific */
1774     switch( scroll )
1775     {
1776         case SB_BOTH:
1777         case SB_HORZ:
1778                         info.nMin = childRect.left;
1779                         info.nMax = childRect.right - clientRect.right;
1780                         info.nPos = clientRect.left - childRect.left;
1781                         SetScrollInfo(hwnd, SB_HORZ, &info, TRUE);
1782                         if (scroll == SB_HORZ) break;
1783                         /* fall through */
1784         case SB_VERT:
1785                         info.nMin = childRect.top;
1786                         info.nMax = childRect.bottom - clientRect.bottom;
1787                         info.nPos = clientRect.top - childRect.top;
1788                         SetScrollInfo(hwnd, SB_VERT, &info, TRUE);
1789                         break;
1790     }
1791 }
1792
1793
1794 /***********************************************************************
1795  *              ScrollChildren (USER32.@)
1796  */
1797 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
1798                              LPARAM lParam)
1799 {
1800     INT newPos = -1;
1801     INT curPos, length, minPos, maxPos, shift;
1802     RECT rect;
1803
1804     GetClientRect( hWnd, &rect );
1805
1806     switch(uMsg)
1807     {
1808     case WM_HSCROLL:
1809         GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1810         curPos = GetScrollPos(hWnd,SB_HORZ);
1811         length = (rect.right - rect.left) / 2;
1812         shift = GetSystemMetrics(SM_CYHSCROLL);
1813         break;
1814     case WM_VSCROLL:
1815         GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1816         curPos = GetScrollPos(hWnd,SB_VERT);
1817         length = (rect.bottom - rect.top) / 2;
1818         shift = GetSystemMetrics(SM_CXVSCROLL);
1819         break;
1820     default:
1821         return;
1822     }
1823
1824     switch( wParam )
1825     {
1826         case SB_LINEUP:
1827                         newPos = curPos - shift;
1828                         break;
1829         case SB_LINEDOWN:
1830                         newPos = curPos + shift;
1831                         break;
1832         case SB_PAGEUP:
1833                         newPos = curPos - length;
1834                         break;
1835         case SB_PAGEDOWN:
1836                         newPos = curPos + length;
1837                         break;
1838
1839         case SB_THUMBPOSITION:
1840                         newPos = LOWORD(lParam);
1841                         break;
1842
1843         case SB_THUMBTRACK:
1844                         return;
1845
1846         case SB_TOP:
1847                         newPos = minPos;
1848                         break;
1849         case SB_BOTTOM:
1850                         newPos = maxPos;
1851                         break;
1852         case SB_ENDSCROLL:
1853                         CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1854                         return;
1855     }
1856
1857     if( newPos > maxPos )
1858         newPos = maxPos;
1859     else
1860         if( newPos < minPos )
1861             newPos = minPos;
1862
1863     SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1864
1865     if( uMsg == WM_VSCROLL )
1866         ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL,
1867                         SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1868     else
1869         ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
1870                         SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
1871 }
1872
1873
1874 /******************************************************************************
1875  *              CascadeWindows (USER32.@) Cascades MDI child windows
1876  *
1877  * RETURNS
1878  *    Success: Number of cascaded windows.
1879  *    Failure: 0
1880  */
1881 WORD WINAPI
1882 CascadeWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1883                 UINT cKids, const HWND *lpKids)
1884 {
1885     FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1886     return 0;
1887 }
1888
1889
1890 /******************************************************************************
1891  *              TileWindows (USER32.@) Tiles MDI child windows
1892  *
1893  * RETURNS
1894  *    Success: Number of tiled windows.
1895  *    Failure: 0
1896  */
1897 WORD WINAPI
1898 TileWindows (HWND hwndParent, UINT wFlags, const RECT *lpRect,
1899              UINT cKids, const HWND *lpKids)
1900 {
1901     FIXME("(%p,0x%08x,...,%u,...): stub\n", hwndParent, wFlags, cKids);
1902     return 0;
1903 }
1904
1905 /************************************************************************
1906  *              "More Windows..." functionality
1907  */
1908
1909 /*              MDI_MoreWindowsDlgProc
1910  *
1911  *    This function will process the messages sent to the "More Windows..."
1912  *    dialog.
1913  *    Return values:  0    = cancel pressed
1914  *                    HWND = ok pressed or double-click in the list...
1915  *
1916  */
1917
1918 static INT_PTR WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
1919 {
1920     switch (iMsg)
1921     {
1922        case WM_INITDIALOG:
1923        {
1924            UINT widest       = 0;
1925            UINT length;
1926            UINT i;
1927            MDICLIENTINFO *ci = get_client_info( (HWND)lParam );
1928            HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1929
1930            for (i = 0; i < ci->nActiveChildren; i++)
1931            {
1932                WCHAR buffer[MDI_MAXTITLELENGTH];
1933
1934                if (!InternalGetWindowText( ci->child[i], buffer, sizeof(buffer)/sizeof(WCHAR) ))
1935                    continue;
1936                SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)buffer );
1937                SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM)ci->child[i] );
1938                length = strlenW(buffer);  /* FIXME: should use GetTextExtentPoint */
1939                if (length > widest)
1940                    widest = length;
1941            }
1942            /* Make sure the horizontal scrollbar scrolls ok */
1943            SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
1944
1945            /* Set the current selection */
1946            SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
1947            return TRUE;
1948        }
1949
1950        case WM_COMMAND:
1951            switch (LOWORD(wParam))
1952            {
1953                 default:
1954                     if (HIWORD(wParam) != LBN_DBLCLK) break;
1955                     /* fall through */
1956                 case IDOK:
1957                 {
1958                     /*  windows are sorted by menu ID, so we must return the
1959                      *  window associated to the given id
1960                      */
1961                     HWND hListBox     = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
1962                     UINT index        = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
1963                     LRESULT res = SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
1964                     EndDialog(hDlg, res);
1965                     return TRUE;
1966                 }
1967                 case IDCANCEL:
1968                     EndDialog(hDlg, 0);
1969                     return TRUE;
1970            }
1971            break;
1972     }
1973     return FALSE;
1974 }
1975
1976 /*
1977  *
1978  *                      MDI_MoreWindowsDialog
1979  *
1980  *     Prompts the user with a listbox containing the opened
1981  *     documents. The user can then choose a windows and click
1982  *     on OK to set the current window to the one selected, or
1983  *     CANCEL to cancel. The function returns a handle to the
1984  *     selected window.
1985  */
1986
1987 static HWND MDI_MoreWindowsDialog(HWND hwnd)
1988 {
1989     LPCVOID template;
1990     HRSRC hRes;
1991     HANDLE hDlgTmpl;
1992
1993     hRes = FindResourceA(user32_module, "MDI_MOREWINDOWS", (LPSTR)RT_DIALOG);
1994
1995     if (hRes == 0)
1996         return 0;
1997
1998     hDlgTmpl = LoadResource(user32_module, hRes );
1999
2000     if (hDlgTmpl == 0)
2001         return 0;
2002
2003     template = LockResource( hDlgTmpl );
2004
2005     if (template == 0)
2006         return 0;
2007
2008     return (HWND) DialogBoxIndirectParamA(user32_module,
2009                                           (const DLGTEMPLATE*) template,
2010                                           hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);
2011 }