1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: html printing classes
4 // Author: Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_HTMPRINT_H_
12 #define _WX_HTMPRINT_H_
14 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
15 #pragma interface "htmprint.h"
20 #if wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE
22 #include "wx/html/htmlcell.h"
23 #include "wx/html/winpars.h"
24 #include "wx/html/htmlfilt.h"
27 #include "wx/printdlg.h"
29 #include <limits.h> // INT_MAX
31 //--------------------------------------------------------------------------------
33 // This class is capable of rendering HTML into specified
35 //--------------------------------------------------------------------------------
37 class WXDLLIMPEXP_HTML wxHtmlDCRenderer
: public wxObject
43 // Following 3 methods *must* be called before any call to Render:
45 // Assign DC to this render
46 void SetDC(wxDC
*dc
, double pixel_scale
= 1.0);
48 // Sets size of output rectangle, in pixels. Note that you *can't* change
49 // width of the rectangle between calls to Render! (You can freely change height.)
50 void SetSize(int width
, int height
);
52 // Sets the text to be displayed.
53 // Basepath is base directory (html string would be stored there if it was in
54 // file). It is used to determine path for loading images, for example.
55 // isdir is false if basepath is filename, true if it is directory name
56 // (see wxFileSystem for detailed explanation)
57 void SetHtmlText(const wxString
& html
, const wxString
& basepath
= wxEmptyString
, bool isdir
= true);
59 // Sets fonts to be used when displaying HTML page. (if size null then default sizes used).
60 void SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
= NULL
);
62 // Sets font sizes to be relative to the given size or the system
63 // default size; use either specified or default font
64 void SetStandardFonts(int size
= -1,
65 const wxString
& normal_face
= wxEmptyString
,
66 const wxString
& fixed_face
= wxEmptyString
);
68 // [x,y] is position of upper-left corner of printing rectangle (see SetSize)
69 // from is y-coordinate of the very first visible cell
70 // to is y-coordinate of the next following page break, if any
71 // Returned value is y coordinate of first cell than didn't fit onto page.
72 // Use this value as 'from' in next call to Render in order to print multiple pages
74 // If dont_render is TRUE then nothing is rendered into DC and it only counts
75 // pixels and return y coord of the next page
77 // known_pagebreaks and number_of_pages are used only when counting pages;
78 // otherwise, their default values should be used. Their purpose is to
79 // support pagebreaks using a subset of CSS2's <DIV>. The <DIV> handler
80 // needs to know what pagebreaks have already been set so that it doesn't
81 // set the same pagebreak twice.
83 // CAUTION! Render() changes DC's user scale and does NOT restore it!
84 int Render(int x
, int y
, int from
= 0, int dont_render
= FALSE
,
85 int maxHeight
= INT_MAX
,
86 int *known_pagebreaks
= NULL
, int number_of_pages
= 0);
88 // returns total height of the html document
89 // (compare Render's return value with this)
94 wxHtmlWinParser
*m_Parser
;
96 wxHtmlContainerCell
*m_Cells
;
97 int m_MaxWidth
, m_Width
, m_Height
;
99 DECLARE_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"));
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(wxString normal_face
, 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();
178 void RenderPage(wxDC
*dc
, int page
);
179 // renders one page into dc
180 wxString
TranslateHeader(const wxString
& instr
, int page
);
181 // substitute @PAGENUM@ and @PAGESCNT@ by real values
183 // counts pages and fills m_NumPages and m_PageBreaks
188 int m_PageBreaks
[wxHTML_PRINT_MAX_PAGES
];
190 wxString m_Document
, m_BasePath
;
191 bool m_BasePathIsDir
;
192 wxString m_Headers
[2], m_Footers
[2];
194 int m_HeaderHeight
, m_FooterHeight
;
195 wxHtmlDCRenderer
*m_Renderer
, *m_RendererHdr
;
196 float m_MarginTop
, m_MarginBottom
, m_MarginLeft
, m_MarginRight
, m_MarginSpace
;
198 // list of HTML filters
199 static wxList m_Filters
;
201 DECLARE_NO_COPY_CLASS(wxHtmlPrintout
)
208 //--------------------------------------------------------------------------------
209 // wxHtmlEasyPrinting
210 // This class provides very simple interface to printing
211 // architecture. It allows you to print HTML documents only
212 // with very few commands.
214 // Note : do not create this class on stack only.
215 // You should create an instance on app startup and
216 // use this instance for all printing. Why? The class
217 // stores page&printer settings in it.
218 //--------------------------------------------------------------------------------
220 class WXDLLIMPEXP_HTML wxHtmlEasyPrinting
: public wxObject
223 wxHtmlEasyPrinting(const wxString
& name
= wxT("Printing"), wxWindow
*parentWindow
= NULL
);
224 ~wxHtmlEasyPrinting();
226 bool PreviewFile(const wxString
&htmlfile
);
227 bool PreviewText(const wxString
&htmltext
, const wxString
& basepath
= wxEmptyString
);
228 // Preview file / html-text for printing
229 // (and offers printing)
230 // basepath is base directory for opening subsequent files (e.g. from <img> tag)
232 bool PrintFile(const wxString
&htmlfile
);
233 bool PrintText(const wxString
&htmltext
, const wxString
& basepath
= wxEmptyString
);
234 // Print file / html-text w/o preview
237 // pop up printer or page setup dialog
239 void SetHeader(const wxString
& header
, int pg
= wxPAGE_ALL
);
240 void SetFooter(const wxString
& footer
, int pg
= wxPAGE_ALL
);
241 // sets header/footer for the document. The argument is interpreted as HTML document.
242 // You can use macros in it:
243 // @PAGENUM@ is replaced by page number
244 // @PAGESCNT@ is replaced by total number of pages
246 // pg is one of wxPAGE_ODD, wxPAGE_EVEN and wx_PAGE_ALL constants.
247 // You can set different header/footer for odd and even pages
249 void SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
= 0);
250 // Sets fonts to be used when displaying HTML page. (if size null then default sizes used)
252 // Sets font sizes to be relative to the given size or the system
253 // default size; use either specified or default font
254 void SetStandardFonts(int size
= -1,
255 const wxString
& normal_face
= wxEmptyString
,
256 const wxString
& fixed_face
= wxEmptyString
);
258 wxPrintData
*GetPrintData();
259 wxPageSetupDialogData
*GetPageSetupData() {return m_PageSetupData
;}
260 // return page setting data objects.
261 // (You can set their parameters.)
264 virtual wxHtmlPrintout
*CreatePrintout();
265 virtual bool DoPreview(wxHtmlPrintout
*printout1
, wxHtmlPrintout
*printout2
);
266 virtual bool DoPrint(wxHtmlPrintout
*printout
);
269 wxPrintData
*m_PrintData
;
270 wxPageSetupDialogData
*m_PageSetupData
;
272 int m_FontsSizesArr
[7];
274 wxString m_FontFaceFixed
, m_FontFaceNormal
;
283 wxString m_Headers
[2], m_Footers
[2];
284 wxWindow
*m_ParentWindow
;
286 DECLARE_NO_COPY_CLASS(wxHtmlEasyPrinting
)
292 #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE
294 #endif // _WX_HTMPRINT_H_