]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/html/htmprint.cpp
pressing PageDown and then PageUp should return to the same item
[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
82void wxHtmlDCRenderer::SetHtmlText(const wxString& html, const wxString& basepath, bool isdir)
83{
84 if (m_DC == NULL) return;
85
86 if (m_Cells != NULL) delete m_Cells;
87
88 m_FS->ChangePathTo(basepath, isdir);
89 m_Cells = (wxHtmlContainerCell*) m_Parser->Parse(html);
90 m_Cells->SetIndent(0, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
91 m_Cells->Layout(m_Width);
92}
93
94
95void wxHtmlDCRenderer::SetFonts(wxString normal_face, wxString fixed_face,
96 const int *sizes)
97{
98 m_Parser->SetFonts(normal_face, fixed_face, sizes);
99 if (m_DC == NULL && m_Cells != NULL) m_Cells->Layout(m_Width);
100}
101
102
103int wxHtmlDCRenderer::Render(int x, int y, int from, int dont_render, int to, int *known_pagebreaks, int number_of_pages)
104{
105 int pbreak, hght;
106
107 if (m_Cells == NULL || m_DC == NULL) return 0;
108
109 pbreak = (int)(from + m_Height);
110 while (m_Cells->AdjustPagebreak(&pbreak, known_pagebreaks, number_of_pages)) {}
111 hght = pbreak - from;
112 if(to < hght)
113 hght = to;
114
115 if (!dont_render)
116 {
117 wxHtmlRenderingState rstate(NULL);
118 m_DC->SetBrush(*wxWHITE_BRUSH);
119 m_DC->SetClippingRegion(x, y, m_Width, hght);
120 m_Cells->Draw(*m_DC,
121 x, (y - from),
122 y, pbreak + (y /*- from*/),
123 rstate);
124 m_DC->DestroyClippingRegion();
125 }
126
127 if (pbreak < m_Cells->GetHeight()) return pbreak;
128 else return GetTotalHeight();
129}
130
131
132
133int wxHtmlDCRenderer::GetTotalHeight()
134{
135 if (m_Cells) return m_Cells->GetHeight();
136 else return 0;
137}
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154//--------------------------------------------------------------------------------
155// wxHtmlPrintout
156//--------------------------------------------------------------------------------
157
158
159
160wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title)
161{
162 m_Renderer = new wxHtmlDCRenderer;
163 m_RendererHdr = new wxHtmlDCRenderer;
164 m_NumPages = wxHTML_PRINT_MAX_PAGES;
165 m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = TRUE;
166 m_Headers[0] = m_Headers[1] = wxEmptyString;
167 m_Footers[0] = m_Footers[1] = wxEmptyString;
168 m_HeaderHeight = m_FooterHeight = 0;
169 SetMargins(); // to default values
170}
171
172
173
174wxHtmlPrintout::~wxHtmlPrintout()
175{
176 delete m_Renderer;
177 delete m_RendererHdr;
178}
179
180
181
182bool 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
244bool 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
257void 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
267bool wxHtmlPrintout::HasPage(int pageNum)
268{
269 return (pageNum >= 1 && pageNum <= m_NumPages);
270}
271
272
273
274void 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
281void 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 wxHtmlFilterHTML filter;
293 wxString doc = filter.ReadFile(*ff);
294
295 SetHtmlText(doc, htmlfile, FALSE);
296 delete ff;
297}
298
299
300
301void wxHtmlPrintout::SetHeader(const wxString& header, int pg)
302{
303 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
304 m_Headers[0] = header;
305 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
306 m_Headers[1] = header;
307}
308
309
310
311void wxHtmlPrintout::SetFooter(const wxString& footer, int pg)
312{
313 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
314 m_Footers[0] = footer;
315 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
316 m_Footers[1] = footer;
317}
318
319
320
321void wxHtmlPrintout::CountPages()
322{
323 wxBusyCursor wait;
324 int pageWidth, pageHeight, mm_w, mm_h;
325 float ppmm_h, ppmm_v;
326
327 GetPageSizePixels(&pageWidth, &pageHeight);
328 GetPageSizeMM(&mm_w, &mm_h);
329 ppmm_h = (float)pageWidth / mm_w;
330 ppmm_v = (float)pageHeight / mm_h;
331
332 int pos = 0;
333
334 m_NumPages = 0;
335
336 m_PageBreaks[0] = 0;
337 do
338 {
339 pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft),
340 (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
341 pos, TRUE, INT_MAX, m_PageBreaks, m_NumPages);
342 m_PageBreaks[++m_NumPages] = pos;
343 } while (pos < m_Renderer->GetTotalHeight());
344}
345
346
347
348void wxHtmlPrintout::RenderPage(wxDC *dc, int page)
349{
350 wxBusyCursor wait;
351
352 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
353 float ppmm_h, ppmm_v;
354
355 GetPageSizePixels(&pageWidth, &pageHeight);
356 GetPageSizeMM(&mm_w, &mm_h);
357 ppmm_h = (float)pageWidth / mm_w;
358 ppmm_v = (float)pageHeight / mm_h;
359 wxDisplaySize(&scr_w, &scr_h);
360 dc->GetSize(&dc_w, &dc_h);
361
362 int ppiPrinterX, ppiPrinterY;
363 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
364 int ppiScreenX, ppiScreenY;
365 GetPPIScreen(&ppiScreenX, &ppiScreenY);
366
367 dc->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
368
369 m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY);
370
371 dc->SetBackgroundMode(wxTRANSPARENT);
372
373 m_Renderer->Render((int) (ppmm_h * m_MarginLeft),
374 (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
375 m_PageBreaks[page-1], FALSE, m_PageBreaks[page]-m_PageBreaks[page-1]);
376
377 m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY);
378 if (m_Headers[page % 2] != wxEmptyString)
379 {
380 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page));
381 m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop));
382 }
383 if (m_Footers[page % 2] != wxEmptyString)
384 {
385 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page));
386 m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight));
387 }
388}
389
390
391
392wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page)
393{
394 wxString r = instr;
395 wxString num;
396
397 num.Printf(wxT("%i"), page);
398 r.Replace(wxT("@PAGENUM@"), num);
399
400 num.Printf(wxT("%i"), m_NumPages);
401 r.Replace(wxT("@PAGESCNT@"), num);
402
403 return r;
404}
405
406
407
408void wxHtmlPrintout::SetMargins(float top, float bottom, float left, float right, float spaces)
409{
410 m_MarginTop = top;
411 m_MarginBottom = bottom;
412 m_MarginLeft = left;
413 m_MarginRight = right;
414 m_MarginSpace = spaces;
415}
416
417
418
419
420void wxHtmlPrintout::SetFonts(wxString normal_face, wxString fixed_face,
421 const int *sizes)
422{
423 m_Renderer->SetFonts(normal_face, fixed_face, sizes);
424 m_RendererHdr->SetFonts(normal_face, fixed_face, sizes);
425}
426
427
428
429//----------------------------------------------------------------------------
430// wxHtmlEasyPrinting
431//----------------------------------------------------------------------------
432
433
434wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxWindow *parentWindow)
435{
436 m_ParentWindow = parentWindow;
437 m_Name = name;
438 m_PrintData = new wxPrintData;
439 m_PageSetupData = new wxPageSetupDialogData;
440 m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString;
441
442 m_PageSetupData->EnableMargins(TRUE);
443 m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25));
444 m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25));
445
446 SetFonts(wxEmptyString, wxEmptyString, NULL);
447}
448
449
450
451wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
452{
453 delete m_PrintData;
454 delete m_PageSetupData;
455}
456
457
458
459bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile)
460{
461 wxHtmlPrintout *p1 = CreatePrintout();
462 p1->SetHtmlFile(htmlfile);
463 wxHtmlPrintout *p2 = CreatePrintout();
464 p2->SetHtmlFile(htmlfile);
465 return DoPreview(p1, p2);
466}
467
468
469
470bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath)
471{
472 wxHtmlPrintout *p1 = CreatePrintout();
473 p1->SetHtmlText(htmltext, basepath, TRUE);
474 wxHtmlPrintout *p2 = CreatePrintout();
475 p2->SetHtmlText(htmltext, basepath, TRUE);
476 return DoPreview(p1, p2);
477}
478
479
480
481bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile)
482{
483 wxHtmlPrintout *p = CreatePrintout();
484 p->SetHtmlFile(htmlfile);
485 bool ret = DoPrint(p);
486 delete p;
487 return ret;
488}
489
490
491
492bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath)
493{
494 wxHtmlPrintout *p = CreatePrintout();
495 p->SetHtmlText(htmltext, basepath, TRUE);
496 bool ret = DoPrint(p);
497 delete p;
498 return ret;
499}
500
501
502
503bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2)
504{
505 // Pass two printout objects: for preview, and possible printing.
506 wxPrintDialogData printDialogData(*m_PrintData);
507 wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData);
508 if (!preview->Ok())
509 {
510 delete preview;
511 return FALSE;
512 }
513
514 wxPreviewFrame *frame = new wxPreviewFrame(preview, m_ParentWindow,
515 m_Name + _(" Preview"),
516 wxPoint(100, 100), wxSize(650, 500));
517 frame->Centre(wxBOTH);
518 frame->Initialize();
519 frame->Show(TRUE);
520 return TRUE;
521}
522
523
524
525bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout)
526{
527 wxPrintDialogData printDialogData(*m_PrintData);
528 wxPrinter printer(&printDialogData);
529
530 if (!printer.Print(m_ParentWindow, printout, TRUE))
531 {
532 return FALSE;
533 }
534
535 (*m_PrintData) = printer.GetPrintDialogData().GetPrintData();
536 return TRUE;
537}
538
539
540
541void wxHtmlEasyPrinting::PrinterSetup()
542{
543 wxPrintDialogData printDialogData(*m_PrintData);
544 wxPrintDialog printerDialog(m_ParentWindow, &printDialogData);
545
546 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
547
548 if (printerDialog.ShowModal() == wxID_OK)
549 (*m_PrintData) = printerDialog.GetPrintDialogData().GetPrintData();
550}
551
552
553
554void wxHtmlEasyPrinting::PageSetup()
555{
556 if (!m_PrintData->Ok())
557 {
558 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
559 return;
560 }
561
562 m_PageSetupData->SetPrintData(*m_PrintData);
563 wxPageSetupDialog pageSetupDialog(m_ParentWindow, m_PageSetupData);
564
565 if (pageSetupDialog.ShowModal() == wxID_OK)
566 {
567 (*m_PrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
568 (*m_PageSetupData) = pageSetupDialog.GetPageSetupData();
569 }
570}
571
572
573
574void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg)
575{
576 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
577 m_Headers[0] = header;
578 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
579 m_Headers[1] = header;
580}
581
582
583
584void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg)
585{
586 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
587 m_Footers[0] = footer;
588 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
589 m_Footers[1] = footer;
590}
591
592
593void wxHtmlEasyPrinting::SetFonts(wxString normal_face, wxString fixed_face,
594 const int *sizes)
595{
596 m_FontFaceNormal = normal_face;
597 m_FontFaceFixed = fixed_face;
598
599 if (sizes)
600 {
601 m_FontsSizes = m_FontsSizesArr;
602 for (int i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
603 }
604 else
605 m_FontsSizes = NULL;
606}
607
608
609wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout()
610{
611 wxHtmlPrintout *p = new wxHtmlPrintout(m_Name);
612
613 p->SetFonts(m_FontFaceNormal, m_FontFaceFixed, m_FontsSizes);
614
615 p->SetHeader(m_Headers[0], wxPAGE_EVEN);
616 p->SetHeader(m_Headers[1], wxPAGE_ODD);
617 p->SetFooter(m_Footers[0], wxPAGE_EVEN);
618 p->SetFooter(m_Footers[1], wxPAGE_ODD);
619
620 p->SetMargins(m_PageSetupData->GetMarginTopLeft().y,
621 m_PageSetupData->GetMarginBottomRight().y,
622 m_PageSetupData->GetMarginTopLeft().x,
623 m_PageSetupData->GetMarginBottomRight().x);
624
625 return p;
626}
627
628
629// This hack forces the linker to always link in m_* files
630// (wxHTML doesn't work without handlers from these files)
631#include "wx/html/forcelnk.h"
632FORCE_WXHTML_MODULES()
633
634#endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE