g_array.c 67.3 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
/* 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>
11
#include <ctype.h>
Miller Puckette's avatar
Miller Puckette committed
12

13
14
extern int glob_lmclick;

15
static void garray_select(t_gobj *z, t_glist *glist, int state);
16
static void garray_doredraw(t_gobj *client, t_glist *glist);
17

Miller Puckette's avatar
Miller Puckette committed
18
19
20
21
22
23
24
25
/* 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!) */

26
t_symbol *sharptodollar(t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
27
{
28
29
30
31
    char buf[MAXPDSTRING], *s1, *s2;
    if (strlen(s->s_name) >= MAXPDSTRING)
        return (s);
    for (s1 = s->s_name, s2 = buf; ; s1++, s2++)
Miller Puckette's avatar
Miller Puckette committed
32
    {
33
34
35
36
        if (*s1 == '#' && *s1 && isdigit(s1[1]))
            *s2 = '$';
        else if (!(*s2 = *s1))
            break;
Miller Puckette's avatar
Miller Puckette committed
37
    }
38
    return (gensym(buf));
Miller Puckette's avatar
Miller Puckette committed
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
}

/* --------- "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);
}

void array_resize(t_array *x, int n)
{
68
    //fprintf(stderr,"array_resize %d\n", n);
Miller Puckette's avatar
Miller Puckette committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    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;
}

91
void array_resize_and_redraw(t_array *array, t_glist *glist, int n)
Miller Puckette's avatar
Miller Puckette committed
92
{
93
    //fprintf(stderr,"array_resize_and_redraw\n");
Miller Puckette's avatar
Miller Puckette committed
94
95
96
97
98
    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)
99
        gobj_vis(a2->a_gp.gp_un.gp_gobj, glist, 0);
Miller Puckette's avatar
Miller Puckette committed
100
101
    array_resize(array, n);
    if (vis)
102
        gobj_vis(a2->a_gp.gp_un.gp_gobj, glist, 1);
Miller Puckette's avatar
Miller Puckette committed
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
}

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 */
135
    char x_joc;             /* true if we should "jump on click" in a graph */
Miller Puckette's avatar
Miller Puckette committed
136
    char x_hidename;        /* don't print name above graph */
137
138
139
140
    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
141
142
};

143
144
t_pd *garray_arraytemplatecanvas;
t_pd *garray_floattemplatecanvas;
Miller Puckette's avatar
Miller Puckette committed
145
146
147
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\
148
float linewidth float color symbol fillcolor symbol outlinecolor;\n\
149
#X obj 43 70 plot z color linewidth 0 0 1 style fillcolor outlinecolor;\n\
Miller Puckette's avatar
Miller Puckette committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
";
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);
169
    garray_floattemplatecanvas = s__X.s_thing;
Miller Puckette's avatar
Miller Puckette committed
170
171
172
173
174
175
176
177
178
    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_);
179
    binbuf_free(b);
Miller Puckette's avatar
Miller Puckette committed
180
181
182
183
184
185
186
187
}

/* 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,
188
    t_symbol *fill, t_symbol *outline, int saveit)
Miller Puckette's avatar
Miller Puckette committed
189
190
191
{
    t_garray *x;
    t_template *template;
192
193
    if (!(template = template_findbyname(templatesym)))
        return (0);
Miller Puckette's avatar
Miller Puckette committed
194
195
196
197
198
199
200
    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;
201
202
203
204
    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
205
206
    glist_add(gl, &x->x_gobj);
    x->x_glist = gl;
207
208
209
    char buf[MAXPDSTRING];
    sprintf(buf, "%s_changed", x->x_realname->s_name);
    x->x_send = gensym(buf);
Miller Puckette's avatar
Miller Puckette committed
210
211
212
213
214
215
    return (x);
}

    /* get a garray's "array" structure. */
t_array *garray_getarray(t_garray *x)
{
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
216
    int zonset, ztype;
Miller Puckette's avatar
Miller Puckette committed
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
254
255
256
257
258
259
260
261
262
263
    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);
}

264
265
266
267
268
269
    /* find out if array elements should "jump on click" in a graph */
int garray_joc(t_garray *x)
{
    return (x->x_joc);
}

270
271
272
273
274
275
276
277
278
279
280
281
    /* get a garray's containing glist */
t_glist *garray_getglist(t_garray *x)
{
    return (x->x_glist);
}

    /* get a garray's associated scalar */
t_scalar *garray_getscalar(t_garray *x)
{
    return (x->x_scalar);
}

282
283
284
285
286
287
    /* helper function for fittograph to see if the same GOP has multiple
        arrays in which case take length of the largest one */
static int garray_get_largest_array(t_garray *x)
{
    t_gobj *g;
    t_array *a;
288
289
290
291
292
293
294
295
296
297
298
    t_garray *tmp;

    // checks if there is a PLOTSTYLE_POLY vs others
    // longest array uses has_*, total_* gives total number
    // n_array gives you the number of arrays
    int has_poly = 0;
    int total_poly = 0;
    int has_other = 0;
    int total_other = 0;
    int n_array = 0;

299
    int length = 0;
300

301
302
303
304
    for (g = x->x_glist->gl_list; g; g = g->g_next)
    {
        if (pd_class(&g->g_pd) == garray_class)
        {
305
            n_array++;
306
307
308
309
310
311
312
313
314
315
316
317
            a = garray_getarray((t_garray *)g);
            //if ((t_garray *)g != x)
            //{
            //    an = (x->x_style == PLOTSTYLE_POINTS
            //        || x->x_style == PLOTSTYLE_BARS
            //        || a->a_n == 1 ? a->a_n : a->a_n-1);
            //    if (an > length)
            //        length = an;               
            //}
            //else
            //{
                a = garray_getarray((t_garray *)g);
318
319
320
321
                tmp = (t_garray *)g;

                // if we have new longest plot reset the
                // count for the longest plot style
322
                if (a->a_n > length)
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
                {
                    has_poly = 0;
                    has_other = 0;
                }

                // get the longest (or matching) plots style
                if (a->a_n >= length)
                {
                    if (tmp->x_style == PLOTSTYLE_POLY)
                    {
                        has_poly++;                      
                    }
                    else
                    {
                        has_other++;
                    }
339
                    length = a->a_n;
340
341
342
343
344
345
346
347
348
349
350
                }

                // adjust total number of plot styles
                if (tmp->x_style == PLOTSTYLE_POLY)
                {
                    total_poly++;                        
                }
                else
                {
                    total_other++;
                }
351
352
353
            //}
        }
    }
354
355
356
357
358
359
360
361
362
363
364
365
366
    //fprintf(stderr, "has_poly=%d has_other=%d | total_poly=%d total_other=%d | n_array=%d | pre-length=%d ",
    //        has_poly, has_other, total_poly, total_other, n_array, length);
    if (has_poly && !has_other)
    {
        if (total_other)
            length--;
    }
    if (!has_poly && has_other)
    {
        if (total_poly)
            length++;
    }
    //fprintf(stderr, "post-lenght=%d\n", length);
367
368
369
370
    return(length);
}

    /* reset the graph's coordinates
371
        to fit a new size and style for the garray */
372
373
374
375
376
377
    /* in pd-l2ork we use this for all situations, both one or more
        arrays to ensure that they are always drawn within boundaries. */
    /* flag options:
        -1 = don't resize or redraw, just send bounds message to scalars
         0 = bounds, and also resize
         1 = bounds, resize, and redraw */
378
void garray_fittograph(t_garray *x, int n, int flag)
Miller Puckette's avatar
Miller Puckette committed
379
{
380
    //fprintf(stderr,"garray_fittograph\n");
381
382
383
384
    // 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;
385
386
    int max_length = garray_get_largest_array(x);
    //fprintf(stderr,"garray_fittograph n=%d flag=%d | max_length=%d\n", n, flag, max_length);
Miller Puckette's avatar
Miller Puckette committed
387
    t_array *array = garray_getarray(x);
388
    t_garray *tmp;
Miller Puckette's avatar
Miller Puckette committed
389
    t_glist *gl = x->x_glist;
390
391
392
393
    //if (gl->gl_list == &x->x_gobj && !x->x_gobj.g_next)

    t_gobj *g;
    for (g = gl->gl_list; g; g = g->g_next)
Miller Puckette's avatar
Miller Puckette committed
394
    {
395
396
397
398
399
400
401
402
403
404
405
406
407
        if (pd_class(&g->g_pd) == garray_class)
        {
            //fprintf(stderr,"found array\n");
            tmp = (t_garray *)g;
            vmess(&gl->gl_pd, gensym("bounds"), "ffff",
                0., gl->gl_y1, (double)
                    (tmp->x_style == PLOTSTYLE_POINTS || tmp->x_style == PLOTSTYLE_BARS
                        || max_length == 1 ? max_length : max_length-1),
                        gl->gl_y2);
        }
        /* 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 */
408
        //gfxstub_deleteforkey(gl);
Miller Puckette's avatar
Miller Puckette committed
409
    }
410
411
412
413
414
415
416
    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
417
418
419
420
421
422
423
}

/* 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. */

424
t_garray *graph_array(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
425
{
426
427
428
    t_symbol *fill;
    t_symbol *outline;
    int fflags;
429
    t_symbol *asym = gensym("#A");
430
431
432
433
434
435
436
437
438
439
440

    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
441
    int n = fsize, zonset, ztype, saveit;
Miller Puckette's avatar
Miller Puckette committed
442
443
444
445
446
447
    t_symbol *zarraytype;
    t_garray *x;
    t_template *template, *ztemplate;
    t_symbol *templatesym;
    int flags = fflags;
    int filestyle = ((flags & 6) >> 1);
448
    //fprintf(stderr,"filestyle = %d\n", filestyle);
449
450
    int style = (filestyle == 0 ? PLOTSTYLE_POLY :
        (filestyle == 1 ? PLOTSTYLE_POINTS : filestyle));
Miller Puckette's avatar
Miller Puckette committed
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
    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);
481
    x = graph_scalar(gl, name, templatesym, fill, outline, saveit);
Miller Puckette's avatar
Miller Puckette committed
482
    x->x_hidename = ((flags & 8) >> 3);
483
484
485
486
    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
487
488
489
490
491

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

492
493
494
495
496
    // 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);
497
498
    //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;
499

Miller Puckette's avatar
Miller Puckette committed
500
    template_setfloat(template, gensym("style"), x->x_scalar->sc_vec,
501
        x->x_style, 1);
Miller Puckette's avatar
Miller Puckette committed
502
    template_setfloat(template, gensym("linewidth"), x->x_scalar->sc_vec, 
503
        ((x->x_style == PLOTSTYLE_POINTS) ? 2 : 1), 1);
504
505
506
507
    template_setsymbol(template, gensym("fillcolor"), x->x_scalar->sc_vec,
        fill, 1);
    template_setsymbol(template, gensym("outlinecolor"), x->x_scalar->sc_vec,
        outline, 1);
508
509
510
511
512
513
514
515
516
517

           /* bashily unbind #A -- this would create garbage if #A were
           multiply bound but we believe in this context it's at most
           bound to whichever textobj or array was created most recently */
    asym->s_thing = 0;
        /* and now bind #A to us to receive following messages in the
        saved file or copy buffer */
    pd_bind(&x->x_gobj.g_pd, asym);

    /*if (x2 = pd_findbyclass(gensym("#A"), garray_class))
Miller Puckette's avatar
Miller Puckette committed
518
        pd_unbind(x2, gensym("#A"));
519
520
    pd_bind(&x->x_gobj.g_pd, gensym("#A"));*/

Miller Puckette's avatar
Miller Puckette committed
521
    garray_redraw(x);
522
523
524

/* todo: need to test to see if this is necessary
   doesn't seem like it is...
525
    garray_fittograph(x, n, 1);
526
*/
Miller Puckette's avatar
Miller Puckette committed
527
528
529
530
531
532
    return (x);
}

    /* called from array menu item to create a new one */
void canvas_menuarray(t_glist *canvas)
{
533
    if (canvas_hasarray(canvas)) return;
Miller Puckette's avatar
Miller Puckette committed
534
    t_glist *x = (t_glist *)canvas;
535
    pd_vmess(&x->gl_pd, gensym("editmode"), "i", 1);
536
537
    //char cmdbuf[200];
    gcount++;
538
    gui_vmess("gui_array_new", "si",
539
        gfxstub_new2(&x->gl_pd, x),
540
541
542
543
        gcount);
    //sprintf(cmdbuf, "pdtk_array_dialog %%s array%d 100 3 1 .x%lx black black\n",
    //    ++gcount, (long unsigned int)canvas);
    //gfxstub_new(&x->gl_pd, x, cmdbuf);
Miller Puckette's avatar
Miller Puckette committed
544
545
}

546
547
548
    /* called from canvas_dialog to return array properties for the gui */
int garray_properties(t_garray *x, t_symbol **gfxstubp, t_symbol **namep,
    int *sizep, int *flagsp, t_symbol **fillp, t_symbol **outlinep)
Miller Puckette's avatar
Miller Puckette committed
549
{
550
    char namebuf[MAXPDSTRING];
Miller Puckette's avatar
Miller Puckette committed
551
552
    t_array *a = garray_getarray(x);
    if (!a)
553
554
555
        return 0;
    //gfxstub_deleteforkey(x);
        /* LATER fix this to escape '$'
Miller Puckette's avatar
Miller Puckette committed
556
557
        properly; right now we just detect a leading '$' and escape
        it.  There should be a systematic way of doing this. */
558
    /* still don't understand this filestyle business... */
559
560
    int filestyle = (x->x_style == 0 ? PLOTSTYLE_POLY :
        (x->x_style == 1 ? PLOTSTYLE_POINTS : x->x_style));
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
    int flags = x->x_saveit + 2 * filestyle + 8 * x->x_hidename + 16 * x->x_joc;

    if (x->x_name->s_name[0] == '$')
    {
        sprintf(namebuf, "\\%s", x->x_name->s_name);
    }
    else
    {
        sprintf(namebuf, "%s", x->x_name->s_name);
    }

    *gfxstubp = gensym(gfxstub_new2(&x->x_gobj.g_pd, x));
    *namep = gensym(namebuf);
    *sizep = a->a_n;
    *flagsp = flags;
    *fillp = x->x_fillcolor;
    *outlinep = x->x_outlinecolor;
    //sprintf(cmdbuf, ((x->x_name->s_name[0] == '$') ?
    //    "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 * filestyle + 8 * x->x_hidename + 16 * x->x_joc,
    //        (long unsigned int)glist_getcanvas(canvas),
    //        x->x_fillcolor->s_name,
    //        x->x_outlinecolor->s_name);
    //gfxstub_new(&x->x_gobj.g_pd, x, cmdbuf);
    return 1;
Miller Puckette's avatar
Miller Puckette committed
589
590
591
592
}

    /* 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. */
593
594
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
595
{
596
597
598
599
600
601
602
603
604
605
606
607
    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);
608

Miller Puckette's avatar
Miller Puckette committed
609
610
611
612
613
    t_glist *gl;
    t_garray *a;
    int flags = fflags;
    if (size < 1)
        size = 1;
614
615
    if (otherflag == 0 || (!(gl = glist_findgraph(parent))))
    {
Miller Puckette's avatar
Miller Puckette committed
616
        gl = glist_addglist(parent, &s_, 0, 1,
617
618
            (size > 1 ? size-1 : size), -1, xdraw+30, ydraw+30,
            xdraw+30+GLIST_DEFGRAPHWIDTH, ydraw+30+GLIST_DEFGRAPHHEIGHT);
619
620
        gl->gl_hidetext = 1;
    }
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
    //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);
    
637
    //canvas_redraw(glist_getcanvas(parent));
638
    garray_fittograph(a, (int)size, -1);
639
640
641
642
643
644
    // we queue gui here because otherwise garray is not drawn properly
    // since things are still being created and the object has not yet
    // been associated with glist
    // sys_queuegui((t_gobj *)gl, glist_getcanvas(gl), graph_redraw);
    //fprintf(stderr,"glist_arraydialog done\n");
    glist_redraw(gl);
645
    canvas_getscroll(glist_getcanvas(parent));
646
    canvas_dirty(parent, 1);
Miller Puckette's avatar
Miller Puckette committed
647
648
}

649
650
extern void canvas_apply_setundo(t_canvas *x, t_gobj *y);

Miller Puckette's avatar
Miller Puckette committed
651
    /* this is called from the properties dialog window for an existing array */
652
void garray_arraydialog(t_garray *x, t_symbol *s, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
653
{
654
655
656
    //fprintf(stderr,"================garray_arraydialog\n");

    int deleteit = atom_getfloatarg(3, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
657
658
    if (deleteit != 0)
    {
659
660
661
662
        //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
Jonathan Wilkes's avatar
Jonathan Wilkes committed
663
        t_canvas *c = x->x_glist;
664
        int dspwas = canvas_suspend_dsp();
Miller Puckette's avatar
Miller Puckette committed
665
        glist_delete(x->x_glist, &x->x_gobj);
666
        canvas_resume_dsp(dspwas);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
667
        canvas_redraw(glist_getcanvas(c));
Miller Puckette's avatar
Miller Puckette committed
668
    }
669
670
671
672
673
674
675
676
677
678
679
680
681
682
    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);
683
        int saveit = ((flags & 1) != 0);
684
685
686
        int filestyle = ((flags & 6) >> 1);
        int style = (filestyle == 0 ? PLOTSTYLE_POLY :
            (filestyle == 1 ? PLOTSTYLE_POINTS : filestyle));
687

688
689
690
        /* todo: revisit this filestyle business
        if (style < 2) style = !style;
        */
691

692
693
        int hidename = ((flags & 8) >> 3);
        int joc = ((flags & 16) >> 4);
694
695
696
697
698

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

699
        long size;
Miller Puckette's avatar
Miller Puckette committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
        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)
        {
            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);
        }
721
722
723
            /* redraw the whole glist, just so the name change shows up
                there has to be a better way */
        /*if (x->x_glist->gl_havewindow)
724
        {
725
            canvas_redraw(glist_getcanvas(x->x_glist));
726
            //fprintf(stderr,"================REDRAW\n");
727
        }*/
Miller Puckette's avatar
Miller Puckette committed
728
729
730
        size = fsize;
        if (size < 1)
            size = 1;
731
732
733
        if (style != x->x_style)
        {
            x->x_style = style;
734
            //garray_fittograph(x, size, 1);
735
736
        }
        //fprintf(stderr,"style=%d %f\n", style, (t_float)x->x_style);
Miller Puckette's avatar
Miller Puckette committed
737
        template_setfloat(scalartemplate, gensym("style"),
738
            x->x_scalar->sc_vec, (t_float)x->x_style, 0);
739
740
        template_setfloat(scalartemplate, gensym("linewidth"),
            x->x_scalar->sc_vec, ((x->x_style == PLOTSTYLE_POINTS) ? 2 : 1), 1);
741
742
743
744
        template_setsymbol(scalartemplate, gensym("fillcolor"),
            x->x_scalar->sc_vec, fill, 0);
        template_setsymbol(scalartemplate, gensym("outlinecolor"),
            x->x_scalar->sc_vec, outline, 0);
745

746
747
748
        char buf[MAXPDSTRING];
        sprintf(buf, "%s_changed", x->x_realname->s_name);
        x->x_send = gensym(buf);
Miller Puckette's avatar
Miller Puckette committed
749
750

        garray_setsaveit(x, (saveit != 0));
751
752
753
754
755
        x->x_joc = joc;
        x->x_hidename = hidename;
        x->x_fillcolor = fill;
        x->x_outlinecolor = outline;
        x->x_style = style;
756
        if (size != a->a_n)
757
        {
758
            glist_redraw(x->x_glist);
759
            garray_resize_long(x, size);
760
        }
761
        else
762
763
764
765
766
767
        {
            /* So, this next function call is here _solely_ to get the
               the new array name to show up in the graph label if the user
               happened to change it. Let me emphasize-- in order to
               redraw array labels, we must call a function that redraws
               not only the _entire_ array and its graph, but also redraws
768
               the graph in which the array is displayed. There is
769
770
               no interface I can find to just say, "redraw the label".

771
               Worse, Pd redraws a single array at least 3 times-- hard to tell
772
773
774
               because so much data is sent over the wire that I run out of
               buffer in my terminal window. These are a side-effect of
               garray_redraw, as well as the garray_resize branch above. (And 
775
776
               don't forget that the canvas dialog callback causes some
               of these.)
777
778
779
780
781

               Until the Pd codebase handles redrawing in a sane fashion,
               without depending on a vast array of side-effects, there's
               simply no way to effectively maintain it. */
            glist_redraw(x->x_glist);
782
            garray_redraw(x);
783
784
        }

785
        //fprintf(stderr,"garray_arraydialog garray_redraw done\n");
786
        garray_select((t_gobj *)x,glist_getcanvas(x->x_glist),1);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
787
        canvas_dirty(x->x_glist, 1);
Miller Puckette's avatar
Miller Puckette committed
788
789
790
791
792
793
    }
}

static void garray_free(t_garray *x)
{
    t_pd *x2;
794
    sys_unqueuegui(&x->x_gobj);
Miller Puckette's avatar
Miller Puckette committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
    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;
809
810
    t_scalar *sc = (t_scalar *)(a->a_gp.gp_un.gp_gobj);
    scalar_redraw(sc, glist);
Miller Puckette's avatar
Miller Puckette committed
811
812
813
}

    /* routine to get screen coordinates of a point in an array */
814
815
816
817
818
    /* glist_topixels parameter was added because of t_scalar
       and t_draw caching mechanism. For scalers, we're only
       converting to pixels in scalar_getrect. So in plot_getrect
       we need to get the coordinate without regard to the x/y
       offset (and scaling factor) of a gop window */
Miller Puckette's avatar
Miller Puckette committed
819
820
void array_getcoordinate(t_glist *glist,
    char *elem, int xonset, int yonset, int wonset, int indx,
821
    t_float basex, t_float basey, t_float xinc,
Miller Puckette's avatar
Miller Puckette committed
822
    t_fielddesc *xfielddesc, t_fielddesc *yfielddesc, t_fielddesc *wfielddesc,
823
824
    t_float *xp1, t_float *xp2, t_float *yp, t_float *wp,
    int glist_topixels)
Miller Puckette's avatar
Miller Puckette committed
825
{
826
    t_float xval, yval, ypix, wpix;
Miller Puckette's avatar
Miller Puckette committed
827
    if (xonset >= 0)
828
        xval = *(t_float *)(elem + xonset);
Miller Puckette's avatar
Miller Puckette committed
829
830
    else xval = indx * xinc;
    if (yonset >= 0)
831
        yval = *(t_float *)(elem + yonset);
Miller Puckette's avatar
Miller Puckette committed
832
    else yval = 0;
833
834
835
836
    /* This is from the old API. Now we group drawing instructions in
       a hierarchy so we can do glist_ytopixels math on the GUI side */
    //ypix = glist_ytopixels(glist, basey +
    //    fielddesc_cvttocoord(yfielddesc, yval));
837
838
839
    ypix = basey + fielddesc_cvttocoord(yfielddesc, yval);
    if (glist_topixels)
        ypix = glist_ytopixels(glist, ypix);
Miller Puckette's avatar
Miller Puckette committed
840
841
842
    if (wonset >= 0)
    {
            /* found "w" field which controls linewidth. */
843
        t_float wval = *(t_float *)(elem + wonset);
844
845
846
847
        /* ytopixels is now done GUI-side */
        //wpix = glist_ytopixels(glist, basey +
        //    fielddesc_cvttocoord(yfielddesc, yval) +
        //        fielddesc_cvttocoord(wfielddesc, wval)) - ypix;
848
849
850
851
852
        wpix = basey + fielddesc_cvttocoord(yfielddesc, yval) +
            fielddesc_cvttocoord(wfielddesc, wval);
        if (glist_topixels)
            wpix = glist_ytopixels(glist, wpix);
        wpix -= ypix;
Miller Puckette's avatar
Miller Puckette committed
853
854
855
856
        if (wpix < 0)
            wpix = -wpix;
    }
    else wpix = 1;
857
858
859
860
861
862
863
864
865
866
867
868
    if (glist_topixels)
    {
        *xp1 = glist_xtopixels(glist, basex +
            fielddesc_cvttocoord(xfielddesc, xval));
        *xp2 = glist_xtopixels(glist, basex +
            fielddesc_cvttocoord(xfielddesc, xval+1))-1;
    }
    else
    {
        *xp1 = basex + fielddesc_cvttocoord(xfielddesc, xval);
        *xp2 = basex + fielddesc_cvttocoord(xfielddesc, xval+1)-1;
    }
Miller Puckette's avatar
Miller Puckette committed
869
    *yp = ypix;
870
871
872
873
    // increased following on 20140830 to 8 (+4 and -4) and 
    // so that the smallest hitbox is always at least 8x8
    // check with all_about_arrays.pd inside custom scalars
    // in an array
874
    if (glist_topixels && *xp1 == *xp2)
875
876
877
    {
        *xp1 = *xp1 - 4;
        *xp2 = *xp2 + 4;
878
        if (wpix < 8) wpix = 8;
879
    }
Miller Puckette's avatar
Miller Puckette committed
880
881
882
    *wp = wpix;
}

883
extern int array_joc; /* from g_canvas.h */
884
885
static t_float array_motion_xcumulative;
static t_float array_motion_ycumulative;
Miller Puckette's avatar
Miller Puckette committed
886
887
888
889
890
891
892
893
894
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
895
//static int array_motion_altkey;
896
897
898
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
899
900
901
902
903
904
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 ... */

905
906
    /* 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
907
908
909
910
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;
911
    //fprintf(stderr,"array_motion %f %f %f %f\n", array_motion_xcumulative, array_motion_ycumulative, dx, dy);
912

913
914
915
    // used to set up boundaries and update sends accordingly
    t_glist *graph = NULL;
    if (array_garray != NULL) graph = array_garray->x_glist;
916

Miller Puckette's avatar
Miller Puckette committed
917
918
919
920
921
922
923
924
    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);
925
            t_float xwas = fielddesc_getcoord(array_motion_xfield, 
Miller Puckette's avatar
Miller Puckette committed
926
                array_motion_template, thisword, 1);
927
            t_float ywas = (array_motion_yfield ?
Miller Puckette's avatar
Miller Puckette committed
928
929
930
931
932
933
934
935
936
937
                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)
                    {
938
                        t_float newy = ywas + dy * array_motion_yperpix;
Miller Puckette's avatar
Miller Puckette committed
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
                        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;
959
        t_float newy = array_motion_ycumulative,
Miller Puckette's avatar
Miller Puckette committed
960
961
962
963
964
            oldy = fielddesc_getcoord(array_motion_yfield,
                array_motion_template,
                    (t_word *)(((char *)array_motion_wp) +
                        array_motion_elemsize * array_motion_lastx),
                            1);
965
        // here we block scalar from exceeding the array GOP edges
966
        // LATER: see if we need to do the same for an x plot
967
968
969
970
971
972
973
974
975
976
977
978
979
980
        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);
981
        t_float ydiff = newy - oldy;
Miller Puckette's avatar
Miller Puckette committed
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
        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;
    }
999
    //fprintf(stderr, "%f %f\n", graph->gl_y1, graph->gl_y2);
1000

Miller Puckette's avatar
Miller Puckette committed
1001
1002
1003
1004
    if (array_motion_scalar)
        scalar_redraw(array_motion_scalar, array_motion_glist);
    if (array_motion_array)
        array_redraw(array_motion_array, array_motion_glist);
1005

1006
1007
1008
1009
    /* 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
1010
1011
1012
1013
}

int scalar_doclick(t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
1014
    t_float xloc, t_float yloc, int xpix, int ypix,
Miller Puckette's avatar
Miller Puckette committed
1015
1016
1017
1018
1019
1020
    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,
1021
    t_float linewidth, t_float xloc, t_float xinc, t_float yloc,
Miller Puckette's avatar
Miller Puckette committed
1022
1023
1024
    t_fielddesc *xfield, t_fielddesc *yfield, t_fielddesc *wfield,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
1025
    //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
1026
1027
1028
    t_canvas *elemtemplatecanvas;
    t_template *elemtemplate;
    int elemsize, yonset, wonset, xonset, i, incr, hit;
1029
    t_float xsum;
Miller Puckette's avatar
Miller Puckette committed
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042

    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)
    {
1043
        t_float usexloc, useyloc;
Miller Puckette's avatar
Miller Puckette committed
1044
1045
        if (xonset >= 0)
            usexloc = xloc + fielddesc_cvttocoord(xfield, 
1046
                *(t_float *)(((char *)(array->a_vec) + elemsize * i) + xonset));
Miller Puckette's avatar
Miller Puckette committed
1047
1048
        else usexloc = xloc + xsum, xsum += xinc;
        useyloc = yloc + (yonset >= 0 ? fielddesc_cvttocoord(yfield,
1049
            *(t_float *)(((char *)(array->a_vec) + elemsize * i) + yonset)) : 0);
Miller Puckette's avatar
Miller Puckette committed
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
        
        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,
1065
    t_float linewidth, t_float xloc, t_float xinc, t_float yloc, t_float scalarvis,
Miller Puckette's avatar
Miller Puckette committed
1066
1067
1068
    t_fielddesc *xfield, t_fielddesc *yfield, t_fielddesc *wfield,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
1069
    //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
1070
1071
1072
1073
1074
1075
1076
1077
    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))
    {
1078
        t_float best = -1;
Miller Puckette's avatar
Miller Puckette committed
1079
1080
            /* if it has more than 2000 points, just check 1000 of them. */
        int incr = (array->a_n <= 2000 ? 1 : array->a_n / 1000);
1081
        t_float pxpix1 = 0.0, pxpix2 = 0.0, pypix = 0.0, pwpix = 0.0,
1082
          dy, dy2, dy3;
Miller Puckette's avatar
Miller Puckette committed
1083
1084
1085
1086
        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,
1087
                xfield, yfield, wfield, &pxpix1, &pxpix2, &pypix, &pwpix, 1);
1088
1089
            // increased following on 20140830 to 8 and updated
            // array_getcoordinate
1090
1091
1092
1093
            // so that the smallest hitbox is always at least 8x8--check with
            // all_about_arrays.pd inside custom scalars in an array
            if (pwpix < 8)
                pwpix = 8;
1094

1095
            if (xpix >= (int)pxpix1-pwpix && xpix <= (int)pxpix2+pwpix &&
1096
1097
1098
1099
1100
1101
                ((array_joc) ||
                 (ypix >= pypix-pwpix && ypix <= pypix+pwpix)))
            {
                best = i;
                break;
            }
Miller Puckette's avatar
Miller Puckette committed
1102
        }
1103
        //fprintf(stderr,"    best = %f\n", best);
1104
1105
1106
        /* this is the arbitrary radius away from the actual object's
           center, originally 8 */
        if (best == -1 && (array_joc == 0))
Miller Puckette's avatar
Miller Puckette committed
1107
        {
1108
            //fprintf(stderr,"best > 8\n");
1109
1110
1111
            if (scalarvis != 0)
            {
                //fprintf(stderr,"    array_doclick_element\n");
Miller Puckette's avatar
Miller Puckette committed
1112
1113
1114
1115
                return (array_doclick_element(array, glist, sc, ap,
                    elemtemplatesym, linewidth, xloc, xinc, yloc,
                        xfield, yfield, wfield,
                        xpix, ypix, shift, alt, dbl, doit));
1116
1117
1118
1119
1120
1121
            }
            else
            {
                //fprintf(stderr,"    return 0\n");
                return (0);
            }
Miller Puckette's avatar
Miller Puckette committed
1122
        }
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
        dy = pypix - ypix;
        if (dy < 0) dy = -dy;
        if (wonset >= 0)
        {
            dy2 = (pypix + pwpix) - ypix;
            if (dy2 < 0) dy2 = -dy2;
            dy3 = (pypix - pwpix) - ypix;
            if (dy3 < 0) dy3 = -dy3;
            if (yonset < 0)
                dy = 100;
        }
        else dy2 = dy3 = 100;
#if 0 // this doesn't seem to be used anywhere -ag
        int dx;
        int hit = 0;
        if(array_joc)
        {
                hit = (xpix >= pxpix1) && (xpix < pxpix2);
        }
        else
            hit = dx + dy <= best || dx + dy2 <= best || dx + dy3 <= best;
#endif
        if (dy < dy2 && dy < dy3)
        {
            array_motion_fatten = 0;
            //fprintf(stderr,"A\n");
        }
        else if (dy2 < dy3)
        {
            array_motion_fatten = -1;
            //fprintf(stderr,"B\n");
        }
        else if (!array_joc)
        {
            array_motion_fatten = 1;
            //fprintf(stderr,"C\n");
        }
        if (doit || (glob_lmclick && array_joc))
        {
            char *elem = (char *)array->a_vec;
            array_motion_elemsize = elemsize;
            array_motion_glist = glist;
            array_motion_scalar = sc;
            array_motion_array = ap;
            array_motion_template = elemtemplate;
            array_motion_xperpix = glist_dpixtodx(glist, 1);
            array_motion_yperpix = glist_dpixtody(glist, 1);
            if (alt) /* delete a point */
Miller Puckette's avatar
Miller Puckette committed
1171
            {
1172
1173
1174
1175
1176
1177
1178
1179
1180
                if (array->a_n <= 1)
                    return (0);
                memmove((char *)(array->a_vec) + elemsize * i, 
                    (char *)(array->a_vec) + elemsize * (i+1),
                        (array->a_n - 1 - i) * elemsize);
                //array_resize_and_redraw(array, glist, array->a_n - 1);
                garray_resize(array_garray, array->a_n - 1);
                canvas_setcursor(glist_getcanvas(glist), 0);
                return (0);
Miller Puckette's avatar
Miller Puckette committed
1181
            }
1182
            else if (shift)
1183
            {
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
                /* add a point (after the clicked-on one) */
                //fprintf(stderr,"add a point\n");
                //array_resize_and_redraw(array, glist, array->a_n + 1);
                elem = (char *)array->a_vec;
                memmove(elem + elemsize * (i+1), 
                    elem + elemsize * i,
                        (array->a_n - i - 1) * elemsize);
                i++;
                garray_resize(array_garray, array->a_n + 1);
                canvas_setcursor(glist_getcanvas(glist), 0);
            }
            if (xonset >= 0)
            {
                //fprintf(stderr, "   xonset >=0\n");
                array_motion_xfield = xfield;
                array_motion_xcumulative = 
                    fielddesc_getcoord(xfield, array_motion_template,
                        (t_word *)(elem + i * elemsize), 1);
                    array_motion_wp = (t_word *)(elem + i * elemsize);
                if (shift)
                    array_motion_npoints = array->a_n - i;
                else array_motion_npoints = 1;
1206
1207
            }
            else
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
            {
                //fprintf(stderr, "   !(xonset >=0)\n");
                array_motion_xfield = 0;
                array_motion_xcumulative = 0;
                array_motion_wp = (t_word *)elem;
                array_motion_npoints = array->a_n;

                array_motion_initx = i;
                array_motion_lastx = i;
                array_motion_xperpix *= (xinc == 0 ? 1 : 1./xinc);
            }
            if (array_motion_fatten)
            {
                //fprintf(stderr, "   motion_fatten\n");
                array_motion_yfield = wfield;
                array_motion_ycumulative = 
                    fielddesc_getcoord(wfield, array_motion_template,
                        (t_word *)(elem + i * elemsize), 1);
                array_motion_yperpix *= -array_motion_fatten;
            }
            else if (yonset >= 0)
            {
                //fprintf(stderr, "   yonset >=0\n");
                array_motion_yfield = yfield;
                array_motion_ycumulative =
                    fielddesc_getcoord(yfield, array_motion_template,
                        (t_word *)(elem + i * elemsize), 1);
                    /* *(t_float *)((elem + elemsize * i) + yonset); */
                // we do cursor detection based on the kind of a graph.
                // (e.g. 3-point PLOYSTYLE_POINTS creates a graph that has 4
                // delimiting points, while a 3-point PLOTSTYLE_POLY creates
                // a graph that has only 3 delimiting points)
                // This, therefore takes into account whether we should count
                // the center point of a bar or the starting point (poly) as
                // our reference.
                // TODO: see if we can reimplement Bezier curves
                if (array_garray != NULL &&
                    (array_garray->x_style == PLOTSTYLE_POLY ||
                     array_garray->x_style == PLOTSTYLE_BEZ))
1247
                {
1248
                    array_motion(0, xpix - pxpix1, ypix - pypix);
1249
                }
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
                else
                    array_motion(0, (xpix - (pxpix1 + (pxpix2 - pxpix1)/2)),
                        ypix - pypix);
            }
            else
            {
                //fprintf(stderr, "   else 0\n");
                array_motion_yfield = 0;
                array_motion_ycumulative = 0;
            }
            //fprintf(stderr,"    glist_grab %d %d\n", xpix, ypix);
            glist_grab(glist, 0, array_motion, 0, xpix, ypix);
        }
        if (alt)
        {
            return (CURSOR_EDITMODE_DISCONNECT);
        }
        else if (shift)
        {
            return (CURSOR_RUNMODE_ADDPOINT);
        }
        else return (array_motion_fatten ?
            CURSOR_RUNMODE_THICKEN : CURSOR_RUNMODE_CLICKME);
Miller Puckette's avatar
Miller Puckette committed
1273
1274
1275
1276
    }
    return (0);
}

1277
#if 0 // this doesn't seem to be used anywhere -ag
Miller Puckette's avatar
Miller Puckette committed
1278
1279
1280
static void array_getrect(t_array *array, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
1281
    //fprintf(stderr,"array getrect %d %d\n", glist_istoplevel(glist), (array_joc != 0 ? 1 : 0));
1282
    t_float x1 = 0x7fffffff, y1 = 0x7fffffff, x2 = -0x7fffffff, y2 = -0x7fffffff;
Miller Puckette's avatar
Miller Puckette committed
1283
1284
1285
    t_canvas *elemtemplatecanvas;
    t_template *elemtemplate;
    int elemsize, yonset, wonset, xonset, i;
1286
    
Miller Puckette's avatar
Miller Puckette committed
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
    if (!array_getfields(array->a_templatesym, &elemtemplatecanvas,
        &elemtemplate, &elemsize, 0, 0, 0, &xonset, &yonset, &wonset))
    {
        int incr;
            /* 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; i < array->a_n; i += incr)
        {
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
1297
            t_float pxpix1, pxpix2, pypix, pwpix;
Miller Puckette's avatar
Miller Puckette committed
1298
1299
1300
1301
            array_getcoordinate(glist, (char *)(array->a_vec) +
                i * elemsize,
                xonset, yonset, wonset, i, 0, 0, 1,
                0, 0, 0,
1302
                &pxpix1, &pxpix2, &pypix, &pwpix, 1);
Miller Puckette's avatar
Miller Puckette committed
1303
1304
            if (pwpix < 2)
                pwpix = 2;
1305
1306
1307
1308
            if (pxpix1 < x1)
                x1 = pxpix1;
            if (pxpix2 > x2)
                x2 = pxpix2;
Miller Puckette's avatar
Miller Puckette committed
1309
1310
1311
1312
1313
1314
            if (pypix - pwpix < y1)
                y1 = pypix - pwpix;
            if (pypix + pwpix > y2)
                y2 = pypix + pwpix;
        }
    }
1315
    //fprintf(stderr,"array_getrect %f %f %f %f\n", x1, y1, x2, y2);
Miller Puckette's avatar
Miller Puckette committed
1316
1317
1318
1319
1320
    *xp1 = x1;
    *yp1 = y1;
    *xp2 = x2;
    *yp2 = y2;
}
1321
#endif
Miller Puckette's avatar
Miller Puckette committed
1322
1323
1324
1325
1326
1327

/* -------------------- widget behavior for garray ------------ */

static void garray_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
1328
    //fprintf(stderr,"garray_getrect\n");
Miller Puckette's avatar
Miller Puckette committed
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
    t_garray *x = (t_garray *)z;
    gobj_getrect(&x->x_scalar->sc_gobj, glist, xp1, yp1, xp2, yp2);
}

static void garray_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
    /* refuse */
}

static void garray_select(t_gobj *z, t_glist *glist, int state)
{
1340
    //fprintf(stderr,">>>>>>>>>>>>garray_select %d\n", state);
1341
    t_garray *x = (t_garray *)z;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1342
1343
1344
1345
    /* There's no replacement for the following command in the
       new GUI, but it looks like it's not needed anymore. */
    //sys_vgui("pdtk_select_all_gop_widgets .x%lx %lx %d\n",
    //    glist_getcanvas(glist), x->x_glist, state);
1346

1347
    extern void scalar_select(t_gobj *z, t_glist *owner, int state);
1348
    scalar_select((t_gobj *)x->x_scalar, glist, state);
Miller Puckette's avatar
Miller Puckette committed
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
}

static void garray_activate(t_gobj *z, t_glist *glist, int state)
{
}

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

1360
1361
extern int do_not_redraw;

Miller Puckette's avatar
Miller Puckette committed
1362
1363
static void garray_vis(t_gobj *z, t_glist *glist, int vis)
{
1364
    //fprintf(stderr,"garray_vis %d\n", vis);
Miller Puckette's avatar
Miller Puckette committed
1365
    t_garray *x = (t_garray *)z;
1366
1367
1368
1369
1370
1371

    if (vis) {