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