home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware in MacFormat / brailler0.5b / brlr ƒ / Shell ƒ / print meat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  3.1 KB  |  120 lines  |  [TEXT/MMCC]

  1. #include "print meat.h"
  2. #include <Printing.h>
  3.  
  4. THPrint gPrinterRecord;
  5.  
  6. void InitThePrinting(void)
  7. {
  8.     gPrinterRecord = (THPrint) NewHandle(sizeof(TPrint));
  9.     if (gPrinterRecord != nil)
  10.     {
  11.         // if we got a print handle, initialize it to default values
  12.         PrOpen();
  13.         PrintDefault(gPrinterRecord);
  14.         PrClose();
  15.     };
  16. }
  17.  
  18. void DoThePageSetup(void)
  19. {
  20.     PrOpen();
  21.     if (PrError() == noErr)
  22.         (void) PrStlDialog(gPrinterRecord);
  23.     PrClose();
  24. }
  25.  
  26. void PrintText(TEHandle theText)
  27. {
  28.     const short kMargin = 20; // page margins in pixels
  29.     const Rect zeroRect = { 0, 0, 0, 0 };
  30.     short totalLines;
  31.     GrafPtr oldPort;
  32.     Rect oldViewRect;
  33.     Rect oldDestRect;
  34.     Rect viewRect;
  35.     Rect updateRect;
  36.     Rect clipRect;
  37.     short totalHeight;
  38.     short currentLine;
  39.     short scrollAmount;
  40.     TPrStatus thePrinterStatus;
  41.     Boolean printManagerIsOpen = false;
  42.     Boolean userHasCancelled = false;
  43.     short viewHeight;
  44.     TPPrPort thePrinterPort;
  45.     
  46.     if (gPrinterRecord != nil)
  47.     {
  48.         PrOpen();
  49.         if (PrJobDialog(gPrinterRecord))
  50.         {
  51.             GetPort(&oldPort);
  52.             oldViewRect = (*theText)->viewRect;
  53.             oldDestRect = (*theText)->destRect;
  54.             thePrinterPort = PrOpenDoc(gPrinterRecord, nil, nil);
  55.             printManagerIsOpen = (PrError() == noErr);
  56.         };
  57.     };
  58.     
  59.     if (printManagerIsOpen)
  60.     {
  61.         SetPort((GrafPtr) thePrinterPort);
  62.         
  63.         // re-wrap the text to fill the entire page minus margins
  64.         viewRect = (*gPrinterRecord)->prInfo.rPage;
  65.         InsetRect(&viewRect, kMargin, kMargin);
  66.         (*theText)->inPort = (GrafPtr) thePrinterPort;
  67.         (*theText)->destRect = viewRect;
  68.         (*theText)->viewRect = viewRect;
  69.         TECalText(theText);
  70.         totalLines = (*theText)->nLines;
  71.         totalHeight = TEGetHeight(totalLines, 0, theText);
  72.         (*theText)->destRect.bottom = (*theText)->destRect.top + totalHeight;
  73.         
  74.         currentLine = 1;
  75.         
  76.         while ((!userHasCancelled) && (currentLine <= totalLines))
  77.         {
  78.             PrOpenPage(thePrinterPort, nil);
  79.             scrollAmount = 0;
  80.             clipRect = (*gPrinterRecord)->prInfo.rPage;
  81.             ClipRect(&clipRect);
  82.             
  83.             viewHeight = (*theText)->viewRect.bottom - (*theText)->viewRect.top + 1;
  84.             
  85.             while (((scrollAmount + TEGetHeight(currentLine, currentLine, theText)) <= viewHeight)
  86.                         && (currentLine <= totalLines))
  87.             {
  88.                 scrollAmount += TEGetHeight(currentLine, currentLine, theText);
  89.                 currentLine++;
  90.             };
  91.             
  92.             (*theText)->viewRect.bottom = scrollAmount + kMargin;
  93.             TEDeactivate(theText); // avoid printing selections
  94.             updateRect = (*theText)->viewRect;
  95.             TEUpdate(&updateRect, theText);
  96.             ClipRect(&zeroRect); // prevent TEScroll from redrawing the text
  97.             TEScroll(0, -scrollAmount, theText); // scroll so we can print the next page
  98.             (*theText)->viewRect.bottom = viewRect.bottom; // reset to full page;
  99.             
  100.             if (PrError() == iPrAbort)
  101.                 userHasCancelled = true;
  102.             PrClosePage(thePrinterPort);
  103.         };
  104.         
  105.         PrCloseDoc(thePrinterPort);
  106.  
  107.         if ((*gPrinterRecord)->prJob.bJDocLoop == bSpoolLoop && PrError() == noErr)
  108.             PrPicFile(gPrinterRecord, nil, nil, nil, &thePrinterStatus);
  109.         PrClose();
  110.         
  111.         SetPort(oldPort);
  112.         (*theText)->inPort = oldPort;
  113.         (*theText)->viewRect = oldViewRect;
  114.         (*theText)->destRect = oldDestRect;
  115.         TECalText(theText);
  116.         updateRect = (*theText)->viewRect;
  117.         TEUpdate(&updateRect, theText);
  118.     };
  119. }
  120.