home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / ColorSync 2.5.1 SDK / Sample Code / CSDemo 2.5 / ShellSources / appPrint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-09  |  8.1 KB  |  266 lines  |  [TEXT/CWIE]

  1. #if PIGS_SHELL_PRINT
  2.  
  3. #include <Types.h>
  4.  
  5. #include "appGlobals.h"
  6. #include "appPrint.h"
  7. #include "appMenus.h"
  8. #include "appErrors.h"
  9. #include "appAEvts.h"
  10.  
  11. #include "qdPrintUtils.h"
  12.  
  13. #include "cursorUtils.h"
  14.  
  15. #include "win.h"
  16. #include "winTables.h"
  17.  
  18.  
  19. /**\
  20. |**| ==============================================================================
  21. |**| PRIVATE FUNCTION PROTOTYPES
  22. |**| ==============================================================================
  23. \**/
  24. void    AdjustMenusForPrinting    ( void ) ;
  25.  
  26.  
  27. /**\
  28. |**| ==============================================================================
  29. |**| PUBLIC FUNCTIONS
  30. |**| ==============================================================================
  31. \**/
  32.  
  33.  
  34. /*------------------------------------------------------------------------------*\
  35.     DoAppPageSetupCommand
  36.  *------------------------------------------------------------------------------*
  37. \*------------------------------------------------------------------------------*/
  38. void DoAppPageSetupCommand ( winHandle win ) 
  39. {
  40.     OSErr            err ;
  41.     FSSpec            spec = {0,0,"\p"} ;
  42.  
  43.     if ( win==nil) return ;
  44.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  45.     WarnIfErr( err ) ;
  46.     if ( err ) return ;
  47.     
  48.     HiliteMenu(0) ;                            // Unhighlight whatever MenuSelect or MenuKey hilited
  49.     AdjustMenusForPrinting() ;
  50.     
  51.     err = QDStyleDlog( GetWinPrintRec(win) ) ;// do the classic page setup dialog
  52.  
  53.     spec = GetWinFSSpec( win ) ;
  54.     if (spec.name[0])                        // if the win has an associated file
  55.     {
  56.         err = SaveResourceTHPrint( GetWinPrintRec(win), &spec ) ;
  57.     }
  58. }
  59.  
  60.  
  61. /*------------------------------------------------------------------------------*\
  62.     DoAppPrintCommand
  63.  *------------------------------------------------------------------------------*
  64. \*------------------------------------------------------------------------------*/
  65. void DoAppPrintCommand ( winHandle win ) 
  66. {
  67.     OSErr            err ;
  68.     FSSpec            spec = {0,0,"\p"} ;
  69.     
  70.     if ( win==nil) return ;
  71.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  72.     WarnIfErr( err ) ;
  73.     if ( err ) return ;
  74.     
  75.     HiliteMenu(0) ;                            // Unhighlight whatever MenuSelect or MenuKey hilited
  76.     AdjustMenusForPrinting() ;
  77.  
  78.     err = QDJobDlog( GetWinPrintRec(win) ) ; // do the classic page setup dialog
  79.     if (err) return ;
  80.  
  81.     spec = GetWinFSSpec( win ) ;
  82.     if (spec.name[0])                        // if the win has an associated file
  83.     {
  84.         err = SaveResourceTHPrint( GetWinPrintRec(win), &spec ) ;
  85.     }
  86.  
  87.     DoAppPrintOneCommand( win ) ;
  88. }
  89.  
  90.  
  91. /*------------------------------------------------------------------------------*\
  92.     DoAppPrintOneCommand
  93.  *------------------------------------------------------------------------------*
  94. \*------------------------------------------------------------------------------*/
  95. void DoAppPrintOneCommand ( winHandle win ) 
  96. {
  97.     OSErr            err ;
  98.     FSSpec            spec = {0,0,"\p"} ;
  99.     
  100.     if ( win == nil) return ;
  101.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  102.     WarnIfErr( err ) ;
  103.     if ( err ) return ;
  104.     
  105.     // we can just issue a print command, because we have a valid print record for the 
  106.     // document.  This is in the dPrintRec record of the document record, we created
  107.     // it (or read it in from the document's file) at the time we created the window.
  108.     // for more information on this look at the routines SetupPrintHdl() and LoadPHdl()
  109.  
  110.     spec = GetWinFSSpec( win ) ;
  111.     
  112.     if (spec.name[0])                        // if the win has an associated file
  113.         SendPDOC( &spec ) ;                    // send print apple event
  114.     else
  115.         DoAppPrintLoop( win ) ;
  116. }
  117.  
  118.  
  119. /*------------------------------------------------------------------------------*\
  120.     DoAppGetPrintStrutures
  121.  *------------------------------------------------------------------------------*
  122. \*------------------------------------------------------------------------------*/
  123. OSErr DoAppGetPrintStrutures ( winHandle win )
  124. {
  125.     FSSpec             spec ;
  126.     THPrint            theTHPrint = nil ;
  127.     OSErr            err ;
  128.     
  129.     spec = GetWinFSSpec( win ) ;
  130.     
  131.     if ( (err=QDPrinting_available()) == noErr )            // if printing is possible
  132.     {
  133.         if ( GetWinPrintRec(win) ) return noErr ;             // we already have it so return 
  134.         if (spec.name[0])                                    // if the win has an associated file
  135.             err = LoadResourceTHPrint( &theTHPrint,&spec) ;    // get job from file
  136.         if (theTHPrint==nil)                                // if didnt get it
  137.             err = GetDefaultTHPrint( &theTHPrint ) ;        // get default
  138.         SetWinPrintRec( win, theTHPrint ) ;
  139.     }
  140.     return err ;                                            // return above error 
  141. }
  142.  
  143.  
  144. /*------------------------------------------------------------------------------*\
  145.     DoAppGetPageSize
  146.  *------------------------------------------------------------------------------*
  147. \*------------------------------------------------------------------------------*/
  148. OSErr DoAppGetPageSize ( winHandle win, Rect *pageRect )
  149. {
  150.     THPrint            theTHPrint = nil ;
  151.     OSErr            err ;
  152.     
  153.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  154.     WarnIfErr( err ) ;
  155.     if ( err ) return err ;
  156.     
  157.     if ( (err=QDPrinting_available()) == noErr )            // if printing is possible
  158.     {
  159.         theTHPrint = GetWinPrintRec(win) ;
  160.         err = QDGetPageSize( theTHPrint, pageRect ) ;
  161.     }
  162.     return err ;                                            // return above error 
  163. }
  164.  
  165.  
  166. /*------------------------------------------------------------------------------*\
  167.     DoAppPrintLoop
  168.  *------------------------------------------------------------------------------*
  169.         This is a basic GX print loop for QuickDraw-based applications
  170.         For simplicity, we don't implement a facility for the user to select
  171.         different formats for each page.  In another app (a word processor
  172.         for example) we may want to do this, in which case the jobFormat field
  173.         of the document record could be implemented as an array with one 
  174.         slot in the array for each page in the win, or as a list.  We wanted
  175.         to keep this simple...
  176. \*------------------------------------------------------------------------------*/
  177. OSErr DoAppPrintLoop ( winHandle win )
  178. {
  179.     short            pageCount ;
  180.     long            firstPage, lastPage;
  181.     GrafPtr            savedPort ;
  182.     OSErr            err ;
  183.     
  184.     err = DoAppGetPrintStrutures( win ) ;    // make sure that we have the needed print structures
  185.     WarnIfErr( err ) ;
  186.     if ( err ) return err ;
  187.  
  188.     err = CallWinPageCountProc ( win, &pageCount ) ;
  189.     firstPage = 1; lastPage = pageCount ;
  190.     WarnIfErr( err ) ;
  191.     if ( err ) return err;
  192.     
  193.     SetUpCursors( kWatchACursor ) ;
  194.     GetPort( &savedPort ) ;
  195.     
  196.  
  197.     if ( (err=QDPrinting_available()) == noErr )    // else if printing is possible
  198.     {
  199.         QDPrintLoopParamsRec    params ;
  200.  
  201.         params.hPrint = GetWinPrintRec(win) ;
  202.         params.window = GetWinWindow(win) ;
  203.         params.firstPage = firstPage ;
  204.         params.lastPage = lastPage ;
  205.             
  206.         err = QDPrintLoopBegin( ¶ms ) ;
  207.         if (!err)
  208.         {
  209.             for (    params.currentPage = params.firstPage;
  210.                     params.currentPage <= params.lastPage && !err ;
  211.                     params.currentPage ++ )
  212.             {
  213.                 if (!err) err = QDPrintLoopPageBefore( ¶ms ) ;
  214.                 if (!err) err = CallWinPagePrintProc( win, params.printingPort, params.currentPage ) ;
  215.                 if (!err) err = QDPrintLoopPageAfter( ¶ms ) ;
  216.             }
  217.             err = QDPrintLoopEnd( ¶ms ) ;
  218.         }
  219.     }
  220.  
  221.  
  222.     SetPort( savedPort ) ;
  223.     TearDownCursors( kArrowCursor ) ;
  224.     return err ;                                        // return above error 
  225. }
  226.  
  227.  
  228. /**\
  229. |**| ==============================================================================
  230. |**| PRIVATE FUNCTIONS
  231. |**| ==============================================================================
  232. \**/
  233.  
  234.  
  235. /*------------------------------------------------------------------------------*\
  236.     AdjustMenusForPrinting
  237.  *------------------------------------------------------------------------------*
  238.         This routine enables the Edit menu items so that print dialogs can
  239.         do cut, copy, and paste.
  240.         This routine is called by the menu handling code of any windows that 
  241.         support Page Setup or Print commands.
  242. \*------------------------------------------------------------------------------*/
  243. static void AdjustMenusForPrinting ( void ) 
  244. {
  245.     MenuHandle    theMenu ;
  246.  
  247.     // if we are printing, disable everything except the edit menu
  248.  
  249.     dimAllMenus() ;
  250.         
  251.     // do the method menu
  252.     theMenu = GetMenuHandle ( mEdit ) ;
  253.         EnableItem ( theMenu, kWholeMenu ) ;
  254.         EnableItem ( theMenu, iUndo ) ;
  255.         EnableItem ( theMenu, iCut ) ;
  256.         EnableItem ( theMenu, iCopy ) ;
  257.         EnableItem ( theMenu, iPaste ) ;
  258.         EnableItem ( theMenu, iClear ) ;
  259.  
  260.     DrawMenuBar() ;
  261. }
  262.  
  263. #endif
  264.  
  265.  
  266.