ref: 1890aeca379976e46deabb0ce844aefc8be1a2c5
dir: /n_label.c/
#include <u.h> #include <libc.h> #include <draw.h> #include <event.h> #include "nate_construct.h" #include "n_label.h" #define N_TYPE NLabel_Type char* NLabel_Type = "NLabel"; static char* getlabelstr(NLabel* l) { if (l->labelfunc) return l->labelfunc(); if (l->label) return l->label; return ""; } static Rectangle label_calcsize(Nelem* nelem, Image*, Rectangle r) { NLabel* l = (NLabel*)nelem; GUARD(l); r.max = stringsize(l->font, getlabelstr(l)); r.max.x += l->margin.left + l->margin.right; r.max.y += l->margin.top + l->margin.bottom; r.max = addpt(r.min, r.max); l->r = r; return r; } static void label_draw(Nelem* nelem, Image* img) { char* str; Rectangle r; NLabel* l = (NLabel*)nelem; GUARD(l); str = getlabelstr(l); r = l->r; r.min.x += l->margin.left; r.min.y += l->margin.top; string(img, r.min, l->color, ZP, l->font, str); if (nateborders) { border(img, l->r, 1, ncolor.blue, ZP); } } static char* label_getname(Nelem *nelem) { NLabel *l = (NLabel*)nelem; GUARD(l); return getlabelstr(l); } static Nelemfunctions Nlabelfunctions = { .calcsize = label_calcsize, .draw = label_draw, .getname = label_getname, }; DEF_ACCESSOR_OneParam(NLabel, label, char*, label); DEF_ACCESSOR_OneParam(NLabel, labelfunc, StringGetter, labelfunc); DEF_ACCESSOR_OneParam(NLabel, lfont, Font*, font); DEF_ACCESSOR_OneParam(NLabel, lcolor, Image*, color); DEF_ACCESSOR_OneParam(NLabel, lmargin, Nmargin, margin); NLabel* New_Label(char *name) { NLabel *e = MakeNelem(NLabel, NLabel_Type, &Nlabelfunctions, name, 0); e->Label = label; e->LabelFunc = labelfunc; e->Font = lfont; e->Color = lcolor; e->Margin = lmargin; e->label = nil; e->labelfunc = nil; e->font = display->defaultfont; e->color = display->black; nc_push(e); return e; }