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