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

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

*/

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

t_class *scalar_class;

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

void word_restore(t_word *wp, t_template *template,
    int argc, t_atom *argv)
{
    int i, nitems = template->t_n;
    t_dataslot *datatypes = template->t_vec;
    for (i = 0; i < nitems; i++, datatypes++, wp++)
    {
        int type = datatypes->ds_type;
        if (type == DT_FLOAT)
        {
52
            t_float f;
Miller Puckette's avatar
Miller Puckette committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
            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);
    }
}

    /* 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);
    }
    x = (t_scalar *)getbytes(sizeof(t_scalar) +
        (template->t_n - 1) * sizeof(*x->sc_vec));
    x->sc_gobj.g_pd = scalar_class;
    x->sc_template = templatesym;
    gpointer_setglist(&gp, owner, x);
    word_init(x->sc_vec, template, &gp);
    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);
}

/* -------------------- widget behavior for scalar ------------ */
148
void scalar_getbasexy(t_scalar *x, t_float *basex, t_float *basey)
Miller Puckette's avatar
Miller Puckette committed
149
150
151
152
153
154
{
    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);
}

155
156
extern int array_joc;

157
158
159
160
161
extern void template_notifyforscalar(t_template *template, t_glist *owner,
    t_scalar *sc, t_symbol *s, int argc, t_atom *argv);

static int sc_isentered = 0;
static t_scalar *sc_entered = 0;
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

static void scalar_leave(t_scalar *x, t_glist *owner, t_template *template,
    int force_leave)
{
    /* hack for enter/leave struct notifications */
    if (sc_isentered == 1)
    {
        /* change value to see if there's a call
           to scalar_click the next time around. Of
           course it will be too late so we'll be
           one pixel off, but it's better than nothing.
        */
        sc_isentered = -1;
    }
    else if (sc_isentered == -1 || force_leave)
    {
        t_atom at[1];
        template_notifyforscalar(template, owner, x, gensym("leave"), 1, at);
        sc_entered = 0;
        sc_isentered = 0;
    }
}

Miller Puckette's avatar
Miller Puckette committed
185
186
187
static void scalar_getrect(t_gobj *z, t_glist *owner,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
188
	//fprintf(stderr,"scalar_getrect %d\n", array_joc);
Miller Puckette's avatar
Miller Puckette committed
189
    t_scalar *x = (t_scalar *)z;
190

Miller Puckette's avatar
Miller Puckette committed
191
192
193
194
    t_template *template = template_findbyname(x->sc_template);
    t_canvas *templatecanvas = template_findcanvas(template);
    int x1 = 0x7fffffff, x2 = -0x7fffffff, y1 = 0x7fffffff, y2 = -0x7fffffff;
    t_gobj *y;
195
    t_float basex, basey;
196

197
    if (sc_entered == x)
198
        scalar_leave(x, owner, template, 0);
199

200
201
202
203
204
    // EXPERIMENTAL: we assume that entire canvas is withing the rectangle--this is for arrays
    // with "jump on click" enabled TODO: test for other regressions (there shouuld not be any
    // provided the global variable array_joc is properly maintained)
    if (glist_istoplevel(owner) && array_joc) {
        x1 = -0x7fffffff, y1 = -0x7fffffff, x2 = 0x7fffffff, y2 = 0x7fffffff;
Miller Puckette's avatar
Miller Puckette committed
205
    }
206
207
208
209
210

    else {
        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
211
        {
212
213
214
215
216
217
    		//fprintf(stderr,"...point\n");
            x1 = x2 = glist_xtopixels(owner, basex);
            y1 = y2 = glist_ytopixels(owner, basey);
        }
        else
        {
218
219
220
221
222
223
224
225
226
            /* todo: bad flow with internal return here. make it cleaner */
            if (x->sc_bboxcache)
            {
                *xp1 = x->sc_x1;
                *yp1 = x->sc_y1;
                *xp2 = x->sc_x2;
                *yp2 = x->sc_y2;
                return;
            }
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
            x1 = y1 = 0x7fffffff;
            x2 = y2 = -0x7fffffff;
            for (y = templatecanvas->gl_list; y; y = y->g_next)
            {
                t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
                int nx1, ny1, nx2, ny2;
                if (!wb) continue;
                (*wb->w_parentgetrectfn)(y, owner,
                    x->sc_vec, 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);
            }
            if (x2 < x1 || y2 < y1)
                x1 = y1 = x2 = y2 = 0;
Miller Puckette's avatar
Miller Puckette committed
245
246
        }
    }
Ivica Bukvic's avatar
Ivica Bukvic committed
247
    //fprintf(stderr,"FINAL scalar_getrect x1 %d y1 %d x2 %d y2 %d\n", x1, y1, x2, y2);
248
249
250
251
252
    *xp1 = x->sc_x1 = x1;
    *yp1 = x->sc_y1 = y1;
    *xp2 = x->sc_x2 = x2;
    *yp2 = x->sc_y2 = y2; 
    x->sc_bboxcache = 1;
Miller Puckette's avatar
Miller Puckette committed
253
254
}

255
void scalar_drawselectrect(t_scalar *x, t_glist *glist, int state)
Miller Puckette's avatar
Miller Puckette committed
256
257
258
259
260
261
262
{
    if (state)
    {
        int x1, y1, x2, y2;
       
        scalar_getrect(&x->sc_gobj, glist, &x1, &y1, &x2, &y2);
        x1--; x2++; y1--; y2++;
263
		if (glist_istoplevel(glist))
264
		    sys_vgui(".x%lx.c create polyline %d %d %d %d %d %d %d %d %d %d \
265
		        -strokewidth 1 -stroke $pd_colors(selection) -tags {select%lx selected}\n",
266
267
		            glist_getcanvas(glist), x1, y1, x1, y2, x2, y2, x2, y1, x1, y1,
		            x);
Miller Puckette's avatar
Miller Puckette committed
268
269
270
    }
    else
    {
271
272
		if (glist_istoplevel(glist))
        	sys_vgui(".x%lx.c delete select%lx\n", glist_getcanvas(glist), x);
Miller Puckette's avatar
Miller Puckette committed
273
274
275
    }
}

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* This is a workaround.  Since scalars are contained within a tkpath
   group, and since tkpath groups don't have coords, we have to fudge
   things with regard to Pd-l2ork's normal *_displace_withtag functionality.
   (Additionally, we can't just fall back to the old displace method
   because it too assumes the canvas item has an xy coord.)

   We draw the selection rect but don't add the scalar to the
   "selected" tag.  This means when Pd-l2ork issues the canvas "move"
   command, our scalar doesn't go anywhere.  Instead we get the callback
   to scalar_displace_withtag and do another workaround to get the
   new position and feed it to the scalar's matrix.

   This creates a problem with gop canvases, and yet _another_ partial
   workaround.  We need the global variable below to let the so that
   we can figure out whether the displace call is coming from the scalar
   itself or from a parent graph.  This means scalars won't currently
   respond properly in nested gops.  (I think that requires a window
   item on the canvas to replace the current gop rect.)
*/
t_glist *select_owner = 0; /* kludge variable used by displace_withtag fn */
void scalar_select(t_gobj *z, t_glist *owner, int state)
Miller Puckette's avatar
Miller Puckette committed
297
{
298
	//fprintf(stderr,"scalar_select %d\n", state);
Miller Puckette's avatar
Miller Puckette committed
299
300
301
302
    t_scalar *x = (t_scalar *)z;
    t_template *tmpl;
    t_symbol *templatesym = x->sc_template;
    t_atom at;
303
	t_canvas *templatecanvas = NULL;
Miller Puckette's avatar
Miller Puckette committed
304
305
306
307
    t_gpointer gp;
    gpointer_init(&gp);
    gpointer_setglist(&gp, owner, x);
    SETPOINTER(&at, &gp);
308
    if (tmpl = template_findbyname(templatesym)) {
Miller Puckette's avatar
Miller Puckette committed
309
310
        template_notify(tmpl, (state ? gensym("select") : gensym("deselect")),
            1, &at);
311
312
		templatecanvas = template_findcanvas(tmpl);
	}
Miller Puckette's avatar
Miller Puckette committed
313
    gpointer_unset(&gp);
314
	if (state) {
315
316
                select_owner = owner;
		sys_vgui(".x%lx.c addtag selected withtag blankscalar%lx\n",
317
			glist_getcanvas(owner), x);
318
319
320
321
322
323
324
325
		/* how do we navigate through a t_word list?
        if (x->sc_vec) {
            t_word *v = x->sc_vec;
            while(v) {
    			sys_vgui(".x%lx.c addtag selected withtag .x%lx.x%lx.template%lx\n",
    				glist_getcanvas(owner), glist_getcanvas(owner), owner, v);
            }
        }*/
326
		/*if (templatecanvas) {
327
328
329
330
331
			// get the universal tag for all nested objects
			t_canvas *tag = owner;
			while (tag->gl_owner) {
				tag = tag->gl_owner;
			}
332
			sys_vgui(".x%lx.c addtag selected withtag %lx\n",
333
				glist_getcanvas(owner), (t_int)tag);
334
		}*/
335
	} else {
336
337
        select_owner = 0;
		sys_vgui(".x%lx.c dtag blankscalar%lx selected\n",
338
			glist_getcanvas(owner), x);
339
340
                sys_vgui(".x%lx.c dtag .x%lx.x%lx.template%lx selected\n",
                    glist_getcanvas(owner), glist_getcanvas(owner), owner, x->sc_vec);
341
342
343
344
345
346
347
348
        /* how do we navigate through a t_word list?
        if (x->sc_vec) {
            t_word *v = x->sc_vec;
            while (v) {
        		sys_vgui(".x%lx.c dtag .x%lx.x%lx.template%lx selected\n",
        			glist_getcanvas(owner), glist_getcanvas(owner), owner, x->sc_vec);
            }
        }*/
349
		/*if (templatecanvas) {
Ivica Bukvic's avatar
Ivica Bukvic committed
350
351
352
353
354
			// get the universal tag for all nested objects
			t_canvas *tag = owner;
			while (tag->gl_owner) {
				tag = tag->gl_owner;
			}
355
			sys_vgui(".x%lx.c dtag %lx selected\n",
Ivica Bukvic's avatar
Ivica Bukvic committed
356
				glist_getcanvas(owner), (t_int)tag);
357
		}*/
358
	}
359
	//sys_vgui("pdtk_select_all_gop_widgets .x%lx %lx %d\n", glist_getcanvas(owner), owner, state);
Miller Puckette's avatar
Miller Puckette committed
360
361
362
363
364
    scalar_drawselectrect(x, owner, state);
}

static void scalar_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
365
	//fprintf(stderr,"scalar_displace\n");
Miller Puckette's avatar
Miller Puckette committed
366
367
368
369
370
371
372
    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;
373
    t_float basex = 0, basey = 0;
Miller Puckette's avatar
Miller Puckette committed
374
375
376
377
378
379
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return;
    }
    gotx = template_find_field(template, gensym("x"), &xonset, &xtype, &zz);
380
    if ((gotx && (xtype != DT_FLOAT)) || select_owner != glist)
Miller Puckette's avatar
Miller Puckette committed
381
382
        gotx = 0;
    goty = template_find_field(template, gensym("y"), &yonset, &ytype, &zz);
383
    if ((goty && (ytype != DT_FLOAT)) || select_owner != glist)
Miller Puckette's avatar
Miller Puckette committed
384
385
386
387
388
389
390
391
392
393
        goty = 0;
    if (gotx)
        *(t_float *)(((char *)(x->sc_vec)) + xonset) +=
            dx * (glist_pixelstox(glist, 1) - glist_pixelstox(glist, 0));
    if (goty)
        *(t_float *)(((char *)(x->sc_vec)) + yonset) +=
            dy * (glist_pixelstoy(glist, 1) - glist_pixelstoy(glist, 0));
    gpointer_init(&gp);
    gpointer_setglist(&gp, glist, x);
    SETPOINTER(&at[0], &gp);
394
395
    SETFLOAT(&at[1], (t_float)dx);
    SETFLOAT(&at[2], (t_float)dy);
Miller Puckette's avatar
Miller Puckette committed
396
397
398
399
    template_notify(template, gensym("displace"), 2, at);
    scalar_redraw(x, glist);
}

400
401
402
403
404
405
406
/* Very complicated at the moment. If a scalar is in a gop canvas, then
   we don't need to update its x/y fields (if it even has them) when displacing
   it.  Otherwise we do.  The global selected_owner variable is used to store
   the "owner" canvas-- if it matches the glist parameter below then we know
   the scalar is directly selected.  If not it's in a gop canvas.  (This doesn't
   yet handle nested GOPs, unfortunately.)
*/
407
408
static void scalar_displace_withtag(t_gobj *z, t_glist *glist, int dx, int dy)
{
Ivica Bukvic's avatar
Ivica Bukvic committed
409
	//fprintf(stderr,"scalar_displace_withtag %lx %d %d\n", (t_int)z, dx, dy);
410
411
412
413
414
415
416
    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;
417
    t_float basex = 0, basey = 0;
418
419
420
421
422
423
    if (!template)
    {
        error("scalar: couldn't find template %s", templatesym->s_name);
        return;
    }
    gotx = template_find_field(template, gensym("x"), &xonset, &xtype, &zz);
424
    if ((gotx && (xtype != DT_FLOAT)) || select_owner != glist)
425
426
        gotx = 0;
    goty = template_find_field(template, gensym("y"), &yonset, &ytype, &zz);
427
    if ((goty && (ytype != DT_FLOAT)) || select_owner != glist)
428
429
        goty = 0;
    if (gotx)
430
    {
431
432
        *(t_float *)(((char *)(x->sc_vec)) + xonset) +=
            dx * (glist_pixelstox(glist, 1) - glist_pixelstox(glist, 0));
433
434
435
        x->sc_x1 += dx;
        x->sc_x2 += dx;
    }
436
    if (goty)
437
    {
438
439
        *(t_float *)(((char *)(x->sc_vec)) + yonset) +=
            dy * (glist_pixelstoy(glist, 1) - glist_pixelstoy(glist, 0));
440
441
442
        x->sc_y1 += dy;
        x->sc_y2 += dy;
    }
443
    scalar_getbasexy(x, &basex, &basey);
444
445
446
447
448
449
    gpointer_init(&gp);
    gpointer_setglist(&gp, glist, x);
    SETPOINTER(&at[0], &gp);
    SETFLOAT(&at[1], (t_float)dx);
    SETFLOAT(&at[2], (t_float)dy);
    template_notify(template, gensym("displace"), 2, at);
450
451
452
453
454
455
456

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

    sys_vgui(".x%lx.c itemconfigure {.scalar%lx} -matrix { {%g %g} {%g %g} {%d %d} }\n",
        glist_getcanvas(glist), x->sc_vec, 1.0, 0.0, 0.0, 1.0, (int)glist_xtopixels(select_owner, basex) + (select_owner == glist ? 0 : dx), (int)glist_ytopixels(select_owner, basey) + (select_owner == glist ? 0 : dy));

457
458
459
    //scalar_redraw(x, glist);
}

Miller Puckette's avatar
Miller Puckette committed
460
461
462
463
464
465
466
467
468
469
470
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 */
}

471
472
473
474
475
476
477
/* At present, scalars have a three-level hierarchy in tkpath,
   with two levels accessible by the user from within Pd:
   scalar - tkpath group with matrix derived from x/y fields,
     |      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.
478
     v
479
480
481
482
   dgroup - user-facing group which is the parent for all the
     |      scalar's drawing commands. Its matrix and options
     |      can be accessed from the [draw group] object (one
     |      per templatecanvas).
483
     v
484
485
486
   draw   - the actual drawing command: rectangle, path, etc.
            Each has its own matrix and options which can set
            with messages to the corresponding [draw] object.
487
488
489
490

   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
491
492
static void scalar_vis(t_gobj *z, t_glist *owner, int vis)
{
493
	//fprintf(stderr,"scalar_vis %d\n", vis);
Miller Puckette's avatar
Miller Puckette committed
494
    t_scalar *x = (t_scalar *)z;
495
496
497

    x->sc_bboxcache = 0;

Miller Puckette's avatar
Miller Puckette committed
498
499
500
    t_template *template = template_findbyname(x->sc_template);
    t_canvas *templatecanvas = template_findcanvas(template);
    t_gobj *y;
501
    t_float basex, basey;
Miller Puckette's avatar
Miller Puckette committed
502
503
504
505
506
507
508
509
    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);
510
            sys_vgui(".x%lx.c create prect %d %d %d %d -tags {blankscalar%lx}\n",
511
                glist_getcanvas(owner), x1-1, y1-1, x1+1, y1+1, x);
Miller Puckette's avatar
Miller Puckette committed
512
        }
513
        else sys_vgui(".x%lx.c delete blankscalar%lx\n", glist_getcanvas(owner), x);
Miller Puckette's avatar
Miller Puckette committed
514
515
        return;
    }
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
	//else sys_vgui(".x%lx.c delete blankscalar%lx\n", glist_getcanvas(owner), x);

    if (vis)
    {
        t_float xscale = glist_xtopixels(owner, 1) - glist_xtopixels(owner, 0);
        t_float yscale = glist_ytopixels(owner, 1) - glist_ytopixels(owner, 0);
        /* we could use the tag .template%lx for easy access from
           the draw_class, but that's not necessary at this point */
        sys_vgui(".x%lx.c create group -tags {.scalar%lx} "
            "-matrix { {%g %g} {%g %g} {%d %d} }\n",
            glist_getcanvas(owner), x->sc_vec,
            1.0, 0.0, 0.0, 1.0, (int)glist_xtopixels(owner, basex), (int)glist_ytopixels(owner, basey)
            );
        sys_vgui(".x%lx.c create group -tags {.dgroup%lx} -parent {.scalar%lx}\n",
            glist_getcanvas(owner), x->sc_vec, x->sc_vec);
    }
Miller Puckette's avatar
Miller Puckette committed
532
533
534
535
536

    for (y = templatecanvas->gl_list; y; y = y->g_next)
    {
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
        if (!wb) continue;
537
        (*wb->w_parentvisfn)(y, owner, x, x->sc_vec, template, basex, basey, vis);
Miller Puckette's avatar
Miller Puckette committed
538
    }
539
540
541
    if (!vis)
        sys_vgui(".x%lx.c delete .scalar%lx\n", glist_getcanvas(owner), x->sc_vec);

542

543
    sys_unqueuegui(x);
Miller Puckette's avatar
Miller Puckette committed
544
545
    if (glist_isselected(owner, &x->sc_gobj))
    {
546
		scalar_select(z, owner, 1);
Miller Puckette's avatar
Miller Puckette committed
547
548
549
550
551
552
553
554
555
        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);
556
557
558
559
	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);
	}
Miller Puckette's avatar
Miller Puckette committed
560
561
562
563
564
}

void scalar_redraw(t_scalar *x, t_glist *glist)
{
    if (glist_isvisible(glist))
565
566
		scalar_doredraw((t_gobj *)x, glist);
        //sys_queuegui(x, glist, scalar_doredraw);
Miller Puckette's avatar
Miller Puckette committed
567
568
569
570
}

int scalar_doclick(t_word *data, t_template *template, t_scalar *sc,
    t_array *ap, struct _glist *owner,
571
    t_float xloc, t_float yloc, int xpix, int ypix,
Miller Puckette's avatar
Miller Puckette committed
572
573
574
575
576
    int shift, int alt, int dbl, int doit)
{
    int hit = 0;
    t_canvas *templatecanvas = template_findcanvas(template);
    t_gobj *y;
577
    t_atom at[2];
578
579
    t_float basex = template_getfloat(template, gensym("x"), data, 0);
    t_float basey = template_getfloat(template, gensym("y"), data, 0);
580
	//fprintf(stderr,"=================scalar_doclick %f %f %f %f %d\n", basex, basey, xloc, yloc, doit);
Ivica Bukvic's avatar
Ivica Bukvic committed
581
582
583
584
585

	SETFLOAT(at, basex + xloc);
	SETFLOAT(at+1, basey + yloc);
    if (doit) {
		//fprintf(stderr,"	doit\n");
586
587
        template_notifyforscalar(template, owner, 
            sc, gensym("click"), 2, at);
Ivica Bukvic's avatar
Ivica Bukvic committed
588
589
590
591
592
593
594
595
596
	}

	// if we are nested ignore xloc and yloc, otherwise nested objects get their hitbox miscalculated
	if (xloc != 0.0 || yloc != 0.0) {
		//fprintf(stderr,"ignoring\n");
		basex = 0.0;
		basey = 0.0;
	}

Miller Puckette's avatar
Miller Puckette committed
597
598
    for (y = templatecanvas->gl_list; y; y = y->g_next)
    {
Ivica Bukvic's avatar
Ivica Bukvic committed
599
		//fprintf(stderr,"looking for template... %f %f %f %f %lx %lx\n", basex, basey, xloc, yloc, (t_int)owner, (t_int)data);
Miller Puckette's avatar
Miller Puckette committed
600
601
        t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
        if (!wb) continue;
Ivica Bukvic's avatar
Ivica Bukvic committed
602
603
604
605
606
607
	    if (hit = (*wb->w_parentclickfn)(y, owner,
	        data, template, sc, ap, basex + xloc, basey + yloc,
	        xpix, ypix, shift, alt, dbl, doit)) {
				//fprintf(stderr,"	...got it %f %f\n", basex + xloc, basey + yloc);
	            return (hit);
		}
Miller Puckette's avatar
Miller Puckette committed
608
609
610
611
612
613
614
    }
    return (0);
}

static int scalar_click(t_gobj *z, struct _glist *owner,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
Ivica Bukvic's avatar
Ivica Bukvic committed
615
	//fprintf(stderr,"scalar_click %d %d\n", xpix, ypix);
Miller Puckette's avatar
Miller Puckette committed
616
617
    t_scalar *x = (t_scalar *)z;
    t_template *template = template_findbyname(x->sc_template);
618

619
620
621
622
623
624
625
626
627
628
    /* hack for enter/leave notifications. The second part
       of the conditional is to catch edge cases where the
       pointer enters a scalar in one window then moves to
       an overlapping window before leaving the first one.

       There is an edge case with this hack-- if the mouse
       is inside a scalar in one window and then moves to
    */
    if (sc_isentered == 0 ||
        (sc_entered && sc_entered != x))
629
630
    {
        t_atom at[1];
631
632
        if (sc_entered && sc_entered !=x)
            scalar_leave(sc_entered, owner, template, 1);
633
634
635
636
637
638
        template_notifyforscalar(template, owner, x, gensym("enter"), 1, at);
        sc_isentered = 1;
        sc_entered = x;
    }
    else sc_isentered = 1;
    
Miller Puckette's avatar
Miller Puckette committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
    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();
    t_atom a, *argv;
    int i, argc;
    canvas_writescalar(x->sc_template, x->sc_vec, b2, 0);
    binbuf_addv(b, "ss", &s__X, gensym("scalar"));
    binbuf_addbinbuf(b, b2);
    binbuf_addsemi(b);
    binbuf_free(b2);
}

static void scalar_properties(t_gobj *z, struct _glist *owner)
{
    t_scalar *x = (t_scalar *)z;
    char *buf, buf2[80];
    int bufsize;
    t_binbuf *b;
    glist_noselect(owner);
    glist_select(owner, z);
    b = glist_writetobinbuf(owner, 0);
    binbuf_gettext(b, &buf, &bufsize);
    binbuf_free(b);
    buf = t_resizebytes(buf, bufsize, bufsize+1);
    buf[bufsize] = 0;
    sprintf(buf2, "pdtk_data_dialog %%s {");
    gfxstub_new((t_pd *)owner, x, buf2);
    sys_gui(buf);
    sys_gui("}\n");
    t_freebytes(buf, bufsize+1);
}

static t_widgetbehavior scalar_widgetbehavior =
{
    scalar_getrect,
    scalar_displace,
    scalar_select,
    scalar_activate,
    scalar_delete,
    scalar_vis,
    scalar_click,
688
	scalar_displace_withtag,
Miller Puckette's avatar
Miller Puckette committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
};

static void scalar_free(t_scalar *x)
{
    int i;
    t_dataslot *datatypes, *dt;
    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);
    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);
    class_setwidget(scalar_class, &scalar_widgetbehavior);
    class_setsavefn(scalar_class, scalar_save);
    class_setpropertiesfn(scalar_class, scalar_properties);
}