]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/printps.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/printps.cpp
3 // Purpose: Postscript print/preview framework
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
32 #include "wx/msgdlg.h"
34 #include "wx/progdlg.h"
36 #include "wx/dcprint.h"
39 #include "wx/generic/printps.h"
40 #include "wx/printdlg.h"
41 #include "wx/generic/prntdlgg.h"
42 #include "wx/progdlg.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter
, wxPrinterBase
)
52 IMPLEMENT_CLASS(wxPostScriptPrintPreview
, wxPrintPreviewBase
)
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData
*data
)
67 wxPostScriptPrinter::~wxPostScriptPrinter()
71 bool wxPostScriptPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
74 sm_abortWindow
= NULL
;
78 sm_lastError
= wxPRINTER_ERROR
;
82 if (m_printDialogData
.GetMinPage() < 1)
83 m_printDialogData
.SetMinPage(1);
84 if (m_printDialogData
.GetMaxPage() < 1)
85 m_printDialogData
.SetMaxPage(9999);
87 // Create a suitable device context
91 dc
= PrintDialog(parent
);
97 dc
= new wxPostScriptDC(GetPrintDialogData().GetPrintData());
100 // May have pressed cancel.
101 if (!dc
|| !dc
->IsOk())
104 sm_lastError
= wxPRINTER_ERROR
;
108 wxSize ScreenPixels
= wxGetDisplaySize();
109 wxSize ScreenMM
= wxGetDisplaySizeMM();
111 printout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
112 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
113 printout
->SetPPIPrinter( dc
->GetResolution(),
114 dc
->GetResolution() );
116 // Set printout parameters
121 printout
->SetPageSizePixels((int)w
, (int)h
);
122 printout
->SetPaperRectPixels(wxRect(0, 0, w
, h
));
124 dc
->GetSizeMM(&mw
, &mh
);
125 printout
->SetPageSizeMM((int)mw
, (int)mh
);
127 // Create an abort window
130 printout
->OnPreparePrinting();
132 // Get some parameters from the printout, if defined
133 int fromPage
, toPage
;
134 int minPage
, maxPage
;
135 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
139 sm_lastError
= wxPRINTER_ERROR
;
144 // Only set min and max, because from and to have been
146 m_printDialogData
.SetMinPage(minPage
);
147 m_printDialogData
.SetMaxPage(maxPage
);
149 if (m_printDialogData
.GetFromPage() < minPage
)
150 m_printDialogData
.SetFromPage( minPage
);
151 if (m_printDialogData
.GetToPage() > maxPage
)
152 m_printDialogData
.SetToPage( maxPage
);
155 pagesPerCopy
= m_printDialogData
.GetToPage()-m_printDialogData
.GetFromPage()+1,
156 totalPages
= pagesPerCopy
* m_printDialogData
.GetNoCopies(),
158 // Open the progress bar dialog
159 wxProgressDialog
*progressDialog
= new wxProgressDialog (
160 printout
->GetTitle(),
164 wxPD_CAN_ABORT
|wxPD_AUTO_HIDE
|wxPD_APP_MODAL
);
166 printout
->OnBeginPrinting();
168 sm_lastError
= wxPRINTER_NO_ERROR
;
170 bool keepGoing
= true;
173 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
175 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
178 wxLogError(_("Could not start printing."));
179 sm_lastError
= wxPRINTER_ERROR
;
184 sm_lastError
= wxPRINTER_CANCELLED
;
189 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
195 sm_lastError
= wxPRINTER_CANCELLED
;
201 msg
.Printf(_("Printing page %d..."), printedPages
+1);
202 if(progressDialog
->Update(printedPages
++, msg
))
205 printout
->OnPrintPage(pn
);
211 sm_lastError
= wxPRINTER_CANCELLED
;
217 printout
->OnEndDocument();
220 printout
->OnEndPrinting();
221 delete progressDialog
;
227 return (sm_lastError
== wxPRINTER_NO_ERROR
);
230 wxDC
* wxPostScriptPrinter::PrintDialog(wxWindow
*parent
)
234 wxGenericPrintDialog
dialog( parent
, &m_printDialogData
);
235 if (dialog
.ShowModal() == wxID_OK
)
237 dc
= dialog
.GetPrintDC();
238 m_printDialogData
= dialog
.GetPrintDialogData();
241 sm_lastError
= wxPRINTER_ERROR
;
243 sm_lastError
= wxPRINTER_NO_ERROR
;
246 sm_lastError
= wxPRINTER_CANCELLED
;
251 bool wxPostScriptPrinter::Setup(wxWindow
*WXUNUSED(parent
))
254 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
255 dialog
->GetPrintDialogData().SetSetupDialog(true);
257 int ret
= dialog
->ShowModal();
261 m_printDialogData
= dialog
->GetPrintDialogData();
266 return (ret
== wxID_OK
);
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 void wxPostScriptPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
277 wxPrintout
* WXUNUSED(printoutForPrinting
))
279 // Have to call it here since base constructor can't call it
283 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
284 wxPrintout
*printoutForPrinting
,
285 wxPrintDialogData
*data
)
286 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
288 Init(printout
, printoutForPrinting
);
291 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
292 wxPrintout
*printoutForPrinting
,
294 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
296 Init(printout
, printoutForPrinting
);
299 wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
303 bool wxPostScriptPrintPreview::Print(bool interactive
)
305 if (!m_printPrintout
)
308 // Assume that on Unix, the preview may use the PostScript
309 // (generic) version, but printing using the native system is required.
310 // TODO: make a generic print preview class from which wxPostScriptPrintPreview
313 wxPrinter
printer(& m_printDialogData
);
315 wxPostScriptPrinter
printer(& m_printDialogData
);
317 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
320 void wxPostScriptPrintPreview::DetermineScaling()
322 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
323 if (paperType
== wxPAPER_NONE
)
324 paperType
= wxPAPER_NONE
;
326 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
328 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
332 int resolution
= 600; // TODO, this is correct, but get this from wxPSDC somehow
334 const wxSize screenPPI
= wxGetDisplayPPI();
335 int logPPIScreenX
= screenPPI
.GetWidth();
336 int logPPIScreenY
= screenPPI
.GetHeight();
337 int logPPIPrinterX
= resolution
;
338 int logPPIPrinterY
= resolution
;
340 m_previewPrintout
->SetPPIScreen( logPPIScreenX
, logPPIScreenY
);
341 m_previewPrintout
->SetPPIPrinter( logPPIPrinterX
, logPPIPrinterY
);
343 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
344 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* resolution
/ 72.0);
345 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* resolution
/ 72.0);
346 wxSize
sizeTenthsMM(paper
->GetSize());
347 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
349 // If in landscape mode, we need to swap the width and height.
350 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
352 m_pageWidth
= sizeDevUnits
.y
;
353 m_pageHeight
= sizeDevUnits
.x
;
354 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
358 m_pageWidth
= sizeDevUnits
.x
;
359 m_pageHeight
= sizeDevUnits
.y
;
360 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
362 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
363 m_previewPrintout
->SetPaperRectPixels(wxRect(0, 0, m_pageWidth
, m_pageHeight
));
365 // At 100%, the page should look about page-size on the screen.
366 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
367 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;