X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/77ffb5937e89927b621128789401db8921fe580f..18867602777b0a4787f24fa94ecf332a7434032f:/src/msw/printwin.cpp diff --git a/src/msw/printwin.cpp b/src/msw/printwin.cpp index 39149befce..587a4749fc 100644 --- a/src/msw/printwin.cpp +++ b/src/msw/printwin.cpp @@ -1,12 +1,12 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: printwin.cpp +// Name: src/msw/printwin.cpp // Purpose: wxWindowsPrinter framework // Author: Julian Smart // Modified by: // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart -// Licence: wxWidgets licence +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// // =========================================================================== @@ -17,10 +17,6 @@ // headers // --------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "printwin.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -28,13 +24,12 @@ #pragma hdrstop #endif -#include "wx/defs.h" - // Don't use the Windows printer if we're in wxUniv mode and using // the PostScript architecture #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" @@ -42,20 +37,22 @@ #include "wx/app.h" #include "wx/msgdlg.h" #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/dcprint.h" -#include "wx/printdlg.h" -#include "wx/log.h" +#include "wx/msw/printdlg.h" #include "wx/msw/private.h" +#include "wx/msw/dcprint.h" +#include "wx/msw/enhmeta.h" #include -#include "wx/msw/private.h" - -#include - #ifndef __WIN32__ #include #endif @@ -84,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() @@ -115,10 +112,10 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt m_printDialogData.SetMaxPage(9999); // Create a suitable device context - wxDC *dc = NULL; + wxPrinterDC *dc wxDUMMY_INITIALIZE(NULL); if (prompt) { - dc = PrintDialog(parent); + dc = wxDynamicCast(PrintDialog(parent), wxPrinterDC); if (!dc) return false; } @@ -128,24 +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; } - int logPPIScreenX = 0; - int logPPIScreenY = 0; - int logPPIPrinterX = 0; - int logPPIPrinterY = 0; + wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); HDC hdc = ::GetDC(NULL); - logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX); - logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY); + int logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX); + int logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY); ::ReleaseDC(NULL, hdc); - logPPIPrinterX = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSX); - 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; @@ -162,6 +156,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); @@ -190,14 +185,14 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt wxWindow *win = CreateAbortWindow(parent, printout); wxYield(); -#if 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 @@ -215,6 +210,7 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt sm_lastError = wxPRINTER_ERROR; delete dc; + return false; } sm_abortWindow = win; sm_abortWindow->Show(); @@ -286,14 +282,14 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt delete dc; - return (sm_lastError == wxPRINTER_NO_ERROR); + 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) @@ -311,8 +307,10 @@ wxDC* wxWindowsPrinter::PrintDialog(wxWindow *parent) return dc; } -bool wxWindowsPrinter::Setup(wxWindow *parent) +bool wxWindowsPrinter::Setup(wxWindow *WXUNUSED(parent)) { +#if 0 + // We no longer expose that dialog wxPrintDialog dialog(parent, & m_printDialogData); dialog.GetPrintDialogData().SetSetupDialog(true); @@ -324,6 +322,9 @@ bool wxWindowsPrinter::Setup(wxWindow *parent) } return (ret == wxID_OK); +#else + return false; +#endif } /* @@ -360,51 +361,116 @@ bool wxWindowsPrintPreview::Print(bool interactive) void wxWindowsPrintPreview::DetermineScaling() { - HDC dc = ::GetDC(NULL); - int screenWidth = ::GetDeviceCaps(dc, HORZSIZE); - int screenYRes = ::GetDeviceCaps(dc, VERTRES); + ScreenHDC dc; int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX); int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY); m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY); - ::ReleaseDC(NULL, dc); - // Get a device context for the currently selected printer wxPrinterDC printerDC(m_printDialogData.GetPrintData()); - int printerWidth = 150; - int printerHeight = 250; - int printerXRes = 1500; - int printerYRes = 2500; + int printerWidthMM; + int printerHeightMM; + int printerXRes; + int printerYRes; + int logPPIPrinterX; + int logPPIPrinterY; + + wxRect paperRect; - dc = GetHdcOf(printerDC); - if ( dc ) + if ( printerDC.IsOk() ) { - printerWidth = ::GetDeviceCaps(dc, HORZSIZE); - printerHeight = ::GetDeviceCaps(dc, VERTSIZE); + 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); + paperRect = printerDC.GetPaperRect(); - m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); - m_previewPrintout->SetPageSizeMM(printerWidth, printerHeight); - - if (logPPIPrinterX == 0 || logPPIPrinterY == 0 || printerWidth == 0 || printerHeight == 0) + if ( logPPIPrinterX == 0 || + logPPIPrinterY == 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_previewScale = (float)((float)screenWidth/(float)printerWidth); - m_previewScale = m_previewScale * (float)((float)screenYRes/(float)printerYRes); + m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX; + 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() @@ -422,15 +488,15 @@ LONG APIENTRY _EXPORT wxAbortProc(HDC WXUNUSED(hPr), int WXUNUSED(Code)) /* Process messages intended for the abort dialog box */ - while (!wxPrinterBase::sm_abortIt && PeekMessage(&msg, 0, 0, 0, TRUE)) + while (!wxPrinterBase::sm_abortIt && ::PeekMessage(&msg, 0, 0, 0, TRUE)) if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } - /* bAbort is TRUE (return is FALSE) if the user has aborted */ + /* bAbort is TRUE (return is FALSE) if the user has aborted */ - return (!wxPrinterBase::sm_abortIt); + return !wxPrinterBase::sm_abortIt; } #endif