📄 tprint.tex
字号:
\section{Printing overview}\label{printingoverview}Classes: \helpref{wxPrintout}{wxprintout}, \helpref{wxPrinter}{wxprinter}, \helpref{wxPrintPreview}{wxprintpreview}, \helpref{wxPrinterDC}{wxprinterdc}, \helpref{wxPostScriptDC}{wxpostscriptdc}, \helpref{wxPrintDialog}{wxprintdialog}, \helpref{wxPrintData}{wxprintdata}, \helpref{wxPrintDialogData}{wxprintdialogdata}, \helpref{wxPageSetupDialog}{wxpagesetupdialog}, \helpref{wxPageSetupDialogData}{wxpagesetupdialogdata}The printing framework relies on the application to provide classes whose memberfunctions can respond to particular requests, such as `print this page' or `doesthis page exist in the document?'. This method allows wxWidgets to take over thehousekeeping duties of turning preview pages, calling the print dialog box,creating the printer device context, and so on: the application can concentrateon the rendering of the information onto a device context.In most cases, the only class you will need to derive from is\helpref{wxPrintout}{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 differentprinting backends have to be offered, please have a look at the\helpref{Unix printing overview}{unixprinting}.\subsection{\helpref{wxPrintout}{wxprintout}}A document's printing ability is represented in an application by a derivedwxPrintout class. This class prints a page on request, and can be passed to thePrint function of a wxPrinter object to actually print the document, or can bepassed to a wxPrintPreview object to initiate previewing. The following code(from the printing sample) shows how easy it is to initiate printing, previewingand the print setup dialog, once the wxPrintout functionality has been defined.Notice the use of MyPrintout for both printing and previewing. All the previewuser interface functionality is taken care of by wxWidgets. For more details on howMyPrintout is defined, please look at the printout sample code.\begin{verbatim} 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; }\end{verbatim}Class \helpref{wxPrintout}{wxprintout} assembles the printed page and (usingyour subclass's overrides) writes requested pages to a \helpref{wxDC}{wxdc} thatis passed to it. This wxDC could be a \helpref{wxMemoryDC}{wxmemorydc} (fordisplaying the preview image on-screen), a \helpref{wxPrinterDC}{wxprinterdc}(for printing under MSW and Mac), or a \helpref{wxPostScriptDC}{wxpostscriptdc}(for printing under GTK or generating PostScript output).The \helpref{document/view framework}{docviewoverview} creates a defaultwxPrintout object for every view, calling wxView::OnDraw to achieve aprepackaged 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 portionsof the image on your printout. Your wxPrintout subclass can also make its owncalls to its wxDC to draw headers, footers, page numbers, etc.The scaling of the drawn image typically differs from the screen to the previewand printed images. This class provides a set of routines namedFitThisSizeToXXX(), MapScreenSizeToXXX(), and GetLogicalXXXRect, which can beused to set the user scale and origin of the wxPrintout's DC so that your classcan easily map your image to the printout withough getting into the details ofscreen and printer PPI and scaling. See the printing sample for examples of howthese routines are used.\subsection{\helpref{wxPrinter}{wxprinter}}Class wxPrinter encapsulates the platform-dependent print function with a commoninterface. 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.\subsection{\helpref{wxPrintPreview}{wxprintpreview}}Class wxPrintPreview manages the print preview process. Among other things, itconstructs the wxDCs that get passed to your wxPrintout subclass for printingand manages the display of multiple pages, a zoomable preview image, and soforth. In most cases you will use this class as-is, but you can create your ownsubclass, for example, to change the layout or contents of the preview window.\subsection{\helpref{wxPrinterDC}{wxprinterdc}}Class wxPrinterDC is the wxDC that represents the actual printed page under MSWand Mac. During printing, an object of this class will be passed to your derivedwxPrintout object to draw upon. The size of the wxPrinterDC will depend on thepaper orientation and the resolution of the printer.There are two important rectangles in printing: the \em{page rectangle} definesthe printable area seen by the application, and under MSW and Mac, it is theprintable area specified by the printer. (For PostScript printing, the pagerectangle is the entire page.) The inherited function\helpref{wxDC::GetSize}{wxdcgetsize} returns the page size in device pixels. Thepoint (0,0) on the wxPrinterDC represents the top left corner of the pagerectangle; that is, the page rect is given by wxRect(0, 0, w, h), where (w,h)are the values returned by GetSize.The \em{paper rectangle}, on the other hand, represents the entire paper areaincluding the non-printable border. Thus, the coordinates of the top left cornerof the paper rectangle will have small negative values, while the width andheight will be somewhat larger than that of the page rectangle. ThewxPrinterDC-specific function\helpref{wxPrinterDC::GetPaperRect}{wxprinterdcgetpaperrect} returns the paperrectangle of the given wxPrinterDC.\subsection{\helpref{wxPostScriptDC}{wxpostscriptdc}}Class wxPostScriptDC is the wxDC that represents the actual printed page underGTK and other PostScript printing. During printing, an object of this class willbe passed to your derived wxPrintout object to draw upon. The size of thewxPostScriptDC will depend upon the \helpref{wxPrintData}{wxprintdata} used toconstruct it.Unlike a wxPrinterDC, there is no distinction between the page rectangle and thepaper rectangle in a wxPostScriptDC; both rectangles are taken to represent theentire sheet of paper.\subsection{\helpref{wxPrintDialog}{wxprintdialog}}Class wxPrintDialog puts up the standard print dialog, which allows you toselect the page range for printing (as well as many other print settings, whichmay vary from platform to platform). You provide an object of type\helpref{wxPrintDialogData}{wxprintdialogdata} to the wxPrintDialog atconstruction, which is used to populate the dialog.\subsection{\helpref{wxPrintData}{wxprintdata}}Class wxPrintData is a subset of wxPrintDialogData that is used (internally) toinitialize a wxPrinterDC or wxPostScriptDC. (In fact, a wxPrintData is a datamember of a wxPrintDialogData and a wxPageSetupDialogData). Essentially,wxPrintData contains those bits of information from the two dialogs necessary toconfigure the wxPrinterDC or wxPostScriptDC (e.g., size, orientation, etc.). Youmight wish to create a global instance of this object to provide call-to-callpersistence to your application's print settings.\subsection{\helpref{wxPrintDialogData}{wxprintdialogdata}}Class wxPrintDialogData contains the settings entered by the user in the printdialog. 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 takescare of asking your wxPrintout derived object for the pages requested by theuser.\subsection{\helpref{wxPageSetupDialog}{wxpagesetupdialog}}Class wxPageSetupDialog puts up the standard page setup dialog, which allows youto specify the orientation, paper size, and related settings. You provide itwith a wxPageSetupDialogData object at intialization, which is used to populatethe dialog; when the dialog is dismissed, this object contains the settingschosen by the user, including orientation and/or page margins.Note that on Macintosh, the native page setup dialog does not contain entriesthat allow you to change the page margins. You can use the Mac-specific classwxMacPageMarginsDialog (which, like wxPageSetupDialog, takes awxPageSetupDialogData object in its constructor) to provide this capability; seethe printing sample for an example.\subsection{\helpref{wxPageSetupDialogData}{wxpagesetupdialogdata}}Class wxPageSetupDialogData contains settings affecting the page size (papersize), orientation, margins, and so forth. Note that not all platforms populateall fields; for example, the MSW page setup dialog lets you set the page marginswhile the Mac setup dialog does not.You will typically create a global instance of each of a wxPrintData andwxPageSetupDialogData at program initiation, which will contain the defaultsettings provided by the system. Each time the user calls up either thewxPrintDialog or the wxPageSetupDialog, you pass these data structures toinitialize the dialog values and to be updated by the dialog. The framework thenqueries these data structures to get information like the printed page range(from the wxPrintDialogData) or the paper size and/or page orientation (from thewxPageSetupDialogData).\section{Printing under Unix (GTK+)}\label{unixprinting}Printing under Unix has always been a cause of problems as Unixdoes not provide a standard way to display text and graphicson screen and print it to a printer using the same applicationprogramming interface - instead, displaying on screen is donevia the X11 library while printing has to be done with usingPostScript commands. This was particularly difficult to handlefor the case of fonts with the result that only a selectednumber of application could offer WYSIWYG under Unix. Equally,wxWidgets offered its own printing implementation using PostScriptwhich never really matched the screen display.Starting with version 2.8.X, the GNOME project provides printingsupport through the libgnomeprint and libgnomeprintui librariesby which especially the font problem is mostly solved. Beginningwith version 2.5.4, the GTK+ port of wxWidgets can make use ofthese libraries if wxWidgets is configured accordingly and if thelibraries are present. You need to configure wxWidgets with the{\it configure --with-gnomeprint} switch and your application willthen search for the GNOME print libraries at runtime. If theyare found, printing will be done through these, otherwise theapplication will fall back to the old PostScript printing code.Note that the application will not require the GNOME print librariesto be installed in order to run (there will be no dependency onthese libraries).In version GTK+ 2.10, support for printing has finally beenadded to GTK+ itself. Support for this has yet to be writtenfor wxGTK (which requires drawing through Cairo).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -