home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
develop, the CD; issue 1
/
Apple_Develop_1989.bin
/
Compatibility
/
Printing
/
PRSave.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-10
|
9KB
|
268 lines
/* PrSave.c */
/* */
/* This is an example of saving a print record into a resource file. Saving the */
/* print record in document resource files provides a method of retaining the user */
/* setting from the last print job. For example, if a user elects to print a */
/* document using landscape orientation, that information is stored in the print */
/* record. If the record is saved with the document, the orientation information */
/* will be available for the next time the document is printed. When the 'Page */
/* Setup' dialog is presented, the user's choices from the last time the document */
/* was printed will be displayed as defaults. This provides a convenient, device */
/* independent method for saving print job information. */
/* */
/* NOTE: Information from the Page Setup dialog is saved into the print record. */
/* Information from the Print dialog (i.e. # of copies, page range...) is */
/* considered to be per job information, and is not saved. This method */
/* will not allow you to provide new defaults for the PrJobDialog. */
/* */
#include <Types.h>
#include <Resources.h>
#include <QuickDraw.h>
#include <Fonts.h>
#include <Events.h>
#include <Windows.h>
#include <Menus.h>
#include <Memory.h>
#include <Files.h>
#include <Printing.h>
#include <Traps.h>
/* POPT = Print OPTions. This type can be anything */
/* but to avoid confusion with Printing Manager */
/* resources, the following types should NOT be */
/* used: PREC, PDEF, & POST... */
#define gPRResType 'POPT'
/* This can also be any value. Since there should */
/* only be one print record per document, the ID is */
/* a constant. */
#define gPRResID 128
/* Resource name. */
#define gPRResName "\pPrint Record"
/* Define the globals for this program... */
THPrint gPrintRecordHdl;
short gTargetResFile;
/* ReportError */
/* */
/* This procedure is responsible for reporting an error to the user. This is done */
/* by first converting the error code passed in theError into a message that can be */
/* displayed for the user. See Technical Note #161, "When to call PrOpen and */
/* PrClose". */
void ReportError(theError)
OSErr theError;
{
/* Real programs handle errors by displayed comprehensible error messages. */
/* This is NOT a real program... */
if (theError != noErr)
SysBeep(10);
}
/* InitializePrintRecord */
/* */
/* This procedure is responsible for initializing a newly created print record. */
/* It begins by calling PrintDefault to fill in default values, and then presents */
/* the standard 'Page Setup' dialog allowing the user to specify page setup options.*/
/* The modified print record is then returned. */
void InitializePrintRecord(thePrintRecord)
THPrint thePrintRecord;
{
Boolean ignored;
PrOpen();
if (PrError() == noErr) {
PrintDefault(thePrintRecord);
ignored = PrStlDialog(thePrintRecord);
}
PrClose();
}
/* SavePrintRecord */
/* */
/* This procedure is responsible for saving a print record into a resource file. */
/* On entry, the print record should be initialized, and the resource file should */
/* be open with permission to write. */
void SavePrintRecord(thePrintRecord, theResFile)
THPrint thePrintRecord;
short theResFile;
{
short currentResFile;
Handle existingResHdl;
Handle newResHdl;
OSErr theError;
/* First save the currently selected resource file (before calling UseResFile). */
currentResFile = CurResFile();
/* Now select the target resource file. */
UseResFile(theResFile);
theError = ResError();
if (theError == noErr) {
existingResHdl = GetResource(gPRResType, gPRResID);
if (existingResHdl != NULL) {
/* There is already a print record resource in this file, so we need to */
/* delete it before adding the new one. */
RmveResource(existingResHdl);
theError = ResError();
if (theError == noErr) {
/* If the resource was successfully removed, dispose of its memory */
/* and update the resource file. */
DisposHandle(existingResHdl);
UpdateResFile(theResFile);
}
}
if (theError == noErr) {
/* Okay, now we have successfully opened the file, and deleted any */
/* previously saved print record resources. Finally we can add the new */
/* one... */
/* Since the Resource Manager is going to keep the handle we pass it, */
/* we need to make a copy before calling AddResource. We'll let the */
/* system do it for us by calling HandToHand. */
newResHdl = (Handle)thePrintRecord;
theError = HandToHand(&newResHdl);
if (theError == noErr) {
AddResource(newResHdl, gPRResType, gPRResID, gPRResName);
theError = ResError();
if (theError == noErr)
UpdateResFile(theResFile);
theError = ResError();
}
}
}
if (theError != noErr)
ReportError(theError);
/* Be polite and restore the original resource file to the top of the chain. */
UseResFile(currentResFile);
}
/* GetPrintRecord */
/* */
/* This function is responsible for loading a resource containing a valid print */
/* record. On entry theResFile should be open with permission to read. */
THPrint GetPrintRecord(theResFile)
short theResFile;
{
short currentResFile;
Handle theResource;
OSErr theError;
currentResFile = CurResFile();
UseResFile(theResFile);
theError = ResError();
if (theError == noErr) {
theResource = GetResource(gPRResType, gPRResID);
theError = ResError();
if (theError == noErr) {
PrOpen();
theError = PrError();
if (theError == noErr) {
if (PrValidate((THPrint)theResource)) ;
}
PrClose();
}
}
if (theError != noErr)
ReportError(theError);
UseResFile(currentResFile);
return((THPrint)theResource);
}
/* TestPrintRecord */
/* */
/* This procedure is used to test a print record. It will print a line of text */
/* using the options specified in thePrintRecord passed. On exit, a line of text */
/* will have been printed. */
void TestPrintRecord(thePrintRecord)
THPrint thePrintRecord;
{
GrafPtr currentPort;
TPPrPort thePMPort;
OSErr theError;
TPrStatus thePMStatus;
GetPort(¤tPort);
PrOpen();
if (PrError() == noErr) {
if (PrJobDialog(thePrintRecord)) {
thePMPort = PrOpenDoc(thePrintRecord, NULL, NULL);
if (PrError() == noErr) {
PrOpenPage(thePMPort, NULL);
if (PrError() == noErr) {
SetPort(&thePMPort->gPort);
MoveTo(100, 100);
DrawString("\pThis is a test...");
}
PrClosePage(thePMPort);
}
PrCloseDoc(thePMPort);
if (((*thePrintRecord)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
PrPicFile(thePrintRecord, NULL, NULL, NULL, &thePMStatus);
}
}
theError = PrError(); /* Any errors? */
PrClose(); /* Close the Printing Manager before attempting */
/* to report the error. */
if (theError != noErr) /* If there was an error during printing... */
ReportError(theError); /* ...report the error to the user. */
SetPort(currentPort);
}
main()
{
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(NULL);
InitCursor();
/* Get the ID of our resource file. Since we were just opened, the CurResFile() */
/* will be ours. In a real application, the resource file ID would be the ID */
/* of your application's document file. */
gTargetResFile = CurResFile();
/* Create a valid print record */
gPrintRecordHdl = (THPrint)NewHandle(sizeof(TPrint));
if (gPrintRecordHdl != NULL) {
/* Okay, we got a print record, now initialize it. */
InitializePrintRecord(gPrintRecordHdl);
/* Now save the print record into the resource file. */
SavePrintRecord(gPrintRecordHdl, gTargetResFile);
/* Now that it's saved, kill it off. We'll restore it by */
/* calling GetPrintRecord.