X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/131fc120229c186993abd08d8b156e58bd848e20..c9ed413ab4f5673ffff00198dc6ce1397398bda4:/src/html/htmprint.cpp diff --git a/src/html/htmprint.cpp b/src/html/htmprint.cpp index f4c978dbf3..4ce2e3b80a 100644 --- a/src/html/htmprint.cpp +++ b/src/html/htmprint.cpp @@ -24,6 +24,7 @@ #include "wx/settings.h" #include "wx/msgdlg.h" #include "wx/module.h" + #include "wx/sizer.h" #endif #include "wx/print.h" @@ -31,12 +32,35 @@ #include "wx/html/htmprint.h" #include "wx/wxhtml.h" #include "wx/wfstream.h" +#include "wx/infobar.h" // default font size of normal text (HTML font size 0) for printing, in points: #define DEFAULT_PRINT_FONT_SIZE 12 +// CSS specification offer following guidance on dealing with pixel sizes +// when printing at +// http://www.w3.org/TR/2004/CR-CSS21-20040225/syndata.html#length-units: +// +// Pixel units are relative to the resolution of the viewing device, i.e., +// most often a computer display. If the pixel density of the output +// device is very different from that of a typical computer display, the +// user agent should rescale pixel values. It is recommended that the [ +// reference pixel] be the visual angle of one pixel on a device with a +// pixel density of 96dpi and a distance from the reader of an arm's +// length. For a nominal arm's length of 28 inches, the visual angle is +// therefore about 0.0213 degrees. +// +// For reading at arm's length, 1px thus corresponds to about 0.26 mm +// (1/96 inch). When printed on a laser printer, meant for reading at a +// little less than arm's length (55 cm, 21 inches), 1px is about 0.20 mm. +// On a 300 dots-per-inch (dpi) printer, that may be rounded up to 3 dots +// (0.25 mm); on a 600 dpi printer, it can be rounded to 5 dots. +// +// See also http://trac.wxwidgets.org/ticket/10942. +#define TYPICAL_SCREEN_DPI 96.0 + //-------------------------------------------------------------------------------- // wxHtmlDCRenderer //-------------------------------------------------------------------------------- @@ -64,10 +88,10 @@ wxHtmlDCRenderer::~wxHtmlDCRenderer() -void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale) +void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale, double font_scale) { m_DC = dc; - m_Parser->SetDC(m_DC, pixel_scale); + m_Parser->SetDC(m_DC, pixel_scale, font_scale); } @@ -150,11 +174,14 @@ int wxHtmlDCRenderer::Render(int x, int y, else return GetTotalHeight(); } +int wxHtmlDCRenderer::GetTotalWidth() const +{ + return m_Cells ? m_Cells->GetWidth() : 0; +} -int wxHtmlDCRenderer::GetTotalHeight() +int wxHtmlDCRenderer::GetTotalHeight() const { - if (m_Cells) return m_Cells->GetHeight(); - else return 0; + return m_Cells ? m_Cells->GetHeight() : 0; } @@ -169,7 +196,7 @@ wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title) { m_Renderer = new wxHtmlDCRenderer; m_RendererHdr = new wxHtmlDCRenderer; - m_NumPages = wxHTML_PRINT_MAX_PAGES; + m_NumPages = INT_MAX; m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = true; m_Headers[0] = m_Headers[1] = wxEmptyString; m_Footers[0] = m_Footers[1] = wxEmptyString; @@ -197,9 +224,77 @@ void wxHtmlPrintout::AddFilter(wxHtmlFilter *filter) m_Filters.Append(filter); } +bool +wxHtmlPrintout::CheckFit(const wxSize& pageArea, const wxSize& docArea) const +{ + // Nothing to do if the contents fits horizontally. + if ( docArea.x <= pageArea.x ) + return true; + + // Otherwise warn the user more or less intrusively depending on whether + // we're previewing or printing: + if ( wxPrintPreview * const preview = GetPreview() ) + { + // Don't annoy the user too much when previewing by using info bar + // instead of a dialog box. +#if wxUSE_INFOBAR + wxFrame * const parent = preview->GetFrame(); + wxCHECK_MSG( parent, false, "No parent preview frame?" ); + + wxSizer * const sizer = parent->GetSizer(); + wxCHECK_MSG( sizer, false, "Preview frame should be using sizers" ); + + wxInfoBar * const bar = new wxInfoBar(parent); + sizer->Add(bar, wxSizerFlags().Expand()); + + // Note that the message here is similar to the one below but not + // exactly the same, notably we don't use the document title here + // because it's already clear which document it pertains to and the + // title may be long enough to make the text not fit in the window. + bar->ShowMessage + ( + _("This document doesn't fit on the page horizontally and " + "will be truncated when it is printed."), + wxICON_WARNING + ); +#endif // wxUSE_INFOBAR + } + else // We're going to really print and not just preview. + { + // This is our last chance to warn the user that the output will be + // mangled so do show a message box. + wxMessageDialog + dlg + ( + NULL, + wxString::Format + ( + _("The document \"%s\" doesn't fit on the page " + "horizontally and will be truncated if printed.\n" + "\n" + "Would you like to proceed with printing it nevertheless?"), + GetTitle() + ), + _("Printing"), + wxOK | wxCANCEL | wxCANCEL_DEFAULT | wxICON_QUESTION + ); + dlg.SetExtendedMessage + ( + _("If possible, try changing the layout parameters to " + "make the printout more narrow.") + ); + dlg.SetOKLabel(wxID_PRINT); + + if ( dlg.ShowModal() == wxID_CANCEL ) + return false; + } + + return true; +} + void wxHtmlPrintout::OnPreparePrinting() { - int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; + int pageWidth, pageHeight, mm_w, mm_h, dc_w, dc_h; float ppmm_h, ppmm_v; GetPageSizePixels(&pageWidth, &pageHeight); @@ -214,7 +309,6 @@ void wxHtmlPrintout::OnPreparePrinting() GetPPIScreen(&ppiScreenX, &ppiScreenY); wxUnusedVar(ppiScreenX); - wxDisplaySize(&scr_w, &scr_h); GetDC()->GetSize(&dc_w, &dc_h); GetDC()->SetUserScale((double)dc_w / (double)pageWidth, @@ -222,7 +316,9 @@ void wxHtmlPrintout::OnPreparePrinting() /* prepare headers/footers renderer: */ - m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY); + m_RendererHdr->SetDC(GetDC(), + (double)ppiPrinterY / TYPICAL_SCREEN_DPI, + (double)ppiPrinterY / (double)ppiScreenY); m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom))); if (m_Headers[0] != wxEmptyString) @@ -247,15 +343,30 @@ void wxHtmlPrintout::OnPreparePrinting() } /* prepare main renderer: */ - m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY); - m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), - (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) - - m_FooterHeight - m_HeaderHeight - - ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) - - ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v) - )); + m_Renderer->SetDC(GetDC(), + (double)ppiPrinterY / TYPICAL_SCREEN_DPI, + (double)ppiPrinterY / (double)ppiScreenY); + + const int printAreaW = int(ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)); + int printAreaH = int(ppmm_v * (mm_h - m_MarginTop - m_MarginBottom)); + if ( m_HeaderHeight ) + printAreaH -= int(m_HeaderHeight + m_MarginSpace * ppmm_v); + if ( m_FooterHeight ) + printAreaH -= int(m_FooterHeight + m_MarginSpace * ppmm_v); + + m_Renderer->SetSize(printAreaW, printAreaH); m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir); - CountPages(); + + if ( CheckFit(wxSize(printAreaW, printAreaH), + wxSize(m_Renderer->GetTotalWidth(), + m_Renderer->GetTotalHeight())) || IsPreview() ) + { + // do paginate the document + CountPages(); + } + //else: if we don't call CountPages() m_PageBreaks remains empty and our + // GetPageInfo() will return 0 as max page and so nothing will be + // printed } bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage) @@ -381,7 +492,6 @@ void wxHtmlPrintout::CountPages() int pos = 0; m_NumPages = 0; - // m_PageBreaks[0] = 0; m_PageBreaks.Clear(); m_PageBreaks.Add( 0); @@ -392,12 +502,6 @@ void wxHtmlPrintout::CountPages() m_PageBreaks, pos, true, INT_MAX); m_PageBreaks.Add( pos); - if( m_PageBreaks.GetCount() > wxHTML_PRINT_MAX_PAGES) - { - wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can't continue any longer!"), - _("Warning"), wxCANCEL | wxICON_ERROR ); - break; - } } while (pos < m_Renderer->GetTotalHeight()); } @@ -407,14 +511,13 @@ void wxHtmlPrintout::RenderPage(wxDC *dc, int page) { wxBusyCursor wait; - int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; + int pageWidth, pageHeight, mm_w, mm_h, dc_w, dc_h; float ppmm_h, ppmm_v; GetPageSizePixels(&pageWidth, &pageHeight); GetPageSizeMM(&mm_w, &mm_h); ppmm_h = (float)pageWidth / mm_w; ppmm_v = (float)pageHeight / mm_h; - wxDisplaySize(&scr_w, &scr_h); dc->GetSize(&dc_w, &dc_h); int ppiPrinterX, ppiPrinterY; @@ -427,7 +530,9 @@ void wxHtmlPrintout::RenderPage(wxDC *dc, int page) dc->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_h / (double)pageHeight); - m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); + m_Renderer->SetDC(dc, + (double)ppiPrinterY / TYPICAL_SCREEN_DPI, + (double)ppiPrinterY / (double)ppiScreenY); dc->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); @@ -436,7 +541,9 @@ void wxHtmlPrintout::RenderPage(wxDC *dc, int page) m_PageBreaks[page-1], false, m_PageBreaks[page]-m_PageBreaks[page-1]); - m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); + m_RendererHdr->SetDC(dc, + (double)ppiPrinterY / TYPICAL_SCREEN_DPI, + (double)ppiPrinterY / (double)ppiScreenY); if (m_Headers[page % 2] != wxEmptyString) { m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page));