1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: html printing classes
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_HTMPRINT_H_
12 #define _WX_HTMPRINT_H_
16 #if wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE
18 #include "wx/html/htmlcell.h"
19 #include "wx/html/winpars.h"
20 #include "wx/html/htmlfilt.h"
23 #include "wx/printdlg.h"
25 #include <limits.h> // INT_MAX
27 //--------------------------------------------------------------------------------
29 // This class is capable of rendering HTML into specified
31 //--------------------------------------------------------------------------------
33 class WXDLLIMPEXP_HTML wxHtmlDCRenderer
: public wxObject
37 virtual ~wxHtmlDCRenderer();
39 // Following 3 methods *must* be called before any call to Render:
41 // Assign DC to this render
42 void SetDC(wxDC
*dc
, double pixel_scale
= 1.0)
43 { SetDC(dc
, pixel_scale
, pixel_scale
); }
44 void SetDC(wxDC
*dc
, double pixel_scale
, double font_scale
);
46 // Sets size of output rectangle, in pixels. Note that you *can't* change
47 // width of the rectangle between calls to Render! (You can freely change height.)
48 void SetSize(int width
, int height
);
50 // Sets the text to be displayed.
51 // Basepath is base directory (html string would be stored there if it was in
52 // file). It is used to determine path for loading images, for example.
53 // isdir is false if basepath is filename, true if it is directory name
54 // (see wxFileSystem for detailed explanation)
55 void SetHtmlText(const wxString
& html
, const wxString
& basepath
= wxEmptyString
, bool isdir
= true);
57 // Sets fonts to be used when displaying HTML page. (if size null then default sizes used).
58 void SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
= NULL
);
60 // Sets font sizes to be relative to the given size or the system
61 // default size; use either specified or default font
62 void SetStandardFonts(int size
= -1,
63 const wxString
& normal_face
= wxEmptyString
,
64 const wxString
& fixed_face
= wxEmptyString
);
66 // [x,y] is position of upper-left corner of printing rectangle (see SetSize)
67 // from is y-coordinate of the very first visible cell
68 // to is y-coordinate of the next following page break, if any
69 // Returned value is y coordinate of first cell than didn't fit onto page.
70 // Use this value as 'from' in next call to Render in order to print multiple pages
72 // If dont_render is TRUE then nothing is rendered into DC and it only counts
73 // pixels and return y coord of the next page
75 // known_pagebreaks and number_of_pages are used only when counting pages;
76 // otherwise, their default values should be used. Their purpose is to
77 // support pagebreaks using a subset of CSS2's <DIV>. The <DIV> handler
78 // needs to know what pagebreaks have already been set so that it doesn't
79 // set the same pagebreak twice.
81 // CAUTION! Render() changes DC's user scale and does NOT restore it!
82 int Render(int x
, int y
, wxArrayInt
& known_pagebreaks
, int from
= 0,
83 int dont_render
= false, int to
= INT_MAX
);
85 // returns total width of the html document
86 int GetTotalWidth() const;
88 // returns total height of the html document
89 // (compare Render's return value with this)
90 int GetTotalHeight() const;
94 wxHtmlWinParser
*m_Parser
;
96 wxHtmlContainerCell
*m_Cells
;
97 int m_MaxWidth
, m_Width
, m_Height
;
99 wxDECLARE_NO_COPY_CLASS(wxHtmlDCRenderer
);
114 //--------------------------------------------------------------------------------
116 // This class is derived from standard wxWidgets printout class
117 // and is used to print HTML documents.
118 //--------------------------------------------------------------------------------
121 class WXDLLIMPEXP_HTML wxHtmlPrintout
: public wxPrintout
124 wxHtmlPrintout(const wxString
& title
= wxT("Printout"));
125 virtual ~wxHtmlPrintout();
127 void SetHtmlText(const wxString
& html
, const wxString
&basepath
= wxEmptyString
, bool isdir
= true);
128 // prepares the class for printing this html document.
129 // Must be called before using the class, in fact just after constructor
131 // basepath is base directory (html string would be stored there if it was in
132 // file). It is used to determine path for loading images, for example.
133 // isdir is false if basepath is filename, true if it is directory name
134 // (see wxFileSystem for detailed explanation)
136 void SetHtmlFile(const wxString
&htmlfile
);
137 // same as SetHtmlText except that it takes regular file as the parameter
139 void SetHeader(const wxString
& header
, int pg
= wxPAGE_ALL
);
140 void SetFooter(const wxString
& footer
, int pg
= wxPAGE_ALL
);
141 // sets header/footer for the document. The argument is interpreted as HTML document.
142 // You can use macros in it:
143 // @PAGENUM@ is replaced by page number
144 // @PAGESCNT@ is replaced by total number of pages
146 // pg is one of wxPAGE_ODD, wxPAGE_EVEN and wx_PAGE_ALL constants.
147 // You can set different header/footer for odd and even pages
149 // Sets fonts to be used when displaying HTML page. (if size null then default sizes used).
150 void SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
= NULL
);
152 // Sets font sizes to be relative to the given size or the system
153 // default size; use either specified or default font
154 void SetStandardFonts(int size
= -1,
155 const wxString
& normal_face
= wxEmptyString
,
156 const wxString
& fixed_face
= wxEmptyString
);
158 void SetMargins(float top
= 25.2, float bottom
= 25.2, float left
= 25.2, float right
= 25.2,
160 // sets margins in milimeters. Defaults to 1 inch for margins and 0.5cm for space
161 // between text and header and/or footer
164 bool OnPrintPage(int page
);
165 bool HasPage(int page
);
166 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
167 bool OnBeginDocument(int startPage
, int endPage
);
168 void OnPreparePrinting();
171 static void AddFilter(wxHtmlFilter
*filter
);
174 static void CleanUpStatics();
177 // this function is called by the base class OnPreparePrinting()
178 // implementation and by default checks whether the document fits into
179 // pageArea horizontally and warns the user if it does not, giving him
180 // the possibility to cancel printing in this case
182 // you may override it to either suppress this check if truncation of the
183 // HTML being printed is acceptable or, on the contrary, add more checks to
184 // it, e.g. for the fit in the vertical direction if the document should
185 // always appear on a single page
187 // return true if printing should go ahead or false to cancel it
188 virtual bool CheckFit(const wxSize
& pageArea
, const wxSize
& docArea
) const;
190 void RenderPage(wxDC
*dc
, int page
);
191 // renders one page into dc
192 wxString
TranslateHeader(const wxString
& instr
, int page
);
193 // substitute @PAGENUM@ and @PAGESCNT@ by real values
195 // counts pages and fills m_NumPages and m_PageBreaks
200 //int m_PageBreaks[wxHTML_PRINT_MAX_PAGES];
201 wxArrayInt m_PageBreaks
;
203 wxString m_Document
, m_BasePath
;
204 bool m_BasePathIsDir
;
205 wxString m_Headers
[2], m_Footers
[2];
207 int m_HeaderHeight
, m_FooterHeight
;
208 wxHtmlDCRenderer
*m_Renderer
, *m_RendererHdr
;
209 float m_MarginTop
, m_MarginBottom
, m_MarginLeft
, m_MarginRight
, m_MarginSpace
;
211 // list of HTML filters
212 static wxList m_Filters
;
214 wxDECLARE_NO_COPY_CLASS(wxHtmlPrintout
);
221 //--------------------------------------------------------------------------------
222 // wxHtmlEasyPrinting
223 // This class provides very simple interface to printing
224 // architecture. It allows you to print HTML documents only
225 // with very few commands.
227 // Note : do not create this class on stack only.
228 // You should create an instance on app startup and
229 // use this instance for all printing. Why? The class
230 // stores page&printer settings in it.
231 //--------------------------------------------------------------------------------
233 class WXDLLIMPEXP_HTML wxHtmlEasyPrinting
: public wxObject
236 wxHtmlEasyPrinting(const wxString
& name
= wxT("Printing"), wxWindow
*parentWindow
= NULL
);
237 virtual ~wxHtmlEasyPrinting();
239 bool PreviewFile(const wxString
&htmlfile
);
240 bool PreviewText(const wxString
&htmltext
, const wxString
& basepath
= wxEmptyString
);
241 // Preview file / html-text for printing
242 // (and offers printing)
243 // basepath is base directory for opening subsequent files (e.g. from <img> tag)
245 bool PrintFile(const wxString
&htmlfile
);
246 bool PrintText(const wxString
&htmltext
, const wxString
& basepath
= wxEmptyString
);
247 // Print file / html-text w/o preview
250 // pop up printer or page setup dialog
252 void SetHeader(const wxString
& header
, int pg
= wxPAGE_ALL
);
253 void SetFooter(const wxString
& footer
, int pg
= wxPAGE_ALL
);
254 // sets header/footer for the document. The argument is interpreted as HTML document.
255 // You can use macros in it:
256 // @PAGENUM@ is replaced by page number
257 // @PAGESCNT@ is replaced by total number of pages
259 // pg is one of wxPAGE_ODD, wxPAGE_EVEN and wx_PAGE_ALL constants.
260 // You can set different header/footer for odd and even pages
262 void SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
= 0);
263 // Sets fonts to be used when displaying HTML page. (if size null then default sizes used)
265 // Sets font sizes to be relative to the given size or the system
266 // default size; use either specified or default font
267 void SetStandardFonts(int size
= -1,
268 const wxString
& normal_face
= wxEmptyString
,
269 const wxString
& fixed_face
= wxEmptyString
);
271 wxPrintData
*GetPrintData();
272 wxPageSetupDialogData
*GetPageSetupData() {return m_PageSetupData
;}
273 // return page setting data objects.
274 // (You can set their parameters.)
276 wxWindow
* GetParentWindow() const { return m_ParentWindow
; }
277 // get the parent window
278 void SetParentWindow(wxWindow
* window
) { m_ParentWindow
= window
; }
279 // set the parent window
281 const wxString
& GetName() const { return m_Name
; }
282 // get the printout name
283 void SetName(const wxString
& name
) { m_Name
= name
; }
284 // set the printout name
287 virtual wxHtmlPrintout
*CreatePrintout();
288 virtual bool DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
);
289 virtual bool DoPrint(wxHtmlPrintout
*printout
);
292 wxPrintData
*m_PrintData
;
293 wxPageSetupDialogData
*m_PageSetupData
;
295 int m_FontsSizesArr
[7];
297 wxString m_FontFaceFixed
, m_FontFaceNormal
;
306 wxString m_Headers
[2], m_Footers
[2];
307 wxWindow
*m_ParentWindow
;
309 wxDECLARE_NO_COPY_CLASS(wxHtmlEasyPrinting
);
315 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE
317 #endif // _WX_HTMPRINT_H_