1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        htmprint.h
3 // Purpose:     html printing classes
4 // Author:      Vaclav Slavik
5 // Created:     25/09/99
6 // RCS-ID:      $Id: htmprint.h 62758 2009-12-01 20:21:46Z BP $
7 // Copyright:   (c) Vaclav Slavik
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef _WX_HTMPRINT_H_
12 #define _WX_HTMPRINT_H_
13 
14 #include "wx/defs.h"
15 
16 #if wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE
17 
18 #include "wx/html/htmlcell.h"
19 #include "wx/html/winpars.h"
20 #include "wx/html/htmlfilt.h"
21 
22 #include "wx/print.h"
23 #include "wx/printdlg.h"
24 
25 #include <limits.h> // INT_MAX
26 
27 //--------------------------------------------------------------------------------
28 // wxHtmlDCRenderer
29 //                  This class is capable of rendering HTML into specified
30 //                  portion of DC
31 //--------------------------------------------------------------------------------
32 
33 class WXDLLIMPEXP_HTML wxHtmlDCRenderer : public wxObject
34 {
35 public:
36     wxHtmlDCRenderer();
37     virtual ~wxHtmlDCRenderer();
38 
39     // Following 3 methods *must* be called before any call to Render:
40 
41     // Assign DC to this render
42     void SetDC(wxDC *dc, double pixel_scale = 1.0);
43 
44     // Sets size of output rectangle, in pixels. Note that you *can't* change
45     // width of the rectangle between calls to Render! (You can freely change height.)
46     void SetSize(int width, int height);
47 
48     // Sets the text to be displayed.
49     // Basepath is base directory (html string would be stored there if it was in
50     // file). It is used to determine path for loading images, for example.
51     // isdir is false if basepath is filename, true if it is directory name
52     // (see wxFileSystem for detailed explanation)
53     void SetHtmlText(const wxString& html, const wxString& basepath = wxEmptyString, bool isdir = true);
54 
55     // Sets fonts to be used when displaying HTML page. (if size null then default sizes used).
56     void SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes = NULL);
57 
58     // Sets font sizes to be relative to the given size or the system
59     // default size; use either specified or default font
60     void SetStandardFonts(int size = -1,
61                           const wxString& normal_face = wxEmptyString,
62                           const wxString& fixed_face = wxEmptyString);
63 
64     // [x,y] is position of upper-left corner of printing rectangle (see SetSize)
65     // from is y-coordinate of the very first visible cell
66     // to is y-coordinate of the next following page break, if any
67     // Returned value is y coordinate of first cell than didn't fit onto page.
68     // Use this value as 'from' in next call to Render in order to print multiple pages
69     // document
70     // If dont_render is TRUE then nothing is rendered into DC and it only counts
71     // pixels and return y coord of the next page
72     //
73     // known_pagebreaks and number_of_pages are used only when counting pages;
74     // otherwise, their default values should be used. Their purpose is to
75     // support pagebreaks using a subset of CSS2's <DIV>. The <DIV> handler
76     // needs to know what pagebreaks have already been set so that it doesn't
77     // set the same pagebreak twice.
78     //
79     // CAUTION! Render() changes DC's user scale and does NOT restore it!
80     int Render(int x, int y, wxArrayInt& known_pagebreaks, int from = 0,
81                int dont_render = FALSE, int to = INT_MAX);
82 
83     // returns total height of the html document
84     // (compare Render's return value with this)
85     int GetTotalHeight();
86 
87 private:
88     wxDC *m_DC;
89     wxHtmlWinParser *m_Parser;
90     wxFileSystem *m_FS;
91     wxHtmlContainerCell *m_Cells;
92     int m_MaxWidth, m_Width, m_Height;
93 
94     DECLARE_NO_COPY_CLASS(wxHtmlDCRenderer)
95 };
96 
97 
98 
99 
100 
101 enum {
102     wxPAGE_ODD,
103     wxPAGE_EVEN,
104     wxPAGE_ALL
105 };
106 
107 
108 
109 //--------------------------------------------------------------------------------
110 // wxHtmlPrintout
111 //                  This class is derived from standard wxWidgets printout class
112 //                  and is used to print HTML documents.
113 //--------------------------------------------------------------------------------
114 
115 
116 class WXDLLIMPEXP_HTML wxHtmlPrintout : public wxPrintout
117 {
118 public:
119     wxHtmlPrintout(const wxString& title = wxT("Printout"));
120     virtual ~wxHtmlPrintout();
121 
122     void SetHtmlText(const wxString& html, const wxString &basepath = wxEmptyString, bool isdir = true);
123             // prepares the class for printing this html document.
124             // Must be called before using the class, in fact just after constructor
125             //
126             // basepath is base directory (html string would be stored there if it was in
127             // file). It is used to determine path for loading images, for example.
128             // isdir is false if basepath is filename, true if it is directory name
129             // (see wxFileSystem for detailed explanation)
130 
131     void SetHtmlFile(const wxString &htmlfile);
132             // same as SetHtmlText except that it takes regular file as the parameter
133 
134     void SetHeader(const wxString& header, int pg = wxPAGE_ALL);
135     void SetFooter(const wxString& footer, int pg = wxPAGE_ALL);
136             // sets header/footer for the document. The argument is interpreted as HTML document.
137             // You can use macros in it:
138             //   @PAGENUM@ is replaced by page number
139             //   @PAGESCNT@ is replaced by total number of pages
140             //
141             // pg is one of wxPAGE_ODD, wxPAGE_EVEN and wx_PAGE_ALL constants.
142             // You can set different header/footer for odd and even pages
143 
144     // Sets fonts to be used when displaying HTML page. (if size null then default sizes used).
145     void SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes = NULL);
146 
147     // Sets font sizes to be relative to the given size or the system
148     // default size; use either specified or default font
149     void SetStandardFonts(int size = -1,
150                           const wxString& normal_face = wxEmptyString,
151                           const wxString& fixed_face = wxEmptyString);
152 
153     void SetMargins(float top = 25.2, float bottom = 25.2, float left = 25.2, float right = 25.2,
154                     float spaces = 5);
155             // sets margins in milimeters. Defaults to 1 inch for margins and 0.5cm for space
156             // between text and header and/or footer
157 
158     // wxPrintout stuff:
159     bool OnPrintPage(int page);
160     bool HasPage(int page);
161     void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
162     bool OnBeginDocument(int startPage, int endPage);
163     void OnPreparePrinting();
164 
165     // Adds input filter
166     static void AddFilter(wxHtmlFilter *filter);
167 
168     // Cleanup
169     static void CleanUpStatics();
170 
171 private:
172 
173     void RenderPage(wxDC *dc, int page);
174             // renders one page into dc
175     wxString TranslateHeader(const wxString& instr, int page);
176             // substitute @PAGENUM@ and @PAGESCNT@ by real values
177     void CountPages();
178             // counts pages and fills m_NumPages and m_PageBreaks
179 
180 
181 private:
182     int m_NumPages;
183     //int m_PageBreaks[wxHTML_PRINT_MAX_PAGES];
184     wxArrayInt m_PageBreaks;
185 
186     wxString m_Document, m_BasePath;
187     bool m_BasePathIsDir;
188     wxString m_Headers[2], m_Footers[2];
189 
190     int m_HeaderHeight, m_FooterHeight;
191     wxHtmlDCRenderer *m_Renderer, *m_RendererHdr;
192     float m_MarginTop, m_MarginBottom, m_MarginLeft, m_MarginRight, m_MarginSpace;
193 
194     // list of HTML filters
195     static wxList m_Filters;
196 
197     DECLARE_NO_COPY_CLASS(wxHtmlPrintout)
198 };
199 
200 
201 
202 
203 
204 //--------------------------------------------------------------------------------
205 // wxHtmlEasyPrinting
206 //                  This class provides very simple interface to printing
207 //                  architecture. It allows you to print HTML documents only
208 //                  with very few commands.
209 //
210 //                  Note : do not create this class on stack only.
211 //                         You should create an instance on app startup and
212 //                         use this instance for all printing. Why? The class
213 //                         stores page&printer settings in it.
214 //--------------------------------------------------------------------------------
215 
216 class WXDLLIMPEXP_HTML wxHtmlEasyPrinting : public wxObject
217 {
218 public:
219     wxHtmlEasyPrinting(const wxString& name = wxT("Printing"), wxWindow *parentWindow = NULL);
220     virtual ~wxHtmlEasyPrinting();
221 
222     bool PreviewFile(const wxString &htmlfile);
223     bool PreviewText(const wxString &htmltext, const wxString& basepath = wxEmptyString);
224             // Preview file / html-text for printing
225             // (and offers printing)
226             // basepath is base directory for opening subsequent files (e.g. from <img> tag)
227 
228     bool PrintFile(const wxString &htmlfile);
229     bool PrintText(const wxString &htmltext, const wxString& basepath = wxEmptyString);
230             // Print file / html-text w/o preview
231 
232     void PageSetup();
233             // pop up printer or page setup dialog
234 
235     void SetHeader(const wxString& header, int pg = wxPAGE_ALL);
236     void SetFooter(const wxString& footer, int pg = wxPAGE_ALL);
237             // sets header/footer for the document. The argument is interpreted as HTML document.
238             // You can use macros in it:
239             //   @PAGENUM@ is replaced by page number
240             //   @PAGESCNT@ is replaced by total number of pages
241             //
242             // pg is one of wxPAGE_ODD, wxPAGE_EVEN and wx_PAGE_ALL constants.
243             // You can set different header/footer for odd and even pages
244 
245     void SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes = 0);
246     // Sets fonts to be used when displaying HTML page. (if size null then default sizes used)
247 
248     // Sets font sizes to be relative to the given size or the system
249     // default size; use either specified or default font
250     void SetStandardFonts(int size = -1,
251                           const wxString& normal_face = wxEmptyString,
252                           const wxString& fixed_face = wxEmptyString);
253 
254     wxPrintData *GetPrintData();
GetPageSetupData()255     wxPageSetupDialogData *GetPageSetupData() {return m_PageSetupData;}
256             // return page setting data objects.
257             // (You can set their parameters.)
258 
259 #if wxABI_VERSION >= 20805
GetParentWindow()260     wxWindow* GetParentWindow() const { return m_ParentWindow; }
261             // get the parent window
SetParentWindow(wxWindow * window)262     void SetParentWindow(wxWindow* window) { m_ParentWindow = window; }
263             // set the parent window
264 #endif
265 
266 #if wxABI_VERSION >= 20811
GetName()267     const wxString& GetName() const { return m_Name; }
268             // get the printout name
SetName(const wxString & name)269     void SetName(const wxString& name) { m_Name = name; }
270             // set the printout name
271 #endif
272 
273 protected:
274     virtual wxHtmlPrintout *CreatePrintout();
275     virtual bool DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2);
276     virtual bool DoPrint(wxHtmlPrintout *printout);
277 
278 private:
279     wxPrintData *m_PrintData;
280     wxPageSetupDialogData *m_PageSetupData;
281     wxString m_Name;
282     int m_FontsSizesArr[7];
283     int *m_FontsSizes;
284     wxString m_FontFaceFixed, m_FontFaceNormal;
285 
286     enum FontMode
287     {
288         FontMode_Explicit,
289         FontMode_Standard
290     };
291     FontMode m_fontMode;
292 
293     wxString m_Headers[2], m_Footers[2];
294     wxWindow *m_ParentWindow;
295 
296     DECLARE_NO_COPY_CLASS(wxHtmlEasyPrinting)
297 };
298 
299 
300 
301 
302 #endif  // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE
303 
304 #endif // _WX_HTMPRINT_H_
305 
306