sxirc

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 2b9b38098412ce5fcea2fd38417511d983abbcb6
Author: aabacchus <ben@bvnf.space>
Date:   Tue, 29 Nov 2022 14:07:07 +0000

init

Diffstat:
AMakefile | 13+++++++++++++
Aa.c | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 109 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,13 @@ +.POSIX: + +BIN = sxtest +LIBS = -lsx +CFLAGS = -Wall -Wextra -pedantic -Og -g + +$(BIN): a.c + $(CC) a.c $(CFLAGS) -o $(BIN) $(LIBS) + +clean: + rm -f $(BIN) + +.PHONY: clean diff --git a/a.c b/a.c @@ -0,0 +1,96 @@ +#include <stdio.h> +#include <stdlib.h> +#include <libsx.h> + +struct textpair { + Widget in, out; +}; + +void +submit_cb(Widget w, void *d) { + struct textpair *tp = (struct textpair *)d; + char *new, *old, *s; + new = GetTextWidgetText(tp->in); + old = GetTextWidgetText(tp->out); + if (new == NULL || old == NULL) { + fprintf(stderr, "error getting text\n"); + exit(1); + } + + size_t len = strlen(old); + s = malloc(strlen(new) + len + 2); + if (s == NULL) { + perror("malloc"); + exit(1); + } + + strcpy(s, old); + if (len > 0 && old[len - 1] != '\n') + strcat(s, "\n"); + strcat(s, new); + SetStringEntry(tp->in, ""); + SetTextWidgetText(tp->out, s, 0); + free(s); +} + +void +string_cb(Widget w, char *s, void *data) { + char *old = GetTextWidgetText((Widget)data); + size_t len = strlen(old); + char *new = malloc(strlen(s) + len + 2); + strcpy(new, old); + if (len > 0 && old[len - 1] != '\n') + strcat(new, "\n"); + strcat(new, s); + SetTextWidgetText((Widget)data, new, 0); + SetStringEntry(w, ""); + free(new); +} + +void +quit(Widget w, void *d) { + exit(0); +} + +int +main(int argc, char **argv) { + Widget w[4]; + + if (OpenDisplay(argc, argv) == 0) { + fprintf(stderr, "couldn't open display\n"); + return 1; + } + + w[0] = MakeTextWidget("", 0, 0, 300, 200); + //w[1] = MakeTextWidget("", 0, 1, 300, 30); + w[1] = MakeStringEntry(NULL, 200, string_cb, w[0]); + struct textpair tp = {w[1], w[0]}; + + w[2] = MakeButton("Submit", submit_cb, &tp); + w[3] = MakeButton("Quit", quit, NULL); + + + SetWidgetPos(w[1], PLACE_UNDER, w[0], NO_CARE, NULL); + SetWidgetPos(w[2], PLACE_RIGHT, w[1], PLACE_UNDER, w[0]); + SetWidgetPos(w[3], PLACE_RIGHT, w[2], PLACE_UNDER, w[0]); + + if (!w[0] || !w[1]) + return 1; + + XtAppContext app = XtWidgetToApplicationContext(w[0]); + if (app == NULL) + return 1; + + Widget parent, oldparent; + oldparent = w[0]; + do { + parent = oldparent; + oldparent = XtParent(oldparent); + } while (oldparent != NULL && parent != oldparent); + fprintf(stderr, "widget %p is toplevel shell: %d\n", parent, XtIsTopLevelShell(parent)); + + ShowDisplay(); + GetStandardColors(); + MainLoop(); + return 0; +}