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