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