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