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