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