UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2025 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "toolkit.h" 30 #include "container.h" 31 32 #include "webview.h" 33 #include "widget.h" 34 35 #ifdef UI_WEBVIEW 36 37 UIWIDGET ui_webview_create(UiObject *obj, UiWebviewArgs *args) { 38 GtkWidget *webview = webkit_web_view_new(); 39 40 ui_set_name_and_style(webview, args->name, args->style_class); 41 42 ui_widget_size_request(webview, args->width, args->height); 43 44 UiVar *var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_GENERIC); 45 if(var) { 46 WebViewData *data = malloc(sizeof(WebViewData)); 47 memset(data, 0, sizeof(WebViewData)); 48 data->webview = WEBKIT_WEB_VIEW(webview); 49 WebKitSettings *settings = webkit_web_view_get_settings(data->webview); 50 data->javascript = webkit_settings_get_enable_javascript(settings); 51 data->zoom = webkit_web_view_get_zoom_level(data->webview); 52 53 UiGeneric *value = var->value; 54 value->get = ui_webview_get; 55 value->get_type = ui_webview_get_type; 56 value->set = ui_webview_set; 57 value->obj = data; 58 if(value->value && value->type && !strcmp(value->type, UI_WEBVIEW_OBJECT_TYPE)) { 59 ui_webview_set(value, value->value, UI_WEBVIEW_OBJECT_TYPE); 60 } 61 } 62 63 ui_set_widget_states(obj->ctx, webview, args->states); 64 UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end; 65 UiLayout layout = UI_ARGS2LAYOUT(args); 66 ct->add(ct, webview, &layout); 67 68 return webview; 69 } 70 71 void* ui_webview_get(UiGeneric *g) { 72 return g->value; 73 } 74 75 const char* ui_webview_get_type(UiGeneric *g) { 76 return UI_WEBVIEW_OBJECT_TYPE; 77 } 78 79 int ui_webview_set(UiGeneric *g, void *value, const char *type) { 80 if(!type || strcmp(type, UI_WEBVIEW_OBJECT_TYPE)) { 81 return 1; 82 } 83 84 WebViewData *obj = g->obj; 85 if(!obj->webview) { 86 return 1; 87 } 88 89 WebViewData *data = value; 90 if(data->type == WEBVIEW_CONTENT_URL) { 91 webkit_web_view_load_uri(obj->webview, data->uri); 92 } else { 93 if(!data->content) { 94 return 1; 95 } 96 97 GBytes *bytes = g_bytes_new(data->content, data->contentlength); 98 webkit_web_view_load_bytes(obj->webview, bytes, data->mimetype, data->encoding, data->uri); 99 } 100 101 ui_webview_enable_javascript(g, data->javascript); 102 ui_webview_set_zoom(g, data->zoom); 103 104 return 0; 105 } 106 107 void ui_webview_load_url(UiGeneric *g, const char *url) { 108 WebViewData data = { .uri = (char*)url, .type = WEBVIEW_CONTENT_URL, .javascript = TRUE, .zoom = 1 }; 109 g->set(g, &data, UI_WEBVIEW_OBJECT_TYPE); 110 } 111 112 void ui_webview_load_content( 113 UiGeneric *g, 114 const char *uri, 115 const char *content, 116 size_t contentlength, 117 const char *mimetype, 118 const char *encoding) 119 { 120 WebViewData *data0 = g->obj; 121 if(!data0) { 122 return; 123 } 124 125 WebViewData data; 126 memset(&data, 0, sizeof(WebViewData)); 127 data.webview = data0->webview; 128 data.uri = (char*)uri; 129 data.content = (char*)content; 130 data.contentlength = contentlength; 131 data.mimetype = (char*)mimetype; 132 data.encoding = (char*)encoding; 133 data.type = WEBVIEW_CONTENT_CONTENT; 134 data.javascript = FALSE; 135 data.zoom = 1; 136 g->set(g, &data, UI_WEBVIEW_OBJECT_TYPE); 137 } 138 139 void ui_webview_reload(UiGeneric *g) { 140 WebViewData *webview = g->obj; 141 webkit_web_view_reload(webview->webview); 142 } 143 144 UiBool ui_webview_can_go_back(UiGeneric *g) { 145 WebViewData *webview = g->obj; 146 return webkit_web_view_can_go_back(webview->webview); 147 } 148 149 UiBool ui_webview_can_go_forward(UiGeneric *g) { 150 WebViewData *webview = g->obj; 151 return webkit_web_view_can_go_forward(webview->webview); 152 } 153 154 void ui_webview_go_back(UiGeneric *g) { 155 WebViewData *webview = g->obj; 156 webkit_web_view_go_back(webview->webview); 157 } 158 159 void ui_webview_go_forward(UiGeneric *g) { 160 WebViewData *webview = g->obj; 161 webkit_web_view_go_forward(webview->webview); 162 } 163 164 const char* ui_webview_get_uri(UiGeneric *g) { 165 WebViewData *webview = g->obj; 166 return webkit_web_view_get_uri(webview->webview); 167 } 168 169 void ui_webview_enable_javascript(UiGeneric *g, UiBool enable) { 170 WebViewData *webview = g->obj; 171 WebKitSettings *settings = webkit_web_view_get_settings(webview->webview); 172 webkit_settings_set_enable_javascript(settings, enable); 173 } 174 175 void ui_webview_set_zoom(UiGeneric *g, double zoom) { 176 WebViewData *webview = g->obj; 177 webkit_web_view_set_zoom_level(webview->webview, zoom); 178 webview->zoom = zoom; 179 } 180 181 double ui_webview_get_zoom(UiGeneric *g) { 182 WebViewData *webview = g->obj; 183 webview->zoom = webkit_web_view_get_zoom_level(webview->webview); 184 return webview->zoom; 185 } 186 187 188 #endif 189