ui/cocoa/webview.m

changeset 876
2131c806440d
parent 875
0575ca45f1bb
child 878
862a57990eb8
equal deleted inserted replaced
875:0575ca45f1bb 876:2131c806440d
90 free(data->content); 90 free(data->content);
91 free(data); 91 free(data);
92 } 92 }
93 93
94 void* ui_webview_get(UiGeneric *g) { 94 void* ui_webview_get(UiGeneric *g) {
95 UiWebViewData *data = g->value;
96 WKWebView *webview = (__bridge WKWebView*)g->obj;
97
98 if(data->type == WEBVIEW_CONTENT_URL) {
99 (void)ui_webview_get_uri(g); // this updates data->uri
100 }
101
95 return ui_webview_data_clone(g->value); 102 return ui_webview_data_clone(g->value);
96 } 103 }
97 104
98 const char* ui_webview_get_type(UiGeneric *g) { 105 const char* ui_webview_get_type(UiGeneric *g) {
99 return UI_WEBVIEW_OBJECT_TYPE; 106 return UI_WEBVIEW_OBJECT_TYPE;
132 } 139 }
133 140
134 141
135 void ui_webview_load_url(UiGeneric *g, const char *url) { 142 void ui_webview_load_url(UiGeneric *g, const char *url) {
136 WKWebView *webview = (__bridge WKWebView*)g->obj; 143 WKWebView *webview = (__bridge WKWebView*)g->obj;
137 if(url) { 144 UiWebViewData *data = g->value;
138 NSURL *nsurl = [NSURL URLWithString:[[NSString alloc] initWithUTF8String:url]]; 145 data->type = WEBVIEW_CONTENT_URL;
139 if(nsurl) { 146
140 NSURLRequest *req = [NSURLRequest requestWithURL:nsurl]; 147 if(!url) {
141 if(req) { 148 url = "abount:blank";
142 [webview loadRequest:req]; 149 }
143 } 150
151 NSURL *nsurl = [NSURL URLWithString:[[NSString alloc] initWithUTF8String:url]];
152 if(nsurl) {
153 NSURLRequest *req = [NSURLRequest requestWithURL:nsurl];
154 if(req) {
155 [webview loadRequest:req];
144 } 156 }
145 } 157 }
146 } 158 }
147 159
148 void ui_webview_load_content( 160 void ui_webview_load_content(
151 const char *content, 163 const char *content,
152 size_t contentlength, 164 size_t contentlength,
153 const char *mimetype, 165 const char *mimetype,
154 const char *encoding) 166 const char *encoding)
155 { 167 {
156 168 UiWebViewData *data = g->value;
169 WKWebView *webview = (__bridge WKWebView*)g->obj;
170
171 data->type = WEBVIEW_CONTENT_CONTENT;
172
173 free(data->uri);
174 data->uri = NULL;
175 free(data->mimetype);
176 free(data->encoding);
177 free(data->content);
178 data->type = WEBVIEW_CONTENT_URL;
179
180 data->content = malloc(contentlength+1);
181 memcpy(data->content, content, contentlength);
182 data->content[contentlength] = 0;
183
184 if(!mimetype) {
185 mimetype = "text/plain";
186 }
187 if(!encoding) {
188 encoding = "UTF-8";
189 }
190
191 data->mimetype = strdup(mimetype);
192 data->encoding = strdup(encoding);
193
194 NSString *mtype = [[NSString alloc]initWithUTF8String:mimetype];
195 NSString *enc = [[NSString alloc]initWithUTF8String:encoding];
196 NSData *ct = [NSData dataWithBytes:content length:contentlength];
197 NSURL *url;
198 if(uri) {
199 NSString *uriStr = [[NSString alloc]initWithUTF8String:uri];
200 url = [NSURL URLWithString:uriStr];
201 } else {
202 url = [NSURL URLWithString:@"file:///"];
203 }
204 [webview loadData:ct MIMEType:mtype characterEncodingName:enc baseURL:url];
157 } 205 }
158 206
159 207
160 void ui_webview_reload(UiGeneric *g) { 208 void ui_webview_reload(UiGeneric *g) {
161 209 WKWebView *webview = (__bridge WKWebView*)g->obj;
210 [webview reload];
162 } 211 }
163 212
164 UiBool ui_webview_can_go_back(UiGeneric *g) { 213 UiBool ui_webview_can_go_back(UiGeneric *g) {
165 return FALSE; 214 return FALSE;
166 } 215 }
176 void ui_webview_go_forward(UiGeneric *g) { 225 void ui_webview_go_forward(UiGeneric *g) {
177 226
178 } 227 }
179 228
180 const char * ui_webview_get_uri(UiGeneric *g) { 229 const char * ui_webview_get_uri(UiGeneric *g) {
181 return NULL; 230 UiWebViewData *data = g->value;
231 WKWebView *webview = (__bridge WKWebView*)g->obj;
232
233 free(data->uri);
234 data->uri = NULL;
235
236 NSURL *url = webview.URL;
237 if(url) {
238 NSString *s = [url absoluteString];
239 if(s) {
240 data->uri = strdup(s.UTF8String);
241 }
242 }
243 return data->uri;
182 } 244 }
183 245
184 void ui_webview_enable_javascript(UiGeneric *g, UiBool enable) { 246 void ui_webview_enable_javascript(UiGeneric *g, UiBool enable) {
185 247
186 } 248 }

mercurial