3 * Copyright 1994, Bob Amstadt
5 * This file contains routines to support MDI features.
14 #include "sysmetrics.h"
16 #define DEBUG_MDI /* */
18 extern WORD MENU_DrawMenuBar( HDC hDC, LPRECT lprect,
19 HWND hwnd, BOOL suppress_draw ); /* menu.c */
21 /**********************************************************************
25 MDIRecreateMenuList(MDICLIENTINFO *ci)
32 fprintf(stderr, "MDIRecreateMenuList: hWindowMenu %04.4x\n",
36 id = ci->idFirstChild;
37 while (DeleteMenu(ci->hWindowMenu, id, MF_BYCOMMAND))
41 fprintf(stderr, "MDIRecreateMenuList: id %04.4x, idFirstChild %04.4x\n",
42 id, ci->idFirstChild);
45 if (!ci->flagMenuAltered)
47 ci->flagMenuAltered = TRUE;
48 AppendMenu(ci->hWindowMenu, MF_SEPARATOR, 0, NULL);
51 id = ci->idFirstChild;
53 for (chi = ci->infoActiveChildren; chi != NULL; chi = chi->next)
55 n = sprintf(buffer, "%d ", index++);
56 GetWindowText(chi->hwnd, buffer + n, sizeof(buffer) - n - 1);
59 fprintf(stderr, "MDIRecreateMenuList: id %04.4x, '%s'\n",
63 AppendMenu(ci->hWindowMenu, MF_STRING, id++, buffer);
67 /**********************************************************************
71 MDICreateChild(WND *w, MDICLIENTINFO *ci, HWND parent, LPMDICREATESTRUCT cs)
78 cs->style &= (WS_MINIMIZE | WS_MAXIMIZE | WS_HSCROLL | WS_VSCROLL);
80 hwnd = CreateWindowEx(0, cs->szClass, cs->szTitle,
81 WS_CHILD | WS_BORDER | WS_CAPTION | WS_CLIPSIBLINGS |
82 WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU |
83 WS_THICKFRAME | WS_VISIBLE | cs->style,
84 cs->x, cs->y, cs->cx, cs->cy, parent, (HMENU) 0,
85 w->hInstance, (LPSTR) cs);
89 HANDLE h = USER_HEAP_ALLOC(GMEM_MOVEABLE, sizeof(MDICHILDINFO));
90 MDICHILDINFO *child_info = USER_HEAP_ADDR(h);
97 ci->nActiveChildren++;
99 child_info->next = ci->infoActiveChildren;
100 child_info->prev = NULL;
101 child_info->hwnd = hwnd;
103 if (ci->infoActiveChildren)
104 ci->infoActiveChildren->prev = child_info;
106 ci->infoActiveChildren = child_info;
108 SendMessage(parent, WM_CHILDACTIVATE, 0, 0);
114 /**********************************************************************
118 MDIDestroyChild(WND *w_parent, MDICLIENTINFO *ci, HWND parent, HWND child,
123 chi = ci->infoActiveChildren;
124 while (chi && chi->hwnd != child)
130 chi->prev->next = chi->next;
132 chi->next->prev = chi->prev;
133 if (ci->infoActiveChildren == chi)
134 ci->infoActiveChildren = chi->next;
136 ci->nActiveChildren--;
138 if (chi->hwnd == ci->hwndActiveChild)
139 SendMessage(parent, WM_CHILDACTIVATE, 0, 0);
141 USER_HEAP_FREE((HANDLE) (LONG) chi);
144 DestroyWindow(child);
150 /**********************************************************************
153 void MDIBringChildToTop(HWND parent, WORD id, WORD by_id, BOOL send_to_bottom)
160 w = WIN_FindWndPtr(parent);
161 ci = (MDICLIENTINFO *) w->wExtra;
164 fprintf(stderr, "MDIBringToTop: id %04.4x, by_id %d\n", id, by_id);
168 id -= ci->idFirstChild;
169 if (!by_id || id < ci->nActiveChildren)
171 chi = ci->infoActiveChildren;
175 for (i = 0; i < id; i++)
180 while (chi && chi->hwnd != id)
188 fprintf(stderr, "MDIBringToTop: child %04.4x\n", chi->hwnd);
190 if (chi != ci->infoActiveChildren)
192 if (ci->flagChildMaximized)
194 RECT rectOldRestore, rect;
196 w = WIN_FindWndPtr(chi->hwnd);
198 rectOldRestore = ci->rectRestore;
199 GetWindowRect(chi->hwnd, &ci->rectRestore);
201 rect.top = (ci->rectMaximize.top -
202 (w->rectClient.top - w->rectWindow.top));
203 rect.bottom = (ci->rectMaximize.bottom +
204 (w->rectWindow.bottom - w->rectClient.bottom));
205 rect.left = (ci->rectMaximize.left -
206 (w->rectClient.left - w->rectWindow.left));
207 rect.right = (ci->rectMaximize.right +
208 (w->rectWindow.right - w->rectClient.right));
209 w->dwStyle |= WS_MAXIMIZE;
210 SetWindowPos(chi->hwnd, HWND_TOP, rect.left, rect.top,
211 rect.right - rect.left + 1,
212 rect.bottom - rect.top + 1, 0);
213 SendMessage(chi->hwnd, WM_SIZE, SIZE_MAXIMIZED,
214 MAKELONG(w->rectClient.right-w->rectClient.left,
215 w->rectClient.bottom-w->rectClient.top));
217 w = WIN_FindWndPtr(ci->hwndActiveChild);
218 w->dwStyle &= ~WS_MAXIMIZE;
219 SetWindowPos(ci->hwndActiveChild, HWND_BOTTOM,
220 rectOldRestore.left, rectOldRestore.top,
221 rectOldRestore.right - rectOldRestore.left + 1,
222 rectOldRestore.bottom - rectOldRestore.top + 1,
224 (send_to_bottom ? 0 : SWP_NOZORDER));
228 SetWindowPos(chi->hwnd, HWND_TOP, 0, 0, 0, 0,
229 SWP_NOMOVE | SWP_NOSIZE );
232 SetWindowPos(ci->hwndActiveChild, HWND_BOTTOM, 0, 0, 0, 0,
233 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
238 chi->next->prev = chi->prev;
241 chi->prev->next = chi->next;
244 chi->next = ci->infoActiveChildren;
245 chi->next->prev = chi;
246 ci->infoActiveChildren = chi;
248 SendMessage(parent, WM_CHILDACTIVATE, 0, 0);
252 fprintf(stderr, "MDIBringToTop: pos %04.4x, hwnd %04.4x\n",
258 /**********************************************************************
261 LONG MDIMaximizeChild(HWND parent, HWND child, MDICLIENTINFO *ci)
263 WND *w = WIN_FindWndPtr(child);
266 MDIBringChildToTop(parent, child, FALSE, FALSE);
267 ci->rectRestore = w->rectWindow;
269 rect.top = (ci->rectMaximize.top -
270 (w->rectClient.top - w->rectWindow.top));
271 rect.bottom = (ci->rectMaximize.bottom +
272 (w->rectWindow.bottom - w->rectClient.bottom));
273 rect.left = (ci->rectMaximize.left -
274 (w->rectClient.left - w->rectWindow.left));
275 rect.right = (ci->rectMaximize.right +
276 (w->rectWindow.right - w->rectClient.right));
277 w->dwStyle |= WS_MAXIMIZE;
278 SetWindowPos(child, 0, rect.left, rect.top,
279 rect.right - rect.left + 1, rect.bottom - rect.top + 1,
280 SWP_NOACTIVATE | SWP_NOZORDER);
282 ci->flagChildMaximized = TRUE;
284 SendMessage(child, WM_SIZE, SIZE_MAXIMIZED,
285 MAKELONG(w->rectClient.right-w->rectClient.left,
286 w->rectClient.bottom-w->rectClient.top));
287 SendMessage(GetParent(parent), WM_NCPAINT, 1, 0);
292 /**********************************************************************
295 LONG MDIRestoreChild(HWND parent, MDICLIENTINFO *ci)
298 WND *w = WIN_FindWndPtr(child);
299 LPRECT lprect = &ci->rectRestore;
301 printf("restoring mdi child\n");
303 child = ci->hwndActiveChild;
305 w->dwStyle &= ~WS_MAXIMIZE;
306 SetWindowPos(child, 0, lprect->left, lprect->top,
307 lprect->right - lprect->left + 1,
308 lprect->bottom - lprect->top + 1,
309 SWP_NOACTIVATE | SWP_NOZORDER);
311 ci->flagChildMaximized = FALSE;
313 ShowWindow(child, SW_RESTORE); /* display the window */
314 SendMessage(GetParent(parent), WM_NCPAINT, 1, 0);
315 MDIBringChildToTop(parent, child, FALSE, FALSE);
320 /**********************************************************************
323 LONG MDIChildActivated(WND *w, MDICLIENTINFO *ci, HWND parent)
331 fprintf(stderr, "MDIChildActivate: top %04.4x\n", w->hwndChild);
334 chi = ci->infoActiveChildren;
337 deact_hwnd = ci->hwndActiveChild;
338 act_hwnd = chi->hwnd;
339 lParam = ((LONG) deact_hwnd << 16) | act_hwnd;
342 fprintf(stderr, "MDIChildActivate: deact %04.4x, act %04.4x\n",
343 deact_hwnd, act_hwnd);
346 ci->hwndActiveChild = act_hwnd;
348 if (deact_hwnd != act_hwnd)
350 MDIRecreateMenuList(ci);
351 SendMessage(deact_hwnd, WM_NCACTIVATE, FALSE, 0);
352 SendMessage(deact_hwnd, WM_MDIACTIVATE, FALSE, lParam);
355 SendMessage(act_hwnd, WM_NCACTIVATE, TRUE, 0);
356 SendMessage(act_hwnd, WM_MDIACTIVATE, TRUE, lParam);
359 if (chi || ci->nActiveChildren == 0)
361 MDIRecreateMenuList(ci);
362 SendMessage(GetParent(parent), WM_NCPAINT, 0, 0);
368 /**********************************************************************
371 LONG MDICascade(HWND parent, MDICLIENTINFO *ci)
375 int spacing, xsize, ysize;
378 if (ci->flagChildMaximized)
379 MDIRestoreChild(parent, ci);
381 GetClientRect(parent, &rect);
382 spacing = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME);
383 ysize = rect.bottom - 8 * spacing;
384 xsize = rect.right - 8 * spacing;
388 "MDICascade: Client wnd at (%d,%d) - (%d,%d), spacing %d\n",
389 rect.left, rect.top, rect.right, rect.bottom, spacing);
390 fprintf(stderr, "MDICascade: searching for last child\n");
392 for (chi = ci->infoActiveChildren; chi->next != NULL; chi = chi->next)
396 fprintf(stderr, "MDICascade: last child is %04.4x\n", chi->hwnd);
400 for ( ; chi != NULL; chi = chi->prev)
403 fprintf(stderr, "MDICascade: move %04.4x to (%d,%d) size [%d,%d]\n",
404 chi->hwnd, x, y, xsize, ysize);
406 SetWindowPos(chi->hwnd, 0, x, y, xsize, ysize,
407 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
416 /**********************************************************************
419 LONG MDITile(HWND parent, MDICLIENTINFO *ci)
429 if (ci->flagChildMaximized)
430 MDIRestoreChild(parent, ci);
432 GetClientRect(parent, &rect);
433 rows = (int) sqrt((double) ci->nActiveChildren);
434 columns = ci->nActiveChildren / rows;
435 ysize = rect.bottom / rows;
436 xsize = rect.right / columns;
438 chi = ci->infoActiveChildren;
441 for (c = 1; c <= columns; c++)
445 rows = ci->nActiveChildren - i;
446 ysize = rect.bottom / rows;
450 for (r = 1; r <= rows; r++, i++, chi = chi->next)
452 SetWindowPos(chi->hwnd, 0, x, y, xsize, ysize,
453 SWP_DRAWFRAME | SWP_NOACTIVATE | SWP_NOZORDER);
465 /**********************************************************************
468 BOOL MDIHandleLButton(HWND hwndFrame, HWND hwndClient,
469 WORD wParam, LONG lParam)
476 w = WIN_FindWndPtr(hwndClient);
477 ci = (MDICLIENTINFO *) w->wExtra;
479 if (wParam == HTMENU && ci->flagChildMaximized)
483 NC_GetInsideRect(hwndFrame, &rect);
484 if (x < rect.left + SYSMETRICS_CXSIZE)
486 SendMessage(ci->hwndActiveChild, WM_SYSCOMMAND,
490 else if (x >= rect.right - SYSMETRICS_CXSIZE)
492 SendMessage(ci->hwndActiveChild, WM_SYSCOMMAND,
501 /**********************************************************************
504 LONG MDIPaintMaximized(HWND hwndFrame, HWND hwndClient, WORD message,
505 WORD wParam, LONG lParam)
507 static HBITMAP hbitmapClose = 0;
508 static HBITMAP hbitmapMaximized = 0;
515 WND *wndPtr = WIN_FindWndPtr(hwndFrame);
517 w = WIN_FindWndPtr(hwndClient);
518 ci = (MDICLIENTINFO *) w->wExtra;
522 "MDIPaintMaximized: frame %04x, client %04x"
523 ", max flag %d, menu %04x\n",
524 hwndFrame, hwndClient,
525 ci->flagChildMaximized, wndPtr ? wndPtr->wIDmenu : 0);
528 if (ci->flagChildMaximized && wndPtr && wndPtr->wIDmenu != 0)
530 rv = NC_DoNCPaint( hwndFrame, (HRGN) 1, wParam, TRUE);
532 hdc = GetDCEx(hwndFrame, 0, DCX_CACHE | DCX_WINDOW);
536 hdcMem = CreateCompatibleDC(hdc);
538 if (hbitmapClose == 0)
540 hbitmapClose = LoadBitmap(0, MAKEINTRESOURCE(OBM_OLD_CLOSE));
541 hbitmapMaximized = LoadBitmap(0, MAKEINTRESOURCE(OBM_RESTORE));
546 "MDIPaintMaximized: hdcMem %04x, close bitmap %04x, "
547 "maximized bitmap %04x\n",
548 hdcMem, hbitmapClose, hbitmapMaximized);
551 NC_GetInsideRect(hwndFrame, &rect);
552 rect.top += ((wndPtr->dwStyle & WS_CAPTION) ?
553 SYSMETRICS_CYSIZE + 1 : 0);
554 SelectObject(hdcMem, hbitmapClose);
555 BitBlt(hdc, rect.left, rect.top + 1,
556 SYSMETRICS_CXSIZE, SYSMETRICS_CYSIZE,
557 hdcMem, 1, 1, SRCCOPY);
559 NC_GetInsideRect(hwndFrame, &rect);
560 rect.top += ((wndPtr->dwStyle & WS_CAPTION) ?
561 SYSMETRICS_CYSIZE + 1 : 0);
562 rect.left = rect.right - SYSMETRICS_CXSIZE;
563 SelectObject(hdcMem, hbitmapMaximized);
564 BitBlt(hdc, rect.left, rect.top + 1,
565 SYSMETRICS_CXSIZE, SYSMETRICS_CYSIZE,
566 hdcMem, 1, 1, SRCCOPY);
568 NC_GetInsideRect(hwndFrame, &rect);
569 rect.top += ((wndPtr->dwStyle & WS_CAPTION) ?
570 SYSMETRICS_CYSIZE + 1 : 0);
571 rect.left += SYSMETRICS_CXSIZE;
572 rect.right -= SYSMETRICS_CXSIZE;
573 rect.bottom = rect.top + SYSMETRICS_CYMENU;
575 MENU_DrawMenuBar(hdc, &rect, hwndFrame, FALSE);
578 ReleaseDC(hwndFrame, hdc);
581 DefWindowProc(hwndFrame, message, wParam, lParam);
586 /**********************************************************************
589 * This function is the handler for all MDI requests.
592 MDIClientWndProc(HWND hwnd, WORD message, WORD wParam, LONG lParam)
595 LPCLIENTCREATESTRUCT ccs;
599 w = WIN_FindWndPtr(hwnd);
600 ci = (MDICLIENTINFO *) w->wExtra;
604 case WM_CHILDACTIVATE:
605 return MDIChildActivated(w, ci, hwnd);
608 cs = (LPCREATESTRUCT) lParam;
609 ccs = (LPCLIENTCREATESTRUCT) cs->lpCreateParams;
610 ci->hWindowMenu = ccs->hWindowMenu;
611 ci->idFirstChild = ccs->idFirstChild;
612 ci->infoActiveChildren = NULL;
613 ci->flagMenuAltered = FALSE;
614 ci->flagChildMaximized = FALSE;
615 w->dwStyle |= WS_CLIPCHILDREN;
617 GetClientRect(w->hwndParent, &ci->rectMaximize);
618 MoveWindow(hwnd, 0, 0,
619 ci->rectMaximize.right, ci->rectMaximize.bottom, 1);
624 MDIBringChildToTop(hwnd, wParam, FALSE, FALSE);
628 return MDICascade(hwnd, ci);
631 return MDICreateChild(w, ci, hwnd, (LPMDICREATESTRUCT) lParam);
634 return MDIDestroyChild(w, ci, hwnd, wParam, TRUE);
636 case WM_MDIGETACTIVE:
637 return ((LONG) ci->hwndActiveChild |
638 ((LONG) ci->flagChildMaximized << 16));
640 case WM_MDIICONARRANGE:
641 /* return MDIIconArrange(...) */
645 return MDIMaximizeChild(hwnd, wParam, ci);
648 MDIBringChildToTop(hwnd, wParam, FALSE, TRUE);
652 return MDIRestoreChild(hwnd, ci);
655 /* return MDISetMenu(...) */
659 return MDITile(hwnd, ci);
662 SendMessage(ci->hwndActiveChild, message, wParam, lParam);
665 case WM_PARENTNOTIFY:
666 if (wParam == WM_DESTROY)
667 return MDIDestroyChild(w, ci, hwnd, LOWORD(lParam), FALSE);
668 else if (wParam == WM_LBUTTONDOWN)
669 MDIBringChildToTop(hwnd, ci->hwndHitTest, FALSE, FALSE);
673 GetClientRect(w->hwndParent, &ci->rectMaximize);
678 return DefWindowProc(hwnd, message, wParam, lParam);
681 /**********************************************************************
682 * DefFrameProc (USER.445)
686 DefFrameProc(HWND hwnd, HWND hwndMDIClient, WORD message,
687 WORD wParam, LONG lParam)
694 MDIBringChildToTop(hwndMDIClient, wParam, TRUE, FALSE);
697 case WM_NCLBUTTONDOWN:
698 if (MDIHandleLButton(hwnd, hwndMDIClient, wParam, lParam))
703 SendMessage(hwndMDIClient, message, wParam, lParam);
704 return MDIPaintMaximized(hwnd, hwndMDIClient,
705 message, wParam, lParam);
708 return MDIPaintMaximized(hwnd, hwndMDIClient,
709 message, wParam, lParam);
712 SendMessage(hwndMDIClient, WM_SETFOCUS, wParam, lParam);
716 MoveWindow(hwndMDIClient, 0, 0,
717 LOWORD(lParam), HIWORD(lParam), TRUE);
722 return DefWindowProc(hwnd, message, wParam, lParam);
725 /**********************************************************************
726 * DefMDIChildProc (USER.447)
730 DefMDIChildProc(HWND hwnd, WORD message, WORD wParam, LONG lParam)
735 w = WIN_FindWndPtr(GetParent(hwnd));
736 ci = (MDICLIENTINFO *) w->wExtra;
741 ci->hwndHitTest = hwnd;
745 return NC_DoNCPaint(hwnd, (HRGN)1,
746 hwnd == ci->hwndActiveChild);
752 return SendMessage(GetParent(hwnd), WM_MDIMAXIMIZE, hwnd, 0);
755 return SendMessage(GetParent(hwnd), WM_MDIRESTORE, hwnd, 0);
761 return DefWindowProc(hwnd, message, wParam, lParam);
764 /**********************************************************************
765 * TranslateMDISysAccel (USER.451)
768 BOOL TranslateMDISysAccel(HWND hwndClient, LPMSG msg)