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