home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / mfcqtclasses.win / cquicktime.cpp next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  11.3 KB  |  513 lines

  1. /*
  2.     File:        CQuickTime.c
  3.  
  4.     Written by: Keith Gurganus
  5.  
  6.     Note: Set project settings to "Not using precompiled headers" for this file.
  7. */
  8.  
  9. #include "QTML.h"
  10. #include "CQuickTime.h"
  11.  
  12. CQuickTime::CQuickTime()
  13. {
  14.     movieOpened = FALSE;
  15.     theMovie = NULL;
  16.     theMC = NULL;
  17.     theHwnd = NULL;
  18.     theViewHwnd = NULL;
  19.     theAppName[0] = '\0';
  20.     theFullPath[0] = '\0';
  21. }
  22.  
  23. CQuickTime::~CQuickTime()
  24. {
  25. }
  26.  
  27. void CQuickTime::NewMovieFile(void){
  28.     // Close any previously opened movie
  29.     CloseMovie();
  30.  
  31.     // Set the port    
  32.     SetGWorld((CGrafPtr)GetHWNDPort(theViewHwnd), nil);
  33.  
  34.     // Create a newMovie
  35.     theMovie = NewMovie(newMovieActive);
  36.  
  37.     if ( theMovie != NULL ) {
  38.         // set window title to name    
  39.         strcpy((char*)theFullPath,"Untitled");
  40.         SetWindowTitle();
  41.  
  42.         // Create the movie controller
  43.         CreateNewMovieController(theMovie);
  44.         
  45.         movieOpened = TRUE;
  46.     }    
  47. }
  48.  
  49.  
  50. BOOL CQuickTime::OpenMovie(unsigned char *fullPath)
  51. {
  52.     BOOL    isMovieGood = FALSE;
  53.  
  54.     if ( strlen ((char*)fullPath ) != 0)
  55.     {
  56.         OSErr                err;
  57.         short                theFile = 0;
  58.         long                controllerFlags = 0L;
  59.         FSSpec                sfFile;
  60.         short                movieResFile;
  61.  
  62.         // Close any previously opened movie
  63.         CloseMovie();
  64.     
  65.         // make a copy of our full path name
  66.         strcpy ( (char *)theFullPath, (const char *) fullPath );
  67.  
  68.         // convert theFullPath to pstring
  69.         CToPstr((char*)theFullPath);
  70.  
  71.         // Make a FSSpec with a pascal string filename
  72.         FSMakeFSSpec(0,0L,theFullPath, &sfFile);
  73.         
  74.         // Set the port    
  75.         SetGWorld((CGrafPtr)GetHWNDPort(theViewHwnd), nil);
  76.  
  77.         // Open the movie file
  78.         err = OpenMovieFile(&sfFile, &movieResFile, fsRdPerm);
  79.         if (err == noErr)
  80.         {
  81.             // Get the Movie from the file
  82.             err = NewMovieFromFile(&theMovie,movieResFile, 
  83.                                     nil, 
  84.                                     nil, 
  85.                                     newMovieActive, /* flags */
  86.                                     nil);
  87.         
  88.             // Close the movie file
  89.             CloseMovieFile(movieResFile);
  90.  
  91.             if (err == noErr)
  92.             {
  93.                 // Create the movie controller
  94.                    CreateNewMovieController(theMovie);
  95.                 isMovieGood = movieOpened = TRUE;    
  96.                 PToCstr((char*)theFullPath);
  97.             } else
  98.                 theFullPath[0] = '\0'; 
  99.                 
  100.         } else
  101.             theFullPath[0] = '\0';
  102.         
  103.         SetWindowTitle();
  104.     }
  105.     return isMovieGood;
  106. }
  107.  
  108. void CQuickTime::CloseMovie(void)
  109. {
  110.     if (movieOpened == TRUE ){
  111.         movieOpened = FALSE;
  112.      
  113.         if (theMC)
  114.             DisposeMovieController(theMC);
  115.  
  116.         if (theMovie)
  117.             DisposeMovie(theMovie);
  118.  
  119.         theMovie = NULL;
  120.         theMC = NULL;
  121.     }
  122.  
  123.     Rect initialRect;
  124.     initialRect.top = initialRect.left = initialRect.bottom = 0;
  125.     initialRect.right = MINWINDOWWIDTH;
  126.     
  127.     //SizeWindow((WindowPtr)GetHWNDPort(theHwnd), initialRect.right, initialRect.bottom, FALSE);
  128.     SizeWindow((WindowPtr)GetHWNDPort(theViewHwnd), initialRect.right, initialRect.bottom, FALSE);
  129.  
  130.     /* set window title to empty name */
  131.     theFullPath[0] ='\0';
  132.     SetWindowTitle ();
  133. }
  134.  
  135.  
  136. void CQuickTime::SaveAsMovie(void){
  137.     unsigned char    lpszPathName[256];
  138.     OPENFILENAME    ofn;
  139.  
  140.     memset(&ofn, 0, sizeof(OPENFILENAME));
  141.     lpszPathName[0] = '\0';
  142.  
  143.     ofn.lStructSize = sizeof(OPENFILENAME);
  144.     ofn.hwndOwner = ::GetActiveWindow ();
  145.     ofn.lpstrFilter = "QuickTime Movies (*.mov) \0 *.mov\0";
  146.     ofn.lpstrFile = (char *)lpszPathName;
  147.     ofn.nMaxFile = sizeof(lpszPathName);
  148.     ofn.lpstrFileTitle = NULL;
  149.     ofn.nMaxFileTitle = (unsigned long)NULL;
  150.     ofn.lpstrInitialDir = NULL;
  151.     ofn.Flags = OFN_OVERWRITEPROMPT;
  152.  
  153.     // Put up a save file dialog
  154.     if (GetSaveFileName(&ofn)) {
  155.         long            movieFlattenFlags = flattenAddMovieToDataFork;
  156.         FSSpec            sfFile;
  157.         OSType            creator = FOUR_CHAR_CODE('TVOD');
  158.         long            createMovieFlags = createMovieFileDeleteCurFile;
  159.  
  160.         // Convert full path name to pstring
  161.         CToPstr((char *)lpszPathName);    
  162.  
  163.         // Make a FSSpec with a pascal string filename
  164.         FSMakeFSSpec(0,0L,lpszPathName, &sfFile);
  165.  
  166.         // FlattenMovie
  167.         FlattenMovie(    theMovie,
  168.                         movieFlattenFlags,
  169.                         &sfFile,
  170.                         creator,
  171.                         -1,
  172.                         createMovieFlags,
  173.                         nil,
  174.                         NULL );
  175.     
  176.     }
  177. }
  178.  
  179. void CQuickTime::OnEditCut() 
  180. {
  181.     Movie                scrapMovie;
  182.     ComponentResult        theErr = noErr;
  183.     
  184.     if (theMC){
  185.         scrapMovie = MCCut(theMC);
  186.         if ( scrapMovie ) {
  187.             theErr = PutMovieOnScrap(scrapMovie, 0L);
  188.             DisposeMovie(scrapMovie);
  189.         } 
  190.     }
  191. }
  192.  
  193. void CQuickTime::OnEditCopy() 
  194. {
  195.     Movie                scrapMovie;
  196.     ComponentResult        theErr = noErr;
  197.     
  198.     if (theMC){
  199.         scrapMovie = MCCopy(theMC);
  200.         if ( scrapMovie ) {
  201.             theErr = PutMovieOnScrap(scrapMovie, 0L);
  202.             DisposeMovie(scrapMovie);
  203.         }
  204.     }
  205. }
  206.  
  207. void CQuickTime::OnEditPaste() 
  208. {
  209.     if (theMC)
  210.         MCPaste(theMC, nil);
  211. }
  212.  
  213. void CQuickTime::OnEditClear() 
  214. {
  215.     if (theMC)
  216.         MCClear(theMC);
  217. }
  218.  
  219. void CQuickTime::OnEditUndo() 
  220. {
  221.     if (theMC)
  222.         MCUndo(theMC);
  223. }
  224.  
  225.  
  226. void CQuickTime::OnEditSelectall() 
  227. {
  228.     TimeRecord             tr;
  229.     ComponentResult        theErr = noErr;
  230.     
  231.     if ( theMovie && theMC ) {
  232.         tr.value.hi = 0;
  233.         tr.value.lo = 0;
  234.         tr.base = 0;
  235.         tr.scale = GetMovieTimeScale(theMovie);
  236.         MCDoAction(theMC, mcActionSetSelectionBegin, &tr);
  237.         tr.value.lo = GetMovieDuration(theMovie);
  238.         MCDoAction(theMC, mcActionSetSelectionDuration, &tr);
  239.     } else {
  240.         if ( theMovie == NULL )
  241.             theErr = invalidMovie;
  242.         else
  243.             theErr = -1;
  244.     }    
  245.     
  246. }
  247.  
  248. void CQuickTime::ProcessMovieEvent(HWND hWnd, unsigned int message, unsigned int wParam, long lParam) 
  249. {    
  250.     // Convert the Windows event to a QTML event
  251.     MSG                theMsg;
  252.     EventRecord        macEvent;
  253.     LONG            thePoints = GetMessagePos();
  254.  
  255.     theMsg.hwnd = hWnd;
  256.     theMsg.message = message;
  257.     theMsg.wParam = wParam;
  258.     theMsg.lParam = lParam;
  259.     theMsg.time = GetMessageTime();
  260.     theMsg.pt.x = LOWORD(thePoints);
  261.     theMsg.pt.y = HIWORD(thePoints);
  262.  
  263.     // tranlate a windows event to a mac event
  264.     WinEventToMacEvent(&theMsg, &macEvent);
  265.  
  266.     // Pump messages as mac event
  267.     MCIsPlayerEvent(theMC,(const EventRecord *)&macEvent);
  268. }
  269.  
  270. int CQuickTime::OnMovieWindowCreate(HWND hWnd, CREATESTRUCT *lpCreateStruct) 
  271. {
  272.  
  273.     if ( hWnd != NULL) { 
  274.         theViewHwnd = hWnd;                    // the view's hwnd
  275.         theHwnd = ::GetParent(theViewHwnd);    // the parent hwnd     hwndParent
  276.  
  277.         int borderWidth = GetWindowsBorderWidth();
  278.         int titlebarHeight = GetWindowsTitleHeight();
  279.         int captionHeight = GetWindowsCaptionHeight();    
  280.  
  281.         lpCreateStruct->cy = borderWidth + titlebarHeight + captionHeight;
  282.         lpCreateStruct->cx = MINWINDOWWIDTH;
  283.  
  284.         SetWindowTitle();
  285.         Rect initialRect;
  286.         initialRect.top = initialRect.left = initialRect.bottom = 0;
  287.         initialRect.right = MINWINDOWWIDTH;
  288.  
  289.         // Create GrafPort <-> HWND association
  290.         CreatePortAssociation(theViewHwnd, NULL, 0);    
  291.     }
  292.  
  293.     return 0;
  294. }
  295.  
  296. void CQuickTime::CreateNewMovieController(Movie theMovie)
  297. {
  298.     Rect    bounds;
  299.     Rect    maxBounds;
  300.     long     controllerFlags;
  301.  
  302.     // 0,0 Movie coordinates
  303.     GetMovieBox(theMovie, &theMovieRect);
  304.     MacOffsetRect(&theMovieRect, -theMovieRect.left, -theMovieRect.top);
  305.  
  306.     // Attach a movie controller
  307.     theMC = NewMovieController(theMovie, &theMovieRect, mcTopLeftMovie );
  308.  
  309.     // Get the controller rect 
  310.     MCGetControllerBoundsRect(theMC, &bounds);
  311.  
  312.     // Enable editing
  313.     MCEnableEditing(theMC,TRUE);
  314.  
  315.     // Tell the controller to attach a movie's CLUT to the window as appropriate.
  316.     MCDoAction(theMC, mcActionGetFlags, &controllerFlags);
  317.     MCDoAction(theMC, mcActionSetFlags, (void *)(controllerFlags | mcFlagsUseWindowPalette));
  318.  
  319.     // Allow the controller to accept keyboard events
  320.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)TRUE);
  321.  
  322.     // Set the controller action filter
  323.     MCSetActionFilterWithRefCon(theMC, MCFilter, (long)theViewHwnd);
  324.  
  325.     // Set the grow box amound
  326.     GetMaxBounds(&maxBounds);
  327.     MCDoAction(theMC, mcActionSetGrowBoxBounds, &maxBounds);
  328.  
  329.     // Size our window
  330.     SizeWindow((WindowPtr)GetHWNDPort(theViewHwnd), bounds.right, bounds.bottom, FALSE);
  331. }
  332.  
  333. void CQuickTime::OnMovieWindowDestroy() 
  334. {    
  335.     CGrafPtr    windowPort = NULL;
  336.     
  337.     // close any movies    before destroying PortAssocation
  338.     CloseMovie();
  339.  
  340.     // Destroy the view's GrafPort <-> HWND association
  341.     if (theViewHwnd)
  342.         windowPort = (CGrafPtr)GetHWNDPort(theViewHwnd);
  343.     
  344.     if (windowPort)
  345.         DestroyPortAssociation(windowPort);
  346.  
  347. }
  348.  
  349. void CQuickTime::SetWindowTitle(void) 
  350. {
  351.     /* set window title to name */
  352.     unsigned char    fileName[64];
  353.     unsigned char    appName[32];
  354.     unsigned char    titleName[256];
  355.  
  356.     fileName[0] = '\0';
  357.     appName[0] = '\0';
  358.     titleName[0] = '\0';
  359.  
  360.     GetAppName((unsigned char *)&appName);
  361.     if (strlen((char*)appName))
  362.         strcpy ((char *)titleName, (char *)appName);
  363.  
  364.     GetFileNameFromFullPath((unsigned char *)&fileName);
  365.     if (strlen((char*)fileName)){
  366.         if (strlen((char*)appName))
  367.             strcat ((char *)titleName, " - ");
  368.  
  369.         strcat ((char *)titleName, (char *)fileName);
  370.     }
  371.     ::SetWindowText(theHwnd, (const char *)titleName);
  372. }
  373.  
  374. void CQuickTime::GetFileNameFromFullPath(unsigned char *fileName) 
  375. {
  376.     /* pluck the filename from the fullpath, */
  377.     int        i = 0, j = -1, stringLen = 0;
  378.  
  379.     stringLen = strlen((char *)theFullPath);
  380.     if (stringLen > 0 ) {
  381.         while(i<stringLen){
  382.             if (theFullPath[i] == 0x5c || theFullPath[i] == '/' )
  383.                 j = i;
  384.             i++;
  385.         }
  386.         if ( j>-1)
  387.             strcpy((char *)fileName, (char *)&theFullPath[j+1]);
  388.         else
  389.             strcpy((char *)fileName, (char *)theFullPath);
  390.  
  391.     }
  392. }
  393.  
  394. void CQuickTime::GetAppName(unsigned char *appName)
  395. #if (!WIN32)
  396.     HINSTANCE theModule = (HINSTANCE)GetWindowLong(theHwnd, GWL_HINSTANCE);
  397.     GetModuleFileName(theModule, (char *)appName, strlen((char *)appName) );
  398. #else
  399.     if (strlen((char*)theAppName) > 0)
  400.         strcpy((char*)appName, (char*)theAppName);
  401. #endif
  402. }
  403.  
  404. void CQuickTime::CToPstr(char *theString)
  405. {
  406.     char    tempString[256];
  407.  
  408.     tempString[0] = strlen (theString);
  409.     tempString[1] = '\0';
  410.  
  411.     strcat ( tempString, theString );
  412.     strcpy    ( theString, tempString );
  413. }
  414.  
  415. void CQuickTime::PToCstr(char *theString)
  416. {
  417.     char    tempString[256];
  418.     int        len = theString[0];
  419.  
  420.     memcpy ( tempString, &theString[1], theString[0]);
  421.     tempString[len] = '\0';
  422.     strcpy    ( theString, tempString );
  423. }
  424.  
  425. int    CQuickTime::GetWindowsBorderWidth (void)
  426. {
  427.     RECT    windowRect, clientRect;
  428.     int        windowWidth, clientWidth;
  429.     
  430.     ::GetWindowRect(theHwnd, &windowRect);
  431.     ::GetClientRect(theHwnd, &clientRect);
  432.  
  433.     windowWidth = windowRect.right - windowRect.left;
  434.     clientWidth = clientRect.right - clientRect.left;
  435.  
  436.     return windowWidth - clientWidth;
  437. }
  438.  
  439. int    CQuickTime::GetWindowsTitleHeight (void)
  440. {
  441.     RECT    windowRect, clientRect;
  442.     int        windowHeight, clientHeight;
  443.     int        windowWidth, clientWidth, bordersWidth;
  444.     int        captionHeight, titlebarHeight;
  445.     
  446.     ::GetWindowRect(theHwnd, &windowRect);
  447.     ::GetClientRect(theHwnd, &clientRect);
  448.  
  449.     windowWidth = windowRect.right - windowRect.left;
  450.     clientWidth = clientRect.right - clientRect.left;
  451.     bordersWidth = windowWidth - clientWidth;
  452.  
  453.     windowHeight = windowRect.bottom - windowRect.top;
  454.     clientHeight = clientRect.bottom - clientRect.top;
  455.     captionHeight = GetSystemMetrics(SM_CYCAPTION);    
  456.     titlebarHeight = (windowHeight - clientHeight) - captionHeight;
  457.  
  458.     return titlebarHeight;
  459. }
  460.  
  461. int CQuickTime::GetWindowsCaptionHeight(void)
  462. {
  463.      DWORD    dwStyle = 0L;
  464.     int        retValue = 0L;
  465.  
  466.     if(!IsWindow(theHwnd))
  467.         return 0L;
  468.  
  469.     dwStyle = GetWindowLong(theHwnd, GWL_STYLE);
  470.  
  471.     if (dwStyle & WS_CAPTION)
  472.         retValue = GetSystemMetrics(SM_CYCAPTION);
  473.  
  474.     return retValue;
  475. }
  476.  
  477. void CQuickTime::GetMaxBounds(Rect *maxRect)
  478. {
  479.     RECT deskRect;
  480.  
  481.     GetWindowRect(GetDesktopWindow(), &deskRect);
  482.  
  483.     OffsetRect(&deskRect, -deskRect.left, -deskRect.top);
  484.  
  485.     maxRect->top = (short)deskRect.top;
  486.     maxRect->bottom = (short)deskRect.bottom;
  487.     maxRect->left = (short)deskRect.left;
  488.     maxRect->right = (short)deskRect.right;
  489. }
  490.  
  491. Movie CQuickTime::GetMovie()
  492. {
  493.     return theMovie;
  494. }
  495.  
  496. Boolean MCFilter(MovieController mc, short action, void*params, long refCon)
  497. {
  498.     if(action == mcActionControllerSizeChanged) {
  499.         Rect        bounds;
  500.         WindowPtr    w;
  501.         MCGetControllerBoundsRect(mc, &bounds);
  502.  
  503.         w = GetHWNDPort((HWND)refCon);
  504.         SizeWindow((WindowPtr)w, bounds.right, bounds.bottom, TRUE);
  505.     }
  506.     return FALSE;
  507. }
  508.  
  509.  
  510.  
  511.  
  512.