g_editor.c 134 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
/* 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.  */

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

void glist_readfrombinbuf(t_glist *x, t_binbuf *b, char *filename,
    int selectem);

void open_via_helppath(const char *name, const char *dir);
char *class_gethelpdir(t_class *c);

20
21
//static int toggle_moving = 0; //global variable

Miller Puckette's avatar
Miller Puckette committed
22
23
24
25
26
27
28
29
30
/* ------------------ forward declarations --------------- */
static void canvas_doclear(t_canvas *x);
static void glist_setlastxy(t_glist *gl, int xval, int yval);
static void glist_donewloadbangs(t_glist *x);
static t_binbuf *canvas_docopy(t_canvas *x);
static void canvas_dopaste(t_canvas *x, t_binbuf *b);
static void canvas_paste(t_canvas *x);
static void canvas_clearline(t_canvas *x);
static t_binbuf *copy_binbuf;
31
32
//static char *canvas_textcopybuf;
//static int canvas_textcopybufsize;
Miller Puckette's avatar
Miller Puckette committed
33
static t_glist *glist_finddirty(t_glist *x);
34
35
36
static void canvas_reselect(t_canvas *x);
static void canvas_cut(t_canvas *x);
static void canvas_undo(t_canvas *x);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
37
static int paste_xyoffset = 0; /* a counter of pastes to make x,y offsets */
38
static void canvas_mouseup_gop(t_canvas *x, t_gobj *g);
39
40
static void canvas_done_popup(t_canvas *x, t_float which, t_float xpos, t_float ypos);
static void canvas_doarrange(t_canvas *x, t_float which, t_gobj *oldy, t_gobj *oldy_prev, t_gobj *oldy_next);
41
static void canvas_paste_xyoffset(t_canvas *x);
42
void canvas_setgraph(t_glist *x, int flag, int nogoprect);
43
44
45
46
47
48
static char canvas_cnct_inlet_tag[4096];
static char canvas_cnct_outlet_tag[4096];
static int outlet_issignal = 0;
static int inlet_issignal = 0;
static int last_inlet_filter = 0;
static int last_outlet_filter = 0;
49
static int copyfromexternalbuffer = 0;
50
51
52
53
54
55
56
57
static int screenx1;            /* screen coordinates when doing copyfromexternalbuffer */
static int screeny1;
static int screenx2;
static int screeny2;
static int copiedfont;
static void canvas_dofont(t_canvas *x, t_floatarg font, t_floatarg xresize,
    t_floatarg yresize);
extern void canvas_setbounds(t_canvas *x, int x1, int y1, int x2, int y2);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
struct _outlet
{
    t_object *o_owner;
    struct _outlet *o_next;
    t_outconnect *o_connections;
    t_symbol *o_sym;
};

/* used for new duplicate behavior where we can "duplicate" into new window */
static t_canvas *c_selection;

/* iemgui uses black inlets and outlets while default objects use gray ones
   add here more as necessary */
int gobj_filter_highlight_behavior(t_rtext *y) {

	char *buf;
	char name[4];
	int bufsize, i;
	rtext_gettext(y, &buf, &bufsize);
	for (i = 0; i < 3; i++) {
		name[i] = buf[i];
	}
	name[3]='\0';
	//fprintf(stderr,"object name = >%s<\n", name);
	if (!strcmp(name, "bng") ||
		!strcmp(name, "nbx") ||
		!strcmp(name, "hdl") ||
		!strcmp(name, "hsl") ||
		!strcmp(name, "tgl") ||
		!strcmp(name, "vdl") ||
		!strcmp(name, "vsl") ||
		!strcmp(name, "vu ") ||
		/* alternative names for hradio and vradio when invoked from the menu */
		!strcmp(name, "hra") ||
		!strcmp(name, "vra")
		)
		return 1;

	return 0;
}
Miller Puckette's avatar
Miller Puckette committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

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

void gobj_getrect(t_gobj *x, t_glist *glist, int *x1, int *y1,
    int *x2, int *y2)
{
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_getrectfn)
        (*x->g_pd->c_wb->w_getrectfn)(x, glist, x1, y1, x2, y2);
}

void gobj_displace(t_gobj *x, t_glist *glist, int dx, int dy)
{
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_displacefn)
        (*x->g_pd->c_wb->w_displacefn)(x, glist, dx, dy);
}

114
115
116
117
118
119
void gobj_displace_withtag(t_gobj *x, t_glist *glist, int dx, int dy)
{
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_displacefnwtag)
        (*x->g_pd->c_wb->w_displacefnwtag)(x, glist, dx, dy);
}

Miller Puckette's avatar
Miller Puckette committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
void gobj_select(t_gobj *x, t_glist *glist, int state)
{
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_selectfn)
        (*x->g_pd->c_wb->w_selectfn)(x, glist, state);
}

void gobj_activate(t_gobj *x, t_glist *glist, int state)
{
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_activatefn)
        (*x->g_pd->c_wb->w_activatefn)(x, glist, state);
}

void gobj_delete(t_gobj *x, t_glist *glist)
{
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_deletefn)
        (*x->g_pd->c_wb->w_deletefn)(x, glist);
}

138
139
140
int gobj_shouldvis(t_gobj *x, struct _glist *glist)
{
    t_object *ob;
141
	//fprintf(stderr,"shouldvis\n");
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    if (!glist->gl_havewindow && glist->gl_isgraph && glist->gl_goprect &&
        glist->gl_owner && (pd_class(&glist->gl_pd) != garray_class))
    {
        /* if we're graphing-on-parent and the object falls outside the
        graph rectangle, don't draw it. */
        int x1, y1, x2, y2, gx1, gy1, gx2, gy2, m;
        gobj_getrect(&glist->gl_gobj, glist->gl_owner, &x1, &y1, &x2, &y2);
        if (x1 > x2)
            m = x1, x1 = x2, x2 = m;
        if (y1 > y2)
            m = y1, y1 = y2, y2 = m;
        gobj_getrect(x, glist, &gx1, &gy1, &gx2, &gy2);
        if (gx1 < x1 || gx1 > x2 || gx2 < x1 || gx2 > x2 ||
            gy1 < y1 || gy1 > y2 || gy2 < y1 || gy2 > y2)
                return (0);
157
158
		if (glist==glist_getcanvas(glist))
        	sys_vgui(".x%lx.c raise all_cords\n", glist_getcanvas(glist));
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    }
    if (ob = pd_checkobject(&x->g_pd))
    {
        /* return true if the text box should be drawn.  We don't show text
        boxes inside graphs---except comments, if we're doing the new
        (goprect) style. */
        return (glist->gl_havewindow ||
            (ob->te_pd != canvas_class &&
                ob->te_pd->c_wb != &text_widgetbehavior) ||
            (ob->te_pd == canvas_class && (((t_glist *)ob)->gl_isgraph)) ||
            (glist->gl_goprect && (ob->te_type == T_TEXT)));
    }
    else return (1);
}

Miller Puckette's avatar
Miller Puckette committed
174
175
void gobj_vis(t_gobj *x, struct _glist *glist, int flag)
{
176
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_visfn && gobj_shouldvis(x, glist))
Miller Puckette's avatar
Miller Puckette committed
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
        (*x->g_pd->c_wb->w_visfn)(x, glist, flag);
}

int gobj_click(t_gobj *x, struct _glist *glist,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_clickfn)
        return ((*x->g_pd->c_wb->w_clickfn)(x,
            glist, xpix, ypix, shift, alt, dbl, doit));
    else return (0);
}

/* ------------------------ managing the selection ----------------- */

void glist_selectline(t_glist *x, t_outconnect *oc, int index1,
    int outno, int index2, int inno)
{
    if (x->gl_editor)
    {
        glist_noselect(x);
        x->gl_editor->e_selectedline = 1;
        x->gl_editor->e_selectline_index1 = index1;
        x->gl_editor->e_selectline_outno = outno;
        x->gl_editor->e_selectline_index2 = index2;
        x->gl_editor->e_selectline_inno = inno;
        x->gl_editor->e_selectline_tag = oc;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
203
204
        sys_vgui(".x%lx.c itemconfigure l%lx -fill $select_color\n",
            x, x->gl_editor->e_selectline_tag);
205
206
207
        sys_vgui(".x%lx.c addtag selected withtag l%lx\n",
            glist_getcanvas(x), x->gl_editor->e_selectline_tag);
		c_selection = x;
208
    }
Miller Puckette's avatar
Miller Puckette committed
209
210
211
212
213
214
}

void glist_deselectline(t_glist *x)
{
    if (x->gl_editor)
    {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
215
216
        t_linetraverser t;
        t_outconnect *oc;
Miller Puckette's avatar
Miller Puckette committed
217
        x->gl_editor->e_selectedline = 0;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
218
219
220
221
222
223
224
225
226
227
228
229
        linetraverser_start(&t, glist_getcanvas(x));
        do {
            oc = linetraverser_next(&t);
        } while (oc && oc != x->gl_editor->e_selectline_tag);
        int issignal;
        if(outlet_getsymbol(t.tr_outlet) == &s_signal)
            issignal = 1;
        else
            issignal = 0;
        sys_vgui(".x%lx.c itemconfigure l%lx -fill %s\n",
            x, x->gl_editor->e_selectline_tag,
            (issignal ? "$signal_cord" : "$msg_cord"));
230
231
        sys_vgui(".x%lx.c dtag l%lx selected\n",
            glist_getcanvas(x), glist_getcanvas(x)->gl_editor->e_selectline_tag);
Miller Puckette's avatar
Miller Puckette committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    }    
}

int glist_isselected(t_glist *x, t_gobj *y)
{
    if (x->gl_editor)
    {
        t_selection *sel;
        for (sel = x->gl_editor->e_selection; sel; sel = sel->sel_next)
            if (sel->sel_what == y) return (1);
    }
    return (0);
}

    /* call this for unselected objects only */
void glist_select(t_glist *x, t_gobj *y)
{
    if (x->gl_editor)
    {
251
252
		if (c_selection && c_selection != x)
			glist_noselect(c_selection);
Miller Puckette's avatar
Miller Puckette committed
253
254
255
256
257
258
259
260
261
        t_selection *sel = (t_selection *)getbytes(sizeof(*sel));
        if (x->gl_editor->e_selectedline)
            glist_deselectline(x);
            /* LATER #ifdef out the following check */
        if (glist_isselected(x, y)) bug("glist_select");
        sel->sel_next = x->gl_editor->e_selection;
        sel->sel_what = y;
        x->gl_editor->e_selection = sel;
        gobj_select(y, x, 1);
262
		c_selection = x;
263
264

		sys_vgui("pdtk_canvas_update_edit_menu .x%lx 1\n", x);
Miller Puckette's avatar
Miller Puckette committed
265
266
267
    }
}

Miller Puckette's avatar
Miller Puckette committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    /* recursively deselect everything in a gobj "g", if it happens to be
    a glist, in preparation for deselecting g itself in glist_dselect() */
static void glist_checkanddeselectall(t_glist *gl, t_gobj *g)
{
    t_glist *gl2;
    t_gobj *g2;
    if (pd_class(&g->g_pd) != canvas_class)
        return;
    gl2 = (t_glist *)g;
    for (g2 = gl2->gl_list; g2; g2 = g2->g_next)
        glist_checkanddeselectall(gl2, g2);
    glist_noselect(gl2);
}

Miller Puckette's avatar
Miller Puckette committed
282
283
284
    /* call this for selected objects only */
void glist_deselect(t_glist *x, t_gobj *y)
{
285
	//fprintf(stderr, "deselect\n");
Miller Puckette's avatar
Miller Puckette committed
286
287
    int fixdsp = 0;
    static int reenter = 0;
Miller Puckette's avatar
Miller Puckette committed
288
    /* if (reenter) return; */
Miller Puckette's avatar
Miller Puckette committed
289
290
291
292
293
294
295
296
    reenter = 1;
    if (x->gl_editor)
    {
        t_selection *sel, *sel2;
        t_rtext *z = 0;
        if (!glist_isselected(x, y)) bug("glist_deselect");
        if (x->gl_editor->e_textedfor)
        {
297
			//fprintf(stderr, "e_textedfor\n");
Miller Puckette's avatar
Miller Puckette committed
298
299
300
            t_rtext *fuddy = glist_findrtext(x, (t_text *)y);
            if (x->gl_editor->e_textedfor == fuddy)
            {
301
				//fprintf(stderr, "e_textedfor == fuddy\n");
Miller Puckette's avatar
Miller Puckette committed
302
303
                if (x->gl_editor->e_textdirty)
                {
304
					//fprintf(stderr, "textdirty yes\n");
Miller Puckette's avatar
Miller Puckette committed
305
306
                    z = fuddy;
                    canvas_stowconnections(glist_getcanvas(x));
Miller Puckette's avatar
Miller Puckette committed
307
                    glist_checkanddeselectall(x, y);
Miller Puckette's avatar
Miller Puckette committed
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
                }
                gobj_activate(y, x, 0);
            }
            if (zgetfn(&y->g_pd, gensym("dsp")))
                fixdsp = canvas_suspend_dsp();
        }
        if ((sel = x->gl_editor->e_selection)->sel_what == y)
        {
            x->gl_editor->e_selection = x->gl_editor->e_selection->sel_next;
            gobj_select(sel->sel_what, x, 0);
            freebytes(sel, sizeof(*sel));
        }
        else
        {
            for (sel = x->gl_editor->e_selection; sel2 = sel->sel_next;
                sel = sel2)
            {
                if (sel2->sel_what == y)
                {
                    sel->sel_next = sel2->sel_next;
                    gobj_select(sel2->sel_what, x, 0);
                    freebytes(sel2, sizeof(*sel2));
                    break;
                }
            }
        }
        if (z)
        {
336
			//fprintf(stderr, "setto\n");
Miller Puckette's avatar
Miller Puckette committed
337
338
339
340
341
342
343
344
345
346
            char *buf;
            int bufsize;

            rtext_gettext(z, &buf, &bufsize);
            text_setto((t_text *)y, x, buf, bufsize);
            canvas_fixlinesfor(glist_getcanvas(x), (t_text *)y);
            x->gl_editor->e_textedfor = 0;
        }
        if (fixdsp)
            canvas_resume_dsp(1);
347
348
		if (!x->gl_editor->e_selection)
			sys_vgui("pdtk_canvas_update_edit_menu .x%lx 0\n", x);
Miller Puckette's avatar
Miller Puckette committed
349
350
351
352
353
354
355
356
    }
    reenter = 0;
}

void glist_noselect(t_glist *x)
{
    if (x->gl_editor)
    {
357
358
359
360
		if (x->gl_editor->e_selection) {
		    while (x->gl_editor->e_selection)
		        glist_deselect(x, x->gl_editor->e_selection->sel_what);
		}
Miller Puckette's avatar
Miller Puckette committed
361
362
        if (x->gl_editor->e_selectedline)
            glist_deselectline(x);
363
364
		if (c_selection == x)
			c_selection = NULL;
Miller Puckette's avatar
Miller Puckette committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    }
}

void glist_selectall(t_glist *x)
{
    if (x->gl_editor)
    {
        glist_noselect(x);
        if (x->gl_list)
        {
            t_selection *sel = (t_selection *)getbytes(sizeof(*sel));
            t_gobj *y = x->gl_list;
            x->gl_editor->e_selection = sel;
            sel->sel_what = y;
            gobj_select(y, x, 1);
            while (y = y->g_next)
            {
                t_selection *sel2 = (t_selection *)getbytes(sizeof(*sel2));
                sel->sel_next = sel2;
                sel = sel2;
                sel->sel_what = y;
                gobj_select(y, x, 1);
            }
            sel->sel_next = 0;
389
			c_selection = x;
Miller Puckette's avatar
Miller Puckette committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
        }
    }
}

    /* get the index of a gobj in a glist.  If y is zero, return the
    total number of objects. */
int glist_getindex(t_glist *x, t_gobj *y)
{
    t_gobj *y2;
    int indx;

    for (y2 = x->gl_list, indx = 0; y2 && y2 != y; y2 = y2->g_next)
        indx++;
    return (indx);
}

    /* get the index of the object, among selected items, if "selected"
    is set; otherwise, among unselected ones.  If y is zero, just
    counts the selected or unselected objects. */
int glist_selectionindex(t_glist *x, t_gobj *y, int selected)
{
    t_gobj *y2;
    int indx;

    for (y2 = x->gl_list, indx = 0; y2 && y2 != y; y2 = y2->g_next)
        if (selected == glist_isselected(x, y2))
            indx++;
    return (indx);
}

static t_gobj *glist_nth(t_glist *x, int n)
{
    t_gobj *y;
    int indx;
    for (y = x->gl_list, indx = 0; y; y = y->g_next, indx++)
        if (indx == n)
            return (y);
    return (0);
}

/* ------------------- support for undo/redo  -------------------------- */

static t_undofn canvas_undo_fn;         /* current undo function if any */
static int canvas_undo_whatnext;        /* whether we can now UNDO or REDO */
static void *canvas_undo_buf;           /* data private to the undo function */
static t_canvas *canvas_undo_canvas;    /* which canvas we can undo on */
static const char *canvas_undo_name;

void canvas_setundo(t_canvas *x, t_undofn undofn, void *buf,
    const char *name)
{
441
	//fprintf(stderr,"canvas_setundo %s\n", name);
442

Miller Puckette's avatar
Miller Puckette committed
443
444
445
446
447
    int hadone = 0;
        /* blow away the old undo information.  In one special case the
        old undo info is re-used; if so we shouldn't free it here. */
    if (canvas_undo_fn && canvas_undo_buf && (buf != canvas_undo_buf))
    {
448
		//fprintf(stderr,"hadone canvas_setundo\n");
Miller Puckette's avatar
Miller Puckette committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
        (*canvas_undo_fn)(canvas_undo_canvas, canvas_undo_buf, UNDO_FREE);
        hadone = 1;
    }
    canvas_undo_canvas = x;
    canvas_undo_fn = undofn;
    canvas_undo_buf = buf;
    canvas_undo_whatnext = UNDO_UNDO;
    canvas_undo_name = name;
    if (x && glist_isvisible(x) && glist_istoplevel(x))
            /* enable undo in menu */
        sys_vgui("pdtk_undomenu .x%lx %s no\n", x, name);
    else if (hadone)
        sys_vgui("pdtk_undomenu nobody no no\n");
}

    /* clear undo if it happens to be for the canvas x.
     (but if x is 0, clear it regardless of who owns it.) */
void canvas_noundo(t_canvas *x)
{
    if (!x || (x == canvas_undo_canvas))
        canvas_setundo(0, 0, 0, "foo");
}

static void canvas_undo(t_canvas *x)
{
474
	//fprintf(stderr,"canvas_undo\n");
Miller Puckette's avatar
Miller Puckette committed
475
476
477
478
479
480
    if (x != canvas_undo_canvas)
        bug("canvas_undo 1");
    else if (canvas_undo_whatnext != UNDO_UNDO)
        bug("canvas_undo 2");
    else
    {
481
        //fprintf(stderr,"undo\n");
Miller Puckette's avatar
Miller Puckette committed
482
483
484
485
486
        (*canvas_undo_fn)(canvas_undo_canvas, canvas_undo_buf, UNDO_UNDO);
            /* enable redo in menu */
        if (glist_isvisible(x) && glist_istoplevel(x))
            sys_vgui("pdtk_undomenu .x%lx no %s\n", x, canvas_undo_name);
        canvas_undo_whatnext = UNDO_REDO;
487
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x);
Miller Puckette's avatar
Miller Puckette committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
    }
}

static void canvas_redo(t_canvas *x)
{
    if (x != canvas_undo_canvas)
        bug("canvas_undo 1");
    else if (canvas_undo_whatnext != UNDO_REDO)
        bug("canvas_undo 2");
    else
    {
        /* post("redo"); */
        (*canvas_undo_fn)(canvas_undo_canvas, canvas_undo_buf, UNDO_REDO);
            /* enable undo in menu */
        if (glist_isvisible(x) && glist_istoplevel(x))
            sys_vgui("pdtk_undomenu .x%lx %s no\n", x, canvas_undo_name);
        canvas_undo_whatnext = UNDO_UNDO;
505
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x);
Miller Puckette's avatar
Miller Puckette committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
    }
}

/* ------- specific undo methods: 1. connect and disconnect -------- */

typedef struct _undo_connect    
{
    int u_index1;
    int u_outletno;
    int u_index2;
    int u_inletno;
} t_undo_connect;

static void *canvas_undo_set_disconnect(t_canvas *x,
    int index1, int outno, int index2, int inno)
{
    t_undo_connect *buf = (t_undo_connect *)getbytes(sizeof(*buf));
    buf->u_index1 = index1;
    buf->u_outletno = outno;
    buf->u_index2 = index2;
    buf->u_inletno = inno;
    return (buf);
}

void canvas_disconnect(t_canvas *x,
531
    t_float index1, t_float outno, t_float index2, t_float inno)
Miller Puckette's avatar
Miller Puckette committed
532
533
534
535
536
537
538
539
540
541
542
543
{
    t_linetraverser t;
    t_outconnect *oc;
    linetraverser_start(&t, x);
    while (oc = linetraverser_next(&t))
    {
        int srcno = canvas_getindex(x, &t.tr_ob->ob_g);
        int sinkno = canvas_getindex(x, &t.tr_ob2->ob_g);
        if (srcno == index1 && t.tr_outno == outno &&
            sinkno == index2 && t.tr_inno == inno)
        {
            sys_vgui(".x%lx.c delete l%lx\n", x, oc);
544
545
546
547
548
549
            // jsarlo
			if(x->gl_magic_glass) {
            	magicGlass_unbind(x->gl_magic_glass);
            	magicGlass_hide(x->gl_magic_glass);
			}
            // end jsarlo
Miller Puckette's avatar
Miller Puckette committed
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
            obj_disconnect(t.tr_ob, t.tr_outno, t.tr_ob2, t.tr_inno);
            break;
        }
    }
}

static void canvas_undo_disconnect(t_canvas *x, void *z, int action)
{
    t_undo_connect *buf = z;
    if (action == UNDO_UNDO)
    {
        canvas_connect(x, buf->u_index1, buf->u_outletno,
            buf->u_index2, buf->u_inletno);
    }
    else if (action == UNDO_REDO)
    {
        canvas_disconnect(x, buf->u_index1, buf->u_outletno,
            buf->u_index2, buf->u_inletno);
    }
    else if (action == UNDO_FREE)
        t_freebytes(buf, sizeof(*buf));
}

    /* connect just calls disconnect actions backward... */
static void *canvas_undo_set_connect(t_canvas *x,
    int index1, int outno, int index2, int inno)
{
    return (canvas_undo_set_disconnect(x, index1, outno, index2, inno));
}

static void canvas_undo_connect(t_canvas *x, void *z, int action)
{
    int myaction;
    if (action == UNDO_UNDO)
        myaction = UNDO_REDO;
    else if (action == UNDO_REDO)
        myaction = UNDO_UNDO;
    else myaction = action;
    canvas_undo_disconnect(x, z, myaction);
}

/* ---------- ... 2. cut, clear, and typing into objects: -------- */

#define UCUT_CUT 1          /* operation was a cut */
#define UCUT_CLEAR 2        /* .. a clear */
#define UCUT_TEXT 3         /* text typed into a box */

typedef struct _undo_cut        
{
    t_binbuf *u_objectbuf;      /* the object cleared or typed into */
    t_binbuf *u_reconnectbuf;   /* connections into and out of object */
    t_binbuf *u_redotextbuf;    /* buffer to paste back for redo if TEXT */
    int u_mode;                 /* from flags above */
} t_undo_cut;

static void *canvas_undo_set_cut(t_canvas *x, int mode)
{
    t_undo_cut *buf;
    t_gobj *y;
    t_linetraverser t;
    t_outconnect *oc;
    int nnotsel= glist_selectionindex(x, 0, 0);
    buf = (t_undo_cut *)getbytes(sizeof(*buf));
    buf->u_mode = mode;
    buf->u_redotextbuf = 0;

        /* store connections into/out of the selection */
    buf->u_reconnectbuf = binbuf_new();
    linetraverser_start(&t, x);
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
	if (linetraverser_next(&t)) {
		while (oc = linetraverser_next(&t))
		{
		    int issel1 = glist_isselected(x, &t.tr_ob->ob_g);
		    int issel2 = glist_isselected(x, &t.tr_ob2->ob_g);
		    if (issel1 != issel2)
		    {
		        binbuf_addv(buf->u_reconnectbuf, "ssiiii;",
		            gensym("#X"), gensym("connect"),
		            (issel1 ? nnotsel : 0)
		                + glist_selectionindex(x, &t.tr_ob->ob_g, issel1),
		            t.tr_outno,
		            (issel2 ? nnotsel : 0) +
		                glist_selectionindex(x, &t.tr_ob2->ob_g, issel2),
		            t.tr_inno);
		    }
		}
	}
Miller Puckette's avatar
Miller Puckette committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
    if (mode == UCUT_TEXT)
    {
        buf->u_objectbuf = canvas_docopy(x);
    }
    else if (mode == UCUT_CUT)
    {
        buf->u_objectbuf = 0;
    }
    else if (mode == UCUT_CLEAR)
    {
        buf->u_objectbuf = canvas_docopy(x);
    }
    return (buf);
}

static void canvas_undo_cut(t_canvas *x, void *z, int action)
{
654
	//fprintf(stderr, "canvas_undo_cut canvas=%d buf=%d action=%d\n", (int)x, (int)z, action);
Miller Puckette's avatar
Miller Puckette committed
655
656
657
658
    t_undo_cut *buf = z;
    int mode = buf->u_mode;
    if (action == UNDO_UNDO)
    {
659
		//fprintf(stderr,"UNDO_UNDO\n");
660
661
        if (mode == UCUT_CUT) {
			//fprintf(stderr, "UCUT_CUT\n");
Miller Puckette's avatar
Miller Puckette committed
662
            canvas_dopaste(x, copy_binbuf);
663
664
665
		}
        else if (mode == UCUT_CLEAR) {
			//fprintf(stderr, "UCUT_CLEAR\n");
Miller Puckette's avatar
Miller Puckette committed
666
            canvas_dopaste(x, buf->u_objectbuf);
667
		}
Miller Puckette's avatar
Miller Puckette committed
668
669
        else if (mode == UCUT_TEXT)
        {
670
			//fprintf(stderr, "UCUT_TEXT\n");
Miller Puckette's avatar
Miller Puckette committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
            t_gobj *y1, *y2;
            glist_noselect(x);
            for (y1 = x->gl_list; y2 = y1->g_next; y1 = y2)
                ;
            if (y1)
            {
                if (!buf->u_redotextbuf)
                {
                    glist_noselect(x);
                    glist_select(x, y1);
                    buf->u_redotextbuf = canvas_docopy(x);
                    glist_noselect(x);
                }
                glist_delete(x, y1);
            }
            canvas_dopaste(x, buf->u_objectbuf);
        }
        pd_bind(&x->gl_pd, gensym("#X"));
        binbuf_eval(buf->u_reconnectbuf, 0, 0, 0);
        pd_unbind(&x->gl_pd, gensym("#X"));
    }
    else if (action == UNDO_REDO)
    {
694
		//fprintf(stderr,"UNDO_REDO\n");
Miller Puckette's avatar
Miller Puckette committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
        if (mode == UCUT_CUT || mode == UCUT_CLEAR)
            canvas_doclear(x);
        else if (mode == UCUT_TEXT)
        {
            t_gobj *y1, *y2;
            for (y1 = x->gl_list; y2 = y1->g_next; y1 = y2)
                ;
            if (y1)
                glist_delete(x, y1);
            canvas_dopaste(x, buf->u_redotextbuf);
            pd_bind(&x->gl_pd, gensym("#X"));
            binbuf_eval(buf->u_reconnectbuf, 0, 0, 0);
            pd_unbind(&x->gl_pd, gensym("#X"));
        }
    }
    else if (action == UNDO_FREE)
    {
712
		//fprintf(stderr,"UNDO_FREE\n");
Miller Puckette's avatar
Miller Puckette committed
713
714
715
716
717
718
        if (buf->u_objectbuf)
            binbuf_free(buf->u_objectbuf);
        if (buf->u_reconnectbuf)
            binbuf_free(buf->u_reconnectbuf);
        if (buf->u_redotextbuf)
            binbuf_free(buf->u_redotextbuf);
719
        if (buf != NULL) t_freebytes(buf, sizeof(*buf));
Miller Puckette's avatar
Miller Puckette committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
    }
}

/* --------- 3. motion, including "tidy up" and stretching ----------- */

typedef struct _undo_move_elem  
{
    int e_index;
    int e_xpix;
    int e_ypix;
} t_undo_move_elem;

typedef struct _undo_move       
{
    t_undo_move_elem *u_vec;
    int u_n;
} t_undo_move;

static int canvas_undo_already_set_move;

static void *canvas_undo_set_move(t_canvas *x, int selected)
{
    int x1, y1, x2, y2, i, indx;
    t_gobj *y;
    t_undo_move *buf =  (t_undo_move *)getbytes(sizeof(*buf));
    buf->u_n = selected ? glist_selectionindex(x, 0, 1) : glist_getindex(x, 0);
    buf->u_vec = (t_undo_move_elem *)getbytes(sizeof(*buf->u_vec) *
        (selected ? glist_selectionindex(x, 0, 1) : glist_getindex(x, 0)));
    if (selected)
    {
        for (y = x->gl_list, i = indx = 0; y; y = y->g_next, indx++)
            if (glist_isselected(x, y))
        {
            gobj_getrect(y, x, &x1, &y1, &x2, &y2);
            buf->u_vec[i].e_index = indx;
            buf->u_vec[i].e_xpix = x1;
            buf->u_vec[i].e_ypix = y1;
            i++;
        }
    }
    else
    {
        for (y = x->gl_list, indx = 0; y; y = y->g_next, indx++)
        {
            gobj_getrect(y, x, &x1, &y1, &x2, &y2);
            buf->u_vec[indx].e_index = indx;
            buf->u_vec[indx].e_xpix = x1;
            buf->u_vec[indx].e_ypix = y1;
        }
    }
    canvas_undo_already_set_move = 1;
    return (buf);
}

static void canvas_undo_move(t_canvas *x, void *z, int action)
{
    t_undo_move *buf = z;
777
778
	t_class *cl;
	int resortin = 0, resortout = 0;
Miller Puckette's avatar
Miller Puckette committed
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
    if (action == UNDO_UNDO || action == UNDO_REDO)
    {
        int i;
        for (i = 0; i < buf->u_n; i++)
        {
            int x1, y1, x2, y2, newx, newy;
            t_gobj *y;
            newx = buf->u_vec[i].e_xpix;
            newy = buf->u_vec[i].e_ypix;
            y = glist_nth(x, buf->u_vec[i].e_index);
            if (y)
            {
                gobj_getrect(y, x, &x1, &y1, &x2, &y2);
                gobj_displace(y, x, newx-x1, newy - y1);
                buf->u_vec[i].e_xpix = x1;
                buf->u_vec[i].e_ypix = y1;
795
796
797
				cl = pd_class(&y->g_pd);
		        if (cl == vinlet_class) resortin = 1;
		        else if (cl == voutlet_class) resortout = 1;
Miller Puckette's avatar
Miller Puckette committed
798
799
            }
        }
800
801
		if (resortin) canvas_resortinlets(x);
		if (resortout) canvas_resortoutlets(x);
Miller Puckette's avatar
Miller Puckette committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
    }
    else if (action == UNDO_FREE)
    {
        t_freebytes(buf->u_vec, buf->u_n * sizeof(*buf->u_vec));
        t_freebytes(buf, sizeof(*buf));
    }
}

/* --------- 4. paste (also duplicate) ----------- */

typedef struct _undo_paste      
{
    int u_index;    /* index of first object pasted */  
} t_undo_paste;

static void *canvas_undo_set_paste(t_canvas *x)
{
    t_undo_paste *buf =  (t_undo_paste *)getbytes(sizeof(*buf));
    buf->u_index = glist_getindex(x, 0);
    return (buf);
}

static void canvas_undo_paste(t_canvas *x, void *z, int action)
{
    t_undo_paste *buf = z;
    if (action == UNDO_UNDO)
    {
        t_gobj *y;
        glist_noselect(x);
        for (y = glist_nth(x, buf->u_index); y; y = y->g_next)
            glist_select(x, y);
        canvas_doclear(x);
    }
    else if (action == UNDO_REDO)
    {
        t_selection *sel;
        canvas_dopaste(x, copy_binbuf);
            /* if it was "duplicate" have to re-enact the displacement. */
        if (canvas_undo_name && canvas_undo_name[0] == 'd')
841
842
843
            //for (sel = x->gl_editor->e_selection; sel; sel = sel->sel_next)
            //    gobj_displace(sel->sel_what, x, 10, 10);
			canvas_paste_xyoffset(x);
Miller Puckette's avatar
Miller Puckette committed
844
845
846
847
848
849
850
851
852
853
854
    }
else if (action == UNDO_FREE)
        t_freebytes(buf, sizeof(*buf));
}

    /* recursively check for abstractions to reload as result of a save. 
    Don't reload the one we just saved ("except") though. */
    /*  LATER try to do the same trick for externs. */
static void glist_doreload(t_glist *gl, t_symbol *name, t_symbol *dir,
    t_gobj *except)
{
855
	//fprintf(stderr,"doreload\n");
Miller Puckette's avatar
Miller Puckette committed
856
857
    t_gobj *g;
    int i, nobj = glist_getindex(gl, 0);  /* number of objects */
858
    int hadwindow = gl->gl_havewindow;
Miller Puckette's avatar
Miller Puckette committed
859
860
861
862
863
864
865
866
867
868
869
    for (g = gl->gl_list, i = 0; g && i < nobj; i++)
    {
        if (g != except && pd_class(&g->g_pd) == canvas_class &&
            canvas_isabstraction((t_canvas *)g) &&
                ((t_canvas *)g)->gl_name == name &&
                    canvas_getdir((t_canvas *)g) == dir)
        {
                /* we're going to remake the object, so "g" will go stale.
                Get its index here, and afterward restore g.  Also, the
                replacement will be at the end of the list, so we don't
                do g = g->g_next in this case. */
870
			//fprintf(stderr, "rebuildlicious\n");
Miller Puckette's avatar
Miller Puckette committed
871
            int j = glist_getindex(gl, g);
872
873
            if (!gl->gl_havewindow)
                canvas_vis(glist_getcanvas(gl), 1);
Miller Puckette's avatar
Miller Puckette committed
874
875
876
877
878
879
880
881
882
883
884
885
886
            glist_noselect(gl);
            glist_select(gl, g);
            canvas_setundo(gl, canvas_undo_cut,
                canvas_undo_set_cut(gl, UCUT_CLEAR), "clear");
            canvas_doclear(gl);
            canvas_undo(gl);
            glist_noselect(gl);
            g = glist_nth(gl, j);
        }
        else
        {
            if (g != except && pd_class(&g->g_pd) == canvas_class)
                glist_doreload((t_canvas *)g, name, dir, except);
887
             	g = g->g_next;
Miller Puckette's avatar
Miller Puckette committed
888
889
        }
    }
890
    if (!hadwindow && gl->gl_havewindow)
891
        canvas_vis(glist_getcanvas(gl), 0);
Miller Puckette's avatar
Miller Puckette committed
892
893
}

894
895
896
897
    /* this flag stops canvases from being marked "dirty" if we have to touch
    them to reload an abstraction; also suppress window list update */
int glist_amreloadingabstractions = 0;

Miller Puckette's avatar
Miller Puckette committed
898
899
900
901
    /* call canvas_doreload on everyone */
void canvas_reload(t_symbol *name, t_symbol *dir, t_gobj *except)
{
    t_canvas *x;
Miller Puckette's avatar
Miller Puckette committed
902
    int dspwas = canvas_suspend_dsp();
Miller Puckette's avatar
Miller Puckette committed
903
    glist_amreloadingabstractions = 1;
Miller Puckette's avatar
Miller Puckette committed
904
905
906
        /* find all root canvases */
    for (x = canvas_list; x; x = x->gl_next)
        glist_doreload(x, name, dir, except);
907
    glist_amreloadingabstractions = 0;
Miller Puckette's avatar
Miller Puckette committed
908
    canvas_resume_dsp(dspwas);
Miller Puckette's avatar
Miller Puckette committed
909
910
}

911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
/* --------- 5. apply  ----------- */

typedef struct _undo_apply        
{
    t_binbuf *u_objectbuf;      /* the object cleared or typed into */
    t_binbuf *u_reconnectbuf;   /* connections into and out of object */
	int u_index;				/* index of the previous object */
} t_undo_apply;

static void *canvas_undo_set_apply(t_canvas *x, t_gobj *obj)
{
    t_undo_apply *buf;
    t_gobj *y;
    t_linetraverser t;
    t_outconnect *oc;
926
927
928
	/* enable editor (in case it is disabled) and select the object we are working on */
	if (!x->gl_edit)
		canvas_editmode(x, 1);
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
	if (!glist_isselected(x, obj))
		glist_select(x, obj);
    int nnotsel= glist_selectionindex(x, 0, 0); /* get number of all items for the offset below */
    buf = (t_undo_apply *)getbytes(sizeof(*buf));

    /* store connections into/out of the selection */
    buf->u_reconnectbuf = binbuf_new();
    linetraverser_start(&t, x);
    while (oc = linetraverser_next(&t))
    {
        int issel1 = glist_isselected(x, &t.tr_ob->ob_g);
        int issel2 = glist_isselected(x, &t.tr_ob2->ob_g);
        if (issel1 != issel2)
        {
            binbuf_addv(buf->u_reconnectbuf, "ssiiii;",
                gensym("#X"), gensym("connect"),
                (issel1 ? nnotsel : 0)
                    + glist_selectionindex(x, &t.tr_ob->ob_g, issel1),
                t.tr_outno,
                (issel2 ? nnotsel : 0) +
                    glist_selectionindex(x, &t.tr_ob2->ob_g, issel2),
                t.tr_inno);
        }
    }
	/* copy object in its current state */
    buf->u_objectbuf = canvas_docopy(x);

	/* store index of the currently selected object */
957
	buf->u_index = glist_getindex(x, obj);
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003

    return (buf);
}

static void canvas_undo_apply(t_canvas *x, void *z, int action)
{
    t_undo_apply *buf = z;
    if (action == UNDO_UNDO || action == UNDO_REDO)
    {
		/* find current instance */
		glist_noselect(x);
		glist_select(x, glist_nth(x, buf->u_index));

		/* copy it for the new undo/redo */
		t_binbuf *tmp = canvas_docopy(x);

		/* delete current instance */
		canvas_doclear(x);

		/* replace it with previous instance */
        canvas_dopaste(x, buf->u_objectbuf);

		/* change previous instance with current one */
		buf->u_objectbuf = tmp;
		buf->u_index = glist_selectionindex(x, 0, 0);

		/* connections should stay the same */
        pd_bind(&x->gl_pd, gensym("#X"));
        binbuf_eval(buf->u_reconnectbuf, 0, 0, 0);
        pd_unbind(&x->gl_pd, gensym("#X"));
    }
    else if (action == UNDO_FREE)
    {
        if (buf->u_objectbuf)
            binbuf_free(buf->u_objectbuf);
        if (buf->u_reconnectbuf)
            binbuf_free(buf->u_reconnectbuf);
        t_freebytes(buf, sizeof(*buf));
    }
}

void canvas_apply_setundo(t_canvas *x, t_gobj *y)
{
	canvas_setundo(x, canvas_undo_apply, canvas_undo_set_apply(x, y), "apply");
}

1004
1005
1006
1007
1008
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
1039
1040
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
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
/* --------- 6. arrange (to front/back)  ----------- */

typedef struct _undo_arrange       
{
	int u_previndex;			/* old index */
	int u_newindex;				/* new index */
} t_undo_arrange;

static void *canvas_undo_set_arrange(t_canvas *x, t_gobj *obj, int newindex)
{
	// newindex tells us is the new index at the beginning (0) or the end (1)

    t_undo_arrange *buf;
    t_gobj *y;
	/* enable editor (in case it is disabled) and select the object we are working on */
	if (!x->gl_edit)
		canvas_editmode(x, 1);

	// select the object
	if (!glist_isselected(x, obj))
		glist_select(x, obj);

    buf = (t_undo_arrange *)getbytes(sizeof(*buf));

	// set the u_newindex appropriately
	if (newindex == 0) buf->u_newindex = 0;
	else buf->u_newindex = glist_getindex(x, 0) - 1;

	/* store index of the currently selected object */
	buf->u_previndex = glist_getindex(x, obj);

	//fprintf(stderr,"undo_set_arrange %d %d\n", buf->u_previndex, buf->u_newindex);		

    return (buf);
}

static void canvas_undo_arrange(t_canvas *x, void *z, int action)
{
    t_undo_arrange *buf = z;
	t_gobj *y=NULL, *prev=NULL, *next=NULL;

	if (!x->gl_edit)
		canvas_editmode(x, 1);

    if (action == UNDO_UNDO)
    {
		// this is our object
		y = glist_nth(x, buf->u_newindex);

		//fprintf(stderr,"canvas_undo_arrange UNDO_UNDO %d %d\n", buf->u_previndex, buf->u_newindex);		

		/* select object */
		glist_noselect(x);
		glist_select(x, y);

		if (buf->u_newindex) {
			// if it is the last object
			
			// first previous object should point to nothing
			prev = glist_nth(x, buf->u_newindex - 1);
			prev->g_next = NULL;	

			/* now we reuse vars for the follwoing:
			   old index should be right before the object previndex
			   is pointing to as the object was moved to the end */

			/* old position is not first */
			if (buf->u_previndex) {
				prev = glist_nth(x, buf->u_previndex - 1);
				next = prev->g_next;

				// now readjust pointers
				prev->g_next = y;
				y->g_next = next;
			}
			/* old position is first */
			else {
				prev = NULL;
				next = x->gl_list;

				// now readjust pointers
				y->g_next = next;
				x->gl_list = y;
			}

			// and finally redraw canvas
			canvas_redraw(x);
		}
		else {
			// if it is the first object

			/* old index should be right after the object previndex
			   is pointing to as the object was moved to the end */
			prev = glist_nth(x, buf->u_previndex);

			// next may be NULL and that is ok
			next = prev->g_next;

			//first glist pointer needs to point to the second object
			x->gl_list = y->g_next;

			//now readjust pointers
			prev->g_next = y;
			y->g_next = next;

			// and finally redraw canvas
			canvas_redraw(x);
		}
    }
	else if (action == UNDO_REDO) {
		// find our object
		y = glist_nth(x, buf->u_previndex);

		//fprintf(stderr,"canvas_undo_arrange UNDO_REDO %d %d\n", buf->u_previndex, buf->u_newindex);	

		/* select object */
		glist_noselect(x);
		glist_select(x, y);

		int action;
		if (!buf->u_newindex) action = 4;
		else action = 3;

		t_gobj *oldy_prev=NULL, *oldy_next=NULL;

1129
		// if there is an object before ours (in other words our index is > 0)
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
		if (glist_getindex(x,y))
			oldy_prev = glist_nth(x, buf->u_previndex - 1);
			
		// if there is an object after ours
		if (y->g_next)
			oldy_next = y->g_next;

		canvas_doarrange(x, action, y, oldy_prev, oldy_next);
	}
    else if (action == UNDO_FREE)
    {
        t_freebytes(buf, sizeof(*buf));
    }
}

void canvas_arrange_setundo(t_canvas *x, t_gobj *obj, int newindex)
{
	canvas_setundo(x, canvas_undo_arrange, canvas_undo_set_arrange(x, obj, newindex), "arrange");
}

1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
/* --------- 7. apply on canvas ----------- */

typedef struct _undo_canvas_properties      
{
    int gl_pixwidth;            /* width in pixels (on parent, if a graph) */
    int gl_pixheight;
    t_float gl_x1;                /* bounding rectangle in our own coordinates */
    t_float gl_y1;
    t_float gl_x2;
    t_float gl_y2;
    int gl_screenx1;            /* screen coordinates when toplevel */
    int gl_screeny1;
    int gl_screenx2;
    int gl_screeny2;
    int gl_xmargin;                /* origin for GOP rectangle */
    int gl_ymargin;

    unsigned int gl_goprect:1;      /* draw rectangle for graph-on-parent */
    unsigned int gl_isgraph:1;      /* show as graph on parent */
    unsigned int gl_hidetext:1;     /* hide object-name + args when doing graph on parent */
} t_undo_canvas_properties;

Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
1172
t_undo_canvas_properties global_buf;
1173
1174
1175
1176
1177
1178
1179

static void *canvas_undo_set_canvas(t_canvas *x)
{
	/* enable editor (in case it is disabled) and select the object we are working on */
	if (!x->gl_edit)
		canvas_editmode(x, 1);

Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
	global_buf.gl_pixwidth = x->gl_pixwidth;
	global_buf.gl_pixheight = x->gl_pixheight;
	global_buf.gl_x1 = x->gl_x1;
	global_buf.gl_y1 = x->gl_y1;
	global_buf.gl_x2 = x->gl_x2;
	global_buf.gl_y2 = x->gl_y2;
	global_buf.gl_screenx1 = x->gl_screenx1;
	global_buf.gl_screeny1 = x->gl_screeny1;
	global_buf.gl_screenx2 = x->gl_screenx2;
	global_buf.gl_screeny2 = x->gl_screeny2;
	global_buf.gl_xmargin = x->gl_xmargin;
	global_buf.gl_ymargin = x->gl_ymargin;
	global_buf.gl_goprect = x->gl_goprect;
	global_buf.gl_isgraph = x->gl_isgraph;
	global_buf.gl_hidetext = x->gl_hidetext;
1195
	
1196
    return (&global_buf);
1197
1198
1199
1200
1201
1202
1203
}

extern int gfxstub_haveproperties(void *key);

static void canvas_undo_canvas_apply(t_canvas *x, void *z, int action)
{
    t_undo_canvas_properties *buf = z;
1204
    t_undo_canvas_properties tmp;
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217

	if (!x->gl_edit)
		canvas_editmode(x, 1);

	if (action == UNDO_UNDO || action == UNDO_REDO)
	{
		//close properties window first
		t_int properties = gfxstub_haveproperties((void *)x);
		if (properties) {
			sys_vgui("destroy .gfxstub%lx\n", properties);
		}

		//store current canvas values into temporary data holder
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
		tmp.gl_pixwidth = x->gl_pixwidth;
		tmp.gl_pixheight = x->gl_pixheight;
		tmp.gl_x1 = x->gl_x1;
		tmp.gl_y1 = x->gl_y1;
		tmp.gl_x2 = x->gl_x2;
		tmp.gl_y2 = x->gl_y2;
		tmp.gl_screenx1 = x->gl_screenx1;
		tmp.gl_screeny1 = x->gl_screeny1;
		tmp.gl_screenx2 = x->gl_screenx2;
		tmp.gl_screeny2 = x->gl_screeny2;
		tmp.gl_xmargin = x->gl_xmargin;
		tmp.gl_ymargin = x->gl_ymargin;
		tmp.gl_goprect = x->gl_goprect;
		tmp.gl_isgraph = x->gl_isgraph;
		tmp.gl_hidetext = x->gl_hidetext;
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251

		//change canvas values with the ones from the undo buffer
		x->gl_pixwidth = buf->gl_pixwidth;
		x->gl_pixheight = buf->gl_pixheight;
		x->gl_x1 = buf->gl_x1;
		x->gl_y1 = buf->gl_y1;
		x->gl_x2 = buf->gl_x2;
		x->gl_y2 = buf->gl_y2;
		x->gl_screenx1 = buf->gl_screenx1;
		x->gl_screeny1 = buf->gl_screeny1;
		x->gl_screenx2 = buf->gl_screenx2;
		x->gl_screeny2 = buf->gl_screeny2;
		x->gl_xmargin = buf->gl_xmargin;
		x->gl_ymargin = buf->gl_ymargin;
		x->gl_goprect = buf->gl_goprect;
		x->gl_isgraph = buf->gl_isgraph;
		x->gl_hidetext = buf->gl_hidetext;

		//copy data values from the temporary data to the undo buffer
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
		buf->gl_pixwidth = tmp.gl_pixwidth;
		buf->gl_pixheight = tmp.gl_pixheight;
		buf->gl_x1 = tmp.gl_x1;
		buf->gl_y1 = tmp.gl_y1;
		buf->gl_x2 = tmp.gl_x2;
		buf->gl_y2 = tmp.gl_y2;
		buf->gl_screenx1 = tmp.gl_screenx1;
		buf->gl_screeny1 = tmp.gl_screeny1;
		buf->gl_screenx2 = tmp.gl_screenx2;
		buf->gl_screeny2 = tmp.gl_screeny2;
		buf->gl_xmargin = tmp.gl_xmargin;
		buf->gl_ymargin = tmp.gl_ymargin;
		buf->gl_goprect = tmp.gl_goprect;
		buf->gl_isgraph = tmp.gl_isgraph;
		buf->gl_hidetext = tmp.gl_hidetext;
1267

1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
		//redraw
		canvas_setgraph(x, x->gl_isgraph, 0);
		canvas_dirty(x, 1);
		if (x->gl_havewindow) {
		    canvas_redraw(x);
		}
		if (x->gl_owner && glist_isvisible(x->gl_owner))
		{
			glist_noselect(x);
		    gobj_vis(&x->gl_gobj, x->gl_owner, 0);
		    gobj_vis(&x->gl_gobj, x->gl_owner, 1);
			canvas_redraw(x->gl_owner);
		}
		//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
1284
1285
1286
1287
1288
		if (!x->gl_isgraph && x->gl_owner) {
			canvas=canvas->gl_owner;
			canvas_redraw(canvas);
		}
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x);
1289
1290
1291
1292
1293
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)canvas);
	}

    else if (action == UNDO_FREE)
    {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
1294
		//do nothing since undo apply uses a global_buf struct rather than a pointer
1295
1296
1297
1298
1299
1300
1301
    }
}

void canvas_canvas_setundo(t_canvas *x)
{
	canvas_setundo(x, canvas_undo_canvas_apply, canvas_undo_set_canvas(x), "apply");
}
1302

1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
/* --------- 8. create ----------- */

typedef struct _undo_create      
{
    int u_index;    /* index of the created object object */
    t_binbuf *u_objectbuf;      /* the object cleared or typed into */
    t_binbuf *u_reconnectbuf;   /* connections into and out of object */
} t_undo_create;

void *canvas_undo_set_create(t_canvas *x)
{
    t_gobj *y, *last;
    t_linetraverser t;
    t_outconnect *oc;

    t_undo_create *buf = (t_undo_create *)getbytes(sizeof(*buf));
    buf->u_index = glist_getindex(x, 0) - 1;
    int nnotsel= glist_selectionindex(x, 0, 0);

    buf->u_objectbuf = binbuf_new();
1323
1324
1325
1326
1327
1328
	if (x->gl_list) {
		for (y = x->gl_list; y; y = y->g_next)
		{
		    if (glist_isselected(x, y)) {
		        gobj_save(y, buf->u_objectbuf);
			}
1329
		}
1330
	}
1331
1332
    buf->u_reconnectbuf = binbuf_new();
    linetraverser_start(&t, x);
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
	if (linetraverser_next(&t)) {
		while (oc = linetraverser_next(&t))
		{
		    int issel1 = glist_isselected(x, &t.tr_ob->ob_g);
		    int issel2 = glist_isselected(x, &t.tr_ob2->ob_g);
		    if (issel1 != issel2)
		    {
		        binbuf_addv(buf->u_reconnectbuf, "ssiiii;",
		            gensym("#X"), gensym("connect"),
		            (issel1 ? nnotsel : 0)
		                + glist_selectionindex(x, &t.tr_ob->ob_g, issel1),
		            t.tr_outno,
		            (issel2 ? nnotsel : 0) +
		                glist_selectionindex(x, &t.tr_ob2->ob_g, issel2),
		            t.tr_inno);
		    }
		}
	}
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
    return (buf);
}

void canvas_undo_create(t_canvas *x, void *z, int action)
{
    t_undo_create *buf = z;
    t_gobj *y;

	//fprintf(stderr,"canvas = %lx buf->u_index = %d\n", (t_int)x, buf->u_index);

    if (action == UNDO_UNDO)
    {
        glist_noselect(x);
        y = glist_nth(x, buf->u_index);
        glist_select(x, y);
        canvas_doclear(x);
    }
    else if (action == UNDO_REDO)
    {
        pd_bind(&x->gl_pd, gensym("#X"));
   		binbuf_eval(buf->u_objectbuf, 0, 0, 0);
    	pd_unbind(&x->gl_pd, gensym("#X"));
        pd_bind(&x->gl_pd, gensym("#X"));
   		binbuf_eval(buf->u_reconnectbuf, 0, 0, 0);
    	pd_unbind(&x->gl_pd, gensym("#X"));
        y = glist_nth(x, buf->u_index);
        glist_select(x, y);
    }
	else if (action == UNDO_FREE) {
		binbuf_free(buf->u_objectbuf);
		binbuf_free(buf->u_reconnectbuf);
        t_freebytes(buf, sizeof(*buf));
	}
}

Miller Puckette's avatar
Miller Puckette committed
1386
1387
1388
/* ------------------------ event handling ------------------------ */

static char *cursorlist[] = {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1389
1390
1391
1392
1393
1394
1395
    "$cursor_runmode_nothing",
    "$cursor_runmode_clickme",
    "$cursor_runmode_thicken",
    "$cursor_runmode_addpoint",
    "$cursor_editmode_nothing",
    "$cursor_editmode_connect",
    "$cursor_editmode_disconnect"
Miller Puckette's avatar
Miller Puckette committed
1396
1397
1398
1399
1400
1401
1402
1403
};

void canvas_setcursor(t_canvas *x, unsigned int cursornum)
{
    static t_canvas *xwas;
    static unsigned int cursorwas;
    if (cursornum >= sizeof(cursorlist)/sizeof *cursorlist)
    {
1404
	bug("canvas_setcursor");
Miller Puckette's avatar
Miller Puckette committed
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
        return;
    }
    if (xwas != x || cursorwas != cursornum)
    {
        sys_vgui(".x%lx configure -cursor %s\n", x, cursorlist[cursornum]);
        xwas = x;
        cursorwas = cursornum;
    }
}

    /* check if a point lies in a gobj.  */
int canvas_hitbox(t_canvas *x, t_gobj *y, int xpos, int ypos,
    int *x1p, int *y1p, int *x2p, int *y2p)
{
    int x1, y1, x2, y2;
    t_text *ob;
1421
1422
    if (!gobj_shouldvis(y, x))
        return (0);
Miller Puckette's avatar
Miller Puckette committed
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
    gobj_getrect(y, x, &x1, &y1, &x2, &y2);
    if (xpos >= x1 && xpos <= x2 && ypos >= y1 && ypos <= y2)
    {
        *x1p = x1;
        *y1p = y1;
        *x2p = x2;
        *y2p = y2;
        return (1);
    }
    else return (0);
}

    /* find the last gobj, if any, containing the point. */
static t_gobj *canvas_findhitbox(t_canvas *x, int xpos, int ypos,
    int *x1p, int *y1p, int *x2p, int *y2p)
{
    t_gobj *y, *rval = 0;
    int x1, y1, x2, y2;
    *x1p = -0x7fffffff;
    for (y = x->gl_list; y; y = y->g_next)
    {
        if (canvas_hitbox(x, y, xpos, ypos, &x1, &y1, &x2, &y2)
            && (x1 > *x1p))
                *x1p = x1, *y1p = y1, *x2p = x2, *y2p = y2, rval = y; 
    }
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
        /* if there are at least two selected objects, we'd prefer
        to find a selected one (never mind which) to the one we got. */
    if (x->gl_editor && x->gl_editor->e_selection &&
        x->gl_editor->e_selection->sel_next && !glist_isselected(x, y))
    {
        t_selection *sel;
        for (sel = x->gl_editor->e_selection; sel; sel = sel->sel_next)
            if (canvas_hitbox(x, sel->sel_what, xpos, ypos, &x1, &y1, &x2, &y2))
                *x1p = x1, *y1p = y1, *x2p = x2, *y2p = y2,
                    rval = sel->sel_what; 
    }
Miller Puckette's avatar
Miller Puckette committed
1459
1460
1461
1462
1463
1464
    return (rval);
}

    /* right-clicking on a canvas object pops up a menu. */
static void canvas_rightclick(t_canvas *x, int xpos, int ypos, t_gobj *y)
{
1465
    int canprop, canopen, isobject;
1466
1467
1468
	/* abstractions should only allow for properties inside them 
	   otherwise they end-up being dirty without visible notification
	   besides, why would one mess with their properties without
1469
1470
	   seeing what is inside them? CURRENTLY DISABLED */
    canprop = (!y || (y && class_getpropertiesfn(pd_class(&y->g_pd))) /*&& !canvas_isabstraction( ((t_glist*)y) )*/ );
Miller Puckette's avatar
Miller Puckette committed
1471
    canopen = (y && zgetfn(&y->g_pd, gensym("menu-open")));
1472
1473
1474
1475
1476
1477
	if (y) {
		isobject = 1;
	}
	else isobject = 0;
    sys_vgui("pdtk_canvas_popup .x%lx %d %d %d %d %d\n",
        x, xpos, ypos, canprop, canopen, isobject);
Miller Puckette's avatar
Miller Puckette committed
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
1503
1504
/* ----  editors -- perhaps this and "vis" should go to g_editor.c ------- */

static t_editor *editor_new(t_glist *owner)
{
    char buf[40];
    t_editor *x = (t_editor *)getbytes(sizeof(*x));
    x->e_connectbuf = binbuf_new();
    x->e_deleted = binbuf_new();
    x->e_glist = owner;
    sprintf(buf, ".x%lx", (t_int)owner);
    x->e_guiconnect = guiconnect_new(&owner->gl_pd, gensym(buf));
    return (x);
}

static void editor_free(t_editor *x, t_glist *y)
{
    glist_noselect(y);
    guiconnect_notarget(x->e_guiconnect, 1000);
    binbuf_free(x->e_connectbuf);
    binbuf_free(x->e_deleted);
    freebytes((void *)x, sizeof(*x));
}

    /* recursively create or destroy all editors of a glist and its 
    sub-glists, as long as they aren't toplevels. */
Miller Puckette's avatar
test7    
Miller Puckette committed
1505
void canvas_create_editor(t_glist *x)
1506
1507
1508
{
    t_gobj *y;
    t_object *ob;
Miller Puckette's avatar
test7    
Miller Puckette committed
1509
    if (!x->gl_editor)
1510
    {
Miller Puckette's avatar
test7    
Miller Puckette committed
1511
1512
1513
1514
        x->gl_editor = editor_new(x);
        for (y = x->gl_list; y; y = y->g_next)
            if (ob = pd_checkobject(&y->g_pd))
                rtext_new(x, ob);
1515
    }
Miller Puckette's avatar
test7    
Miller Puckette committed
1516
1517
1518
1519
1520
1521
}

void canvas_destroy_editor(t_glist *x)
{
    t_gobj *y;
    t_object *ob;
1522
	glist_noselect(x);
Miller Puckette's avatar
test7    
Miller Puckette committed
1523
    if (x->gl_editor)
1524
    {
1525
1526
1527
1528
1529
		if (x->gl_list) {
        	for (y = x->gl_list; y; y = y->g_next)
        	    if (ob = pd_checkobject(&y->g_pd))
        	        rtext_free(glist_findrtext(x, ob));
		}
1530
1531
1532
1533
		//if (x->gl_editor) {
		editor_free(x->gl_editor, x);
		x->gl_editor = 0;
		//}
1534
	}
1535
1536
1537
1538
1539
1540
1541
1542
}

void canvas_reflecttitle(t_canvas *x);
void canvas_map(t_canvas *x, t_floatarg f);

    /* we call this when we want the window to become visible, mapped, and
    in front of all windows; or with "f" zero, when we want to get rid of
    the window. */
1543
1544
1545
1546
1547
//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);

1548
1549
void canvas_vis(t_canvas *x, t_floatarg f)
{
1550
	//fprintf(stderr,"canvas_vis %f\n", f);
1551
1552
    char buf[30];
    int flag = (f != 0);
1553
1554
    if (x != glist_getcanvas(x))
        bug("canvas_vis");
1555
1556
    if (flag)
    {
1557
1558
1559
1560
1561
        /* post("havewindow %d, isgraph %d, isvisible %d  editor %d",
            x->gl_havewindow, x->gl_isgraph, glist_isvisible(x),
                (x->gl_editor != 0)); */
            /* test if we're already visible and toplevel */
        if (x->gl_editor)
1562
        {           /* just put us in front */
1563
1564
1565
1566
#ifdef MSW
            canvas_vis(x, 0);
            canvas_vis(x, 1);
#else
1567
1568
            sys_vgui("raise .x%lx\n", x);
            sys_vgui("focus .x%lx.c\n", x);
1569
            sys_vgui("wm deiconify .x%lx\n", x);
1570
#endif
1571
1572
1573
        }
        else
        {
1574
			//fprintf(stderr,"new window\n");
Miller Puckette's avatar
test7    
Miller Puckette committed
1575
            canvas_create_editor(x);
1576
            sys_vgui("catch {pdtk_canvas_new .x%lx %d %d +%d+%d %d}\n", x,
1577
1578
1579
1580
1581
1582
                (int)(x->gl_screenx2 - x->gl_screenx1),
                (int)(x->gl_screeny2 - x->gl_screeny1),
                (int)(x->gl_screenx1), (int)(x->gl_screeny1),
                x->gl_edit);
            canvas_reflecttitle(x);
            x->gl_havewindow = 1;
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
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
/*
			//newly opened arrays created prior to pd-l2ork require fittograph
			t_gobj *g, *gg = NULL;
			t_garray *ga = NULL;
			t_array *a = NULL;
			int  num_elem = 0;
			t_text *t = NULL;
			int objnamesize;
			char *objname;

			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);
					}
				}
				//for garrays in gop inside a newly opened patch window
				else {
					t_text *t = (t_text *)g;
					t_rtext *y = glist_findrtext(x, t);
					if (y) {
						rtext_gettext(y, &objname, &objnamesize);
						//fprintf(stderr,"objname %s\n", objname);					
						if (!strcmp(objname, "graph ")) {
							//fprintf(stderr,"got a graph\n");
							for (gg = ((t_glist *)g)->gl_list; gg; gg = gg->g_next) {
								//fprintf(stderr, "sub-searching\n");
								if (pd_class(&gg->g_pd) == garray_class) {
									//fprintf(stderr,"sub found ya\n");
									ga = (t_garray *)gg;
									if (ga) {
										a = garray_getarray(ga);
										num_elem = a->a_n;
										garray_fittograph(ga, num_elem);
										canvas_dirty(x, 2);
									}
								}
							}					
						}
						//else fprintf(stderr,"fail %d >%s< >graph<\n", strcmp(objname, "graph"), objname);
					}
				}
			}
*/
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
            canvas_updatewindowlist();
        }
    }
    else    /* make invisible */
    {
        int i;
        t_canvas *x2;
        if (!x->gl_havewindow)
        {
                /* bug workaround -- a graph in a visible patch gets "invised"
                when the patch is closed, and must lose the editor here.  It's
                probably not the natural place to do this.  Other cases like
                subpatches fall here too but don'd need the editor freed, so
                we check if it exists. */
            if (x->gl_editor)
Miller Puckette's avatar
test7    
Miller Puckette committed
1650
                canvas_destroy_editor(x);
1651
1652
1653
1654
1655
1656
            return;
        }
        sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x);
        glist_noselect(x);
        if (glist_isvisible(x))
            canvas_map(x, 0);
Miller Puckette's avatar
test7    
Miller Puckette committed
1657
        canvas_destroy_editor(x);
1658
1659
1660
        sys_vgui("destroy .x%lx\n", x);
        for (i = 1, x2 = x; x2; x2 = x2->gl_next, i++)
            ;
1661
        //sys_vgui(".mbar.find delete %d\n", i);
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
            /* if we're a graph on our parent, and if the parent exists
               and is visible, show ourselves on parent. */
        if (glist_isgraph(x) && x->gl_owner)
        {
            t_glist *gl2 = x->gl_owner;
            if (glist_isvisible(gl2))
                gobj_vis(&x->gl_gobj, gl2, 0);
            x->gl_havewindow = 0;
            if (glist_isvisible(gl2))
                gobj_vis(&x->gl_gobj, gl2, 1);
        }
        else x->gl_havewindow = 0;
        canvas_updatewindowlist();
    }
}

Miller Puckette's avatar
Miller Puckette committed
1678
1679
1680
1681
1682
1683
    /* set a canvas up as a graph-on-parent.  Set reasonable defaults for
    any missing paramters and redraw things if necessary. */
void canvas_setgraph(t_glist *x, int flag, int nogoprect)
{
    if (!flag && glist_isgraph(x))
    {
1684
        int hadeditor = (x->gl_editor != 0);
Miller Puckette's avatar
Miller Puckette committed
1685
1686
        if (x->gl_owner && !x->gl_loading && glist_isvisible(x->gl_owner))
            gobj_vis(&x->gl_gobj, x->gl_owner, 0);
1687
1688
        if (hadeditor)
            canvas_destroy_editor(x);
Miller Puckette's avatar
Miller Puckette committed
1689
        x->gl_isgraph = 0;
1690
		x->gl_hidetext = 0;
Miller Puckette's avatar
Miller Puckette committed
1691
1692
1693
1694
1695
        if (x->gl_owner && !x->gl_loading && glist_isvisible(x->gl_owner))
        {
            gobj_vis(&x->gl_gobj, x->gl_owner, 1);
            canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
        }
1696
		x->gl_goprect = 0;
Miller Puckette's avatar
Miller Puckette committed
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
    }
    else if (flag)
    {
        if (x->gl_pixwidth <= 0)
            x->gl_pixwidth = GLIST_DEFGRAPHWIDTH;

        if (x->gl_pixheight <= 0)
            x->gl_pixheight = GLIST_DEFGRAPHHEIGHT;

        if (x->gl_owner && !x->gl_loading && glist_isvisible(x->gl_owner))
1707
		{
Miller Puckette's avatar
Miller Puckette committed
1708
            gobj_vis(&x->gl_gobj, x->gl_owner, 0);
1709
		}
Miller Puckette's avatar
Miller Puckette committed
1710
1711
1712
1713
        x->gl_isgraph = 1;
        x->gl_hidetext = !(!(flag&2));
        if (!nogoprect && !x->gl_goprect)
        {
1714
1715
			/* Ivica Ico Bukvic 5/16/10 <ico@bukvic.net> */
			x->gl_goprect = 1;
Miller Puckette's avatar
Miller Puckette committed
1716
        }
1717
        if (glist_isvisible(x) && x->gl_goprect) {
Miller Puckette's avatar
Miller Puckette committed
1718
            glist_redraw(x);
1719
		}
Miller Puckette's avatar
Miller Puckette committed
1720
1721
1722
1723
1724
1725
1726
1727
        if (x->gl_owner && !x->gl_loading && glist_isvisible(x->gl_owner))
        {
            gobj_vis(&x->gl_gobj, x->gl_owner, 1);
            canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
        }
    }
}

1728
void garray_properties(t_garray *x, t_glist *canvas);
Miller Puckette's avatar
Miller Puckette committed
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755

    /* tell GUI to create a properties dialog on the canvas.  We tell
    the user the negative of the "pixel" y scale to make it appear to grow
    naturally upward, whereas pixels grow downward. */
void canvas_properties(t_glist *x)
{
    t_gobj *y;
    char graphbuf[200];
    if (glist_isgraph(x) != 0)
        sprintf(graphbuf,
            "pdtk_canvas_dialog %%s %g %g %d %g %g %g %g %d %d %d %d\n",
                0., 0.,
                glist_isgraph(x) ,//1,
                x->gl_x1, x->gl_y1, x->gl_x2, x->gl_y2, 
                (int)x->gl_pixwidth, (int)x->gl_pixheight,
                (int)x->gl_xmargin, (int)x->gl_ymargin);
    else sprintf(graphbuf,
            "pdtk_canvas_dialog %%s %g %g %d %g %g %g %g %d %d %d %d\n",
                glist_dpixtodx(x, 1), -glist_dpixtody(x, 1),
                0,
                0., -1., 1., 1., 
                (int)x->gl_pixwidth, (int)x->gl_pixheight,
                (int)x->gl_xmargin, (int)x->gl_ymargin);
    gfxstub_new(&x->gl_pd, x, graphbuf);
        /* if any arrays are in the graph, put out their dialogs too */
    for (y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == garray_class) 
1756
            garray_properties((t_garray *)y, x);
Miller Puckette's avatar
Miller Puckette committed
1757
1758
1759
1760
1761
1762
1763
}

    /* called from the gui when "OK" is selected on the canvas properties
        dialog.  Again we negate "y" scale. */
static void canvas_donecanvasdialog(t_glist *x,
    t_symbol *s, int argc, t_atom *argv)
{
1764
1765
	/* parent windows are treated differently than applies to individual objects */
	if (glist_getcanvas(x) != x && !canvas_isabstraction(x)) {
1766
		canvas_apply_setundo(glist_getcanvas(x), (t_gobj *)x);