home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / Windows / Samples / ViewerSample / 3DShell.c next >
Encoding:
C/C++ Source or Header  |  1996-05-03  |  14.2 KB  |  471 lines  |  [TEXT/LMAN]

  1. #include <windows.h>        // required for all Windows applications
  2. #include "resource.h"        // Windows resource IDs
  3. #include "3dshell.h"        // specific to this program
  4. #include <stdio.h>
  5. #include "QD3DWinViewer.h"
  6.  
  7. HINSTANCE hInst;              // current instance
  8. HWND gHwnd;                    // main hwnd
  9.  
  10. TQ3WinViewerObject        gViewer;
  11.  
  12. char szAppName[] = "QuickDraw 3D Viewer Demo";        // The name of this application
  13. char szTitle[]   = "QuickDraw 3D Viewer Demo";        // The title bar text
  14.  
  15. void DoFlagCommand (unsigned long flag);
  16.  
  17. BOOL        OpenModelFile();
  18.  
  19. TQ3Status    BrowseForPathName(char *inPathName, BOOLEAN fOpen);
  20.  
  21. int CALLBACK WinMain(
  22.         HINSTANCE hInstance,
  23.         HINSTANCE hPrevInstance,
  24.         LPSTR lpCmdLine,
  25.         int nCmdShow)
  26. {
  27.  
  28.         MSG msg;
  29.         HANDLE hAccelTable;
  30.  
  31.         if (!InitApplication(hInstance)) 
  32.         {
  33.             return (FALSE);    
  34.         }
  35.  
  36.         if (!InitInstance(hInstance, nCmdShow)) 
  37.         {
  38.             return (FALSE);
  39.         }
  40.  
  41.         hAccelTable = LoadAccelerators (hInstance, MAKEINTRESOURCE(IDR_GENERIC));
  42.  
  43.         if( FALSE == OpenModelFile() )     // if the user cancels the open dialog exit
  44.         {        
  45.             DestroyWindow(gHwnd);
  46.         }
  47.  
  48.         while (GetMessage(&msg, NULL, 0, 0)) {
  49.             if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg)) {
  50.                 TranslateMessage(&msg);
  51.                 DispatchMessage(&msg);
  52.             }
  53.         }
  54.  
  55.         return (msg.wParam); // Returns the value from PostQuitMessage
  56. }
  57.  
  58.  
  59. BOOL InitApplication(HINSTANCE hInstance)
  60. {
  61.         WNDCLASS  wc;
  62.         ATOM atom;
  63.         
  64.         wc.style         = CS_HREDRAW | CS_VREDRAW;
  65.         wc.lpfnWndProc   = (WNDPROC)WndProc;       
  66.         wc.cbClsExtra    = 0;                      
  67.         wc.cbWndExtra    = 0;                     
  68.         wc.hInstance     = hInstance;             
  69.         wc.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_APP)); 
  70.         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  71.         wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  72.         wc.lpszMenuName  = MAKEINTRESOURCE(IDR_GENERIC); 
  73.         wc.lpszClassName = szAppName;              
  74.         
  75.         // The followin call is only temporary
  76.         atom = Q3WinInitializeViewerWindowClass (hInstance);
  77.         return (RegisterClass(&wc));
  78. }
  79.  
  80. BOOL InitInstance(
  81.         HINSTANCE       hInstance,
  82.         int             nCmdShow)
  83. {
  84.  
  85.         hInst = hInstance; 
  86.         gHwnd =  CreateWindowEx(
  87.             WS_EX_WINDOWEDGE|WS_EX_CLIENTEDGE,    // extended window style
  88.             szAppName,    // pointer to registered class name
  89.             szTitle,    // pointer to window name
  90.             WS_OVERLAPPED | WS_CAPTION |WS_THICKFRAME | WS_SYSMENU,    // window style
  91.             25, 25, 400, 450,      // fixed size windows
  92.             NULL,    // handle to parent or owner window
  93.             NULL,    // handle to menu, or child-window identifier
  94.             hInstance,    // handle to application instance
  95.             NULL     // pointer to window-creation data
  96.         );
  97.  
  98.         if (!gHwnd) 
  99.         {
  100.             return (FALSE);
  101.         }
  102.  
  103.  
  104.         ShowWindow(gHwnd, nCmdShow); 
  105.         UpdateWindow(gHwnd);        
  106.  
  107.         return( TRUE );
  108. }
  109.  
  110. void DoFlagCommand (unsigned long flag )
  111. {
  112.     unsigned long flags;
  113.     RECT        aRect;
  114.     TQ3Status    status;
  115.  
  116.     if( NULL == gViewer )
  117.         return;
  118.  
  119.     flags = Q3WinViewerGetFlags (gViewer);
  120.  
  121.     if (flags & flag) 
  122.     {
  123.         flags = flags & ~flag;
  124.     }
  125.     else
  126.     {
  127.         flags = flags | flag;
  128.     }
  129.     
  130.     status = Q3WinViewerSetFlags (gViewer, flags);
  131.  
  132.     (void) GetClientRect(gHwnd, (LPRECT)&aRect);
  133.     (void) InvalidateRect(gHwnd, &aRect, FALSE);    
  134.  
  135. }
  136.  
  137. void InitFlagMenu ( void )
  138. {
  139.     HMENU hMenu;
  140.     unsigned long flags;
  141.  
  142.     if( NULL == gViewer )
  143.         return;
  144.  
  145.     flags = Q3WinViewerGetFlags (gViewer);
  146.     hMenu = GetMenu( gHwnd );
  147.  
  148.     if (flags & kQ3ViewerControllerVisible) 
  149.         CheckMenuItem( hMenu, IDM_CONTROLSTRIP, MF_BYCOMMAND | MF_CHECKED );
  150.     else
  151.         CheckMenuItem( hMenu, IDM_CONTROLSTRIP, MF_BYCOMMAND | MF_UNCHECKED );
  152.     
  153.     if (flags & kQ3ViewerButtonCamera) 
  154.         CheckMenuItem( hMenu, IDM_CAMERABUTTON, MF_BYCOMMAND | MF_CHECKED );
  155.     else
  156.         CheckMenuItem( hMenu, IDM_CAMERABUTTON, MF_BYCOMMAND | MF_UNCHECKED );
  157.  
  158.     if (flags & kQ3ViewerButtonTruck) 
  159.         CheckMenuItem( hMenu, IDM_TRUCKBUTTON, MF_BYCOMMAND | MF_CHECKED );
  160.     else
  161.         CheckMenuItem( hMenu, IDM_TRUCKBUTTON, MF_BYCOMMAND | MF_UNCHECKED );
  162.  
  163.     if (flags & kQ3ViewerButtonOrbit) 
  164.         CheckMenuItem( hMenu, IDM_ORBITBUTTON, MF_BYCOMMAND | MF_CHECKED );
  165.     else
  166.         CheckMenuItem( hMenu, IDM_ORBITBUTTON, MF_BYCOMMAND | MF_UNCHECKED );
  167.  
  168.     if (flags & kQ3ViewerButtonDolly) 
  169.         CheckMenuItem( hMenu, IDM_DOLLYBUTTON, MF_BYCOMMAND | MF_CHECKED );
  170.     else
  171.         CheckMenuItem( hMenu, IDM_DOLLYBUTTON, MF_BYCOMMAND | MF_UNCHECKED );
  172.  
  173.     if (flags & kQ3ViewerButtonReset) 
  174.         CheckMenuItem( hMenu, IDM_RESETBUTTON, MF_BYCOMMAND | MF_CHECKED );
  175.     else
  176.         CheckMenuItem( hMenu, IDM_RESETBUTTON, MF_BYCOMMAND | MF_UNCHECKED );
  177.     
  178. }
  179.  
  180. LRESULT CALLBACK WndProc(
  181.                 HWND hWnd,        
  182.                 UINT message,      
  183.                 WPARAM uParam,     
  184.                 LPARAM lParam)   
  185. {
  186.         int wmId, wmEvent;
  187.  
  188.         switch (message) 
  189.         {
  190.             case WM_COMMAND:
  191.  
  192.                 wmId    = LOWORD(uParam);
  193.                 wmEvent = HIWORD(uParam);
  194.  
  195.                 switch (wmId) 
  196.                 {
  197.                     case IDM_ABOUT:
  198.                         DialogBox(hInst,          
  199.                                 MAKEINTRESOURCE(IDD_ABOUTBOX),
  200.                                 hWnd,                 
  201.                                 (DLGPROC)About);
  202.                         break;
  203.                     case IDM_OPEN:
  204.                         (void) OpenModelFile();
  205.                         break;
  206.                     case IDM_SAVEAS:
  207.                 //        SaveModelFile();
  208.                         break;
  209.  
  210.                     case IDM_EXIT:
  211.                         DestroyWindow (hWnd);
  212.                         break;
  213.  
  214.                     case IDM_CONTROLSTRIP:
  215.                         DoFlagCommand (kQ3ViewerControllerVisible);
  216.                         break;
  217.  
  218.                     case IDM_CAMERABUTTON:
  219.                         DoFlagCommand (kQ3ViewerButtonCamera);
  220.                         break;
  221.  
  222.                     case IDM_TRUCKBUTTON:
  223.                         DoFlagCommand (kQ3ViewerButtonTruck);
  224.  
  225.                     case IDM_ORBITBUTTON:
  226.                         DoFlagCommand (kQ3ViewerButtonOrbit);
  227.                         break;
  228.  
  229.                     case IDM_DOLLYBUTTON:
  230.                         DoFlagCommand (kQ3ViewerButtonDolly);
  231.                         break;
  232.  
  233.                     case IDM_RESETBUTTON:
  234.                         DoFlagCommand (kQ3ViewerButtonReset);
  235.                         break;
  236.  
  237.                     default:
  238.                         return (DefWindowProc(hWnd, message, uParam, lParam));
  239.                 }
  240.                 break;
  241.  
  242.             case WM_INITMENU:
  243.                 {
  244.                     InitFlagMenu();
  245.                 }
  246.                 break;
  247.  
  248.             case WM_CREATE:
  249.                 {
  250.                 RECT aWinRect;
  251.                 GetClientRect(hWnd, (LPRECT)&aWinRect);
  252.  
  253.                 gViewer = Q3WinViewerNew (hInst, hWnd, &aWinRect, (unsigned long) kQ3ViewerDefault);
  254.  
  255.                 if( gViewer == NULL )
  256.                     return -1;
  257.                 }
  258.                 break;
  259.  
  260.             case WM_DESTROY:  // message: window being destroyed
  261.                 PostQuitMessage(0);
  262.                 break;
  263.  
  264.             case WM_SIZE:
  265.                 {
  266.                 long width  = LOWORD(lParam);
  267.                 long height  = HIWORD(lParam);
  268.                 RECT rect;
  269.                 
  270.                 rect.top = 0;
  271.                 rect.left = 0;
  272.                 rect.right = width;
  273.                 rect.bottom = height;
  274.                 Q3WinViewerSetBounds (gViewer, &rect);
  275.                 }
  276.                 break;
  277.  
  278.             case WM_SETFOCUS:
  279.                 SetFocus( Q3WinViewerGetWindow( gViewer ) );
  280.                 break;
  281.  
  282.             default:          // Passes it on if unproccessed
  283.                     return (DefWindowProc(hWnd, message, uParam, lParam));
  284.         }
  285.         return (0);
  286. }
  287.  
  288.  
  289. char ExtFilter[] = "QuickDraw 3D Metafiles\0*.3dmf;*.3dm;*.q3d;*.qd3d\0All Files\0*.*\0\0";
  290.  
  291. // put up common dialog; fOpen == TRUE for Open, FALSE for Save As.
  292. BOOL    BrowseForPathName(char *inPathName, BOOLEAN fOpen) 
  293. {
  294.     OPENFILENAME aFileName;
  295.     BOOL     aStatus = TRUE;
  296.  
  297.     inPathName[0] = 0;
  298.     aFileName.lStructSize = sizeof(OPENFILENAME);
  299.     aFileName.hwndOwner = gHwnd;
  300.     aFileName.hInstance = hInst;
  301.     aFileName.lpstrFilter = ExtFilter;
  302.     aFileName.lpstrCustomFilter = NULL; 
  303.     aFileName.nMaxCustFilter = 0L; 
  304.     aFileName.nFilterIndex = 0; 
  305.     aFileName.lpstrFile = inPathName; 
  306.     aFileName.nMaxFile = 255; 
  307.     aFileName.lpstrFileTitle = NULL;    
  308.     aFileName.nMaxFileTitle = 0; 
  309.     aFileName.lpstrInitialDir = NULL;  
  310.     aFileName.lpstrTitle = NULL;    
  311.     aFileName.Flags = OFN_EXPLORER + OFN_LONGNAMES + OFN_PATHMUSTEXIST; 
  312.     if( fOpen )
  313.         aFileName.Flags += OFN_FILEMUSTEXIST;
  314.     aFileName.nFileOffset = 0; 
  315.     aFileName.nFileExtension = 0; 
  316.     aFileName.lpstrDefExt = NULL; 
  317.     aFileName.lCustData = 0; 
  318.     aFileName.lpfnHook = NULL; 
  319.     aFileName.lpTemplateName = NULL; 
  320.     if (fOpen )
  321.         if (GetOpenFileName((LPOPENFILENAME)&aFileName))
  322.             aStatus = TRUE;
  323.         else
  324.             aStatus = FALSE;
  325.     else //saving
  326.         if (GetSaveFileName((LPOPENFILENAME)&aFileName))
  327.             aStatus = TRUE;
  328.         else
  329.             aStatus = FALSE;
  330.  
  331.     return aStatus;
  332. }
  333.  
  334. BOOL    OpenModelFile()
  335. {
  336.     char                *pathName;
  337.     char                pathNameChars[255];
  338.     TQ3Status            aStatus;
  339.     RECT                aWinRect;
  340.  
  341.     pathName = &(pathNameChars[0]);
  342.     if (BrowseForPathName(pathName, TRUE) != kQ3Success)
  343.         return FALSE;
  344.     
  345.     aStatus = Q3WinViewerUseUnixPath (gViewer, pathName);
  346.     (void) GetClientRect(gHwnd, (LPRECT)&aWinRect);
  347.     (void) InvalidateRect(gHwnd, &aWinRect, FALSE);    
  348.     return TRUE;
  349. }
  350.  
  351.  
  352. BOOL CenterWindow (HWND hwndChild, HWND hwndParent)
  353. {
  354.         RECT    rChild, rParent;
  355.         int     wChild, hChild, wParent, hParent;
  356.         int     wScreen, hScreen, xNew, yNew;
  357.         HDC     hdc;
  358.  
  359.         // Get the Height and Width of the child window
  360.         GetWindowRect (hwndChild, &rChild);
  361.         wChild = rChild.right - rChild.left;
  362.         hChild = rChild.bottom - rChild.top;
  363.  
  364.         // Get the Height and Width of the parent window
  365.         GetWindowRect (hwndParent, &rParent);
  366.         wParent = rParent.right - rParent.left;
  367.         hParent = rParent.bottom - rParent.top;
  368.  
  369.         // Get the display limits
  370.         hdc = GetDC (hwndChild);
  371.         wScreen = GetDeviceCaps (hdc, HORZRES);
  372.         hScreen = GetDeviceCaps (hdc, VERTRES);
  373.         ReleaseDC (hwndChild, hdc);
  374.  
  375.         // Calculate new X position, then adjust for screen
  376.         xNew = rParent.left + ((wParent - wChild) /2);
  377.         if (xNew < 0) {
  378.                 xNew = 0;
  379.         } else if ((xNew+wChild) > wScreen) {
  380.                 xNew = wScreen - wChild;
  381.         }
  382.  
  383.         // Calculate new Y position, then adjust for screen
  384.         yNew = rParent.top  + ((hParent - hChild) /2);
  385.         if (yNew < 0) {
  386.                 yNew = 0;
  387.         } else if ((yNew+hChild) > hScreen) {
  388.                 yNew = hScreen - hChild;
  389.         }
  390.  
  391.         // Set it, and return
  392.         return SetWindowPos (hwndChild, NULL,
  393.                 xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  394. }
  395.  
  396.  
  397. // About box callback
  398. LRESULT CALLBACK About(
  399.                 HWND hDlg,           // window handle of the dialog box
  400.                 UINT message,        // type of message
  401.                 WPARAM uParam,       // message-specific information
  402.                 LPARAM lParam)
  403. {
  404.         static  HFONT hfontDlg;
  405.         LPSTR   lpVersion;
  406.         DWORD   dwVerInfoSize;
  407.         DWORD   dwVerHnd;
  408.         UINT    uVersionLen;
  409.         WORD    wRootLen;
  410.         BOOL    bRetCode;
  411.         int     i;
  412.         char    szFullPath[256];
  413.         char    szResult[256];
  414.         char    szGetName[256];
  415.  
  416.         switch (message) {
  417.                 case WM_INITDIALOG:  // message: initialize dialog box
  418.                         // Center the dialog over the application window
  419.                         CenterWindow (hDlg, GetWindow (hDlg, GW_OWNER));
  420.  
  421.                         // Get version information from the application
  422.                         GetModuleFileName (hInst, szFullPath, sizeof(szFullPath));
  423.                         dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
  424.                         if (dwVerInfoSize) {
  425.                                 // If we were able to get the information, process it:
  426.                                 LPSTR   lpstrVffInfo;
  427.                                 HANDLE  hMem;
  428.                                 hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
  429.                                 lpstrVffInfo  = GlobalLock(hMem);
  430.                                 GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpstrVffInfo);
  431.                                 lstrcpy(szGetName, "\\StringFileInfo\\040904e4\\");
  432.                                 wRootLen = lstrlen(szGetName);
  433.  
  434.                                 // Walk through the dialog items that we want to replace:
  435.                                 for (i = IDC_FILEDESCRIPTION; i <= IDC_LEGALTRADEMARKS; i++) {
  436.                                         GetDlgItemText(hDlg, i, szResult, sizeof(szResult));
  437.                                         szGetName[wRootLen] = (char)0;
  438.                                         lstrcat (szGetName, szResult);
  439.                                         uVersionLen   = 0;
  440.                                         lpVersion     = NULL;
  441.                                         bRetCode      =  VerQueryValue((LPVOID)lpstrVffInfo,
  442.                                                 (LPSTR)szGetName,
  443.                                                 (LPVOID)&lpVersion,
  444.                                                 (LPDWORD)&uVersionLen); // For MIPS strictness
  445.  
  446.                                         if ( bRetCode && uVersionLen && lpVersion) {
  447.                                                 // Replace dialog item text with version info
  448.                                                 lstrcpy(szResult, lpVersion);
  449.                                                 SetDlgItemText(hDlg, i, szResult);
  450.                                         }
  451.                                 }
  452.  
  453.                                 GlobalUnlock(hMem);
  454.                                 GlobalFree(hMem);
  455.                         } // if (dwVerInfoSize)
  456.                         return (TRUE);
  457.  
  458.                 case WM_COMMAND:                      // message: received a command
  459.                         if (LOWORD(uParam) == IDOK        // "OK" box selected?
  460.                         || LOWORD(uParam) == IDCANCEL) {  // System menu close command?
  461.                                 EndDialog(hDlg, TRUE);        // Exit the dialog
  462.                                 DeleteObject (hfontDlg);
  463.                                 return (TRUE);
  464.                         }
  465.                         break;
  466.         }
  467.         return (FALSE); // Didn't process the message
  468. }
  469.  
  470.  
  471.