home *** CD-ROM | disk | FTP | other *** search
/ develop, the CD; issue 1 / Apple_Develop_1989.bin / Compatibility / Printing / PRSave.c < prev    next >
C/C++ Source or Header  |  1989-12-10  |  9KB  |  268 lines

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