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
, int to
, int *known_pagebreaks
, int number_of_pages
)
119 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
121 pbreak
= (int)(from
+ m_Height
);
122 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
, number_of_pages
)) {}
123 hght
= pbreak
- from
;
129 wxHtmlRenderingInfo rinfo
;
130 wxDefaultHtmlRenderingStyle rstyle
;
131 rinfo
.SetStyle(&rstyle
);
132 m_DC
->SetBrush(*wxWHITE_BRUSH
);
133 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
136 y
, pbreak
+ (y
/*- from*/),
138 m_DC
->DestroyClippingRegion();
141 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
142 else return GetTotalHeight();
147 int wxHtmlDCRenderer::GetTotalHeight()
149 if (m_Cells
) return m_Cells
->GetHeight();
154 //--------------------------------------------------------------------------------
156 //--------------------------------------------------------------------------------
159 wxList
wxHtmlPrintout::m_Filters
;
161 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
163 m_Renderer
= new wxHtmlDCRenderer
;
164 m_RendererHdr
= new wxHtmlDCRenderer
;
165 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
166 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
167 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
168 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
169 m_HeaderHeight
= m_FooterHeight
= 0;
170 SetMargins(); // to default values
175 wxHtmlPrintout::~wxHtmlPrintout()
178 delete m_RendererHdr
;
181 void wxHtmlPrintout::CleanUpStatics()
183 WX_CLEAR_LIST(wxList
, m_Filters
);
187 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
189 m_Filters
.Append(filter
);
192 void wxHtmlPrintout::OnPreparePrinting()
194 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
195 float ppmm_h
, ppmm_v
;
197 GetPageSizePixels(&pageWidth
, &pageHeight
);
198 GetPageSizeMM(&mm_w
, &mm_h
);
199 ppmm_h
= (float)pageWidth
/ mm_w
;
200 ppmm_v
= (float)pageHeight
/ mm_h
;
202 int ppiPrinterX
, ppiPrinterY
;
203 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
204 int ppiScreenX
, ppiScreenY
;
205 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
207 wxDisplaySize(&scr_w
, &scr_h
);
208 GetDC()->GetSize(&dc_w
, &dc_h
);
210 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
212 /* prepare headers/footers renderer: */
214 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
215 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
216 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
217 if (m_Headers
[0] != wxEmptyString
)
219 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
220 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
222 else if (m_Headers
[1] != wxEmptyString
)
224 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
225 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
227 if (m_Footers
[0] != wxEmptyString
)
229 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
230 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
232 else if (m_Footers
[1] != wxEmptyString
)
234 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
235 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
238 /* prepare main renderer: */
239 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
240 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
241 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
242 m_FooterHeight
- m_HeaderHeight
-
243 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
244 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
246 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
250 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
252 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
258 bool wxHtmlPrintout::OnPrintPage(int page
)
264 RenderPage(dc
, page
);
271 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
274 *maxPage
= m_NumPages
;
276 *selPageTo
= m_NumPages
;
281 bool wxHtmlPrintout::HasPage(int pageNum
)
283 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
288 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
291 m_BasePath
= basepath
;
292 m_BasePathIsDir
= isdir
;
295 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
300 if (wxFileExists(htmlfile
))
301 ff
= fs
.OpenFile(wxFileSystem::FileNameToURL(htmlfile
));
303 ff
= fs
.OpenFile(htmlfile
);
307 wxLogError(htmlfile
+ _(": file does not exist!"));
312 wxHtmlFilterHTML defaultFilter
;
315 wxList::compatibility_iterator node
= m_Filters
.GetFirst();
318 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
321 doc
= h
->ReadFile(*ff
);
325 node
= node
->GetNext();
329 doc
= defaultFilter
.ReadFile(*ff
);
331 SetHtmlText(doc
, htmlfile
, FALSE
);
337 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
339 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
340 m_Headers
[0] = header
;
341 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
342 m_Headers
[1] = header
;
347 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
349 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
350 m_Footers
[0] = footer
;
351 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
352 m_Footers
[1] = footer
;
357 void wxHtmlPrintout::CountPages()
360 int pageWidth
, pageHeight
, mm_w
, mm_h
;
361 float ppmm_h
, ppmm_v
;
363 GetPageSizePixels(&pageWidth
, &pageHeight
);
364 GetPageSizeMM(&mm_w
, &mm_h
);
365 ppmm_h
= (float)pageWidth
/ mm_w
;
366 ppmm_v
= (float)pageHeight
/ mm_h
;
375 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
376 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
377 pos
, TRUE
, INT_MAX
, m_PageBreaks
, m_NumPages
);
378 m_PageBreaks
[++m_NumPages
] = pos
;
379 } while (pos
< m_Renderer
->GetTotalHeight());
384 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
388 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
389 float ppmm_h
, ppmm_v
;
391 GetPageSizePixels(&pageWidth
, &pageHeight
);
392 GetPageSizeMM(&mm_w
, &mm_h
);
393 ppmm_h
= (float)pageWidth
/ mm_w
;
394 ppmm_v
= (float)pageHeight
/ mm_h
;
395 wxDisplaySize(&scr_w
, &scr_h
);
396 dc
->GetSize(&dc_w
, &dc_h
);
398 int ppiPrinterX
, ppiPrinterY
;
399 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
400 wxUnusedVar(ppiPrinterX
);
401 int ppiScreenX
, ppiScreenY
;
402 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
403 wxUnusedVar(ppiScreenX
);
405 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
407 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
409 dc
->SetBackgroundMode(wxTRANSPARENT
);
411 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
412 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
413 m_PageBreaks
[page
-1], FALSE
, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
415 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
416 if (m_Headers
[page
% 2] != wxEmptyString
)
418 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
419 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
421 if (m_Footers
[page
% 2] != wxEmptyString
)
423 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
424 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
430 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
435 num
.Printf(wxT("%i"), page
);
436 r
.Replace(wxT("@PAGENUM@"), num
);
438 num
.Printf(wxT("%i"), m_NumPages
);
439 r
.Replace(wxT("@PAGESCNT@"), num
);
446 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
449 m_MarginBottom
= bottom
;
451 m_MarginRight
= right
;
452 m_MarginSpace
= spaces
;
458 void wxHtmlPrintout::SetFonts(wxString normal_face
, wxString fixed_face
,
461 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
462 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
465 void wxHtmlPrintout::SetStandardFonts(int size
,
466 const wxString
& normal_face
,
467 const wxString
& fixed_face
)
469 m_Renderer
->SetStandardFonts(size
, normal_face
, fixed_face
);
470 m_RendererHdr
->SetStandardFonts(size
, normal_face
, fixed_face
);
475 //----------------------------------------------------------------------------
476 // wxHtmlEasyPrinting
477 //----------------------------------------------------------------------------
480 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
482 m_ParentWindow
= parentWindow
;
485 m_PageSetupData
= new wxPageSetupDialogData
;
486 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
488 m_PageSetupData
->EnableMargins(TRUE
);
489 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
490 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
492 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
497 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
500 delete m_PageSetupData
;
504 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
506 if (m_PrintData
== NULL
)
507 m_PrintData
= new wxPrintData();
512 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
514 wxHtmlPrintout
*p1
= CreatePrintout();
515 p1
->SetHtmlFile(htmlfile
);
516 wxHtmlPrintout
*p2
= CreatePrintout();
517 p2
->SetHtmlFile(htmlfile
);
518 return DoPreview(p1
, p2
);
523 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
525 wxHtmlPrintout
*p1
= CreatePrintout();
526 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
527 wxHtmlPrintout
*p2
= CreatePrintout();
528 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
529 return DoPreview(p1
, p2
);
534 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
536 wxHtmlPrintout
*p
= CreatePrintout();
537 p
->SetHtmlFile(htmlfile
);
538 bool ret
= DoPrint(p
);
545 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
547 wxHtmlPrintout
*p
= CreatePrintout();
548 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
549 bool ret
= DoPrint(p
);
556 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
558 // Pass two printout objects: for preview, and possible printing.
559 wxPrintDialogData
printDialogData(*GetPrintData());
560 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
567 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
568 m_Name
+ _(" Preview"),
569 wxPoint(100, 100), wxSize(650, 500));
570 frame
->Centre(wxBOTH
);
578 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
580 wxPrintDialogData
printDialogData(*GetPrintData());
581 wxPrinter
printer(&printDialogData
);
583 if (!printer
.Print(m_ParentWindow
, printout
, TRUE
))
588 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
594 void wxHtmlEasyPrinting::PrinterSetup()
596 wxPrintDialogData
printDialogData(*GetPrintData());
597 wxPrintDialog
printerDialog(m_ParentWindow
, &printDialogData
);
599 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
601 if (printerDialog
.ShowModal() == wxID_OK
)
602 (*GetPrintData()) = printerDialog
.GetPrintDialogData().GetPrintData();
607 void wxHtmlEasyPrinting::PageSetup()
609 if (!GetPrintData()->Ok())
611 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
615 m_PageSetupData
->SetPrintData(*GetPrintData());
616 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
618 if (pageSetupDialog
.ShowModal() == wxID_OK
)
620 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
621 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
627 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
629 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
630 m_Headers
[0] = header
;
631 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
632 m_Headers
[1] = header
;
637 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
639 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
640 m_Footers
[0] = footer
;
641 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
642 m_Footers
[1] = footer
;
646 void wxHtmlEasyPrinting::SetFonts(wxString normal_face
, wxString fixed_face
,
649 m_fontMode
= FontMode_Explicit
;
650 m_FontFaceNormal
= normal_face
;
651 m_FontFaceFixed
= fixed_face
;
655 m_FontsSizes
= m_FontsSizesArr
;
656 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
662 void wxHtmlEasyPrinting::SetStandardFonts(int size
,
663 const wxString
& normal_face
,
664 const wxString
& fixed_face
)
666 m_fontMode
= FontMode_Standard
;
667 m_FontFaceNormal
= normal_face
;
668 m_FontFaceFixed
= fixed_face
;
669 m_FontsSizesArr
[0] = size
;
673 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
675 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
677 if (m_fontMode
== FontMode_Explicit
)
679 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
681 else // FontMode_Standard
683 p
->SetStandardFonts(m_FontsSizesArr
[0],
684 m_FontFaceNormal
, m_FontFaceFixed
);
687 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
688 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
689 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
690 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
692 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
693 m_PageSetupData
->GetMarginBottomRight().y
,
694 m_PageSetupData
->GetMarginTopLeft().x
,
695 m_PageSetupData
->GetMarginBottomRight().x
);
700 // A module to allow initialization/cleanup
701 // without calling these functions from app.cpp or from
702 // the user's application.
704 class wxHtmlPrintingModule
: public wxModule
706 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
708 wxHtmlPrintingModule() : wxModule() {}
709 bool OnInit() { return TRUE
; }
710 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
713 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
716 // This hack forces the linker to always link in m_* files
717 // (wxHTML doesn't work without handlers from these files)
718 #include "wx/html/forcelnk.h"
719 FORCE_WXHTML_MODULES()
721 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE