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