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 //--------------------------------------------------------------------------------
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
);
107 int wxHtmlDCRenderer::Render(int x
, int y
,
108 wxArrayInt
& known_pagebreaks
,
109 int from
, int dont_render
, int to
)
113 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
115 pbreak
= (int)(from
+ m_Height
);
116 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
)) {}
117 hght
= pbreak
- from
;
123 wxHtmlRenderingInfo rinfo
;
124 wxDefaultHtmlRenderingStyle rstyle
;
125 rinfo
.SetStyle(&rstyle
);
126 m_DC
->SetBrush(*wxWHITE_BRUSH
);
127 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
130 y
, pbreak
+ (y
/*- from*/),
132 m_DC
->DestroyClippingRegion();
135 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
136 else return GetTotalHeight();
140 int wxHtmlDCRenderer::GetTotalHeight()
142 if (m_Cells
) return m_Cells
->GetHeight();
147 //--------------------------------------------------------------------------------
149 //--------------------------------------------------------------------------------
152 wxList
wxHtmlPrintout::m_Filters
;
154 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
156 m_Renderer
= new wxHtmlDCRenderer
;
157 m_RendererHdr
= new wxHtmlDCRenderer
;
158 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
159 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= true;
160 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
161 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
162 m_HeaderHeight
= m_FooterHeight
= 0;
163 SetMargins(); // to default values
168 wxHtmlPrintout::~wxHtmlPrintout()
171 delete m_RendererHdr
;
174 void wxHtmlPrintout::CleanUpStatics()
176 WX_CLEAR_LIST(wxList
, m_Filters
);
180 void wxHtmlPrintout::AddFilter(wxHtmlFilter
*filter
)
182 m_Filters
.Append(filter
);
185 void wxHtmlPrintout::OnPreparePrinting()
187 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
188 float ppmm_h
, ppmm_v
;
190 GetPageSizePixels(&pageWidth
, &pageHeight
);
191 GetPageSizeMM(&mm_w
, &mm_h
);
192 ppmm_h
= (float)pageWidth
/ mm_w
;
193 ppmm_v
= (float)pageHeight
/ mm_h
;
195 int ppiPrinterX
, ppiPrinterY
;
196 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
197 int ppiScreenX
, ppiScreenY
;
198 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
200 wxDisplaySize(&scr_w
, &scr_h
);
201 GetDC()->GetSize(&dc_w
, &dc_h
);
203 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
205 /* prepare headers/footers renderer: */
207 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
208 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
209 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
210 if (m_Headers
[0] != wxEmptyString
)
212 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
213 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
215 else if (m_Headers
[1] != wxEmptyString
)
217 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
218 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
220 if (m_Footers
[0] != wxEmptyString
)
222 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
223 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
225 else if (m_Footers
[1] != wxEmptyString
)
227 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
228 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
231 /* prepare main renderer: */
232 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
233 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
234 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
235 m_FooterHeight
- m_HeaderHeight
-
236 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
237 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
239 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
243 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
245 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return false;
251 bool wxHtmlPrintout::OnPrintPage(int page
)
257 RenderPage(dc
, page
);
264 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
267 if ( m_NumPages
>= (signed)m_PageBreaks
.Count()-1)
268 *maxPage
= m_NumPages
;
270 *maxPage
= (signed)m_PageBreaks
.Count()-1;
272 *selPageTo
= (signed)m_PageBreaks
.Count()-1;
277 bool wxHtmlPrintout::HasPage(int pageNum
)
279 return (pageNum
>= 1 && pageNum
-1 <= (signed)m_PageBreaks
.Count());
284 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
287 m_BasePath
= basepath
;
288 m_BasePathIsDir
= isdir
;
291 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
296 if (wxFileExists(htmlfile
))
297 ff
= fs
.OpenFile(wxFileSystem::FileNameToURL(htmlfile
));
299 ff
= fs
.OpenFile(htmlfile
);
303 wxLogError(htmlfile
+ _(": file does not exist!"));
308 wxHtmlFilterHTML defaultFilter
;
311 wxList::compatibility_iterator node
= m_Filters
.GetFirst();
314 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
317 doc
= h
->ReadFile(*ff
);
321 node
= node
->GetNext();
325 doc
= defaultFilter
.ReadFile(*ff
);
327 SetHtmlText(doc
, htmlfile
, false);
333 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
335 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
336 m_Headers
[0] = header
;
337 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
338 m_Headers
[1] = header
;
343 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
345 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
346 m_Footers
[0] = footer
;
347 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
348 m_Footers
[1] = footer
;
353 void wxHtmlPrintout::CountPages()
356 int pageWidth
, pageHeight
, mm_w
, mm_h
;
357 float ppmm_h
, ppmm_v
;
359 GetPageSizePixels(&pageWidth
, &pageHeight
);
360 GetPageSizeMM(&mm_w
, &mm_h
);
361 ppmm_h
= (float)pageWidth
/ mm_w
;
362 ppmm_v
= (float)pageHeight
/ mm_h
;
366 // m_PageBreaks[0] = 0;
368 m_PageBreaks
.Clear();
369 m_PageBreaks
.Add( 0);
372 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
373 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
376 m_PageBreaks
.Add( pos
);
377 if( m_PageBreaks
.Count() > wxHTML_PRINT_MAX_PAGES
)
379 wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can continue any longer!"),
380 _("Warning"), wxCANCEL
| wxICON_ERROR
);
383 } while (pos
< m_Renderer
->GetTotalHeight());
388 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
392 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
393 float ppmm_h
, ppmm_v
;
395 GetPageSizePixels(&pageWidth
, &pageHeight
);
396 GetPageSizeMM(&mm_w
, &mm_h
);
397 ppmm_h
= (float)pageWidth
/ mm_w
;
398 ppmm_v
= (float)pageHeight
/ mm_h
;
399 wxDisplaySize(&scr_w
, &scr_h
);
400 dc
->GetSize(&dc_w
, &dc_h
);
402 int ppiPrinterX
, ppiPrinterY
;
403 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
404 wxUnusedVar(ppiPrinterX
);
405 int ppiScreenX
, ppiScreenY
;
406 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
407 wxUnusedVar(ppiScreenX
);
409 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
411 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
413 dc
->SetBackgroundMode(wxTRANSPARENT
);
415 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
416 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
), m_PageBreaks
,
417 m_PageBreaks
[page
-1], false, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
420 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
421 if (m_Headers
[page
% 2] != wxEmptyString
)
423 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
424 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
), m_PageBreaks
);
426 if (m_Footers
[page
% 2] != wxEmptyString
)
428 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
429 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
), m_PageBreaks
);
435 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
440 num
.Printf(wxT("%i"), page
);
441 r
.Replace(wxT("@PAGENUM@"), num
);
443 num
.Printf(wxT("%lu"), (unsigned long)(m_PageBreaks
.Count() - 1));
444 r
.Replace(wxT("@PAGESCNT@"), num
);
446 const wxDateTime now
= wxDateTime::Now();
447 r
.Replace(wxT("@DATE@"), now
.FormatDate());
448 r
.Replace(wxT("@TIME@"), now
.FormatTime());
450 r
.Replace(wxT("@TITLE@"), GetTitle());
457 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
460 m_MarginBottom
= bottom
;
462 m_MarginRight
= right
;
463 m_MarginSpace
= spaces
;
469 void wxHtmlPrintout::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
472 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
473 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
476 void wxHtmlPrintout::SetStandardFonts(int size
,
477 const wxString
& normal_face
,
478 const wxString
& fixed_face
)
480 m_Renderer
->SetStandardFonts(size
, normal_face
, fixed_face
);
481 m_RendererHdr
->SetStandardFonts(size
, normal_face
, fixed_face
);
486 //----------------------------------------------------------------------------
487 // wxHtmlEasyPrinting
488 //----------------------------------------------------------------------------
491 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
493 m_ParentWindow
= parentWindow
;
496 m_PageSetupData
= new wxPageSetupDialogData
;
497 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
499 m_PageSetupData
->EnableMargins(true);
500 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
501 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
503 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
508 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
511 delete m_PageSetupData
;
515 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
517 if (m_PrintData
== NULL
)
518 m_PrintData
= new wxPrintData();
523 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
525 wxHtmlPrintout
*p1
= CreatePrintout();
526 p1
->SetHtmlFile(htmlfile
);
527 wxHtmlPrintout
*p2
= CreatePrintout();
528 p2
->SetHtmlFile(htmlfile
);
529 return DoPreview(p1
, p2
);
534 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
536 wxHtmlPrintout
*p1
= CreatePrintout();
537 p1
->SetHtmlText(htmltext
, basepath
, true);
538 wxHtmlPrintout
*p2
= CreatePrintout();
539 p2
->SetHtmlText(htmltext
, basepath
, true);
540 return DoPreview(p1
, p2
);
545 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
547 wxHtmlPrintout
*p
= CreatePrintout();
548 p
->SetHtmlFile(htmlfile
);
549 bool ret
= DoPrint(p
);
556 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
558 wxHtmlPrintout
*p
= CreatePrintout();
559 p
->SetHtmlText(htmltext
, basepath
, true);
560 bool ret
= DoPrint(p
);
567 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
569 // Pass two printout objects: for preview, and possible printing.
570 wxPrintDialogData
printDialogData(*GetPrintData());
571 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
578 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
579 m_Name
+ _(" Preview"),
580 wxPoint(100, 100), wxSize(650, 500));
581 frame
->Centre(wxBOTH
);
589 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
591 wxPrintDialogData
printDialogData(*GetPrintData());
592 wxPrinter
printer(&printDialogData
);
594 if (!printer
.Print(m_ParentWindow
, printout
, true))
599 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
606 void wxHtmlEasyPrinting::PageSetup()
608 if (!GetPrintData()->Ok())
610 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
614 m_PageSetupData
->SetPrintData(*GetPrintData());
615 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
617 if (pageSetupDialog
.ShowModal() == wxID_OK
)
619 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
620 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
626 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
628 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
629 m_Headers
[0] = header
;
630 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
631 m_Headers
[1] = header
;
636 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
638 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
639 m_Footers
[0] = footer
;
640 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
641 m_Footers
[1] = footer
;
645 void wxHtmlEasyPrinting::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
648 m_fontMode
= FontMode_Explicit
;
649 m_FontFaceNormal
= normal_face
;
650 m_FontFaceFixed
= fixed_face
;
654 m_FontsSizes
= m_FontsSizesArr
;
655 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
661 void wxHtmlEasyPrinting::SetStandardFonts(int size
,
662 const wxString
& normal_face
,
663 const wxString
& fixed_face
)
665 m_fontMode
= FontMode_Standard
;
666 m_FontFaceNormal
= normal_face
;
667 m_FontFaceFixed
= fixed_face
;
668 m_FontsSizesArr
[0] = size
;
672 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
674 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
676 if (m_fontMode
== FontMode_Explicit
)
678 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
680 else // FontMode_Standard
682 p
->SetStandardFonts(m_FontsSizesArr
[0],
683 m_FontFaceNormal
, m_FontFaceFixed
);
686 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
687 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
688 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
689 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
691 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
692 m_PageSetupData
->GetMarginBottomRight().y
,
693 m_PageSetupData
->GetMarginTopLeft().x
,
694 m_PageSetupData
->GetMarginBottomRight().x
);
699 // A module to allow initialization/cleanup
700 // without calling these functions from app.cpp or from
701 // the user's application.
703 class wxHtmlPrintingModule
: public wxModule
705 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
707 wxHtmlPrintingModule() : wxModule() {}
708 bool OnInit() { return true; }
709 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
712 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
715 // This hack forces the linker to always link in m_* files
716 // (wxHTML doesn't work without handlers from these files)
717 #include "wx/html/forcelnk.h"
718 FORCE_WXHTML_MODULES()
720 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE