shithub: libnate

ref: dcda9073c2fef62d70544e8dac46c2919c908587
dir: /n_hbox.c/

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

#define N_TYPE NHBox_Type
char* NHBox_Type = "NHBox";

typedef struct csizep csizep;
struct csizep {
	Rectangle crect;
	Rectangle frect;
	Image *screen;
};

void
hbox_childsize(Nelem* nelem, int, void *aux)
{
	csizep *p = (csizep*)aux;
	Rectangle r = ncallcalcsize(nelem, p->screen, p->crect);
	combinerect(&p->frect, r);
	p->crect.min.x = r.max.x;
}

static Rectangle
hbox_calcsize(Nelem* nelem, Image* screen, Rectangle r)
{
	csizep params;
	NHBox* b = (NHBox*)nelem;
	GUARD(b);
	
	params.screen = screen;
	params.crect = r;
	params.frect.min = r.min;
	params.frect.max = r.min;
	
	lforeach(&b->children, hbox_childsize, &params);

	nelem->r = params.frect;
	return nelem->r;
}

void
hbox_childdraw(Nelem* elem, int, void *aux)
{
	
	ncalldraw(elem, (Image*)aux);
}

void
hbox_draw(Nelem* nelem, Image* img)
{
	NHBox* b = (NHBox*)nelem;
	GUARD(b);
	
	lforeach(&b->children, hbox_childdraw, img);
	
	if (nateborders) {
		border(img, b->r, 1, ncolor.red, ZP);
	}
}

void
hbox_free(Nelem* nelem)
{
	NHBox* b = (NHBox*)nelem;
	if (nisroot(b))
		return;
	
	lfreelist(&b->children);
	free(b);
}

Nlist*
hbox_getchildren(Nelem* nelem)
{
	NHBox* b = (NHBox*)nelem;
	GUARD(b);
	return &b->children;
}

static Nelemfunctions Nhboxfunctions = {
	.calcsize = hbox_calcsize,
	.draw = hbox_draw,
	.free = hbox_free,
	.getchildren = hbox_getchildren,
};

NHBox*
hbox_slot(Nelem* child)
{
	if (child == nc_get()) {
		nc_pop();
	}
	NHBox* b = (NHBox*)nc_get();
	GUARD(b);
	
	ladd(&b->children, child);
	return b;
}

DEF_ACCESSOR_OneParam(NHBox, hbox_sizetocontent, int, sizetocontent);

NHBox*
New_HBox(char *name)
{
	NHBox *b = MakeNelem(NHBox, NHBox_Type, &Nhboxfunctions, name);
	
	b->Slot = hbox_slot;
	b->SizeToContent = hbox_sizetocontent;
	
	linit(&b->children);
	b->sizetocontent = 0;
	nc_push(b);
	return b;
}