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