Open:Close.c

//
//      This is sample code which will make QTVR object movies from Linear QuickTime movies.
//
//      © 1991-1996 Apple Computer, Inc.  All rights reserved.
//
 
#include    "MakeQTVRObject.h"
#include    "extern.h" 
 
OSErr   OpenMovie(FSSpec    *theFSSpec)
{
    Rect            windowBounds = {40,10,240,330};
    Boolean         wasChanged;
    MovieInstance   *newInstance;
    OSErr           err;
    Rect            controllerBox;
    long             i=0;
    Rect            movieRect;
    
    // First, we create a new Movie Instance
    newInstance = (MovieInstance*)NewPtrClear(sizeof(MovieInstance));
    if(!newInstance)
        {
        UserMessage("\pNot enough memory.");
        return 8;
        }
    newInstance->spec = *theFSSpec;
    
    // Create a new window
    newInstance->window=(CWindowPtr) NewCWindow(nil, &windowBounds, theFSSpec->name , false, noGrowDocProc, (WindowPtr)-1, true, 0);
    if(!newInstance->window)
        {
        UserMessage("\pNot enough memory.");
        DisposePtr((Ptr)newInstance);
        return 1;
        }
    SetGWorld(newInstance->window,nil);
        
    // Open the movie   
    err = OpenMovieFile(theFSSpec,&newInstance->movieResFile,fsRdWrPerm);
    if(err != noErr)
        {
        UserMessage("\pCould not open the movie.");
        DisposePtr((Ptr)newInstance);
        DisposeWindow((WindowPtr)newInstance->window);
        return 2;
        }
    
    err = NewMovieFromFile(&newInstance->movie,newInstance->movieResFile,&newInstance->movieResID,nil,newMovieActive,&wasChanged);
    //CloseMovieFile(newInstance->movieResFile);
    if(err != noErr)
        {
        UserMessage("\pCould not create movie from movie file.");
        DisposeWindow((WindowPtr)newInstance->window);
        DisposePtr((Ptr)newInstance);
        return 3;
        }
        
    // Determine the size of the movie
    GetMovieBox(newInstance->movie,&movieRect);
    OffsetRect(&movieRect,-movieRect.left,-movieRect.top);
    SetMovieBox(newInstance->movie,&movieRect);
    
    if(!ValidMovieType(newInstance))
        {
        DisposeWindow((WindowPtr)newInstance->window);
        DisposeMovie(newInstance->movie);
        DisposePtr((Ptr)newInstance);
        return 4;
        }
    //Create a movie controller
    newInstance->movieController = NewMovieController(newInstance->movie,&movieRect,mcTopLeftMovie);
    if(!newInstance->movieController)
        {
        UserMessage("\pCould not create movie controller.");
        DisposeWindow((WindowPtr)newInstance->window);
        DisposeMovie(newInstance->movie);
        DisposePtr((Ptr)newInstance);
        return 5;
        }
    
    // Resize the window to fit the movie and the controller
    MCGetControllerBoundsRect(newInstance->movieController,&controllerBox);
    SizeWindow((WindowPtr)newInstance->window,controllerBox.right,controllerBox.bottom,true);
    ShowWindow((WindowPtr)newInstance->window);
    
    AttachMovieToWindow((WindowPtr)newInstance->window,newInstance);
    
    MCIdle (newInstance->movieController);
    return 0;
}
 
OSErr   ReopenMovie(MovieInstance   *theInstance)
{
    Rect            movieRect;
    
    if(!theInstance->movieController)
        return 1;
    
    // Kill the current movie controller
    DisposeMovieController(theInstance->movieController);
        
    // Determine the size of the movie
    GetMovieBox(theInstance->movie,&movieRect);
    OffsetRect(&movieRect,-movieRect.left,-movieRect.top);
    SetMovieBox(theInstance->movie,&movieRect);
    
    
    //Create a movie controller
    theInstance->movieController = NewMovieController(theInstance->movie,&movieRect,mcTopLeftMovie);
    if(!theInstance->movieController)
        {
        UserMessage("\pCould not create movie controller.");
        DisposeWindow((WindowPtr)theInstance->window);
        DisposeMovie(theInstance->movie);
        DisposePtr((Ptr)theInstance);
        return 5;
        }
    
    MCIdle (theInstance->movieController);
    return 0;
}
 
void    CloseMovie(WindowPtr    theWindow)
{
    MovieInstance   *theInstance;
    CWindowPtr      newWindow;
    if(!theWindow)
        return;
        
    theInstance = GetMovieInstanceFromWindow(theWindow);
    DisposeWindow(theWindow);
    if(!theInstance)
        return;
    CloseMovieFile(theInstance->movieResFile);  
    if(theInstance->movieController)
        DisposeMovieController(theInstance->movieController);
    if(theInstance->movie)  
        DisposeMovie(theInstance->movie);
    
    if(newWindow = (CWindowPtr)FrontWindow())
        SetGWorld(newWindow,nil);
        
    DisposePtr((Ptr)theInstance);
}
 
 
Boolean GetAMovie(FSSpec    *theSpec)
{
    SFTypeList          theTypeList = {'MooV'};
    StandardFileReply   theReply;
    
    StandardGetFilePreview(0L, 1, theTypeList, &theReply);
    
    *theSpec = theReply.sfFile;
    
    return (theReply.sfGood);
}
 
Boolean ValidMovieType(MovieInstance    *theInstance)
{
    OSType controllerSubType;
    
    theInstance->isObjectMovie = false;
    
    if (GetUserDataItem(GetMovieUserData(theInstance->movie), &controllerSubType, sizeof(controllerSubType), 'ctyp', 0) != noErr)
        return true;
        
    if (controllerSubType == 'stna')
        {
        theInstance->isObjectMovie = true;
        return true;
        }
    else if (controllerSubType == 'stpn' || controllerSubType == 'STpn')
        {
        UserMessage("\pMakeQTVRObject cannot display panoramic movies.");
        return false;
        }
        
    return true;
}