#include #include #include #include #include #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(BLOCK_SIZE_ARGS) { int w_value; int w_const; int w_v; if (my_w < 0) { w_value = widthof("-0.000",6); w_const = widthof("Const",5); w_v = widthof("V",1); my_w = ((w_value > w_const) ? w_value : w_const) + w_v + (3 * font_space); v_x = my_w - w_v - font_space; const_x = (v_x - w_const) / 2; value_x = (v_x - w_value) / 2; value_w = w_value; 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(BLOCK_SETUP_ARGS) { SB *sb; sb = malloc(sizeof(SB)); sb->cont = cont; sb->contarg = contarg; setup_getarg("Constant",&setup_cont,sb,&sb->p.v,PAR_SIGNAL); } static void destroy(BLOCK_DESTROY_ARGS) { free(priv); } static void render(BLOCK_RENDER_ARGS) { PRIV *p; int y; char s[16]; int l; p = priv; 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[l++] = '0'; XDrawString(disp,d,gc_fg,value_x+((value_w-widthof(&s[0],l))/2),y,&s[0],l); y = font_baselineskip + font_baseline; XDrawString(disp,d,gc_fg,v_x,y,"V",1); } static int outy(BLOCK_OUTPUT_Y_ARGS) { return((3*font_baselineskip)/2); } static void rinit(BLOCK_RUN_INIT_ARGS) { } static void rstep(BLOCK_RUN_STEP_ARGS) { } static double out(BLOCK_RUN_OUT_ARGS) { return(((PRIV *)priv)->v); } static const char *outs[] = { "V" }; static BLOCKPARAM params[] = { { .name = "Value", .type = PAR_SIGNAL } }; BLOCKTYPE block_constant = BLOCKTYPE_O_P("Constant",outs,params,size,setup,destroy,render,outy,rinit,rstep,out);