]>
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; | |
306 | wxFSFile *ff = fs.OpenFile(htmlfile); | |
04dbb646 | 307 | |
f5ba273e | 308 | if (ff == NULL) |
04dbb646 | 309 | { |
f5ba273e VS |
310 | wxLogError(htmlfile + _(": file does not exist!")); |
311 | return; | |
312 | } | |
04dbb646 | 313 | |
fa10c70c JS |
314 | bool done = FALSE; |
315 | wxHtmlFilterHTML defaultFilter; | |
316 | wxString doc; | |
317 | ||
222ed1d6 | 318 | wxList::compatibility_iterator node = m_Filters.GetFirst(); |
fa10c70c JS |
319 | while (node) |
320 | { | |
321 | wxHtmlFilter *h = (wxHtmlFilter*) node->GetData(); | |
322 | if (h->CanRead(*ff)) | |
323 | { | |
324 | doc = h->ReadFile(*ff); | |
325 | done = TRUE; | |
326 | break; | |
327 | } | |
328 | node = node->GetNext(); | |
329 | } | |
330 | ||
331 | if (!done) | |
332 | doc = defaultFilter.ReadFile(*ff); | |
333 | ||
3ce369e6 | 334 | SetHtmlText(doc, htmlfile, FALSE); |
2b5f62a0 | 335 | delete ff; |
3ce369e6 VS |
336 | } |
337 | ||
338 | ||
339 | ||
340 | void wxHtmlPrintout::SetHeader(const wxString& header, int pg) | |
341 | { | |
04dbb646 | 342 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 343 | m_Headers[0] = header; |
04dbb646 | 344 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
345 | m_Headers[1] = header; |
346 | } | |
347 | ||
348 | ||
349 | ||
350 | void wxHtmlPrintout::SetFooter(const wxString& footer, int pg) | |
351 | { | |
04dbb646 | 352 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 353 | m_Footers[0] = footer; |
04dbb646 | 354 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
355 | m_Footers[1] = footer; |
356 | } | |
357 | ||
358 | ||
359 | ||
360 | void wxHtmlPrintout::CountPages() | |
361 | { | |
362 | wxBusyCursor wait; | |
363 | int pageWidth, pageHeight, mm_w, mm_h; | |
364 | float ppmm_h, ppmm_v; | |
365 | ||
366 | GetPageSizePixels(&pageWidth, &pageHeight); | |
367 | GetPageSizeMM(&mm_w, &mm_h); | |
368 | ppmm_h = (float)pageWidth / mm_w; | |
369 | ppmm_v = (float)pageHeight / mm_h; | |
370 | ||
371 | int pos = 0; | |
372 | ||
373 | m_NumPages = 0; | |
04dbb646 | 374 | |
3ce369e6 | 375 | m_PageBreaks[0] = 0; |
04dbb646 | 376 | do |
4f9297b0 | 377 | { |
04dbb646 | 378 | pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft), |
68be9f09 | 379 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), |
f2034f1b | 380 | pos, TRUE, INT_MAX, m_PageBreaks, m_NumPages); |
3ce369e6 | 381 | m_PageBreaks[++m_NumPages] = pos; |
4f9297b0 | 382 | } while (pos < m_Renderer->GetTotalHeight()); |
3ce369e6 VS |
383 | } |
384 | ||
385 | ||
386 | ||
387 | void wxHtmlPrintout::RenderPage(wxDC *dc, int page) | |
388 | { | |
389 | wxBusyCursor wait; | |
390 | ||
edbd0635 | 391 | int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; |
3ce369e6 VS |
392 | float ppmm_h, ppmm_v; |
393 | ||
394 | GetPageSizePixels(&pageWidth, &pageHeight); | |
395 | GetPageSizeMM(&mm_w, &mm_h); | |
396 | ppmm_h = (float)pageWidth / mm_w; | |
397 | ppmm_v = (float)pageHeight / mm_h; | |
edbd0635 | 398 | wxDisplaySize(&scr_w, &scr_h); |
4f9297b0 | 399 | dc->GetSize(&dc_w, &dc_h); |
3ce369e6 | 400 | |
edbd0635 VS |
401 | int ppiPrinterX, ppiPrinterY; |
402 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
999836aa | 403 | wxUnusedVar(ppiPrinterX); |
edbd0635 VS |
404 | int ppiScreenX, ppiScreenY; |
405 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
999836aa | 406 | wxUnusedVar(ppiScreenX); |
edbd0635 | 407 | |
4f9297b0 | 408 | dc->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth); |
04dbb646 | 409 | |
4f9297b0 | 410 | m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); |
04dbb646 | 411 | |
4f9297b0 | 412 | dc->SetBackgroundMode(wxTRANSPARENT); |
3ce369e6 | 413 | |
04dbb646 | 414 | m_Renderer->Render((int) (ppmm_h * m_MarginLeft), |
68be9f09 | 415 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), |
f2034f1b | 416 | m_PageBreaks[page-1], FALSE, m_PageBreaks[page]-m_PageBreaks[page-1]); |
04dbb646 | 417 | |
4f9297b0 | 418 | m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); |
04dbb646 | 419 | if (m_Headers[page % 2] != wxEmptyString) |
4f9297b0 VS |
420 | { |
421 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page)); | |
422 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop)); | |
3ce369e6 | 423 | } |
04dbb646 | 424 | if (m_Footers[page % 2] != wxEmptyString) |
4f9297b0 VS |
425 | { |
426 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page)); | |
427 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight)); | |
3ce369e6 VS |
428 | } |
429 | } | |
430 | ||
431 | ||
432 | ||
433 | wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page) | |
434 | { | |
435 | wxString r = instr; | |
436 | wxString num; | |
04dbb646 | 437 | |
66a77a74 OK |
438 | num.Printf(wxT("%i"), page); |
439 | r.Replace(wxT("@PAGENUM@"), num); | |
3ce369e6 | 440 | |
66a77a74 OK |
441 | num.Printf(wxT("%i"), m_NumPages); |
442 | r.Replace(wxT("@PAGESCNT@"), num); | |
3ce369e6 VS |
443 | |
444 | return r; | |
445 | } | |
446 | ||
447 | ||
448 | ||
449 | void wxHtmlPrintout::SetMargins(float top, float bottom, float left, float right, float spaces) | |
450 | { | |
451 | m_MarginTop = top; | |
452 | m_MarginBottom = bottom; | |
453 | m_MarginLeft = left; | |
454 | m_MarginRight = right; | |
455 | m_MarginSpace = spaces; | |
456 | } | |
457 | ||
458 | ||
459 | ||
460 | ||
4eecf115 VS |
461 | void wxHtmlPrintout::SetFonts(wxString normal_face, wxString fixed_face, |
462 | const int *sizes) | |
463 | { | |
464 | m_Renderer->SetFonts(normal_face, fixed_face, sizes); | |
465 | m_RendererHdr->SetFonts(normal_face, fixed_face, sizes); | |
466 | } | |
3ce369e6 VS |
467 | |
468 | ||
7acd3625 RD |
469 | void wxHtmlPrintout::NormalizeFontSizes(int size) |
470 | { | |
471 | int f_sizes[7]; | |
472 | if (size == -1) | |
473 | size = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT).GetPointSize(); | |
474 | ||
475 | f_sizes[0] = int(size * 0.6); | |
476 | f_sizes[1] = int(size * 0.8); | |
477 | f_sizes[2] = size; | |
478 | f_sizes[3] = int(size * 1.2); | |
479 | f_sizes[4] = int(size * 1.4); | |
480 | f_sizes[5] = int(size * 1.6); | |
481 | f_sizes[6] = int(size * 1.8); | |
482 | ||
483 | SetFonts(wxEmptyString, wxEmptyString, f_sizes); | |
484 | } | |
485 | ||
486 | ||
4eecf115 VS |
487 | |
488 | //---------------------------------------------------------------------------- | |
3ce369e6 | 489 | // wxHtmlEasyPrinting |
4eecf115 | 490 | //---------------------------------------------------------------------------- |
3ce369e6 VS |
491 | |
492 | ||
a5ae8241 | 493 | wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxWindow *parentWindow) |
3ce369e6 | 494 | { |
a5ae8241 | 495 | m_ParentWindow = parentWindow; |
3ce369e6 | 496 | m_Name = name; |
632783de | 497 | m_PrintData = NULL; |
3ce369e6 VS |
498 | m_PageSetupData = new wxPageSetupDialogData; |
499 | m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString; | |
04dbb646 | 500 | |
4f9297b0 | 501 | m_PageSetupData->EnableMargins(TRUE); |
04dbb646 | 502 | m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25)); |
4f9297b0 | 503 | m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25)); |
4eecf115 VS |
504 | |
505 | SetFonts(wxEmptyString, wxEmptyString, NULL); | |
3ce369e6 VS |
506 | } |
507 | ||
508 | ||
509 | ||
510 | wxHtmlEasyPrinting::~wxHtmlEasyPrinting() | |
511 | { | |
512 | delete m_PrintData; | |
513 | delete m_PageSetupData; | |
514 | } | |
515 | ||
516 | ||
632783de VS |
517 | wxPrintData *wxHtmlEasyPrinting::GetPrintData() |
518 | { | |
519 | if (m_PrintData == NULL) | |
520 | m_PrintData = new wxPrintData(); | |
521 | return m_PrintData; | |
522 | } | |
523 | ||
3ce369e6 | 524 | |
f6bcfd97 | 525 | bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile) |
3ce369e6 VS |
526 | { |
527 | wxHtmlPrintout *p1 = CreatePrintout(); | |
4f9297b0 | 528 | p1->SetHtmlFile(htmlfile); |
3ce369e6 | 529 | wxHtmlPrintout *p2 = CreatePrintout(); |
4f9297b0 | 530 | p2->SetHtmlFile(htmlfile); |
f6bcfd97 | 531 | return DoPreview(p1, p2); |
3ce369e6 VS |
532 | } |
533 | ||
534 | ||
535 | ||
f6bcfd97 | 536 | bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath) |
3ce369e6 VS |
537 | { |
538 | wxHtmlPrintout *p1 = CreatePrintout(); | |
4f9297b0 | 539 | p1->SetHtmlText(htmltext, basepath, TRUE); |
3ce369e6 | 540 | wxHtmlPrintout *p2 = CreatePrintout(); |
4f9297b0 | 541 | p2->SetHtmlText(htmltext, basepath, TRUE); |
f6bcfd97 | 542 | return DoPreview(p1, p2); |
3ce369e6 VS |
543 | } |
544 | ||
545 | ||
546 | ||
f6bcfd97 | 547 | bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile) |
3ce369e6 VS |
548 | { |
549 | wxHtmlPrintout *p = CreatePrintout(); | |
4f9297b0 | 550 | p->SetHtmlFile(htmlfile); |
453507f2 VS |
551 | bool ret = DoPrint(p); |
552 | delete p; | |
553 | return ret; | |
3ce369e6 VS |
554 | } |
555 | ||
556 | ||
557 | ||
f6bcfd97 | 558 | bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath) |
3ce369e6 VS |
559 | { |
560 | wxHtmlPrintout *p = CreatePrintout(); | |
4f9297b0 | 561 | p->SetHtmlText(htmltext, basepath, TRUE); |
453507f2 VS |
562 | bool ret = DoPrint(p); |
563 | delete p; | |
564 | return ret; | |
3ce369e6 VS |
565 | } |
566 | ||
567 | ||
568 | ||
f6bcfd97 | 569 | bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2) |
3ce369e6 VS |
570 | { |
571 | // Pass two printout objects: for preview, and possible printing. | |
632783de | 572 | wxPrintDialogData printDialogData(*GetPrintData()); |
3ce369e6 | 573 | wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData); |
04dbb646 | 574 | if (!preview->Ok()) |
4f9297b0 | 575 | { |
3ce369e6 | 576 | delete preview; |
f6bcfd97 | 577 | return FALSE; |
3ce369e6 | 578 | } |
3ca6a5f0 | 579 | |
a5ae8241 | 580 | wxPreviewFrame *frame = new wxPreviewFrame(preview, m_ParentWindow, |
04dbb646 | 581 | m_Name + _(" Preview"), |
3ca6a5f0 | 582 | wxPoint(100, 100), wxSize(650, 500)); |
4f9297b0 VS |
583 | frame->Centre(wxBOTH); |
584 | frame->Initialize(); | |
585 | frame->Show(TRUE); | |
3ca6a5f0 | 586 | return TRUE; |
3ce369e6 VS |
587 | } |
588 | ||
589 | ||
590 | ||
f6bcfd97 | 591 | bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout) |
3ce369e6 | 592 | { |
632783de | 593 | wxPrintDialogData printDialogData(*GetPrintData()); |
3ce369e6 VS |
594 | wxPrinter printer(&printDialogData); |
595 | ||
a5ae8241 | 596 | if (!printer.Print(m_ParentWindow, printout, TRUE)) |
f6bcfd97 BP |
597 | { |
598 | return FALSE; | |
599 | } | |
3ca6a5f0 | 600 | |
632783de | 601 | (*GetPrintData()) = printer.GetPrintDialogData().GetPrintData(); |
3ca6a5f0 | 602 | return TRUE; |
3ce369e6 VS |
603 | } |
604 | ||
605 | ||
606 | ||
607 | void wxHtmlEasyPrinting::PrinterSetup() | |
608 | { | |
632783de | 609 | wxPrintDialogData printDialogData(*GetPrintData()); |
a5ae8241 | 610 | wxPrintDialog printerDialog(m_ParentWindow, &printDialogData); |
04dbb646 | 611 | |
3ce369e6 VS |
612 | printerDialog.GetPrintDialogData().SetSetupDialog(TRUE); |
613 | ||
614 | if (printerDialog.ShowModal() == wxID_OK) | |
632783de | 615 | (*GetPrintData()) = printerDialog.GetPrintDialogData().GetPrintData(); |
3ce369e6 VS |
616 | } |
617 | ||
618 | ||
619 | ||
620 | void wxHtmlEasyPrinting::PageSetup() | |
621 | { | |
632783de | 622 | if (!GetPrintData()->Ok()) |
58cf0491 | 623 | { |
936b18ac | 624 | wxLogError(_("There was a problem during page setup: you may need to set a default printer.")); |
58cf0491 JS |
625 | return; |
626 | } | |
627 | ||
632783de | 628 | m_PageSetupData->SetPrintData(*GetPrintData()); |
a5ae8241 | 629 | wxPageSetupDialog pageSetupDialog(m_ParentWindow, m_PageSetupData); |
3ce369e6 | 630 | |
04dbb646 | 631 | if (pageSetupDialog.ShowModal() == wxID_OK) |
4f9297b0 | 632 | { |
632783de | 633 | (*GetPrintData()) = pageSetupDialog.GetPageSetupData().GetPrintData(); |
3ce369e6 VS |
634 | (*m_PageSetupData) = pageSetupDialog.GetPageSetupData(); |
635 | } | |
636 | } | |
637 | ||
638 | ||
639 | ||
640 | void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg) | |
641 | { | |
04dbb646 | 642 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 643 | m_Headers[0] = header; |
04dbb646 | 644 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
645 | m_Headers[1] = header; |
646 | } | |
647 | ||
648 | ||
649 | ||
650 | void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg) | |
651 | { | |
04dbb646 | 652 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
3ce369e6 | 653 | m_Footers[0] = footer; |
04dbb646 | 654 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
3ce369e6 VS |
655 | m_Footers[1] = footer; |
656 | } | |
657 | ||
658 | ||
4eecf115 VS |
659 | void wxHtmlEasyPrinting::SetFonts(wxString normal_face, wxString fixed_face, |
660 | const int *sizes) | |
661 | { | |
662 | m_FontFaceNormal = normal_face; | |
663 | m_FontFaceFixed = fixed_face; | |
664 | ||
665 | if (sizes) | |
666 | { | |
667 | m_FontsSizes = m_FontsSizesArr; | |
668 | for (int i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i]; | |
669 | } | |
670 | else | |
671 | m_FontsSizes = NULL; | |
672 | } | |
673 | ||
7acd3625 RD |
674 | void wxHtmlEasyPrinting::NormalizeFontSizes(int size) |
675 | { | |
676 | int f_sizes[7]; | |
677 | if (size == -1) | |
678 | size = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT).GetPointSize(); | |
679 | ||
680 | f_sizes[0] = int(size * 0.6); | |
681 | f_sizes[1] = int(size * 0.8); | |
682 | f_sizes[2] = size; | |
683 | f_sizes[3] = int(size * 1.2); | |
684 | f_sizes[4] = int(size * 1.4); | |
685 | f_sizes[5] = int(size * 1.6); | |
686 | f_sizes[6] = int(size * 1.8); | |
687 | ||
688 | SetFonts(wxEmptyString, wxEmptyString, f_sizes); | |
689 | } | |
690 | ||
3ce369e6 VS |
691 | |
692 | wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout() | |
693 | { | |
694 | wxHtmlPrintout *p = new wxHtmlPrintout(m_Name); | |
04dbb646 | 695 | |
4eecf115 VS |
696 | p->SetFonts(m_FontFaceNormal, m_FontFaceFixed, m_FontsSizes); |
697 | ||
4f9297b0 VS |
698 | p->SetHeader(m_Headers[0], wxPAGE_EVEN); |
699 | p->SetHeader(m_Headers[1], wxPAGE_ODD); | |
700 | p->SetFooter(m_Footers[0], wxPAGE_EVEN); | |
701 | p->SetFooter(m_Footers[1], wxPAGE_ODD); | |
702 | ||
703 | p->SetMargins(m_PageSetupData->GetMarginTopLeft().y, | |
704 | m_PageSetupData->GetMarginBottomRight().y, | |
705 | m_PageSetupData->GetMarginTopLeft().x, | |
706 | m_PageSetupData->GetMarginBottomRight().x); | |
04dbb646 | 707 | |
3ce369e6 VS |
708 | return p; |
709 | } | |
710 | ||
fa10c70c JS |
711 | // A module to allow initialization/cleanup |
712 | // without calling these functions from app.cpp or from | |
713 | // the user's application. | |
714 | ||
715 | class wxHtmlPrintingModule: public wxModule | |
716 | { | |
717 | DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule) | |
718 | public: | |
719 | wxHtmlPrintingModule() : wxModule() {} | |
720 | bool OnInit() { return TRUE; } | |
721 | void OnExit() { wxHtmlPrintout::CleanUpStatics(); } | |
722 | }; | |
723 | ||
724 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule, wxModule) | |
725 | ||
3ce369e6 | 726 | |
0cecad31 VS |
727 | // This hack forces the linker to always link in m_* files |
728 | // (wxHTML doesn't work without handlers from these files) | |
729 | #include "wx/html/forcelnk.h" | |
730 | FORCE_WXHTML_MODULES() | |
3ce369e6 | 731 | |
72cdf4c9 | 732 | #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE |