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