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