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