]>
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 | ||
3ce369e6 VS |
40 | //-------------------------------------------------------------------------------- |
41 | // wxHtmlDCRenderer | |
42 | //-------------------------------------------------------------------------------- | |
43 | ||
44 | ||
45 | wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject() | |
46 | { | |
47 | m_DC = NULL; | |
48 | m_Width = m_Height = 0; | |
49 | m_Cells = NULL; | |
bc55e31b | 50 | m_Parser = new wxHtmlWinParser(); |
3ce369e6 | 51 | m_FS = new wxFileSystem(); |
4f9297b0 | 52 | m_Parser->SetFS(m_FS); |
7258d995 | 53 | SetStandardFonts(DEFAULT_PRINT_FONT_SIZE); |
3ce369e6 VS |
54 | } |
55 | ||
56 | ||
57 | ||
58 | wxHtmlDCRenderer::~wxHtmlDCRenderer() | |
59 | { | |
60 | if (m_Cells) delete m_Cells; | |
61 | if (m_Parser) delete m_Parser; | |
62 | if (m_FS) delete m_FS; | |
63 | } | |
64 | ||
65 | ||
66 | ||
edbd0635 | 67 | void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale) |
3ce369e6 | 68 | { |
3ce369e6 | 69 | m_DC = dc; |
4f9297b0 | 70 | m_Parser->SetDC(m_DC, pixel_scale); |
3ce369e6 VS |
71 | } |
72 | ||
73 | ||
74 | ||
75 | void wxHtmlDCRenderer::SetSize(int width, int height) | |
76 | { | |
131fc120 VS |
77 | wxCHECK_RET( width, "width must be non-zero" ); |
78 | wxCHECK_RET( height, "height must be non-zero" ); | |
79 | ||
edbd0635 VS |
80 | m_Width = width; |
81 | m_Height = height; | |
3ce369e6 VS |
82 | } |
83 | ||
84 | ||
3ce369e6 VS |
85 | void wxHtmlDCRenderer::SetHtmlText(const wxString& html, const wxString& basepath, bool isdir) |
86 | { | |
131fc120 VS |
87 | wxCHECK_RET( m_DC, "SetDC() must be called before SetHtmlText()" ); |
88 | wxCHECK_RET( m_Width, "SetSize() must be called before SetHtmlText()" ); | |
3ce369e6 | 89 | |
131fc120 | 90 | wxDELETE(m_Cells); |
04dbb646 | 91 | |
4f9297b0 VS |
92 | m_FS->ChangePathTo(basepath, isdir); |
93 | m_Cells = (wxHtmlContainerCell*) m_Parser->Parse(html); | |
94 | m_Cells->SetIndent(0, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); | |
95 | m_Cells->Layout(m_Width); | |
3ce369e6 VS |
96 | } |
97 | ||
98 | ||
fbfb8bcc | 99 | void wxHtmlDCRenderer::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
4eecf115 VS |
100 | const int *sizes) |
101 | { | |
102 | m_Parser->SetFonts(normal_face, fixed_face, sizes); | |
131fc120 VS |
103 | |
104 | if ( m_Cells ) | |
10e5c7ea | 105 | m_Cells->Layout(m_Width); |
131fc120 | 106 | // else: SetHtmlText() not yet called, no need for relayout |
4eecf115 VS |
107 | } |
108 | ||
10e5c7ea VS |
109 | void wxHtmlDCRenderer::SetStandardFonts(int size, |
110 | const wxString& normal_face, | |
111 | const wxString& fixed_face) | |
7acd3625 | 112 | { |
10e5c7ea | 113 | m_Parser->SetStandardFonts(size, normal_face, fixed_face); |
131fc120 VS |
114 | |
115 | if ( m_Cells ) | |
10e5c7ea | 116 | m_Cells->Layout(m_Width); |
131fc120 | 117 | // else: SetHtmlText() not yet called, no need for relayout |
7acd3625 RD |
118 | } |
119 | ||
fd0bab43 VZ |
120 | int wxHtmlDCRenderer::Render(int x, int y, |
121 | wxArrayInt& known_pagebreaks, | |
122 | int from, int dont_render, int to) | |
3ce369e6 | 123 | { |
131fc120 VS |
124 | wxCHECK_MSG( m_Cells, 0, "SetHtmlText() must be called before Render()" ); |
125 | wxCHECK_MSG( m_DC, 0, "SetDC() must be called before Render()" ); | |
04dbb646 | 126 | |
131fc120 | 127 | int pbreak, hght; |
04dbb646 | 128 | |
edbd0635 | 129 | pbreak = (int)(from + m_Height); |
fd0bab43 | 130 | while (m_Cells->AdjustPagebreak(&pbreak, known_pagebreaks)) {} |
edbd0635 | 131 | hght = pbreak - from; |
fd0bab43 VZ |
132 | if(to < hght) |
133 | hght = to; | |
04dbb646 VZ |
134 | |
135 | if (!dont_render) | |
4f9297b0 | 136 | { |
f30e67db VS |
137 | wxHtmlRenderingInfo rinfo; |
138 | wxDefaultHtmlRenderingStyle rstyle; | |
139 | rinfo.SetStyle(&rstyle); | |
4f9297b0 | 140 | m_DC->SetBrush(*wxWHITE_BRUSH); |
2a2e4f4a | 141 | m_DC->SetClippingRegion(x, y, m_Width, hght); |
04dbb646 | 142 | m_Cells->Draw(*m_DC, |
36c4ff4d | 143 | x, (y - from), |
26fba7b2 | 144 | y, y + hght, |
f30e67db | 145 | rinfo); |
2a2e4f4a | 146 | m_DC->DestroyClippingRegion(); |
3ce369e6 | 147 | } |
04dbb646 | 148 | |
4f9297b0 | 149 | if (pbreak < m_Cells->GetHeight()) return pbreak; |
3ce369e6 VS |
150 | else return GetTotalHeight(); |
151 | } | |
152 | ||
153 | ||
3ce369e6 VS |
154 | int wxHtmlDCRenderer::GetTotalHeight() |
155 | { | |
4f9297b0 | 156 | if (m_Cells) return m_Cells->GetHeight(); |
3ce369e6 VS |
157 | else return 0; |
158 | } | |
159 | ||
160 | ||
3ce369e6 VS |
161 | //-------------------------------------------------------------------------------- |
162 | // wxHtmlPrintout | |
163 | //-------------------------------------------------------------------------------- | |
164 | ||
165 | ||
fa10c70c | 166 | wxList wxHtmlPrintout::m_Filters; |
3ce369e6 VS |
167 | |
168 | wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title) | |
169 | { | |
170 | m_Renderer = new wxHtmlDCRenderer; | |
171 | m_RendererHdr = new wxHtmlDCRenderer; | |
efba2b89 | 172 | m_NumPages = wxHTML_PRINT_MAX_PAGES; |
d1da8872 | 173 | m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = true; |
3ce369e6 VS |
174 | m_Headers[0] = m_Headers[1] = wxEmptyString; |
175 | m_Footers[0] = m_Footers[1] = wxEmptyString; | |
176 | m_HeaderHeight = m_FooterHeight = 0; | |
177 | SetMargins(); // to default values | |
7258d995 | 178 | SetStandardFonts(DEFAULT_PRINT_FONT_SIZE); |
3ce369e6 VS |
179 | } |
180 | ||
181 | ||
182 | ||
183 | wxHtmlPrintout::~wxHtmlPrintout() | |
184 | { | |
185 | delete m_Renderer; | |
186 | delete m_RendererHdr; | |
187 | } | |
188 | ||
fa10c70c JS |
189 | void wxHtmlPrintout::CleanUpStatics() |
190 | { | |
222ed1d6 | 191 | WX_CLEAR_LIST(wxList, m_Filters); |
fa10c70c | 192 | } |
3ce369e6 | 193 | |
fa10c70c JS |
194 | // Adds input filter |
195 | void wxHtmlPrintout::AddFilter(wxHtmlFilter *filter) | |
196 | { | |
197 | m_Filters.Append(filter); | |
198 | } | |
3ce369e6 | 199 | |
d2b354f9 | 200 | void wxHtmlPrintout::OnPreparePrinting() |
3ce369e6 | 201 | { |
edbd0635 | 202 | int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; |
3ce369e6 | 203 | float ppmm_h, ppmm_v; |
04dbb646 | 204 | |
3ce369e6 VS |
205 | GetPageSizePixels(&pageWidth, &pageHeight); |
206 | GetPageSizeMM(&mm_w, &mm_h); | |
207 | ppmm_h = (float)pageWidth / mm_w; | |
208 | ppmm_v = (float)pageHeight / mm_h; | |
209 | ||
edbd0635 VS |
210 | int ppiPrinterX, ppiPrinterY; |
211 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
e822d1bd | 212 | wxUnusedVar(ppiPrinterX); |
edbd0635 VS |
213 | int ppiScreenX, ppiScreenY; |
214 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
e822d1bd | 215 | wxUnusedVar(ppiScreenX); |
edbd0635 VS |
216 | |
217 | wxDisplaySize(&scr_w, &scr_h); | |
4f9297b0 | 218 | GetDC()->GetSize(&dc_w, &dc_h); |
edbd0635 | 219 | |
9f7e7edb VS |
220 | GetDC()->SetUserScale((double)dc_w / (double)pageWidth, |
221 | (double)dc_h / (double)pageHeight); | |
edbd0635 | 222 | |
3ce369e6 | 223 | /* prepare headers/footers renderer: */ |
04dbb646 | 224 | |
4f9297b0 | 225 | m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY); |
04dbb646 | 226 | m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), |
68be9f09 | 227 | (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom))); |
04dbb646 | 228 | if (m_Headers[0] != wxEmptyString) |
4f9297b0 VS |
229 | { |
230 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1)); | |
231 | m_HeaderHeight = m_RendererHdr->GetTotalHeight(); | |
3ce369e6 | 232 | } |
04dbb646 | 233 | else if (m_Headers[1] != wxEmptyString) |
4f9297b0 VS |
234 | { |
235 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1)); | |
236 | m_HeaderHeight = m_RendererHdr->GetTotalHeight(); | |
3ce369e6 | 237 | } |
04dbb646 | 238 | if (m_Footers[0] != wxEmptyString) |
4f9297b0 VS |
239 | { |
240 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1)); | |
241 | m_FooterHeight = m_RendererHdr->GetTotalHeight(); | |
3ce369e6 | 242 | } |
04dbb646 | 243 | else if (m_Footers[1] != wxEmptyString) |
4f9297b0 VS |
244 | { |
245 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1)); | |
246 | m_FooterHeight = m_RendererHdr->GetTotalHeight(); | |
3ce369e6 | 247 | } |
04dbb646 | 248 | |
3ce369e6 | 249 | /* prepare main renderer: */ |
4f9297b0 | 250 | m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY); |
6c5b628e | 251 | m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), |
04dbb646 | 252 | (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) - |
3ce369e6 VS |
253 | m_FooterHeight - m_HeaderHeight - |
254 | ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) - | |
255 | ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v) | |
68be9f09 | 256 | )); |
4f9297b0 | 257 | m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir); |
3ce369e6 | 258 | CountPages(); |
d2b354f9 JS |
259 | } |
260 | ||
261 | bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage) | |
262 | { | |
d1da8872 | 263 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) return false; |
d2b354f9 | 264 | |
d1da8872 | 265 | return true; |
3ce369e6 VS |
266 | } |
267 | ||
268 | ||
269 | bool wxHtmlPrintout::OnPrintPage(int page) | |
270 | { | |
271 | wxDC *dc = GetDC(); | |
9695c53a | 272 | if (dc && dc->IsOk()) |
4f9297b0 | 273 | { |
3ce369e6 VS |
274 | if (HasPage(page)) |
275 | RenderPage(dc, page); | |
d1da8872 | 276 | return true; |
04dbb646 | 277 | } |
d1da8872 | 278 | else return false; |
3ce369e6 VS |
279 | } |
280 | ||
281 | ||
282 | void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) | |
283 | { | |
284 | *minPage = 1; | |
b4a980f4 | 285 | if ( m_NumPages >= (signed)m_PageBreaks.GetCount()-1) |
fd0bab43 VZ |
286 | *maxPage = m_NumPages; |
287 | else | |
b4a980f4 | 288 | *maxPage = (signed)m_PageBreaks.GetCount()-1; |
3ce369e6 | 289 | *selPageFrom = 1; |
b4a980f4 | 290 | *selPageTo = (signed)m_PageBreaks.GetCount()-1; |
3ce369e6 VS |
291 | } |
292 | ||
293 | ||
294 | ||
295 | bool wxHtmlPrintout::HasPage(int pageNum) | |
296 | { | |
b4a980f4 | 297 | return pageNum > 0 && (unsigned)pageNum < m_PageBreaks.GetCount(); |
3ce369e6 VS |
298 | } |
299 | ||
300 | ||
301 | ||
302 | void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath, bool isdir) | |
303 | { | |
304 | m_Document = html; | |
305 | m_BasePath = basepath; | |
306 | m_BasePathIsDir = isdir; | |
307 | } | |
308 | ||
3ce369e6 VS |
309 | void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile) |
310 | { | |
311 | wxFileSystem fs; | |
72b1ad5c | 312 | wxFSFile *ff; |
d1da8872 | 313 | |
72b1ad5c VS |
314 | if (wxFileExists(htmlfile)) |
315 | ff = fs.OpenFile(wxFileSystem::FileNameToURL(htmlfile)); | |
316 | else | |
317 | ff = fs.OpenFile(htmlfile); | |
04dbb646 | 318 | |
f5ba273e | 319 | if (ff == NULL) |
04dbb646 | 320 | { |
f5ba273e VS |
321 | wxLogError(htmlfile + _(": file does not exist!")); |
322 | return; | |
323 | } | |
04dbb646 | 324 | |
d1da8872 | 325 | bool done = false; |
fa10c70c JS |
326 | wxHtmlFilterHTML defaultFilter; |
327 | wxString doc; | |
328 | ||
222ed1d6 | 329 | wxList::compatibility_iterator node = m_Filters.GetFirst(); |
fa10c70c JS |
330 | while (node) |
331 | { | |
332 | wxHtmlFilter *h = (wxHtmlFilter*) node->GetData(); | |
333 | if (h->CanRead(*ff)) | |
334 | { | |
335 | doc = h->ReadFile(*ff); | |
d1da8872 | 336 | done = true; |
fa10c70c JS |
337 | break; |
338 | } | |
339 | node = node->GetNext(); | |
340 | } | |
341 | ||
342 | if (!done) | |
343 | doc = defaultFilter.ReadFile(*ff); | |
d1da8872 WS |
344 | |
345 | SetHtmlText(doc, htmlfile, false); | |
2b5f62a0 | 346 | delete ff; |
3ce369e6 VS |
347 | } |
348 | ||
349 | ||
350 | ||
351 | void wxHtmlPrintout::SetHeader(const wxString& header, int pg) | |
352 | { | |
04dbb646 | 353 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 354 | m_Headers[0] = header; |
04dbb646 | 355 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
356 | m_Headers[1] = header; |
357 | } | |
358 | ||
359 | ||
360 | ||
361 | void wxHtmlPrintout::SetFooter(const wxString& footer, int pg) | |
362 | { | |
04dbb646 | 363 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 364 | m_Footers[0] = footer; |
04dbb646 | 365 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
366 | m_Footers[1] = footer; |
367 | } | |
368 | ||
369 | ||
370 | ||
371 | void wxHtmlPrintout::CountPages() | |
372 | { | |
373 | wxBusyCursor wait; | |
374 | int pageWidth, pageHeight, mm_w, mm_h; | |
375 | float ppmm_h, ppmm_v; | |
376 | ||
377 | GetPageSizePixels(&pageWidth, &pageHeight); | |
378 | GetPageSizeMM(&mm_w, &mm_h); | |
379 | ppmm_h = (float)pageWidth / mm_w; | |
380 | ppmm_v = (float)pageHeight / mm_h; | |
381 | ||
382 | int pos = 0; | |
3ce369e6 | 383 | m_NumPages = 0; |
fd0bab43 | 384 | // m_PageBreaks[0] = 0; |
04dbb646 | 385 | |
fd0bab43 VZ |
386 | m_PageBreaks.Clear(); |
387 | m_PageBreaks.Add( 0); | |
04dbb646 | 388 | do |
4f9297b0 | 389 | { |
04dbb646 | 390 | pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft), |
fd0bab43 VZ |
391 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), |
392 | m_PageBreaks, | |
393 | pos, true, INT_MAX); | |
394 | m_PageBreaks.Add( pos); | |
b4a980f4 | 395 | if( m_PageBreaks.GetCount() > wxHTML_PRINT_MAX_PAGES) |
fd0bab43 | 396 | { |
a1d5a293 | 397 | wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can't continue any longer!"), |
fd0bab43 VZ |
398 | _("Warning"), wxCANCEL | wxICON_ERROR ); |
399 | break; | |
400 | } | |
4f9297b0 | 401 | } while (pos < m_Renderer->GetTotalHeight()); |
3ce369e6 VS |
402 | } |
403 | ||
404 | ||
405 | ||
406 | void wxHtmlPrintout::RenderPage(wxDC *dc, int page) | |
407 | { | |
408 | wxBusyCursor wait; | |
409 | ||
edbd0635 | 410 | int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; |
3ce369e6 VS |
411 | float ppmm_h, ppmm_v; |
412 | ||
413 | GetPageSizePixels(&pageWidth, &pageHeight); | |
414 | GetPageSizeMM(&mm_w, &mm_h); | |
415 | ppmm_h = (float)pageWidth / mm_w; | |
416 | ppmm_v = (float)pageHeight / mm_h; | |
edbd0635 | 417 | wxDisplaySize(&scr_w, &scr_h); |
4f9297b0 | 418 | dc->GetSize(&dc_w, &dc_h); |
3ce369e6 | 419 | |
edbd0635 VS |
420 | int ppiPrinterX, ppiPrinterY; |
421 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
999836aa | 422 | wxUnusedVar(ppiPrinterX); |
edbd0635 VS |
423 | int ppiScreenX, ppiScreenY; |
424 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
999836aa | 425 | wxUnusedVar(ppiScreenX); |
edbd0635 | 426 | |
9f7e7edb VS |
427 | dc->SetUserScale((double)dc_w / (double)pageWidth, |
428 | (double)dc_h / (double)pageHeight); | |
04dbb646 | 429 | |
4f9297b0 | 430 | m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); |
04dbb646 | 431 | |
04ee05f9 | 432 | dc->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); |
3ce369e6 | 433 | |
04dbb646 | 434 | m_Renderer->Render((int) (ppmm_h * m_MarginLeft), |
fd0bab43 | 435 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), m_PageBreaks, |
d1da8872 | 436 | m_PageBreaks[page-1], false, m_PageBreaks[page]-m_PageBreaks[page-1]); |
04dbb646 | 437 | |
fd0bab43 | 438 | |
4f9297b0 | 439 | m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); |
04dbb646 | 440 | if (m_Headers[page % 2] != wxEmptyString) |
4f9297b0 VS |
441 | { |
442 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page)); | |
fd0bab43 | 443 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop), m_PageBreaks); |
3ce369e6 | 444 | } |
04dbb646 | 445 | if (m_Footers[page % 2] != wxEmptyString) |
4f9297b0 VS |
446 | { |
447 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page)); | |
fd0bab43 | 448 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight), m_PageBreaks); |
3ce369e6 VS |
449 | } |
450 | } | |
451 | ||
452 | ||
453 | ||
454 | wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page) | |
455 | { | |
456 | wxString r = instr; | |
457 | wxString num; | |
04dbb646 | 458 | |
66a77a74 OK |
459 | num.Printf(wxT("%i"), page); |
460 | r.Replace(wxT("@PAGENUM@"), num); | |
3ce369e6 | 461 | |
b4a980f4 | 462 | num.Printf(wxT("%lu"), (unsigned long)(m_PageBreaks.GetCount() - 1)); |
66a77a74 | 463 | r.Replace(wxT("@PAGESCNT@"), num); |
3ce369e6 | 464 | |
766571a7 VZ |
465 | const wxDateTime now = wxDateTime::Now(); |
466 | r.Replace(wxT("@DATE@"), now.FormatDate()); | |
467 | r.Replace(wxT("@TIME@"), now.FormatTime()); | |
468 | ||
469 | r.Replace(wxT("@TITLE@"), GetTitle()); | |
470 | ||
3ce369e6 VS |
471 | return r; |
472 | } | |
473 | ||
474 | ||
475 | ||
476 | void wxHtmlPrintout::SetMargins(float top, float bottom, float left, float right, float spaces) | |
477 | { | |
478 | m_MarginTop = top; | |
479 | m_MarginBottom = bottom; | |
480 | m_MarginLeft = left; | |
481 | m_MarginRight = right; | |
482 | m_MarginSpace = spaces; | |
483 | } | |
484 | ||
485 | ||
486 | ||
487 | ||
fbfb8bcc | 488 | void wxHtmlPrintout::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
4eecf115 VS |
489 | const int *sizes) |
490 | { | |
491 | m_Renderer->SetFonts(normal_face, fixed_face, sizes); | |
492 | m_RendererHdr->SetFonts(normal_face, fixed_face, sizes); | |
493 | } | |
3ce369e6 | 494 | |
10e5c7ea VS |
495 | void wxHtmlPrintout::SetStandardFonts(int size, |
496 | const wxString& normal_face, | |
497 | const wxString& fixed_face) | |
7acd3625 | 498 | { |
10e5c7ea VS |
499 | m_Renderer->SetStandardFonts(size, normal_face, fixed_face); |
500 | m_RendererHdr->SetStandardFonts(size, normal_face, fixed_face); | |
7acd3625 RD |
501 | } |
502 | ||
503 | ||
4eecf115 VS |
504 | |
505 | //---------------------------------------------------------------------------- | |
3ce369e6 | 506 | // wxHtmlEasyPrinting |
4eecf115 | 507 | //---------------------------------------------------------------------------- |
3ce369e6 VS |
508 | |
509 | ||
a5ae8241 | 510 | wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxWindow *parentWindow) |
3ce369e6 | 511 | { |
a5ae8241 | 512 | m_ParentWindow = parentWindow; |
3ce369e6 | 513 | m_Name = name; |
632783de | 514 | m_PrintData = NULL; |
3ce369e6 VS |
515 | m_PageSetupData = new wxPageSetupDialogData; |
516 | m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString; | |
04dbb646 | 517 | |
d1da8872 | 518 | m_PageSetupData->EnableMargins(true); |
04dbb646 | 519 | m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25)); |
4f9297b0 | 520 | m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25)); |
4eecf115 | 521 | |
7258d995 | 522 | SetStandardFonts(DEFAULT_PRINT_FONT_SIZE); |
3ce369e6 VS |
523 | } |
524 | ||
525 | ||
526 | ||
527 | wxHtmlEasyPrinting::~wxHtmlEasyPrinting() | |
528 | { | |
529 | delete m_PrintData; | |
530 | delete m_PageSetupData; | |
531 | } | |
532 | ||
533 | ||
632783de VS |
534 | wxPrintData *wxHtmlEasyPrinting::GetPrintData() |
535 | { | |
536 | if (m_PrintData == NULL) | |
537 | m_PrintData = new wxPrintData(); | |
538 | return m_PrintData; | |
539 | } | |
540 | ||
3ce369e6 | 541 | |
f6bcfd97 | 542 | bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile) |
3ce369e6 VS |
543 | { |
544 | wxHtmlPrintout *p1 = CreatePrintout(); | |
4f9297b0 | 545 | p1->SetHtmlFile(htmlfile); |
3ce369e6 | 546 | wxHtmlPrintout *p2 = CreatePrintout(); |
4f9297b0 | 547 | p2->SetHtmlFile(htmlfile); |
f6bcfd97 | 548 | return DoPreview(p1, p2); |
3ce369e6 VS |
549 | } |
550 | ||
551 | ||
552 | ||
f6bcfd97 | 553 | bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath) |
3ce369e6 VS |
554 | { |
555 | wxHtmlPrintout *p1 = CreatePrintout(); | |
d1da8872 | 556 | p1->SetHtmlText(htmltext, basepath, true); |
3ce369e6 | 557 | wxHtmlPrintout *p2 = CreatePrintout(); |
d1da8872 | 558 | p2->SetHtmlText(htmltext, basepath, true); |
f6bcfd97 | 559 | return DoPreview(p1, p2); |
3ce369e6 VS |
560 | } |
561 | ||
562 | ||
563 | ||
f6bcfd97 | 564 | bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile) |
3ce369e6 VS |
565 | { |
566 | wxHtmlPrintout *p = CreatePrintout(); | |
4f9297b0 | 567 | p->SetHtmlFile(htmlfile); |
453507f2 VS |
568 | bool ret = DoPrint(p); |
569 | delete p; | |
570 | return ret; | |
3ce369e6 VS |
571 | } |
572 | ||
573 | ||
574 | ||
f6bcfd97 | 575 | bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath) |
3ce369e6 VS |
576 | { |
577 | wxHtmlPrintout *p = CreatePrintout(); | |
d1da8872 | 578 | p->SetHtmlText(htmltext, basepath, true); |
453507f2 VS |
579 | bool ret = DoPrint(p); |
580 | delete p; | |
581 | return ret; | |
3ce369e6 VS |
582 | } |
583 | ||
584 | ||
585 | ||
f6bcfd97 | 586 | bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2) |
3ce369e6 VS |
587 | { |
588 | // Pass two printout objects: for preview, and possible printing. | |
632783de | 589 | wxPrintDialogData printDialogData(*GetPrintData()); |
3ce369e6 | 590 | wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData); |
04dbb646 | 591 | if (!preview->Ok()) |
4f9297b0 | 592 | { |
3ce369e6 | 593 | delete preview; |
d1da8872 | 594 | return false; |
3ce369e6 | 595 | } |
3ca6a5f0 | 596 | |
a5ae8241 | 597 | wxPreviewFrame *frame = new wxPreviewFrame(preview, m_ParentWindow, |
04dbb646 | 598 | m_Name + _(" Preview"), |
3ca6a5f0 | 599 | wxPoint(100, 100), wxSize(650, 500)); |
4f9297b0 VS |
600 | frame->Centre(wxBOTH); |
601 | frame->Initialize(); | |
d1da8872 WS |
602 | frame->Show(true); |
603 | return true; | |
3ce369e6 VS |
604 | } |
605 | ||
606 | ||
607 | ||
f6bcfd97 | 608 | bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout) |
3ce369e6 | 609 | { |
632783de | 610 | wxPrintDialogData printDialogData(*GetPrintData()); |
3ce369e6 VS |
611 | wxPrinter printer(&printDialogData); |
612 | ||
d1da8872 | 613 | if (!printer.Print(m_ParentWindow, printout, true)) |
f6bcfd97 | 614 | { |
d1da8872 | 615 | return false; |
f6bcfd97 | 616 | } |
3ca6a5f0 | 617 | |
632783de | 618 | (*GetPrintData()) = printer.GetPrintDialogData().GetPrintData(); |
d1da8872 | 619 | return true; |
3ce369e6 VS |
620 | } |
621 | ||
622 | ||
623 | ||
3ce369e6 VS |
624 | |
625 | void wxHtmlEasyPrinting::PageSetup() | |
626 | { | |
632783de | 627 | if (!GetPrintData()->Ok()) |
58cf0491 | 628 | { |
936b18ac | 629 | wxLogError(_("There was a problem during page setup: you may need to set a default printer.")); |
58cf0491 JS |
630 | return; |
631 | } | |
632 | ||
632783de | 633 | m_PageSetupData->SetPrintData(*GetPrintData()); |
a5ae8241 | 634 | wxPageSetupDialog pageSetupDialog(m_ParentWindow, m_PageSetupData); |
3ce369e6 | 635 | |
04dbb646 | 636 | if (pageSetupDialog.ShowModal() == wxID_OK) |
4f9297b0 | 637 | { |
632783de | 638 | (*GetPrintData()) = pageSetupDialog.GetPageSetupData().GetPrintData(); |
3ce369e6 VS |
639 | (*m_PageSetupData) = pageSetupDialog.GetPageSetupData(); |
640 | } | |
641 | } | |
642 | ||
643 | ||
644 | ||
645 | void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg) | |
646 | { | |
04dbb646 | 647 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 648 | m_Headers[0] = header; |
04dbb646 | 649 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
650 | m_Headers[1] = header; |
651 | } | |
652 | ||
653 | ||
654 | ||
655 | void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg) | |
656 | { | |
04dbb646 | 657 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 658 | m_Footers[0] = footer; |
04dbb646 | 659 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
660 | m_Footers[1] = footer; |
661 | } | |
662 | ||
663 | ||
fbfb8bcc | 664 | void wxHtmlEasyPrinting::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
4eecf115 VS |
665 | const int *sizes) |
666 | { | |
10e5c7ea | 667 | m_fontMode = FontMode_Explicit; |
4eecf115 VS |
668 | m_FontFaceNormal = normal_face; |
669 | m_FontFaceFixed = fixed_face; | |
670 | ||
671 | if (sizes) | |
672 | { | |
673 | m_FontsSizes = m_FontsSizesArr; | |
674 | for (int i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i]; | |
675 | } | |
676 | else | |
677 | m_FontsSizes = NULL; | |
678 | } | |
679 | ||
10e5c7ea VS |
680 | void wxHtmlEasyPrinting::SetStandardFonts(int size, |
681 | const wxString& normal_face, | |
682 | const wxString& fixed_face) | |
7acd3625 | 683 | { |
10e5c7ea VS |
684 | m_fontMode = FontMode_Standard; |
685 | m_FontFaceNormal = normal_face; | |
686 | m_FontFaceFixed = fixed_face; | |
687 | m_FontsSizesArr[0] = size; | |
7acd3625 RD |
688 | } |
689 | ||
3ce369e6 VS |
690 | |
691 | wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout() | |
692 | { | |
693 | wxHtmlPrintout *p = new wxHtmlPrintout(m_Name); | |
04dbb646 | 694 | |
10e5c7ea VS |
695 | if (m_fontMode == FontMode_Explicit) |
696 | { | |
697 | p->SetFonts(m_FontFaceNormal, m_FontFaceFixed, m_FontsSizes); | |
698 | } | |
699 | else // FontMode_Standard | |
700 | { | |
701 | p->SetStandardFonts(m_FontsSizesArr[0], | |
702 | m_FontFaceNormal, m_FontFaceFixed); | |
703 | } | |
4eecf115 | 704 | |
4f9297b0 VS |
705 | p->SetHeader(m_Headers[0], wxPAGE_EVEN); |
706 | p->SetHeader(m_Headers[1], wxPAGE_ODD); | |
707 | p->SetFooter(m_Footers[0], wxPAGE_EVEN); | |
708 | p->SetFooter(m_Footers[1], wxPAGE_ODD); | |
709 | ||
710 | p->SetMargins(m_PageSetupData->GetMarginTopLeft().y, | |
711 | m_PageSetupData->GetMarginBottomRight().y, | |
712 | m_PageSetupData->GetMarginTopLeft().x, | |
713 | m_PageSetupData->GetMarginBottomRight().x); | |
04dbb646 | 714 | |
3ce369e6 VS |
715 | return p; |
716 | } | |
717 | ||
fa10c70c JS |
718 | // A module to allow initialization/cleanup |
719 | // without calling these functions from app.cpp or from | |
720 | // the user's application. | |
721 | ||
722 | class wxHtmlPrintingModule: public wxModule | |
723 | { | |
724 | DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule) | |
725 | public: | |
726 | wxHtmlPrintingModule() : wxModule() {} | |
d1da8872 | 727 | bool OnInit() { return true; } |
fa10c70c JS |
728 | void OnExit() { wxHtmlPrintout::CleanUpStatics(); } |
729 | }; | |
730 | ||
731 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule, wxModule) | |
732 | ||
3ce369e6 | 733 | |
0cecad31 VS |
734 | // This hack forces the linker to always link in m_* files |
735 | // (wxHTML doesn't work without handlers from these files) | |
736 | #include "wx/html/forcelnk.h" | |
737 | FORCE_WXHTML_MODULES() | |
3ce369e6 | 738 | |
72cdf4c9 | 739 | #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE |