ref: 794e2ee40b0345746539fbd41ad0eda122f8603e
dir: /n_vbox.c/
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include "nate_construct.h"
#include "n_vbox.h"
#define N_TYPE NVBox_Type
char* NVBox_Type = "NVBox";
typedef struct csizep csizep;
struct csizep {
Rectangle crect;
Rectangle frect;
Image *screen;
};
static void
vbox_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.y = r.max.y;
}
static Rectangle
vbox_calcsize(Nelem* nelem, Image* img, Rectangle r)
{
csizep params;
NVBox* b = (NVBox*)nelem;
GUARD(b);
params.crect = r;
params.screen = img;
params.frect.min = r.min;
params.frect.max = r.min;
lforeach(&b->children, vbox_childsize, ¶ms);
b->r = params.frect;
return b->r;
}
static void
vbox_childdraw(Nelem* elem, int, void *aux)
{
ncalldraw(elem, (Image*)aux);
}
static void
vbox_draw(Nelem* nelem, Image* img)
{
NVBox* b = (NVBox*)nelem;
GUARD(b);
lforeach(&b->children, vbox_childdraw, img);
}
static void
vbox_free(Nelem* nelem)
{
NVBox* b = (NVBox*)nelem;
if (nisroot(b))
return;
lfreelist(&b->children);
free(b);
}
static Nlist*
vbox_getchildren(Nelem* nelem)
{
NVBox* b = (NVBox*)nelem;
GUARD(b);
return &b->children;
}
static Nelemfunctions Nvboxfunctions = {
.calcsize = vbox_calcsize,
.draw = vbox_draw,
.free = vbox_free,
.getchildren = vbox_getchildren,
};
static NVBox*
vbox_slot(Nelem* child)
{
if (child == nc_get()) {
nc_pop();
}
NVBox* b = (NVBox*)nc_get();
GUARD(b);
ladd(&b->children, child);
return b;
}
DEF_ACCESSOR_OneParam(NVBox, vbox_sizetocontent, int, sizetocontent);
NVBox*
New_VBox(char *name)
{
NVBox *b = MakeNelem(NVBox, NVBox_Type, &Nvboxfunctions, name);
b->Slot = vbox_slot;
b->SizeToContent = vbox_sizetocontent;
linit(&b->children);
b->sizetocontent = 0;
nc_push(b);
return b;
}