]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmprint.cpp
811147e71f8c850d0e6a6388103a0217a3aa37aa
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: html printing classes
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik, 1999
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
29 #if wxUSE_HTML && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS
32 #include "wx/printdlg.h"
33 #include "wx/html/htmprint.h"
34 #include "wx/wxhtml.h"
35 #include "wx/wfstream.h"
38 //--------------------------------------------------------------------------------
40 //--------------------------------------------------------------------------------
43 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
46 m_Width
= m_Height
= 0;
48 m_Parser
= new wxHtmlWinParser(NULL
);
49 m_FS
= new wxFileSystem();
50 m_Parser
->SetFS(m_FS
);
55 wxHtmlDCRenderer::~wxHtmlDCRenderer()
57 if (m_Cells
) delete m_Cells
;
58 if (m_Parser
) delete m_Parser
;
59 if (m_FS
) delete m_FS
;
64 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
67 m_Parser
->SetDC(m_DC
, pixel_scale
);
72 void wxHtmlDCRenderer::SetSize(int width
, int height
)
80 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
82 if (m_DC
== NULL
) return;
84 if (m_Cells
!= NULL
) delete m_Cells
;
86 m_FS
->ChangePathTo(basepath
, isdir
);
87 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
88 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
89 m_Cells
->Layout(m_Width
);
94 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
)
98 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
100 pbreak
= (int)(from
+ m_Height
);
101 while (m_Cells
->AdjustPagebreak(&pbreak
)) {}
102 hght
= pbreak
- from
;
106 m_DC
->SetBrush(*wxWHITE_BRUSH
);
108 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
111 y
, pbreak
+ (y
/*- from*/));
112 m_DC
->DestroyClippingRegion();
115 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
116 else return GetTotalHeight();
121 int wxHtmlDCRenderer::GetTotalHeight()
123 if (m_Cells
) return m_Cells
->GetHeight();
142 //--------------------------------------------------------------------------------
144 //--------------------------------------------------------------------------------
148 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
150 m_Renderer
= new wxHtmlDCRenderer
;
151 m_RendererHdr
= new wxHtmlDCRenderer
;
152 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
153 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
154 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
155 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
156 m_HeaderHeight
= m_FooterHeight
= 0;
157 SetMargins(); // to default values
162 wxHtmlPrintout::~wxHtmlPrintout()
165 delete m_RendererHdr
;
170 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
172 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
173 float ppmm_h
, ppmm_v
;
175 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
177 GetPageSizePixels(&pageWidth
, &pageHeight
);
178 GetPageSizeMM(&mm_w
, &mm_h
);
179 ppmm_h
= (float)pageWidth
/ mm_w
;
180 ppmm_v
= (float)pageHeight
/ mm_h
;
182 int ppiPrinterX
, ppiPrinterY
;
183 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
184 int ppiScreenX
, ppiScreenY
;
185 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
187 wxDisplaySize(&scr_w
, &scr_h
);
188 GetDC()->GetSize(&dc_w
, &dc_h
);
190 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
192 /* prepare headers/footers renderer: */
194 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
195 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
196 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
197 if (m_Headers
[0] != wxEmptyString
)
199 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
200 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
202 else if (m_Headers
[1] != wxEmptyString
)
204 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
205 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
207 if (m_Footers
[0] != wxEmptyString
)
209 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
210 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
212 else if (m_Footers
[1] != wxEmptyString
)
214 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
215 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
218 /* prepare main renderer: */
219 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
220 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
221 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
222 m_FooterHeight
- m_HeaderHeight
-
223 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
224 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
226 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
232 bool wxHtmlPrintout::OnPrintPage(int page
)
238 RenderPage(dc
, page
);
245 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
248 *maxPage
= wxHTML_PRINT_MAX_PAGES
;
250 *selPageTo
= wxHTML_PRINT_MAX_PAGES
;
255 bool wxHtmlPrintout::HasPage(int pageNum
)
257 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
262 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
265 m_BasePath
= basepath
;
266 m_BasePathIsDir
= isdir
;
271 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
274 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
278 wxLogError(htmlfile
+ _(": file does not exist!"));
282 wxInputStream
*st
= ff
->GetStream();
283 char *t
= new char[st
->GetSize() + 1];
284 st
->Read(t
, st
->GetSize());
285 t
[st
->GetSize()] = 0;
287 wxString doc
= wxString(t
);
291 SetHtmlText(doc
, htmlfile
, FALSE
);
296 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
298 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
299 m_Headers
[0] = header
;
300 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
301 m_Headers
[1] = header
;
306 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
308 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
309 m_Footers
[0] = footer
;
310 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
311 m_Footers
[1] = footer
;
316 void wxHtmlPrintout::CountPages()
319 int pageWidth
, pageHeight
, mm_w
, mm_h
;
320 float ppmm_h
, ppmm_v
;
322 GetPageSizePixels(&pageWidth
, &pageHeight
);
323 GetPageSizeMM(&mm_w
, &mm_h
);
324 ppmm_h
= (float)pageWidth
/ mm_w
;
325 ppmm_v
= (float)pageHeight
/ mm_h
;
334 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
335 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
337 m_PageBreaks
[++m_NumPages
] = pos
;
338 } while (pos
< m_Renderer
->GetTotalHeight());
343 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
347 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
348 float ppmm_h
, ppmm_v
;
350 GetPageSizePixels(&pageWidth
, &pageHeight
);
351 GetPageSizeMM(&mm_w
, &mm_h
);
352 ppmm_h
= (float)pageWidth
/ mm_w
;
353 ppmm_v
= (float)pageHeight
/ mm_h
;
354 wxDisplaySize(&scr_w
, &scr_h
);
355 dc
->GetSize(&dc_w
, &dc_h
);
357 int ppiPrinterX
, ppiPrinterY
;
358 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
359 int ppiScreenX
, ppiScreenY
;
360 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
362 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
364 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
366 dc
->SetBackgroundMode(wxTRANSPARENT
);
368 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
369 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
370 m_PageBreaks
[page
-1]);
372 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
373 if (m_Headers
[page
% 2] != wxEmptyString
)
375 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
376 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
378 if (m_Footers
[page
% 2] != wxEmptyString
)
380 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
381 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
387 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
392 num
.Printf(wxT("%i"), page
);
393 r
.Replace(wxT("@PAGENUM@"), num
);
395 num
.Printf(wxT("%i"), m_NumPages
);
396 r
.Replace(wxT("@PAGESCNT@"), num
);
403 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
406 m_MarginBottom
= bottom
;
408 m_MarginRight
= right
;
409 m_MarginSpace
= spaces
;
417 //--------------------------------------------------------------------------------
418 // wxHtmlEasyPrinting
419 //--------------------------------------------------------------------------------
422 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxFrame
*parent_frame
)
424 m_Frame
= parent_frame
;
426 m_PrintData
= new wxPrintData
;
427 #if (defined __WXGTK__) || (defined __WXMOTIF__)
428 (*m_PrintData
) = (*wxThePrintSetupData
);
430 m_PageSetupData
= new wxPageSetupDialogData
;
431 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
433 m_PageSetupData
->EnableMargins(TRUE
);
434 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
435 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
440 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
443 delete m_PageSetupData
;
448 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
450 wxHtmlPrintout
*p1
= CreatePrintout();
451 p1
->SetHtmlFile(htmlfile
);
452 wxHtmlPrintout
*p2
= CreatePrintout();
453 p2
->SetHtmlFile(htmlfile
);
454 return DoPreview(p1
, p2
);
459 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
461 wxHtmlPrintout
*p1
= CreatePrintout();
462 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
463 wxHtmlPrintout
*p2
= CreatePrintout();
464 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
465 return DoPreview(p1
, p2
);
470 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
472 wxHtmlPrintout
*p
= CreatePrintout();
473 p
->SetHtmlFile(htmlfile
);
479 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
481 wxHtmlPrintout
*p
= CreatePrintout();
482 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
488 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
490 // Pass two printout objects: for preview, and possible printing.
491 wxPrintDialogData
printDialogData(*m_PrintData
);
492 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
499 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_Frame
,
500 m_Name
+ _(" Preview"),
501 wxPoint(100, 100), wxSize(650, 500));
502 frame
->Centre(wxBOTH
);
510 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
512 wxPrintDialogData
printDialogData(*m_PrintData
);
513 wxPrinter
printer(&printDialogData
);
515 if (!printer
.Print(m_Frame
, printout
, TRUE
))
520 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
526 void wxHtmlEasyPrinting::PrinterSetup()
528 wxPrintDialogData
printDialogData(*m_PrintData
);
529 wxPrintDialog
printerDialog(m_Frame
, &printDialogData
);
531 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
533 if (printerDialog
.ShowModal() == wxID_OK
)
534 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
539 void wxHtmlEasyPrinting::PageSetup()
541 m_PageSetupData
->SetPrintData(*m_PrintData
);
542 wxPageSetupDialog
pageSetupDialog(m_Frame
, m_PageSetupData
);
544 if (pageSetupDialog
.ShowModal() == wxID_OK
)
546 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
547 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
553 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
555 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
556 m_Headers
[0] = header
;
557 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
558 m_Headers
[1] = header
;
563 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
565 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
566 m_Footers
[0] = footer
;
567 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
568 m_Footers
[1] = footer
;
573 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
575 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
577 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
578 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
579 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
580 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
582 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
583 m_PageSetupData
->GetMarginBottomRight().y
,
584 m_PageSetupData
->GetMarginTopLeft().x
,
585 m_PageSetupData
->GetMarginBottomRight().x
);
592 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE