home *** CD-ROM | disk | FTP | other *** search
- #include <windows.h> // required for all Windows applications
- #include "resource.h" // Windows resource IDs
- #include "3dshell.h" // specific to this program
- #include <stdio.h>
- #include "QD3DWinViewer.h"
-
- HINSTANCE hInst; // current instance
- HWND gHwnd; // main hwnd
-
- TQ3WinViewerObject gViewer;
-
- char szAppName[] = "QuickDraw 3D Viewer Demo"; // The name of this application
- char szTitle[] = "QuickDraw 3D Viewer Demo"; // The title bar text
-
- void DoFlagCommand (unsigned long flag);
-
- BOOL OpenModelFile();
-
- TQ3Status BrowseForPathName(char *inPathName, BOOLEAN fOpen);
-
- int CALLBACK WinMain(
- HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
-
- MSG msg;
- HANDLE hAccelTable;
-
- if (!InitApplication(hInstance))
- {
- return (FALSE);
- }
-
- if (!InitInstance(hInstance, nCmdShow))
- {
- return (FALSE);
- }
-
- hAccelTable = LoadAccelerators (hInstance, MAKEINTRESOURCE(IDR_GENERIC));
-
- if( FALSE == OpenModelFile() ) // if the user cancels the open dialog exit
- {
- DestroyWindow(gHwnd);
- }
-
- while (GetMessage(&msg, NULL, 0, 0)) {
- if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
-
- return (msg.wParam); // Returns the value from PostQuitMessage
- }
-
-
- BOOL InitApplication(HINSTANCE hInstance)
- {
- WNDCLASS wc;
- ATOM atom;
-
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC)WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_APP));
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = MAKEINTRESOURCE(IDR_GENERIC);
- wc.lpszClassName = szAppName;
-
- // The followin call is only temporary
- atom = Q3WinInitializeViewerWindowClass (hInstance);
- return (RegisterClass(&wc));
- }
-
- BOOL InitInstance(
- HINSTANCE hInstance,
- int nCmdShow)
- {
-
- hInst = hInstance;
- gHwnd = CreateWindowEx(
- WS_EX_WINDOWEDGE|WS_EX_CLIENTEDGE, // extended window style
- szAppName, // pointer to registered class name
- szTitle, // pointer to window name
- WS_OVERLAPPED | WS_CAPTION |WS_THICKFRAME | WS_SYSMENU, // window style
- 25, 25, 400, 450, // fixed size windows
- NULL, // handle to parent or owner window
- NULL, // handle to menu, or child-window identifier
- hInstance, // handle to application instance
- NULL // pointer to window-creation data
- );
-
- if (!gHwnd)
- {
- return (FALSE);
- }
-
-
- ShowWindow(gHwnd, nCmdShow);
- UpdateWindow(gHwnd);
-
- return( TRUE );
- }
-
- void DoFlagCommand (unsigned long flag )
- {
- unsigned long flags;
- RECT aRect;
- TQ3Status status;
-
- if( NULL == gViewer )
- return;
-
- flags = Q3WinViewerGetFlags (gViewer);
-
- if (flags & flag)
- {
- flags = flags & ~flag;
- }
- else
- {
- flags = flags | flag;
- }
-
- status = Q3WinViewerSetFlags (gViewer, flags);
-
- (void) GetClientRect(gHwnd, (LPRECT)&aRect);
- (void) InvalidateRect(gHwnd, &aRect, FALSE);
-
- }
-
- void InitFlagMenu ( void )
- {
- HMENU hMenu;
- unsigned long flags;
-
- if( NULL == gViewer )
- return;
-
- flags = Q3WinViewerGetFlags (gViewer);
- hMenu = GetMenu( gHwnd );
-
- if (flags & kQ3ViewerControllerVisible)
- CheckMenuItem( hMenu, IDM_CONTROLSTRIP, MF_BYCOMMAND | MF_CHECKED );
- else
- CheckMenuItem( hMenu, IDM_CONTROLSTRIP, MF_BYCOMMAND | MF_UNCHECKED );
-
- if (flags & kQ3ViewerButtonCamera)
- CheckMenuItem( hMenu, IDM_CAMERABUTTON, MF_BYCOMMAND | MF_CHECKED );
- else
- CheckMenuItem( hMenu, IDM_CAMERABUTTON, MF_BYCOMMAND | MF_UNCHECKED );
-
- if (flags & kQ3ViewerButtonTruck)
- CheckMenuItem( hMenu, IDM_TRUCKBUTTON, MF_BYCOMMAND | MF_CHECKED );
- else
- CheckMenuItem( hMenu, IDM_TRUCKBUTTON, MF_BYCOMMAND | MF_UNCHECKED );
-
- if (flags & kQ3ViewerButtonOrbit)
- CheckMenuItem( hMenu, IDM_ORBITBUTTON, MF_BYCOMMAND | MF_CHECKED );
- else
- CheckMenuItem( hMenu, IDM_ORBITBUTTON, MF_BYCOMMAND | MF_UNCHECKED );
-
- if (flags & kQ3ViewerButtonDolly)
- CheckMenuItem( hMenu, IDM_DOLLYBUTTON, MF_BYCOMMAND | MF_CHECKED );
- else
- CheckMenuItem( hMenu, IDM_DOLLYBUTTON, MF_BYCOMMAND | MF_UNCHECKED );
-
- if (flags & kQ3ViewerButtonReset)
- CheckMenuItem( hMenu, IDM_RESETBUTTON, MF_BYCOMMAND | MF_CHECKED );
- else
- CheckMenuItem( hMenu, IDM_RESETBUTTON, MF_BYCOMMAND | MF_UNCHECKED );
-
- }
-
- LRESULT CALLBACK WndProc(
- HWND hWnd,
- UINT message,
- WPARAM uParam,
- LPARAM lParam)
- {
- int wmId, wmEvent;
-
- switch (message)
- {
- case WM_COMMAND:
-
- wmId = LOWORD(uParam);
- wmEvent = HIWORD(uParam);
-
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst,
- MAKEINTRESOURCE(IDD_ABOUTBOX),
- hWnd,
- (DLGPROC)About);
- break;
- case IDM_OPEN:
- (void) OpenModelFile();
- break;
- case IDM_SAVEAS:
- // SaveModelFile();
- break;
-
- case IDM_EXIT:
- DestroyWindow (hWnd);
- break;
-
- case IDM_CONTROLSTRIP:
- DoFlagCommand (kQ3ViewerControllerVisible);
- break;
-
- case IDM_CAMERABUTTON:
- DoFlagCommand (kQ3ViewerButtonCamera);
- break;
-
- case IDM_TRUCKBUTTON:
- DoFlagCommand (kQ3ViewerButtonTruck);
-
- case IDM_ORBITBUTTON:
- DoFlagCommand (kQ3ViewerButtonOrbit);
- break;
-
- case IDM_DOLLYBUTTON:
- DoFlagCommand (kQ3ViewerButtonDolly);
- break;
-
- case IDM_RESETBUTTON:
- DoFlagCommand (kQ3ViewerButtonReset);
- break;
-
- default:
- return (DefWindowProc(hWnd, message, uParam, lParam));
- }
- break;
-
- case WM_INITMENU:
- {
- InitFlagMenu();
- }
- break;
-
- case WM_CREATE:
- {
- RECT aWinRect;
- GetClientRect(hWnd, (LPRECT)&aWinRect);
-
- gViewer = Q3WinViewerNew (hInst, hWnd, &aWinRect, (unsigned long) kQ3ViewerDefault);
-
- if( gViewer == NULL )
- return -1;
- }
- break;
-
- case WM_DESTROY: // message: window being destroyed
- PostQuitMessage(0);
- break;
-
- case WM_SIZE:
- {
- long width = LOWORD(lParam);
- long height = HIWORD(lParam);
- RECT rect;
-
- rect.top = 0;
- rect.left = 0;
- rect.right = width;
- rect.bottom = height;
- Q3WinViewerSetBounds (gViewer, &rect);
- }
- break;
-
- case WM_SETFOCUS:
- SetFocus( Q3WinViewerGetWindow( gViewer ) );
- break;
-
- default: // Passes it on if unproccessed
- return (DefWindowProc(hWnd, message, uParam, lParam));
- }
- return (0);
- }
-
-
- char ExtFilter[] = "QuickDraw 3D Metafiles\0*.3dmf;*.3dm;*.q3d;*.qd3d\0All Files\0*.*\0\0";
-
- // put up common dialog; fOpen == TRUE for Open, FALSE for Save As.
- BOOL BrowseForPathName(char *inPathName, BOOLEAN fOpen)
- {
- OPENFILENAME aFileName;
- BOOL aStatus = TRUE;
-
- inPathName[0] = 0;
- aFileName.lStructSize = sizeof(OPENFILENAME);
- aFileName.hwndOwner = gHwnd;
- aFileName.hInstance = hInst;
- aFileName.lpstrFilter = ExtFilter;
- aFileName.lpstrCustomFilter = NULL;
- aFileName.nMaxCustFilter = 0L;
- aFileName.nFilterIndex = 0;
- aFileName.lpstrFile = inPathName;
- aFileName.nMaxFile = 255;
- aFileName.lpstrFileTitle = NULL;
- aFileName.nMaxFileTitle = 0;
- aFileName.lpstrInitialDir = NULL;
- aFileName.lpstrTitle = NULL;
- aFileName.Flags = OFN_EXPLORER + OFN_LONGNAMES + OFN_PATHMUSTEXIST;
- if( fOpen )
- aFileName.Flags += OFN_FILEMUSTEXIST;
- aFileName.nFileOffset = 0;
- aFileName.nFileExtension = 0;
- aFileName.lpstrDefExt = NULL;
- aFileName.lCustData = 0;
- aFileName.lpfnHook = NULL;
- aFileName.lpTemplateName = NULL;
- if (fOpen )
- if (GetOpenFileName((LPOPENFILENAME)&aFileName))
- aStatus = TRUE;
- else
- aStatus = FALSE;
- else //saving
- if (GetSaveFileName((LPOPENFILENAME)&aFileName))
- aStatus = TRUE;
- else
- aStatus = FALSE;
-
- return aStatus;
- }
-
- BOOL OpenModelFile()
- {
- char *pathName;
- char pathNameChars[255];
- TQ3Status aStatus;
- RECT aWinRect;
-
- pathName = &(pathNameChars[0]);
- if (BrowseForPathName(pathName, TRUE) != kQ3Success)
- return FALSE;
-
- aStatus = Q3WinViewerUseUnixPath (gViewer, pathName);
- (void) GetClientRect(gHwnd, (LPRECT)&aWinRect);
- (void) InvalidateRect(gHwnd, &aWinRect, FALSE);
- return TRUE;
- }
-
-
- BOOL CenterWindow (HWND hwndChild, HWND hwndParent)
- {
- RECT rChild, rParent;
- int wChild, hChild, wParent, hParent;
- int wScreen, hScreen, xNew, yNew;
- HDC hdc;
-
- // Get the Height and Width of the child window
- GetWindowRect (hwndChild, &rChild);
- wChild = rChild.right - rChild.left;
- hChild = rChild.bottom - rChild.top;
-
- // Get the Height and Width of the parent window
- GetWindowRect (hwndParent, &rParent);
- wParent = rParent.right - rParent.left;
- hParent = rParent.bottom - rParent.top;
-
- // Get the display limits
- hdc = GetDC (hwndChild);
- wScreen = GetDeviceCaps (hdc, HORZRES);
- hScreen = GetDeviceCaps (hdc, VERTRES);
- ReleaseDC (hwndChild, hdc);
-
- // Calculate new X position, then adjust for screen
- xNew = rParent.left + ((wParent - wChild) /2);
- if (xNew < 0) {
- xNew = 0;
- } else if ((xNew+wChild) > wScreen) {
- xNew = wScreen - wChild;
- }
-
- // Calculate new Y position, then adjust for screen
- yNew = rParent.top + ((hParent - hChild) /2);
- if (yNew < 0) {
- yNew = 0;
- } else if ((yNew+hChild) > hScreen) {
- yNew = hScreen - hChild;
- }
-
- // Set it, and return
- return SetWindowPos (hwndChild, NULL,
- xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
- }
-
-
- // About box callback
- LRESULT CALLBACK About(
- HWND hDlg, // window handle of the dialog box
- UINT message, // type of message
- WPARAM uParam, // message-specific information
- LPARAM lParam)
- {
- static HFONT hfontDlg;
- LPSTR lpVersion;
- DWORD dwVerInfoSize;
- DWORD dwVerHnd;
- UINT uVersionLen;
- WORD wRootLen;
- BOOL bRetCode;
- int i;
- char szFullPath[256];
- char szResult[256];
- char szGetName[256];
-
- switch (message) {
- case WM_INITDIALOG: // message: initialize dialog box
- // Center the dialog over the application window
- CenterWindow (hDlg, GetWindow (hDlg, GW_OWNER));
-
- // Get version information from the application
- GetModuleFileName (hInst, szFullPath, sizeof(szFullPath));
- dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
- if (dwVerInfoSize) {
- // If we were able to get the information, process it:
- LPSTR lpstrVffInfo;
- HANDLE hMem;
- hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
- lpstrVffInfo = GlobalLock(hMem);
- GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpstrVffInfo);
- lstrcpy(szGetName, "\\StringFileInfo\\040904e4\\");
- wRootLen = lstrlen(szGetName);
-
- // Walk through the dialog items that we want to replace:
- for (i = IDC_FILEDESCRIPTION; i <= IDC_LEGALTRADEMARKS; i++) {
- GetDlgItemText(hDlg, i, szResult, sizeof(szResult));
- szGetName[wRootLen] = (char)0;
- lstrcat (szGetName, szResult);
- uVersionLen = 0;
- lpVersion = NULL;
- bRetCode = VerQueryValue((LPVOID)lpstrVffInfo,
- (LPSTR)szGetName,
- (LPVOID)&lpVersion,
- (LPDWORD)&uVersionLen); // For MIPS strictness
-
- if ( bRetCode && uVersionLen && lpVersion) {
- // Replace dialog item text with version info
- lstrcpy(szResult, lpVersion);
- SetDlgItemText(hDlg, i, szResult);
- }
- }
-
- GlobalUnlock(hMem);
- GlobalFree(hMem);
- } // if (dwVerInfoSize)
- return (TRUE);
-
- case WM_COMMAND: // message: received a command
- if (LOWORD(uParam) == IDOK // "OK" box selected?
- || LOWORD(uParam) == IDCANCEL) { // System menu close command?
- EndDialog(hDlg, TRUE); // Exit the dialog
- DeleteObject (hfontDlg);
- return (TRUE);
- }
- break;
- }
- return (FALSE); // Didn't process the message
- }
-
-
-