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