aboutsummaryrefslogtreecommitdiffstats
path: root/widget.c
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-02-16 16:31:08 -0500
committerSam Anthony <sam@samanthony.xyz>2025-02-16 16:31:08 -0500
commit4ffae4aeec2e5cc708f3a13899ad6b280b25b83f (patch)
tree32a94ebef53a1e31965fa5eeab8323114a0532d1 /widget.c
parent9bd3edfb51ac9b975bb02596eebafb2c9069aa36 (diff)
downloadvolute-4ffae4aeec2e5cc708f3a13899ad6b280b25b83f.zip
number field widget
Diffstat (limited to 'widget.c')
-rw-r--r--widget.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/widget.c b/widget.c
new file mode 100644
index 0000000..8a77b8f
--- /dev/null
+++ b/widget.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+
+#include "microui.h"
+#include "widget.h"
+
+void
+init_field(Field *f) {
+ f->buf[0] = '\0';
+ f->value = 0.0;
+}
+
+/* field draws a Field widget and updates its value.
+ * It returns MU_RES_CHANGE if the value has changed. */
+int
+field(mu_Context *ctx, Field *f) {
+ double value;
+ int changed = 0;
+ if (mu_textbox(ctx, f->buf, sizeof(f->buf)) & MU_RES_CHANGE) {
+ if (sscanf(f->buf, "%lf", &value) == 1) {
+ f->value = value;
+ changed = 1;
+ } else if (f->buf[0] == '\0') {
+ f->value = 0.0;
+ changed = 1;
+ }
+ }
+ return changed ? MU_RES_CHANGE : 0;
+}