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