shithub: libnate

ref: 6ff9ef0c4bef23bfd91f33f0c2f5cab0b88c0307
dir: /n_window.c/

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

#define N_TYPE NWindow_Type
char* NWindow_Type = "NWindow";

Point
wcalcsize(Nelem* nelem, Image* screen)
{
	GUARD(nelem);
	return subpt(screen->r.max, screen->r.min);
}

void
wdraw(Nelem* nelem, Image* img, Rectangle r)
{
	Nelem* f;
	NWindow* w = (NWindow*)nelem;
	GUARD(w);
	f = lgetfirst(&w->child);
	if (f)
		ncalldraw(f, img, r);
}

void
wfree(Nelem* nelem)
{
	Nelem* f;
	NWindow* w = (NWindow*)nelem;
	if (nisroot(w))
		return;
	
	if ((f = lgetfirst(&w->child)))
		ncallfree(f);
	free(w);
}

Nlist*
wgetchildren(Nelem* nelem)
{
	NWindow* w = (NWindow*)nelem;
	GUARD(w);
	return &w->child;
}

static Nelemfunctions Nwindowfunctions = {
	.calcsize = wcalcsize,
	.draw = wdraw,
	.checkhit = nd_checkhit,
	.hit = nil,
	.free = wfree,
	.getchildren = wgetchildren,
};

NWindow*
slot(Nelem* child)
{
	if (child == nc_get()) {
		nc_pop();
	}
	NWindow* w = (NWindow*)nc_get();
	GUARD(w);
	// old child is potential leak!
	lsetfirst(&w->child, child);
	return w;
}

NWindow*
makeroot(void)
{
	NWindow* w = (NWindow*)nc_get();
	GUARD(w);
	nregroot(w);
	return w;
}

NWindow*
New_Window(void)
{
	NWindow* e = malloc(sizeof(NWindow));
	assert(e);
	
	e->type = NWindow_Type;
	e->funcs = &Nwindowfunctions;
	
	e->Slot = slot;
	e->MakeRoot = makeroot;
	
	linit(&e->child);
	nc_push(e);
	return e;
}