]>
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);
91 if (m_printDialogData
.GetMinPage() < 1)
92 m_printDialogData
.SetMinPage(1);
93 if (m_printDialogData
.GetMaxPage() < 1)
94 m_printDialogData
.SetMaxPage(9999);
96 // Create a suitable device context
100 dc
= PrintDialog(parent
);
106 dc
= new wxPostScriptDC(GetPrintDialogData().GetPrintData());
109 // May have pressed cancel.
110 if (!dc
|| !dc
->Ok())
113 sm_lastError
= wxPRINTER_ERROR
;
117 wxSize ScreenPixels
= wxGetDisplaySize();
118 wxSize ScreenMM
= wxGetDisplaySizeMM();
120 printout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
121 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
122 printout
->SetPPIPrinter( wxPostScriptDC::GetResolution(),
123 wxPostScriptDC::GetResolution() );
125 // Set printout parameters
130 printout
->SetPageSizePixels((int)w
, (int)h
);
131 dc
->GetSizeMM(&w
, &h
);
132 printout
->SetPageSizeMM((int)w
, (int)h
);
134 // Create an abort window
137 printout
->OnPreparePrinting();
139 // Get some parameters from the printout, if defined
140 int fromPage
, toPage
;
141 int minPage
, maxPage
;
142 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
146 sm_lastError
= wxPRINTER_ERROR
;
151 // Only set min and max, because from and to have been
153 m_printDialogData
.SetMinPage(minPage
);
154 m_printDialogData
.SetMaxPage(maxPage
);
156 if (m_printDialogData
.GetFromPage() < minPage
)
157 m_printDialogData
.SetFromPage( minPage
);
158 if (m_printDialogData
.GetToPage() > maxPage
)
159 m_printDialogData
.SetToPage( maxPage
);
162 pagesPerCopy
= m_printDialogData
.GetToPage()-m_printDialogData
.GetFromPage()+1,
163 totalPages
= pagesPerCopy
* m_printDialogData
.GetNoCopies(),
165 // Open the progress bar dialog
166 wxProgressDialog
*progressDialog
= new wxProgressDialog (
167 printout
->GetTitle(),
171 wxPD_CAN_ABORT
|wxPD_AUTO_HIDE
|wxPD_APP_MODAL
);
173 printout
->OnBeginPrinting();
175 sm_lastError
= wxPRINTER_NO_ERROR
;
177 bool keepGoing
= true;
180 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
182 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
185 wxLogError(_("Could not start printing."));
186 sm_lastError
= wxPRINTER_ERROR
;
191 sm_lastError
= wxPRINTER_CANCELLED
;
196 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
202 sm_lastError
= wxPRINTER_CANCELLED
;
208 msg
.Printf(_("Printing page %d..."), printedPages
+1);
209 if(progressDialog
->Update(printedPages
++, msg
))
212 printout
->OnPrintPage(pn
);
218 sm_lastError
= wxPRINTER_CANCELLED
;
224 printout
->OnEndDocument();
227 printout
->OnEndPrinting();
228 delete progressDialog
;
234 return (sm_lastError
== wxPRINTER_NO_ERROR
);
237 wxDC
* wxPostScriptPrinter::PrintDialog(wxWindow
*parent
)
239 wxDC
* dc
= (wxDC
*) NULL
;
241 wxGenericPrintDialog
dialog( parent
, &m_printDialogData
);
242 if (dialog
.ShowModal() == wxID_OK
)
244 dc
= dialog
.GetPrintDC();
245 m_printDialogData
= dialog
.GetPrintDialogData();
248 sm_lastError
= wxPRINTER_ERROR
;
250 sm_lastError
= wxPRINTER_NO_ERROR
;
253 sm_lastError
= wxPRINTER_CANCELLED
;
258 bool wxPostScriptPrinter::Setup(wxWindow
*parent
)
260 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
261 dialog
->GetPrintDialogData().SetSetupDialog(true);
263 int ret
= dialog
->ShowModal();
267 m_printDialogData
= dialog
->GetPrintDialogData();
272 return (ret
== wxID_OK
);
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 void wxPostScriptPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
280 wxPrintout
* WXUNUSED(printoutForPrinting
))
282 // Have to call it here since base constructor can't call it
286 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
287 wxPrintout
*printoutForPrinting
,
288 wxPrintDialogData
*data
)
289 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
291 Init(printout
, printoutForPrinting
);
294 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
295 wxPrintout
*printoutForPrinting
,
297 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
299 Init(printout
, printoutForPrinting
);
302 wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
306 bool wxPostScriptPrintPreview::Print(bool interactive
)
308 if (!m_printPrintout
)
310 wxPostScriptPrinter
printer(& m_printDialogData
);
311 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
314 void wxPostScriptPrintPreview::DetermineScaling()
316 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
317 if (paperType
== wxPAPER_NONE
)
318 paperType
= wxPAPER_NONE
;
320 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
322 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
326 wxSize ScreenPixels
= wxGetDisplaySize();
327 wxSize ScreenMM
= wxGetDisplaySizeMM();
329 m_previewPrintout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
330 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
331 m_previewPrintout
->SetPPIPrinter(wxPostScriptDC::GetResolution(), wxPostScriptDC::GetResolution());
333 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
334 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* wxPostScriptDC::GetResolution() / 72.0);
335 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* wxPostScriptDC::GetResolution() / 72.0);
336 wxSize
sizeTenthsMM(paper
->GetSize());
337 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
339 // If in landscape mode, we need to swap the width and height.
340 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
342 m_pageWidth
= sizeDevUnits
.y
;
343 m_pageHeight
= sizeDevUnits
.x
;
344 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
345 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
349 m_pageWidth
= sizeDevUnits
.x
;
350 m_pageHeight
= sizeDevUnits
.y
;
351 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
352 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
355 // At 100%, the page should look about page-size on the screen.
356 m_previewScale
= (float)0.8 * 72.0 / (float)wxPostScriptDC::GetResolution();