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
28 #include "wx/printdlg.h"
29 #include "wx/html/htmprint.h"
30 #include "wx/wxhtml.h"
31 #include "wx/wfstream.h"
32 #include "wx/module.h"
33 #include "wx/settings.h"
36 //--------------------------------------------------------------------------------
38 //--------------------------------------------------------------------------------
41 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
44 m_Width
= m_Height
= 0;
46 m_Parser
= new wxHtmlWinParser();
47 m_FS
= new wxFileSystem();
48 m_Parser
->SetFS(m_FS
);
53 wxHtmlDCRenderer::~wxHtmlDCRenderer()
55 if (m_Cells
) delete m_Cells
;
56 if (m_Parser
) delete m_Parser
;
57 if (m_FS
) delete m_FS
;
62 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
65 m_Parser
->SetDC(m_DC
, pixel_scale
);
70 void wxHtmlDCRenderer::SetSize(int width
, int height
)
77 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
79 if (m_DC
== NULL
) return;
81 if (m_Cells
!= NULL
) delete m_Cells
;
83 m_FS
->ChangePathTo(basepath
, isdir
);
84 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
85 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
86 m_Cells
->Layout(m_Width
);
90 void wxHtmlDCRenderer::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
93 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
94 if (m_DC
== NULL
&& m_Cells
!= NULL
)
95 m_Cells
->Layout(m_Width
);
98 void wxHtmlDCRenderer::SetStandardFonts(int size
,
99 const wxString
& normal_face
,
100 const wxString
& fixed_face
)
102 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
103 if (m_DC
== NULL
&& m_Cells
!= NULL
)
104 m_Cells
->Layout(m_Width
);
108 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
,
110 int *known_pagebreaks
, int number_of_pages
)
114 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
116 pbreak
= (int)(from
+ m_Height
);
117 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
, number_of_pages
)) {}
118 hght
= pbreak
- from
;
119 if (maxHeight
< hght
)
124 wxHtmlRenderingInfo rinfo
;
125 wxDefaultHtmlRenderingStyle rstyle
;
126 rinfo
.SetStyle(&rstyle
);
127 m_DC
->SetBrush(*wxWHITE_BRUSH
);
128 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
131 y
, pbreak
+ (y
/*- from*/),
133 m_DC
->DestroyClippingRegion();
136 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
137 else return GetTotalHeight();
142 int wxHtmlDCRenderer::GetTotalHeight()
144 if (m_Cells
) return m_Cells
->GetHeight();
149 //--------------------------------------------------------------------------------
151 //--------------------------------------------------------------------------------
154 wxList
wxHtmlPrintout::m_Filters
;
156 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
158 m_Renderer
= new wxHtmlDCRenderer
;
159 m_RendererHdr
= new wxHtmlDCRenderer
;
160 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
161 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= true;
162 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
163 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
164 m_HeaderHeight
= m_FooterHeight
= 0;
165 SetMargins(); // to default values
170 wxHtmlPrintout::~wxHtmlPrintout()
173 delete m_RendererHdr
;
176 void wxHtmlPrintout::CleanUpStatics()
178 WX_CLEAR_LIST(wxList
, m_Filters
);
182 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
184 m_Filters
.Append(filter
);
187 void wxHtmlPrintout::OnPreparePrinting()
189 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
190 float ppmm_h
, ppmm_v
;
192 GetPageSizePixels(&pageWidth
, &pageHeight
);
193 GetPageSizeMM(&mm_w
, &mm_h
);
194 ppmm_h
= (float)pageWidth
/ mm_w
;
195 ppmm_v
= (float)pageHeight
/ mm_h
;
197 int ppiPrinterX
, ppiPrinterY
;
198 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
199 int ppiScreenX
, ppiScreenY
;
200 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
202 wxDisplaySize(&scr_w
, &scr_h
);
203 GetDC()->GetSize(&dc_w
, &dc_h
);
205 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
207 /* prepare headers/footers renderer: */
209 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
210 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
211 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
212 if (m_Headers
[0] != wxEmptyString
)
214 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
215 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
217 else if (m_Headers
[1] != wxEmptyString
)
219 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
220 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
222 if (m_Footers
[0] != wxEmptyString
)
224 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
225 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
227 else if (m_Footers
[1] != wxEmptyString
)
229 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
230 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
233 /* prepare main renderer: */
234 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
235 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
236 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
237 m_FooterHeight
- m_HeaderHeight
-
238 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
239 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
241 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
245 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
247 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return false;
253 bool wxHtmlPrintout::OnPrintPage(int page
)
259 RenderPage(dc
, page
);
266 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
269 *maxPage
= m_NumPages
;
271 *selPageTo
= m_NumPages
;
276 bool wxHtmlPrintout::HasPage(int pageNum
)
278 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
283 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
286 m_BasePath
= basepath
;
287 m_BasePathIsDir
= isdir
;
290 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
295 if (wxFileExists(htmlfile
))
296 ff
= fs
.OpenFile(wxFileSystem::FileNameToURL(htmlfile
));
298 ff
= fs
.OpenFile(htmlfile
);
302 wxLogError(htmlfile
+ _(": file does not exist!"));
307 wxHtmlFilterHTML defaultFilter
;
310 wxList::compatibility_iterator node
= m_Filters
.GetFirst();
313 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
316 doc
= h
->ReadFile(*ff
);
320 node
= node
->GetNext();
324 doc
= defaultFilter
.ReadFile(*ff
);
326 SetHtmlText(doc
, htmlfile
, false);
332 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
334 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
335 m_Headers
[0] = header
;
336 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
337 m_Headers
[1] = header
;
342 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
344 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
345 m_Footers
[0] = footer
;
346 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
347 m_Footers
[1] = footer
;
352 void wxHtmlPrintout::CountPages()
355 int pageWidth
, pageHeight
, mm_w
, mm_h
;
356 float ppmm_h
, ppmm_v
;
358 GetPageSizePixels(&pageWidth
, &pageHeight
);
359 GetPageSizeMM(&mm_w
, &mm_h
);
360 ppmm_h
= (float)pageWidth
/ mm_w
;
361 ppmm_v
= (float)pageHeight
/ mm_h
;
370 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
371 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
372 pos
, true, INT_MAX
, m_PageBreaks
, m_NumPages
);
373 m_PageBreaks
[++m_NumPages
] = pos
;
374 } while (pos
< m_Renderer
->GetTotalHeight());
379 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
383 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
384 float ppmm_h
, ppmm_v
;
386 GetPageSizePixels(&pageWidth
, &pageHeight
);
387 GetPageSizeMM(&mm_w
, &mm_h
);
388 ppmm_h
= (float)pageWidth
/ mm_w
;
389 ppmm_v
= (float)pageHeight
/ mm_h
;
390 wxDisplaySize(&scr_w
, &scr_h
);
391 dc
->GetSize(&dc_w
, &dc_h
);
393 int ppiPrinterX
, ppiPrinterY
;
394 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
395 wxUnusedVar(ppiPrinterX
);
396 int ppiScreenX
, ppiScreenY
;
397 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
398 wxUnusedVar(ppiScreenX
);
400 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
402 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
404 dc
->SetBackgroundMode(wxTRANSPARENT
);
406 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
407 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
408 m_PageBreaks
[page
-1], false, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
410 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
411 if (m_Headers
[page
% 2] != wxEmptyString
)
413 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
414 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
416 if (m_Footers
[page
% 2] != wxEmptyString
)
418 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
419 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
425 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
430 num
.Printf(wxT("%i"), page
);
431 r
.Replace(wxT("@PAGENUM@"), num
);
433 num
.Printf(wxT("%i"), m_NumPages
);
434 r
.Replace(wxT("@PAGESCNT@"), num
);
441 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
444 m_MarginBottom
= bottom
;
446 m_MarginRight
= right
;
447 m_MarginSpace
= spaces
;
453 void wxHtmlPrintout::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
456 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
457 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
460 void wxHtmlPrintout::SetStandardFonts(int size
,
461 const wxString
& normal_face
,
462 const wxString
& fixed_face
)
464 m_Renderer
->SetStandardFonts(size
, normal_face
, fixed_face
);
465 m_RendererHdr
->SetStandardFonts(size
, normal_face
, fixed_face
);
470 //----------------------------------------------------------------------------
471 // wxHtmlEasyPrinting
472 //----------------------------------------------------------------------------
475 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
477 m_ParentWindow
= parentWindow
;
480 m_PageSetupData
= new wxPageSetupDialogData
;
481 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
483 m_PageSetupData
->EnableMargins(true);
484 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
485 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
487 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
492 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
495 delete m_PageSetupData
;
499 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
501 if (m_PrintData
== NULL
)
502 m_PrintData
= new wxPrintData();
507 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
509 wxHtmlPrintout
*p1
= CreatePrintout();
510 p1
->SetHtmlFile(htmlfile
);
511 wxHtmlPrintout
*p2
= CreatePrintout();
512 p2
->SetHtmlFile(htmlfile
);
513 return DoPreview(p1
, p2
);
518 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
520 wxHtmlPrintout
*p1
= CreatePrintout();
521 p1
->SetHtmlText(htmltext
, basepath
, true);
522 wxHtmlPrintout
*p2
= CreatePrintout();
523 p2
->SetHtmlText(htmltext
, basepath
, true);
524 return DoPreview(p1
, p2
);
529 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
531 wxHtmlPrintout
*p
= CreatePrintout();
532 p
->SetHtmlFile(htmlfile
);
533 bool ret
= DoPrint(p
);
540 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
542 wxHtmlPrintout
*p
= CreatePrintout();
543 p
->SetHtmlText(htmltext
, basepath
, true);
544 bool ret
= DoPrint(p
);
551 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
553 // Pass two printout objects: for preview, and possible printing.
554 wxPrintDialogData
printDialogData(*GetPrintData());
555 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
562 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
563 m_Name
+ _(" Preview"),
564 wxPoint(100, 100), wxSize(650, 500));
565 frame
->Centre(wxBOTH
);
573 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
575 wxPrintDialogData
printDialogData(*GetPrintData());
576 wxPrinter
printer(&printDialogData
);
578 if (!printer
.Print(m_ParentWindow
, printout
, true))
583 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
590 void wxHtmlEasyPrinting::PageSetup()
592 if (!GetPrintData()->Ok())
594 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
598 m_PageSetupData
->SetPrintData(*GetPrintData());
599 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
601 if (pageSetupDialog
.ShowModal() == wxID_OK
)
603 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
604 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
610 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
612 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
613 m_Headers
[0] = header
;
614 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
615 m_Headers
[1] = header
;
620 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
622 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
623 m_Footers
[0] = footer
;
624 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
625 m_Footers
[1] = footer
;
629 void wxHtmlEasyPrinting::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
632 m_fontMode
= FontMode_Explicit
;
633 m_FontFaceNormal
= normal_face
;
634 m_FontFaceFixed
= fixed_face
;
638 m_FontsSizes
= m_FontsSizesArr
;
639 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
645 void wxHtmlEasyPrinting::SetStandardFonts(int size
,
646 const wxString
& normal_face
,
647 const wxString
& fixed_face
)
649 m_fontMode
= FontMode_Standard
;
650 m_FontFaceNormal
= normal_face
;
651 m_FontFaceFixed
= fixed_face
;
652 m_FontsSizesArr
[0] = size
;
656 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
658 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
660 if (m_fontMode
== FontMode_Explicit
)
662 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
664 else // FontMode_Standard
666 p
->SetStandardFonts(m_FontsSizesArr
[0],
667 m_FontFaceNormal
, m_FontFaceFixed
);
670 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
671 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
672 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
673 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
675 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
676 m_PageSetupData
->GetMarginBottomRight().y
,
677 m_PageSetupData
->GetMarginTopLeft().x
,
678 m_PageSetupData
->GetMarginBottomRight().x
);
683 // A module to allow initialization/cleanup
684 // without calling these functions from app.cpp or from
685 // the user's application.
687 class wxHtmlPrintingModule
: public wxModule
689 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
691 wxHtmlPrintingModule() : wxModule() {}
692 bool OnInit() { return true; }
693 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
696 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
699 // This hack forces the linker to always link in m_* files
700 // (wxHTML doesn't work without handlers from these files)
701 #include "wx/html/forcelnk.h"
702 FORCE_WXHTML_MODULES()
704 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE