AlwaysPreview.c

/*
    File:       AlwaysPreview.c
 
    Contains:   This simple app demonstrates how to force the preview enabled mode by install a modal
                dialog filter proc in the CustomGetFilePreview routine. This works with System 7 call.
                You should check out the Standard File documentation in Inside Mac: Files as well.
 
                This sample is based on CustomPreview code in QT1.0 CD.
 
    Written by: Larry Lai   
 
    Copyright:  Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/28/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include "ImageCompression.h"
#include <Files.h>
#include <GestaltEqu.h>
#include <StandardFile.h>
#include <Controls.h>
#include <Fonts.h>
#include <ToolUtils.h>
 
// some constants for the buttons we added to the dialog
enum {
    buttonPictureFiles = 16,
    buttonAllFiles
};
 
// prototypes
void updateFileButtons(DialogPtr d, Boolean showAllFiles);
pascal short myDialogHook7(short item, DialogPtr d, Boolean *showAllFiles);
pascal Boolean myFileFilter7(CInfoPBPtr pb, Boolean *showAllFiles);
Boolean doCustom7(FSSpec *fss);
pascal  Boolean myModalFilter7(DialogPtr theDlg, EventRecord *theEvent, short *itemHit, Boolean *showAllFiles);
 
 
/*
    resource id numbers of the dialogs.
 
    Note that both of these dialogs have a Dialog Color Table ('dctb')
    resource as well. This is so that the window used by Standard File will
    be a color window. In some cases this may allow previews to display more
    accurate colors. On System 7, owing to a bug in the PopUp menu CDEF, it
    is necessary to make it a color window, or else the CDEF draws incorrectly
    in some cases (when the port origin is not (0,0)).
*/
#define Custom7DialogID (129)
 
// a global to keep track of whether we are showing all files or just pictures
Boolean gShowAllFiles = false;
 
Boolean doCustom7(FSSpec *fss);         // returns true if a file was selected
 
/*
     routine shared by both the system 6 and system 7 filters. 
     this routine simply hilites the two radio buttons depending on
        the value of the "showAllFiles" parameter.
*/
void updateFileButtons(DialogPtr d, Boolean showAllFiles)
{
    short kind;
    Rect r;
    ControlHandle ch;
 
    GetDialogItem(d, buttonPictureFiles, &kind, (Handle *)&ch, &r);
    SetControlValue(ch, !showAllFiles);
 
    GetDialogItem(d, buttonAllFiles, &kind, (Handle *)&ch, &r);
    SetControlValue(ch, showAllFiles);
}
 
 
/*****************************************************
 
    System 7 Preview code
 
    Note that the system 7 standard file interface allows for a
        YDPtr (YourDataPtr) to be passed to all filters. This means
        that the filter procs do not need to use any global
        variables.
 
******************************************************/
 
pascal short myDialogHook7(short item, DialogPtr d, Boolean *showAllFiles)
{
 
    switch (item) {
        case sfHookFirstCall:
                // put buttons in correct initial state
                updateFileButtons(d, *showAllFiles);
                break;
        case buttonPictureFiles:
                if (*showAllFiles) {
                    *showAllFiles = false;
                    updateFileButtons(d, *showAllFiles);
                    item = sfHookRebuildList;   // force standard file to redisplay list
                }
                break;
        case buttonAllFiles:
                if (!*showAllFiles) {
                    *showAllFiles = true;
                    updateFileButtons(d, *showAllFiles);
                    item = sfHookRebuildList;   // force standard file to redisplay list
                }
                break;
    }
 
    return(item);
}
 
pascal Boolean myFileFilter7(CInfoPBPtr pb, Boolean *showAllFiles)
{
    /*
        return false to indicate that file/directory should be displayed.
        note that unlike the system 6 version of this filter, you can suppress the
        display of directories as well as files. thus the extra check for directories
        is required below
    */
 
    if (*showAllFiles)
        return(false);
    else {
        if (pb->hFileInfo.ioFlFndrInfo.fdType == 'PICT')
            return(false);
        else
            return(!(pb->hFileInfo.ioFlAttrib & 16));       // directory
    }
}
 
pascal  Boolean myModalFilter7(DialogPtr theDlg, EventRecord *theEvent, short *itemHit, Boolean *showAllFiles)
{
    short   iType;
    Handle  iHandle;
    Rect    iRect;
    short   c;
    
    *showAllFiles = true;
    
    /*
        handle null event, set the control value of show preview checkbox to 1 if it is 
        unchecked. event from ModalDialog will pass here first before it gets handed to
        the standard file package filter
        
    */
 
    switch (theEvent->what) {
        case nullEvent:
            *itemHit = 15;  // 15 is the resource ID of show preview checkbox
            GetDialogItem(theDlg, *itemHit, &iType, &iHandle, &iRect);
            c = GetControlValue ((ControlHandle) iHandle);
    
            if (!c){
                SetControlValue((ControlHandle)iHandle, true);
            }
            else
                return false;
                
            return true;
            break;
        default:
            return false;
    }
}
        
Boolean doCustom7(FSSpec *fss)
{
    #pragma unused(fss)
    Point where;
    StandardFileReply reply;
 
    where.h = where.v = -2;             // center the dialog on the "best" screen
 
    CustomGetFilePreview(NewFileFilterYDProc(myFileFilter7), -1, 0, &reply, Custom7DialogID, where,
                            NewDlgHookYDProc(myDialogHook7), NewModalFilterYDProc(myModalFilter7), 0, 0, &gShowAllFiles);
 
    return(reply.sfGood);
}
 
/*
    Simple main routine.
 
*/
void main(void)
{
    long response;
    FSSpec fileSpec;
 
    // initialize the world
    InitGraf(&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(0L);
    InitCursor();
    MaxApplZone();
 
    // must have image compression manager to use Standard Preview
    if (Gestalt(gestaltCompressionMgr, &response))
        return;
 
    // system 7 calls are only available if the standard file selectors 5 through 8 are around
    if ( (Gestalt(gestaltStandardFileAttr, &response) == noErr) &&
        BitTst((Ptr)&response, 31 - gestaltStandardFile58) )
        doCustom7(&fileSpec);
}