]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: printps.cpp | |
3 | // Purpose: Postscript print/preview framework | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
8826f46f | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
8826f46f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
c801d85f | 20 | #ifdef __GNUG__ |
8826f46f | 21 | #pragma implementation "printps.h" |
c801d85f KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
8826f46f | 28 | #pragma hdrstop |
c801d85f KB |
29 | #endif |
30 | ||
31 | #include "wx/defs.h" | |
32 | ||
33 | #ifndef WX_PRECOMP | |
8826f46f VZ |
34 | #include "wx/utils.h" |
35 | #include "wx/dc.h" | |
36 | #include "wx/app.h" | |
37 | #include "wx/msgdlg.h" | |
8fa2e6a2 KB |
38 | #include "wx/intl.h" |
39 | #include "wx/progdlg.h" | |
c801d85f KB |
40 | #endif |
41 | ||
42 | #include "wx/generic/printps.h" | |
43 | #include "wx/dcprint.h" | |
44 | #include "wx/printdlg.h" | |
45 | #include "wx/generic/prntdlgg.h" | |
7bcb11d3 | 46 | #include "wx/paper.h" |
c801d85f KB |
47 | |
48 | #include <stdlib.h> | |
49 | ||
8826f46f VZ |
50 | // ---------------------------------------------------------------------------- |
51 | // wxWin macros | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
c801d85f | 54 | #if !USE_SHARED_LIBRARY |
8826f46f VZ |
55 | IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase) |
56 | IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase) | |
c801d85f KB |
57 | #endif |
58 | ||
8826f46f VZ |
59 | // ============================================================================ |
60 | // implementation | |
61 | // ============================================================================ | |
7bcb11d3 | 62 | |
8826f46f VZ |
63 | // ---------------------------------------------------------------------------- |
64 | // Printer | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData *data) | |
68 | : wxPrinterBase(data) | |
c801d85f KB |
69 | { |
70 | } | |
71 | ||
8826f46f | 72 | wxPostScriptPrinter::~wxPostScriptPrinter() |
c801d85f KB |
73 | { |
74 | } | |
75 | ||
76 | bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
77 | { | |
7bcb11d3 JS |
78 | sm_abortIt = FALSE; |
79 | sm_abortWindow = (wxWindow *) NULL; | |
8826f46f | 80 | |
7bcb11d3 JS |
81 | if (!printout) |
82 | return FALSE; | |
8826f46f | 83 | |
7bcb11d3 JS |
84 | printout->SetIsPreview(FALSE); |
85 | printout->OnPreparePrinting(); | |
8826f46f | 86 | |
7bcb11d3 JS |
87 | // Get some parameters from the printout, if defined |
88 | int fromPage, toPage; | |
89 | int minPage, maxPage; | |
90 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
8826f46f | 91 | |
7bcb11d3 JS |
92 | if (maxPage == 0) |
93 | return FALSE; | |
8826f46f | 94 | |
7bcb11d3 JS |
95 | m_printDialogData.SetMinPage(minPage); |
96 | m_printDialogData.SetMaxPage(maxPage); | |
97 | if (fromPage != 0) | |
98 | m_printDialogData.SetFromPage(fromPage); | |
99 | if (toPage != 0) | |
100 | m_printDialogData.SetToPage(toPage); | |
101 | ||
102 | if (minPage != 0) | |
c801d85f | 103 | { |
7bcb11d3 JS |
104 | m_printDialogData.EnablePageNumbers(TRUE); |
105 | if (m_printDialogData.GetFromPage() < m_printDialogData.GetMinPage()) | |
106 | m_printDialogData.SetFromPage(m_printDialogData.GetMinPage()); | |
107 | else if (m_printDialogData.GetFromPage() > m_printDialogData.GetMaxPage()) | |
108 | m_printDialogData.SetFromPage(m_printDialogData.GetMaxPage()); | |
109 | if (m_printDialogData.GetToPage() > m_printDialogData.GetMaxPage()) | |
110 | m_printDialogData.SetToPage(m_printDialogData.GetMaxPage()); | |
111 | else if (m_printDialogData.GetToPage() < m_printDialogData.GetMinPage()) | |
112 | m_printDialogData.SetToPage(m_printDialogData.GetMinPage()); | |
c801d85f | 113 | } |
7bcb11d3 JS |
114 | else |
115 | m_printDialogData.EnablePageNumbers(FALSE); | |
c801d85f | 116 | |
8fa2e6a2 | 117 | |
8826f46f | 118 | // Create a suitable device context |
7bcb11d3 JS |
119 | wxDC *dc = (wxDC *) NULL; |
120 | if (prompt) | |
c801d85f | 121 | { |
7bcb11d3 JS |
122 | dc = PrintDialog(parent); |
123 | if (!dc) | |
124 | return FALSE; | |
c801d85f | 125 | } |
7bcb11d3 JS |
126 | else |
127 | { | |
128 | dc = new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), FALSE, (wxWindow *) NULL); | |
129 | } | |
8826f46f | 130 | |
7bcb11d3 JS |
131 | // May have pressed cancel. |
132 | if (!dc || !dc->Ok()) | |
133 | { | |
134 | if (dc) delete dc; | |
135 | return FALSE; | |
136 | } | |
8826f46f | 137 | |
7bcb11d3 JS |
138 | int logPPIScreenX = 0; |
139 | int logPPIScreenY = 0; | |
140 | int logPPIPrinterX = 0; | |
141 | int logPPIPrinterY = 0; | |
8826f46f | 142 | |
7bcb11d3 JS |
143 | logPPIScreenX = 100; |
144 | logPPIScreenY = 100; | |
8826f46f | 145 | |
7bcb11d3 JS |
146 | /* |
147 | // Correct values for X/PostScript? | |
148 | logPPIPrinterX = 100; | |
149 | logPPIPrinterY = 100; | |
150 | */ | |
8826f46f | 151 | |
7bcb11d3 JS |
152 | logPPIPrinterX = 72; |
153 | logPPIPrinterY = 72; | |
8826f46f | 154 | |
7bcb11d3 JS |
155 | printout->SetPPIScreen(logPPIScreenX, logPPIScreenY); |
156 | printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); | |
8826f46f VZ |
157 | |
158 | // Set printout parameters | |
7bcb11d3 | 159 | printout->SetDC(dc); |
8826f46f | 160 | |
7bcb11d3 JS |
161 | int w, h; |
162 | dc->GetSize(&w, &h); | |
163 | printout->SetPageSizePixels((int)w, (int)h); | |
164 | dc->GetSizeMM(&w, &h); | |
165 | printout->SetPageSizeMM((int)w, (int)h); | |
8826f46f | 166 | |
7bcb11d3 JS |
167 | // Create an abort window |
168 | wxBeginBusyCursor(); | |
8826f46f | 169 | |
67df9783 | 170 | int |
e0eeb9b3 | 171 | pagesPerCopy = m_printDialogData.GetToPage()-m_printDialogData.GetFromPage()+1, |
67df9783 KB |
172 | totalPages = pagesPerCopy * m_printDialogData.GetNoCopies(), |
173 | printedPages = 0; | |
8fa2e6a2 KB |
174 | // Open the progress bar dialog |
175 | wxProgressDialog *progressDialog = new wxProgressDialog ( | |
176 | printout->GetTitle(), | |
177 | _("Printing..."), | |
67df9783 | 178 | totalPages, |
c4208367 | 179 | parent ); |
8fa2e6a2 | 180 | |
7bcb11d3 | 181 | printout->OnBeginPrinting(); |
8826f46f | 182 | |
7bcb11d3 | 183 | bool keepGoing = TRUE; |
8826f46f | 184 | |
7bcb11d3 JS |
185 | int copyCount; |
186 | for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++) | |
187 | { | |
188 | if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) | |
189 | { | |
190 | wxEndBusyCursor(); | |
191 | wxMessageBox(_("Could not start printing."), _("Print Error"), wxOK, parent); | |
192 | break; | |
193 | } | |
194 | if (sm_abortIt) | |
195 | break; | |
8826f46f | 196 | |
7bcb11d3 JS |
197 | int pn; |
198 | for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); | |
199 | pn++) | |
200 | { | |
201 | if (sm_abortIt) | |
202 | { | |
203 | keepGoing = FALSE; | |
204 | break; | |
205 | } | |
206 | else | |
207 | { | |
8fa2e6a2 | 208 | wxString msg; |
67df9783 KB |
209 | msg.Printf(_("Printing page %d..."), printedPages+1); |
210 | if(progressDialog->Update(printedPages++, msg)) | |
8fa2e6a2 KB |
211 | { |
212 | dc->StartPage(); | |
213 | printout->OnPrintPage(pn); | |
214 | dc->EndPage(); | |
215 | } | |
216 | else | |
217 | { | |
218 | sm_abortIt = true; | |
67df9783 | 219 | keepGoing = false; |
8fa2e6a2 | 220 | } |
7bcb11d3 JS |
221 | } |
222 | } | |
223 | printout->OnEndDocument(); | |
224 | } | |
8826f46f | 225 | |
7bcb11d3 | 226 | printout->OnEndPrinting(); |
67df9783 | 227 | delete progressDialog; |
8826f46f | 228 | |
7bcb11d3 | 229 | wxEndBusyCursor(); |
8826f46f | 230 | |
7bcb11d3 | 231 | delete dc; |
8826f46f | 232 | |
7bcb11d3 | 233 | return TRUE; |
c801d85f KB |
234 | } |
235 | ||
7bcb11d3 | 236 | wxDC* wxPostScriptPrinter::PrintDialog(wxWindow *parent) |
c801d85f | 237 | { |
7bcb11d3 JS |
238 | wxDC* dc = (wxDC*) NULL; |
239 | wxGenericPrintDialog* dialog = new wxGenericPrintDialog(parent, & m_printDialogData); | |
240 | int ret = dialog->ShowModal() ; | |
241 | if (ret == wxID_OK) | |
242 | { | |
243 | dc = dialog->GetPrintDC(); | |
244 | m_printDialogData = dialog->GetPrintDialogData(); | |
245 | } | |
246 | dialog->Destroy(); | |
247 | ||
248 | return dc; | |
c801d85f KB |
249 | } |
250 | ||
251 | bool wxPostScriptPrinter::Setup(wxWindow *parent) | |
252 | { | |
7bcb11d3 JS |
253 | wxGenericPrintDialog* dialog = new wxGenericPrintDialog(parent, & m_printDialogData); |
254 | dialog->GetPrintDialogData().SetSetupDialog(TRUE); | |
255 | ||
256 | int ret = dialog->ShowModal(); | |
257 | ||
258 | if (ret == wxID_OK) | |
259 | { | |
260 | m_printDialogData = dialog->GetPrintDialogData(); | |
261 | } | |
262 | ||
263 | dialog->Destroy(); | |
264 | ||
265 | return (ret == wxID_OK); | |
c801d85f KB |
266 | } |
267 | ||
8826f46f VZ |
268 | // ---------------------------------------------------------------------------- |
269 | // Print preview | |
270 | // ---------------------------------------------------------------------------- | |
c801d85f | 271 | |
8826f46f VZ |
272 | void wxPostScriptPrintPreview::Init(wxPrintout * WXUNUSED(printout), |
273 | wxPrintout * WXUNUSED(printoutForPrinting)) | |
c801d85f | 274 | { |
7bcb11d3 JS |
275 | // Have to call it here since base constructor can't call it |
276 | DetermineScaling(); | |
c801d85f KB |
277 | } |
278 | ||
8826f46f VZ |
279 | wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout, |
280 | wxPrintout *printoutForPrinting, | |
281 | wxPrintDialogData *data) | |
282 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
283 | { | |
284 | Init(printout, printoutForPrinting); | |
285 | } | |
286 | ||
287 | wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout, | |
288 | wxPrintout *printoutForPrinting, | |
289 | wxPrintData *data) | |
290 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
291 | { | |
292 | Init(printout, printoutForPrinting); | |
293 | } | |
294 | ||
295 | wxPostScriptPrintPreview::~wxPostScriptPrintPreview() | |
c801d85f KB |
296 | { |
297 | } | |
298 | ||
299 | bool wxPostScriptPrintPreview::Print(bool interactive) | |
300 | { | |
7bcb11d3 JS |
301 | if (!m_printPrintout) |
302 | return FALSE; | |
303 | wxPostScriptPrinter printer(& m_printDialogData); | |
304 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
c801d85f KB |
305 | } |
306 | ||
8826f46f | 307 | void wxPostScriptPrintPreview::DetermineScaling() |
c801d85f | 308 | { |
7bcb11d3 JS |
309 | wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId(); |
310 | if (paperType == wxPAPER_NONE) | |
311 | paperType = wxPAPER_NONE; | |
8826f46f | 312 | |
c801d85f KB |
313 | wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType); |
314 | if (!paper) | |
7bcb11d3 JS |
315 | paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4); |
316 | ||
c801d85f KB |
317 | if (paper) |
318 | { | |
7bcb11d3 JS |
319 | m_previewPrintout->SetPPIScreen(100, 100); |
320 | // m_previewPrintout->SetPPIPrinter(100, 100); | |
321 | m_previewPrintout->SetPPIPrinter(72, 72); | |
8826f46f | 322 | |
7bcb11d3 JS |
323 | wxSize sizeDevUnits(paper->GetSizeDeviceUnits()); |
324 | wxSize sizeTenthsMM(paper->GetSize()); | |
325 | wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10); | |
326 | ||
327 | // If in landscape mode, we need to swap the width and height. | |
328 | if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE ) | |
329 | { | |
330 | m_pageWidth = sizeDevUnits.y; | |
331 | m_pageHeight = sizeDevUnits.x; | |
332 | m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x); | |
333 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
334 | } | |
335 | else | |
336 | { | |
337 | m_pageWidth = sizeDevUnits.x; | |
338 | m_pageHeight = sizeDevUnits.y; | |
339 | m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y); | |
340 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
341 | } | |
8826f46f | 342 | |
7bcb11d3 JS |
343 | // At 100%, the page should look about page-size on the screen. |
344 | m_previewScale = (float)0.8; | |
c801d85f KB |
345 | } |
346 | } | |
347 |