]>
Commit | Line | Data |
---|---|---|
3ce369e6 | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/html/htmprint.cpp |
3ce369e6 VS |
3 | // Purpose: html printing classes |
4 | // Author: Vaclav Slavik | |
5 | // Created: 25/09/99 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Vaclav Slavik, 1999 | |
65571936 | 8 | // Licence: wxWindows licence |
3ce369e6 VS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
3ce369e6 VS |
11 | // For compilers that support precompilation, includes "wx/wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
93763ad5 | 15 | #pragma hdrstop |
3ce369e6 VS |
16 | #endif |
17 | ||
93763ad5 WS |
18 | #if wxUSE_HTML && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS |
19 | ||
3ce369e6 | 20 | #ifndef WX_PRECOMP |
04dbb646 VZ |
21 | #include "wx/log.h" |
22 | #include "wx/intl.h" | |
5438a566 | 23 | #include "wx/dc.h" |
9eddec69 | 24 | #include "wx/settings.h" |
706e740f | 25 | #include "wx/msgdlg.h" |
02761f6c | 26 | #include "wx/module.h" |
3ce369e6 VS |
27 | #endif |
28 | ||
3ce369e6 VS |
29 | #include "wx/print.h" |
30 | #include "wx/printdlg.h" | |
31 | #include "wx/html/htmprint.h" | |
32 | #include "wx/wxhtml.h" | |
33 | #include "wx/wfstream.h" | |
34 | ||
35 | ||
7258d995 VS |
36 | // default font size of normal text (HTML font size 0) for printing, in points: |
37 | #define DEFAULT_PRINT_FONT_SIZE 12 | |
38 | ||
39 | ||
c44a49b8 VS |
40 | // CSS specification offer following guidance on dealing with pixel sizes |
41 | // when printing at | |
42 | // http://www.w3.org/TR/2004/CR-CSS21-20040225/syndata.html#length-units: | |
43 | // | |
44 | // Pixel units are relative to the resolution of the viewing device, i.e., | |
45 | // most often a computer display. If the pixel density of the output | |
46 | // device is very different from that of a typical computer display, the | |
47 | // user agent should rescale pixel values. It is recommended that the [ | |
48 | // reference pixel] be the visual angle of one pixel on a device with a | |
49 | // pixel density of 96dpi and a distance from the reader of an arm's | |
50 | // length. For a nominal arm's length of 28 inches, the visual angle is | |
51 | // therefore about 0.0213 degrees. | |
52 | // | |
53 | // For reading at arm's length, 1px thus corresponds to about 0.26 mm | |
54 | // (1/96 inch). When printed on a laser printer, meant for reading at a | |
55 | // little less than arm's length (55 cm, 21 inches), 1px is about 0.20 mm. | |
56 | // On a 300 dots-per-inch (dpi) printer, that may be rounded up to 3 dots | |
57 | // (0.25 mm); on a 600 dpi printer, it can be rounded to 5 dots. | |
58 | // | |
59 | // See also http://trac.wxwidgets.org/ticket/10942. | |
60 | #define TYPICAL_SCREEN_DPI 96.0 | |
61 | ||
3ce369e6 VS |
62 | //-------------------------------------------------------------------------------- |
63 | // wxHtmlDCRenderer | |
64 | //-------------------------------------------------------------------------------- | |
65 | ||
66 | ||
67 | wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject() | |
68 | { | |
69 | m_DC = NULL; | |
70 | m_Width = m_Height = 0; | |
71 | m_Cells = NULL; | |
bc55e31b | 72 | m_Parser = new wxHtmlWinParser(); |
3ce369e6 | 73 | m_FS = new wxFileSystem(); |
4f9297b0 | 74 | m_Parser->SetFS(m_FS); |
7258d995 | 75 | SetStandardFonts(DEFAULT_PRINT_FONT_SIZE); |
3ce369e6 VS |
76 | } |
77 | ||
78 | ||
79 | ||
80 | wxHtmlDCRenderer::~wxHtmlDCRenderer() | |
81 | { | |
82 | if (m_Cells) delete m_Cells; | |
83 | if (m_Parser) delete m_Parser; | |
84 | if (m_FS) delete m_FS; | |
85 | } | |
86 | ||
87 | ||
88 | ||
c44a49b8 | 89 | void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale, double font_scale) |
3ce369e6 | 90 | { |
3ce369e6 | 91 | m_DC = dc; |
c44a49b8 | 92 | m_Parser->SetDC(m_DC, pixel_scale, font_scale); |
3ce369e6 VS |
93 | } |
94 | ||
95 | ||
96 | ||
97 | void wxHtmlDCRenderer::SetSize(int width, int height) | |
98 | { | |
131fc120 VS |
99 | wxCHECK_RET( width, "width must be non-zero" ); |
100 | wxCHECK_RET( height, "height must be non-zero" ); | |
101 | ||
edbd0635 VS |
102 | m_Width = width; |
103 | m_Height = height; | |
3ce369e6 VS |
104 | } |
105 | ||
106 | ||
3ce369e6 VS |
107 | void wxHtmlDCRenderer::SetHtmlText(const wxString& html, const wxString& basepath, bool isdir) |
108 | { | |
131fc120 VS |
109 | wxCHECK_RET( m_DC, "SetDC() must be called before SetHtmlText()" ); |
110 | wxCHECK_RET( m_Width, "SetSize() must be called before SetHtmlText()" ); | |
3ce369e6 | 111 | |
131fc120 | 112 | wxDELETE(m_Cells); |
04dbb646 | 113 | |
4f9297b0 VS |
114 | m_FS->ChangePathTo(basepath, isdir); |
115 | m_Cells = (wxHtmlContainerCell*) m_Parser->Parse(html); | |
116 | m_Cells->SetIndent(0, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); | |
117 | m_Cells->Layout(m_Width); | |
3ce369e6 VS |
118 | } |
119 | ||
120 | ||
fbfb8bcc | 121 | void wxHtmlDCRenderer::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
4eecf115 VS |
122 | const int *sizes) |
123 | { | |
124 | m_Parser->SetFonts(normal_face, fixed_face, sizes); | |
131fc120 VS |
125 | |
126 | if ( m_Cells ) | |
10e5c7ea | 127 | m_Cells->Layout(m_Width); |
131fc120 | 128 | // else: SetHtmlText() not yet called, no need for relayout |
4eecf115 VS |
129 | } |
130 | ||
10e5c7ea VS |
131 | void wxHtmlDCRenderer::SetStandardFonts(int size, |
132 | const wxString& normal_face, | |
133 | const wxString& fixed_face) | |
7acd3625 | 134 | { |
10e5c7ea | 135 | m_Parser->SetStandardFonts(size, normal_face, fixed_face); |
131fc120 VS |
136 | |
137 | if ( m_Cells ) | |
10e5c7ea | 138 | m_Cells->Layout(m_Width); |
131fc120 | 139 | // else: SetHtmlText() not yet called, no need for relayout |
7acd3625 RD |
140 | } |
141 | ||
fd0bab43 VZ |
142 | int wxHtmlDCRenderer::Render(int x, int y, |
143 | wxArrayInt& known_pagebreaks, | |
144 | int from, int dont_render, int to) | |
3ce369e6 | 145 | { |
131fc120 VS |
146 | wxCHECK_MSG( m_Cells, 0, "SetHtmlText() must be called before Render()" ); |
147 | wxCHECK_MSG( m_DC, 0, "SetDC() must be called before Render()" ); | |
04dbb646 | 148 | |
131fc120 | 149 | int pbreak, hght; |
04dbb646 | 150 | |
edbd0635 | 151 | pbreak = (int)(from + m_Height); |
fd0bab43 | 152 | while (m_Cells->AdjustPagebreak(&pbreak, known_pagebreaks)) {} |
edbd0635 | 153 | hght = pbreak - from; |
fd0bab43 VZ |
154 | if(to < hght) |
155 | hght = to; | |
04dbb646 VZ |
156 | |
157 | if (!dont_render) | |
4f9297b0 | 158 | { |
f30e67db VS |
159 | wxHtmlRenderingInfo rinfo; |
160 | wxDefaultHtmlRenderingStyle rstyle; | |
161 | rinfo.SetStyle(&rstyle); | |
4f9297b0 | 162 | m_DC->SetBrush(*wxWHITE_BRUSH); |
2a2e4f4a | 163 | m_DC->SetClippingRegion(x, y, m_Width, hght); |
04dbb646 | 164 | m_Cells->Draw(*m_DC, |
36c4ff4d | 165 | x, (y - from), |
26fba7b2 | 166 | y, y + hght, |
f30e67db | 167 | rinfo); |
2a2e4f4a | 168 | m_DC->DestroyClippingRegion(); |
3ce369e6 | 169 | } |
04dbb646 | 170 | |
4f9297b0 | 171 | if (pbreak < m_Cells->GetHeight()) return pbreak; |
3ce369e6 VS |
172 | else return GetTotalHeight(); |
173 | } | |
174 | ||
4209475c VZ |
175 | int wxHtmlDCRenderer::GetTotalWidth() const |
176 | { | |
177 | return m_Cells ? m_Cells->GetWidth() : 0; | |
178 | } | |
3ce369e6 | 179 | |
4209475c | 180 | int wxHtmlDCRenderer::GetTotalHeight() const |
3ce369e6 | 181 | { |
4209475c | 182 | return m_Cells ? m_Cells->GetHeight() : 0; |
3ce369e6 VS |
183 | } |
184 | ||
185 | ||
3ce369e6 VS |
186 | //-------------------------------------------------------------------------------- |
187 | // wxHtmlPrintout | |
188 | //-------------------------------------------------------------------------------- | |
189 | ||
190 | ||
fa10c70c | 191 | wxList wxHtmlPrintout::m_Filters; |
3ce369e6 VS |
192 | |
193 | wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title) | |
194 | { | |
195 | m_Renderer = new wxHtmlDCRenderer; | |
196 | m_RendererHdr = new wxHtmlDCRenderer; | |
efba2b89 | 197 | m_NumPages = wxHTML_PRINT_MAX_PAGES; |
d1da8872 | 198 | m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = true; |
3ce369e6 VS |
199 | m_Headers[0] = m_Headers[1] = wxEmptyString; |
200 | m_Footers[0] = m_Footers[1] = wxEmptyString; | |
201 | m_HeaderHeight = m_FooterHeight = 0; | |
202 | SetMargins(); // to default values | |
7258d995 | 203 | SetStandardFonts(DEFAULT_PRINT_FONT_SIZE); |
3ce369e6 VS |
204 | } |
205 | ||
206 | ||
207 | ||
208 | wxHtmlPrintout::~wxHtmlPrintout() | |
209 | { | |
210 | delete m_Renderer; | |
211 | delete m_RendererHdr; | |
212 | } | |
213 | ||
fa10c70c JS |
214 | void wxHtmlPrintout::CleanUpStatics() |
215 | { | |
222ed1d6 | 216 | WX_CLEAR_LIST(wxList, m_Filters); |
fa10c70c | 217 | } |
3ce369e6 | 218 | |
fa10c70c JS |
219 | // Adds input filter |
220 | void wxHtmlPrintout::AddFilter(wxHtmlFilter *filter) | |
221 | { | |
222 | m_Filters.Append(filter); | |
223 | } | |
3ce369e6 | 224 | |
4209475c VZ |
225 | bool |
226 | wxHtmlPrintout::CheckFit(const wxSize& pageArea, const wxSize& docArea) const | |
227 | { | |
228 | if ( docArea.x > pageArea.x ) | |
229 | { | |
230 | wxMessageDialog | |
231 | dlg | |
232 | ( | |
233 | NULL, | |
234 | wxString::Format | |
235 | ( | |
236 | _("The document \"%s\" doesn't fit on the page " | |
237 | "horizontally and will be truncated if printed.\n" | |
238 | "\n" | |
239 | "Would you like to proceed with printing it nevertheless?"), | |
240 | GetTitle() | |
241 | ), | |
242 | _("Printing"), | |
9b95e87c | 243 | wxOK | wxCANCEL | wxCANCEL_DEFAULT | wxICON_QUESTION |
4209475c VZ |
244 | ); |
245 | dlg.SetExtendedMessage | |
246 | ( | |
247 | _("If possible, try changing the layout parameters to " | |
39dfe3d4 | 248 | "make the printout more narrow.") |
4209475c | 249 | ); |
3fff7533 | 250 | dlg.SetOKLabel(wxID_PRINT); |
4209475c | 251 | |
9b95e87c | 252 | if ( dlg.ShowModal() == wxID_CANCEL ) |
4209475c VZ |
253 | return false; |
254 | } | |
255 | ||
256 | return true; | |
257 | } | |
258 | ||
d2b354f9 | 259 | void wxHtmlPrintout::OnPreparePrinting() |
3ce369e6 | 260 | { |
5a21001c | 261 | int pageWidth, pageHeight, mm_w, mm_h, dc_w, dc_h; |
3ce369e6 | 262 | float ppmm_h, ppmm_v; |
04dbb646 | 263 | |
3ce369e6 VS |
264 | GetPageSizePixels(&pageWidth, &pageHeight); |
265 | GetPageSizeMM(&mm_w, &mm_h); | |
266 | ppmm_h = (float)pageWidth / mm_w; | |
267 | ppmm_v = (float)pageHeight / mm_h; | |
268 | ||
edbd0635 VS |
269 | int ppiPrinterX, ppiPrinterY; |
270 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
e822d1bd | 271 | wxUnusedVar(ppiPrinterX); |
edbd0635 VS |
272 | int ppiScreenX, ppiScreenY; |
273 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
e822d1bd | 274 | wxUnusedVar(ppiScreenX); |
edbd0635 | 275 | |
4f9297b0 | 276 | GetDC()->GetSize(&dc_w, &dc_h); |
edbd0635 | 277 | |
9f7e7edb VS |
278 | GetDC()->SetUserScale((double)dc_w / (double)pageWidth, |
279 | (double)dc_h / (double)pageHeight); | |
edbd0635 | 280 | |
3ce369e6 | 281 | /* prepare headers/footers renderer: */ |
04dbb646 | 282 | |
c44a49b8 VS |
283 | m_RendererHdr->SetDC(GetDC(), |
284 | (double)ppiPrinterY / TYPICAL_SCREEN_DPI, | |
285 | (double)ppiPrinterY / (double)ppiScreenY); | |
04dbb646 | 286 | m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), |
68be9f09 | 287 | (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom))); |
04dbb646 | 288 | if (m_Headers[0] != wxEmptyString) |
4f9297b0 VS |
289 | { |
290 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1)); | |
291 | m_HeaderHeight = m_RendererHdr->GetTotalHeight(); | |
3ce369e6 | 292 | } |
04dbb646 | 293 | else if (m_Headers[1] != wxEmptyString) |
4f9297b0 VS |
294 | { |
295 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1)); | |
296 | m_HeaderHeight = m_RendererHdr->GetTotalHeight(); | |
3ce369e6 | 297 | } |
04dbb646 | 298 | if (m_Footers[0] != wxEmptyString) |
4f9297b0 VS |
299 | { |
300 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1)); | |
301 | m_FooterHeight = m_RendererHdr->GetTotalHeight(); | |
3ce369e6 | 302 | } |
04dbb646 | 303 | else if (m_Footers[1] != wxEmptyString) |
4f9297b0 VS |
304 | { |
305 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1)); | |
306 | m_FooterHeight = m_RendererHdr->GetTotalHeight(); | |
3ce369e6 | 307 | } |
04dbb646 | 308 | |
3ce369e6 | 309 | /* prepare main renderer: */ |
c44a49b8 VS |
310 | m_Renderer->SetDC(GetDC(), |
311 | (double)ppiPrinterY / TYPICAL_SCREEN_DPI, | |
312 | (double)ppiPrinterY / (double)ppiScreenY); | |
4209475c VZ |
313 | |
314 | const int printAreaW = int(ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)); | |
315 | int printAreaH = int(ppmm_v * (mm_h - m_MarginTop - m_MarginBottom)); | |
316 | if ( m_HeaderHeight ) | |
7ff1542d | 317 | printAreaH -= int(m_HeaderHeight + m_MarginSpace * ppmm_v); |
4209475c | 318 | if ( m_FooterHeight ) |
7ff1542d | 319 | printAreaH -= int(m_FooterHeight + m_MarginSpace * ppmm_v); |
4209475c VZ |
320 | |
321 | m_Renderer->SetSize(printAreaW, printAreaH); | |
4f9297b0 | 322 | m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir); |
4209475c VZ |
323 | |
324 | if ( CheckFit(wxSize(printAreaW, printAreaH), | |
325 | wxSize(m_Renderer->GetTotalWidth(), | |
326 | m_Renderer->GetTotalHeight())) ) | |
327 | { | |
328 | // do paginate the document | |
329 | CountPages(); | |
330 | } | |
331 | //else: if we don't call CountPages() m_PageBreaks remains empty and our | |
332 | // GetPageInfo() will return 0 as max page and so nothing will be | |
333 | // printed | |
d2b354f9 JS |
334 | } |
335 | ||
336 | bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage) | |
337 | { | |
d1da8872 | 338 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) return false; |
d2b354f9 | 339 | |
d1da8872 | 340 | return true; |
3ce369e6 VS |
341 | } |
342 | ||
343 | ||
344 | bool wxHtmlPrintout::OnPrintPage(int page) | |
345 | { | |
346 | wxDC *dc = GetDC(); | |
9695c53a | 347 | if (dc && dc->IsOk()) |
4f9297b0 | 348 | { |
3ce369e6 VS |
349 | if (HasPage(page)) |
350 | RenderPage(dc, page); | |
d1da8872 | 351 | return true; |
04dbb646 | 352 | } |
d1da8872 | 353 | else return false; |
3ce369e6 VS |
354 | } |
355 | ||
356 | ||
357 | void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) | |
358 | { | |
359 | *minPage = 1; | |
b4a980f4 | 360 | if ( m_NumPages >= (signed)m_PageBreaks.GetCount()-1) |
fd0bab43 VZ |
361 | *maxPage = m_NumPages; |
362 | else | |
b4a980f4 | 363 | *maxPage = (signed)m_PageBreaks.GetCount()-1; |
3ce369e6 | 364 | *selPageFrom = 1; |
b4a980f4 | 365 | *selPageTo = (signed)m_PageBreaks.GetCount()-1; |
3ce369e6 VS |
366 | } |
367 | ||
368 | ||
369 | ||
370 | bool wxHtmlPrintout::HasPage(int pageNum) | |
371 | { | |
b4a980f4 | 372 | return pageNum > 0 && (unsigned)pageNum < m_PageBreaks.GetCount(); |
3ce369e6 VS |
373 | } |
374 | ||
375 | ||
376 | ||
377 | void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath, bool isdir) | |
378 | { | |
379 | m_Document = html; | |
380 | m_BasePath = basepath; | |
381 | m_BasePathIsDir = isdir; | |
382 | } | |
383 | ||
3ce369e6 VS |
384 | void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile) |
385 | { | |
386 | wxFileSystem fs; | |
72b1ad5c | 387 | wxFSFile *ff; |
d1da8872 | 388 | |
72b1ad5c VS |
389 | if (wxFileExists(htmlfile)) |
390 | ff = fs.OpenFile(wxFileSystem::FileNameToURL(htmlfile)); | |
391 | else | |
392 | ff = fs.OpenFile(htmlfile); | |
04dbb646 | 393 | |
f5ba273e | 394 | if (ff == NULL) |
04dbb646 | 395 | { |
f5ba273e VS |
396 | wxLogError(htmlfile + _(": file does not exist!")); |
397 | return; | |
398 | } | |
04dbb646 | 399 | |
d1da8872 | 400 | bool done = false; |
fa10c70c JS |
401 | wxHtmlFilterHTML defaultFilter; |
402 | wxString doc; | |
403 | ||
222ed1d6 | 404 | wxList::compatibility_iterator node = m_Filters.GetFirst(); |
fa10c70c JS |
405 | while (node) |
406 | { | |
407 | wxHtmlFilter *h = (wxHtmlFilter*) node->GetData(); | |
408 | if (h->CanRead(*ff)) | |
409 | { | |
410 | doc = h->ReadFile(*ff); | |
d1da8872 | 411 | done = true; |
fa10c70c JS |
412 | break; |
413 | } | |
414 | node = node->GetNext(); | |
415 | } | |
416 | ||
417 | if (!done) | |
418 | doc = defaultFilter.ReadFile(*ff); | |
d1da8872 WS |
419 | |
420 | SetHtmlText(doc, htmlfile, false); | |
2b5f62a0 | 421 | delete ff; |
3ce369e6 VS |
422 | } |
423 | ||
424 | ||
425 | ||
426 | void wxHtmlPrintout::SetHeader(const wxString& header, int pg) | |
427 | { | |
04dbb646 | 428 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 429 | m_Headers[0] = header; |
04dbb646 | 430 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
431 | m_Headers[1] = header; |
432 | } | |
433 | ||
434 | ||
435 | ||
436 | void wxHtmlPrintout::SetFooter(const wxString& footer, int pg) | |
437 | { | |
04dbb646 | 438 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 439 | m_Footers[0] = footer; |
04dbb646 | 440 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
441 | m_Footers[1] = footer; |
442 | } | |
443 | ||
444 | ||
445 | ||
446 | void wxHtmlPrintout::CountPages() | |
447 | { | |
448 | wxBusyCursor wait; | |
449 | int pageWidth, pageHeight, mm_w, mm_h; | |
450 | float ppmm_h, ppmm_v; | |
451 | ||
452 | GetPageSizePixels(&pageWidth, &pageHeight); | |
453 | GetPageSizeMM(&mm_w, &mm_h); | |
454 | ppmm_h = (float)pageWidth / mm_w; | |
455 | ppmm_v = (float)pageHeight / mm_h; | |
456 | ||
457 | int pos = 0; | |
3ce369e6 | 458 | m_NumPages = 0; |
fd0bab43 | 459 | // m_PageBreaks[0] = 0; |
04dbb646 | 460 | |
fd0bab43 VZ |
461 | m_PageBreaks.Clear(); |
462 | m_PageBreaks.Add( 0); | |
04dbb646 | 463 | do |
4f9297b0 | 464 | { |
04dbb646 | 465 | pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft), |
fd0bab43 VZ |
466 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), |
467 | m_PageBreaks, | |
468 | pos, true, INT_MAX); | |
469 | m_PageBreaks.Add( pos); | |
b4a980f4 | 470 | if( m_PageBreaks.GetCount() > wxHTML_PRINT_MAX_PAGES) |
fd0bab43 | 471 | { |
a1d5a293 | 472 | wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can't continue any longer!"), |
fd0bab43 VZ |
473 | _("Warning"), wxCANCEL | wxICON_ERROR ); |
474 | break; | |
475 | } | |
4f9297b0 | 476 | } while (pos < m_Renderer->GetTotalHeight()); |
3ce369e6 VS |
477 | } |
478 | ||
479 | ||
480 | ||
481 | void wxHtmlPrintout::RenderPage(wxDC *dc, int page) | |
482 | { | |
483 | wxBusyCursor wait; | |
484 | ||
5a21001c | 485 | int pageWidth, pageHeight, mm_w, mm_h, dc_w, dc_h; |
3ce369e6 VS |
486 | float ppmm_h, ppmm_v; |
487 | ||
488 | GetPageSizePixels(&pageWidth, &pageHeight); | |
489 | GetPageSizeMM(&mm_w, &mm_h); | |
490 | ppmm_h = (float)pageWidth / mm_w; | |
491 | ppmm_v = (float)pageHeight / mm_h; | |
4f9297b0 | 492 | dc->GetSize(&dc_w, &dc_h); |
3ce369e6 | 493 | |
edbd0635 VS |
494 | int ppiPrinterX, ppiPrinterY; |
495 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
999836aa | 496 | wxUnusedVar(ppiPrinterX); |
edbd0635 VS |
497 | int ppiScreenX, ppiScreenY; |
498 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
999836aa | 499 | wxUnusedVar(ppiScreenX); |
edbd0635 | 500 | |
9f7e7edb VS |
501 | dc->SetUserScale((double)dc_w / (double)pageWidth, |
502 | (double)dc_h / (double)pageHeight); | |
04dbb646 | 503 | |
c44a49b8 VS |
504 | m_Renderer->SetDC(dc, |
505 | (double)ppiPrinterY / TYPICAL_SCREEN_DPI, | |
506 | (double)ppiPrinterY / (double)ppiScreenY); | |
04dbb646 | 507 | |
04ee05f9 | 508 | dc->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); |
3ce369e6 | 509 | |
04dbb646 | 510 | m_Renderer->Render((int) (ppmm_h * m_MarginLeft), |
fd0bab43 | 511 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), m_PageBreaks, |
d1da8872 | 512 | m_PageBreaks[page-1], false, m_PageBreaks[page]-m_PageBreaks[page-1]); |
04dbb646 | 513 | |
fd0bab43 | 514 | |
c44a49b8 VS |
515 | m_RendererHdr->SetDC(dc, |
516 | (double)ppiPrinterY / TYPICAL_SCREEN_DPI, | |
517 | (double)ppiPrinterY / (double)ppiScreenY); | |
04dbb646 | 518 | if (m_Headers[page % 2] != wxEmptyString) |
4f9297b0 VS |
519 | { |
520 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page)); | |
fd0bab43 | 521 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop), m_PageBreaks); |
3ce369e6 | 522 | } |
04dbb646 | 523 | if (m_Footers[page % 2] != wxEmptyString) |
4f9297b0 VS |
524 | { |
525 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page)); | |
fd0bab43 | 526 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight), m_PageBreaks); |
3ce369e6 VS |
527 | } |
528 | } | |
529 | ||
530 | ||
531 | ||
532 | wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page) | |
533 | { | |
534 | wxString r = instr; | |
535 | wxString num; | |
04dbb646 | 536 | |
66a77a74 OK |
537 | num.Printf(wxT("%i"), page); |
538 | r.Replace(wxT("@PAGENUM@"), num); | |
3ce369e6 | 539 | |
b4a980f4 | 540 | num.Printf(wxT("%lu"), (unsigned long)(m_PageBreaks.GetCount() - 1)); |
66a77a74 | 541 | r.Replace(wxT("@PAGESCNT@"), num); |
3ce369e6 | 542 | |
766571a7 VZ |
543 | const wxDateTime now = wxDateTime::Now(); |
544 | r.Replace(wxT("@DATE@"), now.FormatDate()); | |
545 | r.Replace(wxT("@TIME@"), now.FormatTime()); | |
546 | ||
547 | r.Replace(wxT("@TITLE@"), GetTitle()); | |
548 | ||
3ce369e6 VS |
549 | return r; |
550 | } | |
551 | ||
552 | ||
553 | ||
554 | void wxHtmlPrintout::SetMargins(float top, float bottom, float left, float right, float spaces) | |
555 | { | |
556 | m_MarginTop = top; | |
557 | m_MarginBottom = bottom; | |
558 | m_MarginLeft = left; | |
559 | m_MarginRight = right; | |
560 | m_MarginSpace = spaces; | |
561 | } | |
562 | ||
563 | ||
564 | ||
565 | ||
fbfb8bcc | 566 | void wxHtmlPrintout::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
4eecf115 VS |
567 | const int *sizes) |
568 | { | |
569 | m_Renderer->SetFonts(normal_face, fixed_face, sizes); | |
570 | m_RendererHdr->SetFonts(normal_face, fixed_face, sizes); | |
571 | } | |
3ce369e6 | 572 | |
10e5c7ea VS |
573 | void wxHtmlPrintout::SetStandardFonts(int size, |
574 | const wxString& normal_face, | |
575 | const wxString& fixed_face) | |
7acd3625 | 576 | { |
10e5c7ea VS |
577 | m_Renderer->SetStandardFonts(size, normal_face, fixed_face); |
578 | m_RendererHdr->SetStandardFonts(size, normal_face, fixed_face); | |
7acd3625 RD |
579 | } |
580 | ||
581 | ||
4eecf115 VS |
582 | |
583 | //---------------------------------------------------------------------------- | |
3ce369e6 | 584 | // wxHtmlEasyPrinting |
4eecf115 | 585 | //---------------------------------------------------------------------------- |
3ce369e6 VS |
586 | |
587 | ||
a5ae8241 | 588 | wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxWindow *parentWindow) |
3ce369e6 | 589 | { |
a5ae8241 | 590 | m_ParentWindow = parentWindow; |
3ce369e6 | 591 | m_Name = name; |
632783de | 592 | m_PrintData = NULL; |
3ce369e6 VS |
593 | m_PageSetupData = new wxPageSetupDialogData; |
594 | m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString; | |
04dbb646 | 595 | |
d1da8872 | 596 | m_PageSetupData->EnableMargins(true); |
04dbb646 | 597 | m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25)); |
4f9297b0 | 598 | m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25)); |
4eecf115 | 599 | |
7258d995 | 600 | SetStandardFonts(DEFAULT_PRINT_FONT_SIZE); |
3ce369e6 VS |
601 | } |
602 | ||
603 | ||
604 | ||
605 | wxHtmlEasyPrinting::~wxHtmlEasyPrinting() | |
606 | { | |
607 | delete m_PrintData; | |
608 | delete m_PageSetupData; | |
609 | } | |
610 | ||
611 | ||
632783de VS |
612 | wxPrintData *wxHtmlEasyPrinting::GetPrintData() |
613 | { | |
614 | if (m_PrintData == NULL) | |
615 | m_PrintData = new wxPrintData(); | |
616 | return m_PrintData; | |
617 | } | |
618 | ||
3ce369e6 | 619 | |
f6bcfd97 | 620 | bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile) |
3ce369e6 VS |
621 | { |
622 | wxHtmlPrintout *p1 = CreatePrintout(); | |
4f9297b0 | 623 | p1->SetHtmlFile(htmlfile); |
3ce369e6 | 624 | wxHtmlPrintout *p2 = CreatePrintout(); |
4f9297b0 | 625 | p2->SetHtmlFile(htmlfile); |
f6bcfd97 | 626 | return DoPreview(p1, p2); |
3ce369e6 VS |
627 | } |
628 | ||
629 | ||
630 | ||
f6bcfd97 | 631 | bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath) |
3ce369e6 VS |
632 | { |
633 | wxHtmlPrintout *p1 = CreatePrintout(); | |
d1da8872 | 634 | p1->SetHtmlText(htmltext, basepath, true); |
3ce369e6 | 635 | wxHtmlPrintout *p2 = CreatePrintout(); |
d1da8872 | 636 | p2->SetHtmlText(htmltext, basepath, true); |
f6bcfd97 | 637 | return DoPreview(p1, p2); |
3ce369e6 VS |
638 | } |
639 | ||
640 | ||
641 | ||
f6bcfd97 | 642 | bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile) |
3ce369e6 VS |
643 | { |
644 | wxHtmlPrintout *p = CreatePrintout(); | |
4f9297b0 | 645 | p->SetHtmlFile(htmlfile); |
453507f2 VS |
646 | bool ret = DoPrint(p); |
647 | delete p; | |
648 | return ret; | |
3ce369e6 VS |
649 | } |
650 | ||
651 | ||
652 | ||
f6bcfd97 | 653 | bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath) |
3ce369e6 VS |
654 | { |
655 | wxHtmlPrintout *p = CreatePrintout(); | |
d1da8872 | 656 | p->SetHtmlText(htmltext, basepath, true); |
453507f2 VS |
657 | bool ret = DoPrint(p); |
658 | delete p; | |
659 | return ret; | |
3ce369e6 VS |
660 | } |
661 | ||
662 | ||
663 | ||
f6bcfd97 | 664 | bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2) |
3ce369e6 VS |
665 | { |
666 | // Pass two printout objects: for preview, and possible printing. | |
632783de | 667 | wxPrintDialogData printDialogData(*GetPrintData()); |
3ce369e6 | 668 | wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData); |
04dbb646 | 669 | if (!preview->Ok()) |
4f9297b0 | 670 | { |
3ce369e6 | 671 | delete preview; |
d1da8872 | 672 | return false; |
3ce369e6 | 673 | } |
3ca6a5f0 | 674 | |
a5ae8241 | 675 | wxPreviewFrame *frame = new wxPreviewFrame(preview, m_ParentWindow, |
04dbb646 | 676 | m_Name + _(" Preview"), |
3ca6a5f0 | 677 | wxPoint(100, 100), wxSize(650, 500)); |
4f9297b0 VS |
678 | frame->Centre(wxBOTH); |
679 | frame->Initialize(); | |
d1da8872 WS |
680 | frame->Show(true); |
681 | return true; | |
3ce369e6 VS |
682 | } |
683 | ||
684 | ||
685 | ||
f6bcfd97 | 686 | bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout) |
3ce369e6 | 687 | { |
632783de | 688 | wxPrintDialogData printDialogData(*GetPrintData()); |
3ce369e6 VS |
689 | wxPrinter printer(&printDialogData); |
690 | ||
d1da8872 | 691 | if (!printer.Print(m_ParentWindow, printout, true)) |
f6bcfd97 | 692 | { |
d1da8872 | 693 | return false; |
f6bcfd97 | 694 | } |
3ca6a5f0 | 695 | |
632783de | 696 | (*GetPrintData()) = printer.GetPrintDialogData().GetPrintData(); |
d1da8872 | 697 | return true; |
3ce369e6 VS |
698 | } |
699 | ||
700 | ||
701 | ||
3ce369e6 VS |
702 | |
703 | void wxHtmlEasyPrinting::PageSetup() | |
704 | { | |
632783de | 705 | if (!GetPrintData()->Ok()) |
58cf0491 | 706 | { |
936b18ac | 707 | wxLogError(_("There was a problem during page setup: you may need to set a default printer.")); |
58cf0491 JS |
708 | return; |
709 | } | |
710 | ||
632783de | 711 | m_PageSetupData->SetPrintData(*GetPrintData()); |
a5ae8241 | 712 | wxPageSetupDialog pageSetupDialog(m_ParentWindow, m_PageSetupData); |
3ce369e6 | 713 | |
04dbb646 | 714 | if (pageSetupDialog.ShowModal() == wxID_OK) |
4f9297b0 | 715 | { |
632783de | 716 | (*GetPrintData()) = pageSetupDialog.GetPageSetupData().GetPrintData(); |
3ce369e6 VS |
717 | (*m_PageSetupData) = pageSetupDialog.GetPageSetupData(); |
718 | } | |
719 | } | |
720 | ||
721 | ||
722 | ||
723 | void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg) | |
724 | { | |
04dbb646 | 725 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 726 | m_Headers[0] = header; |
04dbb646 | 727 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
728 | m_Headers[1] = header; |
729 | } | |
730 | ||
731 | ||
732 | ||
733 | void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg) | |
734 | { | |
04dbb646 | 735 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 736 | m_Footers[0] = footer; |
04dbb646 | 737 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
738 | m_Footers[1] = footer; |
739 | } | |
740 | ||
741 | ||
fbfb8bcc | 742 | void wxHtmlEasyPrinting::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
4eecf115 VS |
743 | const int *sizes) |
744 | { | |
10e5c7ea | 745 | m_fontMode = FontMode_Explicit; |
4eecf115 VS |
746 | m_FontFaceNormal = normal_face; |
747 | m_FontFaceFixed = fixed_face; | |
748 | ||
749 | if (sizes) | |
750 | { | |
751 | m_FontsSizes = m_FontsSizesArr; | |
752 | for (int i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i]; | |
753 | } | |
754 | else | |
755 | m_FontsSizes = NULL; | |
756 | } | |
757 | ||
10e5c7ea VS |
758 | void wxHtmlEasyPrinting::SetStandardFonts(int size, |
759 | const wxString& normal_face, | |
760 | const wxString& fixed_face) | |
7acd3625 | 761 | { |
10e5c7ea VS |
762 | m_fontMode = FontMode_Standard; |
763 | m_FontFaceNormal = normal_face; | |
764 | m_FontFaceFixed = fixed_face; | |
765 | m_FontsSizesArr[0] = size; | |
7acd3625 RD |
766 | } |
767 | ||
3ce369e6 VS |
768 | |
769 | wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout() | |
770 | { | |
771 | wxHtmlPrintout *p = new wxHtmlPrintout(m_Name); | |
04dbb646 | 772 | |
10e5c7ea VS |
773 | if (m_fontMode == FontMode_Explicit) |
774 | { | |
775 | p->SetFonts(m_FontFaceNormal, m_FontFaceFixed, m_FontsSizes); | |
776 | } | |
777 | else // FontMode_Standard | |
778 | { | |
779 | p->SetStandardFonts(m_FontsSizesArr[0], | |
780 | m_FontFaceNormal, m_FontFaceFixed); | |
781 | } | |
4eecf115 | 782 | |
4f9297b0 VS |
783 | p->SetHeader(m_Headers[0], wxPAGE_EVEN); |
784 | p->SetHeader(m_Headers[1], wxPAGE_ODD); | |
785 | p->SetFooter(m_Footers[0], wxPAGE_EVEN); | |
786 | p->SetFooter(m_Footers[1], wxPAGE_ODD); | |
787 | ||
788 | p->SetMargins(m_PageSetupData->GetMarginTopLeft().y, | |
789 | m_PageSetupData->GetMarginBottomRight().y, | |
790 | m_PageSetupData->GetMarginTopLeft().x, | |
791 | m_PageSetupData->GetMarginBottomRight().x); | |
04dbb646 | 792 | |
3ce369e6 VS |
793 | return p; |
794 | } | |
795 | ||
fa10c70c JS |
796 | // A module to allow initialization/cleanup |
797 | // without calling these functions from app.cpp or from | |
798 | // the user's application. | |
799 | ||
800 | class wxHtmlPrintingModule: public wxModule | |
801 | { | |
802 | DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule) | |
803 | public: | |
804 | wxHtmlPrintingModule() : wxModule() {} | |
d1da8872 | 805 | bool OnInit() { return true; } |
fa10c70c JS |
806 | void OnExit() { wxHtmlPrintout::CleanUpStatics(); } |
807 | }; | |
808 | ||
809 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule, wxModule) | |
810 | ||
3ce369e6 | 811 | |
0cecad31 VS |
812 | // This hack forces the linker to always link in m_* files |
813 | // (wxHTML doesn't work without handlers from these files) | |
814 | #include "wx/html/forcelnk.h" | |
815 | FORCE_WXHTML_MODULES() | |
3ce369e6 | 816 | |
72cdf4c9 | 817 | #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE |