home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / Apple Event Registry / AE Suites Under Development / Word Services SDK 1.0 / Writeswell Jr. Source / PrintWWJr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-25  |  6.9 KB  |  326 lines  |  [TEXT/KAHL]

  1. /* PrintWWJr.c
  2.  * Handle printing in Writeswell Jr.
  3.  *
  4.  * ©1992 Working Software, Inc.
  5.  * This source code is copyrighted.  Permission is granted to use the Word Services
  6.  * portion of the Writeswell Jr. source code in your own programs, but you 
  7.  * may not distribute the Writeswell Jr. word-processor code as a 
  8.  * commercial product.  If you modify the code, please do not call it 
  9.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  10.  * program and don’t have to deal with a number of different versions with 
  11.  * who-knows-what going on in the code.
  12.  * 
  13.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  14.  *
  15.  * 17 Oct 92 Mike Crawford - The third anniversary of the Loma Prieta Earthquake.
  16.  *                            Downtown Santa Cruz is still under reconstruction.
  17.  */
  18.  
  19. #include <EPPC.h>
  20. #include <AppleEvents.h>
  21. #include <AEObjects.h>
  22. #include <Printing.h>
  23. #include "AERegistry.h"
  24. #include "WordServices.h"
  25. #include "TestBed.h"
  26. #include "TBConstants.h"
  27. #include "Gripe.h"
  28. #include "Scroll.h"
  29. #include "Prefs.h"
  30. #include "TBGlobals.h"
  31. #include "PrintWWJr.h"
  32. #include "UnloadStuff.h"
  33. #include "PageSetup.h"
  34.  
  35. void PostPrintingErrors( OSErr theErr );
  36. void PrintPage( Rect rPage, short pageNumber, Boolean *morePages );
  37. void MakePrinterText( THPrint thePrRecHdl );
  38. void SetPrintRect( Rect *rPagePtr );
  39. void SetWindowRect( void );
  40.  
  41. static Rect    gOldDestRect;
  42. static Rect gOldViewRect;
  43.  
  44. void DoPrint( void )
  45. {
  46.     GrafPtr        oldPort;
  47.     short        copies;
  48.     short        firstPage;
  49.     short        lastPage;
  50.     short        numberOfCopies;
  51.     short        printmgrsResFile;
  52.     short        realNumberOfPagesInDoc;
  53.     short        pageNumber;
  54.     short        printError;
  55.     THPrint        thePrRecHdl;
  56.     TPPrPort    thePrPort;
  57.     TPrStatus    theStatus;
  58.     Rect        rPage;
  59.     Boolean        morePages;
  60.  
  61.     GetPort( &oldPort );
  62.  
  63.     thePrRecHdl = GetPrintRecord();
  64.  
  65.     if ( !thePrRecHdl ){
  66.         Gripe( "\pCannot get print record" );
  67.         return;
  68.     }
  69.  
  70.     UnloadForPrint();
  71.         
  72.     PrOpen();
  73.     
  74.     if ( PrError() == noErr ){
  75.     
  76.         printmgrsResFile = CurResFile();
  77.         
  78.         if ( PrJobDialog( thePrRecHdl ) ){
  79.         
  80.             numberOfCopies = (**thePrRecHdl).prJob.iCopies;
  81.             firstPage = (**thePrRecHdl).prJob.iFstPage;
  82.             lastPage = (**thePrRecHdl).prJob.iLstPage;
  83.                     
  84.             /* STUB show printing status dialog */
  85.             
  86.             for ( copies = 1; copies <= numberOfCopies; copies++ ){
  87.             
  88.                 /* STUB install idle proc */
  89.                 
  90.                 /* Restore the resource file to the printer driver */
  91.                 
  92.                 UseResFile( printmgrsResFile );
  93.                 
  94.                 thePrPort = PrOpenDoc( thePrRecHdl,
  95.                                         (TPPrPort)NULL,
  96.                                         (Ptr)NULL );
  97.                 
  98.                 pageNumber = firstPage;
  99.                 
  100.                 rPage = (**thePrRecHdl).prInfo.rPage;
  101.                 
  102.                 SetPrintRect( &rPage );        /* Set TE Rec to fit on printer paper */
  103.  
  104.                 morePages = true;
  105.  
  106.                 while ( pageNumber <= lastPage && morePages && PrError() == noErr ){
  107.                     PicHandle    thePict;
  108.                     GrafPtr        curPort;
  109.                     
  110.                     GetPort( &curPort );
  111.                     SetPort( gDocWindow );
  112.                 
  113.                     thePict = OpenPicture( &rPage );
  114.                     
  115.                     /*ClipRect( &(thePort->portRect) );*/
  116.                     /*ClipRect( &rPage );*/
  117.                     
  118.                     if ( !thePict ){
  119.                         PrSetError( memFullErr );
  120.                     }else{
  121.                         PrintPage( rPage, pageNumber, &morePages );
  122.                         ClosePicture();
  123.                         
  124.                         SetPort( curPort );
  125.  
  126.                         PrOpenPage( thePrPort, (TPRect)NULL );
  127.                         
  128.                         if ( PrError() == noErr ){
  129.                             DrawPicture( thePict, &rPage );
  130.                         }
  131.                         
  132.                         PrClosePage( thePrPort );
  133.                         
  134.                         DisposeHandle( thePict );
  135.                     }
  136.                     pageNumber++;
  137.                 }/* while pageNumber */
  138.                 
  139.                 SetWindowRect();            /* Restore TE Rec to fit in window */
  140.  
  141.                 PrCloseDoc( thePrPort );
  142.             }/* for copies */
  143.         }else{
  144.             /* The print job is being canceled by the user
  145.              * from the job dialog
  146.              */
  147.             PrSetError( iPrAbort );
  148.         }
  149.     }/* If PrError */
  150.     
  151.     printError = PrError();
  152.     
  153.     PrClose();
  154.     
  155.     if ( printError != noErr )
  156.         PostPrintingErrors( printError );
  157.     
  158.     ReleaseResource( thePrRecHdl );
  159.     
  160.     /* STUB dispose of progress dialog */
  161.  
  162.     SetPort( oldPort );
  163.  
  164.     return;
  165. }
  166.  
  167. void PostPrintingErrors( OSErr theErr )
  168. {
  169.     /* STUB Explain what the error really was */
  170.     
  171.     Gripe( "\pAn error occurred while printing" );
  172.  
  173.     return;
  174. }
  175.  
  176. void SetPrintRect( Rect *rPagePtr )
  177. {
  178.     TEHandle    textH;
  179.  
  180.     /* Resize the TextEdit record's dest and view rects to fit the printer paper */
  181.  
  182.     textH = (TEHandle)GetWRefCon( gDocWindow );
  183.     
  184.     gOldDestRect = (*textH)->destRect;
  185.     gOldViewRect = (*textH)->viewRect;
  186.     
  187.     (*textH)->destRect = *rPagePtr;
  188.     (*textH)->viewRect = *rPagePtr;
  189.  
  190.     TECalText( textH );
  191.  
  192.     return;
  193. }
  194.  
  195. void SetWindowRect( void )
  196. {
  197.     TEHandle    textH;
  198.     GrafPtr        curPort;
  199.     
  200.     /* Restore the TE rec's original view and dest rects */
  201.  
  202.     GetPort( &curPort );
  203.     SetPort( gDocWindow );
  204.  
  205.     textH = (TEHandle)GetWRefCon( gDocWindow );
  206.     
  207.     (*textH)->destRect = gOldDestRect;
  208.     (*textH)->viewRect = gOldViewRect;
  209.  
  210.     TECalText( textH );
  211.  
  212.     /* Force a redraw of the text */
  213.     InvalRect( &gOldViewRect );
  214.     
  215.     SetPort( curPort );
  216.  
  217.     return;
  218. }
  219.  
  220. void PrintPage( Rect rPage, short pageNumber, Boolean *morePages )
  221. {
  222.     TEHandle    textH;
  223.     GrafPtr        curPort;
  224.     Rect        clipRect;
  225.     short        docHeight;
  226.     short        pageHeight;
  227.     Rect        viewRect;
  228.     Rect        destRect;
  229.  
  230.     textH = (TEHandle)GetWRefCon( gDocWindow );
  231.  
  232.     docHeight = TEGetHeight( 1, (*textH)->nLines, textH );
  233.     
  234.     pageHeight = rPage.bottom - rPage.top;
  235.     
  236.     if ( pageHeight * pageNumber > docHeight ){
  237.         *morePages = false;
  238.     }else{
  239.         *morePages = true;
  240.     }
  241.  
  242.     SetRect( &clipRect, 0, 0, 0, 0 );
  243.     ClipRect( &clipRect );
  244.     
  245.     viewRect = (*textH)->viewRect;
  246.     destRect = (*textH)->destRect;
  247.  
  248.     TEScroll( 0, viewRect.top - destRect.top, textH );
  249.     TEScroll( 0, - ( pageHeight * (pageNumber - 1) ), textH );
  250.  
  251.     clipRect = rPage;
  252.  
  253. /*
  254.     clipRect.top += (pageHeight * (pageNumber - 1) );    
  255.     clipRect.bottom += (pageHeight * (pageNumber - 1) );
  256. */
  257.  
  258.     ClipRect( &clipRect );
  259.  
  260.     TEUpdate( &rPage, textH );
  261.             
  262.     return;
  263. }
  264.  
  265. #ifdef NEVER
  266. void MakePrinterText( THPrint thePrRecHdl )
  267. {
  268.     Rect            destRect;
  269.     Rect            viewRect;
  270.     CharsHandle        textHdl;
  271.     TEStyleHandle    stylHdl;
  272.     TEHandle        winTEHdl;
  273.     long            textLen;
  274.     short            oldStart;
  275.     short            oldEnd;
  276.  
  277.     /* If there are any errors, we post them into the printer manager, so
  278.      * that upon return, the error is checked and we will exit the printing loop.
  279.      * I think that should work properly.
  280.      *
  281.      * It will have the advantage that the printing loop will be entirely exited
  282.      * before the error is actually displayed.
  283.      */
  284.  
  285.     destRect = (*thePrRecHdl)->prInfo.rPage;
  286.     viewRect = destRect;
  287.  
  288.     gPrintTextHdl = TEStylNew( &destRect, &viewRect );
  289.     
  290.     if ( !gPrintTextHdl ){
  291.         PrSetError( memFullErr );
  292.         return;
  293.     }
  294.  
  295. #ifdef NEVER    
  296.     /* Copy the text from the window's TE rec to the printer's TE rec */
  297.     
  298.     winTEHdl = (TEHandle)GetWRefCon( gDocWindow );
  299.  
  300.     textHdl = TEGetText( winTEHdl );
  301.     
  302.     textLen = GetHandleSize( textHdl );
  303.     
  304.     HLock( textHdl );
  305.     TEInsert( *textHdl, textLen, gPrintTextHdl );
  306.     HUnlock( textHdl );
  307.     
  308.     /* Copy the styles from the window's TE rec to the printer's */
  309.  
  310.     oldStart = (*winTEHdl)->selStart;
  311.     oldEnd = (*winTEHdl)->selEnd;
  312.     (*winTEHdl)->selStart = 0;
  313.     (*winTEHdl)->selEnd = 32767;
  314.  
  315.     stylHdl = GetStylHandle( winTEHdl );
  316.  
  317.     (*winTEHdl)->selStart = oldStart;
  318.     (*winTEHdl)->selEnd = oldEnd;
  319.     
  320.     SetStylHandle( stylHdl, gPrintTextHdl );
  321. #endif
  322.  
  323.     return;
  324. }
  325. #endif
  326.