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
27 #include "wx/printdlg.h"
28 #include "wx/html/htmprint.h"
29 #include "wx/wxhtml.h"
30 #include "wx/wfstream.h"
31 #include "wx/module.h"
32 #include "wx/settings.h"
35 //--------------------------------------------------------------------------------
37 //--------------------------------------------------------------------------------
40 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
43 m_Width
= m_Height
= 0;
45 m_Parser
= new wxHtmlWinParser();
46 m_FS
= new wxFileSystem();
47 m_Parser
->SetFS(m_FS
);
52 wxHtmlDCRenderer::~wxHtmlDCRenderer()
54 if (m_Cells
) delete m_Cells
;
55 if (m_Parser
) delete m_Parser
;
56 if (m_FS
) delete m_FS
;
61 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
64 m_Parser
->SetDC(m_DC
, pixel_scale
);
69 void wxHtmlDCRenderer::SetSize(int width
, int height
)
76 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
78 if (m_DC
== NULL
) return;
80 if (m_Cells
!= NULL
) delete m_Cells
;
82 m_FS
->ChangePathTo(basepath
, isdir
);
83 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
84 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
85 m_Cells
->Layout(m_Width
);
89 void wxHtmlDCRenderer::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
92 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
93 if (m_DC
== NULL
&& m_Cells
!= NULL
)
94 m_Cells
->Layout(m_Width
);
97 void wxHtmlDCRenderer::SetStandardFonts(int size
,
98 const wxString
& normal_face
,
99 const wxString
& fixed_face
)
101 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
102 if (m_DC
== NULL
&& m_Cells
!= NULL
)
103 m_Cells
->Layout(m_Width
);
107 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
,
109 int *known_pagebreaks
, int number_of_pages
)
113 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
115 pbreak
= (int)(from
+ m_Height
);
116 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
, number_of_pages
)) {}
117 hght
= pbreak
- from
;
118 if (maxHeight
< hght
)
123 wxHtmlRenderingInfo rinfo
;
124 wxDefaultHtmlRenderingStyle rstyle
;
125 rinfo
.SetStyle(&rstyle
);
126 m_DC
->SetBrush(*wxWHITE_BRUSH
);
127 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
130 y
, pbreak
+ (y
/*- from*/),
132 m_DC
->DestroyClippingRegion();
135 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
136 else return GetTotalHeight();
141 int wxHtmlDCRenderer::GetTotalHeight()
143 if (m_Cells
) return m_Cells
->GetHeight();
148 //--------------------------------------------------------------------------------
150 //--------------------------------------------------------------------------------
153 wxList
wxHtmlPrintout::m_Filters
;
155 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
157 m_Renderer
= new wxHtmlDCRenderer
;
158 m_RendererHdr
= new wxHtmlDCRenderer
;
159 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
160 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= true;
161 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
162 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
163 m_HeaderHeight
= m_FooterHeight
= 0;
164 SetMargins(); // to default values
169 wxHtmlPrintout::~wxHtmlPrintout()
172 delete m_RendererHdr
;
175 void wxHtmlPrintout::CleanUpStatics()
177 WX_CLEAR_LIST(wxList
, m_Filters
);
181 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
183 m_Filters
.Append(filter
);
186 void wxHtmlPrintout::OnPreparePrinting()
188 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
189 float ppmm_h
, ppmm_v
;
191 GetPageSizePixels(&pageWidth
, &pageHeight
);
192 GetPageSizeMM(&mm_w
, &mm_h
);
193 ppmm_h
= (float)pageWidth
/ mm_w
;
194 ppmm_v
= (float)pageHeight
/ mm_h
;
196 int ppiPrinterX
, ppiPrinterY
;
197 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
198 int ppiScreenX
, ppiScreenY
;
199 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
201 wxDisplaySize(&scr_w
, &scr_h
);
202 GetDC()->GetSize(&dc_w
, &dc_h
);
204 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
206 /* prepare headers/footers renderer: */
208 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
209 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
210 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
211 if (m_Headers
[0] != wxEmptyString
)
213 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
214 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
216 else if (m_Headers
[1] != wxEmptyString
)
218 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
219 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
221 if (m_Footers
[0] != wxEmptyString
)
223 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
224 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
226 else if (m_Footers
[1] != wxEmptyString
)
228 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
229 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
232 /* prepare main renderer: */
233 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
234 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
235 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
236 m_FooterHeight
- m_HeaderHeight
-
237 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
238 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
240 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
244 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
246 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return false;
252 bool wxHtmlPrintout::OnPrintPage(int page
)
258 RenderPage(dc
, page
);
265 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
268 *maxPage
= m_NumPages
;
270 *selPageTo
= m_NumPages
;
275 bool wxHtmlPrintout::HasPage(int pageNum
)
277 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
282 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
285 m_BasePath
= basepath
;
286 m_BasePathIsDir
= isdir
;
289 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
294 if (wxFileExists(htmlfile
))
295 ff
= fs
.OpenFile(wxFileSystem::FileNameToURL(htmlfile
));
297 ff
= fs
.OpenFile(htmlfile
);
301 wxLogError(htmlfile
+ _(": file does not exist!"));
306 wxHtmlFilterHTML defaultFilter
;
309 wxList::compatibility_iterator node
= m_Filters
.GetFirst();
312 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
315 doc
= h
->ReadFile(*ff
);
319 node
= node
->GetNext();
323 doc
= defaultFilter
.ReadFile(*ff
);
325 SetHtmlText(doc
, htmlfile
, false);
331 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
333 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
334 m_Headers
[0] = header
;
335 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
336 m_Headers
[1] = header
;
341 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
343 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
344 m_Footers
[0] = footer
;
345 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
346 m_Footers
[1] = footer
;
351 void wxHtmlPrintout::CountPages()
354 int pageWidth
, pageHeight
, mm_w
, mm_h
;
355 float ppmm_h
, ppmm_v
;
357 GetPageSizePixels(&pageWidth
, &pageHeight
);
358 GetPageSizeMM(&mm_w
, &mm_h
);
359 ppmm_h
= (float)pageWidth
/ mm_w
;
360 ppmm_v
= (float)pageHeight
/ mm_h
;
369 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
370 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
371 pos
, true, INT_MAX
, m_PageBreaks
, m_NumPages
);
372 m_PageBreaks
[++m_NumPages
] = pos
;
373 } while (pos
< m_Renderer
->GetTotalHeight());
378 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
382 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
383 float ppmm_h
, ppmm_v
;
385 GetPageSizePixels(&pageWidth
, &pageHeight
);
386 GetPageSizeMM(&mm_w
, &mm_h
);
387 ppmm_h
= (float)pageWidth
/ mm_w
;
388 ppmm_v
= (float)pageHeight
/ mm_h
;
389 wxDisplaySize(&scr_w
, &scr_h
);
390 dc
->GetSize(&dc_w
, &dc_h
);
392 int ppiPrinterX
, ppiPrinterY
;
393 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
394 wxUnusedVar(ppiPrinterX
);
395 int ppiScreenX
, ppiScreenY
;
396 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
397 wxUnusedVar(ppiScreenX
);
399 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
401 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
403 dc
->SetBackgroundMode(wxTRANSPARENT
);
405 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
406 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
407 m_PageBreaks
[page
-1], false, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
409 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
410 if (m_Headers
[page
% 2] != wxEmptyString
)
412 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
413 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
415 if (m_Footers
[page
% 2] != wxEmptyString
)
417 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
418 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
424 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
429 num
.Printf(wxT("%i"), page
);
430 r
.Replace(wxT("@PAGENUM@"), num
);
432 num
.Printf(wxT("%i"), m_NumPages
);
433 r
.Replace(wxT("@PAGESCNT@"), num
);
440 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
443 m_MarginBottom
= bottom
;
445 m_MarginRight
= right
;
446 m_MarginSpace
= spaces
;
452 void wxHtmlPrintout::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
455 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
456 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
459 void wxHtmlPrintout::SetStandardFonts(int size
,
460 const wxString
& normal_face
,
461 const wxString
& fixed_face
)
463 m_Renderer
->SetStandardFonts(size
, normal_face
, fixed_face
);
464 m_RendererHdr
->SetStandardFonts(size
, normal_face
, fixed_face
);
469 //----------------------------------------------------------------------------
470 // wxHtmlEasyPrinting
471 //----------------------------------------------------------------------------
474 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
476 m_ParentWindow
= parentWindow
;
479 m_PageSetupData
= new wxPageSetupDialogData
;
480 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
482 m_PageSetupData
->EnableMargins(true);
483 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
484 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
486 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
491 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
494 delete m_PageSetupData
;
498 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
500 if (m_PrintData
== NULL
)
501 m_PrintData
= new wxPrintData();
506 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
508 wxHtmlPrintout
*p1
= CreatePrintout();
509 p1
->SetHtmlFile(htmlfile
);
510 wxHtmlPrintout
*p2
= CreatePrintout();
511 p2
->SetHtmlFile(htmlfile
);
512 return DoPreview(p1
, p2
);
517 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
519 wxHtmlPrintout
*p1
= CreatePrintout();
520 p1
->SetHtmlText(htmltext
, basepath
, true);
521 wxHtmlPrintout
*p2
= CreatePrintout();
522 p2
->SetHtmlText(htmltext
, basepath
, true);
523 return DoPreview(p1
, p2
);
528 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
530 wxHtmlPrintout
*p
= CreatePrintout();
531 p
->SetHtmlFile(htmlfile
);
532 bool ret
= DoPrint(p
);
539 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
541 wxHtmlPrintout
*p
= CreatePrintout();
542 p
->SetHtmlText(htmltext
, basepath
, true);
543 bool ret
= DoPrint(p
);
550 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
552 // Pass two printout objects: for preview, and possible printing.
553 wxPrintDialogData
printDialogData(*GetPrintData());
554 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
561 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
562 m_Name
+ _(" Preview"),
563 wxPoint(100, 100), wxSize(650, 500));
564 frame
->Centre(wxBOTH
);
572 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
574 wxPrintDialogData
printDialogData(*GetPrintData());
575 wxPrinter
printer(&printDialogData
);
577 if (!printer
.Print(m_ParentWindow
, printout
, true))
582 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
589 void wxHtmlEasyPrinting::PageSetup()
591 if (!GetPrintData()->Ok())
593 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
597 m_PageSetupData
->SetPrintData(*GetPrintData());
598 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
600 if (pageSetupDialog
.ShowModal() == wxID_OK
)
602 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
603 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
609 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
611 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
612 m_Headers
[0] = header
;
613 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
614 m_Headers
[1] = header
;
619 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
621 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
622 m_Footers
[0] = footer
;
623 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
624 m_Footers
[1] = footer
;
628 void wxHtmlEasyPrinting::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
631 m_fontMode
= FontMode_Explicit
;
632 m_FontFaceNormal
= normal_face
;
633 m_FontFaceFixed
= fixed_face
;
637 m_FontsSizes
= m_FontsSizesArr
;
638 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
644 void wxHtmlEasyPrinting::SetStandardFonts(int size
,
645 const wxString
& normal_face
,
646 const wxString
& fixed_face
)
648 m_fontMode
= FontMode_Standard
;
649 m_FontFaceNormal
= normal_face
;
650 m_FontFaceFixed
= fixed_face
;
651 m_FontsSizesArr
[0] = size
;
655 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
657 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
659 if (m_fontMode
== FontMode_Explicit
)
661 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
663 else // FontMode_Standard
665 p
->SetStandardFonts(m_FontsSizesArr
[0],
666 m_FontFaceNormal
, m_FontFaceFixed
);
669 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
670 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
671 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
672 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
674 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
675 m_PageSetupData
->GetMarginBottomRight().y
,
676 m_PageSetupData
->GetMarginTopLeft().x
,
677 m_PageSetupData
->GetMarginBottomRight().x
);
682 // A module to allow initialization/cleanup
683 // without calling these functions from app.cpp or from
684 // the user's application.
686 class wxHtmlPrintingModule
: public wxModule
688 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
690 wxHtmlPrintingModule() : wxModule() {}
691 bool OnInit() { return true; }
692 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
695 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
698 // This hack forces the linker to always link in m_* files
699 // (wxHTML doesn't work without handlers from these files)
700 #include "wx/html/forcelnk.h"
701 FORCE_WXHTML_MODULES()
703 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE