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