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