home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / luschsrc.sit / print.c < prev    next >
Text File  |  1990-05-23  |  5KB  |  212 lines

  1. /********************************************************************************
  2.  *    print.c
  3.  *
  4.  *    Printing Package
  5.  *
  6.  *    Written by Paco Xander Nathan
  7.  *    ⌐1990, Motorola Inc.  Public domain source code.
  8.  ********************************************************************************/
  9.  
  10. #include "applic.h"
  11. #include "window.h"
  12. #include "dialog.h"
  13. #include "error.h"
  14. #include "print.h"
  15. #include <PrintMgr.h>
  16. #include <IntlPkg.h>
  17.  
  18. #include "test.h"
  19. #include "gnosis.h"
  20. #include "analysis.h"
  21.  
  22.  
  23. /* Local Data Structures
  24.  */
  25. static THPrint 
  26.     printHdl = NULL;
  27.     
  28. static TPPrPort 
  29.     printPort;
  30.  
  31. static TPrStatus
  32.     theStatus;
  33.  
  34. static Boolean
  35.     printFirst = TRUE;
  36.  
  37.  
  38. #ifdef PROTOTYPES
  39. /* Local Function Prototypes
  40.  */
  41. Boolean PrintStyle (WindowPtr theWindow);
  42. void PrintText (TEHandle teHdl, Rect *pageRect);
  43. #endif
  44.  
  45.  
  46. /* Call the standard page setup dialog
  47.  */
  48. static Boolean
  49. PrintStyle (theWindow)
  50.     register WindowPtr theWindow;
  51. {
  52.     register Boolean result = TRUE;
  53.  
  54.     /* Validate the print handle if the user has selected this already...
  55.      */
  56.     if (printFirst || !theWindow) {
  57.         result = PrStlDialog(printHdl);
  58.         PrValidate(printHdl);
  59.     }
  60.  
  61.     return result;
  62. }
  63.  
  64.  
  65. /* Print a TE record, page by page
  66.  */
  67. void
  68. PrintText (teHdl, pageRect)
  69.     register TEHandle teHdl;
  70.     register Rect *pageRect;
  71. {
  72.     register TEHandle pageTE;
  73.     register CharsHandle charHdl;
  74.     register WORD maxLines, linesInto, pageLines;
  75.  
  76.     /* Create a temporary TE for imaging
  77.      */
  78.     pageTE = TENew(pageRect, pageRect);
  79.     charHdl = TEGetText(teHdl);
  80.     TESetText(*charHdl, GetHandleSize(charHdl), pageTE);
  81.  
  82.     TECalText(pageTE);
  83.     maxLines = (*pageTE)->nLines;
  84.     linesInto = 0;
  85.     pageLines = (pageRect->bottom - pageRect->top) / (*pageTE)->lineHeight;
  86.  
  87.     while (TRUE) {
  88.         /* Draw this viewRect and track how many lines have been drawn
  89.          */
  90.         TEUpdate(pageRect, pageTE);
  91.         linesInto += pageLines;
  92.  
  93.         /* Go on to another page, si necessito
  94.          */
  95.         if (linesInto < maxLines) {
  96.             PrClosePage(printPort);
  97.             PrOpenPage(printPort, NULL);
  98.         
  99.             if (PrError() == noErr) {
  100.                 SetPort(printPort);
  101.  
  102.                 /* Find the new pageRect and round it to an even number of 
  103.                  * lines per page
  104.                  */
  105.                 *pageRect = (*printHdl)->prInfo.rPage;
  106.                 pageLines = (pageRect->bottom - pageRect->top) / (*pageTE)->lineHeight;
  107.                 pageRect->bottom = pageRect->top + (pageLines * (*pageTE)->lineHeight);
  108.  
  109.                 /* Scroll to the next page
  110.                  */
  111.                 (*pageTE)->destRect = (*pageTE)->viewRect = *pageRect;
  112.                 TECalText(pageTE);
  113.                 TEScroll(0, linesInto * -(*pageTE)->lineHeight, pageTE);
  114.             }
  115.         }
  116.         else {
  117.             TEDispose(pageTE);
  118.             return;
  119.         }
  120.     }
  121. }
  122.  
  123.  
  124. /* Print the specified document window; pass in NULL for the window argument if
  125.  * only a Print Setup dialog is requested.
  126.  */
  127. void
  128. PrintDocument (theWindow)
  129.     register WindowPtr theWindow;
  130. {
  131.     register InfoPtr infoPtr = (InfoPtr) GetWRefCon(wPtrText);
  132.     register TEHandle teHdl = infoPtr->item.text.teHdl;
  133.     register WORD numPages, printErr;
  134.     Rect pageRect;
  135.     GrafPtr savePort;
  136.  
  137.     GetPort(&savePort);
  138.  
  139.     /* Unload tons of segments, since the print driver is larger than your
  140.      * worst/best nightmares of Divine after a Halloween binge
  141.      */
  142.  
  143.     /* New trip into this printing quagmire?
  144.      */
  145.     if (printFirst)
  146.         printHdl = (THPrint) ErrNewHandle(sizeof(TPrint));
  147.  
  148.     InitCursor();
  149.     PrOpen();
  150.  
  151.     if (PrError() == noErr) {
  152.         PrintDefault(printHdl);
  153.  
  154.         if (PrError() == noErr) {
  155.             if (PrintStyle(theWindow)) {
  156.                 if (theWindow && PrJobDialog(printHdl)) {
  157.                     /* get first and last pages of document to be printed
  158.                      * from the iFstPage and iLastPage fields in the
  159.                      * TPrJob record (IM II-151)
  160.                      */
  161.  
  162.                     numPages = (*printHdl)->prJob.iCopies;
  163.                     printPort = PrOpenDoc(printHdl, NULL, NULL);
  164.  
  165.                     if (PrError() == noErr) {
  166.                         for (; numPages-- > 0; ) {
  167.                             PrOpenPage(printPort, NULL);
  168.  
  169.                             if (PrError() == noErr) {
  170.                                 /* rPage (IM II-150) is the printable area
  171.                                  * and the printer port is already open so
  172.                                  * just redraw your window here
  173.                                  */
  174.                                 SetPort(printPort);
  175.                                 TextFont(FONTNUM);
  176.                                 TextSize(FONTSIZE);
  177.  
  178.                                 pageRect = (*printHdl)->prInfo.rPage;
  179.                                 AnalPrint(&pageRect);
  180.                                 PrintText(teHdl, &pageRect);
  181.                             }
  182.  
  183.                             PrClosePage(printPort);
  184.                         }
  185.                     }
  186.  
  187.                     PrCloseDoc(printPort);
  188.                 }
  189.             }
  190.         }
  191.     }
  192.  
  193.     /* Finish spool printing, if used
  194.      */
  195.     if (theWindow && (((TPPrint) *printHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
  196.         PrPicFile(printHdl, NULL, NULL, NULL, &theStatus);
  197.         
  198.     /* Grab the printer errors before closing the PrintMgr and the error disappears
  199.      */
  200.     printErr = PrError();
  201.     PrClose();
  202.     
  203.     /* Don't report printing errors before falling through the print loop, to make
  204.      * sure that all of the PrintMgr's open calls get closed properly
  205.      */
  206.     if (printErr != noErr)
  207.         SysBeep(5);
  208.     
  209.     printFirst = FALSE;
  210.     SetPort(savePort);
  211. }
  212.