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