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