Release 951212
[wine/multimedia.git] / windows / mdi.c
blob0fe19aa447966832cc64a373659afa653616b381
1 /* MDI.C
3 * Copyright 1994, Bob Amstadt
4 * 1995, Alex Korobka
6 * This file contains routines to support MDI features.
8 * Notes: Windows keeps ID of MDI menu item in the wIDenu field
9 * of corresponding MDI child.
11 * Basic child activation routine is MDI_ChildActivate and
12 * SetWindowPos(childHwnd,...) implicitly calls it if SWP_NOACTIVATE i
13 * is not used.
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <math.h>
20 #include "windows.h"
21 #include "win.h"
22 #include "nonclient.h"
23 #include "mdi.h"
24 #include "user.h"
25 #include "menu.h"
26 #include "sysmetrics.h"
27 #include "stddebug.h"
28 #include "debug.h"
30 void ScrollChildren(HWND , UINT , WPARAM , LPARAM );
31 void CalcChildScroll(HWND, WORD);
33 /* ----------------- declarations ----------------- */
35 static LONG MDI_ChildActivate(WND* ,HWND );
37 /* -------- Miscellaneous service functions ----------
39 * MDI_GetChildByID
42 static HWND MDI_GetChildByID(WND* mdiClient,int id)
44 HWND hWnd = mdiClient->hwndChild;
45 WND* wndPtr = WIN_FindWndPtr( hWnd );
47 if( !wndPtr ) return 0;
49 while( wndPtr )
51 if( wndPtr->wIDmenu == id ) return hWnd;
52 wndPtr = WIN_FindWndPtr(hWnd = wndPtr->hwndNext);
55 return 0;
58 /**********************************************************************
59 * MDI_MenuAppendItem
61 static BOOL MDI_MenuAppendItem(WND *clientWnd, HWND hWndChild)
63 char buffer[128];
64 MDICLIENTINFO *clientInfo = (MDICLIENTINFO*)clientWnd->wExtra;
65 WND *wndPtr = WIN_FindWndPtr(hWndChild);
66 LPSTR lpWndText = (LPSTR) USER_HEAP_LIN_ADDR(wndPtr->hText);
67 int n = sprintf(buffer, "%d ",
68 clientInfo->nActiveChildren);
70 if( !clientInfo->hWindowMenu ) return 0;
72 if( lpWndText )
73 strncpy(buffer + n, lpWndText, sizeof(buffer) - n - 1);
74 return AppendMenu(clientInfo->hWindowMenu,MF_STRING,
75 wndPtr->wIDmenu,(LPSTR)buffer);
78 /**********************************************************************
79 * MDI_MenuModifyItem
81 static BOOL MDI_MenuModifyItem(WND* clientWnd, HWND hWndChild )
83 char buffer[128];
84 MDICLIENTINFO *clientInfo = (MDICLIENTINFO*)clientWnd->wExtra;
85 WND *wndPtr = WIN_FindWndPtr(hWndChild);
86 LPSTR lpWndText = (LPSTR) USER_HEAP_LIN_ADDR(wndPtr->hText);
87 UINT n = sprintf(buffer, "%d ",
88 wndPtr->wIDmenu - clientInfo->idFirstChild + 1);
89 BOOL bRet = 0;
91 if( !clientInfo->hWindowMenu ) return 0;
93 if( lpWndText )
94 strncpy(buffer + n, lpWndText, sizeof(buffer) - n - 1);
96 n = GetMenuState(clientInfo->hWindowMenu,wndPtr->wIDmenu ,MF_BYCOMMAND);
97 bRet = ModifyMenu(clientInfo->hWindowMenu , wndPtr->wIDmenu ,
98 MF_BYCOMMAND | MF_STRING, wndPtr->wIDmenu ,(LPSTR)buffer );
99 CheckMenuItem(clientInfo->hWindowMenu ,wndPtr->wIDmenu , n & MF_CHECKED);
100 return bRet;
103 /**********************************************************************
104 * MDI_MenuDeleteItem
106 static BOOL MDI_MenuDeleteItem(WND* clientWnd, HWND hWndChild )
108 char buffer[128];
109 MDICLIENTINFO *clientInfo = (MDICLIENTINFO*)clientWnd->wExtra;
110 WND *wndPtr = WIN_FindWndPtr(hWndChild);
111 LPSTR lpWndText;
112 INT index = 0,id,n;
114 if( !clientInfo->nActiveChildren ||
115 !clientInfo->hWindowMenu ) return 0;
117 id = wndPtr->wIDmenu;
118 DeleteMenu(clientInfo->hWindowMenu,id,MF_BYCOMMAND);
120 /* walk the rest of MDI children to prevent gaps in the id
121 * sequence and in the menu child list
124 for( index = id+1; index <= clientInfo->nActiveChildren +
125 clientInfo->idFirstChild; index++ )
127 wndPtr = WIN_FindWndPtr(MDI_GetChildByID(clientWnd,index));
128 if( !wndPtr )
130 dprintf_mdi(stddeb,"MDIMenuDeleteItem: no window for id=%i\n",index);
131 continue;
134 /* set correct id */
135 wndPtr->wIDmenu--;
137 n = sprintf(buffer, "%d ",index - clientInfo->idFirstChild);
138 lpWndText = (LPSTR) USER_HEAP_LIN_ADDR(wndPtr->hText);
140 if( lpWndText )
141 strncpy(buffer + n, lpWndText, sizeof(buffer) - n - 1);
143 /* change menu */
144 ModifyMenu(clientInfo->hWindowMenu ,index ,MF_BYCOMMAND | MF_STRING,
145 index - 1 ,(LPSTR)buffer );
147 return 1;
150 /**********************************************************************
151 * MDI_GetWindow
153 * returns "activateable" child or zero
155 HWND MDI_GetWindow(WND *clientWnd, HWND hWnd, WORD wTo )
157 HWND hWndNext;
158 MDICLIENTINFO *clientInfo = (MDICLIENTINFO*)clientWnd->wExtra;
159 WND *wndPtr;
161 if( !hWnd ) hWnd = clientInfo->hwndActiveChild;
163 if( !(wndPtr = WIN_FindWndPtr(hWnd)) ) return 0;
165 hWndNext = hWnd;
166 wTo = wTo ? GW_HWNDPREV : GW_HWNDNEXT;
168 while( hWndNext )
170 if( clientWnd->hwndChild == hWndNext && wTo == GW_HWNDPREV )
171 hWndNext = GetWindow( hWndNext, GW_HWNDLAST);
172 else if( wndPtr->hwndNext == 0 && wTo == GW_HWNDNEXT )
173 hWndNext = clientWnd->hwndChild;
174 else
175 hWndNext = GetWindow( hWndNext, wTo );
177 wndPtr = WIN_FindWndPtr( hWndNext );
179 if( (wndPtr->dwStyle & WS_VISIBLE) &&
180 !(wndPtr->dwStyle & WS_DISABLED) )
181 break;
183 /* check if all windows were iterated through */
184 if( hWndNext == hWnd ) break;
187 return ( hWnd == hWndNext )? 0 : hWndNext;
191 /**********************************************************************
192 * MDISetMenu
193 * FIXME: This is not complete.
195 HMENU MDISetMenu(HWND hwnd, BOOL fRefresh, HMENU hmenuFrame, HMENU hmenuWindow)
197 dprintf_mdi(stddeb, "WM_MDISETMENU: "NPFMT" %04x "NPFMT" "NPFMT"\n", hwnd, fRefresh, hmenuFrame, hmenuWindow);
198 if (!fRefresh) {
199 HWND hwndFrame = GetParent(hwnd);
200 HMENU oldFrameMenu = GetMenu(hwndFrame);
201 SetMenu(hwndFrame, hmenuFrame);
202 return oldFrameMenu;
204 return 0;
207 /**********************************************************************
208 * MDIIconArrange
210 WORD MDIIconArrange(HWND parent)
212 return ArrangeIconicWindows(parent); /* Any reason why the */
213 /* existing icon arrange */
214 /* can't be used here? */
215 /* -DRP */
218 /**********************************************************************
219 * MDICreateChild
221 HWND MDICreateChild(WND *w, MDICLIENTINFO *ci, HWND parent, LPARAM lParam )
223 MDICREATESTRUCT *cs = (MDICREATESTRUCT *)PTR_SEG_TO_LIN(lParam);
224 HWND hwnd;
225 WORD wIDmenu = ci->idFirstChild + ci->nActiveChildren;
226 int spacing;
227 char chDef = '\0';
230 * Create child window
232 cs->style &= (WS_MINIMIZE | WS_MAXIMIZE | WS_HSCROLL | WS_VSCROLL);
234 /* The child windows should probably */
235 /* stagger, shouldn't they? -DRP */
236 spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME);
237 cs->x = ci->nActiveChildren * spacing;
238 cs->y = ci->nActiveChildren * spacing;
240 /* this menu is needed to set a check mark in MDI_ChildActivate */
241 AppendMenu(ci->hWindowMenu ,MF_STRING ,wIDmenu, (LPSTR)&chDef);
243 hwnd = CreateWindow( cs->szClass, cs->szTitle,
244 WS_CHILD | WS_BORDER | WS_CAPTION | WS_CLIPSIBLINGS |
245 WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU |
246 WS_THICKFRAME | WS_VISIBLE | cs->style,
247 cs->x, cs->y, cs->cx, cs->cy, parent,
248 (HMENU) wIDmenu, w->hInstance, (SEGPTR)lParam);
250 if (hwnd)
252 ci->nActiveChildren++;
253 MDI_MenuModifyItem(w ,hwnd);
255 /* FIXME: at this point NC area of hwnd stays inactive */
257 else
258 DeleteMenu(ci->hWindowMenu,wIDmenu,MF_BYCOMMAND);
260 return hwnd;
263 /**********************************************************************
264 * MDI_SwitchActiveChild
266 * Notes: SetWindowPos sends WM_CHILDACTIVATE to the child window that is
267 * being activated
269 * Ideally consecutive SetWindowPos should be replaced by
270 * BeginDeferWindowPos/EndDeferWindowPos but currently it doesn't
271 * matter.
273 * wTo is basically lParam of WM_MDINEXT message
275 void MDI_SwitchActiveChild(HWND clientHwnd, HWND childHwnd, WORD wTo )
277 WND *w = WIN_FindWndPtr(clientHwnd);
278 HWND hwndTo = MDI_GetWindow(w,childHwnd,wTo);
279 HWND hwndPrev;
280 MDICLIENTINFO *ci;
283 ci = (MDICLIENTINFO *) w->wExtra;
285 dprintf_mdi(stddeb, "MDI_SwitchActiveChild: "NPFMT", %i\n",childHwnd,wTo);
287 if ( !childHwnd || !hwndTo ) return;
289 hwndPrev = ci->hwndActiveChild;
291 if ( hwndTo != hwndPrev )
293 if (ci->flagChildMaximized)
295 RECT rectOldRestore, rect;
297 w = WIN_FindWndPtr(hwndTo);
299 /* save old window dimensions */
300 rectOldRestore = ci->rectRestore;
301 GetWindowRect(hwndTo, &ci->rectRestore);
303 rect.top = (ci->rectMaximize.top -
304 (w->rectClient.top - w->rectWindow.top));
305 rect.bottom = (ci->rectMaximize.bottom +
306 (w->rectWindow.bottom - w->rectClient.bottom));
307 rect.left = (ci->rectMaximize.left -
308 (w->rectClient.left - w->rectWindow.left));
309 rect.right = (ci->rectMaximize.right +
310 (w->rectWindow.right - w->rectClient.right));
311 w->dwStyle |= WS_MAXIMIZE;
313 /* maximize it */
314 ci->flagChildMaximized = childHwnd; /* prevent maximization
315 * in MDI_ChildActivate
318 SetWindowPos( hwndTo, HWND_TOP, rect.left, rect.top,
319 rect.right - rect.left + 1,
320 rect.bottom - rect.top + 1, 0);
322 SendMessage( hwndTo, WM_SIZE, SIZE_MAXIMIZED,
323 MAKELONG(w->rectClient.right-w->rectClient.left,
324 w->rectClient.bottom-w->rectClient.top));
326 w = WIN_FindWndPtr(hwndPrev);
328 if( w )
330 w->dwStyle &= ~WS_MAXIMIZE;
332 /* push hwndPrev to the bottom if needed */
333 if( !wTo )
334 SetWindowPos(hwndPrev, HWND_BOTTOM,
335 rectOldRestore.left, rectOldRestore.top,
336 rectOldRestore.right - rectOldRestore.left + 1,
337 rectOldRestore.bottom - rectOldRestore.top + 1,
338 SWP_NOACTIVATE );
341 else
343 SetWindowPos( hwndTo, HWND_TOP, 0, 0, 0, 0,
344 SWP_NOMOVE | SWP_NOSIZE );
345 if( !wTo && hwndPrev )
347 SetWindowPos( hwndPrev, HWND_BOTTOM, 0, 0, 0, 0,
348 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
356 /**********************************************************************
357 * MDIDestroyChild
359 HWND MDIDestroyChild(WND *w_parent, MDICLIENTINFO *ci, HWND parent,
360 HWND child, BOOL flagDestroy)
362 WND *childPtr = WIN_FindWndPtr(child);
364 if( childPtr )
366 if( child == ci->hwndActiveChild )
368 MDI_SwitchActiveChild(parent,child,0);
370 if( child == ci->hwndActiveChild )
371 MDI_ChildActivate(w_parent,0);
373 MDI_MenuDeleteItem(w_parent, child);
376 ci->nActiveChildren--;
378 if( ci->flagChildMaximized == child )
379 ci->flagChildMaximized = 1;
381 if (flagDestroy)
383 DestroyWindow(child);
384 PostMessage(parent,WM_MDICALCCHILDSCROLL,0,0L);
385 ci->sbRecalc |= (SB_BOTH+1);
389 return 0;
393 /**********************************************************************
394 * MDIMaximizeChild
396 LONG MDIMaximizeChild(HWND parent, HWND child, MDICLIENTINFO *ci)
399 WND *w = WIN_FindWndPtr(child);
400 RECT rect;
402 if( !SendMessage( child, WM_QUERYOPEN, 0, 0L) )
403 return 0;
405 ci->rectRestore = w->rectWindow;
407 rect.top = (ci->rectMaximize.top -
408 (w->rectClient.top - w->rectWindow.top));
409 rect.bottom = (ci->rectMaximize.bottom +
410 (w->rectWindow.bottom - w->rectClient.bottom));
411 rect.left = (ci->rectMaximize.left -
412 (w->rectClient.left - w->rectWindow.left));
413 rect.right = (ci->rectMaximize.right +
414 (w->rectWindow.right - w->rectClient.right));
415 w->dwStyle |= WS_MAXIMIZE;
417 SetWindowPos(child, 0, rect.left, rect.top,
418 rect.right - rect.left + 1, rect.bottom - rect.top + 1, 0);
420 ci->flagChildMaximized = child;
422 SendMessage(child, WM_SIZE, SIZE_MAXIMIZED,
423 MAKELONG(w->rectClient.right-w->rectClient.left,
424 w->rectClient.bottom-w->rectClient.top));
426 SendMessage(GetParent(parent), WM_NCPAINT, 0, 0);
428 return 0;
431 /**********************************************************************
432 * MDIRestoreChild
434 LONG MDIRestoreChild(HWND parent, MDICLIENTINFO *ci)
436 HWND hWnd;
438 hWnd = ci->hwndActiveChild;
440 dprintf_mdi(stddeb,"MDIRestoreChild: restore "NPFMT"\n", hWnd);
442 ci->flagChildMaximized = FALSE;
444 ShowWindow(hWnd, SW_RESTORE); /* display the window */
446 hWnd = GetParent(parent);
448 SendMessage(hWnd,WM_NCPAINT , 0, 0);
450 return 0;
453 /**********************************************************************
454 * MDI_ChildActivate
456 * Note: hWndChild is NULL when last child is being destroyed
458 LONG MDI_ChildActivate(WND *clientPtr, HWND hWndChild)
460 MDICLIENTINFO *clientInfo = (MDICLIENTINFO*)clientPtr->wExtra;
461 HWND prevActiveWnd = clientInfo->hwndActiveChild;
462 WND *wndPtr = WIN_FindWndPtr( hWndChild );
463 WND *wndPrev = WIN_FindWndPtr( prevActiveWnd );
464 BOOL isActiveFrameWnd = 0;
466 if( hWndChild == prevActiveWnd ) return 0L;
468 if( wndPtr )
469 if( wndPtr->dwStyle & WS_DISABLED ) return 0L;
471 dprintf_mdi(stddeb,"MDI_ChildActivate: "NPFMT"\n", hWndChild);
473 if( GetActiveWindow() == clientPtr->hwndParent )
474 isActiveFrameWnd = TRUE;
476 /* deactivate prev. active child */
477 if( wndPrev )
479 SendMessage( prevActiveWnd, WM_NCACTIVATE, FALSE, 0L );
480 SendMessage( prevActiveWnd, WM_MDIACTIVATE, FALSE,
481 MAKELONG(hWndChild,prevActiveWnd));
482 /* uncheck menu item */
483 if( clientInfo->hWindowMenu )
484 CheckMenuItem( clientInfo->hWindowMenu,
485 wndPrev->wIDmenu, 0);
488 /* set appearance */
489 if( clientInfo->flagChildMaximized )
490 if( clientInfo->flagChildMaximized != hWndChild )
491 if( hWndChild )
493 clientInfo->hwndActiveChild = hWndChild;
494 MDIMaximizeChild(GetParent(hWndChild),hWndChild,clientInfo);
497 clientInfo->hwndActiveChild = hWndChild;
499 /* check if we have any children left */
500 if( !hWndChild )
502 if( isActiveFrameWnd )
503 SetFocus( GetParent(hWndChild) );
504 return 0;
507 /* check menu item */
508 if( clientInfo->hWindowMenu )
509 CheckMenuItem( clientInfo->hWindowMenu,
510 wndPtr->wIDmenu, MF_CHECKED);
512 /* bring active child to the top */
513 SetWindowPos( hWndChild, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
515 if( isActiveFrameWnd )
517 SendMessage( hWndChild, WM_NCACTIVATE, TRUE, 0L);
518 if( GetFocus() == GetParent(hWndChild) )
519 SendMessage( GetParent(hWndChild), WM_SETFOCUS,
520 GetParent(hWndChild), 0L );
521 else
522 SetFocus( GetParent(hWndChild) );
525 SendMessage( hWndChild, WM_MDIACTIVATE, TRUE,
526 MAKELONG(prevActiveWnd,hWndChild) );
528 return 1;
531 /**********************************************************************
532 * MDI_BuildWCL
534 * iTotal returns number of children available for tiling or cascading
536 MDIWCL* MDI_BuildWCL(WND* clientWnd, int* iTotal)
538 MDIWCL *listTop,*listNext;
539 WND *childWnd;
541 if (!(listTop = (MDIWCL*)malloc( sizeof(MDIWCL) ))) return NULL;
543 listTop->hChild = clientWnd->hwndChild;
544 listTop->prev = NULL;
545 *iTotal = 1;
547 /* build linked list from top child to bottom */
549 childWnd = WIN_FindWndPtr( listTop->hChild );
550 while( childWnd && childWnd->hwndNext )
552 listNext = (MDIWCL*)malloc(sizeof(MDIWCL));
554 if( !listNext )
556 /* quit gracefully */
557 listNext = listTop->prev;
558 while( listTop )
560 listNext = listTop->prev;
561 free(listTop);
562 listTop = listNext;
564 fprintf(stdnimp,"MDICascade: allocation failed\n");
565 return NULL;
568 if( (childWnd->dwStyle & WS_DISABLED) ||
569 (childWnd->dwStyle & WS_MINIMIZE) ||
570 !(childWnd->dwStyle & WS_VISIBLE) )
572 listTop->hChild = 0;
573 (*iTotal)--;
576 listNext->hChild = childWnd->hwndNext;
577 listNext->prev = listTop;
578 listTop = listNext;
579 (*iTotal)++;
581 childWnd = WIN_FindWndPtr( childWnd->hwndNext );
584 if( (childWnd->dwStyle & WS_DISABLED) ||
585 (childWnd->dwStyle & WS_MINIMIZE) ||
586 !(childWnd->dwStyle & WS_VISIBLE) )
588 listTop->hChild = 0;
589 (*iTotal)--;
592 return listTop;
595 /**********************************************************************
596 * MDICascade
598 LONG MDICascade(HWND parent, MDICLIENTINFO *ci)
600 WND *clientWnd;
601 MDIWCL *listTop,*listPrev;
602 RECT rect;
603 int spacing, xsize, ysize;
604 int x, y;
605 int iToPosition = 0;
607 if (ci->flagChildMaximized)
608 MDIRestoreChild(parent, ci);
610 if (ci->nActiveChildren == 0) return 0;
612 GetClientRect(parent, &rect);
613 spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME);
614 ysize = rect.bottom - 8 * spacing;
615 xsize = rect.right - 8 * spacing;
617 dprintf_mdi(stddeb,
618 "MDICascade: Client wnd at (%ld,%ld) - (%ld,%ld), spacing %d\n",
619 (LONG)rect.left, (LONG)rect.top, (LONG)rect.right, (LONG)rect.bottom,
620 spacing);
622 clientWnd = WIN_FindWndPtr( parent );
624 listTop = MDI_BuildWCL(clientWnd,&iToPosition);
626 if( !listTop ) return 0;
628 x = 0;
629 y = 0;
631 /* walk list and move windows */
632 while ( listTop )
634 dprintf_mdi(stddeb, "MDICascade: move "NPFMT" to (%d,%d) size [%d,%d]\n",
635 listTop->hChild, x, y, xsize, ysize);
637 if( listTop->hChild )
639 SetWindowPos(listTop->hChild, 0, x, y, xsize, ysize,
640 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
642 x += spacing;
643 y += spacing;
646 listPrev = listTop->prev;
647 free(listTop);
648 listTop = listPrev;
651 return 0;
654 /**********************************************************************
655 * MDITile
658 LONG MDITile(HWND parent, MDICLIENTINFO *ci)
660 WND *wndClient = WIN_FindWndPtr(parent);
661 MDIWCL *listTop,*listPrev;
662 RECT rect;
663 int xsize, ysize;
664 int x, y;
665 int rows, columns;
666 int r, c;
667 int i;
668 int iToPosition = 0;
670 if (ci->flagChildMaximized)
671 MDIRestoreChild(parent, ci);
673 if (ci->nActiveChildren == 0) return 0;
675 listTop = MDI_BuildWCL(wndClient, &iToPosition);
677 dprintf_mdi(stddeb,"MDITile: %i windows to tile\n",iToPosition);
679 if( !listTop ) return 0;
681 GetClientRect(parent, &rect);
683 rows = (int) sqrt((double) iToPosition);
684 columns = iToPosition / rows;
686 /* hack */
687 if( iToPosition != ci->nActiveChildren)
689 y = rect.bottom - 2 * SYSMETRICS_CYICONSPACING - SYSMETRICS_CYICON;
690 rect.bottom = ( y - SYSMETRICS_CYICON < rect.top )? rect.bottom: y;
693 ysize = rect.bottom / rows;
694 xsize = rect.right / columns;
696 x = 0;
697 i = 0;
699 for (c = 1; c <= columns; c++)
701 if (c == columns)
703 rows = iToPosition - i;
704 ysize = rect.bottom / rows;
707 y = 0;
708 for (r = 1; r <= rows; r++, i++)
710 /* shouldn't happen but... */
711 if( !listTop )
712 break;
714 if( listTop->hChild )
716 SetWindowPos(listTop->hChild, 0, x, y, xsize, ysize,
717 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
718 y += ysize;
721 listPrev = listTop->prev;
722 free(listTop);
723 listTop = listPrev;
726 x += xsize;
729 /* free the rest if any */
730 while( listTop ) {
731 listPrev = listTop->prev;
732 free(listTop);
733 listTop = listPrev; }
735 return 0;
738 /**********************************************************************
739 * MDIHandleLButton
741 BOOL MDIHandleLButton(HWND hwndFrame, HWND hwndClient,
742 WORD wParam, LONG lParam)
744 MDICLIENTINFO *ci;
745 WND *w;
746 RECT rect;
747 WORD x;
749 w = WIN_FindWndPtr(hwndClient);
750 ci = (MDICLIENTINFO *) w->wExtra;
752 if (wParam == HTMENU && ci->flagChildMaximized)
754 x = LOWORD(lParam);
756 NC_GetInsideRect(hwndFrame, &rect);
757 if (x < rect.left + SYSMETRICS_CXSIZE)
759 SendMessage(ci->hwndActiveChild, WM_SYSCOMMAND,
760 SC_CLOSE, lParam);
761 return TRUE;
763 else if (x >= rect.right - SYSMETRICS_CXSIZE)
765 SendMessage(ci->hwndActiveChild, WM_SYSCOMMAND,
766 SC_RESTORE, lParam);
767 return TRUE;
771 return FALSE;
774 /**********************************************************************
775 * MDIPaintMaximized
777 LONG MDIPaintMaximized(HWND hwndFrame, HWND hwndClient, WORD message,
778 WORD wParam, LONG lParam)
780 static HBITMAP hbitmapClose = 0;
781 static HBITMAP hbitmapMaximized = 0;
783 MDICLIENTINFO *ci;
784 WND *w;
785 HDC hdc, hdcMem;
786 RECT rect;
787 WND *wndPtr = WIN_FindWndPtr(hwndFrame);
789 w = WIN_FindWndPtr(hwndClient);
790 ci = (MDICLIENTINFO *) w->wExtra;
792 dprintf_mdi(stddeb, "MDIPaintMaximized: frame "NPFMT", client "NPFMT
793 ", max flag %d, menu %04x\n", hwndFrame, hwndClient,
794 ci->flagChildMaximized, wndPtr ? wndPtr->wIDmenu : 0);
796 if (ci->flagChildMaximized && wndPtr && wndPtr->wIDmenu != 0)
798 NC_DoNCPaint(hwndFrame, wParam, TRUE);
800 hdc = GetDCEx(hwndFrame, 0, DCX_CACHE | DCX_WINDOW);
801 if (!hdc) return 0;
803 hdcMem = CreateCompatibleDC(hdc);
805 if (hbitmapClose == 0)
807 hbitmapClose = LoadBitmap(0, MAKEINTRESOURCE(OBM_OLD_CLOSE));
808 hbitmapMaximized = LoadBitmap(0, MAKEINTRESOURCE(OBM_RESTORE));
811 dprintf_mdi(stddeb,
812 "MDIPaintMaximized: hdcMem "NPFMT", close bitmap "NPFMT", "
813 "maximized bitmap "NPFMT"\n",
814 hdcMem, hbitmapClose, hbitmapMaximized);
816 NC_GetInsideRect(hwndFrame, &rect);
817 rect.top += (wndPtr->dwStyle & WS_CAPTION) ? SYSMETRICS_CYSIZE + 1 : 0;
818 SelectObject(hdcMem, hbitmapClose);
819 BitBlt(hdc, rect.left, rect.top + 1,
820 SYSMETRICS_CXSIZE, SYSMETRICS_CYSIZE,
821 hdcMem, 1, 1, SRCCOPY);
823 NC_GetInsideRect(hwndFrame, &rect);
824 rect.top += (wndPtr->dwStyle & WS_CAPTION) ? SYSMETRICS_CYSIZE + 1 : 0;
825 rect.left = rect.right - SYSMETRICS_CXSIZE;
826 SelectObject(hdcMem, hbitmapMaximized);
827 BitBlt(hdc, rect.left, rect.top + 1,
828 SYSMETRICS_CXSIZE, SYSMETRICS_CYSIZE,
829 hdcMem, 1, 1, SRCCOPY);
831 NC_GetInsideRect(hwndFrame, &rect);
832 rect.top += (wndPtr->dwStyle & WS_CAPTION) ? SYSMETRICS_CYSIZE + 1 : 0;
833 rect.left += SYSMETRICS_CXSIZE;
834 rect.right -= SYSMETRICS_CXSIZE;
835 rect.bottom = rect.top + SYSMETRICS_CYMENU;
837 MENU_DrawMenuBar(hdc, &rect, hwndFrame, FALSE);
839 DeleteDC(hdcMem);
840 ReleaseDC(hwndFrame, hdc);
842 else
843 return DefWindowProc(hwndFrame, message, wParam, lParam);
845 return 0;
848 /**********************************************************************
849 * MDIClientWndProc
851 * This function is the handler for all MDI requests.
853 LRESULT MDIClientWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
855 LPCREATESTRUCT cs;
856 LPCLIENTCREATESTRUCT ccs;
857 MDICLIENTINFO *ci;
858 WND *w;
860 w = WIN_FindWndPtr(hwnd);
861 ci = (MDICLIENTINFO *) w->wExtra;
863 switch (message)
865 case WM_CREATE:
866 cs = (LPCREATESTRUCT) PTR_SEG_TO_LIN(lParam);
867 ccs = (LPCLIENTCREATESTRUCT) PTR_SEG_TO_LIN(cs->lpCreateParams);
868 ci->hWindowMenu = ccs->hWindowMenu;
869 ci->idFirstChild = ccs->idFirstChild;
870 ci->flagChildMaximized = FALSE;
871 ci->sbStop = 0;
873 w->dwStyle |= WS_CLIPCHILDREN;
875 AppendMenu(ccs->hWindowMenu,MF_SEPARATOR,0,NULL);
877 GetClientRect(w->hwndParent, &ci->rectMaximize);
878 MoveWindow(hwnd, 0, 0,
879 ci->rectMaximize.right, ci->rectMaximize.bottom, 1);
881 return 0;
883 case WM_MDIACTIVATE:
884 SetWindowPos(wParam,0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
885 return 0;
887 case WM_MDICASCADE:
888 return MDICascade(hwnd, ci);
890 case WM_MDICREATE:
891 return (LONG)MDICreateChild(w, ci, hwnd, lParam );
893 case WM_MDIDESTROY:
894 return MDIDestroyChild(w, ci, hwnd, wParam, TRUE);
896 case WM_MDIGETACTIVE:
897 return ((LONG) ci->hwndActiveChild |
898 ((LONG) (ci->flagChildMaximized>0) << 16));
900 case WM_MDIICONARRANGE:
901 ci->sbStop = TRUE;
902 MDIIconArrange(hwnd);
903 ci->sbStop = FALSE;
904 SendMessage(hwnd,WM_MDICALCCHILDSCROLL,0,0L);
905 return 0;
907 case WM_MDIMAXIMIZE:
908 return MDIMaximizeChild(hwnd, wParam, ci);
910 case WM_MDINEXT:
911 MDI_SwitchActiveChild(hwnd, (HWND)wParam, lParam);
912 break;
914 case WM_MDIRESTORE:
915 return MDIRestoreChild(hwnd, ci);
917 case WM_MDISETMENU:
918 return MDISetMenu(hwnd, wParam, LOWORD(lParam), HIWORD(lParam));
920 case WM_MDITILE:
921 ci->sbStop = TRUE;
922 ShowScrollBar(hwnd,SB_BOTH,FALSE);
923 MDITile(hwnd, ci);
924 ci->sbStop = FALSE;
925 return 0;
927 case WM_VSCROLL:
928 case WM_HSCROLL:
929 ci->sbStop = TRUE;
930 ScrollChildren(hwnd,message,wParam,lParam);
931 ci->sbStop = FALSE;
932 return 0;
934 case WM_SETFOCUS:
935 if( ci->hwndActiveChild )
937 w = WIN_FindWndPtr( ci->hwndActiveChild );
938 if( !(w->dwStyle & WS_MINIMIZE) )
939 SetFocus( ci->hwndActiveChild );
941 return 0;
943 case WM_NCACTIVATE:
944 if( ci->hwndActiveChild )
945 SendMessage(ci->hwndActiveChild, message, wParam, lParam);
946 break;
948 case WM_PARENTNOTIFY:
949 if (wParam == WM_LBUTTONDOWN)
950 SetWindowPos(ci->hwndHitTest, 0,0,0,0,0, SWP_NOSIZE | SWP_NOMOVE );
951 break;
953 case WM_SIZE:
954 GetClientRect(w->hwndParent, &ci->rectMaximize);
955 if( !ci->hwndActiveChild )
957 PostMessage(hwnd,WM_MDICALCCHILDSCROLL,0,0L);
958 ci->sbRecalc |= (SB_BOTH+1);
960 break;
962 case WM_MDICALCCHILDSCROLL:
963 if( !ci->sbStop )
964 if( ci->sbRecalc )
966 CalcChildScroll(hwnd, ci->sbRecalc-1);
967 ci->sbRecalc = 0;
969 return 0;
972 return DefWindowProc(hwnd, message, wParam, lParam);
975 /**********************************************************************
976 * DefFrameProc (USER.445)
979 LRESULT DefFrameProc(HWND hwnd, HWND hwndMDIClient, UINT message,
980 WPARAM wParam, LPARAM lParam)
982 HWND childHwnd;
984 if (hwndMDIClient)
986 switch (message)
988 case WM_COMMAND:
989 childHwnd = MDI_GetChildByID( WIN_FindWndPtr(hwndMDIClient),
990 wParam );
991 if( childHwnd )
992 SendMessage(hwndMDIClient, WM_MDIACTIVATE, childHwnd , 0L);
993 break;
995 case WM_NCLBUTTONDOWN:
996 if (MDIHandleLButton(hwnd, hwndMDIClient, wParam, lParam))
997 return 0;
998 break;
1000 case WM_NCACTIVATE:
1001 SendMessage(hwndMDIClient, message, wParam, lParam);
1002 return MDIPaintMaximized(hwnd, hwndMDIClient,
1003 message, wParam, lParam);
1005 case WM_NCPAINT:
1006 return MDIPaintMaximized(hwnd, hwndMDIClient,
1007 message, wParam, lParam);
1009 case WM_SETFOCUS:
1010 SetFocus(hwndMDIClient);
1011 break;
1013 case WM_SIZE:
1014 MoveWindow(hwndMDIClient, 0, 0,
1015 LOWORD(lParam), HIWORD(lParam), TRUE);
1016 break;
1020 return DefWindowProc(hwnd, message, wParam, lParam);
1023 /**********************************************************************
1024 * DefMDIChildProc (USER.447)
1027 #ifdef WINELIB32
1028 LONG DefMDIChildProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1029 #else
1030 LONG DefMDIChildProc(HWND hwnd, WORD message, WORD wParam, LONG lParam)
1031 #endif
1033 MDICLIENTINFO *ci;
1034 WND *clientWnd;
1036 clientWnd = WIN_FindWndPtr(GetParent(hwnd));
1037 ci = (MDICLIENTINFO *) clientWnd->wExtra;
1039 switch (message)
1041 case WM_NCHITTEST:
1042 ci->hwndHitTest = hwnd;
1043 break;
1045 case WM_SETTEXT:
1046 DefWindowProc(hwnd, message, wParam, lParam);
1047 MDI_MenuModifyItem(clientWnd,hwnd);
1048 return 0;
1050 case WM_CLOSE:
1051 SendMessage(GetParent(hwnd),WM_MDIDESTROY,hwnd,0L);
1052 return 0;
1054 case WM_SIZE:
1055 if( ci->hwndActiveChild != hwnd )
1056 MDI_ChildActivate(clientWnd, hwnd);
1057 break;
1059 case WM_CHILDACTIVATE:
1060 MDI_ChildActivate(clientWnd, hwnd);
1061 return 0;
1063 case WM_NCPAINT:
1064 dprintf_mdi(stddeb,"DefMDIChildProc: WM_NCPAINT for "NPFMT", active "NPFMT"\n",
1065 hwnd, ci->hwndActiveChild );
1066 break;
1068 case WM_SYSCOMMAND:
1069 switch (wParam)
1071 case SC_MAXIMIZE:
1072 return SendMessage(GetParent(hwnd), WM_MDIMAXIMIZE, (WPARAM)hwnd, 0);
1074 case SC_RESTORE:
1075 return SendMessage(GetParent(hwnd), WM_MDIRESTORE, (WPARAM)hwnd, 0);
1077 break;
1079 /* should also handle following messages */
1080 case WM_GETMINMAXINFO:
1081 /* should return rect of MDI client
1082 * so that normal ShowWindow will be able to handle
1083 * actions that are handled by MDIMaximize and MDIRestore */
1085 case WM_SETVISIBLE:
1086 if( !ci->sbStop )
1088 PostMessage(GetParent(hwnd),WM_MDICALCCHILDSCROLL,0,0L);
1089 ci->sbRecalc |= (SB_BOTH+1);
1091 break;
1092 case WM_SETFOCUS:
1093 if( IsChild( GetActiveWindow(), GetParent(hwnd)) )
1094 SendMessage(clientWnd->hwndChild,WM_CHILDACTIVATE,0,0L);
1095 if( !ci->sbStop )
1097 PostMessage(GetParent(hwnd),WM_MDICALCCHILDSCROLL,0,0L);
1098 ci->sbRecalc |= (SB_BOTH+1);
1100 break;
1102 case WM_MENUCHAR:
1103 case WM_NEXTMENU:
1104 /* set current menu to child system menu */
1106 break;
1109 return DefWindowProc(hwnd, message, wParam, lParam);
1112 /**********************************************************************
1113 * TranslateMDISysAccel (USER.451)
1116 BOOL TranslateMDISysAccel(HWND hwndClient, LPMSG msg)
1118 return 0;
1122 /***********************************************************************
1123 * CalcChildScroll (USER.462)
1125 void CalcChildScroll( HWND hwnd, WORD scroll )
1127 RECT childRect, clientRect;
1128 HWND hwndChild;
1130 GetClientRect( hwnd, &clientRect );
1131 SetRectEmpty( &childRect );
1132 hwndChild = GetWindow( hwnd, GW_CHILD );
1133 while (hwndChild)
1135 WND *wndPtr = WIN_FindWndPtr( hwndChild );
1136 UnionRect( &childRect, &wndPtr->rectWindow, &childRect );
1137 hwndChild = wndPtr->hwndNext;
1139 UnionRect( &childRect, &clientRect, &childRect );
1141 if ((scroll == SB_HORZ) || (scroll == SB_BOTH))
1143 SetScrollRange( hwnd, SB_HORZ, childRect.left,
1144 childRect.right - clientRect.right, FALSE );
1145 SetScrollPos( hwnd, SB_HORZ, clientRect.left - childRect.left, TRUE );
1147 if ((scroll == SB_VERT) || (scroll == SB_BOTH))
1149 SetScrollRange( hwnd, SB_VERT, childRect.top,
1150 childRect.bottom - clientRect.bottom, FALSE );
1151 SetScrollPos( hwnd, SB_HORZ, clientRect.top - childRect.top, TRUE );
1155 /***********************************************************************
1156 * ScrollChildren (USER.463)
1158 void ScrollChildren(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1160 WND *wndPtr = WIN_FindWndPtr(hWnd);
1161 short newPos;
1162 short curPos;
1163 short length;
1164 short minPos;
1165 short maxPos;
1166 short shift;
1168 if( !wndPtr ) return;
1170 if( uMsg == WM_HSCROLL )
1172 GetScrollRange(hWnd,SB_HORZ,&minPos,&maxPos);
1173 curPos = GetScrollPos(hWnd,SB_HORZ);
1174 length = (wndPtr->rectClient.right - wndPtr->rectClient.left)/2;
1175 shift = SYSMETRICS_CYHSCROLL;
1177 else if( uMsg == WM_VSCROLL )
1179 GetScrollRange(hWnd,SB_VERT,&minPos,&maxPos);
1180 curPos = GetScrollPos(hWnd,SB_VERT);
1181 length = (wndPtr->rectClient.bottom - wndPtr->rectClient.top)/2;
1182 shift = SYSMETRICS_CXVSCROLL;
1184 else return;
1186 switch( wParam )
1188 case SB_LINEUP:
1189 newPos = curPos - shift;
1190 break;
1191 case SB_LINEDOWN:
1192 newPos = curPos + shift;
1193 break;
1194 case SB_PAGEUP:
1195 newPos = curPos - length;
1196 break;
1197 case SB_PAGEDOWN:
1198 newPos = curPos + length;
1199 break;
1201 case SB_THUMBPOSITION:
1202 newPos = LOWORD(lParam);
1203 break;
1205 case SB_THUMBTRACK:
1206 return;
1208 case SB_TOP:
1209 newPos = minPos;
1210 break;
1211 case SB_BOTTOM:
1212 newPos = maxPos;
1213 break;
1214 case SB_ENDSCROLL:
1215 CalcChildScroll(hWnd,(uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ);
1216 return;
1219 if( newPos > maxPos )
1220 newPos = maxPos;
1221 else if( newPos < minPos )
1222 newPos = minPos;
1224 SetScrollPos(hWnd, (uMsg == WM_VSCROLL)?SB_VERT:SB_HORZ , newPos, TRUE);
1226 if( uMsg == WM_VSCROLL )
1227 ScrollWindow(hWnd ,0 ,curPos - newPos, NULL, NULL);
1228 else
1229 ScrollWindow(hWnd ,curPos - newPos, 0, NULL, NULL);