]> git.saurik.com Git - wxWidgets.git/blame - src/generic/printps.cpp
Initialize the field count in Create(). It was using a garbage value
[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
ce4169a4
RR
33#if wxUSE_PRINTING_ARCHITECTURE
34
c801d85f 35#ifndef WX_PRECOMP
8826f46f
VZ
36 #include "wx/utils.h"
37 #include "wx/dc.h"
38 #include "wx/app.h"
39 #include "wx/msgdlg.h"
8fa2e6a2
KB
40 #include "wx/intl.h"
41 #include "wx/progdlg.h"
c801d85f
KB
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"
2179d579 48#include "wx/generic/progdlgg.h"
7bcb11d3 49#include "wx/paper.h"
c801d85f
KB
50
51#include <stdlib.h>
52
8826f46f
VZ
53// ----------------------------------------------------------------------------
54// wxWin macros
55// ----------------------------------------------------------------------------
56
8826f46f
VZ
57 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase)
58 IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase)
c801d85f 59
8826f46f
VZ
60// ============================================================================
61// implementation
62// ============================================================================
7bcb11d3 63
8826f46f
VZ
64// ----------------------------------------------------------------------------
65// Printer
66// ----------------------------------------------------------------------------
67
68wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData *data)
69 : wxPrinterBase(data)
c801d85f
KB
70{
71}
72
8826f46f 73wxPostScriptPrinter::~wxPostScriptPrinter()
c801d85f
KB
74{
75}
76
77bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
78{
7bcb11d3
JS
79 sm_abortIt = FALSE;
80 sm_abortWindow = (wxWindow *) NULL;
8826f46f 81
7bcb11d3
JS
82 if (!printout)
83 return FALSE;
8826f46f 84
7bcb11d3 85 printout->SetIsPreview(FALSE);
1044a386
JS
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();
8826f46f 92
7bcb11d3
JS
93 // Get some parameters from the printout, if defined
94 int fromPage, toPage;
95 int minPage, maxPage;
96 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
8826f46f 97
7bcb11d3
JS
98 if (maxPage == 0)
99 return FALSE;
8826f46f 100
7bcb11d3
JS
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)
c801d85f 109 {
7bcb11d3
JS
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());
c801d85f 119 }
7bcb11d3
JS
120 else
121 m_printDialogData.EnablePageNumbers(FALSE);
c801d85f 122
8fa2e6a2 123
8826f46f 124 // Create a suitable device context
7bcb11d3
JS
125 wxDC *dc = (wxDC *) NULL;
126 if (prompt)
c801d85f 127 {
7bcb11d3
JS
128 dc = PrintDialog(parent);
129 if (!dc)
130 return FALSE;
c801d85f 131 }
7bcb11d3
JS
132 else
133 {
134 dc = new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), FALSE, (wxWindow *) NULL);
135 }
8826f46f 136
7bcb11d3
JS
137 // May have pressed cancel.
138 if (!dc || !dc->Ok())
139 {
140 if (dc) delete dc;
141 return FALSE;
142 }
8826f46f 143
7bcb11d3
JS
144 int logPPIScreenX = 0;
145 int logPPIScreenY = 0;
146 int logPPIPrinterX = 0;
147 int logPPIPrinterY = 0;
8826f46f 148
7bcb11d3
JS
149 logPPIScreenX = 100;
150 logPPIScreenY = 100;
8826f46f 151
7bcb11d3
JS
152 /*
153 // Correct values for X/PostScript?
154 logPPIPrinterX = 100;
155 logPPIPrinterY = 100;
156 */
8826f46f 157
7bcb11d3
JS
158 logPPIPrinterX = 72;
159 logPPIPrinterY = 72;
8826f46f 160
7bcb11d3
JS
161 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
162 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
8826f46f
VZ
163
164 // Set printout parameters
7bcb11d3 165 printout->SetDC(dc);
8826f46f 166
7bcb11d3
JS
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);
8826f46f 172
7bcb11d3
JS
173 // Create an abort window
174 wxBeginBusyCursor();
8826f46f 175
1044a386
JS
176 printout->OnPreparePrinting();
177
67df9783 178 int
e0eeb9b3 179 pagesPerCopy = m_printDialogData.GetToPage()-m_printDialogData.GetFromPage()+1,
67df9783
KB
180 totalPages = pagesPerCopy * m_printDialogData.GetNoCopies(),
181 printedPages = 0;
8fa2e6a2
KB
182 // Open the progress bar dialog
183 wxProgressDialog *progressDialog = new wxProgressDialog (
184 printout->GetTitle(),
185 _("Printing..."),
67df9783 186 totalPages,
ffdaed2f
KB
187 parent,
188 wxPD_CAN_ABORT|wxPD_AUTO_HIDE|wxPD_APP_MODAL);
2179d579 189
7bcb11d3 190 printout->OnBeginPrinting();
8826f46f 191
7bcb11d3 192 bool keepGoing = TRUE;
8826f46f 193
7bcb11d3
JS
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;
8826f46f 205
7bcb11d3
JS
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 {
8fa2e6a2 217 wxString msg;
67df9783
KB
218 msg.Printf(_("Printing page %d..."), printedPages+1);
219 if(progressDialog->Update(printedPages++, msg))
8fa2e6a2
KB
220 {
221 dc->StartPage();
222 printout->OnPrintPage(pn);
223 dc->EndPage();
224 }
225 else
226 {
58c7cd12
RR
227 sm_abortIt = TRUE;
228 keepGoing = FALSE;
8fa2e6a2 229 }
7bcb11d3 230 }
4080c40a 231 wxYield();
7bcb11d3
JS
232 }
233 printout->OnEndDocument();
234 }
8826f46f 235
7bcb11d3 236 printout->OnEndPrinting();
67df9783 237 delete progressDialog;
8826f46f 238
7bcb11d3 239 wxEndBusyCursor();
8826f46f 240
7bcb11d3 241 delete dc;
8826f46f 242
7bcb11d3 243 return TRUE;
c801d85f
KB
244}
245
7bcb11d3 246wxDC* wxPostScriptPrinter::PrintDialog(wxWindow *parent)
c801d85f 247{
7bcb11d3
JS
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;
c801d85f
KB
259}
260
261bool wxPostScriptPrinter::Setup(wxWindow *parent)
262{
7bcb11d3
JS
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);
c801d85f
KB
276}
277
8826f46f
VZ
278// ----------------------------------------------------------------------------
279// Print preview
280// ----------------------------------------------------------------------------
c801d85f 281
8826f46f
VZ
282void wxPostScriptPrintPreview::Init(wxPrintout * WXUNUSED(printout),
283 wxPrintout * WXUNUSED(printoutForPrinting))
c801d85f 284{
7bcb11d3
JS
285 // Have to call it here since base constructor can't call it
286 DetermineScaling();
c801d85f
KB
287}
288
8826f46f
VZ
289wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
290 wxPrintout *printoutForPrinting,
291 wxPrintDialogData *data)
292 : wxPrintPreviewBase(printout, printoutForPrinting, data)
293{
294 Init(printout, printoutForPrinting);
295}
296
297wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
298 wxPrintout *printoutForPrinting,
299 wxPrintData *data)
300 : wxPrintPreviewBase(printout, printoutForPrinting, data)
301{
302 Init(printout, printoutForPrinting);
303}
304
305wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
c801d85f
KB
306{
307}
308
309bool wxPostScriptPrintPreview::Print(bool interactive)
310{
7bcb11d3
JS
311 if (!m_printPrintout)
312 return FALSE;
313 wxPostScriptPrinter printer(& m_printDialogData);
314 return printer.Print(m_previewFrame, m_printPrintout, interactive);
c801d85f
KB
315}
316
8826f46f 317void wxPostScriptPrintPreview::DetermineScaling()
c801d85f 318{
7bcb11d3
JS
319 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
320 if (paperType == wxPAPER_NONE)
321 paperType = wxPAPER_NONE;
8826f46f 322
c801d85f
KB
323 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
324 if (!paper)
7bcb11d3
JS
325 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
326
c801d85f
KB
327 if (paper)
328 {
7bcb11d3
JS
329 m_previewPrintout->SetPPIScreen(100, 100);
330 // m_previewPrintout->SetPPIPrinter(100, 100);
331 m_previewPrintout->SetPPIPrinter(72, 72);
8826f46f 332
7bcb11d3
JS
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 }
8826f46f 352
7bcb11d3
JS
353 // At 100%, the page should look about page-size on the screen.
354 m_previewScale = (float)0.8;
c801d85f
KB
355 }
356}
357
ce4169a4 358#endif