]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmprint.cpp
added wxUSE_INTL around wxLocale::GetSystemEncoding
[wxWidgets.git] / src / html / htmprint.cpp
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
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #include "wx/defs.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #if wxUSE_HTML && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS
30
31 #include "wx/print.h"
32 #include "wx/printdlg.h"
33 #include "wx/html/htmprint.h"
34 #include "wx/wxhtml.h"
35 #include "wx/wfstream.h"
36
37
38 //--------------------------------------------------------------------------------
39 // wxHtmlDCRenderer
40 //--------------------------------------------------------------------------------
41
42
43 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
44 {
45 m_DC = NULL;
46 m_Width = m_Height = 0;
47 m_Cells = NULL;
48 m_Parser = new wxHtmlWinParser(NULL);
49 m_FS = new wxFileSystem();
50 m_Parser->SetFS(m_FS);
51 }
52
53
54
55 wxHtmlDCRenderer::~wxHtmlDCRenderer()
56 {
57 if (m_Cells) delete m_Cells;
58 if (m_Parser) delete m_Parser;
59 if (m_FS) delete m_FS;
60 }
61
62
63
64 void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale)
65 {
66 m_DC = dc;
67 m_Parser->SetDC(m_DC, pixel_scale);
68 }
69
70
71
72 void wxHtmlDCRenderer::SetSize(int width, int height)
73 {
74 m_Width = width;
75 m_Height = height;
76 }
77
78
79
80 void wxHtmlDCRenderer::SetHtmlText(const wxString& html, const wxString& basepath, bool isdir)
81 {
82 if (m_DC == NULL) return;
83
84 if (m_Cells != NULL) delete m_Cells;
85
86 m_FS->ChangePathTo(basepath, isdir);
87 m_Cells = (wxHtmlContainerCell*) m_Parser->Parse(html);
88 m_Cells->SetIndent(0, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
89 m_Cells->Layout(m_Width);
90 }
91
92
93
94 int wxHtmlDCRenderer::Render(int x, int y, int from, int dont_render)
95 {
96 int pbreak, hght;
97
98 if (m_Cells == NULL || m_DC == NULL) return 0;
99
100 pbreak = (int)(from + m_Height);
101 while (m_Cells->AdjustPagebreak(&pbreak)) {}
102 hght = pbreak - from;
103
104 if (!dont_render)
105 {
106 m_DC->SetBrush(*wxWHITE_BRUSH);
107
108 m_DC->SetClippingRegion(x, y, m_Width, hght);
109 m_Cells->Draw(*m_DC,
110 x, (y - from),
111 y, pbreak + (y /*- from*/));
112 m_DC->DestroyClippingRegion();
113 }
114
115 if (pbreak < m_Cells->GetHeight()) return pbreak;
116 else return GetTotalHeight();
117 }
118
119
120
121 int wxHtmlDCRenderer::GetTotalHeight()
122 {
123 if (m_Cells) return m_Cells->GetHeight();
124 else return 0;
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 //--------------------------------------------------------------------------------
143 // wxHtmlPrintout
144 //--------------------------------------------------------------------------------
145
146
147
148 wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title)
149 {
150 m_Renderer = new wxHtmlDCRenderer;
151 m_RendererHdr = new wxHtmlDCRenderer;
152 m_NumPages = wxHTML_PRINT_MAX_PAGES;
153 m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = TRUE;
154 m_Headers[0] = m_Headers[1] = wxEmptyString;
155 m_Footers[0] = m_Footers[1] = wxEmptyString;
156 m_HeaderHeight = m_FooterHeight = 0;
157 SetMargins(); // to default values
158 }
159
160
161
162 wxHtmlPrintout::~wxHtmlPrintout()
163 {
164 delete m_Renderer;
165 delete m_RendererHdr;
166 }
167
168
169
170 bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage)
171 {
172 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
173 float ppmm_h, ppmm_v;
174
175 if (!wxPrintout::OnBeginDocument(startPage, endPage)) return FALSE;
176
177 GetPageSizePixels(&pageWidth, &pageHeight);
178 GetPageSizeMM(&mm_w, &mm_h);
179 ppmm_h = (float)pageWidth / mm_w;
180 ppmm_v = (float)pageHeight / mm_h;
181
182 int ppiPrinterX, ppiPrinterY;
183 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
184 int ppiScreenX, ppiScreenY;
185 GetPPIScreen(&ppiScreenX, &ppiScreenY);
186
187 wxDisplaySize(&scr_w, &scr_h);
188 GetDC()->GetSize(&dc_w, &dc_h);
189
190 GetDC()->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
191
192 /* prepare headers/footers renderer: */
193
194 m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
195 m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
196 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom)));
197 if (m_Headers[0] != wxEmptyString)
198 {
199 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1));
200 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
201 }
202 else if (m_Headers[1] != wxEmptyString)
203 {
204 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1));
205 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
206 }
207 if (m_Footers[0] != wxEmptyString)
208 {
209 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1));
210 m_FooterHeight = m_RendererHdr->GetTotalHeight();
211 }
212 else if (m_Footers[1] != wxEmptyString)
213 {
214 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1));
215 m_FooterHeight = m_RendererHdr->GetTotalHeight();
216 }
217
218 /* prepare main renderer: */
219 m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
220 m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
221 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) -
222 m_FooterHeight - m_HeaderHeight -
223 ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) -
224 ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v)
225 ));
226 m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir);
227 CountPages();
228 return TRUE;
229 }
230
231
232 bool wxHtmlPrintout::OnPrintPage(int page)
233 {
234 wxDC *dc = GetDC();
235 if (dc)
236 {
237 if (HasPage(page))
238 RenderPage(dc, page);
239 return TRUE;
240 }
241 else return FALSE;
242 }
243
244
245 void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
246 {
247 *minPage = 1;
248 *maxPage = wxHTML_PRINT_MAX_PAGES;
249 *selPageFrom = 1;
250 *selPageTo = wxHTML_PRINT_MAX_PAGES;
251 }
252
253
254
255 bool wxHtmlPrintout::HasPage(int pageNum)
256 {
257 return (pageNum >= 1 && pageNum <= m_NumPages);
258 }
259
260
261
262 void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath, bool isdir)
263 {
264 m_Document = html;
265 m_BasePath = basepath;
266 m_BasePathIsDir = isdir;
267 }
268
269
270
271 void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile)
272 {
273 wxFileSystem fs;
274 wxFSFile *ff = fs.OpenFile(htmlfile);
275
276 if (ff == NULL)
277 {
278 wxLogError(htmlfile + _(": file does not exist!"));
279 return;
280 }
281
282 wxInputStream *st = ff->GetStream();
283 char *t = new char[st->GetSize() + 1];
284 st->Read(t, st->GetSize());
285 t[st->GetSize()] = 0;
286
287 wxString doc = wxString(t);
288 delete t;
289 delete ff;
290
291 SetHtmlText(doc, htmlfile, FALSE);
292 }
293
294
295
296 void wxHtmlPrintout::SetHeader(const wxString& header, int pg)
297 {
298 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
299 m_Headers[0] = header;
300 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
301 m_Headers[1] = header;
302 }
303
304
305
306 void wxHtmlPrintout::SetFooter(const wxString& footer, int pg)
307 {
308 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
309 m_Footers[0] = footer;
310 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
311 m_Footers[1] = footer;
312 }
313
314
315
316 void wxHtmlPrintout::CountPages()
317 {
318 wxBusyCursor wait;
319 int pageWidth, pageHeight, mm_w, mm_h;
320 float ppmm_h, ppmm_v;
321
322 GetPageSizePixels(&pageWidth, &pageHeight);
323 GetPageSizeMM(&mm_w, &mm_h);
324 ppmm_h = (float)pageWidth / mm_w;
325 ppmm_v = (float)pageHeight / mm_h;
326
327 int pos = 0;
328
329 m_NumPages = 0;
330
331 m_PageBreaks[0] = 0;
332 do
333 {
334 pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft),
335 (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
336 pos, TRUE);
337 m_PageBreaks[++m_NumPages] = pos;
338 } while (pos < m_Renderer->GetTotalHeight());
339 }
340
341
342
343 void wxHtmlPrintout::RenderPage(wxDC *dc, int page)
344 {
345 wxBusyCursor wait;
346
347 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
348 float ppmm_h, ppmm_v;
349
350 GetPageSizePixels(&pageWidth, &pageHeight);
351 GetPageSizeMM(&mm_w, &mm_h);
352 ppmm_h = (float)pageWidth / mm_w;
353 ppmm_v = (float)pageHeight / mm_h;
354 wxDisplaySize(&scr_w, &scr_h);
355 dc->GetSize(&dc_w, &dc_h);
356
357 int ppiPrinterX, ppiPrinterY;
358 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
359 int ppiScreenX, ppiScreenY;
360 GetPPIScreen(&ppiScreenX, &ppiScreenY);
361
362 dc->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
363
364 m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY);
365
366 dc->SetBackgroundMode(wxTRANSPARENT);
367
368 m_Renderer->Render((int) (ppmm_h * m_MarginLeft),
369 (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
370 m_PageBreaks[page-1]);
371
372 m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY);
373 if (m_Headers[page % 2] != wxEmptyString)
374 {
375 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page));
376 m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop));
377 }
378 if (m_Footers[page % 2] != wxEmptyString)
379 {
380 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page));
381 m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight));
382 }
383 }
384
385
386
387 wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page)
388 {
389 wxString r = instr;
390 wxString num;
391
392 num.Printf(wxT("%i"), page);
393 r.Replace(wxT("@PAGENUM@"), num);
394
395 num.Printf(wxT("%i"), m_NumPages);
396 r.Replace(wxT("@PAGESCNT@"), num);
397
398 return r;
399 }
400
401
402
403 void wxHtmlPrintout::SetMargins(float top, float bottom, float left, float right, float spaces)
404 {
405 m_MarginTop = top;
406 m_MarginBottom = bottom;
407 m_MarginLeft = left;
408 m_MarginRight = right;
409 m_MarginSpace = spaces;
410 }
411
412
413
414
415
416
417 //--------------------------------------------------------------------------------
418 // wxHtmlEasyPrinting
419 //--------------------------------------------------------------------------------
420
421
422 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxFrame *parent_frame)
423 {
424 m_Frame = parent_frame;
425 m_Name = name;
426 m_PrintData = new wxPrintData;
427 #if (defined __WXGTK__) || (defined __WXMOTIF__)
428 (*m_PrintData) = (*wxThePrintSetupData);
429 #endif
430 m_PageSetupData = new wxPageSetupDialogData;
431 m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString;
432
433 m_PageSetupData->EnableMargins(TRUE);
434 m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25));
435 m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25));
436 }
437
438
439
440 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
441 {
442 delete m_PrintData;
443 delete m_PageSetupData;
444 }
445
446
447
448 bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile)
449 {
450 wxHtmlPrintout *p1 = CreatePrintout();
451 p1->SetHtmlFile(htmlfile);
452 wxHtmlPrintout *p2 = CreatePrintout();
453 p2->SetHtmlFile(htmlfile);
454 return DoPreview(p1, p2);
455 }
456
457
458
459 bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath)
460 {
461 wxHtmlPrintout *p1 = CreatePrintout();
462 p1->SetHtmlText(htmltext, basepath, TRUE);
463 wxHtmlPrintout *p2 = CreatePrintout();
464 p2->SetHtmlText(htmltext, basepath, TRUE);
465 return DoPreview(p1, p2);
466 }
467
468
469
470 bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile)
471 {
472 wxHtmlPrintout *p = CreatePrintout();
473 p->SetHtmlFile(htmlfile);
474 bool ret = DoPrint(p);
475 delete p;
476 return ret;
477 }
478
479
480
481 bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath)
482 {
483 wxHtmlPrintout *p = CreatePrintout();
484 p->SetHtmlText(htmltext, basepath, TRUE);
485 bool ret = DoPrint(p);
486 delete p;
487 return ret;
488 }
489
490
491
492 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2)
493 {
494 // Pass two printout objects: for preview, and possible printing.
495 wxPrintDialogData printDialogData(*m_PrintData);
496 wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData);
497 if (!preview->Ok())
498 {
499 delete preview;
500 return FALSE;
501 }
502
503 wxPreviewFrame *frame = new wxPreviewFrame(preview, m_Frame,
504 m_Name + _(" Preview"),
505 wxPoint(100, 100), wxSize(650, 500));
506 frame->Centre(wxBOTH);
507 frame->Initialize();
508 frame->Show(TRUE);
509 return TRUE;
510 }
511
512
513
514 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout)
515 {
516 wxPrintDialogData printDialogData(*m_PrintData);
517 wxPrinter printer(&printDialogData);
518
519 if (!printer.Print(m_Frame, printout, TRUE))
520 {
521 return FALSE;
522 }
523
524 (*m_PrintData) = printer.GetPrintDialogData().GetPrintData();
525 return TRUE;
526 }
527
528
529
530 void wxHtmlEasyPrinting::PrinterSetup()
531 {
532 wxPrintDialogData printDialogData(*m_PrintData);
533 wxPrintDialog printerDialog(m_Frame, &printDialogData);
534
535 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
536
537 if (printerDialog.ShowModal() == wxID_OK)
538 (*m_PrintData) = printerDialog.GetPrintDialogData().GetPrintData();
539 }
540
541
542
543 void wxHtmlEasyPrinting::PageSetup()
544 {
545 m_PageSetupData->SetPrintData(*m_PrintData);
546 wxPageSetupDialog pageSetupDialog(m_Frame, m_PageSetupData);
547
548 if (pageSetupDialog.ShowModal() == wxID_OK)
549 {
550 (*m_PrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
551 (*m_PageSetupData) = pageSetupDialog.GetPageSetupData();
552 }
553 }
554
555
556
557 void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg)
558 {
559 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
560 m_Headers[0] = header;
561 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
562 m_Headers[1] = header;
563 }
564
565
566
567 void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg)
568 {
569 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
570 m_Footers[0] = footer;
571 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
572 m_Footers[1] = footer;
573 }
574
575
576
577 wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout()
578 {
579 wxHtmlPrintout *p = new wxHtmlPrintout(m_Name);
580
581 p->SetHeader(m_Headers[0], wxPAGE_EVEN);
582 p->SetHeader(m_Headers[1], wxPAGE_ODD);
583 p->SetFooter(m_Footers[0], wxPAGE_EVEN);
584 p->SetFooter(m_Footers[1], wxPAGE_ODD);
585
586 p->SetMargins(m_PageSetupData->GetMarginTopLeft().y,
587 m_PageSetupData->GetMarginBottomRight().y,
588 m_PageSetupData->GetMarginTopLeft().x,
589 m_PageSetupData->GetMarginBottomRight().x);
590
591 return p;
592 }
593
594
595
596 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE