more backwards compatibility
[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 #ifndef WX_PRECOMP
34 #include "wx/utils.h"
35 #include "wx/dc.h"
36 #include "wx/app.h"
37 #include "wx/msgdlg.h"
38 #include <wx/intl.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/paper.h"
46
47 #include <stdlib.h>
48
49 // ----------------------------------------------------------------------------
50 // wxWin macros
51 // ----------------------------------------------------------------------------
52
53 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase)
55 IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase)
56 #endif
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // Printer
64 // ----------------------------------------------------------------------------
65
66 wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData *data)
67 : wxPrinterBase(data)
68 {
69 }
70
71 wxPostScriptPrinter::~wxPostScriptPrinter()
72 {
73 }
74
75 bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
76 {
77 sm_abortIt = FALSE;
78 sm_abortWindow = (wxWindow *) NULL;
79
80 if (!printout)
81 return FALSE;
82
83 printout->SetIsPreview(FALSE);
84 printout->OnPreparePrinting();
85
86 // Get some parameters from the printout, if defined
87 int fromPage, toPage;
88 int minPage, maxPage;
89 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
90
91 if (maxPage == 0)
92 return FALSE;
93
94 m_printDialogData.SetMinPage(minPage);
95 m_printDialogData.SetMaxPage(maxPage);
96 if (fromPage != 0)
97 m_printDialogData.SetFromPage(fromPage);
98 if (toPage != 0)
99 m_printDialogData.SetToPage(toPage);
100
101 if (minPage != 0)
102 {
103 m_printDialogData.EnablePageNumbers(TRUE);
104 if (m_printDialogData.GetFromPage() < m_printDialogData.GetMinPage())
105 m_printDialogData.SetFromPage(m_printDialogData.GetMinPage());
106 else if (m_printDialogData.GetFromPage() > m_printDialogData.GetMaxPage())
107 m_printDialogData.SetFromPage(m_printDialogData.GetMaxPage());
108 if (m_printDialogData.GetToPage() > m_printDialogData.GetMaxPage())
109 m_printDialogData.SetToPage(m_printDialogData.GetMaxPage());
110 else if (m_printDialogData.GetToPage() < m_printDialogData.GetMinPage())
111 m_printDialogData.SetToPage(m_printDialogData.GetMinPage());
112 }
113 else
114 m_printDialogData.EnablePageNumbers(FALSE);
115
116 // Create a suitable device context
117 wxDC *dc = (wxDC *) NULL;
118 if (prompt)
119 {
120 dc = PrintDialog(parent);
121 if (!dc)
122 return FALSE;
123 }
124 else
125 {
126 dc = new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), FALSE, (wxWindow *) NULL);
127 }
128
129 // May have pressed cancel.
130 if (!dc || !dc->Ok())
131 {
132 if (dc) delete dc;
133 return FALSE;
134 }
135
136 int logPPIScreenX = 0;
137 int logPPIScreenY = 0;
138 int logPPIPrinterX = 0;
139 int logPPIPrinterY = 0;
140
141 logPPIScreenX = 100;
142 logPPIScreenY = 100;
143
144 /*
145 // Correct values for X/PostScript?
146 logPPIPrinterX = 100;
147 logPPIPrinterY = 100;
148 */
149
150 logPPIPrinterX = 72;
151 logPPIPrinterY = 72;
152
153 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
154 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
155
156 // Set printout parameters
157 printout->SetDC(dc);
158
159 int w, h;
160 dc->GetSize(&w, &h);
161 printout->SetPageSizePixels((int)w, (int)h);
162 dc->GetSizeMM(&w, &h);
163 printout->SetPageSizeMM((int)w, (int)h);
164
165 // Create an abort window
166 wxBeginBusyCursor();
167
168 printout->OnBeginPrinting();
169
170 bool keepGoing = TRUE;
171
172 int copyCount;
173 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
174 {
175 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
176 {
177 wxEndBusyCursor();
178 wxMessageBox(_("Could not start printing."), _("Print Error"), wxOK, parent);
179 break;
180 }
181 if (sm_abortIt)
182 break;
183
184 int pn;
185 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
186 pn++)
187 {
188 if (sm_abortIt)
189 {
190 keepGoing = FALSE;
191 break;
192 }
193 else
194 {
195 dc->StartPage();
196 printout->OnPrintPage(pn);
197 dc->EndPage();
198 }
199 }
200 printout->OnEndDocument();
201 }
202
203 printout->OnEndPrinting();
204
205 wxEndBusyCursor();
206
207 delete dc;
208
209 return TRUE;
210 }
211
212 wxDC* wxPostScriptPrinter::PrintDialog(wxWindow *parent)
213 {
214 wxDC* dc = (wxDC*) NULL;
215 wxGenericPrintDialog* dialog = new wxGenericPrintDialog(parent, & m_printDialogData);
216 int ret = dialog->ShowModal() ;
217 if (ret == wxID_OK)
218 {
219 dc = dialog->GetPrintDC();
220 m_printDialogData = dialog->GetPrintDialogData();
221 }
222 dialog->Destroy();
223
224 return dc;
225 }
226
227 bool wxPostScriptPrinter::Setup(wxWindow *parent)
228 {
229 wxGenericPrintDialog* dialog = new wxGenericPrintDialog(parent, & m_printDialogData);
230 dialog->GetPrintDialogData().SetSetupDialog(TRUE);
231
232 int ret = dialog->ShowModal();
233
234 if (ret == wxID_OK)
235 {
236 m_printDialogData = dialog->GetPrintDialogData();
237 }
238
239 dialog->Destroy();
240
241 return (ret == wxID_OK);
242 }
243
244 // ----------------------------------------------------------------------------
245 // Print preview
246 // ----------------------------------------------------------------------------
247
248 void wxPostScriptPrintPreview::Init(wxPrintout * WXUNUSED(printout),
249 wxPrintout * WXUNUSED(printoutForPrinting))
250 {
251 // Have to call it here since base constructor can't call it
252 DetermineScaling();
253 }
254
255 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
256 wxPrintout *printoutForPrinting,
257 wxPrintDialogData *data)
258 : wxPrintPreviewBase(printout, printoutForPrinting, data)
259 {
260 Init(printout, printoutForPrinting);
261 }
262
263 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
264 wxPrintout *printoutForPrinting,
265 wxPrintData *data)
266 : wxPrintPreviewBase(printout, printoutForPrinting, data)
267 {
268 Init(printout, printoutForPrinting);
269 }
270
271 wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
272 {
273 }
274
275 bool wxPostScriptPrintPreview::Print(bool interactive)
276 {
277 if (!m_printPrintout)
278 return FALSE;
279 wxPostScriptPrinter printer(& m_printDialogData);
280 return printer.Print(m_previewFrame, m_printPrintout, interactive);
281 }
282
283 void wxPostScriptPrintPreview::DetermineScaling()
284 {
285 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
286 if (paperType == wxPAPER_NONE)
287 paperType = wxPAPER_NONE;
288
289 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
290 if (!paper)
291 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
292
293 if (paper)
294 {
295 m_previewPrintout->SetPPIScreen(100, 100);
296 // m_previewPrintout->SetPPIPrinter(100, 100);
297 m_previewPrintout->SetPPIPrinter(72, 72);
298
299 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
300 wxSize sizeTenthsMM(paper->GetSize());
301 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
302
303 // If in landscape mode, we need to swap the width and height.
304 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
305 {
306 m_pageWidth = sizeDevUnits.y;
307 m_pageHeight = sizeDevUnits.x;
308 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
309 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
310 }
311 else
312 {
313 m_pageWidth = sizeDevUnits.x;
314 m_pageHeight = sizeDevUnits.y;
315 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
316 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
317 }
318
319 // At 100%, the page should look about page-size on the screen.
320 m_previewScale = (float)0.8;
321 }
322 }
323