htmprint.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: html/htmprint.h
  3. // Purpose: interface of wxHtmlDCRenderer
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxHtmlDCRenderer
  9. This class can render HTML document into a specified area of a DC.
  10. You can use it in your own printing code, although use of wxHtmlEasyPrinting
  11. or wxHtmlPrintout is strongly recommended.
  12. @library{wxhtml}
  13. @category{html}
  14. */
  15. class wxHtmlDCRenderer : public wxObject
  16. {
  17. public:
  18. /**
  19. Constructor.
  20. */
  21. wxHtmlDCRenderer();
  22. /**
  23. Returns the width of the HTML text in pixels.
  24. This can be compared with the width parameter of SetSize() to check if
  25. the document being printed fits into the page boundary.
  26. @see GetTotalHeight()
  27. @since 2.9.0
  28. */
  29. int GetTotalWidth() const;
  30. /**
  31. Returns the height of the HTML text in pixels.
  32. This is important if area height (see wxHtmlDCRenderer::SetSize) is
  33. smaller that total height and thus the page cannot fit into it. In that
  34. case you're supposed to call Render() as long as its return value is
  35. smaller than GetTotalHeight()'s.
  36. @see GetTotalWidth()
  37. */
  38. int GetTotalHeight() const;
  39. /**
  40. Renders HTML text to the DC.
  41. @param x,y
  42. position of upper-left corner of printing rectangle (see SetSize()).
  43. @param known_pagebreaks
  44. @todo docme
  45. @param from
  46. y-coordinate of the very first visible cell.
  47. @param dont_render
  48. if @true then this method only returns y coordinate of the next page
  49. and does not output anything.
  50. @param to
  51. y-coordinate of the last visible cell.
  52. Returned value is y coordinate of first cell than didn't fit onto page.
  53. Use this value as from in next call to Render() in order to print
  54. multipages document.
  55. @note
  56. The following three methods @b must always be called before any call to
  57. Render(), in this order:
  58. - SetDC()
  59. - SetSize()
  60. - SetHtmlText()
  61. @note Render() changes the DC's user scale and does NOT restore it.
  62. */
  63. int Render(int x, int y, wxArrayInt& known_pagebreaks, int from = 0,
  64. int dont_render = false, int to = INT_MAX);
  65. /**
  66. Assign DC instance to the renderer.
  67. @a pixel_scale can be used when rendering to high-resolution DCs (e.g. printer)
  68. to adjust size of pixel metrics.
  69. (Many dimensions in HTML are given in pixels -- e.g. image sizes. 300x300
  70. image would be only one inch wide on typical printer. With pixel_scale = 3.0
  71. it would be 3 inches.)
  72. */
  73. void SetDC(wxDC* dc, double pixel_scale = 1.0);
  74. /**
  75. This function sets font sizes and faces.
  76. @param normal_face
  77. This is face name for normal (i.e. non-fixed) font.
  78. It can be either empty string (then the default face is chosen) or
  79. platform-specific face name. Examples are "helvetica" under Unix or
  80. "Times New Roman" under Windows.
  81. @param fixed_face
  82. The same thing for fixed face ( \<TT\>..\</TT\> )
  83. @param sizes
  84. This is an array of 7 items of int type.
  85. The values represent size of font with HTML size from -2 to +4
  86. ( \<FONT SIZE=-2\> to \<FONT SIZE=+4\> ).
  87. Default sizes are used if sizes is @NULL.
  88. Default font sizes are defined by constants wxHTML_FONT_SIZE_1,
  89. wxHTML_FONT_SIZE_2, ..., wxHTML_FONT_SIZE_7.
  90. Note that they differ among platforms. Default face names are empty strings.
  91. @see SetSize()
  92. */
  93. void SetFonts(const wxString& normal_face, const wxString& fixed_face,
  94. const int* sizes = NULL);
  95. /**
  96. Sets font sizes to be relative to the given size or the system
  97. default size; use either specified or default font
  98. @param size
  99. Point size of the default HTML text
  100. @param normal_face
  101. This is face name for normal (i.e. non-fixed) font. It can be
  102. either empty string (then the default face is chosen) or
  103. platform-specific face name. Examples are "helvetica" under
  104. Unix or "Times New Roman" under Windows.
  105. @param fixed_face
  106. The same thing for fixed face ( \<TT\>..\</TT\> )
  107. @see SetSize()
  108. */
  109. void SetStandardFonts(int size = -1,
  110. const wxString& normal_face = wxEmptyString,
  111. const wxString& fixed_face = wxEmptyString);
  112. /**
  113. Assign text to the renderer. Render() then draws the text onto DC.
  114. @param html
  115. HTML text. This is not a filename.
  116. @param basepath
  117. base directory (html string would be stored there if it was in file).
  118. It is used to determine path for loading images, for example.
  119. @param isdir
  120. @false if basepath is filename, @true if it is directory name
  121. (see wxFileSystem for detailed explanation).
  122. */
  123. void SetHtmlText(const wxString& html,
  124. const wxString& basepath = wxEmptyString,
  125. bool isdir = true);
  126. /**
  127. Set size of output rectangle, in pixels. Note that you @b can't change
  128. width of the rectangle between calls to Render() !
  129. (You can freely change height.)
  130. */
  131. void SetSize(int width, int height);
  132. };
  133. /**
  134. @class wxHtmlEasyPrinting
  135. This class provides very simple interface to printing architecture.
  136. It allows you to print HTML documents using only a few commands.
  137. @note
  138. Do not create this class on the stack only. You should create an instance
  139. on app startup and use this instance for all printing operations.
  140. The reason is that this class stores various settings in it.
  141. @library{wxhtml}
  142. @category{html,printing}
  143. */
  144. class wxHtmlEasyPrinting : public wxObject
  145. {
  146. public:
  147. /**
  148. Constructor.
  149. @param name
  150. Name of the printing object. Used by preview frames and setup dialogs.
  151. @param parentWindow
  152. pointer to the window that will own the preview frame and setup dialogs.
  153. May be @NULL.
  154. */
  155. wxHtmlEasyPrinting(const wxString& name = "Printing",
  156. wxWindow* parentWindow = NULL);
  157. /**
  158. Returns the current name being used for preview frames and setup
  159. dialogs.
  160. @since 2.8.11 / 2.9.1
  161. */
  162. const wxString& GetName() const;
  163. /**
  164. Returns a pointer to wxPageSetupDialogData instance used by this class.
  165. You can set its parameters (via SetXXXX methods).
  166. */
  167. wxPageSetupDialogData* GetPageSetupData();
  168. /**
  169. Gets the parent window for dialogs.
  170. */
  171. wxWindow* GetParentWindow() const;
  172. /**
  173. Returns pointer to wxPrintData instance used by this class.
  174. You can set its parameters (via SetXXXX methods).
  175. */
  176. wxPrintData* GetPrintData();
  177. /**
  178. Display page setup dialog and allows the user to modify settings.
  179. */
  180. void PageSetup();
  181. /**
  182. Preview HTML file.
  183. Returns @false in case of error -- call wxPrinter::GetLastError to get detailed
  184. information about the kind of the error.
  185. */
  186. bool PreviewFile(const wxString& htmlfile);
  187. /**
  188. Preview HTML text (not file!).
  189. Returns @false in case of error -- call wxPrinter::GetLastError to get detailed
  190. information about the kind of the error.
  191. @param htmltext
  192. HTML text.
  193. @param basepath
  194. base directory (html string would be stored there if it was in file).
  195. It is used to determine path for loading images, for example.
  196. */
  197. bool PreviewText(const wxString& htmltext,
  198. const wxString& basepath = wxEmptyString);
  199. /**
  200. Print HTML file.
  201. Returns @false in case of error -- call wxPrinter::GetLastError to get detailed
  202. information about the kind of the error.
  203. */
  204. bool PrintFile(const wxString& htmlfile);
  205. /**
  206. Print HTML text (not file!).
  207. Returns @false in case of error -- call wxPrinter::GetLastError to get detailed
  208. information about the kind of the error.
  209. @param htmltext
  210. HTML text.
  211. @param basepath
  212. base directory (html string would be stored there if it was in file).
  213. It is used to determine path for loading images, for example.
  214. */
  215. bool PrintText(const wxString& htmltext,
  216. const wxString& basepath = wxEmptyString);
  217. /**
  218. Sets fonts. See wxHtmlDCRenderer::SetFonts for detailed description.
  219. */
  220. void SetFonts(const wxString& normal_face, const wxString& fixed_face,
  221. const int* sizes = NULL);
  222. /**
  223. Sets the name used for preview frames and setup dialogs.
  224. @since 2.8.11 / 2.9.1
  225. */
  226. void SetName(const wxString& name);
  227. /**
  228. Sets default font sizes and/or default font size.
  229. See wxHtmlDCRenderer::SetStandardFonts for detailed description.
  230. @see SetFonts()
  231. */
  232. void SetStandardFonts(int size = -1,
  233. const wxString& normal_face = wxEmptyString,
  234. const wxString& fixed_face = wxEmptyString);
  235. /**
  236. Set page footer. The following macros can be used inside it:
  237. @@DATE@ is replaced by the current date in default format
  238. @@PAGENUM@ is replaced by page number
  239. @@PAGESCNT@ is replaced by total number of pages
  240. @@TIME@ is replaced by the current time in default format
  241. @@TITLE@ is replaced with the title of the document
  242. @param footer
  243. HTML text to be used as footer.
  244. @param pg
  245. one of wxPAGE_ODD, wxPAGE_EVEN and wxPAGE_ALL constants.
  246. */
  247. void SetFooter(const wxString& footer, int pg = wxPAGE_ALL);
  248. /**
  249. Set page header. The following macros can be used inside it:
  250. - @@DATE@ is replaced by the current date in default format
  251. - @@PAGENUM@ is replaced by page number
  252. - @@PAGESCNT@ is replaced by total number of pages
  253. - @@TIME@ is replaced by the current time in default format
  254. - @@TITLE@ is replaced with the title of the document
  255. @param header
  256. HTML text to be used as header.
  257. @param pg
  258. one of wxPAGE_ODD, wxPAGE_EVEN and wxPAGE_ALL constants.
  259. */
  260. void SetHeader(const wxString& header, int pg = wxPAGE_ALL);
  261. /**
  262. Sets the parent window for dialogs.
  263. */
  264. void SetParentWindow(wxWindow* window);
  265. private:
  266. /**
  267. Check whether the document fits into the page area.
  268. This function is called by the base class OnPreparePrinting()
  269. implementation and by default checks whether the document fits into
  270. @a pageArea horizontally and warns the user if it does not, giving him
  271. the possibility to cancel printing in this case (presumably in order to
  272. change some layout options and retry it again).
  273. You may override it to either suppress this check if truncation of the
  274. HTML being printed is acceptable or, on the contrary, add more checks to
  275. it, e.g. for the fit in the vertical direction if the document should
  276. always appear on a single page.
  277. @return
  278. @true if wxHtmlPrintout should continue or @false to cancel
  279. printing.
  280. @since 2.9.0
  281. */
  282. virtual bool CheckFit(const wxSize& pageArea, const wxSize& docArea) const;
  283. };
  284. enum {
  285. wxPAGE_ODD,
  286. wxPAGE_EVEN,
  287. wxPAGE_ALL
  288. };
  289. /**
  290. @class wxHtmlPrintout
  291. This class serves as printout class for HTML documents.
  292. @library{wxhtml}
  293. @category{html,printing}
  294. */
  295. class wxHtmlPrintout : public wxPrintout
  296. {
  297. public:
  298. /**
  299. Constructor.
  300. */
  301. wxHtmlPrintout(const wxString& title = "Printout");
  302. /**
  303. Adds a filter to the static list of filters for wxHtmlPrintout.
  304. See wxHtmlFilter for further information.
  305. */
  306. static void AddFilter(wxHtmlFilter* filter);
  307. /**
  308. This function sets font sizes and faces. See wxHtmlWindow::SetFonts
  309. for detailed description.
  310. */
  311. void SetFonts(const wxString& normal_face, const wxString& fixed_face,
  312. const int* sizes = NULL);
  313. /**
  314. Set page footer. The following macros can be used inside it:
  315. - @@DATE@ is replaced by the current date in default format
  316. - @@PAGENUM@ is replaced by page number
  317. - @@PAGESCNT@ is replaced by total number of pages
  318. - @@TIME@ is replaced by the current time in default format
  319. - @@TITLE@ is replaced with the title of the document
  320. @param footer
  321. HTML text to be used as footer.
  322. @param pg
  323. one of wxPAGE_ODD, wxPAGE_EVEN and wxPAGE_ALL constants.
  324. */
  325. void SetFooter(const wxString& footer, int pg = wxPAGE_ALL);
  326. /**
  327. Set page header. The following macros can be used inside it:
  328. - @@DATE@ is replaced by the current date in default format
  329. - @@PAGENUM@ is replaced by page number
  330. - @@PAGESCNT@ is replaced by total number of pages
  331. - @@TIME@ is replaced by the current time in default format
  332. - @@TITLE@ is replaced with the title of the document
  333. @param header
  334. HTML text to be used as header.
  335. @param pg
  336. one of wxPAGE_ODD, wxPAGE_EVEN and wxPAGE_ALL constants.
  337. */
  338. void SetHeader(const wxString& header, int pg = wxPAGE_ALL);
  339. /**
  340. Prepare the class for printing this HTML @b file.
  341. The file may be located on any virtual file system or it may be normal file.
  342. */
  343. void SetHtmlFile(const wxString& htmlfile);
  344. /**
  345. Prepare the class for printing this HTML text.
  346. @param html
  347. HTML text. (NOT file!)
  348. @param basepath
  349. base directory (html string would be stored there if it was in file).
  350. It is used to determine path for loading images, for example.
  351. @param isdir
  352. @false if basepath is filename, @true if it is directory name
  353. (see wxFileSystem for detailed explanation).
  354. */
  355. void SetHtmlText(const wxString& html,
  356. const wxString& basepath = wxEmptyString,
  357. bool isdir = true);
  358. /**
  359. Sets margins in millimeters.
  360. Defaults to 1 inch for margins and 0.5cm for space between text and header
  361. and/or footer.
  362. */
  363. void SetMargins(float top = 25.2, float bottom = 25.2,
  364. float left = 25.2,
  365. float right = 25.2,
  366. float spaces = 5);
  367. };