home *** CD-ROM | disk | FTP | other *** search
/ PC Direkt 1995 March / PCD_395.iso / starview / pm2csci / german / print.cx_ / PRINT.CXX
Encoding:
C/C++ Source or Header  |  1994-10-20  |  11.0 KB  |  401 lines

  1. /*******************************************************************
  2. *  PRINT.CXX
  3. *  (c) 1992-1994 STAR DIVISION
  4. *******************************************************************/
  5.  
  6. #include <sv.hxx>
  7.  
  8. #include "prvdlg.hxx"
  9. #include "print.hrc"
  10.  
  11. Printer* pPrinter;
  12.  
  13. // --- class MyApp -------------------------------------------------
  14.  
  15. class MyApp : public Application
  16. {
  17. public:
  18.     virtual void  Main( int, char*[] );
  19. };
  20.  
  21. // --- class AbortDialog -------------------------------------------
  22.  
  23. class AbortDialog : public ModelessDialog
  24. {
  25. private:
  26.     FixedText       aText;
  27.     CancelButton    aAbort;
  28.  
  29. public:
  30.                     AbortDialog( Window* pParent );
  31.     void            AbortHdl( CancelButton* );
  32. };
  33.  
  34. // --- class InfoDialog --------------------------------------------
  35.  
  36. class InfoDialog : public ModalDialog
  37. {
  38. private:
  39.     FixedText   aPrinterName;
  40.     FixedText   aPaperSize;
  41.     FixedText   aOutputSize;
  42.     FixedText   aPageOffset;
  43.     FixedText   aPaperBins;
  44.     OKButton    aOKButton;
  45.  
  46. public:
  47.                 InfoDialog( Window* pParent );
  48. };
  49.  
  50. // --- class AppWin ------------------------------------------------
  51.  
  52. class AppWin : public WorkWindow
  53. {
  54. protected:
  55.     AbortDialog     aAbortDialog;
  56.  
  57. public:
  58.                     AppWin();
  59.  
  60.     virtual void    Paint( const Rectangle& );
  61.     virtual void    Resize();
  62.  
  63.     long            MenuSelect( Menu* pMenu );
  64.  
  65.     void            DoPrint();
  66.     void            DoSetup();
  67.     void            DoInfo();
  68.     void            DoPreview();
  69.  
  70.     void            Print( OutputDevice* pOutDev, USHORT nPage );
  71.     void            PrintPageHdl( Preview* pPreview );
  72.     void            StartPrintHdl( Printer* pPrn );
  73.     void            EndPrintHdl( Printer* pPrn );
  74. };
  75.  
  76. // --- AppWin::AppWin() --------------------------------------------
  77.  
  78. AppWin::AppWin() :
  79.             WorkWindow( NULL, WB_APP | WB_STDWORK ),
  80.             aAbortDialog( this )
  81. {
  82.     SetText( "Printer Demo" );
  83. }
  84.  
  85. // --- AppWin::Paint() ---------------------------------------------
  86.  
  87. void AppWin::Paint( const Rectangle& )
  88. {
  89.     Print( this, 1 );
  90. }
  91.  
  92. // --- AppWin::Resize() --------------------------------------------
  93.  
  94. void AppWin::Resize()
  95. {
  96.     Invalidate();
  97. }
  98.  
  99. // --- AppWin::MenuSelect() ----------------------------------------
  100.  
  101. long AppWin::MenuSelect( Menu* pMenu )
  102. {
  103.     switch ( pMenu->GetCurItemId() )
  104.     {
  105.         case MI_SETUP:
  106.             DoSetup();
  107.             break;
  108.  
  109.         case MI_PRINT:
  110.             DoPrint();
  111.             break;
  112.  
  113.         case MI_INFO:
  114.             DoInfo();
  115.             break;
  116.  
  117.         case MI_PREVIEW:
  118.             DoPreview();
  119.             break;
  120.  
  121.         case MI_EXIT:
  122.             Close();
  123.             break;
  124.     }
  125.  
  126.     return TRUE;
  127. }
  128.  
  129. // --- AppWin::DoPrint() -------------------------------------------
  130.  
  131. void AppWin::DoPrint()
  132. {
  133.     PrintDialog aPrintDlg( this, WB_SVLOOK );
  134.  
  135.     aPrintDlg.ChangePrinter( pPrinter );
  136.     aPrintDlg.ChangeCopyCount( 1 );
  137.     aPrintDlg.ChangeFirstPage( 1 );
  138.     aPrintDlg.ChangeLastPage( 5 );
  139.     aPrintDlg.EnablePageFields( TRUE );
  140.     aPrintDlg.EnableSelection( FALSE );
  141.     aPrintDlg.EnableCollate( TRUE );
  142.     aPrintDlg.CheckCollate( FALSE );
  143.  
  144.     if ( aPrintDlg.Execute() )
  145.     {
  146.         pPrinter->SetCopyCount( aPrintDlg.GetCopyCount(),
  147.                                 aPrintDlg.IsCollateChecked() );
  148.  
  149.         if ( !pPrinter->StartJob( "StarView-Print" ) )
  150.         {
  151.             ErrorBox( NULL, WB_OK, "StartJob Error" ).Execute();
  152.             return;
  153.         }
  154.         Disable();
  155.  
  156.         USHORT nFirstPage = aPrintDlg.GetFirstPage();
  157.         USHORT nLastPage = aPrintDlg.GetLastPage();
  158.  
  159.         for ( USHORT i = nFirstPage; i <= nLastPage; i++ )
  160.         {
  161.             pPrinter->StartPage();
  162.             Print( pPrinter, i );
  163.             pPrinter->EndPage();
  164.         }
  165.         pPrinter->EndJob();
  166.  
  167.         Enable();
  168.     }
  169. }
  170.  
  171. // --- AppWin::DoSetup() -------------------------------------------
  172.  
  173. void AppWin::DoSetup()
  174. {
  175.     PrinterSetupDialog aSetupDlg( this, WB_SVLOOK );
  176.     aSetupDlg.ChangePrinter( pPrinter );
  177.     aSetupDlg.Execute();
  178. }
  179.  
  180. // --- AppWin::DoInfo() --------------------------------------------
  181.  
  182. void AppWin::DoInfo()
  183. {
  184.     InfoDialog( this ).Execute();
  185. }
  186.  
  187. // --- AppWin::DoPreview() -----------------------------------------
  188.  
  189. void AppWin::DoPreview()
  190. {
  191.     JobSetup* pSetup;
  192.  
  193.     pSetup = (JobSetup*)new char[ pPrinter->GetJobSetupSize() ];
  194.     pPrinter->GetJobSetup( pSetup );
  195.  
  196.     PreviewDialog aDialog( this, pSetup );
  197.     aDialog.ChangeRequestPageHdl( LINK( this,
  198.                                         AppWin, PrintPageHdl ) );
  199.     aDialog.Execute();
  200.     delete pSetup;
  201. }
  202.  
  203. // --- AppWin::Print() ---------------------------------------------
  204.  
  205. void AppWin::Print( OutputDevice* pOutDev, USHORT nPage )
  206. {
  207.     pOutDev->Push();
  208.     MapMode aMapMode( MAP_10TH_MM );
  209.     pOutDev->ChangeMapMode( aMapMode );
  210.  
  211.     Size aOutputSize = pOutDev->GetOutputSize();
  212.  
  213.     Font aFont;
  214.     aFont.ChangeFamily( FAMILY_SWISS );
  215.     aFont.ChangePitch( PITCH_VARIABLE );
  216.     aFont.ChangeSize( Size( 0, 180 ) );
  217.     aFont.ChangeName( "Helvetica" );
  218.     pOutDev->ChangeFont( aFont );
  219.  
  220.     // Geometrische Objeke zeichnen
  221.     Size  aSize( (aOutputSize.Width()-100)/3, 500 );
  222.     Color aRedColor( COL_RED );
  223.     Brush aBrush( aRedColor );
  224.     pOutDev->ChangeFillInBrush( aBrush );
  225.     pOutDev->DrawRect( Rectangle( Point( 25, 0 ), aSize ) );
  226.     aBrush.ChangeColor( Color( COL_GREEN ) );
  227.     pOutDev->ChangeFillInBrush( aBrush );
  228.     pOutDev->DrawEllipse( Rectangle( Point( aSize.Width()+50, 0 ),
  229.                                      aSize ) );
  230.     aBrush.ChangeColor( Color( COL_BLUE ) );
  231.     pOutDev->ChangeFillInBrush( aBrush );
  232.     Point aPtAry[ 3 ];
  233.     aPtAry[0] = Point( aSize.Width()*2+75 + aSize.Width()/2, 0 );
  234.     aPtAry[1] = Point( aSize.Width()*3+75, aSize.Height() );
  235.     aPtAry[2] = Point( aSize.Width()*2+75, aSize.Height() );
  236.     pOutDev->DrawPolygon( Polygon( 3, aPtAry ) );
  237.  
  238.  
  239.     aSize.Height() += 100;
  240.     String aText( "StarView Printdemo" );
  241.     Size   aTextSize = pOutDev->GetTextSize( aText );
  242.     pOutDev->DrawText( Point( (aOutputSize.Width()>>1) -
  243.                               (aTextSize.Width()>>1),
  244.                               aSize.Height() ),
  245.                        aText );
  246.     aSize.Height() += aTextSize.Height() + 50;
  247.  
  248.     aText = String( "Page : " ) + String( nPage );
  249.     aTextSize = pOutDev->GetTextSize( aText );
  250.     pOutDev->DrawText( Point( (aOutputSize.Width()>>1) -
  251.                               (aTextSize.Width()>>1),
  252.                               aSize.Height() ),
  253.                        aText );
  254.     aSize.Height() += aTextSize.Height() + 50;
  255.  
  256.     Pen aPen;
  257.     aPen.ChangeWidth( 15 );
  258.     pOutDev->ChangePen( aPen );
  259.     pOutDev->DrawLine( Point( 300, aSize.Height() ),
  260.                        Point( aOutputSize.Width()-300,
  261.                               aSize.Height() ) );
  262.  
  263.     if ( aOutputSize.Height() > (aSize.Height() + 150) )
  264.     {
  265.         aFont.ChangeSize( Size( 0, 40 ) );
  266.         pOutDev->ChangeFont( aFont );
  267.         aSize.Height() = aOutputSize.Height()-100;
  268.  
  269.         aText = "This is the end of the page.";
  270.         aTextSize = pOutDev->GetTextSize( aText );
  271.         pOutDev->DrawText( Point( (aOutputSize.Width()>>1) -
  272.                                   (aTextSize.Width()>>1),
  273.                                   aSize.Height() ),
  274.                            aText );
  275.         aSize.Height() += aTextSize.Height() + 50;
  276.  
  277.         pOutDev->DrawLine( Point( 0, aSize.Height() ),
  278.                            Point( aOutputSize.Width(),
  279.                                   aSize.Height() ) );
  280.     }
  281.  
  282.     pOutDev->Pop();
  283. }
  284.  
  285. // --- AppWin::PrintPageHdl() --------------------------------------
  286.  
  287. void AppWin::PrintPageHdl( Preview* pPreview )
  288. {
  289.     Print( (Printer*)pPreview, pPreview->GetCurPage() );
  290. }
  291.  
  292. // --- AppWin::StartPrintHdl() -------------------------------------
  293.  
  294. void AppWin::StartPrintHdl( Printer* )
  295. {
  296.     pApp->GetAppMenu()->GetPopupMenu( POPUP_PRINTER )->
  297.                             EnableItem( MI_PRINT, FALSE );
  298.     aAbortDialog.Show();
  299. }
  300.  
  301. // --- AppWin::EndPrintHdl() ---------------------------------------
  302.  
  303. void AppWin::EndPrintHdl( Printer* )
  304. {
  305.     pApp->GetAppMenu()->GetPopupMenu( POPUP_PRINTER )->
  306.                             EnableItem( MI_PRINT, TRUE );
  307.     aAbortDialog.Hide();
  308. }
  309.  
  310. // --- AbortDialog::AbortDialog() ----------------------------------
  311.  
  312. AbortDialog::AbortDialog( Window* pParent ) :
  313.                  ModelessDialog( pParent, ResId( DLG_ABORT ) ),
  314.                  aText( this, ResId( DA_ABORTTEXT ) ),
  315.                  aAbort( this, ResId( DA_ABORT ) )
  316. {
  317.     FreeResource();
  318.  
  319.     aAbort.ChangeClickHdl( LINK( this, AbortDialog, AbortHdl ) );
  320. }
  321.  
  322. // --- AbortDialog::AbortHdl() -------------------------------------
  323.  
  324. void AbortDialog::AbortHdl( CancelButton* )
  325. {
  326.     pPrinter->AbortJob();
  327. }
  328.  
  329. // --- InfoDialog::InfoDialog() ------------------------------------
  330.  
  331. InfoDialog::InfoDialog( Window* pParent ) :
  332.                 ModalDialog( pParent, ResId( DLG_INFO ) ),
  333.                 aOKButton(this, ResId( DI_OK ) ),
  334.                 aPrinterName( this, ResId( DI_PRINTERNAME ) ),
  335.                 aPaperSize( this, ResId( DI_PAPERSIZE ) ),
  336.                 aOutputSize( this, ResId( DI_OUTPUTSIZE ) ),
  337.                 aPageOffset( this, ResId( DI_PAGEOFFSET ) ),
  338.                 aPaperBins( this, ResId( DI_PAPERBINS ) )
  339. {
  340.     FreeResource();
  341.  
  342.     String aStr;
  343.  
  344.     aPrinterName.SetText( pPrinter->GetName() );
  345.  
  346.     Size aSize = pPrinter->GetPaperSizePixel();
  347.     aStr = aPaperSize.GetText();
  348.     aStr += aSize.Width();
  349.     aStr += " ; ";
  350.     aStr += aSize.Height();
  351.     aPaperSize.SetText( aStr );
  352.  
  353.     aSize = pPrinter->GetOutputSizePixel();
  354.     aStr = aOutputSize.GetText();
  355.     aStr += aSize.Width();
  356.     aStr += " ; ";
  357.     aStr += aSize.Height();
  358.     aOutputSize.SetText( aStr );
  359.  
  360.     Point aOffset = pPrinter->GetPageOffsetPixel();
  361.     aStr = aPageOffset.GetText();
  362.     aStr += aOffset.X();
  363.     aStr += " ; ";
  364.     aStr += aOffset.Y();
  365.     aPageOffset.SetText( aStr );
  366.  
  367.     aStr = aPaperBins.GetText();
  368.     aStr.Insert( String( pPrinter->GetPaperBinCount() ), 0 );
  369.     aPaperBins.SetText( aStr );
  370. }
  371.  
  372. // --- MyApp::Main() -----------------------------------------------
  373.  
  374. void MyApp::Main( int, char*[] )
  375. {
  376.     EnableSVLook();
  377.  
  378.     AppWin  aAppWin;
  379.     MenuBar aAppMenu( ResId( MENU_APPMENU ) );
  380.  
  381.     aAppMenu.PushSelectHdl( LINK( &aAppWin,
  382.                                   AppWin, MenuSelect ) );
  383.     ChangeAppMenu( &aAppMenu );
  384.  
  385.     aAppWin.Show();
  386.  
  387.     pPrinter = new Printer();
  388.     pPrinter->ChangeStartPrintHdl( LINK( &aAppWin,
  389.                                          AppWin, StartPrintHdl ) );
  390.     pPrinter->ChangeEndPrintHdl( LINK( &aAppWin,
  391.                                        AppWin, EndPrintHdl ) );
  392.  
  393.     Execute();
  394.  
  395.     delete pPrinter;
  396. }
  397.  
  398. // --- aMyApp ------------------------------------------------------
  399.  
  400. MyApp aMyApp;
  401.