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