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"
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 void wxHtmlPrintout::OnPreparePrinting()
183 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
184 float ppmm_h
, ppmm_v
;
186 GetPageSizePixels(&pageWidth
, &pageHeight
);
187 GetPageSizeMM(&mm_w
, &mm_h
);
188 ppmm_h
= (float)pageWidth
/ mm_w
;
189 ppmm_v
= (float)pageHeight
/ mm_h
;
191 int ppiPrinterX
, ppiPrinterY
;
192 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
193 int ppiScreenX
, ppiScreenY
;
194 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
196 wxDisplaySize(&scr_w
, &scr_h
);
197 GetDC()->GetSize(&dc_w
, &dc_h
);
199 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
201 /* prepare headers/footers renderer: */
203 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
204 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
205 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
206 if (m_Headers
[0] != wxEmptyString
)
208 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
209 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
211 else if (m_Headers
[1] != wxEmptyString
)
213 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
214 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
216 if (m_Footers
[0] != wxEmptyString
)
218 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
219 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
221 else if (m_Footers
[1] != wxEmptyString
)
223 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
224 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
227 /* prepare main renderer: */
228 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
229 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
230 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
231 m_FooterHeight
- m_HeaderHeight
-
232 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
233 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
235 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
239 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
241 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
247 bool wxHtmlPrintout::OnPrintPage(int page
)
253 RenderPage(dc
, page
);
260 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
263 *maxPage
= m_NumPages
;
265 *selPageTo
= m_NumPages
;
270 bool wxHtmlPrintout::HasPage(int pageNum
)
272 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
277 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
280 m_BasePath
= basepath
;
281 m_BasePathIsDir
= isdir
;
284 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
287 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
291 wxLogError(htmlfile
+ _(": file does not exist!"));
296 wxHtmlFilterHTML defaultFilter
;
299 wxList::compatibility_iterator node
= m_Filters
.GetFirst();
302 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
305 doc
= h
->ReadFile(*ff
);
309 node
= node
->GetNext();
313 doc
= defaultFilter
.ReadFile(*ff
);
315 SetHtmlText(doc
, htmlfile
, FALSE
);
321 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
323 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
324 m_Headers
[0] = header
;
325 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
326 m_Headers
[1] = header
;
331 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
333 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
334 m_Footers
[0] = footer
;
335 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
336 m_Footers
[1] = footer
;
341 void wxHtmlPrintout::CountPages()
344 int pageWidth
, pageHeight
, mm_w
, mm_h
;
345 float ppmm_h
, ppmm_v
;
347 GetPageSizePixels(&pageWidth
, &pageHeight
);
348 GetPageSizeMM(&mm_w
, &mm_h
);
349 ppmm_h
= (float)pageWidth
/ mm_w
;
350 ppmm_v
= (float)pageHeight
/ mm_h
;
359 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
360 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
361 pos
, TRUE
, INT_MAX
, m_PageBreaks
, m_NumPages
);
362 m_PageBreaks
[++m_NumPages
] = pos
;
363 } while (pos
< m_Renderer
->GetTotalHeight());
368 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
372 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
373 float ppmm_h
, ppmm_v
;
375 GetPageSizePixels(&pageWidth
, &pageHeight
);
376 GetPageSizeMM(&mm_w
, &mm_h
);
377 ppmm_h
= (float)pageWidth
/ mm_w
;
378 ppmm_v
= (float)pageHeight
/ mm_h
;
379 wxDisplaySize(&scr_w
, &scr_h
);
380 dc
->GetSize(&dc_w
, &dc_h
);
382 int ppiPrinterX
, ppiPrinterY
;
383 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
384 wxUnusedVar(ppiPrinterX
);
385 int ppiScreenX
, ppiScreenY
;
386 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
387 wxUnusedVar(ppiScreenX
);
389 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
391 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
393 dc
->SetBackgroundMode(wxTRANSPARENT
);
395 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
396 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
397 m_PageBreaks
[page
-1], FALSE
, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
399 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
400 if (m_Headers
[page
% 2] != wxEmptyString
)
402 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
403 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
405 if (m_Footers
[page
% 2] != wxEmptyString
)
407 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
408 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
414 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
419 num
.Printf(wxT("%i"), page
);
420 r
.Replace(wxT("@PAGENUM@"), num
);
422 num
.Printf(wxT("%i"), m_NumPages
);
423 r
.Replace(wxT("@PAGESCNT@"), num
);
430 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
433 m_MarginBottom
= bottom
;
435 m_MarginRight
= right
;
436 m_MarginSpace
= spaces
;
442 void wxHtmlPrintout::SetFonts(wxString normal_face
, wxString fixed_face
,
445 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
446 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
451 //----------------------------------------------------------------------------
452 // wxHtmlEasyPrinting
453 //----------------------------------------------------------------------------
456 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
458 m_ParentWindow
= parentWindow
;
461 m_PageSetupData
= new wxPageSetupDialogData
;
462 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
464 m_PageSetupData
->EnableMargins(TRUE
);
465 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
466 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
468 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
473 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
476 delete m_PageSetupData
;
480 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
482 if (m_PrintData
== NULL
)
483 m_PrintData
= new wxPrintData();
488 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
490 wxHtmlPrintout
*p1
= CreatePrintout();
491 p1
->SetHtmlFile(htmlfile
);
492 wxHtmlPrintout
*p2
= CreatePrintout();
493 p2
->SetHtmlFile(htmlfile
);
494 return DoPreview(p1
, p2
);
499 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
501 wxHtmlPrintout
*p1
= CreatePrintout();
502 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
503 wxHtmlPrintout
*p2
= CreatePrintout();
504 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
505 return DoPreview(p1
, p2
);
510 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
512 wxHtmlPrintout
*p
= CreatePrintout();
513 p
->SetHtmlFile(htmlfile
);
514 bool ret
= DoPrint(p
);
521 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
523 wxHtmlPrintout
*p
= CreatePrintout();
524 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
525 bool ret
= DoPrint(p
);
532 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
534 // Pass two printout objects: for preview, and possible printing.
535 wxPrintDialogData
printDialogData(*GetPrintData());
536 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
543 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
544 m_Name
+ _(" Preview"),
545 wxPoint(100, 100), wxSize(650, 500));
546 frame
->Centre(wxBOTH
);
554 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
556 wxPrintDialogData
printDialogData(*GetPrintData());
557 wxPrinter
printer(&printDialogData
);
559 if (!printer
.Print(m_ParentWindow
, printout
, TRUE
))
564 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
570 void wxHtmlEasyPrinting::PrinterSetup()
572 wxPrintDialogData
printDialogData(*GetPrintData());
573 wxPrintDialog
printerDialog(m_ParentWindow
, &printDialogData
);
575 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
577 if (printerDialog
.ShowModal() == wxID_OK
)
578 (*GetPrintData()) = printerDialog
.GetPrintDialogData().GetPrintData();
583 void wxHtmlEasyPrinting::PageSetup()
585 if (!GetPrintData()->Ok())
587 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
591 m_PageSetupData
->SetPrintData(*GetPrintData());
592 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
594 if (pageSetupDialog
.ShowModal() == wxID_OK
)
596 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
597 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
603 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
605 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
606 m_Headers
[0] = header
;
607 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
608 m_Headers
[1] = header
;
613 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
615 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
616 m_Footers
[0] = footer
;
617 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
618 m_Footers
[1] = footer
;
622 void wxHtmlEasyPrinting::SetFonts(wxString normal_face
, wxString fixed_face
,
625 m_FontFaceNormal
= normal_face
;
626 m_FontFaceFixed
= fixed_face
;
630 m_FontsSizes
= m_FontsSizesArr
;
631 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
638 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
640 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
642 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
644 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
645 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
646 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
647 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
649 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
650 m_PageSetupData
->GetMarginBottomRight().y
,
651 m_PageSetupData
->GetMarginTopLeft().x
,
652 m_PageSetupData
->GetMarginBottomRight().x
);
657 // A module to allow initialization/cleanup
658 // without calling these functions from app.cpp or from
659 // the user's application.
661 class wxHtmlPrintingModule
: public wxModule
663 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
665 wxHtmlPrintingModule() : wxModule() {}
666 bool OnInit() { return TRUE
; }
667 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
670 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
673 // This hack forces the linker to always link in m_* files
674 // (wxHTML doesn't work without handlers from these files)
675 #include "wx/html/forcelnk.h"
676 FORCE_WXHTML_MODULES()
678 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE