]>
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 | ||
154 | int wxHtmlDCRenderer::GetTotalHeight() | |
155 | { | |
156 | if (m_Cells) return m_Cells->GetHeight(); | |
157 | else return 0; | |
158 | } | |
159 | ||
160 | ||
161 | //-------------------------------------------------------------------------------- | |
162 | // wxHtmlPrintout | |
163 | //-------------------------------------------------------------------------------- | |
164 | ||
165 | ||
166 | wxList wxHtmlPrintout::m_Filters; | |
167 | ||
168 | wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title) | |
169 | { | |
170 | m_Renderer = new wxHtmlDCRenderer; | |
171 | m_RendererHdr = new wxHtmlDCRenderer; | |
172 | m_NumPages = wxHTML_PRINT_MAX_PAGES; | |
173 | m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = true; | |
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 | |
178 | SetStandardFonts(DEFAULT_PRINT_FONT_SIZE); | |
179 | } | |
180 | ||
181 | ||
182 | ||
183 | wxHtmlPrintout::~wxHtmlPrintout() | |
184 | { | |
185 | delete m_Renderer; | |
186 | delete m_RendererHdr; | |
187 | } | |
188 | ||
189 | void wxHtmlPrintout::CleanUpStatics() | |
190 | { | |
191 | WX_CLEAR_LIST(wxList, m_Filters); | |
192 | } | |
193 | ||
194 | // Adds input filter | |
195 | void wxHtmlPrintout::AddFilter(wxHtmlFilter *filter) | |
196 | { | |
197 | m_Filters.Append(filter); | |
198 | } | |
199 | ||
200 | void wxHtmlPrintout::OnPreparePrinting() | |
201 | { | |
202 | int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; | |
203 | float ppmm_h, ppmm_v; | |
204 | ||
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 | ||
210 | int ppiPrinterX, ppiPrinterY; | |
211 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
212 | wxUnusedVar(ppiPrinterX); | |
213 | int ppiScreenX, ppiScreenY; | |
214 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
215 | wxUnusedVar(ppiScreenX); | |
216 | ||
217 | wxDisplaySize(&scr_w, &scr_h); | |
218 | GetDC()->GetSize(&dc_w, &dc_h); | |
219 | ||
220 | GetDC()->SetUserScale((double)dc_w / (double)pageWidth, | |
221 | (double)dc_h / (double)pageHeight); | |
222 | ||
223 | /* prepare headers/footers renderer: */ | |
224 | ||
225 | m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY); | |
226 | m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), | |
227 | (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom))); | |
228 | if (m_Headers[0] != wxEmptyString) | |
229 | { | |
230 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1)); | |
231 | m_HeaderHeight = m_RendererHdr->GetTotalHeight(); | |
232 | } | |
233 | else if (m_Headers[1] != wxEmptyString) | |
234 | { | |
235 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1)); | |
236 | m_HeaderHeight = m_RendererHdr->GetTotalHeight(); | |
237 | } | |
238 | if (m_Footers[0] != wxEmptyString) | |
239 | { | |
240 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1)); | |
241 | m_FooterHeight = m_RendererHdr->GetTotalHeight(); | |
242 | } | |
243 | else if (m_Footers[1] != wxEmptyString) | |
244 | { | |
245 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1)); | |
246 | m_FooterHeight = m_RendererHdr->GetTotalHeight(); | |
247 | } | |
248 | ||
249 | /* prepare main renderer: */ | |
250 | m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY); | |
251 | m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), | |
252 | (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) - | |
253 | m_FooterHeight - m_HeaderHeight - | |
254 | ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) - | |
255 | ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v) | |
256 | )); | |
257 | m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir); | |
258 | CountPages(); | |
259 | } | |
260 | ||
261 | bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage) | |
262 | { | |
263 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) return false; | |
264 | ||
265 | return true; | |
266 | } | |
267 | ||
268 | ||
269 | bool wxHtmlPrintout::OnPrintPage(int page) | |
270 | { | |
271 | wxDC *dc = GetDC(); | |
272 | if (dc && dc->IsOk()) | |
273 | { | |
274 | if (HasPage(page)) | |
275 | RenderPage(dc, page); | |
276 | return true; | |
277 | } | |
278 | else return false; | |
279 | } | |
280 | ||
281 | ||
282 | void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) | |
283 | { | |
284 | *minPage = 1; | |
285 | if ( m_NumPages >= (signed)m_PageBreaks.GetCount()-1) | |
286 | *maxPage = m_NumPages; | |
287 | else | |
288 | *maxPage = (signed)m_PageBreaks.GetCount()-1; | |
289 | *selPageFrom = 1; | |
290 | *selPageTo = (signed)m_PageBreaks.GetCount()-1; | |
291 | } | |
292 | ||
293 | ||
294 | ||
295 | bool wxHtmlPrintout::HasPage(int pageNum) | |
296 | { | |
297 | return pageNum > 0 && (unsigned)pageNum < m_PageBreaks.GetCount(); | |
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 | ||
309 | void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile) | |
310 | { | |
311 | wxFileSystem fs; | |
312 | wxFSFile *ff; | |
313 | ||
314 | if (wxFileExists(htmlfile)) | |
315 | ff = fs.OpenFile(wxFileSystem::FileNameToURL(htmlfile)); | |
316 | else | |
317 | ff = fs.OpenFile(htmlfile); | |
318 | ||
319 | if (ff == NULL) | |
320 | { | |
321 | wxLogError(htmlfile + _(": file does not exist!")); | |
322 | return; | |
323 | } | |
324 | ||
325 | bool done = false; | |
326 | wxHtmlFilterHTML defaultFilter; | |
327 | wxString doc; | |
328 | ||
329 | wxList::compatibility_iterator node = m_Filters.GetFirst(); | |
330 | while (node) | |
331 | { | |
332 | wxHtmlFilter *h = (wxHtmlFilter*) node->GetData(); | |
333 | if (h->CanRead(*ff)) | |
334 | { | |
335 | doc = h->ReadFile(*ff); | |
336 | done = true; | |
337 | break; | |
338 | } | |
339 | node = node->GetNext(); | |
340 | } | |
341 | ||
342 | if (!done) | |
343 | doc = defaultFilter.ReadFile(*ff); | |
344 | ||
345 | SetHtmlText(doc, htmlfile, false); | |
346 | delete ff; | |
347 | } | |
348 | ||
349 | ||
350 | ||
351 | void wxHtmlPrintout::SetHeader(const wxString& header, int pg) | |
352 | { | |
353 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) | |
354 | m_Headers[0] = header; | |
355 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) | |
356 | m_Headers[1] = header; | |
357 | } | |
358 | ||
359 | ||
360 | ||
361 | void wxHtmlPrintout::SetFooter(const wxString& footer, int pg) | |
362 | { | |
363 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) | |
364 | m_Footers[0] = footer; | |
365 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) | |
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; | |
383 | m_NumPages = 0; | |
384 | // m_PageBreaks[0] = 0; | |
385 | ||
386 | m_PageBreaks.Clear(); | |
387 | m_PageBreaks.Add( 0); | |
388 | do | |
389 | { | |
390 | pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft), | |
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); | |
395 | if( m_PageBreaks.GetCount() > wxHTML_PRINT_MAX_PAGES) | |
396 | { | |
397 | wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can't continue any longer!"), | |
398 | _("Warning"), wxCANCEL | wxICON_ERROR ); | |
399 | break; | |
400 | } | |
401 | } while (pos < m_Renderer->GetTotalHeight()); | |
402 | } | |
403 | ||
404 | ||
405 | ||
406 | void wxHtmlPrintout::RenderPage(wxDC *dc, int page) | |
407 | { | |
408 | wxBusyCursor wait; | |
409 | ||
410 | int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; | |
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; | |
417 | wxDisplaySize(&scr_w, &scr_h); | |
418 | dc->GetSize(&dc_w, &dc_h); | |
419 | ||
420 | int ppiPrinterX, ppiPrinterY; | |
421 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
422 | wxUnusedVar(ppiPrinterX); | |
423 | int ppiScreenX, ppiScreenY; | |
424 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
425 | wxUnusedVar(ppiScreenX); | |
426 | ||
427 | dc->SetUserScale((double)dc_w / (double)pageWidth, | |
428 | (double)dc_h / (double)pageHeight); | |
429 | ||
430 | m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); | |
431 | ||
432 | dc->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); | |
433 | ||
434 | m_Renderer->Render((int) (ppmm_h * m_MarginLeft), | |
435 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), m_PageBreaks, | |
436 | m_PageBreaks[page-1], false, m_PageBreaks[page]-m_PageBreaks[page-1]); | |
437 | ||
438 | ||
439 | m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); | |
440 | if (m_Headers[page % 2] != wxEmptyString) | |
441 | { | |
442 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page)); | |
443 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop), m_PageBreaks); | |
444 | } | |
445 | if (m_Footers[page % 2] != wxEmptyString) | |
446 | { | |
447 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page)); | |
448 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight), m_PageBreaks); | |
449 | } | |
450 | } | |
451 | ||
452 | ||
453 | ||
454 | wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page) | |
455 | { | |
456 | wxString r = instr; | |
457 | wxString num; | |
458 | ||
459 | num.Printf(wxT("%i"), page); | |
460 | r.Replace(wxT("@PAGENUM@"), num); | |
461 | ||
462 | num.Printf(wxT("%lu"), (unsigned long)(m_PageBreaks.GetCount() - 1)); | |
463 | r.Replace(wxT("@PAGESCNT@"), num); | |
464 | ||
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 | ||
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 | ||
488 | void wxHtmlPrintout::SetFonts(const wxString& normal_face, const wxString& fixed_face, | |
489 | const int *sizes) | |
490 | { | |
491 | m_Renderer->SetFonts(normal_face, fixed_face, sizes); | |
492 | m_RendererHdr->SetFonts(normal_face, fixed_face, sizes); | |
493 | } | |
494 | ||
495 | void wxHtmlPrintout::SetStandardFonts(int size, | |
496 | const wxString& normal_face, | |
497 | const wxString& fixed_face) | |
498 | { | |
499 | m_Renderer->SetStandardFonts(size, normal_face, fixed_face); | |
500 | m_RendererHdr->SetStandardFonts(size, normal_face, fixed_face); | |
501 | } | |
502 | ||
503 | ||
504 | ||
505 | //---------------------------------------------------------------------------- | |
506 | // wxHtmlEasyPrinting | |
507 | //---------------------------------------------------------------------------- | |
508 | ||
509 | ||
510 | wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxWindow *parentWindow) | |
511 | { | |
512 | m_ParentWindow = parentWindow; | |
513 | m_Name = name; | |
514 | m_PrintData = NULL; | |
515 | m_PageSetupData = new wxPageSetupDialogData; | |
516 | m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString; | |
517 | ||
518 | m_PageSetupData->EnableMargins(true); | |
519 | m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25)); | |
520 | m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25)); | |
521 | ||
522 | SetStandardFonts(DEFAULT_PRINT_FONT_SIZE); | |
523 | } | |
524 | ||
525 | ||
526 | ||
527 | wxHtmlEasyPrinting::~wxHtmlEasyPrinting() | |
528 | { | |
529 | delete m_PrintData; | |
530 | delete m_PageSetupData; | |
531 | } | |
532 | ||
533 | ||
534 | wxPrintData *wxHtmlEasyPrinting::GetPrintData() | |
535 | { | |
536 | if (m_PrintData == NULL) | |
537 | m_PrintData = new wxPrintData(); | |
538 | return m_PrintData; | |
539 | } | |
540 | ||
541 | ||
542 | bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile) | |
543 | { | |
544 | wxHtmlPrintout *p1 = CreatePrintout(); | |
545 | p1->SetHtmlFile(htmlfile); | |
546 | wxHtmlPrintout *p2 = CreatePrintout(); | |
547 | p2->SetHtmlFile(htmlfile); | |
548 | return DoPreview(p1, p2); | |
549 | } | |
550 | ||
551 | ||
552 | ||
553 | bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath) | |
554 | { | |
555 | wxHtmlPrintout *p1 = CreatePrintout(); | |
556 | p1->SetHtmlText(htmltext, basepath, true); | |
557 | wxHtmlPrintout *p2 = CreatePrintout(); | |
558 | p2->SetHtmlText(htmltext, basepath, true); | |
559 | return DoPreview(p1, p2); | |
560 | } | |
561 | ||
562 | ||
563 | ||
564 | bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile) | |
565 | { | |
566 | wxHtmlPrintout *p = CreatePrintout(); | |
567 | p->SetHtmlFile(htmlfile); | |
568 | bool ret = DoPrint(p); | |
569 | delete p; | |
570 | return ret; | |
571 | } | |
572 | ||
573 | ||
574 | ||
575 | bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath) | |
576 | { | |
577 | wxHtmlPrintout *p = CreatePrintout(); | |
578 | p->SetHtmlText(htmltext, basepath, true); | |
579 | bool ret = DoPrint(p); | |
580 | delete p; | |
581 | return ret; | |
582 | } | |
583 | ||
584 | ||
585 | ||
586 | bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2) | |
587 | { | |
588 | // Pass two printout objects: for preview, and possible printing. | |
589 | wxPrintDialogData printDialogData(*GetPrintData()); | |
590 | wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData); | |
591 | if (!preview->Ok()) | |
592 | { | |
593 | delete preview; | |
594 | return false; | |
595 | } | |
596 | ||
597 | wxPreviewFrame *frame = new wxPreviewFrame(preview, m_ParentWindow, | |
598 | m_Name + _(" Preview"), | |
599 | wxPoint(100, 100), wxSize(650, 500)); | |
600 | frame->Centre(wxBOTH); | |
601 | frame->Initialize(); | |
602 | frame->Show(true); | |
603 | return true; | |
604 | } | |
605 | ||
606 | ||
607 | ||
608 | bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout) | |
609 | { | |
610 | wxPrintDialogData printDialogData(*GetPrintData()); | |
611 | wxPrinter printer(&printDialogData); | |
612 | ||
613 | if (!printer.Print(m_ParentWindow, printout, true)) | |
614 | { | |
615 | return false; | |
616 | } | |
617 | ||
618 | (*GetPrintData()) = printer.GetPrintDialogData().GetPrintData(); | |
619 | return true; | |
620 | } | |
621 | ||
622 | ||
623 | ||
624 | ||
625 | void wxHtmlEasyPrinting::PageSetup() | |
626 | { | |
627 | if (!GetPrintData()->Ok()) | |
628 | { | |
629 | wxLogError(_("There was a problem during page setup: you may need to set a default printer.")); | |
630 | return; | |
631 | } | |
632 | ||
633 | m_PageSetupData->SetPrintData(*GetPrintData()); | |
634 | wxPageSetupDialog pageSetupDialog(m_ParentWindow, m_PageSetupData); | |
635 | ||
636 | if (pageSetupDialog.ShowModal() == wxID_OK) | |
637 | { | |
638 | (*GetPrintData()) = pageSetupDialog.GetPageSetupData().GetPrintData(); | |
639 | (*m_PageSetupData) = pageSetupDialog.GetPageSetupData(); | |
640 | } | |
641 | } | |
642 | ||
643 | ||
644 | ||
645 | void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg) | |
646 | { | |
647 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) | |
648 | m_Headers[0] = header; | |
649 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) | |
650 | m_Headers[1] = header; | |
651 | } | |
652 | ||
653 | ||
654 | ||
655 | void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg) | |
656 | { | |
657 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) | |
658 | m_Footers[0] = footer; | |
659 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) | |
660 | m_Footers[1] = footer; | |
661 | } | |
662 | ||
663 | ||
664 | void wxHtmlEasyPrinting::SetFonts(const wxString& normal_face, const wxString& fixed_face, | |
665 | const int *sizes) | |
666 | { | |
667 | m_fontMode = FontMode_Explicit; | |
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 | ||
680 | void wxHtmlEasyPrinting::SetStandardFonts(int size, | |
681 | const wxString& normal_face, | |
682 | const wxString& fixed_face) | |
683 | { | |
684 | m_fontMode = FontMode_Standard; | |
685 | m_FontFaceNormal = normal_face; | |
686 | m_FontFaceFixed = fixed_face; | |
687 | m_FontsSizesArr[0] = size; | |
688 | } | |
689 | ||
690 | ||
691 | wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout() | |
692 | { | |
693 | wxHtmlPrintout *p = new wxHtmlPrintout(m_Name); | |
694 | ||
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 | } | |
704 | ||
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); | |
714 | ||
715 | return p; | |
716 | } | |
717 | ||
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() {} | |
727 | bool OnInit() { return true; } | |
728 | void OnExit() { wxHtmlPrintout::CleanUpStatics(); } | |
729 | }; | |
730 | ||
731 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule, wxModule) | |
732 | ||
733 | ||
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() | |
738 | ||
739 | #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE |