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