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