{"id":4627,"date":"2025-01-10T16:00:24","date_gmt":"2025-01-10T16:00:24","guid":{"rendered":"https:\/\/truelogic.org\/wordpress\/?p=4627"},"modified":"2025-01-10T16:00:24","modified_gmt":"2025-01-10T16:00:24","slug":"17-wxwidgets-wxwebview","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2025\/01\/10\/17-wxwidgets-wxwebview\/","title":{"rendered":"17.wxWidgets &#8211; wxWebView"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>The wxWebView component enables a web browser to be embedded into wxWidgets forms and allow us to manipulate the content in the browser. Depending on the OS, wxWebView either uses Edge or Webkit as its underlying browser engine.<\/p>\n\n\n\n<p>In order for the wxWebView component to be available, it should have been enabled during the installation of wxWidgets. Given below is sample code to load https:\/\/www.imdb.com and then inject javascript to do a search for a movie.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<p>browser.h<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n#include &lt;wx\/wx.h&gt;\n#endif \/\/ WX_PRECOMP\n\n#if !wxUSE_WEBVIEW_WEBKIT &amp;&amp; !wxUSE_WEBBIEW_WEBKIT2 &amp;&amp; !wxUSE_WEBVIEW &amp;&amp; !wxUSE_WEBVIEW_EDGE\n#error &quot;A wxWebView backend is required&quot;\n#endif\n#include &lt;wx\/webview.h&gt;\n\nclass Browser:public wxFrame {\npublic:\n    Browser(const wxString&amp; title);\n    virtual void OnCloseWindow(wxCloseEvent&amp; evt);\n    void OnNavigationComplete(wxWebViewEvent &amp;evt);\n    void OnDocumentLoaded(wxWebViewEvent &amp;evt);\n\nprivate:\n    wxWebView *m_web;\n};\n\n\n<\/pre><\/div>\n\n\n<p>browser.cpp<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;browser.h&quot;\n\nBrowser::Browser(const wxString&amp; title):\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(1000,400)) {\n\n    Bind(wxEVT_CLOSE_WINDOW, &amp;Browser::OnCloseWindow, this);\n    Bind(wxEVT_WEBVIEW_NAVIGATED, &amp;Browser::OnNavigationComplete, this);\n    Bind(wxEVT_WEBVIEW_LOADED, &amp;Browser::OnDocumentLoaded, this);\n\n    wxPanel *pan = new wxPanel(this);\n    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);\n    pan-&gt;SetSizer(vbox);\n\n    m_web = wxWebView::New();\n    m_web-&gt;Create(pan, -1, wxT(&quot;https:\/\/www.imdb.com&quot;), wxDefaultPosition, wxDefaultSize);\n    vbox-&gt;Add(m_web, 1, wxEXPAND | wxALL);\n\n    Center();\n}\n\nvoid Browser::OnCloseWindow(wxCloseEvent &amp;evt) {\n    wxMessageDialog *dlg = new wxMessageDialog(NULL, wxT(&quot;Are you sure?&quot;), wxT(&quot;Alert&quot;),\n                                wxYES_NO | wxICON_QUESTION);\n    int retVal = dlg-&gt;ShowModal();\n    if (retVal == wxNO)\n        evt.Veto();\n    else\n        evt.Skip(TRUE);\n}\n\nvoid Browser::OnNavigationComplete(wxWebViewEvent &amp;evt) {\n\n}\n\nvoid Browser::OnDocumentLoaded(wxWebViewEvent &amp;evt) {\n    m_web-&gt;RunScript(&quot;document.getElementById(\\&quot;suggestion-search\\&quot;).value = \\&quot;The Matrix Revolutions\\&quot;;&quot;);\n    m_web-&gt;RunScript(&quot;document.getElementById(\\&quot;suggestion-search-button\\&quot;).click();&quot;);\n}\n\n<\/pre><\/div>\n\n\n<p>main.h<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n#include &amp;lt;wx\/wx.h&gt;\n#endif \/\/ WX_PRECOMP\n\nclass MainApp:public wxApp {\npublic:\n    virtual bool OnInit();\n};\n\nwxIMPLEMENT_APP(MainApp);\n\n<\/pre><\/div>\n\n\n<p>main.cpp<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;browser.h&quot;\n\nbool MainApp::OnInit() {\n    Browser *browser = new Browser(wxT(&quot;Browser Demo&quot;));\n    browser-&gt;Show(true);\n\n    return TRUE;\n}\n\n<\/pre><\/div>\n\n\n<p>The sample output is shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"543\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/home-940x543.png\" alt=\"\" class=\"wp-image-4632\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/home-940x543.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/home-620x358.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/home-300x173.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/home-768x444.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/home.png 945w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"550\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/search-940x550.png\" alt=\"\" class=\"wp-image-4633\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/search-940x550.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/search-620x363.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/search-300x176.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/search-768x450.png 768w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2025\/01\/search.png 955w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW The wxWebView component enables a web browser to be embedded into wxWidgets forms and allow us to manipulate the content in the browser. Depending <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2025\/01\/10\/17-wxwidgets-wxwebview\/\" title=\"17.wxWidgets &#8211; wxWebView\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":4405,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[365],"tags":[],"class_list":["post-4627","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4627"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4627\/revisions"}],"predecessor-version":[{"id":4634,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4627\/revisions\/4634"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/4405"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}