]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/tprint.tex
[ 1585270 ] Further fixes to wxPathList
[wxWidgets.git] / docs / latex / wx / tprint.tex
1 \section{Printing overview}\label{printingoverview}
2
3 Classes: \helpref{wxPrintout}{wxprintout},
4 \helpref{wxPrinter}{wxprinter},
5 \helpref{wxPrintPreview}{wxprintpreview},
6 \helpref{wxPrinterDC}{wxprinterdc},
7 \helpref{wxPostScriptDC}{wxpostscriptdc},
8 \helpref{wxPrintDialog}{wxprintdialog},
9 \helpref{wxPrintData}{wxprintdata},
10 \helpref{wxPrintDialogData}{wxprintdialogdata},
11 \helpref{wxPageSetupDialog}{wxpagesetupdialog},
12 \helpref{wxPageSetupDialogData}{wxpagesetupdialogdata}
13
14 The printing framework relies on the application to provide classes whose member
15 functions can respond to particular requests, such as `print this page' or `does
16 this page exist in the document?'. This method allows wxWidgets to take over the
17 housekeeping duties of turning preview pages, calling the print dialog box,
18 creating the printer device context, and so on: the application can concentrate
19 on the rendering of the information onto a device context.
20
21 In most cases, the only class you will need to derive from is
22 \helpref{wxPrintout}{wxprintout}; all others will be used as-is.
23
24 A brief description of each class's role and how they work together follows.
25
26 \subsection{\helpref{wxPrintout}{wxprintout}}
27
28 A document's printing ability is represented in an application by a derived
29 wxPrintout class. This class prints a page on request, and can be passed to the
30 Print function of a wxPrinter object to actually print the document, or can be
31 passed to a wxPrintPreview object to initiate previewing. The following code
32 (from the printing sample) shows how easy it is to initiate printing, previewing
33 and the print setup dialog, once the wxPrintout functionality has been defined.
34 Notice the use of MyPrintout for both printing and previewing. All the preview
35 user interface functionality is taken care of by wxWidgets. For more details on how
36 MyPrintout is defined, please look at the printout sample code.
37
38 \begin{verbatim}
39 case WXPRINT_PRINT:
40 {
41 wxPrinter printer;
42 MyPrintout printout("My printout");
43 printer.Print(this, &printout, true);
44 break;
45 }
46 case WXPRINT_PREVIEW:
47 {
48 // Pass two printout objects: for preview, and possible printing.
49 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout);
50 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
51 frame->Centre(wxBOTH);
52 frame->Initialize();
53 frame->Show(true);
54 break;
55 }
56 \end{verbatim}
57
58 Class \helpref{wxPrintout}{wxprintout} assembles the printed page and (using
59 your subclass's overrides) writes requested pages to a \helpref{wxDC}{wxdc} that
60 is passed to it. This wxDC could be a \helpref{wxMemoryDC}{wxmemorydc} (for
61 displaying the preview image on-screen), a \helpref{wxPrinterDC}{wxprinterdc}
62 (for printing under MSW and Mac), or a \helpref{wxPostScriptDC}{wxpostscriptdc}
63 (for printing under GTK or generating PostScript output).
64
65 The \helpref{document/view framework}{docviewoverview} creates a default
66 wxPrintout object for every view, calling wxView::OnDraw to achieve a
67 prepackaged print/preview facility.
68
69 If your window classes have a Draw(wxDC *dc) routine to do screen rendering,
70 your wxPrintout subclass will typically call those routines to create portions
71 of the image on your printout. Your wxPrintout subclass can also make its own
72 calls to its wxDC to draw headers, footers, page numbers, etc.
73
74 The scaling of the drawn image typically differs from the screen to the preview
75 and printed images. This class provides a set of routines named
76 FitThisSizeToXXX(), MapScreenSizeToXXX(), and GetLogicalXXXRect, which can be
77 used to set the user scale and origin of the wxPrintout's DC so that your class
78 can easily map your image to the printout withough getting into the details of
79 screen and printer PPI and scaling. See the printing sample for examples of how
80 these routines are used.
81
82 \subsection{\helpref{wxPrinter}{wxprinter}}
83
84 Class wxPrinter encapsulates the platform-dependent print function with a common
85 interface. In most cases, you will not need to derive a class from wxPrinter;
86 simply create a wxPrinter object in your Print function as in the example above.
87
88 \subsection{\helpref{wxPrintPreview}{wxprintpreview}}
89
90 Class wxPrintPreview manages the print preview process. Among other things, it
91 constructs the wxDCs that get passed to your wxPrintout subclass for printing
92 and manages the display of multiple pages, a zoomable preview image, and so
93 forth. In most cases you will use this class as-is, but you can create your own
94 subclass, for example, to change the layout or contents of the preview window.
95
96
97 \subsection{\helpref{wxPrinterDC}{wxprinterdc}}
98
99 Class wxPrinterDC is the wxDC that represents the actual printed page under MSW
100 and Mac. During printing, an object of this class will be passed to your derived
101 wxPrintout object to draw upon. The size of the wxPrinterDC will depend on the
102 paper orientation and the resolution of the printer.
103
104 There are two important rectangles in printing: the \em{page rectangle} defines
105 the printable area seen by the application, and under MSW and Mac, it is the
106 printable area specified by the printer. (For PostScript printing, the page
107 rectangle is the entire page.) The inherited function
108 \helpref{wxDC::GetSize}{wxdcgetsize} returns the page size in device pixels. The
109 point (0,0) on the wxPrinterDC represents the top left corner of the page
110 rectangle; that is, the page rect is given by wxRect(0, 0, w, h), where (w,h)
111 are the values returned by GetSize.
112
113 The \em{paper rectangle}, on the other hand, represents the entire paper area
114 including the non-printable border. Thus, the coordinates of the top left corner
115 of the paper rectangle will have small negative values, while the width and
116 height will be somewhat larger than that of the page rectangle. The
117 wxPrinterDC-specific function
118 \helpref{wxPrinterDC::GetPaperRect}{wxprinterdcgetpaperrect} returns the paper
119 rectangle of the given wxPrinterDC.
120
121 \subsection{\helpref{wxPostScriptDC}{wxpostscriptdc}}
122
123 Class wxPostScriptDC is the wxDC that represents the actual printed page under
124 GTK and other PostScript printing. During printing, an object of this class will
125 be passed to your derived wxPrintout object to draw upon. The size of the
126 wxPostScriptDC will depend upon the \helpref{wxPrintData}{wxprintdata} used to
127 construct it.
128
129 Unlike a wxPrinterDC, there is no distinction between the page rectangle and the
130 paper rectangle in a wxPostScriptDC; both rectangles are taken to represent the
131 entire sheet of paper.
132
133 \subsection{\helpref{wxPrintDialog}{wxprintdialog}}
134
135 Class wxPrintDialog puts up the standard print dialog, which allows you to
136 select the page range for printing (as well as many other print settings, which
137 may vary from platform to platform). You provide an object of type
138 \helpref{wxPrintDialogData}{wxprintdialogdata} to the wxPrintDialog at
139 construction, which is used to populate the dialog.
140
141 \subsection{\helpref{wxPrintData}{wxprintdata}}
142
143 Class wxPrintData is a subset of wxPrintDialogData that is used (internally) to
144 initialize a wxPrinterDC or wxPostScriptDC. (In fact, a wxPrintData is a data
145 member of a wxPrintDialogData and a wxPageSetupDialogData). Essentially,
146 wxPrintData contains those bits of information from the two dialogs necessary to
147 configure the wxPrinterDC or wxPostScriptDC (e.g., size, orientation, etc.). You
148 might wish to create a global instance of this object to provide call-to-call
149 persistence to your application's print settings.
150
151 \subsection{\helpref{wxPrintDialogData}{wxprintdialogdata}}
152
153 Class wxPrintDialogData contains the settings entered by the user in the print
154 dialog. It contains such things as page range, number of copies, and so forth.
155 In most cases, you won't need to access this information; the framework takes
156 care of asking your wxPrintout derived object for the pages requested by the
157 user.
158
159 \subsection{\helpref{wxPageSetupDialog}{wxpagesetupdialog}}
160
161 Class wxPageSetupDialog puts up the standard page setup dialog, which allows you
162 to specify the orientation, paper size, and related settings. You provide it
163 with a wxPageSetupDialogData object at intialization, which is used to populate
164 the dialog; when the dialog is dismissed, this object contains the settings
165 chosen by the user, including orientation and/or page margins.
166
167 Note that on Macintosh, the native page setup dialog does not contain entries
168 that allow you to change the page margins. You can use the Mac-specific class
169 wxMacPageMarginsDialog (which, like wxPageSetupDialog, takes a
170 wxPageSetupDialogData object in its constructor) to provide this capability; see
171 the printing sample for an example.
172
173 \subsection{\helpref{wxPageSetupDialogData}{wxpagesetupdialogdata}}
174
175 Class wxPageSetupDialogData contains settings affecting the page size (paper
176 size), orientation, margins, and so forth. Note that not all platforms populate
177 all fields; for example, the MSW page setup dialog lets you set the page margins
178 while the Mac setup dialog does not.
179
180 You will typically create a global instance of each of a wxPrintData and
181 wxPageSetupDialogData at program initiation, which will contain the default
182 settings provided by the system. Each time the user calls up either the
183 wxPrintDialog or the wxPageSetupDialog, you pass these data structures to
184 initialize the dialog values and to be updated by the dialog. The framework then
185 queries these data structures to get information like the printed page range
186 (from the wxPrintDialogData) or the paper size and/or page orientation (from the
187 wxPageSetupDialogData).
188
189
190
191 \section{Printing under Unix (GTK+)}\label{unixprinting}
192
193 Printing under Unix has always been a cause of problems as Unix
194 does not provide a standard way to display text and graphics
195 on screen and print it to a printer using the same application
196 programming interface - instead, displaying on screen is done
197 via the X11 library while printing has to be done with using
198 PostScript commands. This was particularly difficult to handle
199 for the case of fonts with the result that only a selected
200 number of application could offer WYSIWYG under Unix. Equally,
201 wxWidgets offered its own printing implementation using PostScript
202 which never really matched the screen display.
203
204 Starting with version 2.8.X, the GNOME project provides printing
205 support through the libgnomeprint and libgnomeprintui libraries
206 by which especially the font problem is mostly solved. Beginning
207 with version 2.5.4, the GTK+ port of wxWidgets can make use of
208 these libraries if wxWidgets is configured accordingly and if the
209 libraries are present. You need to configure wxWidgets with the
210 {\it configure --with-gnomeprint} switch and you application will
211 then search for the GNOME print libraries at runtime. If they
212 are found, printing will be done through these, otherwise the
213 application will fall back to the old PostScript printing code.
214 Note that the application will not require the GNOME print libraries
215 to be installed in order to run (there will be no dependency on
216 these libraries).
217
218 It is expected that the printing code that is currently implemented
219 in the GNOME print libraries will be moved into GTK+ later.
220