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