Search code examples
c++wxwidgets

Migration from wxHTML to wxWebView in wxWidgets


I am trying to replace wxHTML with wxWebView in wxWidgets. I have a problem when trying to get the URL of a clicked hyperlink. Previously the code was like this:

void mywxHtmlWindow::OnLinkClicked( const wxHtmlLinkInfo& link )
{
    ...
    link.GetHref();
    ...
}

and now I have:

void OnLinkClicked( wxWebViewEvent& evt )
   {
     ...
     evt.GetURL();
     ...
   }

The previous result was something like "myURL" and the current result is something like "about:myURL" and I don't know why there is "about:" at the beginning. What should I do to get the same result as before?

Update:

I have set the page source in both cases like below:

mBrowser->SetPage( currentPage_, wxString() );

and all tags in the page source are something like this:

<a href="myURL">clickHere</a>

and I want to get myURL string when I click on the link.

Update 2:

myURL could be any arbitrary string like a hash

(e.g. myURL="git-c2b617b1ec4bbaba2201020ec946b037f5935b77").

It's not a usual URL like "http://www.google.com".


Solution

  • The default scheme for Internet Explorer is about when you are setting a page source manually. So instead of using setPage function, I registered a custom scheme (e.g myScheme) like

      mBrowser->RegisterHandler( wxSharedPtr<wxWebViewHandler>( new WxHtmlFSHandler( "myScheme" ) ) );
    

    load an arbitrary URL like

    myBrowser->LoadURL( "myScheme:URL.com" );
    

    now you have to override the wxWebViewHandler class. Something like below:

    struct WxHtmlFSHandler : public wxWebViewHandler
    {
      WxHtmlFSHandler( const wxString& scheme ): wxWebViewHandler( scheme )
      { }
    
      wxFSFile* GetFile( const wxString& uri ) override
      {
        return ( new wxFSFile( new wxMemoryInputStream( currentPage_.data(), 
        currentPage_.size() ),
        uri, wxT( "text/html" ), currentAnchor_
        #if wxUSE_DATETIME
        , wxDateTime::Now()
        #endif 
        ) );
    }
    };