ui/cocoa/webview.m

changeset 875
0575ca45f1bb
child 876
2131c806440d
equal deleted inserted replaced
874:33618ae398bd 875:0575ca45f1bb
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 #import "webview.h"
30 #import "Container.h"
31
32 UIWIDGET ui_webview_create(UiObject *obj, UiWebviewArgs *args) {
33 WKWebView *webview = [[WKWebView alloc]init];
34
35 UiLayout layout = UI_ARGS2LAYOUT(args);
36 ui_container_add(obj, webview, &layout);
37
38 UiVar *var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_GENERIC);
39 if(var) {
40 UiGeneric *value = var->value;
41 value->get = ui_webview_get;
42 value->get_type = ui_webview_get_type;
43 value->set = ui_webview_set;
44 value->destroy = ui_webview_destroy;
45 value->obj = (__bridge void*)webview;
46 if(value->value) {
47 ui_webview_set(value, value->value, UI_WEBVIEW_OBJECT_TYPE);
48 } else {
49 UiWebViewData *data = malloc(sizeof(UiWebViewData));
50 memset(data, 0, sizeof(UiWebViewData));
51 data->webview = (__bridge void*)webview;
52 data->javascript = TRUE;
53 data->zoom = 1;
54 value->value = value;
55 }
56 }
57
58 return (__bridge void*)webview;
59 }
60
61 UiWebViewData* ui_webview_data_clone(UiWebViewData *data) {
62 UiWebViewData *newdata = malloc(sizeof(UiWebViewData));
63 memset(newdata, 0, sizeof(UiWebViewData));
64
65 if(data) {
66 newdata->uri = data->uri ? strdup(data->uri) : NULL;
67 newdata->mimetype = data->mimetype ? strdup(data->mimetype) : NULL;
68 newdata->encoding = data->encoding ? strdup(data->encoding) : NULL;
69 newdata->type = data->type;
70 newdata->javascript = data->javascript;
71 newdata->zoom = data->zoom;
72 if(data->content && data->contentlength > 0) {
73 newdata->content = malloc(data->contentlength + 1);
74 memcpy(newdata->content, data->content, data->contentlength);
75 newdata->content[data->contentlength] = 0;
76 newdata->contentlength = data->contentlength;
77 }
78 }
79
80 return newdata;
81 }
82
83 void ui_webview_data_free(UiWebViewData *data) {
84 if(!data) {
85 return;
86 }
87 free(data->uri);
88 free(data->mimetype);
89 free(data->encoding);
90 free(data->content);
91 free(data);
92 }
93
94 void* ui_webview_get(UiGeneric *g) {
95 return ui_webview_data_clone(g->value);
96 }
97
98 const char* ui_webview_get_type(UiGeneric *g) {
99 return UI_WEBVIEW_OBJECT_TYPE;
100 }
101
102 int ui_webview_set(UiGeneric *g, void *data, const char *type) {
103 if(!data || !type) {
104 return 1;
105 }
106 if(strcmp(type, UI_WEBVIEW_OBJECT_TYPE)) {
107 return 1;
108 }
109 ui_webview_data_free(g->value);
110 g->value = ui_webview_data_clone(data);
111
112 WKWebView *webview = (__bridge WKWebView*)g->obj;
113 UiWebViewData *webd = data;
114 if(webd->type == WEBVIEW_CONTENT_URL && webd->uri) {
115 NSURL *url = [NSURL URLWithString:[[NSString alloc] initWithUTF8String:webd->uri]];
116 if(url) {
117 NSURLRequest *req = [NSURLRequest requestWithURL:url];
118 if(req) {
119 [webview loadRequest:req];
120 }
121 }
122 } else if(webd->content) {
123 // TODO
124 }
125
126 return 1;
127 }
128
129 void ui_webview_destroy(UiGeneric *g) {
130 ui_webview_data_free(g->value);
131 g->value = NULL;
132 }
133
134
135 void ui_webview_load_url(UiGeneric *g, const char *url) {
136 WKWebView *webview = (__bridge WKWebView*)g->obj;
137 if(url) {
138 NSURL *nsurl = [NSURL URLWithString:[[NSString alloc] initWithUTF8String:url]];
139 if(nsurl) {
140 NSURLRequest *req = [NSURLRequest requestWithURL:nsurl];
141 if(req) {
142 [webview loadRequest:req];
143 }
144 }
145 }
146 }
147
148 void ui_webview_load_content(
149 UiGeneric *g,
150 const char *uri,
151 const char *content,
152 size_t contentlength,
153 const char *mimetype,
154 const char *encoding)
155 {
156
157 }
158
159
160 void ui_webview_reload(UiGeneric *g) {
161
162 }
163
164 UiBool ui_webview_can_go_back(UiGeneric *g) {
165 return FALSE;
166 }
167
168 UiBool ui_webview_can_go_forward(UiGeneric *g) {
169 return FALSE;
170 }
171
172 void ui_webview_go_back(UiGeneric *g) {
173
174 }
175
176 void ui_webview_go_forward(UiGeneric *g) {
177
178 }
179
180 const char * ui_webview_get_uri(UiGeneric *g) {
181 return NULL;
182 }
183
184 void ui_webview_enable_javascript(UiGeneric *g, UiBool enable) {
185
186 }
187
188 void ui_webview_set_zoom(UiGeneric *g, double zoom) {
189
190 }
191
192 double ui_webview_get_zoom(UiGeneric *g) {
193 return 0;
194 }

mercurial