aboutsummaryrefslogtreecommitdiffstats
path: root/widget.c
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-03-01 13:01:24 -0500
committerSam Anthony <sam@samanthony.xyz>2025-03-01 13:01:24 -0500
commit5c871346cb3db341e9b4061618c3cee4497b04e9 (patch)
tree9c70c115d0c026d1d7326a43ff844e510754cd81 /widget.c
parent5438f11e0d826b0d6222b0bc9bea5b5a0c23f6f8 (diff)
downloadvolute-5c871346cb3db341e9b4061618c3cee4497b04e9.zip
draw red box around bad input fields
Diffstat (limited to 'widget.c')
-rw-r--r--widget.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/widget.c b/widget.c
index 5ff6f2f..ae41e89 100644
--- a/widget.c
+++ b/widget.c
@@ -7,35 +7,55 @@
#define nelem(arr) (sizeof(arr)/sizeof(arr[0]))
+static const mu_Color RED = {255, 0, 0, 255};
+
void
w_init_field(w_Field *f) {
f->buf[0] = '\0';
f->value = 0.0;
+ f->invalid = 0;
}
/* field draws a Field widget and updates its value.
* It returns MU_RES_CHANGE if the value has changed. */
int
w_field(mu_Context *ctx, w_Field *f) {
+ mu_Rect rect;
+ int changed;
+ char s[2];
double value;
- int changed = 0;
+
+ rect = mu_layout_next(ctx);
+ mu_layout_set_next(ctx, rect, 0);
+
+ changed = 0;
if (mu_textbox(ctx, f->buf, sizeof(f->buf)) & MU_RES_CHANGE) {
- if (sscanf(f->buf, "%lf", &value) == 1) {
+ /* s used to catch erroneous chars at end of field. */
+ if (sscanf(f->buf, "%lf %1s", &value, s) == 1) {
f->value = value;
+ f->invalid = 0;
changed = 1;
} else if (f->buf[0] == '\0') {
f->value = 0.0;
+ f->invalid = 0;
changed = 1;
+ } else {
+ f->invalid = 1;
}
}
+
+ if (f->invalid) {
+ mu_draw_box(ctx, rect, RED);
+ }
+
return changed ? MU_RES_CHANGE : 0;
}
void
w_set_field(w_Field *f, double val) {
f->value = val;
- snprintf(f->buf, sizeof(f->buf), "%f", val);
+ snprintf(f->buf, sizeof(f->buf), "%.5f", val);
}
void