]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmprint.cpp
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 m_DC
->SetBrush(*wxWHITE_BRUSH
);
119 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
122 y
, pbreak
+ (y
/*- from*/));
123 m_DC
->DestroyClippingRegion();
126 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
127 else return GetTotalHeight();
132 int wxHtmlDCRenderer::GetTotalHeight()
134 if (m_Cells
) return m_Cells
->GetHeight();
153 //--------------------------------------------------------------------------------
155 //--------------------------------------------------------------------------------
159 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
161 m_Renderer
= new wxHtmlDCRenderer
;
162 m_RendererHdr
= new wxHtmlDCRenderer
;
163 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
164 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
165 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
166 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
167 m_HeaderHeight
= m_FooterHeight
= 0;
168 SetMargins(); // to default values
173 wxHtmlPrintout::~wxHtmlPrintout()
176 delete m_RendererHdr
;
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!"));
291 wxHtmlFilterHTML filter
;
292 wxString doc
= filter
.ReadFile(*ff
);
294 SetHtmlText(doc
, htmlfile
, FALSE
);
300 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
302 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
303 m_Headers
[0] = header
;
304 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
305 m_Headers
[1] = header
;
310 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
312 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
313 m_Footers
[0] = footer
;
314 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
315 m_Footers
[1] = footer
;
320 void wxHtmlPrintout::CountPages()
323 int pageWidth
, pageHeight
, mm_w
, mm_h
;
324 float ppmm_h
, ppmm_v
;
326 GetPageSizePixels(&pageWidth
, &pageHeight
);
327 GetPageSizeMM(&mm_w
, &mm_h
);
328 ppmm_h
= (float)pageWidth
/ mm_w
;
329 ppmm_v
= (float)pageHeight
/ mm_h
;
338 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
339 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
340 pos
, TRUE
, INT_MAX
, m_PageBreaks
, m_NumPages
);
341 m_PageBreaks
[++m_NumPages
] = pos
;
342 } while (pos
< m_Renderer
->GetTotalHeight());
347 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
351 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
352 float ppmm_h
, ppmm_v
;
354 GetPageSizePixels(&pageWidth
, &pageHeight
);
355 GetPageSizeMM(&mm_w
, &mm_h
);
356 ppmm_h
= (float)pageWidth
/ mm_w
;
357 ppmm_v
= (float)pageHeight
/ mm_h
;
358 wxDisplaySize(&scr_w
, &scr_h
);
359 dc
->GetSize(&dc_w
, &dc_h
);
361 int ppiPrinterX
, ppiPrinterY
;
362 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
363 int ppiScreenX
, ppiScreenY
;
364 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
366 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
368 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
370 dc
->SetBackgroundMode(wxTRANSPARENT
);
372 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
373 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
374 m_PageBreaks
[page
-1], FALSE
, m_PageBreaks
[page
]-m_PageBreaks
[page
-1]);
376 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
377 if (m_Headers
[page
% 2] != wxEmptyString
)
379 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
380 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
382 if (m_Footers
[page
% 2] != wxEmptyString
)
384 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
385 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
391 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
396 num
.Printf(wxT("%i"), page
);
397 r
.Replace(wxT("@PAGENUM@"), num
);
399 num
.Printf(wxT("%i"), m_NumPages
);
400 r
.Replace(wxT("@PAGESCNT@"), num
);
407 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
410 m_MarginBottom
= bottom
;
412 m_MarginRight
= right
;
413 m_MarginSpace
= spaces
;
419 void wxHtmlPrintout::SetFonts(wxString normal_face
, wxString fixed_face
,
422 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
423 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
428 //----------------------------------------------------------------------------
429 // wxHtmlEasyPrinting
430 //----------------------------------------------------------------------------
433 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxWindow
*parentWindow
)
435 m_ParentWindow
= parentWindow
;
437 m_PrintData
= new wxPrintData
;
438 m_PageSetupData
= new wxPageSetupDialogData
;
439 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
441 m_PageSetupData
->EnableMargins(TRUE
);
442 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
443 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
445 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
450 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
453 delete m_PageSetupData
;
458 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
460 wxHtmlPrintout
*p1
= CreatePrintout();
461 p1
->SetHtmlFile(htmlfile
);
462 wxHtmlPrintout
*p2
= CreatePrintout();
463 p2
->SetHtmlFile(htmlfile
);
464 return DoPreview(p1
, p2
);
469 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
471 wxHtmlPrintout
*p1
= CreatePrintout();
472 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
473 wxHtmlPrintout
*p2
= CreatePrintout();
474 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
475 return DoPreview(p1
, p2
);
480 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
482 wxHtmlPrintout
*p
= CreatePrintout();
483 p
->SetHtmlFile(htmlfile
);
484 bool ret
= DoPrint(p
);
491 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
493 wxHtmlPrintout
*p
= CreatePrintout();
494 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
495 bool ret
= DoPrint(p
);
502 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
504 // Pass two printout objects: for preview, and possible printing.
505 wxPrintDialogData
printDialogData(*m_PrintData
);
506 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
513 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_ParentWindow
,
514 m_Name
+ _(" Preview"),
515 wxPoint(100, 100), wxSize(650, 500));
516 frame
->Centre(wxBOTH
);
524 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
526 wxPrintDialogData
printDialogData(*m_PrintData
);
527 wxPrinter
printer(&printDialogData
);
529 if (!printer
.Print(m_ParentWindow
, printout
, TRUE
))
534 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
540 void wxHtmlEasyPrinting::PrinterSetup()
542 wxPrintDialogData
printDialogData(*m_PrintData
);
543 wxPrintDialog
printerDialog(m_ParentWindow
, &printDialogData
);
545 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
547 if (printerDialog
.ShowModal() == wxID_OK
)
548 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
553 void wxHtmlEasyPrinting::PageSetup()
555 if (!m_PrintData
->Ok())
557 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
561 m_PageSetupData
->SetPrintData(*m_PrintData
);
562 wxPageSetupDialog
pageSetupDialog(m_ParentWindow
, m_PageSetupData
);
564 if (pageSetupDialog
.ShowModal() == wxID_OK
)
566 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
567 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
573 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
575 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
576 m_Headers
[0] = header
;
577 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
578 m_Headers
[1] = header
;
583 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
585 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
586 m_Footers
[0] = footer
;
587 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
588 m_Footers
[1] = footer
;
592 void wxHtmlEasyPrinting::SetFonts(wxString normal_face
, wxString fixed_face
,
595 m_FontFaceNormal
= normal_face
;
596 m_FontFaceFixed
= fixed_face
;
600 m_FontsSizes
= m_FontsSizesArr
;
601 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
608 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
610 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
612 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
614 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
615 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
616 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
617 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
619 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
620 m_PageSetupData
->GetMarginBottomRight().y
,
621 m_PageSetupData
->GetMarginTopLeft().x
,
622 m_PageSetupData
->GetMarginBottomRight().x
);
628 // This hack forces the linker to always link in m_* files
629 // (wxHTML doesn't work without handlers from these files)
630 #include "wx/html/forcelnk.h"
631 FORCE_WXHTML_MODULES()
633 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE