]>
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
35 #include "wx/msgdlg.h"
37 #include "wx/progdlg.h"
41 #include "wx/generic/printps.h"
42 #include "wx/dcprint.h"
43 #include "wx/printdlg.h"
44 #include "wx/generic/prntdlgg.h"
45 #include "wx/generic/progdlgg.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter
, wxPrinterBase
)
55 IMPLEMENT_CLASS(wxPostScriptPrintPreview
, wxPrintPreviewBase
)
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData
*data
)
70 wxPostScriptPrinter::~wxPostScriptPrinter()
74 bool wxPostScriptPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
77 sm_abortWindow
= (wxWindow
*) NULL
;
81 sm_lastError
= wxPRINTER_ERROR
;
85 printout
->SetIsPreview(false);
87 if (m_printDialogData
.GetMinPage() < 1)
88 m_printDialogData
.SetMinPage(1);
89 if (m_printDialogData
.GetMaxPage() < 1)
90 m_printDialogData
.SetMaxPage(9999);
92 // Create a suitable device context
96 dc
= PrintDialog(parent
);
102 dc
= new wxPostScriptDC(GetPrintDialogData().GetPrintData());
105 // May have pressed cancel.
106 if (!dc
|| !dc
->Ok())
109 sm_lastError
= wxPRINTER_ERROR
;
113 wxSize ScreenPixels
= wxGetDisplaySize();
114 wxSize ScreenMM
= wxGetDisplaySizeMM();
116 printout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
117 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
118 printout
->SetPPIPrinter( wxPostScriptDC::GetResolution(),
119 wxPostScriptDC::GetResolution() );
121 // Set printout parameters
126 printout
->SetPageSizePixels((int)w
, (int)h
);
127 dc
->GetSizeMM(&w
, &h
);
128 printout
->SetPageSizeMM((int)w
, (int)h
);
130 // Create an abort window
133 printout
->OnPreparePrinting();
135 // Get some parameters from the printout, if defined
136 int fromPage
, toPage
;
137 int minPage
, maxPage
;
138 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
142 sm_lastError
= wxPRINTER_ERROR
;
147 // Only set min and max, because from and to have been
149 m_printDialogData
.SetMinPage(minPage
);
150 m_printDialogData
.SetMaxPage(maxPage
);
152 if (m_printDialogData
.GetFromPage() < minPage
)
153 m_printDialogData
.SetFromPage( minPage
);
154 if (m_printDialogData
.GetToPage() > maxPage
)
155 m_printDialogData
.SetToPage( maxPage
);
158 pagesPerCopy
= m_printDialogData
.GetToPage()-m_printDialogData
.GetFromPage()+1,
159 totalPages
= pagesPerCopy
* m_printDialogData
.GetNoCopies(),
161 // Open the progress bar dialog
162 wxProgressDialog
*progressDialog
= new wxProgressDialog (
163 printout
->GetTitle(),
167 wxPD_CAN_ABORT
|wxPD_AUTO_HIDE
|wxPD_APP_MODAL
);
169 printout
->OnBeginPrinting();
171 sm_lastError
= wxPRINTER_NO_ERROR
;
173 bool keepGoing
= true;
176 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
178 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
181 wxLogError(_("Could not start printing."));
182 sm_lastError
= wxPRINTER_ERROR
;
187 sm_lastError
= wxPRINTER_CANCELLED
;
192 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
198 sm_lastError
= wxPRINTER_CANCELLED
;
204 msg
.Printf(_("Printing page %d..."), printedPages
+1);
205 if(progressDialog
->Update(printedPages
++, msg
))
208 printout
->OnPrintPage(pn
);
214 sm_lastError
= wxPRINTER_CANCELLED
;
220 printout
->OnEndDocument();
223 printout
->OnEndPrinting();
224 delete progressDialog
;
230 return (sm_lastError
== wxPRINTER_NO_ERROR
);
233 wxDC
* wxPostScriptPrinter::PrintDialog(wxWindow
*parent
)
235 wxDC
* dc
= (wxDC
*) NULL
;
237 wxGenericPrintDialog
dialog( parent
, &m_printDialogData
);
238 if (dialog
.ShowModal() == wxID_OK
)
240 dc
= dialog
.GetPrintDC();
241 m_printDialogData
= dialog
.GetPrintDialogData();
244 sm_lastError
= wxPRINTER_ERROR
;
246 sm_lastError
= wxPRINTER_NO_ERROR
;
249 sm_lastError
= wxPRINTER_CANCELLED
;
254 bool wxPostScriptPrinter::Setup(wxWindow
*WXUNUSED(parent
))
257 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
258 dialog
->GetPrintDialogData().SetSetupDialog(true);
260 int ret
= dialog
->ShowModal();
264 m_printDialogData
= dialog
->GetPrintDialogData();
269 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
)
311 // Assume that on Unix, the preview may use the PostScript
312 // (generic) version, but printing using the native system is required.
313 // TODO: make a generic print preview class from which wxPostScriptPrintPreview
316 wxPrinter
printer(& m_printDialogData
);
318 wxPostScriptPrinter
printer(& m_printDialogData
);
320 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
323 void wxPostScriptPrintPreview::DetermineScaling()
325 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
326 if (paperType
== wxPAPER_NONE
)
327 paperType
= wxPAPER_NONE
;
329 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
331 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
335 wxSize ScreenPixels
= wxGetDisplaySize();
336 wxSize ScreenMM
= wxGetDisplaySizeMM();
338 m_previewPrintout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
339 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
340 m_previewPrintout
->SetPPIPrinter(wxPostScriptDC::GetResolution(), wxPostScriptDC::GetResolution());
342 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
343 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* wxPostScriptDC::GetResolution() / 72.0);
344 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* wxPostScriptDC::GetResolution() / 72.0);
345 wxSize
sizeTenthsMM(paper
->GetSize());
346 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
348 // If in landscape mode, we need to swap the width and height.
349 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
351 m_pageWidth
= sizeDevUnits
.y
;
352 m_pageHeight
= sizeDevUnits
.x
;
353 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
354 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
358 m_pageWidth
= sizeDevUnits
.x
;
359 m_pageHeight
= sizeDevUnits
.y
;
360 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
361 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
364 // At 100%, the page should look about page-size on the screen.
365 m_previewScale
= (float)0.8 * 72.0 / (float)wxPostScriptDC::GetResolution();