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