wxIScan
wxiviewprintout.cpp
Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 // Project:     wxIView
00003 // Purpose:     Complex wxWidgets sample
00004 // Name:        wxiviewprintout.cpp
00005 // Author:      Daniel Nell
00006 // Created:     2005/10/18
00007 // Copyright:   (c) Daniel Nell
00008 // Licence:     wxWindows license
00009 // Modified by:
00010 /////////////////////////////////////////////////////////////////////////////
00011 
00012 // For compilers that support precompilation, includes "wx/wx.h".
00013 #include <wx/wxprec.h>
00014 #ifndef WX_PRECOMP
00015 #   include <wx/wx.h>
00016 #endif
00017 
00018 // Include additional wxWidgets headers.
00019 #include <wx/print.h>
00020 
00021 // Include private wxIView headers.
00022 #include "wxiviewpaint.h"
00023 #include "wxiviewprintout.h"
00024 
00025 
00026 //////////////////////////////////////////////////////////
00027 // Print out object
00028 //
00029 // Standard constructor.
00030 //
00031 wxIViewPrintout::wxIViewPrintout( wxIViewPaintBase *poPaint,
00032                                   const wxString& strTitle,
00033                                   bool bFitToPage,
00034                                   int nResolutionX,
00035                                   int nResolutionY )
00036   : wxPrintout( strTitle )
00037 {
00038     // Initialize member variables.
00039     m_poPaint= poPaint;
00040     m_bFitToPage= bFitToPage;
00041     m_nResolutionX= nResolutionX;
00042     m_nResolutionY= nResolutionY;
00043 }
00044 
00045 // From the wxWidgets documentation:
00046 // Called by the framework to obtain information from the application about minimum and maximum
00047 // page values that the user can select, and the required page range to be printed. By default
00048 // this returns 1, 32000 for the page minimum and maximum values, and 1, 1 for the required page
00049 // range. If minPage is zero, the page number controls in the print dialog will be disabled.
00050 //
00051 void wxIViewPrintout::GetPageInfo( int *nMinPage, int *nMaxPage,
00052                                    int *nSelPageFrom, int *nSelPageTo )
00053 {
00054     // Return 1 for the minimal page number and the correct number of pages
00055     // returned by wxIViewPaintBase::GetMaxPage() (if a valid wxIViewPaintBase object
00056     // is connnected to the wxIViewPrint object).
00057     (*nMinPage)= (*nMaxPage)= (*nSelPageFrom)= (*nSelPageTo)= 1;
00058     if( m_poPaint )
00059     {
00060         (*nMaxPage)= (*nSelPageTo)= m_poPaint->GetMaxPage();
00061     }
00062 }
00063 
00064 // From the wxWidgets documentation:
00065 // Should be overridden to return true if the document has this page, or false if not.
00066 // Returning false signifies the end of the document. By default, HasPage behaves as if
00067 // the document has only one page.
00068 //
00069 bool wxIViewPrintout::HasPage( int nPage )
00070 {
00071     int nMinPage;
00072     int nMaxPage;
00073     int nDummy;
00074 
00075     GetPageInfo( &nMinPage, &nMaxPage, &nDummy, &nDummy );
00076     return ( nPage >= nMinPage) && ( nPage <= nMaxPage );
00077 }
00078 
00079 // From the wxWidgets documentation:
00080 // Called by the framework when a page should be printed. Returning false cancels the print
00081 // job. The application can use wxPrintout::GetDC to obtain a device context to draw on.
00082 //
00083 bool wxIViewPrintout::OnPrintPage( int nPage )
00084 {
00085     // Check validity of the wxIViewPaintBase object (normaly the wxIViewFrame object).
00086     if( !m_poPaint )
00087     {
00088         // Signal error.
00089         return false;
00090     }
00091 
00092     // Get a device context.
00093     wxDC *oDc = GetDC();
00094 
00095     // Check validity of the device context.
00096     if( !oDc )
00097     {
00098         // Signal error.
00099         return false;
00100     }
00101 
00102     // Get printer resolution ...
00103     int nPrinterPpiX;
00104     int nPrinterPpiY;
00105 
00106     GetPPIPrinter( &nPrinterPpiX, &nPrinterPpiY );
00107 
00108     // ... and adjust scalation factor using image resolution.
00109     //
00110     // NOTE: We asume a fixed image resolution of 180 PPI in X and Y direction
00111     //       until there is a possibility to get the image resoltuion from
00112     //       image (EXIF/IPTC) itself. You can change this value by changing
00113     //       nResolutionX and wxivIMAGEPPIY in file wxIViewPaint.h.
00114     //
00115     double nScaleFactorX;
00116     double nScaleFactorY;
00117 
00118     if( IsPreview() )
00119     {
00120         int nPageWidth;
00121         int nPageHeight;
00122         int nDcWidth;
00123         int nDcHeight;
00124 
00125         oDc->GetSize( &nDcWidth, &nDcHeight );
00126         GetPageSizePixels( &nPageWidth, &nPageHeight );
00127         nScaleFactorX=   (double)nPrinterPpiX / (double)m_nResolutionX
00128                        * (double)nDcWidth     / (double)nPageWidth;
00129         nScaleFactorY=   (double)nPrinterPpiY / (double)m_nResolutionY
00130                        * (double)nDcHeight    / (double)nPageHeight;
00131     }
00132     else
00133     {
00134         nScaleFactorX= (double)nPrinterPpiX / (double)m_nResolutionX;
00135         nScaleFactorY= (double)nPrinterPpiY / (double)m_nResolutionY;
00136     }
00137 
00138     // Scale image to fit full page size if given.
00139     if( m_bFitToPage )
00140     {
00141 
00142         int nImageWidthMM;
00143         int nImageHeightMM;
00144         int nPageWidthMM;
00145         int nPageHeightMM;
00146 
00147         if( m_poPaint->GetPageSizeMM( &nImageWidthMM, &nImageHeightMM ) )
00148         {
00149             GetPageSizeMM( &nPageWidthMM, &nPageHeightMM );
00150 
00151             double nFitToPageScaleFactorX=   (double)nPageWidthMM
00152                                            / (double)nImageWidthMM;
00153             double nFitToPageScaleFactorY=   (double)nPageHeightMM
00154                                            / (double)nImageHeightMM;
00155 
00156             if( nFitToPageScaleFactorX > nFitToPageScaleFactorY )
00157             {
00158                 nFitToPageScaleFactorX= nFitToPageScaleFactorY;
00159             }
00160             nScaleFactorX *= nFitToPageScaleFactorX;
00161             nScaleFactorY *= nFitToPageScaleFactorX;
00162         }
00163     }
00164 
00165     // Set device origin to (0, 0).
00166     oDc->SetDeviceOrigin( 0, 0 );
00167 
00168     // Set scale factor.
00169     oDc->SetUserScale( nScaleFactorX, nScaleFactorY );
00170 
00171     // Set printer page...
00172     if( !m_poPaint->GotoPage( nPage ) )
00173     {
00174         return false;
00175     }
00176 
00177     // ... and paint it.
00178     return m_poPaint->Paint( *oDc );
00179 }
00180 
00181 
00182 //////////////////////////////////////////////////////////
00183 // Print preview frame object
00184 //
00185 // Standard constructor
00186 //
00187 void wxIViewPreviewFrame::CreateControlBar()
00188 {
00189     // Note: This code is based on the default implementation of
00190     //       wxPreviewFrame::CreateControlBar() which you can find in file
00191     //       ${WXWIN}/src/common.prntbase.cpp .
00192     //
00193     // Default controls. In this implemention only the zoom control is shown
00194     // by default.
00195     long nButtons= wxPREVIEW_ZOOM;
00196 
00197     // Add the first/previous/next/last page buttons if there is a wxPrintOut object
00198     // associated and if wxPrintOut::GetPageInfo() returns more than one page.
00199     if( m_printPreview->GetPrintout() )
00200     {
00201         int nMinPage;
00202         int nMaxPage;
00203         int nDummy;
00204 
00205         m_printPreview->GetPrintout()->GetPageInfo( &nMinPage, &nMaxPage,
00206                                                     &nDummy, &nDummy );
00207 
00208         // Check if there is more than one page.
00209         if( nMaxPage - nMinPage > 0 )
00210         {
00211             // Add buttons for 1st/previous/next/last page.
00212             nButtons |=   wxPREVIEW_FIRST | wxPREVIEW_PREVIOUS
00213                         | wxPREVIEW_NEXT  | wxPREVIEW_LAST
00214                         | wxPREVIEW_GOTO;
00215         }
00216     }
00217 
00218     // Add the 'Print' button if there is a wxPrintOut object for printing associated.
00219     if( m_printPreview->GetPrintoutForPrinting() )
00220     {
00221         nButtons |= wxPREVIEW_PRINT;
00222     }
00223 
00224     // Create a new preview tool bar...
00225     m_controlBar = new wxPreviewControlBar( m_printPreview, nButtons, this,
00226                                             wxPoint( 0, 0 ), wxSize( 400, 40 ) );
00227 
00228     // ... and update buttons.
00229     m_controlBar->CreateButtons();
00230 }