]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmprint.cpp
more warning fixes; removed references to wxToolBarBase from wxRTTI
[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 WX_CLEAR_LIST(wxList, m_Filters);
173 }
174
175 // Adds input filter
176 void wxHtmlPrintout::AddFilter(wxHtmlFilter *filter)
177 {
178 m_Filters.Append(filter);
179 }
180
181 bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage)
182 {
183 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
184 float ppmm_h, ppmm_v;
185
186 if (!wxPrintout::OnBeginDocument(startPage, endPage)) return FALSE;
187
188 GetPageSizePixels(&pageWidth, &pageHeight);
189 GetPageSizeMM(&mm_w, &mm_h);
190 ppmm_h = (float)pageWidth / mm_w;
191 ppmm_v = (float)pageHeight / mm_h;
192
193 int ppiPrinterX, ppiPrinterY;
194 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
195 int ppiScreenX, ppiScreenY;
196 GetPPIScreen(&ppiScreenX, &ppiScreenY);
197
198 wxDisplaySize(&scr_w, &scr_h);
199 GetDC()->GetSize(&dc_w, &dc_h);
200
201 GetDC()->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
202
203 /* prepare headers/footers renderer: */
204
205 m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
206 m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
207 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom)));
208 if (m_Headers[0] != wxEmptyString)
209 {
210 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1));
211 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
212 }
213 else if (m_Headers[1] != wxEmptyString)
214 {
215 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1));
216 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
217 }
218 if (m_Footers[0] != wxEmptyString)
219 {
220 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1));
221 m_FooterHeight = m_RendererHdr->GetTotalHeight();
222 }
223 else if (m_Footers[1] != wxEmptyString)
224 {
225 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1));
226 m_FooterHeight = m_RendererHdr->GetTotalHeight();
227 }
228
229 /* prepare main renderer: */
230 m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
231 m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
232 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) -
233 m_FooterHeight - m_HeaderHeight -
234 ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) -
235 ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v)
236 ));
237 m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir);
238 CountPages();
239 return TRUE;
240 }
241
242
243 bool wxHtmlPrintout::OnPrintPage(int page)
244 {
245 wxDC *dc = GetDC();
246 if (dc)
247 {
248 if (HasPage(page))
249 RenderPage(dc, page);
250 return TRUE;
251 }
252 else return FALSE;
253 }
254
255
256 void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
257 {
258 *minPage = 1;
259 *maxPage = wxHTML_PRINT_MAX_PAGES;
260 *selPageFrom = 1;
261 *selPageTo = wxHTML_PRINT_MAX_PAGES;
262 }
263
264
265
266 bool wxHtmlPrintout::HasPage(int pageNum)
267 {
268 return (pageNum >= 1 && pageNum <= m_NumPages);
269 }
270
271
272
273 void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath, bool isdir)
274 {
275 m_Document = html;
276 m_BasePath = basepath;
277 m_BasePathIsDir = isdir;
278 }
279
280 void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile)
281 {
282 wxFileSystem fs;
283 wxFSFile *ff = fs.OpenFile(htmlfile);
284
285 if (ff == NULL)
286 {
287 wxLogError(htmlfile + _(": file does not exist!"));
288 return;
289 }
290
291 bool done = FALSE;
292 wxHtmlFilterHTML defaultFilter;
293 wxString doc;
294
295 wxList::compatibility_iterator node = m_Filters.GetFirst();
296 while (node)
297 {
298 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
299 if (h->CanRead(*ff))
300 {
301 doc = h->ReadFile(*ff);
302 done = TRUE;
303 break;
304 }
305 node = node->GetNext();
306 }
307
308 if (!done)
309 doc = defaultFilter.ReadFile(*ff);
310
311 SetHtmlText(doc, htmlfile, FALSE);
312 delete ff;
313 }
314
315
316
317 void wxHtmlPrintout::SetHeader(const wxString& header, int pg)
318 {
319 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
320 m_Headers[0] = header;
321 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
322 m_Headers[1] = header;
323 }
324
325
326
327 void wxHtmlPrintout::SetFooter(const wxString& footer, int pg)
328 {
329 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
330 m_Footers[0] = footer;
331 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
332 m_Footers[1] = footer;
333 }
334
335
336
337 void wxHtmlPrintout::CountPages()
338 {
339 wxBusyCursor wait;
340 int pageWidth, pageHeight, mm_w, mm_h;
341 float ppmm_h, ppmm_v;
342
343 GetPageSizePixels(&pageWidth, &pageHeight);
344 GetPageSizeMM(&mm_w, &mm_h);
345 ppmm_h = (float)pageWidth / mm_w;
346 ppmm_v = (float)pageHeight / mm_h;
347
348 int pos = 0;
349
350 m_NumPages = 0;
351
352 m_PageBreaks[0] = 0;
353 do
354 {
355 pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft),
356 (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
357 pos, TRUE, INT_MAX, m_PageBreaks, m_NumPages);
358 m_PageBreaks[++m_NumPages] = pos;
359 } while (pos < m_Renderer->GetTotalHeight());
360 }
361
362
363
364 void wxHtmlPrintout::RenderPage(wxDC *dc, int page)
365 {
366 wxBusyCursor wait;
367
368 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
369 float ppmm_h, ppmm_v;
370
371 GetPageSizePixels(&pageWidth, &pageHeight);
372 GetPageSizeMM(&mm_w, &mm_h);
373 ppmm_h = (float)pageWidth / mm_w;
374 ppmm_v = (float)pageHeight / mm_h;
375 wxDisplaySize(&scr_w, &scr_h);
376 dc->GetSize(&dc_w, &dc_h);
377
378 int ppiPrinterX, ppiPrinterY;
379 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
380 int ppiScreenX, ppiScreenY;
381 GetPPIScreen(&ppiScreenX, &ppiScreenY);
382
383 dc->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
384
385 m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY);
386
387 dc->SetBackgroundMode(wxTRANSPARENT);
388
389 m_Renderer->Render((int) (ppmm_h * m_MarginLeft),
390 (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
391 m_PageBreaks[page-1], FALSE, m_PageBreaks[page]-m_PageBreaks[page-1]);
392
393 m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY);
394 if (m_Headers[page % 2] != wxEmptyString)
395 {
396 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page));
397 m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop));
398 }
399 if (m_Footers[page % 2] != wxEmptyString)
400 {
401 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page));
402 m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight));
403 }
404 }
405
406
407
408 wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page)
409 {
410 wxString r = instr;
411 wxString num;
412
413 num.Printf(wxT("%i"), page);
414 r.Replace(wxT("@PAGENUM@"), num);
415
416 num.Printf(wxT("%i"), m_NumPages);
417 r.Replace(wxT("@PAGESCNT@"), num);
418
419 return r;
420 }
421
422
423
424 void wxHtmlPrintout::SetMargins(float top, float bottom, float left, float right, float spaces)
425 {
426 m_MarginTop = top;
427 m_MarginBottom = bottom;
428 m_MarginLeft = left;
429 m_MarginRight = right;
430 m_MarginSpace = spaces;
431 }
432
433
434
435
436 void wxHtmlPrintout::SetFonts(wxString normal_face, wxString fixed_face,
437 const int *sizes)
438 {
439 m_Renderer->SetFonts(normal_face, fixed_face, sizes);
440 m_RendererHdr->SetFonts(normal_face, fixed_face, sizes);
441 }
442
443
444
445 //----------------------------------------------------------------------------
446 // wxHtmlEasyPrinting
447 //----------------------------------------------------------------------------
448
449
450 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxWindow *parentWindow)
451 {
452 m_ParentWindow = parentWindow;
453 m_Name = name;
454 m_PrintData = new wxPrintData;
455 m_PageSetupData = new wxPageSetupDialogData;
456 m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString;
457
458 m_PageSetupData->EnableMargins(TRUE);
459 m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25));
460 m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25));
461
462 SetFonts(wxEmptyString, wxEmptyString, NULL);
463 }
464
465
466
467 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
468 {
469 delete m_PrintData;
470 delete m_PageSetupData;
471 }
472
473
474
475 bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile)
476 {
477 wxHtmlPrintout *p1 = CreatePrintout();
478 p1->SetHtmlFile(htmlfile);
479 wxHtmlPrintout *p2 = CreatePrintout();
480 p2->SetHtmlFile(htmlfile);
481 return DoPreview(p1, p2);
482 }
483
484
485
486 bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath)
487 {
488 wxHtmlPrintout *p1 = CreatePrintout();
489 p1->SetHtmlText(htmltext, basepath, TRUE);
490 wxHtmlPrintout *p2 = CreatePrintout();
491 p2->SetHtmlText(htmltext, basepath, TRUE);
492 return DoPreview(p1, p2);
493 }
494
495
496
497 bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile)
498 {
499 wxHtmlPrintout *p = CreatePrintout();
500 p->SetHtmlFile(htmlfile);
501 bool ret = DoPrint(p);
502 delete p;
503 return ret;
504 }
505
506
507
508 bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath)
509 {
510 wxHtmlPrintout *p = CreatePrintout();
511 p->SetHtmlText(htmltext, basepath, TRUE);
512 bool ret = DoPrint(p);
513 delete p;
514 return ret;
515 }
516
517
518
519 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2)
520 {
521 // Pass two printout objects: for preview, and possible printing.
522 wxPrintDialogData printDialogData(*m_PrintData);
523 wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData);
524 if (!preview->Ok())
525 {
526 delete preview;
527 return FALSE;
528 }
529
530 wxPreviewFrame *frame = new wxPreviewFrame(preview, m_ParentWindow,
531 m_Name + _(" Preview"),
532 wxPoint(100, 100), wxSize(650, 500));
533 frame->Centre(wxBOTH);
534 frame->Initialize();
535 frame->Show(TRUE);
536 return TRUE;
537 }
538
539
540
541 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout)
542 {
543 wxPrintDialogData printDialogData(*m_PrintData);
544 wxPrinter printer(&printDialogData);
545
546 if (!printer.Print(m_ParentWindow, printout, TRUE))
547 {
548 return FALSE;
549 }
550
551 (*m_PrintData) = printer.GetPrintDialogData().GetPrintData();
552 return TRUE;
553 }
554
555
556
557 void wxHtmlEasyPrinting::PrinterSetup()
558 {
559 wxPrintDialogData printDialogData(*m_PrintData);
560 wxPrintDialog printerDialog(m_ParentWindow, &printDialogData);
561
562 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
563
564 if (printerDialog.ShowModal() == wxID_OK)
565 (*m_PrintData) = printerDialog.GetPrintDialogData().GetPrintData();
566 }
567
568
569
570 void wxHtmlEasyPrinting::PageSetup()
571 {
572 if (!m_PrintData->Ok())
573 {
574 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
575 return;
576 }
577
578 m_PageSetupData->SetPrintData(*m_PrintData);
579 wxPageSetupDialog pageSetupDialog(m_ParentWindow, m_PageSetupData);
580
581 if (pageSetupDialog.ShowModal() == wxID_OK)
582 {
583 (*m_PrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
584 (*m_PageSetupData) = pageSetupDialog.GetPageSetupData();
585 }
586 }
587
588
589
590 void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg)
591 {
592 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
593 m_Headers[0] = header;
594 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
595 m_Headers[1] = header;
596 }
597
598
599
600 void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg)
601 {
602 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
603 m_Footers[0] = footer;
604 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
605 m_Footers[1] = footer;
606 }
607
608
609 void wxHtmlEasyPrinting::SetFonts(wxString normal_face, wxString fixed_face,
610 const int *sizes)
611 {
612 m_FontFaceNormal = normal_face;
613 m_FontFaceFixed = fixed_face;
614
615 if (sizes)
616 {
617 m_FontsSizes = m_FontsSizesArr;
618 for (int i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
619 }
620 else
621 m_FontsSizes = NULL;
622 }
623
624
625 wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout()
626 {
627 wxHtmlPrintout *p = new wxHtmlPrintout(m_Name);
628
629 p->SetFonts(m_FontFaceNormal, m_FontFaceFixed, m_FontsSizes);
630
631 p->SetHeader(m_Headers[0], wxPAGE_EVEN);
632 p->SetHeader(m_Headers[1], wxPAGE_ODD);
633 p->SetFooter(m_Footers[0], wxPAGE_EVEN);
634 p->SetFooter(m_Footers[1], wxPAGE_ODD);
635
636 p->SetMargins(m_PageSetupData->GetMarginTopLeft().y,
637 m_PageSetupData->GetMarginBottomRight().y,
638 m_PageSetupData->GetMarginTopLeft().x,
639 m_PageSetupData->GetMarginBottomRight().x);
640
641 return p;
642 }
643
644 // A module to allow initialization/cleanup
645 // without calling these functions from app.cpp or from
646 // the user's application.
647
648 class wxHtmlPrintingModule: public wxModule
649 {
650 DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule)
651 public:
652 wxHtmlPrintingModule() : wxModule() {}
653 bool OnInit() { return TRUE; }
654 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
655 };
656
657 IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule, wxModule)
658
659
660 // This hack forces the linker to always link in m_* files
661 // (wxHTML doesn't work without handlers from these files)
662 #include "wx/html/forcelnk.h"
663 FORCE_WXHTML_MODULES()
664
665 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE