]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/printps.cpp
a264bdabc5633a6aae5a4eab1b1a02557d043658
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/printps.cpp
3 // Purpose: Postscript print/preview framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 // ============================================================================
21 // ============================================================================
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
33 #include "wx/msgdlg.h"
35 #include "wx/progdlg.h"
37 #include "wx/dcprint.h"
40 #include "wx/generic/printps.h"
41 #include "wx/printdlg.h"
42 #include "wx/generic/prntdlgg.h"
43 #include "wx/progdlg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter
, wxPrinterBase
)
53 IMPLEMENT_CLASS(wxPostScriptPrintPreview
, wxPrintPreviewBase
)
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData
*data
)
68 wxPostScriptPrinter::~wxPostScriptPrinter()
72 bool wxPostScriptPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
75 sm_abortWindow
= NULL
;
79 sm_lastError
= wxPRINTER_ERROR
;
83 if (m_printDialogData
.GetMinPage() < 1)
84 m_printDialogData
.SetMinPage(1);
85 if (m_printDialogData
.GetMaxPage() < 1)
86 m_printDialogData
.SetMaxPage(9999);
88 // Create a suitable device context
92 dc
= PrintDialog(parent
);
98 dc
= new wxPostScriptDC(GetPrintDialogData().GetPrintData());
101 // May have pressed cancel.
102 if (!dc
|| !dc
->IsOk())
105 sm_lastError
= wxPRINTER_ERROR
;
109 wxSize ScreenPixels
= wxGetDisplaySize();
110 wxSize ScreenMM
= wxGetDisplaySizeMM();
112 printout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
113 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
114 printout
->SetPPIPrinter( dc
->GetResolution(),
115 dc
->GetResolution() );
117 // Set printout parameters
122 printout
->SetPageSizePixels((int)w
, (int)h
);
123 printout
->SetPaperRectPixels(wxRect(0, 0, w
, h
));
125 dc
->GetSizeMM(&mw
, &mh
);
126 printout
->SetPageSizeMM((int)mw
, (int)mh
);
128 // Create an abort window
131 printout
->OnPreparePrinting();
133 // Get some parameters from the printout, if defined
134 int fromPage
, toPage
;
135 int minPage
, maxPage
;
136 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
140 sm_lastError
= wxPRINTER_ERROR
;
145 // Only set min and max, because from and to have been
147 m_printDialogData
.SetMinPage(minPage
);
148 m_printDialogData
.SetMaxPage(maxPage
);
150 if (m_printDialogData
.GetFromPage() < minPage
)
151 m_printDialogData
.SetFromPage( minPage
);
152 if (m_printDialogData
.GetToPage() > maxPage
)
153 m_printDialogData
.SetToPage( maxPage
);
156 pagesPerCopy
= m_printDialogData
.GetToPage()-m_printDialogData
.GetFromPage()+1,
157 totalPages
= pagesPerCopy
* m_printDialogData
.GetNoCopies(),
159 // Open the progress bar dialog
160 wxProgressDialog
*progressDialog
= new wxProgressDialog (
161 printout
->GetTitle(),
165 wxPD_CAN_ABORT
|wxPD_AUTO_HIDE
|wxPD_APP_MODAL
);
167 printout
->OnBeginPrinting();
169 sm_lastError
= wxPRINTER_NO_ERROR
;
171 bool keepGoing
= true;
174 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
176 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
179 wxLogError(_("Could not start printing."));
180 sm_lastError
= wxPRINTER_ERROR
;
185 sm_lastError
= wxPRINTER_CANCELLED
;
190 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
196 sm_lastError
= wxPRINTER_CANCELLED
;
202 msg
.Printf(_("Printing page %d..."), printedPages
+1);
203 if(progressDialog
->Update(printedPages
++, msg
))
206 printout
->OnPrintPage(pn
);
212 sm_lastError
= wxPRINTER_CANCELLED
;
218 printout
->OnEndDocument();
221 printout
->OnEndPrinting();
222 delete progressDialog
;
228 return (sm_lastError
== wxPRINTER_NO_ERROR
);
231 wxDC
* wxPostScriptPrinter::PrintDialog(wxWindow
*parent
)
235 wxGenericPrintDialog
dialog( parent
, &m_printDialogData
);
236 if (dialog
.ShowModal() == wxID_OK
)
238 dc
= dialog
.GetPrintDC();
239 m_printDialogData
= dialog
.GetPrintDialogData();
242 sm_lastError
= wxPRINTER_ERROR
;
244 sm_lastError
= wxPRINTER_NO_ERROR
;
247 sm_lastError
= wxPRINTER_CANCELLED
;
252 bool wxPostScriptPrinter::Setup(wxWindow
*WXUNUSED(parent
))
255 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
256 dialog
->GetPrintDialogData().SetSetupDialog(true);
258 int ret
= dialog
->ShowModal();
262 m_printDialogData
= dialog
->GetPrintDialogData();
267 return (ret
== wxID_OK
);
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 void wxPostScriptPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
278 wxPrintout
* WXUNUSED(printoutForPrinting
))
280 // Have to call it here since base constructor can't call it
284 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
285 wxPrintout
*printoutForPrinting
,
286 wxPrintDialogData
*data
)
287 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
289 Init(printout
, printoutForPrinting
);
292 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
293 wxPrintout
*printoutForPrinting
,
295 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
297 Init(printout
, printoutForPrinting
);
300 wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
304 bool wxPostScriptPrintPreview::Print(bool interactive
)
306 if (!m_printPrintout
)
309 // Assume that on Unix, the preview may use the PostScript
310 // (generic) version, but printing using the native system is required.
311 // TODO: make a generic print preview class from which wxPostScriptPrintPreview
314 wxPrinter
printer(& m_printDialogData
);
316 wxPostScriptPrinter
printer(& m_printDialogData
);
318 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
321 void wxPostScriptPrintPreview::DetermineScaling()
323 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
324 if (paperType
== wxPAPER_NONE
)
325 paperType
= wxPAPER_NONE
;
327 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
329 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
333 int resolution
= 600; // TODO, this is correct, but get this from wxPSDC somehow
335 const wxSize screenPPI
= wxGetDisplayPPI();
336 int logPPIScreenX
= screenPPI
.GetWidth();
337 int logPPIScreenY
= screenPPI
.GetHeight();
338 int logPPIPrinterX
= resolution
;
339 int logPPIPrinterY
= resolution
;
341 m_previewPrintout
->SetPPIScreen( logPPIScreenX
, logPPIScreenY
);
342 m_previewPrintout
->SetPPIPrinter( logPPIPrinterX
, logPPIPrinterY
);
344 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
345 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* resolution
/ 72.0);
346 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* resolution
/ 72.0);
347 wxSize
sizeTenthsMM(paper
->GetSize());
348 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
350 // If in landscape mode, we need to swap the width and height.
351 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
353 m_pageWidth
= sizeDevUnits
.y
;
354 m_pageHeight
= sizeDevUnits
.x
;
355 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
359 m_pageWidth
= sizeDevUnits
.x
;
360 m_pageHeight
= sizeDevUnits
.y
;
361 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
363 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
364 m_previewPrintout
->SetPaperRectPixels(wxRect(0, 0, m_pageWidth
, m_pageHeight
));
366 // At 100%, the page should look about page-size on the screen.
367 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
368 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;