#include #include #include #include #include #include "xte.h" #include "builtins.h" #include "blocktype.h" typedef struct priv PRIV; typedef struct sb SB; struct priv { double v; } ; struct sb { void (*cont)(void *, void *); void *contarg; PRIV p; } ; static int my_w = -1; static int my_h; static int const_x; static int value_x; static int value_w; static int v_x; static BLOCKDIMS size(void) { XCharStruct cs_value; XCharStruct cs_const; XCharStruct cs_v; if (my_w < 0) { XTextExtents(font,"-0.000",6,XTE_JUNK,&cs_value); XTextExtents(font,"Const",5,XTE_JUNK,&cs_const); XTextExtents(font,"V",1,XTE_JUNK,&cs_v); my_w = ((cs_value.width > cs_const.width) ? cs_value.width : cs_const.width) + cs_v.width + (3 * font_space); v_x = my_w - cs_v.width - font_space; const_x = (v_x - cs_const.width) / 2; value_x = (v_x - cs_value.width) / 2; value_w = cs_value.width; my_h = 3 * font_baselineskip; } return((BLOCKDIMS){.w=my_w,.h=my_h}); } static void setup_cont(int ok, void *sbv) { SB sb; PRIV *p; sb = *(SB *)sbv; free(sbv); if (ok) { p = malloc(sizeof(PRIV)); p->v = sb.p.v; } else { p = 0; } (*sb.cont)(p,sb.contarg); } static void setup(void (*cont)(void *, void *), void *arg) { SB *sb; sb = malloc(sizeof(SB)); sb->cont = cont; sb->contarg = arg; setup_getarg("Constant",&setup_cont,sb,PAR_SIGNAL,&sb->p.v); } static void destroy(void *pv) { free(pv); } static void render(void *pv, Drawable d, GC gc_bg, GC gc_fg) { PRIV *p; int y; char s[16]; int l; XCharStruct cs; p = pv; XFillRectangle(disp,d,gc_bg,0,0,my_w,my_h); XDrawLine(disp,d,gc_fg,0,0,my_w-2,0); XDrawLine(disp,d,gc_fg,my_w-1,0,my_w-1,my_h-2); XDrawLine(disp,d,gc_fg,my_w-1,my_h-1,1,my_h-1); XDrawLine(disp,d,gc_fg,0,my_h-1,0,1); y = font_baseline + (font_baselineskip / 2); XDrawString(disp,d,gc_fg,const_x,y,"Const",5); y += font_baselineskip; l = sprintf(&s[0],"%+5.4f",p->v); if (s[1] == '0') { bcopy(&s[2],&s[1],l); l --; } else { l = sprintf(&s[0],"%+5.3f",p->v); } while (s[l-1] == '0') l --; if (s[l-1] == '.') l --; if (l == 1) s[0] = '0'; XTextExtents(font,&s[0],l,XTE_JUNK,&cs); XDrawString(disp,d,gc_fg,value_x+((value_w-cs.width)/2),y,&s[0],l); y = font_baselineskip + font_baseline; XDrawString(disp,d,gc_fg,v_x,y,"V",1); } static const char *outs[] = { "V" }; static BLOCKPARAM params[] = { { .name = "Value", .type = PAR_SIGNAL } }; BLOCKTYPE block_constant = { .name = "Constant", .n_in = 0, .ins = 0, .n_out = 1, .outs = &outs[0], .n_param = 1, .params = ¶ms[0], .compute_size = &size, .setup = &setup, .destroy = &destroy, .render = &render };