g_array.c 63.9 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
/* 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>      /* for read/write to files */
#include "m_pd.h"
#include "g_canvas.h"
#include <math.h>

12
13
extern int glob_lmclick;

Miller Puckette's avatar
Miller Puckette committed
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* jsarlo { */
#define ARRAYPAGESIZE 1000  /* this should match the page size in u_main.tk */
/* } jsarlo */

/* see also the "plot" object in g_scalar.c which deals with graphing
arrays which are fields in scalars.  Someday we should unify the
two, but how? */

    /* aux routine to bash leading '#' to '$' for dialogs in u_main.tk
    which can't send symbols starting with '$' (because the Pd message
    interpreter would change them!) */

static t_symbol *sharptodollar(t_symbol *s)
{
    if (*s->s_name == '#')
    {
        char buf[MAXPDSTRING];
        strncpy(buf, s->s_name, MAXPDSTRING);
        buf[MAXPDSTRING-1] = 0;
        buf[0] = '$';
        return (gensym(buf));
    }
    else return (s);
}

/* --------- "pure" arrays with scalars for elements. --------------- */

/* Pure arrays have no a priori graphical capabilities.
They are instantiated by "garrays" below or can be elements of other
scalars (g_scalar.c); their graphical behavior is defined accordingly. */

t_array *array_new(t_symbol *templatesym, t_gpointer *parent)
{
    t_array *x = (t_array *)getbytes(sizeof (*x));
    t_template *template;
    template = template_findbyname(templatesym);
    x->a_templatesym = templatesym;
    x->a_n = 1;
    x->a_elemsize = sizeof(t_word) * template->t_n;
    x->a_vec = (char *)getbytes(x->a_elemsize);
        /* note here we blithely copy a gpointer instead of "setting" a
        new one; this gpointer isn't accounted for and needn't be since
        we'll be deleted before the thing pointed to gets deleted anyway;
        see array_free. */
    x->a_gp = *parent;
    x->a_stub = gstub_new(0, x);
    word_init((t_word *)(x->a_vec), template, parent);
    return (x);
}

/* jsarlo { */
void garray_arrayviewlist_close(t_garray *x);
/* } jsarlo */

void array_resize(t_array *x, int n)
{
70
    //fprintf(stderr,"array_resize %d\n", n);
Miller Puckette's avatar
Miller Puckette committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    int elemsize, oldn;
    t_template *template = template_findbyname(x->a_templatesym);
    if (n < 1)
        n = 1;
    oldn = x->a_n;
    elemsize = sizeof(t_word) * template->t_n;

    x->a_vec = (char *)resizebytes(x->a_vec, oldn * elemsize, n * elemsize);
    x->a_n = n;
    if (n > oldn)
    {
        char *cp = x->a_vec + elemsize * oldn;
        int i = n - oldn;
        for (; i--; cp += elemsize)
        {
            t_word *wp = (t_word *)cp;
            word_init(wp, template, &x->a_gp);
        }
    }
    x->a_valid = ++glist_valid;
}

static void array_resize_and_redraw(t_array *array, t_glist *glist, int n)
{
95
    //fprintf(stderr,"array_resize_and_redraw\n");
Miller Puckette's avatar
Miller Puckette committed
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
    t_array *a2 = array;
    int vis = glist_isvisible(glist);
    while (a2->a_gp.gp_stub->gs_which == GP_ARRAY)
        a2 = a2->a_gp.gp_stub->gs_un.gs_array;
    if (vis)
        gobj_vis(&a2->a_gp.gp_un.gp_scalar->sc_gobj, glist, 0);
    array_resize(array, n);
    if (vis)
        gobj_vis(&a2->a_gp.gp_un.gp_scalar->sc_gobj, glist, 1);
}

void word_free(t_word *wp, t_template *template);

void array_free(t_array *x)
{
    int i;
    t_template *scalartemplate = template_findbyname(x->a_templatesym);
    gstub_cutoff(x->a_stub);
    for (i = 0; i < x->a_n; i++)
    {
        t_word *wp = (t_word *)(x->a_vec + x->a_elemsize * i);
        word_free(wp, scalartemplate);
    }
    freebytes(x->a_vec, x->a_elemsize * x->a_n);
    freebytes(x, sizeof *x);
}

/* --------------------- graphical arrays (garrays) ------------------- */

t_class *garray_class;
static int gcount = 0;

struct _garray
{
    t_gobj x_gobj;
    t_scalar *x_scalar;     /* scalar "containing" the array */
    t_glist *x_glist;       /* containing glist */
    t_symbol *x_name;       /* unexpanded name (possibly with leading '$') */
    t_symbol *x_realname;   /* expanded name (symbol we're bound to) */
    char x_usedindsp;       /* true if some DSP routine is using this */
    char x_saveit;          /* true if we should save this with parent */
137
    char x_joc;             /* true if we should "jump on click" in a graph */
Miller Puckette's avatar
Miller Puckette committed
138
139
    char x_listviewing;     /* true if list view window is open */
    char x_hidename;        /* don't print name above graph */
140
141
142
143
    int x_style;            /* so much simpler to keep it here */
    t_symbol *x_send;       /* send_changed hook */
    t_symbol *x_fillcolor;       /* color for filled area of the are */
    t_symbol *x_outlinecolor;    /* color of the outline around the element */
Miller Puckette's avatar
Miller Puckette committed
144
145
};

146
147
t_pd *garray_arraytemplatecanvas;
t_pd *garray_floattemplatecanvas;
Miller Puckette's avatar
Miller Puckette committed
148
149
150
static char garray_arraytemplatefile[] = "\
#N canvas 0 0 458 153 10;\n\
#X obj 43 31 struct _float_array array z float float style\n\
151
float linewidth float color symbol fillcolor symbol outlinecolor;\n\
152
#X obj 43 70 old_plot z color linewidth 0 0 1 style fillcolor outlinecolor;\n\
Miller Puckette's avatar
Miller Puckette committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
";
static char garray_floattemplatefile[] = "\
#N canvas 0 0 458 153 10;\n\
#X obj 39 26 struct float float y;\n\
";

/* create invisible, built-in canvases to determine the templates for floats
and float-arrays. */

void garray_init( void)
{
    t_binbuf *b;
    if (garray_arraytemplatecanvas)
        return;
    b = binbuf_new();
    
    glob_setfilename(0, gensym("_float"), gensym("."));
    binbuf_text(b, garray_floattemplatefile, strlen(garray_floattemplatefile));
    binbuf_eval(b, 0, 0, 0);
172
    garray_floattemplatecanvas = s__X.s_thing;
Miller Puckette's avatar
Miller Puckette committed
173
174
175
176
177
178
179
180
181
    vmess(s__X.s_thing, gensym("pop"), "i", 0);
    
    glob_setfilename(0, gensym("_float_array"), gensym("."));
    binbuf_text(b, garray_arraytemplatefile, strlen(garray_arraytemplatefile));
    binbuf_eval(b, 0, 0, 0);
    garray_arraytemplatecanvas = s__X.s_thing;
    vmess(s__X.s_thing, gensym("pop"), "i", 0);

    glob_setfilename(0, &s_, &s_);
182
    binbuf_free(b);
Miller Puckette's avatar
Miller Puckette committed
183
184
185
186
187
188
189
190
}

/* create a new scalar attached to a symbol.  Used to make floating-point
arrays (the scalar will be of type "_float_array").  Currently this is
always called by graph_array() below; but when we make a more general way
to save and create arrays this might get called more directly. */

static t_garray *graph_scalar(t_glist *gl, t_symbol *s, t_symbol *templatesym,
191
    t_symbol *fill, t_symbol *outline, int saveit)
Miller Puckette's avatar
Miller Puckette committed
192
193
194
{
    t_garray *x;
    t_template *template;
195
196
    if (!(template = template_findbyname(templatesym)))
        return (0);
Miller Puckette's avatar
Miller Puckette committed
197
198
199
200
201
202
203
204
    x = (t_garray *)pd_new(garray_class);
    x->x_scalar = scalar_new(gl, templatesym);
    x->x_name = s;
    x->x_realname = canvas_realizedollar(gl, s);
    pd_bind(&x->x_gobj.g_pd, x->x_realname);
    x->x_usedindsp = 0;
    x->x_saveit = saveit;
    x->x_listviewing = 0;
205
206
207
208
    template_setsymbol(template, gensym("fillcolor"), x->x_scalar->sc_vec,
        fill, 1);
    template_setsymbol(template, gensym("outlinecolor"), x->x_scalar->sc_vec,
        outline, 1);
Miller Puckette's avatar
Miller Puckette committed
209
210
    glist_add(gl, &x->x_gobj);
    x->x_glist = gl;
211
212
213
    char buf[MAXPDSTRING];
    sprintf(buf, "%s_changed", x->x_realname->s_name);
    x->x_send = gensym(buf);
Miller Puckette's avatar
Miller Puckette committed
214
215
216
217
218
219
    return (x);
}

    /* get a garray's "array" structure. */
t_array *garray_getarray(t_garray *x)
{
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
220
    int zonset, ztype;
Miller Puckette's avatar
Miller Puckette committed
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    t_symbol *zarraytype;
    t_scalar *sc = x->x_scalar;
    t_symbol *templatesym = sc->sc_template;
    t_template *template = template_findbyname(templatesym);
    if (!template)
    {
        error("array: couldn't find template %s", templatesym->s_name);
        return (0);
    }
    if (!template_find_field(template, gensym("z"), 
        &zonset, &ztype, &zarraytype))
    {
        error("array: template %s has no 'z' field", templatesym->s_name);
        return (0);
    }
    if (ztype != DT_ARRAY)
    {
        error("array: template %s, 'z' field is not an array",
            templatesym->s_name);
        return (0);
    }
    return (sc->sc_vec[zonset].w_array);
}

    /* get the "array" structure and furthermore check it's float */
static t_array *garray_getarray_floatonly(t_garray *x,
    int *yonsetp, int *elemsizep)
{
    t_array *a = garray_getarray(x);
    int yonset, type;
    t_symbol *arraytype;
    t_template *template = template_findbyname(a->a_templatesym);
    if (!template_find_field(template, gensym("y"), &yonset,
        &type, &arraytype) || type != DT_FLOAT)
            return (0);
    *yonsetp = yonset;
    *elemsizep = a->a_elemsize;
    return (a);
}

    /* get the array's name.  Return nonzero if it should be hidden */
int garray_getname(t_garray *x, t_symbol **namep)
{
    *namep = x->x_name;
    return (x->x_hidename);
}

268
269
270
271
272
273
    /* find out if array elements should "jump on click" in a graph */
int garray_joc(t_garray *x)
{
    return (x->x_joc);
}

274
275
276
277
    /* if there is one garray in a graph, reset the graph's coordinates
        to fit a new size and style for the garray */
    // flag options -1 = don't resize or redraw, 0 = resize, 1 = resize and redraw
void garray_fittograph(t_garray *x, int n, int flag)
Miller Puckette's avatar
Miller Puckette committed
278
{
279
    //fprintf(stderr,"garray_fittograph %d %d\n", n, flag);
280
281
282
283
    // here we check for x->x_glist validity because when creating
    // a new array from the menu gl is null at the first garray_vis call
    if (!x->x_glist)
        return;
Miller Puckette's avatar
Miller Puckette committed
284
285
286
287
    t_array *array = garray_getarray(x);
    t_glist *gl = x->x_glist;
    if (gl->gl_list == &x->x_gobj && !x->x_gobj.g_next)
    {
288
        //fprintf(stderr,"loop\n");
Miller Puckette's avatar
Miller Puckette committed
289
290
        vmess(&gl->gl_pd, gensym("bounds"), "ffff",
            0., gl->gl_y1, (double)
291
292
                (x->x_style == PLOTSTYLE_POINTS || x->x_style == PLOTSTYLE_BARS
                    || n == 1 ? n : n-1),
Miller Puckette's avatar
Miller Puckette committed
293
                    gl->gl_y2);
294
295
296
                /* close any dialogs that might have the wrong info now... 
                TODO: make changes dynamic to avoid this as it causes Apply to
                close the properties which is annoying */
Miller Puckette's avatar
Miller Puckette committed
297
298
        gfxstub_deleteforkey(gl);
    }
299
300
301
302
303
304
305
    if (flag >= 0)
    {
        if (flag == 1)
            array_resize_and_redraw(array, x->x_glist, n);
        else
            array_resize(array, n);
    }
Miller Puckette's avatar
Miller Puckette committed
306
307
308
309
310
311
312
}

/* handle "array" message to glists; call graph_scalar above with
an appropriate template; then set size and flags.  This is called
from the menu and in the file format for patches.  LATER replace this
by a more coherent (and general) invocation. */

313
t_garray *graph_array(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
314
{
315
316
317
318
319
320
321
322
323
324
325
326
327
328
    t_symbol *fill;
    t_symbol *outline;
    int fflags;

    if (argc < 3) {pd_error(gl, "garray: not enough args"); return 0;}
    t_symbol *name = atom_getsymbolarg(0, argc--, argv++);
    int fsize = (int)atom_getfloatarg(0, argc--, argv++);
    t_symbol *templateargsym = atom_getsymbolarg(0, argc--, argv++);
    if (argc) fflags = (int)atom_getfloatarg(0, argc--, argv++);
    else fflags = 0;
    if (argc) fill = atom_getsymbolarg(0, argc--, argv++);
    else fill = gensym("black");
    if (argc) outline = atom_getsymbolarg(0, argc--, argv++);
    else outline = gensym("black");
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
329
    int n = fsize, zonset, ztype, saveit;
Miller Puckette's avatar
Miller Puckette committed
330
331
332
333
334
335
336
    t_symbol *zarraytype;
    t_garray *x;
    t_pd *x2;
    t_template *template, *ztemplate;
    t_symbol *templatesym;
    int flags = fflags;
    int filestyle = ((flags & 6) >> 1);
337
    //fprintf(stderr,"filestyle = %d\n", filestyle);
338
339
    int style = (filestyle == 0 ? PLOTSTYLE_POINTS :
        (filestyle == 1 ? PLOTSTYLE_POLY : filestyle));
Miller Puckette's avatar
Miller Puckette committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
    if (templateargsym != &s_float)
    {
        error("array %s: only 'float' type understood", templateargsym->s_name);
        return (0);
    }
    templatesym = gensym("pd-_float_array");
    template = template_findbyname(templatesym);
    if (!template)
    {
        error("array: couldn't find template %s", templatesym->s_name);
        return (0);
    }
    if (!template_find_field(template, gensym("z"), 
        &zonset, &ztype, &zarraytype))
    {
        error("array: template %s has no 'z' field", templatesym->s_name);
        return (0);
    }
    if (ztype != DT_ARRAY)
    {
        error("array: template %s, 'z' field is not an array",
            templatesym->s_name);
        return (0);
    }
    if (!(ztemplate = template_findbyname(zarraytype)))
    {
        error("array: no template of type %s", zarraytype->s_name);
        return (0);
    }
    saveit = ((flags & 1) != 0);
370
    x = graph_scalar(gl, name, templatesym, fill, outline, saveit);
Miller Puckette's avatar
Miller Puckette committed
371
    x->x_hidename = ((flags & 8) >> 3);
372
373
374
375
    x->x_joc = ((flags & 16) >> 4);
    x->x_fillcolor = fill;
    x->x_outlinecolor = outline;
    x->x_style = style;
Miller Puckette's avatar
Miller Puckette committed
376
377
378
379
380

    if (n <= 0)
        n = 100;
    array_resize(x->x_scalar->sc_vec[zonset].w_array, n);

381
382
383
384
385
    // let's make sure that the GOP glist has sane x values to ensure that
    // the entire array fits inside the box (this gets properly initialized
    // when creating from the menu but not when loading from the file)
    int gop_w = (x->x_style == PLOTSTYLE_POINTS || x->x_style == PLOTSTYLE_BARS
                    || n == 1 ? n : n-1);
386
387
    //fprintf(stderr,"graph_array glist->gl_x2=%g gop_w=%d n=%d\n", gl->gl_x2, gop_w, n);
    if (gl->gl_x2 != gop_w) gl->gl_x2 = gop_w;
388

Miller Puckette's avatar
Miller Puckette committed
389
    template_setfloat(template, gensym("style"), x->x_scalar->sc_vec,
390
        x->x_style, 1);
Miller Puckette's avatar
Miller Puckette committed
391
    template_setfloat(template, gensym("linewidth"), x->x_scalar->sc_vec, 
392
        ((x->x_style == PLOTSTYLE_POINTS) ? 2 : 1), 1);
393
394
395
396
    template_setsymbol(template, gensym("fillcolor"), x->x_scalar->sc_vec,
        fill, 1);
    template_setsymbol(template, gensym("outlinecolor"), x->x_scalar->sc_vec,
        outline, 1);
Miller Puckette's avatar
Miller Puckette committed
397
398
399
400
    if (x2 = pd_findbyclass(gensym("#A"), garray_class))
        pd_unbind(x2, gensym("#A"));
    pd_bind(&x->x_gobj.g_pd, gensym("#A"));
    garray_redraw(x);
401
402
403

/* todo: need to test to see if this is necessary
   doesn't seem like it is...
404
    garray_fittograph(x, n, 1);
405
*/
Miller Puckette's avatar
Miller Puckette committed
406
407
408
409
410
411
    return (x);
}

    /* called from array menu item to create a new one */
void canvas_menuarray(t_glist *canvas)
{
412
    if (canvas_hasarray(canvas)) return;
Miller Puckette's avatar
Miller Puckette committed
413
    t_glist *x = (t_glist *)canvas;
414
    pd_vmess(&x->gl_pd, gensym("editmode"), "i", 1);
Miller Puckette's avatar
Miller Puckette committed
415
    char cmdbuf[200];
416
417
    sprintf(cmdbuf, "pdtk_array_dialog %%s array%d 100 3 1 .x%lx black black\n",
        ++gcount, (long unsigned int)canvas);
Miller Puckette's avatar
Miller Puckette committed
418
419
420
421
    gfxstub_new(&x->gl_pd, x, cmdbuf);
}

    /* called from graph_dialog to set properties */
422
void garray_properties(t_garray *x, t_glist *canvas)
Miller Puckette's avatar
Miller Puckette committed
423
424
425
426
427
428
429
430
431
432
{
    char cmdbuf[200];
    t_array *a = garray_getarray(x);
    if (!a)
        return;
    gfxstub_deleteforkey(x);
        /* create dialog window.  LATER fix this to escape '$'
        properly; right now we just detect a leading '$' and escape
        it.  There should be a systematic way of doing this. */
    sprintf(cmdbuf, ((x->x_name->s_name[0] == '$') ?
433
434
435
436
437
        "pdtk_array_dialog %%s \\%s %d %d 0 .x%lx %s %s\n" :
        "pdtk_array_dialog %%s %s %d %d 0 .x%lx %s %s\n"), x->x_name->s_name,
            a->a_n, x->x_saveit +  2 * x->x_style + 8 * x->x_hidename +
            16 * x->x_joc, (long unsigned int)glist_getcanvas(canvas),
             x->x_fillcolor->s_name, x->x_outlinecolor->s_name);
Miller Puckette's avatar
Miller Puckette committed
438
439
440
441
442
    gfxstub_new(&x->x_gobj.g_pd, x, cmdbuf);
}

    /* this is called back from the dialog window to create a garray. 
    The otherflag requests that we find an existing graph to put it in. */
443
444
void glist_arraydialog(t_glist *parent, t_symbol *s, int argc, t_atom *argv)
//t_floatarg size, t_floatarg fflags, t_floatarg otherflag, float xdraw, float ydraw)
Miller Puckette's avatar
Miller Puckette committed
445
{
446
447
448
449
450
451
452
453
454
455
456
457
    t_atom at[6];
    if (argc !=8) {pd_error(parent,
        "arraydialog: wrong number of args"); return;}
    t_float size, fflags, otherflag, xdraw, ydraw;
    t_symbol *name = atom_getsymbolarg(0, argc, argv);
    size = atom_getfloatarg(1, argc, argv);
    fflags = atom_getfloatarg(2, argc, argv);
    otherflag = atom_getfloatarg(3, argc, argv);
    xdraw = atom_getfloatarg(4, argc, argv);
    ydraw = atom_getfloatarg(5, argc, argv);
    t_symbol *fillcolor = atom_getsymbolarg(6, argc, argv);
    t_symbol *outlinecolor = atom_getsymbolarg(7, argc, argv);
458

Miller Puckette's avatar
Miller Puckette committed
459
460
461
462
463
    t_glist *gl;
    t_garray *a;
    int flags = fflags;
    if (size < 1)
        size = 1;
464
465
    if (otherflag == 0 || (!(gl = glist_findgraph(parent))))
    {
Miller Puckette's avatar
Miller Puckette committed
466
        gl = glist_addglist(parent, &s_, 0, 1,
467
468
            (size > 1 ? size-1 : size), -1, xdraw+30, ydraw+30,
            xdraw+30+GLIST_DEFGRAPHWIDTH, ydraw+30+GLIST_DEFGRAPHHEIGHT);
469
470
        gl->gl_hidetext = 1;
    }
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
    //a = graph_array(gl, sharptodollar(name), &s_float, size, flags);
    SETSYMBOL(at, sharptodollar(name));
    SETFLOAT(at+1, size);
    SETSYMBOL(at+2, &s_float);
    SETFLOAT(at+3, flags);

    /* no idea what xdraw and ydraw are used for...
       graph_array doesn't seem to use them 
    SETFLOAT(at+4, xdraw);
    SETFLOAT(at+5, ydraw);
    */

    SETSYMBOL(at+4, fillcolor);
    SETSYMBOL(at+5, outlinecolor);
    a = graph_array(gl, gensym("array"), 8, at);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
486
    canvas_dirty(parent, 1);
487
    
488
    //canvas_redraw(glist_getcanvas(parent));
489
    garray_fittograph(a, (int)size, 1);
490
491
    sys_vgui("pdtk_canvas_getscroll .x%lx.c\n",
        (long unsigned int)glist_getcanvas(parent));
Miller Puckette's avatar
Miller Puckette committed
492
493
}

494
495
extern void canvas_apply_setundo(t_canvas *x, t_gobj *y);

Miller Puckette's avatar
Miller Puckette committed
496
    /* this is called from the properties dialog window for an existing array */
497
void garray_arraydialog(t_garray *x, t_symbol *s, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
498
{
499
500
501
    //fprintf(stderr,"================garray_arraydialog\n");

    int deleteit = atom_getfloatarg(3, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
502
503
    if (deleteit != 0)
    {
504
505
506
507
508
        //fprintf(stderr,"deleteit\n");
        //glist_select(x->x_glist, &x->x_gobj);
        //canvas_undo_add(x->x_glist, 3, "delete", canvas_undo_set_cut(x->x_glist, 2)); // 2 = UCUT_CLEAR (from g_editor.c)
//currently cannot be undo'd until we do a new kind of undo
        int dspwas = canvas_suspend_dsp();
Miller Puckette's avatar
Miller Puckette committed
509
        glist_delete(x->x_glist, &x->x_gobj);
510
511
        canvas_resume_dsp(dspwas);
        canvas_redraw(glist_getcanvas(x->x_glist));
Miller Puckette's avatar
Miller Puckette committed
512
    }
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    else 
    {
        //need a new kind of undo
        //canvas_apply_setundo(glist_getcanvas(x->x_glist), (t_gobj *)x);

        if (argc !=8)
        {
            pd_error(x, "arraydialog: wrong number of args"); return;
        }
        t_symbol *name = atom_getsymbolarg(0, argc, argv);
        int fsize = atom_getfloatarg(1, argc, argv);
        int flags = atom_getfloatarg(2, argc, argv);
        t_symbol *fill = atom_getsymbolarg(6, argc, argv);
        t_symbol *outline = atom_getsymbolarg(7, argc, argv);
527
528
529
        int saveit = ((flags & 1) != 0);
        int style = ((flags & 6) >> 1);

530
531
532
        /* todo: revisit this filestyle business
        if (style < 2) style = !style;
        */
533

534
535
        int hidename = ((flags & 8) >> 3);
        int joc = ((flags & 16) >> 4);
536
537
538
539
540

        /*t_float stylewas = template_getfloat(
        template_findbyname(x->x_scalar->sc_template),
        gensym("style"), x->x_scalar->sc_vec, 1);*/

Miller Puckette's avatar
Miller Puckette committed
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
        int size;
        t_symbol *argname = sharptodollar(name);
        t_array *a = garray_getarray(x);
        t_template *scalartemplate;
        if (!a)
        {
            pd_error(x, "can't find array\n");
            return;
        }
        if (!(scalartemplate = template_findbyname(x->x_scalar->sc_template)))
        {
            error("array: no template of type %s",
                x->x_scalar->sc_template->s_name);
            return;
        }
        if (argname != x->x_name)
        {
            /* jsarlo { */
            if (x->x_listviewing)
            {
              garray_arrayviewlist_close(x);
            }
            /* } jsarlo */
            x->x_name = argname;
            pd_unbind(&x->x_gobj.g_pd, x->x_realname);
            x->x_realname = canvas_realizedollar(x->x_glist, argname);
            pd_bind(&x->x_gobj.g_pd, x->x_realname);
        }
569
            /* redraw the whole glist, just so the name change shows up */
570
571
        if (x->x_glist->gl_havewindow)
        {
572
            canvas_redraw(glist_getcanvas(x->x_glist));
573
574
            //fprintf(stderr,"================REDRAW\n");
        }
Miller Puckette's avatar
Miller Puckette committed
575
576
577
578
579
        size = fsize;
        if (size < 1)
            size = 1;
        if (size != a->a_n)
            garray_resize(x, size);
580
581
582
        if (style != x->x_style)
        {
            x->x_style = style;
583
            garray_fittograph(x, size, 1);
584
585
        }
        //fprintf(stderr,"style=%d %f\n", style, (t_float)x->x_style);
Miller Puckette's avatar
Miller Puckette committed
586
        template_setfloat(scalartemplate, gensym("style"),
587
            x->x_scalar->sc_vec, (t_float)x->x_style, 0);
588
589
        template_setfloat(scalartemplate, gensym("linewidth"),
            x->x_scalar->sc_vec, ((x->x_style == PLOTSTYLE_POINTS) ? 2 : 1), 1);
590
591
592
593
        template_setsymbol(scalartemplate, gensym("fillcolor"),
            x->x_scalar->sc_vec, fill, 0);
        template_setsymbol(scalartemplate, gensym("outlinecolor"),
            x->x_scalar->sc_vec, outline, 0);
594

595
596
597
        char buf[MAXPDSTRING];
        sprintf(buf, "%s_changed", x->x_realname->s_name);
        x->x_send = gensym(buf);
Miller Puckette's avatar
Miller Puckette committed
598
599

        garray_setsaveit(x, (saveit != 0));
600
601
602
603
604
        x->x_joc = joc;
        x->x_hidename = hidename;
        x->x_fillcolor = fill;
        x->x_outlinecolor = outline;
        x->x_style = style;
605
        //fprintf(stderr,"GARRAY_REDRAW\n");
Miller Puckette's avatar
Miller Puckette committed
606
        garray_redraw(x);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
607
        canvas_dirty(x->x_glist, 1);
Miller Puckette's avatar
Miller Puckette committed
608
609
610
611
612
613
    }
}

/* jsarlo { */
void garray_arrayviewlist_new(t_garray *x)
{
614
    int i, type=0, elemsize=0, yonset=0;
615
    t_float yval;
Miller Puckette's avatar
Miller Puckette committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
    char cmdbuf[200];
    t_array *a = garray_getarray_floatonly(x, &yonset, &elemsize);

    if (!a)
    {
        /* FIXME */
        error("error in garray_arrayviewlist_new()");
    }
    x->x_listviewing = 1;
    sprintf(cmdbuf,
            "pdtk_array_listview_new %%s %s %d\n",
            x->x_realname->s_name,
            0);
    gfxstub_new(&x->x_gobj.g_pd, x, cmdbuf);
    for (i = 0; i < ARRAYPAGESIZE && i < a->a_n; i++)
    {
632
        yval = *(t_float *)(a->a_vec +
Miller Puckette's avatar
Miller Puckette committed
633
634
635
636
637
638
639
640
641
642
643
644
645
               elemsize * i + yonset);
        sys_vgui(".%sArrayWindow.lb insert %d {%d) %g}\n",
                 x->x_realname->s_name,
                 i,
                 i,
                 yval);
    }
}

void garray_arrayviewlist_fillpage(t_garray *x,
                                   t_float page,
                                   t_float fTopItem)
{
646
    //fprintf(stderr,"garray_fillpage\n");
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
647
    int i, yonset=0, elemsize=0, topItem;
648
    t_float yval;
Miller Puckette's avatar
Miller Puckette committed
649
650
651
652
653
654
655
656
657
    t_array *a = garray_getarray_floatonly(x, &yonset, &elemsize);
    
    topItem = (int)fTopItem;
    if (!a)
    {
        /* FIXME */
        error("error in garray_arrayviewlist_new()");
    }

658
659
    if (page < 0)
    {
Miller Puckette's avatar
Miller Puckette committed
660
661
662
663
664
      page = 0;
      sys_vgui("pdtk_array_listview_setpage %s %d\n",
               x->x_realname->s_name,
               (int)page);
    }
665
666
    else if ((page * ARRAYPAGESIZE) >= a->a_n)
    {
Miller Puckette's avatar
Miller Puckette committed
667
668
669
670
671
672
673
674
675
676
677
678
      page = (int)(((int)a->a_n - 1)/ (int)ARRAYPAGESIZE);
      sys_vgui("pdtk_array_listview_setpage %s %d\n",
               x->x_realname->s_name,
               (int)page);
    }
    sys_vgui(".%sArrayWindow.lb delete 0 %d\n",
             x->x_realname->s_name,
             ARRAYPAGESIZE - 1);
    for (i = page * ARRAYPAGESIZE;
         (i < (page + 1) * ARRAYPAGESIZE && i < a->a_n);
         i++)
    {
679
        yval = *(t_float *)(a->a_vec + \
Miller Puckette's avatar
Miller Puckette committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
               elemsize * i + yonset);
        sys_vgui(".%sArrayWindow.lb insert %d {%d) %g}\n",
                 x->x_realname->s_name,
                 i % ARRAYPAGESIZE,
                 i,
                 yval);
    }
    sys_vgui(".%sArrayWindow.lb yview %d\n",
             x->x_realname->s_name,
             topItem);
}

void garray_arrayviewlist_close(t_garray *x)
{
    x->x_listviewing = 0;
    sys_vgui("pdtk_array_listview_closeWindow %s\n",
             x->x_realname->s_name);
}
/* } jsarlo */

static void garray_free(t_garray *x)
{
    t_pd *x2;
        sys_unqueuegui(&x->x_gobj);
    /* jsarlo { */
        if (x->x_listviewing)
    {
        garray_arrayviewlist_close(x);
    }
    /* } jsarlo */
    gfxstub_deleteforkey(x);
    pd_unbind(&x->x_gobj.g_pd, x->x_realname);
        /* LATER find a way to get #A unbound earlier (at end of load?) */
    while (x2 = pd_findbyclass(gensym("#A"), garray_class))
        pd_unbind(x2, gensym("#A"));
    pd_free(&x->x_scalar->sc_gobj.g_pd);
}

/* ------------- code used by both array and plot widget functions ---- */

void array_redraw(t_array *a, t_glist *glist)
{
    while (a->a_gp.gp_stub->gs_which == GP_ARRAY)
        a = a->a_gp.gp_stub->gs_un.gs_array;
    scalar_redraw(a->a_gp.gp_un.gp_scalar, glist);
}

    /* routine to get screen coordinates of a point in an array */
void array_getcoordinate(t_glist *glist,
    char *elem, int xonset, int yonset, int wonset, int indx,
730
    t_float basex, t_float basey, t_float xinc,
Miller Puckette's avatar
Miller Puckette committed
731
    t_fielddesc *xfielddesc, t_fielddesc *yfielddesc, t_fielddesc *wfielddesc,
732
    t_float *xp1, t_float *xp2, t_float *yp, t_float *wp)
Miller Puckette's avatar
Miller Puckette committed
733
{
734
    t_float xval, yval, ypix, wpix;
Miller Puckette's avatar
Miller Puckette committed
735
    if (xonset >= 0)
736
        xval = *(t_float *)(elem + xonset);
Miller Puckette's avatar
Miller Puckette committed
737
738
    else xval = indx * xinc;
    if (yonset >= 0)
739
        yval = *(t_float *)(elem + yonset);
Miller Puckette's avatar
Miller Puckette committed
740
741
742
743
744
745
    else yval = 0;
    ypix = glist_ytopixels(glist, basey +
        fielddesc_cvttocoord(yfielddesc, yval));
    if (wonset >= 0)
    {
            /* found "w" field which controls linewidth. */
746
        t_float wval = *(t_float *)(elem + wonset);
Miller Puckette's avatar
Miller Puckette committed
747
748
749
750
751
752
753
        wpix = glist_ytopixels(glist, basey + 
            fielddesc_cvttocoord(yfielddesc, yval) +
                fielddesc_cvttocoord(wfielddesc, wval)) - ypix;
        if (wpix < 0)
            wpix = -wpix;
    }
    else wpix = 1;
754
    *xp1 = glist_xtopixels(glist, basex +
Miller Puckette's avatar
Miller Puckette committed
755
        fielddesc_cvttocoord(xfielddesc, xval));
756
757
    *xp2 = glist_xtopixels(glist, basex +
        fielddesc_cvttocoord(xfielddesc, xval+1))-1;
Miller Puckette's avatar
Miller Puckette committed
758
759
760
761
    *yp = ypix;
    *wp = wpix;
}

762
extern int array_joc; /* from g_canvas.h */
763
764
static t_float array_motion_xcumulative;
static t_float array_motion_ycumulative;
Miller Puckette's avatar
Miller Puckette committed
765
766
767
768
769
770
771
772
773
static t_fielddesc *array_motion_xfield;
static t_fielddesc *array_motion_yfield;
static t_glist *array_motion_glist;
static t_scalar *array_motion_scalar;
static t_array *array_motion_array;
static t_word *array_motion_wp;
static t_template *array_motion_template;
static int array_motion_npoints;
static int array_motion_elemsize;
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
774
//static int array_motion_altkey;
775
776
777
static t_float array_motion_initx;
static t_float array_motion_xperpix;
static t_float array_motion_yperpix;
Miller Puckette's avatar
Miller Puckette committed
778
779
780
781
782
783
static int array_motion_lastx;
static int array_motion_fatten;

    /* LATER protect against the template changing or the scalar disappearing
    probably by attaching a gpointer here ... */

784
785
    /* this is called when a mouse drag happens inside an array (either
       scalar or the whole array--this needs to be tested) */
Miller Puckette's avatar
Miller Puckette committed
786
787
788
789
static void array_motion(void *z, t_floatarg dx, t_floatarg dy)
{
    array_motion_xcumulative += dx * array_motion_xperpix;
    array_motion_ycumulative += dy * array_motion_yperpix;
790
    //fprintf(stderr,"array_motion %f %f %f %f\n", array_motion_xcumulative, array_motion_ycumulative, dx, dy);
791

792
793
794
    // used to set up boundaries and update sends accordingly
    t_glist *graph = NULL;
    if (array_garray != NULL) graph = array_garray->x_glist;
795

Miller Puckette's avatar
Miller Puckette committed
796
797
798
799
800
801
802
803
    if (array_motion_xfield)
    {
            /* it's an x, y plot */
        int i;
        for (i = 0; i < array_motion_npoints; i++)
        {
            t_word *thisword = (t_word *)(((char *)array_motion_wp) +
                i * array_motion_elemsize);
804
            t_float xwas = fielddesc_getcoord(array_motion_xfield, 
Miller Puckette's avatar
Miller Puckette committed
805
                array_motion_template, thisword, 1);
806
            t_float ywas = (array_motion_yfield ?
Miller Puckette's avatar
Miller Puckette committed
807
808
809
810
811
812
813
814
815
816
                fielddesc_getcoord(array_motion_yfield, 
                    array_motion_template, thisword, 1) : 0);
            fielddesc_setcoord(array_motion_xfield,
                array_motion_template, thisword, xwas + dx, 1);
            if (array_motion_yfield)
            {
                if (array_motion_fatten)
                {
                    if (i == 0)
                    {
817
                        t_float newy = ywas + dy * array_motion_yperpix;
Miller Puckette's avatar
Miller Puckette committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
                        if (newy < 0)
                            newy = 0;
                        fielddesc_setcoord(array_motion_yfield,
                            array_motion_template, thisword, newy, 1);
                    }
                }
                else
                {
                    fielddesc_setcoord(array_motion_yfield,
                        array_motion_template, thisword,
                            ywas + dy * array_motion_yperpix, 1);
                }
            }
        }
    }
    else if (array_motion_yfield)
    {
            /* a y-only plot. */
        int thisx = array_motion_initx + array_motion_xcumulative + 0.5, x2;
        int increment, i, nchange;
838
        t_float newy = array_motion_ycumulative,
Miller Puckette's avatar
Miller Puckette committed
839
840
841
842
843
            oldy = fielddesc_getcoord(array_motion_yfield,
                array_motion_template,
                    (t_word *)(((char *)array_motion_wp) +
                        array_motion_elemsize * array_motion_lastx),
                            1);
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
        // here we block scalar from exceeding the array GOP edges
        if (graph)
        {
            if (graph->gl_y1 > graph->gl_y2)
            {
                if (newy > graph->gl_y1) newy = graph->gl_y1;
                if (newy < graph->gl_y2) newy = graph->gl_y2;
            }
            else
            {
                if (newy < graph->gl_y1) newy = graph->gl_y1;
                if (newy > graph->gl_y2) newy = graph->gl_y2;
            }
        }
        //fprintf(stderr, "y = %f\n", newy);
859
        t_float ydiff = newy - oldy;
Miller Puckette's avatar
Miller Puckette committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
        if (thisx < 0) thisx = 0;
        else if (thisx >= array_motion_npoints)
            thisx = array_motion_npoints - 1;
        increment = (thisx > array_motion_lastx ? -1 : 1);
        nchange = 1 + increment * (array_motion_lastx - thisx);

        for (i = 0, x2 = thisx; i < nchange; i++, x2 += increment)
        {
            fielddesc_setcoord(array_motion_yfield,
                array_motion_template,
                    (t_word *)(((char *)array_motion_wp) +
                        array_motion_elemsize * x2), newy, 1);
            if (nchange > 1)
                newy -= ydiff * (1./(nchange - 1));
         }
         array_motion_lastx = thisx;
    }
877
    //fprintf(stderr, "%f %f\n", graph->gl_y1, graph->gl_y2);
878

Miller Puckette's avatar
Miller Puckette committed
879
880
881
882
    if (array_motion_scalar)
        scalar_redraw(array_motion_scalar, array_motion_glist);
    if (array_motion_array)
        array_redraw(array_motion_array, array_motion_glist);
883

884
885
886
887
    /* send a bang to the associated send to reflect the change
       via mouse click/drag */
    if (graph && array_garray->x_send->s_thing)
        pd_bang(array_garray->x_send->s_thing);
Miller Puckette's avatar
Miller Puckette committed
888
889
890
891
}

int scalar_doclick(t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
892
    t_float xloc, t_float yloc, int xpix, int ypix,
Miller Puckette's avatar
Miller Puckette committed
893
894
895
896
897
898
    int shift, int alt, int dbl, int doit);
    /* try clicking on an element of the array as a scalar (if clicking
    on the trace of the array failed) */
static int array_doclick_element(t_array *array, t_glist *glist,
    t_scalar *sc, t_array *ap,
    t_symbol *elemtemplatesym,
899
    t_float linewidth, t_float xloc, t_float xinc, t_float yloc,
Miller Puckette's avatar
Miller Puckette committed
900
901
902
    t_fielddesc *xfield, t_fielddesc *yfield, t_fielddesc *wfield,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
903
    //fprintf(stderr,"array_doclick_element linewidth%f xloc%f xinc%f yloc%f xpix%d ypix%d doit%d\n", linewidth, xloc, xinc, yloc, xpix, ypix, doit);
Miller Puckette's avatar
Miller Puckette committed
904
905
906
    t_canvas *elemtemplatecanvas;
    t_template *elemtemplate;
    int elemsize, yonset, wonset, xonset, i, incr, hit;
907
    t_float xsum;
Miller Puckette's avatar
Miller Puckette committed
908
909
910
911
912
913
914
915
916
917
918
919
920

    if (elemtemplatesym == &s_float)
        return (0);
    if (array_getfields(elemtemplatesym, &elemtemplatecanvas,
        &elemtemplate, &elemsize, xfield, yfield, wfield,
            &xonset, &yonset, &wonset))
                return (0);
        /* if it has more than 2000 points, just check 300 of them. */
    if (array->a_n < 2000)
        incr = 1;
    else incr = array->a_n / 300;
    for (i = 0, xsum = 0; i < array->a_n; i += incr)
    {
921
        t_float usexloc, useyloc;
Miller Puckette's avatar
Miller Puckette committed
922
923
        if (xonset >= 0)
            usexloc = xloc + fielddesc_cvttocoord(xfield, 
924
                *(t_float *)(((char *)(array->a_vec) + elemsize * i) + xonset));
Miller Puckette's avatar
Miller Puckette committed
925
926
        else usexloc = xloc + xsum, xsum += xinc;
        useyloc = yloc + (yonset >= 0 ? fielddesc_cvttocoord(yfield,
927
            *(t_float *)(((char *)(array->a_vec) + elemsize * i) + yonset)) : 0);
Miller Puckette's avatar
Miller Puckette committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
        
        if (hit = scalar_doclick(
            (t_word *)((char *)(array->a_vec) + i * elemsize),
            elemtemplate, 0, array,
            glist, usexloc, useyloc,
            xpix, ypix, shift, alt, dbl, doit))
                return (hit);
    }
    return (0);
}

    /* LATER move this and others back into plot parentwidget code, so
    they can be static (look in g_canvas.h for candidates). */
int array_doclick(t_array *array, t_glist *glist, t_scalar *sc, t_array *ap,
    t_symbol *elemtemplatesym,
943
    t_float linewidth, t_float xloc, t_float xinc, t_float yloc, t_float scalarvis,
Miller Puckette's avatar
Miller Puckette committed
944
945
946
    t_fielddesc *xfield, t_fielddesc *yfield, t_fielddesc *wfield,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
947
    //fprintf(stderr,"array_doclick linewidth%f xloc%f xinc%f yloc%f xpix%d ypix%d doit%d\n", linewidth, xloc, xinc, yloc, xpix, ypix, doit);
Miller Puckette's avatar
Miller Puckette committed
948
949
950
951
952
953
954
955
    t_canvas *elemtemplatecanvas;
    t_template *elemtemplate;
    int elemsize, yonset, wonset, xonset, i;

    if (!array_getfields(elemtemplatesym, &elemtemplatecanvas,
        &elemtemplate, &elemsize, xfield, yfield, wfield,
        &xonset, &yonset, &wonset))
    {
956
        t_float best = -1;
Miller Puckette's avatar
Miller Puckette committed
957
958
            /* if it has more than 2000 points, just check 1000 of them. */
        int incr = (array->a_n <= 2000 ? 1 : array->a_n / 1000);
959
        t_float pxpix1, pxpix2, pypix, pwpix, dx, dy, dy2, dy3;
Miller Puckette's avatar
Miller Puckette committed
960
961
962
963
        for (i = 0; i < array->a_n; i += incr)
        {
            array_getcoordinate(glist, (char *)(array->a_vec) + i * elemsize,
                xonset, yonset, wonset, i, xloc, yloc, xinc,
964
                xfield, yfield, wfield, &pxpix1, &pxpix2, &pypix, &pwpix);
965
            //fprintf(stderr,"    array_getcoordinate %d: pxpix1:%f pxpix2:%f pypix:%f pwpix:%f dx:%f dy:%f elemsize:%d yonset:%d wonset:%d xonset:%d xloc:%f yloc:%f xinc:%f\n", i, pxpix1, pxpix2, pypix, pwpix, dx, dy, elemsize, yonset, wonset, xonset, xloc, yloc, xinc);
Miller Puckette's avatar
Miller Puckette committed
966
967
            if (pwpix < 4)
                pwpix = 4;
968
969
970
971
972
973
974
            if (xpix >= (int)pxpix1 && xpix <= (int)pxpix2 &&
                ((array_joc) ||
                 (ypix >= pypix-pwpix && ypix <= pypix+pwpix)))
            {
                best = i;
                break;
            }
975
976
977
978
/*
            if (pwpix < 4)
                pwpix = 4;
            dx = pxpix1 - xpix;
Miller Puckette's avatar
Miller Puckette committed
979
            if (dx < 0) dx = -dx;
980
            if (dx > pxpix2-pxpix1) //this is the arbitrary radius away from the actual object's center, originally 8
Miller Puckette's avatar
Miller Puckette committed
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
                continue;   
            dy = pypix - ypix;
            if (dy < 0) dy = -dy;
            if (dx + dy < best)
                best = dx + dy;
            if (wonset >= 0)
            {
                dy = (pypix + pwpix) - ypix;
                if (dy < 0) dy = -dy;
                if (dx + dy < best)
                    best = dx + dy;
                dy = (pypix - pwpix) - ypix;
                if (dy < 0) dy = -dy;
                if (dx + dy < best)
                    best = dx + dy;
            }
997
            //fprintf(stderr,"    1st %f %f %f %f %f %d %d %d %d %d\n", pxpix, pypix, pwpix, dx, dy, elemsize, yonset, wonset, xonset, i);*/
Miller Puckette's avatar
Miller Puckette committed
998
        }
999
        //fprintf(stderr,"    best = %f\n", best);
1000
        if (best == -1 && (array_joc == 0)) //this is the arbitrary radius away from the actual object's center, originally 8