1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: html printing classes
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik, 1999
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "htmprint.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
31 #if wxUSE_HTML && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS
35 #include "wx/printdlg.h"
36 #include "wx/html/htmprint.h"
37 #include "wx/wxhtml.h"
38 #include "wx/wfstream.h"
39 #include "wx/module.h"
40 #include "wx/settings.h"
43 //--------------------------------------------------------------------------------
45 //--------------------------------------------------------------------------------
48 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
51 m_Width
= m_Height
= 0;
53 m_Parser
= new wxHtmlWinParser(NULL
);
54 m_FS
= new wxFileSystem();
55 m_Parser
->SetFS(m_FS
);
60 wxHtmlDCRenderer::~wxHtmlDCRenderer()
62 if (m_Cells
) delete m_Cells
;
63 if (m_Parser
) delete m_Parser
;
64 if (m_FS
) delete m_FS
;
69 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
72 m_Parser
->SetDC(m_DC
, pixel_scale
);
77 void wxHtmlDCRenderer::SetSize(int width
, int height
)
84 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
86 if (m_DC
== NULL
) return;
88 if (m_Cells
!= NULL
) delete m_Cells
;
90 m_FS
->ChangePathTo(basepath
, isdir
);
91 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
92 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
93 m_Cells
->Layout(m_Width
);
97 void wxHtmlDCRenderer::SetFonts(wxString normal_face
, wxString fixed_face
,
100 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
101 if (m_DC
== NULL
&& m_Cells
!= NULL
)
102 m_Cells
->Layout(m_Width
);
105 void wxHtmlDCRenderer::SetStandardFonts(int size
,
106 const wxString
& normal_face
,
107 const wxString
& fixed_face
)
109 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
110 if (m_DC
== NULL
&& m_Cells
!= NULL
)
111 m_Cells
->Layout(m_Width
);
115 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
,
117 int *known_pagebreaks
, int number_of_pages
)
121 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
123 pbreak
= (int)(from
+ m_Height
);
124 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
, number_of_pages
)) {}
125 hght
= pbreak
- from
;
126 if (maxHeight
< hght
)
131 wxHtmlRenderingInfo rinfo
;
132 wxDefaultHtmlRenderingStyle rstyle
;
133 rinfo
.SetStyle(&rstyle
);
134 m_DC
->SetBrush(*wxWHITE_BRUSH
);
135 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
138 y
, pbreak
+ (y
/*- from*/),
140 m_DC
->DestroyClippingRegion();
143 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
144 else return GetTotalHeight();
149 int wxHtmlDCRenderer::GetTotalHeight()
151 if (m_Cells
) return m_Cells
->GetHeight();
156 //--------------------------------------------------------------------------------
158 //--------------------------------------------------------------------------------
161 wxList
wxHtmlPrintout::m_Filters
;
163 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
165 m_Renderer
= new wxHtmlDCRenderer
;
166 m_RendererHdr
= new wxHtmlDCRenderer
;
167 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
168 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= true;
169 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
170 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
171 m_HeaderHeight
= m_FooterHeight
= 0;
172 SetMargins(); // to default values
177 wxHtmlPrintout::~wxHtmlPrintout()
180 delete m_RendererHdr
;
183 void wxHtmlPrintout::CleanUpStatics()
185 WX_CLEAR_LIST(wxList
, m_Filters
);
189 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
191 m_Filters
.Append(filter
);
194 void wxHtmlPrintout::OnPreparePrinting()
196 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
197 float ppmm_h
, ppmm_v
;
199 GetPageSizePixels(&pageWidth
, &pageHeight
);
200 GetPageSizeMM(&mm_w
, &mm_h
);
201 ppmm_h
= (float)pageWidth
/ mm_w
;
202 ppmm_v
= (float)pageHeight
/ mm_h
;
204 int ppiPrinterX
, ppiPrinterY
;
205 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
206 int ppiScreenX
, ppiScreenY
;
207 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
209 wxDisplaySize(&scr_w
, &scr_h
);
210 GetDC()->GetSize(&dc_w
, &dc_h
);
212 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
214 /* prepare headers/footers renderer: */
216 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
217 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
218 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
219 if (m_Headers
[0] != wxEmptyString
)
221 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
222 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
224 else if (m_Headers
[1] != wxEmptyString
)
226 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
227 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
229 if (m_Footers
[0] != wxEmptyString
)
231 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
232 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
234 else if (m_Footers
[1] != wxEmptyString
)
236 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
237 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
240 /* prepare main renderer: */
241 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
242 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
243 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
244 m_FooterHeight
- m_HeaderHeight
-
245 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
246 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
248 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
252 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
254 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return false;
260 bool wxHtmlPrintout::OnPrintPage(int page
)
266 RenderPage(dc
, page
);
273 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
276 *maxPage
= m_NumPages
;
278 *selPageTo
= m_NumPages
;
283 bool wxHtmlPrintout::HasPage(int pageNum
)
285 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
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
;
377 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
378 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
379 pos
, true, INT_MAX
, m_PageBreaks
, m_NumPages
);
380 m_PageBreaks
[++m_NumPages
] = pos
;
381 } while (pos
< m_Renderer
->GetTotalHeight());
386 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
390 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
391 float ppmm_h
, ppmm_v
;
393 GetPageSizePixels(&pageWidth
, &pageHeight
);
394 GetPageSizeMM(&mm_w
, &mm_h
);
395 ppmm_h
= (float)pageWidth
/ mm_w
;
396 ppmm_v
= (float)pageHeight
/ mm_h
;
397 wxDisplaySize(&scr_w
, &scr_h
);
398 dc
->GetSize(&dc_w
, &dc_h
);
400 int ppiPrinterX
, ppiPrinterY
;
401 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
402 wxUnusedVar(ppiPrinterX
);
403 int ppiScreenX
, ppiScreenY
;
404 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
405 wxUnusedVar(ppiScreenX
);
407 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
409 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
411 dc
->SetBackgroundMode(wxTRANSPARENT
);
413 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
414 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
415 m_PageBreaks
[page
-1], false, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
417 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
418 if (m_Headers
[page
% 2] != wxEmptyString
)
420 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
421 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
423 if (m_Footers
[page
% 2] != wxEmptyString
)
425 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
426 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
432 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
437 num
.Printf(wxT("%i"), page
);
438 r
.Replace(wxT("@PAGENUM@"), num
);
440 num
.Printf(wxT("%i"), m_NumPages
);
441 r
.Replace(wxT("@PAGESCNT@"), num
);
448 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
451 m_MarginBottom
= bottom
;
453 m_MarginRight
= right
;
454 m_MarginSpace
= spaces
;
460 void wxHtmlPrintout::SetFonts(wxString normal_face
, wxString fixed_face
,
463 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
464 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
467 void wxHtmlPrintout::SetStandardFonts(int size
,
468 const wxString
& normal_face
,
469 const wxString
& fixed_face
)
471 m_Renderer
->SetStandardFonts(size
, normal_face
, fixed_face
);
472 m_RendererHdr
->SetStandardFonts(size
, normal_face
, fixed_face
);
477 //----------------------------------------------------------------------------
478 // wxHtmlEasyPrinting
479 //----------------------------------------------------------------------------
482 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
484 m_ParentWindow
= parentWindow
;
487 m_PageSetupData
= new wxPageSetupDialogData
;
488 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
490 m_PageSetupData
->EnableMargins(true);
491 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
492 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
494 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
499 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
502 delete m_PageSetupData
;
506 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
508 if (m_PrintData
== NULL
)
509 m_PrintData
= new wxPrintData();
514 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
516 wxHtmlPrintout
*p1
= CreatePrintout();
517 p1
->SetHtmlFile(htmlfile
);
518 wxHtmlPrintout
*p2
= CreatePrintout();
519 p2
->SetHtmlFile(htmlfile
);
520 return DoPreview(p1
, p2
);
525 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
527 wxHtmlPrintout
*p1
= CreatePrintout();
528 p1
->SetHtmlText(htmltext
, basepath
, true);
529 wxHtmlPrintout
*p2
= CreatePrintout();
530 p2
->SetHtmlText(htmltext
, basepath
, true);
531 return DoPreview(p1
, p2
);
536 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
538 wxHtmlPrintout
*p
= CreatePrintout();
539 p
->SetHtmlFile(htmlfile
);
540 bool ret
= DoPrint(p
);
547 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
549 wxHtmlPrintout
*p
= CreatePrintout();
550 p
->SetHtmlText(htmltext
, basepath
, true);
551 bool ret
= DoPrint(p
);
558 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
560 // Pass two printout objects: for preview, and possible printing.
561 wxPrintDialogData
printDialogData(*GetPrintData());
562 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
569 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
570 m_Name
+ _(" Preview"),
571 wxPoint(100, 100), wxSize(650, 500));
572 frame
->Centre(wxBOTH
);
580 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
582 wxPrintDialogData
printDialogData(*GetPrintData());
583 wxPrinter
printer(&printDialogData
);
585 if (!printer
.Print(m_ParentWindow
, printout
, true))
590 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
597 void wxHtmlEasyPrinting::PageSetup()
599 if (!GetPrintData()->Ok())
601 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
605 m_PageSetupData
->SetPrintData(*GetPrintData());
606 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
608 if (pageSetupDialog
.ShowModal() == wxID_OK
)
610 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
611 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
617 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
619 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
620 m_Headers
[0] = header
;
621 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
622 m_Headers
[1] = header
;
627 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
629 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
630 m_Footers
[0] = footer
;
631 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
632 m_Footers
[1] = footer
;
636 void wxHtmlEasyPrinting::SetFonts(wxString normal_face
, wxString fixed_face
,
639 m_fontMode
= FontMode_Explicit
;
640 m_FontFaceNormal
= normal_face
;
641 m_FontFaceFixed
= fixed_face
;
645 m_FontsSizes
= m_FontsSizesArr
;
646 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
652 void wxHtmlEasyPrinting::SetStandardFonts(int size
,
653 const wxString
& normal_face
,
654 const wxString
& fixed_face
)
656 m_fontMode
= FontMode_Standard
;
657 m_FontFaceNormal
= normal_face
;
658 m_FontFaceFixed
= fixed_face
;
659 m_FontsSizesArr
[0] = size
;
663 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
665 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
667 if (m_fontMode
== FontMode_Explicit
)
669 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
671 else // FontMode_Standard
673 p
->SetStandardFonts(m_FontsSizesArr
[0],
674 m_FontFaceNormal
, m_FontFaceFixed
);
677 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
678 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
679 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
680 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
682 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
683 m_PageSetupData
->GetMarginBottomRight().y
,
684 m_PageSetupData
->GetMarginTopLeft().x
,
685 m_PageSetupData
->GetMarginBottomRight().x
);
690 // A module to allow initialization/cleanup
691 // without calling these functions from app.cpp or from
692 // the user's application.
694 class wxHtmlPrintingModule
: public wxModule
696 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
698 wxHtmlPrintingModule() : wxModule() {}
699 bool OnInit() { return true; }
700 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
703 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
706 // This hack forces the linker to always link in m_* files
707 // (wxHTML doesn't work without handlers from these files)
708 #include "wx/html/forcelnk.h"
709 FORCE_WXHTML_MODULES()
711 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE