*** empty log message ***
[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 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase)
59 IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase)
60 #endif
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // Printer
68 // ----------------------------------------------------------------------------
69
70 wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData *data)
71 : wxPrinterBase(data)
72 {
73 }
74
75 wxPostScriptPrinter::~wxPostScriptPrinter()
76 {
77 }
78
79 bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
80 {
81 sm_abortIt = FALSE;
82 sm_abortWindow = (wxWindow *) NULL;
83
84 if (!printout)
85 return FALSE;
86
87 printout->SetIsPreview(FALSE);
88 printout->OnPreparePrinting();
89
90 // Get some parameters from the printout, if defined
91 int fromPage, toPage;
92 int minPage, maxPage;
93 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
94
95 if (maxPage == 0)
96 return FALSE;
97
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)
106 {
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());
116 }
117 else
118 m_printDialogData.EnablePageNumbers(FALSE);
119
120
121 // Create a suitable device context
122 wxDC *dc = (wxDC *) NULL;
123 if (prompt)
124 {
125 dc = PrintDialog(parent);
126 if (!dc)
127 return FALSE;
128 }
129 else
130 {
131 dc = new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), FALSE, (wxWindow *) NULL);
132 }
133
134 // May have pressed cancel.
135 if (!dc || !dc->Ok())
136 {
137 if (dc) delete dc;
138 return FALSE;
139 }
140
141 int logPPIScreenX = 0;
142 int logPPIScreenY = 0;
143 int logPPIPrinterX = 0;
144 int logPPIPrinterY = 0;
145
146 logPPIScreenX = 100;
147 logPPIScreenY = 100;
148
149 /*
150 // Correct values for X/PostScript?
151 logPPIPrinterX = 100;
152 logPPIPrinterY = 100;
153 */
154
155 logPPIPrinterX = 72;
156 logPPIPrinterY = 72;
157
158 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
159 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
160
161 // Set printout parameters
162 printout->SetDC(dc);
163
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);
169
170 // Create an abort window
171 wxBeginBusyCursor();
172
173 int
174 pagesPerCopy = m_printDialogData.GetToPage()-m_printDialogData.GetFromPage()+1,
175 totalPages = pagesPerCopy * m_printDialogData.GetNoCopies(),
176 printedPages = 0;
177 // Open the progress bar dialog
178 wxProgressDialog *progressDialog = new wxProgressDialog (
179 printout->GetTitle(),
180 _("Printing..."),
181 totalPages,
182 parent,
183 wxPD_CAN_ABORT|wxPD_AUTO_HIDE|wxPD_APP_MODAL);
184
185 printout->OnBeginPrinting();
186
187 bool keepGoing = TRUE;
188
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;
200
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 {
212 wxString msg;
213 msg.Printf(_("Printing page %d..."), printedPages+1);
214 if(progressDialog->Update(printedPages++, msg))
215 {
216 dc->StartPage();
217 printout->OnPrintPage(pn);
218 dc->EndPage();
219 }
220 else
221 {
222 sm_abortIt = TRUE;
223 keepGoing = FALSE;
224 }
225 }
226 wxYield();
227 }
228 printout->OnEndDocument();
229 }
230
231 printout->OnEndPrinting();
232 delete progressDialog;
233
234 wxEndBusyCursor();
235
236 delete dc;
237
238 return TRUE;
239 }
240
241 wxDC* wxPostScriptPrinter::PrintDialog(wxWindow *parent)
242 {
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;
254 }
255
256 bool wxPostScriptPrinter::Setup(wxWindow *parent)
257 {
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);
271 }
272
273 // ----------------------------------------------------------------------------
274 // Print preview
275 // ----------------------------------------------------------------------------
276
277 void wxPostScriptPrintPreview::Init(wxPrintout * WXUNUSED(printout),
278 wxPrintout * WXUNUSED(printoutForPrinting))
279 {
280 // Have to call it here since base constructor can't call it
281 DetermineScaling();
282 }
283
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()
301 {
302 }
303
304 bool wxPostScriptPrintPreview::Print(bool interactive)
305 {
306 if (!m_printPrintout)
307 return FALSE;
308 wxPostScriptPrinter printer(& m_printDialogData);
309 return printer.Print(m_previewFrame, m_printPrintout, interactive);
310 }
311
312 void wxPostScriptPrintPreview::DetermineScaling()
313 {
314 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
315 if (paperType == wxPAPER_NONE)
316 paperType = wxPAPER_NONE;
317
318 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
319 if (!paper)
320 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
321
322 if (paper)
323 {
324 m_previewPrintout->SetPPIScreen(100, 100);
325 // m_previewPrintout->SetPPIPrinter(100, 100);
326 m_previewPrintout->SetPPIPrinter(72, 72);
327
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 }
347
348 // At 100%, the page should look about page-size on the screen.
349 m_previewScale = (float)0.8;
350 }
351 }
352
353 #endif