]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmprint.cpp
added new project file having the new files
[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/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/print.h"
34 #include "wx/printdlg.h"
35 #include "wx/html/htmprint.h"
36 #include "wx/wxhtml.h"
37 #include "wx/wfstream.h"
38
39
40 //--------------------------------------------------------------------------------
41 // wxHtmlDCRenderer
42 //--------------------------------------------------------------------------------
43
44
45 wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject()
46 {
47 m_DC = NULL;
48 m_Width = m_Height = 0;
49 m_Cells = NULL;
50 m_Parser = new wxHtmlWinParser(NULL);
51 m_FS = new wxFileSystem();
52 m_Parser->SetFS(m_FS);
53 }
54
55
56
57 wxHtmlDCRenderer::~wxHtmlDCRenderer()
58 {
59 if (m_Cells) delete m_Cells;
60 if (m_Parser) delete m_Parser;
61 if (m_FS) delete m_FS;
62 }
63
64
65
66 void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale)
67 {
68 m_DC = dc;
69 m_Parser->SetDC(m_DC, pixel_scale);
70 }
71
72
73
74 void wxHtmlDCRenderer::SetSize(int width, int height)
75 {
76 m_Width = width;
77 m_Height = height;
78 }
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
96 int wxHtmlDCRenderer::Render(int x, int y, int from, int dont_render)
97 {
98 int pbreak, hght;
99
100 if (m_Cells == NULL || m_DC == NULL) return 0;
101
102 pbreak = (int)(from + m_Height);
103 while (m_Cells->AdjustPagebreak(&pbreak)) {}
104 hght = pbreak - from;
105
106 if (!dont_render)
107 {
108 m_DC->SetBrush(*wxWHITE_BRUSH);
109
110 m_DC->SetClippingRegion(x, y, m_Width, hght);
111 m_Cells->Draw(*m_DC,
112 x, (y - from),
113 y, pbreak + (y /*- from*/));
114 m_DC->DestroyClippingRegion();
115 }
116
117 if (pbreak < m_Cells->GetHeight()) return pbreak;
118 else return GetTotalHeight();
119 }
120
121
122
123 int wxHtmlDCRenderer::GetTotalHeight()
124 {
125 if (m_Cells) return m_Cells->GetHeight();
126 else return 0;
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 //--------------------------------------------------------------------------------
145 // wxHtmlPrintout
146 //--------------------------------------------------------------------------------
147
148
149
150 wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title)
151 {
152 m_Renderer = new wxHtmlDCRenderer;
153 m_RendererHdr = new wxHtmlDCRenderer;
154 m_NumPages = wxHTML_PRINT_MAX_PAGES;
155 m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = TRUE;
156 m_Headers[0] = m_Headers[1] = wxEmptyString;
157 m_Footers[0] = m_Footers[1] = wxEmptyString;
158 m_HeaderHeight = m_FooterHeight = 0;
159 SetMargins(); // to default values
160 }
161
162
163
164 wxHtmlPrintout::~wxHtmlPrintout()
165 {
166 delete m_Renderer;
167 delete m_RendererHdr;
168 }
169
170
171
172 bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage)
173 {
174 int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h;
175 float ppmm_h, ppmm_v;
176
177 if (!wxPrintout::OnBeginDocument(startPage, endPage)) return FALSE;
178
179 GetPageSizePixels(&pageWidth, &pageHeight);
180 GetPageSizeMM(&mm_w, &mm_h);
181 ppmm_h = (float)pageWidth / mm_w;
182 ppmm_v = (float)pageHeight / mm_h;
183
184 int ppiPrinterX, ppiPrinterY;
185 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
186 int ppiScreenX, ppiScreenY;
187 GetPPIScreen(&ppiScreenX, &ppiScreenY);
188
189 wxDisplaySize(&scr_w, &scr_h);
190 GetDC()->GetSize(&dc_w, &dc_h);
191
192 GetDC()->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth);
193
194 /* prepare headers/footers renderer: */
195
196 m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
197 m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
198 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom)));
199 if (m_Headers[0] != wxEmptyString)
200 {
201 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1));
202 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
203 }
204 else if (m_Headers[1] != wxEmptyString)
205 {
206 m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1));
207 m_HeaderHeight = m_RendererHdr->GetTotalHeight();
208 }
209 if (m_Footers[0] != wxEmptyString)
210 {
211 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1));
212 m_FooterHeight = m_RendererHdr->GetTotalHeight();
213 }
214 else if (m_Footers[1] != wxEmptyString)
215 {
216 m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1));
217 m_FooterHeight = m_RendererHdr->GetTotalHeight();
218 }
219
220 /* prepare main renderer: */
221 m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY);
222 m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)),
223 (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) -
224 m_FooterHeight - m_HeaderHeight -
225 ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) -
226 ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v)
227 ));
228 m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir);
229 CountPages();
230 return TRUE;
231 }
232
233
234 bool wxHtmlPrintout::OnPrintPage(int page)
235 {
236 wxDC *dc = GetDC();
237 if (dc)
238 {
239 if (HasPage(page))
240 RenderPage(dc, page);
241 return TRUE;
242 }
243 else return FALSE;
244 }
245
246
247 void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
248 {
249 *minPage = 1;
250 *maxPage = wxHTML_PRINT_MAX_PAGES;
251 *selPageFrom = 1;
252 *selPageTo = wxHTML_PRINT_MAX_PAGES;
253 }
254
255
256
257 bool wxHtmlPrintout::HasPage(int pageNum)
258 {
259 return (pageNum >= 1 && pageNum <= m_NumPages);
260 }
261
262
263
264 void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath, bool isdir)
265 {
266 m_Document = html;
267 m_BasePath = basepath;
268 m_BasePathIsDir = isdir;
269 }
270
271
272
273 void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile)
274 {
275 wxFileSystem fs;
276 wxFSFile *ff = fs.OpenFile(htmlfile);
277
278 if (ff == NULL)
279 {
280 wxLogError(htmlfile + _(": file does not exist!"));
281 return;
282 }
283
284 wxInputStream *st = ff->GetStream();
285 char *t = new char[st->GetSize() + 1];
286 st->Read(t, st->GetSize());
287 t[st->GetSize()] = 0;
288
289 wxString doc = wxString(t);
290 delete t;
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 m_PageSetupData->SetPrintData(*m_PrintData);
548 wxPageSetupDialog pageSetupDialog(m_Frame, m_PageSetupData);
549
550 if (pageSetupDialog.ShowModal() == wxID_OK)
551 {
552 (*m_PrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
553 (*m_PageSetupData) = pageSetupDialog.GetPageSetupData();
554 }
555 }
556
557
558
559 void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg)
560 {
561 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
562 m_Headers[0] = header;
563 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
564 m_Headers[1] = header;
565 }
566
567
568
569 void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg)
570 {
571 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
572 m_Footers[0] = footer;
573 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
574 m_Footers[1] = footer;
575 }
576
577
578
579 wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout()
580 {
581 wxHtmlPrintout *p = new wxHtmlPrintout(m_Name);
582
583 p->SetHeader(m_Headers[0], wxPAGE_EVEN);
584 p->SetHeader(m_Headers[1], wxPAGE_ODD);
585 p->SetFooter(m_Footers[0], wxPAGE_EVEN);
586 p->SetFooter(m_Footers[1], wxPAGE_ODD);
587
588 p->SetMargins(m_PageSetupData->GetMarginTopLeft().y,
589 m_PageSetupData->GetMarginBottomRight().y,
590 m_PageSetupData->GetMarginTopLeft().x,
591 m_PageSetupData->GetMarginBottomRight().x);
592
593 return p;
594 }
595
596
597
598 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE