shithub: guifs

ref: 7c6a945996a1d5510ff1412320ac7d07a0f82851
dir: /props.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>

#include "guifs.h"

#define Eparse "could not parse property"

PropVal
defbackground(void)
{
	PropVal v;
	v.colour = mkcolour(DBlack);
	return v;
}

char *
printcolour(PropVal p)
{
	int bufsize = 64;
	char *buf = emalloc(bufsize);
	snprint(buf, bufsize, "%08ulX\n", p.colour->code);
	return buf;
}

char *
parsecolour(char *str, PropVal *p)
{
	char *r;
	ulong c = strtoul(str, &r, 16);
	if((r - str) != 8)
		return Eparse;
	(*p).colour = mkcolour(c);
	return nil;
}

PropVal
getprop(GuiElement *g, int tag)
{
	for(int i = 0; i < g->nprops; i++)
		if(g->props[i].tag == tag)
			return g->props[i].val;
	sysfatal("invalid prop for this gui element");
}

void
setprop(GuiElement *g, int tag, PropVal val)
{
	/* TODO: free old propval */
	for(int i = 0; i < g->nprops; i++)
		if(g->props[i].tag == tag){
			g->props[i].val = val;
			updategui(0);
			return;
		}
}

PropSpec propspecs[Pmax] = {
	[Pbackground] = {"background",	defbackground,	printcolour,	parsecolour},
};