The printing framework relies on the application to provide classes whose member functions can respond to particular requests, such as 'print this page' or 'does this page exist in the document?'. This method allows wxWidgets to take over the housekeeping duties of turning preview pages, calling the print dialog box, creating the printer device context, and so on: the application can concentrate on the rendering of the information onto a device context.
In most cases, the only class you will need to derive from is wxPrintout; all others will be used as-is.
A brief description of each class's role and how they work together follows.
For the special case of printing under Unix, where various different printing backends have to be offered, please have a look at Printing Under Unix (GTK+).
case WXPRINT_PRINT: { wxPrinter printer; MyPrintout printout("My printout"); printer.Print(this, &printout, true); break; } case WXPRINT_PREVIEW: { // Pass two printout objects: for preview, and possible printing. wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout); wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650)); frame->Centre(wxBOTH); frame->Initialize(); frame->Show(true); break; }
wxPrintout assembles the printed page and (using your subclass's overrides) writes requested pages to a wxDC that is passed to it. This wxDC could be a wxMemoryDC (for displaying the preview image on-screen), a wxPrinterDC (for printing under MSW and Mac), or a wxPostScriptDC (for printing under GTK or generating PostScript output).
The document/view framework creates a default wxPrintout object for every view, calling wxView::OnDraw() to achieve a prepackaged print/preview facility.
If your window classes have a Draw(wxDC *dc) routine to do screen rendering, your wxPrintout subclass will typically call those routines to create portions of the image on your printout. Your wxPrintout subclass can also make its own calls to its wxDC to draw headers, footers, page numbers, etc.
The scaling of the drawn image typically differs from the screen to the preview and printed images. This class provides a set of routines named FitThisSizeToXXX(), MapScreenSizeToXXX(), and GetLogicalXXXRect, which can be used to set the user scale and origin of the wxPrintout's DC so that your class can easily map your image to the printout withough getting into the details of screen and printer PPI and scaling. See the printing sample for examples of how these routines are used.
There are two important rectangles in printing: the page rectangle defines the printable area seen by the application, and under MSW and Mac, it is the printable area specified by the printer. (For PostScript printing, the page rectangle is the entire page.) The inherited function wxDC::GetSize() returns the page size in device pixels. The point (0,0) on the wxPrinterDC represents the top left corner of the page rectangle; that is, the page rect is given by wxRect(0, 0, w, h), where (w,h) are the values returned by GetSize.
The paper rectangle, on the other hand, represents the entire paper area including the non-printable border. Thus, the coordinates of the top left corner of the paper rectangle will have small negative values, while the width and height will be somewhat larger than that of the page rectangle. The wxPrinterDC-specific function wxPrinterDC::GetPaperRect() returns the paper rectangle of the given wxPrinterDC.
Unlike a wxPrinterDC, there is no distinction between the page rectangle and the paper rectangle in a wxPostScriptDC; both rectangles are taken to represent the entire sheet of paper.
Note that on Macintosh, the native page setup dialog does not contain entries that allow you to change the page margins. You can use the Mac-specific class wxMacPageMarginsDialog (which, like wxPageSetupDialog, takes a wxPageSetupDialogData object in its constructor) to provide this capability; see the printing sample for an example.
You will typically create a global instance of each of a wxPrintData and wxPageSetupDialogData at program initiation, which will contain the default settings provided by the system. Each time the user calls up either the wxPrintDialog or the wxPageSetupDialog, you pass these data structures to initialize the dialog values and to be updated by the dialog. The framework then queries these data structures to get information like the printed page range (from the wxPrintDialogData) or the paper size and/or page orientation (from the wxPageSetupDialogData).
![]() |
[ top ] |