shithub: libnate

ref: 6ff9ef0c4bef23bfd91f33f0c2f5cab0b88c0307
dir: /n_label.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include "nate_construct.h"
#include "n_label.h"

#define N_TYPE NLabel_Type
char* NLabel_Type = "NLabel";

char*
getlabelstr(NLabel* l)
{
	if (l->labelfunc)
		return l->labelfunc();
	if (l->label)
		return l->label;
	return "";
}

Point
label_calcsize(Nelem* nelem, Image*)
{
	NLabel* l = (NLabel*)nelem;
	GUARD(l);
	
	return stringsize(l->font, getlabelstr(l));
}

void
label_draw(Nelem* nelem, Image* img, Rectangle r)
{
	char* str;
	NLabel* l = (NLabel*)nelem;
	GUARD(l);
	
	str = getlabelstr(l);
	
	string(img, r.min, l->color, ZP, l->font, str);
}

Nelem*
label_checkhit(Nelem* nelem, Image* screen, Rectangle r, Mouse m)
{
	GUARD(nelem);
	return nil;
}

int
label_hit(Nelem* nelem, Mouse m)
{
	GUARD(nelem);
	return -1;
}

void
label_free(Nelem* nelem)
{
	GUARD(nelem);
	if (nisroot(nelem))
		return;
	free(nelem);
}

Nlist*
label_getchildren(Nelem* nelem)
{
	return nil;
}

static Nelemfunctions Nlabelfunctions = {
	.calcsize = label_calcsize,
	.draw = label_draw,
	.checkhit = label_checkhit,
	.hit = label_hit,
	.free = label_free,
	.getchildren = label_getchildren,
};

NLabel*
label(char* label)
{
	NLabel* l = (NLabel*)nc_get();
	GUARD(l);
	l->label = label;
	return l;
}

NLabel*
labelfunc(char* (*f)(void))
{
	NLabel* l = (NLabel*)nc_get();
	GUARD(l);
	l->labelfunc = f;
	return l;
}

NLabel*
lfont(Font* newfont)
{
	NLabel* l = (NLabel*)nc_get();
	GUARD(l);
	l->font = newfont;
	return l;
}

NLabel*
lcolor(Image* img)
{
	NLabel* l = (NLabel*)nc_get();
	GUARD(l);
	l->color = img;
	return l;
}

NLabel*
New_Label()
{
	NLabel* e = malloc(sizeof(NLabel));
	assert(e);
	e->type = NLabel_Type;
	e->funcs = &Nlabelfunctions;
	
	e->Label = label;
	e->LabelFunc = labelfunc;
	e->Font = lfont;
	e->Color = lcolor;
	
	e->label = nil;
	e->labelfunc = nil;
	e->font = display->defaultfont;
	e->color = display->black;
	nc_push(e);
	return e;
}