]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/printps.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Postscript print/preview framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "printps.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
39 #include "wx/msgdlg.h"
41 #include "wx/progdlg.h"
45 #include "wx/generic/printps.h"
46 #include "wx/dcprint.h"
47 #include "wx/printdlg.h"
48 #include "wx/generic/prntdlgg.h"
49 #include "wx/generic/progdlgg.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter
, wxPrinterBase
)
59 IMPLEMENT_CLASS(wxPostScriptPrintPreview
, wxPrintPreviewBase
)
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData
*data
)
74 wxPostScriptPrinter::~wxPostScriptPrinter()
78 bool wxPostScriptPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
81 sm_abortWindow
= (wxWindow
*) NULL
;
85 sm_lastError
= wxPRINTER_ERROR
;
89 printout
->SetIsPreview(false);
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.
96 // printout->OnPreparePrinting();
98 // Get some parameters from the printout, if defined
100 int minPage
, maxPage
;
101 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
105 sm_lastError
= wxPRINTER_ERROR
;
109 m_printDialogData
.SetMinPage(minPage
);
110 m_printDialogData
.SetMaxPage(maxPage
);
112 m_printDialogData
.SetFromPage(fromPage
);
114 m_printDialogData
.SetToPage(toPage
);
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());
129 m_printDialogData
.EnablePageNumbers(false);
132 if (m_printDialogData
.GetMinPage() < 1)
133 m_printDialogData
.SetMinPage(1);
134 if (m_printDialogData
.GetMaxPage() < 1)
135 m_printDialogData
.SetMaxPage(9999);
137 // Create a suitable device context
141 dc
= PrintDialog(parent
);
147 dc
= new wxPostScriptDC(GetPrintDialogData().GetPrintData());
150 // May have pressed cancel.
151 if (!dc
|| !dc
->Ok())
154 sm_lastError
= wxPRINTER_ERROR
;
158 wxSize ScreenPixels
= wxGetDisplaySize();
159 wxSize ScreenMM
= wxGetDisplaySizeMM();
161 printout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
162 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
163 printout
->SetPPIPrinter( wxPostScriptDC::GetResolution(),
164 wxPostScriptDC::GetResolution() );
166 // Set printout parameters
171 printout
->SetPageSizePixels((int)w
, (int)h
);
172 dc
->GetSizeMM(&w
, &h
);
173 printout
->SetPageSizeMM((int)w
, (int)h
);
175 // Create an abort window
178 printout
->OnPreparePrinting();
180 // Get some parameters from the printout, if defined
181 int fromPage
, toPage
;
182 int minPage
, maxPage
;
183 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
187 sm_lastError
= wxPRINTER_ERROR
;
192 // Only set min and max, because from and to have been
194 m_printDialogData
.SetMinPage(minPage
);
195 m_printDialogData
.SetMaxPage(maxPage
);
198 pagesPerCopy
= m_printDialogData
.GetToPage()-m_printDialogData
.GetFromPage()+1,
199 totalPages
= pagesPerCopy
* m_printDialogData
.GetNoCopies(),
201 // Open the progress bar dialog
202 wxProgressDialog
*progressDialog
= new wxProgressDialog (
203 printout
->GetTitle(),
207 wxPD_CAN_ABORT
|wxPD_AUTO_HIDE
|wxPD_APP_MODAL
);
209 printout
->OnBeginPrinting();
211 sm_lastError
= wxPRINTER_NO_ERROR
;
213 bool keepGoing
= true;
216 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
218 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
221 wxLogError(_("Could not start printing."));
222 sm_lastError
= wxPRINTER_ERROR
;
227 sm_lastError
= wxPRINTER_CANCELLED
;
232 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
238 sm_lastError
= wxPRINTER_CANCELLED
;
244 msg
.Printf(_("Printing page %d..."), printedPages
+1);
245 if(progressDialog
->Update(printedPages
++, msg
))
248 printout
->OnPrintPage(pn
);
254 sm_lastError
= wxPRINTER_CANCELLED
;
260 printout
->OnEndDocument();
263 printout
->OnEndPrinting();
264 delete progressDialog
;
270 return (sm_lastError
== wxPRINTER_NO_ERROR
);
273 wxDC
* wxPostScriptPrinter::PrintDialog(wxWindow
*parent
)
275 wxDC
* dc
= (wxDC
*) NULL
;
276 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
277 int ret
= dialog
->ShowModal() ;
280 dc
= dialog
->GetPrintDC();
281 m_printDialogData
= dialog
->GetPrintDialogData();
283 sm_lastError
= wxPRINTER_ERROR
;
285 sm_lastError
= wxPRINTER_NO_ERROR
;
288 sm_lastError
= wxPRINTER_CANCELLED
;
295 bool wxPostScriptPrinter::Setup(wxWindow
*parent
)
297 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
298 dialog
->GetPrintDialogData().SetSetupDialog(true);
300 int ret
= dialog
->ShowModal();
304 m_printDialogData
= dialog
->GetPrintDialogData();
309 return (ret
== wxID_OK
);
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 void wxPostScriptPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
317 wxPrintout
* WXUNUSED(printoutForPrinting
))
319 // Have to call it here since base constructor can't call it
323 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
324 wxPrintout
*printoutForPrinting
,
325 wxPrintDialogData
*data
)
326 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
328 Init(printout
, printoutForPrinting
);
331 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
332 wxPrintout
*printoutForPrinting
,
334 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
336 Init(printout
, printoutForPrinting
);
339 wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
343 bool wxPostScriptPrintPreview::Print(bool interactive
)
345 if (!m_printPrintout
)
347 wxPostScriptPrinter
printer(& m_printDialogData
);
348 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
351 void wxPostScriptPrintPreview::DetermineScaling()
353 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
354 if (paperType
== wxPAPER_NONE
)
355 paperType
= wxPAPER_NONE
;
357 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
359 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
363 wxSize ScreenPixels
= wxGetDisplaySize();
364 wxSize ScreenMM
= wxGetDisplaySizeMM();
366 m_previewPrintout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
367 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
368 m_previewPrintout
->SetPPIPrinter(wxPostScriptDC::GetResolution(), wxPostScriptDC::GetResolution());
370 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
371 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* wxPostScriptDC::GetResolution() / 72.0);
372 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* wxPostScriptDC::GetResolution() / 72.0);
373 wxSize
sizeTenthsMM(paper
->GetSize());
374 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
376 // If in landscape mode, we need to swap the width and height.
377 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
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
);
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
);
392 // At 100%, the page should look about page-size on the screen.
393 m_previewScale
= (float)0.8 * 72.0 / (float)wxPostScriptDC::GetResolution();