g_scalar.c 44.7 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

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

*/

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

t_class *scalar_class;

19
20
void pd_doloadbang(void);

21
void word_init(t_word *data, t_template *template, t_gpointer *gp)
Miller Puckette's avatar
Miller Puckette committed
22
23
24
{
    int i, nitems = template->t_n;
    t_dataslot *datatypes = template->t_vec;
25
    t_word *wp = data;
Miller Puckette's avatar
Miller Puckette committed
26
27
28
29
30
31
32
33
34
    for (i = 0; i < nitems; i++, datatypes++, wp++)
    {
        int type = datatypes->ds_type;
        if (type == DT_FLOAT)
            wp->w_float = 0; 
        else if (type == DT_SYMBOL)
            wp->w_symbol = &s_symbol;
        else if (type == DT_ARRAY)
        {
35
            wp->w_array = array_new(datatypes->ds_fieldtemplate, gp);
Miller Puckette's avatar
Miller Puckette committed
36
37
38
        }
        else if (type == DT_LIST)
        {
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
            /* copied from glob_evalfile... */
            t_pd *x = 0;
            /* even though binbuf_evalfile appears to take care of dspstate,
            we have to do it again here, because canvas_startdsp() assumes
            that all toplevel canvases are visible.  LATER check if this
            is still necessary -- probably not. */
            int dspstate = canvas_suspend_dsp();
// this needs to be set to sane symbols, possibly stored in the dataslot...
            glob_setfilename(0, gensym("foo"), gensym("bar"));
            t_pd *boundx = s__X.s_thing;
            s__X.s_thing = 0;       /* don't save #X; we'll need to leave it
                                       bound for the caller to grab it. */

            /* copied from binbuf_evalfile... we need to refactor at some
               point... */
            /* save bindings of symbols #N, #A (and restore afterward) */
            t_pd *bounda = gensym("#A")->s_thing, *boundn = s__N.s_thing;
            gensym("#A")->s_thing = 0;
            s__N.s_thing = &pd_canvasmaker;
            binbuf_eval(datatypes->ds_binbuf, 0, 0, 0);
            gensym("#A")->s_thing = bounda;
            s__N.s_thing = boundn;
            glob_setfilename(0, &s_, &s_);

            wp->w_list = canvas_getcurrent();
64
65
66
67
68
69
70
71
72
73
74
75
            wp->w_list->gl_templatesym = template->t_sym;
            /* this is bad-- we're storing a reference to a position in
               a dynamically allocated byte array when realloc can potentially
               move this data.  Essentially, we're depending on gcc to never
               move it, which is a bad assumption.  Unfortunately gpointers
               do the same thing, and I haven't heard back from Miller yet
               on how he plans to deal with this problem. Hopefully that same
               solution will be usable here. */
            wp->w_list->gl_vec = data;
            /* Here too we're being dangerous-- I'm not unsetting this
               gpointer yet. */
            gpointer_copy(gp, &wp->w_list->gl_gp);
76
77
78
79

            while ((x != s__X.s_thing) && s__X.s_thing) 
            {
                x = s__X.s_thing;
80
                vmess(x, gensym("pop"), "i", 0);
81
82
83
84
85
86
87
88
89
90
91
            }
            /* oops, can't actually do a loadbang here.
               Why?
               Consider getting the value of a fielddesc that hasn't
               been init'd yet... */
            /* pd_doloadbang(); */
            canvas_resume_dsp(dspstate);

            s__X.s_thing = boundx;
            post("eval'd a canvas with addy x%lx", (long unsigned int)
                wp->w_list);
Miller Puckette's avatar
Miller Puckette committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        }
    }
}

void word_restore(t_word *wp, t_template *template,
    int argc, t_atom *argv)
{
    int i, nitems = template->t_n;
    t_dataslot *datatypes = template->t_vec;
    for (i = 0; i < nitems; i++, datatypes++, wp++)
    {
        int type = datatypes->ds_type;
        if (type == DT_FLOAT)
        {
106
            t_float f;
Miller Puckette's avatar
Miller Puckette committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
            if (argc)
            {
                f =  atom_getfloat(argv);
                argv++, argc--;
            }
            else f = 0;
            wp->w_float = f; 
        }
        else if (type == DT_SYMBOL)
        {
            t_symbol *s;
            if (argc)
            {
                s =  atom_getsymbol(argv);
                argv++, argc--;
            }
            else s = &s_;
            wp->w_symbol = s;
        }
    }
    if (argc)
        post("warning: word_restore: extra arguments");
}

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

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* some of this code is used in a function in g_canvas.c...
   need to modularize it */
static t_object *template_getstruct(t_template *template)
{
    if (template)
    {
        t_gobj *y;
        t_canvas *c;
        if (c = template_findcanvas(template))
        {
            t_symbol *s1 = gensym("struct");
            for (y = c->gl_list; y; y = y->g_next)
            {
                t_object *ob = pd_checkobject(&y->g_pd);
                t_atom *argv;
                if (!ob || ob->te_type != T_OBJECT ||
                    binbuf_getnatom(ob->te_binbuf) < 2)
                    continue;
                argv = binbuf_getvec(ob->te_binbuf);
                if (argv[0].a_w.w_symbol != s1)
                    continue;
                if (canvas_makebindsym(argv[1].a_w.w_symbol) == template->t_sym)
                    return (ob);
            }
        }
    }
    return (0);
}

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

194
int template_check_array_fields(t_symbol *structname, t_template *template)
195
{
196
    /* We're calling this from template_cancreate as well as
197
198
199
200
201
202
203
204
205
206
207
208
209
       gtemplate_cancreate. With gtemplate_cancreate, the t_template doesn't
       exist yet. So we send the struct name to see if it matches the name of
       any array field templates that our gtemplate will depend on. If we find
       a match we will refuse to create the gtemplate. However,
       template_cancreate starts from the struct name for a template that
       already exists.  On the first time through this recursive function,
       there isn't a containing structname yet. So we suppress this conditional
       the first time through the recursive loop, and then set the structname
       for all subsequent iterations through the loop.
       
       1 = success
       0 = circular dependency
       -1 = non-existant array elemtemplate found */
210
211
    if (structname && structname == template->t_sym)
    {
212
213
214
215
        t_object *ob = template_getstruct(template);
        pd_error(ob, "%s: circular dependency",
            template->t_sym->s_name);
       return (0);
216
    }
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
217
    int i, nitems = template->t_n;
218
219
220
221
    t_dataslot *datatypes = template->t_vec;
    t_template *elemtemplate;
    for (i = 0; i < nitems; i++, datatypes++)
    {
222
223
        if (datatypes->ds_type == DT_ARRAY)
        {
224
            elemtemplate = template_findbyname(datatypes->ds_fieldtemplate);
225
226
227
228
            if (!(elemtemplate))
            {
                t_object *ob = template_getstruct(template);
                pd_error(ob, "%s: no such template",
229
                    datatypes->ds_fieldtemplate->s_name);
230
                return (-1);
231
232
233
234
235
            }
            else if (elemtemplate->t_sym == structname)
            {
                t_object *ob = template_getstruct(template);
                pd_error(ob, "%s: circular dependency",
236
                    datatypes->ds_fieldtemplate->s_name);
237
238
239
240
241
242
243
244
245
246
247
                return (0);
            }
            else
            {
                if (!structname)
                {
                    structname = template->t_sym;
                }
                return (template_check_array_fields(structname, elemtemplate));
            }
        }
248
    }
249
    return 1;
250
251
}

252
253
254
int template_cancreate(t_template *template)
{
    /* we send "0" for the structname since there is no container struct */
255
    return (template_check_array_fields(0, template) == 1);
256
257
}

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
int scalar_hascanvasfield(t_scalar *x)
{
    t_template *template = template_findbyname(x->sc_template);
    if (template)
    {
        int i, nitems = template->t_n;
        t_dataslot *datatypes = template->t_vec;
        for (i = 0; i < nitems; i++, datatypes++)
        {
            if (datatypes->ds_type == DT_LIST)
                return 1;
        }
    }
    return 0;
}

Miller Puckette's avatar
Miller Puckette committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    /* make a new scalar and add to the glist.  We create a "gp" here which
    will be used for array items to point back here.  This gp doesn't do
    reference counting or "validation" updates though; the parent won't go away
    without the contained arrays going away too.  The "gp" is copied out
    by value in the word_init() routine so we can throw our copy away. */
t_scalar *scalar_new(t_glist *owner, t_symbol *templatesym)
{
    t_scalar *x;
    t_template *template;
    t_gpointer gp;
    gpointer_init(&gp);
    template = template_findbyname(templatesym);
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return (0);
    }
291
292
    if (!template_cancreate(template))
        return (0);
Miller Puckette's avatar
Miller Puckette committed
293
294
295
296
    x = (t_scalar *)getbytes(sizeof(t_scalar) +
        (template->t_n - 1) * sizeof(*x->sc_vec));
    x->sc_gobj.g_pd = scalar_class;
    x->sc_template = templatesym;
297
    gpointer_setglist(&gp, owner, &x->sc_gobj);
Miller Puckette's avatar
Miller Puckette committed
298
    word_init(x->sc_vec, template, &gp);
299
300
301
    char buf[50];
    sprintf(buf, ".x%lx", (long unsigned int)x);
    pd_bind(&x->sc_gobj.g_pd, gensym(buf));
Miller Puckette's avatar
Miller Puckette committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    return (x);
}

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

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

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

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

335
336
337
338
339
340
341
342
    /* search template fields recursively to see if the template
       depends on elemtemplate */
int template_has_elemtemplate(t_template *t, t_template *elemtemplate)
{
    int returnval = 0;
    if (t && elemtemplate)
    {
        int i;
343
        t_dataslot *d = t->t_vec;
344
345
346
347
        for (i = 0; i < t->t_n; i++, d++)
        {
            if (d->ds_type == DT_ARRAY)
            {
348
                if (d->ds_fieldtemplate == elemtemplate->t_sym)
349
350
351
352
353
354
355
                {
                    returnval = 1;
                    break;
                }
                else
                {
                    returnval = template_has_elemtemplate(
356
                        template_findbyname(d->ds_fieldtemplate),
357
358
359
360
361
362
363
364
                        elemtemplate);
                }
            }
        }
    }
    return (returnval);
}

Miller Puckette's avatar
Miller Puckette committed
365
/* -------------------- widget behavior for scalar ------------ */
366
void scalar_getbasexy(t_scalar *x, t_float *basex, t_float *basey)
Miller Puckette's avatar
Miller Puckette committed
367
368
369
370
371
372
{
    t_template *template = template_findbyname(x->sc_template);
    *basex = template_getfloat(template, gensym("x"), x->sc_vec, 0);
    *basey = template_getfloat(template, gensym("y"), x->sc_vec, 0);
}

373
374
extern int array_joc;

375
376
377
extern void template_notifyforscalar(t_template *template, t_glist *owner,
    t_scalar *sc, t_symbol *s, int argc, t_atom *argv);

378
379
t_canvas *sc_mouseover_canvas;
static void scalar_mouseover(t_scalar *x, t_floatarg state)
380
{
381
382
383
384
385
386
387
388
    t_atom at[1];
    t_template *template = template_findbyname(x->sc_template);
    if (state)
        template_notifyforscalar(template, sc_mouseover_canvas,
            x, gensym("leave"), 1, at);
    else
        template_notifyforscalar(template, sc_mouseover_canvas,
            x, gensym("enter"), 1, at);
389
390
}

391
392
393
394
395
396
397
398
static void scalar_getgrouprect(t_glist *owner, t_glist *groupcanvas,
    t_word *data, t_template *template, int basex, int basey,
    int *x1, int *x2, int *y1, int *y2)
{
    t_gobj *y;
    for (y = groupcanvas->gl_list; y; y = y->g_next)
    {
        if (pd_class(&y->g_pd) == canvas_class &&
399
            ((t_canvas *)y)->gl_svg)
400
401
402
        {
            /* todo: accumulate basex and basey for correct offset */
            scalar_getgrouprect(owner, (t_glist *)y, data, template,
403
                basex, basey, x1, x2, y1, y2);
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
        }
        else
        {
            t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
            int nx1, ny1, nx2, ny2;
            if (!wb) continue;
            (*wb->w_parentgetrectfn)(y, owner,
                data, template, basex, basey,
                &nx1, &ny1, &nx2, &ny2);
            if (nx1 < *x1) *x1 = nx1;
            if (ny1 < *y1) *y1 = ny1;
            if (nx2 > *x2) *x2 = nx2;
            if (ny2 > *y2) *y2 = ny2;
            //fprintf(stderr,"====scalar_getrect x1 %d y1 %d x2 %d y2 %d\n",
            //    x1, y1, x2, y2);
        }
    }
}
 
Miller Puckette's avatar
Miller Puckette committed
423
424
425
static void scalar_getrect(t_gobj *z, t_glist *owner,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
426
    //fprintf(stderr,"scalar_getrect %d\n", array_joc);
Miller Puckette's avatar
Miller Puckette committed
427
    t_scalar *x = (t_scalar *)z;
428

Miller Puckette's avatar
Miller Puckette committed
429
430
431
    t_template *template = template_findbyname(x->sc_template);
    t_canvas *templatecanvas = template_findcanvas(template);
    int x1 = 0x7fffffff, x2 = -0x7fffffff, y1 = 0x7fffffff, y2 = -0x7fffffff;
432
    t_float basex, basey;
433
    t_float screenx1, screeny1, screenx2, screeny2;
434

435
436
437
    // EXPERIMENTAL: we assume that entire canvas is within
    // the rectangle--this is for arrays
    // with "jump on click" enabled TODO: test for other regressions
438
    // (there should not be any
439
    // provided the global variable array_joc is properly maintained)
440
441
    if (glist_istoplevel(owner) && array_joc)
    {
442
        x1 = -0x7fffffff, y1 = -0x7fffffff, x2 = 0x7fffffff, y2 = 0x7fffffff;
Miller Puckette's avatar
Miller Puckette committed
443
    }
444
445
    else
    {
446
447
448
        scalar_getbasexy(x, &basex, &basey);
            /* if someone deleted the template canvas, we're just a point */
        if (!templatecanvas)
Miller Puckette's avatar
Miller Puckette committed
449
        {
450
            //fprintf(stderr,"...point\n");
451
452
453
454
455
            x1 = x2 = glist_xtopixels(owner, basex);
            y1 = y2 = glist_ytopixels(owner, basey);
        }
        else
        {
456
            /* todo: bad flow with internal return here. make it cleaner */
457
            if (x->sc_bboxcache && 0)
458
            {
459
460
461
462
463
464
465
466
467
468
469
470
                screenx1 = glist_xtopixels(owner, x->sc_x1);
                screeny1 = glist_ytopixels(owner, x->sc_y1);
                screenx2 = glist_xtopixels(owner, x->sc_x2);
                screeny2 = glist_ytopixels(owner, x->sc_y2);

                *xp1 = (int)(screenx1 < screenx2 ? screenx1 : screenx2);
                *yp1 = (int)(screeny1 < screeny2 ? screeny1 : screeny2);
                *xp2 = (int)(screenx2 > screenx1 ? screenx2 : screenx1);
                *yp2 = (int)(screeny2 > screeny1 ? screeny2 : screeny1);

                //fprintf(stderr,"CACHED FINAL scalar_getrect "
                //               "x1 %g y1 %g x2 %g y2 %g\n",
471
472
473
474
                //    screenx1,
                //    screeny1,
                //    screenx2,
                //    screeny2);
475

476
477
                return;
            }
478
479
            x1 = y1 = 0x7fffffff;
            x2 = y2 = -0x7fffffff;
480
            scalar_getgrouprect(owner, templatecanvas, x->sc_vec, template,
481
482
                basex, basey, &x1, &x2, &y1, &y2);
            if (x2 < x1 || y2 < y1)
483
                x1 = y1 = x2 = y2 = 0;
Miller Puckette's avatar
Miller Puckette committed
484
485
        }
    }
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
    screenx1 = glist_xtopixels(owner, x1);
    screeny1 = glist_ytopixels(owner, y1);
    screenx2 = glist_xtopixels(owner, x2);
    screeny2 = glist_ytopixels(owner, y2);

    // Values for screen bounding box
    *xp1 = (int)(screenx1 < screenx2 ? screenx1 : screenx2);
    *xp2 = (int)(screenx2 > screenx1 ? screenx2 : screenx1);
    *yp1 = (int)(screeny1 < screeny2 ? screeny1 : screeny2);
    *yp2 = (int)(screeny2 > screeny1 ? screeny2 : screeny1);

    // Cache without glist_topixel (in case a gop is moved, for example)
    x->sc_x1 = x1;
    x->sc_x2 = x2;
    x->sc_y1 = y1;
    x->sc_y2 = y2;

    //fprintf(stderr,"COMPUTED FINAL scalar_getrect "
    //    "x1 %g y1 %g x2 %g y2 %g\n",
    //    screenx1,
    //    screeny1,
    //    screenx2,
    //    screeny2);

    x->sc_bboxcache = 1; // We now have cached values for the next call
Miller Puckette's avatar
Miller Puckette committed
511
512
}

513
void scalar_drawselectrect(t_scalar *x, t_glist *glist, int state)
Miller Puckette's avatar
Miller Puckette committed
514
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
515
516
517
    char tagbuf[MAXPDSTRING];
    sprintf(tagbuf, "scalar%lx", (long unsigned int)x->sc_vec);

518
    //fprintf(stderr,"scalar_drawselecterect%d\n", state);
Miller Puckette's avatar
Miller Puckette committed
519
520
521
    if (state)
    {
        int x1, y1, x2, y2;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
522
523
524
        t_float basex, basey;
        scalar_getbasexy(x, &basex, &basey);

Miller Puckette's avatar
Miller Puckette committed
525
526
        scalar_getrect(&x->sc_gobj, glist, &x1, &y1, &x2, &y2);
        x1--; x2++; y1--; y2++;
527
        if (glist_istoplevel(glist))
Jonathan Wilkes's avatar
Jonathan Wilkes committed
528
        {
529
530
531
532
            t_float xscale = glist_xtopixels(glist, 1) -
                glist_xtopixels(glist, 0);
            t_float yscale = glist_ytopixels(glist, 1) -
                glist_ytopixels(glist, 0);
533
post("xsclae is %g and yscale is %g", xscale, yscale);
534
535
536
537
538
            //sys_vgui(".x%lx.c create prect %d %d %d %d "
            //         "-strokewidth 1 -stroke $pd_colors(selection) "
            //         "-tags {select%lx selected}\n",
            //        glist_getcanvas(glist), x1, y1, x2, y2,
            //        x);
539
540
            gui_vmess("gui_scalar_draw_select_rect", "xsiiiiiff",
                glist_getcanvas(glist), tagbuf,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
541
                state,
542
543
544
545
546
547
                (int)(x1 / xscale + 0.499),
                (int)(y1 / yscale + 0.499),
                (int)(x2 / xscale + 0.499),
                (int)(y2 / yscale + 0.499),
                basex,
                basey);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
548
        }
Miller Puckette's avatar
Miller Puckette committed
549
550
551
    }
    else
    {
552
        if (glist_istoplevel(glist))
553
554
        {
            //sys_vgui(".x%lx.c delete select%lx\n", glist_getcanvas(glist), x);
555
556
            gui_vmess("gui_scalar_draw_select_rect", "xsiiiiiii",
                glist_getcanvas(glist), tagbuf,
557
558
559
                state,
                0, 0, 0, 0, 0, 0);
        }
Miller Puckette's avatar
Miller Puckette committed
560
561
562
    }
}

Jonathan Wilkes's avatar
Jonathan Wilkes committed
563
564
565
566
567
/* This is greatly simplified with Node-Webkit-- we just need to get the
   basex/basey for the scalar, in addition to the bbox of the scalar.

   This can be simplified further by using a single function on the GUI
   side, and sending it the "state" parameter.
568
569
*/
void scalar_select(t_gobj *z, t_glist *owner, int state)
Miller Puckette's avatar
Miller Puckette committed
570
{
571
    //fprintf(stderr,"scalar_select %d\n", state);
Miller Puckette's avatar
Miller Puckette committed
572
    t_scalar *x = (t_scalar *)z;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
573
574
575
576

    char tagbuf[MAXPDSTRING];
    sprintf(tagbuf, "scalar%lx", (long unsigned int)x->sc_vec);

Miller Puckette's avatar
Miller Puckette committed
577
578
579
    t_template *tmpl;
    t_symbol *templatesym = x->sc_template;
    t_atom at;
580
    //t_canvas *templatecanvas = NULL;
Miller Puckette's avatar
Miller Puckette committed
581
582
    t_gpointer gp;
    gpointer_init(&gp);
583
    gpointer_setglist(&gp, owner, &x->sc_gobj);
Miller Puckette's avatar
Miller Puckette committed
584
    SETPOINTER(&at, &gp);
585
586
    if (tmpl = template_findbyname(templatesym))
    {
Miller Puckette's avatar
Miller Puckette committed
587
588
        template_notify(tmpl, (state ? gensym("select") : gensym("deselect")),
            1, &at);
589
        //templatecanvas = template_findcanvas(tmpl);
590
    }
Miller Puckette's avatar
Miller Puckette committed
591
    gpointer_unset(&gp);
592
593
    if (state)
    {
594
        x->sc_selected = owner;
595
596
597
598
        //sys_vgui(".x%lx.c addtag selected withtag blankscalar%lx\n",
        //    glist_getcanvas(owner), x);
        //sys_vgui(".x%lx.c addtag scalar_selected withtag {.scalar%lx}\n",
        //    glist_getcanvas(owner), x->sc_vec);
599
        gui_vmess("gui_gobj_select", "xs",
600
            glist_getcanvas(owner), tagbuf);
601
602
603
    }
    else
    {
604
        x->sc_selected = 0;
605
606
607
608
        //sys_vgui(".x%lx.c dtag blankscalar%lx selected\n",
        //    glist_getcanvas(owner), x);
        //sys_vgui(".x%lx.c dtag .scalar%lx scalar_selected\n",
        //    glist_getcanvas(owner), x->sc_vec);
609
        gui_vmess("gui_gobj_deselect", "xs",
610
            glist_getcanvas(owner), tagbuf);
611
612
613
    }
    //sys_vgui("pdtk_select_all_gop_widgets .x%lx %lx %d\n",
    //    glist_getcanvas(owner), owner, state);
Miller Puckette's avatar
Miller Puckette committed
614
615
616
617
618
    scalar_drawselectrect(x, owner, state);
}

static void scalar_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
619
    //fprintf(stderr,"scalar_displace\n");
Miller Puckette's avatar
Miller Puckette committed
620
621
622
623
624
625
626
627
628
629
630
631
632
    t_scalar *x = (t_scalar *)z;
    t_symbol *templatesym = x->sc_template;
    t_template *template = template_findbyname(templatesym);
    t_symbol *zz;
    t_atom at[3];
    t_gpointer gp;
    int xonset, yonset, xtype, ytype, gotx, goty;
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return;
    }
    gotx = template_find_field(template, gensym("x"), &xonset, &xtype, &zz);
633
    if ((gotx && (xtype != DT_FLOAT)) || x->sc_selected != glist)
Miller Puckette's avatar
Miller Puckette committed
634
635
        gotx = 0;
    goty = template_find_field(template, gensym("y"), &yonset, &ytype, &zz);
636
    if ((goty && (ytype != DT_FLOAT)) || x->sc_selected != glist)
Miller Puckette's avatar
Miller Puckette committed
637
638
        goty = 0;
    if (gotx)
639
    {
Miller Puckette's avatar
Miller Puckette committed
640
641
        *(t_float *)(((char *)(x->sc_vec)) + xonset) +=
            dx * (glist_pixelstox(glist, 1) - glist_pixelstox(glist, 0));
642
643
644
        x->sc_x1 += dx;
        x->sc_x2 += dx;
    }
Miller Puckette's avatar
Miller Puckette committed
645
    if (goty)
646
    {
Miller Puckette's avatar
Miller Puckette committed
647
648
        *(t_float *)(((char *)(x->sc_vec)) + yonset) +=
            dy * (glist_pixelstoy(glist, 1) - glist_pixelstoy(glist, 0));
649
650
651
        x->sc_y1 += dy;
        x->sc_y2 += dy;
    }
Miller Puckette's avatar
Miller Puckette committed
652
    gpointer_init(&gp);
653
    gpointer_setglist(&gp, glist, &x->sc_gobj);
Miller Puckette's avatar
Miller Puckette committed
654
    SETPOINTER(&at[0], &gp);
655
656
    SETFLOAT(&at[1], (t_float)dx);
    SETFLOAT(&at[2], (t_float)dy);
Miller Puckette's avatar
Miller Puckette committed
657
658
659
660
    template_notify(template, gensym("displace"), 2, at);
    scalar_redraw(x, glist);
}

661
/* Kind of complicated at the moment. If a scalar is in a gop canvas, then
662
   we don't need to update its x/y fields (if it even has them) when displacing
663
664
665
   it.  Otherwise we do.  The member sc_selected is used to store the canvas
   where the selection exists-- if it matches the glist parameter below then we
   know the scalar is directly selected.  If not it's in a gop canvas.
666
*/
667
668
static void scalar_displace_withtag(t_gobj *z, t_glist *glist, int dx, int dy)
{
669
    //fprintf(stderr,"scalar_displace_withtag %lx %d %d\n", (t_int)z, dx, dy);
670
671
672
673
674
675
676
    t_scalar *x = (t_scalar *)z;
    t_symbol *templatesym = x->sc_template;
    t_template *template = template_findbyname(templatesym);
    t_symbol *zz;
    t_atom at[3];
    t_gpointer gp;
    int xonset, yonset, xtype, ytype, gotx, goty;
677
    t_float basex = 0, basey = 0;
678
679
680
681
682
683
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return;
    }
    gotx = template_find_field(template, gensym("x"), &xonset, &xtype, &zz);
684
    if ((gotx && (xtype != DT_FLOAT)) || x->sc_selected != glist)
685
686
        gotx = 0;
    goty = template_find_field(template, gensym("y"), &yonset, &ytype, &zz);
687
    if ((goty && (ytype != DT_FLOAT)) || x->sc_selected != glist)
688
689
        goty = 0;
    if (gotx)
690
    {
691
692
        *(t_float *)(((char *)(x->sc_vec)) + xonset) +=
            dx * (glist_pixelstox(glist, 1) - glist_pixelstox(glist, 0));
693
694
695
        x->sc_x1 += dx;
        x->sc_x2 += dx;
    }
696
    if (goty)
697
    {
698
699
        *(t_float *)(((char *)(x->sc_vec)) + yonset) +=
            dy * (glist_pixelstoy(glist, 1) - glist_pixelstoy(glist, 0));
700
701
702
        x->sc_y1 += dy;
        x->sc_y2 += dy;
    }
Ivica Bukvic's avatar
Ivica Bukvic committed
703
    //fprintf(stderr,"gotx=%d goty=%d\n", gotx, goty);
704
    scalar_getbasexy(x, &basex, &basey);
705
    gpointer_init(&gp);
706
    gpointer_setglist(&gp, glist, &x->sc_gobj);
707
708
709
710
    SETPOINTER(&at[0], &gp);
    SETFLOAT(&at[1], (t_float)dx);
    SETFLOAT(&at[2], (t_float)dy);
    template_notify(template, gensym("displace"), 2, at);
711

712
    canvas_getscroll(glist);
713
     
714
715
    /* Apparently this is no longer needed, so it is commented out.  But if
       we merge garrays back into this code we may need it... */
716
    /*
717
718
719
720
721
722
723
724
725
726
727
728
    if (template->t_sym != gensym("_float_array"))
    {
        t_gobj *y;
        t_canvas *templatecanvas = template_findcanvas(template);
        for (y = templatecanvas->gl_list; y; y = y->g_next)
            {
                t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
                if (!wb) continue;
                (*wb->w_parentdisplacefn)(y, glist, x->sc_vec, template,
                    basex, basey, dx, dy);
            }
    }
729
    */
730

731
732
733
    //scalar_redraw(x, glist);
}

Miller Puckette's avatar
Miller Puckette committed
734
735
736
737
738
739
740
741
742
743
744
static void scalar_activate(t_gobj *z, t_glist *owner, int state)
{
    /* post("scalar_activate %d", state); */
    /* later */
}

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

745
746
extern void svg_grouptogui(t_glist *g, t_template *template, t_word *data);

Jonathan Wilkes's avatar
Jonathan Wilkes committed
747
748
749
750
751
752
753
754
755
756
757
758
759
extern void svg_parentwidgettogui(t_gobj *z, t_glist *owner, t_word *data,
    t_template *template);

static void scalar_group_configure(t_scalar *x, t_glist *owner,
    t_template *template, t_glist *gl, t_glist *parent)
{
    t_gobj *y;
    char tagbuf[MAXPDSTRING];
    sprintf(tagbuf, "dgroup%lx.%lx", (long unsigned int)gl,
        (long unsigned int)x->sc_vec);
    char parentbuf[MAXPDSTRING];
    sprintf(parentbuf, "dgroup%lx.%lx", (long unsigned int)parent,
        (long unsigned int)x->sc_vec);
760
761
    gui_start_vmess("gui_draw_configure_all", "xs",
        glist_getcanvas(owner), tagbuf);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
    svg_grouptogui(gl, template, x->sc_vec);
    gui_end_vmess();
    for (y = gl->gl_list; y; y = y->g_next)
    {
        if (pd_class(&y->g_pd) == canvas_class &&
            ((t_glist *)y)->gl_svg)
        {
            scalar_group_configure(x, owner, template, (t_glist *)y, gl);
        }
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
        if (!wb) continue;
        //(*wb->w_parentvisfn)(y, owner, gl, x, x->sc_vec, template,
        //   0, 0, vis);
        svg_parentwidgettogui(y, owner, x->sc_vec, template);
    }
}

void scalar_configure(t_scalar *x, t_glist *owner)
{
    int vis = glist_isvisible(owner);
    if (vis)
    {
        //fprintf(stderr,"scalar_vis %d %lx\n", vis, (t_int)z);
        x->sc_bboxcache = 0;

        t_template *template = template_findbyname(x->sc_template);
        t_canvas *templatecanvas = template_findcanvas(template);
        t_gobj *y;
        t_float basex, basey;
        scalar_getbasexy(x, &basex, &basey);
            /* if we don't know how to draw it, make a small rectangle */

        t_float xscale = glist_xtopixels(owner, 1) - glist_xtopixels(owner, 0);
        t_float yscale = glist_ytopixels(owner, 1) - glist_ytopixels(owner, 0);

        char tagbuf[MAXPDSTRING];
        sprintf(tagbuf, "scalar%lx", (long unsigned int)x->sc_vec);
799
800
        gui_vmess("gui_scalar_configure_gobj", "xsiffffii",
            glist_getcanvas(owner), 
Jonathan Wilkes's avatar
Jonathan Wilkes committed
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
            tagbuf,
            glist_isselected(owner, &x->sc_gobj),
            xscale, 0.0, 0.0, yscale,
            (int)glist_xtopixels(owner, basex),
            (int)glist_ytopixels(owner, basey));

        char groupbuf[MAXPDSTRING];
        // Quick hack to make gui_create_scalar_group more general (so we
        // don't have to tack on "gobj" manually)


        //Not sure if we need this here...
        //sprintf(tagbuf, "scalar%lxgobj", (long unsigned int)x->sc_vec);
        //sprintf(groupbuf, "dgroup%lx.%lx", (long unsigned int)templatecanvas,
        //    (long unsigned int)x->sc_vec);
816
817
        //gui_vmess("gui_create_scalar_group", "xss",
        //    glist_getcanvas(owner), groupbuf, tagbuf); 
Jonathan Wilkes's avatar
Jonathan Wilkes committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
        //sys_vgui("pdtk_bind_scalar_mouseover "
        //         ".x%lx.c .x%lx.x%lx.template%lx {.x%lx}\n",
        //    glist_getcanvas(owner), glist_getcanvas(owner),
        //    owner, x->sc_vec, x);

        for (y = templatecanvas->gl_list; y; y = y->g_next)
        {
            t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
            if (!wb)
            {
                /* check subpatches for more drawing commands.
                   (Optimized to only search [group] subpatches) */
                if (pd_class(&y->g_pd) == canvas_class &&
                    ((t_glist *)y)->gl_svg)
                {
                    scalar_group_configure(x, owner, template,
                        (t_glist *)y, templatecanvas);
                }
                continue;
            }
            //(*wb->w_parentvisfn)(y, owner, 0, x, x->sc_vec, template,
            //    basex, basey, vis);
            svg_parentwidgettogui(y, owner, x->sc_vec, template);
        }
        if (glist_isselected(owner, &x->sc_gobj))
        {
            // we removed this because it caused infinite recursion
            // in the scalar-help.pd example
            //scalar_select(z, owner, 1);
            scalar_drawselectrect(x, owner, 0);
            scalar_drawselectrect(x, owner, 1);
        }
    }
}

853
854
855
856
857
858
static void scalar_groupvis(t_scalar *x, t_glist *owner, t_template *template,
    t_glist *gl, t_glist *parent, int vis)
{
    t_gobj *y;
    if (vis)
    {
859
860
861
862
        //sys_vgui(".x%lx.c create group -tags {.dgroup%lx.%lx} "
        //         "-parent {.dgroup%lx.%lx}\\\n",
        //    glist_getcanvas(owner), gl, x->sc_vec,
        //    parent, x->sc_vec);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
863
864
865
866
867
868
        char tagbuf[MAXPDSTRING];
        sprintf(tagbuf, "dgroup%lx.%lx", (long unsigned int)gl,
            (long unsigned int)x->sc_vec);
        char parentbuf[MAXPDSTRING];
        sprintf(parentbuf, "dgroup%lx.%lx", (long unsigned int)parent,
            (long unsigned int)x->sc_vec);
869
870
        gui_start_vmess("gui_create_scalar_group", "xss",
            glist_getcanvas(owner), tagbuf, parentbuf);
871
        svg_grouptogui(gl, template, x->sc_vec);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
872
873
        gui_end_vmess();
        //sys_gui("\n");
874
875
876
877
    }
    for (y = gl->gl_list; y; y = y->g_next)
    {
        if (pd_class(&y->g_pd) == canvas_class &&
878
            ((t_glist *)y)->gl_svg)
879
        {
880
            scalar_groupvis(x, owner, template, (t_glist *)y, gl, vis);
881
882
883
884
885
886
887
888
        }
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
        if (!wb) continue;
        (*wb->w_parentvisfn)(y, owner, gl, x, x->sc_vec, template,
            0, 0, vis);
    }
}

889
890
/* At present, scalars have a three-level hierarchy in tkpath,
   with two levels accessible by the user from within Pd:
891
892
   scalar - ".scalar%lx", x->sc_vec
     |      tkpath group with matrix derived from x/y fields,
893
894
895
896
     |      gop basexy, and gop scaling values. This group is
     |      not configurable by the user. This means that the
     |      [draw group] below can ignore basexy and gop junk
     |      when computing the transform matrix.
897
     v
898
899
900
901
902
   group  - ".dgroup%lx.%lx", templatecanvas, x->sc_vec
     |      group used as parent for all the toplevel drawing
     |      commands of the scalar (i.e., the ones located on
     |      the same canvas as the [struct]).  Its matrix and
     |      options aren't yet accessible by the user.
903
     v
904
905
   (draw  - ".draw%lx.%lx", (t_draw *ptr), x->sc_vec
     |      the actual drawing command: rectangle, path, etc.
906
907
908
909
910
911
912
     or     Each has its own matrix and options which can set
   scelem   with messages to the corresponding [draw] object.
     or   - ds arrays can nest arbitrarily deep. Scelem is for
   group)   data structure arrays.  group is for more groups.
     |
     v
    etc.
913
914
915
916

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

    x->sc_bboxcache = 0;

Miller Puckette's avatar
Miller Puckette committed
924
925
926
    t_template *template = template_findbyname(x->sc_template);
    t_canvas *templatecanvas = template_findcanvas(template);
    t_gobj *y;
927
    t_float basex, basey;
Miller Puckette's avatar
Miller Puckette committed
928
929
930
931
932
933
934
935
    scalar_getbasexy(x, &basex, &basey);
        /* if we don't know how to draw it, make a small rectangle */
    if (!templatecanvas)
    {
        if (vis)
        {
            int x1 = glist_xtopixels(owner, basex);
            int y1 = glist_ytopixels(owner, basey);
936
            sys_vgui(".x%lx.c create prect %d %d %d %d "
937
938
939
940
                     "-tags {blankscalar%lx %s}\n",
                glist_getcanvas(owner), x1-1, y1-1, x1+1, y1+1, x,
                (glist_isselected(owner, &x->sc_gobj) ?
                    "scalar_selected" : ""));
Miller Puckette's avatar
Miller Puckette committed
941
        }
942
943
        else sys_vgui(".x%lx.c delete blankscalar%lx\n",
            glist_getcanvas(owner), x);
Miller Puckette's avatar
Miller Puckette committed
944
945
        return;
    }
946
947
    //else sys_vgui(".x%lx.c delete blankscalar%lx\n",
    //    glist_getcanvas(owner), x);
948
949
950
951
952

    if (vis)
    {
        t_float xscale = glist_xtopixels(owner, 1) - glist_xtopixels(owner, 0);
        t_float yscale = glist_ytopixels(owner, 1) - glist_ytopixels(owner, 0);
953
954
955
        /* we translate the .scalar%lx group to displace it on the tk side.
           This is the outermost group for the scalar, something like a
           poor man's viewport.
956
957
958
959
960
961
962
           Also:
             * the default stroke is supposed to be "none"
             * default fill is supposed to be black.
             * stroke-linejoin should be "miter", not "round"  
           To fix these, we set the correct fill/stroke/strokelinjoin options
           here on the .scalar%lx group. (Notice also that tkpath doesn't
           understand "None"-- instead we must send an empty symbol.) */
963
964
965
966
967
968
969
970
        //sys_vgui(".x%lx.c create group -tags {.scalar%lx %s} "
        //    "-matrix { {%g %g} {%g %g} {%d %d} } "
        //    "-stroke \"\" -fill black -strokelinejoin miter\n",
        //    glist_getcanvas(owner), x->sc_vec,
        //    (glist_isselected(owner, &x->sc_gobj) ? "scalar_selected" : ""),
        //    xscale, 0.0, 0.0, yscale, (int)glist_xtopixels(owner, basex),
        //    (int)glist_ytopixels(owner, basey)
        //    );
Jonathan Wilkes's avatar
Jonathan Wilkes committed
971
972
        char tagbuf[MAXPDSTRING];
        sprintf(tagbuf, "scalar%lx", (long unsigned int)x->sc_vec);
973
974
        gui_vmess("gui_create_scalar", "xsiffffiii",
            glist_getcanvas(owner), 
Jonathan Wilkes's avatar
Jonathan Wilkes committed
975
976
977
978
            tagbuf,
            glist_isselected(owner, &x->sc_gobj),
            xscale, 0.0, 0.0, yscale,
            (int)glist_xtopixels(owner, basex),
979
980
            (int)glist_ytopixels(owner, basey),
            glist_istoplevel(owner));
981
982
983
        //sys_vgui(".x%lx.c create group -tags {.dgroup%lx.%lx} "
        //         "-parent {.scalar%lx}\n",
        //    glist_getcanvas(owner), templatecanvas, x->sc_vec, x->sc_vec);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
984
985
986
987
988
989
        char groupbuf[MAXPDSTRING];
        // Quick hack to make gui_create_scalar_group more general (so we
        // don't have to tack on "gobj" manually)
        sprintf(tagbuf, "scalar%lxgobj", (long unsigned int)x->sc_vec);
        sprintf(groupbuf, "dgroup%lx.%lx", (long unsigned int)templatecanvas,
            (long unsigned int)x->sc_vec);
990
991
        gui_vmess("gui_create_scalar_group", "xss",
            glist_getcanvas(owner), groupbuf, tagbuf);
992

993
994
        sys_vgui("pdtk_bind_scalar_mouseover "
                 ".x%lx.c .x%lx.x%lx.template%lx {.x%lx}\n",
995
996
            glist_getcanvas(owner), glist_getcanvas(owner),
            owner, x->sc_vec, x);
997
    }
Miller Puckette's avatar
Miller Puckette committed
998

999
    /* warning: don't need--- have recursive func. */
Miller Puckette's avatar
Miller Puckette committed
1000
1001
1002
    for (y = templatecanvas->gl_list; y; y = y->g_next)
    {
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
1003
1004
        if (!wb)
        {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1005
1006
            /* check subpatches for more drawing commands.
               (Optimized to only search [group] subpatches) */
1007
            if (pd_class(&y->g_pd) == canvas_class &&
1008
1009
                ((t_glist *)y)->gl_svg)
            {
1010
1011
                scalar_groupvis(x, owner, template,
                    (t_glist *)y, templatecanvas, vis);
1012
            }
1013
1014
1015
            continue;
        }
        (*wb->w_parentvisfn)(y, owner, 0, x, x->sc_vec, template,
1016
            basex, basey, vis);
Miller Puckette's avatar
Miller Puckette committed
1017
    }
1018
    if (!vis)
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1019
    {
1020
1021
        //sys_vgui(".x%lx.c delete .scalar%lx\n", glist_getcanvas(owner),
        //    x->sc_vec);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1022
1023
        char tagbuf[MAXPDSTRING];
        sprintf(tagbuf, "scalar%lx", (long unsigned int)x->sc_vec);
1024
1025
        gui_vmess("gui_scalar_erase", "xs",
            glist_getcanvas(owner), tagbuf);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1026
    }
1027

1028
    sys_unqueuegui(x);
Miller Puckette's avatar
Miller Puckette committed
1029
1030
    if (glist_isselected(owner, &x->sc_gobj))
    {
1031
1032
        // we removed this because it caused infinite recursion
        // in the scalar-help.pd example
1033
        //scalar_select(z, owner, 1);
1034
        post("here we are in scalar vis, selected...");
Miller Puckette's avatar
Miller Puckette committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
        scalar_drawselectrect(x, owner, 0);
        scalar_drawselectrect(x, owner, 1);
    }
}

static void scalar_doredraw(t_gobj *client, t_glist *glist)
{
    scalar_vis(client, glist, 0);
    scalar_vis(client, glist, 1);
1044
1045
1046
1047
1048
1049
    if (glist_isselected(glist_getcanvas(glist), (t_gobj *)glist))
    {
        //fprintf(stderr,"yes\n");
        sys_vgui("pdtk_select_all_gop_widgets .x%lx %lx %d\n",
            glist_getcanvas(glist), glist, 1);
    }
1050
    canvas_getscroll(glist_getcanvas(glist));
Miller Puckette's avatar
Miller Puckette committed
1051
1052
1053
1054
1055
}

void scalar_redraw(t_scalar *x, t_glist *glist)
{
    if (glist_isvisible(glist))
1056
        scalar_doredraw((t_gobj *)x, glist);
1057
        //sys_queuegui(x, glist, scalar_doredraw);
Miller Puckette's avatar
Miller Puckette committed
1058
1059
}

1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
/* here we call the parentclickfns for drawing commands.
   We recurse all the way to the end of the glist (and, for the
   groups, as deep as they go) and then call the functions from the bottom up.
   This way the clicks can "bubble" up from the bottom.  This has the effect
   that the shape visually closest to the front gets called first.

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

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

/*
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
int scalar_groupclick(struct _glist *groupcanvas,
    t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
    t_float xloc, t_float yloc, int xpix, int ypix,
    int shift, int alt, int dbl, int doit, t_float basex, t_float basey)
{
    int hit = 0;
    t_gobj *y;
    for (y = groupcanvas->gl_list; y; y = y->g_next)
    {
        if (pd_class(&y->g_pd) == canvas_class &&
            ((t_glist *)y)->gl_svg)
        {
            if (hit = scalar_groupclick((t_glist *)y, data, template, sc, ap,
                owner, xloc, yloc, xpix, ypix,
                shift, alt, dbl, doit, basex, basey))
            {
                return (hit);
            }
        }
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
        if (!wb) continue;
        if (hit = (*wb->w_parentclickfn)(y, owner,
            data, template, sc, ap, basex + xloc, basey + yloc,
            xpix, ypix, shift, alt, dbl, doit))
        {
            return (hit);
        }
    }
    return 0;
}
1153
*/
1154

Miller Puckette's avatar
Miller Puckette committed
1155
1156
int scalar_doclick(t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
1157
    t_float xloc, t_float yloc, int xpix, int ypix,
Miller Puckette's avatar
Miller Puckette committed
1158
1159
1160
1161
    int shift, int alt, int dbl, int doit)
{
    int hit = 0;
    t_canvas *templatecanvas = template_findcanvas(template);
1162
    t_atom at[2];
1163
    t_gobj *obj;
1164
1165
    t_float basex = template_getfloat(template, gensym("x"), data, 0);
    t_float basey = template_getfloat(template, gensym("y"), data, 0);
1166
1167
    //fprintf(stderr,"=================scalar_doclick %f %f %f %f %d\n",
    //    basex, basey, xloc, yloc, doit);
Ivica Bukvic's avatar
Ivica Bukvic committed
1168

1169
1170
1171
1172
1173
    SETFLOAT(at, basex + xloc);
    SETFLOAT(at+1, basey + yloc);
    if (doit)
    {
        //fprintf(stderr,"    doit\n");
1174
1175
        template_notifyforscalar(template, owner, 
            sc, gensym("click"), 2, at);
1176
    }
Ivica Bukvic's avatar
Ivica Bukvic committed
1177

1178
1179
1180
1181
1182
    // if we are nested ignore xloc and yloc, otherwise
    // nested objects get their hitbox miscalculated
    if (xloc != 0.0 || yloc != 0.0)
    {
        //fprintf(stderr,"ignoring\n");
1183
1184
        //basex = 0.0;
        //basey = 0.0;
1185
    }
Ivica Bukvic's avatar
Ivica Bukvic committed
1186

1187
    if (templatecanvas)
1188
1189
    {
        if (!(obj = templatecanvas->gl_list)) return 0;
1190
1191
        hit = scalar_groupclick(templatecanvas, data, template, sc, ap,
                    owner, xloc, yloc, xpix, ypix,
1192
1193
                    shift, alt, dbl, doit, basex, basey, obj);
    }
1194
    return hit;
Miller Puckette's avatar
Miller Puckette committed
1195
1196
}

1197
1198
1199
/* Unfortunately, nested gops don't yet handle scalar clicks correctly. The
   nested scalar seems not to receive the click.  However, the enter/leave
   messages happen just fine since most of their logic is in tcl/tk. */
Miller Puckette's avatar
Miller Puckette committed
1200
1201
1202
static int scalar_click(t_gobj *z, struct _glist *owner,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
1203
    //fprintf(stderr,"scalar_click %d %d\n", xpix, ypix);
Miller Puckette's avatar
Miller Puckette committed
1204
    t_scalar *x = (t_scalar *)z;
1205

1206
1207
1208
    x->sc_bboxcache = 0;

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

Miller Puckette's avatar
Miller Puckette committed
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
    return (scalar_doclick(x->sc_vec, template, x, 0,
        owner, 0, 0, xpix, ypix, shift, alt, dbl, doit));
}

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

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

1228
1229
1230
1231
1232
static void scalar_menuopen(t_scalar *x)
{
    post("tried to open a thing");
}

Miller Puckette's avatar
Miller Puckette committed
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
static void scalar_properties(t_gobj *z, struct _glist *owner)
{
    t_scalar *x = (t_scalar *)z;
    char *buf, buf2[80];
    int bufsize;
    t_binbuf *b;
    glist_noselect(owner);
    glist_select(owner, z);
    b = glist_writetobinbuf(owner, 0);
    binbuf_gettext(b, &buf, &bufsize);
    binbuf_free(b);
    buf = t_resizebytes(buf, bufsize, bufsize+1);
    buf[bufsize] = 0;
    sprintf(buf2, "pdtk_data_dialog %%s {");
    gfxstub_new((t_pd *)owner, x, buf2);
    sys_gui(buf);
    sys_gui("}\n");
    t_freebytes(buf, bufsize+1);
}

static t_widgetbehavior scalar_widgetbehavior =
{
    scalar_getrect,
    scalar_displace,
    scalar_select,
    scalar_activate,
    scalar_delete,
    scalar_vis,
    scalar_click,
1262
    scalar_displace_withtag,
Miller Puckette's avatar
Miller Puckette committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
};

static void scalar_free(t_scalar *x)
{
    t_symbol *templatesym = x->sc_template;
    t_template *template = template_findbyname(templatesym);
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return;
    }
    word_free(x->sc_vec, template);
1275
1276
1277
    char buf[50];
    sprintf(buf, ".x%lx", (long unsigned int)x);
    pd_unbind(&x->sc_gobj.g_pd, gensym(buf));
Miller Puckette's avatar
Miller Puckette committed
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
    gfxstub_deleteforkey(x);
        /* the "size" field in the class is zero, so Pd doesn't try to free
        us automatically (see pd_free()) */
    freebytes(x, sizeof(t_scalar) + (template->t_n - 1) * sizeof(*x->sc_vec));
}

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

void g_scalar_setup(void)
{
    scalar_class = class_new(gensym("scalar"), 0, (t_method)scalar_free, 0,
        CLASS_GOBJ, 0);
1290
1291
    class_addmethod(scalar_class, (t_method)scalar_mouseover,
        gensym("mouseover"), A_FLOAT, A_NULL);
1292
1293
    class_addmethod(scalar_class, (t_method)scalar_menuopen,
        gensym("menu-open"), 0);
Miller Puckette's avatar
Miller Puckette committed
1294
1295
1296
1297
    class_setwidget(scalar_class, &scalar_widgetbehavior);
    class_setsavefn(scalar_class, scalar_save);
    class_setpropertiesfn(scalar_class, scalar_properties);
}