home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / cplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-08  |  8.5 KB  |  284 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. /* Player, Quartz multimedia sample
  13.  */
  14.  
  15. #include "stdwin.h"
  16. #include "cplay.h"
  17. #include "about.h"
  18. #include "file.h"
  19. #include "media.h"
  20. #include "toolbar.h"
  21.  
  22. #include "resource.h"
  23.  
  24. #define UNTITLED_STRING " - Untitled"
  25. #define nMaxResourceStrSize 128
  26.  
  27. AppVars appVars;
  28.  
  29. void PlayerMessageBox( UINT nResource ){
  30. /* Load and display an error message */
  31.     char szStr[ nMaxResourceStrSize ];
  32.  
  33.     LoadString( appVars.hInstance, nResource, szStr, nMaxResourceStrSize );
  34.     MessageBox( appVars.hwndMainFrame, szStr, appVars.szAppName,
  35.                 MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION );
  36. }
  37.  
  38. /**************************************************************************/
  39. long ProcessCommand( HWND hwnd, UINT wParam, LONG lParam ){
  40. /* Process a WM_COMMAND message to the main window */
  41.  
  42.     switch( wParam ){
  43.         case ID_FILE_EXIT:
  44.             PostQuitMessage( 0 );
  45.             break;
  46.  
  47.         case ID_HELP_ABOUT:
  48.             DoAboutDialog( appVars.hInstance,hwnd );
  49.             break;
  50.  
  51.         case ID_FILE_OPEN:
  52.             OpenMediaFile( hwnd, NULL );
  53.             break;
  54.  
  55.         case ID_MEDIA_PLAY:
  56.             OnMediaPlay( );
  57.             break;
  58.  
  59.         case ID_MEDIA_PAUSE:
  60.             OnMediaPause( );
  61.             break;
  62.  
  63.         case ID_MEDIA_STOP:
  64.             OnMediaStop( );
  65.             break;
  66.  
  67.         default:
  68.             return DefWindowProc( hwnd, WM_COMMAND, wParam, lParam );
  69.  
  70.     }
  71.  
  72.     return 0;
  73.  
  74. }
  75.  
  76. void OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
  77. { /* Sets the minimum size of the main window and the maximum height */
  78.     RECT rectRequired;
  79.     SIZE sizeToolbar;
  80.  
  81.     // Our client area is going to be the toolbar, so find our its size
  82.     CalcRequiredSize( &sizeToolbar );
  83.  
  84.     rectRequired.left = rectRequired.top = 0;
  85.     rectRequired.right = sizeToolbar.cx;
  86.     rectRequired.bottom = sizeToolbar.cy;
  87.  
  88.     // Take into account the menu, caption and thick frame
  89.     AdjustWindowRect( &rectRequired, WS_CAPTION|WS_THICKFRAME, TRUE );
  90.  
  91.     // Set the min/max sizes
  92.     lpMMI->ptMinTrackSize.x = rectRequired.right - rectRequired.left;
  93.     lpMMI->ptMinTrackSize.y = lpMMI->ptMaxTrackSize.y =
  94.             rectRequired.bottom - rectRequired.top;
  95. }
  96.  
  97. long FAR PASCAL MainFrameProc( HWND hwnd, UINT message, UINT wParam, LONG lParam){
  98. /* Handles the message sent to the main window */
  99.  
  100.     switch( message ){
  101.         case WM_DESTROY:
  102.             PostQuitMessage( 0 );
  103.             break;
  104.  
  105.         case WM_GETMINMAXINFO:
  106.             OnGetMinMaxInfo( (MINMAXINFO FAR *) lParam );
  107.             break;
  108.  
  109.         case WM_DRAWITEM:
  110.             DrawButton( appVars.hInstance, (DRAWITEMSTRUCT FAR *) lParam );
  111.             break;
  112.  
  113.         case WM_INITMENUPOPUP:
  114.             if( lParam == 1 ){          // Media popup menu
  115.                 EnableMenuItem( (HMENU) wParam, ID_MEDIA_PLAY,  CanPlay()  ? MF_ENABLED : MF_GRAYED );
  116.                 EnableMenuItem( (HMENU) wParam, ID_MEDIA_PAUSE, CanPause() ? MF_ENABLED : MF_GRAYED );
  117.                 EnableMenuItem( (HMENU) wParam, ID_MEDIA_STOP,  CanStop()  ? MF_ENABLED : MF_GRAYED );
  118.             } else
  119.                 return DefWindowProc( hwnd, message, wParam, lParam );
  120.             break;
  121.  
  122.         case WM_COMMAND:
  123.             return ProcessCommand( hwnd, wParam, lParam );
  124.  
  125.  
  126.         default:
  127.             return DefWindowProc( hwnd, message, wParam, lParam );
  128.     }
  129.  
  130.     return 0;
  131. }
  132.  
  133. /**************************************************************************/
  134. BOOL InitApplication( void ){
  135.     strcpy( appVars.szAppName, APP_NAME );
  136.  
  137.     // Filter interface initialize?
  138.     if( SUCCEEDED( CoInitialize( NULL ) ) )
  139.         return TRUE;
  140.  
  141.     return FALSE;
  142. }
  143.  
  144. void UnInitApplication( void ){
  145.     CoUninitialize( );
  146. }
  147.  
  148. BOOL InitInstance( HANDLE hInstance, HANDLE hPrevInstance ){
  149. // Set the specific instance data and register our main window class
  150. // if it has not been registered already
  151.     appVars.hInstance = hInstance;
  152.  
  153.     if(!hPrevInstance){
  154.         WNDCLASS wndClass;
  155.  
  156.         wndClass.style          = CS_HREDRAW | CS_VREDRAW;
  157.         wndClass.lpfnWndProc    = MainFrameProc;
  158.         wndClass.cbClsExtra     = 0;
  159.         wndClass.cbWndExtra     = 0;
  160.         wndClass.hInstance      = appVars.hInstance;
  161.         wndClass.hIcon          = LoadIcon( appVars.hInstance, MAKEINTRESOURCE( IDR_MAINFRAME ));
  162.         wndClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  163.         wndClass.hbrBackground  = GetStockObject( LTGRAY_BRUSH );
  164.         wndClass.lpszMenuName   = MAKEINTRESOURCE( IDR_MAINFRAME );
  165.         wndClass.lpszClassName  = appVars.szAppName;
  166.  
  167.         RegisterClass( &wndClass );
  168.     }
  169.  
  170.     return TRUE;
  171. }
  172.  
  173. BOOL InitMainFrame( int nCmdShow ){
  174. // Create out main window
  175.     char szTitle[ 30 ];
  176.  
  177.     strcpy( szTitle, APP_NAME );
  178.     strcat( szTitle, UNTITLED_STRING );
  179.  
  180.     appVars.hwndMainFrame = CreateWindow( appVars.szAppName,
  181.                                           szTitle,
  182.                                           WS_OVERLAPPEDWINDOW &~ WS_MAXIMIZEBOX,
  183.                                           CW_USEDEFAULT,
  184.                                           CW_USEDEFAULT,
  185.                                           0,
  186.                                           65,
  187.                                           NULL,
  188.                                           NULL,
  189.                                           appVars.hInstance,
  190.                                           NULL
  191.                                         );
  192.  
  193.     ShowWindow( appVars.hwndMainFrame, nCmdShow );
  194.     UpdateWindow( appVars.hwndMainFrame );
  195.  
  196.     return TRUE;
  197. }
  198.  
  199. /************************************************************************/
  200. UINT DoMainLoop( void ){
  201. // main message loop
  202.     MSG msg;
  203.     HANDLE  ahObjects[1];               // handles that need to be waited on
  204.     const int cObjects = 1;             // no of objects that we are waiting on
  205.  
  206.     // message loop lasts until we get a WM_QUIT message
  207.     // upon which we shall return from the function
  208.     while (TRUE) {
  209.         if( (ahObjects[ 0 ] = GetGraphEventHandle()) == NULL )
  210.             WaitMessage();
  211.         else{
  212.             // wait for any message sent or posted to this queue
  213.             // or for a graph notification
  214.             DWORD result;
  215.  
  216.             result = MsgWaitForMultipleObjects( cObjects
  217.                                               , ahObjects
  218.                                               , FALSE
  219.                                               , INFINITE
  220.                                               , QS_ALLINPUT
  221.                                               );
  222.             if( result != (WAIT_OBJECT_0 + cObjects) ){
  223.  
  224.                 if( result == WAIT_OBJECT_0 )
  225.                     OnGraphNotify();
  226.  
  227.                 continue;
  228.             }
  229.         }
  230.  
  231.         // When here, we either have a message or no event handle
  232.         // has been created yet.
  233.  
  234.         // read all of the messages in this next loop
  235.         // removing each message as we read it
  236.         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  237.  
  238.             if (msg.message == WM_QUIT)
  239.                 return msg.wParam;
  240.  
  241.             TranslateMessage(&msg);
  242.             DispatchMessage(&msg);
  243.  
  244.         }
  245.  
  246.     } // end of the always while-loop
  247.  
  248. } // DoMainLoop
  249.  
  250.  
  251. /************************************************************************/
  252. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  253.                     LPSTR lpszCmdParam, int nCmdShow ){
  254.     UINT nReturn;
  255.  
  256.     if ( ! InitApplication( ) )
  257.         return 0;
  258.  
  259.     if(     InitInstance( hInstance, hPrevInstance )
  260.          && InitMainFrame( nCmdShow )
  261.          && InitToolbar( hInstance, appVars.hwndMainFrame )
  262.          && InitMedia( )
  263.          && InitFileOpenDialog( appVars.hwndMainFrame )
  264.        ){
  265.         if( lpszCmdParam[0] != '\0' )
  266.             OpenMediaFile( appVars.hwndMainFrame, lpszCmdParam );
  267.  
  268.         // else..
  269.         nReturn = DoMainLoop();
  270.  
  271.         // Stop the graph if we can
  272.         if( CanStop() )
  273.             OnMediaStop();
  274.  
  275.         // release the filter graph
  276.         DeleteContents();
  277.     }
  278.  
  279.     UnInitApplication();
  280.  
  281.     return nReturn;
  282. }
  283.  
  284.