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