]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmprint.cpp
supported controls on toolbar
[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)
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)) {}
111 hght = pbreak - from;
112
113 if (!dont_render)
114 {
115 m_DC->SetBrush(*wxWHITE_BRUSH);
116
117 m_DC->SetClippingRegion(x, y, m_Width, hght);
118 m_Cells->Draw(*m_DC,
119 x, (y - from),
120 y, pbreak + (y /*- from*/));
121 m_DC->DestroyClippingRegion();
122 }
123
124 if (pbreak < m_Cells->GetHeight()) return pbreak;
125 else return GetTotalHeight();
126 }
127
128
129
130 int wxHtmlDCRenderer::GetTotalHeight()
131 {
132 if (m_Cells) return m_Cells->GetHeight();
133 else return 0;
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 //--------------------------------------------------------------------------------
152 // wxHtmlPrintout
153 //--------------------------------------------------------------------------------
154
155
156
157 wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title)
158 {
159 m_Renderer = new wxHtmlDCRenderer;
160 m_RendererHdr = new wxHtmlDCRenderer;
161 m_NumPages = wxHTML_PRINT_MAX_PAGES;
162 m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = TRUE;
163 m_Headers[0] = m_Headers[1] = wxEmptyString;
164 m_Footers[0] = m_Footers[1] = wxEmptyString;
165 m_HeaderHeight = m_FooterHeight = 0;
166 SetMargins(); // to default values
167 }
168
169
170
171 wxHtmlPrintout::~wxHtmlPrintout()
172 {
173 delete m_Renderer;
174 delete m_RendererHdr;
175 }
176
177
178
179 bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage)
180 {
181 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
182 float ppmm_h, ppmm_v;
183
184 if (!wxPrintout::OnBeginDocument(startPage, endPage)) return FALSE;
185
186 GetPageSizePixels(&pageWidth, &pageHeight);
187 GetPageSizeMM(&mm_w, &mm_h);
188 ppmm_h = (float)pageWidth / mm_w;
189 ppmm_v = (float)pageHeight / mm_h;
190
191 int ppiPrinterX, ppiPrinterY;
192 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
193 int ppiScreenX, ppiScreenY;
194 GetPPIScreen(&ppiScreenX, &ppiScreenY);
195
196 wxDisplaySize(&scr_w, &scr_h);
197 GetDC()->GetSize(&dc_w, &dc_h);
198
199 GetDC()->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
200
201 /* prepare headers/footers renderer: */
202
203 m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
204 m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
205 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom)));
206 if (m_Headers[0] != wxEmptyString)
207 {
208 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1));
209 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
210 }
211 else if (m_Headers[1] != wxEmptyString)
212 {
213 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1));
214 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
215 }
216 if (m_Footers[0] != wxEmptyString)
217 {
218 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1));
219 m_FooterHeight = m_RendererHdr->GetTotalHeight();
220 }
221 else if (m_Footers[1] != wxEmptyString)
222 {
223 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1));
224 m_FooterHeight = m_RendererHdr->GetTotalHeight();
225 }
226
227 /* prepare main renderer: */
228 m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
229 m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
230 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) -
231 m_FooterHeight - m_HeaderHeight -
232 ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) -
233 ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v)
234 ));
235 m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir);
236 CountPages();
237 return TRUE;
238 }
239
240
241 bool wxHtmlPrintout::OnPrintPage(int page)
242 {
243 wxDC *dc = GetDC();
244 if (dc)
245 {
246 if (HasPage(page))
247 RenderPage(dc, page);
248 return TRUE;
249 }
250 else return FALSE;
251 }
252
253
254 void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
255 {
256 *minPage = 1;
257 *maxPage = wxHTML_PRINT_MAX_PAGES;
258 *selPageFrom = 1;
259 *selPageTo = wxHTML_PRINT_MAX_PAGES;
260 }
261
262
263
264 bool wxHtmlPrintout::HasPage(int pageNum)
265 {
266 return (pageNum >= 1 && pageNum <= m_NumPages);
267 }
268
269
270
271 void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath, bool isdir)
272 {
273 m_Document = html;
274 m_BasePath = basepath;
275 m_BasePathIsDir = isdir;
276 }
277
278 void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile)
279 {
280 wxFileSystem fs;
281 wxFSFile *ff = fs.OpenFile(htmlfile);
282
283 if (ff == NULL)
284 {
285 wxLogError(htmlfile + _(": file does not exist!"));
286 return;
287 }
288
289 wxHtmlFilterHTML filter;
290 wxString doc = filter.ReadFile(*ff);
291
292 SetHtmlText(doc, htmlfile, FALSE);
293 delete ff;
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 void wxHtmlPrintout::SetFonts(wxString normal_face, wxString fixed_face,
418 const int *sizes)
419 {
420 m_Renderer->SetFonts(normal_face, fixed_face, sizes);
421 m_RendererHdr->SetFonts(normal_face, fixed_face, sizes);
422 }
423
424
425
426 //----------------------------------------------------------------------------
427 // wxHtmlEasyPrinting
428 //----------------------------------------------------------------------------
429
430
431 wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxFrame *parent_frame)
432 {
433 m_Frame = parent_frame;
434 m_Name = name;
435 m_PrintData = new wxPrintData;
436 m_PageSetupData = new wxPageSetupDialogData;
437 m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString;
438
439 m_PageSetupData->EnableMargins(TRUE);
440 m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25));
441 m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25));
442
443 SetFonts(wxEmptyString, wxEmptyString, NULL);
444 }
445
446
447
448 wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
449 {
450 delete m_PrintData;
451 delete m_PageSetupData;
452 }
453
454
455
456 bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile)
457 {
458 wxHtmlPrintout *p1 = CreatePrintout();
459 p1->SetHtmlFile(htmlfile);
460 wxHtmlPrintout *p2 = CreatePrintout();
461 p2->SetHtmlFile(htmlfile);
462 return DoPreview(p1, p2);
463 }
464
465
466
467 bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath)
468 {
469 wxHtmlPrintout *p1 = CreatePrintout();
470 p1->SetHtmlText(htmltext, basepath, TRUE);
471 wxHtmlPrintout *p2 = CreatePrintout();
472 p2->SetHtmlText(htmltext, basepath, TRUE);
473 return DoPreview(p1, p2);
474 }
475
476
477
478 bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile)
479 {
480 wxHtmlPrintout *p = CreatePrintout();
481 p->SetHtmlFile(htmlfile);
482 bool ret = DoPrint(p);
483 delete p;
484 return ret;
485 }
486
487
488
489 bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath)
490 {
491 wxHtmlPrintout *p = CreatePrintout();
492 p->SetHtmlText(htmltext, basepath, TRUE);
493 bool ret = DoPrint(p);
494 delete p;
495 return ret;
496 }
497
498
499
500 bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2)
501 {
502 // Pass two printout objects: for preview, and possible printing.
503 wxPrintDialogData printDialogData(*m_PrintData);
504 wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData);
505 if (!preview->Ok())
506 {
507 delete preview;
508 return FALSE;
509 }
510
511 wxPreviewFrame *frame = new wxPreviewFrame(preview, m_Frame,
512 m_Name + _(" Preview"),
513 wxPoint(100, 100), wxSize(650, 500));
514 frame->Centre(wxBOTH);
515 frame->Initialize();
516 frame->Show(TRUE);
517 return TRUE;
518 }
519
520
521
522 bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout)
523 {
524 wxPrintDialogData printDialogData(*m_PrintData);
525 wxPrinter printer(&printDialogData);
526
527 if (!printer.Print(m_Frame, printout, TRUE))
528 {
529 return FALSE;
530 }
531
532 (*m_PrintData) = printer.GetPrintDialogData().GetPrintData();
533 return TRUE;
534 }
535
536
537
538 void wxHtmlEasyPrinting::PrinterSetup()
539 {
540 wxPrintDialogData printDialogData(*m_PrintData);
541 wxPrintDialog printerDialog(m_Frame, &printDialogData);
542
543 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
544
545 if (printerDialog.ShowModal() == wxID_OK)
546 (*m_PrintData) = printerDialog.GetPrintDialogData().GetPrintData();
547 }
548
549
550
551 void wxHtmlEasyPrinting::PageSetup()
552 {
553 if (!m_PrintData->Ok())
554 {
555 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
556 return;
557 }
558
559 m_PageSetupData->SetPrintData(*m_PrintData);
560 wxPageSetupDialog pageSetupDialog(m_Frame, m_PageSetupData);
561
562 if (pageSetupDialog.ShowModal() == wxID_OK)
563 {
564 (*m_PrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
565 (*m_PageSetupData) = pageSetupDialog.GetPageSetupData();
566 }
567 }
568
569
570
571 void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg)
572 {
573 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
574 m_Headers[0] = header;
575 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
576 m_Headers[1] = header;
577 }
578
579
580
581 void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg)
582 {
583 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
584 m_Footers[0] = footer;
585 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
586 m_Footers[1] = footer;
587 }
588
589
590 void wxHtmlEasyPrinting::SetFonts(wxString normal_face, wxString fixed_face,
591 const int *sizes)
592 {
593 m_FontFaceNormal = normal_face;
594 m_FontFaceFixed = fixed_face;
595
596 if (sizes)
597 {
598 m_FontsSizes = m_FontsSizesArr;
599 for (int i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i];
600 }
601 else
602 m_FontsSizes = NULL;
603 }
604
605
606 wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout()
607 {
608 wxHtmlPrintout *p = new wxHtmlPrintout(m_Name);
609
610 p->SetFonts(m_FontFaceNormal, m_FontFaceFixed, m_FontsSizes);
611
612 p->SetHeader(m_Headers[0], wxPAGE_EVEN);
613 p->SetHeader(m_Headers[1], wxPAGE_ODD);
614 p->SetFooter(m_Footers[0], wxPAGE_EVEN);
615 p->SetFooter(m_Footers[1], wxPAGE_ODD);
616
617 p->SetMargins(m_PageSetupData->GetMarginTopLeft().y,
618 m_PageSetupData->GetMarginBottomRight().y,
619 m_PageSetupData->GetMarginTopLeft().x,
620 m_PageSetupData->GetMarginBottomRight().x);
621
622 return p;
623 }
624
625
626 // This hack forces the linker to always link in m_* files
627 // (wxHTML doesn't work without handlers from these files)
628 #include "wx/html/forcelnk.h"
629 FORCE_WXHTML_MODULES()
630
631 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE