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