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