ref: 1890aeca379976e46deabb0ce844aefc8be1a2c5
dir: /n_button.c/
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include "nate_construct.h"
#include "n_box.h"
#include "n_label.h"
#include "n_button.h"
#define N_TYPE NButton_Type
char *NButton_Type = "NButton";
static Rectangle
button_calcsize(Nelem *nelem, Image *screen, Rectangle r)
{
NButton *b = (NButton*)nelem;
GUARD(b);
b->r = ncallcalcsize(b->box, screen, r);
return b->r;
}
static void
button_draw(Nelem *nelem, Image *img)
{
NButton *b = (NButton*)nelem;
GUARD(b);
ncalldraw(b->box, img);
}
static Nelem*
button_checkhit(Nelem *nelem, Image *screen, Mouse m)
{
NButton *b = (NButton*)nelem;
GUARD(b);
return ncallcheckhit(b->box, screen, m);
}
static void
button_free(Nelem *nelem)
{
NButton *b = (NButton*)nelem;
GUARD(b);
if (nisroot(b) || nisroot(b->box))
return;
b->box->funcs->free(b->box);
free(b);
}
static Nlist*
button_getchildren(Nelem *nelem)
{
NButton *b = (NButton*)nelem;
GUARD(b);
return ncallgetchildren(b->box);
}
static Nelemfunctions Nbuttonfunctions = {
.calcsize = button_calcsize,
.draw = button_draw,
.checkhit = button_checkhit,
.free = button_free,
.getchildren = button_getchildren,
};
static NButton*
button_slot(Nelem* child)
{
if (child == nc_get())
nc_pop();
NButton *c = (NButton*)nc_get();
GUARD(c);
nc_push(c->box);
c->box->Slot(child);
nc_pop();
return c;
}
static NButton*
button_border(int w, Image *i)
{
NButton *c = (NButton*)nc_get();
GUARD(c);
nc_push(c->box);
c->box->Border(w, i);
nc_pop();
return c;
}
static NButton*
button_autosize(int n)
{
NButton *c = (NButton*)nc_get();
GUARD(c);
nc_push(c->box);
c->box->AutoSize(n);
nc_pop();
return c;
}
static NButton*
button_onclick(OnclickHandler f, void *aux)
{
NButton *c = (NButton*)nc_get();
GUARD(c);
nc_push(c->box);
c->box->OnClick(f, aux);
nc_pop();
return c;
}
static NButton*
button_label(char *l)
{
NButton *c = (NButton*)nc_get();
GUARD(c);
nc_push(c->label);
c->label->Label(l);
nc_pop();
return c;
}
static NButton*
button_labelfunc(StringGetter f)
{
NButton *c = (NButton*)nc_get();
GUARD(c);
nc_push(c->label);
c->label->LabelFunc(f);
nc_pop();
return c;
}
NButton*
New_Button(char *name)
{
NButton *b = MakeNelem(NButton, NButton_Type, &Nbuttonfunctions, name, 0);
b->box = New_Box(name)
->Slot(NAssign(NLabel, &b->label, New_Label(nil)));
/* pop box from stack */
nc_pop();
b->Slot = button_slot;
b->Border = button_border;
b->AutoSize = button_autosize;
b->OnClick = button_onclick;
b->Label = button_label;
b->LabelFunc = button_labelfunc;
nc_push(b);
return b;
}