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