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