]>
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
->IsOk())
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( dc
->GetResolution(),
117 dc
->GetResolution() );
119 // Set printout parameters
124 printout
->SetPageSizePixels((int)w
, (int)h
);
125 printout
->SetPaperRectPixels(wxRect(0, 0, w
, h
));
127 dc
->GetSizeMM(&mw
, &mh
);
128 printout
->SetPageSizeMM((int)mw
, (int)mh
);
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 int resolution
= 600; // TODO, this is correct, but get this from wxPSDC somehow
340 m_previewPrintout
->SetPPIScreen( (int) ((ScreenPixels
.GetWidth() * 25.4) / ScreenMM
.GetWidth()),
341 (int) ((ScreenPixels
.GetHeight() * 25.4) / ScreenMM
.GetHeight()) );
342 m_previewPrintout
->SetPPIPrinter( resolution
, resolution
);
344 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
345 sizeDevUnits
.x
= (wxCoord
)((float)sizeDevUnits
.x
* resolution
/ 72.0);
346 sizeDevUnits
.y
= (wxCoord
)((float)sizeDevUnits
.y
* resolution
/ 72.0);
347 wxSize
sizeTenthsMM(paper
->GetSize());
348 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
350 // If in landscape mode, we need to swap the width and height.
351 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
353 m_pageWidth
= sizeDevUnits
.y
;
354 m_pageHeight
= sizeDevUnits
.x
;
355 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
359 m_pageWidth
= sizeDevUnits
.x
;
360 m_pageHeight
= sizeDevUnits
.y
;
361 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
363 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
364 m_previewPrintout
->SetPaperRectPixels(wxRect(0, 0, m_pageWidth
, m_pageHeight
));
366 // At 100%, the page should look about page-size on the screen.
367 m_previewScaleX
= (float)0.8 * 72.0 / (float)resolution
;
368 m_previewScaleY
= m_previewScaleX
;