shithub: libnate

ref: 228a7b0eeda3531eb0bf0efa2211e4532af69681
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";

Rectangle
wcalcsize(Nelem* nelem, Image* screen, Rectangle r)
{
	Nelem *f;
	NWindow *w = (NWindow*)nelem;
	GUARD(w);
	f = lgetfirst(&w->child);
	if (f)
		ncallcalcsize(f, screen, screen->r);
	nelem->r = r;
	return screen->r;
}

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

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(char *name)
{
	NWindow *e = MakeNelem(NWindow, NWindow_Type, &Nwindowfunctions, name);
	
	e->Slot = slot;
	e->MakeRoot = makeroot;
	
	linit(&e->child);
	nc_push(e);
	return e;
}