PrintDialogMagic.c

/*
    File:       PrintDialogMagic.c
 
    Contains:   PrintDialogMagic.c -- an example of how to print from an application
                while still calling the print dialog (so that LW8.3 and such do the
                right thing with the print record), but not requiring the user to
                actually press any buttons or anything. This is most useful for apps
                that want to do automated printing, such as a log to printer option
                within some sort of server application.
 
    Written by: Dave Polaschek  
 
    Copyright:  Copyright © 1996-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/26/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include <Printing.h>
#include <MixedMode.h>
#include <SegLoad.h>
#include <Resources.h>
#include <ToolUtils.h>
#include <Fonts.h>
 
/* Declare ÔpascalÕ functions and procedures */
 
pascal  TPPrDlg MyJobDlgInit(THPrint);      /* Our extention to PrJobInit.      */
OSErr   Print(void);
pascal  Boolean myFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
 
static TPPrDlg PrtJobDialog;        /* pointer to job dialog */
 
static ModalFilterUPP   myFilterUPP;
 
/*------------------------------------------------------------------------*/
 
OSErr Print(void)
{
    TPPrPort    pPrPort;
    Rect        aRect;
    TPrStatus   theStatus;
    THPrint     hPrintRec;
    
    
    /* call PrJobInit to get pointer to the invisible job dialog */
    hPrintRec = (THPrint)(NewHandle(sizeof(TPrint)));
    PrintDefault(hPrintRec);
    PrValidate(hPrintRec);
    if (PrError() != noErr)
        return PrError();        
 
    PrtJobDialog = PrJobInit(hPrintRec);
       
    if (PrError() != noErr)
        return PrError();        
 
    if (!PrDlgMain(hPrintRec, NewPDlgInitProc(MyJobDlgInit))) /* this line does all the stuff */
        return iPrAbort;
 
    if (PrError() != noErr)
        return PrError();        
 
    pPrPort = PrOpenDoc(hPrintRec, NULL, NULL);
    PrOpenPage(pPrPort, NULL);
    
    aRect = (*hPrintRec)->prInfo.rPage;
    InsetRect(&aRect, 20, 20);
    PenSize(4, 4);
    FrameRect(&aRect);
    
    PrClosePage(pPrPort);
    PrCloseDoc(pPrPort);
 
    if (!PrError() && (*hPrintRec)->prJob.bJDocLoop == bSpoolLoop)
        PrPicFile(hPrintRec, NULL, NULL, NULL, &theStatus);
 
 
/* that's all for now */
        
    if (hPrintRec) DisposeHandle((Handle) hPrintRec);
    
    return(noErr);
} /* Print */
 
/*------------------------------------------------------------------------*/
 
pascal TPPrDlg MyJobDlgInit(THPrint hPrint)
/*
   this routine appends items to the standard job dialog and sets up the
   user fields of the printing dialog record TPRDlg
 
   This routine will be called by PrDlgMain
*/
{
#pragma unused(hPrint)
    /* we're going to be simple-minded about this, and just patch the
    ** modal-filter proc for the dialog, and have it say that the user
    ** actually hit the enter-key
    
    ** a more elaborate scheme would hide the dialog, patch ShowWindow
    ** and/or ShowHide and patch out ModalDialog so that the dialog
    ** never even appeared.
    
    ** Our method will briefly show the dialog, which will immediately
    ** go away, which will blink the screen a little. Bummer.
    */
    
    PrtJobDialog->pFltrProc = myFilterUPP;
    
    return PrtJobDialog;
} /*myJobDlgInit*/
 
pascal  Boolean myFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
{
#pragma unused(theDialog)
#pragma unused(theEvent)
    *itemHit = 1;
    return(true);
}
 
 
void main(void)
{    
    Rect        myWRect;
    OSErr       err;
    
    InitGraf(&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    InitDialogs((long)nil);
    InitCursor();
    SetRect(&myWRect,50,260,350,340);
    
    /* call the routine that does printing */
    PrOpen();
    /* build my UPP */
    myFilterUPP = NewModalFilterProc(myFilter);
    err = Print();
    /* clean up */
    DisposeRoutineDescriptor(myFilterUPP);
    
    PrClose();
} /* main */