]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmprint.cpp
added several other accel strings
[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
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/wx.h"
25#endif
26
27#if 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
41wxHtmlDCRenderer::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
54wxHtmlDCRenderer::~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
63void 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
82void 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
90void 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, HTML_INDENT_ALL, HTML_UNITS_PIXELS);
100 m_Cells -> Layout(m_Width);
101}
102
103
104
105int 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
135int 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
162wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title)
163{
164 m_Renderer = new wxHtmlDCRenderer;
165 m_RendererHdr = new wxHtmlDCRenderer;
166 m_NumPages = HTML_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
176wxHtmlPrintout::~wxHtmlPrintout()
177{
178 delete m_Renderer;
179 delete m_RendererHdr;
180}
181
182
183
184bool wxHtmlPrintout::OnBeginDocument(int start, int end)
185{
186 int pageWidth, pageHeight, mm_w, mm_h;
187 float ppmm_h, ppmm_v;
188
189 if (!wxPrintout::OnBeginDocument(start, end)) return FALSE;
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 return TRUE;
230}
231
232
233bool 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
245void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
246{
247 *minPage = 1;
248 *maxPage = HTML_PRINT_MAX_PAGES;
249 *selPageFrom = 1;
250 *selPageTo = 1;
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 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
289void 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
299void 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
309void 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
335void 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
368wxString 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
384void 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
403wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxFrame *parent_frame)
404{
405 m_Frame = parent_frame;
406 m_Name = name;
407 m_PrintData = new wxPrintData;
408 m_PageSetupData = new wxPageSetupDialogData;
409 m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString;
410
411 m_PageSetupData -> EnableMargins(TRUE);
412 m_PageSetupData -> SetMarginTopLeft(wxPoint(25, 25));
413 m_PageSetupData -> SetMarginBottomRight(wxPoint(25, 25));
414
415#if defined(__WXGTK__) || defined(__WXMOTIF__)
416 m_PrintData -> SetPrinterCommand("lpr");
417#endif
418}
419
420
421
422wxHtmlEasyPrinting::~wxHtmlEasyPrinting()
423{
424 delete m_PrintData;
425 delete m_PageSetupData;
426}
427
428
429
430void wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile)
431{
432 wxHtmlPrintout *p1 = CreatePrintout();
433 p1 -> SetHtmlFile(htmlfile);
434 wxHtmlPrintout *p2 = CreatePrintout();
435 p2 -> SetHtmlFile(htmlfile);
436 DoPreview(p1, p2);
437}
438
439
440
441void wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath)
442{
443 wxHtmlPrintout *p1 = CreatePrintout();
444 p1 -> SetHtmlText(htmltext, basepath, TRUE);
445 wxHtmlPrintout *p2 = CreatePrintout();
446 p2 -> SetHtmlText(htmltext, basepath, TRUE);
447 DoPreview(p1, p2);
448}
449
450
451
452void wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile)
453{
454 wxHtmlPrintout *p = CreatePrintout();
455 p -> SetHtmlFile(htmlfile);
456 DoPrint(p);
457}
458
459
460
461void wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath)
462{
463 wxHtmlPrintout *p = CreatePrintout();
464 p -> SetHtmlText(htmltext, basepath, TRUE);
465 DoPrint(p);
466}
467
468
469
470void wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2)
471{
472 // Pass two printout objects: for preview, and possible printing.
473 wxPrintDialogData printDialogData(*m_PrintData);
474 wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData);
475 if (!preview -> Ok()) {
476 delete preview;
477 wxMessageBox(_("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _("Previewing"), wxOK);
478 }
479
480 else {
481 wxPreviewFrame *frame = new wxPreviewFrame(preview, m_Frame,
482 m_Name + _(" Preview"),
483 wxPoint(100, 100), wxSize(500, 500));
484 frame -> Centre(wxBOTH);
485 frame -> Initialize();
486 frame -> Show(TRUE);
487 }
488}
489
490
491
492void wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout)
493{
494 wxPrintDialogData printDialogData(*m_PrintData);
495 wxPrinter printer(&printDialogData);
496
497 if (!printer.Print(m_Frame, printout, TRUE))
498 wxMessageBox(_("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _("Printing"), wxOK);
499 else
500 (*m_PrintData) = printer.GetPrintDialogData().GetPrintData();
501}
502
503
504
505void wxHtmlEasyPrinting::PrinterSetup()
506{
507 wxPrintDialogData printDialogData(*m_PrintData);
508 wxPrintDialog printerDialog(m_Frame, &printDialogData);
509
510 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
511
512 if (printerDialog.ShowModal() == wxID_OK)
513 (*m_PrintData) = printerDialog.GetPrintDialogData().GetPrintData();
514}
515
516
517
518void wxHtmlEasyPrinting::PageSetup()
519{
520 m_PageSetupData -> SetPrintData(*m_PrintData);
521 wxPageSetupDialog pageSetupDialog(m_Frame, m_PageSetupData);
522
523 if (pageSetupDialog.ShowModal() == wxID_OK) {
524 (*m_PrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
525 (*m_PageSetupData) = pageSetupDialog.GetPageSetupData();
526 }
527}
528
529
530
531void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg)
532{
533 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
534 m_Headers[0] = header;
535 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
536 m_Headers[1] = header;
537}
538
539
540
541void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg)
542{
543 if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN)
544 m_Footers[0] = footer;
545 if (pg == wxPAGE_ALL || pg == wxPAGE_ODD)
546 m_Footers[1] = footer;
547}
548
549
550
551wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout()
552{
553 wxHtmlPrintout *p = new wxHtmlPrintout(m_Name);
554
555 p -> SetHeader(m_Headers[0], wxPAGE_EVEN);
556 p -> SetHeader(m_Headers[1], wxPAGE_ODD);
557 p -> SetFooter(m_Footers[0], wxPAGE_EVEN);
558 p -> SetFooter(m_Footers[1], wxPAGE_ODD);
559
560 p -> SetMargins(m_PageSetupData -> GetMarginTopLeft().y,
561 m_PageSetupData -> GetMarginBottomRight().y,
562 m_PageSetupData -> GetMarginTopLeft().x,
563 m_PageSetupData -> GetMarginBottomRight().x);
564
565 return p;
566}
567
568
569
570#endif // wxUSE_PRINTING_ARCHITECTURE