Avoid a few more GetModuleHandle calls.
[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 "user.h"
99 #include "struct32.h"
100 #include "wine/debug.h"
101 #include "dlgs.h"
102
103 WINE_DEFAULT_DEBUG_CHANNEL(mdi);
104
105 #define MDI_MAXTITLELENGTH      0xa1
106
107 #define WM_MDICALCCHILDSCROLL   0x10ac /* this is exactly what Windows uses */
108
109 /* "More Windows..." definitions */
110 #define MDI_MOREWINDOWSLIMIT    9       /* after this number of windows, a "More Windows..."
111                                            option will appear under the Windows menu */
112 #define MDI_IDC_LISTBOX         100
113 #define IDS_MDI_MOREWINDOWS     13
114
115 #define MDIF_NEEDUPDATE         0x0001
116
117 typedef struct
118 {
119     UINT      nActiveChildren;
120     HWND      hwndActiveChild;
121     HWND      *child; /* array of tracked children */
122     HMENU     hFrameMenu;
123     HMENU     hWindowMenu;
124     UINT      idFirstChild;
125     LPWSTR    frameTitle;
126     UINT      nTotalCreated;
127     UINT      mdiFlags;
128     UINT      sbRecalc;   /* SB_xxx flags for scrollbar fixup */
129 } MDICLIENTINFO;
130
131 static HBITMAP hBmpClose   = 0;
132
133 /* ----------------- declarations ----------------- */
134 static void MDI_UpdateFrameText( HWND, HWND, BOOL, LPCWSTR);
135 static BOOL MDI_AugmentFrameMenu( HWND, HWND );
136 static BOOL MDI_RestoreFrameMenu( HWND, HWND );
137 static LONG MDI_ChildActivate( HWND, HWND );
138 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *);
139
140 static HWND MDI_MoreWindowsDialog(HWND);
141 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
142 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
143
144 /* -------- Miscellaneous service functions ----------
145  *
146  *                      MDI_GetChildByID
147  */
148 static HWND MDI_GetChildByID(HWND hwnd, UINT id, MDICLIENTINFO *ci)
149 {
150     int i;
151
152     for (i = 0; ci->nActiveChildren; i++)
153     {
154         if (GetWindowLongW( ci->child[i], GWL_ID ) == id)
155             return ci->child[i];
156     }
157     return 0;
158 }
159
160 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
161 {
162     if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
163     {
164         ci->mdiFlags |= MDIF_NEEDUPDATE;
165         PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
166     }
167     ci->sbRecalc = recalc;
168 }
169
170
171 /*********************************************************************
172  * MDIClient class descriptor
173  */
174 const struct builtin_class_descr MDICLIENT_builtin_class =
175 {
176     "MDIClient",            /* name */
177     0,                      /* style */
178     MDIClientWndProcA,      /* procA */
179     MDIClientWndProcW,      /* procW */
180     sizeof(MDICLIENTINFO),  /* extra */
181     IDC_ARROW,              /* cursor */
182     (HBRUSH)(COLOR_APPWORKSPACE+1)    /* brush */
183 };
184
185
186 static MDICLIENTINFO *get_client_info( HWND client )
187 {
188     MDICLIENTINFO *ret = NULL;
189     WND *win = WIN_GetPtr( client );
190     if (win)
191     {
192         if (win == WND_OTHER_PROCESS)
193         {
194             ERR( "client %p belongs to other process\n", client );
195             return NULL;
196         }
197         if (win->cbWndExtra < sizeof(MDICLIENTINFO)) WARN( "%p is not an MDI client\n", client );
198         else ret = (MDICLIENTINFO *)win->wExtra;
199         WIN_ReleasePtr( win );
200     }
201     return ret;
202 }
203
204 /**********************************************************************
205  *                      MDI_GetWindow
206  *
207  * returns "activateable" child different from the current or zero
208  */
209 static HWND MDI_GetWindow(MDICLIENTINFO *clientInfo, HWND hWnd, BOOL bNext,
210                             DWORD dwStyleMask )
211 {
212     int i;
213     HWND *list;
214     HWND last = 0;
215
216     dwStyleMask |= WS_DISABLED | WS_VISIBLE;
217     if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
218
219     if (!(list = WIN_ListChildren( GetParent(hWnd) ))) return 0;
220     i = 0;
221     /* start from next after hWnd */
222     while (list[i] && list[i] != hWnd) i++;
223     if (list[i]) i++;
224
225     for ( ; list[i]; i++)
226     {
227         if (GetWindow( list[i], GW_OWNER )) continue;
228         if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
229         last = list[i];
230         if (bNext) goto found;
231     }
232     /* now restart from the beginning */
233     for (i = 0; list[i] && list[i] != hWnd; i++)
234     {
235         if (GetWindow( list[i], GW_OWNER )) continue;
236         if ((GetWindowLongW( list[i], GWL_STYLE ) & dwStyleMask) != WS_VISIBLE) continue;
237         last = list[i];
238         if (bNext) goto found;
239     }
240  found:
241     HeapFree( GetProcessHeap(), 0, list );
242     return last;
243 }
244
245 /**********************************************************************
246  *                      MDI_CalcDefaultChildPos
247  *
248  *  It seems that the default height is about 2/3 of the client rect
249  */
250 void MDI_CalcDefaultChildPos( HWND hwndClient, INT total, LPPOINT lpPos, INT delta )
251 {
252     INT  nstagger;
253     RECT rect;
254     INT  spacing = GetSystemMetrics(SM_CYCAPTION) +
255                      GetSystemMetrics(SM_CYFRAME) - 1;
256
257     if (total < 0)
258     {
259         MDICLIENTINFO *ci = get_client_info(hwndClient);
260         total = ci ? ci->nTotalCreated : 0;
261     }
262
263     GetClientRect( hwndClient, &rect );
264     if( rect.bottom - rect.top - delta >= spacing )
265         rect.bottom -= delta;
266
267     nstagger = (rect.bottom - rect.top)/(3 * spacing);
268     lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
269     lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
270     lpPos[0].x = lpPos[0].y = spacing * (total%(nstagger+1));
271 }
272
273 /**********************************************************************
274  *            MDISetMenu
275  */
276 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
277                            HMENU hmenuWindow)
278 {
279     MDICLIENTINFO *ci;
280     HWND hwndFrame = GetParent(hwnd);
281
282     TRACE("%p %p %p\n", hwnd, hmenuFrame, hmenuWindow);
283
284     if (hmenuFrame && !IsMenu(hmenuFrame))
285     {
286         WARN("hmenuFrame is not a menu handle\n");
287         return 0L;
288     }
289
290     if (hmenuWindow && !IsMenu(hmenuWindow))
291     {
292         WARN("hmenuWindow is not a menu handle\n");
293         return 0L;
294     }
295
296     if (!(ci = get_client_info( hwnd ))) return 0;
297
298     if( IsZoomed(ci->hwndActiveChild) && hmenuFrame && hmenuFrame!= ci->hFrameMenu )
299         MDI_RestoreFrameMenu( hwndFrame, ci->hwndActiveChild );
300
301     if( hmenuWindow && hmenuWindow != ci->hWindowMenu )
302     {
303         /* delete menu items from ci->hWindowMenu
304          * and add them to hmenuWindow */
305         /* Agent newsreader calls this function with  ci->hWindowMenu == NULL */
306         if( ci->hWindowMenu && ci->nActiveChildren )
307         {
308             UINT nActiveChildren_old = ci->nActiveChildren;
309
310             /* Remove all items from old Window menu */
311             ci->nActiveChildren = 0;
312             MDI_RefreshMenu(ci);
313
314             ci->hWindowMenu = hmenuWindow;
315
316             /* Add items to the new Window menu */
317             ci->nActiveChildren = nActiveChildren_old;
318             MDI_RefreshMenu(ci);
319         }
320         else
321             ci->hWindowMenu = hmenuWindow;
322     }
323
324     if (hmenuFrame)
325     {
326         SetMenu(hwndFrame, hmenuFrame);
327         if( hmenuFrame != ci->hFrameMenu )
328         {
329             HMENU oldFrameMenu = ci->hFrameMenu;
330
331             ci->hFrameMenu = hmenuFrame;
332             if( IsZoomed(ci->hwndActiveChild) )
333                 MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
334
335             return (LRESULT)oldFrameMenu;
336         }
337     }
338     else
339     {
340         /* SetMenu() may already have been called, meaning that this window
341          * already has its menu. But they may have done a SetMenu() on
342          * an MDI window, and called MDISetMenu() after the fact, meaning
343          * that the "if" to this "else" wouldn't catch the need to
344          * augment the frame menu.
345          */
346         if( IsZoomed(ci->hwndActiveChild) )
347             MDI_AugmentFrameMenu( hwndFrame, ci->hwndActiveChild );
348     }
349
350     return 0;
351 }
352
353 /**********************************************************************
354  *            MDIRefreshMenu
355  */
356 static LRESULT MDI_RefreshMenu(MDICLIENTINFO *ci)
357 {
358     UINT i, count, visible, separator_pos;
359     WCHAR buf[MDI_MAXTITLELENGTH];
360
361     TRACE("children %u, window menu %p\n", ci->nActiveChildren, ci->hWindowMenu);
362
363     if (!ci->hWindowMenu)
364         return 0;
365
366     if (!IsMenu(ci->hWindowMenu))
367     {
368         WARN("Window menu handle %p is no more valid\n", ci->hWindowMenu);
369         return 0;
370     }
371
372     /* Windows finds the last separator in the menu, removes all existing
373      * menu items after it, and then adds visible MDI children.
374      */
375     separator_pos = 0;
376     count = GetMenuItemCount(ci->hWindowMenu);
377     for (i = 0; i < count; i++)
378     {
379         MENUITEMINFOW mii;
380
381         memset(&mii, 0, sizeof(mii));
382         mii.cbSize = sizeof(mii);
383         mii.fMask  = MIIM_TYPE;
384         GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
385
386         if (mii.fType & MFT_SEPARATOR)
387             separator_pos = i;
388     }
389
390     for (i = separator_pos + 1; i < count; i++)
391         RemoveMenu(ci->hWindowMenu, separator_pos + 1, MF_BYPOSITION);
392
393     visible = 0;
394     for (i = 0; i < ci->nActiveChildren; i++)
395     {
396         UINT id = ci->idFirstChild + i;
397
398         if (visible == MDI_MOREWINDOWSLIMIT)
399         {
400             LoadStringW(user32_module, IDS_MDI_MOREWINDOWS, buf, sizeof(buf)/sizeof(WCHAR));
401             AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
402             break;
403         }
404
405         if (GetWindowLongW(ci->child[i], GWL_STYLE) & WS_VISIBLE)
406         {
407             if (!visible && !separator_pos)
408                 /* Visio expects that separator has id 0 */
409                 AppendMenuW(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
410
411             visible++;
412
413             SetWindowLongW(ci->child[i], GWL_ID, id);
414
415             buf[0] = '&';
416             buf[1] = '0' + visible;
417             buf[2] = ' ';
418             InternalGetWindowText(ci->child[i], buf + 3, sizeof(buf)/sizeof(WCHAR) - 3);
419             TRACE("Adding %p, id %u %s\n", ci->child[i], id, debugstr_w(buf));
420             AppendMenuW(ci->hWindowMenu, MF_STRING, id, buf);
421
422             if (ci->child[i] == ci->hwndActiveChild)
423                 CheckMenuItem(ci->hWindowMenu, id, MF_CHECKED);
424         }
425         else
426             TRACE("MDI child %p is not visible, skipping\n", ci->child[i]);
427     }
428
429     return (LRESULT)ci->hFrameMenu;
430 }
431
432
433 /* ------------------ MDI child window functions ---------------------- */
434
435 /**********************************************************************
436  *                      MDI_ChildGetMinMaxInfo
437  *
438  * Note: The rule here is that client rect of the maximized MDI child
439  *       is equal to the client rect of the MDI client window.
440  */
441 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
442 {
443     RECT rect;
444
445     GetClientRect( client, &rect );
446     AdjustWindowRectEx( &rect, GetWindowLongW( hwnd, GWL_STYLE ),
447                         0, GetWindowLongW( hwnd, GWL_EXSTYLE ));
448
449     lpMinMax->ptMaxSize.x = rect.right -= rect.left;
450     lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
451
452     lpMinMax->ptMaxPosition.x = rect.left;
453     lpMinMax->ptMaxPosition.y = rect.top;
454
455     TRACE("max rect (%ld,%ld - %ld, %ld)\n",
456                         rect.left,rect.top,rect.right,rect.bottom);
457 }
458
459 /**********************************************************************
460  *                      MDI_SwitchActiveChild
461  *
462  * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
463  *       being activated
464  */
465 static void MDI_SwitchActiveChild( HWND clientHwnd, HWND childHwnd,
466                                    BOOL bNextWindow )
467 {
468     HWND           hwndTo    = 0;
469     HWND           hwndPrev  = 0;
470     MDICLIENTINFO *ci = get_client_info( clientHwnd );
471
472     hwndTo = MDI_GetWindow(ci, childHwnd, bNextWindow, 0);
473     hwndPrev = ci->hwndActiveChild;
474
475     TRACE("from %p, to %p\n",childHwnd,hwndTo);
476
477     if ( !hwndTo ) return; /* no window to switch to */
478
479     if ( hwndTo != hwndPrev )
480     {
481         SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0,
482                       SWP_NOMOVE | SWP_NOSIZE );
483
484         if( bNextWindow && hwndPrev )
485             SetWindowPos( hwndPrev, HWND_BOTTOM, 0, 0, 0, 0,
486                           SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE );
487     }
488 }
489
490
491 /**********************************************************************
492  *                                      MDIDestroyChild
493  */
494 static LRESULT MDIDestroyChild( HWND parent, MDICLIENTINFO *ci,
495                                 HWND child, BOOL flagDestroy )
496 {
497     UINT i;
498
499     TRACE("# of managed children %u\n", ci->nActiveChildren);
500
501     if( child == ci->hwndActiveChild )
502     {
503         MDI_SwitchActiveChild(parent, child, TRUE);
504
505         if( child == ci->hwndActiveChild )
506         {
507             ShowWindow( child, SW_HIDE);
508             if( child == ci->hwndActiveChild && IsZoomed(ci->hwndActiveChild) )
509             {
510                 HWND frame = GetParent(parent);
511                 MDI_RestoreFrameMenu( frame, child );
512                 MDI_UpdateFrameText( frame, parent, TRUE, NULL);
513             }
514
515             MDI_ChildActivate(parent, 0);
516         }
517     }
518
519     for (i = 0; i < ci->nActiveChildren; i++)
520     {
521         if (ci->child[i] == child)
522         {
523             HWND *new_child = HeapAlloc(GetProcessHeap(), 0, (ci->nActiveChildren - 1) * sizeof(HWND));
524             memcpy(new_child, ci->child, i * sizeof(HWND));
525             if (i + 1 < ci->nActiveChildren)
526                 memcpy(new_child + i, ci->child + i + 1, (ci->nActiveChildren - i - 1) * sizeof(HWND));
527             HeapFree(GetProcessHeap(), 0, ci->child);
528             ci->child = new_child;
529
530             ci->nActiveChildren--;
531             break;
532         }
533     }
534
535     if (flagDestroy)
536     {
537         MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
538         DestroyWindow(child);
539     }
540
541     TRACE("child destroyed - %p\n", child);
542     return 0;
543 }
544
545
546 /**********************************************************************
547  *                                      MDI_ChildActivate
548  *
549  * Note: hWndChild is NULL when last child is being destroyed
550  */
551 static LONG MDI_ChildActivate( HWND client, HWND child )
552 {
553     MDICLIENTINFO *clientInfo;
554     HWND prevActiveWnd;
555     BOOL isActiveFrameWnd;
556
557     if (child && (!IsWindowEnabled( child ))) return 0;
558
559     clientInfo = get_client_info( client );
560
561     /* Don't activate if it is already active. Might happen
562        since ShowWindow DOES activate MDI children */
563     if (clientInfo->hwndActiveChild == child)
564         return 0;
565
566     TRACE("%p\n", child);
567
568     isActiveFrameWnd = (GetActiveWindow() == GetParent(client));
569     prevActiveWnd = clientInfo->hwndActiveChild;
570
571     /* deactivate prev. active child */
572     if(prevActiveWnd)
573     {
574         SetWindowLongW( prevActiveWnd, GWL_STYLE,
575                         GetWindowLongW( prevActiveWnd, GWL_STYLE ) | WS_SYSMENU );
576         SendMessageW( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
577         SendMessageW( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child);
578     }
579
580     /* set appearance */
581     if (IsZoomed(clientInfo->hwndActiveChild) && clientInfo->hwndActiveChild != child)
582     {
583         INT cmd = SW_SHOWNORMAL;
584
585         if( child )
586         {
587             HMENU hSysMenu = GetSystemMenu(child, FALSE);
588             UINT state = 0;
589             if (hSysMenu)
590                 state = GetMenuState(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
591             if (state != 0xFFFFFFFF && !(state & (MF_DISABLED | MF_GRAYED)))
592             {
593                 SendMessageW(clientInfo->hwndActiveChild, WM_SYSCOMMAND, SC_RESTORE, 0);
594                 cmd = SW_SHOWMAXIMIZED;
595             }
596
597             clientInfo->hwndActiveChild = child;
598         }
599
600         ShowWindow( clientInfo->hwndActiveChild, cmd );
601     }
602
603     clientInfo->hwndActiveChild = child;
604
605     /* check if we have any children left */
606     if( !child )
607     {
608         if( isActiveFrameWnd )
609             SetFocus( client );
610         return 0;
611     }
612
613     MDI_RefreshMenu(clientInfo);
614
615     /* bring active child to the top */
616     SetWindowPos( child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
617
618     if( isActiveFrameWnd )
619     {
620             SendMessageA( child, WM_NCACTIVATE, TRUE, 0L);
621             if( GetFocus() == client )
622                 SendMessageA( client, WM_SETFOCUS, (WPARAM)client, 0L );
623             else
624                 SetFocus( client );
625     }
626     SendMessageA( child, WM_MDIACTIVATE, (WPARAM)prevActiveWnd, (LPARAM)child );
627     return TRUE;
628 }
629
630 /* -------------------- MDI client window functions ------------------- */
631
632 /**********************************************************************
633  *                              CreateMDIMenuBitmap
634  */
635 static HBITMAP CreateMDIMenuBitmap(void)
636 {
637  HDC            hDCSrc  = CreateCompatibleDC(0);
638  HDC            hDCDest = CreateCompatibleDC(hDCSrc);
639  HBITMAP        hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_OLD_CLOSE) );
640  HBITMAP        hbCopy;
641  HBITMAP        hobjSrc, hobjDest;
642
643  hobjSrc = SelectObject(hDCSrc, hbClose);
644  hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
645  hobjDest = SelectObject(hDCDest, hbCopy);
646
647  BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
648           hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
649
650  SelectObject(hDCSrc, hobjSrc);
651  DeleteObject(hbClose);
652  DeleteDC(hDCSrc);
653
654  hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
655
656  MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
657  LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
658
659  SelectObject(hDCDest, hobjSrc );
660  SelectObject(hDCDest, hobjDest);
661  DeleteDC(hDCDest);
662
663  return hbCopy;
664 }
665
666 /**********************************************************************
667  *                              MDICascade
668  */
669 static LONG MDICascade( HWND client, MDICLIENTINFO *ci )
670 {
671     HWND *win_array;
672     BOOL has_icons = FALSE;
673     int i, total;
674
675     if (IsZoomed(ci->hwndActiveChild))
676         SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
677
678     if (ci->nActiveChildren == 0) return 0;
679
680     if (!(win_array = WIN_ListChildren( client ))) return 0;
681
682     /* remove all the windows we don't want */
683     for (i = total = 0; win_array[i]; i++)
684     {
685         if (!IsWindowVisible( win_array[i] )) continue;
686         if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows */
687         if (IsIconic( win_array[i] ))
688         {
689             has_icons = TRUE;
690             continue;
691         }
692         win_array[total++] = win_array[i];
693     }
694     win_array[total] = 0;
695
696     if (total)
697     {
698         INT delta = 0, n = 0, i;
699         POINT pos[2];
700         if (has_icons) delta = GetSystemMetrics(SM_CYICONSPACING) + GetSystemMetrics(SM_CYICON);
701
702         /* walk the list (backwards) and move windows */
703         for (i = total - 1; i >= 0; i--)
704         {
705             TRACE("move %p to (%ld,%ld) size [%ld,%ld]\n",
706                   win_array[i], pos[0].x, pos[0].y, pos[1].x, pos[1].y);
707
708             MDI_CalcDefaultChildPos(client, n++, pos, delta);
709             SetWindowPos( win_array[i], 0, pos[0].x, pos[0].y, pos[1].x, pos[1].y,
710                           SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
711         }
712     }
713     HeapFree( GetProcessHeap(), 0, win_array );
714
715     if (has_icons) ArrangeIconicWindows( client );
716     return 0;
717 }
718
719 /**********************************************************************
720  *                                      MDITile
721  */
722 static void MDITile( HWND client, MDICLIENTINFO *ci, WPARAM wParam )
723 {
724     HWND *win_array;
725     int i, total;
726     BOOL has_icons = FALSE;
727
728     if (IsZoomed(ci->hwndActiveChild))
729         SendMessageA(client, WM_MDIRESTORE, (WPARAM)ci->hwndActiveChild, 0);
730
731     if (ci->nActiveChildren == 0) return;
732
733     if (!(win_array = WIN_ListChildren( client ))) return;
734
735     /* remove all the windows we don't want */
736     for (i = total = 0; win_array[i]; i++)
737     {
738         if (!IsWindowVisible( win_array[i] )) continue;
739         if (GetWindow( win_array[i], GW_OWNER )) continue; /* skip owned windows (icon titles) */
740         if (IsIconic( win_array[i] ))
741         {
742             has_icons = TRUE;
743             continue;
744         }
745         if ((wParam & MDITILE_SKIPDISABLED) && !IsWindowEnabled( win_array[i] )) continue;
746         win_array[total++] = win_array[i];
747     }
748     win_array[total] = 0;
749
750     TRACE("%u windows to tile\n", total);
751
752     if (total)
753     {
754         HWND *pWnd = win_array;
755         RECT rect;
756         int x, y, xsize, ysize;
757         int rows, columns, r, c, i;
758
759         GetClientRect(client,&rect);
760         rows    = (int) sqrt((double)total);
761         columns = total / rows;
762
763         if( wParam & MDITILE_HORIZONTAL )  /* version >= 3.1 */
764         {
765             i = rows;
766             rows = columns;  /* exchange r and c */
767             columns = i;
768         }
769
770         if (has_icons)
771         {
772             y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
773             rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
774         }
775
776         ysize   = rect.bottom / rows;
777         xsize   = rect.right  / columns;
778
779         for (x = i = 0, c = 1; c <= columns && *pWnd; c++)
780         {
781             if (c == columns)
782             {
783                 rows  = total - i;
784                 ysize = rect.bottom / rows;
785             }
786
787             y = 0;
788             for (r = 1; r <= rows && *pWnd; r++, i++)
789             {
790                 SetWindowPos(*pWnd, 0, x, y, xsize, ysize,
791                              SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
792                 y += ysize;
793                 pWnd++;
794             }
795             x += xsize;
796         }
797     }
798     HeapFree( GetProcessHeap(), 0, win_array );
799     if (has_icons) ArrangeIconicWindows( client );
800 }
801
802 /* ----------------------- Frame window ---------------------------- */
803
804
805 /**********************************************************************
806  *                                      MDI_AugmentFrameMenu
807  */
808 static BOOL MDI_AugmentFrameMenu( HWND frame, HWND hChild )
809 {
810     HMENU menu = GetMenu( frame );
811     HMENU       hSysPopup = 0;
812     HBITMAP hSysMenuBitmap = 0;
813     INT nItems;
814     UINT iId;
815
816     TRACE("frame %p,child %p\n",frame,hChild);
817
818     if( !menu ) return 0;
819
820     /* if the system buttons already exist do not add them again */
821     nItems = GetMenuItemCount(menu) - 1;
822     iId = GetMenuItemID(menu,nItems) ;
823     if (iId == SC_RESTORE || iId == SC_CLOSE)
824         return 0;
825
826     /* create a copy of sysmenu popup and insert it into frame menu bar */
827     if (!(hSysPopup = GetSystemMenu(hChild, FALSE)))
828         return 0;
829
830     AppendMenuA(menu,MF_HELP | MF_BITMAP,
831                    SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
832     AppendMenuA(menu,MF_HELP | MF_BITMAP,
833                    SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
834
835     /* The close button is only present in Win 95 look */
836     if(TWEAK_WineLook > WIN31_LOOK)
837     {
838         AppendMenuA(menu,MF_HELP | MF_BITMAP,
839                        SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
840     }
841
842   /* In Win 95 look, the system menu is replaced by the child icon */
843
844   if(TWEAK_WineLook > WIN31_LOOK)
845   {
846     HICON hIcon = (HICON)GetClassLongW(hChild, GCL_HICONSM);
847     if (!hIcon)
848         hIcon = (HICON)GetClassLongW(hChild, GCL_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   else
878     hSysMenuBitmap = hBmpClose;
879
880     if( !InsertMenuA(menu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
881                      (UINT_PTR)hSysPopup, (LPSTR)hSysMenuBitmap))
882     {
883         TRACE("not inserted\n");
884         DestroyMenu(hSysPopup);
885         return 0;
886     }
887
888     EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
889     EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
890     EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
891     SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
892
893     /* redraw menu */
894     DrawMenuBar(frame);
895
896     return 1;
897 }
898
899 /**********************************************************************
900  *                                      MDI_RestoreFrameMenu
901  */
902 static BOOL MDI_RestoreFrameMenu( HWND frame, HWND hChild )
903 {
904     MENUITEMINFOW menuInfo;
905     HMENU menu = GetMenu( frame );
906     INT nItems = GetMenuItemCount(menu) - 1;
907     UINT iId = GetMenuItemID(menu,nItems) ;
908
909     TRACE("frame %p,child %p,nIt=%d,iId=%d\n",frame,hChild,nItems,iId);
910
911     /* if there is no system buttons then nothing to do */
912     if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
913         return 0;
914
915     /*
916      * Remove the system menu, If that menu is the icon of the window
917      * as it is in win95, we have to delete the bitmap.
918      */
919     memset(&menuInfo, 0, sizeof(menuInfo));
920     menuInfo.cbSize = sizeof(menuInfo);
921     menuInfo.fMask  = MIIM_DATA | MIIM_TYPE;
922
923     GetMenuItemInfoW(menu,
924                      0,
925                      TRUE,
926                      &menuInfo);
927
928     RemoveMenu(menu,0,MF_BYPOSITION);
929
930     if ( (menuInfo.fType & MFT_BITMAP)           &&
931          (LOWORD(menuInfo.dwTypeData)!=0)        &&
932          (LOWORD(menuInfo.dwTypeData)!=HBITMAP_16(hBmpClose)) )
933     {
934         DeleteObject(HBITMAP_32(LOWORD(menuInfo.dwTypeData)));
935     }
936
937     if(TWEAK_WineLook > WIN31_LOOK)
938     {
939         /* close */
940         DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
941     }
942     /* restore */
943     DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
944     /* minimize */
945     DeleteMenu(menu,GetMenuItemCount(menu) - 1,MF_BYPOSITION);
946
947     DrawMenuBar(frame);
948
949     return 1;
950 }
951
952
953 /**********************************************************************
954  *                                      MDI_UpdateFrameText
955  *
956  * used when child window is maximized/restored
957  *
958  * Note: lpTitle can be NULL
959  */
960 static void MDI_UpdateFrameText( HWND frame, HWND hClient,
961                                  BOOL repaint, LPCWSTR lpTitle )
962 {
963     WCHAR   lpBuffer[MDI_MAXTITLELENGTH+1];
964     MDICLIENTINFO *ci = get_client_info( hClient );
965
966     TRACE("repaint %i, frameText %s\n", repaint, debugstr_w(lpTitle));
967
968     if (!ci) return;
969
970     if (!lpTitle && !ci->frameTitle)  /* first time around, get title from the frame window */
971     {
972         GetWindowTextW( frame, lpBuffer, sizeof(lpBuffer)/sizeof(WCHAR) );
973         lpTitle = lpBuffer;
974     }
975
976     /* store new "default" title if lpTitle is not NULL */
977     if (lpTitle)
978     {
979         if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
980         if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
981             strcpyW( ci->frameTitle, lpTitle );
982     }
983
984     if (ci->frameTitle)
985     {
986         if (IsZoomed(ci->hwndActiveChild) && IsWindowVisible(ci->hwndActiveChild))
987         {
988             /* combine frame title and child title if possible */
989
990             static const WCHAR lpBracket[]  = {' ','-',' ','[',0};
991             static const WCHAR lpBracket2[]  = {']',0};
992             int i_frame_text_length = strlenW(ci->frameTitle);
993
994             lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
995
996             if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
997             {
998                 strcatW( lpBuffer, lpBracket );
999                 if (GetWindowTextW( ci->hwndActiveChild, lpBuffer + i_frame_text_length + 4,
1000                                     MDI_MAXTITLELENGTH - i_frame_text_length - 5 ))
1001                     strcatW( lpBuffer, lpBracket2 );
1002                 else
1003                     lpBuffer[i_frame_text_length] = 0;  /* remove bracket */
1004             }
1005         }
1006         else
1007         {
1008             lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1009         }
1010     }
1011     else
1012         lpBuffer[0] = '\0';
1013
1014     DefWindowProcW( frame, WM_SETTEXT, 0, (LPARAM)lpBuffer );
1015     if( repaint )
1016         SetWindowPos( frame, 0,0,0,0,0, SWP_FRAMECHANGED |
1017                       SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1018 }
1019
1020
1021 /* ----------------------------- Interface ---------------------------- */
1022
1023
1024 /**********************************************************************
1025  *              MDIClientWndProc_common
1026  */
1027 static LRESULT MDIClientWndProc_common( HWND hwnd, UINT message,
1028                                         WPARAM wParam, LPARAM lParam, BOOL unicode )
1029 {
1030     MDICLIENTINFO *ci;
1031
1032     TRACE("%p %04x %08x %08lx\n", hwnd, message, wParam, lParam);
1033
1034     if (!(ci = get_client_info( hwnd ))) return 0;
1035
1036     switch (message)
1037     {
1038       case WM_CREATE:
1039       {
1040           /* Since we are using only cs->lpCreateParams, we can safely
1041            * cast to LPCREATESTRUCTA here */
1042           LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
1043           WND *wndPtr = WIN_GetPtr( hwnd );
1044
1045           wndPtr->flags |= WIN_ISMDICLIENT;
1046
1047         /* Translation layer doesn't know what's in the cs->lpCreateParams
1048          * so we have to keep track of what environment we're in. */
1049
1050         if( wndPtr->flags & WIN_ISWIN32 )
1051         {
1052             LPCLIENTCREATESTRUCT ccs = cs->lpCreateParams;
1053             ci->hWindowMenu     = ccs->hWindowMenu;
1054             ci->idFirstChild    = ccs->idFirstChild;
1055         }
1056         else
1057         {
1058             LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1059             ci->hWindowMenu     = HMENU_32(ccs->hWindowMenu);
1060             ci->idFirstChild    = ccs->idFirstChild;
1061         }
1062         WIN_ReleasePtr( wndPtr );
1063
1064         ci->child = NULL;
1065         ci->nActiveChildren     = 0;
1066         ci->nTotalCreated       = 0;
1067         ci->frameTitle          = NULL;
1068         ci->mdiFlags            = 0;
1069         ci->hFrameMenu = GetMenu(cs->hwndParent);
1070
1071         if (!hBmpClose) hBmpClose = CreateMDIMenuBitmap();
1072
1073         TRACE("Client created - hwnd = %p, idFirst = %u\n", hwnd, ci->idFirstChild );
1074         return 0;
1075       }
1076
1077       case WM_DESTROY:
1078       {
1079           if( IsZoomed(ci->hwndActiveChild) )
1080               MDI_RestoreFrameMenu(GetParent(hwnd), ci->hwndActiveChild);
1081
1082           ci->nActiveChildren = 0;
1083           MDI_RefreshMenu(ci);
1084
1085           if (ci->child) HeapFree( GetProcessHeap(), 0, ci->child );
1086           if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1087
1088           return 0;
1089       }
1090
1091       case WM_MDIACTIVATE:
1092       {
1093         HWND hwndActive;
1094
1095         hwndActive = ci->hwndActiveChild;
1096
1097         if( hwndActive != (HWND)wParam )
1098             SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1099         return 0;
1100       }
1101
1102       case WM_MDICASCADE:
1103         return MDICascade(hwnd, ci);
1104
1105       case WM_MDICREATE:
1106         if (lParam)
1107         {
1108             if (unicode)
1109             {
1110                 MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)lParam;
1111                 return (LRESULT)CreateWindowExW(WS_EX_MDICHILD, csW->szClass,
1112                                             csW->szTitle, csW->style,
1113                                             csW->x, csW->y, csW->cx, csW->cy,
1114                                             hwnd, 0, csW->hOwner,
1115                                             (LPVOID)csW->lParam);
1116             }
1117             else
1118             {
1119                 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lParam;
1120                 return (LRESULT)CreateWindowExA(WS_EX_MDICHILD, csA->szClass,
1121                                             csA->szTitle, csA->style,
1122                                             csA->x, csA->y, csA->cx, csA->cy,
1123                                             hwnd, 0, csA->hOwner,
1124                                             (LPVOID)csA->lParam);
1125             }
1126         }
1127         return 0;
1128
1129       case WM_MDIDESTROY:
1130           return MDIDestroyChild( hwnd, ci, WIN_GetFullHandle( (HWND)wParam ), TRUE );
1131
1132       case WM_MDIGETACTIVE:
1133           if (lParam) *(BOOL *)lParam = IsZoomed(ci->hwndActiveChild);
1134           return (LRESULT)ci->hwndActiveChild;
1135
1136       case WM_MDIICONARRANGE:
1137         ci->mdiFlags |= MDIF_NEEDUPDATE;
1138         ArrangeIconicWindows( hwnd );
1139         ci->sbRecalc = SB_BOTH+1;
1140         SendMessageW( hwnd, WM_MDICALCCHILDSCROLL, 0, 0 );
1141         return 0;
1142
1143       case WM_MDIMAXIMIZE:
1144         ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1145         return 0;
1146
1147       case WM_MDINEXT: /* lParam != 0 means previous window */
1148         MDI_SwitchActiveChild( hwnd, WIN_GetFullHandle( (HWND)wParam ), !lParam );
1149         break;
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                 ci->child[ci->nActiveChildren - 1] = (HWND)lParam;
1200             }
1201             break;
1202
1203         case WM_LBUTTONDOWN:
1204             {
1205             HWND child;
1206             POINT pt;
1207             pt.x = (short)LOWORD(lParam);
1208             pt.y = (short)HIWORD(lParam);
1209             child = ChildWindowFromPoint(hwnd, pt);
1210
1211             TRACE("notification from %p (%li,%li)\n",child,pt.x,pt.y);
1212
1213             if( child && child != hwnd && child != ci->hwndActiveChild )
1214                 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1215             break;
1216             }
1217         }
1218         return 0;
1219
1220       case WM_SIZE:
1221         if( IsWindow(ci->hwndActiveChild) && IsZoomed(ci->hwndActiveChild) &&
1222             IsWindowVisible(ci->hwndActiveChild) )
1223         {
1224             RECT        rect;
1225
1226             rect.left = 0;
1227             rect.top = 0;
1228             rect.right = LOWORD(lParam);
1229             rect.bottom = HIWORD(lParam);
1230
1231             AdjustWindowRectEx(&rect, GetWindowLongA(ci->hwndActiveChild, GWL_STYLE),
1232                                0, GetWindowLongA(ci->hwndActiveChild, GWL_EXSTYLE) );
1233             MoveWindow(ci->hwndActiveChild, rect.left, rect.top,
1234                          rect.right - rect.left, rect.bottom - rect.top, 1);
1235         }
1236         else
1237             MDI_PostUpdate(hwnd, ci, SB_BOTH+1);
1238
1239         break;
1240
1241       case WM_MDICALCCHILDSCROLL:
1242         if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1243         {
1244             CalcChildScroll(hwnd, ci->sbRecalc-1);
1245             ci->sbRecalc = 0;
1246             ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1247         }
1248         return 0;
1249     }
1250     return unicode ? DefWindowProcW( hwnd, message, wParam, lParam ) :
1251                      DefWindowProcA( hwnd, message, wParam, lParam );
1252 }
1253
1254 /***********************************************************************
1255  *              MDIClientWndProcA
1256  */
1257 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1258 {
1259     if (!IsWindow(hwnd)) return 0;
1260     return MDIClientWndProc_common( hwnd, message, wParam, lParam, FALSE );
1261 }
1262
1263 /***********************************************************************
1264  *              MDIClientWndProcW
1265  */
1266 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1267 {
1268     if (!IsWindow(hwnd)) return 0;
1269     return MDIClientWndProc_common( hwnd, message, wParam, lParam, TRUE );
1270 }
1271
1272 /***********************************************************************
1273  *              DefFrameProcA (USER32.@)
1274  */
1275 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1276                                 UINT message, WPARAM wParam, LPARAM lParam)
1277 {
1278     if (hwndMDIClient)
1279     {
1280         switch (message)
1281         {
1282         case WM_SETTEXT:
1283             {
1284                 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, NULL, 0 );
1285                 LPWSTR text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1286                 MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, text, len );
1287                 MDI_UpdateFrameText(hwnd, hwndMDIClient, TRUE, text );
1288                 HeapFree( GetProcessHeap(), 0, text );
1289             }
1290             return 1; /* success. FIXME: check text length */
1291
1292         case WM_COMMAND:
1293         case WM_NCACTIVATE:
1294         case WM_NEXTMENU:
1295         case WM_SETFOCUS:
1296         case WM_SIZE:
1297             return DefFrameProcW( hwnd, hwndMDIClient, message, wParam, lParam );
1298         }
1299     }
1300     return DefWindowProcA(hwnd, message, wParam, lParam);
1301 }
1302
1303
1304 /***********************************************************************
1305  *              DefFrameProcW (USER32.@)
1306  */
1307 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1308                                 UINT message, WPARAM wParam, LPARAM lParam)
1309 {
1310     MDICLIENTINFO *ci = get_client_info( hwndMDIClient );
1311
1312     TRACE("%p %p %04x %08x %08lx\n", hwnd, hwndMDIClient, message, wParam, lParam);
1313
1314     if (ci)
1315     {
1316         switch (message)
1317         {
1318         case WM_COMMAND:
1319             {
1320                 WORD id = LOWORD(wParam);
1321                 /* check for possible syscommands for maximized MDI child */
1322                 if (id <  ci->idFirstChild || id >= ci->idFirstChild + ci->nActiveChildren)
1323                 {
1324                     if( (id - 0xf000) & 0xf00f ) break;
1325                     if( !IsZoomed(ci->hwndActiveChild) ) break;
1326                     switch( id )
1327                     {
1328                     case SC_SIZE:
1329                     case SC_MOVE:
1330                     case SC_MINIMIZE:
1331                     case SC_MAXIMIZE:
1332                     case SC_NEXTWINDOW:
1333                     case SC_PREVWINDOW:
1334                     case SC_CLOSE:
1335                     case SC_RESTORE:
1336                         return SendMessageW( ci->hwndActiveChild, WM_SYSCOMMAND,
1337                                              wParam, lParam);
1338                     }
1339                 }
1340                 else
1341                 {
1342                     HWND childHwnd;
1343                     if (id - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1344                         /* User chose "More Windows..." */
1345                         childHwnd = MDI_MoreWindowsDialog(hwndMDIClient);
1346                     else
1347                         /* User chose one of the windows listed in the "Windows" menu */
1348                         childHwnd = MDI_GetChildByID(hwndMDIClient, id, ci);
1349
1350                     if( childHwnd )
1351                         SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1352                 }
1353             }
1354             break;
1355
1356         case WM_NCACTIVATE:
1357             SendMessageW(hwndMDIClient, message, wParam, lParam);
1358             break;
1359
1360         case WM_SETTEXT:
1361             MDI_UpdateFrameText(hwnd, hwndMDIClient, TRUE, (LPWSTR)lParam );
1362             return 1; /* success. FIXME: check text length */
1363
1364         case WM_SETFOCUS:
1365             SetFocus(hwndMDIClient);
1366             break;
1367
1368         case WM_SIZE:
1369             MoveWindow(hwndMDIClient, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
1370             break;
1371
1372         case WM_NEXTMENU:
1373             {
1374                 MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1375
1376                 if (!IsIconic(hwnd) && ci->hwndActiveChild && !IsZoomed(ci->hwndActiveChild))
1377                 {
1378                     /* control menu is between the frame system menu and
1379                      * the first entry of menu bar */
1380                     WND *wndPtr = WIN_GetPtr(hwnd);
1381
1382                     if( (wParam == VK_LEFT && GetMenu(hwnd) == next_menu->hmenuIn) ||
1383                         (wParam == VK_RIGHT && GetSubMenu(wndPtr->hSysMenu, 0) == next_menu->hmenuIn) )
1384                     {
1385                         WIN_ReleasePtr(wndPtr);
1386                         wndPtr = WIN_GetPtr(ci->hwndActiveChild);
1387                         next_menu->hmenuNext = GetSubMenu(wndPtr->hSysMenu, 0);
1388                         next_menu->hwndNext = ci->hwndActiveChild;
1389                     }
1390                     WIN_ReleasePtr(wndPtr);
1391                 }
1392                 return 0;
1393             }
1394         }
1395     }
1396
1397     return DefWindowProcW( hwnd, message, wParam, lParam );
1398 }
1399
1400 /***********************************************************************
1401  *              DefMDIChildProcA (USER32.@)
1402  */
1403 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1404                                    WPARAM wParam, LPARAM lParam )
1405 {
1406     HWND client = GetParent(hwnd);
1407     MDICLIENTINFO *ci = get_client_info( client );
1408
1409     TRACE("%p %04x %08x %08lx\n", hwnd, message, wParam, lParam);
1410
1411     hwnd = WIN_GetFullHandle( hwnd );
1412     if (!ci) return DefWindowProcA( hwnd, message, wParam, lParam );
1413
1414     switch (message)
1415     {
1416     case WM_SETTEXT:
1417         DefWindowProcA(hwnd, message, wParam, lParam);
1418         if( ci->hwndActiveChild == hwnd && IsZoomed(ci->hwndActiveChild) )
1419             MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1420         return 1; /* success. FIXME: check text length */
1421
1422     case WM_GETMINMAXINFO:
1423     case WM_MENUCHAR:
1424     case WM_CLOSE:
1425     case WM_SETFOCUS:
1426     case WM_CHILDACTIVATE:
1427     case WM_SYSCOMMAND:
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 %08x %08lx\n", hwnd, message, 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_SETVISIBLE:
1507         if (IsZoomed(ci->hwndActiveChild)) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1508         else MDI_PostUpdate(client, ci, SB_BOTH+1);
1509         break;
1510
1511     case WM_SIZE:
1512         if (wParam == SIZE_MAXIMIZED && !IsWindowVisible(hwnd))
1513             wParam = SIZE_RESTORED;
1514
1515         if( hwnd == ci->hwndActiveChild && wParam != SIZE_MAXIMIZED )
1516         {
1517             MDI_RestoreFrameMenu( GetParent(client), hwnd );
1518             MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1519         }
1520
1521         if( wParam == SIZE_MAXIMIZED )
1522         {
1523             TRACE("maximizing child %p\n", hwnd );
1524
1525             MDI_AugmentFrameMenu( GetParent(client), hwnd );
1526             MDI_UpdateFrameText( GetParent(client), client, TRUE, NULL );
1527         }
1528
1529         if( wParam == SIZE_MINIMIZED )
1530         {
1531             HWND switchTo = MDI_GetWindow(ci, hwnd, TRUE, WS_MINIMIZE);
1532
1533             if (switchTo) SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0);
1534         }
1535         MDI_PostUpdate(client, ci, SB_BOTH+1);
1536         break;
1537
1538     case WM_NEXTMENU:
1539         {
1540             MDINEXTMENU *next_menu = (MDINEXTMENU *)lParam;
1541             HWND parent = GetParent(client);
1542
1543             if( wParam == VK_LEFT )  /* switch to frame system menu */
1544             {
1545                 WND *wndPtr = WIN_GetPtr( parent );
1546                 next_menu->hmenuNext = GetSubMenu( wndPtr->hSysMenu, 0 );
1547                 WIN_ReleasePtr( wndPtr );
1548             }
1549             if( wParam == VK_RIGHT )  /* to frame menu bar */
1550             {
1551                 next_menu->hmenuNext = GetMenu(parent);
1552             }
1553             next_menu->hwndNext = parent;
1554             return 0;
1555         }
1556
1557     case WM_SYSCHAR:
1558         if (wParam == '-')
1559         {
1560             SendMessageW( hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (DWORD)VK_SPACE);
1561             return 0;
1562         }
1563         break;
1564     }
1565     return DefWindowProcW(hwnd, message, wParam, lParam);
1566 }
1567
1568 /**********************************************************************
1569  *              CreateMDIWindowA (USER32.@) Creates a MDI child
1570  *
1571  * RETURNS
1572  *    Success: Handle to created window
1573  *    Failure: NULL
1574  */
1575 HWND WINAPI CreateMDIWindowA(
1576     LPCSTR lpClassName,    /* [in] Pointer to registered child class name */
1577     LPCSTR lpWindowName,   /* [in] Pointer to window name */
1578     DWORD dwStyle,         /* [in] Window style */
1579     INT X,               /* [in] Horizontal position of window */
1580     INT Y,               /* [in] Vertical position of window */
1581     INT nWidth,          /* [in] Width of window */
1582     INT nHeight,         /* [in] Height of window */
1583     HWND hWndParent,     /* [in] Handle to parent window */
1584     HINSTANCE hInstance, /* [in] Handle to application instance */
1585     LPARAM lParam)         /* [in] Application-defined value */
1586 {
1587     TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1588           debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
1589           nWidth,nHeight,hWndParent,hInstance,lParam);
1590
1591     return CreateWindowExA(WS_EX_MDICHILD, lpClassName, lpWindowName,
1592                            dwStyle, X, Y, nWidth, nHeight, hWndParent,
1593                            0, hInstance, (LPVOID)lParam);
1594 }
1595
1596 /***********************************************************************
1597  *              CreateMDIWindowW (USER32.@) Creates a MDI child
1598  *
1599  * RETURNS
1600  *    Success: Handle to created window
1601  *    Failure: NULL
1602  */
1603 HWND WINAPI CreateMDIWindowW(
1604     LPCWSTR lpClassName,    /* [in] Pointer to registered child class name */
1605     LPCWSTR lpWindowName,   /* [in] Pointer to window name */
1606     DWORD dwStyle,         /* [in] Window style */
1607     INT X,               /* [in] Horizontal position of window */
1608     INT Y,               /* [in] Vertical position of window */
1609     INT nWidth,          /* [in] Width of window */
1610     INT nHeight,         /* [in] Height of window */
1611     HWND hWndParent,     /* [in] Handle to parent window */
1612     HINSTANCE hInstance, /* [in] Handle to application instance */
1613     LPARAM lParam)         /* [in] Application-defined value */
1614 {
1615     TRACE("(%s,%s,%08lx,%d,%d,%d,%d,%p,%p,%08lx)\n",
1616           debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
1617           nWidth, nHeight, hWndParent, hInstance, lParam);
1618
1619     return CreateWindowExW(WS_EX_MDICHILD, lpClassName, lpWindowName,
1620                            dwStyle, X, Y, nWidth, nHeight, hWndParent,
1621                            0, hInstance, (LPVOID)lParam);
1622 }
1623
1624 /**********************************************************************
1625  *              TranslateMDISysAccel (USER32.@)
1626  */
1627 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
1628 {
1629     if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
1630     {
1631         MDICLIENTINFO *ci = get_client_info( hwndClient );
1632         WPARAM wParam = 0;
1633
1634         if (!ci || !IsWindowEnabled(ci->hwndActiveChild)) return 0;
1635
1636         /* translate if the Ctrl key is down and Alt not. */
1637
1638         if( (GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
1639         {
1640             switch( msg->wParam )
1641             {
1642             case VK_F6:
1643             case VK_TAB:
1644                 wParam = ( GetKeyState(VK_SHIFT) & 0x8000 ) ? SC_NEXTWINDOW : SC_PREVWINDOW;
1645                 break;
1646             case VK_F4:
1647             case VK_RBUTTON:
1648                 wParam = SC_CLOSE;
1649                 break;
1650             default:
1651                 return 0;
1652             }
1653             TRACE("wParam = %04x\n", wParam);
1654             SendMessageW(ci->hwndActiveChild, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
1655             return 1;
1656         }
1657     }
1658     return 0; /* failure */
1659 }
1660
1661 /***********************************************************************
1662  *              CalcChildScroll (USER32.@)
1663  */
1664 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
1665 {
1666     SCROLLINFO info;
1667     RECT childRect, clientRect;
1668     HWND *list;
1669
1670     GetClientRect( hwnd, &clientRect );
1671     SetRectEmpty( &childRect );
1672
1673     if ((list = WIN_ListChildren( hwnd )))
1674     {
1675         int i;
1676         for (i = 0; list[i]; i++)
1677         {
1678             DWORD style = GetWindowLongW( list[i], GWL_STYLE );
1679             if (style & WS_MAXIMIZE)
1680             {
1681                 HeapFree( GetProcessHeap(), 0, list );
1682                 ShowScrollBar( hwnd, SB_BOTH, FALSE );
1683                 return;
1684             }
1685             if (style & WS_VISIBLE)
1686             {
1687                 WND *pWnd = WIN_FindWndPtr( list[i] );
1688                 UnionRect( &childRect, &pWnd->rectWindow, &childRect );
1689                 WIN_ReleaseWndPtr( pWnd );
1690             }
1691         }
1692         HeapFree( GetProcessHeap(), 0, list );
1693     }
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, scroll, &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, scroll, &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                                           (LPDLGTEMPLATEA) template,
1937                                           hwnd, MDI_MoreWindowsDlgProc, (LPARAM) hwnd);
1938 }