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
)
77 wxCHECK_RET( width
, "width must be non-zero" );
78 wxCHECK_RET( height
, "height must be non-zero" );
85 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
87 wxCHECK_RET( m_DC
, "SetDC() must be called before SetHtmlText()" );
88 wxCHECK_RET( m_Width
, "SetSize() must be called before SetHtmlText()" );
92 m_FS
->ChangePathTo(basepath
, isdir
);
93 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
94 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
95 m_Cells
->Layout(m_Width
);
99 void wxHtmlDCRenderer::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
102 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
105 m_Cells
->Layout(m_Width
);
106 // else: SetHtmlText() not yet called, no need for relayout
109 void wxHtmlDCRenderer::SetStandardFonts(int size
,
110 const wxString
& normal_face
,
111 const wxString
& fixed_face
)
113 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
116 m_Cells
->Layout(m_Width
);
117 // else: SetHtmlText() not yet called, no need for relayout
120 int wxHtmlDCRenderer::Render(int x
, int y
,
121 wxArrayInt
& known_pagebreaks
,
122 int from
, int dont_render
, int to
)
124 wxCHECK_MSG( m_Cells
, 0, "SetHtmlText() must be called before Render()" );
125 wxCHECK_MSG( m_DC
, 0, "SetDC() must be called before Render()" );
129 pbreak
= (int)(from
+ m_Height
);
130 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
)) {}
131 hght
= pbreak
- from
;
137 wxHtmlRenderingInfo rinfo
;
138 wxDefaultHtmlRenderingStyle rstyle
;
139 rinfo
.SetStyle(&rstyle
);
140 m_DC
->SetBrush(*wxWHITE_BRUSH
);
141 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
146 m_DC
->DestroyClippingRegion();
149 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
150 else return GetTotalHeight();
154 int wxHtmlDCRenderer::GetTotalHeight()
156 if (m_Cells
) return m_Cells
->GetHeight();
161 //--------------------------------------------------------------------------------
163 //--------------------------------------------------------------------------------
166 wxList
wxHtmlPrintout::m_Filters
;
168 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
170 m_Renderer
= new wxHtmlDCRenderer
;
171 m_RendererHdr
= new wxHtmlDCRenderer
;
172 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
173 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= true;
174 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
175 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
176 m_HeaderHeight
= m_FooterHeight
= 0;
177 SetMargins(); // to default values
178 SetStandardFonts(DEFAULT_PRINT_FONT_SIZE
);
183 wxHtmlPrintout::~wxHtmlPrintout()
186 delete m_RendererHdr
;
189 void wxHtmlPrintout::CleanUpStatics()
191 WX_CLEAR_LIST(wxList
, m_Filters
);
195 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
197 m_Filters
.Append(filter
);
200 void wxHtmlPrintout::OnPreparePrinting()
202 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
203 float ppmm_h
, ppmm_v
;
205 GetPageSizePixels(&pageWidth
, &pageHeight
);
206 GetPageSizeMM(&mm_w
, &mm_h
);
207 ppmm_h
= (float)pageWidth
/ mm_w
;
208 ppmm_v
= (float)pageHeight
/ mm_h
;
210 int ppiPrinterX
, ppiPrinterY
;
211 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
212 wxUnusedVar(ppiPrinterX
);
213 int ppiScreenX
, ppiScreenY
;
214 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
215 wxUnusedVar(ppiScreenX
);
217 wxDisplaySize(&scr_w
, &scr_h
);
218 GetDC()->GetSize(&dc_w
, &dc_h
);
220 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
,
221 (double)dc_h
/ (double)pageHeight
);
223 /* prepare headers/footers renderer: */
225 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
226 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
227 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
228 if (m_Headers
[0] != wxEmptyString
)
230 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
231 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
233 else if (m_Headers
[1] != wxEmptyString
)
235 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
236 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
238 if (m_Footers
[0] != wxEmptyString
)
240 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
241 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
243 else if (m_Footers
[1] != wxEmptyString
)
245 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
246 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
249 /* prepare main renderer: */
250 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
251 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
252 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
253 m_FooterHeight
- m_HeaderHeight
-
254 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
255 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
257 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
261 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
263 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return false;
269 bool wxHtmlPrintout::OnPrintPage(int page
)
272 if (dc
&& dc
->IsOk())
275 RenderPage(dc
, page
);
282 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
285 if ( m_NumPages
>= (signed)m_PageBreaks
.GetCount()-1)
286 *maxPage
= m_NumPages
;
288 *maxPage
= (signed)m_PageBreaks
.GetCount()-1;
290 *selPageTo
= (signed)m_PageBreaks
.GetCount()-1;
295 bool wxHtmlPrintout::HasPage(int pageNum
)
297 return pageNum
> 0 && (unsigned)pageNum
< m_PageBreaks
.GetCount();
302 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
305 m_BasePath
= basepath
;
306 m_BasePathIsDir
= isdir
;
309 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
314 if (wxFileExists(htmlfile
))
315 ff
= fs
.OpenFile(wxFileSystem::FileNameToURL(htmlfile
));
317 ff
= fs
.OpenFile(htmlfile
);
321 wxLogError(htmlfile
+ _(": file does not exist!"));
326 wxHtmlFilterHTML defaultFilter
;
329 wxList::compatibility_iterator node
= m_Filters
.GetFirst();
332 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
335 doc
= h
->ReadFile(*ff
);
339 node
= node
->GetNext();
343 doc
= defaultFilter
.ReadFile(*ff
);
345 SetHtmlText(doc
, htmlfile
, false);
351 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
353 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
354 m_Headers
[0] = header
;
355 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
356 m_Headers
[1] = header
;
361 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
363 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
364 m_Footers
[0] = footer
;
365 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
366 m_Footers
[1] = footer
;
371 void wxHtmlPrintout::CountPages()
374 int pageWidth
, pageHeight
, mm_w
, mm_h
;
375 float ppmm_h
, ppmm_v
;
377 GetPageSizePixels(&pageWidth
, &pageHeight
);
378 GetPageSizeMM(&mm_w
, &mm_h
);
379 ppmm_h
= (float)pageWidth
/ mm_w
;
380 ppmm_v
= (float)pageHeight
/ mm_h
;
384 // m_PageBreaks[0] = 0;
386 m_PageBreaks
.Clear();
387 m_PageBreaks
.Add( 0);
390 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
391 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
394 m_PageBreaks
.Add( pos
);
395 if( m_PageBreaks
.GetCount() > wxHTML_PRINT_MAX_PAGES
)
397 wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can't continue any longer!"),
398 _("Warning"), wxCANCEL
| wxICON_ERROR
);
401 } while (pos
< m_Renderer
->GetTotalHeight());
406 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
410 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
411 float ppmm_h
, ppmm_v
;
413 GetPageSizePixels(&pageWidth
, &pageHeight
);
414 GetPageSizeMM(&mm_w
, &mm_h
);
415 ppmm_h
= (float)pageWidth
/ mm_w
;
416 ppmm_v
= (float)pageHeight
/ mm_h
;
417 wxDisplaySize(&scr_w
, &scr_h
);
418 dc
->GetSize(&dc_w
, &dc_h
);
420 int ppiPrinterX
, ppiPrinterY
;
421 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
422 wxUnusedVar(ppiPrinterX
);
423 int ppiScreenX
, ppiScreenY
;
424 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
425 wxUnusedVar(ppiScreenX
);
427 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
,
428 (double)dc_h
/ (double)pageHeight
);
430 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
432 dc
->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
434 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
435 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
), m_PageBreaks
,
436 m_PageBreaks
[page
-1], false, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
439 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
440 if (m_Headers
[page
% 2] != wxEmptyString
)
442 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
443 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
), m_PageBreaks
);
445 if (m_Footers
[page
% 2] != wxEmptyString
)
447 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
448 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
), m_PageBreaks
);
454 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
459 num
.Printf(wxT("%i"), page
);
460 r
.Replace(wxT("@PAGENUM@"), num
);
462 num
.Printf(wxT("%lu"), (unsigned long)(m_PageBreaks
.GetCount() - 1));
463 r
.Replace(wxT("@PAGESCNT@"), num
);
465 const wxDateTime now
= wxDateTime::Now();
466 r
.Replace(wxT("@DATE@"), now
.FormatDate());
467 r
.Replace(wxT("@TIME@"), now
.FormatTime());
469 r
.Replace(wxT("@TITLE@"), GetTitle());
476 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
479 m_MarginBottom
= bottom
;
481 m_MarginRight
= right
;
482 m_MarginSpace
= spaces
;
488 void wxHtmlPrintout::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
491 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
492 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
495 void wxHtmlPrintout::SetStandardFonts(int size
,
496 const wxString
& normal_face
,
497 const wxString
& fixed_face
)
499 m_Renderer
->SetStandardFonts(size
, normal_face
, fixed_face
);
500 m_RendererHdr
->SetStandardFonts(size
, normal_face
, fixed_face
);
505 //----------------------------------------------------------------------------
506 // wxHtmlEasyPrinting
507 //----------------------------------------------------------------------------
510 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
512 m_ParentWindow
= parentWindow
;
515 m_PageSetupData
= new wxPageSetupDialogData
;
516 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
518 m_PageSetupData
->EnableMargins(true);
519 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
520 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
522 SetStandardFonts(DEFAULT_PRINT_FONT_SIZE
);
527 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
530 delete m_PageSetupData
;
534 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
536 if (m_PrintData
== NULL
)
537 m_PrintData
= new wxPrintData();
542 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
544 wxHtmlPrintout
*p1
= CreatePrintout();
545 p1
->SetHtmlFile(htmlfile
);
546 wxHtmlPrintout
*p2
= CreatePrintout();
547 p2
->SetHtmlFile(htmlfile
);
548 return DoPreview(p1
, p2
);
553 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
555 wxHtmlPrintout
*p1
= CreatePrintout();
556 p1
->SetHtmlText(htmltext
, basepath
, true);
557 wxHtmlPrintout
*p2
= CreatePrintout();
558 p2
->SetHtmlText(htmltext
, basepath
, true);
559 return DoPreview(p1
, p2
);
564 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
566 wxHtmlPrintout
*p
= CreatePrintout();
567 p
->SetHtmlFile(htmlfile
);
568 bool ret
= DoPrint(p
);
575 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
577 wxHtmlPrintout
*p
= CreatePrintout();
578 p
->SetHtmlText(htmltext
, basepath
, true);
579 bool ret
= DoPrint(p
);
586 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
588 // Pass two printout objects: for preview, and possible printing.
589 wxPrintDialogData
printDialogData(*GetPrintData());
590 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
597 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
598 m_Name
+ _(" Preview"),
599 wxPoint(100, 100), wxSize(650, 500));
600 frame
->Centre(wxBOTH
);
608 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
610 wxPrintDialogData
printDialogData(*GetPrintData());
611 wxPrinter
printer(&printDialogData
);
613 if (!printer
.Print(m_ParentWindow
, printout
, true))
618 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
625 void wxHtmlEasyPrinting::PageSetup()
627 if (!GetPrintData()->Ok())
629 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
633 m_PageSetupData
->SetPrintData(*GetPrintData());
634 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
636 if (pageSetupDialog
.ShowModal() == wxID_OK
)
638 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
639 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
645 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
647 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
648 m_Headers
[0] = header
;
649 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
650 m_Headers
[1] = header
;
655 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
657 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
658 m_Footers
[0] = footer
;
659 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
660 m_Footers
[1] = footer
;
664 void wxHtmlEasyPrinting::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
667 m_fontMode
= FontMode_Explicit
;
668 m_FontFaceNormal
= normal_face
;
669 m_FontFaceFixed
= fixed_face
;
673 m_FontsSizes
= m_FontsSizesArr
;
674 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
680 void wxHtmlEasyPrinting::SetStandardFonts(int size
,
681 const wxString
& normal_face
,
682 const wxString
& fixed_face
)
684 m_fontMode
= FontMode_Standard
;
685 m_FontFaceNormal
= normal_face
;
686 m_FontFaceFixed
= fixed_face
;
687 m_FontsSizesArr
[0] = size
;
691 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
693 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
695 if (m_fontMode
== FontMode_Explicit
)
697 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
699 else // FontMode_Standard
701 p
->SetStandardFonts(m_FontsSizesArr
[0],
702 m_FontFaceNormal
, m_FontFaceFixed
);
705 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
706 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
707 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
708 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
710 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
711 m_PageSetupData
->GetMarginBottomRight().y
,
712 m_PageSetupData
->GetMarginTopLeft().x
,
713 m_PageSetupData
->GetMarginBottomRight().x
);
718 // A module to allow initialization/cleanup
719 // without calling these functions from app.cpp or from
720 // the user's application.
722 class wxHtmlPrintingModule
: public wxModule
724 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
726 wxHtmlPrintingModule() : wxModule() {}
727 bool OnInit() { return true; }
728 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
731 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
734 // This hack forces the linker to always link in m_* files
735 // (wxHTML doesn't work without handlers from these files)
736 #include "wx/html/forcelnk.h"
737 FORCE_WXHTML_MODULES()
739 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE