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