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