home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacGofer 0.22d / MacGofer Sources / mac_print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-06  |  7.1 KB  |  381 lines  |  [TEXT/MPS ]

  1. /*****************************************************************************
  2.  
  3.   mac_print.c.   All rights reserved.
  4.   
  5.  Printing Routines.
  6.  
  7.  original version by Kevin Hammond
  8.  
  9.  Updated by MS and AS
  10.      27 Apr 1992   MS
  11.          -- re-designed print engine
  12.      10 May 1992   MS
  13.          -- added support for multiple copies
  14.  
  15.   updated:   10 May 1992 MS
  16.  
  17. *****************************************************************************/
  18.  
  19. #include "mac.h"        /* Common */
  20.  
  21. #pragma segment Print
  22.  
  23. /* margins -- MS
  24.  */
  25.  
  26. #define PrTopMargin     20
  27. #define PrLeftMargin    20
  28. #define PrBottomMargin  20
  29. #define PrRightMargin    20
  30. #define PrTabChar       ((char)'\t')
  31.  
  32. #define NUM_HEADER_LINES    3
  33. #define TABSIZE                8
  34.  
  35. /* TABSIZE -- MS: yes, I know most mac apps use 4, but input.c & ALL other
  36.  *                systems use 8 ... we want our code to parse OK don't we...
  37.  */
  38.  
  39. static THPrint hPrint = NIL;        /* Printer record */
  40. static int prTabWidth;
  41.  
  42.  
  43. int PrDoc( char **hText, long count, THPrint hPrint, int font, int size, 
  44.            int pno, int mpno, char *mtitle);
  45. int MyDrawText( char *p, int count);
  46.  
  47. extern CursHandle watchcurs;                /* Watch cursor */
  48.  
  49.  
  50. TPPrPort   pPrPort = NIL;             /* Printer port */
  51. TPrStatus  prStatus;                /* Printing status */
  52.  
  53.  
  54. /* Candidates for Unloading */
  55.  
  56. extern void eval(), abandon(), compileExp(), readCommand(),
  57.         printExp();
  58.  
  59.  
  60.  
  61.  
  62. /*
  63.   Do the page setup.  -- kh
  64. */ 
  65.  
  66. PageSetup()
  67. {
  68.     PrOpen();
  69.  
  70.     if (PrError() == noErr)
  71.     {
  72.         MakePrintRecord();
  73.         (void) PrStlDialog(hPrint);
  74.         PrClose();
  75.     }
  76.     else
  77.         SysBeep(1);
  78. }
  79.  
  80.  
  81. /* 
  82.   Setup the print job. -- kh
  83. */ 
  84.  
  85. void PrintJobSetup()
  86. {
  87.     PrOpen();
  88.  
  89.     if (PrError() == noErr)
  90.     {
  91.         MakePrintRecord();
  92.         (void) PrJobDialog(hPrint);
  93.         PrClose();
  94.     }
  95.     else
  96.         SysBeep(1);
  97. }
  98.  
  99.  
  100.  
  101. /* 
  102.   Make a new print record and initialise to default settings. -- kh
  103. */
  104.  
  105. MakePrintRecord()
  106. {
  107.     if (hPrint == NIL)
  108.     {
  109.         hPrint = (THPrint)NewHandle(sizeof(TPrint));
  110.         PrintDefault(hPrint);
  111.     }
  112. }
  113.  
  114.  
  115.  
  116. /* 
  117.   Open the printer and driver. -- kh -- MS
  118. */ 
  119.  
  120. OpenThePrinter(cancelled)
  121. Boolean  *cancelled;
  122. {
  123.     PrOpen();
  124.  
  125.     if (PrError() == noErr)                       /* Only do if no error */
  126.     {
  127.         MakePrintRecord();
  128.       
  129.         SetCursor(&(qd.arrow));                   /* -- MS */
  130.  
  131.         *cancelled = !PrJobDialog(hPrint);
  132.       
  133.         SetCursor(*watchcurs);                   /* -- MS */
  134.  
  135.       
  136.         updatewindows();                        /* -- AS */
  137.  
  138.         if (*cancelled)
  139.             pPrPort = NIL;
  140.         else
  141.         {
  142.             pPrPort = PrOpenDoc(hPrint, NIL, NIL);
  143.   
  144.         /*     Unload memory-intensive parts of the application */
  145.             UnloadSeg(eval);
  146.             UnloadSeg(abandon);
  147.             UnloadSeg(compileExp);
  148.             UnloadSeg(readCommand);
  149.             UnloadSeg(printExp);
  150.         }
  151.         
  152.     }
  153.     else
  154.     {
  155.         SysBeep(1);
  156.         *cancelled = TRUE;
  157.     }
  158. }
  159.  
  160.  
  161. /*
  162.   Close the printer.-- kh -- MS
  163. */
  164.  
  165. void CloseThePrinter()
  166. {
  167.  
  168.     if (pPrPort != NIL)
  169.     {
  170.         /* Clear the printing port */
  171.         PrCloseDoc(pPrPort);
  172.  
  173.         pPrPort = NIL;
  174.     }
  175.  
  176.     if (hPrint != NIL)
  177.     {
  178.         /* Spool the file */
  179.         if ((*hPrint)->prJob.bJDocLoop == bSpoolLoop && PrError() == noErr)
  180.             PrPicFile(hPrint, NIL, NIL, NIL, &prStatus);
  181.     }
  182.  
  183.     /* need to release memory locked above.
  184.       * and close
  185.      * MS - 27 Apr 1992
  186.      */
  187.          
  188.     HUnlock((Handle)hPrint);
  189.  
  190.     PrClose();
  191.  
  192. }
  193.  
  194. /*
  195.  * draw a line of text -- MS
  196.  */
  197.  
  198. MyDrawText( char *p, int count)
  199. {
  200.     register char    *p1, *p2;
  201.     int        len;
  202.     Point    pt;
  203.     
  204.     p1 = p;
  205.     p2 = p+count;
  206.     while (p<=p2) {
  207.         while ((p1<=p2) && (*p1 != PrTabChar)) p1++;
  208.         if ((len=p1-p)>0) DrawText( p, 0, p1-p);
  209.         if (*p1==PrTabChar) {
  210.             GetPen(&pt);
  211.             Move((prTabWidth-(pt.h-PrLeftMargin)%prTabWidth), 0);
  212.             p1++;
  213.         }
  214.         p = p1;
  215.     }
  216. }
  217.  
  218. /*
  219.  * Print a document -- MS
  220.  */
  221.  
  222. PrDoc( char **hText, long count, THPrint hPrint, int font, int size, 
  223.        int pno, int noLines, char *mtitle)
  224. {
  225.     register int    line = 0;
  226.     register int    lastLineOnPage = 0;
  227.     int        mpno;
  228.     int        length;
  229.     Rect    printRect;
  230.     int        linesPerPage;
  231.     int        lineBase;
  232.     int        lineHeight;
  233.     int        headerLine;
  234.     register char    *ptr, *p1;
  235.     FontInfo    info;
  236.     char    thbuff[80];
  237.     char    *hbuff = thbuff;
  238.     int        hlen;
  239.     char    pfilename[256];
  240.     
  241.     strcpy(pfilename,mtitle);
  242.     (void) c2pstr(pfilename);
  243.     (**hPrint).prJob.pFileName = (StringPtr) pfilename;
  244.  
  245.     SetPort( &(pPrPort->gPort));
  246.     TextFont( font);
  247.     TextSize( size);
  248.     printRect = (**hPrint).prInfo.rPage;
  249.     GetFontInfo( &info);
  250.     lineHeight = info.leading+info.ascent+info.descent;
  251.     linesPerPage = 
  252.        (printRect.bottom-printRect.top-PrTopMargin-PrBottomMargin)/lineHeight;
  253.     linesPerPage -= NUM_HEADER_LINES;    /* assuming linesPerPage>HEADER */
  254.     mpno = (noLines / linesPerPage);
  255.     if (noLines%linesPerPage != 0)
  256.         mpno++;
  257.     HLock( hText);
  258.     ptr = p1 = (*hText);
  259.     do
  260.         {
  261.         if (PrError() != noErr)      /* cancel or error trap */
  262.             break;
  263.             
  264.         PrOpenPage( pPrPort, NIL);
  265.         updatewindows();
  266.         if (PrError() == noErr)      /* cancel or error trap */
  267.         {
  268.             lastLineOnPage += linesPerPage;
  269.             MoveTo( printRect.left+PrLeftMargin,
  270.                 (lineBase = printRect.top+lineHeight) );
  271.             /* the header */
  272.             for( headerLine=1 ; headerLine <= NUM_HEADER_LINES ; ++headerLine)
  273.                 {
  274.                 switch (headerLine)
  275.                     {
  276.                     case 1:
  277.                         hlen = sprintf( hbuff, 
  278.                                         "Page %i of %i : %s\n", 
  279.                                         pno++, mpno, mtitle);
  280.                         MyDrawText( hbuff, hlen);
  281.                         break;
  282.                     case 2:
  283.                         Line( printRect.right -
  284.                               printRect.left -
  285.                               PrLeftMargin -
  286.                               PrRightMargin, 0);
  287.                         break;
  288.                     default:
  289.                         break;
  290.                 }
  291.                 MoveTo( printRect.left+PrLeftMargin, (lineBase += lineHeight));
  292.             }
  293.             do
  294.                 {
  295.                 /* PrintLine: */
  296.                 while ((ptr<(*hText)+count) && 
  297.                     ( (*(ptr+1) != (char)'\r') && 
  298.                       (*(ptr+1) != (char)'\n') ) ) ptr++;
  299.                 if (ptr<(*hText)+count)
  300.                     ptr++;
  301.                 if ((length=(int)(ptr-p1)-1)>0)
  302.                     MyDrawText( p1, length);
  303.                 MoveTo( printRect.left+PrLeftMargin, (lineBase += lineHeight));
  304.                 p1 = ptr;
  305.             }
  306.             while ((++line != lastLineOnPage) && (ptr<(*hText)+count));
  307.         }
  308.         PrClosePage( pPrPort);
  309.     }
  310.     while (ptr<(*hText)+count);
  311.     HUnlock(hText);
  312. }
  313.  
  314. /*
  315.   Print the front window. -- modified by MS
  316. */ 
  317.  
  318. Print_Window(windex)
  319. int windex;
  320. {
  321.   char        *fwinname;
  322.   char        wname[256];
  323.   GrafPtr   SavedPort;
  324.   Boolean   cancelled;
  325.   FontInfo    info;
  326.   
  327.   if(isLegalWindow(windex))
  328.   {
  329.     getwtitle( WINDOW(windex), wname);
  330.     fwinname = wname;
  331.  
  332.     GetFontInfo( &info );
  333.     
  334.     GetPort(&SavedPort);                    /* Get the current window port */
  335.     
  336.     CheckMemory("Print",16000 /* guess */);
  337.     OpenThePrinter(&cancelled);
  338.     
  339.     if (pPrPort != NIL && hPrint != NIL && !cancelled)
  340.       {
  341.         TEHandle    teh;
  342.         Handle        hteh;
  343.         int            thisCopy;                /* Which copy are we printing.. */
  344.         int            maxCopies;                /* How many to print            */
  345.         int            stpno;                    /* starting page number            */
  346.         
  347.         teh  = TEHANDLE(windex);
  348.         
  349.          SetCursor(*watchcurs);
  350.         
  351.         TEHLock(windex);
  352.         hteh = (Handle) ((*teh)->hText);
  353.     
  354.         stpno = 1;
  355.         
  356.         /* Print it
  357.          */
  358.          
  359.         maxCopies = (int) ( (*hPrint)->prJob.iCopies );
  360.          
  361.         for (thisCopy = 1; thisCopy <= maxCopies && PrError() == noErr;
  362.              thisCopy++)
  363.           {
  364.             prTabWidth = TABSIZE * info.widMax;
  365.             PrDoc( hteh, (*teh)->teLength, hPrint, 
  366.                   (*teh)->txFont, (*teh)->txSize, 
  367.                    stpno, (*teh)->nLines, fwinname);
  368.           }
  369.  
  370.         TEHUnlock(windex);
  371.       }
  372.  
  373.     else if (!cancelled)
  374.       SysBeep(1);
  375.       
  376.     CloseThePrinter();                            /* Close the printer */
  377.     SetPort(SavedPort);
  378.     InitCursor();
  379.   }
  380. }
  381.