WebView
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.
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/win
folder 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
We will need to install the WebKit2 development libraries on our system so that CMake can enable Web support in NAppGUI.
|
sudo apt-get install libwebkit2gtk-4.1-dev // Ubuntu 24 sudo apt-get install libwebkit2gtk-4.1-dev // Ubuntu 22 sudo apt-get install libwebkit2gtk-4.0-dev // Ubuntu 20 sudo apt-get install libwebkit2gtk-4.0-dev // Ubuntu 18 sudo apt-get install libwebkit2gtk-4.0-dev // Ubuntu 16 sudo apt-get install libwebkit2gtk-3.0-dev // Ubuntu 14 |
WebKit2 support is not available on Ubuntu 12 and earlier.
2. Disable WebView
If the NAppGUI CMake script does not detect the native libraries required by the WebView
control on your system, it will disable it. You can also do this explicitly using this CMake parameter.
|
cmake -DNAPPGUI_WEB=NO -S . -B build |
By disabling 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).
Disabling 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. |