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