]>
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 // ----------------------------------------------------------------------------
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
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 // 4/9/99, JACS: this is a silly place to allow preparation, considering
92 // the DC and no parameters have been set in the printout object.
93 // Moved further down.
95 // printout->OnPreparePrinting();
97 // Get some parameters from the printout, if defined
100 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
104 sm_lastError
= wxPRINTER_ERROR
;
108 m_printDialogData
.SetMinPage(minPage
);
109 m_printDialogData
.SetMaxPage(maxPage
);
111 m_printDialogData
.SetFromPage(fromPage
);
113 m_printDialogData
.SetToPage(toPage
);
117 m_printDialogData
.EnablePageNumbers(TRUE
);
118 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
119 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
120 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
121 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
122 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
123 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
124 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
125 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
128 m_printDialogData
.EnablePageNumbers(FALSE
);
131 // Create a suitable device context
132 wxDC
*dc
= (wxDC
*) NULL
;
135 dc
= PrintDialog(parent
);
141 dc
= new wxPostScriptDC(GetPrintDialogData().GetPrintData());
144 // May have pressed cancel.
145 if (!dc
|| !dc
->Ok())
148 sm_lastError
= wxPRINTER_ERROR
;
152 wxSize ScreenPixels
= wxGetDisplaySize();
153 wxSize ScreenMM
= wxGetDisplaySizeMM();
155 printout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
156 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
157 printout
->SetPPIPrinter( wxPostScriptDC::GetResolution(),
158 wxPostScriptDC::GetResolution() );
160 // Set printout parameters
165 printout
->SetPageSizePixels((int)w
, (int)h
);
166 dc
->GetSizeMM(&w
, &h
);
167 printout
->SetPageSizeMM((int)w
, (int)h
);
169 // Create an abort window
172 printout
->OnPreparePrinting();
175 pagesPerCopy
= m_printDialogData
.GetToPage()-m_printDialogData
.GetFromPage()+1,
176 totalPages
= pagesPerCopy
* m_printDialogData
.GetNoCopies(),
178 // Open the progress bar dialog
179 wxProgressDialog
*progressDialog
= new wxProgressDialog (
180 printout
->GetTitle(),
184 wxPD_CAN_ABORT
|wxPD_AUTO_HIDE
|wxPD_APP_MODAL
);
186 printout
->OnBeginPrinting();
188 sm_lastError
= wxPRINTER_NO_ERROR
;
190 bool keepGoing
= TRUE
;
193 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
195 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
198 wxLogError(_("Could not start printing."));
199 sm_lastError
= wxPRINTER_ERROR
;
204 sm_lastError
= wxPRINTER_CANCELLED
;
209 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
215 sm_lastError
= wxPRINTER_CANCELLED
;
221 msg
.Printf(_("Printing page %d..."), printedPages
+1);
222 if(progressDialog
->Update(printedPages
++, msg
))
225 printout
->OnPrintPage(pn
);
231 sm_lastError
= wxPRINTER_CANCELLED
;
237 printout
->OnEndDocument();
240 printout
->OnEndPrinting();
241 delete progressDialog
;
247 return (sm_lastError
== wxPRINTER_NO_ERROR
);
250 wxDC
* wxPostScriptPrinter::PrintDialog(wxWindow
*parent
)
252 wxDC
* dc
= (wxDC
*) NULL
;
253 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
254 int ret
= dialog
->ShowModal() ;
257 dc
= dialog
->GetPrintDC();
258 m_printDialogData
= dialog
->GetPrintDialogData();
260 sm_lastError
= wxPRINTER_ERROR
;
262 sm_lastError
= wxPRINTER_NO_ERROR
;
265 sm_lastError
= wxPRINTER_CANCELLED
;
272 bool wxPostScriptPrinter::Setup(wxWindow
*parent
)
274 wxGenericPrintDialog
* dialog
= new wxGenericPrintDialog(parent
, & m_printDialogData
);
275 dialog
->GetPrintDialogData().SetSetupDialog(TRUE
);
277 int ret
= dialog
->ShowModal();
281 m_printDialogData
= dialog
->GetPrintDialogData();
286 return (ret
== wxID_OK
);
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 void wxPostScriptPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
294 wxPrintout
* WXUNUSED(printoutForPrinting
))
296 // Have to call it here since base constructor can't call it
300 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
301 wxPrintout
*printoutForPrinting
,
302 wxPrintDialogData
*data
)
303 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
305 Init(printout
, printoutForPrinting
);
308 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout
*printout
,
309 wxPrintout
*printoutForPrinting
,
311 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
313 Init(printout
, printoutForPrinting
);
316 wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
320 bool wxPostScriptPrintPreview::Print(bool interactive
)
322 if (!m_printPrintout
)
324 wxPostScriptPrinter
printer(& m_printDialogData
);
325 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
328 void wxPostScriptPrintPreview::DetermineScaling()
330 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
331 if (paperType
== wxPAPER_NONE
)
332 paperType
= wxPAPER_NONE
;
334 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
336 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
340 wxSize ScreenPixels
= wxGetDisplaySize();
341 wxSize ScreenMM
= wxGetDisplaySizeMM();
343 m_previewPrintout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
344 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
345 m_previewPrintout
->SetPPIPrinter(wxPostScriptDC::GetResolution(), wxPostScriptDC::GetResolution());
347 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
348 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* wxPostScriptDC::GetResolution() / 72.0);
349 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* wxPostScriptDC::GetResolution() / 72.0);
350 wxSize
sizeTenthsMM(paper
->GetSize());
351 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
353 // If in landscape mode, we need to swap the width and height.
354 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
356 m_pageWidth
= sizeDevUnits
.y
;
357 m_pageHeight
= sizeDevUnits
.x
;
358 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
359 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
363 m_pageWidth
= sizeDevUnits
.x
;
364 m_pageHeight
= sizeDevUnits
.y
;
365 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
366 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
369 // At 100%, the page should look about page-size on the screen.
370 m_previewScale
= (float)0.8 * 72.0 / (float)wxPostScriptDC::GetResolution();