Calling The Default Browser In WxHtmlWindow

From WxWiki
Revision as of 18:33, 19 October 2018 by Tierra (talk | contribs) (Text replacement - "\<syntaxhighlight title="([^"]+)"\>" to "<syntaxhighlight lang="cpp" title="$1">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The wxHtmlWindow widget is an excellent way to display complex, static HTML-written pages, e.g. in a "about" dialog box. Suppose you have written a wxHtmlWindow-rendered HTML page that includes links to other internal pages (for example saved into a wxMemoryFSHandler object), and external Internet links. The wxHtmlWindow widget should display the internal links when the user clicks on them, but launch the default browser for other external Internet links.

By default, wxHtmlWindow will autonomously attempt to connect to the Internet site and this may not be the preferred behavior. By subclassing wxHtmlWindow, you can "force" the widget to open the browser on URLs prefixed by "http://", for example:


class HtmlWindow: public wxHtmlWindow
{
public:
	HtmlWindow(wxWindow *parent, wxWindowID id = -1,
		const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
		long style = wxHW_SCROLLBAR_AUTO, const wxString& name = _T("htmlWindow"));
	void OnLinkClicked(const wxHtmlLinkInfo& link);
};

HtmlWindow::HtmlWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos,
	const wxSize& size, long style, const wxString& name)
: wxHtmlWindow(parent, id, pos, size, style, name)
{
}

void HtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
{
	if (link.GetHref().StartsWith(_T("http://")))
		wxLaunchDefaultBrowser(link.GetHref());
	else
		wxHtmlWindow::OnLinkClicked(link);
}