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