g_canvas.c 66.8 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
17
#include <string.h>

18
extern int do_not_redraw;
Miller Puckette's avatar
Miller Puckette committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

    /* 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 */ 
t_canvas *canvas_list;              /* list of all root canvases */

/* ------------------ forward function declarations --------------- */
static void canvas_start_dsp(void);
static void canvas_stop_dsp(void);
static void canvas_drawlines(t_canvas *x);
52
void canvas_setbounds(t_canvas *x, int x1, int y1, int x2, int y2);
53
void canvas_reflecttitle(t_canvas *x);
Miller Puckette's avatar
Miller Puckette committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
static void canvas_addtolist(t_canvas *x);
static void canvas_takeofflist(t_canvas *x);
static void canvas_pop(t_canvas *x, t_floatarg fvis);

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

static void glist_doupdatewindowlist(t_glist *gl, char *sbuf)
{
    t_gobj *g;
68
69
    if (glist_amreloadingabstractions)  /* not if we're in a reload */
        return;
Miller Puckette's avatar
Miller Puckette committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    if (!gl->gl_owner)
    {
        /* this is a canvas; if we have a window, put on "windows" list */
        t_canvas *canvas = (t_canvas *)gl;
        if (canvas->gl_havewindow)
        {
            if (strlen(sbuf) + strlen(gl->gl_name->s_name) + 100 <= 1024)
            {
                char tbuf[1024];
                sprintf(tbuf, "{{%s} .x%lx} ", gl->gl_name->s_name,
                    (t_int)canvas);
                strcat(sbuf, tbuf);
            }
        }
    }
    for (g = gl->gl_list; g; g = g->g_next)
    {
        if (pd_class(&g->g_pd) == canvas_class)
            glist_doupdatewindowlist((t_glist *)g, sbuf);
    }
    return;
}

    /* maintain the list of visible toplevels for the GUI's "windows" menu */
void canvas_updatewindowlist( void)
{
    t_canvas *x;
    char sbuf[1024];
    strcpy(sbuf, "set menu_windowlist {");
        /* find all root canvases */
    for (x = canvas_list; x; x = x->gl_next)
        glist_doupdatewindowlist(x, sbuf);
    /* next line updates the window menu state before -postcommand tries it */
    strcat(sbuf, "}\npdtk_fixwindowmenu\n");
    sys_gui(sbuf);
}

    /* add a glist the list of "root" canvases (toplevels without parents.) */
static void canvas_addtolist(t_canvas *x)
{
    x->gl_next = canvas_list;
    canvas_list = x;
}

static void canvas_takeofflist(t_canvas *x)
{
        /* take it off the window list */
    if (x == canvas_list) canvas_list = x->gl_next;
    else
    {
        t_canvas *z;
        for (z = canvas_list; z->gl_next != x; z = z->gl_next)
            ;
        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
165
            bug("t_canvasenvironment");
Miller Puckette's avatar
Miller Puckette committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
    return (x->gl_env);
}

int canvas_getdollarzero( void)
{
    t_canvas *x = canvas_getcurrent();
    t_canvasenvironment *env = (x ? canvas_getenv(x) : 0);
    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;
}

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)
{
    char *dir = canvas_getenv(x)->ce_dir->s_name;
    if (file[0] == '/' || (file[0] && file[1] == ':') || !*dir)
    {
        strncpy(result, file, resultsize);
        result[resultsize-1] = 0;
    }
    else
    {
        int nleft;
        strncpy(result, dir, resultsize);
        result[resultsize-1] = 0;
        nleft = resultsize - strlen(result) - 1;
        if (nleft <= 0) return;
        strcat(result, "/");
        strncat(result, file, nleft);
        result[resultsize-1] = 0;
    }           
}

void canvas_rename(t_canvas *x, t_symbol *s, t_symbol *dir)
{
    if (strcmp(x->gl_name->s_name, "Pd"))
        pd_unbind(&x->gl_pd, canvas_makebindsym(x->gl_name));
    x->gl_name = s;
    if (strcmp(x->gl_name->s_name, "Pd"))
        pd_bind(&x->gl_pd, canvas_makebindsym(x->gl_name));
    if (glist_isvisible(x))
        canvas_reflecttitle(x);
    if (dir && dir != &s_)
    {
        t_canvasenvironment *e = canvas_getenv(x);
        e->ce_dir = dir;
    }
}

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

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);
}

    /* make a new glist.  It will either be a "root" canvas or else
343
    it appears as a "text" object in another window (canvas_getcurrent() 
Miller Puckette's avatar
Miller Puckette committed
344
345
346
347
348
349
350
351
352
    tells us which.) */
t_canvas *canvas_new(void *dummy, t_symbol *sel, int argc, t_atom *argv)
{
    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);
353

Miller Puckette's avatar
Miller Puckette committed
354
    glist_init(x);
355
    x->gl_magic_glass = magicGlass_new(x);
Miller Puckette's avatar
Miller Puckette committed
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
402
403
404
405
406
407
408
409
410
411
412
    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 */
    {
        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);
    }
    else if (argc == 6)  /* subwindow: x, y, w, h, name, vis */
    {
        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);
    }
        /* (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;
    canvas_setbounds(x, xloc, yloc, xloc + width, yloc + height);
    x->gl_owner = owner;
    x->gl_name = (*s->s_name ? s : 
        (canvas_newfilename ? canvas_newfilename : gensym("Pd")));
    if (strcmp(x->gl_name->s_name, "Pd"))
        pd_bind(&x->gl_pd, canvas_makebindsym(x->gl_name));
    x->gl_loading = 1;
413
	//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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    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);
    pd_pushsym(&x->gl_pd);
431

432
433
	//dpsaha@vt.edu gop resize

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
461
462
	//resize blob	
	t_scalehandle *sh;
	char buf[64];
	x->x_handle = pd_new(scalehandle_class);
	sh = (t_scalehandle *)x->x_handle;
	sh->h_master = (t_gobj*)x;
	sprintf(buf, "_h%lx", (t_int)sh);
	pd_bind(x->x_handle, sh->h_bindsym = gensym(buf));
	sprintf(sh->h_outlinetag, "h%lx", (t_int)sh);
	sh->h_dragon = 0;
	sh->h_scale = 1;
	x->scale_offset_x = 0;
	x->scale_offset_y = 0;
	x->scale_vis = 0;
	
	//move blob	
	t_scalehandle *mh;
	char mbuf[64];
	x->x_mhandle = pd_new(scalehandle_class);
	mh = (t_scalehandle *)x->x_mhandle;
	mh->h_master = (t_gobj*)x;
	sprintf(mbuf, "_h%lx", (t_int)mh);
	pd_bind(x->x_mhandle, mh->h_bindsym = gensym(mbuf));
	sprintf(mh->h_outlinetag, "h%lx", (t_int)mh);
	mh->h_dragon = 0;
	mh->h_scale = 0;
	x->move_offset_x = 0;
	x->move_offset_y = 0;
	x->move_vis = 0;
463

464
465
466
	x->canvas_cnct_inlet_tag[0] = 0;
	x->canvas_cnct_outlet_tag[0] = 0;

467
	x->u_queue = canvas_undo_init(x);
468

Miller Puckette's avatar
Miller Puckette committed
469
470
471
472
473
474
475
    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)
{
476
477
478
	//IB: first delete the graph in case we are downsizing the object size via script
	canvas_setgraph(x, 0, 0);

Miller Puckette's avatar
Miller Puckette committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    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,
498
499
    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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
{
    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)
    {
524
        t_float zz;
Miller Puckette's avatar
Miller Puckette committed
525
526
527
528
529
530
531
532
533
        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;
534
535
	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
536
537
538
    if (px1 >= px2 || py1 >= py2)
        px1 = 100, py1 = 20, px2 = 100 + GLIST_DEFGRAPHWIDTH,
            py2 = 20 + GLIST_DEFGRAPHHEIGHT;
539

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
    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);
    x->gl_screenx1 = x->gl_screeny1 = 0;
    x->gl_screenx2 = 450;
    x->gl_screeny2 = 300;
    if (strcmp(x->gl_name->s_name, "Pd"))
        pd_bind(&x->gl_pd, canvas_makebindsym(x->gl_name));
    x->gl_owner = g;
    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);
564
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (long unsigned int)glist_getcanvas(g));
Miller Puckette's avatar
Miller Puckette committed
565
566
567
568
569
570
    return (x);
}

    /* call glist_addglist from a Pd message */
void glist_glist(t_glist *g, t_symbol *s, int argc, t_atom *argv)
{
571
	pd_vmess(&g->gl_pd, gensym("editmode"), "i", 1);
572
573
574
    t_symbol *sym = atom_getsymbolarg(0, argc, argv);
	/* 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_;  
575
576
577
578
579
580
581
582
    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
583
584
585
586
587
588
589
590
591
592
593
594
    glist_addglist(g, sym, x1, y1, x2, y2, px1, py1, px2, py2);
}

    /* 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)
{
  return (x->gl_isgraph|(x->gl_hidetext<<1));
}

    /* This is sent from the GUI to inform a toplevel that its window has been
    moved or resized. */
595
void canvas_setbounds(t_canvas *x, int x1, int y1, int x2, int y2)
Miller Puckette's avatar
Miller Puckette committed
596
{
597
598
	//fprintf(stderr,"canvas_setbounds %d %d %d %d\n", x1, y1, x2, y2);

Miller Puckette's avatar
Miller Puckette committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
    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. */
614
        t_float diff = x->gl_y1 - x->gl_y2;
Miller Puckette's avatar
Miller Puckette committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
        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);
    }
}

t_symbol *canvas_makebindsym(t_symbol *s)
{
    char buf[MAXPDSTRING];
    strcpy(buf, "pd-");
    strcat(buf, s->s_name);
    return (gensym(buf));
}

void canvas_reflecttitle(t_canvas *x)
{
637
	//fprintf(stderr,"canvas_reflecttitle\n");
Miller Puckette's avatar
Miller Puckette committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
    char namebuf[MAXPDSTRING];
    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;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
656
657
658
659
660
#ifdef __APPLE__
    sys_vgui("wm attributes .x%lx -modified %d -titlepath {%s/%s}\n",
        x, x->gl_dirty, canvas_getdir(x)->s_name, x->gl_name->s_name);
    sys_vgui("wm title .x%lx {%s%s}\n", x, x->gl_name->s_name, namebuf);
#else
661
	//if(glist_havewindow(x) || !x->gl_isgraph || x->gl_isgraph && x->gl_havewindow || x->gl_loading || x->gl_dirty) {
662
663
664
		/*fprintf(stderr,"%d %d %d %d %d\n", glist_istoplevel(x), !x->gl_isgraph,
			x->gl_isgraph && x->gl_havewindow, x->gl_loading,
			x->gl_dirty);*/
665
	sys_vgui("wm title .x%lx {%s%c%s - %s}\n", 
666
	        x, x->gl_name->s_name, (x->gl_dirty? '*' : ' '), namebuf,
Miller Puckette's avatar
Miller Puckette committed
667
            canvas_getdir(x)->s_name);
668
	//}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
669
#endif
Miller Puckette's avatar
Miller Puckette committed
670
671
}

Miller Puckette's avatar
Miller Puckette committed
672
673
    /* mark a glist dirty or clean */
void canvas_dirty(t_canvas *x, t_floatarg n)
Miller Puckette's avatar
Miller Puckette committed
674
675
{
    t_canvas *x2 = canvas_getrootfor(x);
676
677
    if (glist_amreloadingabstractions)
        return;
Miller Puckette's avatar
Miller Puckette committed
678
679
680
    if ((unsigned)n != x2->gl_dirty)
    {
        x2->gl_dirty = n;
Miller Puckette's avatar
test7    
Miller Puckette committed
681
682
        if (glist_isvisible(x2))
            canvas_reflecttitle(x2);
Miller Puckette's avatar
Miller Puckette committed
683
684
685
    }
}

686
687
extern void canvas_check_nlet_highlights(t_canvas *x);

688
689
690
/*********** dpsaha@vt.edu resize move hooks ****************/
void canvas_draw_gop_resize_hooks(t_canvas* x)
{
691
692
693
	t_scalehandle *sh = (t_scalehandle *)(x->x_handle);
	t_scalehandle *mh = (t_scalehandle *)(x->x_mhandle);

694
	if(x->gl_edit && glist_isvisible(x) && glist_istoplevel(x) && x->gl_goprect && !x->gl_editor->e_selection) {
695
696
697
		
		//Drawing and Binding Resize_Blob for GOP
		sprintf(sh->h_pathname, ".x%lx.h%lx", (t_int)x, (t_int)sh);
698
699
		sys_vgui("destroy %s\n", sh->h_pathname);	
		sys_vgui(".x%lx.c delete GOP_resblob\n", x);	
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
		sys_vgui("canvas %s -width %d -height %d -bg $select_color -bd 0 -cursor bottom_right_corner\n",
				 sh->h_pathname, SCALEHANDLE_WIDTH, SCALEHANDLE_HEIGHT);
		sys_vgui(".x%x.c create window %d %d -anchor nw -width %d -height %d -window %s -tags {%lxSCALE %lxGOP GOP_resblob}\n",
				 x, x->gl_xmargin + x->gl_pixwidth - SCALEHANDLE_WIDTH - 1,
				 x->gl_ymargin + 3 + x->gl_pixheight - SCALEHANDLE_HEIGHT - 4,
				 SCALEHANDLE_WIDTH, SCALEHANDLE_HEIGHT,
				 sh->h_pathname, x, x);
		
		sys_vgui("bind %s <Button> {pd [concat %s _click 1 %%x %%y \\;]}\n",
				 sh->h_pathname, sh->h_bindsym->s_name);
		sys_vgui("bind %s <ButtonRelease> {pd [concat %s _click 0 0 0 \\;]}\n",
				 sh->h_pathname, sh->h_bindsym->s_name);
		sys_vgui("bind %s <Motion> {pd [concat %s _motion %%x %%y \\;]}\n",
				 sh->h_pathname, sh->h_bindsym->s_name);

		//Drawing and Binding Move_Blob for GOP
		sprintf(mh->h_pathname, ".x%lx.h%lx", (t_int)x, (t_int)mh);
717
718
		sys_vgui("destroy %s\n", mh->h_pathname);
		sys_vgui(".x%lx.c delete GOP_movblob\n", x);	
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
		sys_vgui("canvas %s -width %d -height %d -bg $select_color -bd 0 -cursor crosshair\n",
				 mh->h_pathname, SCALEHANDLE_WIDTH, SCALEHANDLE_HEIGHT);
		sys_vgui(".x%x.c create window %d %d -anchor nw -width %d -height %d -window %s -tags {%lxMOVE %lxGOP GOP_movblob}\n",
				 x, x->gl_xmargin + 2 ,
				 x->gl_ymargin + 2 ,
				 SCALEHANDLE_WIDTH, SCALEHANDLE_HEIGHT,
				 mh->h_pathname, x, x);
		
		sys_vgui("bind %s <Button> {pd [concat %s _click 1 %%x %%y \\;]}\n",
				 mh->h_pathname, mh->h_bindsym->s_name);
		sys_vgui("bind %s <ButtonRelease> {pd [concat %s _click 0 0 0 \\;]}\n",
				 mh->h_pathname, mh->h_bindsym->s_name);
		sys_vgui("bind %s <Motion> {pd [concat %s _motion %%x %%y \\;]}\n",
				 mh->h_pathname, mh->h_bindsym->s_name);

	}
	else{
736
737
738
739
		if (sh)
			sys_vgui("destroy %s\n", sh->h_pathname);
		if (mh)
			sys_vgui("destroy %s\n", mh->h_pathname);
740
		sys_vgui(".x%lx.c delete GOP_resblob ; .x%lx.c delete GOP_movblob ;\n", x, x);					//delete the GOP_resblob and GOP_movblob	
741
	}
742
	canvas_check_nlet_highlights(x);
743
744
745
}
/*****************************************************************************/

Miller Puckette's avatar
Miller Puckette committed
746
747
void canvas_drawredrect(t_canvas *x, int doit)
{
748
    if (doit){
Miller Puckette's avatar
Miller Puckette committed
749
750
751
752
753
754
755
756
        sys_vgui(".x%lx.c create line\
            %d %d %d %d %d %d %d %d %d %d -fill #ff8080 -tags GOP\n",
            glist_getcanvas(x),
            x->gl_xmargin, x->gl_ymargin,
            x->gl_xmargin + x->gl_pixwidth, x->gl_ymargin,
            x->gl_xmargin + x->gl_pixwidth, x->gl_ymargin + x->gl_pixheight,
            x->gl_xmargin, x->gl_ymargin + x->gl_pixheight,
            x->gl_xmargin, x->gl_ymargin);
757
758
759
760
		if (x->gl_goprect && x->gl_edit){
				canvas_draw_gop_resize_hooks(x);					//dpsaha@vt.edu for drawing the GOP_blobs
		}	
	}
Miller Puckette's avatar
Miller Puckette committed
761
762
763
764
765
766
767
768
    else sys_vgui(".x%lx.c delete GOP\n",  glist_getcanvas(x));
}

    /* 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)
{
769
	//fprintf(stderr,"canvas_map\n");
Miller Puckette's avatar
Miller Puckette committed
770
771
772
773
    int flag = (f != 0);
    t_gobj *y;
    if (flag)
    {
774
775
776
777
		//fprintf(stderr,"canvas_map 1\n");
        //if (!glist_isvisible(x))
        //{
			//fprintf(stderr,"canvas_map 1 isvisible\n");
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
        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);
        sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x);
800
        //}
Miller Puckette's avatar
Miller Puckette committed
801
802
803
    }
    else
    {
804
		//fprintf(stderr,"canvas_map 0\n");
Miller Puckette's avatar
Miller Puckette committed
805
806
        if (glist_isvisible(x))
        {
807
            /* just clear out the whole canvas */
808
			sys_vgui(".x%lx.c dtag all selected\n", x);
Miller Puckette's avatar
Miller Puckette committed
809
810
811
812
813
814
815
816
            sys_vgui(".x%lx.c delete all\n", x);
            x->gl_mapped = 0;
        }
    }
}

void canvas_redraw(t_canvas *x)
{
817
	if (do_not_redraw) return;
818
	//fprintf(stderr,"canvas_redraw\n");
Miller Puckette's avatar
Miller Puckette committed
819
820
    if (glist_isvisible(x))
    {
821
		//fprintf(stderr,"canvas_redraw glist_isvisible=true\n");
Miller Puckette's avatar
Miller Puckette committed
822
823
        canvas_map(x, 0);
        canvas_map(x, 1);
824
825
826
		
		/* now re-highlight our selection */
	    t_selection *y;
827
828
829
		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
830
831
832
833
834
835
836
837
    }
}


    /* we call this on a non-toplevel glist to "open" it into its
    own window. */
void glist_menu_open(t_glist *x)
{
838
    if (glist_isvisible(x))
Miller Puckette's avatar
Miller Puckette committed
839
    {
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
		if (!glist_istoplevel(x)) {
		    t_glist *gl2 = x->gl_owner;
		    if (!gl2) 
		        bug("glist_menu_open");  /* shouldn't happen but not dangerous */
		    else
		    {
		            /* erase ourself in parent window */
		        gobj_vis(&x->gl_gobj, gl2, 0);
		                /* get rid of our editor (and subeditors) */
		        if (x->gl_editor)
		            canvas_destroy_editor(x);
		        x->gl_havewindow = 1;
		                /* redraw ourself in parent window (blanked out this time) */
		        gobj_vis(&x->gl_gobj, gl2, 1);
		    }
		} else {
			sys_vgui("focus .x%lx\n", (t_int)x);
		}
858
859
860
861
    } else {
        if (x->gl_editor)
            canvas_destroy_editor(x);
	}
Miller Puckette's avatar
Miller Puckette committed
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
    canvas_vis(x, 1);
}

int glist_isvisible(t_glist *x)
{
    return ((!x->gl_loading) && glist_getcanvas(x)->gl_mapped);
}

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);
}

void canvas_free(t_canvas *x)
{
888
	//fprintf(stderr,"canvas_free %lx\n", x);
Miller Puckette's avatar
Miller Puckette committed
889
890
    t_gobj *y;
    int dspstate = canvas_suspend_dsp();
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
891

892
893
894
    if (x->gl_magic_glass) {
      	//magicGlass_free(x->gl_magic_glass);
		pd_free(&x->gl_magic_glass->x_obj.te_g.g_pd);
895
		x->gl_magic_glass = NULL;
896
	}
897

898
    //canvas_noundo(x);
899
900
	canvas_undo_free(x);

Miller Puckette's avatar
Miller Puckette committed
901
902
903
904
905
906
907
    if (canvas_editing == x)
        canvas_editing = 0;
    if (canvas_whichfind == x)
        canvas_whichfind = 0;
    glist_noselect(x);
    while (y = x->gl_list)
        glist_delete(x, y);
Miller Puckette's avatar
Miller Puckette committed
908
909
    if (x == glist_getcanvas(x))
        canvas_vis(x, 0);
Miller Puckette's avatar
Miller Puckette committed
910

911
912
913
	if (x->gl_editor)
		canvas_destroy_editor(x);

914
915
    if (strcmp(x->gl_name->s_name, "Pd")) {
		//fprintf(stderr,"canvas_free calling pd_unbind\n");
Miller Puckette's avatar
Miller Puckette committed
916
        pd_unbind(&x->gl_pd, canvas_makebindsym(x->gl_name));
917
	}
Miller Puckette's avatar
Miller Puckette committed
918
919
920
921
922
    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));
    }
923
    canvas_resume_dsp(dspstate);
Miller Puckette's avatar
Miller Puckette committed
924
925
926
927
928
929
930
931
932
933
934
935
    glist_cleanup(x);
    gfxstub_deleteforkey(x);        /* probably unnecessary */
    if (!x->gl_owner)
        canvas_takeofflist(x);
}

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

static void canvas_drawlines(t_canvas *x)
{
    t_linetraverser t;
    t_outconnect *oc;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
936
937
    int issignal;
    
Miller Puckette's avatar
Miller Puckette committed
938
939
        linetraverser_start(&t, x);
        while (oc = linetraverser_next(&t))
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
940
941
    {
        issignal = (outlet_getsymbol(t.tr_outlet) == &s_signal ? 1 : 0);
942
943
944
945
946
947
		if (!(pd_class(&t.tr_ob2->ob_g.g_pd) == preset_node_class && pd_class(&t.tr_ob->ob_g.g_pd) != message_class))
		    sys_vgui(".x%lx.c create line %d %d %d %d -width %s -fill %s \
	-tags {l%lx all_cords}\n",
		             glist_getcanvas(x), t.tr_lx1, t.tr_ly1, t.tr_lx2, t.tr_ly2, 
		             (issignal ? "$signal_cord_width" : "$msg_cord_width"), (issignal ? "$signal_cord" : "$msg_cord"),
		             oc);
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
966
967
968
969
970
971
972
973
974
975
976
977
    }
}

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)
        {
            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);
        }
    }
}

    /* 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)
        {
978
            if (x->gl_editor && glist_isvisible(glist_getcanvas(x)))
Miller Puckette's avatar
Miller Puckette committed
979
980
981
982
983
984
985
986
987
            {
                sys_vgui(".x%lx.c delete l%lx\n",
                    glist_getcanvas(x), oc);
            }
            obj_disconnect(t.tr_ob, t.tr_outno, t.tr_ob2, t.tr_inno);
        }
    }
}

988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
    /* 	delete all lines for the object 
		for efficient redrawing of connections */
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)
            {
                sys_vgui(".x%lx.c delete l%lx\n",
                    glist_getcanvas(x), oc);
            }
        }
    }
}


Miller Puckette's avatar
Miller Puckette committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
    /* 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)
            {
                sys_vgui(".x%lx.c delete l%lx\n",
                    glist_getcanvas(x), oc);
            }
            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;
Ivica Bukvic's avatar
Ivica Bukvic committed
1039
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x);
1040
	//fprintf(stderr,"loading = 0 .x%lx owner=.x%lx\n", x, x->gl_owner);
Miller Puckette's avatar
Miller Puckette committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
}

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;
    if (argc > 3)
    {
        t_atom *ap=argv+3;
        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);
    }
}

1071
static void canvas_loadbangabstractions(t_canvas *x, t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
1072
1073
{
    t_gobj *y;
1074
    //t_symbol *s = gensym("loadbang");
Miller Puckette's avatar
Miller Puckette committed
1075
1076
1077
1078
1079
1080
    for (y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == canvas_class)
    {
        if (canvas_isabstraction((t_canvas *)y))
            canvas_loadbang((t_canvas *)y);
        else
1081
            canvas_loadbangabstractions((t_canvas *)y, s);
Miller Puckette's avatar
Miller Puckette committed
1082
1083
1084
    }
}

1085
void canvas_loadbangsubpatches(t_canvas *x, t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
1086
1087
{
    t_gobj *y;
1088
    //t_symbol *s = gensym("loadbang");
Miller Puckette's avatar
Miller Puckette committed
1089
1090
1091
1092
    for (y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == canvas_class)
    {
        if (!canvas_isabstraction((t_canvas *)y))
1093
            canvas_loadbangsubpatches((t_canvas *)y, s);
Miller Puckette's avatar
Miller Puckette committed
1094
1095
1096
1097
1098
1099
1100
1101
1102
    }
    for (y = x->gl_list; y; y = y->g_next)
        if ((pd_class(&y->g_pd) != canvas_class) &&
            zgetfn(&y->g_pd, s))
                pd_vmess(&y->g_pd, s, "");
}

void canvas_loadbang(t_canvas *x)
{
1103
1104
1105
1106
1107
1108
1109
1110
    //t_gobj *y;
	// first loadbang preset hubs and nodes
    canvas_loadbangabstractions(x, gensym("pre-loadbang"));
    canvas_loadbangsubpatches(x, gensym("pre-loadbang"));

	// then do the regular loadbang
    canvas_loadbangabstractions(x, gensym("loadbang"));
    canvas_loadbangsubpatches(x, gensym("loadbang"));
Miller Puckette's avatar
Miller Puckette committed
1111
}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
/* JMZ:
 * initbang is emitted after the canvas is done, but before the parent canvas is done
 * therefore, initbangs cannot reach to the outlets
 */
void canvas_initbang(t_canvas *x)
{
    t_gobj *y;
    t_symbol *s = gensym("initbang");
    /* run "initbang" for all subpatches, but NOT for the child abstractions */
    for (y = x->gl_list; y; y = y->g_next)
      if (pd_class(&y->g_pd) == canvas_class)
        {
          if (!canvas_isabstraction((t_canvas *)y))
            canvas_initbang((t_canvas *)y);
        }
Miller Puckette's avatar
Miller Puckette committed
1127

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
    /* 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))
          {
            pd_vmess(&y->g_pd, s, "");
          }
      }
}
/* 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;
    t_symbol *s = gensym("closebang");
Miller Puckette's avatar
Miller Puckette committed
1145

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
    /* call the closebang()-method for objects that have one 
     * 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))
          {
            pd_vmess(&y->g_pd, s, "");
          }
      }
}
Miller Puckette's avatar
Miller Puckette committed
1158

1159
1160
1161
1162
1163
1164
/* needed for readjustment of garrays */
extern t_array *garray_getarray(t_garray *x);
extern void garray_fittograph(t_garray *x, int n);
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
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
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", &tw, &th, &txpix, &typix) < 4)
        bug("canvas_relocate");
            /* for some reason this is initially called with cw=ch=1 so
            we just suppress that here. */
    if (cw > 5 && ch > 5)
        canvas_setbounds(x, txpix, typix,
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1177
            txpix + cw, typix + ch);
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
	/* readjust garrays (if any) */
	t_gobj *g, *gg = NULL;
	t_garray *ga = NULL;
	t_array *a = NULL;
	int  num_elem = 0;

	for (g = x->gl_list; g; g = g->g_next) {
		//fprintf(stderr, "searching\n");

		//for subpatch garrays
		if (pd_class(&g->g_pd) == garray_class) {
			//fprintf(stderr,"found ya\n");
			ga = (t_garray *)g;
			if (ga) {
				a = garray_getarray(ga);
				num_elem = a->a_n;
				garray_fittograph(ga, num_elem);
			}
		}
	}
Miller Puckette's avatar
Miller Puckette committed
1198
1199
1200
1201
1202
1203
}

void canvas_popabstraction(t_canvas *x)
{
    newest = &x->gl_pd;
    pd_popsym(&x->gl_pd);
1204
1205
    //x->gl_loading = 1;
	//fprintf(stderr,"loading = 1 .x%lx owner=.x%lx\n", x, x->gl_owner);
Miller Puckette's avatar
Miller Puckette committed
1206
1207
    canvas_resortinlets(x);
    canvas_resortoutlets(x);
1208
1209
    x->gl_loading = 0;
	//fprintf(stderr,"loading = 0 .x%lx owner=.x%lx\n", x, x->gl_owner);
Miller Puckette's avatar
Miller Puckette committed
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
}

void canvas_logerror(t_object *y)
{
#ifdef LATER
    canvas_vis(x, 1);
    if (!glist_isselected(x, &y->ob_g))
        glist_select(x, &y->ob_g);
#endif
}

/* -------------------------- subcanvases ---------------------- */

static void *subcanvas_new(t_symbol *s)
{
    t_atom a[6];
    t_canvas *x, *z = canvas_getcurrent();
    if (!*s->s_name) s = gensym("/SUBPATCH/");
    SETFLOAT(a, 0);
    SETFLOAT(a+1, GLIST_DEFCANVASYLOC);
    SETFLOAT(a+2, GLIST_DEFCANVASWIDTH);
    SETFLOAT(a+3, GLIST_DEFCANVASHEIGHT);
    SETSYMBOL(a+4, s);
    SETFLOAT(a+5, 1);
    x = canvas_new(0, 0, 6, a);
    x->gl_owner = z;
    canvas_pop(x, 1);
    return (x);
}

static void canvas_click(t_canvas *x,
    t_floatarg xpos, t_floatarg ypos,
        t_floatarg shift, t_floatarg ctrl, t_floatarg alt)
{
    canvas_vis(x, 1);
}


    /* find out from subcanvas contents how much to fatten the box */
void canvas_fattensub(t_canvas *x,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
    t_gobj *y;
    *xp2 += 50;     /* fake for now */
    *yp2 += 50;
}

static void canvas_rename_method(t_canvas *x, t_symbol *s, int ac, t_atom *av)
{
    if (ac && av->a_type == A_SYMBOL)
        canvas_rename(x, av->a_w.w_symbol, 0);
    else if (ac && av->a_type == A_DOLLSYM)
    {
        t_canvasenvironment *e = canvas_getenv(x);
        canvas_setcurrent(x);
        canvas_rename(x, binbuf_realizedollsym(av->a_w.w_symbol,
            e->ce_argc, e->ce_argv, 1), 0); 
        canvas_unsetcurrent(x);
    }
    else canvas_rename(x, gensym("Pd"), 0);
}

/* ------------------ table ---------------------------*/

static int tabcount = 0;

static void *table_new(t_symbol *s, t_floatarg f)
{
    t_atom a[9];
    t_glist *gl;
    t_canvas *x, *z = canvas_getcurrent();
    if (s == &s_)
    {
         char  tabname[255];
         t_symbol *t = gensym("table"); 
         sprintf(tabname, "%s%d", t->s_name, tabcount++);
         s = gensym(tabname); 
    }
    if (f <= 1)
        f = 100;
    SETFLOAT(a, 0);
    SETFLOAT(a+1, GLIST_DEFCANVASYLOC);
    SETFLOAT(a+2, 600);
    SETFLOAT(a+3, 400);
    SETSYMBOL(a+4, s);
    SETFLOAT(a+5, 0);
    x = canvas_new(0, 0, 6, a);

    x->gl_owner = z;

        /* create a graph for the table */
    gl = glist_addglist((t_glist*)x, &s_, 0, -1, (f > 1 ? f-1 : 1), 1,
        50, 350, 550, 50);

    graph_array(gl, s, &s_float, f, 0);

    canvas_pop(x, 0); 

    return (x);
}

    /* return true if the "canvas" object is an abstraction (so we don't
    save its contents, fogr example.)  */
int canvas_isabstraction(t_canvas *x)
{
    return (x->gl_env != 0);
}

    /* return true if the "canvas" object is a "table". */
int canvas_istable(t_canvas *x)
{
    t_atom *argv = (x->gl_obj.te_binbuf? binbuf_getvec(x->gl_obj.te_binbuf):0);
    int argc = (x->gl_obj.te_binbuf? binbuf_getnatom(x->gl_obj.te_binbuf) : 0);
    int istable = (argc && argv[0].a_type == A_SYMBOL &&
        argv[0].a_w.w_symbol == gensym("table"));
    return (istable);
}

    /* return true if the "canvas" object should be treated as a text
    object.  This is true for abstractions but also for "table"s... */
/* JMZ: add a flag to gop-abstractions to hide the title */
int canvas_showtext(t_canvas *x)
{
    t_atom *argv = (x->gl_obj.te_binbuf? binbuf_getvec(x->gl_obj.te_binbuf):0);
    int argc = (x->gl_obj.te_binbuf? binbuf_getnatom(x->gl_obj.te_binbuf) : 0);
    int isarray = (argc && argv[0].a_type == A_SYMBOL &&
        argv[0].a_w.w_symbol == gensym("graph"));
    if(x->gl_hidetext)
      return 0;
    else
      return (!isarray);
}

    /* get the document containing this canvas */
t_canvas *canvas_getrootfor(t_canvas *x)
{
    if ((!x->gl_owner) || canvas_isabstraction(x))
        return (x);
    else return (canvas_getrootfor(x->gl_owner));
}

/* ------------------------- DSP chain handling ------------------------- */

EXTERN_STRUCT _dspcontext;
#define t_dspcontext struct _dspcontext

void ugen_start(void);
void ugen_stop(void);

t_dspcontext *ugen_start_graph(int toplevel, t_signal **sp,
    int ninlets, int noutlets);
void ugen_add(t_dspcontext *dc, t_object *x);
void ugen_connect(t_dspcontext *dc, t_object *x1, int outno,
    t_object *x2, int inno);
void ugen_done_graph(t_dspcontext *dc);

    /* schedule one canvas for DSP.  This is called below for all "root"
    canvases, but is also called from the "dsp" method for sub-
    canvases, which are treated almost like any other tilde object.  */

static void canvas_dodsp(t_canvas *x, int toplevel, t_signal **sp)
{
    t_linetraverser t;
    t_outconnect *oc;
    t_gobj *y;
    t_object *ob;
    t_symbol *dspsym = gensym("dsp");
    t_dspcontext *dc;    

        /* create a new "DSP graph" object to use in sorting this canvas.
        If we aren't toplevel, there are already other dspcontexts around. */

    dc = ugen_start_graph(toplevel, sp,
        obj_nsiginlets(&x->gl_obj),
        obj_nsigoutlets(&x->gl_obj));

        /* find all the "dsp" boxes and add them to the graph */
1387
1388

    ob = &x->gl_magic_glass->x_obj;
1389
1390
    if (ob && magicGlass_bound(x->gl_magic_glass)) {
		//fprintf(stderr,"adding cord inspector to dsp %d\n", magicGlass_bound(x->gl_magic_glass));
1391
1392
		ugen_add(dc, ob);  // this t_canvas could be an array, hence no gl_magic_glass
	}
Miller Puckette's avatar
Miller Puckette committed
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
    
    for (y = x->gl_list; y; y = y->g_next)
        if ((ob = pd_checkobject(&y->g_pd)) && zgetfn(&y->g_pd, dspsym))
            ugen_add(dc, ob);

        /* ... and all dsp interconnections */
    linetraverser_start(&t, x);
    while (oc = linetraverser_next(&t))
        if (obj_issignaloutlet(t.tr_ob, t.tr_outno))
            ugen_connect(dc, t.tr_ob, t.tr_outno, t.tr_ob2, t.tr_inno);

        /* finally, sort them and add them to the DSP chain */
    ugen_done_graph(dc);
}

static void canvas_dsp(t_canvas *x, t_signal **sp)
{
    canvas_dodsp(x, 0, sp);
}

    /* this routine starts DSP for all root canvases. */
static void canvas_start_dsp(void)
{
    t_canvas *x;
    if (canvas_dspstate) ugen_stop();
    else sys_gui("pdtk_pd_dsp ON\n");
    ugen_start();
    
    for (x = canvas_list; x; x = x->gl_next)
        canvas_dodsp(x, 1, 0);
    
    canvas_dspstate = 1;
}

static void canvas_stop_dsp(void)
{
    if (canvas_dspstate)
    {
        ugen_stop();
        sys_gui("pdtk_pd_dsp OFF\n");
        canvas_dspstate = 0;
    }
}

    /* DSP can be suspended before, and resumed after, operations which
    might affect the DSP chain.  For example, we suspend before loading and
    resume afterward, so that DSP doesn't get resorted for every DSP object
1440
    in the patch. */
Miller Puckette's avatar
Miller Puckette committed
1441
1442
1443
1444

int canvas_suspend_dsp(void)
{
    int rval = canvas_dspstate;
1445
	//fprintf(stderr,"canvas_suspend_dsp %d\n", rval);
Miller Puckette's avatar
Miller Puckette committed
1446
1447
1448
1449
1450
1451
    if (rval) canvas_stop_dsp();
    return (rval);
}

void canvas_resume_dsp(int oldstate)
{
1452
	//fprintf(stderr,"canvas_resume_dsp %d\n", oldstate);
Miller Puckette's avatar
Miller Puckette committed
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
    if (oldstate) canvas_start_dsp();
}

    /* this is equivalent to suspending and resuming in one step. */
void canvas_update_dsp(void)
{
    if (canvas_dspstate) canvas_start_dsp();
}

void glob_dsp(void *dummy, t_symbol *s, int argc, t_atom *argv)
{
    int newstate;
    if (argc)
    {
        newstate = atom_getintarg(0, argc, argv);
        if (newstate && !canvas_dspstate)
        {
            sys_set_audio_state(1);
            canvas_start_dsp();
        }
        else if (!newstate && canvas_dspstate)
        {
            canvas_stop_dsp();
            sys_set_audio_state(0);
        }
    }
    else post("dsp state %d", canvas_dspstate);
}

void *canvas_getblock(t_class *blockclass, t_canvas **canvasp)
{
    t_canvas *canvas = *canvasp;
    t_gobj *g;
    void *ret = 0;
    for (g = canvas->gl_list; g; g = g->g_next)
    {
        if (g->g_pd == blockclass)
            ret = g;
    }
    *canvasp = canvas->gl_owner;
    return(ret);
}
    
/******************* redrawing  data *********************/

    /* redraw all "scalars" (do this if a drawing command is changed.) 
    LATER we'll use the "template" information to select which ones we
    redraw.   Action = 0 for redraw, 1 for draw only, 2 for erase. */
static void glist_redrawall(t_glist *gl, int action)
{
1503
	//fprintf(stderr,"glist_redrawall\n");
Miller Puckette's avatar
Miller Puckette committed
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
    t_gobj *g;
    int vis = glist_isvisible(gl);
    for (g = gl->gl_list; g; g = g->g_next)
    {
        t_class *cl;
        if (vis && g->g_pd == scalar_class)
        {
            if (action == 1)
            {
                if (glist_isvisible(gl))
                    gobj_vis(g, gl, 1);
            }
            else if (action == 2)
            {
                if (glist_isvisible(gl))
                    gobj_vis(g, gl, 0);
            }
            else scalar_redraw((t_scalar *)g, gl);
        }
        else if (g->g_pd == canvas_class)
            glist_redrawall((t_glist *)g, action);
    }
1526
1527
1528
	if (glist_isselected(glist_getcanvas(gl), (t_gobj *)gl)) {
		sys_vgui("pdtk_select_all_gop_widgets .x%lx %lx %d\n", glist_getcanvas(gl), gl, 1);
	}
Miller Puckette's avatar
Miller Puckette committed
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
}

    /* public interface for above. */
void canvas_redrawallfortemplate(t_template *template, int action)
{
    t_canvas *x;
        /* find all root canvases */
    for (x = canvas_list; x; x = x->gl_next)
        glist_redrawall(x, action);
}

    /* find the template defined by a canvas, and redraw all elements
    for that */
void canvas_redrawallfortemplatecanvas(t_canvas *x, int action)
{
1544
	//fprintf(stderr,"canvas_redrawallfortemplatecanvas\n");
Miller Puckette's avatar
Miller Puckette committed
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
    t_gobj *g;
    t_template *tmpl;
    t_symbol *s1 = gensym("struct");
    for (g = x->gl_list; g; g = g->g_next)
    {
        t_object *ob = pd_checkobject(&g->g_pd);
        t_atom *argv;
        if (!ob || ob->te_type != T_OBJECT ||
            binbuf_getnatom(ob->te_binbuf) < 2)
            continue;
        argv = binbuf_getvec(ob->te_binbuf);
        if (argv[0].a_type != A_SYMBOL || argv[1].a_type != A_SYMBOL
            || argv[0].a_w.w_symbol != s1)
                continue;
        tmpl = template_findbyname(argv[1].a_w.w_symbol);
        canvas_redrawallfortemplate(tmpl, action);
    }
    canvas_redrawallfortemplate(0, action);
}

/* ------------------------------- declare ------------------------ */

/* put "declare" objects in a patch to tell it about the environment in
which objects should be created in this canvas.  This includes directories to
search ("-path", "-stdpath") and object libraries to load
("-lib" and "-stdlib").  These must be set before the patch containing
the "declare" object is filled in with its contents; so when the patch is
saved,  we throw early messages to the canvas to set the environment
before any objects are created in it. */

static t_class *declare_class;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1576
extern t_class *import_class;
Miller Puckette's avatar
Miller Puckette committed
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611

typedef struct _declare
{
    t_object x_obj;
    t_canvas *x_canvas;
    int x_useme;
} t_declare;

static void *declare_new(t_symbol *s, int argc, t_atom *argv)
{
    t_declare *x = (t_declare *)pd_new(declare_class);
    x->x_useme = 1;
    x->x_canvas = canvas_getcurrent();
        /* LATER update environment and/or load libraries */
    return (x);
}

static void declare_free(t_declare *x)
{
    x->x_useme = 0;
        /* LATER update environment */
}

void canvas_savedeclarationsto(t_canvas *x, t_binbuf *b)
{
    t_gobj *y;

    for (y = x->gl_list; y; y = y->g_next)
    {
        if (pd_class(&y->g_pd) == declare_class)
        {
            binbuf_addv(b, "s", gensym("#X"));
            binbuf_addbinbuf(b, ((t_declare *)y)->x_obj.te_binbuf);
            binbuf_addv(b, ";");
        }
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
        else if (pd_class(&y->g_pd) == import_class)
        {
            int i, argc;
            t_atom *argv;
            binbuf_addv(b, "s", gensym("#X"));
            binbuf_addv(b, "s", gensym("declare"));
            argc = binbuf_getnatom(((t_object *)y)->te_binbuf) - 1;
            argv = binbuf_getvec(((t_object *)y)->te_binbuf) + 1;
            for(i = 0; i < argc; ++i)
            {
                binbuf_addv(b, "s", gensym("-lib"));
                binbuf_add(b, 1, argv + i);
            }
            binbuf_addv(b, ";");
        }
Miller Puckette's avatar
Miller Puckette committed
1627
1628
1629
1630
1631
        else if (pd_class(&y->g_pd) == canvas_class)
            canvas_savedeclarationsto((t_canvas *)y, b);
    }
}

1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
static void canvas_completepath(char *from, char *to, int bufsize)
{
    if (sys_isabsolutepath(from))
    {
        to[0] = '\0';
    }
    else
    {   // if not absolute path, append Pd lib dir
        strncpy(to, sys_libdir->s_name, bufsize-4);
        to[bufsize-3] = '\0';
        strcat(to, "/");
    }
    strncat(to, from, bufsize-strlen(to));
    to[bufsize-1] = '\0';
}

Miller Puckette's avatar
Miller Puckette committed
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
static void canvas_declare(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
{
    int i;
    t_canvasenvironment *e = canvas_getenv(x);
#if 0
    startpost("declare:: %s", s->s_name);
    postatom(argc, argv);
    endpost();
#endif
    for (i = 0; i < argc; i++)
    {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1659
        char strbuf[FILENAME_MAX];
Miller Puckette's avatar
Miller Puckette committed
1660
1661
1662
1663
1664
1665
1666
1667
1668
        char *flag = atom_getsymbolarg(i, argc, argv)->s_name;
        if ((argc > i+1) && !strcmp(flag, "-path"))
        {
            e->ce_path = namelist_append(e->ce_path, 
                atom_getsymbolarg(i+1, argc, argv)->s_name, 0);
            i++;
        }
        else if ((argc > i+1) && !strcmp(flag, "-stdpath"))
        {
1669
            canvas_completepath(atom_getsymbolarg(i+1, argc, argv)->s_name,
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1670
                strbuf, FILENAME_MAX);
Miller Puckette's avatar
Miller Puckette committed
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
            e->ce_path = namelist_append(e->ce_path, strbuf, 0);
            i++;
        }
        else if ((argc > i+1) && !strcmp(flag, "-lib"))
        {
            sys_load_lib(x, atom_getsymbolarg(i+1, argc, argv)->s_name);
            i++;
        }
        else if ((argc > i+1) && !strcmp(flag, "-stdlib"))
        {
1681
            canvas_completepath(atom_getsymbolarg(i+1, argc, argv)->s_name,
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1682
                strbuf, FILENAME_MAX);
Miller Puckette's avatar
Miller Puckette committed
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
            sys_load_lib(0, strbuf);
            i++;
        }
        else post("declare: %s: unknown declaration", flag);
    }
}

    /* utility function to read a file, looking first down the canvas's search
    path (set with "declare" objects in the patch and recursively in calling
    patches), then down the system one.  The filename is the concatenation of
    "name" and "ext".  "Name" may be absolute, or may be relative with
    slashes.  If anything can be opened, the true directory
    ais put in the buffer dirresult (provided by caller), which should
    be "size" bytes.  The "nameresult" pointer will be set somewhere in
    the interior of "dirresult" and will give the file basename (with
    slashes trimmed).  If "bin" is set a 'binary' open is
    attempted, otherwise ASCII (this only matters on Microsoft.) 
    If "x" is zero, the file is sought in the directory "." or in the
    global path.*/

int canvas_open(t_canvas *x, const char *name, const char *ext,
    char *dirresult, char **nameresult, unsigned int size, int bin)
{
    t_namelist *nl, thislist;
    int fd = -1;
    t_canvas *y;

        /* first check if "name" is absolute (and if so, try to open) */
    if (sys_open_absolute(name, ext, dirresult, nameresult, size, bin, &fd))
        return (fd);
    
        /* otherwise "name" is relative; start trying in directories named
        in this and parent environments */
    for (y = x; y; y = y->gl_owner)
        if (y->gl_env)
    {
        t_namelist *nl;
        t_canvas *x2 = x;
        char *dir;
        while (x2 && x2->gl_owner)
            x2 = x2->gl_owner;
        dir = (x2 ? canvas_getdir(x2)->s_name : ".");
        for (nl = y->gl_env->ce_path; nl; nl = nl->nl_next)
        {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1727
            char realname[FILENAME_MAX];
1728
1729
1730
1731
1732
1733
            if (sys_isabsolutepath(nl->nl_string))
            {
                realname[0] = '\0';
            }
            else
            {   /* if not absolute path, append Pd lib dir */
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1734
1735
                strncpy(realname, dir, FILENAME_MAX);
                realname[FILENAME_MAX-3] = 0;
1736
1737
                strcat(realname, "/");
            }
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1738
1739
            strncat(realname, nl->nl_string, FILENAME_MAX-strlen(realname));
            realname[FILENAME_MAX-1] = 0;
Miller Puckette's avatar
Miller Puckette committed
1740
1741
1742
1743
1744
1745
1746
1747
1748
            if ((fd = sys_trytoopenone(realname, name, ext,
                dirresult, nameresult, size, bin)) >= 0)
                    return (fd);
        }
    }
    return (open_via_path((x ? canvas_getdir(x)->s_name : "."), name, ext,
        dirresult, nameresult, size, bin));
}

1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
void canvasgop_draw_move(t_canvas *x, int doit)
{
	//delete the earlier GOP window so that when dragging 
	//there is only one GOP window present on parent
	sys_vgui(".x%lx.c delete GOP\n",  x);
		
	//redraw the GOP
	canvas_setgraph(x, x->gl_isgraph+2*x->gl_hidetext, 0);
	canvas_dirty(x, 1);
	if (x->gl_havewindow) {
	    canvas_redraw(x);
	}
1761
1762
1763
	//fprintf(stderr,"%d %d\n", (x->gl_owner ? 1:0), glist_isvisible(x->gl_owner));

	if (x->gl_owner && glist_isvisible(x->gl_owner)) {
1764
		glist_noselect(x);
1765
		//vmess(&x->gl_owner->gl_obj.te_pd, gensym("menu-open"), "");
1766
1767
	    gobj_vis(&x->gl_gobj, x->gl_owner, 0);
	    gobj_vis(&x->gl_gobj, x->gl_owner, 1);
1768
		//canvas_redraw(x->gl_owner);
1769
1770
1771
1772
1773
1774
	}
	
	//update scrollbars when GOP potentially exceeds window size
	t_canvas *canvas=(t_canvas *)glist_getcanvas(x);
	
	//if gop is being disabled go one level up
1775
	if (!x->gl_isgraph && x->gl_owner && glist_isvisible(x->gl_owner)) {
1776
		canvas=canvas->gl_owner;
1777
		//canvas_redraw(canvas);
1778
1779
	}
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x);
1780
1781
	if (x->gl_owner && glist_isvisible(x->gl_owner))
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->gl_owner);
1782
1783
1784
1785
}

extern int gfxstub_haveproperties(void *key);
extern void canvas_canvas_setundo(t_canvas *x);
1786
1787
extern void graph_checkgop_rect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2);
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804

void canvasgop__clickhook(t_scalehandle *sh, t_floatarg f, t_floatarg xxx, t_floatarg yyy)
{
	int x1=0, y1=0, x2=0, y2=0; //for getrect

	t_canvas *x = (t_canvas *)(sh->h_master);

 	if (xxx) x->scale_offset_x = xxx;
 	if (yyy) x->scale_offset_y = yyy;

    int newstate = (int)f;
    if (sh->h_dragon && newstate == 0)
    {
		/* done dragging */
		if(sh->h_scale)														//enter if resize_gop hook
		{
			/* first set up the undo apply */
1805
1806
			//canvas_canvas_setundo(x);
			canvas_undo_add(x, 8, "apply", canvas_undo_set_canvas(x));
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816

			if (sh->h_dragx || sh->h_dragy) 
			{
				x->gl_pixwidth = x->gl_pixwidth + sh->h_dragx - x->scale_offset_x;
				if (x->gl_pixwidth < SCALE_GOP_MINWIDTH)
					x->gl_pixwidth = SCALE_GOP_MINWIDTH;
				x->gl_pixheight = x->gl_pixheight + sh->h_dragy - x->scale_offset_y;
				if (x->gl_pixheight < SCALE_GOP_MINHEIGHT)
					x->gl_pixheight = SCALE_GOP_MINHEIGHT;

1817
				// check if the text is not hidden
1818
				// if so make minimum width and height based retrieved from getrect
1819
1820
1821
1822
1823
1824
1825
				if (!x->gl_hidetext)
				{
					if (x->gl_owner) {
						gobj_getrect((t_gobj*)x, x->gl_owner, &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;
					} else {
1826
						graph_checkgop_rect((t_gobj*)x, x, &x1, &y1, &x2, &y2);
1827
1828
1829
						if (x2-x1 > x->gl_pixwidth) x->gl_pixwidth = x2-x1;
						if (y2-y1 > x->gl_pixheight) x->gl_pixheight = y2-y1;
					}
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851