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