]>
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
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/generic/progdlgg.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
= (wxWindow
*) NULL
;
79 sm_lastError
= wxPRINTER_ERROR
;
83 printout
->SetIsPreview(false);
85 if (m_printDialogData
.GetMinPage() < 1)
86 m_printDialogData
.SetMinPage(1);
87 if (m_printDialogData
.GetMaxPage() < 1)
88 m_printDialogData
.SetMaxPage(9999);
90 // Create a suitable device context
94 dc
= PrintDialog(parent
);
100 dc
= new wxPostScriptDC(GetPrintDialogData().GetPrintData());
103 // May have pressed cancel.
104 if (!dc
|| !dc
->Ok())
107 sm_lastError
= wxPRINTER_ERROR
;
111 wxSize ScreenPixels
= wxGetDisplaySize();
112 wxSize ScreenMM
= wxGetDisplaySizeMM();
114 printout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
115 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
116 printout
->SetPPIPrinter( wxPostScriptDC::GetResolution(),
117 wxPostScriptDC::GetResolution() );
119 // Set printout parameters
124 printout
->SetPageSizePixels((int)w
, (int)h
);
125 dc
->GetSizeMM(&w
, &h
);
126 printout
->SetPageSizeMM((int)w
, (int)h
);
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
)
233 wxDC
* dc
= (wxDC
*) NULL
;
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 wxSize ScreenPixels
= wxGetDisplaySize();
334 wxSize ScreenMM
= wxGetDisplaySizeMM();
336 m_previewPrintout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
337 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
338 m_previewPrintout
->SetPPIPrinter(wxPostScriptDC::GetResolution(), wxPostScriptDC::GetResolution());
340 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
341 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* wxPostScriptDC::GetResolution() / 72.0);
342 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* wxPostScriptDC::GetResolution() / 72.0);
343 wxSize
sizeTenthsMM(paper
->GetSize());
344 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
346 // If in landscape mode, we need to swap the width and height.
347 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
349 m_pageWidth
= sizeDevUnits
.y
;
350 m_pageHeight
= sizeDevUnits
.x
;
351 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
352 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
356 m_pageWidth
= sizeDevUnits
.x
;
357 m_pageHeight
= sizeDevUnits
.y
;
358 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
359 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
362 // At 100%, the page should look about page-size on the screen.
363 m_previewScale
= (float)0.8 * 72.0 / (float)wxPostScriptDC::GetResolution();