CPict2VRWindow.cp

/*
    A window that provides a user interface for creating QTVR movies.
    
    Created 29 Jan 1996 by EGH
    
    Copyright © 1996, Apple Computer, Inc. All rights reserved.
*/
 
#include <QuickTimeComponents.h>
 
#include <String_Utils.h>
 
#include "CApp.h"
#include "CBeachBall.h"
#include "CKeyFilters.h"
#include "CMovieMaker.h"
#include "CUtils.h"
 
#include "CPict2VRWindow.h"
 
// =========
// constants
 
const PaneIDT Button_Create = 'okay';
const PaneIDT Button_Cancel = 'canc';
const PaneIDT Button_MakeDefault = 'defa';
const PaneIDT Button_Compression = 'comp';
const PaneIDT Caption_Compression = 'coms';
const PaneIDT Caption_PictFile = 'pict';
const PaneIDT Caption_TileFile = 'tile';
const PaneIDT Caption_QTVRFile = 'move';
const PaneIDT Button_SetVRFile = 'setf';
const PaneIDT Button_SetTileFile = 'sett';
const PaneIDT Edit_Width = 'widt';
const PaneIDT Edit_Height = 'heig';
const PaneIDT Edit_Pan = 'pan ';
const PaneIDT Edit_Tilt = 'tilt';
const PaneIDT Edit_Zoom = 'zoom';
 
// =========
 
 
//static
CPict2VRWindow *CPict2VRWindow::CreatePict2VRWindowWindowStream(
    LStream *inStream)
{
    return new CPict2VRWindow(inStream);
}
 
CPict2VRWindow::CPict2VRWindow(
    LStream *inStream) :
    LDialogBox(inStream)
{
}
 
CPict2VRWindow::~CPict2VRWindow()
{
}
 
void CPict2VRWindow::FinishCreateSelf()
{
    LDialogBox::FinishCreateSelf();
    AddAttachment(new LUndoer); // for the edits
    
    mDepth = 32;
    mSpatialQuality = codecHighQuality;
    mCodec = 'cvid';
    
        // check the app for default parameters
    P2VRPrefsHdl prefs = gApp->GetPreferences();
    if (prefs != nil)
    {
        mDepth = (*prefs)->depth;
        mSpatialQuality = (*prefs)->spatialQuality;
        mCodec = (*prefs)->codec;
        
        SetValueForPaneID(Edit_Width, (*prefs)->width);
        SetValueForPaneID(Edit_Height, (*prefs)->height);
        
        Str255 valueStr;
        LEditField *pane = (LEditField *)FindPaneByID(Edit_Pan);
        if (pane != nil)
        {
            pane->SetKeyFilter(CKeyFilters::RealNumberFieldPositive);
            FixedToStr((*prefs)->pan, valueStr);
            pane->SetDescriptor(valueStr);
        }
        pane = (LEditField *)FindPaneByID(Edit_Tilt);
        if (pane != nil)
        {
            pane->SetKeyFilter(CKeyFilters::RealNumberField);
            FixedToStr((*prefs)->tilt, valueStr);
            pane->SetDescriptor(valueStr);
        }
        pane = (LEditField *)FindPaneByID(Edit_Zoom);
        if (pane != nil)
        {
            pane->SetKeyFilter(CKeyFilters::RealNumberFieldPositive);
            FixedToStr((*prefs)->zoom, valueStr);
            pane->SetDescriptor(valueStr);
        }
    }
    
        // find and listen to the buttons
    LButton *button;
    button = (LButton *)FindPaneByID(Button_MakeDefault);
    ThrowIfNil_(button);
    button->AddListener(this);
    
    button = (LButton *)FindPaneByID(Button_SetTileFile);
    ThrowIfNil_(button);
    button->AddListener(this);
    
    button = (LButton *)FindPaneByID(Button_SetVRFile);
    ThrowIfNil_(button);
    button->AddListener(this);
    
    button = (LButton *)FindPaneByID(Button_Compression);
    ThrowIfNil_(button);
    button->AddListener(this);
    
    SetCompressionText(FindPaneByID(Caption_Compression), mCodec, mSpatialQuality);
}
 
 
void CPict2VRWindow::SetPictFile(
    FSSpec *inMacFSSpec)
{
    P2VRPrefsHdl prefs = gApp->GetPreferences();
    
    try
    {
            // set the source pict file caption
        mSrcSpec = *inMacFSSpec;
        LPane *pictFileCaption = FindPaneByID(Caption_PictFile);
        ThrowIfNil_(pictFileCaption);
        Str255 pathStr;
        GetFullPathName(inMacFSSpec, pathStr);
        SetSizedDescriptor(this, Caption_PictFile, pathStr);
        
            // read the pict's frame to use for checking pan parameters
        ReadPictFrame(*inMacFSSpec, mPictFrame);
        
        unsigned char charspace;
        
            // set a default tile file
        mTileSpec = *inMacFSSpec;
        if (prefs != nil)
        {
            charspace = 31 - (*prefs)->tileSuffix[0];
            if (mTileSpec.name[0] > charspace)
                mTileSpec.name[0] = charspace;
            ConcatPStr(mTileSpec.name, (*prefs)->tileSuffix);
        }
        else
        {
            charspace = 31 - 5;
            if (mTileSpec.name[0] > charspace)
                mTileSpec.name[0] = charspace;
            ConcatPStr(mTileSpec.name, "\p.tile");
        }
        GetFullPathName(&mTileSpec, pathStr);
        SetSizedDescriptor(this, Caption_TileFile, pathStr);
        
            // set a default dest file
        mDestSpec = *inMacFSSpec;
        if (prefs != nil)
        {
            charspace = 31 - (*prefs)->movieSuffix[0];
            if (mDestSpec.name[0] > charspace)
                mDestSpec.name[0] = charspace;
            ConcatPStr(mDestSpec.name, (*prefs)->movieSuffix);
        }
        else
        {
            charspace = 31 - 4;
            if (mDestSpec.name[0] > charspace)
                mDestSpec.name[0] = charspace;
            ConcatPStr(mDestSpec.name, "\p.snm");
        }
        GetFullPathName(&mDestSpec, pathStr);
        SetSizedDescriptor(this, Caption_QTVRFile, pathStr);
        
        SetDescriptor(inMacFSSpec->name);
        
        Show();
    }
    catch (ExceptionCode err)
    {
        delete this;
        throw(err);
    }
}
 
 
/* CPict2VRWindow::ListenToMessage
 
    Listen in on what is happening in the user interface.
*/
void CPict2VRWindow::ListenToMessage(
    MessageT inMessage,
    void *ioParam)
{
    switch (inMessage)
    {
        case Button_Create:
        {
            MovieMakinRec params;
            LPane
                *widthEdit = FindPaneByID(Edit_Width),
                *heightEdit = FindPaneByID(Edit_Height),
                *panEdit = FindPaneByID(Edit_Pan),
                *tiltEdit = FindPaneByID(Edit_Tilt),
                *zoomEdit = FindPaneByID(Edit_Zoom);
            Str255 valueStr;
            params.width = widthEdit->GetValue();
            params.height = heightEdit->GetValue();
            panEdit->GetDescriptor(valueStr);
            params.pan = StrToFixed(valueStr);
            tiltEdit->GetDescriptor(valueStr);
            params.tilt = StrToFixed(valueStr);
            zoomEdit->GetDescriptor(valueStr);
            params.zoom = StrToFixed(valueStr);
            params.srcSpec = mSrcSpec;
            params.tileSpec = mTileSpec;
            params.destSpec = mDestSpec;
            params.codec = mCodec;
            params.spatialQuality = mSpatialQuality;
            params.depth = mDepth;
            
            BroadcastMessage(msg_MakeMovie, &params);
            
            if (!params.completed)
                break;
            // else fall through
        }
        
        case Button_Cancel:
            delete this;
            break;
        
        case Button_Compression:
        {
            ComponentInstance ci = 
                ::OpenDefaultComponent(StandardCompressionType, StandardCompressionSubType);
            ThrowIfNil_(ci);
            
            try
            {
                ComponentResult result;
                
                SCSpatialSettings ss;
                ss.codecType = mCodec;
                ss.spatialQuality = mSpatialQuality;
                ss.depth = mDepth;
                ss.codec = nil;
                result = ::SCSetInfo(ci, scSpatialSettingsType, &ss);
                ThrowIf_(result < 0);
                
                LFile srcPictFile(mSrcSpec);
                Int16 fileRef;
                try
                {
                    
                    fileRef = srcPictFile.OpenDataFork(fsRdPerm);
                    OSErr err = ::SCSetTestImagePictFile(ci, fileRef, nil, scPreferCropping);
                    ThrowIfOSErr_(err);
                }
                catch (ExceptionCode inErr2)
                {
                    srcPictFile.CloseDataFork();
                    
                    throw(inErr2);
                }
                
                result = ::SCRequestImageSettings(ci);
                srcPictFile.CloseDataFork();
                ThrowIf_(result < 0);
                
                if (result != scUserCancelled)
                {
                    result = ::SCGetInfo(ci, scSpatialSettingsType, &ss);
                    ThrowIf_(result < 0);
                    mCodec = ss.codecType;
                    mSpatialQuality = ss.spatialQuality;
                    mDepth = ss.depth;
                    
                    SetCompressionText(FindPaneByID(Caption_Compression), mCodec, mSpatialQuality);
                }
            }
            catch (ExceptionCode inErr)
            {
                ::CloseComponent(ci);
                ReportError(inErr, err_ErrorSettingCompression);
            }
            
            ::CloseComponent(ci);
            break;
        }
        
        case Button_SetTileFile:
        {
            StandardFileReply reply;
            
            ::UDesktop::Deactivate();
            ::StandardPutFile("\pTile destination file:", "\p", &reply);
            ::UDesktop::Activate();
            
            if (reply.sfGood)
            {
                FSSpec parentSpec;
                (void)GetFileParent(&reply.sfFile, &parentSpec);
                Str255 descStr;
                GetFullPathName(&parentSpec, descStr);
                ConcatPStr(descStr, reply.sfFile.name);
                
                SetSizedDescriptor(this, Caption_TileFile, descStr);
                mTileSpec = reply.sfFile;
            }
            break;
        }
        
        case Button_SetVRFile:
        {
            StandardFileReply reply;
            
            ::UDesktop::Deactivate();
            ::StandardPutFile("\pQTVR destination file:", "\p", &reply);
            ::UDesktop::Activate();
            
            if (reply.sfGood)
            {
                FSSpec parentSpec;
                (void)GetFileParent(&reply.sfFile, &parentSpec);
                Str255 descStr;
                GetFullPathName(&parentSpec, descStr);
                ConcatPStr(descStr, reply.sfFile.name);
                
                SetSizedDescriptor(this, Caption_QTVRFile, descStr);
                mDestSpec = reply.sfFile;
            }
            break;
        }
        
        case Button_MakeDefault:
        {
            P2VRPrefsHdl prefs = gApp->GetPreferences();
            if (prefs != nil)
            {
                (*prefs)->depth = mDepth;
                (*prefs)->codec = mCodec;
                (*prefs)->spatialQuality = mSpatialQuality;
                (*prefs)->width = GetValueForPaneID(Edit_Width);
                (*prefs)->height = GetValueForPaneID(Edit_Height);
            }
            break;
        }
        
        default:
            
            LDialogBox::ListenToMessage(inMessage, ioParam);
    }
}