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