shithub: libnate

ref: 7d7806db6cd48b8ebd565f636eebc59939e72c53
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 = r.min;
	
	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);
	
	if (nateborders) {
		border(img, b->r, 1, ncolor.red, ZP);
	}
}

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

DEF_SLOTFUNC(NHBox, hbox_slot);

DEF_ACCESSOR_OneParam(NHBox, hbox_sizetocontent, int, sizetocontent);

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