xbintsc 0.6.10 → 0.6.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/checker/globals.js +2 -1
- package/dist/src/checker/globals.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/state.js +3 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +3 -0
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/compiler.js +1 -1
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +2 -0
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/node/crypto/index.js +12 -1
- package/dist/src/extensions/node/crypto/index.js.map +1 -1
- package/dist/tests/e2e/compile-builtins.test.js +26 -0
- package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
- package/dist/tests/e2e/compile-node.test.js +37 -1
- package/dist/tests/e2e/compile-node.test.js.map +1 -1
- package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
- package/dist/tests/e2e/crypto-random.test.js +63 -0
- package/dist/tests/e2e/crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/date-parse.test.js +27 -1
- package/dist/tests/e2e/date-parse.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-coverage.test.js +1 -1
- package/dist/tests/e2e/gui-dom.test.js +29 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-form.test.d.ts +9 -0
- package/dist/tests/e2e/gui-form.test.js +359 -0
- package/dist/tests/e2e/gui-form.test.js.map +1 -0
- package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
- package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
- package/dist/tests/e2e/gui-style.test.js +82 -0
- package/dist/tests/e2e/gui-style.test.js.map +1 -1
- package/dist/tests/e2e/gui-unsupported.test.js +4 -3
- package/dist/tests/e2e/gui-unsupported.test.js.map +1 -1
- package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
- package/dist/tests/e2e/json-parse-errors.test.js +48 -0
- package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
- package/doc/ai/language-support.md +2 -1
- package/doc/ai/zh-CN/language-support.md +2 -1
- package/doc/gui/api.md +8 -2
- package/doc/gui/dom.md +21 -12
- package/doc/gui/input.md +74 -3
- package/doc/implemented/node/crypto.md +8 -0
- package/doc/implemented/node/fs.md +1 -1
- package/doc/implemented/stdlib.md +10 -2
- package/doc/zh-CN/gui/api.md +8 -2
- package/doc/zh-CN/gui/dom.md +17 -11
- package/doc/zh-CN/gui/input.md +65 -4
- package/doc/zh-CN/implemented/node/crypto.md +6 -0
- package/doc/zh-CN/implemented/node/fs.md +1 -1
- package/doc/zh-CN/implemented/stdlib.md +10 -2
- package/package.json +7 -7
- package/runtime/ext_gui/css_match.cpp +21 -1
- package/runtime/ext_gui/css_selector.cpp +2 -2
- package/runtime/ext_gui/document.cpp +12 -0
- package/runtime/ext_gui/dom.h +41 -2
- package/runtime/ext_gui/dom_api_document.cpp +28 -0
- package/runtime/ext_gui/dom_api_events.cpp +17 -14
- package/runtime/ext_gui/dom_api_form.cpp +567 -0
- package/runtime/ext_gui/dom_api_internal.h +46 -214
- package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
- package/runtime/ext_gui/dom_api_node.cpp +10 -3
- package/runtime/ext_gui/dom_api_node_read.cpp +1 -1
- package/runtime/ext_gui/dom_api_proto.cpp +45 -0
- package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
- package/runtime/ext_gui/dom_parse.cpp +114 -1
- package/runtime/ext_gui/form_focus.cpp +293 -0
- package/runtime/ext_gui/form_input.cpp +448 -0
- package/runtime/ext_gui/form_internal.h +30 -0
- package/runtime/ext_gui/form_shared.cpp +126 -0
- package/runtime/ext_gui/form_state.cpp +536 -0
- package/runtime/ext_gui/form_state.h +97 -0
- package/runtime/ext_gui/gui_engine.h +40 -4
- package/runtime/ext_gui/gui_events.cpp +63 -48
- package/runtime/ext_gui/layout.cpp +6 -0
- package/runtime/ext_gui/layout.h +4 -0
- package/runtime/ext_gui/layout_flow.cpp +23 -1
- package/runtime/ext_gui/paint.cpp +4 -1
- package/runtime/ext_gui/paint_form.cpp +389 -0
- package/runtime/ext_gui/paint_internal.h +6 -0
- package/runtime/ext_gui/style_print.cpp +30 -0
- package/runtime/ext_gui/style_ua.cpp +42 -0
- package/runtime/ext_gui/warnings.cpp +5 -7
- package/runtime/ext_gui/window.cpp +31 -55
- package/runtime/ext_gui/window_diagnostics.cpp +58 -0
- package/runtime/ext_gui/window_emit.cpp +51 -0
- package/runtime/ext_node/crypto/crypto.c +3 -2
- package/runtime/ext_node/crypto/crypto_hash.h +13 -0
- package/runtime/ext_node/crypto/crypto_random.c +339 -0
- package/runtime/ext_node/fs/async_worker.c +1 -1
- package/runtime/ext_node/fs/fs_ops.c +1 -1
- package/runtime/ext_node/fs/parts/platform.inc +65 -0
- package/runtime/rt.h +27 -0
- package/runtime/xt_crypto.c +132 -0
- package/runtime/xt_random.c +143 -0
- package/runtime/xt_regex/matcher.inc +9 -28
- package/runtime/xt_regex/parser.inc +65 -2
- package/runtime/xt_regex/types.inc +11 -1
- package/runtime/xt_regex.h +6 -2
- package/runtime/xt_stdlib/dispatch.inc +2 -0
- package/runtime/xt_stdlib/math.inc +1 -1
- package/runtime/xt_stdlib2/date.inc +204 -4
- package/runtime/xt_stdlib2/json.inc +4 -546
- package/runtime/xt_stdlib2/json_parse.inc +347 -0
- package/runtime/xt_stdlib2/json_stringify.inc +368 -0
- package/runtime/xt_stdlib2/regexp-match.inc +6 -11
- package/runtime/xt_stdlib2.c +0 -4
- package/runtime/xt_typed_array/elements.inc +6 -1
- package/src/checker/globals.ts +2 -0
- package/src/codegen/generator/state.ts +3 -0
- package/src/codegen/generator/tables.ts +3 -0
- package/src/driver/compiler.ts +1 -1
- package/src/driver/runtime-objects.ts +2 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/node/crypto/index.ts +12 -1
|
@@ -308,7 +308,119 @@ bool closesOpenElement(const std::string &open, const std::string &next) {
|
|
|
308
308
|
|
|
309
309
|
} // namespace
|
|
310
310
|
|
|
311
|
-
|
|
311
|
+
namespace {
|
|
312
|
+
|
|
313
|
+
/** Elements the HTML tree builder puts in `<head>` when the source omits it. */
|
|
314
|
+
bool isHeadElement(const Node *node) {
|
|
315
|
+
if (node == nullptr || !node->isElement()) return false;
|
|
316
|
+
return node->tag == "title" || node->tag == "meta" || node->tag == "link" ||
|
|
317
|
+
node->tag == "base" || node->tag == "style";
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** A parsed fragment (`<div>…</div>`, no `<html>`) still needs `document.body`
|
|
321
|
+
* and the HTML structure the UA stylesheet assumes (`body { margin: 8px }`,
|
|
322
|
+
* `:root`, `html`/`body` block boxes). Browsers synthesise the missing elements
|
|
323
|
+
* during tree construction; the engine does the same after parsing, moving
|
|
324
|
+
* head-like top-level nodes into `<head>` and everything else into `<body>`. */
|
|
325
|
+
void synthesizeDocumentStructure(Node *document) {
|
|
326
|
+
Node *html = nullptr;
|
|
327
|
+
for (const std::unique_ptr<Node> &child : document->children) {
|
|
328
|
+
if (child->isElement() && child->tag == "html") {
|
|
329
|
+
html = child.get();
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (html == nullptr) {
|
|
334
|
+
auto html_node = std::make_unique<Node>(NodeType::Element);
|
|
335
|
+
html_node->tag = "html";
|
|
336
|
+
auto head_node = std::make_unique<Node>(NodeType::Element);
|
|
337
|
+
head_node->tag = "head";
|
|
338
|
+
auto body_node = std::make_unique<Node>(NodeType::Element);
|
|
339
|
+
body_node->tag = "body";
|
|
340
|
+
std::vector<std::unique_ptr<Node>> top_level;
|
|
341
|
+
top_level.swap(document->children);
|
|
342
|
+
for (std::unique_ptr<Node> &child : top_level) {
|
|
343
|
+
if (child->isElement() && isHeadElement(child.get())) {
|
|
344
|
+
head_node->append(std::move(child));
|
|
345
|
+
} else if (child->isText() && xt_dom_trim(child->text).empty()) {
|
|
346
|
+
/* Inter-element whitespace outside <body> is dropped, like the HTML
|
|
347
|
+
* parser's "before head/body" insertion modes. */
|
|
348
|
+
} else {
|
|
349
|
+
body_node->append(std::move(child));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
html_node->append(std::move(head_node));
|
|
353
|
+
html_node->append(std::move(body_node));
|
|
354
|
+
document->append(std::move(html_node));
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
Node *head = nullptr;
|
|
359
|
+
Node *body = nullptr;
|
|
360
|
+
for (const std::unique_ptr<Node> &child : html->children) {
|
|
361
|
+
if (child->isElement() && child->tag == "head") head = child.get();
|
|
362
|
+
if (child->isElement() && child->tag == "body") body = child.get();
|
|
363
|
+
}
|
|
364
|
+
if (head == nullptr) {
|
|
365
|
+
auto head_node = std::make_unique<Node>(NodeType::Element);
|
|
366
|
+
head_node->tag = "head";
|
|
367
|
+
Node *reference = html->children.empty() ? nullptr : html->children.front().get();
|
|
368
|
+
html->insertChild(std::move(head_node), reference);
|
|
369
|
+
head = html->firstElementChild();
|
|
370
|
+
/* `insertChild` may append when the reference is not a child. */
|
|
371
|
+
for (const std::unique_ptr<Node> &child : html->children) {
|
|
372
|
+
if (child->isElement() && child->tag == "head") head = child.get();
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/* Move stray head elements (a `<style>` after `</head>`, a fragment's style)
|
|
376
|
+
* into the head so the cascade sees them in the usual place. */
|
|
377
|
+
{
|
|
378
|
+
std::vector<std::unique_ptr<Node>> moved;
|
|
379
|
+
for (auto it = html->children.begin(); it != html->children.end();) {
|
|
380
|
+
if (it->get() != head && it->get() != body && isHeadElement(it->get())) {
|
|
381
|
+
moved.push_back(std::move(*it));
|
|
382
|
+
it = html->children.erase(it);
|
|
383
|
+
} else {
|
|
384
|
+
++it;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
for (std::unique_ptr<Node> &child : moved) head->append(std::move(child));
|
|
388
|
+
}
|
|
389
|
+
if (body == nullptr) {
|
|
390
|
+
auto body_node = std::make_unique<Node>(NodeType::Element);
|
|
391
|
+
body_node->tag = "body";
|
|
392
|
+
std::vector<std::unique_ptr<Node>> moved;
|
|
393
|
+
for (auto it = html->children.begin(); it != html->children.end();) {
|
|
394
|
+
if (it->get() == head || it->get() == body) {
|
|
395
|
+
++it;
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
moved.push_back(std::move(*it));
|
|
399
|
+
it = html->children.erase(it);
|
|
400
|
+
}
|
|
401
|
+
html->append(std::move(body_node));
|
|
402
|
+
body = html->children.back().get();
|
|
403
|
+
for (std::unique_ptr<Node> &child : moved) body->append(std::move(child));
|
|
404
|
+
}
|
|
405
|
+
/* Stray body content directly under <html> (a `<div>` after `</body>`, a
|
|
406
|
+
* fragment mixed with an authored body) belongs in the body. */
|
|
407
|
+
{
|
|
408
|
+
std::vector<std::unique_ptr<Node>> moved;
|
|
409
|
+
for (auto it = html->children.begin(); it != html->children.end();) {
|
|
410
|
+
if (it->get() == head || it->get() == body) {
|
|
411
|
+
++it;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
moved.push_back(std::move(*it));
|
|
415
|
+
it = html->children.erase(it);
|
|
416
|
+
}
|
|
417
|
+
for (std::unique_ptr<Node> &child : moved) body->append(std::move(child));
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
} // namespace
|
|
422
|
+
|
|
423
|
+
std::unique_ptr<Node> xt_html_parse(const std::string &html, bool synthesize_structure) {
|
|
312
424
|
Tokenizer tokenizer(html);
|
|
313
425
|
std::vector<Token> tokens = tokenizer.run();
|
|
314
426
|
|
|
@@ -346,6 +458,7 @@ std::unique_ptr<Node> xt_html_parse(const std::string &html) {
|
|
|
346
458
|
}
|
|
347
459
|
}
|
|
348
460
|
|
|
461
|
+
if (synthesize_structure) synthesizeDocumentStructure(document.get());
|
|
349
462
|
return document;
|
|
350
463
|
}
|
|
351
464
|
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — pointer activation, tab order and form submit/reset.
|
|
3
|
+
*
|
|
4
|
+
* Text editing lives in `form_input.cpp`; this unit owns what a click or a Tab
|
|
5
|
+
* does: focus placement, checkbox/radio/select activation, the tabbable order,
|
|
6
|
+
* and the `submit`/`reset` events of a `<form>`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include "gui_engine.h"
|
|
10
|
+
|
|
11
|
+
#include <algorithm>
|
|
12
|
+
#include <cctype>
|
|
13
|
+
#include <cmath>
|
|
14
|
+
#include <cstdio>
|
|
15
|
+
#include <cstdlib>
|
|
16
|
+
#include <memory>
|
|
17
|
+
#include <string>
|
|
18
|
+
#include <vector>
|
|
19
|
+
|
|
20
|
+
#include "form_internal.h"
|
|
21
|
+
#include "form_state.h"
|
|
22
|
+
#include "layout.h"
|
|
23
|
+
|
|
24
|
+
namespace {
|
|
25
|
+
|
|
26
|
+
using xtgui::FormState;
|
|
27
|
+
using xtgui::Node;
|
|
28
|
+
|
|
29
|
+
void set_range_value(XtGuiWindow *win, Node *node, float x) {
|
|
30
|
+
if (win == nullptr || win->document == nullptr) return;
|
|
31
|
+
xt_gui_flush_dom(win);
|
|
32
|
+
const xtgui::LayoutBox *box = win->document->boxOf(node);
|
|
33
|
+
if (box == nullptr || box->content_width <= 0.0f) return;
|
|
34
|
+
float fraction = (x + win->scroll_x - box->content_x) / box->content_width;
|
|
35
|
+
if (fraction < 0.0f) fraction = 0.0f;
|
|
36
|
+
if (fraction > 1.0f) fraction = 1.0f;
|
|
37
|
+
auto number_attr = [node](const char *name, double fallback) {
|
|
38
|
+
const std::string *value = node->attr(name);
|
|
39
|
+
if (value == nullptr) return fallback;
|
|
40
|
+
return std::strtod(value->c_str(), nullptr);
|
|
41
|
+
};
|
|
42
|
+
double minimum = number_attr("min", 0.0);
|
|
43
|
+
double maximum = number_attr("max", 100.0);
|
|
44
|
+
double step = number_attr("step", 1.0);
|
|
45
|
+
if (maximum <= minimum) maximum = minimum + 1.0;
|
|
46
|
+
double value = minimum + fraction * (maximum - minimum);
|
|
47
|
+
if (step > 0.0) value = minimum + std::floor((value - minimum) / step + 0.5) * step;
|
|
48
|
+
char buffer[64];
|
|
49
|
+
if (value == std::floor(value)) {
|
|
50
|
+
std::snprintf(buffer, sizeof(buffer), "%d", (int)value);
|
|
51
|
+
} else {
|
|
52
|
+
std::snprintf(buffer, sizeof(buffer), "%.6g", value);
|
|
53
|
+
}
|
|
54
|
+
xtgui::xt_form_set_value(node, buffer);
|
|
55
|
+
xtgui::formdetail::dirty(win);
|
|
56
|
+
xtgui::formdetail::emit_input(win, node, "", "insertReplacementText");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
bool reset_form(XtGuiWindow *win, Node *form) {
|
|
60
|
+
if (form == nullptr || !form->isElement() || form->tag != "form") return false;
|
|
61
|
+
xt_value payload = xtgui::formdetail::make_payload("reset");
|
|
62
|
+
if (xt_gui_emit_dom_event(win, "reset", form, payload)) return false;
|
|
63
|
+
xtgui::formdetail::reset_controls(win, form);
|
|
64
|
+
xtgui::formdetail::restyle(win);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
void activate_control(XtGuiWindow *win, Node *node) {
|
|
69
|
+
if (node == nullptr || xtgui::xt_form_is_disabled(node)) return;
|
|
70
|
+
if (node->isElement() && node->tag == "label") {
|
|
71
|
+
Node *target = nullptr;
|
|
72
|
+
const std::string *for_attr = node->attr("for");
|
|
73
|
+
if (for_attr != nullptr) {
|
|
74
|
+
const Node *root = node;
|
|
75
|
+
while (root->parent != nullptr) root = root->parent;
|
|
76
|
+
struct FindById {
|
|
77
|
+
const std::string &id;
|
|
78
|
+
const Node *operator()(const Node *current) const {
|
|
79
|
+
if (current->isElement()) {
|
|
80
|
+
const std::string *value = current->attr("id");
|
|
81
|
+
if (value != nullptr && *value == id) return current;
|
|
82
|
+
}
|
|
83
|
+
for (const std::unique_ptr<Node> &child : current->children) {
|
|
84
|
+
if (const Node *found = (*this)(child.get())) return found;
|
|
85
|
+
}
|
|
86
|
+
return nullptr;
|
|
87
|
+
}
|
|
88
|
+
} find = {*for_attr};
|
|
89
|
+
target = const_cast<Node *>(find(root));
|
|
90
|
+
}
|
|
91
|
+
if (target == nullptr) {
|
|
92
|
+
struct FindControl {
|
|
93
|
+
const Node *operator()(const Node *current) const {
|
|
94
|
+
if (xtgui::xt_form_is_focusable(current)) return current;
|
|
95
|
+
for (const std::unique_ptr<Node> &child : current->children) {
|
|
96
|
+
if (const Node *found = (*this)(child.get())) return found;
|
|
97
|
+
}
|
|
98
|
+
return nullptr;
|
|
99
|
+
}
|
|
100
|
+
} find_control;
|
|
101
|
+
target = const_cast<Node *>(find_control(node));
|
|
102
|
+
}
|
|
103
|
+
if (target != nullptr && target != node) {
|
|
104
|
+
xt_gui_set_focus(win, target);
|
|
105
|
+
activate_control(win, target);
|
|
106
|
+
}
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (xtgui::xt_form_is_button_like(node)) {
|
|
110
|
+
std::string type = xtgui::xt_form_type(node);
|
|
111
|
+
if (type == "reset") {
|
|
112
|
+
reset_form(win, xtgui::formdetail::form_ancestor(node));
|
|
113
|
+
} else if (type == "submit" || type == "image") {
|
|
114
|
+
xtgui::formdetail::submit_form(win, xtgui::formdetail::form_ancestor(node), node);
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (node->isElement() && node->tag == "button") {
|
|
119
|
+
const std::string *attr = node->attr("type");
|
|
120
|
+
std::string type = attr != nullptr ? *attr : "submit";
|
|
121
|
+
for (char &c : type) c = (char)std::tolower((unsigned char)c);
|
|
122
|
+
if (type == "reset") {
|
|
123
|
+
reset_form(win, xtgui::formdetail::form_ancestor(node));
|
|
124
|
+
} else if (type != "button") {
|
|
125
|
+
xtgui::formdetail::submit_form(win, xtgui::formdetail::form_ancestor(node), node);
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (xtgui::xt_form_is_checkable(node)) {
|
|
130
|
+
if (xtgui::xt_form_type(node) == "radio") {
|
|
131
|
+
xtgui::xt_form_set_checked(node, true);
|
|
132
|
+
} else {
|
|
133
|
+
xtgui::xt_form_set_checked(node, !xtgui::xt_form_checked(node));
|
|
134
|
+
}
|
|
135
|
+
xtgui::formdetail::restyle(win);
|
|
136
|
+
xtgui::formdetail::emit_input(win, node, "", "insertReplacementText");
|
|
137
|
+
xtgui::formdetail::emit_change(win, node);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (xtgui::xt_form_is_select(node)) {
|
|
141
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
142
|
+
if (state == nullptr) return;
|
|
143
|
+
int count = 0;
|
|
144
|
+
struct Count {
|
|
145
|
+
int *count;
|
|
146
|
+
void visit(const Node *current) const {
|
|
147
|
+
if (current->isElement() && current->isTag("option")) (*count)++;
|
|
148
|
+
for (const std::unique_ptr<Node> &child : current->children) visit(child.get());
|
|
149
|
+
}
|
|
150
|
+
} counter = {&count};
|
|
151
|
+
counter.visit(node);
|
|
152
|
+
if (count > 0) {
|
|
153
|
+
state->selected_index = (state->selected_index + 1) % count;
|
|
154
|
+
const Node *option = xtgui::xt_form_selected_option(node);
|
|
155
|
+
state->value = option != nullptr ? xtgui::xt_form_option_value(option) : "";
|
|
156
|
+
xtgui::formdetail::restyle(win);
|
|
157
|
+
xtgui::formdetail::emit_input(win, node, "", "insertReplacementText");
|
|
158
|
+
xtgui::formdetail::emit_change(win, node);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
struct TabStop {
|
|
164
|
+
Node *node;
|
|
165
|
+
int index;
|
|
166
|
+
int order;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
int tab_index_of(const Node *node) {
|
|
170
|
+
const std::string *value = node->attr("tabindex");
|
|
171
|
+
if (value != nullptr) return (int)std::strtol(value->c_str(), nullptr, 10);
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
void collect_tab_stops(const Node *node, std::vector<TabStop> *out, int *order) {
|
|
176
|
+
if (node->isElement() && xtgui::xt_form_is_focusable(node)) {
|
|
177
|
+
int index = tab_index_of(node);
|
|
178
|
+
if (index >= 0) out->push_back({const_cast<Node *>(node), index, (*order)++});
|
|
179
|
+
}
|
|
180
|
+
for (const std::unique_ptr<Node> &child : node->children) {
|
|
181
|
+
collect_tab_stops(child.get(), out, order);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
void ensure_visible(XtGuiWindow *win, Node *node) {
|
|
186
|
+
if (win == nullptr || win->document == nullptr) return;
|
|
187
|
+
xt_gui_flush_dom(win);
|
|
188
|
+
const xtgui::LayoutBox *box = win->document->boxOf(node);
|
|
189
|
+
if (box == nullptr) return;
|
|
190
|
+
float viewport_width = 0;
|
|
191
|
+
float viewport_height = 0;
|
|
192
|
+
xt_gui_window_viewport(win, &viewport_width, &viewport_height);
|
|
193
|
+
float x = win->scroll_x;
|
|
194
|
+
float y = win->scroll_y;
|
|
195
|
+
if (box->x < x) x = box->x;
|
|
196
|
+
else if (box->x + box->width > x + viewport_width) x = box->x + box->width - viewport_width;
|
|
197
|
+
if (box->y < y) y = box->y;
|
|
198
|
+
else if (box->y + box->height > y + viewport_height) y = box->y + box->height - viewport_height;
|
|
199
|
+
xt_gui_scroll_to(win, x, y);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
} // namespace
|
|
203
|
+
|
|
204
|
+
void xt_gui_form_focus_at(XtGuiWindow *win, xtgui::Node *node, float x) {
|
|
205
|
+
if (win == nullptr || node == nullptr) return;
|
|
206
|
+
xt_gui_set_focus(win, node);
|
|
207
|
+
if (!xtgui::formdetail::editable(node)) return;
|
|
208
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
209
|
+
if (state == nullptr) return;
|
|
210
|
+
int caret = xtgui::formdetail::caret_at_x(win, node, x);
|
|
211
|
+
state->selection_start = caret;
|
|
212
|
+
state->selection_end = caret;
|
|
213
|
+
state->selection_direction = 0;
|
|
214
|
+
xtgui::formdetail::dirty(win);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
void xt_gui_form_pointer(XtGuiWindow *win, const char *type, xtgui::Node *node, float x, float y) {
|
|
218
|
+
(void)y;
|
|
219
|
+
if (win == nullptr || node == nullptr || !node->isElement()) return;
|
|
220
|
+
std::string event = type;
|
|
221
|
+
if (event == "mousedown") {
|
|
222
|
+
if (xtgui::xt_form_is_focusable(node)) xt_gui_form_focus_at(win, node, x);
|
|
223
|
+
if (xtgui::xt_form_is_input(node) && xtgui::xt_form_type(node) == "range") {
|
|
224
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
225
|
+
if (state != nullptr) state->dragging = true;
|
|
226
|
+
set_range_value(win, node, x);
|
|
227
|
+
}
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (event == "mousemove") {
|
|
231
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
232
|
+
if (state != nullptr && state->dragging && xtgui::xt_form_is_input(node) &&
|
|
233
|
+
xtgui::xt_form_type(node) == "range") {
|
|
234
|
+
set_range_value(win, node, x);
|
|
235
|
+
}
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (event == "mouseup") {
|
|
239
|
+
FormState *state = xtgui::xt_form_state(node);
|
|
240
|
+
if (state != nullptr) {
|
|
241
|
+
bool was_dragging = state->dragging;
|
|
242
|
+
state->dragging = false;
|
|
243
|
+
if (was_dragging && xtgui::xt_form_is_input(node) &&
|
|
244
|
+
xtgui::xt_form_type(node) == "range") {
|
|
245
|
+
xtgui::formdetail::emit_change(win, node);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (event == "click") activate_control(win, node);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
void xt_gui_form_activate(XtGuiWindow *win, xtgui::Node *node) { activate_control(win, node); }
|
|
254
|
+
|
|
255
|
+
void xt_gui_focus_step(XtGuiWindow *win, bool forward) {
|
|
256
|
+
if (win == nullptr || win->document == nullptr) return;
|
|
257
|
+
std::vector<TabStop> stops;
|
|
258
|
+
int order = 0;
|
|
259
|
+
collect_tab_stops(win->document->root(), &stops, &order);
|
|
260
|
+
if (stops.empty()) {
|
|
261
|
+
xt_gui_set_focus(win, nullptr);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
std::stable_sort(stops.begin(), stops.end(), [](const TabStop &a, const TabStop &b) {
|
|
265
|
+
if (a.index == 0 && b.index == 0) return a.order < b.order;
|
|
266
|
+
if (a.index == 0) return false;
|
|
267
|
+
if (b.index == 0) return true;
|
|
268
|
+
if (a.index != b.index) return a.index < b.index;
|
|
269
|
+
return a.order < b.order;
|
|
270
|
+
});
|
|
271
|
+
int current = -1;
|
|
272
|
+
Node *focused = const_cast<Node *>(win->document->focus());
|
|
273
|
+
for (size_t i = 0; i < stops.size(); i++) {
|
|
274
|
+
if (stops[i].node == focused) {
|
|
275
|
+
current = (int)i;
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
int next;
|
|
280
|
+
if (current < 0) {
|
|
281
|
+
next = forward ? 0 : (int)stops.size() - 1;
|
|
282
|
+
} else {
|
|
283
|
+
next = (int)((size_t)current + (forward ? 1u : (size_t)stops.size() - 1u)) % (int)stops.size();
|
|
284
|
+
}
|
|
285
|
+
xt_gui_set_focus(win, stops[(size_t)next].node);
|
|
286
|
+
ensure_visible(win, stops[(size_t)next].node);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
void xt_gui_form_submit_request(XtGuiWindow *win, xtgui::Node *form, xtgui::Node *submitter) {
|
|
290
|
+
xtgui::formdetail::submit_form(win, form, submitter);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
void xt_gui_form_reset(XtGuiWindow *win, xtgui::Node *form) { reset_form(win, form); }
|