home *** CD-ROM | disk | FTP | other *** search
/ develop, the CD; issue 1 / Apple_Develop_1989.bin / Compatibility / Printing / PRSave.p < prev    next >
Text File  |  1989-12-10  |  9KB  |  246 lines

  1. PROGRAM PRSave;
  2. (*                                                                                    *)
  3. (*    PrSave.p                                                                        *)
  4. (*                                                                                    *)
  5. (*    This is an example of saving a print record into a resource file.  Saving the    *)
  6. (* print record in document resource files provides a method of retaining the user    *)
  7. (* setting from the last print job.  For example, if a user elects to print a         *)
  8. (* document using landscape orientation, that information is stored in the print    *)
  9. (* record.  If the record is saved with the document, the orientation information    *)
  10. (* will be available for the next time the document is printed.  When the 'Page        *)
  11. (* Setup' dialog is presented, the user's choices from the last time the document    *)
  12. (* was printed will be displayed as defaults.  This provides a convenient, device    *)
  13. (* independent method for saving print job information.                                *)
  14. (*                                                                                    *)
  15. (*    NOTE:     Information from the Page Setup dialog is saved into the print record.    *)
  16. (*            Information from the Print dialog (i.e. # of copies, page range...) is    *)
  17. (*            considered to be per job information, and is not saved.  This method     *)
  18. (*            will not allow you to provide new defaults for the PrJobDialog.            *)
  19.  
  20. USES Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf,MacPrint;
  21.  
  22. CONST
  23.     gPRResType = 'POPT';        (* POPT = Print OPTions.  This type can be anything    *)
  24.                                 (* but to avoid confusion with Printing Manager        *)
  25.                                 (* resources, the following types should NOT be        *)
  26.                                 (* used: PREC, PDEF, & POST...                        *)
  27.     gPRResID = 128;                (* This can also be any value.  Since there should    *)
  28.                                 (* only be one print record per document, the ID is    *)
  29.                                 (* a constant.                                        *)
  30.     gPRResName = 'Print Record';    (* Resource name.                                *)
  31. VAR
  32.     gPrintRecordHdl:    THPrint;
  33.     gTargetResFile:        INTEGER;
  34.  
  35.  
  36. (* ReportError                                                                        *)
  37. (*                                                                                    *)
  38. (*    This procedure is responsible for reporting an error to the user.  This is done    *)
  39. (* by first converting the error code passed in theError into a message that can be    *)
  40. (* displayed for the user.  See Technical Note #161, "When to call PrOpen and        *)
  41. (* PrClose".                                                                        *)
  42. PROCEDURE ReportError(theError: OSErr);
  43. BEGIN
  44.     (* Real programs handle errors by displayed comprehensible error messages.        *)
  45.     (* This is NOT a real program...                                                *)
  46.     SysBeep(10);
  47. END;
  48.  
  49.  
  50. (* InitializePrintRecord                                                            *)
  51. (*                                                                                    *)
  52. (*  This procedure is responsible for initializing a newly created print record.    *)
  53. (* It begins by calling PrintDefault to fill in default values, and then presents    *)
  54. (* the standard 'Page Setup' dialog allowing the user to specify page setup options.*)
  55. (* The modified print record is then returned.                                        *)
  56. PROCEDURE InitializePrintRecord(thePrintRecord: THPrint);
  57. VAR
  58.     ignored:    BOOLEAN;
  59. BEGIN
  60.     PrOpen;
  61.     IF PrError = noErr THEN BEGIN
  62.         PrintDefault(thePrintRecord);
  63.         ignored := PrStlDialog(thePrintRecord);
  64.     END;
  65.     PrClose;
  66. END;
  67.  
  68.  
  69. (* SavePrintRecord                                                                      *)
  70. (*                                                                                      *)
  71. (*  This procedure is responsible for saving a print record into a resource file.   *)
  72. (* On entry, the print record should be initialized, and the resource file should   *)
  73. (* be open with permission to write.                                                *)
  74. PROCEDURE SavePrintRecord(thePrintRecord: THPrint; theResFile: INTEGER);
  75. VAR
  76.     currentResFile:    INTEGER;
  77.     existingResHdl:    Handle;
  78.     newResHdl:        Handle;
  79.     theError:        OSErr;
  80. BEGIN
  81.     (* First save the currently selected resource file (before calling UseResFile). *)
  82.     currentResFile := CurResFile;
  83.     
  84.     (* Now select the target resource file.                                         *)
  85.     UseResFile(theResFile);
  86.     theError := ResError;
  87.     IF theError = noErr THEN BEGIN
  88.         existingResHdl := GetResource(gPRResType, gPRResID);
  89.         IF existingResHdl <> NIL THEN BEGIN
  90.             (* There is already a print record resource in this file, so we need to *)
  91.             (* delete it before adding the new one.                                    *)
  92.             RmveResource(existingResHdl);
  93.             theError := ResError;
  94.             IF theError = noErr THEN BEGIN
  95.                 (* If the resource was successfully removed, dispose of its memory  *)
  96.                 (* and update the resource file.                                    *)
  97.                 DisposHandle(existingResHdl);
  98.                 UpdateResFile(theResFile);
  99.             END;
  100.         END;
  101.         
  102.         IF theError = noErr THEN BEGIN
  103.             (* Okay, now we have successfully opened the file, and deleted any        *)
  104.             (* previously saved print record resources.  Finally we can add the new *)
  105.             (* one...                                                                *)
  106.             (* Since the Resource Manager is going to keep the handle we pass it,    *)
  107.             (* we need to make a copy before calling AddResource.  We'll let the    *)
  108.             (* system do it for us by calling HandToHand.                            *)
  109.             newResHdl := Handle(thePrintRecord);
  110.             theError := HandToHand(newResHdl);
  111.             IF theError = noErr THEN BEGIN
  112.                 AddResource(newResHdl, gPRResType, gPRResID, gPRResName);
  113.                 theError := ResError;
  114.                 IF theError = noErr THEN UpdateResFile(theResFile);
  115.                 theError := ResError;
  116.             END;
  117.         END;   
  118.     END;
  119.     IF theError <> noErr THEN
  120.         ReportError(theError);
  121.     
  122.     (* Be polite and restore the original resource file to the top of the chain.    *)
  123.     UseResFile(currentResFile);
  124. END;
  125.  
  126.  
  127. (* GetPrintRecord                                                                    *)
  128. (*                                                                                    *)
  129. (*  This function is responsible for loading a resource containing a valid print    *)
  130. (* record.  On entry theResFile should be open with permission to read.                *)
  131. FUNCTION GetPrintRecord(theResFile: INTEGER): THPrint;
  132. VAR
  133.     currentResFile: INTEGER;
  134.     theResource:    Handle;
  135.     theError:       OSErr;
  136. BEGIN
  137.     currentResFile := CurResFile;
  138.     UseResFile(theResFile);
  139.     theError := ResError;
  140.     IF theError = noErr THEN BEGIN
  141.         theResource := GetResource(gPRResType, gPRResID);
  142.         theError := ResError;
  143.         IF theError = noErr THEN BEGIN
  144.             PrOpen;
  145.             theError := PrError;
  146.             IF theError = noErr THEN
  147.                 IF PrValidate(THPrint(theResource)) THEN ;
  148.             PrClose;
  149.         END;
  150.     END;
  151.     IF theError <> noErr THEN
  152.         ReportError(theError);
  153.     UseResFile(currentResFile);
  154.     GetPrintRecord := THPrint(theResource);
  155. END;
  156.  
  157.  
  158. (* TestPrintRecord                                                                      *)
  159. (*                                                                                      *)
  160. (*  This procedure is used to test a print record.  It will print a line of text    *)
  161. (* using the options specified in thePrintRecord passed.  On exit, a line of text   *)
  162. (* will have been printed.                                                              *)
  163. PROCEDURE TestPrintRecord(thePrintRecord: THPrint);
  164. VAR
  165.     currentPort:    GrafPtr;
  166.     thePMPort:          TPPrPort;
  167.     thePMStatus:    TPrStatus;
  168.     theError:           OSErr;
  169. BEGIN
  170.     GetPort(currentPort);
  171.     PrOpen;
  172.     IF PrError = noErr THEN BEGIN
  173.         IF PrJobDialog(thePrintRecord) THEN BEGIN
  174.             thePMPort := PrOpenDoc(thePrintRecord, NIL, NIL);
  175.             IF PrError = noErr THEN BEGIN
  176.                 PrOpenPage(thePMPort, NIL);
  177.                 IF PrError = noErr THEN BEGIN
  178.                     SetPort(@thePMPort^.gPort);
  179.                     
  180.                     MoveTo(100, 100);
  181.                     DrawString('This is a test...');
  182.                 END;
  183.                 PrClosePage(thePMPort);
  184.             END;
  185.             PrCloseDoc(thePMPort);
  186.             
  187.             IF ((thePrintRecord^^.prJob.bJDocLoop = bSpoolLoop) AND (PrError = noErr)) THEN
  188.                 PrPicFile(thePrintRecord, NIL, NIL, NIL, thePMStatus);
  189.         END;
  190.     END;
  191.     theError := PrError;        (* Any errors?                                      *)
  192.     PrClose;                    (* Close the Printing Manager before attempting *)
  193.                                 (* to report the error.                             *)
  194.     IF theError <> noErr THEN   (* If there was an error during printing...         *)
  195.         ReportError(theError);  (* ...report the error to the user.                 *)
  196.     SetPort(currentPort);
  197. END;
  198.  
  199.  
  200. BEGIN
  201.     InitGraf(@thePort);
  202.     InitFonts;
  203.     FlushEvents(everyEvent, 0);
  204.     InitWindows;
  205.     InitMenus;
  206.     TEInit;
  207.     InitDialogs(NIL);
  208.     InitCursor;
  209.  
  210.     (* Get the ID of our resource file.  Since we were just opened, the CurResFile  *)
  211.     (* will be ours.  In a real application, the resource file ID would be the ID   *)
  212.     (* of your application's document file.                                             *)
  213.     gTargetResFile := CurResFile;
  214.     
  215.     (* Create a valid print record                                                     *)
  216.     gPrintRecordHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  217.     IF gPrintRecordHdl <> NIL THEN BEGIN
  218.         (* Okay, we got a print record, now initialize it.                              *)
  219.         InitializePrintRecord(gPrintRecordHdl);
  220.         
  221.         (* Now save the print record into the resource file.                        *)
  222.         SavePrintRecord(gPrintRecordHdl, gTargetResFile);
  223.         
  224.         (* Now that it's saved, kill it off.  We'll restore it by                   *)
  225.         (* calling GetPrintRecord.                                                      *)
  226.         DisposHandle(Handle(gPrint