home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / comm / news102.sit / NewsWatcher / source / printstuff.c < prev    next >
Text File  |  1991-04-03  |  5KB  |  223 lines

  1. /*----------------------------------------------------------
  2. #
  3. #    NewsWatcher    - Macintosh NNTP Client Application
  4. #
  5. #    Written by Steven Falkenburg
  6. #    ⌐1990 Apple Computer, Inc.
  7. #
  8. #-----------------------------------------------------------
  9. #
  10. #    printstuff.c
  11. #
  12. #    This module contains code which prints the contents of
  13. #    a textedit record to a printer.
  14. #
  15. #-----------------------------------------------------------*/
  16.  
  17. #include "compat.h"
  18.  
  19. #ifdef PROTOS
  20. #include <Types.h>
  21. #include <Memory.h>
  22. #include <QuickDraw.h>
  23. #include <ToolUtils.h>
  24. #include <OSUtils.h>
  25. #include <Printing.h>
  26. #include <Lists.h>
  27. #endif
  28.  
  29. #include "nntp.h"
  30. #include "printstuff.h"
  31. #include "miscstuff.h"
  32.  
  33. #define odd(theInt) ( (theInt % 2) == 1 )
  34.  
  35. extern TEHandle gFrontTE;        /* frontmost textedit handle */
  36.  
  37. static THPrint myHPrint;        /* printing handle */
  38. static TEHandle printTE;        /* textedit handle to print */
  39. static Handle txt;                /* text to print */
  40. static short linesPerPage;        /* # of lines per page */
  41.  
  42. static TPPrPort        prPort;        /* printing port */
  43. static Boolean        prIsOpen,docIsOpen,pageIsOpen;    /* printing flags */
  44.  
  45. void CleanUp(void)
  46. {
  47.     MyIOCheck(PrError());
  48.     if (pageIsOpen)
  49.         PrClosePage(prPort);
  50.     if (docIsOpen)
  51.         PrCloseDoc(prPort);
  52.     if (prIsOpen)
  53.         PrClose();
  54. }
  55.  
  56.  
  57. OSErr ExecutePrint(THPrint hPrint)
  58. {
  59.     short theFirst,theLast;
  60.     Boolean wasnil,scratch;
  61.     short nCopies;
  62.     short prDevice;
  63.     Boolean draftMode;
  64.     TPrStatus prStatus;
  65.     GrafPtr savePort;
  66.     OSErr curPrError;
  67.     Rect pageRect;
  68.     short i,p;
  69.  
  70.     prIsOpen = docIsOpen = pageIsOpen = false;
  71.     GetPort(&savePort);
  72.     wasnil = (hPrint == nil);
  73.     PrOpen();
  74.     if (PrError() != noErr) {
  75.         CleanUp();
  76.         return(PrError());
  77.     }
  78.     prIsOpen = true;
  79.     scratch = PrValidate(hPrint);
  80.     if (!PrJobDialog(hPrint)) {
  81.         CleanUp();
  82.         return(PrError());
  83.     }
  84.     if (!MyPrepProc(hPrint)) {
  85.         CleanUp();
  86.         return(PrError());
  87.     }
  88.     prDevice = ( (**hPrint).prStl.wDev >> 8 );
  89.     draftMode = !(odd((**hPrint).prJob.bJDocLoop));
  90.     if ((draftMode) && (prDevice == 1))
  91.         nCopies = (**hPrint).prJob.iCopies;
  92.     else
  93.         nCopies = 1;
  94.     
  95.     prPort = PrOpenDoc(hPrint,nil,nil);
  96.     docIsOpen = true;
  97.     curPrError = PrError();
  98.     if (curPrError != noErr) {
  99.         SetPort(savePort);
  100.         return(curPrError);
  101.     }
  102.     SetPort(&prPort->gPort);
  103.     for (i=1; i<=nCopies; i++) {
  104.         theFirst = (**hPrint).prJob.iFstPage;
  105.         theLast = (**hPrint).prJob.iLstPage;
  106.         (**hPrint).prJob.iFstPage = 1;
  107.         (**hPrint).prJob.iLstPage = 9999;
  108.         for (p=theFirst; p<=theLast; p++) {
  109.             PrOpenPage(prPort,nil);
  110.             pageIsOpen = true;
  111.             HLock((Handle)hPrint);
  112.             pageRect = (**hPrint).prInfo.rPage;
  113.             scratch = MyPageProc(hPrint,&pageRect,p);
  114.             HUnlock((Handle)hPrint);
  115.             PrClosePage(prPort);
  116.             if (!scratch) {
  117.                 curPrError = iPrAbort;
  118.                 CleanUp();
  119.                 return(PrError());
  120.             }
  121.         }
  122.     }
  123.     PrCloseDoc(prPort);
  124.     docIsOpen = false;
  125.     curPrError = PrError();
  126.     
  127.     if ( !draftMode && (curPrError != noErr))
  128.         PrPicFile(hPrint,nil,nil,nil,&prStatus);
  129.     curPrError = PrError();
  130.     PrClose();
  131.     prIsOpen = false;
  132.     
  133.     return(noErr);
  134. }
  135.  
  136.  
  137. Boolean MyPrepProc(THPrint theHPrint)
  138. {
  139.     short numPages,scrVert,scrHori;
  140.     float docInches,printInches;
  141.  
  142.     ScreenRes(&scrVert,&scrHori);
  143.     printInches = ((float) ( (**theHPrint).prInfo.rPage.bottom - (**theHPrint).prInfo.rPage.top)) / ((float)(**theHPrint).prInfo.iVRes);
  144.     linesPerPage = (printInches / ( ((float)((**printTE).lineHeight)) / ((float) scrVert)) );
  145.     if ((**theHPrint).prJob.iLstPage == 9999) {
  146.         docInches = (**gFrontTE).lineHeight * (**gFrontTE).nLines / 72.0;
  147.         numPages = docInches / printInches;
  148.         if ( (docInches / printInches) != numPages )
  149.             numPages++;
  150.         (**theHPrint).prJob.iLstPage = numPages;
  151.     }
  152.     if ( (**theHPrint).prJob.iLstPage > 0 )
  153.         return(true);
  154.     else
  155.         return(false);
  156. }
  157.  
  158. Boolean MyPageProc(THPrint theHPrint,Rect *drawRect,short pageNum)
  159. {
  160.     Ptr txtPtr;
  161.     short start,length;
  162.     
  163.     #pragma unused (theHPrint)
  164.     
  165.     HLock(txt);
  166.     TextFont((**printTE).txFont);
  167.     TextFace((**printTE).txFace);
  168.     TextMode((**printTE).txMode);
  169.     TextSize((**printTE).txSize);
  170.     start = (**printTE).lineStarts[((pageNum - 1) * linesPerPage)];
  171.     if ((**printTE).nLines < (pageNum * linesPerPage))
  172.         length = (**printTE).teLength - start;
  173.     else
  174.         length = (**printTE).lineStarts[(pageNum * linesPerPage)] - start;
  175.     txtPtr = *txt + start;
  176.     TextBox(txtPtr,length,drawRect,teJustLeft);
  177.     HUnlock(txt);
  178.     return(true);
  179. }
  180.  
  181. void InitPrint(void)
  182. {
  183.     PrOpen();
  184.     MyIOCheck(PrError());
  185.     myHPrint = (THPrint) MyNewHandle(sizeof(TPrint));
  186.     if (MyMemErr() != noErr)
  187.         return;
  188.     PrintDefault(myHPrint);
  189.     MyIOCheck(PrError());
  190.     PrClose();
  191. }
  192.  
  193. void DoPageSetup(void)
  194. {
  195.     InitCursor();
  196.     PrOpen();
  197.     if (PrError() == noErr) {
  198.         PrValidate(myHPrint);
  199.         if (PrError() == noErr)
  200.             PrStlDialog(myHPrint);
  201.         else MyIOCheck(PrError());
  202.     }
  203.     else
  204.         MyIOCheck(PrError());
  205.     PrClose();
  206.     MyIOCheck(PrError());
  207. }
  208.  
  209. void DoPrint(void)
  210. {
  211.     InitCursor();
  212.     printTE = gFrontTE;
  213.     if (MyHandToHand((Handle *)&printTE) != noErr)
  214.         return;
  215.     txt = (**gFrontTE).hText;
  216.     if (MyHandToHand(&txt) != noErr)
  217.         return;
  218.     (**printTE).hText = txt;
  219.     MyIOCheck( ExecutePrint(myHPrint) );
  220.     MyDisposHandle(txt);
  221.     MyDisposHandle((Handle)printTE);
  222. }
  223.