use UiGeneric for WebView

4 weeks ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 19 Feb 2025 22:44:49 +0100 (4 weeks ago)
changeset 468
1f251c462f74
parent 467
08e0a66d3bd6
child 469
3f6142271f58

use UiGeneric for WebView

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/webview.c file | annotate | diff | comparison | revisions
ui/gtk/webview.h file | annotate | diff | comparison | revisions
ui/ui/webview.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon Feb 17 22:17:21 2025 +0100
+++ b/application/main.c	Wed Feb 19 22:44:49 2025 +0100
@@ -34,6 +34,8 @@
 #include <cx/buffer.h>
 #include <cx/utils.h>
 
+#include "cx/string.h"
+
 #if !defined(UI_COCOA) && !defined(UI_MOTIF)
 
 typedef struct {
@@ -51,6 +53,7 @@
     UiList *srclist1;
     UiList *srclist2;
     UiList *items;
+    UiGeneric *web;
 } MyDocument;
 
 MyDocument *doc1;
@@ -185,6 +188,8 @@
     ui_list_append(doc->items, "Item 2");
     ui_list_append(doc->items, "Item 3");
     
+    doc->web = ui_generic_new(docctx, NULL);
+    
     //doc->text = ui_text_new(docctx, "text");
     return doc;
 }
@@ -480,7 +485,9 @@
         }
         ui_tab(obj, "Tab 9") {
 #ifdef UI_WEBVIEW
-          ui_webview(obj, .fill = UI_ON);
+          ui_webview(obj, .fill = UI_ON, .value = doc->web);
+          cxstring html = CX_STR("<html><body><h1>Hello Toolkit</h1><p>Toolkit WebView</p></body></html>");
+          ui_webview_load_content(doc->web, "mypage", html.ptr, html.length, NULL, NULL);
 #else
           ui_label(obj, "Webview is not supported");
 #endif
--- a/ui/gtk/webview.c	Mon Feb 17 22:17:21 2025 +0100
+++ b/ui/gtk/webview.c	Wed Feb 19 22:44:49 2025 +0100
@@ -30,6 +30,8 @@
 #include "container.h"
 #include "../ui/webview.h"
 
+#include "webview.h"
+
 #ifdef UI_WEBVIEW
 
 #include <webkit/webkit.h>
@@ -38,10 +40,26 @@
     UiObject* current = uic_current_obj(obj);
     
     GtkWidget *webview = webkit_web_view_new();
-    webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), "https://code.unixwork.de");
+    //webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), "https://code.unixwork.de");
     
     ui_set_name_and_style(webview, args.name, args.style_class);
     
+    UiVar *var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_GENERIC);
+    if(var) {
+        WebViewData *data = malloc(sizeof(WebViewData));
+        memset(data, 0, sizeof(WebViewData));
+        data->widget = webview;
+        
+        UiGeneric *value = var->value;
+        value->get = ui_webview_get;
+        value->get_type = ui_webview_get_type;
+        value->set = ui_webview_set;
+        value->obj = data;
+        if(value->value && value->type && !strcmp(value->type, UI_WEBVIEW_OBJECT_TYPE)) {
+            
+        }
+    }
+    
     ui_set_widget_groups(obj->ctx, webview, args.groups);
     UI_APPLY_LAYOUT1(current, args);
     current->container->add(current->container, webview, FALSE);
@@ -49,5 +67,61 @@
     return webview;
 }
 
+void* ui_webview_get(UiGeneric *g) {
+    return g->value;
+}
+
+const char* ui_webview_get_type(UiGeneric *g) {
+    return UI_WEBVIEW_OBJECT_TYPE;
+}
+
+int ui_webview_set(UiGeneric *g, void *value, const char *type) {
+    if(!type || strcmp(type, UI_WEBVIEW_OBJECT_TYPE)) {
+        return 1;
+    }
+    
+    WebViewData *obj = g->obj;
+    if(!obj->widget) {
+        return 1;
+    }
+    
+    WebViewData *data = value;
+    if(data->type == WEBVIEW_CONTENT_URL) {
+        webkit_web_view_load_uri(WEBKIT_WEB_VIEW(obj->widget), data->uri);
+    } else {
+        if(!data->content) {
+            return 1;
+        }
+        
+        GBytes *bytes = g_bytes_new(data->content, data->contentlength);
+        webkit_web_view_load_bytes(WEBKIT_WEB_VIEW(obj->widget), bytes, data->mimetype, data->encoding, data->uri);
+    }
+    
+    return 0;
+}
+
+void ui_webview_load_url(UiGeneric *g, const char *url) {
+    WebViewData data = { .uri = (char*)url, .type = WEBVIEW_CONTENT_URL };
+    g->set(g, &data, UI_WEBVIEW_OBJECT_TYPE);
+}
+
+void ui_webview_load_content(
+        UiGeneric *g,
+        const char *uri,
+        const char *content,
+        size_t contentlength,
+        const char *mimetype,
+        const char *encoding)
+{
+    WebViewData data;
+    data.uri = (char*)uri;
+    data.content = (char*)content;
+    data.contentlength = contentlength;
+    data.mimetype = (char*)mimetype;
+    data.encoding = (char*)encoding;
+    data.type = WEBVIEW_CONTENT_CONTENT;
+    g->set(g, &data, UI_WEBVIEW_OBJECT_TYPE);
+}
+
 
 #endif
--- a/ui/gtk/webview.h	Mon Feb 17 22:17:21 2025 +0100
+++ b/ui/gtk/webview.h	Wed Feb 19 22:44:49 2025 +0100
@@ -31,8 +31,25 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
+    
+enum WebViewDataType {
+    WEBVIEW_CONTENT_URL,
+    WEBVIEW_CONTENT_CONTENT
+};
+    
+typedef struct WebViewData {
+    GtkWidget *widget;
+    char *uri;
+    char *mimetype;
+    char *encoding;
+    char *content;
+    size_t contentlength;
+    enum WebViewDataType type;
+} WebViewData;
 
-
+void* ui_webview_get(UiGeneric *g);
+const char* ui_webview_get_type(UiGeneric *g);
+int ui_webview_set(UiGeneric *g, void *value, const char *type);
 
 
 #ifdef __cplusplus
--- a/ui/ui/webview.h	Mon Feb 17 22:17:21 2025 +0100
+++ b/ui/ui/webview.h	Wed Feb 19 22:44:49 2025 +0100
@@ -36,6 +36,8 @@
 extern "C" {
 #endif
     
+#define UI_WEBVIEW_OBJECT_TYPE "webview"
+    
 typedef struct UiWebviewArgs {
     UiTri fill;
     UiBool hexpand;
@@ -48,6 +50,9 @@
     const char *name;
     const char *style_class;
     
+    UiGeneric *value;
+    const char *varname;
+    
     const int* groups;
 } UiWebviewArgs;
 
@@ -55,7 +60,15 @@
 
 UIWIDGET ui_webview_create(UiObject *obj, UiWebviewArgs args);
 
+void ui_webview_load_url(UiGeneric *g, const char *url);
 
+void ui_webview_load_content(
+        UiGeneric *g,
+        const char *uri,
+        const char *content,
+        size_t contentlength,
+        const char *mimetype,
+        const char *encoding);
 
 
 #ifdef __cplusplus

mercurial