]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/html/htmprint.cpp
Fixed some warnings (don't know why dialoged Makefile is being committed)
[wxWidgets.git] / src / html / htmprint.cpp
... / ...
CommitLineData
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
43wxHtmlDCRenderer::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
55wxHtmlDCRenderer::~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
64void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale)
65{
66 m_DC = dc;
67 m_Parser->SetDC(m_DC, pixel_scale);
68}
69
70
71
72void wxHtmlDCRenderer::SetSize(int width, int height)
73{
74 m_Width = width;
75 m_Height = height;
76}
77
78
79
80void 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
94int 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
121int 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
148wxHtmlPrintout::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
162wxHtmlPrintout::~wxHtmlPrintout()
163{
164 delete m_Renderer;
165 delete m_RendererHdr;
166}
167
168
169
170bool 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_MarginTop)),
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_MarginTop)),
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
232bool 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
245void 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
255bool wxHtmlPrintout::HasPage(int pageNum)
256{
257 return (pageNum >= 1 && pageNum <= m_NumPages);
258}
259
260
261
262void 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
271void 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
296void 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
306void 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
316void 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
343void 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
387wxString 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
403void 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
422wxHtmlEasyPrinting::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
440wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
441{
442 delete m_PrintData;
443 delete m_PageSetupData;
444}
445
446
447
448bool 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
459bool 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
470bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile)
471{
472 wxHtmlPrintout *p = CreatePrintout();
473 p->SetHtmlFile(htmlfile);
474 return DoPrint(p);
475}
476
477
478
479bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath)
480{
481 wxHtmlPrintout *p = CreatePrintout();
482 p->SetHtmlText(htmltext, basepath, TRUE);
483 return DoPrint(p);
484}
485
486
487
488bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2)
489{
490 // Pass two printout objects: for preview, and possible printing.
491 wxPrintDialogData printDialogData(*m_PrintData);
492 wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData);
493 if (!preview->Ok())
494 {
495 delete preview;
496 return FALSE;
497 }
498
499 wxPreviewFrame *frame = new wxPreviewFrame(preview, m_Frame,
500 m_Name + _(" Preview"),
501 wxPoint(100, 100), wxSize(650, 500));
502 frame->Centre(wxBOTH);
503 frame->Initialize();
504 frame->Show(TRUE);
505 return TRUE;
506}
507
508
509
510bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout)
511{
512 wxPrintDialogData printDialogData(*m_PrintData);
513 wxPrinter printer(&printDialogData);
514
515 if (!printer.Print(m_Frame, printout, TRUE))
516 {
517 return FALSE;
518 }
519
520 (*m_PrintData) = printer.GetPrintDialogData().GetPrintData();
521 return TRUE;
522}
523
524
525
526void wxHtmlEasyPrinting::PrinterSetup()
527{
528 wxPrintDialogData printDialogData(*m_PrintData);
529 wxPrintDialog printerDialog(m_Frame, &printDialogData);
530
531 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
532
533 if (printerDialog.ShowModal() == wxID_OK)
534 (*m_PrintData) = printerDialog.GetPrintDialogData().GetPrintData();
535}
536
537
538
539void wxHtmlEasyPrinting::PageSetup()
540{
541 m_PageSetupData->SetPrintData(*m_PrintData);
542 wxPageSetupDialog pageSetupDialog(m_Frame, m_PageSetupData);
543
544 if (pageSetupDialog.ShowModal() == wxID_OK)
545 {
546 (*m_PrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
547 (*m_PageSetupData) = pageSetupDialog.GetPageSetupData();
548 }
549}
550
551
552
553void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg)
554{
555 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
556 m_Headers[0] = header;
557 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
558 m_Headers[1] = header;
559}
560
561
562
563void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg)
564{
565 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
566 m_Footers[0] = footer;
567 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
568 m_Footers[1] = footer;
569}
570
571
572
573wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout()
574{
575 wxHtmlPrintout *p = new wxHtmlPrintout(m_Name);
576
577 p->SetHeader(m_Headers[0], wxPAGE_EVEN);
578 p->SetHeader(m_Headers[1], wxPAGE_ODD);
579 p->SetFooter(m_Footers[0], wxPAGE_EVEN);
580 p->SetFooter(m_Footers[1], wxPAGE_ODD);
581
582 p->SetMargins(m_PageSetupData->GetMarginTopLeft().y,
583 m_PageSetupData->GetMarginBottomRight().y,
584 m_PageSetupData->GetMarginTopLeft().x,
585 m_PageSetupData->GetMarginBottomRight().x);
586
587 return p;
588}
589
590
591
592#endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE