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