1 /////////////////////////////////////////////////////////////////////////////
2 // Name: html/htmlwin.h
3 // Purpose: interface of wxHtmlWindow
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 #define wxHW_SCROLLBAR_NEVER 0x0002
11 #define wxHW_SCROLLBAR_AUTO 0x0004
12 #define wxHW_NO_SELECTION 0x0008
14 #define wxHW_DEFAULT_STYLE wxHW_SCROLLBAR_AUTO
17 /// Enum for wxHtmlWindow::OnOpeningURL and wxHtmlWindowInterface::OnOpeningURL
18 enum wxHtmlOpeningStatus
20 /// Open the requested URL
22 /// Do not open the URL
24 /// Redirect to another URL (returned from OnOpeningURL)
30 @class wxHtmlWindowInterface
32 Abstract interface to a HTML rendering window (such as wxHtmlWindow or
33 wxHtmlListBox) that is passed to wxHtmlWinParser. It encapsulates all
34 communication from the parser to the window.
36 class wxHtmlWindowInterface
40 wxHtmlWindowInterface();
41 virtual ~wxHtmlWindowInterface();
44 Called by the parser to set window's title to given text.
46 virtual void SetHTMLWindowTitle(const wxString
& title
) = 0;
49 Called when a link is clicked.
51 @param link information about the clicked link
53 virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo
& link
) = 0;
56 Called when the parser needs to open another URL (e.g. an image).
58 @param type Type of the URL request (e.g. image)
59 @param url URL the parser wants to open
60 @param redirect If the return value is wxHTML_REDIRECT, then the
61 URL to redirect to will be stored in this variable
62 (the pointer must never be NULL)
64 @return indicator of how to treat the request
66 virtual wxHtmlOpeningStatus
OnHTMLOpeningURL(wxHtmlURLType type
,
68 wxString
*redirect
) const = 0;
71 Converts coordinates @a pos relative to given @a cell to
72 physical coordinates in the window.
74 virtual wxPoint
HTMLCoordsToWindow(wxHtmlCell
*cell
,
75 const wxPoint
& pos
) const = 0;
77 /// Returns the window used for rendering (may be NULL).
78 virtual wxWindow
* GetHTMLWindow() = 0;
80 /// Returns background colour to use by default.
81 virtual wxColour
GetHTMLBackgroundColour() const = 0;
83 /// Sets window's background to colour @a clr.
84 virtual void SetHTMLBackgroundColour(const wxColour
& clr
) = 0;
86 /// Sets window's background to given bitmap.
87 virtual void SetHTMLBackgroundImage(const wxBitmap
& bmpBg
) = 0;
89 /// Sets status bar text.
90 virtual void SetHTMLStatusText(const wxString
& text
) = 0;
92 /// Type of mouse cursor
95 /// Standard mouse cursor (typically an arrow)
97 /// Cursor shown over links
99 /// Cursor shown over selectable text
104 Returns mouse cursor of given @a type.
106 virtual wxCursor
GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor type
) const = 0;
114 wxHtmlWindow is probably the only class you will directly use unless you want
115 to do something special (like adding new tag handlers or MIME filters).
117 The purpose of this class is to display rich content pages (either local file or
118 downloaded via HTTP protocol) in a window based on a subset of the HTML standard.
119 The width of the window is constant - given in the constructor - and virtual height
120 is changed dynamically depending on page size.
121 Once the window is created you can set its content by calling SetPage() with raw HTML,
122 LoadPage() with a wxFileSystem location or LoadFile() with a filename.
125 If you want complete HTML/CSS support as well as a Javascript engine, see instead
129 wxHtmlWindow uses the wxImage class for displaying images, as such you need to
130 initialize the handlers for any image formats you use before loading a page.
131 See ::wxInitAllImageHandlers and wxImage::AddHandler.
134 @style{wxHW_SCROLLBAR_NEVER}
135 Never display scrollbars, not even when the page is larger than the
137 @style{wxHW_SCROLLBAR_AUTO}
138 Display scrollbars only if page's size exceeds window's size.
139 @style{wxHW_NO_SELECTION}
140 Don't allow the user to select text.
144 @beginEventEmissionTable{wxHtmlCellEvent, wxHtmlLinkEvent}
145 @event{EVT_HTML_CELL_CLICKED(id, func)}
146 A wxHtmlCell was clicked.
147 @event{EVT_HTML_CELL_HOVER(id, func)}
148 The mouse passed over a wxHtmlCell.
149 @event{EVT_HTML_LINK_CLICKED(id, func)}
150 A wxHtmlCell which contains an hyperlink was clicked.
156 @see wxHtmlLinkEvent, wxHtmlCellEvent
158 class wxHtmlWindow
: public wxScrolledWindow
, public wxHtmlWindowInterface
168 The parameters are the same as wxScrolled::wxScrolled() constructor.
170 wxHtmlWindow(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
171 const wxPoint
& pos
= wxDefaultPosition
,
172 const wxSize
& size
= wxDefaultSize
,
173 long style
= wxHW_DEFAULT_STYLE
,
174 const wxString
& name
= "htmlWindow");
177 Adds @ref overview_html_filters "input filter" to the static list of available
178 filters. These filters are present by default:
179 - @c text/html MIME type
180 - @c image/* MIME types
181 - Plain Text filter (this filter is used if no other filter matches)
183 static void AddFilter(wxHtmlFilter
* filter
);
186 Appends HTML fragment to currently displayed text and refreshes the window.
191 @return @false if an error occurred, @true otherwise.
193 bool AppendToPage(const wxString
& source
);
196 Returns pointer to the top-level container.
198 @see @ref overview_html_cells, @ref overview_printing
200 wxHtmlContainerCell
* GetInternalRepresentation() const;
203 Returns anchor within currently opened page (see wxHtmlWindow::GetOpenedPage).
204 If no page is opened or if the displayed page wasn't produced by call to
205 LoadPage(), empty string is returned.
207 wxString
GetOpenedAnchor() const;
210 Returns full location of the opened page.
211 If no page is opened or if the displayed page wasn't produced by call to
212 LoadPage(), empty string is returned.
214 wxString
GetOpenedPage() const;
217 Returns title of the opened page or wxEmptyString if the current page does not
218 contain \<TITLE\> tag.
220 wxString
GetOpenedPageTitle() const;
223 Returns the related frame.
225 wxFrame
* GetRelatedFrame() const;
228 Moves back to the previous page. Only pages displayed using LoadPage()
229 are stored in history list.
234 Returns @true if it is possible to go back in the history
235 i.e. HistoryBack() won't fail.
237 bool HistoryCanBack();
240 Returns @true if it is possible to go forward in the history
241 i.e. HistoryForward() won't fail.
243 bool HistoryCanForward();
251 Moves to next page in history. Only pages displayed using LoadPage()
252 are stored in history list.
254 bool HistoryForward();
257 Loads an HTML page from a file and displays it.
259 @return @false if an error occurred, @true otherwise
263 bool LoadFile(const wxFileName
& filename
);
266 Unlike SetPage() this function first loads the HTML page from @a location
267 and then displays it.
270 The address of the document.
271 See the @ref overview_fs for details on the address format
272 and wxFileSystem for a description of how the file is opened.
274 @return @false if an error occurred, @true otherwise
278 virtual bool LoadPage(const wxString
& location
);
281 Called when user clicks on hypertext link.
282 Default behaviour is to emit a wxHtmlLinkEvent and, if the event was not
283 processed or skipped, call LoadPage() and do nothing else.
285 Overloading this method is deprecated; intercept the event instead.
287 Also see wxHtmlLinkInfo.
289 virtual void OnLinkClicked(const wxHtmlLinkInfo
& link
);
292 Called when an URL is being opened (either when the user clicks on a link or
293 an image is loaded). The URL will be opened only if OnOpeningURL() returns
294 @c wxHTML_OPEN. This method is called by wxHtmlParser::OpenURL.
296 You can override OnOpeningURL() to selectively block some URLs
297 (e.g. for security reasons) or to redirect them elsewhere.
298 Default behaviour is to always return @c wxHTML_OPEN.
301 Indicates type of the resource. Is one of
302 - wxHTML_URL_PAGE: Opening a HTML page.
303 - wxHTML_URL_IMAGE: Opening an image.
304 - wxHTML_URL_OTHER: Opening a resource that doesn't fall into
309 Pointer to wxString variable that must be filled with an
310 URL if OnOpeningURL() returns @c wxHTML_REDIRECT.
313 - wxHTML_OPEN: Open the URL.
314 - wxHTML_BLOCK: Deny access to the URL, wxHtmlParser::OpenURL will return @NULL.
315 - wxHTML_REDIRECT: Don't open url, redirect to another URL.
316 OnOpeningURL() must fill *redirect with the new URL.
317 OnOpeningURL() will be called again on returned URL.
319 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType type
,
321 wxString
* redirect
) const;
324 Called on parsing \<TITLE\> tag.
326 virtual void OnSetTitle(const wxString
& title
);
329 This reads custom settings from wxConfig. It uses the path 'path'
330 if given, otherwise it saves info into currently selected path.
331 The values are stored in sub-path @c wxHtmlWindow.
332 Read values: all things set by SetFonts(), SetBorders().
335 wxConfig from which you want to read the configuration.
337 Optional path in config tree. If not given current path is used.
339 virtual void ReadCustomization(wxConfigBase
* cfg
,
340 wxString path
= wxEmptyString
);
343 Selects all text in the window.
345 @see SelectLine(), SelectWord()
350 Selects the line of text that @a pos points at. Note that @e pos
351 is relative to the top of displayed page, not to window's origin, use
352 wxScrolled::CalcUnscrolledPosition()
353 to convert physical coordinate.
355 @see SelectAll(), SelectWord()
357 void SelectLine(const wxPoint
& pos
);
360 Selects the word at position @a pos.
361 Note that @a pos is relative to the top of displayed page, not to window's
362 origin, use wxScrolled::CalcUnscrolledPosition() to convert physical coordinate.
364 @see SelectAll(), SelectLine()
366 void SelectWord(const wxPoint
& pos
);
369 Returns the current selection as plain text.
370 Returns an empty string if no text is currently selected.
372 wxString
SelectionToText();
375 This function sets the space between border of window and HTML contents.
377 @image html htmlwin_border.png
380 indentation from borders in pixels
382 void SetBorders(int b
);
385 This function sets font sizes and faces. See wxHtmlDCRenderer::SetFonts
386 for detailed description.
390 void SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
391 const int* sizes
= NULL
);
394 Sets default font sizes and/or default font size.
395 See wxHtmlDCRenderer::SetStandardFonts for detailed description.
398 void SetStandardFonts(int size
= -1,
399 const wxString
& normal_face
= wxEmptyString
,
400 const wxString
& fixed_face
= wxEmptyString
);
403 Sets the source of a page and displays it, for example:
405 htmlwin -> SetPage("<html><body>Hello, world!</body></html>");
408 If you want to load a document from some location use LoadPage() instead.
411 The HTML to be displayed.
413 @return @false if an error occurred, @true otherwise.
415 virtual bool SetPage(const wxString
& source
);
418 Sets the frame in which page title will be displayed.
419 @a format is the format of the frame title, e.g. "HtmlHelp : %s".
420 It must contain exactly one %s.
421 This %s is substituted with HTML page title.
423 void SetRelatedFrame(wxFrame
* frame
, const wxString
& format
);
426 @b After calling SetRelatedFrame(), this sets statusbar slot where messages
427 will be displayed. (Default is -1 = no messages.)
430 Statusbar slot number (0..n)
432 void SetRelatedStatusBar(int index
);
435 @b Sets the associated statusbar where messages will be displayed.
436 Call this instead of SetRelatedFrame() if you want statusbar updates only,
437 no changing of the frame title.
442 Statusbar slot number (0..n)
446 void SetRelatedStatusBar(wxStatusBar
* statusbar
, int index
= 0);
449 Returns content of currently displayed page as plain text.
454 Saves custom settings into wxConfig.
455 It uses the path 'path' if given, otherwise it saves info into currently
457 Regardless of whether the path is given or not, the function creates
458 sub-path @c wxHtmlWindow.
460 Saved values: all things set by SetFonts(), SetBorders().
463 wxConfig to which you want to save the configuration.
465 Optional path in config tree. If not given, the current path is used.
467 virtual void WriteCustomization(wxConfigBase
* cfg
,
468 wxString path
= wxEmptyString
);
473 This method is called when a mouse button is clicked inside wxHtmlWindow.
474 The default behaviour is to emit a wxHtmlCellEvent and, if the event was
475 not processed or skipped, call OnLinkClicked() if the cell contains an
478 Overloading this method is deprecated; intercept the event instead.
481 The cell inside which the mouse was clicked, always a simple
482 (i.e. non-container) cell
484 The logical x coordinate of the click point
486 The logical y coordinate of the click point
488 The mouse event containing other information about the click
490 @return @true if a link was clicked, @false otherwise.
492 virtual bool OnCellClicked(wxHtmlCell
* cell
, wxCoord x
, wxCoord y
,
493 const wxMouseEvent
& event
);
496 This method is called when a mouse moves over an HTML cell.
497 Default behaviour is to emit a wxHtmlCellEvent.
499 Overloading this method is deprecated; intercept the event instead.
502 The cell inside which the mouse is currently, always a simple
503 (i.e. non-container) cell
505 The logical x coordinate of the click point
507 The logical y coordinate of the click point
509 virtual void OnCellMouseHover(wxHtmlCell
* cell
, wxCoord x
, wxCoord y
);
514 wxEventType wxEVT_HTML_CELL_CLICKED
;
515 wxEventType wxEVT_HTML_CELL_HOVER
;
516 wxEventType wxEVT_HTML_LINK_CLICKED
;
520 @class wxHtmlLinkEvent
522 This event class is used for the events generated by wxHtmlWindow.
524 @beginEventTable{wxHtmlLinkEvent}
525 @event{EVT_HTML_LINK_CLICKED(id, func)}
526 User clicked on an hyperlink.
532 class wxHtmlLinkEvent
: public wxCommandEvent
536 The constructor is not normally used by the user code.
538 wxHtmlLinkEvent(int id
, const wxHtmlLinkInfo
& linkinfo
);
541 Returns the wxHtmlLinkInfo which contains info about the cell clicked
542 and the hyperlink it contains.
544 const wxHtmlLinkInfo
& GetLinkInfo() const;
550 @class wxHtmlCellEvent
552 This event class is used for the events generated by wxHtmlWindow.
554 @beginEventTable{wxHtmlCellEvent}
555 @event{EVT_HTML_CELL_HOVER(id, func)}
556 User moved the mouse over a wxHtmlCell.
557 @event{EVT_HTML_CELL_CLICKED(id, func)}
558 User clicked on a wxHtmlCell. When handling this event, remember to use
559 wxHtmlCell::SetLinkClicked(true) if the cell contains a link.
565 class wxHtmlCellEvent
: public wxCommandEvent
569 The constructor is not normally used by the user code.
571 wxHtmlCellEvent(wxEventType commandType
, int id
,
573 const wxPoint
& point
,
574 const wxMouseEvent
& ev
);
577 Returns the wxHtmlCellEvent associated with the event.
579 wxHtmlCell
* GetCell() const;
582 Returns @true if SetLinkClicked(@true) has previously been called;
585 bool GetLinkClicked() const;
588 Returns the wxPoint associated with the event.
590 wxPoint
GetPoint() const;
593 Call this function with @a linkclicked set to @true if the cell which has
594 been clicked contained a link or @false otherwise (which is the default).
596 With this function the event handler can return info to the wxHtmlWindow
597 which sent the event.
599 void SetLinkClicked(bool linkclicked
);