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