htmlwin.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: html/htmlwin.h
  3. // Purpose: interface of wxHtmlWindow
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. // wxHtmlWindow flags:
  8. #define wxHW_SCROLLBAR_NEVER 0x0002
  9. #define wxHW_SCROLLBAR_AUTO 0x0004
  10. #define wxHW_NO_SELECTION 0x0008
  11. #define wxHW_DEFAULT_STYLE wxHW_SCROLLBAR_AUTO
  12. /// Enum for wxHtmlWindow::OnOpeningURL and wxHtmlWindowInterface::OnOpeningURL
  13. enum wxHtmlOpeningStatus
  14. {
  15. /// Open the requested URL
  16. wxHTML_OPEN,
  17. /// Do not open the URL
  18. wxHTML_BLOCK,
  19. /// Redirect to another URL (returned from OnOpeningURL)
  20. wxHTML_REDIRECT
  21. };
  22. /**
  23. @class wxHtmlWindowInterface
  24. Abstract interface to a HTML rendering window (such as wxHtmlWindow or
  25. wxHtmlListBox) that is passed to wxHtmlWinParser. It encapsulates all
  26. communication from the parser to the window.
  27. */
  28. class wxHtmlWindowInterface
  29. {
  30. public:
  31. /// Ctor
  32. wxHtmlWindowInterface();
  33. virtual ~wxHtmlWindowInterface();
  34. /**
  35. Called by the parser to set window's title to given text.
  36. */
  37. virtual void SetHTMLWindowTitle(const wxString& title) = 0;
  38. /**
  39. Called when a link is clicked.
  40. @param link information about the clicked link
  41. */
  42. virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo& link) = 0;
  43. /**
  44. Called when the parser needs to open another URL (e.g. an image).
  45. @param type Type of the URL request (e.g. image)
  46. @param url URL the parser wants to open
  47. @param redirect If the return value is wxHTML_REDIRECT, then the
  48. URL to redirect to will be stored in this variable
  49. (the pointer must never be NULL)
  50. @return indicator of how to treat the request
  51. */
  52. virtual wxHtmlOpeningStatus OnHTMLOpeningURL(wxHtmlURLType type,
  53. const wxString& url,
  54. wxString *redirect) const = 0;
  55. /**
  56. Converts coordinates @a pos relative to given @a cell to
  57. physical coordinates in the window.
  58. */
  59. virtual wxPoint HTMLCoordsToWindow(wxHtmlCell *cell,
  60. const wxPoint& pos) const = 0;
  61. /// Returns the window used for rendering (may be NULL).
  62. virtual wxWindow* GetHTMLWindow() = 0;
  63. /// Returns background colour to use by default.
  64. virtual wxColour GetHTMLBackgroundColour() const = 0;
  65. /// Sets window's background to colour @a clr.
  66. virtual void SetHTMLBackgroundColour(const wxColour& clr) = 0;
  67. /// Sets window's background to given bitmap.
  68. virtual void SetHTMLBackgroundImage(const wxBitmap& bmpBg) = 0;
  69. /// Sets status bar text.
  70. virtual void SetHTMLStatusText(const wxString& text) = 0;
  71. /// Type of mouse cursor
  72. enum HTMLCursor
  73. {
  74. /// Standard mouse cursor (typically an arrow)
  75. HTMLCursor_Default,
  76. /// Cursor shown over links
  77. HTMLCursor_Link,
  78. /// Cursor shown over selectable text
  79. HTMLCursor_Text
  80. };
  81. /**
  82. Returns mouse cursor of given @a type.
  83. */
  84. virtual wxCursor GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor type) const = 0;
  85. };
  86. /**
  87. @class wxHtmlWindow
  88. wxHtmlWindow is probably the only class you will directly use unless you want
  89. to do something special (like adding new tag handlers or MIME filters).
  90. The purpose of this class is to display rich content pages (either local file or
  91. downloaded via HTTP protocol) in a window based on a subset of the HTML standard.
  92. The width of the window is constant - given in the constructor - and virtual height
  93. is changed dynamically depending on page size.
  94. Once the window is created you can set its content by calling SetPage() with raw HTML,
  95. LoadPage() with a wxFileSystem location or LoadFile() with a filename.
  96. @note
  97. If you want complete HTML/CSS support as well as a Javascript engine, see instead
  98. wxWebView.
  99. @note
  100. wxHtmlWindow uses the wxImage class for displaying images, as such you need to
  101. initialize the handlers for any image formats you use before loading a page.
  102. See ::wxInitAllImageHandlers and wxImage::AddHandler.
  103. @beginStyleTable
  104. @style{wxHW_SCROLLBAR_NEVER}
  105. Never display scrollbars, not even when the page is larger than the
  106. window.
  107. @style{wxHW_SCROLLBAR_AUTO}
  108. Display scrollbars only if page's size exceeds window's size.
  109. @style{wxHW_NO_SELECTION}
  110. Don't allow the user to select text.
  111. @endStyleTable
  112. @beginEventEmissionTable{wxHtmlCellEvent, wxHtmlLinkEvent}
  113. @event{EVT_HTML_CELL_CLICKED(id, func)}
  114. A wxHtmlCell was clicked.
  115. @event{EVT_HTML_CELL_HOVER(id, func)}
  116. The mouse passed over a wxHtmlCell.
  117. @event{EVT_HTML_LINK_CLICKED(id, func)}
  118. A wxHtmlCell which contains an hyperlink was clicked.
  119. @endEventTable
  120. @library{wxhtml}
  121. @category{html}
  122. @see wxHtmlLinkEvent, wxHtmlCellEvent
  123. */
  124. class wxHtmlWindow : public wxScrolledWindow, public wxHtmlWindowInterface
  125. {
  126. public:
  127. /**
  128. Default ctor.
  129. */
  130. wxHtmlWindow();
  131. /**
  132. Constructor.
  133. The parameters are the same as wxScrolled::wxScrolled() constructor.
  134. */
  135. wxHtmlWindow(wxWindow *parent, wxWindowID id = wxID_ANY,
  136. const wxPoint& pos = wxDefaultPosition,
  137. const wxSize& size = wxDefaultSize,
  138. long style = wxHW_DEFAULT_STYLE,
  139. const wxString& name = "htmlWindow");
  140. /**
  141. Adds @ref overview_html_filters "input filter" to the static list of available
  142. filters. These filters are present by default:
  143. - @c text/html MIME type
  144. - @c image/* MIME types
  145. - Plain Text filter (this filter is used if no other filter matches)
  146. */
  147. static void AddFilter(wxHtmlFilter* filter);
  148. /**
  149. Appends HTML fragment to currently displayed text and refreshes the window.
  150. @param source
  151. HTML code fragment
  152. @return @false if an error occurred, @true otherwise.
  153. */
  154. bool AppendToPage(const wxString& source);
  155. /**
  156. Returns pointer to the top-level container.
  157. @see @ref overview_html_cells, @ref overview_printing
  158. */
  159. wxHtmlContainerCell* GetInternalRepresentation() const;
  160. /**
  161. Returns anchor within currently opened page (see wxHtmlWindow::GetOpenedPage).
  162. If no page is opened or if the displayed page wasn't produced by call to
  163. LoadPage(), empty string is returned.
  164. */
  165. wxString GetOpenedAnchor() const;
  166. /**
  167. Returns full location of the opened page.
  168. If no page is opened or if the displayed page wasn't produced by call to
  169. LoadPage(), empty string is returned.
  170. */
  171. wxString GetOpenedPage() const;
  172. /**
  173. Returns title of the opened page or wxEmptyString if the current page does not
  174. contain \<TITLE\> tag.
  175. */
  176. wxString GetOpenedPageTitle() const;
  177. /**
  178. Returns the related frame.
  179. */
  180. wxFrame* GetRelatedFrame() const;
  181. /**
  182. Moves back to the previous page. Only pages displayed using LoadPage()
  183. are stored in history list.
  184. */
  185. bool HistoryBack();
  186. /**
  187. Returns @true if it is possible to go back in the history
  188. i.e. HistoryBack() won't fail.
  189. */
  190. bool HistoryCanBack();
  191. /**
  192. Returns @true if it is possible to go forward in the history
  193. i.e. HistoryForward() won't fail.
  194. */
  195. bool HistoryCanForward();
  196. /**
  197. Clears history.
  198. */
  199. void HistoryClear();
  200. /**
  201. Moves to next page in history. Only pages displayed using LoadPage()
  202. are stored in history list.
  203. */
  204. bool HistoryForward();
  205. /**
  206. Loads an HTML page from a file and displays it.
  207. @return @false if an error occurred, @true otherwise
  208. @see LoadPage()
  209. */
  210. bool LoadFile(const wxFileName& filename);
  211. /**
  212. Unlike SetPage() this function first loads the HTML page from @a location
  213. and then displays it.
  214. @param location
  215. The address of the document.
  216. See the @ref overview_fs for details on the address format
  217. and wxFileSystem for a description of how the file is opened.
  218. @return @false if an error occurred, @true otherwise
  219. @see LoadFile()
  220. */
  221. virtual bool LoadPage(const wxString& location);
  222. /**
  223. Called when user clicks on hypertext link.
  224. Default behaviour is to emit a wxHtmlLinkEvent and, if the event was not
  225. processed or skipped, call LoadPage() and do nothing else.
  226. Overloading this method is deprecated; intercept the event instead.
  227. Also see wxHtmlLinkInfo.
  228. */
  229. virtual void OnLinkClicked(const wxHtmlLinkInfo& link);
  230. /**
  231. Called when an URL is being opened (either when the user clicks on a link or
  232. an image is loaded). The URL will be opened only if OnOpeningURL() returns
  233. @c wxHTML_OPEN. This method is called by wxHtmlParser::OpenURL.
  234. You can override OnOpeningURL() to selectively block some URLs
  235. (e.g. for security reasons) or to redirect them elsewhere.
  236. Default behaviour is to always return @c wxHTML_OPEN.
  237. @param type
  238. Indicates type of the resource. Is one of
  239. - wxHTML_URL_PAGE: Opening a HTML page.
  240. - wxHTML_URL_IMAGE: Opening an image.
  241. - wxHTML_URL_OTHER: Opening a resource that doesn't fall into
  242. any other category.
  243. @param url
  244. URL being opened.
  245. @param redirect
  246. Pointer to wxString variable that must be filled with an
  247. URL if OnOpeningURL() returns @c wxHTML_REDIRECT.
  248. The return value is:
  249. - wxHTML_OPEN: Open the URL.
  250. - wxHTML_BLOCK: Deny access to the URL, wxHtmlParser::OpenURL will return @NULL.
  251. - wxHTML_REDIRECT: Don't open url, redirect to another URL.
  252. OnOpeningURL() must fill *redirect with the new URL.
  253. OnOpeningURL() will be called again on returned URL.
  254. */
  255. virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType type,
  256. const wxString& url,
  257. wxString* redirect) const;
  258. /**
  259. Called on parsing \<TITLE\> tag.
  260. */
  261. virtual void OnSetTitle(const wxString& title);
  262. /**
  263. This reads custom settings from wxConfig. It uses the path 'path'
  264. if given, otherwise it saves info into currently selected path.
  265. The values are stored in sub-path @c wxHtmlWindow.
  266. Read values: all things set by SetFonts(), SetBorders().
  267. @param cfg
  268. wxConfig from which you want to read the configuration.
  269. @param path
  270. Optional path in config tree. If not given current path is used.
  271. */
  272. virtual void ReadCustomization(wxConfigBase* cfg,
  273. wxString path = wxEmptyString);
  274. /**
  275. Selects all text in the window.
  276. @see SelectLine(), SelectWord()
  277. */
  278. void SelectAll();
  279. /**
  280. Selects the line of text that @a pos points at. Note that @e pos
  281. is relative to the top of displayed page, not to window's origin, use
  282. wxScrolled::CalcUnscrolledPosition()
  283. to convert physical coordinate.
  284. @see SelectAll(), SelectWord()
  285. */
  286. void SelectLine(const wxPoint& pos);
  287. /**
  288. Selects the word at position @a pos.
  289. Note that @a pos is relative to the top of displayed page, not to window's
  290. origin, use wxScrolled::CalcUnscrolledPosition() to convert physical coordinate.
  291. @see SelectAll(), SelectLine()
  292. */
  293. void SelectWord(const wxPoint& pos);
  294. /**
  295. Returns the current selection as plain text.
  296. Returns an empty string if no text is currently selected.
  297. */
  298. wxString SelectionToText();
  299. /**
  300. This function sets the space between border of window and HTML contents.
  301. See image:
  302. @image html htmlwin_border.png
  303. @param b
  304. indentation from borders in pixels
  305. */
  306. void SetBorders(int b);
  307. /**
  308. This function sets font sizes and faces. See wxHtmlDCRenderer::SetFonts
  309. for detailed description.
  310. @see SetSize()
  311. */
  312. void SetFonts(const wxString& normal_face, const wxString& fixed_face,
  313. const int* sizes = NULL);
  314. /**
  315. Sets default font sizes and/or default font size.
  316. See wxHtmlDCRenderer::SetStandardFonts for detailed description.
  317. @see SetFonts()
  318. */
  319. void SetStandardFonts(int size = -1,
  320. const wxString& normal_face = wxEmptyString,
  321. const wxString& fixed_face = wxEmptyString);
  322. /**
  323. Sets the source of a page and displays it, for example:
  324. @code
  325. htmlwin -> SetPage("<html><body>Hello, world!</body></html>");
  326. @endcode
  327. If you want to load a document from some location use LoadPage() instead.
  328. @param source
  329. The HTML to be displayed.
  330. @return @false if an error occurred, @true otherwise.
  331. */
  332. virtual bool SetPage(const wxString& source);
  333. /**
  334. Sets the frame in which page title will be displayed.
  335. @a format is the format of the frame title, e.g. "HtmlHelp : %s".
  336. It must contain exactly one %s.
  337. This %s is substituted with HTML page title.
  338. */
  339. void SetRelatedFrame(wxFrame* frame, const wxString& format);
  340. /**
  341. @b After calling SetRelatedFrame(), this sets statusbar slot where messages
  342. will be displayed. (Default is -1 = no messages.)
  343. @param index
  344. Statusbar slot number (0..n)
  345. */
  346. void SetRelatedStatusBar(int index);
  347. /**
  348. @b Sets the associated statusbar where messages will be displayed.
  349. Call this instead of SetRelatedFrame() if you want statusbar updates only,
  350. no changing of the frame title.
  351. @param statusbar
  352. Statusbar pointer
  353. @param index
  354. Statusbar slot number (0..n)
  355. @since 2.9.0
  356. */
  357. void SetRelatedStatusBar(wxStatusBar* statusbar, int index = 0);
  358. /**
  359. Returns content of currently displayed page as plain text.
  360. */
  361. wxString ToText();
  362. /**
  363. Saves custom settings into wxConfig.
  364. It uses the path 'path' if given, otherwise it saves info into currently
  365. selected path.
  366. Regardless of whether the path is given or not, the function creates
  367. sub-path @c wxHtmlWindow.
  368. Saved values: all things set by SetFonts(), SetBorders().
  369. @param cfg
  370. wxConfig to which you want to save the configuration.
  371. @param path
  372. Optional path in config tree. If not given, the current path is used.
  373. */
  374. virtual void WriteCustomization(wxConfigBase* cfg,
  375. wxString path = wxEmptyString);
  376. protected:
  377. /**
  378. This method is called when a mouse button is clicked inside wxHtmlWindow.
  379. The default behaviour is to emit a wxHtmlCellEvent and, if the event was
  380. not processed or skipped, call OnLinkClicked() if the cell contains an
  381. hypertext link.
  382. Overloading this method is deprecated; intercept the event instead.
  383. @param cell
  384. The cell inside which the mouse was clicked, always a simple
  385. (i.e. non-container) cell
  386. @param x
  387. The logical x coordinate of the click point
  388. @param y
  389. The logical y coordinate of the click point
  390. @param event
  391. The mouse event containing other information about the click
  392. @return @true if a link was clicked, @false otherwise.
  393. */
  394. virtual bool OnCellClicked(wxHtmlCell* cell, wxCoord x, wxCoord y,
  395. const wxMouseEvent& event);
  396. /**
  397. This method is called when a mouse moves over an HTML cell.
  398. Default behaviour is to emit a wxHtmlCellEvent.
  399. Overloading this method is deprecated; intercept the event instead.
  400. @param cell
  401. The cell inside which the mouse is currently, always a simple
  402. (i.e. non-container) cell
  403. @param x
  404. The logical x coordinate of the click point
  405. @param y
  406. The logical y coordinate of the click point
  407. */
  408. virtual void OnCellMouseHover(wxHtmlCell* cell, wxCoord x, wxCoord y);
  409. };
  410. wxEventType wxEVT_HTML_CELL_CLICKED;
  411. wxEventType wxEVT_HTML_CELL_HOVER;
  412. wxEventType wxEVT_HTML_LINK_CLICKED;
  413. /**
  414. @class wxHtmlLinkEvent
  415. This event class is used for the events generated by wxHtmlWindow.
  416. @beginEventTable{wxHtmlLinkEvent}
  417. @event{EVT_HTML_LINK_CLICKED(id, func)}
  418. User clicked on an hyperlink.
  419. @endEventTable
  420. @library{wxhtml}
  421. @category{html}
  422. */
  423. class wxHtmlLinkEvent : public wxCommandEvent
  424. {
  425. public:
  426. /**
  427. The constructor is not normally used by the user code.
  428. */
  429. wxHtmlLinkEvent(int id, const wxHtmlLinkInfo& linkinfo);
  430. /**
  431. Returns the wxHtmlLinkInfo which contains info about the cell clicked
  432. and the hyperlink it contains.
  433. */
  434. const wxHtmlLinkInfo& GetLinkInfo() const;
  435. };
  436. /**
  437. @class wxHtmlCellEvent
  438. This event class is used for the events generated by wxHtmlWindow.
  439. @beginEventTable{wxHtmlCellEvent}
  440. @event{EVT_HTML_CELL_HOVER(id, func)}
  441. User moved the mouse over a wxHtmlCell.
  442. @event{EVT_HTML_CELL_CLICKED(id, func)}
  443. User clicked on a wxHtmlCell. When handling this event, remember to use
  444. wxHtmlCell::SetLinkClicked(true) if the cell contains a link.
  445. @endEventTable
  446. @library{wxhtml}
  447. @category{html}
  448. */
  449. class wxHtmlCellEvent : public wxCommandEvent
  450. {
  451. public:
  452. /**
  453. The constructor is not normally used by the user code.
  454. */
  455. wxHtmlCellEvent(wxEventType commandType, int id,
  456. wxHtmlCell* cell,
  457. const wxPoint& point,
  458. const wxMouseEvent& ev);
  459. /**
  460. Returns the wxHtmlCellEvent associated with the event.
  461. */
  462. wxHtmlCell* GetCell() const;
  463. /**
  464. Returns @true if SetLinkClicked(@true) has previously been called;
  465. @false otherwise.
  466. */
  467. bool GetLinkClicked() const;
  468. /**
  469. Returns the wxPoint associated with the event.
  470. */
  471. wxPoint GetPoint() const;
  472. /**
  473. Call this function with @a linkclicked set to @true if the cell which has
  474. been clicked contained a link or @false otherwise (which is the default).
  475. With this function the event handler can return info to the wxHtmlWindow
  476. which sent the event.
  477. */
  478. void SetLinkClicked(bool linkclicked);
  479. };