home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
develop, the CD; issue 1
/
Apple_Develop_1989.bin
/
Compatibility
/
Printing
/
PRSave.p
< prev
next >
Wrap
Text File
|
1989-12-10
|
9KB
|
246 lines
PROGRAM PRSave;
(* *)
(* PrSave.p *)
(* *)
(* 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. *)
USES Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf,MacPrint;
CONST
gPRResType = 'POPT'; (* 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... *)
gPRResID = 128; (* This can also be any value. Since there should *)
(* only be one print record per document, the ID is *)
(* a constant. *)
gPRResName = 'Print Record'; (* Resource name. *)
VAR
gPrintRecordHdl: THPrint;
gTargetResFile: INTEGER;
(* 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". *)
PROCEDURE ReportError(theError: OSErr);
BEGIN
(* Real programs handle errors by displayed comprehensible error messages. *)
(* This is NOT a real program... *)
SysBeep(10);
END;
(* 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. *)
PROCEDURE InitializePrintRecord(thePrintRecord: THPrint);
VAR
ignored: BOOLEAN;
BEGIN
PrOpen;
IF PrError = noErr THEN BEGIN
PrintDefault(thePrintRecord);
ignored := PrStlDialog(thePrintRecord);
END;
PrClose;
END;
(* 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. *)
PROCEDURE SavePrintRecord(thePrintRecord: THPrint; theResFile: INTEGER);
VAR
currentResFile: INTEGER;
existingResHdl: Handle;
newResHdl: Handle;
theError: OSErr;
BEGIN
(* 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 THEN BEGIN
existingResHdl := GetResource(gPRResType, gPRResID);
IF existingResHdl <> NIL THEN BEGIN
(* 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 THEN BEGIN
(* If the resource was successfully removed, dispose of its memory *)
(* and update the resource file. *)
DisposHandle(existingResHdl);
UpdateResFile(theResFile);
END;
END;
IF theError = noErr THEN BEGIN
(* 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 THEN BEGIN
AddResource(newResHdl, gPRResType, gPRResID, gPRResName);
theError := ResError;
IF theError = noErr THEN UpdateResFile(theResFile);
theError := ResError;
END;
END;
END;
IF theError <> noErr THEN
ReportError(theError);
(* Be polite and restore the original resource file to the top of the chain. *)
UseResFile(currentResFile);
END;
(* GetPrintRecord *)
(* *)
(* This function is responsible for loading a resource containing a valid print *)
(* record. On entry theResFile should be open with permission to read. *)
FUNCTION GetPrintRecord(theResFile: INTEGER): THPrint;
VAR
currentResFile: INTEGER;
theResource: Handle;
theError: OSErr;
BEGIN
currentResFile := CurResFile;
UseResFile(theResFile);
theError := ResError;
IF theError = noErr THEN BEGIN
theResource := GetResource(gPRResType, gPRResID);
theError := ResError;
IF theError = noErr THEN BEGIN
PrOpen;
theError := PrError;
IF theError = noErr THEN
IF PrValidate(THPrint(theResource)) THEN ;
PrClose;
END;
END;
IF theError <> noErr THEN
ReportError(theError);
UseResFile(currentResFile);
GetPrintRecord := THPrint(theResource);
END;
(* 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. *)
PROCEDURE TestPrintRecord(thePrintRecord: THPrint);
VAR
currentPort: GrafPtr;
thePMPort: TPPrPort;
thePMStatus: TPrStatus;
theError: OSErr;
BEGIN
GetPort(currentPort);
PrOpen;
IF PrError = noErr THEN BEGIN
IF PrJobDialog(thePrintRecord) THEN BEGIN
thePMPort := PrOpenDoc(thePrintRecord, NIL, NIL);
IF PrError = noErr THEN BEGIN
PrOpenPage(thePMPort, NIL);
IF PrError = noErr THEN BEGIN
SetPort(@thePMPort^.gPort);
MoveTo(100, 100);
DrawString('This is a test...');
END;
PrClosePage(thePMPort);
END;
PrCloseDoc(thePMPort);
IF ((thePrintRecord^^.prJob.bJDocLoop = bSpoolLoop) AND (PrError = noErr)) THEN
PrPicFile(thePrintRecord, NIL, NIL, NIL, thePMStatus);
END;
END;
theError := PrError; (* Any errors? *)
PrClose; (* Close the Printing Manager before attempting *)
(* to report the error. *)
IF theError <> noErr THEN (* If there was an error during printing... *)
ReportError(theError); (* ...report the error to the user. *)
SetPort(currentPort);
END;
BEGIN
InitGraf(@thePort);
InitFonts;
FlushEvents(everyEvent, 0);
InitWindows;
InitMenus;
TEInit;
InitDialogs(NIL);
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 <> NIL THEN BEGIN
(* 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. *)
DisposHandle(Handle(gPrint