commit c162f582d06070c36e81117f1ab54ee91d7b2480
parent 8e162e7755e980a1f9bb03541201482b15fd89eb
Author: lumidify <nobody@lumidify.org>
Date:   Sun, 24 Jan 2021 20:49:47 +0100
Fix some bugs; add buggy gopher test
Diffstat:
4 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/box.c b/box.c
@@ -221,7 +221,7 @@ ltk_box_add(ltk_window *window, ltk_widget *widget, ltk_box *box, unsigned short
 		*errstr = "Widget already inside a container.\n";
 		return 1;
 	}
-	if (box->num_alloc >= box->num_widgets) {
+	if (box->num_widgets >= box->num_alloc) {
 		size_t new_size = box->num_alloc > 0 ? box->num_alloc * 2 : 4;
 		ltk_widget **new = realloc(box->widgets, new_size * sizeof(ltk_widget *));
 		if (!new)
diff --git a/label.c b/label.c
@@ -43,6 +43,7 @@ static void ltk_label_destroy(ltk_label *label, int shallow);
 
 static struct {
 	LtkColor text_color;
+	LtkColor bg_color;
 	int pad;
 } theme;
 
@@ -51,6 +52,8 @@ ltk_label_setup_theme_defaults(ltk_window *window) {
 	theme.pad = 5;
 	ltk_color_create(window->dpy, window->screen, window->cm,
 	    "#FFFFFF", &theme.text_color);
+	ltk_color_create(window->dpy, window->screen, window->cm,
+	    "#000000", &theme.bg_color);
 }
 
 void
@@ -60,6 +63,9 @@ ltk_label_ini_handler(ltk_window *window, const char *prop, const char *value) {
 	} else if (strcmp(prop, "text_color") == 0) {
 		ltk_color_create(window->dpy, window->screen, window->cm,
 		    value, &theme.text_color);
+	} else if (strcmp(prop, "bg_color") == 0) {
+		ltk_color_create(window->dpy, window->screen, window->cm,
+		    value, &theme.bg_color);
 	} else {
 		ltk_warn("Unknown property \"%s\" for label style.\n", prop);
 	}
@@ -69,12 +75,20 @@ static void
 ltk_label_draw(ltk_label *label, ltk_rect clip) {
 	ltk_window *window = label->widget.window;
 	ltk_rect rect = label->widget.rect;
-
+	ltk_rect clip_final = ltk_rect_intersect(clip, rect);
+	/* no idea why it would be less than 0, but whatever */
+	if (clip_final.w <= 0 || clip_final.h <= 0)
+		return;
+	XCopyArea(window->dpy, label->text_pixmap, window->xwindow, window->gc,
+	    clip_final.x - rect.x, clip_final.y - rect.y,
+	    clip_final.w, clip_final.h, clip_final.x, clip_final.y);
+	/*
 	int text_w, text_h;
 	ltk_text_line_get_size(label->tl, &text_w, &text_h);
 	int text_x = rect.x + (rect.w - text_w) / 2;
 	int text_y = rect.y + (rect.h - text_h) / 2;
 	ltk_text_line_draw(label->tl, window->gc, text_x, text_y, clip);
+	*/
 }
 
 static ltk_label *
@@ -95,6 +109,11 @@ ltk_label_create(ltk_window *window, const char *id, const char *text) {
 	label->widget.ideal_w = text_w + theme.pad * 2;
 	label->widget.ideal_h = text_h + theme.pad * 2;
 	ltk_text_line_render(label->tl, &window->theme.bg, &theme.text_color);
+	label->text_pixmap = XCreatePixmap(window->dpy, window->xwindow,
+	    label->widget.ideal_w, label->widget.ideal_h, window->depth);
+	XSetForeground(window->dpy, window->gc, theme.bg_color.xcolor.pixel);
+	XFillRectangle(window->dpy, label->text_pixmap, window->gc, 0, 0, label->widget.ideal_w, label->widget.ideal_h);
+	ltk_text_line_draw(label->tl, label->text_pixmap, window->gc, theme.pad, theme.pad, label->widget.rect);
 
 	return label;
 }
diff --git a/testbox.gui b/testbox.gui
@@ -1,8 +0,0 @@
-box box1 create vertical
-set-root-widget box1
-button btn1 create "I'm a button!"
-button btn2 create "I'm also a button!"
-button btn3 create "I'm another boring button."
-box box1 add btn1 ew
-box box1 add btn2 e
-box box1 add btn3
diff --git a/testbox.sh b/testbox.sh
@@ -7,7 +7,11 @@ if [ $? -ne 0 ]; then
 	exit 1
 fi
 
-cat testbox.gui | ./ltkc $ltk_id | while read cmd
+cmds="box box1 create vertical\nset-root-widget box1\n$(curl -s gopher://lumidify.org | awk -F'\t' '
+BEGIN {btn = 0; lbl = 0;}
+/^i/ { printf "label lbl%s create \"%s\"\nbox box1 add lbl%s w\n", lbl, substr($1, 2), lbl; lbl++ }
+/^[1gI]/ { printf "button btn%s create \"%s\"\nbox box1 add btn%s w\n", btn, substr($1, 2), btn; btn++ }')"
+echo "$cmds" | ./ltkc $ltk_id | while read cmd
 do
 	case "$cmd" in
 	"btn1 button_click")