g_canvas.c 87.1 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
12
/* 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 defines the "glist" class, also known as "canvas" (the two used
to be different but are now unified except for some fossilized names.) */

#include <stdlib.h>
#include <stdio.h>
#include "m_pd.h"
#include "m_imp.h"
#include "s_stuff.h"
13
#include "g_magicglass.h"
Miller Puckette's avatar
Miller Puckette committed
14
15
#include "g_canvas.h"
#include "g_all_guis.h"
16
#include <string.h>
17
#include <math.h>
18

19
extern int do_not_redraw;
20
21
extern void canvas_drawconnection(t_canvas *x, int lx1, int ly1, int lx2, int ly2, t_int tag, int issignal);
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
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

    /* LATER consider adding font size to this struct (see glist_getfont()) */
struct _canvasenvironment
{
    t_symbol *ce_dir;      /* directory patch lives in */
    int ce_argc;           /* number of "$" arguments */
    t_atom *ce_argv;       /* array of "$" arguments */
    int ce_dollarzero;     /* value of "$0" */
    t_namelist *ce_path;   /* search path */
};

#define GLIST_DEFCANVASWIDTH 450
#define GLIST_DEFCANVASHEIGHT 300

#ifdef __APPLE__
#define GLIST_DEFCANVASYLOC 22
#else
#define GLIST_DEFCANVASYLOC 0
#endif

/* ---------------------- variables --------------------------- */

extern t_pd *newest;
t_class *canvas_class;
int canvas_dspstate;                /* whether DSP is on or off */  
t_canvas *canvas_editing;           /* last canvas to start text edting */ 
t_canvas *canvas_whichfind;         /* last canvas we did a find in */ 
49
//t_canvas *canvas_list;              /* list of all root canvases */
Miller Puckette's avatar
Miller Puckette committed
50
51
52
53
54

/* ------------------ forward function declarations --------------- */
static void canvas_start_dsp(void);
static void canvas_stop_dsp(void);
static void canvas_drawlines(t_canvas *x);
55
void canvas_dosetbounds(t_canvas *x, int x1, int y1, int x2, int y2);
56
void canvas_reflecttitle(t_canvas *x);
Miller Puckette's avatar
Miller Puckette committed
57
58
59
static void canvas_addtolist(t_canvas *x);
static void canvas_takeofflist(t_canvas *x);
static void canvas_pop(t_canvas *x, t_floatarg fvis);
60
61
62
static int canvas_should_bind(t_canvas *x);
static void canvas_bind(t_canvas *x);
static void canvas_unbind(t_canvas *x);
63
void canvas_declare(t_canvas *x, t_symbol *s, int argc, t_atom *argv);
Miller Puckette's avatar
Miller Puckette committed
64
65
66
67
68
69
70
71

/* --------- functions to handle the canvas environment ----------- */

static t_symbol *canvas_newfilename = &s_;
static t_symbol *canvas_newdirectory = &s_;
static int canvas_newargc;
static t_atom *canvas_newargv;

72
73
    /* maintain the list of visible toplevels for the GUI's "windows" menu */
void canvas_updatewindowlist( void)
Miller Puckette's avatar
Miller Puckette committed
74
{
75
    t_canvas *x;
76
77
    if (glist_amreloadingabstractions)  /* not if we're in a reload */
        return;
78
79
    gui_start_vmess("gui_set_toplevel_window_list", "s", "dummy");
    gui_start_array();
80
    for (x = pd_this->pd_canvaslist; x; x = x->gl_next)
Miller Puckette's avatar
Miller Puckette committed
81
    {
82
        if (!x->gl_owner)
Miller Puckette's avatar
Miller Puckette committed
83
        {
84
85
            /* this is a canvas; if we have a window, put on "windows" list */
            if (x->gl_havewindow)
Miller Puckette's avatar
Miller Puckette committed
86
            {
87
88
                gui_s(x->gl_name->s_name);
                gui_x((long unsigned int)x);
Miller Puckette's avatar
Miller Puckette committed
89
90
91
            }
        }
    }
92
93
    gui_end_array();
    gui_end_vmess();
Miller Puckette's avatar
Miller Puckette committed
94
95
96
97
98
}

    /* add a glist the list of "root" canvases (toplevels without parents.) */
static void canvas_addtolist(t_canvas *x)
{
99
100
    x->gl_next = pd_this->pd_canvaslist;
    pd_this->pd_canvaslist = x;
Miller Puckette's avatar
Miller Puckette committed
101
102
103
104
105
}

static void canvas_takeofflist(t_canvas *x)
{
        /* take it off the window list */
106
    if (x == pd_this->pd_canvaslist) pd_this->pd_canvaslist = x->gl_next;
Miller Puckette's avatar
Miller Puckette committed
107
108
109
    else
    {
        t_canvas *z;
110
111
        for (z = pd_this->pd_canvaslist; z->gl_next != x; z = z->gl_next)
            if (!z->gl_next) return;
Miller Puckette's avatar
Miller Puckette committed
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
148
149
150
151
152
153
        z->gl_next = x->gl_next;
    }
}


void canvas_setargs(int argc, t_atom *argv)
{
        /* if there's an old one lying around free it here.  This
        happens if an abstraction is loaded but never gets as far
        as calling canvas_new(). */
    if (canvas_newargv)
        freebytes(canvas_newargv, canvas_newargc * sizeof(t_atom));
    canvas_newargc = argc;
    canvas_newargv = copybytes(argv, argc * sizeof(t_atom));
}

void glob_setfilename(void *dummy, t_symbol *filesym, t_symbol *dirsym)
{
    canvas_newfilename = filesym;
    canvas_newdirectory = dirsym;
}

t_canvas *canvas_getcurrent(void)
{
    return ((t_canvas *)pd_findbyclass(&s__X, canvas_class));
}

void canvas_setcurrent(t_canvas *x)
{
    pd_pushsym(&x->gl_pd);
}

void canvas_unsetcurrent(t_canvas *x)
{
    pd_popsym(&x->gl_pd);
}

t_canvasenvironment *canvas_getenv(t_canvas *x)
{
    if (!x) bug("canvas_getenv");
    while (!x->gl_env)
        if (!(x = x->gl_owner))
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
154
            bug("t_canvasenvironment");
Miller Puckette's avatar
Miller Puckette committed
155
156
157
    return (x->gl_env);
}

158
159
160
161
extern t_class *messresponder_class;
extern t_glist *messresponder_getglist(t_pd *x);

int canvas_getdollarzero(t_pd *x)
Miller Puckette's avatar
Miller Puckette committed
162
{
163
164
165
166
167
168
169
170
171
    t_canvas *cnv;
    /* binbuf_eval can send us a NULL target... */
    if (x && pd_class(x) == messresponder_class)
    {
        cnv = messresponder_getglist(x);
    }
    else
        cnv = canvas_getcurrent();
    t_canvasenvironment *env = (cnv ? canvas_getenv(cnv) : 0);
Miller Puckette's avatar
Miller Puckette committed
172
173
174
175
176
177
178
179
180
181
182
183
    if (env)
        return (env->ce_dollarzero);
    else return (0);
}

void canvas_getargs(int *argcp, t_atom **argvp)
{
    t_canvasenvironment *e = canvas_getenv(canvas_getcurrent());
    *argcp = e->ce_argc;
    *argvp = e->ce_argv;
}

184
185
186
187
188
189
190
void canvas_getargs_after_creation(t_canvas *c, int *argcp, t_atom **argvp)
{
    t_canvasenvironment *e = canvas_getenv(c);
    *argcp = e->ce_argc;
    *argvp = e->ce_argv;
}

Miller Puckette's avatar
Miller Puckette committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
t_symbol *canvas_realizedollar(t_canvas *x, t_symbol *s)
{
    t_symbol *ret;
    char *name = s->s_name;
    if (strchr(name, '$'))
    {
        t_canvasenvironment *env = canvas_getenv(x);
        canvas_setcurrent(x);
        ret = binbuf_realizedollsym(s, env->ce_argc, env->ce_argv, 1);
        canvas_unsetcurrent(x);
    }
    else ret = s;
    return (ret);
}

t_symbol *canvas_getcurrentdir(void)
{
    t_canvasenvironment *e = canvas_getenv(canvas_getcurrent());
    return (e->ce_dir);
}

t_symbol *canvas_getdir(t_canvas *x)
{
    t_canvasenvironment *e = canvas_getenv(x);
    return (e->ce_dir);
}

void canvas_makefilename(t_canvas *x, char *file, char *result, int resultsize)
{
220
221
222
    char interim[FILENAME_MAX];
    sys_expandpathelems(file, interim);
    //fprintf(stderr,"interim = <%s>\n", interim);
Miller Puckette's avatar
Miller Puckette committed
223
    char *dir = canvas_getenv(x)->ce_dir->s_name;
224
    if (interim[0] == '/' || (interim[0] && interim[1] == ':') || !*dir)
Miller Puckette's avatar
Miller Puckette committed
225
    {
226
        //fprintf(stderr,"root file\n");
227
        strncpy(result, interim, resultsize);
Miller Puckette's avatar
Miller Puckette committed
228
229
230
231
        result[resultsize-1] = 0;
    }
    else
    {
232
        //fprintf(stderr,"relative file\n");
Miller Puckette's avatar
Miller Puckette committed
233
234
235
236
237
238
        int nleft;
        strncpy(result, dir, resultsize);
        result[resultsize-1] = 0;
        nleft = resultsize - strlen(result) - 1;
        if (nleft <= 0) return;
        strcat(result, "/");
239
        strncat(result, interim, nleft);
Miller Puckette's avatar
Miller Puckette committed
240
        result[resultsize-1] = 0;
241
    } 
242
    //fprintf(stderr,"resulting file = <%s>\n", result);          
Miller Puckette's avatar
Miller Puckette committed
243
244
245
246
}

void canvas_rename(t_canvas *x, t_symbol *s, t_symbol *dir)
{
247
    canvas_unbind(x);
Miller Puckette's avatar
Miller Puckette committed
248
    x->gl_name = s;
249
    canvas_bind(x);
Miller Puckette's avatar
Miller Puckette committed
250
251
252
253
254
    if (dir && dir != &s_)
    {
        t_canvasenvironment *e = canvas_getenv(x);
        e->ce_dir = dir;
    }
255
256
257
    if (glist_isvisible(x))
        if (x->gl_havewindow) //was glist_isvisible(x)
            canvas_reflecttitle(x);
Miller Puckette's avatar
Miller Puckette committed
258
259
260
261
262
263
264
265
266
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
299
300
301
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
335
336
337
338
339
340
341
342
}

/* --------------- traversing the set of lines in a canvas ----------- */

int canvas_getindex(t_canvas *x, t_gobj *y)
{
    t_gobj *y2;
    int indexno;
    for (indexno = 0, y2 = x->gl_list; y2 && y2 != y; y2 = y2->g_next)
        indexno++;
    return (indexno);
}

void linetraverser_start(t_linetraverser *t, t_canvas *x)
{
    t->tr_ob = 0;
    t->tr_x = x;
    t->tr_nextoc = 0;
    t->tr_nextoutno = t->tr_nout = 0;
}

t_outconnect *linetraverser_next(t_linetraverser *t)
{
    t_outconnect *rval = t->tr_nextoc;
    int outno;
    while (!rval)
    {
        outno = t->tr_nextoutno;
        while (outno == t->tr_nout)
        {
            t_gobj *y;
            t_object *ob = 0;
            if (!t->tr_ob) y = t->tr_x->gl_list;
            else y = t->tr_ob->ob_g.g_next;
            for (; y; y = y->g_next)
                if (ob = pd_checkobject(&y->g_pd)) break;
            if (!ob) return (0);
            t->tr_ob = ob;
            t->tr_nout = obj_noutlets(ob);
            outno = 0;
            if (glist_isvisible(t->tr_x))
                gobj_getrect(y, t->tr_x,
                    &t->tr_x11, &t->tr_y11, &t->tr_x12, &t->tr_y12);
            else t->tr_x11 = t->tr_y11 = t->tr_x12 = t->tr_y12 = 0;
        }
        t->tr_nextoutno = outno + 1;
        rval = obj_starttraverseoutlet(t->tr_ob, &t->tr_outlet, outno);
        t->tr_outno = outno;
    }
    t->tr_nextoc = obj_nexttraverseoutlet(rval, &t->tr_ob2,
        &t->tr_inlet, &t->tr_inno);
    t->tr_nin = obj_ninlets(t->tr_ob2);
    if (!t->tr_nin) bug("drawline");
    if (glist_isvisible(t->tr_x))
    {
        int inplus = (t->tr_nin == 1 ? 1 : t->tr_nin - 1);
        int outplus = (t->tr_nout == 1 ? 1 : t->tr_nout - 1);
        gobj_getrect(&t->tr_ob2->ob_g, t->tr_x,
            &t->tr_x21, &t->tr_y21, &t->tr_x22, &t->tr_y22);
        t->tr_lx1 = t->tr_x11 +
            ((t->tr_x12 - t->tr_x11 - IOWIDTH) * t->tr_outno) /
                outplus + IOMIDDLE;
        t->tr_ly1 = t->tr_y12;
        t->tr_lx2 = t->tr_x21 +
            ((t->tr_x22 - t->tr_x21 - IOWIDTH) * t->tr_inno)/inplus +
                IOMIDDLE;
        t->tr_ly2 = t->tr_y21;
    }
    else
    {
        t->tr_x21 = t->tr_y21 = t->tr_x22 = t->tr_y22 = 0;
        t->tr_lx1 = t->tr_ly1 = t->tr_lx2 = t->tr_ly2 = 0;
    }
    return (rval);
}

void linetraverser_skipobject(t_linetraverser *t)
{
    t->tr_nextoc = 0;
    t->tr_nextoutno = t->tr_nout;
}

/* -------------------- the canvas object -------------------------- */
int glist_valid = 10000;

343
344
345
346
void canvasgop__clickhook(t_scalehandle *sh, int newstate);
void canvasgop__motionhook(t_scalehandle *sh,t_floatarg f1, t_floatarg f2);
extern void glist_setlastxy(t_glist *gl, int xval, int yval);

Miller Puckette's avatar
Miller Puckette committed
347
348
349
350
351
352
353
354
355
void glist_init(t_glist *x)
{
        /* zero out everyone except "pd" field */
    memset(((char *)x) + sizeof(x->gl_pd), 0, sizeof(*x) - sizeof(x->gl_pd));
    x->gl_stub = gstub_new(x, 0);
    x->gl_valid = ++glist_valid;
    x->gl_xlabel = (t_symbol **)t_getbytes(0);
    x->gl_ylabel = (t_symbol **)t_getbytes(0);

356
357
358
359
360
361
    //dpsaha@vt.edu gop resize (refactored by mathieu)
    x->x_handle = scalehandle_new((t_object *)x, x, 1,
        canvasgop__clickhook, canvasgop__motionhook);
    x->x_mhandle = scalehandle_new((t_object *)x, x, 0,
        canvasgop__clickhook, canvasgop__motionhook);
}
362

363
364
365
366
367
368
369
/* These globals are used to set state for the "canvas" field in a
   struct. We try below to make sure they only get set for the toplevel
   canvas, and then set them to NULL for everything else. */
t_symbol *canvas_field_templatesym; /* for "canvas" data type */
t_word *canvas_field_vec;           /* for "canvas" data type */
t_gpointer *canvas_field_gp;        /* parent for "canvas" data type */

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
static int calculate_zoom(t_float zoom_hack)
{
  // This gives back the zoom level stored in the patch (cf. zoom_hack
  // in g_readwrite.c). Make sure to round this value to handle any rounding
  // errors due to the limited float precision in Pd's binbuf writer.
  int zoom = round(zoom_hack*32);
  // This is a 5 bit number in 2's complement, so we need to extend the sign.
  zoom = zoom << 27 >> 27;
  // To be on the safe side, we finally clamp the result to the range -7..8
  // which are the zoom levels supported by Purr Data right now.
  if (zoom < -7) zoom = -7;
  if (zoom > 8) zoom = 8;
  //post("read zoom level: %d", zoom);
  return zoom;
}

Miller Puckette's avatar
Miller Puckette committed
386
    /* make a new glist.  It will either be a "root" canvas or else
387
    it appears as a "text" object in another window (canvas_getcurrent() 
Miller Puckette's avatar
Miller Puckette committed
388
389
390
    tells us which.) */
t_canvas *canvas_new(void *dummy, t_symbol *sel, int argc, t_atom *argv)
{
391
392
393
394
395
396
397
398
399
    /* DEBUG
    int d;
    for (d=0; d < argc; d++)
    {
        if (argv[d].a_type == A_FLOAT)
            fprintf(stderr, " %g ", argv[d].a_w.w_float);
        else fprintf(stderr, " %s ", argv[d].a_w.w_symbol->s_name);
    }
    fprintf(stderr,"\n");*/
Miller Puckette's avatar
Miller Puckette committed
400
401
402
403
404
405
    t_canvas *x = (t_canvas *)pd_new(canvas_class);
    t_canvas *owner = canvas_getcurrent();
    t_symbol *s = &s_;
    int vis = 0, width = GLIST_DEFCANVASWIDTH, height = GLIST_DEFCANVASHEIGHT;
    int xloc = 0, yloc = GLIST_DEFCANVASYLOC;
    int font = (owner ? owner->gl_font : sys_defaultfont);
406
407
    int zoom = 0;
    extern int sys_zoom;
408

Miller Puckette's avatar
Miller Puckette committed
409
    glist_init(x);
410
    //x->gl_magic_glass = magicGlass_new(x);
Miller Puckette's avatar
Miller Puckette committed
411
412
413
414
415
416
417
    x->gl_obj.te_type = T_OBJECT;
    if (!owner)
        canvas_addtolist(x);
    /* post("canvas %lx, owner %lx", x, owner); */

    if (argc == 5)  /* toplevel: x, y, w, h, font */
    {
418
        t_float zoom_hack = atom_getfloatarg(3, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
419
420
421
422
423
        xloc = atom_getintarg(0, argc, argv);
        yloc = atom_getintarg(1, argc, argv);
        width = atom_getintarg(2, argc, argv);
        height = atom_getintarg(3, argc, argv);
        font = atom_getintarg(4, argc, argv);
424
425
426
        zoom_hack -= height;
        if (sys_zoom && zoom_hack > 0)
            zoom = calculate_zoom(zoom_hack);
Miller Puckette's avatar
Miller Puckette committed
427
428
429
    }
    else if (argc == 6)  /* subwindow: x, y, w, h, name, vis */
    {
430
        t_float zoom_hack = atom_getfloatarg(3, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
431
432
433
434
435
436
        xloc = atom_getintarg(0, argc, argv);
        yloc = atom_getintarg(1, argc, argv);
        width = atom_getintarg(2, argc, argv);
        height = atom_getintarg(3, argc, argv);
        s = atom_getsymbolarg(4, argc, argv);
        vis = atom_getintarg(5, argc, argv);
437
438
439
        zoom_hack -= height;
        if (sys_zoom && zoom_hack > 0)
            zoom = calculate_zoom(zoom_hack);
Miller Puckette's avatar
Miller Puckette committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
    }
        /* (otherwise assume we're being created from the menu.) */

    if (canvas_newdirectory->s_name[0])
    {
        static int dollarzero = 1000;
        t_canvasenvironment *env = x->gl_env =
            (t_canvasenvironment *)getbytes(sizeof(*x->gl_env));
        if (!canvas_newargv)
            canvas_newargv = getbytes(0);
        env->ce_dir = canvas_newdirectory;
        env->ce_argc = canvas_newargc;
        env->ce_argv = canvas_newargv;
        env->ce_dollarzero = dollarzero++;
        env->ce_path = 0;
        canvas_newdirectory = &s_;
        canvas_newargc = 0;
        canvas_newargv = 0;
    }
    else x->gl_env = 0;

    if (yloc < GLIST_DEFCANVASYLOC)
        yloc = GLIST_DEFCANVASYLOC;
    if (xloc < 0)
        xloc = 0;
    x->gl_x1 = 0;
    x->gl_y1 = 0;
    x->gl_x2 = 1;
    x->gl_y2 = 1;
469
    canvas_dosetbounds(x, xloc, yloc, xloc + width, yloc + height);
Miller Puckette's avatar
Miller Puckette committed
470
    x->gl_owner = owner;
471
    x->gl_isclone = 0;
Miller Puckette's avatar
Miller Puckette committed
472
473
    x->gl_name = (*s->s_name ? s : 
        (canvas_newfilename ? canvas_newfilename : gensym("Pd")));
474
    canvas_bind(x);
Miller Puckette's avatar
Miller Puckette committed
475
    x->gl_loading = 1;
476
    x->gl_unloading = 0;
477
    //fprintf(stderr,"loading = 1 .x%lx owner=.x%lx\n", (t_int)x, (t_int)x->gl_owner);
Miller Puckette's avatar
Miller Puckette committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    x->gl_goprect = 0;      /* no GOP rectangle unless it's turned on later */
        /* cancel "vis" flag if we're a subpatch of an
         abstraction inside another patch.  A separate mechanism prevents
         the toplevel abstraction from showing up. */
    if (vis && gensym("#X")->s_thing && 
        ((*gensym("#X")->s_thing) == canvas_class))
    {
        t_canvas *zzz = (t_canvas *)(gensym("#X")->s_thing);
        while (zzz && !zzz->gl_env)
            zzz = zzz->gl_owner;
        if (zzz && canvas_isabstraction(zzz) && zzz->gl_owner)
            vis = 0;
    }
    x->gl_willvis = vis;
    x->gl_edit = !strncmp(x->gl_name->s_name, "Untitled", 8);
    x->gl_font = sys_nearestfontsize(font);
494
    x->gl_zoom = zoom;
Miller Puckette's avatar
Miller Puckette committed
495
    pd_pushsym(&x->gl_pd);
496

497
    x->u_queue = canvas_undo_init(x);
498
    //glist_setlastxy(x, 20, 20);
499
500
501
502
503
504
505
506
507
508

    x->gl_templatesym = canvas_field_templatesym;
    x->gl_vec = canvas_field_vec;
    if (canvas_field_gp) gpointer_copy(canvas_field_gp, &x->gl_gp);

    // unset the globals in case this was a canvas field
    canvas_field_templatesym = NULL;
    canvas_field_vec = NULL;
    canvas_field_gp = NULL;

Miller Puckette's avatar
Miller Puckette committed
509
510
511
512
513
514
515
    return(x);
}

void canvas_setgraph(t_glist *x, int flag, int nogoprect);

static void canvas_coords(t_glist *x, t_symbol *s, int argc, t_atom *argv)
{
516
517
    //IB: first delete the graph in case we are downsizing the object size via script
    canvas_setgraph(x, 0, 0);
518

Miller Puckette's avatar
Miller Puckette committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    x->gl_x1 = atom_getfloatarg(0, argc, argv);
    x->gl_y1 = atom_getfloatarg(1, argc, argv);
    x->gl_x2 = atom_getfloatarg(2, argc, argv);
    x->gl_y2 = atom_getfloatarg(3, argc, argv);
    x->gl_pixwidth = atom_getintarg(4, argc, argv);
    x->gl_pixheight = atom_getintarg(5, argc, argv);
    if (argc <= 7)
        canvas_setgraph(x, atom_getintarg(6, argc, argv), 1);
    else
    {
        x->gl_xmargin = atom_getintarg(7, argc, argv);
        x->gl_ymargin = atom_getintarg(8, argc, argv);
        canvas_setgraph(x, atom_getintarg(6, argc, argv), 0);
    }
}

    /* make a new glist and add it to this glist.  It will appear as
    a "graph", not a text object.  */
t_glist *glist_addglist(t_glist *g, t_symbol *sym,
538
539
    t_float x1, t_float y1, t_float x2, t_float y2,
    t_float px1, t_float py1, t_float px2, t_float py2)
Miller Puckette's avatar
Miller Puckette committed
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
{
    static int gcount = 0;
    int zz;
    int menu = 0;
    char *str;
    t_glist *x = (t_glist *)pd_new(canvas_class);
    glist_init(x);
    x->gl_obj.te_type = T_OBJECT;
    if (!*sym->s_name)
    {
        char buf[40];
        sprintf(buf, "graph%d", ++gcount);
        sym = gensym(buf);
        menu = 1;
    }
    else if (!strncmp((str = sym->s_name), "graph", 5)
        && (zz = atoi(str + 5)) > gcount)
            gcount = zz;
        /* in 0.34 and earlier, the pixel rectangle and the y bounds were
        reversed; this would behave the same, except that the dialog window
        would be confusing.  The "correct" way is to have "py1" be the value
        that is higher on the screen. */
    if (py2 < py1)
    {
564
        t_float zz;
Miller Puckette's avatar
Miller Puckette committed
565
566
567
568
569
570
571
572
573
        zz = y2;
        y2 = y1;
        y1 = zz;
        zz = py2;
        py2 = py1;
        py1 = zz;
    }
    if (x1 == x2 || y1 == y2)
        x1 = 0, x2 = 100, y1 = 1, y2 = -1;
574
575
    if (px1 != 0 && px2 == 0) px2 = px1 + GLIST_DEFGRAPHWIDTH;
    if (py1 != 0 && py2 == py1) py2 = py1 + GLIST_DEFGRAPHHEIGHT;
Miller Puckette's avatar
Miller Puckette committed
576
577
578
    if (px1 >= px2 || py1 >= py2)
        px1 = 100, py1 = 20, px2 = 100 + GLIST_DEFGRAPHWIDTH,
            py2 = 20 + GLIST_DEFGRAPHHEIGHT;
579

Miller Puckette's avatar
Miller Puckette committed
580
581
582
583
584
585
586
587
588
589
590
    x->gl_name = sym;
    x->gl_x1 = x1;
    x->gl_x2 = x2;
    x->gl_y1 = y1;
    x->gl_y2 = y2;
    x->gl_obj.te_xpix = px1;
    x->gl_obj.te_ypix = py1;
    x->gl_pixwidth = px2 - px1;
    x->gl_pixheight = py2 - py1;
    x->gl_font =  (canvas_getcurrent() ?
        canvas_getcurrent()->gl_font : sys_defaultfont);
591
    x->gl_zoom = 0;
Miller Puckette's avatar
Miller Puckette committed
592
593
594
595
    x->gl_screenx1 = x->gl_screeny1 = 0;
    x->gl_screenx2 = 450;
    x->gl_screeny2 = 300;
    x->gl_owner = g;
596
    canvas_bind(x);
Miller Puckette's avatar
Miller Puckette committed
597
598
599
600
601
602
603
    x->gl_isgraph = 1;
    x->gl_goprect = 0;
    x->gl_obj.te_binbuf = binbuf_new();
    binbuf_addv(x->gl_obj.te_binbuf, "s", gensym("graph"));
    if (!menu)
        pd_pushsym(&x->gl_pd);
    glist_add(g, &x->gl_gobj);
604
    if (!do_not_redraw) scrollbar_update(glist_getcanvas(g));
Ivica Bukvic's avatar
Ivica Bukvic committed
605
    //fprintf(stderr,"    ... %f %f\n", x->gl_x1, x->gl_x2);
Miller Puckette's avatar
Miller Puckette committed
606
607
608
    return (x);
}

609
610
extern int we_are_undoing;

Miller Puckette's avatar
Miller Puckette committed
611
612
613
    /* call glist_addglist from a Pd message */
void glist_glist(t_glist *g, t_symbol *s, int argc, t_atom *argv)
{
614
    if (canvas_hasarray(g)) return;
615
    pd_vmess(&g->gl_pd, gensym("editmode"), "i", 1);
616
    t_symbol *sym = atom_getsymbolarg(0, argc, argv);
617
618
    /* if we wish to put a graph where the mouse is we need to replace bogus name */
    if (!strcmp(sym->s_name, "NULL")) sym = &s_;  
619
620
621
622
623
624
625
626
    t_float x1 = atom_getfloatarg(1, argc, argv);  
    t_float y1 = atom_getfloatarg(2, argc, argv);  
    t_float x2 = atom_getfloatarg(3, argc, argv);  
    t_float y2 = atom_getfloatarg(4, argc, argv);  
    t_float px1 = atom_getfloatarg(5, argc, argv);  
    t_float py1 = atom_getfloatarg(6, argc, argv);  
    t_float px2 = atom_getfloatarg(7, argc, argv);  
    t_float py2 = atom_getfloatarg(8, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
627
    glist_addglist(g, sym, x1, y1, x2, y2, px1, py1, px2, py2);
628
629
630
    if (!we_are_undoing)
        canvas_undo_add(glist_getcanvas(g), 9, "create",
            (void *)canvas_undo_set_create(glist_getcanvas(g)));
Miller Puckette's avatar
Miller Puckette committed
631
632
633
634
635
636
}

    /* return true if the glist should appear as a graph on parent;
    otherwise it appears as a text box. */
int glist_isgraph(t_glist *x)
{
637
    // testing to see if we have an array and force hiding text (later update GUI accordingly)
638
639
    // we likely need this to silently update legacy arrays
    // (no regressions are expected but this needs to be tested)
640
641
642
643
644
645
    t_gobj *g = x->gl_list;
    int hasarray = 0;
    while (g) {
        if (pd_class(&g->g_pd) == garray_class) hasarray = 1;
        g = g->g_next;
    }
646
647
    if (hasarray)  {
        x->gl_isgraph = 1;
648
        x->gl_hidetext = 1;
649
    }
650
    return (x->gl_isgraph|(x->gl_hidetext<<1));
Miller Puckette's avatar
Miller Puckette committed
651
652
}

653
654
/* bounds-setting for patch/subpatch windows */
void canvas_dosetbounds(t_canvas *x, int x1, int y1, int x2, int y2)
Miller Puckette's avatar
Miller Puckette committed
655
{
656
    //fprintf(stderr,"canvas_setbounds %d %d %d %d\n", x1, y1, x2, y2);
657

Miller Puckette's avatar
Miller Puckette committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
    int heightwas = y2 - y1;
    int heightchange = y2 - y1 - (x->gl_screeny2 - x->gl_screeny1);
    if (x->gl_screenx1 == x1 && x->gl_screeny1 == y1 &&
        x->gl_screenx2 == x2 && x->gl_screeny2 == y2)
            return;
    x->gl_screenx1 = x1;
    x->gl_screeny1 = y1;
    x->gl_screenx2 = x2;
    x->gl_screeny2 = y2;
    if (!glist_isgraph(x) && (x->gl_y2 < x->gl_y1)) 
    {
            /* if it's flipped so that y grows upward,
            fix so that zero is bottom edge and redraw.  This is
            only appropriate if we're a regular "text" object on the
            parent. */
673
        t_float diff = x->gl_y1 - x->gl_y2;
Miller Puckette's avatar
Miller Puckette committed
674
675
676
677
678
679
680
681
682
683
684
685
        t_gobj *y;
        x->gl_y1 = heightwas * diff;
        x->gl_y2 = x->gl_y1 - diff;
            /* and move text objects accordingly; they should stick
            to the bottom, not the top. */
        for (y = x->gl_list; y; y = y->g_next)
            if (pd_checkobject(&y->g_pd))
                gobj_displace(y, x, 0, heightchange);
        canvas_redraw(x);
    }
}

686
687
688
689
690
691
692
693
    /* public method to set the bounds for a patch/subpatch window from the
       GUI. */
static void canvas_setbounds(t_canvas *x, t_float left, t_float top,
                             t_float right, t_float bottom)
{
    canvas_dosetbounds(x, (int)left, (int)top, (int)right, (int)bottom);
}

Miller Puckette's avatar
Miller Puckette committed
694
695
696
t_symbol *canvas_makebindsym(t_symbol *s)
{
    char buf[MAXPDSTRING];
697
698
    snprintf(buf, MAXPDSTRING-1, "pd-%s", s->s_name);
    buf[MAXPDSTRING-1] = 0;
Miller Puckette's avatar
Miller Puckette committed
699
700
701
    return (gensym(buf));
}

Jonathan Wilkes's avatar
Jonathan Wilkes committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
void canvas_args_to_string(char *namebuf, t_canvas *x)
{
    t_canvasenvironment *env = canvas_getenv(x);
    if (env->ce_argc)
    {
        int i;
        strcpy(namebuf, " (");
        for (i = 0; i < env->ce_argc; i++)
        {
            if (strlen(namebuf) > MAXPDSTRING/2 - 5)
                break;
            if (i != 0)
                strcat(namebuf, " ");
            atom_string(&env->ce_argv[i], namebuf + strlen(namebuf), 
                MAXPDSTRING/2);
        }
        strcat(namebuf, ")");
    }
    else namebuf[0] = 0;
}

Miller Puckette's avatar
Miller Puckette committed
723
724
725
void canvas_reflecttitle(t_canvas *x)
{
    char namebuf[MAXPDSTRING];
Jonathan Wilkes's avatar
Jonathan Wilkes committed
726
    canvas_args_to_string(namebuf, x);
727
728
    gui_vmess("gui_canvas_set_title", "xsssi",
        x, x->gl_name->s_name,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
729
        namebuf, canvas_getdir(x)->s_name, x->gl_dirty);
Miller Puckette's avatar
Miller Puckette committed
730
731
}

Miller Puckette's avatar
Miller Puckette committed
732
733
    /* mark a glist dirty or clean */
void canvas_dirty(t_canvas *x, t_floatarg n)
Miller Puckette's avatar
Miller Puckette committed
734
735
{
    t_canvas *x2 = canvas_getrootfor(x);
736
737
    if (glist_amreloadingabstractions)
        return;
Miller Puckette's avatar
Miller Puckette committed
738
739
740
    if ((unsigned)n != x2->gl_dirty)
    {
        x2->gl_dirty = n;
741
        if (x2->gl_havewindow)
Miller Puckette's avatar
test7    
Miller Puckette committed
742
            canvas_reflecttitle(x2);
Miller Puckette's avatar
Miller Puckette committed
743
744
745
    }
}

746
void draw_notify(t_canvas *x, t_symbol *s, int argc, t_atom *argv);
747

748
void canvas_scalar_event(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
749
{
750
751
752
753
754
    /* These events only get sent when we're not in edit mode.  Once
       we get editmode status sync'd in the GUI we can just prevent
       sending any messages when in edit mode. */
    if (!x->gl_edit)
        draw_notify(x, s, argc, argv);
755
756
}

757
758
759
void canvas_show_scrollbars(t_canvas *x, t_floatarg f)
{
    x->gl_noscroll = (int)f;
760
761
    if (x->gl_mapped)
        gui_vmess("gui_canvas_set_scrollbars", "xi", x, (int)f);
762
763
764
765
766
767
768
}

void canvas_show_menu(t_canvas *x, t_floatarg f)
{
    x->gl_nomenu = (int)f;
}

769
770
extern void canvas_check_nlet_highlights(t_canvas *x);

771
772
773
/*********** dpsaha@vt.edu resize move hooks ****************/
void canvas_draw_gop_resize_hooks(t_canvas* x)
{
774
775
    t_scalehandle *sh = (t_scalehandle *)(x->x_handle);
    t_scalehandle *mh = (t_scalehandle *)(x->x_mhandle);
776
    //fprintf(stderr,"draw_gop_resize_hooks START\n");
777
778
779
780
781
782
    //in case we are an array which does not initialize its hooks
    if (!sh || !mh) return;
    if(x->gl_edit && glist_isvisible(x) && glist_istoplevel(x) &&
        x->gl_goprect && !x->gl_editor->e_selection)
    {
        //Drawing and Binding Resize_Blob for GOP
783
        //fprintf(stderr,"draw_gop_resize_hooks DRAW %lx %lx\n", (t_int)x, (t_int)glist_getcanvas(x));
784
785
        sprintf(sh->h_pathname, ".x%lx.h%lx", (t_int)x, (t_int)sh);
        sprintf(mh->h_pathname, ".x%lx.h%lx", (t_int)x, (t_int)mh);
786

787
788
789
790
791
792
793
        /* These are handled now in canvas_doclick */
        //scalehandle_draw_select(sh,
        //    -1-x->gl_obj.te_xpix+x->gl_xmargin + x->gl_pixwidth,
        //    -1-x->gl_obj.te_ypix+x->gl_ymargin + x->gl_pixheight/*,GOP_resblob*/);
        //scalehandle_draw_select(mh,
        //    2+SCALEHANDLE_WIDTH -x->gl_obj.te_xpix+x->gl_xmargin,
        //    2+SCALEHANDLE_HEIGHT-x->gl_obj.te_ypix+x->gl_ymargin /*,"GOP_movblob"*/);
794
795
796
797

        /* these constants don't actually reflect the actual size of the
           click rectangle-- we should probably change them... */

798
        scalehandle_draw_select(mh,
799
            SCALEHANDLE_WIDTH - 4, SCALEHANDLE_HEIGHT - 11);
800
801
802
    }
    else
    {
803
        //fprintf(stderr,"draw_gop_resize_hooks ERASE\n");
804
805
        scalehandle_draw_erase(sh);
        scalehandle_draw_erase(mh);
806
807
    }
    canvas_check_nlet_highlights(x);
808
809
810
}
/*****************************************************************************/

Miller Puckette's avatar
Miller Puckette committed
811
812
void canvas_drawredrect(t_canvas *x, int doit)
{
813
814
    if (doit)
    {
815
        int x1=x->gl_xmargin, y1=x->gl_ymargin + sys_legacy;
816
        int x2=x1+x->gl_pixwidth, y2=y1+x->gl_pixheight;
817
818
        gui_vmess("gui_canvas_drawredrect", "xiiii",
            glist_getcanvas(x),
Jonathan Wilkes's avatar
Jonathan Wilkes committed
819
            x1, y1, x2, y2);
820
821
822
823
        //dpsaha@vt.edu for drawing the GOP_blobs
        if (x->gl_goprect && x->gl_edit)
            canvas_draw_gop_resize_hooks(x);
    }
Jonathan Wilkes's avatar
Jonathan Wilkes committed
824
825
    else
    {
826
827
        gui_vmess("gui_canvas_deleteredrect", "x",
            glist_getcanvas(x));
Jonathan Wilkes's avatar
Jonathan Wilkes committed
828
    }
Miller Puckette's avatar
Miller Puckette committed
829
830
831
832
833
834
835
}

    /* the window becomes "mapped" (visible and not miniaturized) or
    "unmapped" (either miniaturized or just plain gone.)  This should be
    called from the GUI after the fact to "notify" us that we're mapped. */
void canvas_map(t_canvas *x, t_floatarg f)
{
836
    //fprintf(stderr,"canvas_map %lx %f\n", (t_int)x, f);
Miller Puckette's avatar
Miller Puckette committed
837
838
839
840
    int flag = (f != 0);
    t_gobj *y;
    if (flag)
    {
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
        if (!glist_isvisible(x)) {
            t_selection *sel;
            if (!x->gl_havewindow)
            {
                bug("canvas_map");
                canvas_vis(x, 1);
            }
            if (!x->gl_list) {
                //if there are no objects on the canvas
                canvas_create_editor(x);
            }
            else for (y = x->gl_list; y; y = y->g_next) {
                gobj_vis(y, x, 1);
            }
            if (x->gl_editor && x->gl_editor->e_selection)
                for (sel = x->gl_editor->e_selection; sel; sel = sel->sel_next)
                    gobj_select(sel->sel_what, x, 1);
            x->gl_mapped = 1;
            canvas_drawlines(x);
            if (x->gl_isgraph && x->gl_goprect)
                canvas_drawredrect(x, 1);
            scrollbar_update(x);
863
        }
Miller Puckette's avatar
Miller Puckette committed
864
865
866
    }
    else
    {
867
        //fprintf(stderr,"canvas_map 0\n");
Miller Puckette's avatar
Miller Puckette committed
868
869
        if (glist_isvisible(x))
        {
870
871
872
873
874
875
876
877
878
879
880
881
            /* Clear out scalars one by one. We need to do this
               in order to unbind the symbol we used to receive
               [draw] events from the GUI. (We could also just unbind
               here if this turns out to be a bottleneck... */
            t_gobj *y;
            for (y = x->gl_list; y; y = y->g_next)
            {
                if (pd_class(&y->g_pd) == scalar_class)
                    gobj_vis(y, x, 0);
            }
            /* now clear out the rest of the canvas in the GUI */
            gui_vmess("gui_canvas_erase_all_gobjs", "x", x);
Miller Puckette's avatar
Miller Puckette committed
882
883
884
885
886
887
888
            x->gl_mapped = 0;
        }
    }
}

void canvas_redraw(t_canvas *x)
{
889
890
    if (do_not_redraw) return;
    //fprintf(stderr,"canvas_redraw %lx\n", (t_int)x);
Miller Puckette's avatar
Miller Puckette committed
891
892
    if (glist_isvisible(x))
    {
893
        //fprintf(stderr,"canvas_redraw glist_isvisible=true\n");
Miller Puckette's avatar
Miller Puckette committed
894
895
        canvas_map(x, 0);
        canvas_map(x, 1);
896
897
898
899
900
901

        /* now re-highlight our selection */
        t_selection *y;
        if (x->gl_editor && x->gl_editor->e_selection)
            for (y = x->gl_editor->e_selection; y; y = y->sel_next)
                gobj_select(y->sel_what, x, 1);
Miller Puckette's avatar
Miller Puckette committed
902
903
904
905
906
907
908
    }
}

    /* we call this on a non-toplevel glist to "open" it into its
    own window. */
void glist_menu_open(t_glist *x)
{
909
910
911
    /* 20151230: moved to canvas_vis, so that scripted vis calls (e.g. via
       [send pd-abstraction-name.pd] do proper redraws of abstractions */
    /*if (glist_isvisible(x))
Miller Puckette's avatar
Miller Puckette committed
912
    {
913
914
915
916
        if (!glist_istoplevel(x))
        {
            t_glist *gl2 = x->gl_owner;
            if (!gl2) 
917
                bug("glist_menu_open"); // shouldn't happen but not dangerous
918
919
            else
            {
920
                // erase ourself in parent window
921
                gobj_vis(&x->gl_gobj, gl2, 0);
922
                // get rid of our editor (and subeditors)
923
924
925
                if (x->gl_editor)
                    canvas_destroy_editor(x);
                x->gl_havewindow = 1;
926
                // redraw ourself in parent window (blanked out this time)
927
928
929
930
931
                gobj_vis(&x->gl_gobj, gl2, 1);
            }
        }
        else
        {
932
            // Not sure if this needs to get ported... need to test
933
            //sys_vgui("focus .x%lx\n", (t_int)x);
934
935
936
937
        }
    }
    else
    {
938
        if (x->gl_editor)
939
940
        canvas_destroy_editor(x);
    }
941
    */
Miller Puckette's avatar
Miller Puckette committed
942
943
944
945
946
    canvas_vis(x, 1);
}

int glist_isvisible(t_glist *x)
{
947
    return ((!x->gl_loading) && ((x->gl_isgraph && glist_getcanvas(x)->gl_mapped) || (!x->gl_isgraph && x->gl_mapped)));
Miller Puckette's avatar
Miller Puckette committed
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
}

int glist_istoplevel(t_glist *x)
{
        /* we consider a graph "toplevel" if it has its own window
        or if it appears as a box in its parent window so that we
        don't draw the actual contents there. */
    return (x->gl_havewindow || !x->gl_isgraph);
}

int glist_getfont(t_glist *x)
{
    while (!x->gl_env)
        if (!(x = x->gl_owner))
            bug("t_canvasenvironment");
    return (x->gl_font);
}

966
967
extern void canvas_group_free(t_pd *x);

Miller Puckette's avatar
Miller Puckette committed
968
969
void canvas_free(t_canvas *x)
{
970
    //fprintf(stderr,"canvas_free %lx\n", (t_int)x);
Miller Puckette's avatar
Miller Puckette committed
971
972
    t_gobj *y;
    int dspstate = canvas_suspend_dsp();
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
973

974
    //canvas_noundo(x);
975
    canvas_undo_free(x);
976

Miller Puckette's avatar
Miller Puckette committed
977
978
979
980
981
    if (canvas_editing == x)
        canvas_editing = 0;
    if (canvas_whichfind == x)
        canvas_whichfind = 0;
    glist_noselect(x);
982
    x->gl_unloading = 1;
Miller Puckette's avatar
Miller Puckette committed
983
984
    while (y = x->gl_list)
        glist_delete(x, y);
Miller Puckette's avatar
Miller Puckette committed
985
986
    if (x == glist_getcanvas(x))
        canvas_vis(x, 0);
987
988
    if (x->gl_editor)
        canvas_destroy_editor(x);   /* bug workaround; should already be gone*/
989

990
    if (x->x_handle) scalehandle_free(x->x_handle);
991
    if (x->x_mhandle) scalehandle_free(x->x_mhandle);
992
    
993
    canvas_unbind(x);
Miller Puckette's avatar
Miller Puckette committed
994
995
996
997
998
    if (x->gl_env)
    {
        freebytes(x->gl_env->ce_argv, x->gl_env->ce_argc * sizeof(t_atom));
        freebytes(x->gl_env, sizeof(*x->gl_env));
    }
999
    canvas_resume_dsp(dspstate);
1000
1001
1002
    freebytes(x->gl_xlabel, x->gl_nxlabels * sizeof(*(x->gl_xlabel)));
    freebytes(x->gl_ylabel, x->gl_nylabels * sizeof(*(x->gl_ylabel)));
    gstub_cutoff(x->gl_stub);
Miller Puckette's avatar
Miller Puckette committed
1003
    gfxstub_deleteforkey(x);        /* probably unnecessary */
1004
    if (!x->gl_owner && !x->gl_isclone)
Miller Puckette's avatar
Miller Puckette committed
1005
        canvas_takeofflist(x);
1006
1007
    if (x->gl_svg)                   /* for groups, free the data */
        canvas_group_free(x->gl_svg);
Miller Puckette's avatar
Miller Puckette committed
1008
1009
1010
1011
1012
1013
1014
1015
}

/* ----------------- lines ---------- */

static void canvas_drawlines(t_canvas *x)
{
    t_linetraverser t;
    t_outconnect *oc;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1016
    int issignal;
1017
1018
    linetraverser_start(&t, x);
    while (oc = linetraverser_next(&t))
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1019
1020
    {
        issignal = (outlet_getsymbol(t.tr_outlet) == &s_signal ? 1 : 0);
1021
1022
1023
1024
        if (!(pd_class(&t.tr_ob2->ob_g.g_pd) == preset_node_class &&
              pd_class(&t.tr_ob->ob_g.g_pd) != message_class))
            canvas_drawconnection(glist_getcanvas(x), t.tr_lx1, t.tr_ly1,
                t.tr_lx2, t.tr_ly2, (t_int)oc, issignal);
Miller Puckette's avatar
Miller Puckette committed
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
    }
}

void canvas_fixlinesfor(t_canvas *x, t_text *text)
{
    t_linetraverser t;
    t_outconnect *oc;

    linetraverser_start(&t, x);
    while (oc = linetraverser_next(&t))
    {
        if (t.tr_ob == text || t.tr_ob2 == text)
        {
1038
1039
            canvas_updateconnection(x, t.tr_lx1, t.tr_ly1, t.tr_lx2, t.tr_ly2,
                (t_int)oc);
Miller Puckette's avatar
Miller Puckette committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
        }
    }
}

    /* kill all lines for the object */
void canvas_deletelinesfor(t_canvas *x, t_text *text)
{
    t_linetraverser t;
    t_outconnect *oc;
    linetraverser_start(&t, x);
    while (oc = linetraverser_next(&t))
    {
        if (t.tr_ob == text || t.tr_ob2 == text)
        {
1054
            if (x->gl_editor && glist_isvisible(glist_getcanvas(x)))
Miller Puckette's avatar
Miller Puckette committed
1055
            {
1056
1057
1058
1059
                /* Still don't see any place where this gets used. Maybe it's
                   used by older externals? Need to test... */
                //sys_vgui(".x%lx.c delete l%lx\n",
                //    glist_getcanvas(x), oc);
1060
                /* probably need a gui_vmess here */
Miller Puckette's avatar
Miller Puckette committed
1061
1062
1063
1064
1065
1066
            }
            obj_disconnect(t.tr_ob, t.tr_outno, t.tr_ob2, t.tr_inno);
        }
    }
}

1067
1068
    /*  delete all lines for the object 
        for efficient redrawing of connections */
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
void canvas_eraselinesfor(t_canvas *x, t_text *text)
{
    t_linetraverser t;
    t_outconnect *oc;
    linetraverser_start(&t, x);
    while (oc = linetraverser_next(&t))
    {
        if (t.tr_ob == text || t.tr_ob2 == text)
        {
            if (x->gl_editor)
            {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1080
1081
                char tagbuf[MAXPDSTRING];
                sprintf(tagbuf, "l%lx", (long unsigned int)oc);
1082
1083
                gui_vmess("gui_canvas_delete_line", "xs",
                    glist_getcanvas(x), tagbuf);
1084
1085
1086
1087
1088
1089
            }
        }
    }
}


Miller Puckette's avatar
Miller Puckette committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
    /* kill all lines for one inlet or outlet */
void canvas_deletelinesforio(t_canvas *x, t_text *text,
    t_inlet *inp, t_outlet *outp)
{
    t_linetraverser t;
    t_outconnect *oc;
    linetraverser_start(&t, x);
    while (oc = linetraverser_next(&t))
    {
        if ((t.tr_ob == text && t.tr_outlet == outp) ||
            (t.tr_ob2 == text && t.tr_inlet == inp))
        {
            if (x->gl_editor)
            {
1104
1105
1106
1107
1108
                char buf[MAXPDSTRING];
                sprintf(buf, "l%lx", (long unsigned int)oc);
                gui_vmess("gui_canvas_delete_line", "xs",
                    glist_getcanvas(x),
                    buf);
Miller Puckette's avatar
Miller Puckette committed
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
            }
            obj_disconnect(t.tr_ob, t.tr_outno, t.tr_ob2, t.tr_inno);
        }
    }
}

static void canvas_pop(t_canvas *x, t_floatarg fvis)
{
    if (fvis != 0)
        canvas_vis(x, 1);
    pd_popsym(&x->gl_pd);
    canvas_resortinlets(x);
    canvas_resortoutlets(x);
    x->gl_loading = 0;
1123
    //fprintf(stderr,"loading = 0 .x%lx owner=.x%lx\n", x, x->gl_owner);
Miller Puckette's avatar
Miller Puckette committed
1124
1125
}

1126
1127
1128
extern void *svg_new(t_pd *x, t_symbol *s, int argc, t_atom *argv);
extern t_pd *svg_header(t_pd *x);

1129
static void group_svginit(t_glist *gl, t_symbol *type, int argc, t_atom *argv)
1130
{
1131
    gl->gl_svg = (t_pd *)(svg_new((t_pd *)gl, type, argc, argv));
1132
1133
    t_pd *proxy = svg_header(gl->gl_svg);
    inlet_new(&gl->gl_obj, proxy, 0, 0);
1134
    outlet_new(&gl->gl_obj, &s_anything);
1135
1136
}

Miller Puckette's avatar
Miller Puckette committed
1137
1138
1139
1140
1141
void canvas_objfor(t_glist *gl, t_text *x, int argc, t_atom *argv);

void canvas_restore(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
{
    t_pd *z;
1142
    int is_draw_command = 0;
1143
    //fprintf(stderr,"canvas_restore %lx\n", x);
1144
1145
    /* for [draw g] and [draw svg] we add an inlet to the svg attr proxy */
    if (atom_getsymbolarg(2, argc, argv) == gensym("draw"))
1146
    {
1147
1148
1149
1150
1151
        t_symbol *type = (atom_getsymbolarg(3, argc, argv) == gensym("svg")) ?
            gensym("svg") : gensym("g");
        group_svginit(x, type,
            (type == gensym("svg") && argc > 4) ? argc-4 : 0,
            (type == gensym("svg") && argc > 4) ? argv+4 : 0);
1152
        is_draw_command = 1;
1153
    }
1154
    if (argc > 3 || (is_draw_command && argc > 4))
Miller Puckette's avatar
Miller Puckette committed
1155
    {
1156
1157
        int offset = is_draw_command ? 4 : 3;
        t_atom *ap=argv+offset;
Miller Puckette's avatar
Miller Puckette committed
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
        if (ap->a_type == A_SYMBOL)
        {
            t_canvasenvironment *e = canvas_getenv(canvas_getcurrent());
            canvas_rename(x, binbuf_realizedollsym(ap->a_w.w_symbol,
                e->ce_argc, e->ce_argv, 1), 0);
        }
    }
    canvas_pop(x, x->gl_willvis);

    if (!(z = gensym("#X")->s_thing)) error("canvas_restore: out of context");
    else if (*z != canvas_class) error("canvas_restore: wasn't a canvas");
    else
    {
        t_canvas *x2 = (t_canvas *)z;
        x->gl_owner = x2;
        canvas_objfor(x2, &x->gl_obj, argc, argv);
    }
}

1177
void canvas_loadbangsubpatches(t_canvas *x, t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
1178
1179
{
    t_gobj *y;
1180
    //t_symbol *s = gensym("loadbang");
Miller Puckette's avatar
Miller Puckette committed
1181
1182
    for (y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == canvas_class)
1183
1184
1185
1186
1187
        {
            if (!canvas_isabstraction((t_canvas *)y))
            {
            //fprintf(stderr,"%lx s:canvas_loadbangsubpatches %s\n",
            //    x, s->s_name);
1188
            canvas_loadbangsubpatches((t_canvas *)y, s);
1189
1190
            }
        }
1191
1192
    for (y = x->gl_list; y; y = y->g_next)
        if ((pd_class(&y->g_pd) != canvas_class) &&
1193
1194
1195
            zgetfn(&y->g_pd, s))
        {
            //fprintf(stderr,"%lx s:obj_loadbang %s\n",x,s->s_name);
1196
            pd_vmess(&y->g_pd, s, "f", (t_floatarg)LB_LOAD);
1197
        }
Miller Puckette's avatar
Miller Puckette committed
1198
1199
}

1200
static void canvas_loadbangabstractions(t_canvas *x, t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
1201
1202
{
    t_gobj *y;
1203
    //t_symbol *s = gensym("loadbang");
Miller Puckette's avatar
Miller Puckette committed
1204
1205
    for (y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == canvas_class)
1206
1207
1208
        {
            if (canvas_isabstraction((t_canvas *)y))
            {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1209
1210
1211
                //fprintf(stderr,"%lx a:canvas_loadbang %s\n",x,s->s_name);
                canvas_loadbangabstractions((t_canvas *)y, s);
                canvas_loadbangsubpatches((t_canvas *)y, s);
1212
1213
1214
1215
1216
1217
1218
1219
            }
            else
            {
                //fprintf(stderr,"%lx a:canvas_loadbangabstractions %s\n",
                //    x, s->s_name);
                canvas_loadbangabstractions((t_canvas *)y, s);
            }
        }
Miller Puckette's avatar
Miller Puckette committed
1220
1221
1222
1223
}

void canvas_loadbang(t_canvas *x)
{
1224
    //t_gobj *y;
1225
1226
    // first loadbang preset hubs and nodes
    //fprintf(stderr,"%lx 0\n", x);
1227
1228
    canvas_loadbangabstractions(x, gensym("pre-loadbang"));
    canvas_loadbangsubpatches(x, gensym("pre-loadbang"));
1229
1230
    //fprintf(stderr,"%lx 1\n", x);
    // then do the regular loadbang
1231
1232
    canvas_loadbangabstractions(x, gensym("loadbang"));
    canvas_loadbangsubpatches(x, gensym("loadbang"));
1233
    //fprintf(stderr,"%lx 2\n", x);
Miller Puckette's avatar
Miller Puckette committed
1234
}
1235

1236
1237
1238
1239
1240
1241
/* JMZ/MSP:
 * initbang is emitted after a canvas is read from a file, but before the
   parent canvas is finished loading.  This is apparently used so that
   abstractions can create inlets/outlets as a function of creation arguments.
   This practice is quite ugly but there's no other way to do it so far.
*/
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1242
1243
1244
void canvas_initbang(t_canvas *x)
{
    t_gobj *y;
1245
    t_symbol *s = gensym("loadbang");
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1246
1247
    /* run "initbang" for all subpatches, but NOT for the child abstractions */
    for (y = x->gl_list; y; y = y->g_next)
1248
1249
1250
        if (pd_class(&y->g_pd) == canvas_class &&
            !canvas_isabstraction((t_canvas *)y))
                canvas_initbang((t_canvas *)y);
Miller Puckette's avatar
Miller Puckette committed
1251

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1252
1253
1254
    /* call the initbang()-method for objects that have one */
    for (y = x->gl_list; y; y = y->g_next)
        if ((pd_class(&y->g_pd) != canvas_class) && zgetfn(&y->g_pd, s))
1255
            pd_vmess(&y->g_pd, s, "f", (t_floatarg)LB_INIT);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1256
}
1257

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1258
1259
1260
1261
1262
1263
1264
/* JMZ:
 * closebang is emitted before the canvas is destroyed
 * and BEFORE subpatches/abstractions in this canvas are destroyed
 */
void canvas_closebang(t_canvas *x)
{
    t_gobj *y;
1265
    t_symbol *s = gensym("loadbang");
Miller Puckette's avatar
Miller Puckette committed
1266

1267
    /* call the closebang()-method for objects that have one
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1268
1269
1270
1271
1272
     * but NOT for subpatches/abstractions: these are called separately
     * from g_graph:glist_delete()
     */
    for (y = x->gl_list; y; y = y->g_next)
        if ((pd_class(&y->g_pd) != canvas_class) && zgetfn(&y->g_pd, s))
1273
            pd_vmess(&y->g_pd, s, "f", (t_floatarg)LB_CLOSE);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1274
}
Miller Puckette's avatar
Miller Puckette committed
1275

1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
// we use this function to check if the canvas that has sent out the <config>
// signal, meaning it has been resized, if we have scalars in there and
// the canvas has gop enabled, we need to redraw the window to make sure
// scalars scale with the window
void canvas_checkconfig(t_canvas *x)
{
    //fprintf(stderr,"canvas_checkconfig\n");
    t_gobj *y;
    if (x->gl_isgraph)
    {
        for (y = x->gl_list; y; y = y->g_next)
        {
            if (pd_class(&y->g_pd) == scalar_class)
            {
                //fprintf(stderr,"...redrawing\n");
                canvas_redraw(x);
                break;
            }
        }
    }
}

1298
1299
/* needed for readjustment of garrays */
extern t_array *garray_getarray(t_garray *x);
1300
extern void garray_fittograph(t_garray *x, int n, int flag);
1301
1302
1303
extern t_rtext *glist_findrtext(t_glist *gl, t_text *who);
extern void rtext_gettext(t_rtext *x, char **buf, int *bufsize);

Miller Puckette's avatar
Miller Puckette committed
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
static void canvas_relocate(t_canvas *x, t_symbol *canvasgeom,
    t_symbol *topgeom)
{
    int cxpix, cypix, cw, ch, txpix, typix, tw, th;
    if (sscanf(canvasgeom->s_name, "%dx%d+%d+%d", &cw, &ch, &cxpix, &cypix)
        < 4 ||
        sscanf(topgeom->s_name, "%dx%d+%d+%d", &