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