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