]>
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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "printps.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #if wxUSE_PRINTING_ARCHITECTURE
39 #include "wx/msgdlg.h"
41 #include "wx/progdlg.h"
44 #include "wx/generic/printps.h"
45 #include "wx/dcprint.h"
46 #include "wx/printdlg.h"
47 #include "wx/generic/prntdlgg.h"
48 #include "wx/generic/progdlgg.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter
, wxPrinterBase
)
58 IMPLEMENT_CLASS(wxPostScriptPrintPreview
, wxPrintPreviewBase
)
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData
*data
)
73 wxPostScriptPrinter::~wxPostScriptPrinter()
77 bool wxPostScriptPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
80 sm_abortWindow
= (wxWindow
*) NULL
;
84 sm_lastError
= wxPRINTER_ERROR
;
88 printout
->SetIsPreview(FALSE
);
90 // 4/9/99, JACS: this is a silly place to allow preparation, considering
91 // the DC and no parameters have been set in the printout object.
92 // Moved further down.
94 // printout->OnPreparePrinting();
96 // Get some parameters from the printout, if defined
99 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
103 sm_lastError
= wxPRINTER_ERROR
;
107 m_printDialogData
.SetMinPage(minPage
);
108 m_printDialogData
.SetMaxPage(maxPage
);
110 m_printDialogData
.SetFromPage(fromPage
);
112 m_printDialogData
.SetToPage(toPage
);
116 m_printDialogData
.EnablePageNumbers(TRUE
);
117 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
118 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
119 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
120 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
121 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
122 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
123 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
124 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
127 m_printDialogData
.EnablePageNumbers(FALSE
);
130 // Create a suitable device context
131 wxDC
*dc
= (wxDC
*) NULL
;
134 dc
= PrintDialog(parent
);
140 dc
= new wxPostScriptDC(GetPrintDialogData().GetPrintData());
143 // May have pressed cancel.
144 if (!dc
|| !dc
->Ok())
147 sm_lastError
= wxPRINTER_ERROR
;
151 wxSize ScreenPixels
= wxGetDisplaySize();
152 wxSize ScreenMM
= wxGetDisplaySizeMM();
154 printout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
155 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
156 printout
->SetPPIPrinter( wxPostScriptDC::GetResolution(),
157 wxPostScriptDC::GetResolution() );
159 // Set printout parameters
164 printout
->SetPageSizePixels((int)w
, (int)h
);
165 dc
->GetSizeMM(&w
, &h
);
166 printout
->SetPageSizeMM((int)w
, (int)h
);
168 // Create an abort window
171 printout
->OnPreparePrinting();
174 pagesPerCopy
= m_printDialogData
.GetToPage()-m_printDialogData
.GetFromPage()+1,
175 totalPages
= pagesPerCopy
* m_printDialogData
.GetNoCopies(),
177 // Open the progress bar dialog
178 wxProgressDialog
*progressDialog
= new wxProgressDialog (
179 printout
->GetTitle(),
183 wxPD_CAN_ABORT
|wxPD_AUTO_HIDE
|wxPD_APP_MODAL
);
185 printout
->OnBeginPrinting();
187 sm_lastError
= wxPRINTER_NO_ERROR
;
189 bool keepGoing
= TRUE
;
192 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
194 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
197 wxLogError(_("Could not start printing."));
198 sm_lastError
= wxPRINTER_ERROR
;
203 sm_lastError
= wxPRINTER_CANCELLED
;
208 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
214 sm_lastError
= wxPRINTER_CANCELLED
;
220 msg
.Printf(_("Printing page %d..."), printedPages
+1);
221 if(progressDialog
->Update(printedPages
++, msg
))
224 printout
->OnPrintPage(pn
);
230 sm_lastError
= wxPRINTER_CANCELLED
;
236 printout
->OnEndDocument();
239 printout
->OnEndPrinting();
240 delete progressDialog
;
246 return (sm_lastError
== wxPRINTER_NO_ERROR
);
249 wxDC
* wxPostScriptPrinter::PrintDialog(wxWindow
*parent
)
251 wxDC
* dc
= (wxDC
*) NULL
;
252 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
253 int ret
= dialog
->ShowModal() ;
256 dc
= dialog
->GetPrintDC();
257 m_printDialogData
= dialog
->GetPrintDialogData();
259 sm_lastError
= wxPRINTER_ERROR
;
261 sm_lastError
= wxPRINTER_NO_ERROR
;
264 sm_lastError
= wxPRINTER_CANCELLED
;
271 bool wxPostScriptPrinter::Setup(wxWindow
*parent
)
273 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
274 dialog
->GetPrintDialogData().SetSetupDialog(TRUE
);
276 int ret
= dialog
->ShowModal();
280 m_printDialogData
= dialog
->GetPrintDialogData();
285 return (ret
== wxID_OK
);
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 void wxPostScriptPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
293 wxPrintout
* WXUNUSED(printoutForPrinting
))
295 // Have to call it here since base constructor can't call it
299 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
300 wxPrintout
*printoutForPrinting
,
301 wxPrintDialogData
*data
)
302 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
304 Init(printout
, printoutForPrinting
);
307 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
308 wxPrintout
*printoutForPrinting
,
310 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
312 Init(printout
, printoutForPrinting
);
315 wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
319 bool wxPostScriptPrintPreview::Print(bool interactive
)
321 if (!m_printPrintout
)
323 wxPostScriptPrinter
printer(& m_printDialogData
);
324 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
327 void wxPostScriptPrintPreview::DetermineScaling()
329 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
330 if (paperType
== wxPAPER_NONE
)
331 paperType
= wxPAPER_NONE
;
333 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
335 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
339 wxSize ScreenPixels
= wxGetDisplaySize();
340 wxSize ScreenMM
= wxGetDisplaySizeMM();
342 m_previewPrintout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
343 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
344 m_previewPrintout
->SetPPIPrinter(wxPostScriptDC::GetResolution(), wxPostScriptDC::GetResolution());
346 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
347 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* wxPostScriptDC::GetResolution() / 72.0);
348 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* wxPostScriptDC::GetResolution() / 72.0);
349 wxSize
sizeTenthsMM(paper
->GetSize());
350 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
352 // If in landscape mode, we need to swap the width and height.
353 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
355 m_pageWidth
= sizeDevUnits
.y
;
356 m_pageHeight
= sizeDevUnits
.x
;
357 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
358 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
362 m_pageWidth
= sizeDevUnits
.x
;
363 m_pageHeight
= sizeDevUnits
.y
;
364 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
365 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
368 // At 100%, the page should look about page-size on the screen.
369 m_previewScale
= (float)0.8 * 72.0 / (float)wxPostScriptDC::GetResolution();