1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmprint.cpp
3 // Purpose: html printing classes
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik, 1999
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS
24 #include "wx/settings.h"
25 #include "wx/msgdlg.h"
26 #include "wx/module.h"
30 #include "wx/printdlg.h"
31 #include "wx/html/htmprint.h"
32 #include "wx/wxhtml.h"
33 #include "wx/wfstream.h"
36 // default font size of normal text (HTML font size 0) for printing, in points:
37 #define DEFAULT_PRINT_FONT_SIZE 12
40 //--------------------------------------------------------------------------------
42 //--------------------------------------------------------------------------------
45 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
48 m_Width
= m_Height
= 0;
50 m_Parser
= new wxHtmlWinParser();
51 m_FS
= new wxFileSystem();
52 m_Parser
->SetFS(m_FS
);
53 SetStandardFonts(DEFAULT_PRINT_FONT_SIZE
);
58 wxHtmlDCRenderer::~wxHtmlDCRenderer()
60 if (m_Cells
) delete m_Cells
;
61 if (m_Parser
) delete m_Parser
;
62 if (m_FS
) delete m_FS
;
67 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
70 m_Parser
->SetDC(m_DC
, pixel_scale
);
75 void wxHtmlDCRenderer::SetSize(int width
, int height
)
82 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
84 if (m_DC
== NULL
) return;
86 if (m_Cells
!= NULL
) delete m_Cells
;
88 m_FS
->ChangePathTo(basepath
, isdir
);
89 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
90 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
91 m_Cells
->Layout(m_Width
);
95 void wxHtmlDCRenderer::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
98 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
99 if (m_DC
== NULL
&& m_Cells
!= NULL
)
100 m_Cells
->Layout(m_Width
);
103 void wxHtmlDCRenderer::SetStandardFonts(int size
,
104 const wxString
& normal_face
,
105 const wxString
& fixed_face
)
107 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
108 if (m_DC
== NULL
&& m_Cells
!= NULL
)
109 m_Cells
->Layout(m_Width
);
112 int wxHtmlDCRenderer::Render(int x
, int y
,
113 wxArrayInt
& known_pagebreaks
,
114 int from
, int dont_render
, int to
)
118 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
120 pbreak
= (int)(from
+ m_Height
);
121 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
)) {}
122 hght
= pbreak
- from
;
128 wxHtmlRenderingInfo rinfo
;
129 wxDefaultHtmlRenderingStyle rstyle
;
130 rinfo
.SetStyle(&rstyle
);
131 m_DC
->SetBrush(*wxWHITE_BRUSH
);
132 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
135 y
, pbreak
+ (y
/*- from*/),
137 m_DC
->DestroyClippingRegion();
140 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
141 else return GetTotalHeight();
145 int wxHtmlDCRenderer::GetTotalHeight()
147 if (m_Cells
) return m_Cells
->GetHeight();
152 //--------------------------------------------------------------------------------
154 //--------------------------------------------------------------------------------
157 wxList
wxHtmlPrintout::m_Filters
;
159 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
161 m_Renderer
= new wxHtmlDCRenderer
;
162 m_RendererHdr
= new wxHtmlDCRenderer
;
163 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
164 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= true;
165 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
166 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
167 m_HeaderHeight
= m_FooterHeight
= 0;
168 SetMargins(); // to default values
169 SetStandardFonts(DEFAULT_PRINT_FONT_SIZE
);
174 wxHtmlPrintout::~wxHtmlPrintout()
177 delete m_RendererHdr
;
180 void wxHtmlPrintout::CleanUpStatics()
182 WX_CLEAR_LIST(wxList
, m_Filters
);
186 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
188 m_Filters
.Append(filter
);
191 void wxHtmlPrintout::OnPreparePrinting()
193 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
194 float ppmm_h
, ppmm_v
;
196 GetPageSizePixels(&pageWidth
, &pageHeight
);
197 GetPageSizeMM(&mm_w
, &mm_h
);
198 ppmm_h
= (float)pageWidth
/ mm_w
;
199 ppmm_v
= (float)pageHeight
/ mm_h
;
201 int ppiPrinterX
, ppiPrinterY
;
202 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
203 int ppiScreenX
, ppiScreenY
;
204 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
206 wxDisplaySize(&scr_w
, &scr_h
);
207 GetDC()->GetSize(&dc_w
, &dc_h
);
209 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
211 /* prepare headers/footers renderer: */
213 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
214 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
215 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
216 if (m_Headers
[0] != wxEmptyString
)
218 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
219 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
221 else if (m_Headers
[1] != wxEmptyString
)
223 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
224 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
226 if (m_Footers
[0] != wxEmptyString
)
228 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
229 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
231 else if (m_Footers
[1] != wxEmptyString
)
233 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
234 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
237 /* prepare main renderer: */
238 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
239 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
240 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
241 m_FooterHeight
- m_HeaderHeight
-
242 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
243 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
245 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
249 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
251 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return false;
257 bool wxHtmlPrintout::OnPrintPage(int page
)
260 if (dc
&& dc
->IsOk())
263 RenderPage(dc
, page
);
270 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
273 if ( m_NumPages
>= (signed)m_PageBreaks
.GetCount()-1)
274 *maxPage
= m_NumPages
;
276 *maxPage
= (signed)m_PageBreaks
.GetCount()-1;
278 *selPageTo
= (signed)m_PageBreaks
.GetCount()-1;
283 bool wxHtmlPrintout::HasPage(int pageNum
)
285 return pageNum
> 0 && (unsigned)pageNum
< m_PageBreaks
.GetCount();
290 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
293 m_BasePath
= basepath
;
294 m_BasePathIsDir
= isdir
;
297 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
302 if (wxFileExists(htmlfile
))
303 ff
= fs
.OpenFile(wxFileSystem::FileNameToURL(htmlfile
));
305 ff
= fs
.OpenFile(htmlfile
);
309 wxLogError(htmlfile
+ _(": file does not exist!"));
314 wxHtmlFilterHTML defaultFilter
;
317 wxList::compatibility_iterator node
= m_Filters
.GetFirst();
320 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
323 doc
= h
->ReadFile(*ff
);
327 node
= node
->GetNext();
331 doc
= defaultFilter
.ReadFile(*ff
);
333 SetHtmlText(doc
, htmlfile
, false);
339 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
341 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
342 m_Headers
[0] = header
;
343 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
344 m_Headers
[1] = header
;
349 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
351 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
352 m_Footers
[0] = footer
;
353 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
354 m_Footers
[1] = footer
;
359 void wxHtmlPrintout::CountPages()
362 int pageWidth
, pageHeight
, mm_w
, mm_h
;
363 float ppmm_h
, ppmm_v
;
365 GetPageSizePixels(&pageWidth
, &pageHeight
);
366 GetPageSizeMM(&mm_w
, &mm_h
);
367 ppmm_h
= (float)pageWidth
/ mm_w
;
368 ppmm_v
= (float)pageHeight
/ mm_h
;
372 // m_PageBreaks[0] = 0;
374 m_PageBreaks
.Clear();
375 m_PageBreaks
.Add( 0);
378 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
379 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
382 m_PageBreaks
.Add( pos
);
383 if( m_PageBreaks
.GetCount() > wxHTML_PRINT_MAX_PAGES
)
385 wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can't continue any longer!"),
386 _("Warning"), wxCANCEL
| wxICON_ERROR
);
389 } while (pos
< m_Renderer
->GetTotalHeight());
394 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
398 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
399 float ppmm_h
, ppmm_v
;
401 GetPageSizePixels(&pageWidth
, &pageHeight
);
402 GetPageSizeMM(&mm_w
, &mm_h
);
403 ppmm_h
= (float)pageWidth
/ mm_w
;
404 ppmm_v
= (float)pageHeight
/ mm_h
;
405 wxDisplaySize(&scr_w
, &scr_h
);
406 dc
->GetSize(&dc_w
, &dc_h
);
408 int ppiPrinterX
, ppiPrinterY
;
409 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
410 wxUnusedVar(ppiPrinterX
);
411 int ppiScreenX
, ppiScreenY
;
412 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
413 wxUnusedVar(ppiScreenX
);
415 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
417 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
419 dc
->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
421 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
422 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
), m_PageBreaks
,
423 m_PageBreaks
[page
-1], false, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
426 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
427 if (m_Headers
[page
% 2] != wxEmptyString
)
429 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
430 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
), m_PageBreaks
);
432 if (m_Footers
[page
% 2] != wxEmptyString
)
434 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
435 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
), m_PageBreaks
);
441 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
446 num
.Printf(wxT("%i"), page
);
447 r
.Replace(wxT("@PAGENUM@"), num
);
449 num
.Printf(wxT("%lu"), (unsigned long)(m_PageBreaks
.GetCount() - 1));
450 r
.Replace(wxT("@PAGESCNT@"), num
);
452 const wxDateTime now
= wxDateTime::Now();
453 r
.Replace(wxT("@DATE@"), now
.FormatDate());
454 r
.Replace(wxT("@TIME@"), now
.FormatTime());
456 r
.Replace(wxT("@TITLE@"), GetTitle());
463 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
466 m_MarginBottom
= bottom
;
468 m_MarginRight
= right
;
469 m_MarginSpace
= spaces
;
475 void wxHtmlPrintout::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
478 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
479 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
482 void wxHtmlPrintout::SetStandardFonts(int size
,
483 const wxString
& normal_face
,
484 const wxString
& fixed_face
)
486 m_Renderer
->SetStandardFonts(size
, normal_face
, fixed_face
);
487 m_RendererHdr
->SetStandardFonts(size
, normal_face
, fixed_face
);
492 //----------------------------------------------------------------------------
493 // wxHtmlEasyPrinting
494 //----------------------------------------------------------------------------
497 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
499 m_ParentWindow
= parentWindow
;
502 m_PageSetupData
= new wxPageSetupDialogData
;
503 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
505 m_PageSetupData
->EnableMargins(true);
506 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
507 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
509 SetStandardFonts(DEFAULT_PRINT_FONT_SIZE
);
514 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
517 delete m_PageSetupData
;
521 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
523 if (m_PrintData
== NULL
)
524 m_PrintData
= new wxPrintData();
529 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
531 wxHtmlPrintout
*p1
= CreatePrintout();
532 p1
->SetHtmlFile(htmlfile
);
533 wxHtmlPrintout
*p2
= CreatePrintout();
534 p2
->SetHtmlFile(htmlfile
);
535 return DoPreview(p1
, p2
);
540 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
542 wxHtmlPrintout
*p1
= CreatePrintout();
543 p1
->SetHtmlText(htmltext
, basepath
, true);
544 wxHtmlPrintout
*p2
= CreatePrintout();
545 p2
->SetHtmlText(htmltext
, basepath
, true);
546 return DoPreview(p1
, p2
);
551 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
553 wxHtmlPrintout
*p
= CreatePrintout();
554 p
->SetHtmlFile(htmlfile
);
555 bool ret
= DoPrint(p
);
562 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
564 wxHtmlPrintout
*p
= CreatePrintout();
565 p
->SetHtmlText(htmltext
, basepath
, true);
566 bool ret
= DoPrint(p
);
573 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
575 // Pass two printout objects: for preview, and possible printing.
576 wxPrintDialogData
printDialogData(*GetPrintData());
577 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
584 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
585 m_Name
+ _(" Preview"),
586 wxPoint(100, 100), wxSize(650, 500));
587 frame
->Centre(wxBOTH
);
595 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
597 wxPrintDialogData
printDialogData(*GetPrintData());
598 wxPrinter
printer(&printDialogData
);
600 if (!printer
.Print(m_ParentWindow
, printout
, true))
605 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
612 void wxHtmlEasyPrinting::PageSetup()
614 if (!GetPrintData()->Ok())
616 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
620 m_PageSetupData
->SetPrintData(*GetPrintData());
621 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
623 if (pageSetupDialog
.ShowModal() == wxID_OK
)
625 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
626 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
632 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
634 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
635 m_Headers
[0] = header
;
636 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
637 m_Headers
[1] = header
;
642 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
644 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
645 m_Footers
[0] = footer
;
646 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
647 m_Footers
[1] = footer
;
651 void wxHtmlEasyPrinting::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
654 m_fontMode
= FontMode_Explicit
;
655 m_FontFaceNormal
= normal_face
;
656 m_FontFaceFixed
= fixed_face
;
660 m_FontsSizes
= m_FontsSizesArr
;
661 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
667 void wxHtmlEasyPrinting::SetStandardFonts(int size
,
668 const wxString
& normal_face
,
669 const wxString
& fixed_face
)
671 m_fontMode
= FontMode_Standard
;
672 m_FontFaceNormal
= normal_face
;
673 m_FontFaceFixed
= fixed_face
;
674 m_FontsSizesArr
[0] = size
;
678 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
680 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
682 if (m_fontMode
== FontMode_Explicit
)
684 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
686 else // FontMode_Standard
688 p
->SetStandardFonts(m_FontsSizesArr
[0],
689 m_FontFaceNormal
, m_FontFaceFixed
);
692 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
693 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
694 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
695 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
697 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
698 m_PageSetupData
->GetMarginBottomRight().y
,
699 m_PageSetupData
->GetMarginTopLeft().x
,
700 m_PageSetupData
->GetMarginBottomRight().x
);
705 // A module to allow initialization/cleanup
706 // without calling these functions from app.cpp or from
707 // the user's application.
709 class wxHtmlPrintingModule
: public wxModule
711 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
713 wxHtmlPrintingModule() : wxModule() {}
714 bool OnInit() { return true; }
715 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
718 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
721 // This hack forces the linker to always link in m_* files
722 // (wxHTML doesn't work without handlers from these files)
723 #include "wx/html/forcelnk.h"
724 FORCE_WXHTML_MODULES()
726 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE