1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: html printing classes
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik, 1999
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
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"
42 //--------------------------------------------------------------------------------
44 //--------------------------------------------------------------------------------
47 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
50 m_Width
= m_Height
= 0;
52 m_Parser
= new wxHtmlWinParser(NULL
);
53 m_FS
= new wxFileSystem();
54 m_Parser
->SetFS(m_FS
);
59 wxHtmlDCRenderer::~wxHtmlDCRenderer()
61 if (m_Cells
) delete m_Cells
;
62 if (m_Parser
) delete m_Parser
;
63 if (m_FS
) delete m_FS
;
68 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
71 m_Parser
->SetDC(m_DC
, pixel_scale
);
76 void wxHtmlDCRenderer::SetSize(int width
, int height
)
83 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
85 if (m_DC
== NULL
) return;
87 if (m_Cells
!= NULL
) delete m_Cells
;
89 m_FS
->ChangePathTo(basepath
, isdir
);
90 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
91 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
92 m_Cells
->Layout(m_Width
);
96 void wxHtmlDCRenderer::SetFonts(wxString normal_face
, wxString fixed_face
,
99 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
100 if (m_DC
== NULL
&& m_Cells
!= NULL
) m_Cells
->Layout(m_Width
);
104 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
, int to
, int *known_pagebreaks
, int number_of_pages
)
108 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
110 pbreak
= (int)(from
+ m_Height
);
111 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
, number_of_pages
)) {}
112 hght
= pbreak
- from
;
118 wxHtmlRenderingInfo rinfo
;
119 wxDefaultHtmlRenderingStyle rstyle
;
120 rinfo
.SetStyle(&rstyle
);
121 m_DC
->SetBrush(*wxWHITE_BRUSH
);
122 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
125 y
, pbreak
+ (y
/*- from*/),
127 m_DC
->DestroyClippingRegion();
130 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
131 else return GetTotalHeight();
136 int wxHtmlDCRenderer::GetTotalHeight()
138 if (m_Cells
) return m_Cells
->GetHeight();
143 //--------------------------------------------------------------------------------
145 //--------------------------------------------------------------------------------
148 wxList
wxHtmlPrintout::m_Filters
;
150 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
152 m_Renderer
= new wxHtmlDCRenderer
;
153 m_RendererHdr
= new wxHtmlDCRenderer
;
154 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
155 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
156 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
157 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
158 m_HeaderHeight
= m_FooterHeight
= 0;
159 SetMargins(); // to default values
164 wxHtmlPrintout::~wxHtmlPrintout()
167 delete m_RendererHdr
;
170 void wxHtmlPrintout::CleanUpStatics()
172 m_Filters
.DeleteContents(TRUE
);
177 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
179 m_Filters
.Append(filter
);
182 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
184 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
185 float ppmm_h
, ppmm_v
;
187 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
189 GetPageSizePixels(&pageWidth
, &pageHeight
);
190 GetPageSizeMM(&mm_w
, &mm_h
);
191 ppmm_h
= (float)pageWidth
/ mm_w
;
192 ppmm_v
= (float)pageHeight
/ mm_h
;
194 int ppiPrinterX
, ppiPrinterY
;
195 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
196 int ppiScreenX
, ppiScreenY
;
197 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
199 wxDisplaySize(&scr_w
, &scr_h
);
200 GetDC()->GetSize(&dc_w
, &dc_h
);
202 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
204 /* prepare headers/footers renderer: */
206 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
207 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
208 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
209 if (m_Headers
[0] != wxEmptyString
)
211 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
212 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
214 else if (m_Headers
[1] != wxEmptyString
)
216 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
217 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
219 if (m_Footers
[0] != wxEmptyString
)
221 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
222 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
224 else if (m_Footers
[1] != wxEmptyString
)
226 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
227 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
230 /* prepare main renderer: */
231 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
232 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
233 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
234 m_FooterHeight
- m_HeaderHeight
-
235 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
236 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
238 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
244 bool wxHtmlPrintout::OnPrintPage(int page
)
250 RenderPage(dc
, page
);
257 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
260 *maxPage
= wxHTML_PRINT_MAX_PAGES
;
262 *selPageTo
= wxHTML_PRINT_MAX_PAGES
;
267 bool wxHtmlPrintout::HasPage(int pageNum
)
269 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
274 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
277 m_BasePath
= basepath
;
278 m_BasePathIsDir
= isdir
;
281 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
284 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
288 wxLogError(htmlfile
+ _(": file does not exist!"));
293 wxHtmlFilterHTML defaultFilter
;
296 wxNode
* node
= m_Filters
.GetFirst();
299 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
302 doc
= h
->ReadFile(*ff
);
306 node
= node
->GetNext();
310 doc
= defaultFilter
.ReadFile(*ff
);
312 SetHtmlText(doc
, htmlfile
, FALSE
);
318 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
320 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
321 m_Headers
[0] = header
;
322 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
323 m_Headers
[1] = header
;
328 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
330 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
331 m_Footers
[0] = footer
;
332 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
333 m_Footers
[1] = footer
;
338 void wxHtmlPrintout::CountPages()
341 int pageWidth
, pageHeight
, mm_w
, mm_h
;
342 float ppmm_h
, ppmm_v
;
344 GetPageSizePixels(&pageWidth
, &pageHeight
);
345 GetPageSizeMM(&mm_w
, &mm_h
);
346 ppmm_h
= (float)pageWidth
/ mm_w
;
347 ppmm_v
= (float)pageHeight
/ mm_h
;
356 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
357 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
358 pos
, TRUE
, INT_MAX
, m_PageBreaks
, m_NumPages
);
359 m_PageBreaks
[++m_NumPages
] = pos
;
360 } while (pos
< m_Renderer
->GetTotalHeight());
365 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
369 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
370 float ppmm_h
, ppmm_v
;
372 GetPageSizePixels(&pageWidth
, &pageHeight
);
373 GetPageSizeMM(&mm_w
, &mm_h
);
374 ppmm_h
= (float)pageWidth
/ mm_w
;
375 ppmm_v
= (float)pageHeight
/ mm_h
;
376 wxDisplaySize(&scr_w
, &scr_h
);
377 dc
->GetSize(&dc_w
, &dc_h
);
379 int ppiPrinterX
, ppiPrinterY
;
380 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
381 int ppiScreenX
, ppiScreenY
;
382 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
384 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
386 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
388 dc
->SetBackgroundMode(wxTRANSPARENT
);
390 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
391 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
392 m_PageBreaks
[page
-1], FALSE
, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
394 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
395 if (m_Headers
[page
% 2] != wxEmptyString
)
397 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
398 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
400 if (m_Footers
[page
% 2] != wxEmptyString
)
402 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
403 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
409 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
414 num
.Printf(wxT("%i"), page
);
415 r
.Replace(wxT("@PAGENUM@"), num
);
417 num
.Printf(wxT("%i"), m_NumPages
);
418 r
.Replace(wxT("@PAGESCNT@"), num
);
425 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
428 m_MarginBottom
= bottom
;
430 m_MarginRight
= right
;
431 m_MarginSpace
= spaces
;
437 void wxHtmlPrintout::SetFonts(wxString normal_face
, wxString fixed_face
,
440 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
441 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
446 //----------------------------------------------------------------------------
447 // wxHtmlEasyPrinting
448 //----------------------------------------------------------------------------
451 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
453 m_ParentWindow
= parentWindow
;
455 m_PrintData
= new wxPrintData
;
456 m_PageSetupData
= new wxPageSetupDialogData
;
457 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
459 m_PageSetupData
->EnableMargins(TRUE
);
460 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
461 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
463 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
468 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
471 delete m_PageSetupData
;
476 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
478 wxHtmlPrintout
*p1
= CreatePrintout();
479 p1
->SetHtmlFile(htmlfile
);
480 wxHtmlPrintout
*p2
= CreatePrintout();
481 p2
->SetHtmlFile(htmlfile
);
482 return DoPreview(p1
, p2
);
487 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
489 wxHtmlPrintout
*p1
= CreatePrintout();
490 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
491 wxHtmlPrintout
*p2
= CreatePrintout();
492 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
493 return DoPreview(p1
, p2
);
498 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
500 wxHtmlPrintout
*p
= CreatePrintout();
501 p
->SetHtmlFile(htmlfile
);
502 bool ret
= DoPrint(p
);
509 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
511 wxHtmlPrintout
*p
= CreatePrintout();
512 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
513 bool ret
= DoPrint(p
);
520 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
522 // Pass two printout objects: for preview, and possible printing.
523 wxPrintDialogData
printDialogData(*m_PrintData
);
524 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
531 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
532 m_Name
+ _(" Preview"),
533 wxPoint(100, 100), wxSize(650, 500));
534 frame
->Centre(wxBOTH
);
542 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
544 wxPrintDialogData
printDialogData(*m_PrintData
);
545 wxPrinter
printer(&printDialogData
);
547 if (!printer
.Print(m_ParentWindow
, printout
, TRUE
))
552 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
558 void wxHtmlEasyPrinting::PrinterSetup()
560 wxPrintDialogData
printDialogData(*m_PrintData
);
561 wxPrintDialog
printerDialog(m_ParentWindow
, &printDialogData
);
563 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
565 if (printerDialog
.ShowModal() == wxID_OK
)
566 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
571 void wxHtmlEasyPrinting::PageSetup()
573 if (!m_PrintData
->Ok())
575 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
579 m_PageSetupData
->SetPrintData(*m_PrintData
);
580 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
582 if (pageSetupDialog
.ShowModal() == wxID_OK
)
584 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
585 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
591 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
593 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
594 m_Headers
[0] = header
;
595 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
596 m_Headers
[1] = header
;
601 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
603 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
604 m_Footers
[0] = footer
;
605 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
606 m_Footers
[1] = footer
;
610 void wxHtmlEasyPrinting::SetFonts(wxString normal_face
, wxString fixed_face
,
613 m_FontFaceNormal
= normal_face
;
614 m_FontFaceFixed
= fixed_face
;
618 m_FontsSizes
= m_FontsSizesArr
;
619 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
626 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
628 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
630 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
632 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
633 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
634 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
635 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
637 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
638 m_PageSetupData
->GetMarginBottomRight().y
,
639 m_PageSetupData
->GetMarginTopLeft().x
,
640 m_PageSetupData
->GetMarginBottomRight().x
);
645 // A module to allow initialization/cleanup
646 // without calling these functions from app.cpp or from
647 // the user's application.
649 class wxHtmlPrintingModule
: public wxModule
651 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
653 wxHtmlPrintingModule() : wxModule() {}
654 bool OnInit() { return TRUE
; }
655 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
658 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
661 // This hack forces the linker to always link in m_* files
662 // (wxHTML doesn't work without handlers from these files)
663 #include "wx/html/forcelnk.h"
664 FORCE_WXHTML_MODULES()
666 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE