home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-10-27 | 6.3 KB | 288 lines | [TEXT/CWIE] |
- #include "MMovieView.h"
- // MMovieView.cp
- // This class gets a string from the CPPb resource to use as the file name of the movie
- // then it opens and plays the movie upon a message from the play button.
- // Sorry it's not better documented. I had many re-writes before this one, and sometimes
- // I don't know what something actually does, I just know the compiler buys it and it works =)
-
-
-
- #include <Movies.h>
- #include <Gestalt.h>
- #include <GestaltEqu.h>
- #include <LString.h>
- #include <UDrawingState.h>
- #include <UGraphicsUtilities.h>
- #include <Quickdraw.h>
-
-
-
- /* the function that makes the pane work with the button */
- void
- MMovieView::ListenToMessage( MessageT theMessage, void *ioParam )
- {
- #pragma unused ( ioParam )
-
- switch( theMessage )
- {
- case msg_PlayMovie:
- {
-
- if( FileExists != true )
- {
- theMovie = GetMovieFile();
-
- if( theMovie != nil )
- {
- SetMovie();
- PlayMovie();
-
- // display the first frame
- ::GoToBeginningOfMovie( theMovie );
- ::ShowMoviePoster( theMovie );
- }
- }
- else
- if( FileExists )
- {
- PlayMovie();
-
- // display the first frame
- ::GoToBeginningOfMovie( theMovie );
- ::ShowMoviePoster( theMovie );
-
-
- ::DisposeMovie( theMovie );
-
- theMovie = nil;
- FileExists = false; // set this so that it will get a new file
- }
-
- break;
- }
- }
- }
-
-
-
- MMovieView *
- MMovieView::CreateMoviePaneStream(LStream *inStream )
- {
-
- return (new MMovieView( inStream ));
- }
-
-
- // ---------------------------------------------------------------------------------
- // • MMovieView
- // ---------------------------------------------------------------------------------
-
- MMovieView::MMovieView()
- {
- mMovieTitle = gEmptyString;
- }
-
-
- MMovieView::MMovieView( LStream *inStream )
- : LView( inStream )
- {
- /* read in the Pascal String from the CPPb resource */
- inStream->ReadPString( mMovieTitle );
-
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ~MMovieView
- // ---------------------------------------------------------------------------------
-
- MMovieView::~MMovieView()
- {
- ::DisposeMovie( theMovie );
- ::CloseMovieFile( movieResRef );
- FileOpen = false;
- ::ExitMovies();
- }
-
- StringPtr
- MMovieView::GetMovieTitle ( Str255 outTitle ) const
- {
-
- return LString::CopyPStr ( mMovieTitle, outTitle );
-
- }
-
- // ---------------------------------------------------------------------------------
- // • DrawSelf
- // ---------------------------------------------------------------------------------
-
- void
- MMovieView::DrawSelf()
- {
- // Save and normalize the color and pen states.
- StColorPenState savePenState;
- StColorPenState::Normalize();
-
- // Calculate the frame rect.
- Rect theFrame;
- CalcLocalFrameRect( theFrame );
-
- if( FileExists != true )
- {
- theMovie = GetMovieFile(); /* if no file, we need to get one */
- if( theMovie != nil )
- {
- SetMovie(); /* set the movie and the MovieView frame size */
- FileExists = true; /* kewel, we got one ! */
- }
- }
- else // if this else isn't here, it erases itself on the next call to DrawSelf()
- {
- if( FileExists )
- {
- ::GoToBeginningOfMovie( theMovie );
- ::ShowMoviePoster( theMovie );
- }
- }
- }
-
-
- Movie
- MMovieView::GetMovieFile( void )
- {
- FSSpec movieSpec;
- Str255 movieFileName;
- long qtVers;
- short refNum = 0;
- long dirNum = 0;
- OSErr err;
-
- GetMovieTitle( movieFileName );
-
- try {
-
- if( (::Gestalt( gestaltQuickTime, &qtVers )) == noErr )
- {
-
- /* this is the function that gets the movie FSSpec with the Str255 file name */
- err = ::FSMakeFSSpec( refNum, dirNum, movieFileName, &movieSpec );
- if( err == noErr )
- {
- ::EnterMovies();
-
- if( FileOpen != true )
- {
- err = ::OpenMovieFile( &movieSpec, &movieResRef, fsRdPerm );
- if( err == noErr )
- {
- FileOpen = true;
-
- err = ::NewMovieFromFile( &theMovie, movieResRef, nil, nil,
- newMovieActive, nil );
- if( err == noErr )
- {
- return theMovie;
- }
- }
- }
- else /* if the file's already open, why open it again ? */
- {
- err = ::NewMovieFromFile( &theMovie, movieResRef, nil, nil,
- newMovieActive, nil );
- if( err == noErr )
- {
- return theMovie;
- }
- }
- }
- }
- else
- {
- /* oops something went wrong, trash the file */
- ::DisposeMovie( theMovie );
- ::CloseMovieFile( movieResRef );
-
- FileOpen = false;
- FileExists = false;
- theMovie = nil;
-
- return theMovie;
- }
- } // end try
-
- catch(...)
- { /* oops something went wrong, trash the file */
- if( movieResRef )
- {
- ::DisposeMovie( theMovie );
- ::CloseMovieFile( movieResRef );
-
- FileOpen = false;
- FileExists = false;
- theMovie = nil;
-
- return theMovie;
- }
- }
- return theMovie; /* take us home */
- }
-
- void
- MMovieView::SetMovie( void )
- {
- short movieHeight;
- short movieWidth;
- short paneHeight;
- short paneWidth;
-
- Rect movieBounds, frame;
-
- /* heres something interesting: we get the size of the movie and */
- /* then set our MMovieView pane to the size of the movie */
- /* something to work on... need a way to center the resulting frame in */
- /* the frames immediate surroundings ( immediate supervisor ? ) */
-
-
- /* get the movie Rect size and set up the offset and GWorld */
- ::GetMovieBox( theMovie, &movieBounds );
- ::OffsetRect( &movieBounds, -movieBounds.left, -movieBounds.top );
- ::SetMovieGWorld( theMovie, (CGrafPtr) GetMacPort(), nil );
-
- /* determine the movie size in a way that can be used by ResizeFrameBy() */
- movieHeight = ( movieBounds.bottom - movieBounds.top );
- movieWidth = ( movieBounds.right - movieBounds.left );
-
- /* get the current MovieView Rect size */
- CalcLocalFrameRect( frame );
- paneHeight = ( frame.bottom - frame.top );
- paneWidth = ( frame.right - frame.left );
-
- /* resize the current MovieView Rect to the orig movie size */
- ResizeFrameBy( (movieWidth - paneWidth), (movieHeight - paneHeight), 1 );
- CalcLocalFrameRect( frame );
- FocusDraw();
-
- }
-
- void
- MMovieView::PlayMovie( void )
- {
- /* simple play function for QuickTime */
- /* might be better to redefine this as an LPeriodical SpendTime() or something */
- Rect frame;
- CalcLocalFrameRect( frame );
- FocusDraw();
-
- ::GoToBeginningOfMovie( theMovie );
-
- ::MoviesTask( theMovie, 0 );
-
- ::StartMovie( theMovie );
-
- while(( ::IsMovieDone( theMovie ) == false ))
- {
- ::MoviesTask( theMovie, 0 ); /* keep playing until finished */
- }
- }
-
-