X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f2a59080e93be0631d05065b42600cf969e5d995..f559d1a23c71252b698badf63e4685a3abfa1712:/src/msw/printwin.cpp diff --git a/src/msw/printwin.cpp b/src/msw/printwin.cpp index 3721f3ee5a..9018f7de70 100644 --- a/src/msw/printwin.cpp +++ b/src/msw/printwin.cpp @@ -29,6 +29,7 @@ #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW) #ifndef WX_PRECOMP + #include "wx/msw/wrapcdlg.h" #include "wx/window.h" #include "wx/msw/private.h" #include "wx/utils.h" @@ -38,24 +39,26 @@ #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/printdlg.h" +#include "wx/msw/printdlg.h" #include "wx/msw/private.h" - -#include - -#include "wx/msw/wrapcdlg.h" -#ifndef __WIN32__ - #include +#include "wx/msw/dcprint.h" +#if wxUSE_ENH_METAFILE + #include "wx/msw/enhmeta.h" #endif +#include // --------------------------------------------------------------------------- // private functions // --------------------------------------------------------------------------- -LONG APIENTRY _EXPORT wxAbortProc(HDC hPr, int Code); +BOOL CALLBACK wxAbortProc(HDC hdc, int error); // --------------------------------------------------------------------------- // wxWin macros @@ -75,16 +78,6 @@ LONG APIENTRY _EXPORT wxAbortProc(HDC hPr, int Code); wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData *data) : wxPrinterBase(data) { - m_lpAbortProc = (WXFARPROC) MakeProcInstance((FARPROC) wxAbortProc, wxGetInstance()); -} - -wxWindowsPrinter::~wxWindowsPrinter() -{ - // avoids warning about statement with no effect (FreeProcInstance - // doesn't do anything under Win32) -#if !defined(__WIN32__) && !defined(__NT__) - FreeProcInstance((FARPROC) m_lpAbortProc); -#endif } bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) @@ -98,18 +91,16 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt return false; } - printout->SetIsPreview(false); - if (m_printDialogData.GetMinPage() < 1) m_printDialogData.SetMinPage(1); if (m_printDialogData.GetMaxPage() < 1) m_printDialogData.SetMaxPage(9999); // Create a suitable device context - wxDC *dc wxDUMMY_INITIALIZE(NULL); + wxPrinterDC *dc wxDUMMY_INITIALIZE(NULL); if (prompt) { - dc = PrintDialog(parent); + dc = wxDynamicCast(PrintDialog(parent), wxPrinterDC); if (!dc) return false; } @@ -119,19 +110,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; @@ -148,6 +141,7 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt int w, h; dc->GetSize(&w, &h); printout->SetPageSizePixels((int)w, (int)h); + printout->SetPaperRectPixels(dc->GetPaperRect()); dc->GetSizeMM(&w, &h); printout->SetPageSizeMM((int)w, (int)h); @@ -176,24 +170,7 @@ 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__) -#ifdef STRICT - ::SetAbortProc((HDC) dc->GetHDC(), (ABORTPROC) m_lpAbortProc); -#else - ::SetAbortProc((HDC) dc->GetHDC(), (FARPROC) m_lpAbortProc); -#endif -#else - ::SetAbortProc((HDC) dc->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 -#ifdef STRICT - (HDC, int) -#else - () -#endif - )m_lpAbortProc); -#endif + ::SetAbortProc(GetHdcOf(*impl), wxAbortProc); if (!win) { @@ -267,8 +244,7 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt if (sm_abortWindow) { sm_abortWindow->Show(false); - delete sm_abortWindow; - sm_abortWindow = NULL; + wxDELETE(sm_abortWindow); } delete dc; @@ -276,11 +252,11 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt return sm_lastError == wxPRINTER_NO_ERROR; } -wxDC* wxWindowsPrinter::PrintDialog(wxWindow *parent) +wxDC *wxWindowsPrinter::PrintDialog(wxWindow *parent) { - wxDC* dc = (wxDC*) NULL; + wxDC *dc = NULL; - wxPrintDialog dialog(parent, & m_printDialogData); + wxWindowsPrintDialog dialog(parent, & m_printDialogData); int ret = dialog.ShowModal(); if (ret == wxID_OK) @@ -353,69 +329,117 @@ bool wxWindowsPrintPreview::Print(bool interactive) void wxWindowsPrintPreview::DetermineScaling() { ScreenHDC dc; - int screenWidth = ::GetDeviceCaps(dc, HORZSIZE); - int screenXRes = ::GetDeviceCaps(dc, HORZRES); int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX); int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY); m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY); - // Get a device context for the currently selected printer wxPrinterDC printerDC(m_printDialogData.GetPrintData()); - int printerWidth = 150; - int printerXRes = 1500; - int printerYRes = 2500; + int printerWidthMM; + int printerHeightMM; + int printerXRes; + int printerYRes; + int logPPIPrinterX; + int logPPIPrinterY; - if ( printerDC.Ok() ) - { - HDC dc = GetHdcOf(printerDC); + wxRect paperRect; - printerWidth = ::GetDeviceCaps(dc, HORZSIZE); - int printerHeight = ::GetDeviceCaps(dc, VERTSIZE); + if ( printerDC.IsOk() ) + { + wxPrinterDCImpl *impl = (wxPrinterDCImpl*) printerDC.GetImpl(); + HDC dc = GetHdcOf(*impl); + printerWidthMM = ::GetDeviceCaps(dc, HORZSIZE); + printerHeightMM = ::GetDeviceCaps(dc, VERTSIZE); printerXRes = ::GetDeviceCaps(dc, HORZRES); printerYRes = ::GetDeviceCaps(dc, VERTRES); + logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX); + logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY); - int logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX); - int logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY); - - m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); - m_previewPrintout->SetPageSizeMM(printerWidth, printerHeight); + paperRect = printerDC.GetPaperRect(); if ( logPPIPrinterX == 0 || logPPIPrinterY == 0 || - printerWidth == 0 || - printerHeight == 0 ) + printerWidthMM == 0 || + printerHeightMM == 0 ) { m_isOk = false; } } else { + // use some defaults + printerWidthMM = 150; + printerHeightMM = 250; + printerXRes = 1500; + printerYRes = 2500; + logPPIPrinterX = 600; + logPPIPrinterY = 600; + + paperRect = wxRect(0, 0, printerXRes, printerYRes); m_isOk = false; } - m_pageWidth = printerXRes; m_pageHeight = printerYRes; + m_previewPrintout->SetPageSizePixels(printerXRes, printerYRes); + m_previewPrintout->SetPageSizeMM(printerWidthMM, printerHeightMM); + m_previewPrintout->SetPaperRectPixels(paperRect); + m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); // At 100%, the page should look about page-size on the screen. + m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX; + m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY; +} + +#if wxUSE_ENH_METAFILE +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. // - // TODO: the scale could be different in vertical and horizontal directions - float screenDPI = (float)screenXRes / screenWidth; - float printerDPI = (float)printerXRes / printerWidth; + // 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. - m_previewScale = screenDPI / printerDPI; -} -/**************************************************************************** + // 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; - FUNCTION: wxAbortProc() + // now render the metafile: + wxMemoryDC bmpDC; + bmpDC.SelectObject(bmp); + bmpDC.Clear(); - PURPOSE: Processes messages for the Abort Dialog box + wxRect outRect(0, 0, bmp.GetWidth(), bmp.GetHeight()); + metafile->Play(&bmpDC, &outRect); -****************************************************************************/ -LONG APIENTRY _EXPORT wxAbortProc(HDC WXUNUSED(hPr), int WXUNUSED(Code)) + delete metafile; + + // TODO: we should keep the metafile and reuse it when changing zoom level + + return true; +} +#endif // wxUSE_ENH_METAFILE + +BOOL CALLBACK wxAbortProc(HDC WXUNUSED(hdc), int WXUNUSED(error)) { MSG msg;