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