]>
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
34 #include "wx/printdlg.h"
35 #include "wx/html/htmprint.h"
36 #include "wx/wxhtml.h"
37 #include "wx/wfstream.h"
40 //--------------------------------------------------------------------------------
42 //--------------------------------------------------------------------------------
45 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
48 m_Width
= m_Height
= 0;
50 m_Parser
= new wxHtmlWinParser(NULL
);
51 m_FS
= new wxFileSystem();
52 m_Parser
->SetFS(m_FS
);
57 wxHtmlDCRenderer::~wxHtmlDCRenderer()
59 if (m_Cells
) delete m_Cells
;
60 if (m_Parser
) delete m_Parser
;
61 if (m_FS
) delete m_FS
;
66 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
69 m_Parser
->SetDC(m_DC
, pixel_scale
);
74 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
);
96 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
)
100 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
102 pbreak
= (int)(from
+ m_Height
);
103 while (m_Cells
->AdjustPagebreak(&pbreak
)) {}
104 hght
= pbreak
- from
;
108 m_DC
->SetBrush(*wxWHITE_BRUSH
);
110 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
113 y
, pbreak
+ (y
/*- from*/));
114 m_DC
->DestroyClippingRegion();
117 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
118 else return GetTotalHeight();
123 int wxHtmlDCRenderer::GetTotalHeight()
125 if (m_Cells
) return m_Cells
->GetHeight();
144 //--------------------------------------------------------------------------------
146 //--------------------------------------------------------------------------------
150 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
152 m_Renderer
= new wxHtmlDCRenderer
;
153 m_RendererHdr
= new wxHtmlDCRenderer
;
154 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
155 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
156 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
157 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
158 m_HeaderHeight
= m_FooterHeight
= 0;
159 SetMargins(); // to default values
164 wxHtmlPrintout::~wxHtmlPrintout()
167 delete m_RendererHdr
;
172 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
174 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
175 float ppmm_h
, ppmm_v
;
177 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
179 GetPageSizePixels(&pageWidth
, &pageHeight
);
180 GetPageSizeMM(&mm_w
, &mm_h
);
181 ppmm_h
= (float)pageWidth
/ mm_w
;
182 ppmm_v
= (float)pageHeight
/ mm_h
;
184 int ppiPrinterX
, ppiPrinterY
;
185 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
186 int ppiScreenX
, ppiScreenY
;
187 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
189 wxDisplaySize(&scr_w
, &scr_h
);
190 GetDC()->GetSize(&dc_w
, &dc_h
);
192 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
194 /* prepare headers/footers renderer: */
196 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
197 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
198 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
199 if (m_Headers
[0] != wxEmptyString
)
201 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
202 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
204 else if (m_Headers
[1] != wxEmptyString
)
206 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
207 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
209 if (m_Footers
[0] != wxEmptyString
)
211 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
212 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
214 else if (m_Footers
[1] != wxEmptyString
)
216 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
217 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
220 /* prepare main renderer: */
221 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
222 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
223 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
224 m_FooterHeight
- m_HeaderHeight
-
225 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
226 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
228 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
234 bool wxHtmlPrintout::OnPrintPage(int page
)
240 RenderPage(dc
, page
);
247 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
250 *maxPage
= wxHTML_PRINT_MAX_PAGES
;
252 *selPageTo
= wxHTML_PRINT_MAX_PAGES
;
257 bool wxHtmlPrintout::HasPage(int pageNum
)
259 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
264 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
267 m_BasePath
= basepath
;
268 m_BasePathIsDir
= isdir
;
273 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
276 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
280 wxLogError(htmlfile
+ _(": file does not exist!"));
284 wxInputStream
*st
= ff
->GetStream();
285 char *t
= new char[st
->GetSize() + 1];
286 st
->Read(t
, st
->GetSize());
287 t
[st
->GetSize()] = 0;
289 wxString doc
= wxString(t
);
293 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
;
419 //--------------------------------------------------------------------------------
420 // wxHtmlEasyPrinting
421 //--------------------------------------------------------------------------------
424 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxFrame
*parent_frame
)
426 m_Frame
= parent_frame
;
428 m_PrintData
= new wxPrintData
;
429 #if (defined __WXGTK__) || (defined __WXMOTIF__)
430 (*m_PrintData
) = (*wxThePrintSetupData
);
432 m_PageSetupData
= new wxPageSetupDialogData
;
433 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
435 m_PageSetupData
->EnableMargins(TRUE
);
436 m_PageSetupData
->SetMarginTopLeft(wxPoint(25, 25));
437 m_PageSetupData
->SetMarginBottomRight(wxPoint(25, 25));
442 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
445 delete m_PageSetupData
;
450 bool wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
452 wxHtmlPrintout
*p1
= CreatePrintout();
453 p1
->SetHtmlFile(htmlfile
);
454 wxHtmlPrintout
*p2
= CreatePrintout();
455 p2
->SetHtmlFile(htmlfile
);
456 return DoPreview(p1
, p2
);
461 bool wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
463 wxHtmlPrintout
*p1
= CreatePrintout();
464 p1
->SetHtmlText(htmltext
, basepath
, TRUE
);
465 wxHtmlPrintout
*p2
= CreatePrintout();
466 p2
->SetHtmlText(htmltext
, basepath
, TRUE
);
467 return DoPreview(p1
, p2
);
472 bool wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
474 wxHtmlPrintout
*p
= CreatePrintout();
475 p
->SetHtmlFile(htmlfile
);
476 bool ret
= DoPrint(p
);
483 bool wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
485 wxHtmlPrintout
*p
= CreatePrintout();
486 p
->SetHtmlText(htmltext
, basepath
, TRUE
);
487 bool ret
= DoPrint(p
);
494 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
496 // Pass two printout objects: for preview, and possible printing.
497 wxPrintDialogData
printDialogData(*m_PrintData
);
498 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
505 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_Frame
,
506 m_Name
+ _(" Preview"),
507 wxPoint(100, 100), wxSize(650, 500));
508 frame
->Centre(wxBOTH
);
516 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
518 wxPrintDialogData
printDialogData(*m_PrintData
);
519 wxPrinter
printer(&printDialogData
);
521 if (!printer
.Print(m_Frame
, printout
, TRUE
))
526 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
532 void wxHtmlEasyPrinting::PrinterSetup()
534 wxPrintDialogData
printDialogData(*m_PrintData
);
535 wxPrintDialog
printerDialog(m_Frame
, &printDialogData
);
537 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
539 if (printerDialog
.ShowModal() == wxID_OK
)
540 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
545 void wxHtmlEasyPrinting::PageSetup()
547 m_PageSetupData
->SetPrintData(*m_PrintData
);
548 wxPageSetupDialog
pageSetupDialog(m_Frame
, m_PageSetupData
);
550 if (pageSetupDialog
.ShowModal() == wxID_OK
)
552 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
553 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
559 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
561 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
562 m_Headers
[0] = header
;
563 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
564 m_Headers
[1] = header
;
569 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
571 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
572 m_Footers
[0] = footer
;
573 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
574 m_Footers
[1] = footer
;
579 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
581 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
583 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
584 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
585 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
586 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
588 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
589 m_PageSetupData
->GetMarginBottomRight().y
,
590 m_PageSetupData
->GetMarginTopLeft().x
,
591 m_PageSetupData
->GetMarginBottomRight().x
);
598 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE