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