g_scalar.c 31.9 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* This file defines the "scalar" object, which is not a text object, just a
"gobj".  Scalars have templates which describe their structures, which
can contain numbers, sublists, and arrays.

*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>      /* for read/write to files */
#include "m_pd.h"
#include "g_canvas.h"

t_class *scalar_class;

void word_init(t_word *wp, t_template *template, t_gpointer *gp)
{
    int i, nitems = template->t_n;
    t_dataslot *datatypes = template->t_vec;
    for (i = 0; i < nitems; i++, datatypes++, wp++)
    {
        int type = datatypes->ds_type;
        if (type == DT_FLOAT)
            wp->w_float = 0; 
        else if (type == DT_SYMBOL)
            wp->w_symbol = &s_symbol;
        else if (type == DT_ARRAY)
        {
            wp->w_array = array_new(datatypes->ds_arraytemplate, gp);
        }
        else if (type == DT_LIST)
        {
                /* LATER test this and get it to work */
            wp->w_list = canvas_new(0, 0, 0, 0);
        }
    }
}

void word_restore(t_word *wp, t_template *template,
    int argc, t_atom *argv)
{
    int i, nitems = template->t_n;
    t_dataslot *datatypes = template->t_vec;
    for (i = 0; i < nitems; i++, datatypes++, wp++)
    {
        int type = datatypes->ds_type;
        if (type == DT_FLOAT)
        {
52
            t_float f;
Miller Puckette's avatar
Miller Puckette committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
            if (argc)
            {
                f =  atom_getfloat(argv);
                argv++, argc--;
            }
            else f = 0;
            wp->w_float = f; 
        }
        else if (type == DT_SYMBOL)
        {
            t_symbol *s;
            if (argc)
            {
                s =  atom_getsymbol(argv);
                argv++, argc--;
            }
            else s = &s_;
            wp->w_symbol = s;
        }
    }
    if (argc)
        post("warning: word_restore: extra arguments");
}

void word_free(t_word *wp, t_template *template)
{
    int i;
    t_dataslot *dt;
    for (dt = template->t_vec, i = 0; i < template->t_n; i++, dt++)
    {
        if (dt->ds_type == DT_ARRAY)
            array_free(wp[i].w_array);
        else if (dt->ds_type == DT_LIST)
            canvas_free(wp[i].w_list);
    }
}

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* some of this code is used in a function in g_canvas.c...
   need to modularize it */
static t_object *template_getstruct(t_template *template)
{
    if (template)
    {
        t_gobj *y;
        t_canvas *c;
        if (c = template_findcanvas(template))
        {
            t_symbol *s1 = gensym("struct");
            for (y = c->gl_list; y; y = y->g_next)
            {
                t_object *ob = pd_checkobject(&y->g_pd);
                t_atom *argv;
                if (!ob || ob->te_type != T_OBJECT ||
                    binbuf_getnatom(ob->te_binbuf) < 2)
                    continue;
                argv = binbuf_getvec(ob->te_binbuf);
                if (argv[0].a_w.w_symbol != s1)
                    continue;
                if (canvas_makebindsym(argv[1].a_w.w_symbol) == template->t_sym)
                    return (ob);
            }
        }
    }
    return (0);
}

int template_hasxy(t_template *template)
{
    t_symbol *zz;
    int xonset, yonset, xtype, ytype, gotx, goty;
    if (!template)
    {
        error("struct: couldn't find template %s", template->t_sym->s_name);
        return 0;
    }
    gotx = template_find_field(template, gensym("x"), &xonset, &xtype, &zz);
    goty = template_find_field(template, gensym("y"), &yonset, &ytype, &zz);
    if ((gotx && (xtype == DT_FLOAT)) &&
        (goty && (ytype == DT_FLOAT)) &&
        (xonset == 0) && (yonset == sizeof(t_word)))
    {
        return 1;
    }
    else
        return 0;
}

140
int template_check_array_fields(t_symbol *structname, t_template *template)
141
{
142
    /* We're calling this from template_cancreate as well as
143
144
145
146
147
148
149
150
151
152
153
154
155
       gtemplate_cancreate. With gtemplate_cancreate, the t_template doesn't
       exist yet. So we send the struct name to see if it matches the name of
       any array field templates that our gtemplate will depend on. If we find
       a match we will refuse to create the gtemplate. However,
       template_cancreate starts from the struct name for a template that
       already exists.  On the first time through this recursive function,
       there isn't a containing structname yet. So we suppress this conditional
       the first time through the recursive loop, and then set the structname
       for all subsequent iterations through the loop.
       
       1 = success
       0 = circular dependency
       -1 = non-existant array elemtemplate found */
156
157
    if (structname && structname == template->t_sym)
    {
158
159
160
161
        t_object *ob = template_getstruct(template);
        pd_error(ob, "%s: circular dependency",
            template->t_sym->s_name);
       return (0);
162
    }
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
163
    int i, nitems = template->t_n;
164
165
166
167
    t_dataslot *datatypes = template->t_vec;
    t_template *elemtemplate;
    for (i = 0; i < nitems; i++, datatypes++)
    {
168
169
170
171
172
173
174
175
        if (datatypes->ds_type == DT_ARRAY)
        {
            elemtemplate = template_findbyname(datatypes->ds_arraytemplate);
            if (!(elemtemplate))
            {
                t_object *ob = template_getstruct(template);
                pd_error(ob, "%s: no such template",
                    datatypes->ds_arraytemplate->s_name);
176
                return (-1);
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
            }
            else if (elemtemplate->t_sym == structname)
            {
                t_object *ob = template_getstruct(template);
                pd_error(ob, "%s: circular dependency",
                    datatypes->ds_arraytemplate->s_name);
                return (0);
            }
            else
            {
                if (!structname)
                {
                    structname = template->t_sym;
                }
                return (template_check_array_fields(structname, elemtemplate));
            }
        }
194
    }
195
    return 1;
196
197
}

198
199
200
int template_cancreate(t_template *template)
{
    /* we send "0" for the structname since there is no container struct */
201
    return (template_check_array_fields(0, template) == 1);
202
203
}

Miller Puckette's avatar
Miller Puckette committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    /* make a new scalar and add to the glist.  We create a "gp" here which
    will be used for array items to point back here.  This gp doesn't do
    reference counting or "validation" updates though; the parent won't go away
    without the contained arrays going away too.  The "gp" is copied out
    by value in the word_init() routine so we can throw our copy away. */

t_scalar *scalar_new(t_glist *owner, t_symbol *templatesym)
{
    t_scalar *x;
    t_template *template;
    t_gpointer gp;
    gpointer_init(&gp);
    template = template_findbyname(templatesym);
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return (0);
    }
222
223
    if (!template_cancreate(template))
        return (0);
Miller Puckette's avatar
Miller Puckette committed
224
225
226
227
228
229
    x = (t_scalar *)getbytes(sizeof(t_scalar) +
        (template->t_n - 1) * sizeof(*x->sc_vec));
    x->sc_gobj.g_pd = scalar_class;
    x->sc_template = templatesym;
    gpointer_setglist(&gp, owner, x);
    word_init(x->sc_vec, template, &gp);
230
231
232
    char buf[50];
    sprintf(buf, ".x%lx", (long unsigned int)x);
    pd_bind(&x->sc_gobj.g_pd, gensym(buf));
Miller Puckette's avatar
Miller Puckette committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
    return (x);
}

    /* Pd method to create a new scalar, add it to a glist, and initialize
    it from the message arguments. */

int glist_readscalar(t_glist *x, int natoms, t_atom *vec,
    int *p_nextmsg, int selectit);

void glist_scalar(t_glist *glist,
    t_symbol *classname, t_int argc, t_atom *argv)
{
    t_symbol *templatesym =
        canvas_makebindsym(atom_getsymbolarg(0, argc, argv));
    t_binbuf *b;
    int natoms, nextmsg = 0;
    t_atom *vec;
    if (!template_findbyname(templatesym))
    {
        pd_error(glist, "%s: no such template",
            atom_getsymbolarg(0, argc, argv)->s_name);
        return;
    }

    b = binbuf_new();
    binbuf_restore(b, argc, argv);
    natoms = binbuf_getnatom(b);
    vec = binbuf_getvec(b);
    
    glist_readscalar(glist, natoms, vec, &nextmsg, 0);
    binbuf_free(b);
}

266
267
268
269
270
271
272
273
    /* search template fields recursively to see if the template
       depends on elemtemplate */
int template_has_elemtemplate(t_template *t, t_template *elemtemplate)
{
    int returnval = 0;
    if (t && elemtemplate)
    {
        int i;
274
        t_dataslot *d = t->t_vec;
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
        for (i = 0; i < t->t_n; i++, d++)
        {
            if (d->ds_type == DT_ARRAY)
            {
                if (d->ds_arraytemplate == elemtemplate->t_sym)
                {
                    returnval = 1;
                    break;
                }
                else
                {
                    returnval = template_has_elemtemplate(
                        template_findbyname(d->ds_arraytemplate),
                        elemtemplate);
                }
            }
        }
    }
    return (returnval);
}

Miller Puckette's avatar
Miller Puckette committed
296
/* -------------------- widget behavior for scalar ------------ */
297
void scalar_getbasexy(t_scalar *x, t_float *basex, t_float *basey)
Miller Puckette's avatar
Miller Puckette committed
298
299
300
301
302
303
{
    t_template *template = template_findbyname(x->sc_template);
    *basex = template_getfloat(template, gensym("x"), x->sc_vec, 0);
    *basey = template_getfloat(template, gensym("y"), x->sc_vec, 0);
}

304
305
extern int array_joc;

306
307
308
extern void template_notifyforscalar(t_template *template, t_glist *owner,
    t_scalar *sc, t_symbol *s, int argc, t_atom *argv);

309
310
t_canvas *sc_mouseover_canvas;
static void scalar_mouseover(t_scalar *x, t_floatarg state)
311
{
312
313
314
315
316
317
318
319
    t_atom at[1];
    t_template *template = template_findbyname(x->sc_template);
    if (state)
        template_notifyforscalar(template, sc_mouseover_canvas,
            x, gensym("leave"), 1, at);
    else
        template_notifyforscalar(template, sc_mouseover_canvas,
            x, gensym("enter"), 1, at);
320
321
}

322
323
324
325
326
327
328
329
static void scalar_getgrouprect(t_glist *owner, t_glist *groupcanvas,
    t_word *data, t_template *template, int basex, int basey,
    int *x1, int *x2, int *y1, int *y2)
{
    t_gobj *y;
    for (y = groupcanvas->gl_list; y; y = y->g_next)
    {
        if (pd_class(&y->g_pd) == canvas_class &&
330
            ((t_canvas *)y)->gl_svg)
331
332
333
        {
            /* todo: accumulate basex and basey for correct offset */
            scalar_getgrouprect(owner, (t_glist *)y, data, template,
334
                basex, basey, x1, x2, y1, y2);
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
        }
        else
        {
            t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
            int nx1, ny1, nx2, ny2;
            if (!wb) continue;
            (*wb->w_parentgetrectfn)(y, owner,
                data, template, basex, basey,
                &nx1, &ny1, &nx2, &ny2);
            if (nx1 < *x1) *x1 = nx1;
            if (ny1 < *y1) *y1 = ny1;
            if (nx2 > *x2) *x2 = nx2;
            if (ny2 > *y2) *y2 = ny2;
            //fprintf(stderr,"====scalar_getrect x1 %d y1 %d x2 %d y2 %d\n",
            //    x1, y1, x2, y2);
        }
    }
}
 
Miller Puckette's avatar
Miller Puckette committed
354
355
356
static void scalar_getrect(t_gobj *z, t_glist *owner,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
357
    //fprintf(stderr,"scalar_getrect %d\n", array_joc);
Miller Puckette's avatar
Miller Puckette committed
358
    t_scalar *x = (t_scalar *)z;
359

Miller Puckette's avatar
Miller Puckette committed
360
361
362
    t_template *template = template_findbyname(x->sc_template);
    t_canvas *templatecanvas = template_findcanvas(template);
    int x1 = 0x7fffffff, x2 = -0x7fffffff, y1 = 0x7fffffff, y2 = -0x7fffffff;
363
    t_float basex, basey;
364

365
366
367
    // EXPERIMENTAL: we assume that entire canvas is within
    // the rectangle--this is for arrays
    // with "jump on click" enabled TODO: test for other regressions
368
    // (there should not be any
369
    // provided the global variable array_joc is properly maintained)
370
371
    if (glist_istoplevel(owner) && array_joc)
    {
372
        x1 = -0x7fffffff, y1 = -0x7fffffff, x2 = 0x7fffffff, y2 = 0x7fffffff;
Miller Puckette's avatar
Miller Puckette committed
373
    }
374

375
376
    else
    {
377
378
379
        scalar_getbasexy(x, &basex, &basey);
            /* if someone deleted the template canvas, we're just a point */
        if (!templatecanvas)
Miller Puckette's avatar
Miller Puckette committed
380
        {
381
            //fprintf(stderr,"...point\n");
382
383
384
385
386
            x1 = x2 = glist_xtopixels(owner, basex);
            y1 = y2 = glist_ytopixels(owner, basey);
        }
        else
        {
387
388
389
390
391
392
393
394
395
            /* todo: bad flow with internal return here. make it cleaner */
            if (x->sc_bboxcache)
            {
                *xp1 = x->sc_x1;
                *yp1 = x->sc_y1;
                *xp2 = x->sc_x2;
                *yp2 = x->sc_y2;
                return;
            }
396
397
            x1 = y1 = 0x7fffffff;
            x2 = y2 = -0x7fffffff;
398
            scalar_getgrouprect(owner, templatecanvas, x->sc_vec, template,
399
400
                basex, basey, &x1, &x2, &y1, &y2);
            if (x2 < x1 || y2 < y1)
401
                x1 = y1 = x2 = y2 = 0;
Miller Puckette's avatar
Miller Puckette committed
402
403
        }
    }
404
405
    //fprintf(stderr,"FINAL scalar_getrect x1 %d y1 %d x2 %d y2 %d\n",
    //    x1, y1, x2, y2);
406
407
408
409
410
    *xp1 = x->sc_x1 = x1;
    *yp1 = x->sc_y1 = y1;
    *xp2 = x->sc_x2 = x2;
    *yp2 = x->sc_y2 = y2; 
    x->sc_bboxcache = 1;
Miller Puckette's avatar
Miller Puckette committed
411
412
}

413
void scalar_drawselectrect(t_scalar *x, t_glist *glist, int state)
Miller Puckette's avatar
Miller Puckette committed
414
{
415
    //fprintf(stderr,"scalar_drawselecterect%d\n", state);
Miller Puckette's avatar
Miller Puckette committed
416
417
418
419
420
421
    if (state)
    {
        int x1, y1, x2, y2;
       
        scalar_getrect(&x->sc_gobj, glist, &x1, &y1, &x2, &y2);
        x1--; x2++; y1--; y2++;
422
423
424
        if (glist_istoplevel(glist))
            sys_vgui(".x%lx.c create prect %d %d %d %d "
                     "-strokewidth 1 -stroke $pd_colors(selection) "
425
                     "-tags {select%lx selected}\n",
426
427
                    glist_getcanvas(glist), x1, y1, x2, y2,
                    x);
Miller Puckette's avatar
Miller Puckette committed
428
429
430
    }
    else
    {
431
432
        if (glist_istoplevel(glist))
            sys_vgui(".x%lx.c delete select%lx\n", glist_getcanvas(glist), x);
Miller Puckette's avatar
Miller Puckette committed
433
434
435
    }
}

436
/* This is a workaround.  Since scalars are contained within a tkpath
437
438
439
440
441
442
443
444
445
446
447
448
   group, and since tkpath groups don't have coords, we can't just use
   the same "selected" tag that is used to move all other Pd objects. That
   would move scalars in their local coordinate system, which is wrong
   for transformed objects. For example, if a rectangle is rotated 45 and
   we try to do a [canvas move 10 0] command on it, it would get moved to
   the northeast instead of to the right!

   Instead, we tag selected scalars with the "scalar_selected" tag. Then in
   the GUI we use that tag to loop through and change each scalar's group
   matrix, and add (dx,dy) to its current translation values. The scalar
   group matrix .scalar%lx isn't accessible by the user, so it will only
   ever contain these translation values.
449
450
*/
void scalar_select(t_gobj *z, t_glist *owner, int state)
Miller Puckette's avatar
Miller Puckette committed
451
{
452
    //fprintf(stderr,"scalar_select %d\n", state);
Miller Puckette's avatar
Miller Puckette committed
453
454
455
456
    t_scalar *x = (t_scalar *)z;
    t_template *tmpl;
    t_symbol *templatesym = x->sc_template;
    t_atom at;
457
    //t_canvas *templatecanvas = NULL;
Miller Puckette's avatar
Miller Puckette committed
458
459
460
461
    t_gpointer gp;
    gpointer_init(&gp);
    gpointer_setglist(&gp, owner, x);
    SETPOINTER(&at, &gp);
462
463
    if (tmpl = template_findbyname(templatesym))
    {
Miller Puckette's avatar
Miller Puckette committed
464
465
        template_notify(tmpl, (state ? gensym("select") : gensym("deselect")),
            1, &at);
466
        //templatecanvas = template_findcanvas(tmpl);
467
    }
Miller Puckette's avatar
Miller Puckette committed
468
    gpointer_unset(&gp);
469
470
    if (state)
    {
471
        x->sc_selected = owner;
472
473
        sys_vgui(".x%lx.c addtag selected withtag blankscalar%lx\n",
            glist_getcanvas(owner), x);
474
475
        sys_vgui(".x%lx.c addtag scalar_selected withtag {.scalar%lx}\n",
            glist_getcanvas(owner), x->sc_vec);
476
477
478
479
480
481
482
483
        if (x->sc_selected != glist_getcanvas(owner))
        {
            // we are inside gop
            sys_vgui(".x%lx.c addtag scalarGOP withtag blankscalar%lx\n",
                glist_getcanvas(owner), x);
            sys_vgui(".x%lx.c addtag scalarGOP withtag {.scalar%lx}\n",
                glist_getcanvas(owner), x->sc_vec);
        }
484
485
486
487
488
    }
    else
    {
        sys_vgui(".x%lx.c dtag blankscalar%lx selected\n",
            glist_getcanvas(owner), x);
489
490
        sys_vgui(".x%lx.c dtag .scalar%lx scalar_selected\n",
            glist_getcanvas(owner), x->sc_vec);
491
492
493
494
495
496
497
498
        if (x->sc_selected != glist_getcanvas(owner))
        {
            sys_vgui(".x%lx.c dtag blankscalar%lx scalarGOP\n",
                    glist_getcanvas(owner), x);
                sys_vgui(".x%lx.c dtag .scalar%lx scalarGOP\n",
                    glist_getcanvas(owner), x->sc_vec);
        }
        x->sc_selected = 0;
499
500
501
    }
    //sys_vgui("pdtk_select_all_gop_widgets .x%lx %lx %d\n",
    //    glist_getcanvas(owner), owner, state);
Miller Puckette's avatar
Miller Puckette committed
502
503
504
505
506
    scalar_drawselectrect(x, owner, state);
}

static void scalar_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
507
    //fprintf(stderr,"scalar_displace\n");
Miller Puckette's avatar
Miller Puckette committed
508
509
510
511
512
513
514
515
516
517
518
519
520
    t_scalar *x = (t_scalar *)z;
    t_symbol *templatesym = x->sc_template;
    t_template *template = template_findbyname(templatesym);
    t_symbol *zz;
    t_atom at[3];
    t_gpointer gp;
    int xonset, yonset, xtype, ytype, gotx, goty;
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return;
    }
    gotx = template_find_field(template, gensym("x"), &xonset, &xtype, &zz);
521
    if ((gotx && (xtype != DT_FLOAT)) || x->sc_selected != glist)
Miller Puckette's avatar
Miller Puckette committed
522
523
        gotx = 0;
    goty = template_find_field(template, gensym("y"), &yonset, &ytype, &zz);
524
    if ((goty && (ytype != DT_FLOAT)) || x->sc_selected != glist)
Miller Puckette's avatar
Miller Puckette committed
525
526
        goty = 0;
    if (gotx)
527
    {
Miller Puckette's avatar
Miller Puckette committed
528
529
        *(t_float *)(((char *)(x->sc_vec)) + xonset) +=
            dx * (glist_pixelstox(glist, 1) - glist_pixelstox(glist, 0));
530
531
532
        x->sc_x1 += dx;
        x->sc_x2 += dx;
    }
Miller Puckette's avatar
Miller Puckette committed
533
    if (goty)
534
    {
Miller Puckette's avatar
Miller Puckette committed
535
536
        *(t_float *)(((char *)(x->sc_vec)) + yonset) +=
            dy * (glist_pixelstoy(glist, 1) - glist_pixelstoy(glist, 0));
537
538
539
        x->sc_y1 += dy;
        x->sc_y2 += dy;
    }
Miller Puckette's avatar
Miller Puckette committed
540
541
542
    gpointer_init(&gp);
    gpointer_setglist(&gp, glist, x);
    SETPOINTER(&at[0], &gp);
543
544
    SETFLOAT(&at[1], (t_float)dx);
    SETFLOAT(&at[2], (t_float)dy);
Miller Puckette's avatar
Miller Puckette committed
545
546
547
548
    template_notify(template, gensym("displace"), 2, at);
    scalar_redraw(x, glist);
}

549
/* Kind of complicated at the moment. If a scalar is in a gop canvas, then
550
   we don't need to update its x/y fields (if it even has them) when displacing
551
552
553
   it.  Otherwise we do.  The member sc_selected is used to store the canvas
   where the selection exists-- if it matches the glist parameter below then we
   know the scalar is directly selected.  If not it's in a gop canvas.
554
*/
555
556
static void scalar_displace_withtag(t_gobj *z, t_glist *glist, int dx, int dy)
{
557
    //fprintf(stderr,"scalar_displace_withtag %lx %d %d\n", (t_int)z, dx, dy);
558
559
560
561
562
563
564
    t_scalar *x = (t_scalar *)z;
    t_symbol *templatesym = x->sc_template;
    t_template *template = template_findbyname(templatesym);
    t_symbol *zz;
    t_atom at[3];
    t_gpointer gp;
    int xonset, yonset, xtype, ytype, gotx, goty;
565
    t_float basex = 0, basey = 0;
566
567
568
569
570
571
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return;
    }
    gotx = template_find_field(template, gensym("x"), &xonset, &xtype, &zz);
572
    if ((gotx && (xtype != DT_FLOAT)) || x->sc_selected != glist)
573
574
        gotx = 0;
    goty = template_find_field(template, gensym("y"), &yonset, &ytype, &zz);
575
    if ((goty && (ytype != DT_FLOAT)) || x->sc_selected != glist)
576
577
        goty = 0;
    if (gotx)
578
    {
579
580
        *(t_float *)(((char *)(x->sc_vec)) + xonset) +=
            dx * (glist_pixelstox(glist, 1) - glist_pixelstox(glist, 0));
581
582
583
        x->sc_x1 += dx;
        x->sc_x2 += dx;
    }
584
    if (goty)
585
    {
586
587
        *(t_float *)(((char *)(x->sc_vec)) + yonset) +=
            dy * (glist_pixelstoy(glist, 1) - glist_pixelstoy(glist, 0));
588
589
590
        x->sc_y1 += dy;
        x->sc_y2 += dy;
    }
Ivica Bukvic's avatar
Ivica Bukvic committed
591
    //fprintf(stderr,"gotx=%d goty=%d\n", gotx, goty);
592
    scalar_getbasexy(x, &basex, &basey);
593
594
595
596
597
598
    gpointer_init(&gp);
    gpointer_setglist(&gp, glist, x);
    SETPOINTER(&at[0], &gp);
    SETFLOAT(&at[1], (t_float)dx);
    SETFLOAT(&at[2], (t_float)dy);
    template_notify(template, gensym("displace"), 2, at);
599

600
    canvas_getscroll(glist);
601
     
602
603
    /* Apparently this is no longer needed, so it is commented out.  But if
       we merge garrays back into this code we may need it... */
604
    /*
605
606
607
608
609
610
611
612
613
614
615
616
    if (template->t_sym != gensym("_float_array"))
    {
        t_gobj *y;
        t_canvas *templatecanvas = template_findcanvas(template);
        for (y = templatecanvas->gl_list; y; y = y->g_next)
            {
                t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
                if (!wb) continue;
                (*wb->w_parentdisplacefn)(y, glist, x->sc_vec, template,
                    basex, basey, dx, dy);
            }
    }
617
    */
618

619
620
621
    //scalar_redraw(x, glist);
}

Miller Puckette's avatar
Miller Puckette committed
622
623
624
625
626
627
628
629
630
631
632
static void scalar_activate(t_gobj *z, t_glist *owner, int state)
{
    /* post("scalar_activate %d", state); */
    /* later */
}

static void scalar_delete(t_gobj *z, t_glist *glist)
{
    /* nothing to do */
}

633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
extern void svg_grouptogui(t_glist *g, t_template *template, t_word *data);

static void scalar_groupvis(t_scalar *x, t_glist *owner, t_template *template,
    t_glist *gl, t_glist *parent, int vis)
{
    t_gobj *y;
    if (vis)
    {
        sys_vgui(".x%lx.c create group -tags {.dgroup%lx.%lx} "
                 "-parent {.dgroup%lx.%lx}\\\n",
            glist_getcanvas(owner), gl, x->sc_vec,
            parent, x->sc_vec);
        svg_grouptogui(gl, template, x->sc_vec);
        sys_gui("\n");

    }
    for (y = gl->gl_list; y; y = y->g_next)
    {
        if (pd_class(&y->g_pd) == canvas_class &&
652
            ((t_glist *)y)->gl_svg)
653
        {
654
            scalar_groupvis(x, owner, template, (t_glist *)y, gl, vis);
655
656
657
658
659
660
661
662
        }
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
        if (!wb) continue;
        (*wb->w_parentvisfn)(y, owner, gl, x, x->sc_vec, template,
            0, 0, vis);
    }
}

663
664
/* At present, scalars have a three-level hierarchy in tkpath,
   with two levels accessible by the user from within Pd:
665
666
   scalar - ".scalar%lx", x->sc_vec
     |      tkpath group with matrix derived from x/y fields,
667
668
669
670
     |      gop basexy, and gop scaling values. This group is
     |      not configurable by the user. This means that the
     |      [draw group] below can ignore basexy and gop junk
     |      when computing the transform matrix.
671
     v
672
673
674
675
676
   group  - ".dgroup%lx.%lx", templatecanvas, x->sc_vec
     |      group used as parent for all the toplevel drawing
     |      commands of the scalar (i.e., the ones located on
     |      the same canvas as the [struct]).  Its matrix and
     |      options aren't yet accessible by the user.
677
     v
678
679
   (draw  - ".draw%lx.%lx", (t_draw *ptr), x->sc_vec
     |      the actual drawing command: rectangle, path, etc.
680
681
682
683
684
685
686
     or     Each has its own matrix and options which can set
   scelem   with messages to the corresponding [draw] object.
     or   - ds arrays can nest arbitrarily deep. Scelem is for
   group)   data structure arrays.  group is for more groups.
     |
     v
    etc.
687
688
689
690

   The tag "blankscalar" is for scalars that don't have a visual
   representation, but maybe this can just be merged with "scalar"
*/
Miller Puckette's avatar
Miller Puckette committed
691
692
static void scalar_vis(t_gobj *z, t_glist *owner, int vis)
{
693
    //fprintf(stderr,"scalar_vis %d %lx\n", vis, (t_int)z);
Miller Puckette's avatar
Miller Puckette committed
694
    t_scalar *x = (t_scalar *)z;
695
696
697

    x->sc_bboxcache = 0;

Miller Puckette's avatar
Miller Puckette committed
698
699
700
    t_template *template = template_findbyname(x->sc_template);
    t_canvas *templatecanvas = template_findcanvas(template);
    t_gobj *y;
701
    t_float basex, basey;
Miller Puckette's avatar
Miller Puckette committed
702
703
704
705
706
707
708
709
    scalar_getbasexy(x, &basex, &basey);
        /* if we don't know how to draw it, make a small rectangle */
    if (!templatecanvas)
    {
        if (vis)
        {
            int x1 = glist_xtopixels(owner, basex);
            int y1 = glist_ytopixels(owner, basey);
710
            sys_vgui(".x%lx.c create prect %d %d %d %d "
711
712
713
714
                     "-tags {blankscalar%lx %s}\n",
                glist_getcanvas(owner), x1-1, y1-1, x1+1, y1+1, x,
                (glist_isselected(owner, &x->sc_gobj) ?
                    "scalar_selected" : ""));
Miller Puckette's avatar
Miller Puckette committed
715
        }
716
717
        else sys_vgui(".x%lx.c delete blankscalar%lx\n",
            glist_getcanvas(owner), x);
Miller Puckette's avatar
Miller Puckette committed
718
719
        return;
    }
720
721
    //else sys_vgui(".x%lx.c delete blankscalar%lx\n",
    //    glist_getcanvas(owner), x);
722
723
724
725
726

    if (vis)
    {
        t_float xscale = glist_xtopixels(owner, 1) - glist_xtopixels(owner, 0);
        t_float yscale = glist_ytopixels(owner, 1) - glist_ytopixels(owner, 0);
727
728
729
730
731
732
733
734
        /* we translate the .scalar%lx group to displace it on the tk side.
           This is the outermost group for the scalar, something like a
           poor man's viewport.
           Also, the default stroke is supposed to be "none" and default
           fill is supposed to be black. Unfortunately tkpath does the
           opposite.  To fix this, we set the correct fill/stroke options
           here on the .scalar%lx group. Notice also that tkpath doesn't
           understand "None"-- instead we must send an empty symbol. */
735
        sys_vgui(".x%lx.c create group -tags {.scalar%lx %s} "
736
            "-matrix { {%g %g} {%g %g} {%d %d} } -stroke \"\" -fill black\n",
737
            glist_getcanvas(owner), x->sc_vec,
738
            (glist_isselected(owner, &x->sc_gobj) ? "scalar_selected" : ""),
739
740
            xscale, 0.0, 0.0, yscale, (int)glist_xtopixels(owner, basex),
            (int)glist_ytopixels(owner, basey)
741
            );
742
        sys_vgui(".x%lx.c create group -tags {.dgroup%lx.%lx} "
743
                 "-parent {.scalar%lx}\n",
744
            glist_getcanvas(owner), templatecanvas, x->sc_vec, x->sc_vec);
745
746
        sys_vgui("pdtk_bind_scalar_mouseover "
                 ".x%lx.c .x%lx.x%lx.template%lx {.x%lx}\n",
747
748
            glist_getcanvas(owner), glist_getcanvas(owner),
            owner, x->sc_vec, x);
749
    }
Miller Puckette's avatar
Miller Puckette committed
750

751
    /* warning: don't need--- have recursive func. */
Miller Puckette's avatar
Miller Puckette committed
752
753
754
    for (y = templatecanvas->gl_list; y; y = y->g_next)
    {
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
755
756
        if (!wb)
        {
757
            /* check subpatches for more drawing commands.  This
758
759
               can be optimized to only search [group] subpatches */
            if (pd_class(&y->g_pd) == canvas_class &&
760
761
                ((t_glist *)y)->gl_svg)
            {
762
763
                scalar_groupvis(x, owner, template,
                    (t_glist *)y, templatecanvas, vis);
764
            }
765
766
767
            continue;
        }
        (*wb->w_parentvisfn)(y, owner, 0, x, x->sc_vec, template,
768
            basex, basey, vis);
Miller Puckette's avatar
Miller Puckette committed
769
    }
770
    if (!vis)
771
772
        sys_vgui(".x%lx.c delete .scalar%lx\n", glist_getcanvas(owner),
            x->sc_vec);
773

774
    sys_unqueuegui(x);
Miller Puckette's avatar
Miller Puckette committed
775
776
    if (glist_isselected(owner, &x->sc_gobj))
    {
777
778
        // we removed this because it caused infinite recursion
        // in the scalar-help.pd example
779
        //scalar_select(z, owner, 1);
Miller Puckette's avatar
Miller Puckette committed
780
781
782
783
784
785
786
787
788
        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);
789
790
791
792
793
794
    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);
    }
795
    canvas_getscroll(glist_getcanvas(glist));
Miller Puckette's avatar
Miller Puckette committed
796
797
798
799
800
}

void scalar_redraw(t_scalar *x, t_glist *glist)
{
    if (glist_isvisible(glist))
801
        scalar_doredraw((t_gobj *)x, glist);
802
        //sys_queuegui(x, glist, scalar_doredraw);
Miller Puckette's avatar
Miller Puckette committed
803
804
}

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
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
837
838
int scalar_doclick(t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
839
    t_float xloc, t_float yloc, int xpix, int ypix,
Miller Puckette's avatar
Miller Puckette committed
840
841
842
843
    int shift, int alt, int dbl, int doit)
{
    int hit = 0;
    t_canvas *templatecanvas = template_findcanvas(template);
844
    t_atom at[2];
845
846
    t_float basex = template_getfloat(template, gensym("x"), data, 0);
    t_float basey = template_getfloat(template, gensym("y"), data, 0);
847
848
    //fprintf(stderr,"=================scalar_doclick %f %f %f %f %d\n",
    //    basex, basey, xloc, yloc, doit);
Ivica Bukvic's avatar
Ivica Bukvic committed
849

850
851
852
853
854
    SETFLOAT(at, basex + xloc);
    SETFLOAT(at+1, basey + yloc);
    if (doit)
    {
        //fprintf(stderr,"    doit\n");
855
856
        template_notifyforscalar(template, owner, 
            sc, gensym("click"), 2, at);
857
    }
Ivica Bukvic's avatar
Ivica Bukvic committed
858

859
860
861
862
863
864
865
866
    // 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");
        basex = 0.0;
        basey = 0.0;
    }
Ivica Bukvic's avatar
Ivica Bukvic committed
867

868
869
870
871
    hit = scalar_groupclick(templatecanvas, data, template, sc, ap,
                owner, xloc, yloc, xpix, ypix,
                shift, alt, dbl, doit, basex, basey);
    return hit;
Miller Puckette's avatar
Miller Puckette committed
872
873
}

874
875
876
/* 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
877
878
879
static int scalar_click(t_gobj *z, struct _glist *owner,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
880
    //fprintf(stderr,"scalar_click %d %d\n", xpix, ypix);
Miller Puckette's avatar
Miller Puckette committed
881
    t_scalar *x = (t_scalar *)z;
882

883
884
885
    x->sc_bboxcache = 0;

    t_template *template = template_findbyname(x->sc_template);
886

Miller Puckette's avatar
Miller Puckette committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
    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_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,
934
    scalar_displace_withtag,
Miller Puckette's avatar
Miller Puckette committed
935
936
937
938
939
940
941
942
943
944
945
946
};

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);
947
948
949
    char buf[50];
    sprintf(buf, ".x%lx", (long unsigned int)x);
    pd_unbind(&x->sc_gobj.g_pd, gensym(buf));
Miller Puckette's avatar
Miller Puckette committed
950
951
952
953
954
955
956
957
958
959
960
961
    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);
962
963
    class_addmethod(scalar_class, (t_method)scalar_mouseover,
        gensym("mouseover"), A_FLOAT, A_NULL);
Miller Puckette's avatar
Miller Puckette committed
964
965
966
967
    class_setwidget(scalar_class, &scalar_widgetbehavior);
    class_setsavefn(scalar_class, scalar_save);
    class_setpropertiesfn(scalar_class, scalar_properties);
}