]>
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"
29 #if wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE
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
);
56 wxHtmlDCRenderer::~wxHtmlDCRenderer()
58 if (m_Cells
) delete m_Cells
;
59 if (m_Parser
) delete m_Parser
;
60 if (m_FS
) delete m_FS
;
65 void wxHtmlDCRenderer::SetDC(wxDC
*dc
, int maxwidth
)
69 wxDisplaySize(&dx
, &dy
);
70 m_MaxWidth
= maxwidth
;
72 m_Scale
= (float)dx
* 2 / 3 / (float)maxwidth
;
73 // let the width of A4 is approximately 2/3 the screen width
75 m_Scale
= (float)800 / (float)maxwidth
;
76 // for now, assume screen width = 800 => good results
79 m_Parser
-> SetDC(dc
);
84 void wxHtmlDCRenderer::SetSize(int width
, int height
)
86 m_Width
= (int)(width
* m_Scale
);
87 m_Height
= (int)(height
* m_Scale
);
92 void wxHtmlDCRenderer::SetHtmlText(const wxString
& html
, const wxString
& basepath
, bool isdir
)
94 if (m_DC
== NULL
) return;
96 if (m_Cells
!= NULL
) delete m_Cells
;
98 m_FS
-> ChangePathTo(basepath
, isdir
);
99 m_DC
-> SetUserScale(1.0, 1.0);
100 m_Cells
= (wxHtmlContainerCell
*) m_Parser
-> Parse(html
);
101 m_Cells
-> SetIndent(0, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
102 m_Cells
-> Layout(m_Width
);
107 int wxHtmlDCRenderer::Render(int x
, int y
, int from
, int dont_render
)
111 if (m_Cells
== NULL
|| m_DC
== NULL
) return 0;
113 pbreak
= (int)(from
* m_Scale
+ m_Height
);
114 while (m_Cells
-> AdjustPagebreak(&pbreak
)) {}
118 m_DC
-> GetSize(&w
, &h
);
119 float overallScale
= (float)(w
/(float)m_MaxWidth
) / m_Scale
;
120 m_DC
-> SetUserScale(overallScale
, overallScale
);
122 m_DC
-> SetBrush(*wxWHITE_BRUSH
);
124 m_DC
-> SetClippingRegion(x
* m_Scale
, y
* m_Scale
, m_Width
, m_Height
);
125 m_Cells
-> Draw(*m_DC
,
126 x
* m_Scale
, (y
- from
) * m_Scale
,
127 y
* m_Scale
, pbreak
+ (y
- from
) * m_Scale
);
128 m_DC
-> DestroyClippingRegion();
131 if (pbreak
< m_Cells
-> GetHeight()) return (int)(pbreak
/ m_Scale
);
132 else return GetTotalHeight();
137 int wxHtmlDCRenderer::GetTotalHeight()
139 if (m_Cells
) return (int)(m_Cells
-> GetHeight() / m_Scale
);
158 //--------------------------------------------------------------------------------
160 //--------------------------------------------------------------------------------
164 wxHtmlPrintout::wxHtmlPrintout(const wxString
& title
) : wxPrintout(title
)
166 m_Renderer
= new wxHtmlDCRenderer
;
167 m_RendererHdr
= new wxHtmlDCRenderer
;
168 m_NumPages
= wxHTML_PRINT_MAX_PAGES
;
169 m_Document
= m_BasePath
= wxEmptyString
; m_BasePathIsDir
= TRUE
;
170 m_Headers
[0] = m_Headers
[1] = wxEmptyString
;
171 m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
172 m_HeaderHeight
= m_FooterHeight
= 0;
173 SetMargins(); // to default values
178 wxHtmlPrintout::~wxHtmlPrintout()
181 delete m_RendererHdr
;
186 void wxHtmlPrintout::OnBeginPrinting()
188 int pageWidth
, pageHeight
, mm_w
, mm_h
;
189 float ppmm_h
, ppmm_v
;
191 wxPrintout::OnBeginPrinting();
193 GetPageSizePixels(&pageWidth
, &pageHeight
);
194 GetPageSizeMM(&mm_w
, &mm_h
);
195 ppmm_h
= (float)pageWidth
/ mm_w
;
196 ppmm_v
= (float)pageHeight
/ mm_h
;
198 /* prepare headers/footers renderer: */
200 m_RendererHdr
-> SetDC(GetDC(), pageWidth
);
201 m_RendererHdr
-> SetSize(ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginTop
),
202 ppmm_v
* (mm_h
- m_MarginTop
- m_MarginBottom
));
203 if (m_Headers
[0] != wxEmptyString
) {
204 m_RendererHdr
-> SetHtmlText(TranslateHeader(m_Headers
[0], 1));
205 m_HeaderHeight
= m_RendererHdr
-> GetTotalHeight();
207 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
) {
212 m_RendererHdr
-> SetHtmlText(TranslateHeader(m_Footers
[0], 1));
213 m_FooterHeight
= m_RendererHdr
-> GetTotalHeight();
215 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(), pageWidth
);
222 m_Renderer
-> SetSize(ppmm_h
* (mm_w
- m_MarginLeft
- m_MarginTop
),
223 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
);
233 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
);
275 wxInputStream
*st
= ff
-> GetStream();
276 char *t
= new char[st
-> GetSize() + 1];
277 st
-> Read(t
, st
-> GetSize());
278 t
[st
-> GetSize()] = 0;
280 wxString doc
= wxString(t
);
284 SetHtmlText(doc
, htmlfile
, FALSE
);
289 void wxHtmlPrintout::SetHeader(const wxString
& header
, int pg
)
291 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
292 m_Headers
[0] = header
;
293 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
294 m_Headers
[1] = header
;
299 void wxHtmlPrintout::SetFooter(const wxString
& footer
, int pg
)
301 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
302 m_Footers
[0] = footer
;
303 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
304 m_Footers
[1] = footer
;
309 void wxHtmlPrintout::CountPages()
312 int pageWidth
, pageHeight
, mm_w
, mm_h
;
313 float ppmm_h
, ppmm_v
;
315 GetPageSizePixels(&pageWidth
, &pageHeight
);
316 GetPageSizeMM(&mm_w
, &mm_h
);
317 ppmm_h
= (float)pageWidth
/ mm_w
;
318 ppmm_v
= (float)pageHeight
/ mm_h
;
326 pos
= m_Renderer
-> Render(ppmm_h
* m_MarginLeft
,
327 ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
,
329 m_PageBreaks
[++m_NumPages
] = pos
;
330 } while (pos
< m_Renderer
-> GetTotalHeight());
335 void wxHtmlPrintout::RenderPage(wxDC
*dc
, int page
)
339 int pageWidth
, pageHeight
, mm_w
, mm_h
;
340 float ppmm_h
, ppmm_v
;
342 GetPageSizePixels(&pageWidth
, &pageHeight
);
343 GetPageSizeMM(&mm_w
, &mm_h
);
344 ppmm_h
= (float)pageWidth
/ mm_w
;
345 ppmm_v
= (float)pageHeight
/ mm_h
;
347 m_Renderer
-> SetDC(dc
, pageWidth
);
349 dc
-> SetBackgroundMode(wxTRANSPARENT
);
351 m_Renderer
-> Render(ppmm_h
* m_MarginLeft
,
352 ppmm_v
* (m_MarginTop
+ (m_HeaderHeight
== 0 ? 0 : m_MarginSpace
)) + m_HeaderHeight
,
353 m_PageBreaks
[page
-1]);
355 m_RendererHdr
-> SetDC(dc
, pageWidth
);
356 if (m_Headers
[page
% 2] != wxEmptyString
) {
357 m_RendererHdr
-> SetHtmlText(TranslateHeader(m_Headers
[page
% 2], page
));
358 m_RendererHdr
-> Render(ppmm_h
* m_MarginLeft
, ppmm_v
* m_MarginTop
);
360 if (m_Footers
[page
% 2] != wxEmptyString
) {
361 m_RendererHdr
-> SetHtmlText(TranslateHeader(m_Footers
[page
% 2], page
));
362 m_RendererHdr
-> Render(ppmm_h
* m_MarginLeft
, pageHeight
- ppmm_v
* m_MarginBottom
- m_FooterHeight
);
368 wxString
wxHtmlPrintout::TranslateHeader(const wxString
& instr
, int page
)
373 num
.Printf("%i", page
);
374 r
.Replace("@PAGENUM@", num
);
376 num
.Printf("%i", m_NumPages
);
377 r
.Replace("@PAGESCNT@", num
);
384 void wxHtmlPrintout::SetMargins(float top
, float bottom
, float left
, float right
, float spaces
)
387 m_MarginBottom
= bottom
;
389 m_MarginRight
= right
;
390 m_MarginSpace
= spaces
;
398 //--------------------------------------------------------------------------------
399 // wxHtmlEasyPrinting
400 //--------------------------------------------------------------------------------
403 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString
& name
, wxFrame
*parent_frame
)
405 m_Frame
= parent_frame
;
407 m_PrintData
= new wxPrintData
;
408 #if (defined __WXGTK__) || (defined __WXMOTIF__)
409 (*m_PrintData
) = (*wxThePrintSetupData
);
411 m_PageSetupData
= new wxPageSetupDialogData
;
412 m_Headers
[0] = m_Headers
[1] = m_Footers
[0] = m_Footers
[1] = wxEmptyString
;
414 m_PageSetupData
-> EnableMargins(TRUE
);
415 m_PageSetupData
-> SetMarginTopLeft(wxPoint(25, 25));
416 m_PageSetupData
-> SetMarginBottomRight(wxPoint(25, 25));
421 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
424 delete m_PageSetupData
;
429 void wxHtmlEasyPrinting::PreviewFile(const wxString
&htmlfile
)
431 wxHtmlPrintout
*p1
= CreatePrintout();
432 p1
-> SetHtmlFile(htmlfile
);
433 wxHtmlPrintout
*p2
= CreatePrintout();
434 p2
-> SetHtmlFile(htmlfile
);
440 void wxHtmlEasyPrinting::PreviewText(const wxString
&htmltext
, const wxString
&basepath
)
442 wxHtmlPrintout
*p1
= CreatePrintout();
443 p1
-> SetHtmlText(htmltext
, basepath
, TRUE
);
444 wxHtmlPrintout
*p2
= CreatePrintout();
445 p2
-> SetHtmlText(htmltext
, basepath
, TRUE
);
451 void wxHtmlEasyPrinting::PrintFile(const wxString
&htmlfile
)
453 wxHtmlPrintout
*p
= CreatePrintout();
454 p
-> SetHtmlFile(htmlfile
);
460 void wxHtmlEasyPrinting::PrintText(const wxString
&htmltext
, const wxString
&basepath
)
462 wxHtmlPrintout
*p
= CreatePrintout();
463 p
-> SetHtmlText(htmltext
, basepath
, TRUE
);
469 void wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
)
471 // Pass two printout objects: for preview, and possible printing.
472 wxPrintDialogData
printDialogData(*m_PrintData
);
473 wxPrintPreview
*preview
= new wxPrintPreview(printout1
, printout2
, &printDialogData
);
474 if (!preview
-> Ok()) {
476 wxMessageBox(_("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _("Previewing"), wxOK
);
480 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, m_Frame
,
481 m_Name
+ _(" Preview"),
482 wxPoint(100, 100), wxSize(650, 500));
483 frame
-> Centre(wxBOTH
);
484 frame
-> Initialize();
491 void wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout
*printout
)
493 wxPrintDialogData
printDialogData(*m_PrintData
);
494 wxPrinter
printer(&printDialogData
);
496 if (!printer
.Print(m_Frame
, printout
, TRUE
))
497 wxMessageBox(_("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _("Printing"), wxOK
);
499 (*m_PrintData
) = printer
.GetPrintDialogData().GetPrintData();
504 void wxHtmlEasyPrinting::PrinterSetup()
506 wxPrintDialogData
printDialogData(*m_PrintData
);
507 wxPrintDialog
printerDialog(m_Frame
, &printDialogData
);
509 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
511 if (printerDialog
.ShowModal() == wxID_OK
)
512 (*m_PrintData
) = printerDialog
.GetPrintDialogData().GetPrintData();
517 void wxHtmlEasyPrinting::PageSetup()
519 m_PageSetupData
-> SetPrintData(*m_PrintData
);
520 wxPageSetupDialog
pageSetupDialog(m_Frame
, m_PageSetupData
);
522 if (pageSetupDialog
.ShowModal() == wxID_OK
) {
523 (*m_PrintData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
524 (*m_PageSetupData
) = pageSetupDialog
.GetPageSetupData();
530 void wxHtmlEasyPrinting::SetHeader(const wxString
& header
, int pg
)
532 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
533 m_Headers
[0] = header
;
534 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
535 m_Headers
[1] = header
;
540 void wxHtmlEasyPrinting::SetFooter(const wxString
& footer
, int pg
)
542 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_EVEN
)
543 m_Footers
[0] = footer
;
544 if (pg
== wxPAGE_ALL
|| pg
== wxPAGE_ODD
)
545 m_Footers
[1] = footer
;
550 wxHtmlPrintout
*wxHtmlEasyPrinting::CreatePrintout()
552 wxHtmlPrintout
*p
= new wxHtmlPrintout(m_Name
);
554 p
-> SetHeader(m_Headers
[0], wxPAGE_EVEN
);
555 p
-> SetHeader(m_Headers
[1], wxPAGE_ODD
);
556 p
-> SetFooter(m_Footers
[0], wxPAGE_EVEN
);
557 p
-> SetFooter(m_Footers
[1], wxPAGE_ODD
);
559 p
-> SetMargins(m_PageSetupData
-> GetMarginTopLeft().y
,
560 m_PageSetupData
-> GetMarginBottomRight().y
,
561 m_PageSetupData
-> GetMarginTopLeft().x
,
562 m_PageSetupData
-> GetMarginBottomRight().x
);
569 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE