]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/printwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/printwin.cpp
3 // Purpose: wxWindowsPrinter framework
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 // Don't use the Windows printer if we're in wxUniv mode and using
27 // the PostScript architecture
28 #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
31 #include "wx/msw/wrapcdlg.h"
32 #include "wx/window.h"
33 #include "wx/msw/private.h"
37 #include "wx/msgdlg.h"
40 #include "wx/dcprint.h"
41 #include "wx/dcmemory.h"
45 #include "wx/msw/dib.h"
46 #include "wx/msw/dcmemory.h"
47 #include "wx/msw/printwin.h"
48 #include "wx/msw/printdlg.h"
49 #include "wx/msw/private.h"
50 #include "wx/msw/dcprint.h"
51 #if wxUSE_ENH_METAFILE
52 #include "wx/msw/enhmeta.h"
56 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 BOOL CALLBACK
wxAbortProc(HDC hdc
, int error
);
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
67 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
69 // ===========================================================================
71 // ===========================================================================
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
82 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
85 sm_abortWindow
= NULL
;
89 sm_lastError
= wxPRINTER_ERROR
;
93 if (m_printDialogData
.GetMinPage() < 1)
94 m_printDialogData
.SetMinPage(1);
95 if (m_printDialogData
.GetMaxPage() < 1)
96 m_printDialogData
.SetMaxPage(9999);
98 // Create a suitable device context
99 wxPrinterDC
*dc
wxDUMMY_INITIALIZE(NULL
);
102 dc
= wxDynamicCast(PrintDialog(parent
), wxPrinterDC
);
108 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
111 // May have pressed cancel.
112 if (!dc
|| !dc
->IsOk())
118 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
120 HDC hdc
= ::GetDC(NULL
);
121 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
122 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
123 ::ReleaseDC(NULL
, hdc
);
125 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSX
);
126 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSY
);
127 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
130 sm_lastError
= wxPRINTER_ERROR
;
134 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
135 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
137 // Set printout parameters
142 printout
->SetPageSizePixels((int)w
, (int)h
);
143 printout
->SetPaperRectPixels(dc
->GetPaperRect());
145 dc
->GetSizeMM(&w
, &h
);
146 printout
->SetPageSizeMM((int)w
, (int)h
);
148 // Create an abort window
149 wxBusyCursor busyCursor
;
151 printout
->OnPreparePrinting();
153 // Get some parameters from the printout, if defined
154 int fromPage
, toPage
;
155 int minPage
, maxPage
;
156 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
160 sm_lastError
= wxPRINTER_ERROR
;
164 // Only set min and max, because from and to have been
166 m_printDialogData
.SetMinPage(minPage
);
167 m_printDialogData
.SetMaxPage(maxPage
);
169 wxPrintAbortDialog
*win
= CreateAbortWindow(parent
, printout
);
172 ::SetAbortProc(GetHdcOf(*impl
), wxAbortProc
);
176 wxLogDebug(wxT("Could not create an abort dialog."));
177 sm_lastError
= wxPRINTER_ERROR
;
182 sm_abortWindow
= win
;
183 sm_abortWindow
->Show();
186 printout
->OnBeginPrinting();
188 sm_lastError
= wxPRINTER_NO_ERROR
;
190 int minPageNum
= minPage
, maxPageNum
= maxPage
;
192 if ( !m_printDialogData
.GetAllPages() )
194 minPageNum
= m_printDialogData
.GetFromPage();
195 maxPageNum
= m_printDialogData
.GetToPage();
198 // The dc we get from the PrintDialog will do multiple copies without help
199 // if the device supports it. Loop only if we have created a dc from our
200 // own m_printDialogData or the device does not support multiple copies.
201 // m_printDialogData.GetPrintData().GetNoCopies() is set from device
202 // devMode in printdlg.cpp/wxWindowsPrintDialog::ConvertFromNative()
203 const int maxCopyCount
= !prompt
||
204 !m_printDialogData
.GetPrintData().GetNoCopies()
205 ? m_printDialogData
.GetNoCopies() : 1;
206 for ( int copyCount
= 1; copyCount
<= maxCopyCount
; copyCount
++ )
208 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
210 wxLogError(_("Could not start printing."));
211 sm_lastError
= wxPRINTER_ERROR
;
216 sm_lastError
= wxPRINTER_CANCELLED
;
222 for ( pn
= minPageNum
;
223 pn
<= maxPageNum
&& printout
->HasPage(pn
);
226 win
->SetProgress(pn
- minPageNum
+ 1,
227 maxPageNum
- minPageNum
+ 1,
228 copyCount
, maxCopyCount
);
232 sm_lastError
= wxPRINTER_CANCELLED
;
237 bool cont
= printout
->OnPrintPage(pn
);
242 sm_lastError
= wxPRINTER_CANCELLED
;
247 printout
->OnEndDocument();
250 printout
->OnEndPrinting();
254 sm_abortWindow
->Show(false);
255 wxDELETE(sm_abortWindow
);
260 return sm_lastError
== wxPRINTER_NO_ERROR
;
263 wxDC
*wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
267 wxWindowsPrintDialog
dialog(parent
, & m_printDialogData
);
268 int ret
= dialog
.ShowModal();
272 dc
= dialog
.GetPrintDC();
273 m_printDialogData
= dialog
.GetPrintDialogData();
275 sm_lastError
= wxPRINTER_ERROR
;
277 sm_lastError
= wxPRINTER_NO_ERROR
;
280 sm_lastError
= wxPRINTER_CANCELLED
;
285 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
288 // We no longer expose that dialog
289 wxPrintDialog
dialog(parent
, & m_printDialogData
);
290 dialog
.GetPrintDialogData().SetSetupDialog(true);
292 int ret
= dialog
.ShowModal();
296 m_printDialogData
= dialog
.GetPrintDialogData();
299 return (ret
== wxID_OK
);
309 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
310 wxPrintout
*printoutForPrinting
,
311 wxPrintDialogData
*data
)
312 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
317 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
318 wxPrintout
*printoutForPrinting
,
320 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
325 wxWindowsPrintPreview::~wxWindowsPrintPreview()
329 bool wxWindowsPrintPreview::Print(bool interactive
)
331 if (!m_printPrintout
)
333 wxWindowsPrinter
printer(&m_printDialogData
);
334 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
337 void wxWindowsPrintPreview::DetermineScaling()
340 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
341 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
342 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
344 // Get a device context for the currently selected printer
345 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
356 if ( printerDC
.IsOk() )
358 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) printerDC
.GetImpl();
359 HDC dc
= GetHdcOf(*impl
);
360 printerWidthMM
= ::GetDeviceCaps(dc
, HORZSIZE
);
361 printerHeightMM
= ::GetDeviceCaps(dc
, VERTSIZE
);
362 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
363 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
364 logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
365 logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
367 paperRect
= printerDC
.GetPaperRect();
369 if ( logPPIPrinterX
== 0 ||
370 logPPIPrinterY
== 0 ||
371 printerWidthMM
== 0 ||
372 printerHeightMM
== 0 )
380 printerWidthMM
= 150;
381 printerHeightMM
= 250;
384 logPPIPrinterX
= 600;
385 logPPIPrinterY
= 600;
387 paperRect
= wxRect(0, 0, printerXRes
, printerYRes
);
390 m_pageWidth
= printerXRes
;
391 m_pageHeight
= printerYRes
;
392 m_previewPrintout
->SetPageSizePixels(printerXRes
, printerYRes
);
393 m_previewPrintout
->SetPageSizeMM(printerWidthMM
, printerHeightMM
);
394 m_previewPrintout
->SetPaperRectPixels(paperRect
);
395 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
397 // At 100%, the page should look about page-size on the screen.
398 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
399 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;
402 #if wxUSE_ENH_METAFILE
403 bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap
& bmp
, int pageNum
)
405 // The preview, as implemented in wxPrintPreviewBase (and as used prior to
406 // wx3) is inexact: it uses screen DC, which has much lower resolution and
407 // has other properties different from printer DC, so the preview is not
410 // To make matters worse, if the application depends heavily on
411 // GetTextExtent() or does text layout itself, the output in preview and on
412 // paper can be very different. In particular, wxHtmlEasyPrinting is
413 // affected and the preview can be easily off by several pages.
415 // To fix this, we render the preview into high-resolution enhanced
416 // metafile with properties identical to the printer DC. This guarantees
417 // metrics correctness while still being fast.
420 // print the preview into a metafile:
421 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
422 wxEnhMetaFileDC
metaDC(printerDC
,
424 printerDC
.GetSize().x
, printerDC
.GetSize().y
);
426 if ( !RenderPageIntoDC(metaDC
, pageNum
) )
429 wxEnhMetaFile
*metafile
= metaDC
.Close();
433 // now render the metafile:
435 bmpDC
.SelectObject(bmp
);
438 wxRect
outRect(0, 0, bmp
.GetWidth(), bmp
.GetHeight());
439 metafile
->Play(&bmpDC
, &outRect
);
444 // TODO: we should keep the metafile and reuse it when changing zoom level
448 #endif // wxUSE_ENH_METAFILE
450 BOOL CALLBACK
wxAbortProc(HDC
WXUNUSED(hdc
), int WXUNUSED(error
))
454 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
457 /* Process messages intended for the abort dialog box */
459 while (!wxPrinterBase::sm_abortIt
&& ::PeekMessage(&msg
, 0, 0, 0, TRUE
))
460 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
461 TranslateMessage(&msg
);
462 DispatchMessage(&msg
);
465 /* bAbort is TRUE (return is FALSE) if the user has aborted */
467 return !wxPrinterBase::sm_abortIt
;
471 // wxUSE_PRINTING_ARCHITECTURE