]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmprint.cpp
compilation fix for wxUSE_DYNLIB_CLASS == 0
[wxWidgets.git] / src / html / htmprint.cpp
CommitLineData
3ce369e6 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/html/htmprint.cpp
3ce369e6
VS
3// Purpose: html printing classes
4// Author: Vaclav Slavik
5// Created: 25/09/99
6// RCS-ID: $Id$
7// Copyright: (c) Vaclav Slavik, 1999
65571936 8// Licence: wxWindows licence
3ce369e6
VS
9/////////////////////////////////////////////////////////////////////////////
10
3ce369e6
VS
11// For compilers that support precompilation, includes "wx/wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
93763ad5 15 #pragma hdrstop
3ce369e6
VS
16#endif
17
93763ad5
WS
18#if wxUSE_HTML && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS
19
3ce369e6 20#ifndef WX_PRECOMP
04dbb646
VZ
21 #include "wx/log.h"
22 #include "wx/intl.h"
5438a566 23 #include "wx/dc.h"
9eddec69 24 #include "wx/settings.h"
706e740f 25 #include "wx/msgdlg.h"
3ce369e6
VS
26#endif
27
3ce369e6
VS
28#include "wx/print.h"
29#include "wx/printdlg.h"
30#include "wx/html/htmprint.h"
31#include "wx/wxhtml.h"
32#include "wx/wfstream.h"
fa10c70c 33#include "wx/module.h"
3ce369e6
VS
34
35
36//--------------------------------------------------------------------------------
37// wxHtmlDCRenderer
38//--------------------------------------------------------------------------------
39
40
41wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
42{
43 m_DC = NULL;
44 m_Width = m_Height = 0;
45 m_Cells = NULL;
bc55e31b 46 m_Parser = new wxHtmlWinParser();
3ce369e6 47 m_FS = new wxFileSystem();
4f9297b0 48 m_Parser->SetFS(m_FS);
3ce369e6
VS
49}
50
51
52
53wxHtmlDCRenderer::~wxHtmlDCRenderer()
54{
55 if (m_Cells) delete m_Cells;
56 if (m_Parser) delete m_Parser;
57 if (m_FS) delete m_FS;
58}
59
60
61
edbd0635 62void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale)
3ce369e6 63{
3ce369e6 64 m_DC = dc;
4f9297b0 65 m_Parser->SetDC(m_DC, pixel_scale);
3ce369e6
VS
66}
67
68
69
70void wxHtmlDCRenderer::SetSize(int width, int height)
71{
edbd0635
VS
72 m_Width = width;
73 m_Height = height;
3ce369e6
VS
74}
75
76
3ce369e6
VS
77void wxHtmlDCRenderer::SetHtmlText(const wxString& html, const wxString& basepath, bool isdir)
78{
79 if (m_DC == NULL) return;
80
81 if (m_Cells != NULL) delete m_Cells;
04dbb646 82
4f9297b0
VS
83 m_FS->ChangePathTo(basepath, isdir);
84 m_Cells = (wxHtmlContainerCell*) m_Parser->Parse(html);
85 m_Cells->SetIndent(0, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
86 m_Cells->Layout(m_Width);
3ce369e6
VS
87}
88
89
fbfb8bcc 90void wxHtmlDCRenderer::SetFonts(const wxString& normal_face, const wxString& fixed_face,
4eecf115
VS
91 const int *sizes)
92{
93 m_Parser->SetFonts(normal_face, fixed_face, sizes);
10e5c7ea
VS
94 if (m_DC == NULL && m_Cells != NULL)
95 m_Cells->Layout(m_Width);
4eecf115
VS
96}
97
10e5c7ea
VS
98void wxHtmlDCRenderer::SetStandardFonts(int size,
99 const wxString& normal_face,
100 const wxString& fixed_face)
7acd3625 101{
10e5c7ea
VS
102 m_Parser->SetStandardFonts(size, normal_face, fixed_face);
103 if (m_DC == NULL && m_Cells != NULL)
104 m_Cells->Layout(m_Width);
7acd3625
RD
105}
106
fd0bab43
VZ
107int wxHtmlDCRenderer::Render(int x, int y,
108 wxArrayInt& known_pagebreaks,
109 int from, int dont_render, int to)
3ce369e6 110{
ec939b68 111 int pbreak, hght;
04dbb646 112
3ce369e6 113 if (m_Cells == NULL || m_DC == NULL) return 0;
04dbb646 114
edbd0635 115 pbreak = (int)(from + m_Height);
fd0bab43 116 while (m_Cells->AdjustPagebreak(&pbreak, known_pagebreaks)) {}
edbd0635 117 hght = pbreak - from;
fd0bab43
VZ
118 if(to < hght)
119 hght = to;
04dbb646
VZ
120
121 if (!dont_render)
4f9297b0 122 {
f30e67db
VS
123 wxHtmlRenderingInfo rinfo;
124 wxDefaultHtmlRenderingStyle rstyle;
125 rinfo.SetStyle(&rstyle);
4f9297b0 126 m_DC->SetBrush(*wxWHITE_BRUSH);
2a2e4f4a 127 m_DC->SetClippingRegion(x, y, m_Width, hght);
04dbb646 128 m_Cells->Draw(*m_DC,
36c4ff4d 129 x, (y - from),
2a2e4f4a 130 y, pbreak + (y /*- from*/),
f30e67db 131 rinfo);
2a2e4f4a 132 m_DC->DestroyClippingRegion();
3ce369e6 133 }
04dbb646 134
4f9297b0 135 if (pbreak < m_Cells->GetHeight()) return pbreak;
3ce369e6
VS
136 else return GetTotalHeight();
137}
138
139
3ce369e6
VS
140int wxHtmlDCRenderer::GetTotalHeight()
141{
4f9297b0 142 if (m_Cells) return m_Cells->GetHeight();
3ce369e6
VS
143 else return 0;
144}
145
146
3ce369e6
VS
147//--------------------------------------------------------------------------------
148// wxHtmlPrintout
149//--------------------------------------------------------------------------------
150
151
fa10c70c 152wxList wxHtmlPrintout::m_Filters;
3ce369e6
VS
153
154wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title)
155{
156 m_Renderer = new wxHtmlDCRenderer;
157 m_RendererHdr = new wxHtmlDCRenderer;
efba2b89 158 m_NumPages = wxHTML_PRINT_MAX_PAGES;
d1da8872 159 m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = true;
3ce369e6
VS
160 m_Headers[0] = m_Headers[1] = wxEmptyString;
161 m_Footers[0] = m_Footers[1] = wxEmptyString;
162 m_HeaderHeight = m_FooterHeight = 0;
163 SetMargins(); // to default values
164}
165
166
167
168wxHtmlPrintout::~wxHtmlPrintout()
169{
170 delete m_Renderer;
171 delete m_RendererHdr;
172}
173
fa10c70c
JS
174void wxHtmlPrintout::CleanUpStatics()
175{
222ed1d6 176 WX_CLEAR_LIST(wxList, m_Filters);
fa10c70c 177}
3ce369e6 178
fa10c70c
JS
179// Adds input filter
180void wxHtmlPrintout::AddFilter(wxHtmlFilter *filter)
181{
182 m_Filters.Append(filter);
183}
3ce369e6 184
d2b354f9 185void wxHtmlPrintout::OnPreparePrinting()
3ce369e6 186{
edbd0635 187 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
3ce369e6 188 float ppmm_h, ppmm_v;
04dbb646 189
3ce369e6
VS
190 GetPageSizePixels(&pageWidth, &pageHeight);
191 GetPageSizeMM(&mm_w, &mm_h);
192 ppmm_h = (float)pageWidth / mm_w;
193 ppmm_v = (float)pageHeight / mm_h;
194
edbd0635
VS
195 int ppiPrinterX, ppiPrinterY;
196 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
197 int ppiScreenX, ppiScreenY;
198 GetPPIScreen(&ppiScreenX, &ppiScreenY);
199
200 wxDisplaySize(&scr_w, &scr_h);
4f9297b0 201 GetDC()->GetSize(&dc_w, &dc_h);
edbd0635 202
4f9297b0 203 GetDC()->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
edbd0635 204
3ce369e6 205 /* prepare headers/footers renderer: */
04dbb646 206
4f9297b0 207 m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
04dbb646 208 m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
68be9f09 209 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom)));
04dbb646 210 if (m_Headers[0] != wxEmptyString)
4f9297b0
VS
211 {
212 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1));
213 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
3ce369e6 214 }
04dbb646 215 else if (m_Headers[1] != wxEmptyString)
4f9297b0
VS
216 {
217 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1));
218 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
3ce369e6 219 }
04dbb646 220 if (m_Footers[0] != wxEmptyString)
4f9297b0
VS
221 {
222 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1));
223 m_FooterHeight = m_RendererHdr->GetTotalHeight();
3ce369e6 224 }
04dbb646 225 else if (m_Footers[1] != wxEmptyString)
4f9297b0
VS
226 {
227 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1));
228 m_FooterHeight = m_RendererHdr->GetTotalHeight();
3ce369e6 229 }
04dbb646 230
3ce369e6 231 /* prepare main renderer: */
4f9297b0 232 m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
6c5b628e 233 m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
04dbb646 234 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) -
3ce369e6
VS
235 m_FooterHeight - m_HeaderHeight -
236 ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) -
237 ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v)
68be9f09 238 ));
4f9297b0 239 m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir);
3ce369e6 240 CountPages();
d2b354f9
JS
241}
242
243bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage)
244{
d1da8872 245 if (!wxPrintout::OnBeginDocument(startPage, endPage)) return false;
d2b354f9 246
d1da8872 247 return true;
3ce369e6
VS
248}
249
250
251bool wxHtmlPrintout::OnPrintPage(int page)
252{
253 wxDC *dc = GetDC();
04dbb646 254 if (dc)
4f9297b0 255 {
3ce369e6
VS
256 if (HasPage(page))
257 RenderPage(dc, page);
d1da8872 258 return true;
04dbb646 259 }
d1da8872 260 else return false;
3ce369e6
VS
261}
262
263
264void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
265{
266 *minPage = 1;
fd0bab43
VZ
267 if ( m_NumPages >= (signed)m_PageBreaks.Count()-1)
268 *maxPage = m_NumPages;
269 else
270 *maxPage = (signed)m_PageBreaks.Count()-1;
3ce369e6 271 *selPageFrom = 1;
fd0bab43 272 *selPageTo = (signed)m_PageBreaks.Count()-1;
3ce369e6
VS
273}
274
275
276
277bool wxHtmlPrintout::HasPage(int pageNum)
278{
fd0bab43 279 return (pageNum >= 1 && pageNum-1 <= (signed)m_PageBreaks.Count());
3ce369e6
VS
280}
281
282
283
284void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath, bool isdir)
285{
286 m_Document = html;
287 m_BasePath = basepath;
288 m_BasePathIsDir = isdir;
289}
290
3ce369e6
VS
291void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile)
292{
293 wxFileSystem fs;
72b1ad5c 294 wxFSFile *ff;
d1da8872 295
72b1ad5c
VS
296 if (wxFileExists(htmlfile))
297 ff = fs.OpenFile(wxFileSystem::FileNameToURL(htmlfile));
298 else
299 ff = fs.OpenFile(htmlfile);
04dbb646 300
f5ba273e 301 if (ff == NULL)
04dbb646 302 {
f5ba273e
VS
303 wxLogError(htmlfile + _(": file does not exist!"));
304 return;
305 }
04dbb646 306
d1da8872 307 bool done = false;
fa10c70c
JS
308 wxHtmlFilterHTML defaultFilter;
309 wxString doc;
310
222ed1d6 311 wxList::compatibility_iterator node = m_Filters.GetFirst();
fa10c70c
JS
312 while (node)
313 {
314 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
315 if (h->CanRead(*ff))
316 {
317 doc = h->ReadFile(*ff);
d1da8872 318 done = true;
fa10c70c
JS
319 break;
320 }
321 node = node->GetNext();
322 }
323
324 if (!done)
325 doc = defaultFilter.ReadFile(*ff);
d1da8872
WS
326
327 SetHtmlText(doc, htmlfile, false);
2b5f62a0 328 delete ff;
3ce369e6
VS
329}
330
331
332
333void wxHtmlPrintout::SetHeader(const wxString& header, int pg)
334{
04dbb646 335 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
3ce369e6 336 m_Headers[0] = header;
04dbb646 337 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
3ce369e6
VS
338 m_Headers[1] = header;
339}
340
341
342
343void wxHtmlPrintout::SetFooter(const wxString& footer, int pg)
344{
04dbb646 345 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
3ce369e6 346 m_Footers[0] = footer;
04dbb646 347 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
3ce369e6
VS
348 m_Footers[1] = footer;
349}
350
351
352
353void wxHtmlPrintout::CountPages()
354{
355 wxBusyCursor wait;
356 int pageWidth, pageHeight, mm_w, mm_h;
357 float ppmm_h, ppmm_v;
358
359 GetPageSizePixels(&pageWidth, &pageHeight);
360 GetPageSizeMM(&mm_w, &mm_h);
361 ppmm_h = (float)pageWidth / mm_w;
362 ppmm_v = (float)pageHeight / mm_h;
363
364 int pos = 0;
3ce369e6 365 m_NumPages = 0;
fd0bab43 366 // m_PageBreaks[0] = 0;
04dbb646 367
fd0bab43
VZ
368 m_PageBreaks.Clear();
369 m_PageBreaks.Add( 0);
04dbb646 370 do
4f9297b0 371 {
04dbb646 372 pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft),
fd0bab43
VZ
373 (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
374 m_PageBreaks,
375 pos, true, INT_MAX);
376 m_PageBreaks.Add( pos);
377 if( m_PageBreaks.Count() > wxHTML_PRINT_MAX_PAGES)
378 {
379 wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can continue any longer!"),
380 _("Warning"), wxCANCEL | wxICON_ERROR );
381 break;
382 }
4f9297b0 383 } while (pos < m_Renderer->GetTotalHeight());
3ce369e6
VS
384}
385
386
387
388void wxHtmlPrintout::RenderPage(wxDC *dc, int page)
389{
390 wxBusyCursor wait;
391
edbd0635 392 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
3ce369e6
VS
393 float ppmm_h, ppmm_v;
394
395 GetPageSizePixels(&pageWidth, &pageHeight);
396 GetPageSizeMM(&mm_w, &mm_h);
397 ppmm_h = (float)pageWidth / mm_w;
398 ppmm_v = (float)pageHeight / mm_h;
edbd0635 399 wxDisplaySize(&scr_w, &scr_h);
4f9297b0 400 dc->GetSize(&dc_w, &dc_h);
3ce369e6 401
edbd0635
VS
402 int ppiPrinterX, ppiPrinterY;
403 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
999836aa 404 wxUnusedVar(ppiPrinterX);
edbd0635
VS
405 int ppiScreenX, ppiScreenY;
406 GetPPIScreen(&ppiScreenX, &ppiScreenY);
999836aa 407 wxUnusedVar(ppiScreenX);
edbd0635 408
4f9297b0 409 dc->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
04dbb646 410
4f9297b0 411 m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY);
04dbb646 412
4f9297b0 413 dc->SetBackgroundMode(wxTRANSPARENT);
3ce369e6 414
04dbb646 415 m_Renderer->Render((int) (ppmm_h * m_MarginLeft),
fd0bab43 416 (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), m_PageBreaks,
d1da8872 417 m_PageBreaks[page-1], false, m_PageBreaks[page]-m_PageBreaks[page-1]);
04dbb646 418
fd0bab43 419
4f9297b0 420 m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY);
04dbb646 421 if (m_Headers[page % 2] != wxEmptyString)
4f9297b0
VS
422 {
423 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page));
fd0bab43 424 m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop), m_PageBreaks);
3ce369e6 425 }
04dbb646 426 if (m_Footers[page % 2] != wxEmptyString)
4f9297b0
VS
427 {
428 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page));
fd0bab43 429 m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight), m_PageBreaks);
3ce369e6
VS
430 }
431}
432
433
434
435wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page)
436{
437 wxString r = instr;
438 wxString num;
04dbb646 439
66a77a74
OK
440 num.Printf(wxT("%i"), page);
441 r.Replace(wxT("@PAGENUM@"), num);
3ce369e6 442
fc4c80b8 443 num.Printf(wxT("%lu"), (unsigned long)(m_PageBreaks.Count() - 1));
66a77a74 444 r.Replace(wxT("@PAGESCNT@"), num);
3ce369e6
VS
445
446 return r;
447}
448
449
450
451void wxHtmlPrintout::SetMargins(float top, float bottom, float left, float right, float spaces)
452{
453 m_MarginTop = top;
454 m_MarginBottom = bottom;
455 m_MarginLeft = left;
456 m_MarginRight = right;
457 m_MarginSpace = spaces;
458}
459
460
461
462
fbfb8bcc 463void wxHtmlPrintout::SetFonts(const wxString& normal_face, const wxString& fixed_face,
4eecf115
VS
464 const int *sizes)
465{
466 m_Renderer->SetFonts(normal_face, fixed_face, sizes);
467 m_RendererHdr->SetFonts(normal_face, fixed_face, sizes);
468}
3ce369e6 469
10e5c7ea
VS
470void wxHtmlPrintout::SetStandardFonts(int size,
471 const wxString& normal_face,
472 const wxString& fixed_face)
7acd3625 473{
10e5c7ea
VS
474 m_Renderer->SetStandardFonts(size, normal_face, fixed_face);
475 m_RendererHdr->SetStandardFonts(size, normal_face, fixed_face);
7acd3625
RD
476}
477
478
4eecf115
VS
479
480//----------------------------------------------------------------------------
3ce369e6 481// wxHtmlEasyPrinting
4eecf115 482//----------------------------------------------------------------------------
3ce369e6
VS
483
484
a5ae8241 485wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxWindow *parentWindow)
3ce369e6 486{
a5ae8241 487 m_ParentWindow = parentWindow;
3ce369e6 488 m_Name = name;
632783de 489 m_PrintData = NULL;
3ce369e6
VS
490 m_PageSetupData = new wxPageSetupDialogData;
491 m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString;
04dbb646 492
d1da8872 493 m_PageSetupData->EnableMargins(true);
04dbb646 494 m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25));
4f9297b0 495 m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25));
4eecf115
VS
496
497 SetFonts(wxEmptyString, wxEmptyString, NULL);
3ce369e6
VS
498}
499
500
501
502wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
503{
504 delete m_PrintData;
505 delete m_PageSetupData;
506}
507
508
632783de
VS
509wxPrintData *wxHtmlEasyPrinting::GetPrintData()
510{
511 if (m_PrintData == NULL)
512 m_PrintData = new wxPrintData();
513 return m_PrintData;
514}
515
3ce369e6 516
f6bcfd97 517bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile)
3ce369e6
VS
518{
519 wxHtmlPrintout *p1 = CreatePrintout();
4f9297b0 520 p1->SetHtmlFile(htmlfile);
3ce369e6 521 wxHtmlPrintout *p2 = CreatePrintout();
4f9297b0 522 p2->SetHtmlFile(htmlfile);
f6bcfd97 523 return DoPreview(p1, p2);
3ce369e6
VS
524}
525
526
527
f6bcfd97 528bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath)
3ce369e6
VS
529{
530 wxHtmlPrintout *p1 = CreatePrintout();
d1da8872 531 p1->SetHtmlText(htmltext, basepath, true);
3ce369e6 532 wxHtmlPrintout *p2 = CreatePrintout();
d1da8872 533 p2->SetHtmlText(htmltext, basepath, true);
f6bcfd97 534 return DoPreview(p1, p2);
3ce369e6
VS
535}
536
537
538
f6bcfd97 539bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile)
3ce369e6
VS
540{
541 wxHtmlPrintout *p = CreatePrintout();
4f9297b0 542 p->SetHtmlFile(htmlfile);
453507f2
VS
543 bool ret = DoPrint(p);
544 delete p;
545 return ret;
3ce369e6
VS
546}
547
548
549
f6bcfd97 550bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath)
3ce369e6
VS
551{
552 wxHtmlPrintout *p = CreatePrintout();
d1da8872 553 p->SetHtmlText(htmltext, basepath, true);
453507f2
VS
554 bool ret = DoPrint(p);
555 delete p;
556 return ret;
3ce369e6
VS
557}
558
559
560
f6bcfd97 561bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2)
3ce369e6
VS
562{
563 // Pass two printout objects: for preview, and possible printing.
632783de 564 wxPrintDialogData printDialogData(*GetPrintData());
3ce369e6 565 wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData);
04dbb646 566 if (!preview->Ok())
4f9297b0 567 {
3ce369e6 568 delete preview;
d1da8872 569 return false;
3ce369e6 570 }
3ca6a5f0 571
a5ae8241 572 wxPreviewFrame *frame = new wxPreviewFrame(preview, m_ParentWindow,
04dbb646 573 m_Name + _(" Preview"),
3ca6a5f0 574 wxPoint(100, 100), wxSize(650, 500));
4f9297b0
VS
575 frame->Centre(wxBOTH);
576 frame->Initialize();
d1da8872
WS
577 frame->Show(true);
578 return true;
3ce369e6
VS
579}
580
581
582
f6bcfd97 583bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout)
3ce369e6 584{
632783de 585 wxPrintDialogData printDialogData(*GetPrintData());
3ce369e6
VS
586 wxPrinter printer(&printDialogData);
587
d1da8872 588 if (!printer.Print(m_ParentWindow, printout, true))
f6bcfd97 589 {
d1da8872 590 return false;
f6bcfd97 591 }
3ca6a5f0 592
632783de 593 (*GetPrintData()) = printer.GetPrintDialogData().GetPrintData();
d1da8872 594 return true;
3ce369e6
VS
595}
596
597
598
3ce369e6
VS
599
600void wxHtmlEasyPrinting::PageSetup()
601{
632783de 602 if (!GetPrintData()->Ok())
58cf0491 603 {
936b18ac 604 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
58cf0491
JS
605 return;
606 }
607
632783de 608 m_PageSetupData->SetPrintData(*GetPrintData());
a5ae8241 609 wxPageSetupDialog pageSetupDialog(m_ParentWindow, m_PageSetupData);
3ce369e6 610
04dbb646 611 if (pageSetupDialog.ShowModal() == wxID_OK)
4f9297b0 612 {
632783de 613 (*GetPrintData()) = pageSetupDialog.GetPageSetupData().GetPrintData();
3ce369e6
VS
614 (*m_PageSetupData) = pageSetupDialog.GetPageSetupData();
615 }
616}
617
618
619
620void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg)
621{
04dbb646 622 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
3ce369e6 623 m_Headers[0] = header;
04dbb646 624 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
3ce369e6
VS
625 m_Headers[1] = header;
626}
627
628
629
630void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg)
631{
04dbb646 632 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
3ce369e6 633 m_Footers[0] = footer;
04dbb646 634 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
3ce369e6
VS
635 m_Footers[1] = footer;
636}
637
638
fbfb8bcc 639void wxHtmlEasyPrinting::SetFonts(const wxString& normal_face, const wxString& fixed_face,
4eecf115
VS
640 const int *sizes)
641{
10e5c7ea 642 m_fontMode = FontMode_Explicit;
4eecf115
VS
643 m_FontFaceNormal = normal_face;
644 m_FontFaceFixed = fixed_face;
645
646 if (sizes)
647 {
648 m_FontsSizes = m_FontsSizesArr;
649 for (int i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
650 }
651 else
652 m_FontsSizes = NULL;
653}
654
10e5c7ea
VS
655void wxHtmlEasyPrinting::SetStandardFonts(int size,
656 const wxString& normal_face,
657 const wxString& fixed_face)
7acd3625 658{
10e5c7ea
VS
659 m_fontMode = FontMode_Standard;
660 m_FontFaceNormal = normal_face;
661 m_FontFaceFixed = fixed_face;
662 m_FontsSizesArr[0] = size;
7acd3625
RD
663}
664
3ce369e6
VS
665
666wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout()
667{
668 wxHtmlPrintout *p = new wxHtmlPrintout(m_Name);
04dbb646 669
10e5c7ea
VS
670 if (m_fontMode == FontMode_Explicit)
671 {
672 p->SetFonts(m_FontFaceNormal, m_FontFaceFixed, m_FontsSizes);
673 }
674 else // FontMode_Standard
675 {
676 p->SetStandardFonts(m_FontsSizesArr[0],
677 m_FontFaceNormal, m_FontFaceFixed);
678 }
4eecf115 679
4f9297b0
VS
680 p->SetHeader(m_Headers[0], wxPAGE_EVEN);
681 p->SetHeader(m_Headers[1], wxPAGE_ODD);
682 p->SetFooter(m_Footers[0], wxPAGE_EVEN);
683 p->SetFooter(m_Footers[1], wxPAGE_ODD);
684
685 p->SetMargins(m_PageSetupData->GetMarginTopLeft().y,
686 m_PageSetupData->GetMarginBottomRight().y,
687 m_PageSetupData->GetMarginTopLeft().x,
688 m_PageSetupData->GetMarginBottomRight().x);
04dbb646 689
3ce369e6
VS
690 return p;
691}
692
fa10c70c
JS
693// A module to allow initialization/cleanup
694// without calling these functions from app.cpp or from
695// the user's application.
696
697class wxHtmlPrintingModule: public wxModule
698{
699DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule)
700public:
701 wxHtmlPrintingModule() : wxModule() {}
d1da8872 702 bool OnInit() { return true; }
fa10c70c
JS
703 void OnExit() { wxHtmlPrintout::CleanUpStatics(); }
704};
705
706IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule, wxModule)
707
3ce369e6 708
0cecad31
VS
709// This hack forces the linker to always link in m_* files
710// (wxHTML doesn't work without handlers from these files)
711#include "wx/html/forcelnk.h"
712FORCE_WXHTML_MODULES()
3ce369e6 713
72cdf4c9 714#endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE