Added mappings for a few messages.
[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  * Notes: Fairly complete implementation.
10  *        Also, Excel and WinWord do _not_ use MDI so if you're trying
11  *        to fix them look elsewhere. 
12  *
13  * Notes on how the "More Windows..." is implemented:
14  *
15  *      When we have more than 9 opened windows, a "More Windows..."
16  *      option appears in the "Windows" menu. Each child window has
17  *      a WND* associated with it, accesible via the children list of
18  *      the parent window. This WND* has a wIDmenu member, which reflects
19  *      the position of the child in the window list. For example, with
20  *      9 child windows, we could have the following pattern:
21  *
22  *
23  *
24  *                Name of the child window    pWndChild->wIDmenu
25  *                     Doc1                       5000
26  *                     Doc2                       5001
27  *                     Doc3                       5002
28  *                     Doc4                       5003
29  *                     Doc5                       5004
30  *                     Doc6                       5005
31  *                     Doc7                       5006
32  *                     Doc8                       5007
33  *                     Doc9                       5008
34  *
35  *
36  *       The "Windows" menu, as the "More windows..." dialog, are constructed
37  *       in this order. If we add a child, we would have the following list:
38  *
39  *
40  *               Name of the child window    pWndChild->wIDmenu
41  *                     Doc1                       5000
42  *                     Doc2                       5001
43  *                     Doc3                       5002
44  *                     Doc4                       5003
45  *                     Doc5                       5004
46  *                     Doc6                       5005
47  *                     Doc7                       5006
48  *                     Doc8                       5007
49  *                     Doc9                       5008
50  *                     Doc10                      5009
51  *
52  *       But only 5000 to 5008 would be displayed in the "Windows" menu. We want
53  *       the last created child to be in the menu, so we swap the last child with
54  *       the 9th... Doc9 will be accessible via the "More Windows..." option.
55  *
56  *                     Doc1                       5000
57  *                     Doc2                       5001
58  *                     Doc3                       5002
59  *                     Doc4                       5003
60  *                     Doc5                       5004
61  *                     Doc6                       5005
62  *                     Doc7                       5006
63  *                     Doc8                       5007
64  *                     Doc9                       5009
65  *                     Doc10                      5008
66  *
67  */
68
69 #include <stdlib.h>
70 #include <stdio.h>
71 #include <string.h>
72 #include <math.h>
73
74 #include "windef.h"
75 #include "winbase.h"
76 #include "wingdi.h"
77 #include "winuser.h"
78 #include "wine/unicode.h"
79 #include "win.h"
80 #include "heap.h"
81 #include "nonclient.h"
82 #include "controls.h"
83 #include "user.h"
84 #include "struct32.h"
85 #include "debugtools.h"
86 #include "dlgs.h"
87
88 DEFAULT_DEBUG_CHANNEL(mdi);
89
90 #define MDI_MAXLISTLENGTH       0x40
91 #define MDI_MAXTITLELENGTH      0xa1
92
93 #define MDI_NOFRAMEREPAINT      0
94 #define MDI_REPAINTFRAMENOW     1
95 #define MDI_REPAINTFRAME        2
96
97 #define WM_MDICALCCHILDSCROLL   0x10ac /* this is exactly what Windows uses */
98
99 /* "More Windows..." definitions */
100 #define MDI_MOREWINDOWSLIMIT    9       /* after this number of windows, a "More Windows..." 
101                                            option will appear under the Windows menu */
102 #define MDI_IDC_LISTBOX         100
103 #define MDI_IDS_MOREWINDOWS     13
104
105 #define MDIF_NEEDUPDATE         0x0001
106
107 typedef struct
108 {
109     UINT      nActiveChildren;
110     HWND      hwndChildMaximized;
111     HWND      hwndActiveChild;
112     HMENU     hWindowMenu;
113     UINT      idFirstChild;
114     LPWSTR    frameTitle;
115     UINT      nTotalCreated;
116     UINT      mdiFlags;
117     UINT      sbRecalc;   /* SB_xxx flags for scrollbar fixup */
118     HWND      self;
119 } MDICLIENTINFO;
120
121 static HBITMAP hBmpClose   = 0;
122 static HBITMAP hBmpRestore = 0;
123
124 /* ----------------- declarations ----------------- */
125 static void MDI_UpdateFrameText(WND *, HWND, BOOL, LPCWSTR);
126 static BOOL MDI_AugmentFrameMenu(WND *, HWND);
127 static BOOL MDI_RestoreFrameMenu(WND *, HWND);
128
129 static LONG MDI_ChildActivate( WND*, HWND );
130
131 static HWND MDI_MoreWindowsDialog(WND*);
132 static void MDI_SwapMenuItems(WND *, UINT, UINT);
133 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
134 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
135
136 /* -------- Miscellaneous service functions ----------
137  *
138  *                      MDI_GetChildByID
139  */
140 static HWND MDI_GetChildByID(WND* wndPtr, UINT id)
141 {
142     for (wndPtr = wndPtr->child; wndPtr; wndPtr = wndPtr->next)
143         if (wndPtr->wIDmenu == id) return wndPtr->hwndSelf;
144     return 0;
145 }
146
147 static void MDI_PostUpdate(HWND hwnd, MDICLIENTINFO* ci, WORD recalc)
148 {
149     if( !(ci->mdiFlags & MDIF_NEEDUPDATE) )
150     {
151         ci->mdiFlags |= MDIF_NEEDUPDATE;
152         PostMessageA( hwnd, WM_MDICALCCHILDSCROLL, 0, 0);
153     }
154     ci->sbRecalc = recalc;
155 }
156
157
158 /*********************************************************************
159  * MDIClient class descriptor
160  */
161 const struct builtin_class_descr MDICLIENT_builtin_class =
162 {
163     "MDIClient",            /* name */
164     CS_GLOBALCLASS,         /* style */
165     MDIClientWndProcA,      /* procA */
166     MDIClientWndProcW,      /* procW */
167     sizeof(MDICLIENTINFO),  /* extra */
168     IDC_ARROWA,             /* cursor */
169     COLOR_APPWORKSPACE+1    /* brush */
170 };
171
172
173 /**********************************************************************
174  *                      MDI_MenuModifyItem
175  */
176 static BOOL MDI_MenuModifyItem(WND* clientWnd, HWND hWndChild )
177 {
178     WCHAR           buffer[128];
179     static const WCHAR format[] = {'%','d',' ',0};
180     MDICLIENTINFO  *clientInfo = (MDICLIENTINFO*)clientWnd->wExtra;
181     WND            *wndPtr     = WIN_FindWndPtr(hWndChild);
182     UINT            n          = wsprintfW(buffer, format,
183                                            wndPtr->wIDmenu - clientInfo->idFirstChild + 1);
184     BOOL            bRet            = 0;
185
186     if( !clientInfo->hWindowMenu )
187     {
188         bRet =  FALSE;
189         goto END;
190     }
191
192     if (wndPtr->text) lstrcpynW(buffer + n, wndPtr->text, sizeof(buffer)/sizeof(WCHAR) - n );
193
194     n    = GetMenuState(clientInfo->hWindowMenu,wndPtr->wIDmenu ,MF_BYCOMMAND); 
195     bRet = ModifyMenuW(clientInfo->hWindowMenu , wndPtr->wIDmenu, 
196                       MF_BYCOMMAND | MF_STRING, wndPtr->wIDmenu, buffer );
197     CheckMenuItem(clientInfo->hWindowMenu ,wndPtr->wIDmenu , n & MF_CHECKED);
198 END:
199     WIN_ReleaseWndPtr(wndPtr);
200     return bRet;
201 }
202
203 /**********************************************************************
204  *                      MDI_MenuDeleteItem
205  */
206 static BOOL MDI_MenuDeleteItem(WND* clientWnd, HWND hWndChild )
207 {
208     WCHAR        buffer[128];
209     static const WCHAR format[] = {'&','%','d',' ',0};
210     MDICLIENTINFO *clientInfo = (MDICLIENTINFO*)clientWnd->wExtra;
211     WND         *wndPtr     = WIN_FindWndPtr(hWndChild);
212     UINT         index      = 0,id,n;
213     BOOL         retvalue;
214
215     if( !clientInfo->nActiveChildren ||
216         !clientInfo->hWindowMenu )
217     {
218         retvalue = FALSE;
219         goto END;
220     }
221
222     id = wndPtr->wIDmenu;
223     DeleteMenu(clientInfo->hWindowMenu,id,MF_BYCOMMAND);
224
225  /* walk the rest of MDI children to prevent gaps in the id 
226   * sequence and in the menu child list */
227
228     for( index = id+1; index <= clientInfo->nActiveChildren + 
229                                 clientInfo->idFirstChild; index++ )
230     {
231         WND *tmpWnd = WIN_FindWndPtr(MDI_GetChildByID(clientWnd,index));
232         if( !tmpWnd )
233         {
234               TRACE("no window for id=%i\n",index);
235             WIN_ReleaseWndPtr(tmpWnd);
236               continue;
237         }
238     
239         /* set correct id */
240         tmpWnd->wIDmenu--;
241
242         n = wsprintfW(buffer, format ,index - clientInfo->idFirstChild);
243         if (tmpWnd->text)
244             lstrcpynW(buffer + n, tmpWnd->text, sizeof(buffer)/sizeof(WCHAR) - n );     
245
246         /*  change menu if the current child is to be shown in the 
247          *  "Windows" menu
248          */
249         if (index <= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT) 
250         ModifyMenuW(clientInfo->hWindowMenu ,index ,MF_BYCOMMAND | MF_STRING,
251                       index - 1 , buffer ); 
252         WIN_ReleaseWndPtr(tmpWnd);
253     }
254
255     /*  We must restore the "More Windows..." option if there is enough child 
256      */
257     if (clientInfo->nActiveChildren - 1 > MDI_MOREWINDOWSLIMIT)
258     {
259         WCHAR szTmp[50];
260         LoadStringW(GetModuleHandleA("USER32"), MDI_IDS_MOREWINDOWS, szTmp, sizeof(szTmp)/sizeof(szTmp[0]));
261         AppendMenuW(clientInfo->hWindowMenu, MF_STRING, clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT, szTmp);
262     }
263     retvalue = TRUE;
264 END:
265     WIN_ReleaseWndPtr(wndPtr);
266     return retvalue;
267 }
268
269 /**********************************************************************
270  *                      MDI_GetWindow
271  *
272  * returns "activateable" child different from the current or zero
273  */
274 static HWND MDI_GetWindow(WND *clientWnd, HWND hWnd, BOOL bNext,
275                             DWORD dwStyleMask )
276 {
277     MDICLIENTINFO *clientInfo = (MDICLIENTINFO*)clientWnd->wExtra;
278     WND *wndPtr, *pWnd, *pWndLast = NULL;
279     
280     dwStyleMask |= WS_DISABLED | WS_VISIBLE;
281     if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
282
283     if( !(wndPtr = WIN_FindWndPtr(hWnd)) ) return 0;
284
285     for ( pWnd = WIN_LockWndPtr(wndPtr->next); ; WIN_UpdateWndPtr(&pWnd,pWnd->next))
286     {
287         if (!pWnd ) WIN_UpdateWndPtr(&pWnd,wndPtr->parent->child);
288
289         if ( pWnd == wndPtr ) break; /* went full circle */
290
291         if (!pWnd->owner && (pWnd->dwStyle & dwStyleMask) == WS_VISIBLE )
292         {
293             pWndLast = pWnd;
294             if ( bNext ) break;
295         }
296     }
297     WIN_ReleaseWndPtr(wndPtr);
298     WIN_ReleaseWndPtr(pWnd);
299     return pWndLast ? pWndLast->hwndSelf : 0;
300 }
301
302 /**********************************************************************
303  *                      MDI_CalcDefaultChildPos
304  *
305  *  It seems that the default height is about 2/3 of the client rect
306  */
307 static void MDI_CalcDefaultChildPos( WND* w, WORD n, LPPOINT lpPos,
308                                      INT delta)
309 {
310     INT  nstagger;
311     RECT rect = w->rectClient;
312     INT  spacing = GetSystemMetrics(SM_CYCAPTION) +
313                      GetSystemMetrics(SM_CYFRAME) - 1; 
314
315     if( rect.bottom - rect.top - delta >= spacing ) 
316         rect.bottom -= delta;
317
318     nstagger = (rect.bottom - rect.top)/(3 * spacing);
319     lpPos[1].x = (rect.right - rect.left - nstagger * spacing);
320     lpPos[1].y = (rect.bottom - rect.top - nstagger * spacing);
321     lpPos[0].x = lpPos[0].y = spacing * (n%(nstagger+1));
322 }
323
324 /**********************************************************************
325  *            MDISetMenu
326  */
327 static LRESULT MDISetMenu( HWND hwnd, HMENU hmenuFrame,
328                            HMENU hmenuWindow)
329 {
330     WND *w;
331     MDICLIENTINFO *ci;
332     HWND hwndFrame = GetParent(hwnd);
333     HMENU oldFrameMenu = GetMenu(hwndFrame);
334
335     TRACE("%04x %04x %04x\n",
336                 hwnd, hmenuFrame, hmenuWindow);
337
338     if (hmenuFrame && !IsMenu(hmenuFrame))
339     {
340         WARN("hmenuFrame is not a menu handle\n");
341         return 0L;
342     }
343         
344     if (hmenuWindow && !IsMenu(hmenuWindow))
345     {
346         WARN("hmenuWindow is not a menu handle\n");
347         return 0L;
348     }
349         
350     w = WIN_FindWndPtr(hwnd);
351     ci = (MDICLIENTINFO *) w->wExtra;
352
353     if( ci->hwndChildMaximized && hmenuFrame && hmenuFrame!=oldFrameMenu )
354         MDI_RestoreFrameMenu(w->parent, ci->hwndChildMaximized );
355
356     if( hmenuWindow && ci->hWindowMenu && hmenuWindow!=ci->hWindowMenu )
357     {
358         /* delete menu items from ci->hWindowMenu 
359          * and add them to hmenuWindow */
360
361         INT i = GetMenuItemCount(ci->hWindowMenu) - 1;
362         INT pos = GetMenuItemCount(hmenuWindow) + 1;
363
364         AppendMenuA( hmenuWindow, MF_SEPARATOR, 0, NULL);
365
366         if( ci->nActiveChildren )
367         {
368             INT j;
369             LPWSTR buffer = NULL;
370             MENUITEMINFOW mii;
371             INT nbWindowsMenuItems; /* num of documents shown + "More Windows..." if present */
372
373             if (ci->nActiveChildren <= MDI_MOREWINDOWSLIMIT)
374                 nbWindowsMenuItems = ci->nActiveChildren;
375             else
376                 nbWindowsMenuItems = MDI_MOREWINDOWSLIMIT + 1;
377
378             j = i - nbWindowsMenuItems + 1;
379
380             for( ; i >= j ; i-- )
381             {
382                 memset(&mii, 0, sizeof(mii));
383                 mii.cbSize = sizeof(mii);
384                 mii.fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_ID | MIIM_STATE
385                   | MIIM_SUBMENU | MIIM_TYPE | MIIM_BITMAP;
386
387                 GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
388                 if(mii.cch) { /* Menu is MFT_STRING */
389                     mii.cch++; /* add room for '\0' */
390                     buffer = HeapAlloc(GetProcessHeap(), 0,
391                                        mii.cch * sizeof(WCHAR));
392                     mii.dwTypeData = buffer;
393                     GetMenuItemInfoW(ci->hWindowMenu, i, TRUE, &mii);
394                 }
395                 DeleteMenu(ci->hWindowMenu, i, MF_BYPOSITION);
396                 InsertMenuItemW(hmenuWindow, pos, TRUE, &mii);
397                 if(buffer) {
398                     HeapFree(GetProcessHeap(), 0, buffer);
399                     buffer = NULL;
400                 }
401             }
402         }
403
404         /* remove separator */
405         DeleteMenu(ci->hWindowMenu, i, MF_BYPOSITION); 
406
407         ci->hWindowMenu = hmenuWindow;
408     } 
409
410     if (hmenuFrame)
411     {
412         SetMenu(hwndFrame, hmenuFrame);
413         if( hmenuFrame!=oldFrameMenu )
414         {
415             if( ci->hwndChildMaximized )
416                 MDI_AugmentFrameMenu( w->parent, ci->hwndChildMaximized );
417             WIN_ReleaseWndPtr(w);
418             return oldFrameMenu;
419         }
420     }
421     else
422     {
423         INT nItems = GetMenuItemCount(w->parent->wIDmenu) - 1;
424         UINT iId = GetMenuItemID(w->parent->wIDmenu,nItems) ;
425
426         if( !(iId == SC_RESTORE || iId == SC_CLOSE) )
427         {
428             /* SetMenu() may already have been called, meaning that this window
429              * already has its menu. But they may have done a SetMenu() on
430              * an MDI window, and called MDISetMenu() after the fact, meaning
431              * that the "if" to this "else" wouldn't catch the need to
432              * augment the frame menu.
433              */
434             if( ci->hwndChildMaximized )
435                 MDI_AugmentFrameMenu( w->parent, ci->hwndChildMaximized );
436         }
437     }
438     WIN_ReleaseWndPtr(w);
439     return 0;
440 }
441
442 /**********************************************************************
443  *            MDIRefreshMenu
444  */
445 static LRESULT MDIRefreshMenu( HWND hwnd, HMENU hmenuFrame,
446                            HMENU hmenuWindow)
447 {
448     HWND hwndFrame = GetParent(hwnd);
449     HMENU oldFrameMenu = GetMenu(hwndFrame);
450
451     TRACE("%04x %04x %04x\n",
452                 hwnd, hmenuFrame, hmenuWindow);
453
454     FIXME("partially function stub\n");
455
456     return oldFrameMenu;
457 }
458
459
460 /* ------------------ MDI child window functions ---------------------- */
461
462
463 /**********************************************************************
464  *                                      MDICreateChild
465  */
466 static HWND MDICreateChild( WND *wndParent, MDICLIENTINFO *ci,
467                             LPMDICREATESTRUCTA cs, BOOL unicode )
468 {
469     POINT          pos[2]; 
470     DWORD            style = cs->style | (WS_CHILD | WS_CLIPSIBLINGS);
471     HWND             hwnd, hwndMax = 0;
472     WORD             wIDmenu = ci->idFirstChild + ci->nActiveChildren;
473     static const WCHAR lpstrDef[] = {'j','u','n','k','!',0};
474
475     TRACE("origin %i,%i - dim %i,%i, style %08lx\n", 
476                 cs->x, cs->y, cs->cx, cs->cy, cs->style);
477     /* calculate placement */
478     MDI_CalcDefaultChildPos(wndParent, ci->nTotalCreated++, pos, 0);
479
480     if (cs->cx == CW_USEDEFAULT || !cs->cx) cs->cx = pos[1].x;
481     if (cs->cy == CW_USEDEFAULT || !cs->cy) cs->cy = pos[1].y;
482
483     if( cs->x == CW_USEDEFAULT )
484     {
485         cs->x = pos[0].x;
486         cs->y = pos[0].y;
487     }
488
489     /* restore current maximized child */
490     if( (style & WS_VISIBLE) && ci->hwndChildMaximized )
491     {
492         TRACE("Restoring current maximized child %04x\n", ci->hwndChildMaximized);
493         if( style & WS_MAXIMIZE )
494             SendMessageW(wndParent->hwndSelf, WM_SETREDRAW, FALSE, 0L);
495         hwndMax = ci->hwndChildMaximized;
496         ShowWindow( hwndMax, SW_SHOWNOACTIVATE );
497         if( style & WS_MAXIMIZE )
498             SendMessageW(wndParent->hwndSelf, WM_SETREDRAW, TRUE, 0L);
499     }
500
501     if (ci->nActiveChildren <= MDI_MOREWINDOWSLIMIT)
502     /* this menu is needed to set a check mark in MDI_ChildActivate */
503     if (ci->hWindowMenu != 0)
504         AppendMenuW(ci->hWindowMenu, MF_STRING, wIDmenu, lpstrDef);
505
506     ci->nActiveChildren++;
507
508     /* fix window style */
509     if( !(wndParent->dwStyle & MDIS_ALLCHILDSTYLES) )
510     {
511         TRACE("MDIS_ALLCHILDSTYLES is missing, fixing window style\n");
512         style &= (WS_CHILD | WS_CLIPSIBLINGS | WS_MINIMIZE | WS_MAXIMIZE |
513                   WS_CLIPCHILDREN | WS_DISABLED | WS_VSCROLL | WS_HSCROLL );
514         style |= (WS_VISIBLE | WS_OVERLAPPEDWINDOW);
515     }
516
517     if( wndParent->flags & WIN_ISWIN32 )
518     {
519         if(unicode)
520         {
521             MDICREATESTRUCTW *csW = (MDICREATESTRUCTW *)cs;
522             hwnd = CreateWindowW( csW->szClass, csW->szTitle, style, 
523                                 csW->x, csW->y, csW->cx, csW->cy, wndParent->hwndSelf,
524                                 (HMENU)wIDmenu, csW->hOwner, csW );
525         }
526         else
527             hwnd = CreateWindowA( cs->szClass, cs->szTitle, style, 
528                                 cs->x, cs->y, cs->cx, cs->cy, wndParent->hwndSelf,
529                                 (HMENU)wIDmenu, cs->hOwner, cs );
530     }
531     else
532     {
533         MDICREATESTRUCT16 *cs16;
534         LPSTR title, cls;
535
536         cs16 = SEGPTR_NEW(MDICREATESTRUCT16);
537         STRUCT32_MDICREATESTRUCT32Ato16( cs, cs16 );
538         title = SEGPTR_STRDUP( cs->szTitle );
539         cls   = SEGPTR_STRDUP( cs->szClass );
540         cs16->szTitle = SEGPTR_GET(title);
541         cs16->szClass = SEGPTR_GET(cls);
542
543         hwnd = CreateWindow16( cs->szClass, cs->szTitle, style, 
544                                cs16->x, cs16->y, cs16->cx, cs16->cy, wndParent->hwndSelf, 
545                                (HMENU)wIDmenu, cs16->hOwner,
546                                (LPVOID)SEGPTR_GET(cs16) );
547         SEGPTR_FREE( title );
548         SEGPTR_FREE( cls );
549         SEGPTR_FREE( cs16 );
550     }
551
552     /* MDI windows are WS_CHILD so they won't be activated by CreateWindow */
553
554     if (hwnd)
555     {
556         WND* wnd = WIN_FindWndPtr( hwnd );
557
558         /* All MDI child windows have the WS_EX_MDICHILD style */
559         wnd->dwExStyle |= WS_EX_MDICHILD;
560
561         /*  If we have more than 9 windows, we must insert the new one at the
562          *  9th position in order to see it in the "Windows" menu
563          */
564         if (ci->nActiveChildren > MDI_MOREWINDOWSLIMIT)
565             MDI_SwapMenuItems(wnd->parent, wnd->wIDmenu, ci->idFirstChild + MDI_MOREWINDOWSLIMIT - 1);
566
567         MDI_MenuModifyItem(wndParent, hwnd);
568
569         /* Have we hit the "More Windows..." limit? If so, we must 
570          * add a "More Windows..." option 
571          */
572         if (ci->nActiveChildren == MDI_MOREWINDOWSLIMIT + 1)
573         {
574             WCHAR szTmp[50];
575             LoadStringW(GetModuleHandleA("USER32"), MDI_IDS_MOREWINDOWS, szTmp, sizeof(szTmp)/sizeof(szTmp[0]));
576
577             ModifyMenuW(ci->hWindowMenu,
578                         ci->idFirstChild + MDI_MOREWINDOWSLIMIT, 
579                         MF_BYCOMMAND | MF_STRING, 
580                         ci->idFirstChild + MDI_MOREWINDOWSLIMIT, 
581                         szTmp);
582         }
583         
584         if( (wnd->dwStyle & WS_MINIMIZE) && ci->hwndActiveChild )
585         {
586             TRACE("Minimizing created MDI child %04x\n", hwnd);
587             ShowWindow( hwnd, SW_SHOWMINNOACTIVE );
588         }
589         else
590         {
591             /* WS_VISIBLE is clear if a) the MDI client has
592              * MDIS_ALLCHILDSTYLES style and 2) the flag is cleared in the
593              * MDICreateStruct. If so the created window is not shown nor 
594              * activated.
595              */
596             if(wnd->dwStyle & WS_VISIBLE)
597                 ShowWindow(hwnd, SW_SHOW);
598         }
599         WIN_ReleaseWndPtr(wnd);
600         TRACE("created child - %04x\n",hwnd);
601     }
602     else
603     {
604         ci->nActiveChildren--;
605         DeleteMenu(ci->hWindowMenu,wIDmenu,MF_BYCOMMAND);
606         if( IsWindow(hwndMax) )
607             ShowWindow(hwndMax, SW_SHOWMAXIMIZED);
608     }
609         
610     return hwnd;
611 }
612
613 /**********************************************************************
614  *                      MDI_ChildGetMinMaxInfo
615  *
616  * Note: The rule here is that client rect of the maximized MDI child 
617  *       is equal to the client rect of the MDI client window.
618  */
619 static void MDI_ChildGetMinMaxInfo( WND* clientWnd, HWND hwnd,
620                                     MINMAXINFO* lpMinMax )
621 {
622     WND*        childWnd = WIN_FindWndPtr(hwnd);
623     RECT        rect     = clientWnd->rectClient;
624
625     MapWindowPoints( clientWnd->parent->hwndSelf, 
626                      ((MDICLIENTINFO*)clientWnd->wExtra)->self, (LPPOINT)&rect, 2);
627     AdjustWindowRectEx( &rect, childWnd->dwStyle, 0, childWnd->dwExStyle );
628
629     lpMinMax->ptMaxSize.x = rect.right -= rect.left;
630     lpMinMax->ptMaxSize.y = rect.bottom -= rect.top;
631
632     lpMinMax->ptMaxPosition.x = rect.left;
633     lpMinMax->ptMaxPosition.y = rect.top; 
634
635     WIN_ReleaseWndPtr(childWnd);
636     
637     TRACE("max rect (%i,%i - %i, %i)\n", 
638                         rect.left,rect.top,rect.right,rect.bottom);
639     
640 }
641
642 /**********************************************************************
643  *                      MDI_SwitchActiveChild
644  * 
645  * Note: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
646  *       being activated 
647  */
648 static void MDI_SwitchActiveChild( HWND clientHwnd, HWND childHwnd,
649                                    BOOL bNextWindow )
650 {
651     WND           *w         = WIN_FindWndPtr(clientHwnd);
652     HWND           hwndTo    = 0;
653     HWND           hwndPrev  = 0;
654     MDICLIENTINFO *ci;
655
656     hwndTo = MDI_GetWindow(w, childHwnd, bNextWindow, 0);
657  
658     ci = (MDICLIENTINFO *) w->wExtra;
659
660     TRACE("from %04x, to %04x\n",childHwnd,hwndTo);
661
662     if ( !hwndTo ) goto END; /* no window to switch to */
663
664     hwndPrev = ci->hwndActiveChild;
665
666     if ( hwndTo != hwndPrev )
667     {
668         BOOL bOptimize = 0;
669
670         if( ci->hwndChildMaximized )
671         {
672             bOptimize = 1; 
673             w->dwStyle &= ~WS_VISIBLE;
674         }
675
676         SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0, 
677                         SWP_NOMOVE | SWP_NOSIZE );
678
679         if( bNextWindow && hwndPrev )
680             SetWindowPos( hwndPrev, HWND_BOTTOM, 0, 0, 0, 0, 
681                             SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE );
682         if( bOptimize )
683             ShowWindow( clientHwnd, SW_SHOW );
684     }
685 END:
686     WIN_ReleaseWndPtr(w);
687 }
688
689             
690 /**********************************************************************
691  *                                      MDIDestroyChild
692  */
693 static LRESULT MDIDestroyChild( WND *w_parent, MDICLIENTINFO *ci,
694                                 HWND child, BOOL flagDestroy )
695 {
696     WND         *childPtr = WIN_FindWndPtr(child);
697
698     if( childPtr )
699     {
700         if( child == ci->hwndActiveChild )
701         {
702             MDI_SwitchActiveChild(w_parent->hwndSelf, child, TRUE);
703
704             if( child == ci->hwndActiveChild )
705             {
706                 ShowWindow( child, SW_HIDE);
707                 if( child == ci->hwndChildMaximized )
708                 {
709                     MDI_RestoreFrameMenu(w_parent->parent, child);
710                     ci->hwndChildMaximized = 0;
711                     MDI_UpdateFrameText(w_parent->parent, w_parent->hwndSelf, TRUE, NULL);
712                 }
713
714                 MDI_ChildActivate(w_parent, 0);
715             }
716         }
717
718         MDI_MenuDeleteItem(w_parent, child);
719
720         WIN_ReleaseWndPtr(childPtr);
721         
722         ci->nActiveChildren--;
723
724         TRACE("child destroyed - %04x\n",child);
725
726         if (flagDestroy)
727         {
728             MDI_PostUpdate(GetParent(child), ci, SB_BOTH+1);
729             DestroyWindow(child);
730         }
731     }
732
733     return 0;
734 }
735
736
737 /**********************************************************************
738  *                                      MDI_ChildActivate
739  *
740  * Note: hWndChild is NULL when last child is being destroyed
741  */
742 static LONG MDI_ChildActivate( WND *clientPtr, HWND hWndChild )
743 {
744     MDICLIENTINFO       *clientInfo = (MDICLIENTINFO*)clientPtr->wExtra; 
745     HWND               prevActiveWnd = clientInfo->hwndActiveChild;
746     WND                 *wndPtr = WIN_FindWndPtr( hWndChild );
747     WND                 *wndPrev = WIN_FindWndPtr( prevActiveWnd );
748     BOOL                 isActiveFrameWnd = 0;   
749     LONG               retvalue;
750
751     if( wndPtr )
752     {
753         if( wndPtr->dwStyle & WS_DISABLED )
754         {
755             retvalue = 0L;
756             goto END;
757         }
758     }
759
760     /* Don't activate if it is already active. Might happen 
761        since ShowWindow DOES activate MDI children */
762     if (clientInfo->hwndActiveChild == hWndChild)
763     {
764       retvalue = 0L;
765       goto END;
766     }
767
768     TRACE("%04x\n", hWndChild);
769
770     if( GetActiveWindow() == clientPtr->parent->hwndSelf )
771         isActiveFrameWnd = TRUE;
772         
773     /* deactivate prev. active child */
774     if( wndPrev )
775     {
776         wndPrev->dwStyle |= WS_SYSMENU;
777         SendMessageA( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
778         SendMessageA( prevActiveWnd, WM_MDIACTIVATE, (WPARAM)prevActiveWnd,
779                         (LPARAM)hWndChild);
780         /* uncheck menu item */
781         if( clientInfo->hWindowMenu )
782         {
783             WORD wPrevID = wndPrev->wIDmenu - clientInfo->idFirstChild;
784
785             if (wPrevID < MDI_MOREWINDOWSLIMIT)
786                 CheckMenuItem( clientInfo->hWindowMenu,
787                                  wndPrev->wIDmenu, 0);
788             else
789                 CheckMenuItem( clientInfo->hWindowMenu,
790                                clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1, 0);
791     }
792     }
793
794     /* set appearance */
795     if( clientInfo->hwndChildMaximized )
796     {
797       if( clientInfo->hwndChildMaximized != hWndChild ) {
798         if( hWndChild ) {
799                   clientInfo->hwndActiveChild = hWndChild;
800                   ShowWindow( hWndChild, SW_SHOWMAXIMIZED);
801         } else
802                 ShowWindow( clientInfo->hwndActiveChild, SW_SHOWNORMAL );
803       }
804     }
805
806     clientInfo->hwndActiveChild = hWndChild;
807
808     /* check if we have any children left */
809     if( !hWndChild )
810     {
811         if( isActiveFrameWnd )
812             SetFocus( clientInfo->self );
813         retvalue = 0;
814         goto END;
815     }
816         
817     /* check menu item */
818     if( clientInfo->hWindowMenu )
819     {
820           /* The window to be activated must be displayed in the "Windows" menu */
821           if (wndPtr->wIDmenu >= clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT)
822           {
823               MDI_SwapMenuItems(wndPtr->parent, wndPtr->wIDmenu, clientInfo->idFirstChild + MDI_MOREWINDOWSLIMIT - 1);    
824               MDI_MenuModifyItem(wndPtr->parent ,wndPtr->hwndSelf); 
825           }
826
827           CheckMenuItem(clientInfo->hWindowMenu, wndPtr->wIDmenu, MF_CHECKED);
828     }
829     /* bring active child to the top */
830     SetWindowPos( hWndChild, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
831
832     if( isActiveFrameWnd )
833     {
834             SendMessageA( hWndChild, WM_NCACTIVATE, TRUE, 0L);
835             if( GetFocus() == clientInfo->self )
836                 SendMessageA( clientInfo->self, WM_SETFOCUS, 
837                                 (WPARAM)clientInfo->self, 0L );
838             else
839                 SetFocus( clientInfo->self );
840     }
841     SendMessageA( hWndChild, WM_MDIACTIVATE, (WPARAM)prevActiveWnd,
842                     (LPARAM)hWndChild );
843     retvalue = 1;
844 END:
845     WIN_ReleaseWndPtr(wndPtr);
846     WIN_ReleaseWndPtr(wndPrev);
847     return retvalue;
848 }
849
850 /* -------------------- MDI client window functions ------------------- */
851
852 /**********************************************************************
853  *                              CreateMDIMenuBitmap
854  */
855 static HBITMAP CreateMDIMenuBitmap(void)
856 {
857  HDC            hDCSrc  = CreateCompatibleDC(0);
858  HDC            hDCDest = CreateCompatibleDC(hDCSrc);
859  HBITMAP        hbClose = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CLOSE) );
860  HBITMAP        hbCopy;
861  HBITMAP        hobjSrc, hobjDest;
862
863  hobjSrc = SelectObject(hDCSrc, hbClose);
864  hbCopy = CreateCompatibleBitmap(hDCSrc,GetSystemMetrics(SM_CXSIZE),GetSystemMetrics(SM_CYSIZE));
865  hobjDest = SelectObject(hDCDest, hbCopy);
866
867  BitBlt(hDCDest, 0, 0, GetSystemMetrics(SM_CXSIZE), GetSystemMetrics(SM_CYSIZE),
868           hDCSrc, GetSystemMetrics(SM_CXSIZE), 0, SRCCOPY);
869   
870  SelectObject(hDCSrc, hobjSrc);
871  DeleteObject(hbClose);
872  DeleteDC(hDCSrc);
873
874  hobjSrc = SelectObject( hDCDest, GetStockObject(BLACK_PEN) );
875
876  MoveToEx( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, 0, NULL );
877  LineTo( hDCDest, GetSystemMetrics(SM_CXSIZE) - 1, GetSystemMetrics(SM_CYSIZE) - 1);
878
879  SelectObject(hDCDest, hobjSrc );
880  SelectObject(hDCDest, hobjDest);
881  DeleteDC(hDCDest);
882
883  return hbCopy;
884 }
885
886 /**********************************************************************
887  *                              MDICascade
888  */
889 static LONG MDICascade(WND* clientWnd, MDICLIENTINFO *ci)
890 {
891     WND**       ppWnd;
892     UINT        total;
893   
894     if (ci->hwndChildMaximized)
895         SendMessageA( clientWnd->hwndSelf, WM_MDIRESTORE,
896                         (WPARAM)ci->hwndChildMaximized, 0);
897
898     if (ci->nActiveChildren == 0) return 0;
899
900     if ((ppWnd = WIN_BuildWinArray(clientWnd, BWA_SKIPHIDDEN | BWA_SKIPOWNED | 
901                                               BWA_SKIPICONIC, &total)))
902     {
903         WND**   heapPtr = ppWnd;
904         if( total )
905         {
906             INT delta = 0, n = 0;
907             POINT       pos[2];
908             if( total < ci->nActiveChildren )
909                 delta = GetSystemMetrics(SM_CYICONSPACING) +
910                         GetSystemMetrics(SM_CYICON);
911
912             /* walk the list (backwards) and move windows */
913             while (*ppWnd) ppWnd++;
914             while (ppWnd != heapPtr)
915             {
916                 ppWnd--;
917                 TRACE("move %04x to (%ld,%ld) size [%ld,%ld]\n", 
918                             (*ppWnd)->hwndSelf, pos[0].x, pos[0].y, pos[1].x, pos[1].y);
919
920                 MDI_CalcDefaultChildPos(clientWnd, n++, pos, delta);
921                 SetWindowPos( (*ppWnd)->hwndSelf, 0, pos[0].x, pos[0].y,
922                                 pos[1].x, pos[1].y,
923                                 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
924             }
925         }
926         WIN_ReleaseWinArray(heapPtr);
927     }
928
929     if( total < ci->nActiveChildren )
930         ArrangeIconicWindows( clientWnd->hwndSelf );
931     return 0;
932 }
933
934 /**********************************************************************
935  *                                      MDITile
936  */
937 static void MDITile( WND* wndClient, MDICLIENTINFO *ci, WPARAM wParam )
938 {
939     WND**       ppWnd;
940     UINT        total = 0;
941
942     if (ci->hwndChildMaximized)
943         SendMessageA( wndClient->hwndSelf, WM_MDIRESTORE,
944                         (WPARAM)ci->hwndChildMaximized, 0);
945
946     if (ci->nActiveChildren == 0) return;
947
948     ppWnd = WIN_BuildWinArray(wndClient, BWA_SKIPHIDDEN | BWA_SKIPOWNED | BWA_SKIPICONIC |
949             ((wParam & MDITILE_SKIPDISABLED)? BWA_SKIPDISABLED : 0), &total );
950
951     TRACE("%u windows to tile\n", total);
952
953     if( ppWnd )
954     {
955         WND**   heapPtr = ppWnd;
956
957         if( total )
958         {
959             RECT        rect;
960             int         x, y, xsize, ysize;
961             int         rows, columns, r, c, i;
962
963             GetClientRect(wndClient->hwndSelf,&rect);
964             rows    = (int) sqrt((double)total);
965             columns = total / rows;
966
967             if( wParam & MDITILE_HORIZONTAL )  /* version >= 3.1 */
968             {
969                 i = rows;
970                 rows = columns;  /* exchange r and c */
971                 columns = i;
972             }
973
974             if( total != ci->nActiveChildren)
975             {
976                 y = rect.bottom - 2 * GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
977                 rect.bottom = ( y - GetSystemMetrics(SM_CYICON) < rect.top )? rect.bottom: y;
978             }
979
980             ysize   = rect.bottom / rows;
981             xsize   = rect.right  / columns;
982     
983             for (x = i = 0, c = 1; c <= columns && *ppWnd; c++)
984             {
985                 if (c == columns)
986                 {
987                     rows  = total - i;
988                     ysize = rect.bottom / rows;
989                 }
990
991                 y = 0;
992                 for (r = 1; r <= rows && *ppWnd; r++, i++)
993                 {
994                     SetWindowPos((*ppWnd)->hwndSelf, 0, x, y, xsize, ysize, 
995                                    SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
996                     y += ysize;
997                     ppWnd++;
998                 }
999                 x += xsize;
1000             }
1001         }
1002         WIN_ReleaseWinArray(heapPtr);
1003     }
1004   
1005     if( total < ci->nActiveChildren ) ArrangeIconicWindows( wndClient->hwndSelf );
1006 }
1007
1008 /* ----------------------- Frame window ---------------------------- */
1009
1010
1011 /**********************************************************************
1012  *                                      MDI_AugmentFrameMenu
1013  */
1014 static BOOL MDI_AugmentFrameMenu( WND *frame, HWND hChild )
1015 {
1016     WND*        child = WIN_FindWndPtr(hChild);
1017     HMENU       hSysPopup = 0;
1018   HBITMAP hSysMenuBitmap = 0;
1019
1020     TRACE("frame %p,child %04x\n",frame,hChild);
1021
1022     if( !frame->wIDmenu || !child->hSysMenu )
1023     {
1024         WIN_ReleaseWndPtr(child);
1025         return 0;
1026     }
1027     WIN_ReleaseWndPtr(child);
1028
1029     /* create a copy of sysmenu popup and insert it into frame menu bar */
1030
1031     if (!(hSysPopup = LoadMenuA(GetModuleHandleA("USER32"), "SYSMENU")))
1032         return 0;
1033  
1034     TRACE("\tgot popup %04x in sysmenu %04x\n", 
1035                 hSysPopup, child->hSysMenu);
1036  
1037     AppendMenuA(frame->wIDmenu,MF_HELP | MF_BITMAP,
1038                    SC_MINIMIZE, (LPSTR)(DWORD)HBMMENU_MBAR_MINIMIZE ) ;
1039     AppendMenuA(frame->wIDmenu,MF_HELP | MF_BITMAP,
1040                    SC_RESTORE, (LPSTR)(DWORD)HBMMENU_MBAR_RESTORE );
1041
1042   /* In Win 95 look, the system menu is replaced by the child icon */
1043
1044   if(TWEAK_WineLook > WIN31_LOOK)
1045   {
1046     HICON hIcon = GetClassLongA(hChild, GCL_HICONSM);
1047     if (!hIcon)
1048       hIcon = GetClassLongA(hChild, GCL_HICON);
1049     if (hIcon)
1050     {
1051       HDC hMemDC;
1052       HBITMAP hBitmap, hOldBitmap;
1053       HBRUSH hBrush;
1054       HDC hdc = GetDC(hChild);
1055
1056       if (hdc)
1057       {
1058         int cx, cy;
1059         cx = GetSystemMetrics(SM_CXSMICON);
1060         cy = GetSystemMetrics(SM_CYSMICON);
1061         hMemDC = CreateCompatibleDC(hdc);
1062         hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
1063         hOldBitmap = SelectObject(hMemDC, hBitmap);
1064         SetMapMode(hMemDC, MM_TEXT);
1065         hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
1066         DrawIconEx(hMemDC, 0, 0, hIcon, cx, cy, 0, hBrush, DI_NORMAL);
1067         SelectObject (hMemDC, hOldBitmap);
1068         DeleteObject(hBrush);
1069         DeleteDC(hMemDC);
1070         ReleaseDC(hChild, hdc);
1071         hSysMenuBitmap = hBitmap;
1072       }
1073     }
1074   }
1075   else
1076     hSysMenuBitmap = hBmpClose;
1077
1078     if( !InsertMenuA(frame->wIDmenu,0,MF_BYPOSITION | MF_BITMAP | MF_POPUP,
1079                     hSysPopup, (LPSTR)(DWORD)hSysMenuBitmap))
1080     {  
1081         TRACE("not inserted\n");
1082         DestroyMenu(hSysPopup); 
1083         return 0; 
1084     }
1085
1086     /* The close button is only present in Win 95 look */
1087     if(TWEAK_WineLook > WIN31_LOOK)
1088     {
1089         AppendMenuA(frame->wIDmenu,MF_HELP | MF_BITMAP,
1090                        SC_CLOSE, (LPSTR)(DWORD)HBMMENU_MBAR_CLOSE );
1091     }
1092
1093     EnableMenuItem(hSysPopup, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
1094     EnableMenuItem(hSysPopup, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
1095     EnableMenuItem(hSysPopup, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
1096     SetMenuDefaultItem(hSysPopup, SC_CLOSE, FALSE);
1097
1098     /* redraw menu */
1099     DrawMenuBar(frame->hwndSelf);
1100
1101     return 1;
1102 }
1103
1104 /**********************************************************************
1105  *                                      MDI_RestoreFrameMenu
1106  */
1107 static BOOL MDI_RestoreFrameMenu( WND *frameWnd, HWND hChild )
1108 {
1109     MENUITEMINFOW menuInfo;
1110     INT nItems = GetMenuItemCount(frameWnd->wIDmenu) - 1;
1111     UINT iId = GetMenuItemID(frameWnd->wIDmenu,nItems) ;
1112
1113     TRACE("frameWnd %p,(%04x),child %04x,nIt=%d,iId=%d\n",
1114            frameWnd,frameWnd->hwndSelf,hChild,nItems,iId);
1115
1116     if(!(iId == SC_RESTORE || iId == SC_CLOSE) )
1117         return 0; 
1118
1119     /*
1120      * Remove the system menu, If that menu is the icon of the window
1121      * as it is in win95, we have to delete the bitmap.
1122      */
1123     memset(&menuInfo, 0, sizeof(menuInfo));
1124     menuInfo.cbSize = sizeof(menuInfo);
1125     menuInfo.fMask  = MIIM_DATA | MIIM_TYPE;
1126
1127     GetMenuItemInfoW(frameWnd->wIDmenu, 
1128                      0, 
1129                      TRUE,
1130                      &menuInfo);
1131
1132     RemoveMenu(frameWnd->wIDmenu,0,MF_BYPOSITION);
1133
1134     if ( (menuInfo.fType & MFT_BITMAP)           &&
1135          (LOWORD(menuInfo.dwTypeData)!=0)        &&
1136          (LOWORD(menuInfo.dwTypeData)!=hBmpClose) )
1137     {
1138       DeleteObject((HBITMAP)LOWORD(menuInfo.dwTypeData));
1139     }
1140
1141     if(TWEAK_WineLook > WIN31_LOOK)
1142     {
1143         /* close */
1144         DeleteMenu(frameWnd->wIDmenu,GetMenuItemCount(frameWnd->wIDmenu) - 1,MF_BYPOSITION);
1145     }
1146     /* restore */
1147     DeleteMenu(frameWnd->wIDmenu,GetMenuItemCount(frameWnd->wIDmenu) - 1,MF_BYPOSITION);
1148     /* minimize */
1149     DeleteMenu(frameWnd->wIDmenu,GetMenuItemCount(frameWnd->wIDmenu) - 1,MF_BYPOSITION);
1150
1151     DrawMenuBar(frameWnd->hwndSelf);
1152
1153     return 1;
1154 }
1155
1156
1157 /**********************************************************************
1158  *                                      MDI_UpdateFrameText
1159  *
1160  * used when child window is maximized/restored 
1161  *
1162  * Note: lpTitle can be NULL
1163  */
1164 static void MDI_UpdateFrameText( WND *frameWnd, HWND hClient,
1165                                  BOOL repaint, LPCWSTR lpTitle )
1166 {
1167     WCHAR   lpBuffer[MDI_MAXTITLELENGTH+1];
1168     WND*   clientWnd = WIN_FindWndPtr(hClient);
1169     MDICLIENTINFO *ci = (MDICLIENTINFO *) clientWnd->wExtra;
1170
1171     TRACE("repaint %i, frameText %s\n", repaint, (lpTitle)?debugstr_w(lpTitle):"NULL");
1172
1173     if (!clientWnd)
1174            return;
1175
1176     if (!ci)
1177     {
1178        WIN_ReleaseWndPtr(clientWnd);
1179            return;
1180     }
1181
1182     /* store new "default" title if lpTitle is not NULL */
1183     if (lpTitle) 
1184     {
1185         if (ci->frameTitle) HeapFree( GetProcessHeap(), 0, ci->frameTitle );
1186         if ((ci->frameTitle = HeapAlloc( GetProcessHeap(), 0, (strlenW(lpTitle)+1)*sizeof(WCHAR))))
1187             strcpyW( ci->frameTitle, lpTitle );
1188     }
1189
1190     if (ci->frameTitle)
1191     {
1192         WND* childWnd = WIN_FindWndPtr( ci->hwndChildMaximized );     
1193
1194         if( childWnd && childWnd->text )
1195         {
1196             /* combine frame title and child title if possible */
1197
1198             static const WCHAR lpBracket[]  = {' ','-',' ','[',0};
1199             static const WCHAR lpBracket2[]  = {']',0};
1200             int i_frame_text_length = strlenW(ci->frameTitle);
1201             int i_child_text_length = strlenW(childWnd->text);
1202
1203             lstrcpynW( lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH);
1204
1205             if( i_frame_text_length + 6 < MDI_MAXTITLELENGTH )
1206             {
1207                 strcatW( lpBuffer, lpBracket );
1208
1209                 if( i_frame_text_length + i_child_text_length + 6 < MDI_MAXTITLELENGTH )
1210                 {
1211                     strcatW( lpBuffer, childWnd->text );
1212                     strcatW( lpBuffer, lpBracket2 );
1213                 }
1214                 else
1215                 {
1216                     lstrcpynW( lpBuffer + i_frame_text_length + 4, 
1217                                  childWnd->text, MDI_MAXTITLELENGTH - i_frame_text_length - 5 );
1218                     strcatW( lpBuffer, lpBracket2 );
1219                 }
1220             }
1221         }
1222         else
1223         {
1224             lstrcpynW(lpBuffer, ci->frameTitle, MDI_MAXTITLELENGTH+1 );
1225         }
1226         WIN_ReleaseWndPtr(childWnd);
1227
1228     }
1229     else
1230         lpBuffer[0] = '\0';
1231
1232     DEFWND_SetTextW( frameWnd, lpBuffer );
1233     if( repaint == MDI_REPAINTFRAME)
1234         SetWindowPos( frameWnd->hwndSelf, 0,0,0,0,0, SWP_FRAMECHANGED |
1235                         SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1236
1237     WIN_ReleaseWndPtr(clientWnd);
1238
1239 }
1240
1241
1242 /* ----------------------------- Interface ---------------------------- */
1243
1244
1245 /**********************************************************************
1246  *              MDIClientWndProc_locked
1247  */
1248 static LRESULT WINAPI MDIClientWndProc_locked( WND *wndPtr, UINT message,
1249                                 WPARAM wParam, LPARAM lParam, BOOL unicode )
1250 {
1251     LPCREATESTRUCTA      cs;
1252     MDICLIENTINFO       *ci;
1253     RECT                 rect;
1254     WND                 *frameWnd;
1255     INT                  nItems;
1256     LRESULT              retvalue;
1257     LRESULT (WINAPI *pSendMessage)(HWND, UINT, WPARAM, LPARAM);
1258
1259     pSendMessage = unicode ? SendMessageW : SendMessageA;
1260
1261     if ( !wndPtr )
1262        return 0;
1263
1264     if ( ( frameWnd = WIN_LockWndPtr(wndPtr->parent) ) == NULL )
1265        return 0;
1266
1267     ci = (MDICLIENTINFO *) wndPtr->wExtra;
1268
1269     switch (message)
1270     {
1271       case WM_CREATE:
1272         /* Since we are using only cs->lpCreateParams, we can safely
1273          * cast to LPCREATESTRUCTA here */
1274         cs = (LPCREATESTRUCTA)lParam;
1275
1276         /* Translation layer doesn't know what's in the cs->lpCreateParams
1277          * so we have to keep track of what environment we're in. */
1278
1279         if( wndPtr->flags & WIN_ISWIN32 )
1280         {
1281 #define ccs ((LPCLIENTCREATESTRUCT)cs->lpCreateParams)
1282             ci->hWindowMenu     = ccs->hWindowMenu;
1283             ci->idFirstChild    = ccs->idFirstChild;
1284 #undef ccs
1285         }
1286         else    
1287         {
1288             LPCLIENTCREATESTRUCT16 ccs = MapSL((SEGPTR)cs->lpCreateParams);
1289             ci->hWindowMenu     = ccs->hWindowMenu;
1290             ci->idFirstChild    = ccs->idFirstChild;
1291         }
1292
1293         ci->hwndChildMaximized  = 0;
1294         ci->nActiveChildren     = 0;
1295         ci->nTotalCreated       = 0;
1296         ci->frameTitle          = NULL;
1297         ci->mdiFlags            = 0;
1298         ci->self                = wndPtr->hwndSelf;
1299         wndPtr->dwStyle        |= WS_CLIPCHILDREN;
1300
1301         if (!hBmpClose)
1302         {
1303             hBmpClose = CreateMDIMenuBitmap();
1304             hBmpRestore = LoadBitmapW( 0, MAKEINTRESOURCEW(OBM_RESTORE) );
1305         }
1306         MDI_UpdateFrameText(frameWnd, wndPtr->hwndSelf, MDI_NOFRAMEREPAINT, frameWnd->text);
1307
1308         if (ci->hWindowMenu != 0)
1309             AppendMenuW( ci->hWindowMenu, MF_SEPARATOR, 0, NULL );
1310
1311         GetClientRect(frameWnd->hwndSelf, &rect);
1312         NC_HandleNCCalcSize( wndPtr->hwndSelf, &rect );
1313         wndPtr->rectClient = rect;
1314
1315         TRACE("Client created - hwnd = %04x, idFirst = %u\n",
1316                                 wndPtr->hwndSelf, ci->idFirstChild );
1317
1318         retvalue = 0;
1319         goto END;
1320       
1321       case WM_DESTROY:
1322         if( ci->hwndChildMaximized )
1323             MDI_RestoreFrameMenu(wndPtr->parent, ci->hwndChildMaximized);
1324         if((ci->hWindowMenu != 0) && 
1325            (nItems = GetMenuItemCount(ci->hWindowMenu)) > 0) 
1326         {
1327             ci->idFirstChild = nItems - 1;
1328             ci->nActiveChildren++;              /* to delete a separator */
1329             while( ci->nActiveChildren-- )
1330                 DeleteMenu(ci->hWindowMenu,MF_BYPOSITION,ci->idFirstChild--);
1331         }
1332         retvalue = 0;
1333         goto END;
1334
1335       case WM_MDIACTIVATE:
1336         if( ci->hwndActiveChild != (HWND)wParam )
1337             SetWindowPos((HWND)wParam, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
1338         retvalue = 0;
1339         goto END;
1340
1341       case WM_MDICASCADE:
1342         retvalue = MDICascade(wndPtr, ci);
1343         goto END;
1344
1345       case WM_MDICREATE:
1346         if (lParam) retvalue = MDICreateChild( wndPtr, ci,
1347                                     (MDICREATESTRUCTA *)lParam, unicode );
1348         else retvalue = 0;
1349         goto END;
1350
1351       case WM_MDIDESTROY:
1352         retvalue = MDIDestroyChild( wndPtr, ci, (HWND)wParam, TRUE );
1353         goto END;
1354
1355       case WM_MDIGETACTIVE:
1356           if (lParam) *(BOOL *)lParam = (ci->hwndChildMaximized > 0);
1357           retvalue = ci->hwndActiveChild;
1358           goto END;
1359
1360       case WM_MDIICONARRANGE:
1361         ci->mdiFlags |= MDIF_NEEDUPDATE;
1362         ArrangeIconicWindows(wndPtr->hwndSelf);
1363         ci->sbRecalc = SB_BOTH+1;
1364         pSendMessage(wndPtr->hwndSelf, WM_MDICALCCHILDSCROLL, 0, 0L);
1365         retvalue = 0;
1366         goto END;
1367         
1368       case WM_MDIMAXIMIZE:
1369         ShowWindow( (HWND)wParam, SW_MAXIMIZE );
1370         retvalue = 0;
1371         goto END;
1372
1373       case WM_MDINEXT: /* lParam != 0 means previous window */
1374         MDI_SwitchActiveChild(wndPtr->hwndSelf, (HWND)wParam, (lParam)? FALSE : TRUE );
1375         break;
1376         
1377       case WM_MDIRESTORE:
1378         pSendMessage( (HWND)wParam, WM_SYSCOMMAND, SC_RESTORE, 0);
1379         retvalue = 0;
1380         goto END;
1381
1382       case WM_MDISETMENU:
1383           retvalue = MDISetMenu( wndPtr->hwndSelf, (HMENU)wParam, (HMENU)lParam );
1384           goto END;
1385
1386       case WM_MDIREFRESHMENU:
1387           retvalue = MDIRefreshMenu( wndPtr->hwndSelf, (HMENU)wParam, (HMENU)lParam );
1388           goto END;
1389
1390       case WM_MDITILE:
1391         ci->mdiFlags |= MDIF_NEEDUPDATE;
1392         ShowScrollBar(wndPtr->hwndSelf, SB_BOTH, FALSE);
1393         MDITile(wndPtr, ci, wParam);
1394         ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1395         retvalue = 0;
1396         goto END;
1397
1398       case WM_VSCROLL:
1399       case WM_HSCROLL:
1400         ci->mdiFlags |= MDIF_NEEDUPDATE;
1401         ScrollChildren(wndPtr->hwndSelf, message, wParam, lParam);
1402         ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1403         retvalue = 0;
1404         goto END;
1405
1406       case WM_SETFOCUS:
1407         if( ci->hwndActiveChild )
1408         {
1409            WND* pw = WIN_FindWndPtr( ci->hwndActiveChild );
1410            if( !(pw->dwStyle & WS_MINIMIZE) )
1411                SetFocus( pw->hwndSelf );
1412            WIN_ReleaseWndPtr(pw);
1413         } 
1414         retvalue = 0;
1415         goto END;
1416         
1417       case WM_NCACTIVATE:
1418         if( ci->hwndActiveChild )
1419              pSendMessage(ci->hwndActiveChild, message, wParam, lParam);
1420         break;
1421         
1422       case WM_PARENTNOTIFY:
1423         if (LOWORD(wParam) == WM_LBUTTONDOWN)
1424         {
1425             HWND child;
1426             POINT pt;
1427             pt.x = SLOWORD(lParam);
1428             pt.y = SHIWORD(lParam);
1429             child = ChildWindowFromPoint(wndPtr->hwndSelf, pt);
1430
1431             TRACE("notification from %04x (%li,%li)\n",child,pt.x,pt.y);
1432
1433             if( child && child != wndPtr->hwndSelf && child != ci->hwndActiveChild )
1434                 SetWindowPos(child, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
1435         }
1436         retvalue = 0;
1437         goto END;
1438
1439       case WM_SIZE:
1440         if( IsWindow(ci->hwndChildMaximized) )
1441         {
1442             WND*        child = WIN_FindWndPtr(ci->hwndChildMaximized);
1443             RECT        rect;
1444
1445             rect.left = 0;
1446             rect.top = 0;
1447             rect.right = LOWORD(lParam);
1448             rect.bottom = HIWORD(lParam);
1449
1450             AdjustWindowRectEx(&rect, child->dwStyle, 0, child->dwExStyle);
1451             MoveWindow(ci->hwndChildMaximized, rect.left, rect.top,
1452                          rect.right - rect.left, rect.bottom - rect.top, 1);
1453             WIN_ReleaseWndPtr(child);
1454         }
1455         else
1456             MDI_PostUpdate(wndPtr->hwndSelf, ci, SB_BOTH+1);
1457
1458         break;
1459
1460       case WM_MDICALCCHILDSCROLL:
1461         if( (ci->mdiFlags & MDIF_NEEDUPDATE) && ci->sbRecalc )
1462         {
1463             CalcChildScroll(wndPtr->hwndSelf, ci->sbRecalc-1);
1464             ci->sbRecalc = 0;
1465             ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1466         }
1467         retvalue = 0;
1468         goto END;
1469     }
1470     
1471     retvalue = unicode ? DefWindowProcW( wndPtr->hwndSelf, message, wParam, lParam ) :
1472                          DefWindowProcA( wndPtr->hwndSelf, message, wParam, lParam );
1473 END:
1474     WIN_ReleaseWndPtr(frameWnd);
1475     return retvalue;
1476 }
1477
1478 /***********************************************************************
1479  *              MDIClientWndProcA
1480  */
1481 static LRESULT WINAPI MDIClientWndProcA( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1482 {
1483     LRESULT res;
1484     WND *wndPtr = WIN_FindWndPtr(hwnd);
1485
1486     res = MDIClientWndProc_locked(wndPtr, message, wParam, lParam, FALSE);
1487
1488     WIN_ReleaseWndPtr(wndPtr);
1489     return res;
1490 }
1491
1492 /***********************************************************************
1493  *              MDIClientWndProcW
1494  */
1495 static LRESULT WINAPI MDIClientWndProcW( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
1496 {
1497     LRESULT res;
1498     WND *wndPtr = WIN_FindWndPtr(hwnd);
1499
1500     res = MDIClientWndProc_locked(wndPtr, message, wParam, lParam, TRUE);
1501
1502     WIN_ReleaseWndPtr(wndPtr);
1503     return res;
1504 }
1505
1506 /***********************************************************************
1507  *              DefFrameProc (USER.445)
1508  */
1509 LRESULT WINAPI DefFrameProc16( HWND16 hwnd, HWND16 hwndMDIClient,
1510                                UINT16 message, WPARAM16 wParam, LPARAM lParam )
1511 {
1512     HWND16               childHwnd;
1513     MDICLIENTINFO*       ci;
1514     WND*                 wndPtr;
1515
1516     if (hwndMDIClient)
1517     {
1518         switch (message)
1519         {
1520           case WM_COMMAND:
1521             wndPtr = WIN_FindWndPtr(hwndMDIClient);
1522
1523             if (!wndPtr) {
1524                ERR("null wndPtr for mdi window hwndMDIClient=%04x\n",
1525                              hwndMDIClient);
1526                return 0;
1527             } 
1528
1529             ci     = (MDICLIENTINFO*)wndPtr->wExtra;
1530
1531             /* check for possible syscommands for maximized MDI child */
1532             WIN_ReleaseWndPtr(wndPtr);
1533
1534             if( ci && (
1535                 wParam <  ci->idFirstChild || 
1536                 wParam >= ci->idFirstChild + ci->nActiveChildren
1537             )){
1538                 if( (wParam - 0xF000) & 0xF00F ) break;
1539                 switch( wParam )
1540                   {
1541                     case SC_SIZE:
1542                     case SC_MOVE:
1543                     case SC_MINIMIZE:
1544                     case SC_MAXIMIZE:
1545                     case SC_NEXTWINDOW:
1546                     case SC_PREVWINDOW:
1547                     case SC_CLOSE:
1548                     case SC_RESTORE:
1549                        if( ci->hwndChildMaximized )
1550                            return SendMessageW( ci->hwndChildMaximized, WM_SYSCOMMAND,
1551                                                 wParam, lParam);
1552                   }
1553               }
1554             else
1555               {
1556                 wndPtr = WIN_FindWndPtr(hwndMDIClient);
1557                 ci     = (MDICLIENTINFO*)wndPtr->wExtra;
1558
1559                 if (wParam - ci->idFirstChild == MDI_MOREWINDOWSLIMIT)
1560                     /* User chose "More Windows..." */
1561                     childHwnd = MDI_MoreWindowsDialog(wndPtr);
1562                 else
1563                     /* User chose one of the windows listed in the "Windows" menu */
1564                 childHwnd = MDI_GetChildByID(wndPtr,wParam );
1565
1566                 WIN_ReleaseWndPtr(wndPtr);
1567                 if( childHwnd )
1568                     SendMessageW( hwndMDIClient, WM_MDIACTIVATE, (WPARAM)childHwnd, 0 );
1569               }
1570             break;
1571
1572           case WM_NCACTIVATE:
1573             SendMessageW(hwndMDIClient, message, wParam, lParam);
1574             break;
1575
1576           case WM_SETTEXT:
1577             {
1578                 LPWSTR text = HEAP_strdupAtoW( GetProcessHeap(), 0, MapSL(lParam) );
1579                 wndPtr = WIN_FindWndPtr(hwnd);
1580                 MDI_UpdateFrameText(wndPtr, hwndMDIClient,
1581                                     MDI_REPAINTFRAME, text );
1582                 WIN_ReleaseWndPtr(wndPtr);
1583                 HeapFree( GetProcessHeap(), 0, text );
1584             }
1585             return 1; /* success. FIXME: check text length */
1586         
1587           case WM_SETFOCUS:
1588             SetFocus(hwndMDIClient);
1589             break;
1590
1591           case WM_SIZE:
1592             MoveWindow16(hwndMDIClient, 0, 0, 
1593                          LOWORD(lParam), HIWORD(lParam), TRUE);
1594             break;
1595
1596           case WM_NEXTMENU:
1597
1598             wndPtr = WIN_FindWndPtr(hwndMDIClient);
1599             ci     = (MDICLIENTINFO*)wndPtr->wExtra;
1600
1601             if( !(wndPtr->parent->dwStyle & WS_MINIMIZE) 
1602                 && ci->hwndActiveChild && !ci->hwndChildMaximized )
1603             {
1604                 /* control menu is between the frame system menu and 
1605                  * the first entry of menu bar */
1606
1607                 if( (wParam == VK_LEFT &&
1608                      wndPtr->parent->wIDmenu == LOWORD(lParam)) ||
1609                     (wParam == VK_RIGHT &&
1610                      GetSubMenu16(wndPtr->parent->hSysMenu, 0) == LOWORD(lParam)) )
1611                 {
1612                     LRESULT retvalue;
1613                     WIN_ReleaseWndPtr(wndPtr);
1614                     wndPtr = WIN_FindWndPtr(ci->hwndActiveChild);
1615                     retvalue = MAKELONG( GetSubMenu16(wndPtr->hSysMenu, 0),
1616                                                   ci->hwndActiveChild);
1617                     WIN_ReleaseWndPtr(wndPtr);
1618                     return retvalue;
1619                 }
1620             }
1621             WIN_ReleaseWndPtr(wndPtr);
1622             break;
1623         }
1624     }
1625     
1626     return DefWindowProc16(hwnd, message, wParam, lParam);
1627 }
1628
1629
1630 /***********************************************************************
1631  *              DefFrameProcA (USER32.@)
1632  */
1633 LRESULT WINAPI DefFrameProcA( HWND hwnd, HWND hwndMDIClient,
1634                                 UINT message, WPARAM wParam, LPARAM lParam)
1635 {
1636     if (hwndMDIClient)
1637     {
1638         switch (message)
1639         {
1640           case WM_COMMAND:
1641               return DefFrameProc16( hwnd, hwndMDIClient, message,
1642                                      (WPARAM16)wParam,
1643                               MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ) );
1644
1645           case WM_NCACTIVATE:
1646             SendMessageA(hwndMDIClient, message, wParam, lParam);
1647             break;
1648
1649           case WM_SETTEXT: {
1650                 LRESULT ret;
1651                 LPSTR   segstr = SEGPTR_STRDUP((LPSTR)lParam);
1652
1653                 ret = DefFrameProc16(hwnd, hwndMDIClient, message,
1654                                      wParam, (LPARAM)SEGPTR_GET(segstr) );
1655                 SEGPTR_FREE(segstr);
1656                 return ret;
1657           }
1658         
1659           case WM_NEXTMENU:
1660           case WM_SETFOCUS:
1661           case WM_SIZE:
1662               return DefFrameProc16( hwnd, hwndMDIClient, message,
1663                                      wParam, lParam );
1664         }
1665     }
1666     
1667     return DefWindowProcA(hwnd, message, wParam, lParam);
1668 }
1669
1670
1671 /***********************************************************************
1672  *              DefFrameProcW (USER32.@)
1673  */
1674 LRESULT WINAPI DefFrameProcW( HWND hwnd, HWND hwndMDIClient,
1675                                 UINT message, WPARAM wParam, LPARAM lParam)
1676 {
1677     if (hwndMDIClient)
1678     {
1679         switch (message)
1680         {
1681           case WM_COMMAND:
1682               return DefFrameProc16( hwnd, hwndMDIClient, message,
1683                                      (WPARAM16)wParam,
1684                               MAKELPARAM( (HWND16)lParam, HIWORD(wParam) ) );
1685
1686           case WM_NCACTIVATE:
1687             SendMessageW(hwndMDIClient, message, wParam, lParam);
1688             break;
1689
1690           case WM_SETTEXT: 
1691           {
1692               LPSTR txt = HEAP_strdupWtoA(GetProcessHeap(),0,(LPWSTR)lParam);
1693               LRESULT ret = DefFrameProcA( hwnd, hwndMDIClient, message,
1694                                      wParam, (DWORD)txt );
1695               HeapFree(GetProcessHeap(),0,txt);
1696               return ret;
1697           }
1698           case WM_NEXTMENU:
1699           case WM_SETFOCUS:
1700           case WM_SIZE:
1701               return DefFrameProcA( hwnd, hwndMDIClient, message,
1702                                       wParam, lParam );
1703         }
1704     }
1705     
1706     return DefWindowProcW( hwnd, message, wParam, lParam );
1707 }
1708
1709
1710 /***********************************************************************
1711  *              DefMDIChildProc (USER.447)
1712  */
1713 LRESULT WINAPI DefMDIChildProc16( HWND16 hwnd, UINT16 message,
1714                                   WPARAM16 wParam, LPARAM lParam )
1715 {
1716     MDICLIENTINFO       *ci;
1717     WND                 *clientWnd,*tmpWnd = 0;
1718     LRESULT             retvalue;
1719
1720     tmpWnd     = WIN_FindWndPtr(hwnd);
1721     if (!tmpWnd) return 0;
1722     clientWnd  = WIN_FindWndPtr(tmpWnd->parent->hwndSelf);
1723     ci         = (MDICLIENTINFO *) clientWnd->wExtra;
1724     WIN_ReleaseWndPtr(tmpWnd);
1725
1726     /* Sanity check */
1727     if (clientWnd->cbWndExtra < sizeof(MDICLIENTINFO))
1728     {
1729         WARN("called on non-MDI child window %x\n", hwnd);
1730         WIN_ReleaseWndPtr(clientWnd);
1731         return DefWindowProc16(hwnd, message, wParam, lParam);
1732     }
1733
1734     switch (message)
1735     {
1736       case WM_SETTEXT:
1737         DefWindowProc16(hwnd, message, wParam, lParam);
1738         MDI_MenuModifyItem(clientWnd,hwnd);
1739         if( ci->hwndChildMaximized == hwnd )
1740             MDI_UpdateFrameText( clientWnd->parent, ci->self,
1741                                  MDI_REPAINTFRAME, NULL );
1742         retvalue = 1; /* success. FIXME: check text length */
1743         goto END;
1744
1745       case WM_CLOSE:
1746           SendMessageW( ci->self, WM_MDIDESTROY, (WPARAM)hwnd, 0 );
1747         retvalue = 0;
1748         goto END;
1749
1750       case WM_SETFOCUS:
1751         if( ci->hwndActiveChild != hwnd )
1752             MDI_ChildActivate(clientWnd, hwnd);
1753         break;
1754
1755       case WM_CHILDACTIVATE:
1756         MDI_ChildActivate(clientWnd, hwnd);
1757         retvalue = 0;
1758         goto END;
1759
1760       case WM_NCPAINT:
1761         TRACE("WM_NCPAINT for %04x, active %04x\n",
1762                                              hwnd, ci->hwndActiveChild );
1763         break;
1764
1765       case WM_SYSCOMMAND:
1766         switch( wParam )
1767         {
1768                 case SC_MOVE:
1769                      if( ci->hwndChildMaximized == hwnd)
1770                      {
1771                          retvalue = 0;
1772                          goto END;
1773                      }
1774                      break;
1775                 case SC_RESTORE:
1776                 case SC_MINIMIZE:
1777                      tmpWnd = WIN_FindWndPtr(hwnd);
1778                      tmpWnd->dwStyle |= WS_SYSMENU;
1779                      WIN_ReleaseWndPtr(tmpWnd);
1780                      break;
1781                 case SC_MAXIMIZE:
1782                      if( ci->hwndChildMaximized == hwnd) 
1783                      {
1784                          retvalue = SendMessageW( clientWnd->parent->hwndSelf,
1785                                                   message, wParam, lParam);
1786                           goto END;
1787                      }
1788                      tmpWnd = WIN_FindWndPtr(hwnd);
1789                      tmpWnd->dwStyle &= ~WS_SYSMENU;
1790                      WIN_ReleaseWndPtr(tmpWnd);
1791                      break;
1792                 case SC_NEXTWINDOW:
1793                      SendMessageW( ci->self, WM_MDINEXT, 0, 0);
1794                      retvalue = 0;
1795                      goto END;
1796                 case SC_PREVWINDOW:
1797                      SendMessageW( ci->self, WM_MDINEXT, 0, 1);
1798                      retvalue = 0;
1799                      goto END;
1800         }
1801         break;
1802         
1803       case WM_GETMINMAXINFO:
1804         {
1805             MINMAXINFO16 *mmi16 = (MINMAXINFO16 *)MapSL(lParam);
1806             MINMAXINFO mmi;
1807             STRUCT32_MINMAXINFO16to32( mmi16, &mmi );
1808             MDI_ChildGetMinMaxInfo( clientWnd, hwnd, &mmi );
1809             STRUCT32_MINMAXINFO32to16( &mmi, mmi16 );
1810         }
1811         retvalue = 0;
1812         goto END;
1813
1814       case WM_SETVISIBLE:
1815          if( ci->hwndChildMaximized) ci->mdiFlags &= ~MDIF_NEEDUPDATE;
1816          else
1817             MDI_PostUpdate(clientWnd->hwndSelf, ci, SB_BOTH+1);
1818         break;
1819
1820       case WM_SIZE:
1821         /* do not change */
1822
1823         if( ci->hwndActiveChild == hwnd && wParam != SIZE_MAXIMIZED )
1824         {
1825             ci->hwndChildMaximized = 0;
1826             
1827             MDI_RestoreFrameMenu( clientWnd->parent, hwnd);
1828             MDI_UpdateFrameText( clientWnd->parent, ci->self,
1829                                  MDI_REPAINTFRAME, NULL );
1830         }
1831
1832         if( wParam == SIZE_MAXIMIZED )
1833         {
1834             HWND16 hMaxChild = ci->hwndChildMaximized;
1835
1836             if( hMaxChild == hwnd ) break;
1837
1838             if( hMaxChild)
1839             {       
1840                 SendMessageW( hMaxChild, WM_SETREDRAW, FALSE, 0 );
1841
1842                 MDI_RestoreFrameMenu( clientWnd->parent, hMaxChild);
1843                 ShowWindow16( hMaxChild, SW_SHOWNOACTIVATE);
1844
1845                 SendMessageW( hMaxChild, WM_SETREDRAW, TRUE, 0 );
1846             }
1847
1848             TRACE("maximizing child %04x\n", hwnd );
1849
1850             /*
1851              * Keep track of the maximized window.
1852              */
1853             ci->hwndChildMaximized = hwnd; /* !!! */
1854
1855             /*
1856              * The maximized window should also be the active window
1857              */
1858             MDI_ChildActivate(clientWnd, hwnd);
1859
1860             MDI_AugmentFrameMenu( clientWnd->parent, hwnd );
1861             MDI_UpdateFrameText( clientWnd->parent, ci->self,
1862                                  MDI_REPAINTFRAME, NULL ); 
1863         }
1864
1865         if( wParam == SIZE_MINIMIZED )
1866         {
1867             HWND16 switchTo = MDI_GetWindow(clientWnd, hwnd, TRUE, WS_MINIMIZE);
1868
1869             if( switchTo )
1870                 SendMessageW( switchTo, WM_CHILDACTIVATE, 0, 0);
1871         }
1872          
1873         MDI_PostUpdate(clientWnd->hwndSelf, ci, SB_BOTH+1);
1874         break;
1875
1876       case WM_MENUCHAR:
1877
1878         /* MDI children don't have menu bars */
1879         retvalue = 0x00010000L;
1880         goto END;
1881
1882       case WM_NEXTMENU:
1883
1884         if( wParam == VK_LEFT )         /* switch to frame system menu */
1885         {
1886             retvalue = MAKELONG( GetSubMenu16(clientWnd->parent->hSysMenu, 0),
1887                            clientWnd->parent->hwndSelf );
1888             goto END;
1889         }
1890         if( wParam == VK_RIGHT )        /* to frame menu bar */
1891         {
1892             retvalue = MAKELONG( clientWnd->parent->wIDmenu,
1893                            clientWnd->parent->hwndSelf );
1894             goto END;
1895         }
1896
1897         break;  
1898
1899       case WM_SYSCHAR:
1900            if (wParam == '-')
1901            {
1902                SendMessageW(hwnd,WM_SYSCOMMAND, SC_KEYMENU, (LPARAM)(DWORD)VK_SPACE);
1903                 retvalue = 0;
1904                 goto END;
1905            }
1906     }
1907         
1908     retvalue = DefWindowProc16(hwnd, message, wParam, lParam);
1909 END:
1910     WIN_ReleaseWndPtr(clientWnd);
1911     return retvalue;
1912 }
1913
1914
1915 /***********************************************************************
1916  *              DefMDIChildProcA (USER32.@)
1917  */
1918 LRESULT WINAPI DefMDIChildProcA( HWND hwnd, UINT message,
1919                                    WPARAM wParam, LPARAM lParam )
1920 {
1921     MDICLIENTINFO       *ci;
1922     WND                 *clientWnd,*tmpWnd;
1923     LRESULT             retvalue;
1924
1925     tmpWnd     = WIN_FindWndPtr(hwnd);
1926     if (!tmpWnd) return 0;
1927     clientWnd  = WIN_FindWndPtr(tmpWnd->parent->hwndSelf);
1928     ci         = (MDICLIENTINFO *) clientWnd->wExtra;
1929     WIN_ReleaseWndPtr(tmpWnd);
1930
1931     /* Sanity check */
1932     if (clientWnd->cbWndExtra < sizeof(MDICLIENTINFO))
1933     {
1934         WARN("called on non-MDI child window %x\n", hwnd);
1935         WIN_ReleaseWndPtr(clientWnd);
1936         return DefWindowProcA(hwnd, message, wParam, lParam);
1937     }
1938
1939     switch (message)
1940     {
1941       case WM_SETTEXT:
1942         DefWindowProcA(hwnd, message, wParam, lParam);
1943         MDI_MenuModifyItem(clientWnd,hwnd);
1944         if( ci->hwndChildMaximized == hwnd )
1945             MDI_UpdateFrameText( clientWnd->parent, ci->self,
1946                                  MDI_REPAINTFRAME, NULL );
1947         retvalue = 1; /* success. FIXME: check text length */
1948         goto END;
1949
1950       case WM_GETMINMAXINFO:
1951         MDI_ChildGetMinMaxInfo( clientWnd, hwnd, (MINMAXINFO *)lParam );
1952         retvalue = 0;
1953         goto END;
1954
1955       case WM_MENUCHAR:
1956
1957         /* MDI children don't have menu bars */
1958         retvalue = 0x00010000L;
1959         goto END;
1960
1961       case WM_CLOSE:
1962       case WM_SETFOCUS:
1963       case WM_CHILDACTIVATE:
1964       case WM_NCPAINT:
1965       case WM_SYSCOMMAND:
1966       case WM_SETVISIBLE:
1967       case WM_SIZE:
1968       case WM_NEXTMENU:
1969           retvalue = DefMDIChildProc16( hwnd, message, (WPARAM16)wParam, lParam );
1970           goto END;
1971
1972       case WM_SYSCHAR:
1973            if (wParam == '-')
1974            {
1975                 SendMessageA(hwnd,WM_SYSCOMMAND,
1976                         (WPARAM)SC_KEYMENU, (LPARAM)(DWORD)VK_SPACE);
1977                 retvalue = 0;
1978                 goto END;
1979            }
1980     }
1981     retvalue = DefWindowProcA(hwnd, message, wParam, lParam);
1982 END:
1983     WIN_ReleaseWndPtr(clientWnd);
1984     return retvalue;
1985 }
1986
1987
1988 /***********************************************************************
1989  *              DefMDIChildProcW (USER32.@)
1990  */
1991 LRESULT WINAPI DefMDIChildProcW( HWND hwnd, UINT message,
1992                                    WPARAM wParam, LPARAM lParam )
1993 {
1994     MDICLIENTINFO       *ci;
1995     WND                 *clientWnd,*tmpWnd;
1996     LRESULT             retvalue;
1997
1998     tmpWnd     = WIN_FindWndPtr(hwnd);
1999     if (!tmpWnd) return 0;
2000     clientWnd  = WIN_FindWndPtr(tmpWnd->parent->hwndSelf);
2001     ci         = (MDICLIENTINFO *) clientWnd->wExtra;
2002     WIN_ReleaseWndPtr(tmpWnd);
2003
2004     /* Sanity check */
2005     if (clientWnd->cbWndExtra < sizeof(MDICLIENTINFO))
2006     {
2007         WARN("called on non-MDI child window %x\n", hwnd);
2008         WIN_ReleaseWndPtr(clientWnd);
2009         return DefWindowProcW(hwnd, message, wParam, lParam);
2010     }
2011
2012     switch (message)
2013     {
2014       case WM_SETTEXT:
2015         DefWindowProcW(hwnd, message, wParam, lParam);
2016         MDI_MenuModifyItem(clientWnd,hwnd);
2017         if( ci->hwndChildMaximized == hwnd )
2018             MDI_UpdateFrameText( clientWnd->parent, ci->self,
2019                                  MDI_REPAINTFRAME, NULL );
2020         retvalue = 1; /* success. FIXME: check text length */
2021         goto END;
2022
2023       case WM_GETMINMAXINFO:
2024       case WM_MENUCHAR:
2025       case WM_CLOSE:
2026       case WM_SETFOCUS:
2027       case WM_CHILDACTIVATE:
2028       case WM_NCPAINT:
2029       case WM_SYSCOMMAND:
2030       case WM_SETVISIBLE:
2031       case WM_SIZE:
2032       case WM_NEXTMENU:
2033           retvalue = DefMDIChildProcA( hwnd, message, (WPARAM16)wParam, lParam );
2034           goto END;
2035
2036       case WM_SYSCHAR:
2037            if (wParam == '-')
2038            {
2039                 SendMessageW(hwnd,WM_SYSCOMMAND,
2040                         (WPARAM)SC_KEYMENU, (LPARAM)(DWORD)VK_SPACE);
2041                 retvalue = 0;
2042                 goto END;
2043            }
2044     }
2045     retvalue = DefWindowProcW(hwnd, message, wParam, lParam);
2046 END:
2047     WIN_ReleaseWndPtr(clientWnd);
2048     return retvalue;
2049     
2050 }
2051
2052 /**********************************************************************
2053  *              CreateMDIWindowA (USER32.@) Creates a MDI child
2054  *
2055  * RETURNS
2056  *    Success: Handle to created window
2057  *    Failure: NULL
2058  */
2059 HWND WINAPI CreateMDIWindowA(
2060     LPCSTR lpClassName,    /* [in] Pointer to registered child class name */
2061     LPCSTR lpWindowName,   /* [in] Pointer to window name */
2062     DWORD dwStyle,         /* [in] Window style */
2063     INT X,               /* [in] Horizontal position of window */
2064     INT Y,               /* [in] Vertical position of window */
2065     INT nWidth,          /* [in] Width of window */
2066     INT nHeight,         /* [in] Height of window */
2067     HWND hWndParent,     /* [in] Handle to parent window */
2068     HINSTANCE hInstance, /* [in] Handle to application instance */
2069     LPARAM lParam)         /* [in] Application-defined value */
2070 {
2071     MDICLIENTINFO* pCi;
2072     MDICREATESTRUCTA cs;
2073     WND *pWndParent = WIN_FindWndPtr(hWndParent);
2074     HWND retvalue;
2075
2076     TRACE("(%s,%s,%ld,%d,%d,%d,%d,%x,%d,%ld)\n",
2077           debugstr_a(lpClassName),debugstr_a(lpWindowName),dwStyle,X,Y,
2078           nWidth,nHeight,hWndParent,hInstance,lParam);
2079
2080     if(!pWndParent)
2081     {
2082         ERR("bad hwnd for MDI-client: %04x\n", hWndParent);
2083         return 0;
2084     }
2085     cs.szClass=lpClassName;
2086     cs.szTitle=lpWindowName;
2087     cs.hOwner=hInstance;
2088     cs.x=X;
2089     cs.y=Y;
2090     cs.cx=nWidth;
2091     cs.cy=nHeight;
2092     cs.style=dwStyle;
2093     cs.lParam=lParam;
2094
2095     pCi = (MDICLIENTINFO *)pWndParent->wExtra;
2096     
2097     retvalue = MDICreateChild(pWndParent, pCi, &cs, FALSE);
2098     WIN_ReleaseWndPtr(pWndParent);
2099     return retvalue;
2100 }
2101
2102 /***********************************************************************
2103  *              CreateMDIWindowW (USER32.@) Creates a MDI child
2104  *
2105  * RETURNS
2106  *    Success: Handle to created window
2107  *    Failure: NULL
2108  */
2109 HWND WINAPI CreateMDIWindowW(
2110     LPCWSTR lpClassName,    /* [in] Pointer to registered child class name */
2111     LPCWSTR lpWindowName,   /* [in] Pointer to window name */
2112     DWORD dwStyle,         /* [in] Window style */
2113     INT X,               /* [in] Horizontal position of window */
2114     INT Y,               /* [in] Vertical position of window */
2115     INT nWidth,          /* [in] Width of window */
2116     INT nHeight,         /* [in] Height of window */
2117     HWND hWndParent,     /* [in] Handle to parent window */
2118     HINSTANCE hInstance, /* [in] Handle to application instance */
2119     LPARAM lParam)         /* [in] Application-defined value */
2120 {
2121     MDICLIENTINFO *pCi;
2122     MDICREATESTRUCTW cs;
2123     WND *pWndParent = WIN_FindWndPtr(hWndParent);
2124     HWND retvalue;
2125
2126     TRACE("(%s,%s,%ld,%d,%d,%d,%d,%x,%d,%ld)\n",
2127           debugstr_w(lpClassName), debugstr_w(lpWindowName), dwStyle, X, Y,
2128           nWidth, nHeight, hWndParent, hInstance, lParam);
2129
2130     if(!pWndParent)
2131     {
2132         ERR("bad hwnd for MDI-client: %04x\n", hWndParent);
2133         return 0;
2134     }
2135     cs.szClass = lpClassName;
2136     cs.szTitle = lpWindowName;
2137     cs.hOwner = hInstance;
2138     cs.x = X;
2139     cs.y = Y;
2140     cs.cx = nWidth;
2141     cs.cy = nHeight;
2142     cs.style = dwStyle;
2143     cs.lParam = lParam;
2144
2145     pCi = (MDICLIENTINFO *)pWndParent->wExtra;
2146     
2147     retvalue = MDICreateChild(pWndParent, pCi, (MDICREATESTRUCTA *)&cs, TRUE);
2148     WIN_ReleaseWndPtr(pWndParent);
2149     return retvalue;
2150 }
2151
2152 /**********************************************************************
2153  *              TranslateMDISysAccel (USER.451)
2154  */
2155 BOOL16 WINAPI TranslateMDISysAccel16( HWND16 hwndClient, LPMSG16 msg )
2156 {
2157     if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
2158     {
2159         MSG msg32;
2160         msg32.hwnd    = msg->hwnd;
2161         msg32.message = msg->message;
2162         msg32.wParam  = msg->wParam;
2163         msg32.lParam  = msg->lParam;
2164         /* MDICLIENTINFO is still the same for win32 and win16 ... */
2165         return TranslateMDISysAccel(hwndClient, &msg32);
2166     }
2167     return 0;
2168 }
2169
2170 /**********************************************************************
2171  *              TranslateMDISysAccel (USER32.@)
2172  */
2173 BOOL WINAPI TranslateMDISysAccel( HWND hwndClient, LPMSG msg )
2174 {
2175
2176     if( IsWindow(hwndClient) && (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN))
2177     {
2178         MDICLIENTINFO   *ci = NULL;
2179         HWND            wnd;
2180         WND             *clientWnd = WIN_FindWndPtr(hwndClient);
2181
2182         ci = (MDICLIENTINFO*) clientWnd->wExtra;
2183         wnd = ci->hwndActiveChild;
2184
2185         WIN_ReleaseWndPtr(clientWnd);
2186
2187         if( IsWindow(wnd) && !(GetWindowLongW(wnd, GWL_STYLE) & WS_DISABLED) )
2188         {
2189             WPARAM wParam = 0;
2190
2191             /* translate if the Ctrl key is down and Alt not. */
2192   
2193             if( (GetKeyState(VK_CONTROL) & 0x8000) && 
2194                !(GetKeyState(VK_MENU) & 0x8000))
2195             {
2196                 switch( msg->wParam )
2197                 {
2198                     case VK_F6:
2199                     case VK_TAB:
2200                          wParam = ( GetKeyState(VK_SHIFT) & 0x8000 )
2201                                   ? SC_NEXTWINDOW : SC_PREVWINDOW;
2202                          break;
2203                     case VK_F4:
2204                     case VK_RBUTTON:
2205                          wParam = SC_CLOSE; 
2206                          break;
2207                     default:
2208                          return 0;
2209                 }
2210                 TRACE("wParam = %04x\n", wParam);
2211                 SendMessageW(wnd, WM_SYSCOMMAND, wParam, (LPARAM)msg->wParam);
2212                 return 1;
2213             }
2214         }
2215     }
2216     return 0; /* failure */
2217 }
2218
2219 /***********************************************************************
2220  *              CalcChildScroll (USER.462)
2221  */
2222 void WINAPI CalcChildScroll16( HWND16 hwnd, WORD scroll )
2223 {
2224     return CalcChildScroll( hwnd, scroll );
2225 }
2226
2227 /***********************************************************************
2228  *              CalcChildScroll (USER32.@)
2229  */
2230 void WINAPI CalcChildScroll( HWND hwnd, INT scroll )
2231 {
2232     SCROLLINFO info;
2233     RECT childRect, clientRect;
2234     INT  vmin, vmax, hmin, hmax, vpos, hpos;
2235     WND *pWnd, *Wnd;
2236
2237     if (!(pWnd = WIN_FindWndPtr( hwnd ))) return;
2238     Wnd = WIN_FindWndPtr(hwnd);
2239     GetClientRect( hwnd, &clientRect );
2240     SetRectEmpty( &childRect );
2241
2242     for ( WIN_UpdateWndPtr(&pWnd,pWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd,pWnd->next))
2243     {
2244           if( pWnd->dwStyle & WS_MAXIMIZE )
2245           {
2246               ShowScrollBar(hwnd, SB_BOTH, FALSE);
2247               WIN_ReleaseWndPtr(pWnd);
2248               WIN_ReleaseWndPtr(Wnd);
2249               return;
2250           }
2251           if( pWnd->dwStyle & WS_VISIBLE )
2252               UnionRect( &childRect, &pWnd->rectWindow, &childRect );
2253     } 
2254     WIN_ReleaseWndPtr(pWnd);
2255     UnionRect( &childRect, &clientRect, &childRect );
2256
2257     hmin = childRect.left; hmax = childRect.right - clientRect.right;
2258     hpos = clientRect.left - childRect.left;
2259     vmin = childRect.top; vmax = childRect.bottom - clientRect.bottom;
2260     vpos = clientRect.top - childRect.top;
2261
2262     switch( scroll )
2263     {
2264         case SB_HORZ:
2265                         vpos = hpos; vmin = hmin; vmax = hmax;
2266         case SB_VERT:
2267                         info.cbSize = sizeof(info);
2268                         info.nMax = vmax; info.nMin = vmin; info.nPos = vpos;
2269                         info.fMask = SIF_POS | SIF_RANGE;
2270                         SetScrollInfo(hwnd, scroll, &info, TRUE);
2271                         break;
2272         case SB_BOTH:
2273                         SCROLL_SetNCSbState( Wnd->hwndSelf, vmin, vmax, vpos,
2274                                              hmin, hmax, hpos);
2275     }    
2276     WIN_ReleaseWndPtr(Wnd);
2277 }
2278
2279
2280 /***********************************************************************
2281  *              ScrollChildren (USER.463)
2282  */
2283 void WINAPI ScrollChildren16(HWND16 hWnd, UINT16 uMsg, WPARAM16 wParam, LPARAM lParam)
2284 {
2285     ScrollChildren( hWnd, uMsg, wParam, lParam );
2286 }
2287
2288
2289 /***********************************************************************
2290  *              ScrollChildren (USER32.@)
2291  */
2292 void WINAPI ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam,
2293                              LPARAM lParam)
2294 {
2295     WND *wndPtr = WIN_FindWndPtr(hWnd);
2296     INT newPos = -1;
2297     INT curPos, length, minPos, maxPos, shift;
2298
2299     if( !wndPtr ) return;
2300
2301     if( uMsg == WM_HSCROLL )
2302     {
2303         GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
2304         curPos = GetScrollPos(hWnd,SB_HORZ);
2305         length = (wndPtr->rectClient.right - wndPtr->rectClient.left)/2;
2306         shift = GetSystemMetrics(SM_CYHSCROLL);
2307     }
2308     else if( uMsg == WM_VSCROLL )
2309     {
2310         GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
2311         curPos = GetScrollPos(hWnd,SB_VERT);
2312         length = (wndPtr->rectClient.bottom - wndPtr->rectClient.top)/2;
2313         shift = GetSystemMetrics(SM_CXVSCROLL);
2314     }
2315     else
2316     {
2317         WIN_ReleaseWndPtr(wndPtr);
2318         return;
2319     }
2320
2321     WIN_ReleaseWndPtr(wndPtr);
2322     switch( wParam )
2323     {
2324         case SB_LINEUP: 
2325                         newPos = curPos - shift;
2326                         break;
2327         case SB_LINEDOWN:    
2328                         newPos = curPos + shift;
2329                         break;
2330         case SB_PAGEUP: 
2331                         newPos = curPos - length;
2332                         break;
2333         case SB_PAGEDOWN:    
2334                         newPos = curPos + length;
2335                         break;
2336
2337         case SB_THUMBPOSITION: 
2338                         newPos = LOWORD(lParam);
2339                         break;
2340
2341         case SB_THUMBTRACK:  
2342                         return;
2343
2344         case SB_TOP:            
2345                         newPos = minPos;
2346                         break;
2347         case SB_BOTTOM: 
2348                         newPos = maxPos;
2349                         break;
2350         case SB_ENDSCROLL:
2351                         CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
2352                         return;
2353     }
2354
2355     if( newPos > maxPos )
2356         newPos = maxPos;
2357     else 
2358         if( newPos < minPos )
2359             newPos = minPos;
2360
2361     SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
2362
2363     if( uMsg == WM_VSCROLL )
2364         ScrollWindowEx(hWnd ,0 ,curPos - newPos, NULL, NULL, 0, NULL, 
2365                         SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
2366     else
2367         ScrollWindowEx(hWnd ,curPos - newPos, 0, NULL, NULL, 0, NULL,
2368                         SW_INVALIDATE | SW_ERASE | SW_SCROLLCHILDREN );
2369 }
2370
2371
2372 /******************************************************************************
2373  *              CascadeWindows (USER32.@) Cascades MDI child windows
2374  *
2375  * RETURNS
2376  *    Success: Number of cascaded windows.
2377  *    Failure: 0
2378  */
2379 WORD WINAPI
2380 CascadeWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
2381                 UINT cKids, const HWND *lpKids)
2382 {
2383     FIXME("(0x%08x,0x%08x,...,%u,...): stub\n",
2384            hwndParent, wFlags, cKids);
2385
2386     return 0;
2387 }
2388
2389
2390 /******************************************************************************
2391  *              TileWindows (USER32.@) Tiles MDI child windows
2392  *
2393  * RETURNS
2394  *    Success: Number of tiled windows.
2395  *    Failure: 0
2396  */
2397 WORD WINAPI
2398 TileWindows (HWND hwndParent, UINT wFlags, const LPRECT lpRect,
2399              UINT cKids, const HWND *lpKids)
2400 {
2401     FIXME("(0x%08x,0x%08x,...,%u,...): stub\n",
2402            hwndParent, wFlags, cKids);
2403
2404     return 0;
2405 }
2406
2407 /************************************************************************
2408  *              "More Windows..." functionality
2409  */
2410
2411 /*              MDI_MoreWindowsDlgProc
2412  *
2413  *    This function will process the messages sent to the "More Windows..."
2414  *    dialog.
2415  *    Return values:  0    = cancel pressed
2416  *                    HWND = ok pressed or double-click in the list...
2417  *
2418  */
2419
2420 static BOOL WINAPI MDI_MoreWindowsDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
2421 {
2422     switch (iMsg)
2423     {
2424        case WM_INITDIALOG:
2425        {
2426            WND  *pWnd;
2427            UINT widest       = 0;
2428            UINT length;
2429            UINT i;
2430            WND  *pParentWnd  = (WND *)lParam;
2431            MDICLIENTINFO *ci = (MDICLIENTINFO*)pParentWnd->wExtra;
2432            HWND hListBox = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2433
2434            /* Fill the list, sorted by id... */
2435            for (i = 0; i < ci->nActiveChildren; i++)
2436            {
2437
2438                /* Find the window with the current ID */
2439                for (pWnd = WIN_LockWndPtr(pParentWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd, pWnd->next))
2440                    if (pWnd->wIDmenu == ci->idFirstChild + i)
2441                        break;
2442
2443                SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM) pWnd->text);
2444                SendMessageW(hListBox, LB_SETITEMDATA, i, (LPARAM) pWnd);
2445                length = strlenW(pWnd->text);
2446                WIN_ReleaseWndPtr(pWnd);
2447
2448                if (length > widest)
2449                    widest = length;
2450            }
2451            /* Make sure the horizontal scrollbar scrolls ok */
2452            SendMessageW(hListBox, LB_SETHORIZONTALEXTENT, widest * 6, 0);
2453
2454            /* Set the current selection */
2455            SendMessageW(hListBox, LB_SETCURSEL, MDI_MOREWINDOWSLIMIT, 0);
2456            return TRUE;
2457        }
2458
2459        case WM_COMMAND:
2460            switch (LOWORD(wParam))
2461            {
2462                 case IDOK:
2463                 {
2464                     /*  windows are sorted by menu ID, so we must return the
2465                      *  window associated to the given id
2466                      */
2467                     HWND hListBox     = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2468                     UINT index        = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
2469                     WND *pWnd         = (WND *)SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
2470
2471                     EndDialog(hDlg, pWnd->hwndSelf);
2472                     return TRUE;
2473                 }
2474                 case IDCANCEL:
2475                     EndDialog(hDlg, 0);
2476                     return TRUE;
2477
2478                 default:
2479                     switch (HIWORD(wParam))
2480                     {
2481                         case LBN_DBLCLK:
2482                         {
2483                             /*  windows are sorted by menu ID, so we must return the
2484                              *  window associated to the given id
2485                              */
2486                             HWND hListBox     = GetDlgItem(hDlg, MDI_IDC_LISTBOX);
2487                             UINT index        = SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
2488                             WND *pWnd         = (WND *)SendMessageW(hListBox, LB_GETITEMDATA, index, 0);
2489
2490                             EndDialog(hDlg, pWnd->hwndSelf);
2491                             return TRUE;
2492                         }
2493                     }
2494                     break;
2495            }
2496            break;
2497     }
2498     return FALSE;
2499 }
2500
2501 /*
2502  *
2503  *                      MDI_MoreWindowsDialog
2504  *
2505  *     Prompts the user with a listbox containing the opened
2506  *     documents. The user can then choose a windows and click
2507  *     on OK to set the current window to the one selected, or
2508  *     CANCEL to cancel. The function returns a handle to the
2509  *     selected window.
2510  */
2511
2512 static HWND MDI_MoreWindowsDialog(WND* wndPtr)
2513 {
2514     LPCVOID template;
2515     HRSRC hRes;
2516     HANDLE hDlgTmpl;
2517
2518     hRes = FindResourceA(GetModuleHandleA("USER32"), "MDI_MOREWINDOWS", RT_DIALOGA);
2519
2520     if (hRes == 0)
2521         return 0;
2522
2523     hDlgTmpl = LoadResource(GetModuleHandleA("USER32"), hRes );
2524
2525     if (hDlgTmpl == 0)
2526         return 0;
2527
2528     template = LockResource( hDlgTmpl );
2529
2530     if (template == 0)
2531         return 0;
2532
2533     return (HWND) DialogBoxIndirectParamA(GetModuleHandleA("USER32"),
2534                                           (LPDLGTEMPLATEA) template,
2535                                           wndPtr->hwndSelf,
2536                                           (DLGPROC) MDI_MoreWindowsDlgProc,
2537                                           (LPARAM) wndPtr);
2538 }
2539
2540 /*
2541  *
2542  *                      MDI_SwapMenuItems
2543  *
2544  *      Will swap the menu IDs for the given 2 positions.
2545  *      pos1 and pos2 are menu IDs
2546  *     
2547  *    
2548  */
2549
2550 static void MDI_SwapMenuItems(WND *parentWnd, UINT pos1, UINT pos2)
2551 {
2552     WND *pWnd;
2553
2554     for (pWnd = WIN_LockWndPtr(parentWnd->child); pWnd; WIN_UpdateWndPtr(&pWnd,pWnd->next))
2555     {
2556         if (pWnd->wIDmenu == pos1)
2557             pWnd->wIDmenu = pos2;
2558         else
2559             if (pWnd->wIDmenu == pos2)
2560                 pWnd->wIDmenu = pos1;
2561     }
2562
2563     WIN_ReleaseWndPtr(pWnd);
2564 }
2565