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