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