home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- Module name: MDI.C
- Programmer : Jeffrey M. Richter & Elvira Peretsman.
- *****************************************************************************/
-
- #include "..\nowindws.h"
- #undef NOCTLMGR
- #undef NOGDI
- #undef NOKERNEL
- #undef NOMB
- #undef NOMDI
- #undef NOMENUS
- #undef NOMSG
- #undef NOSYSMETRICS
- #undef NOUSER
- #undef NOWINMESSAGES
- #undef NOWINSTYLES
- #include <windows.h>
-
- #include "mdi.h"
-
-
- // Application global variables.
- char _szAppName[] = "MDI"; // The name of the application.
- HANDLE _hInstance = NULL; // Data instance of the application.
- HANDLE _hAccelTable = NULL; // Handle to active Accelerator table.
- HWND _hWndMDIClient = NULL; // Handle to MDICLIENT window.
- HWND _hDlgRibbon = NULL; // Handle to Ribbon modeless dialog box.
-
- /************************* Main Application Loop ****************************/
-
- // NEAR or FAR dependant on compilation model S or M respectively.
- int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
- LPSTR lpszCmdLine, int nCmdShow) {
- MSG msg;
- FARPROC fpProc;
- HWND hWndFrame;
-
- if (hPrevInstance != NULL) {
- // Only allow one instance of the application to run.
- MessageBox(NULL, "MDI application is already running.", _szAppName,
- MB_OK | MB_ICONINFORMATION);
- return(0);
- }
-
- _hInstance = hInstance;
-
- // Register the Frame window class.
- if (!RegisterFrameWndClass()) return(0);
-
- // Register the MDI Child window classes.
- if (!RegisterSheetWndClass()) return(0);
- if (!RegisterChartWndClass()) return(0);
-
- // Create the Frame window.
- hWndFrame = CreateWindow("Frame", _szAppName,
- WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_MAXIMIZE | WS_VISIBLE |
- WS_MAXIMIZEBOX | WS_MINIMIZEBOX,
- CW_USEDEFAULT, nCmdShow, CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL, _hInstance, NULL);
- if (hWndFrame == NULL) return(0);
-
- // Create the Ribbon dialog box with Frame as owner.
- fpProc = MakeProcInstance(RibbonDlgProc, _hInstance);
- _hDlgRibbon = CreateDialog(_hInstance, "RIBBON", hWndFrame, fpProc);
- if (_hDlgRibbon == NULL) {
- FreeProcInstance(fpProc);
- return(0);
- }
-
- while (GetMessage(&msg, NULL, 0, 0)) {
- if (!TranslateMDISysAccel(_hWndMDIClient, &msg)) {
- if (_hAccelTable == NULL || !TranslateAccelerator(hWndFrame, _hAccelTable, &msg)) {
- if (!IsDialogMessage(_hDlgRibbon, &msg)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- }
- }
-
- FreeProcInstance(fpProc);
- return(msg.wParam);
- }
-
- // Function to make creating MDI Child as easy as creating any other
- // kind of window.
- HWND FAR PASCAL CreateMDIChild (LPSTR szClassName, LPSTR szWindowName,
- DWORD dwStyle, short x, short y, short nWidth, short nHeight,
- HWND hWndMDIClient, HANDLE hInstance, LONG lParam) {
- MDICREATESTRUCT cs;
- HWND hWndChild;
-
- cs.szClass = szClassName;
- cs.szTitle = szWindowName;
- cs.hOwner = hInstance;
- cs.x = x;
- cs.y = y;
- cs.cx = nWidth;
- cs.cy = nHeight;
- cs.style = dwStyle;
- cs.lParam = lParam;
- hWndChild = (HWND) SendMessage(hWndMDIClient, WM_MDICREATE,
- 0, (LONG) (LPMDICREATESTRUCT) &cs);
- return(hWndChild);
- }
-
-
- // Function to change the menu in the Frame window whenever a
- // new MDI Child becomes active.
- void FAR PASCAL ChangeMDIMenu (HWND hWndFrame, HWND hWndClient,
- HMENU hMenuNew, WORD wMenuID) {
- WORD wCount;
- HMENU hSubMenu = 0;
-
- // Get number of top-level menu items in the menu used by the window
- // being activated.
- wCount = GetMenuItemCount(hMenuNew);
-
- // Locate the POPUP menu that contains the menu option with the
- // 'wMenuID' identifier in it. This must be an identifier for an option
- // in the new menu's "Window" popup menu.
- while (wCount) {
- hSubMenu = GetSubMenu(hMenuNew, wCount - 1);
- if ((int) GetMenuState(hSubMenu, wMenuID, MF_BYCOMMAND) != -1)
- break;
- wCount--;
- }
-
- // Tell the MDICLIENT window to setup the new menu.
- SendMessage(hWndClient, WM_MDISETMENU, 0, MAKELONG(hMenuNew, hSubMenu));
-
- DrawMenuBar(hWndFrame);
- }
-
-
- // ***************************************************************************
- // This function processes all messages sent to the About dialog box.
-
- BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
- char szBuffer[100];
- BOOL fProcessed = TRUE;
-
- switch (wMsg) {
-
- case WM_INITDIALOG:
- // Set version static window to have date and time of compilation.
- wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
- SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
- break;
-
- case WM_COMMAND:
- switch (wParam) {
- case IDOK: case IDCANCEL:
- if (HIWORD(lParam) == BN_CLICKED)
- EndDialog(hDlg, wParam);
- break;
-
- default:
- break;
- }
- break;
-
- default:
- fProcessed = FALSE; break;
- }
- return(fProcessed);
- }
-
-
- BOOL FAR PASCAL RibbonDlgProc (HWND hDlg, WORD wMsg, WORD wParam, DWORD dwParam) {
- BOOL fProcessed = TRUE;
- HPEN hPen;
- RECT rc;
- PAINTSTRUCT ps;
- HWND hCtl;
- int i;
- char szBuf[25];
-
- switch (wMsg) {
-
- case WM_INITDIALOG:
- // Add strings to the font combobox.
- hCtl = GetDlgItem(hDlg, ID_FONT);
- i = IDS_FONT;
- while (LoadString(_hInstance, i++, szBuf, sizeof(szBuf)) != 0)
- SendMessage(hCtl, CB_ADDSTRING, 0, (LONG) (LPSTR) szBuf);
- SendMessage(hCtl, CB_SETCURSEL, 0, 0);
-
- // Add strings to the fontsize combobox.
- hCtl = GetDlgItem(hDlg, ID_SIZE);
- i = IDS_SIZE;
- while (LoadString(_hInstance, i++, szBuf, sizeof(szBuf)) != 0)
- SendMessage(hCtl, CB_ADDSTRING, 0, (LONG) (LPSTR) szBuf);
- SendMessage(hCtl, CB_SETCURSEL, 0, 0);
- break;
-
- case WM_ENABLE:
- // Make all child windows have the same status as the dialog box.
- hCtl = GetWindow(hDlg, GW_CHILD);
- while (hCtl != NULL) {
- EnableWindow(hCtl, wParam);
- hCtl = GetWindow(hCtl, GW_HWNDNEXT);
- }
- break;
-
- case WM_PAINT:
- // Paint a horizontal dividing line between the Ribbon and the
- // MDICLIENT window.
- BeginPaint(hDlg, &ps);
- hPen = CreatePen(PS_SOLID, GetSystemMetrics(SM_CYBORDER),
- RGB(0, 0, 0));
- SelectObject(ps.hdc, hPen);
- GetClientRect(hDlg, &rc);
- MoveTo(ps.hdc, 0, rc.bottom - GetSystemMetrics(SM_CYBORDER));
- LineTo(ps.hdc, rc.right, rc.bottom - GetSystemMetrics(SM_CYBORDER));
- EndPaint(hDlg, &ps);
- DeleteObject(hPen);
- break;
-
-
- case WM_COMMAND:
- // Make sure that focus is given back to the Frame after an
- // option is chosen by the user.
-
- switch (wParam) {
- case ID_FONT:
- case ID_SIZE:
- if (HIWORD(dwParam) != CBN_SELCHANGE) break;
- SetFocus(GetParent(hDlg));
- break;
-
- case ID_BOLD:
- case ID_ITALIC:
- case ID_UNDERLINE:
- if (HIWORD(dwParam) != BN_CLICKED) break;
- SetFocus(GetParent(hDlg));
- break;
-
- case IDOK:
- case IDCANCEL:
- SetFocus(GetParent(hDlg));
- break;
- }
- break;
-
- default:
- fProcessed = FALSE;
- break;
- }
- return(fProcessed);
- }
-