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"
29 #include "wx/printdlg.h"
30 #include "wx/html/htmprint.h"
31 #include "wx/wxhtml.h"
32 #include "wx/wfstream.h"
33 #include "wx/module.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("%i"), m_PageBreaks
.Count()-1);
444 r
.Replace(wxT("@PAGESCNT@"), num
);
451 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
454 m_MarginBottom
= bottom
;
456 m_MarginRight
= right
;
457 m_MarginSpace
= spaces
;
463 void wxHtmlPrintout::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
466 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
467 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
470 void wxHtmlPrintout::SetStandardFonts(int size
,
471 const wxString
& normal_face
,
472 const wxString
& fixed_face
)
474 m_Renderer
->SetStandardFonts(size
, normal_face
, fixed_face
);
475 m_RendererHdr
->SetStandardFonts(size
, normal_face
, fixed_face
);
480 //----------------------------------------------------------------------------
481 // wxHtmlEasyPrinting
482 //----------------------------------------------------------------------------
485 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
487 m_ParentWindow
= parentWindow
;
490 m_PageSetupData
= new wxPageSetupDialogData
;
491 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
493 m_PageSetupData
->EnableMargins(true);
494 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
495 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
497 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
502 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
505 delete m_PageSetupData
;
509 wxPrintData
*wxHtmlEasyPrinting::GetPrintData()
511 if (m_PrintData
== NULL
)
512 m_PrintData
= new wxPrintData();
517 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
519 wxHtmlPrintout
*p1
= CreatePrintout();
520 p1
->SetHtmlFile(htmlfile
);
521 wxHtmlPrintout
*p2
= CreatePrintout();
522 p2
->SetHtmlFile(htmlfile
);
523 return DoPreview(p1
, p2
);
528 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
530 wxHtmlPrintout
*p1
= CreatePrintout();
531 p1
->SetHtmlText(htmltext
, basepath
, true);
532 wxHtmlPrintout
*p2
= CreatePrintout();
533 p2
->SetHtmlText(htmltext
, basepath
, true);
534 return DoPreview(p1
, p2
);
539 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
541 wxHtmlPrintout
*p
= CreatePrintout();
542 p
->SetHtmlFile(htmlfile
);
543 bool ret
= DoPrint(p
);
550 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
552 wxHtmlPrintout
*p
= CreatePrintout();
553 p
->SetHtmlText(htmltext
, basepath
, true);
554 bool ret
= DoPrint(p
);
561 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
563 // Pass two printout objects: for preview, and possible printing.
564 wxPrintDialogData
printDialogData(*GetPrintData());
565 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
572 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
573 m_Name
+ _(" Preview"),
574 wxPoint(100, 100), wxSize(650, 500));
575 frame
->Centre(wxBOTH
);
583 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
585 wxPrintDialogData
printDialogData(*GetPrintData());
586 wxPrinter
printer(&printDialogData
);
588 if (!printer
.Print(m_ParentWindow
, printout
, true))
593 (*GetPrintData()) = printer
.GetPrintDialogData().GetPrintData();
600 void wxHtmlEasyPrinting::PageSetup()
602 if (!GetPrintData()->Ok())
604 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
608 m_PageSetupData
->SetPrintData(*GetPrintData());
609 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
611 if (pageSetupDialog
.ShowModal() == wxID_OK
)
613 (*GetPrintData()) = pageSetupDialog
.GetPageSetupData().GetPrintData();
614 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
620 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
622 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
623 m_Headers
[0] = header
;
624 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
625 m_Headers
[1] = header
;
630 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
632 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
633 m_Footers
[0] = footer
;
634 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
635 m_Footers
[1] = footer
;
639 void wxHtmlEasyPrinting::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
642 m_fontMode
= FontMode_Explicit
;
643 m_FontFaceNormal
= normal_face
;
644 m_FontFaceFixed
= fixed_face
;
648 m_FontsSizes
= m_FontsSizesArr
;
649 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
655 void wxHtmlEasyPrinting::SetStandardFonts(int size
,
656 const wxString
& normal_face
,
657 const wxString
& fixed_face
)
659 m_fontMode
= FontMode_Standard
;
660 m_FontFaceNormal
= normal_face
;
661 m_FontFaceFixed
= fixed_face
;
662 m_FontsSizesArr
[0] = size
;
666 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
668 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
670 if (m_fontMode
== FontMode_Explicit
)
672 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
674 else // FontMode_Standard
676 p
->SetStandardFonts(m_FontsSizesArr
[0],
677 m_FontFaceNormal
, m_FontFaceFixed
);
680 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
681 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
682 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
683 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
685 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
686 m_PageSetupData
->GetMarginBottomRight().y
,
687 m_PageSetupData
->GetMarginTopLeft().x
,
688 m_PageSetupData
->GetMarginBottomRight().x
);
693 // A module to allow initialization/cleanup
694 // without calling these functions from app.cpp or from
695 // the user's application.
697 class wxHtmlPrintingModule
: public wxModule
699 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule
)
701 wxHtmlPrintingModule() : wxModule() {}
702 bool OnInit() { return true; }
703 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
706 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule
, wxModule
)
709 // This hack forces the linker to always link in m_* files
710 // (wxHTML doesn't work without handlers from these files)
711 #include "wx/html/forcelnk.h"
712 FORCE_WXHTML_MODULES()
714 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE