/***************************************************************************** * * Copyright (c) 1999, KL GROUP INC. All Rights Reserved. * http://www.klgroup.com * * This file is provided for demonstration and educational uses only. * Permission to use, copy, modify and distribute this file for * any purpose and without fee is hereby granted, provided that the * above copyright notice and this permission notice appear in all * copies, and that the name of KL Group not be used in advertising * or publicity pertaining to this material without the specific, * prior written permission of an authorized representative of * KL Group. * * KL GROUP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. KL GROUP SHALL NOT BE LIABLE FOR ANY * DAMAGES SUFFERED BY USERS AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ #include #include #include #include #include "olch3d.h" #include "resource.h" Xrt3dData* pData3d = NULL; HANDLE ghInst; HWND gMainHwnd; HXRT3D hChart; HBRUSH hbrushBack; HANDLE hDrawMesh; HANDLE hDrawShaded; HANDLE hDrawContours; HANDLE hDrawZones; /* Create the data area and calculate the 3d data for the chart */ Xrt3dData* CalculateGrid() { int i, j; int nRows = 20; int nColumns = 20; double x, y; /* Create a regular grid with (x,y) origins at (-3,-3) */ Xrt3dGridData* pGridData = (Xrt3dGridData*) Xrt3dMakeGridData(nRows, nColumns, XRT3D_HUGE_VAL, 8.0 / (nRows - 1), 8.0 / (nColumns - 1), -3.0, -3.0, TRUE); if (!pGridData) { return (Xrt3dData*) NULL; } /* Populate the grid with data */ for (i = 0; i < nRows; i++) { x = pGridData->xorig + i * pGridData->xstep; for (j = 0; j < nColumns; j++) { y = pGridData->yorig + j * pGridData->ystep; pGridData->values[i][j] = 3*x*y - x*x*x - y*y*y; } } return (Xrt3dData*) pGridData; } /* Create all the controls (the chart and the check boxes) used in the main window */ void CreateControls(HWND parent) { HDC hdc; TEXTMETRIC tm; int cxChar, cyChar; int controlWidth; COLORREF crBack; hdc = GetDC (parent) ; SetMapMode(hdc, MM_TEXT); SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ; cyChar = tm.tmHeight + tm.tmExternalLeading ; controlWidth = cxChar * 16; ReleaseDC (parent, hdc) ; /* * Create the 3d chart and their controls. * The size and position of the chart will be recalculated * during the WM_SIZE message. */ /* WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, */ /* Chart */ hChart = Xrt3dCreateWindow("simple", 0, 0, 0, 0, parent, ghInst); /* Check boxes */ hDrawMesh = CreateWindow("BUTTON", "DrawMesh", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, cxChar, cyChar/2, controlWidth, cyChar, parent, NULL, ghInst, NULL); hDrawShaded = CreateWindow("BUTTON", "DrawShaded", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, cxChar+4+controlWidth, cyChar/2, controlWidth, cyChar, parent, NULL, ghInst, NULL); hDrawContours = CreateWindow("BUTTON", "DrawContours", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, cxChar+4+(controlWidth*2), cyChar/2, controlWidth, cyChar, parent, NULL, ghInst, NULL); hDrawZones = CreateWindow("BUTTON", "DrawZones", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, cxChar+4+(controlWidth*3), cyChar/2, controlWidth, cyChar, parent, NULL, ghInst, NULL); /* Get the current background color used by the demo and * use that color for the background of the rest of the window. */ Xrt3dGetValues(hChart, XRT3D_BACKGROUND_COLOR, &crBack, NULL); hbrushBack = CreateSolidBrush(crBack); #ifdef _WIN32 SetClassLong(gMainHwnd, GCL_HBRBACKGROUND, (long) hbrushBack); #else SetClassWord(gMainHwnd, GCW_HBRBACKGROUND, (WORD) hbrushBack); #endif } /* Resize/reposition the chart based on the new size of the main window */ void SizeChart(HWND hwnd, int width, int height) { HDC hdc; TEXTMETRIC tm; int cyChar; int chartHeight; int chartWidth; int controlHeight; hdc = GetDC(hwnd); SetMapMode(hdc, MM_TEXT); SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; GetTextMetrics (hdc, &tm) ; ReleaseDC(hwnd, hdc); cyChar = tm.tmHeight + tm.tmExternalLeading ; controlHeight = cyChar * 2; chartWidth = width; chartHeight = height - (controlHeight * 4); /* place chart in bottom portion of window */ SetWindowPos(Xrt3dGetWindow(hChart), HWND_BOTTOM, 0, (controlHeight * 2), chartWidth, chartHeight, SWP_SHOWWINDOW | SWP_NOZORDER); InvalidateRect(gMainHwnd, NULL, TRUE); } /* draw routine */ void DrawChart(HWND hwnd, HDC hdc) { TEXTMETRIC tm; RECT rect; HBRUSH hbrush; int cxChar, cyChar; int chartWidth; int controlHeight; SetMapMode(hdc, MM_TEXT); SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ; cyChar = tm.tmHeight + tm.tmExternalLeading ; controlHeight = cyChar * 2; GetClientRect(hwnd, &rect); chartWidth = rect.right - rect.left; /* place controls in top portion of window */ MoveToEx(hdc, 0, controlHeight, NULL); LineTo(hdc, chartWidth, controlHeight); #ifdef _WIN32 hbrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); #else hbrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW)); #endif rect.left = 0; rect.top = 0; rect.right = chartWidth; rect.bottom = controlHeight; FillRect(hdc, &rect, hbrush); DeleteObject(hbrush); ShowWindow(hDrawMesh, SW_SHOW); ShowWindow(hDrawShaded, SW_SHOW); ShowWindow(hDrawContours, SW_SHOW); ShowWindow(hDrawZones, SW_SHOW); } /* Main window WndProc */ LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HWND hwndCtrl; HDC hdc; PAINTSTRUCT ps; int nCheck; switch(msg) { case WM_COMMAND: /* handle commands */ switch(LOWORD(wParam)) { case IDM_EXIT: DestroyWindow(hwnd); break; case IDM_ABOUTDEMO: case IDMHELP: WinHelp(hwnd, "OLCH-DMO.HLP", HELP_CONTEXT, 40); break; case IDM_ABOUTOLECTRACHART: WinHelp(hwnd, "OLCH-DMO.HLP", HELP_CONTEXT, 19); break; /* handle messages from check buttons */ default: #ifdef _WIN32 hwndCtrl = (HWND)lParam; #else hwndCtrl = (HWND)LOWORD(lParam); #endif if (hwndCtrl == hDrawMesh) { nCheck = (int) SendMessage(hwndCtrl, BM_GETCHECK, 0, 0L); Xrt3dSetValues(hChart, XRT3D_DRAW_MESH, nCheck, NULL); } else if (hwndCtrl == hDrawShaded) { nCheck = (int) SendMessage(hwndCtrl, BM_GETCHECK, 0, 0L); Xrt3dSetValues(hChart, XRT3D_DRAW_SHADED, nCheck, NULL); } else if (hwndCtrl == hDrawContours) { nCheck = (int) SendMessage(hwndCtrl, BM_GETCHECK, 0, 0L); Xrt3dSetValues(hChart, XRT3D_DRAW_CONTOURS, nCheck, NULL); } else if (hwndCtrl == hDrawZones) { nCheck = (int) SendMessage(hwndCtrl, BM_GETCHECK, 0, 0L); Xrt3dSetValues(hChart, XRT3D_DRAW_ZONES, nCheck, NULL); } break; } return(0); case WM_SIZE: /* resize the chart */ SizeChart(hwnd, (int) LOWORD(lParam), (int) HIWORD(lParam)); return(0); case WM_PAINT: hdc = BeginPaint(hwnd, &ps); DrawChart(hwnd, hdc); EndPaint(hwnd, &ps); break; /* * To properly handle colors the chart needs to be * notified about any system palette changes. */ case XRT3DN_PALETTECHANGED: SendMessage(Xrt3dGetWindow(hChart), WM_QUERYNEWPALETTE, 0, 0); break; case WM_QUERYNEWPALETTE: /* Make sure the window is at the top of the Z-order so that the background palettes can be realized before any other application gets a chance to realize a palette. */ /* In Windows for Workgroups, this message can be received before the window is moved to the top of the Z-order causing the first WM_PALETTECHANGED message to be sent not to this window but the window which was "previously" at the top of the Z-order. */ BringWindowToTop(hwnd); SendMessage(Xrt3dGetWindow(hChart), WM_QUERYNEWPALETTE, 0, 0); break; case WM_PALETTECHANGED: SendMessage(Xrt3dGetWindow(hChart), msg, wParam, lParam); break; case WM_DESTROY: PostQuitMessage(0); return(0); } return(DefWindowProc(hwnd, msg, wParam, lParam)); } /* Entry point for main program */ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; MSG msg; HACCEL hAccel; if (!hPrevInstance) { memset(&wc, 0, sizeof(wc)); wc.lpfnWndProc = (WNDPROC) MainWndProc; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, "simpleicon"); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetStockObject(BLACK_BRUSH); wc.lpszMenuName = (LPSTR) "SimpleMenu"; wc.lpszClassName = (LPSTR) "Chart3D_Simple"; if (!RegisterClass(&wc)) { MessageBox(NULL, "Cannot RegisterClass()", "Err! - TEST", MB_OK | MB_ICONEXCLAMATION); return(FALSE); } } hAccel = LoadAccelerators(hInstance, "SimpleAccelerators"); ghInst = hInstance; /* create main window */ gMainHwnd = CreateWindow("Chart3D_Simple", "Olectra Chart 3D - Simple 3D Plot Example", WS_OVERLAPPEDWINDOW, 0, 0, 700, 400, NULL, NULL, ghInst, NULL); if (!gMainHwnd) { return 0; } /* create all controls in main window */ CreateControls(gMainHwnd); /* calculate and set the 3d data for the chart */ pData3d = CalculateGrid(); Xrt3dSetValues(hChart, XRT3D_SURFACE_DATA, pData3d, NULL); /* Set the axis titles */ Xrt3dSetValues(hChart, XRT3D_XAXIS_TITLE, "X Axis", NULL); Xrt3dSetValues(hChart, XRT3D_YAXIS_TITLE, "Y Axis", NULL); Xrt3dSetValues(hChart, XRT3D_ZAXIS_TITLE, "Z Axis", NULL); /* Meshed is on by default */ SendMessage(hDrawMesh, BM_SETCHECK, 1, 0L); ShowWindow(gMainHwnd, nCmdShow); UpdateWindow(gMainHwnd); while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(gMainHwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } /* remember to free up memory */ Xrt3dDestroyData(pData3d, TRUE); Xrt3dDestroy(hChart); DeleteObject(hbrushBack); return(msg.wParam); }