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