g_template.c 258 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
/* 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.  */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
8
#include <math.h>  /* for matrix transforms */
Miller Puckette's avatar
Miller Puckette committed
9
10
11
12
13
14

#include "m_pd.h"
#include "s_stuff.h"    /* for sys_hostfontsize */
#include "g_canvas.h"

void array_redraw(t_array *a, t_glist *glist);
15
16
void graph_graphrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2);
Miller Puckette's avatar
Miller Puckette committed
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
/*
This file contains text objects you would put in a canvas to define a
template.  Templates describe objects of type "array" (g_array.c) and
"scalar" (g_scalar.c).
*/

    /* the structure of a "struct" object (also the obsolete "gtemplate"
    you get when using the name "template" in a box.) */

struct _gtemplate
{
    t_object x_obj;
    t_template *x_template;
    t_canvas *x_owner;
    t_symbol *x_sym;
    struct _gtemplate *x_next;
    int x_argc;
    t_atom *x_argv;
};

/* ---------------- forward definitions ---------------- */

static void template_conformarray(t_template *tfrom, t_template *tto,
    int *conformaction, t_array *a);
static void template_conformglist(t_template *tfrom, t_template *tto,
    t_glist *glist,  int *conformaction);
43
t_canvas *canvas_templatecanvas_forgroup(t_canvas *c);
44
static int drawimage_getindex(void *z, t_template *template, t_word *data);
Miller Puckette's avatar
Miller Puckette committed
45
46
47

/* ---------------------- storage ------------------------- */

48
t_class *gtemplate_class;
Miller Puckette's avatar
Miller Puckette committed
49
50
51
52
53
54
55
56
57
58
59
60
static t_class *template_class;

/* there's a pre-defined "float" template.  LATER should we bind this
to a symbol such as "pd-float"??? */

    /* return true if two dataslot definitions match */
static int dataslot_matches(t_dataslot *ds1, t_dataslot *ds2,
    int nametoo)
{
    return ((!nametoo || ds1->ds_name == ds2->ds_name) &&
        ds1->ds_type == ds2->ds_type &&
            (ds1->ds_type != DT_ARRAY ||
61
                ds1->ds_fieldtemplate == ds2->ds_fieldtemplate));
Miller Puckette's avatar
Miller Puckette committed
62
63
64
65
66
67
68
}

/* -- templates, the active ingredient in gtemplates defined below. ------- */

t_template *template_new(t_symbol *templatesym, int argc, t_atom *argv)
{
    t_template *x = (t_template *)pd_new(template_class);
69
    //fprintf(stderr,"template_new %lx\n", x);
Miller Puckette's avatar
Miller Puckette committed
70
    x->t_n = 0;
Ivica Bukvic's avatar
Ivica Bukvic committed
71
    x->t_transformable = 0;
Miller Puckette's avatar
Miller Puckette committed
72
73
74
75
76
    x->t_vec = (t_dataslot *)t_getbytes(0);
    while (argc > 0)
    {
        int newtype, oldn, newn;
        t_symbol *newname, *newarraytemplate = &s_, *newtypesym;
77
        t_binbuf *newbinbuf = NULL;
Miller Puckette's avatar
Miller Puckette committed
78
79
80
81
82
83
84
85
86
        if (argc < 2 || argv[0].a_type != A_SYMBOL ||
            argv[1].a_type != A_SYMBOL)
                goto bad;
        newtypesym = argv[0].a_w.w_symbol;
        newname = argv[1].a_w.w_symbol;
        if (newtypesym == &s_float)
            newtype = DT_FLOAT;
        else if (newtypesym == &s_symbol)
            newtype = DT_SYMBOL;
87
88
        //else if (newtypesym == &s_list)
        //    newtype = DT_LIST;
89
90
        else if (newtypesym == gensym("canvas"))
        {
91
            char filename[MAXPDSTRING+3];
92
93
94
95
96
97
            t_binbuf *b = binbuf_new();
            if (argc < 3 || argv[2].a_type != A_SYMBOL)
            {
                pd_error(x, "canvas lacks template or name");
                goto bad;
            }
98
99
            /* If we keep the canvas field API this needs to be revisited */
            //filename = canvas_makebindsym(argv[2].a_w.w_symbol);
100
101
102
            sprintf(filename, "%s.pd", argv[2].a_w.w_symbol->s_name);
            if (binbuf_read_via_canvas(b, filename, canvas_getcurrent(), 0))
                post("warning: abstraction %s not found", filename);
103
104
            else
                newbinbuf = b;
Miller Puckette's avatar
Miller Puckette committed
105
            newtype = DT_LIST;
106
107
108
            argc--;
            argv++;
        }
Miller Puckette's avatar
Miller Puckette committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        else if (newtypesym == gensym("array"))
        {
            if (argc < 3 || argv[2].a_type != A_SYMBOL)
            {
                pd_error(x, "array lacks element template or name");
                goto bad;
            }
            newarraytemplate = canvas_makebindsym(argv[2].a_w.w_symbol);
            newtype = DT_ARRAY;
            argc--;
            argv++;
        }
        else
        {
            pd_error(x, "%s: no such type", newtypesym->s_name);
            goto bad;
        }
        newn = (oldn = x->t_n) + 1;
        x->t_vec = (t_dataslot *)t_resizebytes(x->t_vec,
            oldn * sizeof(*x->t_vec), newn * sizeof(*x->t_vec));
        x->t_n = newn;
        x->t_vec[oldn].ds_type = newtype;
        x->t_vec[oldn].ds_name = newname;
132
        x->t_vec[oldn].ds_fieldtemplate = newarraytemplate;
133
        x->t_vec[oldn].ds_binbuf = newbinbuf;
Miller Puckette's avatar
Miller Puckette committed
134
135
136
    bad: 
        argc -= 2; argv += 2;
    }
137
    if (*templatesym->s_name)
Miller Puckette's avatar
Miller Puckette committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    {
        x->t_sym = templatesym;
        pd_bind(&x->t_pdobj, x->t_sym);
    }
    else x->t_sym = templatesym;
    return (x);
}

int template_size(t_template *x)
{
    return (x->t_n * sizeof(t_word));
}

int template_find_field(t_template *x, t_symbol *name, int *p_onset,
    int *p_type, t_symbol **p_arraytype)
{
    int i, n;
    if (!x)
    {
        bug("template_find_field");
        return (0);
    }
    n = x->t_n;
    for (i = 0; i < n; i++)
        if (x->t_vec[i].ds_name == name)
    {
        *p_onset = i * sizeof(t_word);
        *p_type = x->t_vec[i].ds_type;
166
        *p_arraytype = x->t_vec[i].ds_fieldtemplate;
Miller Puckette's avatar
Miller Puckette committed
167
168
169
170
171
172
173
174
175
176
        return (1);
    }
    return (0);
}

t_float template_getfloat(t_template *x, t_symbol *fieldname, t_word *wp,
    int loud)
{
    int onset, type;
    t_symbol *arraytype;
177
    t_float val = 0;
Miller Puckette's avatar
Miller Puckette committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    if (template_find_field(x, fieldname, &onset, &type, &arraytype))
    {
        if (type == DT_FLOAT)
            val = *(t_float *)(((char *)wp) + onset);
        else if (loud) error("%s.%s: not a number",
            x->t_sym->s_name, fieldname->s_name);
    }
    else if (loud) error("%s.%s: no such field",
        x->t_sym->s_name, fieldname->s_name);
    return (val);
}

void template_setfloat(t_template *x, t_symbol *fieldname, t_word *wp, 
    t_float f, int loud)
{
    int onset, type;
    t_symbol *arraytype;
    if (template_find_field(x, fieldname, &onset, &type, &arraytype))
     {
        if (type == DT_FLOAT)
            *(t_float *)(((char *)wp) + onset) = f;
        else if (loud) error("%s.%s: not a number",
            x->t_sym->s_name, fieldname->s_name);
    }
    else if (loud) error("%s.%s: no such field",
        x->t_sym->s_name, fieldname->s_name);
}

t_symbol *template_getsymbol(t_template *x, t_symbol *fieldname, t_word *wp,
    int loud)
{
    int onset, type;
    t_symbol *arraytype;
    t_symbol *val = &s_;
    if (template_find_field(x, fieldname, &onset, &type, &arraytype))
    {
        if (type == DT_SYMBOL)
            val = *(t_symbol **)(((char *)wp) + onset);
        else if (loud) error("%s.%s: not a symbol",
            x->t_sym->s_name, fieldname->s_name);
    }
    else if (loud) error("%s.%s: no such field",
        x->t_sym->s_name, fieldname->s_name);
    return (val);
}

void template_setsymbol(t_template *x, t_symbol *fieldname, t_word *wp, 
    t_symbol *s, int loud)
{
    int onset, type;
    t_symbol *arraytype;
    if (template_find_field(x, fieldname, &onset, &type, &arraytype))
     {
        if (type == DT_SYMBOL)
            *(t_symbol **)(((char *)wp) + onset) = s;
        else if (loud) error("%s.%s: not a symbol",
            x->t_sym->s_name, fieldname->s_name);
    }
    else if (loud) error("%s.%s: no such field",
        x->t_sym->s_name, fieldname->s_name);
}

    /* stringent check to see if a "saved" template, x2, matches the current
        one (x1).  It's OK if x1 has additional scalar elements but not (yet)
        arrays or lists.  This is used for reading in "data files". */
int template_match(t_template *x1, t_template *x2)
{
    int i;
    if (x1->t_n < x2->t_n)
        return (0);
    for (i = x2->t_n; i < x1->t_n; i++)
    {
        if (x1->t_vec[i].ds_type == DT_ARRAY || 
            x1->t_vec[i].ds_type == DT_LIST)
                return (0);
    }
Ivica Bukvic's avatar
Ivica Bukvic committed
254
255
    //if (x2->t_n > x1->t_n)
    //    post("add elements...");
Miller Puckette's avatar
Miller Puckette committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    for (i = 0; i < x2->t_n; i++)
        if (!dataslot_matches(&x1->t_vec[i], &x2->t_vec[i], 1))
            return (0);
    return (1);
}

/* --------------- CONFORMING TO CHANGES IN A TEMPLATE ------------ */

/* the following routines handle updating scalars to agree with changes
in their template.  The old template is assumed to be the "installed" one
so we can delete old items; but making new ones we have to avoid scalar_new
which would make an old one whereas we will want a new one (but whose array
elements might still be old ones.)
    LATER deal with graphics updates too... */

    /* conform the word vector of a scalar to the new template */    
static void template_conformwords(t_template *tfrom, t_template *tto,
    int *conformaction, t_word *wfrom, t_word *wto)
{
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
275
    int nto = tto->t_n, i;
Miller Puckette's avatar
Miller Puckette committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    for (i = 0; i < nto; i++)
    {
        if (conformaction[i] >= 0)
        {
                /* we swap the two, in case it's an array or list, so that
                when "wfrom" is deleted the old one gets cleaned up. */
            t_word wwas = wto[i];
            wto[i] = wfrom[conformaction[i]];
            wfrom[conformaction[i]] = wwas;
        }
    }
}

    /* conform a scalar, recursively conforming sublists and arrays  */
static t_scalar *template_conformscalar(t_template *tfrom, t_template *tto,
    int *conformaction, t_glist *glist, t_scalar *scfrom)
{
    t_scalar *x;
    t_gpointer gp;
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
295
    int i;
Miller Puckette's avatar
Miller Puckette committed
296
297
298
299
300
301
302
303
304
305
306
    t_template *scalartemplate;
    /* post("conform scalar"); */
        /* possibly replace the scalar */
    if (scfrom->sc_template == tfrom->t_sym)
    {
            /* see scalar_new() for comment about the gpointer. */
        gpointer_init(&gp);
        x = (t_scalar *)getbytes(sizeof(t_scalar) +
            (tto->t_n - 1) * sizeof(*x->sc_vec));
        x->sc_gobj.g_pd = scalar_class;
        x->sc_template = tfrom->t_sym;
307
        gpointer_setglist(&gp, glist, &x->sc_gobj);
Miller Puckette's avatar
Miller Puckette committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
            /* Here we initialize to the new template, but array and list
            elements will still belong to old template. */
        word_init(x->sc_vec, tto, &gp);

        template_conformwords(tfrom, tto, conformaction,
            scfrom->sc_vec, x->sc_vec);
            
            /* replace the old one with the new one in the list */
        if (glist->gl_list == &scfrom->sc_gobj)
        {
            glist->gl_list = &x->sc_gobj;
            x->sc_gobj.g_next = scfrom->sc_gobj.g_next;
        }
        else
        {
            t_gobj *y, *y2;
            for (y = glist->gl_list; y2 = y->g_next; y = y2)
                if (y2 == &scfrom->sc_gobj)
            {
                x->sc_gobj.g_next = y2->g_next;
                y->g_next = &x->sc_gobj;
                goto nobug;
            }
            bug("template_conformscalar");
        nobug: ;
        }
            /* burn the old one */
        pd_free(&scfrom->sc_gobj.g_pd);
        scalartemplate = tto;
    }
    else
    {
        x = scfrom;
        scalartemplate = template_findbyname(x->sc_template);
    }
        /* convert all array elements and sublists */
    for (i = 0; i < scalartemplate->t_n; i++)
    {
        t_dataslot *ds = scalartemplate->t_vec + i;
        if (ds->ds_type == DT_LIST)
        {
            t_glist *gl2 = x->sc_vec[i].w_list;
            template_conformglist(tfrom, tto, gl2, conformaction);
        }
        else if (ds->ds_type == DT_ARRAY)
        {
354
            t_symbol *arraytemplate = ds->ds_fieldtemplate;
355
356
357
358
359
            if (arraytemplate == tfrom->t_sym ||
                arraytemplate == tto->t_sym)
            {
                return 0;
            }
Miller Puckette's avatar
Miller Puckette committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
            template_conformarray(tfrom, tto, conformaction, 
                x->sc_vec[i].w_array);
        }
    }
    return (x);
}

    /* conform an array, recursively conforming sublists and arrays  */
static void template_conformarray(t_template *tfrom, t_template *tto,
    int *conformaction, t_array *a)
{
    int i, j;
    t_template *scalartemplate = 0;
    if (a->a_templatesym == tfrom->t_sym)
    {
        /* the array elements must all be conformed */
        int oldelemsize = sizeof(t_word) * tfrom->t_n,
            newelemsize = sizeof(t_word) * tto->t_n;
        char *newarray = getbytes(newelemsize * a->a_n);
        char *oldarray = a->a_vec;
        if (a->a_elemsize != oldelemsize)
            bug("template_conformarray");
        for (i = 0; i < a->a_n; i++)
        {
            t_word *wp = (t_word *)(newarray + newelemsize * i);
            word_init(wp, tto, &a->a_gp);
            template_conformwords(tfrom, tto, conformaction,
                (t_word *)(oldarray + oldelemsize * i), wp);
            word_free((t_word *)(oldarray + oldelemsize * i), tfrom);
        }
        scalartemplate = tto;
        a->a_vec = newarray;
        freebytes(oldarray, oldelemsize * a->a_n);
    }
    else scalartemplate = template_findbyname(a->a_templatesym);
        /* convert all arrays and sublist fields in each element of the array */
    for (i = 0; i < a->a_n; i++)
    {
        t_word *wp = (t_word *)(a->a_vec + sizeof(t_word) * a->a_n * i);
        for (j = 0; j < scalartemplate->t_n; j++)
        {
            t_dataslot *ds = scalartemplate->t_vec + j;
            if (ds->ds_type == DT_LIST)
            {
                t_glist *gl2 = wp[j].w_list;
                template_conformglist(tfrom, tto, gl2, conformaction);
            }
            else if (ds->ds_type == DT_ARRAY)
            {
                template_conformarray(tfrom, tto, conformaction, 
                    wp[j].w_array);
            }
        }
    }
}

    /* this routine searches for every scalar in the glist that belongs
    to the "from" template and makes it belong to the "to" template.  Descend
    glists recursively.
    We don't handle redrawing here; this is to be filled in LATER... */

t_array *garray_getarray(t_garray *x);

static void template_conformglist(t_template *tfrom, t_template *tto,
    t_glist *glist,  int *conformaction)
{
    t_gobj *g;
    /* post("conform glist %s", glist->gl_name->s_name); */
    for (g = glist->gl_list; g; g = g->g_next)
    {
        if (pd_class(&g->g_pd) == scalar_class)
            g = &template_conformscalar(tfrom, tto, conformaction,
                glist, (t_scalar *)g)->sc_gobj;
        else if (pd_class(&g->g_pd) == canvas_class)
            template_conformglist(tfrom, tto, (t_glist *)g, conformaction);
        else if (pd_class(&g->g_pd) == garray_class)
            template_conformarray(tfrom, tto, conformaction,
                garray_getarray((t_garray *)g));
    }
}

    /* globally conform all scalars from one template to another */ 
void template_conform(t_template *tfrom, t_template *tto)
{
    int nto = tto->t_n, nfrom = tfrom->t_n, i, j,
        *conformaction = (int *)getbytes(sizeof(int) * nto),
        *conformedfrom = (int *)getbytes(sizeof(int) * nfrom), doit = 0;
    for (i = 0; i < nto; i++)
        conformaction[i] = -1;
    for (i = 0; i < nfrom; i++)
        conformedfrom[i] = 0;
    for (i = 0; i < nto; i++)
    {
        t_dataslot *dataslot = &tto->t_vec[i];
        for (j = 0; j < nfrom; j++)
        {
            t_dataslot *dataslot2 = &tfrom->t_vec[j];
            if (dataslot_matches(dataslot, dataslot2, 1))
            {
                conformaction[i] = j;
                conformedfrom[j] = 1;
            }
        }
    }
    for (i = 0; i < nto; i++)
        if (conformaction[i] < 0)
    {
        t_dataslot *dataslot = &tto->t_vec[i];
        for (j = 0; j < nfrom; j++)
            if (!conformedfrom[j] &&
                dataslot_matches(dataslot, &tfrom->t_vec[j], 0))
        {
            conformaction[i] = j;
            conformedfrom[j] = 1;
        }
    }
    if (nto != nfrom)
        doit = 1;
    else for (i = 0; i < nto; i++)
        if (conformaction[i] != i)
            doit = 1;

    if (doit)
    {
        t_glist *gl;
        post("conforming template '%s' to new structure",
            tfrom->t_sym->s_name);
Ivica Bukvic's avatar
Ivica Bukvic committed
487
488
        //for (i = 0; i < nto; i++)
        //    post("... %d", conformaction[i]);
Miller Puckette's avatar
Miller Puckette committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
        for (gl = canvas_list; gl; gl = gl->gl_next)
            template_conformglist(tfrom, tto, gl, conformaction);
    }
    freebytes(conformaction, sizeof(int) * nto);
    freebytes(conformedfrom, sizeof(int) * nfrom);
}

t_template *template_findbyname(t_symbol *s)
{
    return ((t_template *)pd_findbyclass(s, template_class));
}

t_canvas *template_findcanvas(t_template *template)
{
    t_gtemplate *gt;
504
505
    if (!template)
    {
Miller Puckette's avatar
Miller Puckette committed
506
        bug("template_findcanvas");
507
508
        return (0);
    }
Miller Puckette's avatar
Miller Puckette committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
    if (!(gt = template->t_list))
        return (0);
    return (gt->x_owner);
    /* return ((t_canvas *)pd_findbyclass(template->t_sym, canvas_class)); */
}

void template_notify(t_template *template, t_symbol *s, int argc, t_atom *argv)
{
    if (template->t_list)
        outlet_anything(template->t_list->x_obj.ob_outlet, s, argc, argv);
}

    /* bash the first of (argv) with a pointer to a scalar, and send on
    to template as a notification message */
523
void template_notifyforscalar(t_template *template, t_glist *owner,
Miller Puckette's avatar
Miller Puckette committed
524
525
526
527
    t_scalar *sc, t_symbol *s, int argc, t_atom *argv)
{
    t_gpointer gp;
    gpointer_init(&gp);
528
    gpointer_setglist(&gp, owner, &sc->sc_gobj);
Miller Puckette's avatar
Miller Puckette committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
    SETPOINTER(argv, &gp);
    template_notify(template, s, argc, argv);
    gpointer_unset(&gp);
}

    /* call this when reading a patch from a file to declare what templates
    we'll need.  If there's already a template, check if it matches.
    If it doesn't it's still OK as long as there are no "struct" (gtemplate)
    objects hanging from it; we just conform everyone to the new template.
    If there are still struct objects belonging to the other template, we're
    in trouble.  LATER we'll figure out how to conform the new patch's objects
    to the pre-existing struct. */
static void *template_usetemplate(void *dummy, t_symbol *s,
    int argc, t_atom *argv)
{
    t_template *x;
    t_symbol *templatesym =
        canvas_makebindsym(atom_getsymbolarg(0, argc, argv));
    if (!argc)
        return (0);
    argc--; argv++;
            /* check if there's already a template by this name. */
    if ((x = (t_template *)pd_findbyclass(templatesym, template_class)))
    {
        t_template *y = template_new(&s_, argc, argv), *y2;
            /* If the new template is the same as the old one,
            there's nothing to do.  */
        if (!template_match(x, y))
        {
                /* Are there "struct" objects upholding this template? */
            if (x->t_list)
            {
                    /* don't know what to do here! */
                error("%s: template mismatch",
                    templatesym->s_name);
            }
            else
            {
                    /* conform everyone to the new template */
                template_conform(x, y);
                pd_free(&x->t_pdobj);
                y2 = template_new(templatesym, argc, argv);
                y2->t_list = 0;
            }
        }
        pd_free(&y->t_pdobj);
    }
        /* otherwise, just make one. */
    else template_new(templatesym, argc, argv);
    return (0);
}

    /* here we assume someone has already cleaned up all instances of this. */
void template_free(t_template *x)
{
    if (*x->t_sym->s_name)
        pd_unbind(&x->t_pdobj, x->t_sym);
    t_freebytes(x->t_vec, x->t_n * sizeof(*x->t_vec));
}

static void template_setup(void)
{
    template_class = class_new(gensym("template"), 0, (t_method)template_free,
        sizeof(t_template), CLASS_PD, 0);
    class_addmethod(pd_canvasmaker, (t_method)template_usetemplate,
        gensym("struct"), A_GIMME, 0);
        
}

/* ---------------- gtemplates.  One per canvas. ----------- */

/* "Struct": an object that searches for, and if necessary creates, 
a template (above).  Other objects in the canvas then can give drawing
instructions for the template.  The template doesn't go away when the
"struct" is deleted, so that you can replace it with
another one to add new fields, for example. */

606
607
static void gtemplate_free(t_gtemplate *x);

Miller Puckette's avatar
Miller Puckette committed
608
609
static void *gtemplate_donew(t_symbol *sym, int argc, t_atom *argv)
{
Ivica Bukvic's avatar
Ivica Bukvic committed
610
611
612
613
614
615
616
617
618
619
620
    t_canvas *cur = canvas_getcurrent();
    t_gobj *gob = cur->gl_list;
    while (gob)
    {
        if (pd_class(&gob->g_pd) == gtemplate_class)
        {
            error("%s: only one struct allowed per canvas.", cur->gl_name->s_name);
            return(0);
        }
        gob = gob->g_next;
    }
Miller Puckette's avatar
Miller Puckette committed
621
622
    t_gtemplate *x = (t_gtemplate *)pd_new(gtemplate_class);
    t_template *t = template_findbyname(sym);
623

Miller Puckette's avatar
Miller Puckette committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
    int i;
    x->x_owner = canvas_getcurrent();
    x->x_next = 0;
    x->x_sym = sym;
    x->x_argc = argc;
    x->x_argv = (t_atom *)getbytes(argc * sizeof(t_atom));
    for (i = 0; i < argc; i++)
        x->x_argv[i] = argv[i];

        /* already have a template by this name? */
    if (t)
    {
        x->x_template = t;
            /* if it's already got a "struct" object we
            just tack this one to the end of the list and leave it
            there. */
        if (t->t_list)
        {
            t_gtemplate *x2, *x3;
            for (x2 = x->x_template->t_list; x3 = x2->x_next; x2 = x3)
                ;
            x2->x_next = x;
            post("template %s: warning: already exists.", sym->s_name);
        }
        else
        {
                /* if there's none, we just replace the template with
                our own and conform it. */
            t_template *y = template_new(&s_, argc, argv);
653

Miller Puckette's avatar
Miller Puckette committed
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
            canvas_redrawallfortemplate(t, 2);
                /* Unless the new template is different from the old one,
                there's nothing to do.  */
            if (!template_match(t, y))
            {
                    /* conform everyone to the new template */
                template_conform(t, y);
                pd_free(&t->t_pdobj);
                t = template_new(sym, argc, argv);
            }
            pd_free(&y->t_pdobj);
            t->t_list = x;
            canvas_redrawallfortemplate(t, 1);
        }
    }
    else
    {
            /* otherwise make a new one and we're the only struct on it. */
        x->x_template = t = template_new(sym, argc, argv);
        t->t_list = x;
674
   }
Miller Puckette's avatar
Miller Puckette committed
675
676
677
678
    outlet_new(&x->x_obj, 0);
    return (x);
}

679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
int template_check_array_fields(t_symbol *structname, t_template *template);

/* probably duplicating some code from template_new here... */
int gtemplate_cancreate(t_symbol *templatename, int argc, t_atom *argv)
{
    while (argc > 1)
    {
        t_symbol *typesym = argv[0].a_w.w_symbol;
        if (typesym == &s_float || typesym == &s_symbol)
        {
            argc -= 2;
            argv += 2; 
        }
        else if (typesym == gensym("array"))
        {
            if (argc > 2 && argv[2].a_type == A_SYMBOL)
            {
                /* check for cancreation here */
                t_template *elemtemplate =
                    template_findbyname(canvas_makebindsym(argv[2].a_w.w_symbol));
699
700
701
702
703
704
705
                if (!elemtemplate)
                {
                    post("warning: template %s does not exist",
                        argv[2].a_w.w_symbol->s_name);
                }
                else if (template_check_array_fields(
                        canvas_makebindsym(templatename), elemtemplate) == 0)
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
                {
                    return 0;
                }
                argc -= 3;
                argv += 3;
            }
            else
            {
                argc -= 2;
                argv += 2;
            }
        }
        else
        {
            argc -= 2;
            argv += 2;
        }
    }
    return 1;
}

Miller Puckette's avatar
Miller Puckette committed
727
728
729
730
731
static void *gtemplate_new(t_symbol *s, int argc, t_atom *argv)
{
    t_symbol *sym = atom_getsymbolarg(0, argc, argv);
    if (argc >= 1)
        argc--; argv++;
732
733
734
735
736
    if (gtemplate_cancreate(sym, argc, argv))
    {
        return (gtemplate_donew(canvas_makebindsym(sym), argc, argv));
    }
    else
737
        return (0);
Miller Puckette's avatar
Miller Puckette committed
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
}

    /* old version (0.34) -- delete 2003 or so */
static void *gtemplate_new_old(t_symbol *s, int argc, t_atom *argv)
{
    t_symbol *sym = canvas_makebindsym(canvas_getcurrent()->gl_name);
    static int warned;
    if (!warned)
    {
        post("warning -- 'template' (%s) is obsolete; replace with 'struct'",
            sym->s_name);
        warned = 1;
    }
    return (gtemplate_donew(sym, argc, argv));
}

t_template *gtemplate_get(t_gtemplate *x)
{
    return (x->x_template);
}

static void gtemplate_free(t_gtemplate *x)
{
        /* get off the template's list */
    t_template *t = x->x_template;
    t_gtemplate *y;
    if (x == t->t_list)
    {
        canvas_redrawallfortemplate(t, 2);
        if (x->x_next)
        {
                /* if we were first on the list, and there are others on
                the list, make a new template corresponding to the new
                first-on-list and replace the existing template with it. */
            t_template *z = template_new(&s_,
                x->x_next->x_argc, x->x_next->x_argv);
            template_conform(t, z);
            pd_free(&t->t_pdobj);
            pd_free(&z->t_pdobj);
            z = template_new(x->x_sym, x->x_next->x_argc, x->x_next->x_argv);
            z->t_list = x->x_next;
            for (y = z->t_list; y ; y = y->x_next)
                y->x_template = z;
        }
        else t->t_list = 0;
        canvas_redrawallfortemplate(t, 1);
    }
    else
    {
        t_gtemplate *x2, *x3;
        for (x2 = t->t_list; x3 = x2->x_next; x2 = x3)
        {
            if (x == x3)
            {
                x2->x_next = x3->x_next;
                break;
            }
        }
    }
    freebytes(x->x_argv, sizeof(t_atom) * x->x_argc);
}

static void gtemplate_setup(void)
{
    gtemplate_class = class_new(gensym("struct"),
        (t_newmethod)gtemplate_new, (t_method)gtemplate_free,
        sizeof(t_gtemplate), CLASS_NOINLET, A_GIMME, 0);
    class_addcreator((t_newmethod)gtemplate_new_old, gensym("template"),
        A_GIMME, 0);
}

/* ---------------  FIELD DESCRIPTORS ---------------------- */

/* a field descriptor can hold a constant or a variable; if a variable,
it's the name of a field in the template we belong to.  LATER, we might
want to cache the offset of the field so we don't have to search for it
every single time we draw the object.
*/

struct _fielddesc
{
    char fd_type;       /* LATER consider removing this? */
    char fd_var;
    union
    {
        t_float fd_float;       /* the field is a constant float */
        t_symbol *fd_symbol;    /* the field is a constant symbol */
        t_symbol *fd_varsym;    /* the field is variable and this is the name */
    } fd_un;
827
828
829
830
831
    float fd_v1;        /* min and max values */
    float fd_v2;
    float fd_screen1;   /* min and max screen values */
    float fd_screen2;
    float fd_quantum;   /* quantization in value */ 
Miller Puckette's avatar
Miller Puckette committed
832
833
};

834
static void fielddesc_setfloat_const(t_fielddesc *fd, t_float f)
Miller Puckette's avatar
Miller Puckette committed
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
{
    fd->fd_type = A_FLOAT;
    fd->fd_var = 0;
    fd->fd_un.fd_float = f;
    fd->fd_v1 = fd->fd_v2 = fd->fd_screen1 = fd->fd_screen2 =
        fd->fd_quantum = 0;
}

static void fielddesc_setsymbol_const(t_fielddesc *fd, t_symbol *s)
{
    fd->fd_type = A_SYMBOL;
    fd->fd_var = 0;
    fd->fd_un.fd_symbol = s;
    fd->fd_v1 = fd->fd_v2 = fd->fd_screen1 = fd->fd_screen2 =
        fd->fd_quantum = 0;
}

static void fielddesc_setfloat_var(t_fielddesc *fd, t_symbol *s)
{
    char *s1, *s2, *s3, strbuf[MAXPDSTRING];
    fd->fd_type = A_FLOAT;
    fd->fd_var = 1;
    if (!(s1 = strchr(s->s_name, '(')) || !(s2 = strchr(s->s_name, ')'))
        || (s1 > s2))
    {
        fd->fd_un.fd_varsym = s;
        fd->fd_v1 = fd->fd_v2 = fd->fd_screen1 = fd->fd_screen2 =
            fd->fd_quantum = 0;
    }
    else
    {
        int cpy = s1 - s->s_name, got;
        if (cpy > MAXPDSTRING-5)
            cpy = MAXPDSTRING-5;
        strncpy(strbuf, s->s_name, cpy);
        strbuf[cpy] = 0;
        fd->fd_un.fd_varsym = gensym(strbuf);
        got = sscanf(s1, "(%f:%f)(%f:%f)(%f)",
            &fd->fd_v1, &fd->fd_v2, &fd->fd_screen1, &fd->fd_screen2,
                &fd->fd_quantum);
        if (got < 2)
            goto fail;
        if (got == 3 || (got < 4 && strchr(s2, '(')))
            goto fail;
        if (got < 5 && (s3 = strchr(s2, '(')) && strchr(s3+1, '('))
            goto fail;
        if (got == 4)
            fd->fd_quantum = 0;
        else if (got == 2)
        {
            fd->fd_quantum = 0;
            fd->fd_screen1 = fd->fd_v1;
            fd->fd_screen2 = fd->fd_v2;
        }
        return;
    fail:
        post("parse error: %s", s->s_name);
        fd->fd_v1 = fd->fd_screen1 = fd->fd_v2 = fd->fd_screen2 =
            fd->fd_quantum = 0;
    }
}

#define CLOSED 1
#define BEZ 2
#define NOMOUSE 4
#define A_ARRAY 55      /* LATER decide whether to enshrine this in m_pd.h */

static void fielddesc_setfloatarg(t_fielddesc *fd, int argc, t_atom *argv)
{
        if (argc <= 0) fielddesc_setfloat_const(fd, 0);
        else if (argv->a_type == A_SYMBOL)
            fielddesc_setfloat_var(fd, argv->a_w.w_symbol);
        else fielddesc_setfloat_const(fd, argv->a_w.w_float);
}

static void fielddesc_setsymbolarg(t_fielddesc *fd, int argc, t_atom *argv)
{
        if (argc <= 0) fielddesc_setsymbol_const(fd, &s_);
        else if (argv->a_type == A_SYMBOL)
        {
            fd->fd_type = A_SYMBOL;
            fd->fd_var = 1;
            fd->fd_un.fd_varsym = argv->a_w.w_symbol;
            fd->fd_v1 = fd->fd_v2 = fd->fd_screen1 = fd->fd_screen2 =
                fd->fd_quantum = 0;
        }
        else fielddesc_setsymbol_const(fd, &s_);
}

static void fielddesc_setarrayarg(t_fielddesc *fd, int argc, t_atom *argv)
{
        if (argc <= 0) fielddesc_setfloat_const(fd, 0);
        else if (argv->a_type == A_SYMBOL)
        {
            fd->fd_type = A_ARRAY;
            fd->fd_var = 1;
            fd->fd_un.fd_varsym = argv->a_w.w_symbol;
        }
        else fielddesc_setfloat_const(fd, argv->a_w.w_float);
}

    /* getting and setting values via fielddescs -- note confusing names;
    the above are setting up the fielddesc itself. */
static t_float fielddesc_getfloat(t_fielddesc *f, t_template *template,
    t_word *wp, int loud)
{
    if (f->fd_type == A_FLOAT)
    {
        if (f->fd_var)
            return (template_getfloat(template, f->fd_un.fd_varsym, wp, loud));
        else return (f->fd_un.fd_float);
    }
    else
    {
        if (loud)
            error("symbolic data field used as number");
        return (0);
    }
}

    /* convert a variable's value to a screen coordinate via its fielddesc */
956
t_float fielddesc_cvttocoord(t_fielddesc *f, t_float val)
Miller Puckette's avatar
Miller Puckette committed
957
{
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
958
    t_float coord, extreme, div;
Miller Puckette's avatar
Miller Puckette committed
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
    if (f->fd_v2 == f->fd_v1)
        return (val);
    div = (f->fd_screen2 - f->fd_screen1)/(f->fd_v2 - f->fd_v1);
    coord = f->fd_screen1 + (val - f->fd_v1) * div;
    extreme = (f->fd_screen1 < f->fd_screen2 ?
        f->fd_screen1 : f->fd_screen2);
    if (coord < extreme)
        coord = extreme;
    extreme = (f->fd_screen1 > f->fd_screen2 ? 
        f->fd_screen1 : f->fd_screen2);
    if (coord > extreme)
        coord = extreme;
    return (coord);
}

    /* read a variable via fielddesc and convert to screen coordinate */
t_float fielddesc_getcoord(t_fielddesc *f, t_template *template,
    t_word *wp, int loud)
{
    if (f->fd_type == A_FLOAT)
    {
        if (f->fd_var)
        {
982
            t_float val = template_getfloat(template,
Miller Puckette's avatar
Miller Puckette committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
                f->fd_un.fd_varsym, wp, loud);
            return (fielddesc_cvttocoord(f, val));
        }
        else return (f->fd_un.fd_float);
    }
    else
    {
        if (loud)
            error("symbolic data field used as number");
        return (0);
    }
}

static t_symbol *fielddesc_getsymbol(t_fielddesc *f, t_template *template,
    t_word *wp, int loud)
{
    if (f->fd_type == A_SYMBOL)
    {
        if (f->fd_var)
            return(template_getsymbol(template, f->fd_un.fd_varsym, wp, loud));
        else return (f->fd_un.fd_symbol);
    }
    else
    {
        if (loud)
            error("numeric data field used as symbol");
        return (&s_);
    }
}

    /* convert from a screen coordinate to a variable value */
1014
t_float fielddesc_cvtfromcoord(t_fielddesc *f, t_float coord)
Miller Puckette's avatar
Miller Puckette committed
1015
{
1016
    t_float val;
Miller Puckette's avatar
Miller Puckette committed
1017
1018
1019
1020
    if (f->fd_screen2 == f->fd_screen1)
        val = coord;
    else
    {
1021
1022
        t_float div = (f->fd_v2 - f->fd_v1)/(f->fd_screen2 - f->fd_screen1);
        t_float extreme;
Miller Puckette's avatar
Miller Puckette committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
        val = f->fd_v1 + (coord - f->fd_screen1) * div;
        if (f->fd_quantum != 0)
            val = ((int)((val/f->fd_quantum) + 0.5)) *  f->fd_quantum;
        extreme = (f->fd_v1 < f->fd_v2 ?
            f->fd_v1 : f->fd_v2);
        if (val < extreme) val = extreme;
        extreme = (f->fd_v1 > f->fd_v2 ?
            f->fd_v1 : f->fd_v2);
        if (val > extreme) val = extreme;
    }
    return (val);
 }

void fielddesc_setcoord(t_fielddesc *f, t_template *template,
1037
    t_word *wp, t_float coord, int loud)
Miller Puckette's avatar
Miller Puckette committed
1038
1039
1040
{
    if (f->fd_type == A_FLOAT && f->fd_var)
    {
1041
        t_float val = fielddesc_cvtfromcoord(f, coord);
Miller Puckette's avatar
Miller Puckette committed
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
        template_setfloat(template,
                f->fd_un.fd_varsym, wp, val, loud);
    }
    else
    {
        if (loud)
            error("attempt to set constant or symbolic data field to a number");
    }
}

1052
1053
1054
/* ---------------- draw svg shapes and paths ---------------- */

/*
1055
1056
1057
draw objects belong to templates and describe how the data in the template
are to be drawn.  The coordinates of the draw (and other display features)
can be attached to fields in the template.
1058

1059
1060
todo: some better way than drawcurve has for defining click widgetbehaviors
    (just checking for field variables and moving joints is too simplistic)
1061
1062
1063
1064
*/

t_class *draw_class;

1065
1066
t_class *drawimage_class;

1067
1068
t_class *svg_class;

1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
/* this is a wrapper around t_fielddesc-- it adds a flag for two reasons:
   1) tkpath defaults to inheriting values for options from the parent until
      you specify them-- after that, there's no way to get back to "inherit".
      We set the flag to "0" to mean "inherit"-- that way we don't send a tcl
      string for that option.  There's still the bug that the user can't set
      an attribute back to "inherit" after they've set something explicitly,
      however.
   2) This might be generally useful even with Qt, so that if the user calls
      a method for an attribute without any arguments, Qt goes back to
      inheriting the value from the parent.  That gives us a way to
      differentiate "inherit" from fielddescriptors, the value "none" and
      "".  (Similar to the way "set" resets a message box.)
*/
1082
typedef struct _svg_attr
1083
{
1084
1085
1086
1087
    int a_flag;
    t_fielddesc a_attr;
} t_svg_attr;

1088
1089
1090
/* events on which to output a notification from the outlet of [draw] */
typedef struct _svg_event
{
1091
1092
1093
1094
1095
1096
1097
1098
    t_svg_attr e_focusin,
               e_focusout,
               e_activate,
               e_click,
               e_mousedown,
               e_mouseup,
               e_mouseover,
               e_mousemove,
1099
1100
               e_mouseout,
               e_drag; /* not in the svg spec, but should have been */
1101
1102
} t_svg_event;

1103
1104
1105
1106
/* svg attributes */
typedef struct _svg
{
    t_pd x_pd;
1107
    void *x_parent; /* parent object-- [group], [draw] or [draw image] */
1108
1109
1110
1111
1112
1113
1114
1115
    int x_flags;
    t_symbol *x_type;
    t_fielddesc x_fill[3];
    int x_filltype;
    t_svg_attr x_fillopacity;
    t_svg_attr x_fillrule;
    t_fielddesc x_stroke[3];
    int x_stroketype;
1116
1117
    int x_ndash;
    t_fielddesc *x_strokedasharray; /* array of lengths */
1118
    t_svg_attr x_strokedashoffset;
1119
1120
    t_svg_event x_events;
    t_fielddesc x_drag; /* convenience event, not part of the svg spec */
1121
    t_svg_attr x_opacity;
1122
    t_svg_attr x_pointerevents;
1123
1124
1125
1126
1127
    t_svg_attr x_strokelinecap;
    t_svg_attr x_strokelinejoin;
    t_svg_attr x_strokemiterlimit;
    t_svg_attr x_strokeopacity;
    t_svg_attr x_strokewidth;
1128
1129
1130
1131
1132
    t_svg_attr x_rx; /* for rounded rectangles, ellipse,
                        and abused for
                        circle (aka 'r')
                        line (aka 'x2') */
    t_svg_attr x_ry; /* abused for line (aka 'y2') */
1133
1134
    int x_transform_n;
    t_fielddesc *x_transform;
1135
1136
1137
1138
    t_svg_attr x_x; /* doubles as 'cx' and 'x1' */
    t_svg_attr x_y; /* doubles as 'cy' and 'y1' */
    t_svg_attr x_width;
    t_svg_attr x_height;
1139
    t_svg_attr x_vis;
1140
    t_fielddesc x_bbox; /* turn bbox calculation on or off */
1141
1142
1143
    int x_pathrect_cache; /* 0 to recalc on next draw_getrect call
                             1 for cached
                            -1 to turn off caching */
1144
    int x_x1; /* for cached bbox */
1145
1146
1147
1148
1149
1150
1151
1152
    int x_y1;
    int x_x2;
    int x_y2;
    int x_nargs;
    t_fielddesc *x_vec;
    int *x_nargs_per_cmd;      /* points per each path command */
    int x_npathcmds;
    char *x_pathcmds; /* for path commands */
1153
1154
1155
1156
1157
1158
} t_svg;

typedef struct _draw
{
    t_object x_obj;
    t_symbol *x_drawtype;
1159
    t_canvas *x_canvas;
1160
    t_pd *x_attr;
1161
1162
} t_draw;

1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
typedef struct _drawimage
{
    t_object x_obj;
    t_fielddesc x_value; /* todo: rename to index */
    t_fielddesc x_xloc;
    t_fielddesc x_yloc;
    t_fielddesc x_vis;
    t_symbol *x_img;
    t_float x_w;
    t_float x_h;
    int x_flags;
    int x_deleteme;
    t_canvas *x_canvas;
    t_pd *x_attr;
} t_drawimage;

1179
1180
extern t_outlet *obj_rightmost_outlet(t_object *x);

1181
void draw_notifyforscalar(t_object *x, t_glist *owner,
1182
1183
1184
    t_scalar *sc, t_symbol *s, int argc, t_atom *argv)
{
    t_gpointer gp;
1185
1186
    t_binbuf *b = binbuf_new();
    t_atom at[1];
1187
1188
    gpointer_init(&gp);
    gpointer_setglist(&gp, owner, &sc->sc_gobj);
1189
1190
1191
    SETPOINTER(at, &gp);
    binbuf_add(b, 1, at);
    binbuf_add(b, argc, argv);
1192
    if (x)
1193
1194
1195
1196
1197
1198
1199
    {
        t_outlet *out;
        if (pd_class(&x->te_pd) == canvas_class)
            out = obj_rightmost_outlet(x);
        else
            out = x->ob_outlet;
        outlet_anything(out, s, binbuf_getnatom(b),
1200
            binbuf_getvec(b));
1201
    }
1202
    gpointer_unset(&gp);
1203
    binbuf_free(b);
1204
1205
}

1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
static int is_svgpath_cmd(t_symbol *s)
{
    /* 1 for absolute cmd, 2 for relative */
    if (s == gensym("M") || s == gensym("Z") || s == gensym("L") ||
        s == gensym("H") || s == gensym("V") || s == gensym("C") ||
        s == gensym("S") || s == gensym("Q") || s == gensym("T") ||
        s == gensym("A"))
        return 1;
    else if (s == gensym("m") || s == gensym("z") || s == gensym("l") ||
        s == gensym("h") || s == gensym("v") || s == gensym("c") ||
        s == gensym("s") || s == gensym("q") || s == gensym("t") ||
        s == gensym("a"))
        return 2;
    else return 0;
}

static int path_ncmds(int argc, t_atom *argv)
{
    int i, j = 0;
    for(i = 0; i < argc; i++)
    {
1227
1228
        if (argv[i].a_type == A_SYMBOL &&
            is_svgpath_cmd(atom_getsymbol(argv+i)))
1229
1230
1231
1232
1233
            j++;
    }
    return j;
}

1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
void svg_attr_setfloatarg(t_svg_attr *a, int argc, t_atom *argv)
{
    fielddesc_setfloatarg(&a->a_attr, argc, argv);
    a->a_flag = 1;
}

void svg_attr_setfloat_const(t_svg_attr *a, float n)
{
    fielddesc_setfloat_const(&a->a_attr, 0);
    a->a_flag = 0;
}

1246
void *svg_new(t_pd *parent, t_symbol *s, int argc, t_atom *argv)
1247
{
1248
    t_fielddesc *fd;
user's avatar
user committed
1249
    int i, flags = 0;
1250
    t_svg *x = (t_svg *)pd_new(svg_class);
1251
    t_symbol *type = x->x_type = s;
1252
    x->x_flags = flags;
1253
1254
1255
1256
1257
1258
    x->x_parent = (void *)parent;
    fielddesc_setfloat_const(&x->x_vis.a_attr, 1);
    /* let it inherit visibility from parent group, if present */
    x->x_vis.a_flag = 0;
    /* the following should be set in method space */
    /* flags |= NOMOUSE; */
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
    if (type == gensym("rect") ||
        type == gensym("circle") ||
        type == gensym("ellipse") ||
        type == gensym("line"))
    {
        if (argc) svg_attr_setfloatarg(&x->x_x, argc--, argv++);
        else svg_attr_setfloat_const(&x->x_x, 0);
        if (argc) svg_attr_setfloatarg(&x->x_y, argc--, argv++);
        else svg_attr_setfloat_const(&x->x_x, 0);
        if (type == gensym("rect"))
        {
            if (argc) svg_attr_setfloatarg(&x->x_width, argc--, argv++);
            else svg_attr_setfloat_const(&x->x_width, 0);
            if (argc) svg_attr_setfloatarg(&x->x_height, argc--, argv++);
            else svg_attr_setfloat_const(&x->x_height, 0);
        }
        else
        {
            if (argc) svg_attr_setfloatarg(&x->x_rx, argc--, argv++);
            else svg_attr_setfloat_const(&x->x_rx, 0);
            if (argc) svg_attr_setfloatarg(&x->x_ry, argc--, argv++);
            else svg_attr_setfloat_const(&x->x_ry, 0);
        }
    }
1283
    if (x->x_type == gensym("path"))
1284
1285
1286
1287
1288
    {
        int ncmds = x->x_npathcmds = path_ncmds(argc, argv);
        x->x_pathcmds = (char *)t_getbytes(ncmds * sizeof(char));
        x->x_nargs_per_cmd = (int *)t_getbytes(ncmds * sizeof(int));
        for (i = 0; i < ncmds; i++) x->x_nargs_per_cmd[i] = 0;
user's avatar
user committed
1289
        x->x_nargs = argc - ncmds;
1290
    }
1291
    else if (x->x_type == gensym("group"))
1292
1293
1294
1295
    {
        x->x_nargs = 0;
        x->x_vec = 0;
    }
user's avatar
user committed
1296
    else
1297
    {
user's avatar
user committed
1298
        x->x_nargs = argc;
1299
    }
user's avatar
user committed
1300
    x->x_vec = (t_fielddesc *)t_getbytes(x->x_nargs * sizeof(t_fielddesc));
1301
    if (argc && x->x_type == gensym("path"))
1302
1303
    {
        if (argv->a_type != A_SYMBOL ||
1304
1305
            (atom_getsymbol(argv) != gensym("M") &&
             atom_getsymbol(argv) != gensym("m")))
1306
        {
1307
1308
            /* this should probably be the parent instead of "x" */
            pd_error(x->x_parent, "draw path: path data must start "
1309
                        "with a moveto command (M or m)");
1310
1311
1312
1313
1314
1315
            return 0;
        }
    }
    int cmdn = -1; /* hack */
    for (i = 0, fd = x->x_vec; i < argc; i++, argv++)
    {
1316
        if (x->x_type == gensym("path") &&
1317
1318
1319
1320
1321
1322
1323
1324
1325
            argv->a_type == A_SYMBOL &&
            is_svgpath_cmd(atom_getsymbol(argv)))
        {
                x->x_pathcmds[++cmdn] = *(atom_getsymbol(argv)->s_name);
        }
        else
        {
            fielddesc_setfloatarg(fd++, 1, argv);
            /* post("got a coord"); */
1326
            if (x->x_type == gensym("path"))
1327
1328
1329
1330
1331
1332
1333
1334
1335
            {
                (x->x_nargs_per_cmd[cmdn])++;
                /* if we get a field variable, just
                   turn off the get_rect caching */
                if (argv->a_type == A_SYMBOL)
                    x->x_pathrect_cache = -1;
            }
        }
    }
1336
1337
    fielddesc_setfloat_const(&x->x_bbox, 1);
    fielddesc_setfloat_const(&x->x_drag, 0);
1338
    x->x_filltype = 0;
1339
1340
    x->x_fillopacity.a_flag = 0;
    x->x_fillrule.a_flag = 0;
1341
1342
    fielddesc_setfloat_const(&x->x_pointerevents.a_attr, 1);
    x->x_pointerevents.a_flag = 0;
1343
1344
1345
1346
1347
1348
    x->x_stroketype = 0;
    x->x_strokelinecap.a_flag = 0;
    x->x_strokelinejoin.a_flag = 0;
    x->x_strokemiterlimit.a_flag = 0;
    x->x_strokeopacity.a_flag = 0;
    x->x_strokewidth.a_flag = 0;
1349
1350
1351
1352
1353
1354
    x->x_x1 = 0;
    x->x_x2 = 0;
    x->x_y1 = 0;
    x->x_y2 = 0;

    x->x_ndash = 0;
1355
    x->x_strokedasharray =
1356
        (t_fielddesc *)t_getbytes(x->x_ndash * sizeof(t_fielddesc));
1357
    x->x_transform_n = 0;
1358
    x->x_strokedashoffset.a_flag = 0;
1359
1360
    x->x_transform = (t_fielddesc *)t_getbytes(x->x_transform_n *
        sizeof(t_fielddesc));
1361
    /* initialize events */
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
    fielddesc_setfloat_const(&x->x_events.e_focusin.a_attr, 0);
    x->x_events.e_focusin.a_flag = 0;
    fielddesc_setfloat_const(&x->x_events.e_focusout.a_attr, 0);
    x->x_events.e_focusout.a_flag = 0;
    fielddesc_setfloat_const(&x->x_events.e_activate.a_attr, 0);
    x->x_events.e_activate.a_flag = 0;
    fielddesc_setfloat_const(&x->x_events.e_click.a_attr, 0);
    x->x_events.e_click.a_flag = 0;
    fielddesc_setfloat_const(&x->x_events.e_mousedown.a_attr, 0);
    x->x_events.e_mousedown.a_flag = 0;
    fielddesc_setfloat_const(&x->x_events.e_mouseup.a_attr, 0);
    x->x_events.e_mouseup.a_flag = 0;
    fielddesc_setfloat_const(&x->x_events.e_mouseover.a_attr, 0);
    x->x_events.e_mouseover.a_flag = 0;
    fielddesc_setfloat_const(&x->x_events.e_mousemove.a_attr, 0);
    x->x_events.e_mousemove.a_flag = 0;
    fielddesc_setfloat_const(&x->x_events.e_mouseout.a_attr, 0);
    x->x_events.e_mouseout.a_flag = 0;
1380
1381
    fielddesc_setfloat_const(&x->x_events.e_drag.a_attr, 0);
    x->x_events.e_drag.a_flag = 0;
1382
1383
1384
1385
1386
1387
1388
1389

    char buf[50];
    // Here we bind the parent object to the addy for
    // the svg. This way both [group] and [draw] will have
    // the same binding symbol
    sprintf(buf, "x%lx", (long unsigned int)x);
    pd_bind(parent, gensym(buf));

1390
1391
1392
    return (x);
}

1393
t_pd *svg_header(t_pd *x)
1394
{
1395
1396
1397
1398
1399
1400
1401
1402
1403
    return (&(((t_svg *)x)->x_pd));
}

static int symbol_isdrawtype(t_symbol *s)
{
    if (s == gensym("circle")  || s == gensym("ellipse")  ||
        s == gensym("line")    || s == gensym("path")     ||
        s == gensym("polygon") || s == gensym("polyline") ||
        s == gensym("rect")    || s == gensym("image")    ||
1404
        s == gensym("sprite")  || s == gensym("group"))
1405
    {
1406
        return 1;
1407
    }
1408
1409
1410
    else
        return 0;
}
1411

1412
static void *drawimage_new(t_symbol *classsym, int argc, t_atom *argv);
1413
extern void *group_new(t_symbol *s);
1414
1415
1416

static void *draw_new(t_symbol *classsym, t_int argc, t_atom *argv)
{
1417
    t_symbol *type;
1418
1419
1420
1421
    if (argc && argv->a_type == A_SYMBOL &&
        symbol_isdrawtype(argv[0].a_w.w_symbol))
    {
        type = atom_getsymbolarg(0, argc--, argv++);
1422
1423
        /* sprite and image have their own widgetbehavior, so they
        have their own class and new function */
1424
1425
        if (type == gensym("sprite") || type == gensym("image"))
            return (drawimage_new(type, argc, argv));
1426
1427
1428
1429
1430
1431
1432
1433
1434
        else if (type == gensym("group"))
        {
            t_symbol *group_name;
            if (argc > 0 && argv->a_type == A_SYMBOL)
                group_name = atom_getsymbolarg(0, argc, argv);
            else group_name = &s_;
            post("group name is %s", group_name->s_name);
            return (group_new(group_name));
        }
1435
    }
1436
    else
1437
    {
1438
1439
        type = gensym("rect");
        post("warning: draw: no shape specified, defaulting to 'rect'");
1440
    }
1441
1442
1443

    t_draw *x = (t_draw *)pd_new(draw_class);

1444

1445
    /* create a proxy for drawing/svg attributes */
1446
    if (!(x->x_attr = (t_pd *)svg_new((t_pd *)x, type, argc, argv)))
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
    {
        pd_error(x, "draw: path data must start with 'm' or 'M'");
        return (0);
    }
    /* now that we have our proxy, make an inlet for it.
       The inlet will belong to our draw object, but the
       svg_class will actually receive the messages
       to the inlet. */
    t_svg *sa = (t_svg *)x->x_attr;
    inlet_new(&x->x_obj, &sa->x_pd, 0, 0);
1457
1458
1459
1460

    /* outlet for event notifications */
    outlet_new(&x->x_obj, &s_anything);

1461
1462
    /* x_canvas can stay here */
    x->x_canvas = canvas_getcurrent();
1463
1464


1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
    return (x);
}

t_canvas *svg_parentcanvas(t_svg *x)
{
    /* this is probably a better interface... I am
       returning c sometimes because it works for my
       use case, but that doesn't seem like a sensible
       interface in general. */
    t_canvas *ret = 0;
    if (x->x_type == gensym("group"))
    {
        t_canvas *c = (t_canvas *)x->x_parent;
        if (c->gl_owner)
            ret = c->gl_owner;
        else
            ret = c;
    }
    else if (x->x_type == gensym("sprite") ||
             x->x_type == gensym("image"))
    {
        t_drawimage *d = (t_drawimage *)x->x_parent;
        ret = d->x_canvas;
    }
    else
    {
        t_draw *d = (t_draw *)x->x_parent;
        ret = d->x_canvas;
    }
    return (ret);
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
}

static char *rgb_to_hex(int r, int g, int b)
{
    static char hexc[10];
    int r1 = r < 0 ? 0 : r;
    if (r1 > 255) r1 = 255;
    int g1 = g < 0 ? 0 : g;
    if (g1 > 255) g1 = 255;
    int b1 = b < 0 ? 0 : b;
    if (b1 > 255) b1 = 255;
    sprintf(hexc, "#%.6x", (r1 << 16) + (g1 << 8) + b1);
    return hexc;
}

char *get_strokelinecap(int a)
{
1512
    static char strokelinecap[15];
1513
1514
    if (a == 0) sprintf(strokelinecap, "butt");
    else if (a == 1) sprintf(strokelinecap, "round");
1515
    else if (a == 2) sprintf(strokelinecap, "projecting");
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
    else sprintf(strokelinecap, "butt");
    return (strokelinecap);
}

char *get_strokelinejoin(int a)
{
    static char strokelinejoin[8];
    if (a == 0) sprintf(strokelinejoin, "miter");
    else if (a == 1) sprintf(strokelinejoin, "round");
    else if (a == 2) sprintf(strokelinejoin, "bevel");
    else sprintf(strokelinejoin, "miter");
    return (strokelinejoin);
}

user's avatar
user committed
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
t_svg_attr *svg_getattr(t_svg *x, t_symbol *s)
{
    if (s == gensym("fill-opacity")) return &x->x_fillopacity;
    else if (s == gensym("fill-rule")) return &x->x_fillrule;
    else if (s == gensym("height")) return &x->x_height;
    else if (s == gensym("opacity")) return &x->x_opacity;
    else if (s == gensym("pointer-events")) return &x->x_pointerevents;
    else if (s == gensym("rx") || s == gensym("r") || s == gensym("x2"))
        return &x->x_rx;
    else if (s == gensym("ry") || s == gensym("y2"))
        return &x->x_ry;
    else if (s == gensym("stroke-opacity")) return &x->x_strokeopacity;
    else if (s == gensym("stroke-dashoffset")) return &x->x_strokedashoffset;
    else if (s == gensym("stroke-linecap")) return &x->x_strokelinecap;
    else if (s == gensym("stroke-linejoin")) return &x->x_strokelinejoin;
    else if (s == gensym("stroke-miterlimit")) return &x->x_strokemiterlimit;
    else if (s == gensym("stroke-width")) return &x->x_strokewidth;
    else if (s == gensym("vis")) return &x->x_vis;
    else if (s == gensym("width")) return &x->x_width;
    else if (s == gensym("x") || s == gensym("cx") || s == gensym("x1"))
        return &x->x_x;
    else if (s == gensym("y") || s == gensym("cy") || s == gensym("y1"))
        return &x->x_y;
    return 0;
}

1556
1557
1558
1559
1560
1561
void svg_parsetransform(t_svg *x, t_template *template, t_word *data,
    t_float *mp1, t_float *mp2, t_float *mp3,
    t_float *mp4, t_float *mp5, t_float *mp6);

void svg_group_pathrect_cache(t_svg *x, int state);

1562
extern void scalar_drawselectrect(t_scalar *x, t_glist *glist, int state);
1563
1564
1565
1566
1567

void svg_sendupdate(t_svg *x, t_canvas *c, t_symbol *s,
    t_template *template, t_word *data, int *predraw_bbox, void *parent,
    t_scalar *sc)
{
user's avatar
user committed
1568
   /* todo-- I'm mixing "c" with glist_getcanvas(c) too freely...
1569
1570
1571
      need to experiment with gop scalars to make sure I'm not breaking
      anything */ 
    int in_array = (sc->sc_vec != data);
1572
    //post("in_array is %d", in_array);
1573
    char tag[MAXPDSTRING];
1574
1575
    if (x->x_type == gensym("group"))
    {
1576
        sprintf(tag, "%s%lx.%lx",
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1577
            (in_array ? "scelem" : "dgroup"),
1578
            (long unsigned int)x->x_parent,
1579
1580
1581
1582
            (long unsigned int)data);
    }
    else
    {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1583
        sprintf(tag, "draw%lx.%lx",
1584
1585
1586
            (long unsigned int)x->x_parent,
            (long unsigned int)data);
    }
1587
1588
1589
    if (s == gensym("bbox"))
        *predraw_bbox = 1;
    else if (s == gensym("fill"))
1590
1591
1592
1593
1594
1595
1596
1597
    {
        t_symbol *fill;
        t_fielddesc *fd = x->x_fill;
        if (x->x_filltype == 1)
            fill = fielddesc_getsymbol(fd, template, data, 1);
        else if (x->x_filltype == 2)
        {
            fill = gensym(rgb_to_hex(
1598
1599
1600
                (int)fielddesc_getcoord(fd, template, data, 1),
                (int)fielddesc_getcoord(fd+1, template, data, 1),
                (int)fielddesc_getcoord(fd+2, template, data, 1)));
1601
1602
1603
        }
        else
            fill = &s_;
1604
1605
        gui_vmess("gui_draw_configure", "xsss",
            glist_getcanvas(c), tag, s->s_name, fill->s_name);
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
    }
    else if (s == gensym("stroke"))
    {
        t_symbol *stroke;
        t_fielddesc *fd = x->x_stroke;
        if (x->x_stroketype == 1)
            stroke = fielddesc_getsymbol(fd, template, data, 1);
        else if (x->x_stroketype == 2)
        {
            stroke = gensym(rgb_to_hex(
1616
1617
1618
                (int)fielddesc_getcoord(fd, template, data, 1),
                (int)fielddesc_getcoord(fd+1, template, data, 1),
                (int)fielddesc_getcoord(fd+2, template, data, 1)));
1619
1620
        }
        else stroke = &s_;
1621
1622
        gui_vmess("gui_draw_configure", "xsss",
            glist_getcanvas(c), tag, s->s_name, stroke->s_name);
1623
1624
    }
    else if (s == gensym("fill-rule"))
1625
1626
        gui_vmess("gui_draw_configure", "xssi",
            glist_getcanvas(c), tag, "fill-rule",
user's avatar
user committed
1627
            (int)fielddesc_getcoord(
1628
1629
                &x->x_fillrule.a_attr, template, data, 1) ?
                    "evenodd" : "nonzero");
user's avatar
user committed
1630
1631
1632
1633
1634
    else if (s == gensym("fill-opacity") ||
             s == gensym("opacity") ||
             s == gensym("stroke-dashoffset") ||
             s == gensym("stroke-miterlimit") ||
             s == gensym("stroke-opacity"))
1635
1636
        gui_vmess("gui_draw_configure", "xssf",
            glist_getcanvas(c), tag, s->s_name,
user's avatar
user committed
1637
1638
            fielddesc_getcoord(
                &(svg_getattr(x, s)->a_attr), template, data, 1));
1639
1640
    else if (s == gensym("pointer-events"))
        *predraw_bbox = 1;
1641
    else if (s == gensym("stroke-linecap"))
1642
1643
        gui_vmess("gui_draw_configure", "xsss",
            glist_getcanvas(c), tag, "stroke-linecap",
user's avatar
user committed
1644
1645
1646
                get_strokelinecap(
                    (int)fielddesc_getcoord(&x->x_strokelinecap.a_attr,
                        template, data, 1)));
1647
    else if (s == gensym("stroke-linejoin"))
1648
1649
        gui_vmess("gui_draw_configure", "xsss",
            glist_getcanvas(c), tag, "stroke-linejoin",
user's avatar
user committed
1650
1651
1652
1653
1654
1655
1656
1657
1658
                get_strokelinejoin(
                    (int)fielddesc_getcoord(&x->x_strokelinejoin.a_attr,
                        template, data, 1)));
    else if (s == gensym("stroke-width") ||
             s == gensym("rx") || s == gensym("r") || s == gensym("x2") ||
             s == gensym("ry") || s == gensym("y2") ||
             s == gensym("x") || s == gensym("cx") || s == gensym("x1") ||
             s == gensym("y") || s == gensym("cy") || s == gensym("y1") ||
             s == gensym("height") || s == gensym("width"))
1659
    {
1660
1661
        gui_vmess("gui_draw_configure", "xssf",
            glist_getcanvas(c), tag, s->s_name,
user's avatar
user committed
1662
1663
            fielddesc_getcoord(
                &(svg_getattr(x, s)->a_attr), template, data, 1));
1664
        *predraw_bbox = 1;
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
    }
    else if (s == gensym("transform"))
    {
        t_float m1, m2, m3, m4, m5, m6;
        /* we'll probably get a different bbox now, so we
           will calculate a new one the next time we call
           draw_getrect for this draw command. For groups
           we need to do it for all of the draw commands
           inside it.
        */
        if (x->x_type == gensym("group"))
            svg_group_pathrect_cache(x, 0);
        else if (x->x_pathrect_cache != -1)
        {
            x->x_pathrect_cache = 0;
        }
        svg_parsetransform(x, template, data, &m1, &m2, &m3, &m4, &m5, &m6);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1682
1683
        char mbuf[MAXPDSTRING];
        sprintf(mbuf, "matrix(%g %g %g %g %g %g)", m1, m2, m3, m4, m5, m6);
1684
1685
        gui_vmess("gui_draw_configure", "xsss",
            glist_getcanvas(c), tag, s->s_name,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1686
            mbuf);
1687
1688
        *predraw_bbox = 1;
    }
1689
1690
1691
    else if (s == gensym("mouseover"))
    {
        gui_vmess("gui_draw_event", "xsxxsi",
1692
            glist_getcanvas(c), tag, sc, x, "mouseover",
1693
            (int)fielddesc_getcoord(
1694
                &x->x_events.e_mouseover.a_attr, template, data, 1));
1695
1696
1697
1698
    }
    else if (s == gensym("mouseout"))
    {
        gui_vmess("gui_draw_event", "xsxxsi",
1699
            glist_getcanvas(c), tag, sc, x, "mouseout",
1700
            (int)fielddesc_getcoord(
1701
                &x->x_events.e_mouseout.a_attr, template, data, 1));
1702
1703
1704
1705
    }
    else if (s == gensym("mousemove"))
    {
        gui_vmess("gui_draw_event", "xsxxsi",
1706
            glist_getcanvas(c), tag, sc, x, "mousemove",
1707
            (int)fielddesc_getcoord(
1708
                &x->x_events.e_mousemove.a_attr, template, data, 1));
1709
1710
1711
1712
    }
    else if (s == gensym("mouseup"))
    {
        gui_vmess("gui_draw_event", "xsxxsi",
1713
            glist_getcanvas(c), tag, sc, x, "mouseup",
1714
            (int)fielddesc_getcoord(
1715
                &x->x_events.e_mouseup.a_attr, template, data, 1));
1716
1717
1718
1719
    }
    else if (s == gensym("mousedown"))
    {
        gui_vmess("gui_draw_event", "xsxxsi",
1720
            glist_getcanvas(c), tag, sc, x, "mousedown",
1721
            (int)fielddesc_getcoord(
1722
                &x->x_events.e_mousedown.a_attr, template, data, 1));
1723
    }
1724
1725
1726
1727
1728
1729
1730
    else if (s == gensym("drag"))
    {
        gui_vmess("gui_draw_drag_event", "xsxxsi",
            glist_getcanvas(c), tag, sc, x, "drag",
            (int)fielddesc_getcoord(
                &x->x_events.e_drag.a_attr, template, data, 1));
    }
1731
1732
    else if (s == gensym("vis"))
    {
1733
1734
        gui_vmess("gui_draw_configure", "xsss",
            glist_getcanvas(c), tag, "visibility",
user's avatar
user committed
1735
1736
            (int)fielddesc_getcoord(
                &x->x_vis.a_attr, template, data, 1) ? "visible" : "hidden");
1737
1738
        *predraw_bbox = 1;
    }
1739
1740
1741
1742
1743
1744
    else if (s == gensym("stroke-dasharray"))
    {
        if (x->x_ndash)
        {
            t_fielddesc *fd = x->x_strokedasharray;
            int i;
1745
1746
            gui_start_vmess("gui_draw_configure", "xss",
                glist_getcanvas(c), tag, s->s_name);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1747
            gui_start_array();
1748
            for (i = 0; i < x->x_ndash; i++)
1749
                gui_f(fielddesc_getcoord(fd+i, template, data, 1));
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1750
1751
            gui_end_array();
            gui_end_vmess();
1752
1753
1754
1755
        }
    }
    else if (s == gensym("data"))
    {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1756
        char tagbuf[MAXPDSTRING];
user's avatar
user committed
1757
1758
        sprintf(tagbuf, "draw%lx.%lx",
            (long unsigned int)parent, (long unsigned int)data);
1759
1760
        gui_start_vmess("gui_draw_configure", "xss",
            glist_getcanvas(c), tagbuf, "d"); 
1761
1762
1763
1764
1765
1766
1767
1768
        /* let's turn off bbox caching so we can recalculate the bbox */
        if (x->x_pathrect_cache != -1)
           x->x_pathrect_cache = 0;
        *predraw_bbox = 1;
        int i;
        char *cmd;
        t_fielddesc *f;
        int totalpoints = 0; /* running tally */
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1769
1770
1771
        /* start an array parameter */
        char cmdbuf[2];
        gui_start_array();
1772
1773
1774
1775
1776
1777
        /* path parser: no error checking yet */
        for (i = 0, cmd = x->x_pathcmds; i < x->x_npathcmds; i++, cmd++)
        {
            int j;
            int cargs = x->x_nargs_per_cmd[i];
            f = (x->x_vec)+totalpoints;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1778
            sprintf(cmdbuf, "%c", *(cmd));
1779
            gui_s(cmdbuf);
1780
            for (j = 0; j < x->x_nargs_per_cmd[i]; j++)
1781
                gui_f(fielddesc_getcoord(
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1782
                    f+j, template, data, 0));
1783
1784
            totalpoints += x->x_nargs_per_cmd[i];
        }
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1785
1786
        gui_end_array();
        gui_end_vmess();
1787
1788
1789
    }
    else if (s == gensym("index"))
    {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1790
1791
        gui_vmess("gui_drawimage_index", "xxxi",
            glist_getcanvas(c), parent, data, drawimage_getindex(parent, template, data));
1792
1793
1794
    }
    else if (s == gensym("points"))
    {
1795
        char tagbuf[MAXPDSTRING];
user's avatar
user committed
1796
1797
        sprintf(tagbuf, "draw%lx.%lx",
            (long unsigned int)parent, (long unsigned int)data);
1798
1799
        gui_start_vmess("gui_draw_coords", "xss",
            glist_getcanvas(c), tagbuf, x->x_type->s_name);
1800
        gui_start_array();
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
        /* let's turn off bbox caching so we can recalculate the bbox */
        if (x->x_pathrect_cache != -1)
           x->x_pathrect_cache = 0;
        *predraw_bbox = 1;
        int i;
        t_fielddesc *f = x->x_vec;
        if (x->x_type == gensym("rect") ||
            x->x_type == gensym("ellipse"))
        {
            t_float xval, yval, w, h;
            xval = fielddesc_getcoord(f, template, data, 1);
            yval = fielddesc_getcoord(f+1, template, data, 1);
            if (x->x_type == gensym("rect"))
            {
                w = xval + (fielddesc_getcoord(f+2, template, data, 1));
                h = yval + (fielddesc_getcoord(f+3, template, data, 1));
1817
1818
1819
1820
                gui_f(xval);
                gui_f(yval);
                gui_f(w);
                gui_f(h);
1821
1822
1823
            }
            else
            {
1824
1825
                gui_f(xval);
                gui_f(yval);
1826
1827
1828
1829
1830
1831
            }
        }
        else
        {
            int n = (x->x_type == gensym("circle")) ? 2 : x->x_nargs;
            for (i = 0; i < n; i++)
1832
                gui_f(fielddesc_getcoord(f+i, template, data, 1));
1833
        }
1834
1835
        gui_end_array();
        gui_end_vmess();
1836
1837
1838
1839
1840
1841
1842
    }
}

void svg_updatevec(t_canvas *c, t_word *data, t_template *template,
    t_template *target, void *parent, t_symbol *s, t_svg *x, t_scalar *sc,
    int *predraw_bbox)
{
1843
    // post("updateing vec...");
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
    int i, j;
    for (i = 0; i < template->t_n; i++)
    {
        if (template->t_vec[i].ds_type == DT_ARRAY)
        {
            int arrayonset, type, elemsize;
            t_symbol *targetsym = target->t_sym, *elemtemplatesym;
            t_array *array;
            char *elem;
            if (!template_find_field(template, ((template->t_vec)+i)->ds_name,
                &arrayonset, &type, &elemtemplatesym))
            {
                error("draw: problem finding array...");
                return;
            }

            t_template *elemtemplate = template_findbyname(elemtemplatesym);
            t_canvas *elemtemplatecanvas = template_findcanvas(elemtemplate);
            /* this is a doozy... */
            array = *(t_array **)(((char *)data) + arrayonset);
            elem = (char *)array->a_vec;

            if (elemtemplatesym == targetsym)
            {
                elemsize = array->a_elemsize;
                for (j = 0; j < array->a_n; j++)
                {
                        int redraw_bbox = 0;
                        svg_sendupdate(x, glist_getcanvas(c), s,
                            elemtemplate, (t_word *)(elem + elemsize * j),
                            predraw_bbox, parent, sc);
                }
            }
            svg_updatevec(c, (t_word *)elem, elemtemplate, target, parent,
                s, x, sc, predraw_bbox);
        }
    }
}

/* todo: i think svg_togui and this are unnecessarily duplicating code... */
extern void scalar_select(t_gobj *z, t_glist *owner, int state);
t_template *canvas_findtemplate(t_canvas *c);
1886
void svg_doupdate(t_svg *x, t_canvas *c, t_symbol *s)
1887
1888
1889
{
    t_gobj *g;
    t_template *template;
1890
    t_canvas *visible = c;
1891
1892
1893
    void *parent = x->x_parent;
    t_canvas *parentcanvas =
        canvas_templatecanvas_forgroup(svg_parentcanvas(x));
1894
    t_template *parenttemplate = canvas_findtemplate(parentcanvas);
1895
1896
1897
1898
1899
    int redraw_bbox = 0;
     /* "visible" is here because we could be drawn in a gop, in
       which case we must "climb" out to the parent canvas on
       which we are drawn. There's probably a function somewhere
       that abstracts this away... */
1900
    while(visible->gl_isgraph && visible->gl_owner)
1901
        visible = visible->gl_owner;
1902
1903
    for (g = c->gl_list; g; g = g->g_next)
    {
1904
        if (glist_isvisible(c) && g->g_pd == scalar_class)
1905
        {
1906
1907
            template = template_findbyname((((t_scalar *)g)->sc_template));
            /* still kinda hacky-- redrawbbox side-effect is weird */
1908
            t_word *data = ((t_scalar *)g)->sc_vec;
1909
1910
1911
1912
            if (parenttemplate == template)
            { 
                svg_sendupdate(x, visible, s, template,
                    data, &redraw_bbox, parent, (t_scalar *)g);
1913
            }
1914
            else
1915
            {
1916
                if (template_has_elemtemplate(template, parenttemplate))
1917
                {
1918
1919
1920
                    svg_updatevec(c, ((t_scalar *)g)->sc_vec, template,
                        parenttemplate, parent, s, x, (t_scalar *)g,
                        &redraw_bbox);
1921
                }
1922
            }
1923