shithub: libnate

ref: 1890aeca379976e46deabb0ce844aefc8be1a2c5
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;
};

static 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.x = r.min.x;
	params.frect.max.y = b->autoheight ? r.min.y : r.max.y;
	
	lforeach(&b->children, hbox_childsize, &params);

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

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

static void
hbox_draw(Nelem* nelem, Image* img)
{
	NHBox* b = (NHBox*)nelem;
	GUARD(b);
	
	lforeach(&b->children, hbox_childdraw, img);
}

static Nelemfunctions Nhboxfunctions = {
	.calcsize = hbox_calcsize,
	.draw = hbox_draw,
};

DEF_SLOTFUNC(NHBox, hbox_slot);

DEF_ACCESSOR_OneParam(NHBox, hbox_autoheight, int, autoheight);

NHBox*
New_HBox(char *name)
{
	NHBox *b = MakeNelem(NHBox, NHBox_Type, &Nhboxfunctions, name, -1);
	
	b->Slot = hbox_slot;
	b->AutoHeight = hbox_autoheight;
	
	linit(&b->children);
	b->autoheight = 0;
	nc_push(b);
	return b;
}