htmprint.h 12 KB

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