printing overviewclasses: wxprintout, wxprinter, wxprintpreview, wxprinterdc, wxpostscriptdc, wxprintdialog, wxprintdata, wxprintdialogdata, wxpagesetupdialog, wxpagesetupdialogdata 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 the unix printing overview.
wxprintout
wxprintouta document's printing ability is represented in an application by a derived wxprintout class. this class prints a page on request, and can be passed to the print function of a wxprinter object to actually print the document, or can be passed to a wxprintpreview object to initiate previewing. the following code (from the printing sample) shows how easy it is to initiate printing, previewing and the print setup dialog, once the wxprintout functionality has been defined. notice the use of myprintout for both printing and previewing. all the preview user interface functionality is taken care of by wxwidgets. for more details on how myprintout is defined, please look at the printout sample code.
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; }class 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.
wxprinterclass wxprinter encapsulates the platform-dependent print function with a common interface. in most cases, you will not need to derive a class from wxprinter; simply create a wxprinter object in your print function as in the example above.
wxprintpreviewclass wxprintpreview manages the print preview process. among other things, it constructs the wxdcs that get passed to your wxprintout subclass for printing and manages the display of multiple pages, a zoomable preview image, and so forth. in most cases you will use this class as-is, but you can create your own subclass, for example, to change the layout or contents of the preview window.
wxprinterdcclass wxprinterdc is the wxdc that represents the actual printed page under msw and mac. during printing, an object of this class will be passed to your derived wxprintout object to draw upon. the size of the wxprinterdc will depend on the paper orientation and the resolution of the printer. 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.
wxpostscriptdcclass wxpostscriptdc is the wxdc that represents the actual printed page under gtk and other postscript printing. during printing, an object of this class will be passed to your derived wxprintout object to draw upon. the size of the wxpostscriptdc will depend upon the wxprintdata used to construct it. 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.
wxprintdialogclass wxprintdialog puts up the standard print dialog, which allows you to select the page range for printing (as well as many other print settings, which may vary from platform to platform). you provide an object of type wxprintdialogdata to the wxprintdialog at construction, which is used to populate the dialog.
wxprintdataclass wxprintdata is a subset of wxprintdialogdata that is used (internally) to initialize a wxprinterdc or wxpostscriptdc. (in fact, a wxprintdata is a data member of a wxprintdialogdata and a wxpagesetupdialogdata). essentially, wxprintdata contains those bits of information from the two dialogs necessary to configure the wxprinterdc or wxpostscriptdc (e.g., size, orientation, etc.). you might wish to create a global instance of this object to provide call-to-call persistence to your application's print settings.
wxprintdialogdataclass wxprintdialogdata contains the settings entered by the user in the print dialog. it contains such things as page range, number of copies, and so forth. in most cases, you won't need to access this information; the framework takes care of asking your wxprintout derived object for the pages requested by the user.
wxpagesetupdialogclass wxpagesetupdialog puts up the standard page setup dialog, which allows you to specify the orientation, paper size, and related settings. you provide it with a wxpagesetupdialogdata object at intialization, which is used to populate the dialog; when the dialog is dismissed, this object contains the settings chosen by the user, including orientation and/or page margins. 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.
wxpagesetupdialogdataclass wxpagesetupdialogdata contains settings affecting the page size (paper size), orientation, margins, and so forth. note that not all platforms populate all fields; for example, the msw page setup dialog lets you set the page margins while the mac setup dialog does not. 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).
|