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