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