]>
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
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
)
83 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
85 if (m_DC
== NULL
) return;
87 if (m_Cells
!= NULL
) delete m_Cells
;
89 m_FS
->ChangePathTo(basepath
, isdir
);
90 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
91 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
92 m_Cells
->Layout(m_Width
);
97 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
)
101 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
103 pbreak
= (int)(from
+ m_Height
);
104 while (m_Cells
->AdjustPagebreak(&pbreak
)) {}
105 hght
= pbreak
- from
;
109 m_DC
->SetBrush(*wxWHITE_BRUSH
);
111 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
114 y
, pbreak
+ (y
/*- from*/));
115 m_DC
->DestroyClippingRegion();
118 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
119 else return GetTotalHeight();
124 int wxHtmlDCRenderer::GetTotalHeight()
126 if (m_Cells
) return m_Cells
->GetHeight();
145 //--------------------------------------------------------------------------------
147 //--------------------------------------------------------------------------------
151 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
153 m_Renderer
= new wxHtmlDCRenderer
;
154 m_RendererHdr
= new wxHtmlDCRenderer
;
155 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
156 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
157 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
158 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
159 m_HeaderHeight
= m_FooterHeight
= 0;
160 SetMargins(); // to default values
165 wxHtmlPrintout::~wxHtmlPrintout()
168 delete m_RendererHdr
;
173 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
175 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
176 float ppmm_h
, ppmm_v
;
178 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
180 GetPageSizePixels(&pageWidth
, &pageHeight
);
181 GetPageSizeMM(&mm_w
, &mm_h
);
182 ppmm_h
= (float)pageWidth
/ mm_w
;
183 ppmm_v
= (float)pageHeight
/ mm_h
;
185 int ppiPrinterX
, ppiPrinterY
;
186 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
187 int ppiScreenX
, ppiScreenY
;
188 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
190 wxDisplaySize(&scr_w
, &scr_h
);
191 GetDC()->GetSize(&dc_w
, &dc_h
);
193 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
195 /* prepare headers/footers renderer: */
197 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
198 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
199 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
200 if (m_Headers
[0] != wxEmptyString
)
202 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
203 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
205 else if (m_Headers
[1] != wxEmptyString
)
207 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
208 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
210 if (m_Footers
[0] != wxEmptyString
)
212 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
213 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
215 else if (m_Footers
[1] != wxEmptyString
)
217 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
218 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
221 /* prepare main renderer: */
222 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
223 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
224 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
225 m_FooterHeight
- m_HeaderHeight
-
226 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
227 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
229 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
235 bool wxHtmlPrintout::OnPrintPage(int page
)
241 RenderPage(dc
, page
);
248 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
251 *maxPage
= wxHTML_PRINT_MAX_PAGES
;
253 *selPageTo
= wxHTML_PRINT_MAX_PAGES
;
258 bool wxHtmlPrintout::HasPage(int pageNum
)
260 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
265 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
268 m_BasePath
= basepath
;
269 m_BasePathIsDir
= isdir
;
274 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
277 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
281 wxLogError(htmlfile
+ _(": file does not exist!"));
285 wxInputStream
*st
= ff
->GetStream();
286 char *t
= new char[st
->GetSize() + 1];
287 st
->Read(t
, st
->GetSize());
288 t
[st
->GetSize()] = 0;
290 wxString doc
= wxString(t
);
294 SetHtmlText(doc
, htmlfile
, FALSE
);
299 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
301 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
302 m_Headers
[0] = header
;
303 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
304 m_Headers
[1] = header
;
309 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
311 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
312 m_Footers
[0] = footer
;
313 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
314 m_Footers
[1] = footer
;
319 void wxHtmlPrintout::CountPages()
322 int pageWidth
, pageHeight
, mm_w
, mm_h
;
323 float ppmm_h
, ppmm_v
;
325 GetPageSizePixels(&pageWidth
, &pageHeight
);
326 GetPageSizeMM(&mm_w
, &mm_h
);
327 ppmm_h
= (float)pageWidth
/ mm_w
;
328 ppmm_v
= (float)pageHeight
/ mm_h
;
337 pos
= m_Renderer
->Render((int)( ppmm_h
* m_MarginLeft
),
338 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
340 m_PageBreaks
[++m_NumPages
] = pos
;
341 } while (pos
< m_Renderer
->GetTotalHeight());
346 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
350 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
351 float ppmm_h
, ppmm_v
;
353 GetPageSizePixels(&pageWidth
, &pageHeight
);
354 GetPageSizeMM(&mm_w
, &mm_h
);
355 ppmm_h
= (float)pageWidth
/ mm_w
;
356 ppmm_v
= (float)pageHeight
/ mm_h
;
357 wxDisplaySize(&scr_w
, &scr_h
);
358 dc
->GetSize(&dc_w
, &dc_h
);
360 int ppiPrinterX
, ppiPrinterY
;
361 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
362 int ppiScreenX
, ppiScreenY
;
363 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
365 dc
->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
367 m_Renderer
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
369 dc
->SetBackgroundMode(wxTRANSPARENT
);
371 m_Renderer
->Render((int) (ppmm_h
* m_MarginLeft
),
372 (int) (ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
),
373 m_PageBreaks
[page
-1]);
375 m_RendererHdr
->SetDC(dc
, (double)ppiPrinterY
/ (double)ppiScreenY
);
376 if (m_Headers
[page
% 2] != wxEmptyString
)
378 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
379 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (ppmm_v
* m_MarginTop
));
381 if (m_Footers
[page
% 2] != wxEmptyString
)
383 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
384 m_RendererHdr
->Render((int) (ppmm_h
* m_MarginLeft
), (int) (pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
));
390 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
395 num
.Printf(wxT("%i"), page
);
396 r
.Replace(wxT("@PAGENUM@"), num
);
398 num
.Printf(wxT("%i"), m_NumPages
);
399 r
.Replace(wxT("@PAGESCNT@"), num
);
406 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
409 m_MarginBottom
= bottom
;
411 m_MarginRight
= right
;
412 m_MarginSpace
= spaces
;
420 //--------------------------------------------------------------------------------
421 // wxHtmlEasyPrinting
422 //--------------------------------------------------------------------------------
425 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxFrame
*parent_frame
)
427 m_Frame
= parent_frame
;
429 m_PrintData
= new wxPrintData
;
430 #if (defined __WXGTK__) || (defined __WXMOTIF__)
431 (*m_PrintData
) = (*wxThePrintSetupData
);
433 m_PageSetupData
= new wxPageSetupDialogData
;
434 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
436 m_PageSetupData
->EnableMargins(TRUE
);
437 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
438 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
443 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
446 delete m_PageSetupData
;
451 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
453 wxHtmlPrintout
*p1
= CreatePrintout();
454 p1
->SetHtmlFile(htmlfile
);
455 wxHtmlPrintout
*p2
= CreatePrintout();
456 p2
->SetHtmlFile(htmlfile
);
457 return DoPreview(p1
, p2
);
462 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
464 wxHtmlPrintout
*p1
= CreatePrintout();
465 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
466 wxHtmlPrintout
*p2
= CreatePrintout();
467 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
468 return DoPreview(p1
, p2
);
473 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
475 wxHtmlPrintout
*p
= CreatePrintout();
476 p
->SetHtmlFile(htmlfile
);
477 bool ret
= DoPrint(p
);
484 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
486 wxHtmlPrintout
*p
= CreatePrintout();
487 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
488 bool ret
= DoPrint(p
);
495 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
497 // Pass two printout objects: for preview, and possible printing.
498 wxPrintDialogData
printDialogData(*m_PrintData
);
499 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
506 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_Frame
,
507 m_Name
+ _(" Preview"),
508 wxPoint(100, 100), wxSize(650, 500));
509 frame
->Centre(wxBOTH
);
517 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
519 wxPrintDialogData
printDialogData(*m_PrintData
);
520 wxPrinter
printer(&printDialogData
);
522 if (!printer
.Print(m_Frame
, printout
, TRUE
))
527 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
533 void wxHtmlEasyPrinting::PrinterSetup()
535 wxPrintDialogData
printDialogData(*m_PrintData
);
536 wxPrintDialog
printerDialog(m_Frame
, &printDialogData
);
538 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
540 if (printerDialog
.ShowModal() == wxID_OK
)
541 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
546 void wxHtmlEasyPrinting::PageSetup()
548 m_PageSetupData
->SetPrintData(*m_PrintData
);
549 wxPageSetupDialog
pageSetupDialog(m_Frame
, m_PageSetupData
);
551 if (pageSetupDialog
.ShowModal() == wxID_OK
)
553 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
554 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
560 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
562 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
563 m_Headers
[0] = header
;
564 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
565 m_Headers
[1] = header
;
570 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
572 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
573 m_Footers
[0] = footer
;
574 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
575 m_Footers
[1] = footer
;
580 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
582 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
584 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
585 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
586 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
587 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
589 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
590 m_PageSetupData
->GetMarginBottomRight().y
,
591 m_PageSetupData
->GetMarginTopLeft().x
,
592 m_PageSetupData
->GetMarginBottomRight().x
);
598 // This hack forces the linker to always link in m_* files
599 // (wxHTML doesn't work without handlers from these files)
600 #include "wx/html/forcelnk.h"
601 FORCE_WXHTML_MODULES()
603 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE