1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/ui/gateui_wasmhtml.h"
#if defined(GATE_UI_WASMHTML)
#include "gate/results.h"
#include "gate/utilities.h"
#include "gate/platform/wasm/wasm_gate.h"
static gate_string_t const html_tag_div = GATE_STRING_INIT_STATIC("div");
static gate_string_t const html_ui_desktop_surface_id = GATE_STRING_INIT_STATIC("gate_wasm_html_desktop");
static gate_string_t const html_ui_id_prefix = GATE_STRING_INIT_STATIC("gate_wasm_html_id_");
static gate_string_t const attr_ctrl_id = GATE_STRING_INIT_STATIC("data-gate-ui-control-id");
static gate_string_t const attr_ctrl_ptr = GATE_STRING_INIT_STATIC("data-gate-ui-control-ptr");
static gate_string_t const attr_ctrl_container_id = GATE_STRING_INIT_STATIC("data-gate-ui-container-id");
static gate_atomic_int_t next_html_ui_id = 0;
gate_result_t gate_ui_wasm_host_init(gate_ui_host_t* host, gate_uintptr_t apphandle, void* userparam)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
gate_mem_clear(host, sizeof(gate_ui_host_t));
GATE_UI_WASM_SET_HOST_APPHANDLE(host, apphandle);
GATE_UI_WASM_SET_HOST_IMPL(host, NULL);
GATE_UI_WASM_SET_HOST_THREADID(host, 0);
GATE_UI_WASM_SET_HOST_USER_PARAM(host, userparam);
GATE_UI_WASM_SET_HOST_LINEHEIGHT(host, 16);
gate_wasm_html_add_element(&html_tag_div, &html_ui_desktop_surface_id, NULL, NULL, NULL, 0);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_ui_wasm_host_uninit(gate_ui_host_t* host)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_run_op_thread(gate_ui_host_t* host)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_post_to_op_thread(gate_ui_host_t* host, gate_entrypoint_t func, void* param)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_post_to_ui_thread(gate_ui_host_t* host, gate_entrypoint_t func, void* param)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_new_ctrl_id(gate_ui_host_t* host, gate_uint32_t* ctrl_id)
{
const gate_int32_t new_id = gate_atomic_int_inc(&next_html_ui_id);
if (ctrl_id)
{
*ctrl_id = (gate_uint32_t)new_id;
}
return GATE_RESULT_OK;
}
static gate_string_t* create_html_id(gate_uint32_t ctrl_id, gate_string_t* ptr_str)
{
gate_strbuilder_t builder;
gate_strbuilder_create(&builder, 32);
gate_strbuilder_append_string(&builder, &html_ui_id_prefix);
gate_strbuilder_append_uint32(&builder, ctrl_id);
return gate_strbuilder_to_string(&builder, ptr_str);
}
gate_result_t gate_ui_wasm_ctrl_create(gate_ui_host_t* host, gate_uint32_t ctrl_id, gate_uint32_t parent_ctrl_id, gate_string_t const* ctrl_class,
void* user_param, gate_ui_ctrl_t* ptr_ctrl)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_string_t html_id = GATE_STRING_INIT_EMPTY;
gate_string_t html_parent_id = GATE_STRING_INIT_EMPTY;
gate_string_t attr_names[5] = GATE_STRING_INIT_EMPTY;
gate_string_t attr_values[5] = GATE_STRING_INIT_EMPTY;
gate_size_t attrs_count = 0;
char num_buffer[16];
gate_size_t len;
do
{
if (NULL == create_html_id(ctrl_id, &html_id))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
if (parent_ctrl_id == 0)
{
gate_string_clone(&html_parent_id, &html_ui_desktop_surface_id);
}
else
{
if (NULL == create_html_id(parent_ctrl_id, &html_parent_id))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
{
gate_string_duplicate(&attr_names[attrs_count], &attr_ctrl_id);
len = gate_str_print_uint32(num_buffer, sizeof(num_buffer) - 1, ctrl_id);
gate_string_create(&attr_values[attrs_count], num_buffer, len);
++attrs_count;
}
{
gate_string_duplicate(&attr_names[attrs_count], &attr_ctrl_ptr);
len = gate_str_print_uint64(num_buffer, sizeof(num_buffer) - 1, (gate_uint64_t)(gate_uintptr_t)ptr_ctrl);
gate_string_create(&attr_values[attrs_count], num_buffer, len);
++attrs_count;
}
if (0 == gate_wasm_html_add_element(ctrl_class, &html_id, &html_parent_id, attr_names, attr_values, attrs_count))
{
ret = GATE_RESULT_FAILED;<--- Variable 'ret' is reassigned a value before the old one has been used.
}
if (ptr_ctrl)
{
GATE_UI_WASM_SET_CTRL_ID(ptr_ctrl, ctrl_id);
GATE_UI_WASM_SET_CTRL_PARENT_ID(ptr_ctrl, parent_ctrl_id);
GATE_UI_WASM_SET_CTRL_HOST(ptr_ctrl, host);
GATE_UI_WASM_SET_CTRL_CONTAINER_ID(ptr_ctrl, 0);
GATE_UI_WASM_SET_CTRL_USER_PARAM(ptr_ctrl, user_param);
}
ret = GATE_RESULT_OK;<--- Variable 'ret' is reassigned a value before the old one has been used.
} while (0);
while (attrs_count > 0)
{
--attrs_count;
gate_string_release(&attr_names[attrs_count]);
gate_string_release(&attr_values[attrs_count]);
}
gate_string_release(&html_parent_id);
gate_string_release(&html_id);
return ret;
}
gate_result_t gate_ui_wasm_ctrl_get_attributes(gate_ui_host_t* host, gate_uint32_t ctrl_id, gate_map_t* ptr_map)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_get_attribute(gate_ui_host_t* host, gate_uint32_t ctrl_id, gate_string_t const* attr, gate_string_t* value)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_string_t html_id = GATE_STRING_INIT_EMPTY;
if (!ctrl_id || !attr || !value)
{
return GATE_RESULT_INVALIDARG;
}
create_html_id(ctrl_id, &html_id);
if (gate_wasm_html_get_element_attrib(&html_id, attr, value))
{
ret = GATE_RESULT_OK;
}
gate_string_release(&html_id);
return ret;
}
gate_result_t gate_ui_wasm_ctrl_set_attribute(gate_ui_host_t* host, gate_uint32_t ctrl_id, gate_string_t const* attr, gate_string_t const* value)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_string_t html_id = GATE_STRING_INIT_EMPTY;
if (!ctrl_id || !attr || !value)
{
return GATE_RESULT_INVALIDARG;
}
create_html_id(ctrl_id, &html_id);
if (gate_wasm_html_set_element_attribs(&html_id, attr, value, 1))
{
ret = GATE_RESULT_OK;
}
gate_string_release(&html_id);
return ret;
}
gate_result_t gate_ui_wasm_ctrl_get_property(gate_ui_host_t* host, gate_uint32_t ctrl_id, gate_string_t const* prop, gate_string_t* value)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_string_t html_id = GATE_STRING_INIT_EMPTY;
if (!ctrl_id || !prop || !value)
{
return GATE_RESULT_INVALIDARG;
}
create_html_id(ctrl_id, &html_id);
if (gate_wasm_html_get_element_prop(&html_id, prop, value))
{
ret = GATE_RESULT_OK;
}
gate_string_release(&html_id);
return ret;
}
gate_result_t gate_ui_wasm_ctrl_set_property(gate_ui_host_t* host, gate_uint32_t ctrl_id, gate_string_t const* prop, gate_string_t const* value)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_string_t html_id = GATE_STRING_INIT_EMPTY;
if (!ctrl_id || !prop || !value)
{
return GATE_RESULT_INVALIDARG;
}
create_html_id(ctrl_id, &html_id);
if (gate_wasm_html_set_element_props(&html_id, prop, value, 1))
{
ret = GATE_RESULT_OK;
}
gate_string_release(&html_id);
return ret;
}
static gate_result_t set_html_attr(gate_ui_ctrl_t* ctrl, char const* attr, char const* value)
{
gate_uint32_t ctrl_id = GATE_UI_WASM_GET_CTRL_ID(ctrl);
gate_string_t attr_name = GATE_STRING_INIT_EMPTY;
gate_string_t attr_value = GATE_STRING_INIT_EMPTY;
gate_string_create_static(&attr_name, attr);
gate_string_create_static(&attr_value, value);
gate_ui_wasm_ctrl_set_attribute(NULL, ctrl_id, &attr_name, &attr_value);
gate_string_release(&attr_value);
gate_string_release(&attr_name);
return GATE_RESULT_OK;
}
static gate_result_t set_html_attr_str_string(gate_ui_ctrl_t* ctrl, char const* attr, gate_string_t const* value)
{
gate_uint32_t ctrl_id = GATE_UI_WASM_GET_CTRL_ID(ctrl);
gate_string_t attr_name = GATE_STRING_INIT_EMPTY;
gate_string_create_static(&attr_name, attr);
gate_ui_wasm_ctrl_set_attribute(NULL, ctrl_id, &attr_name, value);
gate_string_release(&attr_name);
return GATE_RESULT_OK;
}
static gate_result_t set_html_prop(gate_ui_ctrl_t* ctrl, gate_string_t const* prop, gate_string_t const* value)
{
gate_uint32_t ctrl_id = GATE_UI_WASM_GET_CTRL_ID(ctrl);
return gate_ui_wasm_ctrl_set_property(NULL, ctrl_id, prop, value);
}
static gate_result_t set_html_prop_str(gate_ui_ctrl_t* ctrl, char const* prop, gate_string_t const* value)
{
const gate_string_t prop_name = GATE_STRING_INIT_STATIC(prop);
return set_html_prop(ctrl, &prop_name, value);
}
gate_result_t gate_ui_wasm_ctrl_add_event(gate_ui_host_t* host, gate_uint32_t ctrl_id, gate_string_t const* evt)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_destroy(gate_ui_ctrl_t* ctrl)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_ui_host_t* gate_ui_wasm_ctrl_get_host(gate_ui_ctrl_t* ctrl)
{
return (ctrl == NULL) ? NULL : GATE_UI_WASM_GET_CTRL_HOST(ctrl);
}
gate_uint32_t gate_ui_wasm_ctrl_get_container(gate_ui_ctrl_t* ctrl)
{
gate_uint32_t container_id = 0;
if (ctrl != NULL)
{
container_id = GATE_UI_WASM_GET_CTRL_CONTAINER_ID(ctrl);
if (container_id == 0)
{
container_id = GATE_UI_WASM_GET_CTRL_ID(ctrl);
}
}
return container_id;
}
gate_bool_t gate_ui_wasm_ctrl_is_enabled(gate_ui_ctrl_t* ctrl)
{
return false;
}
gate_bool_t gate_ui_wasm_ctrl_is_visible(gate_ui_ctrl_t* ctrl)
{
return false;
}
gate_bool_t gate_ui_wasm_ctrl_is_focused(gate_ui_ctrl_t* ctrl)
{
return false;
}
gate_result_t gate_ui_wasm_ctrl_get_position(gate_ui_ctrl_t* ctrl, gate_ui_position_t* position)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_get_size(gate_ui_ctrl_t* ctrl, gate_ui_size_t* size)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_get_children(gate_ui_ctrl_t* ctrl, gate_array_t* children)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_get_text_length(gate_ui_ctrl_t* ctrl, gate_uint32_t* textlength)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_get_text(gate_ui_ctrl_t* ctrl, gate_string_t* text)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_get_state(gate_ui_ctrl_t* ctrl, gate_int32_t* state)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_set_enabled(gate_ui_ctrl_t* ctrl, gate_bool_t enabledstate)
{
return set_html_attr(ctrl, "disabled", enabledstate ? "false" : "true");
}
gate_result_t gate_ui_wasm_ctrl_set_visible(gate_ui_ctrl_t* ctrl, gate_bool_t visibility)
{
return set_html_attr(ctrl, "style.visibility", visibility ? "visible" : "hidden");
}
gate_result_t gate_ui_wasm_ctrl_set_focus(gate_ui_ctrl_t* ctrl)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_set_position(gate_ui_ctrl_t* ctrl, gate_ui_point_t const* position, gate_ui_size_t const* size)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_set_text(gate_ui_ctrl_t* ctrl, gate_string_t const* text)
{
static gate_string_t const prop_innertext = GATE_STRING_INIT_STATIC("innerText");
return set_html_prop(ctrl, &prop_innertext, text);
}
gate_result_t gate_ui_wasm_ctrl_set_state(gate_ui_ctrl_t* ctrl, gate_int32_t state)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_wasm_ctrl_refresh(gate_ui_ctrl_t* ctrl)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
#endif /* GATE_UI_WASMHTML */
|