WebView
#Header
#include <gui/webview.h>
#Functions
| WebView* | webview_create (void) |
| void | webview_OnFocus (...) |
| void | webview_size (...) |
| void | webview_navigate (...) |
| void | webview_back (...) |
| void | webview_forward (...) |
A WebView control will allow us to embed Web content in our application. It will behave in the same way as other view controls such as View or TextView in terms of layout or resizing, displaying a fully functional browser in its client area (Figure 1). In Hello WebView! you have an example application.
- Use webview_create to create a webview.
- Use webview_navigate to display the content of a URL.
- Use webview_back to go to the previous page.
- Use webview_forward to go to the next page.
#1. WebView dependencies
WebView depends on native technologies in each operating system: Edge:WebView2 on Windows, WebKit2 on GTK/Linux and WKWebView on macOS (Figure 2). The NAppGUI build system will be in charge of managing the dependencies for us automatically. However, you must take these points into account before using this control in your applications successfully.
#1.1. WebView on Windows
- In the
prj/depend/web/winfolder are the WebView2 headers and static libraries. - They have been obtained from this NuGet package.
- There are problems when compiling in Debug mode from VS2017 and VS2015. However, in Release mode, applications work without problem using these versions of Visual Studio. More information here.
- Visual Studio 2013 and earlier do not support compilation of WebView2.
- MinGW does not support the compilation of WebView2.
- It is recommended to use VS2019 or VS2022 to generate applications with WebView2.
- WebView2 only works on Windows 10 and Windows 11.
#1.2. WebView on macOS
You don't have to do anything special to compile the WebView on macOS. CMake will automatically link to WebKit.framework. At the moment, this support is available for macOS 10.10 Yosemite and later, as WKWebView is not present in previous distributions.
#1.3. WebView on Linux
NAppGUI loads WebKit2 at runtime via dlib_open, when the first WebView is created. It is not necessary to install the WebKit2 development libraries to compile, nor will applications depend on WebKit2 to start. For the control to display Web content, the machine where the application runs must have WebKit2 installed.
sudo apt-get install libwebkit2gtk-4.1-0 // Ubuntu 22 and later sudo apt-get install libwebkit2gtk-4.0-37 // Ubuntu 16, 18, 20 sudo apt-get install libwebkit2gtk-3.0-25 // Ubuntu 14 |
WebKit2 support is not available on Ubuntu 12 and earlier.
#2. WebView without Web support
If there is no Web support, applications that make use of the WebView control will compile and be able to run. The difference is that they will not display Web content in the control, just an empty area (Figure 3). This happens when WebKit2 is not installed on Linux, when compiling with MinGW or Visual Studio 2013 and earlier, or on macOS earlier than 10.10 Yosemite. CMake will report it in the WEB_SUPPORT line of the configuration.
The absence of Web support will not generate compilation or link errors in applications that use the WebView.
#webview_create ()
Create a Web View.
WebView* webview_create(void);
Return
The Web View.
#webview_OnFocus ()
Sets a handler for keyboard focus.
void webview_OnFocus(WebView *view, Listener *listener);
| view | The view. |
| listener | callback function that will be called when keyboard focus is received or lost. |
Remarks
See GUI Events.
#webview_size ()
Sets the default size of the view.
void webview_size(WebView *view, const S2Df size);
| view | The view. |
| size | The size. |
Remarks
Corresponds to the Natural sizing of the control. By default 245x144.
#webview_navigate ()
Loads a URL in the web view.
void webview_navigate(WebView *view, const char_t *url);
| view | The view. |
| url | URL to load, null-terminated UTF8 C string |
#webview_back ()
Go back to the previous page in the browser stack.
void webview_back(WebView *view);
| view | The view. |
#webview_forward ()
Moves to the next page in the browser stack.
void webview_forward(WebView *view);
| view | The view. |


