g_canvas.c 95.9 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 "s_utf8.h"
14
#include "g_magicglass.h"
Miller Puckette's avatar
Miller Puckette committed
15
16
#include "g_canvas.h"
#include "g_all_guis.h"
17
#include <string.h>
18
#include <math.h>
19

20
21
22
23
24
25
26
#ifdef MSW
#include <io.h>
#endif
#ifdef _MSC_VER
#define snprintf _snprintf
#endif

27
28
29
30
31
t_garray *array_garray;
t_class *preset_hub_class;
t_class *preset_node_class;
int array_joc;

32
extern int do_not_redraw;
33
34
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
35
36
37
38
39
40
41
42
43
44
45

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

46
47
48
49
50
51
52
53
54
55
56
57
58
t_canvasenvironment *dummy_canvas_env(const char *dir)
{
 static t_canvasenvironment dummy_env = {
   .ce_dir = NULL,
   .ce_argc = 0,
   .ce_argv = NULL,
   .ce_dollarzero = 0,
   .ce_path = NULL
 };
 dummy_env.ce_dir = gensym(dir);
 return &dummy_env;
}

Miller Puckette's avatar
Miller Puckette committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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 */ 
75
//t_canvas *canvas_list;              /* list of all root canvases */
Miller Puckette's avatar
Miller Puckette committed
76
77
78
79
80

/* ------------------ forward function declarations --------------- */
static void canvas_start_dsp(void);
static void canvas_stop_dsp(void);
static void canvas_drawlines(t_canvas *x);
81
void canvas_dosetbounds(t_canvas *x, int x1, int y1, int x2, int y2);
82
void canvas_reflecttitle(t_canvas *x);
Miller Puckette's avatar
Miller Puckette committed
83
84
85
static void canvas_addtolist(t_canvas *x);
static void canvas_takeofflist(t_canvas *x);
static void canvas_pop(t_canvas *x, t_floatarg fvis);
86
87
88
static int canvas_should_bind(t_canvas *x);
static void canvas_bind(t_canvas *x);
static void canvas_unbind(t_canvas *x);
89
void canvas_declare(t_canvas *x, t_symbol *s, int argc, t_atom *argv);
Miller Puckette's avatar
Miller Puckette committed
90
91
92
93
94
95
96
97

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

98
99
    /* maintain the list of visible toplevels for the GUI's "windows" menu */
void canvas_updatewindowlist( void)
Miller Puckette's avatar
Miller Puckette committed
100
{
101
    t_canvas *x;
102
103
    if (glist_amreloadingabstractions)  /* not if we're in a reload */
        return;
104
105
    gui_start_vmess("gui_set_toplevel_window_list", "s", "dummy");
    gui_start_array();
106
    for (x = pd_this->pd_canvaslist; x; x = x->gl_next)
Miller Puckette's avatar
Miller Puckette committed
107
    {
108
        if (!x->gl_owner)
Miller Puckette's avatar
Miller Puckette committed
109
        {
110
111
            /* this is a canvas; if we have a window, put on "windows" list */
            if (x->gl_havewindow)
Miller Puckette's avatar
Miller Puckette committed
112
            {
113
                gui_s(x->gl_name->s_name);
114
                gui_x((t_int)x);
Miller Puckette's avatar
Miller Puckette committed
115
116
117
            }
        }
    }
118
119
    gui_end_array();
    gui_end_vmess();
Miller Puckette's avatar
Miller Puckette committed
120
121
122
123
124
}

    /* add a glist the list of "root" canvases (toplevels without parents.) */
static void canvas_addtolist(t_canvas *x)
{
125
126
    x->gl_next = pd_this->pd_canvaslist;
    pd_this->pd_canvaslist = x;
Miller Puckette's avatar
Miller Puckette committed
127
128
129
130
131
}

static void canvas_takeofflist(t_canvas *x)
{
        /* take it off the window list */
132
    if (x == pd_this->pd_canvaslist) pd_this->pd_canvaslist = x->gl_next;
Miller Puckette's avatar
Miller Puckette committed
133
134
135
    else
    {
        t_canvas *z;
136
137
        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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        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
180
            bug("t_canvasenvironment");
Miller Puckette's avatar
Miller Puckette committed
181
182
183
    return (x->gl_env);
}

184
185
186
187
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
188
{
189
190
191
192
193
194
195
196
197
    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
198
199
200
201
202
203
204
205
206
207
208
209
    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;
}

210
211
212
213
214
215
216
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
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
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)
{
246
247
248
    char interim[FILENAME_MAX];
    sys_expandpathelems(file, interim);
    //fprintf(stderr,"interim = <%s>\n", interim);
Miller Puckette's avatar
Miller Puckette committed
249
    char *dir = canvas_getenv(x)->ce_dir->s_name;
250
    if (interim[0] == '/' || (interim[0] && interim[1] == ':') || !*dir)
Miller Puckette's avatar
Miller Puckette committed
251
    {
252
        //fprintf(stderr,"root file\n");
253
        strncpy(result, interim, resultsize);
Miller Puckette's avatar
Miller Puckette committed
254
255
256
257
        result[resultsize-1] = 0;
    }
    else
    {
258
        //fprintf(stderr,"relative file\n");
Miller Puckette's avatar
Miller Puckette committed
259
260
261
262
263
264
        int nleft;
        strncpy(result, dir, resultsize);
        result[resultsize-1] = 0;
        nleft = resultsize - strlen(result) - 1;
        if (nleft <= 0) return;
        strcat(result, "/");
265
        strncat(result, interim, nleft);
Miller Puckette's avatar
Miller Puckette committed
266
        result[resultsize-1] = 0;
267
    } 
268
    //fprintf(stderr,"resulting file = <%s>\n", result);          
Miller Puckette's avatar
Miller Puckette committed
269
270
271
272
}

void canvas_rename(t_canvas *x, t_symbol *s, t_symbol *dir)
{
273
    canvas_unbind(x);
Miller Puckette's avatar
Miller Puckette committed
274
    x->gl_name = s;
275
    canvas_bind(x);
Miller Puckette's avatar
Miller Puckette committed
276
277
278
279
280
    if (dir && dir != &s_)
    {
        t_canvasenvironment *e = canvas_getenv(x);
        e->ce_dir = dir;
    }
281
282
283
    if (glist_isvisible(x))
        if (x->gl_havewindow) //was glist_isvisible(x)
            canvas_reflecttitle(x);
Miller Puckette's avatar
Miller Puckette committed
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
}

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

369
370
371
372
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
373
374
375
376
377
378
379
380
381
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);

382
383
384
385
386
387
    //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);
}
388

389
390
391
392
393
394
395
/* 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 */

396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
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;
}

412
413
int canvas_dirty_broadcast_all(t_symbol *name, t_symbol *dir, int mess);

Miller Puckette's avatar
Miller Puckette committed
414
    /* make a new glist.  It will either be a "root" canvas or else
415
    it appears as a "text" object in another window (canvas_getcurrent() 
Miller Puckette's avatar
Miller Puckette committed
416
417
418
    tells us which.) */
t_canvas *canvas_new(void *dummy, t_symbol *sel, int argc, t_atom *argv)
{
419
420
421
422
423
424
425
426
427
    /* 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
428
429
430
431
432
433
    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);
434
435
    int zoom = 0;
    extern int sys_zoom;
436

Miller Puckette's avatar
Miller Puckette committed
437
    glist_init(x);
438
    //x->gl_magic_glass = magicGlass_new(x);
Miller Puckette's avatar
Miller Puckette committed
439
440
441
    x->gl_obj.te_type = T_OBJECT;
    if (!owner)
        canvas_addtolist(x);
442
    /* post("canvas %zx, owner %zx", x, owner); */
Miller Puckette's avatar
Miller Puckette committed
443
444
445

    if (argc == 5)  /* toplevel: x, y, w, h, font */
    {
446
        t_float zoom_hack = atom_getfloatarg(3, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
447
448
449
450
451
        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);
452
453
454
        zoom_hack -= height;
        if (sys_zoom && zoom_hack > 0)
            zoom = calculate_zoom(zoom_hack);
Miller Puckette's avatar
Miller Puckette committed
455
456
457
    }
    else if (argc == 6)  /* subwindow: x, y, w, h, name, vis */
    {
458
        t_float zoom_hack = atom_getfloatarg(3, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
459
460
461
462
463
464
        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);
465
466
467
        zoom_hack -= height;
        if (sys_zoom && zoom_hack > 0)
            zoom = calculate_zoom(zoom_hack);
Miller Puckette's avatar
Miller Puckette committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
    }
        /* (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;

489
    x->gl_subdirties = 0;
490
    x->gl_dirties = 0;
491

Miller Puckette's avatar
Miller Puckette committed
492
493
494
495
496
497
498
499
    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;
500
    canvas_dosetbounds(x, xloc, yloc, xloc + width, yloc + height);
Miller Puckette's avatar
Miller Puckette committed
501
    x->gl_owner = owner;
502
    x->gl_isclone = 0;
Miller Puckette's avatar
Miller Puckette committed
503
504
    x->gl_name = (*s->s_name ? s : 
        (canvas_newfilename ? canvas_newfilename : gensym("Pd")));
505
    canvas_bind(x);
Miller Puckette's avatar
Miller Puckette committed
506
    x->gl_loading = 1;
507
    x->gl_unloading = 0;
508
    //fprintf(stderr,"loading = 1 .x%zx owner=.x%zx\n", (t_uint)x, (t_uint)x->gl_owner);
Miller Puckette's avatar
Miller Puckette committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
    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);
524
    x->gl_edit_save = 0;
Miller Puckette's avatar
Miller Puckette committed
525
    x->gl_font = sys_nearestfontsize(font);
526
    x->gl_zoom = zoom;
Miller Puckette's avatar
Miller Puckette committed
527
    pd_pushsym(&x->gl_pd);
528

529
    x->u_queue = canvas_undo_init(x);
530
    //glist_setlastxy(x, 20, 20);
531
532
533
534
535
536
537
538
539
540

    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;

541
542
543
544
545
546
547
548
    /* in the case it is an abstraction (gl_env is not null)
        get the number of dirty instances of this same abstraction */
    if(x->gl_env)
    {
        x->gl_dirties = canvas_dirty_broadcast_all(x->gl_name,
                                canvas_getdir(x), 0);
    }

Miller Puckette's avatar
Miller Puckette committed
549
550
551
552
553
554
555
    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)
{
556
557
    //IB: first delete the graph in case we are downsizing the object size via script
    canvas_setgraph(x, 0, 0);
558

Miller Puckette's avatar
Miller Puckette committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
    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,
578
579
    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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
{
    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)
    {
604
        t_float zz;
Miller Puckette's avatar
Miller Puckette committed
605
606
607
608
609
610
611
612
613
        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;
614
615
    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
616
617
618
    if (px1 >= px2 || py1 >= py2)
        px1 = 100, py1 = 20, px2 = 100 + GLIST_DEFGRAPHWIDTH,
            py2 = 20 + GLIST_DEFGRAPHHEIGHT;
619

Miller Puckette's avatar
Miller Puckette committed
620
621
622
623
624
625
626
627
628
629
630
    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);
631
    x->gl_zoom = 0;
Miller Puckette's avatar
Miller Puckette committed
632
633
634
635
    x->gl_screenx1 = x->gl_screeny1 = 0;
    x->gl_screenx2 = 450;
    x->gl_screeny2 = 300;
    x->gl_owner = g;
636
    canvas_bind(x);
Miller Puckette's avatar
Miller Puckette committed
637
638
639
640
641
642
643
    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);
644
    if (!do_not_redraw) scrollbar_update(glist_getcanvas(g));
Ivica Bukvic's avatar
Ivica Bukvic committed
645
    //fprintf(stderr,"    ... %f %f\n", x->gl_x1, x->gl_x2);
Miller Puckette's avatar
Miller Puckette committed
646
647
648
    return (x);
}

649
650
extern int we_are_undoing;

Miller Puckette's avatar
Miller Puckette committed
651
652
653
    /* call glist_addglist from a Pd message */
void glist_glist(t_glist *g, t_symbol *s, int argc, t_atom *argv)
{
654
    if (canvas_hasarray(g)) return;
655
    pd_vmess(&g->gl_pd, gensym("editmode"), "i", 1);
656
657
    // disallow creation of new objects in scalars only window
    if (canvas_has_scalars_only(g) == 2) return;
658
    t_symbol *sym = atom_getsymbolarg(0, argc, argv);
659
660
    /* 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_;  
661
662
663
664
665
666
667
668
    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
669
    glist_addglist(g, sym, x1, y1, x2, y2, px1, py1, px2, py2);
670
671
672
    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
673
674
675
676
677
678
}

    /* 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)
{
679
    // testing to see if we have an array and force hiding text (later update GUI accordingly)
680
681
    // we likely need this to silently update legacy arrays
    // (no regressions are expected but this needs to be tested)
682
683
684
685
686
687
    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;
    }
688
689
    if (hasarray)  {
        x->gl_isgraph = 1;
690
        x->gl_hidetext = 1;
691
    }
692
    return (x->gl_isgraph|(x->gl_hidetext<<1));
Miller Puckette's avatar
Miller Puckette committed
693
694
}

695
696
/* 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
697
{
698
    //fprintf(stderr,"canvas_setbounds %d %d %d %d\n", x1, y1, x2, y2);
699

Miller Puckette's avatar
Miller Puckette committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
    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. */
715
        t_float diff = x->gl_y1 - x->gl_y2;
Miller Puckette's avatar
Miller Puckette committed
716
717
718
719
720
721
722
723
724
725
726
727
        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);
    }
}

728
729
730
731
732
733
734
735
    /* 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
736
737
738
t_symbol *canvas_makebindsym(t_symbol *s)
{
    char buf[MAXPDSTRING];
739
740
    snprintf(buf, MAXPDSTRING-1, "pd-%s", s->s_name);
    buf[MAXPDSTRING-1] = 0;
Miller Puckette's avatar
Miller Puckette committed
741
742
743
    return (gensym(buf));
}

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

Jonathan Wilkes's avatar
Jonathan Wilkes committed
746
747
748
749
750
751
752
753
754
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++)
        {
755
            if (strlen(namebuf) > MAXPDSTRING / 2 - 5)
Jonathan Wilkes's avatar
Jonathan Wilkes committed
756
757
758
                break;
            if (i != 0)
                strcat(namebuf, " ");
759
760
            atom_string(&env->ce_argv[i], namebuf + strlen(namebuf),
                MAXPDSTRING / 2);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
761
762
763
        }
        strcat(namebuf, ")");
    }
764
765
766
767
    else
    {
        namebuf[0] = 0;
        t_gobj *g = NULL;
768
        t_garray *a = NULL;
769
        t_symbol *arrayname;
770
        int found = 0, res;
771
772
773
774
775
        for (g = x->gl_list; g; g = g->g_next)
        {

            if (pd_class(&g->g_pd) == garray_class)
            {
776
                res = garray_getname((t_garray *)g, &arrayname);
777
778
779
780
781
782
783
784
785
786
                if (found)
                {
                    strcat(namebuf, " ");
                }
                strcat(namebuf, arrayname->s_name);
                found++;
                //post("found=%d %s %s", found, arrayname->s_name, namebuf);
            }
        }
    }
Jonathan Wilkes's avatar
Jonathan Wilkes committed
787
788
}

Miller Puckette's avatar
Miller Puckette committed
789
790
791
void canvas_reflecttitle(t_canvas *x)
{
    char namebuf[MAXPDSTRING];
Jonathan Wilkes's avatar
Jonathan Wilkes committed
792
    canvas_args_to_string(namebuf, x);
793
794
    gui_vmess("gui_canvas_set_title", "xsssi",
        x, x->gl_name->s_name,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
795
        namebuf, canvas_getdir(x)->s_name, x->gl_dirty);
Miller Puckette's avatar
Miller Puckette committed
796
797
}

798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/* --------------------- */

/* climbs up to the root canvas while enabling or disabling visual markings for dirtiness
    of traversed canvases */
void canvas_dirtyclimb(t_canvas *x, int n)
{
    if (x->gl_owner)
    {
        gobj_dirty(&x->gl_gobj, x->gl_owner,
            (n ? 1 : (x->gl_subdirties ? 2 : 0)));
        x = x->gl_owner;
        while(x->gl_owner)
        {
            x->gl_subdirties += ((unsigned)n ? 1 : -1);
            if(!x->gl_dirty)
                gobj_dirty(&x->gl_gobj, x->gl_owner, (x->gl_subdirties ? 2 : 0));
            x = x->gl_owner;
        }
    }
}

819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/* the following functions are used to broadcast messages to all instances of
    a specific abstraction (either file-based or ab).
    the state of these instances change accoring to the message sent. */

void clone_iterate(t_pd *z, t_canvas_iterator it, void* data);
int clone_match(t_pd *z, t_symbol *name, t_symbol *dir);

static void canvas_dirty_common(t_canvas *x, int mess)
{
    if(mess == 2)
    {
        if(x->gl_dirty)
        {
            if(!x->gl_havewindow) canvas_vis(x, 1);
            gui_vmess("gui_canvas_emphasize", "x", x);
        }
    }
    else
    {
        x->gl_dirties += mess;
        if(x->gl_havewindow)
            canvas_warning(x, (x->gl_dirties > 1 ?
                                (x->gl_dirty ? 2 : 1)
                                : (x->gl_dirties ? !x->gl_dirty : 0)));
    }
}

/* packed data passing structure for canvas_dirty_broadcast */
typedef struct _dirty_broadcast_data
{
    t_symbol *name;
    t_symbol *dir;
    int mess;
    int *res;   /* return value */
} t_dirty_broadcast_data;

static void canvas_dirty_deliver_packed(t_canvas *x, t_dirty_broadcast_data *data)
{
    *data->res += (x->gl_dirty > 0);
    canvas_dirty_common(x, data->mess);
}

static int canvas_dirty_broadcast_packed(t_canvas *x, t_dirty_broadcast_data *data);

static int canvas_dirty_broadcast(t_canvas *x, t_symbol *name, t_symbol *dir, int mess)
{
    int res = 0;
    t_gobj *g;
    for (g = x->gl_list; g; g = g->g_next)
    {
        if(pd_class(&g->g_pd) == canvas_class)
        {
            if(canvas_isabstraction((t_canvas *)g)
                && ((t_canvas *)g)->gl_name == name
                && canvas_getdir((t_canvas *)g) == dir)
            {
                res += (((t_canvas *)g)->gl_dirty > 0);
                canvas_dirty_common((t_canvas *)g, mess);
            }
            else
                res += canvas_dirty_broadcast((t_canvas *)g, name, dir, mess);
        }
        else if(pd_class(&g->g_pd) == clone_class)
        {
            int cres = 0;
            t_dirty_broadcast_data data;
            data.name = name; data.dir = dir; data.mess = mess; data.res = &cres;
            if(clone_match(&g->g_pd, name, dir))
            {
                clone_iterate(&g->g_pd, canvas_dirty_deliver_packed, &data);
            }
            else
            {
                clone_iterate(&g->g_pd, canvas_dirty_broadcast_packed, &data);
            }
            res += cres;
        }
    }
    return (res);
}

static int canvas_dirty_broadcast_packed(t_canvas *x, t_dirty_broadcast_data *data)
{
    *data->res = canvas_dirty_broadcast(x, data->name, data->dir, data->mess);
}

int canvas_dirty_broadcast_all(t_symbol *name, t_symbol *dir, int mess)
{
    int res = 0;
    t_canvas *x;
    for (x = pd_this->pd_canvaslist; x; x = x->gl_next)
        res += canvas_dirty_broadcast(x, name, dir, mess);
    return (res);
}
Miller Puckette's avatar
Miller Puckette committed
913
914
    /* mark a glist dirty or clean */
void canvas_dirty(t_canvas *x, t_floatarg n)
Miller Puckette's avatar
Miller Puckette committed
915
916
{
    t_canvas *x2 = canvas_getrootfor(x);
917
918
    if (glist_amreloadingabstractions)
        return;
Miller Puckette's avatar
Miller Puckette committed
919
920
921
    if ((unsigned)n != x2->gl_dirty)
    {
        x2->gl_dirty = n;
922
        if (x2->gl_havewindow)
Miller Puckette's avatar
test7    
Miller Puckette committed
923
            canvas_reflecttitle(x2);
924
925
926

        /* set dirtiness visual markings */
        canvas_dirtyclimb(x2, (unsigned)n);
927
928
929
930
931
932
933
934
935
        /* in the case it is an abstraction, we tell all other
            instances that there is eiher one more dirty instance or
            one less dirty instance */
        if(canvas_isabstraction(x2)
            && (x2->gl_owner || x2->gl_isclone))
        {
            canvas_dirty_broadcast_all(x2->gl_name, canvas_getdir(x2),
                (x2->gl_dirty ? 1 : -1));
        }
Miller Puckette's avatar
Miller Puckette committed
936
937
938
    }
}

939
void draw_notify(t_canvas *x, t_symbol *s, int argc, t_atom *argv);
940

941
void canvas_scalar_event(t_canvas *x, t_symbol *s, int argc, t_atom *argv)
942
{
943
944
945
946
947
    /* 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);
948
949
}

950
951
952
void canvas_show_scrollbars(t_canvas *x, t_floatarg f)
{
    x->gl_noscroll = (int)f;
953
954
    if (x->gl_mapped)
        gui_vmess("gui_canvas_set_scrollbars", "xi", x, (int)f);
955
956
957
958
959
960
961
}

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

962
963
extern void canvas_check_nlet_highlights(t_canvas *x);

964
965
966
/*********** dpsaha@vt.edu resize move hooks ****************/
void canvas_draw_gop_resize_hooks(t_canvas* x)
{
967
968
    t_scalehandle *sh = (t_scalehandle *)(x->x_handle);
    t_scalehandle *mh = (t_scalehandle *)(x->x_mhandle);
969
    //fprintf(stderr,"draw_gop_resize_hooks START\n");
970
971
972
973
974
975
    //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
976
977
978
        //fprintf(stderr,"draw_gop_resize_hooks DRAW %zx %zx\n", (t_uint)x, (t_uint)glist_getcanvas(x));
        sprintf(sh->h_pathname, ".x%zx.h%zx", (t_uint)x, (t_uint)sh);
        sprintf(mh->h_pathname, ".x%zx.h%zx", (t_uint)x, (t_uint)mh);
979

980
981
982
983
984
985
986
        /* 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"*/);
987
988
989
990

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

991
        scalehandle_draw_select(mh,
992
            SCALEHANDLE_WIDTH - 4, SCALEHANDLE_HEIGHT - 11);
993
994
995
    }
    else
    {
996
        //fprintf(stderr,"draw_gop_resize_hooks ERASE\n");
997
998
        scalehandle_draw_erase(sh);
        scalehandle_draw_erase(mh);
999
1000
    }
    canvas_check_nlet_highlights(x);