]>
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$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
8826f46f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
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 | ||
12bdd77c | 33 | #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW) |
ce4169a4 | 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" | |
ca65c044 | 42 | #include "wx/log.h" |
c801d85f KB |
43 | #endif |
44 | ||
45 | #include "wx/generic/printps.h" | |
46 | #include "wx/dcprint.h" | |
47 | #include "wx/printdlg.h" | |
48 | #include "wx/generic/prntdlgg.h" | |
2179d579 | 49 | #include "wx/generic/progdlgg.h" |
7bcb11d3 | 50 | #include "wx/paper.h" |
c801d85f KB |
51 | |
52 | #include <stdlib.h> | |
53 | ||
8826f46f VZ |
54 | // ---------------------------------------------------------------------------- |
55 | // wxWin macros | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
8826f46f VZ |
58 | IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase) |
59 | IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase) | |
ca65c044 | 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 | { | |
ca65c044 | 80 | sm_abortIt = false; |
7bcb11d3 | 81 | sm_abortWindow = (wxWindow *) NULL; |
8826f46f | 82 | |
7bcb11d3 | 83 | if (!printout) |
f6bcfd97 BP |
84 | { |
85 | sm_lastError = wxPRINTER_ERROR; | |
ca65c044 | 86 | return false; |
f6bcfd97 | 87 | } |
8826f46f | 88 | |
ca65c044 | 89 | printout->SetIsPreview(false); |
1044a386 | 90 | |
d2b354f9 JS |
91 | if (m_printDialogData.GetMinPage() < 1) |
92 | m_printDialogData.SetMinPage(1); | |
93 | if (m_printDialogData.GetMaxPage() < 1) | |
94 | m_printDialogData.SetMaxPage(9999); | |
8fa2e6a2 | 95 | |
8826f46f | 96 | // Create a suitable device context |
999836aa | 97 | wxDC *dc; |
7bcb11d3 | 98 | if (prompt) |
c801d85f | 99 | { |
7bcb11d3 JS |
100 | dc = PrintDialog(parent); |
101 | if (!dc) | |
ca65c044 | 102 | return false; |
c801d85f | 103 | } |
7bcb11d3 JS |
104 | else |
105 | { | |
f6bcfd97 | 106 | dc = new wxPostScriptDC(GetPrintDialogData().GetPrintData()); |
7bcb11d3 | 107 | } |
8826f46f | 108 | |
7bcb11d3 JS |
109 | // May have pressed cancel. |
110 | if (!dc || !dc->Ok()) | |
111 | { | |
112 | if (dc) delete dc; | |
f6bcfd97 | 113 | sm_lastError = wxPRINTER_ERROR; |
ca65c044 | 114 | return false; |
7bcb11d3 | 115 | } |
8826f46f | 116 | |
904a68b6 RL |
117 | wxSize ScreenPixels = wxGetDisplaySize(); |
118 | wxSize ScreenMM = wxGetDisplaySizeMM(); | |
8826f46f | 119 | |
7a5e6267 JS |
120 | printout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()), |
121 | (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) ); | |
904a68b6 RL |
122 | printout->SetPPIPrinter( wxPostScriptDC::GetResolution(), |
123 | wxPostScriptDC::GetResolution() ); | |
8826f46f VZ |
124 | |
125 | // Set printout parameters | |
7bcb11d3 | 126 | printout->SetDC(dc); |
8826f46f | 127 | |
7bcb11d3 JS |
128 | int w, h; |
129 | dc->GetSize(&w, &h); | |
130 | printout->SetPageSizePixels((int)w, (int)h); | |
131 | dc->GetSizeMM(&w, &h); | |
132 | printout->SetPageSizeMM((int)w, (int)h); | |
8826f46f | 133 | |
7bcb11d3 JS |
134 | // Create an abort window |
135 | wxBeginBusyCursor(); | |
8826f46f | 136 | |
1044a386 JS |
137 | printout->OnPreparePrinting(); |
138 | ||
d2b354f9 JS |
139 | // Get some parameters from the printout, if defined |
140 | int fromPage, toPage; | |
141 | int minPage, maxPage; | |
142 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
143 | ||
144 | if (maxPage == 0) | |
145 | { | |
146 | sm_lastError = wxPRINTER_ERROR; | |
147 | wxEndBusyCursor(); | |
ca65c044 | 148 | return false; |
d2b354f9 JS |
149 | } |
150 | ||
151 | // Only set min and max, because from and to have been | |
152 | // set by the user | |
153 | m_printDialogData.SetMinPage(minPage); | |
154 | m_printDialogData.SetMaxPage(maxPage); | |
01b5396f RR |
155 | |
156 | if (m_printDialogData.GetFromPage() < minPage) | |
157 | m_printDialogData.SetFromPage( minPage ); | |
158 | if (m_printDialogData.GetToPage() > maxPage) | |
159 | m_printDialogData.SetToPage( maxPage ); | |
160 | ||
67df9783 | 161 | int |
e0eeb9b3 | 162 | pagesPerCopy = m_printDialogData.GetToPage()-m_printDialogData.GetFromPage()+1, |
67df9783 KB |
163 | totalPages = pagesPerCopy * m_printDialogData.GetNoCopies(), |
164 | printedPages = 0; | |
8fa2e6a2 KB |
165 | // Open the progress bar dialog |
166 | wxProgressDialog *progressDialog = new wxProgressDialog ( | |
167 | printout->GetTitle(), | |
168 | _("Printing..."), | |
67df9783 | 169 | totalPages, |
ffdaed2f KB |
170 | parent, |
171 | wxPD_CAN_ABORT|wxPD_AUTO_HIDE|wxPD_APP_MODAL); | |
2179d579 | 172 | |
7bcb11d3 | 173 | printout->OnBeginPrinting(); |
8826f46f | 174 | |
f6bcfd97 BP |
175 | sm_lastError = wxPRINTER_NO_ERROR; |
176 | ||
ca65c044 | 177 | bool keepGoing = true; |
8826f46f | 178 | |
7bcb11d3 JS |
179 | int copyCount; |
180 | for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++) | |
181 | { | |
182 | if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) | |
183 | { | |
184 | wxEndBusyCursor(); | |
f6bcfd97 BP |
185 | wxLogError(_("Could not start printing.")); |
186 | sm_lastError = wxPRINTER_ERROR; | |
7bcb11d3 JS |
187 | break; |
188 | } | |
189 | if (sm_abortIt) | |
f6bcfd97 BP |
190 | { |
191 | sm_lastError = wxPRINTER_CANCELLED; | |
7bcb11d3 | 192 | break; |
f6bcfd97 | 193 | } |
8826f46f | 194 | |
7bcb11d3 JS |
195 | int pn; |
196 | for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); | |
197 | pn++) | |
198 | { | |
199 | if (sm_abortIt) | |
200 | { | |
ca65c044 | 201 | keepGoing = false; |
f6bcfd97 | 202 | sm_lastError = wxPRINTER_CANCELLED; |
7bcb11d3 JS |
203 | break; |
204 | } | |
205 | else | |
206 | { | |
8fa2e6a2 | 207 | wxString msg; |
67df9783 KB |
208 | msg.Printf(_("Printing page %d..."), printedPages+1); |
209 | if(progressDialog->Update(printedPages++, msg)) | |
8fa2e6a2 KB |
210 | { |
211 | dc->StartPage(); | |
212 | printout->OnPrintPage(pn); | |
213 | dc->EndPage(); | |
214 | } | |
215 | else | |
216 | { | |
ca65c044 | 217 | sm_abortIt = true; |
f6bcfd97 | 218 | sm_lastError = wxPRINTER_CANCELLED; |
ca65c044 | 219 | keepGoing = false; |
8fa2e6a2 | 220 | } |
7bcb11d3 | 221 | } |
ca65c044 | 222 | wxYield(); |
7bcb11d3 JS |
223 | } |
224 | printout->OnEndDocument(); | |
225 | } | |
8826f46f | 226 | |
7bcb11d3 | 227 | printout->OnEndPrinting(); |
67df9783 | 228 | delete progressDialog; |
8826f46f | 229 | |
7bcb11d3 | 230 | wxEndBusyCursor(); |
8826f46f | 231 | |
7bcb11d3 | 232 | delete dc; |
8826f46f | 233 | |
f6bcfd97 | 234 | return (sm_lastError == wxPRINTER_NO_ERROR); |
c801d85f KB |
235 | } |
236 | ||
7bcb11d3 | 237 | wxDC* wxPostScriptPrinter::PrintDialog(wxWindow *parent) |
c801d85f | 238 | { |
7bcb11d3 | 239 | wxDC* dc = (wxDC*) NULL; |
01b5396f RR |
240 | |
241 | wxGenericPrintDialog dialog( parent, &m_printDialogData ); | |
242 | if (dialog.ShowModal() == wxID_OK) | |
7bcb11d3 | 243 | { |
01b5396f RR |
244 | dc = dialog.GetPrintDC(); |
245 | m_printDialogData = dialog.GetPrintDialogData(); | |
246 | ||
f6bcfd97 BP |
247 | if (dc == NULL) |
248 | sm_lastError = wxPRINTER_ERROR; | |
249 | else | |
250 | sm_lastError = wxPRINTER_NO_ERROR; | |
7bcb11d3 | 251 | } |
f6bcfd97 BP |
252 | else |
253 | sm_lastError = wxPRINTER_CANCELLED; | |
254 | ||
7bcb11d3 | 255 | return dc; |
c801d85f KB |
256 | } |
257 | ||
4e4528f6 | 258 | bool wxPostScriptPrinter::Setup(wxWindow *WXUNUSED(parent)) |
c801d85f | 259 | { |
04a014a5 | 260 | #if 0 |
7bcb11d3 | 261 | wxGenericPrintDialog* dialog = new wxGenericPrintDialog(parent, & m_printDialogData); |
ca65c044 | 262 | dialog->GetPrintDialogData().SetSetupDialog(true); |
7bcb11d3 JS |
263 | |
264 | int ret = dialog->ShowModal(); | |
265 | ||
266 | if (ret == wxID_OK) | |
267 | { | |
268 | m_printDialogData = dialog->GetPrintDialogData(); | |
269 | } | |
270 | ||
271 | dialog->Destroy(); | |
04a014a5 | 272 | |
7bcb11d3 | 273 | return (ret == wxID_OK); |
04a014a5 RR |
274 | #endif |
275 | ||
4e4528f6 | 276 | return false; |
c801d85f KB |
277 | } |
278 | ||
8826f46f VZ |
279 | // ---------------------------------------------------------------------------- |
280 | // Print preview | |
281 | // ---------------------------------------------------------------------------- | |
c801d85f | 282 | |
8826f46f VZ |
283 | void wxPostScriptPrintPreview::Init(wxPrintout * WXUNUSED(printout), |
284 | wxPrintout * WXUNUSED(printoutForPrinting)) | |
c801d85f | 285 | { |
7bcb11d3 JS |
286 | // Have to call it here since base constructor can't call it |
287 | DetermineScaling(); | |
c801d85f KB |
288 | } |
289 | ||
8826f46f VZ |
290 | wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout, |
291 | wxPrintout *printoutForPrinting, | |
292 | wxPrintDialogData *data) | |
293 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
294 | { | |
295 | Init(printout, printoutForPrinting); | |
296 | } | |
297 | ||
298 | wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout, | |
299 | wxPrintout *printoutForPrinting, | |
300 | wxPrintData *data) | |
301 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
302 | { | |
303 | Init(printout, printoutForPrinting); | |
304 | } | |
305 | ||
306 | wxPostScriptPrintPreview::~wxPostScriptPrintPreview() | |
c801d85f KB |
307 | { |
308 | } | |
309 | ||
310 | bool wxPostScriptPrintPreview::Print(bool interactive) | |
311 | { | |
7bcb11d3 | 312 | if (!m_printPrintout) |
ca65c044 | 313 | return false; |
7bcb11d3 JS |
314 | wxPostScriptPrinter printer(& m_printDialogData); |
315 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
c801d85f KB |
316 | } |
317 | ||
8826f46f | 318 | void wxPostScriptPrintPreview::DetermineScaling() |
c801d85f | 319 | { |
7bcb11d3 JS |
320 | wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId(); |
321 | if (paperType == wxPAPER_NONE) | |
322 | paperType = wxPAPER_NONE; | |
8826f46f | 323 | |
c801d85f KB |
324 | wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType); |
325 | if (!paper) | |
7bcb11d3 JS |
326 | paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4); |
327 | ||
c801d85f KB |
328 | if (paper) |
329 | { | |
904a68b6 RL |
330 | wxSize ScreenPixels = wxGetDisplaySize(); |
331 | wxSize ScreenMM = wxGetDisplaySizeMM(); | |
332 | ||
7a5e6267 JS |
333 | m_previewPrintout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()), |
334 | (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) ); | |
ca65c044 | 335 | m_previewPrintout->SetPPIPrinter(wxPostScriptDC::GetResolution(), wxPostScriptDC::GetResolution()); |
8826f46f | 336 | |
7bcb11d3 | 337 | wxSize sizeDevUnits(paper->GetSizeDeviceUnits()); |
b64de916 VS |
338 | sizeDevUnits.x = (wxCoord)((float)sizeDevUnits.x * wxPostScriptDC::GetResolution() / 72.0); |
339 | sizeDevUnits.y = (wxCoord)((float)sizeDevUnits.y * wxPostScriptDC::GetResolution() / 72.0); | |
7bcb11d3 JS |
340 | wxSize sizeTenthsMM(paper->GetSize()); |
341 | wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10); | |
342 | ||
343 | // If in landscape mode, we need to swap the width and height. | |
344 | if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE ) | |
345 | { | |
346 | m_pageWidth = sizeDevUnits.y; | |
347 | m_pageHeight = sizeDevUnits.x; | |
348 | m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x); | |
349 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
350 | } | |
351 | else | |
352 | { | |
353 | m_pageWidth = sizeDevUnits.x; | |
354 | m_pageHeight = sizeDevUnits.y; | |
355 | m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y); | |
356 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
357 | } | |
8826f46f | 358 | |
7bcb11d3 | 359 | // At 100%, the page should look about page-size on the screen. |
b64de916 | 360 | m_previewScale = (float)0.8 * 72.0 / (float)wxPostScriptDC::GetResolution(); |
c801d85f KB |
361 | } |
362 | } | |
363 | ||
ce4169a4 | 364 | #endif |