]>
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
*WXUNUSED(parent
))
261 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
262 dialog
->GetPrintDialogData().SetSetupDialog(true);
264 int ret
= dialog
->ShowModal();
268 m_printDialogData
= dialog
->GetPrintDialogData();
273 return (ret
== wxID_OK
);
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 void wxPostScriptPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
284 wxPrintout
* WXUNUSED(printoutForPrinting
))
286 // Have to call it here since base constructor can't call it
290 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
291 wxPrintout
*printoutForPrinting
,
292 wxPrintDialogData
*data
)
293 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
295 Init(printout
, printoutForPrinting
);
298 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
299 wxPrintout
*printoutForPrinting
,
301 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
303 Init(printout
, printoutForPrinting
);
306 wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
310 bool wxPostScriptPrintPreview::Print(bool interactive
)
312 if (!m_printPrintout
)
314 wxPostScriptPrinter
printer(& m_printDialogData
);
315 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
318 void wxPostScriptPrintPreview::DetermineScaling()
320 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
321 if (paperType
== wxPAPER_NONE
)
322 paperType
= wxPAPER_NONE
;
324 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
326 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
330 wxSize ScreenPixels
= wxGetDisplaySize();
331 wxSize ScreenMM
= wxGetDisplaySizeMM();
333 m_previewPrintout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
334 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
335 m_previewPrintout
->SetPPIPrinter(wxPostScriptDC::GetResolution(), wxPostScriptDC::GetResolution());
337 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
338 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* wxPostScriptDC::GetResolution() / 72.0);
339 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* wxPostScriptDC::GetResolution() / 72.0);
340 wxSize
sizeTenthsMM(paper
->GetSize());
341 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
343 // If in landscape mode, we need to swap the width and height.
344 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
346 m_pageWidth
= sizeDevUnits
.y
;
347 m_pageHeight
= sizeDevUnits
.x
;
348 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
349 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
353 m_pageWidth
= sizeDevUnits
.x
;
354 m_pageHeight
= sizeDevUnits
.y
;
355 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
356 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
359 // At 100%, the page should look about page-size on the screen.
360 m_previewScale
= (float)0.8 * 72.0 / (float)wxPostScriptDC::GetResolution();