]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmprint.cpp
62ff13af35a256346ec6098082708f8b9224259b
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
)
107 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
109 pbreak
= (int)(from
+ m_Height
);
110 while (m_Cells
->AdjustPagebreak(&pbreak
)) {}
111 hght
= pbreak
- from
;
115 m_DC
->SetBrush(*wxWHITE_BRUSH
);
117 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
120 y
, pbreak
+ (y
/*- from*/));
121 m_DC
->DestroyClippingRegion();
124 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
125 else return GetTotalHeight();
130 int wxHtmlDCRenderer::GetTotalHeight()
132 if (m_Cells
) return m_Cells
->GetHeight();
151 //--------------------------------------------------------------------------------
153 //--------------------------------------------------------------------------------
157 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
159 m_Renderer
= new wxHtmlDCRenderer
;
160 m_RendererHdr
= new wxHtmlDCRenderer
;
161 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
162 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
163 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
164 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
165 m_HeaderHeight
= m_FooterHeight
= 0;
166 SetMargins(); // to default values
171 wxHtmlPrintout::~wxHtmlPrintout()
174 delete m_RendererHdr
;
179 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
181 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
182 float ppmm_h
, ppmm_v
;
184 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
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
);
241 bool wxHtmlPrintout::OnPrintPage(int page
)
247 RenderPage(dc
, page
);
254 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
257 *maxPage
= wxHTML_PRINT_MAX_PAGES
;
259 *selPageTo
= wxHTML_PRINT_MAX_PAGES
;
264 bool wxHtmlPrintout::HasPage(int pageNum
)
266 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
271 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
274 m_BasePath
= basepath
;
275 m_BasePathIsDir
= isdir
;
278 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
281 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
285 wxLogError(htmlfile
+ _(": file does not exist!"));
289 wxHtmlFilterHTML filter
;
290 wxString doc
= filter
.ReadFile(*ff
);
292 SetHtmlText(doc
, htmlfile
, FALSE
);
298 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
300 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
301 m_Headers
[0] = header
;
302 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
303 m_Headers
[1] = header
;
308 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
310 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
311 m_Footers
[0] = footer
;
312 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
313 m_Footers
[1] = footer
;
318 void wxHtmlPrintout::CountPages()
321 int pageWidth
, pageHeight
, mm_w
, mm_h
;
322 float ppmm_h
, ppmm_v
;
324 GetPageSizePixels(&pageWidth
, &pageHeight
);
325 GetPageSizeMM(&mm_w
, &mm_h
);
326 ppmm_h
= (float)pageWidth
/ mm_w
;
327 ppmm_v
= (float)pageHeight
/ mm_h
;
336 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
337 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
339 m_PageBreaks
[++m_NumPages
] = pos
;
340 } while (pos
< m_Renderer
->GetTotalHeight());
345 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
349 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
350 float ppmm_h
, ppmm_v
;
352 GetPageSizePixels(&pageWidth
, &pageHeight
);
353 GetPageSizeMM(&mm_w
, &mm_h
);
354 ppmm_h
= (float)pageWidth
/ mm_w
;
355 ppmm_v
= (float)pageHeight
/ mm_h
;
356 wxDisplaySize(&scr_w
, &scr_h
);
357 dc
->GetSize(&dc_w
, &dc_h
);
359 int ppiPrinterX
, ppiPrinterY
;
360 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
361 int ppiScreenX
, ppiScreenY
;
362 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
364 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
366 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
368 dc
->SetBackgroundMode(wxTRANSPARENT
);
370 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
371 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
372 m_PageBreaks
[page
-1]);
374 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
375 if (m_Headers
[page
% 2] != wxEmptyString
)
377 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
378 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
380 if (m_Footers
[page
% 2] != wxEmptyString
)
382 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
383 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
389 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
394 num
.Printf(wxT("%i"), page
);
395 r
.Replace(wxT("@PAGENUM@"), num
);
397 num
.Printf(wxT("%i"), m_NumPages
);
398 r
.Replace(wxT("@PAGESCNT@"), num
);
405 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
408 m_MarginBottom
= bottom
;
410 m_MarginRight
= right
;
411 m_MarginSpace
= spaces
;
417 void wxHtmlPrintout::SetFonts(wxString normal_face
, wxString fixed_face
,
420 m_Renderer
->SetFonts(normal_face
, fixed_face
, sizes
);
421 m_RendererHdr
->SetFonts(normal_face
, fixed_face
, sizes
);
426 //----------------------------------------------------------------------------
427 // wxHtmlEasyPrinting
428 //----------------------------------------------------------------------------
431 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxFrame
*parent_frame
)
433 m_Frame
= parent_frame
;
435 m_PrintData
= new wxPrintData
;
436 m_PageSetupData
= new wxPageSetupDialogData
;
437 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
439 m_PageSetupData
->EnableMargins(TRUE
);
440 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
441 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
443 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
448 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
451 delete m_PageSetupData
;
456 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
458 wxHtmlPrintout
*p1
= CreatePrintout();
459 p1
->SetHtmlFile(htmlfile
);
460 wxHtmlPrintout
*p2
= CreatePrintout();
461 p2
->SetHtmlFile(htmlfile
);
462 return DoPreview(p1
, p2
);
467 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
469 wxHtmlPrintout
*p1
= CreatePrintout();
470 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
471 wxHtmlPrintout
*p2
= CreatePrintout();
472 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
473 return DoPreview(p1
, p2
);
478 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
480 wxHtmlPrintout
*p
= CreatePrintout();
481 p
->SetHtmlFile(htmlfile
);
482 bool ret
= DoPrint(p
);
489 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
491 wxHtmlPrintout
*p
= CreatePrintout();
492 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
493 bool ret
= DoPrint(p
);
500 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
502 // Pass two printout objects: for preview, and possible printing.
503 wxPrintDialogData
printDialogData(*m_PrintData
);
504 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
511 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_Frame
,
512 m_Name
+ _(" Preview"),
513 wxPoint(100, 100), wxSize(650, 500));
514 frame
->Centre(wxBOTH
);
522 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
524 wxPrintDialogData
printDialogData(*m_PrintData
);
525 wxPrinter
printer(&printDialogData
);
527 if (!printer
.Print(m_Frame
, printout
, TRUE
))
532 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
538 void wxHtmlEasyPrinting::PrinterSetup()
540 wxPrintDialogData
printDialogData(*m_PrintData
);
541 wxPrintDialog
printerDialog(m_Frame
, &printDialogData
);
543 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
545 if (printerDialog
.ShowModal() == wxID_OK
)
546 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
551 void wxHtmlEasyPrinting::PageSetup()
553 if (!m_PrintData
->Ok())
555 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
559 m_PageSetupData
->SetPrintData(*m_PrintData
);
560 wxPageSetupDialog
pageSetupDialog(m_Frame
, m_PageSetupData
);
562 if (pageSetupDialog
.ShowModal() == wxID_OK
)
564 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
565 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
571 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
573 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
574 m_Headers
[0] = header
;
575 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
576 m_Headers
[1] = header
;
581 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
583 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
584 m_Footers
[0] = footer
;
585 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
586 m_Footers
[1] = footer
;
590 void wxHtmlEasyPrinting::SetFonts(wxString normal_face
, wxString fixed_face
,
593 m_FontFaceNormal
= normal_face
;
594 m_FontFaceFixed
= fixed_face
;
598 m_FontsSizes
= m_FontsSizesArr
;
599 for (int i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
606 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
608 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
610 p
->SetFonts(m_FontFaceNormal
, m_FontFaceFixed
, m_FontsSizes
);
612 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
613 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
614 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
615 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
617 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
618 m_PageSetupData
->GetMarginBottomRight().y
,
619 m_PageSetupData
->GetMarginTopLeft().x
,
620 m_PageSetupData
->GetMarginBottomRight().x
);
626 // This hack forces the linker to always link in m_* files
627 // (wxHTML doesn't work without handlers from these files)
628 #include "wx/html/forcelnk.h"
629 FORCE_WXHTML_MODULES()
631 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE