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