X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f4322df68b099e5e983acc0a3dad4ad5841b57b5..79d81c45ca3e1b6c6d0c8712986e0b3ab42ad28f:/src/msw/printwin.cpp diff --git a/src/msw/printwin.cpp b/src/msw/printwin.cpp index 975a0936fd..587a4749fc 100644 --- a/src/msw/printwin.cpp +++ b/src/msw/printwin.cpp @@ -39,11 +39,17 @@ #include "wx/intl.h" #include "wx/log.h" #include "wx/dcprint.h" + #include "wx/dcmemory.h" + #include "wx/image.h" #endif +#include "wx/msw/dib.h" +#include "wx/msw/dcmemory.h" #include "wx/msw/printwin.h" #include "wx/msw/printdlg.h" #include "wx/msw/private.h" +#include "wx/msw/dcprint.h" +#include "wx/msw/enhmeta.h" #include @@ -75,7 +81,7 @@ LONG APIENTRY _EXPORT wxAbortProc(HDC hPr, int Code); wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData *data) : wxPrinterBase(data) { - m_lpAbortProc = (WXFARPROC) MakeProcInstance((FARPROC) wxAbortProc, wxGetInstance()); + m_lpAbortProc = (WXFARPROC)wxAbortProc; } wxWindowsPrinter::~wxWindowsPrinter() @@ -119,19 +125,21 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt } // May have pressed cancel. - if (!dc || !dc->Ok()) + if (!dc || !dc->IsOk()) { if (dc) delete dc; return false; } + wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); + HDC hdc = ::GetDC(NULL); int logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX); int logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY); ::ReleaseDC(NULL, hdc); - int logPPIPrinterX = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSX); - int logPPIPrinterY = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSY); + int logPPIPrinterX = ::GetDeviceCaps((HDC) impl->GetHDC(), LOGPIXELSX); + int logPPIPrinterY = ::GetDeviceCaps((HDC) impl->GetHDC(), LOGPIXELSY); if (logPPIPrinterX == 0 || logPPIPrinterY == 0) { delete dc; @@ -177,14 +185,14 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt wxWindow *win = CreateAbortWindow(parent, printout); wxYield(); -#if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__) +#if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__GNUWIN32__) || !defined(__WIN32__) #ifdef STRICT - ::SetAbortProc((HDC) dc->GetHDC(), (ABORTPROC) m_lpAbortProc); + ::SetAbortProc((HDC) impl->GetHDC(), (ABORTPROC) m_lpAbortProc); #else - ::SetAbortProc((HDC) dc->GetHDC(), (FARPROC) m_lpAbortProc); + ::SetAbortProc((HDC) impl->GetHDC(), (FARPROC) m_lpAbortProc); #endif #else - ::SetAbortProc((HDC) dc->GetHDC(), (int (_stdcall *) + ::SetAbortProc((HDC) impl->GetHDC(), (int (_stdcall *) // cast it to right type only if required // FIXME it's really cdecl and we're casting it to stdcall - either there is // something I don't understand or it will crash at first usage @@ -279,7 +287,7 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt wxDC *wxWindowsPrinter::PrintDialog(wxWindow *parent) { - wxDC *dc = (wxPrinterDC*) NULL; + wxDC *dc = NULL; wxWindowsPrintDialog dialog(parent, & m_printDialogData); int ret = dialog.ShowModal(); @@ -370,9 +378,10 @@ void wxWindowsPrintPreview::DetermineScaling() wxRect paperRect; - if ( printerDC.Ok() ) + if ( printerDC.IsOk() ) { - HDC dc = GetHdcOf(printerDC); + wxPrinterDCImpl *impl = (wxPrinterDCImpl*) printerDC.GetImpl(); + HDC dc = GetHdcOf(*impl); printerWidthMM = ::GetDeviceCaps(dc, HORZSIZE); printerHeightMM = ::GetDeviceCaps(dc, VERTSIZE); printerXRes = ::GetDeviceCaps(dc, HORZRES); @@ -415,6 +424,53 @@ void wxWindowsPrintPreview::DetermineScaling() m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY; } +bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum) +{ + // The preview, as implemented in wxPrintPreviewBase (and as used prior to + // wx3) is inexact: it uses screen DC, which has much lower resolution and + // has other properties different from printer DC, so the preview is not + // quite right. + // + // To make matters worse, if the application depends heavily on + // GetTextExtent() or does text layout itself, the output in preview and on + // paper can be very different. In particular, wxHtmlEasyPrinting is + // affected and the preview can be easily off by several pages. + // + // To fix this, we render the preview into high-resolution enhanced + // metafile with properties identical to the printer DC. This guarantees + // metrics correctness while still being fast. + + + // print the preview into a metafile: + wxPrinterDC printerDC(m_printDialogData.GetPrintData()); + wxEnhMetaFileDC metaDC(printerDC, + wxEmptyString, + printerDC.GetSize().x, printerDC.GetSize().y); + + if ( !RenderPageIntoDC(metaDC, pageNum) ) + return false; + + wxEnhMetaFile *metafile = metaDC.Close(); + if ( !metafile ) + return false; + + // now render the metafile: + wxMemoryDC bmpDC; + bmpDC.SelectObject(bmp); + bmpDC.Clear(); + + wxRect outRect(0, 0, bmp.GetWidth(), bmp.GetHeight()); + metafile->Play(&bmpDC, &outRect); + + + delete metafile; + + // TODO: we should keep the metafile and reuse it when changing zoom level + + return true; +} + + /**************************************************************************** FUNCTION: wxAbortProc()