]>
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"
29 #include "wx/msgdlg.h"
32 #if wxUSE_HTML && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS
36 #include "wx/printdlg.h"
37 #include "wx/html/htmprint.h"
38 #include "wx/wxhtml.h"
39 #include "wx/wfstream.h"
42 //--------------------------------------------------------------------------------
44 //--------------------------------------------------------------------------------
47 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
50 m_Width
= m_Height
= 0;
52 m_Parser
= new wxHtmlWinParser(NULL
);
53 m_FS
= new wxFileSystem();
54 m_Parser
->SetFS(m_FS
);
59 wxHtmlDCRenderer::~wxHtmlDCRenderer()
61 if (m_Cells
) delete m_Cells
;
62 if (m_Parser
) delete m_Parser
;
63 if (m_FS
) delete m_FS
;
68 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, double pixel_scale
)
71 m_Parser
->SetDC(m_DC
, pixel_scale
);
76 void wxHtmlDCRenderer::SetSize(int width
, int height
)
84 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
86 if (m_DC
== NULL
) return;
88 if (m_Cells
!= NULL
) delete m_Cells
;
90 m_FS
->ChangePathTo(basepath
, isdir
);
91 m_Cells
= (wxHtmlContainerCell
*) m_Parser
->Parse(html
);
92 m_Cells
->SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
93 m_Cells
->Layout(m_Width
);
98 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
)
102 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
104 pbreak
= (int)(from
+ m_Height
);
105 while (m_Cells
->AdjustPagebreak(&pbreak
)) {}
106 hght
= pbreak
- from
;
110 m_DC
->SetBrush(*wxWHITE_BRUSH
);
112 m_DC
->SetClippingRegion(x
, y
, m_Width
, hght
);
115 y
, pbreak
+ (y
/*- from*/));
116 m_DC
->DestroyClippingRegion();
119 if (pbreak
< m_Cells
->GetHeight()) return pbreak
;
120 else return GetTotalHeight();
125 int wxHtmlDCRenderer::GetTotalHeight()
127 if (m_Cells
) return m_Cells
->GetHeight();
146 //--------------------------------------------------------------------------------
148 //--------------------------------------------------------------------------------
152 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
154 m_Renderer
= new wxHtmlDCRenderer
;
155 m_RendererHdr
= new wxHtmlDCRenderer
;
156 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
157 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
158 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
159 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
160 m_HeaderHeight
= m_FooterHeight
= 0;
161 SetMargins(); // to default values
166 wxHtmlPrintout::~wxHtmlPrintout()
169 delete m_RendererHdr
;
174 bool wxHtmlPrintout::OnBeginDocument(int startPage
, int endPage
)
176 int pageWidth
, pageHeight
, mm_w
, mm_h
, scr_w
, scr_h
, dc_w
, dc_h
;
177 float ppmm_h
, ppmm_v
;
179 if (!wxPrintout::OnBeginDocument(startPage
, endPage
)) return FALSE
;
181 GetPageSizePixels(&pageWidth
, &pageHeight
);
182 GetPageSizeMM(&mm_w
, &mm_h
);
183 ppmm_h
= (float)pageWidth
/ mm_w
;
184 ppmm_v
= (float)pageHeight
/ mm_h
;
186 int ppiPrinterX
, ppiPrinterY
;
187 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
188 int ppiScreenX
, ppiScreenY
;
189 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
191 wxDisplaySize(&scr_w
, &scr_h
);
192 GetDC()->GetSize(&dc_w
, &dc_h
);
194 GetDC()->SetUserScale((double)dc_w
/ (double)pageWidth
, (double)dc_w
/ (double)pageWidth
);
196 /* prepare headers/footers renderer: */
198 m_RendererHdr
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
199 m_RendererHdr
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
200 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
)));
201 if (m_Headers
[0] != wxEmptyString
)
203 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[0], 1));
204 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
206 else if (m_Headers
[1] != wxEmptyString
)
208 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Headers
[1], 1));
209 m_HeaderHeight
= m_RendererHdr
->GetTotalHeight();
211 if (m_Footers
[0] != wxEmptyString
)
213 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[0], 1));
214 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
216 else if (m_Footers
[1] != wxEmptyString
)
218 m_RendererHdr
->SetHtmlText(TranslateHeader(m_Footers
[1], 1));
219 m_FooterHeight
= m_RendererHdr
->GetTotalHeight();
222 /* prepare main renderer: */
223 m_Renderer
->SetDC(GetDC(), (double)ppiPrinterY
/ (double)ppiScreenY
);
224 m_Renderer
->SetSize((int) (ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginRight
)),
225 (int) (ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
) -
226 m_FooterHeight
- m_HeaderHeight
-
227 ((m_HeaderHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
) -
228 ((m_FooterHeight
== 0) ? 0 : m_MarginSpace
* ppmm_v
)
230 m_Renderer
->SetHtmlText(m_Document
, m_BasePath
, m_BasePathIsDir
);
236 bool wxHtmlPrintout::OnPrintPage(int page
)
242 RenderPage(dc
, page
);
249 void wxHtmlPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
252 *maxPage
= wxHTML_PRINT_MAX_PAGES
;
254 *selPageTo
= wxHTML_PRINT_MAX_PAGES
;
259 bool wxHtmlPrintout::HasPage(int pageNum
)
261 return (pageNum
>= 1 && pageNum
<= m_NumPages
);
266 void wxHtmlPrintout::SetHtmlText(const wxString
& html
, const wxString
&basepath
, bool isdir
)
269 m_BasePath
= basepath
;
270 m_BasePathIsDir
= isdir
;
273 // defined in htmlfilt.cpp
274 void wxPrivate_ReadString(wxString
& str
, wxInputStream
* s
);
276 void wxHtmlPrintout::SetHtmlFile(const wxString
& htmlfile
)
279 wxFSFile
*ff
= fs
.OpenFile(htmlfile
);
283 wxLogError(htmlfile
+ _(": file does not exist!"));
287 wxInputStream
*st
= ff
->GetStream();
289 wxPrivate_ReadString(doc
, st
);
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 if (!m_PrintData
->Ok())
549 wxMessageBox(_("Sorry, there was a problem: you may need to set a default printer."),
550 _("Page Setup Problem"), wxICON_INFORMATION
|wxOK
, m_Frame
);
554 m_PageSetupData
->SetPrintData(*m_PrintData
);
555 wxPageSetupDialog
pageSetupDialog(m_Frame
, m_PageSetupData
);
557 if (pageSetupDialog
.ShowModal() == wxID_OK
)
559 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
560 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
566 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
568 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
569 m_Headers
[0] = header
;
570 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
571 m_Headers
[1] = header
;
576 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
578 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
579 m_Footers
[0] = footer
;
580 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
581 m_Footers
[1] = footer
;
586 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
588 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
590 p
->SetHeader(m_Headers
[0], wxPAGE_EVEN
);
591 p
->SetHeader(m_Headers
[1], wxPAGE_ODD
);
592 p
->SetFooter(m_Footers
[0], wxPAGE_EVEN
);
593 p
->SetFooter(m_Footers
[1], wxPAGE_ODD
);
595 p
->SetMargins(m_PageSetupData
->GetMarginTopLeft().y
,
596 m_PageSetupData
->GetMarginBottomRight().y
,
597 m_PageSetupData
->GetMarginTopLeft().x
,
598 m_PageSetupData
->GetMarginBottomRight().x
);
604 // This hack forces the linker to always link in m_* files
605 // (wxHTML doesn't work without handlers from these files)
606 #include "wx/html/forcelnk.h"
607 FORCE_WXHTML_MODULES()
609 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE