]> git.saurik.com Git - wxWidgets.git/blame - src/generic/printps.cpp
removed (wrong?) assert
[wxWidgets.git] / src / generic / printps.cpp
CommitLineData
c801d85f
KB
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
8826f46f 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
8826f46f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
8826f46f 21 #pragma implementation "printps.h"
c801d85f
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
8826f46f 28 #pragma hdrstop
c801d85f
KB
29#endif
30
31#include "wx/defs.h"
32
33#ifndef WX_PRECOMP
8826f46f
VZ
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>
c801d85f
KB
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"
7bcb11d3 45#include "wx/paper.h"
c801d85f
KB
46
47#include <stdlib.h>
48
8826f46f
VZ
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
c801d85f 53#if !USE_SHARED_LIBRARY
8826f46f
VZ
54 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase)
55 IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase)
c801d85f
KB
56#endif
57
8826f46f
VZ
58// ============================================================================
59// implementation
60// ============================================================================
7bcb11d3 61
8826f46f
VZ
62// ----------------------------------------------------------------------------
63// Printer
64// ----------------------------------------------------------------------------
65
66wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData *data)
67 : wxPrinterBase(data)
c801d85f
KB
68{
69}
70
8826f46f 71wxPostScriptPrinter::~wxPostScriptPrinter()
c801d85f
KB
72{
73}
74
75bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
76{
7bcb11d3
JS
77 sm_abortIt = FALSE;
78 sm_abortWindow = (wxWindow *) NULL;
8826f46f 79
7bcb11d3
JS
80 if (!printout)
81 return FALSE;
8826f46f 82
7bcb11d3
JS
83 printout->SetIsPreview(FALSE);
84 printout->OnPreparePrinting();
8826f46f 85
7bcb11d3
JS
86 // Get some parameters from the printout, if defined
87 int fromPage, toPage;
88 int minPage, maxPage;
89 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
8826f46f 90
7bcb11d3
JS
91 if (maxPage == 0)
92 return FALSE;
8826f46f 93
7bcb11d3
JS
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)
c801d85f 102 {
7bcb11d3
JS
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());
c801d85f 112 }
7bcb11d3
JS
113 else
114 m_printDialogData.EnablePageNumbers(FALSE);
c801d85f 115
8826f46f 116 // Create a suitable device context
7bcb11d3
JS
117 wxDC *dc = (wxDC *) NULL;
118 if (prompt)
c801d85f 119 {
7bcb11d3
JS
120 dc = PrintDialog(parent);
121 if (!dc)
122 return FALSE;
c801d85f 123 }
7bcb11d3
JS
124 else
125 {
126 dc = new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), FALSE, (wxWindow *) NULL);
127 }
8826f46f 128
7bcb11d3
JS
129 // May have pressed cancel.
130 if (!dc || !dc->Ok())
131 {
132 if (dc) delete dc;
133 return FALSE;
134 }
8826f46f 135
7bcb11d3
JS
136 int logPPIScreenX = 0;
137 int logPPIScreenY = 0;
138 int logPPIPrinterX = 0;
139 int logPPIPrinterY = 0;
8826f46f 140
7bcb11d3
JS
141 logPPIScreenX = 100;
142 logPPIScreenY = 100;
8826f46f 143
7bcb11d3
JS
144 /*
145 // Correct values for X/PostScript?
146 logPPIPrinterX = 100;
147 logPPIPrinterY = 100;
148 */
8826f46f 149
7bcb11d3
JS
150 logPPIPrinterX = 72;
151 logPPIPrinterY = 72;
8826f46f 152
7bcb11d3
JS
153 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
154 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
8826f46f
VZ
155
156 // Set printout parameters
7bcb11d3 157 printout->SetDC(dc);
8826f46f 158
7bcb11d3
JS
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);
8826f46f 164
7bcb11d3
JS
165 // Create an abort window
166 wxBeginBusyCursor();
8826f46f 167
7bcb11d3 168 printout->OnBeginPrinting();
8826f46f 169
7bcb11d3 170 bool keepGoing = TRUE;
8826f46f 171
7bcb11d3
JS
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;
8826f46f 183
7bcb11d3
JS
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 }
8826f46f 202
7bcb11d3 203 printout->OnEndPrinting();
8826f46f 204
7bcb11d3 205 wxEndBusyCursor();
8826f46f 206
7bcb11d3 207 delete dc;
8826f46f 208
7bcb11d3 209 return TRUE;
c801d85f
KB
210}
211
7bcb11d3 212wxDC* wxPostScriptPrinter::PrintDialog(wxWindow *parent)
c801d85f 213{
7bcb11d3
JS
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;
c801d85f
KB
225}
226
227bool wxPostScriptPrinter::Setup(wxWindow *parent)
228{
7bcb11d3
JS
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);
c801d85f
KB
242}
243
8826f46f
VZ
244// ----------------------------------------------------------------------------
245// Print preview
246// ----------------------------------------------------------------------------
c801d85f 247
8826f46f
VZ
248void wxPostScriptPrintPreview::Init(wxPrintout * WXUNUSED(printout),
249 wxPrintout * WXUNUSED(printoutForPrinting))
c801d85f 250{
7bcb11d3
JS
251 // Have to call it here since base constructor can't call it
252 DetermineScaling();
c801d85f
KB
253}
254
8826f46f
VZ
255wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
256 wxPrintout *printoutForPrinting,
257 wxPrintDialogData *data)
258 : wxPrintPreviewBase(printout, printoutForPrinting, data)
259{
260 Init(printout, printoutForPrinting);
261}
262
263wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
264 wxPrintout *printoutForPrinting,
265 wxPrintData *data)
266 : wxPrintPreviewBase(printout, printoutForPrinting, data)
267{
268 Init(printout, printoutForPrinting);
269}
270
271wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
c801d85f
KB
272{
273}
274
275bool wxPostScriptPrintPreview::Print(bool interactive)
276{
7bcb11d3
JS
277 if (!m_printPrintout)
278 return FALSE;
279 wxPostScriptPrinter printer(& m_printDialogData);
280 return printer.Print(m_previewFrame, m_printPrintout, interactive);
c801d85f
KB
281}
282
8826f46f 283void wxPostScriptPrintPreview::DetermineScaling()
c801d85f 284{
7bcb11d3
JS
285 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
286 if (paperType == wxPAPER_NONE)
287 paperType = wxPAPER_NONE;
8826f46f 288
c801d85f
KB
289 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
290 if (!paper)
7bcb11d3
JS
291 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
292
c801d85f
KB
293 if (paper)
294 {
7bcb11d3
JS
295 m_previewPrintout->SetPPIScreen(100, 100);
296 // m_previewPrintout->SetPPIPrinter(100, 100);
297 m_previewPrintout->SetPPIPrinter(72, 72);
8826f46f 298
7bcb11d3
JS
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 }
8826f46f 318
7bcb11d3
JS
319 // At 100%, the page should look about page-size on the screen.
320 m_previewScale = (float)0.8;
c801d85f
KB
321 }
322}
323