g_graph.c 53 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
/* Copyright (c) 1997-2001 Miller Puckette and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* This file deals with the behavior of glists as either "text objects" or
"graphs" inside another glist.  LATER move the inlet/outlet code of g_canvas.c 
to this file... */

#include <stdlib.h>
#include "m_pd.h"
11
#include "m_imp.h"
Miller Puckette's avatar
Miller Puckette committed
12
#include "g_canvas.h"
13
#include "g_all_guis.h" /* for canvas handle freeing */
Miller Puckette's avatar
Miller Puckette committed
14
15
16
17
#include "s_stuff.h"    /* for sys_hostfontsize */
#include <stdio.h>
#include <string.h>

18
19
20
extern int array_joc;
int garray_joc(t_garray *x);

Miller Puckette's avatar
Miller Puckette committed
21
22
23
/* ---------------------- forward definitions ----------------- */

static void graph_vis(t_gobj *gr, t_glist *unused_glist, int vis);
24
void graph_graphrect(t_gobj *z, t_glist *glist,
Miller Puckette's avatar
Miller Puckette committed
25
26
27
    int *xp1, int *yp1, int *xp2, int *yp2);
static void graph_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2);
28
29
void graph_checkgop_rect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2);
Miller Puckette's avatar
Miller Puckette committed
30

31
32
extern t_template *template_findbydrawcommand(t_gobj *g);

33
extern int do_not_redraw;
34
int gop_redraw = 0;
35

Miller Puckette's avatar
Miller Puckette committed
36
37
38
39
/* -------------------- maintaining the list -------------------- */

void canvas_drawredrect(t_canvas *x, int doit);

40
int canvas_isgroup(t_canvas *x)
41
{
42
43
44
45
46
47
48
49
    //t_binbuf *b = x->gl_obj.te_binbuf;
    //if (!b)
    //{
    //    bug("canvas_isgroup");
    //    return 0;
    //}
    //t_atom *argv = binbuf_getvec(x->gl_obj.te_binbuf);
    //if (argv[0].a_type == A_SYMBOL &&
50
    //    argv[0].a_w.w_symbol == gensym("g"))
51
52
53
    //    return 1;
    //else
    //    return 0;
54
55
56
57
58
59
60
61
62
    if (x->gl_svg)
        return 1;
    else
        return 0;
}

extern t_template *canvas_findtemplate(t_canvas *c);
extern t_canvas *canvas_templatecanvas_forgroup(t_canvas *c);

Miller Puckette's avatar
Miller Puckette committed
63
64
void glist_add(t_glist *x, t_gobj *y)
{
65
66
    //fprintf(stderr,"glist_add %lx %d\n", (t_int)x, (x->gl_editor ? 1 : 0));    
    t_object *ob;
Miller Puckette's avatar
Miller Puckette committed
67
    y->g_next = 0;
68
    int index = 0;
69

Miller Puckette's avatar
Miller Puckette committed
70
71
72
73
    if (!x->gl_list) x->gl_list = y;
    else
    {
        t_gobj *y2;
74
        for (y2 = x->gl_list; y2->g_next; y2 = y2->g_next)
75
            index++;
Miller Puckette's avatar
Miller Puckette committed
76
77
        y2->g_next = y;
    }
78
79
    if (x->gl_editor && (ob = pd_checkobject(&y->g_pd)))
    {
Miller Puckette's avatar
Miller Puckette committed
80
        rtext_new(x, ob);
81
82
83
84
85
86
        //let's now set up create undo
        //glist_select(x, y);
        //canvas_setundo(x, canvas_undo_create,
        //    canvas_undo_set_create(x, index), "create");
        //glist_noselect(x);
    }
Miller Puckette's avatar
Miller Puckette committed
87
88
89
90
91
92
93
94
95
    if (x->gl_editor && x->gl_isgraph && !x->gl_goprect
        && pd_checkobject(&y->g_pd))
    {
        x->gl_goprect = 1;
        canvas_drawredrect(x, 1);
    }
    if (glist_isvisible(x))
        gobj_vis(y, x, 1);
    if (class_isdrawcommand(y->g_pd)) 
96
97
98
99
    {
        t_template *tmpl = template_findbydrawcommand(y);
        canvas_redrawallfortemplate(tmpl, 0);
    }
100
101
102
103
    if (pd_class(&y->g_pd) == canvas_class &&
        canvas_isgroup((t_canvas *)y))
    {
        t_canvas *templatecanvas =
104
105
106
            canvas_templatecanvas_forgroup((t_canvas *)y);
        t_template *tmpl = canvas_findtemplate(templatecanvas);
        canvas_redrawallfortemplate(tmpl, 0);
107
    }
Miller Puckette's avatar
Miller Puckette committed
108
109
110
111
112
113
114
115
116
117
118
119
}

    /* this is to protect against a hairy problem in which deleting
    a sub-canvas might delete an inlet on a box, after the box had
    been invisible-ized, so that we have to protect against redrawing it! */
int canvas_setdeleting(t_canvas *x, int flag)
{
    int ret = x->gl_isdeleting;
    x->gl_isdeleting = flag;
    return (ret);
}

120
121
    /* check if canvas has an array and return 1, otherwise return 0
    this is used to prevent creation of new objects in an array window */
122
123
int canvas_hasarray(t_canvas *x)
{
124
125
    t_gobj *g = x->gl_list;
    int hasarray = 0;
126
127
    while (g)
    {
128
129
130
131
132
133
        if (pd_class(&g->g_pd) == garray_class) hasarray = 1;
        g = g->g_next;
    }
    return(hasarray);
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
134
135
136
/* JMZ: emit a closebang message */
void canvas_closebang(t_canvas *x);

Miller Puckette's avatar
Miller Puckette committed
137
138
139
    /* delete an object from a glist and free it */
void glist_delete(t_glist *x, t_gobj *y)
{
140
    //fprintf(stderr,"glist_delete y=%lx x=%lx glist_getcanvas=%lx\n", y, x, glist_getcanvas(x));
141
142
143
144
145
    if (x->gl_list)
    {
        //fprintf(stderr,"glist_delete YES\n");
        t_gobj *g;
        t_object *ob;
146
        t_template *tmpl = NULL;
147
148
149
150
151
152
153
154
155
156
157
        t_gotfn chkdsp = zgetfn(&y->g_pd, gensym("dsp"));
        t_canvas *canvas = glist_getcanvas(x);
        int drawcommand = class_isdrawcommand(y->g_pd);
        int wasdeleting;
        t_rtext *rt = NULL;
        int late_rtext_free = 0;

        if (pd_class(&y->g_pd) == canvas_class)
        {
          /* JMZ: send a closebang to the canvas */
          canvas_closebang((t_canvas *)y);
158
159
160
161
162
163
164
          /* and this little hack so drawing commands can tell
             if a [group] is deleting them (and thus suppress
             their own redraws) */
          ((t_canvas *)y)->gl_unloading = 1;
          /* if we are a group, let's call ourselves a drawcommand */
          if (((t_canvas *)y)->gl_svg)
              drawcommand = 1;
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
        }
     
        wasdeleting = canvas_setdeleting(canvas, 1);
        if (x->gl_editor)
        {
            if (x->gl_editor->e_grab == y) x->gl_editor->e_grab = 0;
            if (glist_isselected(x, y)) glist_deselect(x, y);

                /* HACK -- we had phantom outlets not getting erased on the
                screen because the canvas_setdeleting() mechanism is too
                crude.  LATER carefully set up rules for when the rtexts
                should exist, so that they stay around until all the
                steps of becoming invisible are done.  In the meantime, just
                zap the inlets and outlets here... */
            if (pd_class(&y->g_pd) == canvas_class)
            {
181
                if (glist_isvisible(x))
182
                {
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
                    t_glist *gl = (t_glist *)y;
                    if (gl->gl_isgraph)
                    {
                        char tag[80];
                        //sprintf(tag, "graph%lx", (t_int)gl);
                        //t_glist *yy = (t_glist *)y;
                        sprintf(tag, "%s",
                            rtext_gettag(glist_findrtext(x, &gl->gl_obj)));
                        glist_eraseiofor(x, &gl->gl_obj, tag);
                        text_eraseborder(&gl->gl_obj, x,
                            rtext_gettag(glist_findrtext(x, &gl->gl_obj)));
                    }
                    else
                    {
                        text_eraseborder(&gl->gl_obj, x,
                            rtext_gettag(glist_findrtext(x, &gl->gl_obj)));
                    }
200
201
202
203
                }
            }
        }
            /* if we're a drawing command, erase all scalars that
204
                       belong to our template, before deleting
205
206
               it; we'll redraw them once it's deleted below. */
        if (drawcommand)
207
208
        {
            tmpl = template_findbydrawcommand(y);
209
            if (!(canvas_isgroup(canvas) && canvas->gl_unloading))
210
211
212
            {
                canvas_redrawallfortemplate(tmpl, 2);
            }
213
        }
214
        if (glist_isvisible(canvas))
215
216
        {
            //fprintf(stderr,"...deleting %lx %lx\n", x, glist_getcanvas(x));
217
            gobj_vis(y, x, 0);
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
        if (x->gl_editor && (ob = pd_checkobject(&y->g_pd)))
        {
            //rtext_new(x, ob);
            rt = glist_findrtext(x, ob);
            if (rt)
                late_rtext_free = 1;
        }
        if (x->gl_list == y)
        {
            if (y->g_next)
                x->gl_list = y->g_next;
            else
                x->gl_list = NULL;
        }
        else for (g = x->gl_list; g; g = g->g_next)
        {
            if (g->g_next == y)
            {
                if (y->g_next)
                    g->g_next = y->g_next;
                else g->g_next = NULL;
                break;
            }
        }
        gobj_delete(y, x);
        pd_free(&y->g_pd);
        if (chkdsp) canvas_update_dsp();
        if (drawcommand)
247
        {
248
            if (tmpl != NULL && !(canvas_isgroup(canvas) && canvas->gl_unloading))
249
            {
250
                canvas_redrawallfortemplate(tmpl, 1);
251
            }
252
        }
253
254
255
256
257
258
259
260
        canvas_setdeleting(canvas, wasdeleting);
        x->gl_valid = ++glist_valid;
        if (late_rtext_free)
        {
            //fprintf(stderr,"glist_delete late_rtext_free\n");
            rtext_free(rt);
        }
    }
Miller Puckette's avatar
Miller Puckette committed
261
262
263
264
265
}

    /* remove every object from a glist.  Experimental. */
void glist_clear(t_glist *x)
{
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
266
    t_gobj *y;
Miller Puckette's avatar
Miller Puckette committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
    int dspstate = 0, suspended = 0;
    t_symbol *dspsym = gensym("dsp");
    while (y = x->gl_list)
    {
            /* to avoid unnecessary DSP resorting, we suspend DSP
            only if we hit a patchable object. */
        if (!suspended && pd_checkobject(&y->g_pd) && zgetfn(&y->g_pd, dspsym))
        {
            dspstate = canvas_suspend_dsp();
            suspended = 1;
        }
            /* here's the real deletion. */
        glist_delete(x, y);
    }
    if (suspended)
        canvas_resume_dsp(dspstate);
}

void glist_retext(t_glist *glist, t_text *y)
{
        /* check that we have built rtexts yet.  LATER need a better test. */
    if (glist->gl_editor && glist->gl_editor->e_rtext)
    {
        t_rtext *rt = glist_findrtext(glist, y);
        if (rt)
            rtext_retext(rt);
    }
}

void glist_grab(t_glist *x, t_gobj *y, t_glistmotionfn motionfn,
    t_glistkeyfn keyfn, int xpos, int ypos)
{
299
    //fprintf(stderr,"glist_grab\n");
Miller Puckette's avatar
Miller Puckette committed
300
301
302
303
304
305
306
307
308
309
310
311
312
    t_glist *x2 = glist_getcanvas(x);
    if (motionfn)
        x2->gl_editor->e_onmotion = MA_PASSOUT;
    else x2->gl_editor->e_onmotion = 0;
    x2->gl_editor->e_grab = y;
    x2->gl_editor->e_motionfn = motionfn;
    x2->gl_editor->e_keyfn = keyfn;
    x2->gl_editor->e_xwas = xpos;
    x2->gl_editor->e_ywas = ypos;
}

t_canvas *glist_getcanvas(t_glist *x)
{
313
    //fprintf(stderr,"glist_getcanvas\n");
314
    while (x->gl_owner && !x->gl_havewindow && x->gl_isgraph)
315
    {
316
317
318
            //fprintf(stderr,"x=%lx x->gl_owner=%d x->gl_havewindow=%d "
            //               "x->gl_isgraph=%d gobj_shouldvis=%d\n", 
            //    x, (x->gl_owner ? 1:0), x->gl_havewindow, x->gl_isgraph,
319
            //    gobj_shouldvis(&x->gl_gobj, x->gl_owner));
320
            x = x->gl_owner;
321
322
            //fprintf(stderr,"+\n");
    }
Miller Puckette's avatar
Miller Puckette committed
323
324
325
    return((t_canvas *)x);
}

326
static t_float gobj_getxforsort(t_gobj *g)
Miller Puckette's avatar
Miller Puckette committed
327
328
329
{
    if (pd_class(&g->g_pd) == scalar_class)
    {
330
        t_float x1, y1;
Miller Puckette's avatar
Miller Puckette committed
331
332
333
334
335
336
337
338
339
        scalar_getbasexy((t_scalar *)g, &x1, &y1);
        return(x1);
    }
    else return (0);
}

static t_gobj *glist_merge(t_glist *x, t_gobj *g1, t_gobj *g2)
{
    t_gobj *g = 0, *g9 = 0;
340
    t_float f1 = 0, f2 = 0;
Miller Puckette's avatar
Miller Puckette committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
    if (g1)
        f1 = gobj_getxforsort(g1);
    if (g2)
        f2 = gobj_getxforsort(g2);
    while (1)
    {
        if (g1)
        {
            if (g2)
            {
                if (f1 <= f2)
                    goto put1;
                else goto put2;
            }
            else goto put1;     
        }
        else if (g2)
            goto put2;
        else break;
    put1:
        if (g9)
            g9->g_next = g1, g9 = g1;
        else g9 = g = g1;
        if (g1 = g1->g_next)
            f1 = gobj_getxforsort(g1);
        g9->g_next = 0;
        continue;
    put2:
        if (g9)
            g9->g_next = g2, g9 = g2;
        else g9 = g = g2;
        if (g2 = g2->g_next)
            f2 = gobj_getxforsort(g2);
        g9->g_next = 0;
        continue;
    }
    return (g);
}

static t_gobj *glist_dosort(t_glist *x,
    t_gobj *g, int nitems)
{
    if (nitems < 2)
        return (g);
    else
    {
        int n1 = nitems/2, n2 = nitems - n1, i;
        t_gobj *g2, *g3;
        for (g2 = g, i = n1-1; i--; g2 = g2->g_next)
            ;
        g3 = g2->g_next;
        g2->g_next = 0;
        g = glist_dosort(x, g, n1);
        g3 = glist_dosort(x, g3, n2);
        return (glist_merge(x, g, g3));
    }
}

void glist_sort(t_glist *x)
{
    int nitems = 0, foo = 0;
402
    t_float lastx = -1e37;
Miller Puckette's avatar
Miller Puckette committed
403
404
405
    t_gobj *g;
    for (g = x->gl_list; g; g = g->g_next)
    {
406
        t_float x1 = gobj_getxforsort(g);
Miller Puckette's avatar
Miller Puckette committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
        if (x1 < lastx)
            foo = 1;
        lastx = x1;
        nitems++;
    }
    if (foo)
        x->gl_list = glist_dosort(x, x->gl_list, nitems);
}

/* --------------- inlets and outlets  ----------- */


t_inlet *canvas_addinlet(t_canvas *x, t_pd *who, t_symbol *s)
{
421
    //fprintf(stderr,"canvas_addinlet %d %lx %d\n", x->gl_loading, x->gl_owner, glist_isvisible(x->gl_owner));
Miller Puckette's avatar
Miller Puckette committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    t_inlet *ip = inlet_new(&x->gl_obj, who, s, 0);
    if (!x->gl_loading && x->gl_owner && glist_isvisible(x->gl_owner))
    {
        gobj_vis(&x->gl_gobj, x->gl_owner, 0);
        gobj_vis(&x->gl_gobj, x->gl_owner, 1);
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
    }
    if (!x->gl_loading) canvas_resortinlets(x);
    return (ip);
}

void canvas_rminlet(t_canvas *x, t_inlet *ip)
{
    t_canvas *owner = x->gl_owner;
    int redraw = (owner && glist_isvisible(owner) && (!owner->gl_isdeleting)
        && glist_istoplevel(owner));
    
    if (owner) canvas_deletelinesforio(owner, &x->gl_obj, ip, 0);
    if (redraw)
        gobj_vis(&x->gl_gobj, x->gl_owner, 0);
    inlet_free(ip);
    if (redraw)
    {
        gobj_vis(&x->gl_gobj, x->gl_owner, 1);
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
    }
}

extern t_inlet *vinlet_getit(t_pd *x);
extern void obj_moveinletfirst(t_object *x, t_inlet *i);

void canvas_resortinlets(t_canvas *x)
{
    int ninlets = 0, i, j, xmax;
    t_gobj *y, **vec, **vp, **maxp;
    
    for (ninlets = 0, y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == vinlet_class) ninlets++;

461
    if (ninlets < 2  && !(canvas_isgroup(x))) return;
Miller Puckette's avatar
Miller Puckette committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    vec = (t_gobj **)getbytes(ninlets * sizeof(*vec));
    
    for (y = x->gl_list, vp = vec; y; y = y->g_next)
        if (pd_class(&y->g_pd) == vinlet_class) *vp++ = y;
    
    for (i = ninlets; i--;)
    {
        t_inlet *ip;
        for (vp = vec, xmax = -0x7fffffff, maxp = 0, j = ninlets;
            j--; vp++)
        {
            int x1, y1, x2, y2;
            t_gobj *g = *vp;
            if (!g) continue;
            gobj_getrect(g, x, &x1, &y1, &x2, &y2);
            if (x1 > xmax) xmax = x1, maxp = vp;
        }
        if (!maxp) break;
        y = *maxp;
        *maxp = 0;
        ip = vinlet_getit(&y->g_pd);
        
        obj_moveinletfirst(&x->gl_obj, ip);
    }
    freebytes(vec, ninlets * sizeof(*vec));
487
488
489
490
    if (x->gl_owner &&
        glist_isvisible(x->gl_owner) && glist_isvisible(x) &&
        !x->gl_owner->gl_loading && !x->gl_loading)
    {
Miller Puckette's avatar
Miller Puckette committed
491
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
492
493
494
495
496
497
498
499
500
        //fprintf(stderr,"good place to fix redrawing of inlets "
        //               ".x%lx owner=.x%lx %d (parent)%d\n",
        //    x, x->gl_owner, x->gl_loading, x->gl_owner->gl_loading);

        /*
        t_object *ob = pd_checkobject(&y->g_pd);
        t_rtext *rt = glist_findrtext(x->gl_owner, (t_text *)&ob->ob_g);
        for (i = 0; i < ninlets; i++)
        {
501
502
503
504
505
506
            //sys_vgui(".x%x.c itemconfigure %si%d -fill %s -width 1\n",
            //    x, rtext_gettag(rt), i, 
            //    (obj_issignalinlet(ob, i) ?
            //        "$signal_nlet" : "$pd_colors_control_nlet)"));
            sprintf(xlet_tag, "%si%d", rtext_gettag(rt), i);
            char xlet_tag[MAXPDSTRING];
507
            gui_vmess("gui_gobj_configure_io", "xsiii",
508
                x,
509
510
511
512
                xlet_tag,
                0,
                obj_issignalinlet(ob, i),
                1);
513
514
515
516
517
518
519
        }
        */

        //glist_redraw(x);
        graph_vis(&x->gl_gobj, x->gl_owner, 0); 
        graph_vis(&x->gl_gobj, x->gl_owner, 1);
    }
Miller Puckette's avatar
Miller Puckette committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
}

t_outlet *canvas_addoutlet(t_canvas *x, t_pd *who, t_symbol *s)
{
    t_outlet *op = outlet_new(&x->gl_obj, s);
    if (!x->gl_loading && x->gl_owner && glist_isvisible(x->gl_owner))
    {
        gobj_vis(&x->gl_gobj, x->gl_owner, 0);
        gobj_vis(&x->gl_gobj, x->gl_owner, 1);
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
    }
    if (!x->gl_loading) canvas_resortoutlets(x);
    return (op);
}

void canvas_rmoutlet(t_canvas *x, t_outlet *op)
{
    t_canvas *owner = x->gl_owner;
    int redraw = (owner && glist_isvisible(owner) && (!owner->gl_isdeleting)
        && glist_istoplevel(owner));
    
    if (owner) canvas_deletelinesforio(owner, &x->gl_obj, 0, op);
    if (redraw)
        gobj_vis(&x->gl_gobj, x->gl_owner, 0);

    outlet_free(op);
    if (redraw)
    {
        gobj_vis(&x->gl_gobj, x->gl_owner, 1);
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
    }
}

extern t_outlet *voutlet_getit(t_pd *x);
extern void obj_moveoutletfirst(t_object *x, t_outlet *i);

void canvas_resortoutlets(t_canvas *x)
{
    int noutlets = 0, i, j, xmax;
    t_gobj *y, **vec, **vp, **maxp;
    
    for (noutlets = 0, y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == voutlet_class) noutlets++;

564
    if (noutlets < 2 && !(canvas_isgroup(x))) return;
Miller Puckette's avatar
Miller Puckette committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
    
    vec = (t_gobj **)getbytes(noutlets * sizeof(*vec));
    
    for (y = x->gl_list, vp = vec; y; y = y->g_next)
        if (pd_class(&y->g_pd) == voutlet_class) *vp++ = y;
    
    for (i = noutlets; i--;)
    {
        t_outlet *ip;
        for (vp = vec, xmax = -0x7fffffff, maxp = 0, j = noutlets;
            j--; vp++)
        {
            int x1, y1, x2, y2;
            t_gobj *g = *vp;
            if (!g) continue;
            gobj_getrect(g, x, &x1, &y1, &x2, &y2);
            if (x1 > xmax) xmax = x1, maxp = vp;
        }
        if (!maxp) break;
        y = *maxp;
        *maxp = 0;
        ip = voutlet_getit(&y->g_pd);
        
        obj_moveoutletfirst(&x->gl_obj, ip);
    }
    freebytes(vec, noutlets * sizeof(*vec));
591
592
593
594
    if (x->gl_owner &&
        glist_isvisible(x->gl_owner) && glist_isvisible(x) &&
        !x->gl_owner->gl_loading && !x->gl_loading)
    {
Miller Puckette's avatar
Miller Puckette committed
595
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
596
597
598
        //fprintf(stderr,"good place to fix redrawing of outlets\n");
        //fprintf(stderr,"found it\n");
        //glist_redraw(x);
599
600
        graph_vis(&x->gl_gobj, x->gl_owner, 0); 
        graph_vis(&x->gl_gobj, x->gl_owner, 1);
601
    }
Miller Puckette's avatar
Miller Puckette committed
602
603
604
605
606
607
608
609
}

/* ----------calculating coordinates and controlling appearance --------- */


static void graph_bounds(t_glist *x, t_floatarg x1, t_floatarg y1,
    t_floatarg x2, t_floatarg y2)
{
610
    if (x1==x2 || y1==y2) {
Miller Puckette's avatar
Miller Puckette committed
611
612
613
614
        error("graph: empty bounds rectangle");
        x1 = y1 = 0;
        x2 = y2 = 1;
    }
615
    if (x->gl_x1!=x1 || x->gl_y1!=y1 || x->gl_x2!=x2 || x->gl_y2!=y2) {
616
        //printf("%f %f %f %f %f %f %f %f\n",x->gl_x1,x1,x->gl_y1,y1,x->gl_x2,x2,x->gl_y2,y2);
617
618
619
620
        x->gl_x1 = x1;
        x->gl_x2 = x2;
        x->gl_y1 = y1;
        x->gl_y2 = y2;
621
622
        if (!do_not_redraw)
            glist_redraw(x);
623
    }
Miller Puckette's avatar
Miller Puckette committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
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
}

static void graph_xticks(t_glist *x,
    t_floatarg point, t_floatarg inc, t_floatarg f)
{
    x->gl_xtick.k_point = point;
    x->gl_xtick.k_inc = inc;
    x->gl_xtick.k_lperb = f;
    glist_redraw(x);
}

static void graph_yticks(t_glist *x,
    t_floatarg point, t_floatarg inc, t_floatarg f)
{
    x->gl_ytick.k_point = point;
    x->gl_ytick.k_inc = inc;
    x->gl_ytick.k_lperb = f;
    glist_redraw(x);
}

static void graph_xlabel(t_glist *x, t_symbol *s, int argc, t_atom *argv)
{
    int i;
    if (argc < 1) error("graph_xlabel: no y value given");
    else
    {
        x->gl_xlabely = atom_getfloat(argv);
        argv++; argc--;
        x->gl_xlabel = (t_symbol **)t_resizebytes(x->gl_xlabel, 
            x->gl_nxlabels * sizeof (t_symbol *), argc * sizeof (t_symbol *));
        x->gl_nxlabels = argc;
        for (i = 0; i < argc; i++) x->gl_xlabel[i] = atom_gensym(&argv[i]);
    }
    glist_redraw(x);
}
    
static void graph_ylabel(t_glist *x, t_symbol *s, int argc, t_atom *argv)
{
    int i;
    if (argc < 1) error("graph_ylabel: no x value given");
    else
    {
        x->gl_ylabelx = atom_getfloat(argv);
        argv++; argc--;
        x->gl_ylabel = (t_symbol **)t_resizebytes(x->gl_ylabel, 
            x->gl_nylabels * sizeof (t_symbol *), argc * sizeof (t_symbol *));
        x->gl_nylabels = argc;
        for (i = 0; i < argc; i++) x->gl_ylabel[i] = atom_gensym(&argv[i]);
    }
    glist_redraw(x);
}

/****** routines to convert pixels to X or Y value and vice versa ******/

    /* convert an x pixel value to an x coordinate value */
679
t_float glist_pixelstox(t_glist *x, t_float xpix)
Miller Puckette's avatar
Miller Puckette committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
{
        /* if we appear as a text box on parent, our range in our
        coordinates (x1, etc.) specifies the coordinate range
        of a one-pixel square at top left of the window. */
    if (!x->gl_isgraph)
        return (x->gl_x1 + (x->gl_x2 - x->gl_x1) * xpix);

        /* if we're a graph when shown on parent, but own our own
        window right now, our range in our coordinates (x1, etc.) is spread
        over the visible window size, given by screenx1, etc. */  
    else if (x->gl_isgraph && x->gl_havewindow)
        return (x->gl_x1 + (x->gl_x2 - x->gl_x1) * 
            (xpix) / (x->gl_screenx2 - x->gl_screenx1));

        /* otherwise, we appear in a graph within a parent glist,
         so get our screen rectangle on parent and transform. */
    else 
    {
        int x1, y1, x2, y2;
        if (!x->gl_owner)
            bug("glist_pixelstox");         
        graph_graphrect(&x->gl_gobj, x->gl_owner, &x1, &y1, &x2, &y2);
        return (x->gl_x1 + (x->gl_x2 - x->gl_x1) * 
            (xpix - x1) / (x2 - x1));
    }
}

707
t_float glist_pixelstoy(t_glist *x, t_float ypix)
Miller Puckette's avatar
Miller Puckette committed
708
709
710
711
712
713
714
715
716
717
{
    if (!x->gl_isgraph)
        return (x->gl_y1 + (x->gl_y2 - x->gl_y1) * ypix);
    else if (x->gl_isgraph && x->gl_havewindow)
        return (x->gl_y1 + (x->gl_y2 - x->gl_y1) * 
                (ypix) / (x->gl_screeny2 - x->gl_screeny1));
    else 
    {
        int x1, y1, x2, y2;
        if (!x->gl_owner)
718
            bug("glist_pixelstoy");
Miller Puckette's avatar
Miller Puckette committed
719
720
721
722
723
724
725
        graph_graphrect(&x->gl_gobj, x->gl_owner, &x1, &y1, &x2, &y2);
        return (x->gl_y1 + (x->gl_y2 - x->gl_y1) * 
            (ypix - y1) / (y2 - y1));
    }
}

    /* convert an x coordinate value to an x pixel location in window */
726
t_float glist_xtopixels(t_glist *x, t_float xval)
Miller Puckette's avatar
Miller Puckette committed
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
{
    if (!x->gl_isgraph)
        return ((xval - x->gl_x1) / (x->gl_x2 - x->gl_x1));
    else if (x->gl_isgraph && x->gl_havewindow)
        return (x->gl_screenx2 - x->gl_screenx1) * 
            (xval - x->gl_x1) / (x->gl_x2 - x->gl_x1);
    else
    {
        int x1, y1, x2, y2;
        if (!x->gl_owner)
            bug("glist_pixelstox");
        graph_graphrect(&x->gl_gobj, x->gl_owner, &x1, &y1, &x2, &y2);
        return (x1 + (x2 - x1) * (xval - x->gl_x1) / (x->gl_x2 - x->gl_x1));
    }
}

743
t_float glist_ytopixels(t_glist *x, t_float yval)
Miller Puckette's avatar
Miller Puckette committed
744
745
746
747
748
749
750
751
752
753
{
    if (!x->gl_isgraph)
        return ((yval - x->gl_y1) / (x->gl_y2 - x->gl_y1));
    else if (x->gl_isgraph && x->gl_havewindow)
        return (x->gl_screeny2 - x->gl_screeny1) * 
                (yval - x->gl_y1) / (x->gl_y2 - x->gl_y1);
    else 
    {
        int x1, y1, x2, y2;
        if (!x->gl_owner)
754
            bug("glist_pixelstoy");
Miller Puckette's avatar
Miller Puckette committed
755
756
757
758
759
760
761
762
        graph_graphrect(&x->gl_gobj, x->gl_owner, &x1, &y1, &x2, &y2);
        return (y1 + (y2 - y1) * (yval - x->gl_y1) / (x->gl_y2 - x->gl_y1));
    }
}

    /* convert an X screen distance to an X coordinate increment.
      This is terribly inefficient;
      but probably not a big enough CPU hog to warrant optimizing. */
763
t_float glist_dpixtodx(t_glist *x, t_float dxpix)
Miller Puckette's avatar
Miller Puckette committed
764
765
766
767
{ 
    return (dxpix * (glist_pixelstox(x, 1) - glist_pixelstox(x, 0)));
}

768
t_float glist_dpixtody(t_glist *x, t_float dypix)
Miller Puckette's avatar
Miller Puckette committed
769
770
771
772
773
774
775
776
777
778
779
780
{
    return (dypix * (glist_pixelstoy(x, 1) - glist_pixelstoy(x, 0)));
}

    /* get the window location in pixels of a "text" object.  The
    object's x and y positions are in pixels when the glist they're
    in is toplevel.  Otherwise, if it's a new-style graph-on-parent
    (so gl_goprect is set) we use the offset into the framing subrectangle
    as an offset into the parent rectangle.  Finally, it might be an old,
    proportional-style GOP.  In this case we do a coordinate transformation. */
int text_xpix(t_text *x, t_glist *glist)
{
781
    int xpix = 0; 
Miller Puckette's avatar
Miller Puckette committed
782
    if (glist->gl_havewindow || !glist->gl_isgraph)
783
        xpix = x->te_xpix; 
Miller Puckette's avatar
Miller Puckette committed
784
    else if (glist->gl_goprect)
785
786
787
        xpix = glist_xtopixels(glist, glist->gl_x1) +
            x->te_xpix - glist->gl_xmargin;
    else xpix = (glist_xtopixels(glist, 
Miller Puckette's avatar
Miller Puckette committed
788
789
            glist->gl_x1 + (glist->gl_x2 - glist->gl_x1) * 
                x->te_xpix / (glist->gl_screenx2 - glist->gl_screenx1)));
790
791
792
    if (x->te_iemgui == 1)
        xpix += ((t_iemgui *)x)->legacy_x*sys_legacy;
    return(xpix);
Miller Puckette's avatar
Miller Puckette committed
793
794
795
796
}

int text_ypix(t_text *x, t_glist *glist)
{
797
    int ypix = 0; 
Miller Puckette's avatar
Miller Puckette committed
798
    if (glist->gl_havewindow || !glist->gl_isgraph)
799
        ypix = x->te_ypix; 
Miller Puckette's avatar
Miller Puckette committed
800
    else if (glist->gl_goprect)
801
802
803
        ypix = glist_ytopixels(glist, glist->gl_y1) +
            x->te_ypix - glist->gl_ymargin;
    else ypix = (glist_ytopixels(glist, 
Miller Puckette's avatar
Miller Puckette committed
804
805
            glist->gl_y1 + (glist->gl_y2 - glist->gl_y1) * 
                x->te_ypix / (glist->gl_screeny2 - glist->gl_screeny1)));
806
807
808
    if (x->te_iemgui == 1)
        ypix += ((t_iemgui *)x)->legacy_y*sys_legacy;
    return(ypix);
Miller Puckette's avatar
Miller Puckette committed
809
810
}

811
812
extern void canvas_updateconnection(t_canvas *x, int lx1, int ly1, int lx2, int ly2, t_int tag);

Miller Puckette's avatar
Miller Puckette committed
813
814
815
816
817
    /* redraw all the items in a glist.  We construe this to mean
    redrawing in its own window and on parent, as needed in each case.
    This is too conservative -- for instance, when you draw an "open"
    rectangle on the parent, you shouldn't have to redraw the window!  */
void glist_redraw(t_glist *x)
818
{
Miller Puckette's avatar
Miller Puckette committed
819
820
821
    if (glist_isvisible(x))
    {
            /* LATER fix the graph_vis() code to handle both cases */
822
        if (glist_istoplevel(x) && x->gl_havewindow)
Miller Puckette's avatar
Miller Puckette committed
823
824
825
826
827
828
829
830
831
832
833
834
        {
            t_gobj *g;
            t_linetraverser t;
            t_outconnect *oc;
            for (g = x->gl_list; g; g = g->g_next)
            {
                gobj_vis(g, x, 0);
                gobj_vis(g, x, 1);
            }
                /* redraw all the lines */
            linetraverser_start(&t, x);
            while (oc = linetraverser_next(&t))
835
836
837
838
                canvas_updateconnection(glist_getcanvas(x), t.tr_lx1, t.tr_ly1, t.tr_lx2, t.tr_ly2, (t_int)oc);
                //sys_vgui(".x%lx.c coords l%lx %d %d %d %d\n",
                //    glist_getcanvas(x), oc,
                //        t.tr_lx1, t.tr_ly1, t.tr_lx2, t.tr_ly2);
Miller Puckette's avatar
Miller Puckette committed
839
840
841
            canvas_drawredrect(x, 0);
            if (x->gl_goprect)
            {
842
                //post("draw it");
843
844
845
846
847
848
849
850
                /* update gop rect size on toplevel in case font has
                changed and we are showing text */
                /*if (!x->gl_hidetext) {
                    int x1, y1, x2, y2;
                    graph_getrect((t_gobj *)x, x, &x1, &y1, &x2, &y2);
                    if (x2-x1 > x->gl_pixwidth) x->gl_pixwidth = x2-x1;
                    if (y2-y1 > x->gl_pixheight) x->gl_pixheight = y2-y1;
                }*/
Miller Puckette's avatar
Miller Puckette committed
851
852
853
                canvas_drawredrect(x, 1);
            }
        }
854
855
856
857
858
    }
    if (x->gl_owner && glist_isvisible(x->gl_owner))
    {
        graph_vis(&x->gl_gobj, x->gl_owner, 0); 
        graph_vis(&x->gl_gobj, x->gl_owner, 1);
Miller Puckette's avatar
Miller Puckette committed
859
860
861
862
863
864
865
866
867
868
869
870
871
    }
}

/* --------------------------- widget behavior  ------------------- */

int garray_getname(t_garray *x, t_symbol **namep);


    /* Note that some code in here would also be useful for drawing
    graph decorations in toplevels... */
static void graph_vis(t_gobj *gr, t_glist *parent_glist, int vis)
{
    t_glist *x = (t_glist *)gr;
872
873
874
875
876
877
878
    //fprintf(stderr,"graph vis canvas=%lx gobj=%lx %d\n",
    //    (t_int)parent_glist, (t_int)gr, vis);
    //fprintf(stderr, "graph_vis gr=.x%lx parent_glist=.x%lx "
    //                "glist_getcanvas(x->gl_owner)=.x%lx vis=%d\n",
    //    (t_int)gr, (t_int)parent_glist,
    //    (t_int)glist_getcanvas(x->gl_owner), vis);  
    char tag[50];
Miller Puckette's avatar
Miller Puckette committed
879
880
881
882
883
884
885
886
887
    t_gobj *g;
    int x1, y1, x2, y2;
        /* ordinary subpatches: just act like a text object */
    if (!x->gl_isgraph)
    {
        text_widgetbehavior.w_visfn(gr, parent_glist, vis);
        return;
    }

888
889
890
891
892
893
894
895
896
897
    // weird exception
    //int exception = 0;
    //t_canvas* tgt = glist_getcanvas(x->gl_owner);
    //if (parent_glist->gl_owner && !parent_glist->gl_mapped &&
    //        parent_glist->gl_owner->gl_mapped) {
    //    tgt = parent_glist;
    //    exception = 1;
    //}
    //fprintf(stderr,"tgt=.x%lx %d\n", (t_int)tgt, exception);

Jonathan Wilkes's avatar
Jonathan Wilkes committed
898
899
900
901
902
903
904
    sprintf(tag, "%s", rtext_gettag(glist_findrtext(parent_glist, &x->gl_obj)));

    if (vis & gobj_shouldvis(gr, parent_glist))
    {
        int xpix, ypix;
        xpix = text_xpix(&x->gl_obj, parent_glist);
        ypix = text_ypix(&x->gl_obj, parent_glist);
905
        gui_vmess("gui_gobj_new", "xssiii",
906
            glist_getcanvas(x->gl_owner),
907
            tag, "graph", xpix, ypix, 1);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
908
909
910
911
        if (canvas_showtext(x))
            rtext_draw(glist_findrtext(parent_glist, &x->gl_obj));
    }

912
    //sprintf(tag, "%s", rtext_gettag(glist_findrtext(parent_glist, &x->gl_obj)));
Jonathan Wilkes's avatar
Jonathan Wilkes committed
913
914
915

    // need the rect to create the gobj, so this should perhaps be above the
    // conditional
Miller Puckette's avatar
Miller Puckette committed
916
    graph_getrect(gr, parent_glist, &x1, &y1, &x2, &y2);
917
    //fprintf(stderr,"%d %d %d %d\n", x1, y1, x2, y2);
918
919
920
921
922
923
    if (sys_legacy == 1)
    {
        //fprintf(stderr,"legacy  gop\n");
        y1 += 1;
        y2 += 1;
    }
Jonathan Wilkes's avatar
Jonathan Wilkes committed
924

Miller Puckette's avatar
Miller Puckette committed
925
926
927
    if (!vis)
        rtext_erase(glist_findrtext(parent_glist, &x->gl_obj));

928
    //sprintf(tag, "graph%lx", (t_int)x);
929
930
931
932
    //fprintf(stderr, "gettag=%s, tag=graph%lx\n",
    //    rtext_gettag(glist_findrtext(parent_glist, &x->gl_obj)),(t_int)x);
    /* if we look like a graph but have been moved to a toplevel,
       just show the bounding rectangle */
Miller Puckette's avatar
Miller Puckette committed
933
934
    if (x->gl_havewindow)
    {
935
936
        if (vis && gobj_shouldvis(gr, parent_glist))
        {
937
            gui_vmess("gui_text_draw_border", "xssiiiii",
938
                glist_getcanvas(x->gl_owner),
Jonathan Wilkes's avatar
Jonathan Wilkes committed
939
                tag,
940
                "none",
Jonathan Wilkes's avatar
Jonathan Wilkes committed
941
                0, x1, y1, x2, y2);
942
            glist_noselect(x->gl_owner);
943
944
            gui_vmess("gui_graph_fill_border", "xsi",
                glist_getcanvas(x->gl_owner),
945
                tag);
946
947
948
        }
        else if (gobj_shouldvis(gr, parent_glist))
        {
949
950
            gui_vmess("gui_gobj_erase", "xs",
                glist_getcanvas(x->gl_owner),
951
                tag);
952
        }
Miller Puckette's avatar
Miller Puckette committed
953
954
955
956
957
958
        return;
    }
        /* otherwise draw (or erase) us as a graph inside another glist. */
    if (vis)
    {
        int i;
959
        t_float f;
Miller Puckette's avatar
Miller Puckette committed
960
961
962
        t_gobj *g;
        t_symbol *arrayname;
            /* draw a rectangle around the graph */
963
964
965
966
        char *ylabelanchor =
            (x->gl_ylabelx > 0.5*(x->gl_x1 + x->gl_x2) ? "w" : "e");
        char *xlabelanchor =
            (x->gl_xlabely > 0.5*(x->gl_y1 + x->gl_y2) ? "s" : "n");
Jonathan Wilkes's avatar
Jonathan Wilkes committed
967
968
        char tagbuf[MAXPDSTRING];
        sprintf(tagbuf, "%sR", tag);
969

970
        gui_vmess("gui_text_draw_border", "xssiiiii",
971
            glist_getcanvas(x->gl_owner),
Jonathan Wilkes's avatar
Jonathan Wilkes committed
972
            tag,
973
            "none",
Jonathan Wilkes's avatar
Jonathan Wilkes committed
974
            0, x1, y1, x2, y2);
975
            /* write garrays' names along the top */
Jonathan Wilkes's avatar
Jonathan Wilkes committed
976
        for (i = 0, g = x->gl_list; g; g = g->g_next, i++)
977
978
979
980
        {
            //fprintf(stderr,".\n");
            //if (g->g_pd == garray_class)
            //    fprintf(stderr,"garray_getname=%d\n",garray_getname((t_garray *)g, &arrayname));
Miller Puckette's avatar
Miller Puckette committed
981
982
            if (g->g_pd == garray_class &&
                !garray_getname((t_garray *)g, &arrayname))
983
            {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
984
                gui_vmess("gui_graph_label", "xsiissisi",
985
                    glist_getcanvas(x),
986
                    tag,
987
                    i,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
988
                    sys_fontheight(glist_getfont(x)),
989
990
991
992
993
                    arrayname->s_name,
                    sys_font,
                    sys_hostfontsize(glist_getfont(x)),
                    sys_fontweight,
                    glist_isselected(x, gr));
994
            }
Miller Puckette's avatar
Miller Puckette committed
995
996
997
998
999
        }
            /* draw ticks on horizontal borders.  If lperb field is
            zero, this is disabled. */
        if (x->gl_xtick.k_lperb)
        {
1000
            t_float upix, lpix;
Miller Puckette's avatar
Miller Puckette committed
1001
1002
1003
1004
1005
1006
1007
1008
            if (y2 < y1)
                upix = y1, lpix = y2;
            else upix = y2, lpix = y1;
            for (i = 0, f = x->gl_xtick.k_point;
                f < 0.99 * x->gl_x2 + 0.01*x->gl_x1; i++,
                    f += x->gl_xtick.k_inc)
            {
                int tickpix = (i % x->gl_xtick.k_lperb ? 2 : 4);
1009
1010
                gui_vmess("gui_graph_vtick", "xsiiiiii",
                    glist_getcanvas(x->gl_owner),
1011
1012
1013
1014
1015
1016
1017
                    tag,
                    (int)glist_xtopixels(x, f),
                    (int)upix,
                    (int)lpix,
                    (int)tickpix,
                    x1,
                    y1);
Miller Puckette's avatar
Miller Puckette committed
1018
1019
1020
1021
1022
1023
            }
            for (i = 1, f = x->gl_xtick.k_point - x->gl_xtick.k_inc;
                f > 0.99 * x->gl_x1 + 0.01*x->gl_x2;
                    i++, f -= x->gl_xtick.k_inc)
            {
                int tickpix = (i % x->gl_xtick.k_lperb ? 2 : 4);
1024
1025
                gui_vmess("gui_graph_vtick", "xsiiiiii",
                    glist_getcanvas(x->gl_owner),
1026
1027
1028
1029
1030
1031
1032
                    tag,
                    (int)glist_xtopixels(x, f),
                    (int)upix,
                    (int)lpix,
                    (int)tickpix,
                    x1,
                    y1);
Miller Puckette's avatar
Miller Puckette committed
1033
1034
1035
1036
1037
1038
            }
        }

            /* draw ticks in vertical borders*/
        if (x->gl_ytick.k_lperb)
        {
1039
            t_float ubound, lbound;
Miller Puckette's avatar
Miller Puckette committed
1040
1041
1042
1043
1044
1045
1046
1047
            if (x->gl_y2 < x->gl_y1)
                ubound = x->gl_y1, lbound = x->gl_y2;
            else ubound = x->gl_y2, lbound = x->gl_y1;
            for (i = 0, f = x->gl_ytick.k_point;
                f < 0.99 * ubound + 0.01 * lbound;
                    i++, f += x->gl_ytick.k_inc)
            {
                int tickpix = (i % x->gl_ytick.k_lperb ? 2 : 4);
1048
1049
                gui_vmess("gui_graph_htick", "xsiiiiii",
                    glist_getcanvas(x->gl_owner),
1050
1051
1052
1053
1054
1055
1056
                    tag,
                    (int)glist_ytopixels(x, f),
                    x1,
                    x2,
                    (int)tickpix,
                    x1,
                    y1);
Miller Puckette's avatar
Miller Puckette committed
1057
1058
1059
1060
1061
1062
            }
            for (i = 1, f = x->gl_ytick.k_point - x->gl_ytick.k_inc;
                f > 0.99 * lbound + 0.01 * ubound;
                    i++, f -= x->gl_ytick.k_inc)
            {
                int tickpix = (i % x->gl_ytick.k_lperb ? 2 : 4);
1063
1064
                gui_vmess("gui_graph_htick", "xsiiiiii",
                    glist_getcanvas(x->gl_owner),
1065
1066
1067
1068
1069
1070
1071
                    tag,
                    (int)glist_ytopixels(x, f),
                    x1,
                    x2,
                    (int)tickpix,
                    x1,
                    y1);
Miller Puckette's avatar
Miller Puckette committed
1072
1073
1074
1075
            }
        }
            /* draw x labels */
        for (i = 0; i < x->gl_nxlabels; i++)
1076
        {
1077
            gui_vmess("gui_graph_tick_label", "xsiissisiis",
1078
                glist_getcanvas(x),
1079
                tag,
Miller Puckette's avatar
Miller Puckette committed
1080
                (int)glist_xtopixels(x, atof(x->gl_xlabel[i]->s_name)),
Miller Puckette's avatar
Miller Puckette committed
1081
                (int)glist_ytopixels(x, x->gl_xlabely),
1082
1083
                x->gl_xlabel[i]->s_name,
                sys_font, 
1084
                sys_hostfontsize(glist_getfont(x)),
1085
1086
                sys_fontweight,
                x1,
1087
1088
                y1,
                xlabelanchor);
1089
        }
Miller Puckette's avatar
Miller Puckette committed
1090
1091
1092

            /* draw y labels */
        for (i = 0; i < x->gl_nylabels; i++)
1093
        {
1094
            gui_vmess("gui_graph_tick_label", "xsiissisiis",
1095
                glist_getcanvas(x),
1096
                tag,
Miller Puckette's avatar
Miller Puckette committed
1097
1098
                (int)glist_xtopixels(x, x->gl_ylabelx),
                (int)glist_ytopixels(x, atof(x->gl_ylabel[i]->s_name)),
1099
1100
                x->gl_ylabel[i]->s_name,
                sys_font, 
1101
                sys_hostfontsize(glist_getfont(x)),
1102
1103
                sys_fontweight,
                x1,
1104
1105
                y1,
                ylabelanchor);
1106
        }
Miller Puckette's avatar
Miller Puckette committed
1107
1108

            /* draw contents of graph as glist */
1109
1110
1111
1112
        for (g = x->gl_list; g; g = g->g_next)
        {
            gop_redraw = 1;
            //fprintf(stderr,"drawing gop objects\n");
Miller Puckette's avatar
Miller Puckette committed
1113
            gobj_vis(g, x, 1);
1114
1115
1116
1117
            //fprintf(stderr,"done\n");
            gop_redraw = 0;
        }
        /* reselect it upon redrawing if it was selected before */
1118
1119
        glist_drawiofor(parent_glist, &x->gl_obj, 1,
            tag, x1, y1, x2, y2);
1120
1121
        if (glist_isselected(parent_glist, gr))
            gobj_select(gr, parent_glist, 1);
1122
1123
1124
        // here we check for changes in scrollbar because of legacy
        // objects that can fall outside gop window, e.g. scalars
        canvas_getscroll(glist_getcanvas(x->gl_owner));
1125
        //fprintf(stderr,"******************graph_vis SELECT\n");
Miller Puckette's avatar
Miller Puckette committed
1126
1127
1128
    }
    else
    {
1129
        glist_eraseiofor(parent_glist, &x->gl_obj, tag);
Miller Puckette's avatar
Miller Puckette committed
1130
1131
        for (g = x->gl_list; g; g = g->g_next)
            gobj_vis(g, x, 0);
1132

1133
1134
        gui_vmess("gui_gobj_erase", "xs",
            glist_getcanvas(x->gl_owner),
1135
1136
            tag);

1137
1138
1139
        // here we check for changes in scrollbar because of legacy
        // objects that can fall outside gop window, e.g. scalars
        canvas_getscroll(glist_getcanvas(x->gl_owner));
Miller Puckette's avatar
Miller Puckette committed
1140
1141
1142
1143
1144
1145
    }
}

    /* get the graph's rectangle, not counting extra swelling for controls
    to keep them inside the graph.  This is the "logical" pixel size. */

1146
void graph_graphrect(t_gobj *z, t_glist *glist,
Miller Puckette's avatar
Miller Puckette committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
    int *xp1, int *yp1, int *xp2, int *yp2)
{
    t_glist *x = (t_glist *)z;
    int x1 = text_xpix(&x->gl_obj, glist);
    int y1 = text_ypix(&x->gl_obj, glist);
    int x2, y2;
    x2 = x1 + x->gl_pixwidth;
    y2 = y1 + x->gl_pixheight;

    *xp1 = x1;
    *yp1 = y1;
    *xp2 = x2;
    *yp2 = y2;
}

1162
1163
    /* check if the gop size needs to change due to gop's text
    in case hidetext is not enabled */
1164
void graph_checkgop_rect(t_gobj *z, t_glist *glist,
1165
1166
    int *xp1, int *yp1, int *xp2, int *yp2)
{
1167

1168
1169
    //fprintf(stderr,"graph_checkgop_rect\n");
    t_glist *x = (t_glist *)z;
1170
1171
1172
1173
    t_gobj *g;
    int fw = sys_fontwidth(x->gl_font);
    int fh = sys_fontheight(x->gl_font);

1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
    if (!x->gl_hidetext)
    {
        int x21, y21, x22, y22;
        text_widgetbehavior.w_getrectfn(z, glist, &x21, &y21, &x22, &y22);
        if (x22 > *xp2)
            *xp2 = x22;
        if (y22 > *yp2) 
            *yp2 = y22;
        // WARNING: ugly hack trying to replicate rtext_senditup
        // if we have no parent. Later consider fixing hardwired values
        int tcols = strlen(x->gl_name->s_name) - 3;
        int th = fh + fh * (tcols/60) + 4;
        if (tcols > 60) tcols = 60;
        int tw = fw * tcols + 4;
        if (tw + *xp1 > *xp2)
            *xp2 = tw + *xp1;
        if (th + *yp1 > *yp2)
            *yp2 = th + *yp1;
    }

    // check if the gop has array members and if so,
    // make its minimum size based on array names size
1196
1197
1198
1199
    t_symbol *arrayname;
    int cols_tmp = 0;
    int arrayname_cols = 0;
    int arrayname_rows = 0;
1200
1201
1202
1203
1204
    for (g = x->gl_list; g; g = g->g_next)
    {
        if (pd_class(&g->g_pd) == garray_class &&
            !garray_getname((t_garray *)g, &arrayname))
        {
1205
1206
1207
1208
1209
            arrayname_rows += 1;
            cols_tmp = strlen(arrayname->s_name);
            if(cols_tmp > arrayname_cols) arrayname_cols = cols_tmp;
        }
    }
1210
1211
    if (arrayname_rows)
    {
1212
1213
        int fontwidth = sys_fontwidth(x->gl_font);
        int fontheight = sys_fontheight(x->gl_font);
1214
1215
1216
1217
        if ((arrayname_rows * fontheight - 1) > (*yp2 - *yp1))
            *yp2 = *yp1 + (arrayname_rows * fontheight - 1);
        if ((arrayname_cols * fontwidth + 2) > (*xp2 - *xp1))
            *xp2 = *xp1 + (arrayname_cols * fontwidth + 2);
1218
1219
    }

1220
1221
    // failsafe where we cannot have a gop that is smaller than 1x1 pixels
    // regardless whether the text is hidden
1222
1223
1224
1225
1226
1227
1228
1229
    int in = obj_ninlets(pd_checkobject(&z->g_pd));
    int out = obj_noutlets(pd_checkobject(&z->g_pd));
    int max_xlets = in >= out ? in : out;
    int minhsize = (max_xlets * IOWIDTH) +
        ((max_xlets > 1 ? max_xlets - 1 : 0) * IOWIDTH);
    if (minhsize < SCALE_GOP_MINWIDTH) minhsize = SCALE_GOP_MINWIDTH;
    int minvsize = ((in > 0 ? 1 : 0) + (out > 0 ? 1 : 0)) * 2;
    if (minvsize < SCALE_GOP_MINHEIGHT) minvsize = SCALE_GOP_MINHEIGHT;
1230
1231
    if (*xp2 < *xp1+minhsize) *xp2 = *xp1+minhsize;
    if (*yp2 < *yp1+minvsize) *yp2 = *yp1+minvsize;
1232
1233
}

Miller Puckette's avatar
Miller Puckette committed
1234
1235
1236
1237
1238
1239
1240
    /* get the rectangle, enlarged to contain all the "contents" --
    meaning their formal bounds rectangles. */
static void graph_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
    int x1 = 0x7fffffff, y1 = 0x7fffffff, x2 = -0x7fffffff, y2 = -0x7fffffff;
    t_glist *x = (t_glist *)z;
1241
    //fprintf(stderr,"graph_getrect %d\n", x->gl_isgraph);
Miller Puckette's avatar
Miller Puckette committed
1242
1243
1244
1245
1246
1247
    if (x->gl_isgraph)
    {
        int hadwindow;
        t_gobj *g;
        int x21, y21, x22, y22;
        graph_graphrect(z, glist, &x1, &y1, &x2, &y2);
1248
        //fprintf(stderr,"%d %d %d %d\n", x1, y1, x2, y2);
1249

Miller Puckette's avatar
Miller Puckette committed
1250
1251
1252
1253
1254
1255
1256
        if (canvas_showtext(x))
        {
            text_widgetbehavior.w_getrectfn(z, glist, &x21, &y21, &x22, &y22);
            if (x22 > x2) 
                x2 = x22;
            if (y22 > y2) 
                y2 = y22;
1257
            //fprintf(stderr,"canvas_showtext %d %d %d %d\n", x1, y1, x2, y2);
Miller Puckette's avatar
Miller Puckette committed
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
        }
        if (!x->gl_goprect)
        {
            /* expand the rectangle to fit in text objects; this applies only
            to the old (0.37) graph-on-parent behavior. */
            /* lie about whether we have our own window to affect gobj_getrect
            calls below.  */
            hadwindow = x->gl_havewindow;
            x->gl_havewindow = 0;
            for (g = x->gl_list; g; g = g->g_next)
1268
                if (gobj_shouldvis(g, x))
Miller Puckette's avatar
Miller Puckette committed
1269
1270
1271
            {
                    /* don't do this for arrays, just let them hang outside the
                    box. */
1272
1273
                if (pd_class(&g->g_pd) == garray_class ||
                    pd_class(&g->g_pd) == scalar_class)
Miller Puckette's avatar
Miller Puckette committed
1274
1275
1276
1277
1278
1279
1280
1281
1282
                    continue;
                gobj_getrect(g, x, &x21, &y21, &x22, &y22);
                if (x22 > x2) 
                    x2 = x22;
                if (y22 > y2) 
                    y2 = y22;
            }
            x->gl_havewindow = hadwindow;
        }
1283

1284
1285
1286
        //fprintf(stderr,"%d %d %d %d\n", x1, y1, x2, y2);

        // check if the text is not hidden and if so use that as the
1287
1288
        // limit of the gop's size (we check for hidden flag inside
        // the function we point to)
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
        graph_checkgop_rect(z, glist, &x1, &y1, &x2, &y2);

        /* fix visibility of edge items for garrays */
        int has_garray = 0;
        for (g = x->gl_list; g; g = g->g_next)
        {
            if (pd_class(&g->g_pd) == garray_class)
            {
                has_garray = 1;
            }
        }
        /*if (has_garray) {
            x1 -= 1;
            y1 -= 2;
            //x2 += 1;
            y2 += 1;
        }*/
Miller Puckette's avatar
Miller Puckette committed
1306
1307
    }
    else text_widgetbehavior.w_getrectfn(z, glist, &x1, &y1, &x2, &y2);
1308
1309
1310
1311
1312
1313
1314
1315
1316

    if (sys_legacy == 1)
    {
        //fprintf(stderr,"legacy  gop\n");
        y1 += 1;
        y2 += 1;
    }
    //fprintf(stderr,"    post %d %d %d %d\n", x1, y1, x2, y2); 

Miller Puckette's avatar
Miller Puckette committed
1317
1318
1319
1320
1321
1322
1323
1324
    *xp1 = x1;
    *yp1 = y1;
    *xp2 = x2;
    *yp2 = y2;
}

static void graph_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
1325
    //fprintf(stderr,"graph_displace %d %d\n", dx, dy);
Miller Puckette's avatar
Miller Puckette committed
1326
1327
1328
1329
1330
1331
1332
    t_glist *x = (t_glist *)z;
    if (!x->gl_isgraph)
        text_widgetbehavior.w_displacefn(z, glist, dx, dy);
    else
    {
        x->gl_obj.te_xpix += dx;
        x->gl_obj.te_ypix += dy;
1333
1334
1335
1336
        /*char tag[80];
        sprintf(tag, "%s",
            rtext_gettag(
                glist_findrtext((x->gl_owner ? x->gl_owner: x), &x->gl_obj)));
1337
1338
1339
1340
        sys_vgui(".x%lx.c move %s %d %d\n",
            glist_getcanvas(x->gl_owner), tag, dx, dy);
        sys_vgui(".x%lx.c move %sR %d %d\n",
            glist_getcanvas(x->gl_owner), tag, dx, dy);*/
1341
1342
1343
1344
1345
1346
1347
        if (!do_not_redraw)
        {
            //fprintf(stderr,"graph_displace redraw\n");
            glist_redraw(glist_getcanvas(glist));
            //gobj_select(z, glist, 1);
            canvas_fixlinesfor(glist_getcanvas(glist), &x->gl_obj);
        }
1348
1349
1350
    }
}

1351
extern int old_displace; //from g_editor.c for legacy drawing
1352

1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
static void graph_displace_scalars(t_glist *x, t_glist *glist, int dx, int dy)
{
    t_gobj *g;
    for (g = x->gl_list; g; g = g->g_next)
    {
        if (pd_class((t_pd *)g) == scalar_class &&
            g->g_pd->c_wb->w_displacefnwtag != NULL)
        {
            (*(g->g_pd->c_wb->w_displacefnwtag))(g, glist, dx, dy);
        }
        else if (pd_class(&g->g_pd) == canvas_class &&
                 ((t_glist *)g)->gl_isgraph)
        {
            graph_displace_scalars((t_glist *)g, glist, dx, dy); 
        }
    }
}

1371
1372
static void graph_displace_withtag(t_gobj *z, t_glist *glist, int dx, int dy)
{
1373
    //fprintf(stderr,"graph_displace_withtag %d %d\n", dx, dy);
1374
1375
1376
1377
1378
    t_glist *x = (t_glist *)z;
    if (!x->gl_isgraph)
        text_widgetbehavior.w_displacefnwtag(z, glist, dx, dy);
    else
    {
1379
1380
1381
        // first check for legacy objects that don't offer