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"
41 //--------------------------------------------------------------------------------
43 //--------------------------------------------------------------------------------
46 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
49 m_Width
= m_Height
= 0;
51 m_Parser
= new wxHtmlWinParser(NULL
);
52 m_FS
= new wxFileSystem();
53 m_Parser
->SetFS(m_FS
);
58 wxHtmlDCRenderer::~wxHtmlDCRenderer()
60 if (m_Cells
) delete m_Cells
;
61 if (m_Parser
) delete m_Parser
;
62 if (m_FS
) delete m_FS
;
67 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
70 m_Parser
->SetDC(m_DC
, pixel_scale
);
75 void wxHtmlDCRenderer::SetSize(int width
, int height
)
82 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
84 if (m_DC
== NULL
) return;
86 if (m_Cells
!= NULL
) delete m_Cells
;
88 m_FS
->ChangePathTo(basepath
, isdir
);
89 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
90 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
91 m_Cells
->Layout(m_Width
);
95 void wxHtmlDCRenderer::SetFonts(wxString normal_face
, wxString fixed_face
,
98 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
99 if (m_DC
== NULL
&& m_Cells
!= NULL
) m_Cells
->Layout(m_Width
);
103 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
, int to
, int *known_pagebreaks
, int number_of_pages
)
107 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
109 pbreak
= (int)(from
+ m_Height
);
110 while (m_Cells
->AdjustPagebreak(&pbreak
, known_pagebreaks
, number_of_pages
)) {}
111 hght
= pbreak
- from
;
117 wxHtmlRenderingInfo rinfo
;
118 wxDefaultHtmlRenderingStyle rstyle
;
119 rinfo
.SetStyle(&rstyle
);
120 m_DC
->SetBrush(*wxWHITE_BRUSH
);
121 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
124 y
, pbreak
+ (y
/*- from*/),
126 m_DC
->DestroyClippingRegion();
129 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
130 else return GetTotalHeight();
135 int wxHtmlDCRenderer::GetTotalHeight()
137 if (m_Cells
) return m_Cells
->GetHeight();
156 //--------------------------------------------------------------------------------
158 //--------------------------------------------------------------------------------
162 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
164 m_Renderer
= new wxHtmlDCRenderer
;
165 m_RendererHdr
= new wxHtmlDCRenderer
;
166 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
167 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
168 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
169 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
170 m_HeaderHeight
= m_FooterHeight
= 0;
171 SetMargins(); // to default values
176 wxHtmlPrintout::~wxHtmlPrintout()
179 delete m_RendererHdr
;
184 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
186 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
187 float ppmm_h
, ppmm_v
;
189 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
191 GetPageSizePixels(&pageWidth
, &pageHeight
);
192 GetPageSizeMM(&mm_w
, &mm_h
);
193 ppmm_h
= (float)pageWidth
/ mm_w
;
194 ppmm_v
= (float)pageHeight
/ mm_h
;
196 int ppiPrinterX
, ppiPrinterY
;
197 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
198 int ppiScreenX
, ppiScreenY
;
199 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
201 wxDisplaySize(&scr_w
, &scr_h
);
202 GetDC()->GetSize(&dc_w
, &dc_h
);
204 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
206 /* prepare headers/footers renderer: */
208 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
209 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
210 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
211 if (m_Headers
[0] != wxEmptyString
)
213 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
214 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
216 else if (m_Headers
[1] != wxEmptyString
)
218 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
219 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
221 if (m_Footers
[0] != wxEmptyString
)
223 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
224 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
226 else if (m_Footers
[1] != wxEmptyString
)
228 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
229 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
232 /* prepare main renderer: */
233 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
234 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
235 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
236 m_FooterHeight
- m_HeaderHeight
-
237 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
238 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
240 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
246 bool wxHtmlPrintout::OnPrintPage(int page
)
252 RenderPage(dc
, page
);
259 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
262 *maxPage
= wxHTML_PRINT_MAX_PAGES
;
264 *selPageTo
= wxHTML_PRINT_MAX_PAGES
;
269 bool wxHtmlPrintout::HasPage(int pageNum
)
271 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
276 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
279 m_BasePath
= basepath
;
280 m_BasePathIsDir
= isdir
;
283 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
286 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
290 wxLogError(htmlfile
+ _(": file does not exist!"));
294 wxHtmlFilterHTML filter
;
295 wxString doc
= filter
.ReadFile(*ff
);
297 SetHtmlText(doc
, htmlfile
, FALSE
);
303 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
305 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
306 m_Headers
[0] = header
;
307 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
308 m_Headers
[1] = header
;
313 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
315 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
316 m_Footers
[0] = footer
;
317 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
318 m_Footers
[1] = footer
;
323 void wxHtmlPrintout::CountPages()
326 int pageWidth
, pageHeight
, mm_w
, mm_h
;
327 float ppmm_h
, ppmm_v
;
329 GetPageSizePixels(&pageWidth
, &pageHeight
);
330 GetPageSizeMM(&mm_w
, &mm_h
);
331 ppmm_h
= (float)pageWidth
/ mm_w
;
332 ppmm_v
= (float)pageHeight
/ mm_h
;
341 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
342 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
343 pos
, TRUE
, INT_MAX
, m_PageBreaks
, m_NumPages
);
344 m_PageBreaks
[++m_NumPages
] = pos
;
345 } while (pos
< m_Renderer
->GetTotalHeight());
350 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
354 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
355 float ppmm_h
, ppmm_v
;
357 GetPageSizePixels(&pageWidth
, &pageHeight
);
358 GetPageSizeMM(&mm_w
, &mm_h
);
359 ppmm_h
= (float)pageWidth
/ mm_w
;
360 ppmm_v
= (float)pageHeight
/ mm_h
;
361 wxDisplaySize(&scr_w
, &scr_h
);
362 dc
->GetSize(&dc_w
, &dc_h
);
364 int ppiPrinterX
, ppiPrinterY
;
365 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
366 int ppiScreenX
, ppiScreenY
;
367 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
369 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
371 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
373 dc
->SetBackgroundMode(wxTRANSPARENT
);
375 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
376 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
377 m_PageBreaks
[page
-1], FALSE
, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
379 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
380 if (m_Headers
[page
% 2] != wxEmptyString
)
382 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
383 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
385 if (m_Footers
[page
% 2] != wxEmptyString
)
387 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
388 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
394 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
399 num
.Printf(wxT("%i"), page
);
400 r
.Replace(wxT("@PAGENUM@"), num
);
402 num
.Printf(wxT("%i"), m_NumPages
);
403 r
.Replace(wxT("@PAGESCNT@"), num
);
410 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
413 m_MarginBottom
= bottom
;
415 m_MarginRight
= right
;
416 m_MarginSpace
= spaces
;
422 void wxHtmlPrintout::SetFonts(wxString normal_face
, wxString fixed_face
,
425 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
426 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
431 //----------------------------------------------------------------------------
432 // wxHtmlEasyPrinting
433 //----------------------------------------------------------------------------
436 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
438 m_ParentWindow
= parentWindow
;
440 m_PrintData
= new wxPrintData
;
441 m_PageSetupData
= new wxPageSetupDialogData
;
442 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
444 m_PageSetupData
->EnableMargins(TRUE
);
445 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
446 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
448 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
453 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
456 delete m_PageSetupData
;
461 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
463 wxHtmlPrintout
*p1
= CreatePrintout();
464 p1
->SetHtmlFile(htmlfile
);
465 wxHtmlPrintout
*p2
= CreatePrintout();
466 p2
->SetHtmlFile(htmlfile
);
467 return DoPreview(p1
, p2
);
472 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
474 wxHtmlPrintout
*p1
= CreatePrintout();
475 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
476 wxHtmlPrintout
*p2
= CreatePrintout();
477 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
478 return DoPreview(p1
, p2
);
483 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
485 wxHtmlPrintout
*p
= CreatePrintout();
486 p
->SetHtmlFile(htmlfile
);
487 bool ret
= DoPrint(p
);
494 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
496 wxHtmlPrintout
*p
= CreatePrintout();
497 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
498 bool ret
= DoPrint(p
);
505 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
507 // Pass two printout objects: for preview, and possible printing.
508 wxPrintDialogData
printDialogData(*m_PrintData
);
509 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
516 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
517 m_Name
+ _(" Preview"),
518 wxPoint(100, 100), wxSize(650, 500));
519 frame
->Centre(wxBOTH
);
527 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
529 wxPrintDialogData
printDialogData(*m_PrintData
);
530 wxPrinter
printer(&printDialogData
);
532 if (!printer
.Print(m_ParentWindow
, printout
, TRUE
))
537 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
543 void wxHtmlEasyPrinting::PrinterSetup()
545 wxPrintDialogData
printDialogData(*m_PrintData
);
546 wxPrintDialog
printerDialog(m_ParentWindow
, &printDialogData
);
548 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
550 if (printerDialog
.ShowModal() == wxID_OK
)
551 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
556 void wxHtmlEasyPrinting::PageSetup()
558 if (!m_PrintData
->Ok())
560 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
564 m_PageSetupData
->SetPrintData(*m_PrintData
);
565 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
567 if (pageSetupDialog
.ShowModal() == wxID_OK
)
569 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
570 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
576 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
578 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
579 m_Headers
[0] = header
;
580 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
581 m_Headers
[1] = header
;
586 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
588 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
589 m_Footers
[0] = footer
;
590 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
591 m_Footers
[1] = footer
;
595 void wxHtmlEasyPrinting::SetFonts(wxString normal_face
, wxString fixed_face
,
598 m_FontFaceNormal
= normal_face
;
599 m_FontFaceFixed
= fixed_face
;
603 m_FontsSizes
= m_FontsSizesArr
;
604 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
611 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
613 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
615 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
617 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
618 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
619 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
620 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
622 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
623 m_PageSetupData
->GetMarginBottomRight().y
,
624 m_PageSetupData
->GetMarginTopLeft().x
,
625 m_PageSetupData
->GetMarginBottomRight().x
);
631 // This hack forces the linker to always link in m_* files
632 // (wxHTML doesn't work without handlers from these files)
633 #include "wx/html/forcelnk.h"
634 FORCE_WXHTML_MODULES()
636 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE