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