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 WX_CLEAR_LIST(wxList
, m_Filters
);
176 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
178 m_Filters
.Append(filter
);
181 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
183 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
184 float ppmm_h
, ppmm_v
;
186 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
188 GetPageSizePixels(&pageWidth
, &pageHeight
);
189 GetPageSizeMM(&mm_w
, &mm_h
);
190 ppmm_h
= (float)pageWidth
/ mm_w
;
191 ppmm_v
= (float)pageHeight
/ mm_h
;
193 int ppiPrinterX
, ppiPrinterY
;
194 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
195 int ppiScreenX
, ppiScreenY
;
196 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
198 wxDisplaySize(&scr_w
, &scr_h
);
199 GetDC()->GetSize(&dc_w
, &dc_h
);
201 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
203 /* prepare headers/footers renderer: */
205 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
206 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
207 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
208 if (m_Headers
[0] != wxEmptyString
)
210 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
211 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
213 else if (m_Headers
[1] != wxEmptyString
)
215 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
216 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
218 if (m_Footers
[0] != wxEmptyString
)
220 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
221 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
223 else if (m_Footers
[1] != wxEmptyString
)
225 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
226 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
229 /* prepare main renderer: */
230 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
231 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
232 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
233 m_FooterHeight
- m_HeaderHeight
-
234 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
235 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
237 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
243 bool wxHtmlPrintout::OnPrintPage(int page
)
249 RenderPage(dc
, page
);
256 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
259 *maxPage
= wxHTML_PRINT_MAX_PAGES
;
261 *selPageTo
= wxHTML_PRINT_MAX_PAGES
;
266 bool wxHtmlPrintout::HasPage(int pageNum
)
268 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
273 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
276 m_BasePath
= basepath
;
277 m_BasePathIsDir
= isdir
;
280 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
283 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
287 wxLogError(htmlfile
+ _(": file does not exist!"));
292 wxHtmlFilterHTML defaultFilter
;
295 wxList::compatibility_iterator node
= m_Filters
.GetFirst();
298 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
301 doc
= h
->ReadFile(*ff
);
305 node
= node
->GetNext();
309 doc
= defaultFilter
.ReadFile(*ff
);
311 SetHtmlText(doc
, htmlfile
, FALSE
);
317 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
319 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
320 m_Headers
[0] = header
;
321 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
322 m_Headers
[1] = header
;
327 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
329 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
330 m_Footers
[0] = footer
;
331 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
332 m_Footers
[1] = footer
;
337 void wxHtmlPrintout::CountPages()
340 int pageWidth
, pageHeight
, mm_w
, mm_h
;
341 float ppmm_h
, ppmm_v
;
343 GetPageSizePixels(&pageWidth
, &pageHeight
);
344 GetPageSizeMM(&mm_w
, &mm_h
);
345 ppmm_h
= (float)pageWidth
/ mm_w
;
346 ppmm_v
= (float)pageHeight
/ mm_h
;
355 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
356 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
357 pos
, TRUE
, INT_MAX
, m_PageBreaks
, m_NumPages
);
358 m_PageBreaks
[++m_NumPages
] = pos
;
359 } while (pos
< m_Renderer
->GetTotalHeight());
364 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
368 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
369 float ppmm_h
, ppmm_v
;
371 GetPageSizePixels(&pageWidth
, &pageHeight
);
372 GetPageSizeMM(&mm_w
, &mm_h
);
373 ppmm_h
= (float)pageWidth
/ mm_w
;
374 ppmm_v
= (float)pageHeight
/ mm_h
;
375 wxDisplaySize(&scr_w
, &scr_h
);
376 dc
->GetSize(&dc_w
, &dc_h
);
378 int ppiPrinterX
, ppiPrinterY
;
379 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
380 int ppiScreenX
, ppiScreenY
;
381 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
383 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
385 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
387 dc
->SetBackgroundMode(wxTRANSPARENT
);
389 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
390 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
391 m_PageBreaks
[page
-1], FALSE
, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
393 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
394 if (m_Headers
[page
% 2] != wxEmptyString
)
396 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
397 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
399 if (m_Footers
[page
% 2] != wxEmptyString
)
401 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
402 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
408 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
413 num
.Printf(wxT("%i"), page
);
414 r
.Replace(wxT("@PAGENUM@"), num
);
416 num
.Printf(wxT("%i"), m_NumPages
);
417 r
.Replace(wxT("@PAGESCNT@"), num
);
424 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
427 m_MarginBottom
= bottom
;
429 m_MarginRight
= right
;
430 m_MarginSpace
= spaces
;
436 void wxHtmlPrintout::SetFonts(wxString normal_face
, wxString fixed_face
,
439 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
440 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
445 //----------------------------------------------------------------------------
446 // wxHtmlEasyPrinting
447 //----------------------------------------------------------------------------
450 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
452 m_ParentWindow
= parentWindow
;
454 m_PrintData
= new wxPrintData
;
455 m_PageSetupData
= new wxPageSetupDialogData
;
456 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
458 m_PageSetupData
->EnableMargins(TRUE
);
459 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
460 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
462 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
467 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
470 delete m_PageSetupData
;
475 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
477 wxHtmlPrintout
*p1
= CreatePrintout();
478 p1
->SetHtmlFile(htmlfile
);
479 wxHtmlPrintout
*p2
= CreatePrintout();
480 p2
->SetHtmlFile(htmlfile
);
481 return DoPreview(p1
, p2
);
486 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
488 wxHtmlPrintout
*p1
= CreatePrintout();
489 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
490 wxHtmlPrintout
*p2
= CreatePrintout();
491 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
492 return DoPreview(p1
, p2
);
497 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
499 wxHtmlPrintout
*p
= CreatePrintout();
500 p
->SetHtmlFile(htmlfile
);
501 bool ret
= DoPrint(p
);
508 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
510 wxHtmlPrintout
*p
= CreatePrintout();
511 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
512 bool ret
= DoPrint(p
);
519 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
521 // Pass two printout objects: for preview, and possible printing.
522 wxPrintDialogData
printDialogData(*m_PrintData
);
523 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
530 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
531 m_Name
+ _(" Preview"),
532 wxPoint(100, 100), wxSize(650, 500));
533 frame
->Centre(wxBOTH
);
541 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
543 wxPrintDialogData
printDialogData(*m_PrintData
);
544 wxPrinter
printer(&printDialogData
);
546 if (!printer
.Print(m_ParentWindow
, printout
, TRUE
))
551 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
557 void wxHtmlEasyPrinting::PrinterSetup()
559 wxPrintDialogData
printDialogData(*m_PrintData
);
560 wxPrintDialog
printerDialog(m_ParentWindow
, &printDialogData
);
562 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
564 if (printerDialog
.ShowModal() == wxID_OK
)
565 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
570 void wxHtmlEasyPrinting::PageSetup()
572 if (!m_PrintData
->Ok())
574 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
578 m_PageSetupData
->SetPrintData(*m_PrintData
);
579 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
581 if (pageSetupDialog
.ShowModal() == wxID_OK
)
583 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
584 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
590 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
592 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
593 m_Headers
[0] = header
;
594 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
595 m_Headers
[1] = header
;
600 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
602 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
603 m_Footers
[0] = footer
;
604 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
605 m_Footers
[1] = footer
;
609 void wxHtmlEasyPrinting::SetFonts(wxString normal_face
, wxString fixed_face
,
612 m_FontFaceNormal
= normal_face
;
613 m_FontFaceFixed
= fixed_face
;
617 m_FontsSizes
= m_FontsSizesArr
;
618 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
625 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
627 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
629 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
631 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
632 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
633 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
634 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
636 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
637 m_PageSetupData
->GetMarginBottomRight().y
,
638 m_PageSetupData
->GetMarginTopLeft().x
,
639 m_PageSetupData
->GetMarginBottomRight().x
);
644 // A module to allow initialization/cleanup
645 // without calling these functions from app.cpp or from
646 // the user's application.
648 class wxHtmlPrintingModule
: public wxModule
650 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
652 wxHtmlPrintingModule() : wxModule() {}
653 bool OnInit() { return TRUE
; }
654 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
657 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
660 // This hack forces the linker to always link in m_* files
661 // (wxHTML doesn't work without handlers from these files)
662 #include "wx/html/forcelnk.h"
663 FORCE_WXHTML_MODULES()
665 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE