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