source/NavServices.c

/*
    File:       NavServices.c
    
    Description:Code that calls NavServices and handles Nav events sent back to the application.
 
    Author:     MC
 
    Copyright:  © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
    
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                ("Apple") in consideration of your agreement to the following terms, and your
                use, installation, modification or redistribution of this Apple software
                constitutes acceptance of these terms.  If you do not agree with these terms,
                please do not use, install, modify or redistribute this Apple software.
 
                In consideration of your agreement to abide by the following terms, and subject
                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                copyrights in this original Apple software (the "Apple Software"), to use,
                reproduce, modify and redistribute the Apple Software, with or without
                modifications, in source and/or binary forms; provided that if you redistribute
                the Apple Software in its entirety and without modifications, you must retain
                this notice and the following text and disclaimers in all such redistributions of
                the Apple Software.  Neither the name, trademarks, service marks or logos of
                Apple Computer, Inc. may be used to endorse or promote products derived from the
                Apple Software without specific prior written permission from Apple.  Except as
                expressly stated in this notice, no other rights or licenses, express or implied,
                are granted by Apple herein, including but not limited to any patent rights that
                may be infringed by your derivative works or by other works in which the Apple
                Software may be incorporated.
 
                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                COMBINATION WITH YOUR PRODUCTS.
 
                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                
    Change History (most recent first):
 
*/
 
#define TARGET_API_MAC_CARBON 1
 
#include "NavServices.h"
 
Movie                               gPreviewMovie;
MovieController                     gPreviewMC;
long                                gLastFilesID;
extern OSType *                     gAcceptedFlavorTypes;
extern long                         gNumGIComponents;
 
static pascal Boolean NavFileFilterProc (AEDesc* theItem, void* info, NavCallBackUserData callBackUD, NavFilterModes filterMode) {
#pragma unused (theItem, callBackUD, filterMode)
    Boolean                     display;
    NavFileOrFolderInfo*        theInfo;
    int                         i;
 
    theInfo = (NavFileOrFolderInfo*)info;
    i = 0;
    display = false;
 
    do {
        if (theInfo->isFolder) {
            display = true;
        } else {
            if (theInfo->fileAndFolder.fileInfo.finderInfo.fdType == gAcceptedFlavorTypes[i++] || 
                theInfo->fileAndFolder.fileInfo.finderInfo.fdType == MovieFileType) {
                display = true; // Show this item
            }
        }
    } while (display == false && i <= gNumGIComponents);
 
    return display;
}
 
static pascal Boolean NavPreviewProc (NavCBRecPtr callBackParms, NavCallBackUserData callBackUD) {
#pragma unused (callBackUD)
    ComponentResult             err;
    Boolean                     prevVis;
    AEDesc                      selectionDesc;
    FSSpec                      finalFSSpec;
    GraphicsImportComponent     giFromQT;
    Rect                        boundsRect;
    long                        count,
                                fileID;
    short                       resRefNum;
    HParamBlockRec              pb;
    DescType                    returnedType;
    Size                        actualSize;
    AEKeyword                   keywd;
 
    err = NavCustomControl (callBackParms->context, kNavCtlIsPreviewShowing, &prevVis);
    if (prevVis == true) {
        err = NavCustomControl (callBackParms->context, kNavCtlGetSelection, &selectionDesc);
 
        if (err == noErr) {
            (void)AECountItems (&selectionDesc, &count);
            // Just preview the last thing they selected (if they selected multiple items)
            err = AEGetNthPtr (&selectionDesc, count, typeFSS, &keywd, &returnedType, &finalFSSpec, sizeof (FSSpec), &actualSize);
            if (err == noErr) {
                CursHandle      cursor = nil;
 
                cursor = GetCursor (watchCursor);
                SetCursor (*cursor);
                err = GetGraphicsImporterForFile (&finalFSSpec, &giFromQT);
                if (err != noErr) {
                    // Don't stop the movie if we are asked to preview the
                    // same movie we are currently previewing.
                    pb.fidParam.ioCompletion = nil;
                    pb.fidParam.ioNamePtr = finalFSSpec.name;
                    pb.fidParam.ioVRefNum = finalFSSpec.vRefNum;
                    pb.fidParam.ioSrcDirID = finalFSSpec.parID;
                    (void)PBCreateFileIDRefSync (&pb);
                    fileID = pb.fidParam.ioFileID;
 
                    err = noErr;
                    if (fileID != gLastFilesID) {
                        // Preview a new movie
                        gLastFilesID = fileID;
                        if (gPreviewMovie != nil) {
                            StopMovie (gPreviewMovie);
                            DisposeMovieController (gPreviewMC);
                            DisposeMovie (gPreviewMovie);
                        }
 
                        err = OpenMovieFile (&finalFSSpec, &resRefNum, fsRdPerm);
 
                        if (err == noErr) {
                            err = NewMovieFromFile (&gPreviewMovie, resRefNum, 0, nil, newMovieActive, nil);
                            CloseMovieFile (resRefNum);
                        }
 
                        if (err == noErr) {
                            GetMovieNaturalBoundsRect (gPreviewMovie, &boundsRect);
                            // Set movie's origin to 0,0.
                            boundsRect.bottom -= boundsRect.top;
                            boundsRect.bottom += 16;    // Room for the controller.
                            boundsRect.right -= boundsRect.left;
                            boundsRect.top = 0;
                            boundsRect.left = 0;
                            ScaleImageToRect (&boundsRect, &callBackParms->previewRect, &boundsRect);
                            gPreviewMC = NewMovieController (gPreviewMovie, &boundsRect, mcScaleMovieToFit);
                        }
 
                        if (gPreviewMC != nil) {
                            (void)MCDraw (gPreviewMC, callBackParms->window);
                        }
                    } else {
                        // Just update the preview.
                        (void)MCDraw (gPreviewMC, callBackParms->window);
                    }
                } else {
                    if (gPreviewMovie != nil) {
                        StopMovie (gPreviewMovie);
                        DisposeMovieController (gPreviewMC);
                        DisposeMovie (gPreviewMovie);
                        gPreviewMC = nil;
                        gPreviewMovie = nil;
                        gLastFilesID = 0;
                    }
                    // If it's not a movie, try it as an image file.
                    // giFromQT is already valid because we got it at the start
                    // of this function.
                    err = GraphicsImportGetBoundsRect (giFromQT, &boundsRect);
 
                    if (err == noErr) {
                        ScaleImageToRect (&boundsRect, &callBackParms->previewRect, &boundsRect);
                        err = GraphicsImportSetBoundsRect (giFromQT, &boundsRect);
                    }
 
                    if (err == noErr) {
                        err = GraphicsImportDraw (giFromQT);
                    }
 
                    if (giFromQT != nil) {
                        (void)CloseComponent (giFromQT);
                    }
                }
                cursor = (CursHandle)NewHandle (sizeof (Cursor));
                GetQDGlobalsArrow (*cursor);
                SetCursor (*cursor);
                DisposeHandle ((Handle)cursor);
            }
        }
    }
 
    (void)AEDisposeDesc (&selectionDesc);
 
    if (err == noErr) {
        return true;
    } else {
        return false;
    }
}
 
static pascal void NavEventProc (NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms, NavCallBackUserData callBackUD) {
#pragma unused (callBackUD)
    OSErr                       err;
    Point                       where;
    Rect                        boundsRect;
 
    switch (callBackSelector) {
        case kNavCBEvent:
            if ((gPreviewMC != nil && MCIsPlayerEvent (gPreviewMC, callBackParms->eventData.eventDataParms.event) == 0) || gPreviewMC == nil) {
                switch (callBackParms->eventData.eventDataParms.event->what) {
                    case nullEvent:
                        MCIdle (gPreviewMC);
                        break;
                    case updateEvt:
                        err = DispatchWindowUpdate ((WindowPtr)callBackParms->eventData.eventDataParms.event->message);
                        break;
                    case mouseDown:
                        where = callBackParms->eventData.eventDataParms.event->where;
                        GlobalToLocal (&where);
                        (void)MCClick (gPreviewMC, callBackParms->window, where, (SInt32)callBackParms->eventData.eventDataParms.event->when, callBackParms->eventData.eventDataParms.event->modifiers);
                        break;
                    case kHighLevelEvent:
                        err = AEProcessAppleEvent (callBackParms->eventData.eventDataParms.event);
                        break;
                }
            }
            break;
        case kNavCBAdjustRect:
            break;
        case kNavCBAdjustPreview:
            if (gPreviewMC != nil) {
                GetMovieNaturalBoundsRect (gPreviewMovie, &boundsRect);
                // Set movie's origin to 0,0.
                boundsRect.bottom -= boundsRect.top;
                boundsRect.bottom += 16;    // Room for the controller.
                boundsRect.right -= boundsRect.left;
                boundsRect.top = 0;
                boundsRect.left = 0;
                DisposeMovieController (gPreviewMC);
                gPreviewMC = nil;
                ScaleImageToRect (&boundsRect, &callBackParms->previewRect, &boundsRect);
                gPreviewMC = NewMovieController (gPreviewMovie, &boundsRect, mcScaleMovieToFit);
 
                if (gPreviewMC != nil) {
                    (void)MCDraw (gPreviewMC, callBackParms->window);
                }
            }
            break;
        case kNavCBTerminate:
            if (gPreviewMovie != nil) {
                StopMovie (gPreviewMovie);
                DisposeMovieController (gPreviewMC);
                DisposeMovie (gPreviewMovie);
                gPreviewMC = nil;
                gPreviewMovie = nil;
                gLastFilesID = 0;
            }
            break;
    }
}
 
OSErr NavGetFilePreview (void) {
    OSErr                       err;
    NavReplyRecord              navReply;
    NavTypeListHandle           openTypeList;
    NavDialogOptions            dialogOptions;
 
    err = NavGetDefaultDialogOptions (&dialogOptions);
    if (err == noErr) {
        dialogOptions.dialogOptionFlags = kNavAllFilesInPopup | kNavAllowMultipleFiles | kNavAllowPreviews;
        openTypeList = (NavTypeListHandle)GetResource ('open', 128);
        if (openTypeList == nil) {
            err = ResError ();
            if (err == noErr) {
                err = memFullErr;
            }
        }
    }
 
    if (err == noErr) {
        NavEventUPP                 eventProc;
        NavPreviewUPP               previewProc;
        NavObjectFilterUPP          filterProc;
 
        eventProc = NewNavEventProc (NavEventProc);
        previewProc = NewNavPreviewProc (NavPreviewProc);
        filterProc = NewNavObjectFilterProc (NavFileFilterProc);
        err = NavGetFile (nil, &navReply, &dialogOptions, eventProc, previewProc, filterProc, nil, nil); //openTypeList
        DisposeNavEventUPP (eventProc);
        DisposeNavPreviewUPP (previewProc);
        DisposeNavObjectFilterUPP (filterProc);
    }
 
    if (navReply.validRecord && err == noErr) {
        ProcessSerialNumber         processSN       = {0, kCurrentProcess};
        AEAddressDesc               targetAddress   = {typeNull, nil};
        AppleEvent                  theODOC         = {typeNull, nil},
                                    theReply        = {typeNull, nil};
 
        // Create an Apple Event to ourselves.
        err = AECreateDesc (typeProcessSerialNumber, &processSN, sizeof (ProcessSerialNumber), &targetAddress);
 
        if (err == noErr) {
            // Create the open document event.
            err = AECreateAppleEvent (kCoreEventClass, kAEOpenDocuments, &targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theODOC);
            AEDisposeDesc (&targetAddress);
        }
 
        if (err == noErr) {
            // Put the list of files into the open document event Apple Event.
            err = AEPutParamDesc (&theODOC, keyDirectObject, &(navReply.selection));
        }
 
        if (err == noErr) {
            // Send the open document event to ourselves.
            err = AESend (&theODOC, &theReply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
            AEDisposeDesc (&theODOC);
            AEDisposeDesc (&theReply);
        }
 
    }
 
    (void)NavDisposeReply (&navReply);
 
    if (openTypeList != nil) {
        ReleaseResource ((Handle)openTypeList);
    }
 
    return err;
}