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