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