source/ChooserSupportPACK.c

/*
    ChooserSupport.c - C code for PACK and LDEF resources used by the Chooser.
    
    Copyright © 1992-1994 Apple Computer, Inc.
    All rights reserved.
 
    12/20/93        dmh             Sync'd with the shipping 1.0b3 GX driver.
     8/26/94        dmh             Sync'd with the shipping 1.0.1 GX driver.
     8/26/94        dmh             Universalized code.
*/
 
#include <Types.h>
#include <QuickDraw.h>
#include <Fonts.h>
#include <Lists.h>
#include <Devices.h>
#include <Resources.h>
#include <Script.h>
#include <ToolUtils.h>
#include <LowMem.h>
 
#ifndef __GXGRAPHICS__
    #include <GXGraphics.h>
#endif
#ifndef __GXPRINTERDRIVERS__
    #include <GXPrinterDrivers.h>
#endif
 
#include "ChooserSupportProtos.h"
 
 
 
 
#if defined(__MWERKS__)
 
#define     kLeftButton         0x08000000
#define     kUsesOnAndOff       0x00100000
#define     kAcceptsInit        0x00020000
#define     kAcceptsTerminate   0x00000800
 
asm void __Startup__(void) ;
asm void __Startup__(void)
{
    BRA.S   code                            // branch to our actual code
    DC.W    169                             // device ID
    DC.L    'PACK'                          // device Type
    DC.W    0xF000                          // master ID for resources (-4096)
    DC.W    2                               // version
    DC.L    kLeftButton+kUsesOnAndOff+kAcceptsInit+kAcceptsTerminate    // flags
        
data:
    ds.b    sizeof(PACKglobalRec)
code:
    MOVE.L  (a7)+,a0        // move return address into a0
    PEA     data            // add a pointer to our global data onto stack as last parameter
    MOVE.L  a0,-(a7)        // put the return address back on the stack
    jmp     Device          // jump to it, so when it does the return, it goes to OUR caller
    rts
}
#endif  
 
 
// ------------------------------------------------------------------------
// INTERNAL DEFINES
// ------------------------------------------------------------------------
// Chooser initialize message selector
#define initializeMsg   11
 
// Icon Suite support
#define ttNone      0x0000
#define ttDisabled  0x0001
#define ttOffline   0x0002
#define ttOpen      0x0003
#define ttSelected  0x4000
#define ttSelectedDisabled  (ttSelected + ttDisabled)
#define ttSelectedOffline   (ttSelected + ttOffline)
#define ttSelectedOpen      (ttSelected + ttOpen)
 
#define ttLabel0    0x0000
#define ttLabel1    0x0100
#define ttLabel2    0x0200
#define ttLabel3    0x0300
#define ttLabel4    0x0400
#define ttLabel5    0x0500
#define ttLabel6    0x0600
#define ttLabel7    0x0700
 
 
// Copy of the DrawText trap
pascal void OldDrawText(const void *textBuf,short firstByte,short byteCount)
    = 0xA885; 
 
// ------------------------------------------------------------------------
// MAIN CODE FOR PACK
// ------------------------------------------------------------------------
pascal OSErr Device(short message, short caller, StringPtr objName, 
                    StringPtr zoneName, ListHandle theList, long p2, PACKglobalPtr global)
{
    
    OSErr           anErr = noErr;
 
    if (message == initializeMsg)   // InitializeMsg--start up GX
    {
        FCBPBRec    pb;
 
    /*
        Get the name of our driver for GXHandleChooserMessage.
        (The user may have renamed us.)
    */
        pb.ioCompletion     = nil;
        pb.ioNamePtr        = global->driverName;
        pb.ioVRefNum        = 0;
        pb.ioRefNum         = CurResFile();
        pb.ioFCBIndx        = 0;
        anErr = PBGetFCBInfo(&pb, false);
 
    /*
        Clear *pJob, because it hasn't been initialized yet.  That doesn't
        happen until we pass GXHandleChooserMessage an initializeMsg.
    */
        global->job = nil;
 
    /*
        We need to initialize GX printing so that we can call
        GXHandleChooserMessage.  Since printing requires a graphics
        client, call GXEnterGraphics first.  If there are errors,
        (for example, due to memory limitations), post an alert.
    */
        if (anErr == noErr)
        {
            GXEnterGraphics();
            anErr = GXGetGraphicsError(nil);
            if (anErr == noErr)
            {
                anErr = GXInitPrinting();
                if (anErr != noErr)
                    GXExitGraphics();
            }
                
            if (anErr != noErr)
                StopAlert(-4095, nil);
        }
    }
 
/*
    If the Chooser hasn't created a job yet, do nothing unless we were
    sent an initializeMsg.  In its default implementation of
    GXHandleChooserMessage, QuickDraw GX will create the job for our
    driver when it receives an initializeMsg.  It will store a reference
    to it in our pJob pointer.
    
    For all other messages, if a job has been created, call
    GXHandleChooserMessage to handle things.
*/
    if (anErr == noErr)
    {
        if ((global->job != nil) || (message == initializeMsg))
        {
            anErr = GXHandleChooserMessage(&(global->job), global->driverName, message, caller, objName, zoneName, theList, p2);
    
        /*
            If we just got a terminateMsg, and p2 is also terminateMsg, the
            Chooser is closing.  QuickDraw GX just disposed of the gxJob it
            created earlier when GXHandleChooserMessage was passed
            initializeMsg, so we just need to call GXExitPrinting and
            GXExitGraphics to clean up.
            
            Note that we must test the p2 parameter, because the Chooser
            can also send terminateMsg when it wants to empty the device
            list, but not dispose of us.  For example, this will happen
            when the user turns off AppleTalk in the Chooser.
        */
            if ((message == terminateMsg) && (p2 == terminateMsg))
            {
                GXExitPrinting();
                GXExitGraphics();
            }
        }
    }
        
    return anErr ;
    
} // Device