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