Skip to content
Snippets Groups Projects
g_scalar.c 44.8 KiB
Newer Older
        sprintf(tagbuf, "scalar%lxgobj", (long unsigned int)x->sc_vec);
        sprintf(groupbuf, "dgroup%lx.%lx", (long unsigned int)templatecanvas,
            (long unsigned int)x->sc_vec);
        gui_vmess("gui_create_scalar_group", "xss",
            glist_getcanvas(owner), groupbuf, tagbuf);
    /* warning: don't need--- have recursive func. */
Miller Puckette's avatar
Miller Puckette committed
    for (y = templatecanvas->gl_list; y; y = y->g_next)
    {
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
            /* check subpatches for more drawing commands.
               (Optimized to only search [group] subpatches) */
            if (pd_class(&y->g_pd) == canvas_class &&
                scalar_groupvis(x, owner, template,
                    (t_glist *)y, templatecanvas, vis);
            continue;
        }
        (*wb->w_parentvisfn)(y, owner, 0, x, x->sc_vec, template,
        //sys_vgui(".x%lx.c delete .scalar%lx\n", glist_getcanvas(owner),
        //    x->sc_vec);
        char tagbuf[MAXPDSTRING];
        sprintf(tagbuf, "scalar%lx", (long unsigned int)x->sc_vec);
        gui_vmess("gui_scalar_erase", "xs",
            glist_getcanvas(owner), tagbuf);
Miller Puckette's avatar
Miller Puckette committed
    if (glist_isselected(owner, &x->sc_gobj))
    {
        // we removed this because it caused infinite recursion
        // in the scalar-help.pd example
        //scalar_select(z, owner, 1);
        post("here we are in scalar vis, selected...");
Miller Puckette's avatar
Miller Puckette committed
        scalar_drawselectrect(x, owner, 0);
        scalar_drawselectrect(x, owner, 1);
    }
}

static void scalar_doredraw(t_gobj *client, t_glist *glist)
{
    scalar_vis(client, glist, 0);
    scalar_vis(client, glist, 1);
    if (glist_isselected(glist_getcanvas(glist), (t_gobj *)glist))
    {
        //fprintf(stderr,"yes\n");
        sys_vgui("pdtk_select_all_gop_widgets .x%lx %lx %d\n",
            glist_getcanvas(glist), glist, 1);
    }
Miller Puckette's avatar
Miller Puckette committed
}

void scalar_redraw(t_scalar *x, t_glist *glist)
{
    if (glist_isvisible(glist))
        scalar_doredraw((t_gobj *)x, glist);
        //sys_queuegui(x, glist, scalar_doredraw);
/* here we call the parentclickfns for drawing commands.
   We recurse all the way to the end of the glist (and, for the
   groups, as deep as they go) and then call the functions from the bottom up.
   This way the clicks can "bubble" up from the bottom.  This has the effect
   that the shape visually closest to the front gets called first.

   This is called "event bubbling" in the HTML5 DOM.  You can also call
   events in top down order in HTML5/SVG as well (called "capturing" or
   "trickling"). But because some old version of Internet Explorer only did
   bubbling, that is the most widely used model today, and the only one
   I decided to implement here.

   However, [group] doesn't yet have an outlet to report events. Only a
   single [draw] should report a click here atm.
*/
int scalar_groupclick(struct _glist *groupcanvas,
    t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
    t_float xloc, t_float yloc, int xpix, int ypix,
    int shift, int alt, int dbl, int doit, t_float basex, t_float basey,
    t_gobj *obj)
{
    int hit = 0;
    t_gobj *nextobj = obj->g_next;
    /* let's skip over any objects that aren't drawing instructions
       or groups */
    while (nextobj &&
           !class_isdrawcommand(pd_class((t_pd *)nextobj)) &&
           !(pd_class(&nextobj->g_pd) == canvas_class &&
               ((t_glist *)nextobj)->gl_svg))
        nextobj = nextobj->g_next;
    /* If there's another link in the list go ahead and recurse with it */
    if (nextobj)
    {
        hit = (scalar_groupclick(groupcanvas, data, template, sc, ap,
            owner, xloc, yloc, xpix, ypix,
            shift, alt, dbl, doit, basex, basey, nextobj));
        if (hit) return hit;
    }
    /* recurse inside a [group] object to look for more objects */
    if (pd_class(&obj->g_pd) == canvas_class && ((t_glist *)obj)->gl_svg)
    {
            t_canvas *cnv = (t_canvas *)obj;
            obj = cnv->gl_list;
            if (obj)
                return (scalar_groupclick(cnv, data, template, sc, ap,
                owner, xloc, yloc, xpix, ypix,
                shift, alt, dbl, doit, basex, basey, obj));
    }
    else /* finally, try to call the parent click function ... */
    {
        t_parentwidgetbehavior *wb = pd_getparentwidget(&obj->g_pd);
        if (!wb) return hit;
        if ((*wb->w_parentclickfn)(obj, owner,
            data, template, sc, ap, basex + xloc, basey + yloc,
            xpix, ypix, shift, alt, dbl, doit))
                hit = 1;
    }
    return (hit);
}

/*
int scalar_groupclick(struct _glist *groupcanvas,
    t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
    t_float xloc, t_float yloc, int xpix, int ypix,
    int shift, int alt, int dbl, int doit, t_float basex, t_float basey)
{
    int hit = 0;
    t_gobj *y;
    for (y = groupcanvas->gl_list; y; y = y->g_next)
    {
        if (pd_class(&y->g_pd) == canvas_class &&
            ((t_glist *)y)->gl_svg)
        {
            if (hit = scalar_groupclick((t_glist *)y, data, template, sc, ap,
                owner, xloc, yloc, xpix, ypix,
                shift, alt, dbl, doit, basex, basey))
            {
                return (hit);
            }
        }
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
        if (!wb) continue;
        if (hit = (*wb->w_parentclickfn)(y, owner,
            data, template, sc, ap, basex + xloc, basey + yloc,
            xpix, ypix, shift, alt, dbl, doit))
        {
            return (hit);
        }
    }
    return 0;
}
Miller Puckette's avatar
Miller Puckette committed
int scalar_doclick(t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
    t_float xloc, t_float yloc, int xpix, int ypix,
Miller Puckette's avatar
Miller Puckette committed
    int shift, int alt, int dbl, int doit)
{
    int hit = 0;
    t_canvas *templatecanvas = template_findcanvas(template);
    t_float basex = template_getfloat(template, gensym("x"), data, 0);
    t_float basey = template_getfloat(template, gensym("y"), data, 0);
    //fprintf(stderr,"=================scalar_doclick %f %f %f %f %d\n",
    //    basex, basey, xloc, yloc, doit);
Ivica Bukvic's avatar
Ivica Bukvic committed

    SETFLOAT(at, basex + xloc);
    SETFLOAT(at+1, basey + yloc);
    if (doit)
    {
        //fprintf(stderr,"    doit\n");
        template_notifyforscalar(template, owner, 
            sc, gensym("click"), 2, at);
Ivica Bukvic's avatar
Ivica Bukvic committed

    // if we are nested ignore xloc and yloc, otherwise
    // nested objects get their hitbox miscalculated
    if (xloc != 0.0 || yloc != 0.0)
    {
        //fprintf(stderr,"ignoring\n");
Ivica Bukvic's avatar
Ivica Bukvic committed

    {
        if (!(obj = templatecanvas->gl_list)) return 0;
        hit = scalar_groupclick(templatecanvas, data, template, sc, ap,
                    owner, xloc, yloc, xpix, ypix,
                    shift, alt, dbl, doit, basex, basey, obj);
    }
/* Unfortunately, nested gops don't yet handle scalar clicks correctly. The
   nested scalar seems not to receive the click.  However, the enter/leave
   messages happen just fine since most of their logic is in tcl/tk. */
Miller Puckette's avatar
Miller Puckette committed
static int scalar_click(t_gobj *z, struct _glist *owner,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
    //fprintf(stderr,"scalar_click %d %d\n", xpix, ypix);
Miller Puckette's avatar
Miller Puckette committed
    t_scalar *x = (t_scalar *)z;
    x->sc_bboxcache = 0;

    t_template *template = template_findbyname(x->sc_template);
Miller Puckette's avatar
Miller Puckette committed
    return (scalar_doclick(x->sc_vec, template, x, 0,
        owner, 0, 0, xpix, ypix, shift, alt, dbl, doit));
}

void canvas_writescalar(t_symbol *templatesym, t_word *w, t_binbuf *b,
    int amarrayelement);

static void scalar_save(t_gobj *z, t_binbuf *b)
{
    t_scalar *x = (t_scalar *)z;
    t_binbuf *b2 = binbuf_new();
    canvas_writescalar(x->sc_template, x->sc_vec, b2, 0);
    binbuf_addv(b, "ss", &s__X, gensym("scalar"));
    binbuf_addbinbuf(b, b2);
    binbuf_addsemi(b);
    binbuf_free(b2);
}

static void scalar_menuopen(t_scalar *x)
{
    t_canvas *c = scalar_getcanvasfield(x);
    canvas_vis(c, 1);
Miller Puckette's avatar
Miller Puckette committed
static void scalar_properties(t_gobj *z, struct _glist *owner)
{
    t_scalar *x = (t_scalar *)z;
    char *buf, buf2[80];
    int bufsize;
    t_binbuf *b;
    glist_noselect(owner);
    glist_select(owner, z);
    b = glist_writetobinbuf(owner, 0);
    binbuf_gettext(b, &buf, &bufsize);
    binbuf_free(b);
    buf = t_resizebytes(buf, bufsize, bufsize+1);
    buf[bufsize] = 0;
    sprintf(buf2, "pdtk_data_dialog %%s {");
    gfxstub_new((t_pd *)owner, x, buf2);
    sys_gui(buf);
    sys_gui("}\n");
    t_freebytes(buf, bufsize+1);
}

static t_widgetbehavior scalar_widgetbehavior =
{
    scalar_getrect,
    scalar_displace,
    scalar_select,
    scalar_activate,
    scalar_delete,
    scalar_vis,
    scalar_click,
Miller Puckette's avatar
Miller Puckette committed
};

static void scalar_free(t_scalar *x)
{
    t_symbol *templatesym = x->sc_template;
    t_template *template = template_findbyname(templatesym);
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return;
    }
    word_free(x->sc_vec, template);
    sprintf(buf, "x%lx", (long unsigned int)x);
Miller Puckette's avatar
Miller Puckette committed
    gfxstub_deleteforkey(x);
        /* the "size" field in the class is zero, so Pd doesn't try to free
        us automatically (see pd_free()) */
    freebytes(x, sizeof(t_scalar) + (template->t_n - 1) * sizeof(*x->sc_vec));
}

/* ----------------- setup function ------------------- */

void g_scalar_setup(void)
{
    scalar_class = class_new(gensym("scalar"), 0, (t_method)scalar_free, 0,
        CLASS_GOBJ, 0);
    class_addmethod(scalar_class, (t_method)scalar_menuopen,
        gensym("menu-open"), 0);
Miller Puckette's avatar
Miller Puckette committed
    class_setwidget(scalar_class, &scalar_widgetbehavior);
    class_setsavefn(scalar_class, scalar_save);
    class_setpropertiesfn(scalar_class, scalar_properties);
}