home *** CD-ROM | disk | FTP | other *** search
/ develop, the CD; issue 1 / Apple_Develop_1989.bin / Compatibility / Printing / PRSave.Pas < prev    next >
Pascal/Delphi Source File  |  1989-07-18  |  10KB  |  221 lines

  1. PROGRAM PRSave;
  2.  
  3. {$U-}
  4. USES PasInOut,Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf,MacPrint;
  5.  
  6. CONST
  7.     gPRResType = 'POPT';      (* POPT = Print OPTions.  This type can be anything *)
  8.                               (* but to avoid confusion with Printing Manager     *)
  9.                               (* resources, the following types should NOT be     *)
  10.                               (* used: PREC, PDEF, & POST...                      *)
  11.     gPRResID = 128;           (* This can also be any value.  Since there should  *)
  12.                               (* only be one print record per document, the ID is *)
  13.                               (* a constant.                                      *)
  14.     gPRResName = 'Print Record';    (* Resource name.                             *)
  15. VAR
  16.     gPrintRecordHdl:    THPrint;
  17.     gTargetResFile:     INTEGER;
  18.  
  19. PROCEDURE Debugger; INLINE $A9FF;
  20.  
  21.  
  22. (* ReportError                                                                      *)
  23. (*                                                                                  *)
  24. (*  This procedure is responsible for reporting an error to the user.  This is done *)
  25. (* by first converting the error code passed in theError into a message that can be *)
  26. (* displayed for the user.  See Technical Note #161, "When to call PrOpen and       *)
  27. (* PrClose".                                                                        *)
  28. PROCEDURE ReportError(theError: OSErr);
  29. BEGIN
  30.     (* Real programs handle errors by displayed comprehensible error messages.  *)
  31.     (* This is NOT a real program...                                            *)
  32.     SysBeep(10);
  33. END;
  34.  
  35.  
  36. (* InitializePrintRecord                                                            *)
  37. (*                                                                                  *)
  38. (*  This procedure is responsible for initializing a newly created print record.    *)
  39. (* It begins by calling PrintDefault to fill in default values, and then presents   *)
  40. (* the standard 'Page Setup' dialog allowing the user to specify page setup options.*)
  41. (* The modified print record is then returned.                                      *)
  42. PROCEDURE InitializePrintRecord(thePrintRecord: THPrint);
  43. VAR
  44.     ignored:    BOOLEAN;
  45. BEGIN
  46.     PrOpen;
  47.     IF PrError = noErr THEN BEGIN
  48.         PrintDefault(thePrintRecord);
  49.         ignored := PrStlDialog(thePrintRecord);
  50.     END;
  51.     PrClose;
  52. END;
  53.  
  54.  
  55. (* SavePrintRecord                                                                  *)
  56. (*                                                                                  *)
  57. (*  This procedure is responsible for saving a print record into a resource file.   *)
  58. (* On entry, the print record should be initialized, and the resource file should   *)
  59. (* be open with permission to write.                                                *)
  60. PROCEDURE SavePrintRecord(thePrintRecord: THPrint; theResFile: INTEGER);
  61. VAR
  62.     currentResFile: INTEGER;
  63.     existingResHdl: Handle;
  64.     newResHdl:      Handle;
  65.     theError:       OSErr;
  66. BEGIN
  67.     (* First save the currently selected resource file (before calling UseResFile). *)
  68.     currentResFile := CurResFile;
  69.     
  70.     (* Now select the target resource file. *)
  71.     UseResFile(theResFile);
  72.     theError := ResError;
  73.     IF theError = noErr THEN BEGIN
  74.         existingResHdl := GetResource(gPRResType, gPRResID);
  75.         IF existingResHdl <> NIL THEN BEGIN
  76.             (* There is already a print record resource in this file, so we need to *)
  77.             (* delete it before adding the new one.                                 *)
  78.             RmveResource(existingResHdl);
  79.             theError := ResError;
  80.             IF theError = noErr THEN BEGIN
  81.                 (* If the resource was successfully removed, dispose of its memory  *)
  82.                 (* and update the resource file.                                    *)
  83.                 DisposHandle(existingResHdl);
  84.                 UpdateResFile(theResFile);
  85.             END;
  86.         END;
  87.         
  88.         IF theError = noErr THEN BEGIN
  89.             (* Okay, now we have successfully opened the file, and deleted any      *)
  90.             (* previously saved print record resources.  Finally we can add the new *)
  91.             (* one...                                                               *)
  92.             (* Since the Resource Manager is going to keep the handle we pass it,   *)
  93.             (* we need to make a copy before calling AddResource.  We'll let the    *)
  94.             (* system do it for us by calling HandToHand.                           *)
  95.             newResHdl := Handle(thePrintRecord);
  96.             theError := HandToHand(newResHdl);
  97.             IF theError = noErr THEN BEGIN
  98.                 AddResource(newResHdl, gPRResType, gPRResID, gPRResName);
  99.                 theError := ResError;
  100.                 IF theError = noErr THEN UpdateResFile(theResFile);
  101.                 theError := ResError;
  102.             END;
  103.         END;   
  104.     END;
  105.     IF theError <> noErr THEN
  106.         ReportError(theError);
  107.     
  108.     (* Be polite and restore the original resource file to the top of the chain.    *)
  109.     UseResFile(currentResFile);
  110. END;
  111.  
  112.  
  113. (* GetPrintRecord                                                                   *)
  114. (*                                                                                  *)
  115. (*  This function is responsible for loading a resource containing a valid print    *)
  116. (* record.  On entry theResFile should be open with permission to read.             *)
  117. FUNCTION GetPrintRecord(theResFile: INTEGER): THPrint;
  118. VAR
  119.     currentResFile: INTEGER;
  120.     theResource:    Handle;
  121.     theError:       OSErr;
  122. BEGIN
  123.     currentResFile := CurResFile;
  124.     UseResFile(theResFile);
  125.     theError := ResError;
  126.     IF theError = noErr THEN BEGIN
  127.         theResource := GetResource(gPRResType, gPRResID);
  128.         theError := ResError;
  129.     END;
  130.     IF theError <> noErr THEN
  131.         ReportError(theError);
  132.     UseResFile(currentResFile);
  133.     GetPrintRecord := THPrint(theResource);
  134. END;
  135.  
  136.  
  137. (* TestPrintRecord                                                                  *)
  138. (*                                                                                  *)
  139. (*  This procedure is used to test a print record.  It will print a line of text    *)
  140. (* using the options specified in thePrintRecord passed.  On exit, a line of text   *)
  141. (* will have been printed.                                                          *)
  142. PROCEDURE TestPrintRecord(thePrintRecord: THPrint);
  143. VAR
  144.     currentPort:    GrafPtr;
  145.     thePMPort:      TPPrPort;
  146.     theError:       OSErr;
  147. BEGIN
  148.     GetPort(currentPort);
  149.     PrOpen;
  150.     IF PrError = noErr THEN BEGIN
  151.         IF PrJobDialog(thePrintRecord) THEN BEGIN
  152.             thePMPort := PrOpenDoc(thePrintRecord, NIL, NIL);
  153.             IF PrError = noErr THEN BEGIN
  154.                 PrOpenPage(thePMPort, NIL);
  155.                 IF PrError = noErr THEN BEGIN
  156.                     SetPort(@thePMPort^.gPort);
  157.                     
  158.                     MoveTo(100, 100);
  159.                     DrawString('This is a test...');
  160.                 END;
  161.                 PrClosePage(thePMPort);
  162.             END;
  163.             PrCloseDoc(thePMPort);
  164.         END;
  165.     END;
  166.     theError := PrError;        (* Any errors?                                  *)
  167.     PrClose;                    (* Close the Printing Manager before attempting *)
  168.                                 (* to report the error.                         *)
  169.     IF theError <> noErr THEN   (* If there was an error during printing...     *)
  170.         ReportError(theError);  (* ...report the error to the user.             *)
  171.     SetPort(currentPort);
  172. END;
  173.  
  174.  
  175. BEGIN
  176.     InitGraf(@thePort);                  {initialize QuickDraw}
  177.     InitFonts;                                    {initialize Font Manager}
  178.     FlushEvents(everyEvent, 0); {call OS Event Mgr to discard any previous events}
  179.     InitWindows;                               {initialize Window Manager}
  180.     InitMenus;                                    {initialize Menu Manager}
  181.     TEInit;                                       {initialize TextEdit}
  182.     InitDialogs(NIL);                       {initialize Dialog Manager}
  183.     InitCursor;                                {call QuickDraw to make cursor (pointer) an arrow}
  184.  
  185.     (* G