g_editor.c 210 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
#include "g_canvas.h"
12
#include "s_utf8.h" /*-- moo --*/
13
#include "g_undo.h"
14
#include "x_preset.h"
Miller Puckette's avatar
Miller Puckette committed
15
#include <string.h>
16
#include "g_all_guis.h"
Miller Puckette's avatar
Miller Puckette committed
17
18
19
20
21
22
23

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

24
25
//static int toggle_moving = 0; //global variable

Miller Puckette's avatar
Miller Puckette committed
26
27
/* ------------------ forward declarations --------------- */
static void canvas_doclear(t_canvas *x);
28
void glist_setlastxy(t_glist *gl, int xval, int yval);
Miller Puckette's avatar
Miller Puckette committed
29
30
31
32
33
34
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;
35
36
//static char *canvas_textcopybuf;
//static int canvas_textcopybufsize;
Miller Puckette's avatar
Miller Puckette committed
37
static t_glist *glist_finddirty(t_glist *x);
38
39
40
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
41
static int paste_xyoffset = 0; /* a counter of pastes to make x,y offsets */
42
static void canvas_mouseup_gop(t_canvas *x, t_gobj *g);
43
44
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);
45
static void canvas_paste_xyoffset(t_canvas *x);
46
void canvas_setgraph(t_glist *x, int flag, int nogoprect);
47
void canvas_mouseup(t_canvas *x, t_floatarg fxpos, t_floatarg fypos, t_floatarg fwhich);
48
49
50
51
static int outlet_issignal = 0;
static int inlet_issignal = 0;
static int last_inlet_filter = 0;
static int last_outlet_filter = 0;
52
static int copyfromexternalbuffer = 0;
53
static int tooltips = 0;
54
static int objtooltip = 0;
55
56
57
58
59
60
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,
61
    t_floatarg yresize);
62
extern void canvas_setbounds(t_canvas *x, int x1, int y1, int x2, int y2);
63
int canvas_apply_restore_original_position(t_canvas *x, int orig_pos);
64
extern void canvas_draw_gop_resize_hooks(t_canvas *x);
65
static void canvas_font(t_canvas *x, t_floatarg font, t_floatarg oldfont, t_floatarg resize, t_floatarg preview);
66
67
// for updating preset_node locations in case of operations that alter glist object locations (tofront/back, cut, delete, undo/redo cut/delete)
extern void glob_preset_node_list_check_loc_and_update(void);
68
69
// for preset_node
extern t_class *text_class;
70

71
int do_not_redraw = 0;     // used to optimize redrawing
72
int old_displace = 0;	   // for legacy displaces within gop that are not visible to displaceselection
73
74
75

int connect_exception = 0; // used when autopatching to bypass check whether one is trying to connect signal with non-signal nlet
						   // since this is impossible to figure out when the newly created object is an empty one
76

77
78
79
// used to test whether the shift is pressed and if so, handle various connection exceptions (e.g. multiconnect)
int glob_lmclick = 0;
int glob_shift = 0;
80
81
82
83
84
int glob_ctrl = 0;
int glob_alt = 0;

static t_glist *canvas_last_glist;
static int canvas_last_glist_x, canvas_last_glist_y;
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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);
105
	if (bufsize > 3) bufsize = 3;
106
	for (i = 0; i < bufsize; i++) {
107
108
		name[i] = buf[i];
	}
109
	name[i]='\0';
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	//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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

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

143
144
145
146
147
148
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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);
}

167
168
169
int gobj_shouldvis(t_gobj *x, struct _glist *glist)
{
    t_object *ob;
170
	//fprintf(stderr,"shouldvis %d %d %d %d\n", glist->gl_havewindow, glist->gl_isgraph, glist->gl_goprect, glist->gl_owner != NULL);
171
    if (!glist->gl_havewindow && glist->gl_isgraph && glist->gl_goprect &&
172
        glist->gl_owner && (pd_class(&x->g_pd) != scalar_class) && (pd_class(&glist->gl_pd) != garray_class))
173
174
175
176
177
178
179
180
181
182
    {
        /* 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);
183
		//fprintf(stderr,"gobj_shouldvis gop: %d %d %d %d || object %d %d %d %d\n", x1, x2, y1, y2, gx1, gx2, gy1, gy2);
184
        if (gx1 < x1 || gx1 > x2 || gx2 < x1 || gx2 > x2 ||
185
186
            gy1 < y1 || gy1 > y2 || gy2 < y1 || gy2 > y2) {
				//fprintf(stderr,"does not fit within boundaries\n");
187
                return (0);
188
		}
189
190
		if (glist==glist_getcanvas(glist))
        	sys_vgui(".x%lx.c raise all_cords\n", glist_getcanvas(glist));
191
192
193
194
195
196
    }
    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. */
197
		//fprintf(stderr,"pd_checkobject %lx\n", x);
198
199
200
201
202
203
204
205
		/*fprintf(stderr,"pd_checkobject %d %d %d %d %d %d %d\n",
			glist->gl_havewindow, 
            (ob->te_pd != canvas_class ? 1:0),
			(ob->te_pd->c_wb != &text_widgetbehavior ? 1:0),
			(ob->te_pd == canvas_class ? 1:0),
			((t_glist *)ob)->gl_isgraph,
            glist->gl_goprect,
			(ob->te_type == T_TEXT ? 1:0));*/
206
207
208
209
210
		if (sys_k12_mode && x->g_pd == preset_hub_class) {
			//fprintf(stderr,"glist_select do not select invised preset_hub in K12 mode\n");
			t_preset_hub *ph = (t_preset_hub *)x;
			if (ph->ph_invis > 0) return(0);
		}
211
212
213
214
215
216
        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)));
    }
217
218
    else {
		//fprintf(stderr,"else return 1\n");
219
		return (1);
220
	}
221
222
}

Miller Puckette's avatar
Miller Puckette committed
223
224
void gobj_vis(t_gobj *x, struct _glist *glist, int flag)
{
225
	if (do_not_redraw) return;
226
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_visfn && gobj_shouldvis(x, glist))
Miller Puckette's avatar
Miller Puckette committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
        (*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 ----------------- */

241
242
void canvas_check_nlet_highlights(t_glist *x)
{
243
	if (x->gl_editor->canvas_cnct_inlet_tag[0] != 0)
244
245
	{
		sys_vgui(".x%x.c itemconfigure %s -outline %s -fill %s -width 1\n",
246
		       	x, x->gl_editor->canvas_cnct_inlet_tag,
247
248
249
250
251
252
				(last_inlet_filter ? "black" : (inlet_issignal ? "$signal_cord" : "$msg_cord")),
				(inlet_issignal ? "$signal_nlet" : "$msg_nlet"));
		if (objtooltip) {
			objtooltip = 0;
			sys_vgui("pdtk_canvas_leaveitem .x%x.c;\n", x);
		}
253
		x->gl_editor->canvas_cnct_inlet_tag[0] = 0;
254
255
256
257
258
259
		//if (x->gl_editor->e_onmotion == MA_CONNECT) {
			x->gl_editor->e_onmotion = MA_NONE;
			canvas_setcursor(x, CURSOR_EDITMODE_NOTHING);
		//}
	}

260
	if (x->gl_editor->canvas_cnct_outlet_tag[0] != 0 && x->gl_editor->e_onmotion != MA_CONNECT)
261
262
	{
		sys_vgui(".x%x.c itemconfigure %s -outline %s -fill %s -width 1\n",
263
		       	x, x->gl_editor->canvas_cnct_outlet_tag,
264
265
266
267
268
269
				(last_outlet_filter ? "black" : (outlet_issignal ? "$signal_cord" : "$msg_cord")),
				(outlet_issignal ? "$signal_nlet" : "$msg_nlet"));
		if (objtooltip) {
			objtooltip = 0;
			sys_vgui("pdtk_canvas_leaveitem .x%x.c;\n", x);
		}
270
		x->gl_editor->canvas_cnct_outlet_tag[0] = 0;
271
272
273
274
275
276
277
		//if (x->gl_editor->e_onmotion == MA_CONNECT) {
			x->gl_editor->e_onmotion = MA_NONE;
			canvas_setcursor(x, CURSOR_EDITMODE_NOTHING);
		//}            
	}
}

Miller Puckette's avatar
Miller Puckette committed
278
279
280
281
282
283
284
285
286
287
288
289
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
290
291
        sys_vgui(".x%lx.c itemconfigure l%lx -fill $select_color\n",
            x, x->gl_editor->e_selectline_tag);
292
293
294
        sys_vgui(".x%lx.c addtag selected withtag l%lx\n",
            glist_getcanvas(x), x->gl_editor->e_selectline_tag);
		c_selection = x;
295
		canvas_draw_gop_resize_hooks(x);
296
    }
Miller Puckette's avatar
Miller Puckette committed
297
298
299
300
301
302
}

void glist_deselectline(t_glist *x)
{
    if (x->gl_editor)
    {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
303
304
        t_linetraverser t;
        t_outconnect *oc;
Miller Puckette's avatar
Miller Puckette committed
305
        x->gl_editor->e_selectedline = 0;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
306
307
308
309
310
311
312
313
314
315
316
317
        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"));
318
319
        sys_vgui(".x%lx.c dtag l%lx selected\n",
            glist_getcanvas(x), glist_getcanvas(x)->gl_editor->e_selectline_tag);
320
		canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
    }    
}

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)
{
338
	//fprintf(stderr,"glist_select c_selection=%lx x=%lx\n", (t_int)c_selection, (t_int)x);
Miller Puckette's avatar
Miller Puckette committed
339
340
    if (x->gl_editor)
    {
341
		//fprintf(stderr,"have editor\n");
342
343
344
345
346
347
348
349
#ifdef PDL2ORK
		// exception: if we are in K12 mode and preset_hub is hidden, do not select it
		if (sys_k12_mode && y->g_pd == preset_hub_class) {
			//fprintf(stderr,"glist_select do not select invised preset_hub in K12 mode\n");
			t_preset_hub *ph = (t_preset_hub *)y;
			if (ph->ph_invis > 0) return;
		}
#endif
350
351
		if (c_selection && c_selection != x) {
			//fprintf(stderr,"deselecting\n");
352
			glist_noselect(c_selection);
353
		}
Miller Puckette's avatar
Miller Puckette committed
354
        t_selection *sel = (t_selection *)getbytes(sizeof(*sel));
355
356
        if (x->gl_editor->e_selectedline) {
			//fprintf(stderr,"deselecting line\n");
Miller Puckette's avatar
Miller Puckette committed
357
            glist_deselectline(x);
358
		}
Miller Puckette's avatar
Miller Puckette committed
359
360
361
362
363
364
            /* 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);
365
		c_selection = x;
366
367

		sys_vgui("pdtk_canvas_update_edit_menu .x%lx 1\n", x);
368
		canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
369
    }
370
	//fprintf(stderr,"select done\n");
Miller Puckette's avatar
Miller Puckette committed
371
372
}

Miller Puckette's avatar
Miller Puckette committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    /* 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);
}

387
388
int glist_getindex(t_glist *x, t_gobj *y);

Miller Puckette's avatar
Miller Puckette committed
389
390
391
    /* call this for selected objects only */
void glist_deselect(t_glist *x, t_gobj *y)
{
392
	//fprintf(stderr, "deselect\n");
Miller Puckette's avatar
Miller Puckette committed
393
394
    int fixdsp = 0;
    static int reenter = 0;
Miller Puckette's avatar
Miller Puckette committed
395
    /* if (reenter) return; */
Miller Puckette's avatar
Miller Puckette committed
396
397
398
399
400
401
    reenter = 1;
    if (x->gl_editor)
    {
        t_selection *sel, *sel2;
        t_rtext *z = 0;
        if (!glist_isselected(x, y)) bug("glist_deselect");
402
403
404
405
		// following information is for undo_apply
		// we need info on the old object's position
		// in the gl_list so that we can restore it
		int pos = glist_getindex(glist_getcanvas(x), y);
Miller Puckette's avatar
Miller Puckette committed
406
407
        if (x->gl_editor->e_textedfor)
        {
408
			//fprintf(stderr, "e_textedfor\n");
Miller Puckette's avatar
Miller Puckette committed
409
410
411
            t_rtext *fuddy = glist_findrtext(x, (t_text *)y);
            if (x->gl_editor->e_textedfor == fuddy)
            {
412
				//fprintf(stderr, "e_textedfor == fuddy\n");
Miller Puckette's avatar
Miller Puckette committed
413
414
                if (x->gl_editor->e_textdirty)
                {
415
					//fprintf(stderr, "textdirty yes\n");
Miller Puckette's avatar
Miller Puckette committed
416
417
                    z = fuddy;
                    canvas_stowconnections(glist_getcanvas(x));
Miller Puckette's avatar
Miller Puckette committed
418
                    glist_checkanddeselectall(x, y);
Miller Puckette's avatar
Miller Puckette committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                }
                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)
        {
447
			//fprintf(stderr, "setto\n");
Miller Puckette's avatar
Miller Puckette committed
448
449
450
451
            char *buf;
            int bufsize;

            rtext_gettext(z, &buf, &bufsize);
452
            text_setto((t_text *)y, x, buf, bufsize, pos);
Miller Puckette's avatar
Miller Puckette committed
453
454
455
456
457
            canvas_fixlinesfor(glist_getcanvas(x), (t_text *)y);
            x->gl_editor->e_textedfor = 0;
        }
        if (fixdsp)
            canvas_resume_dsp(1);
458
459
		if (!x->gl_editor->e_selection)
			sys_vgui("pdtk_canvas_update_edit_menu .x%lx 0\n", x);
460
		canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
461
462
463
464
465
466
    }
    reenter = 0;
}

void glist_noselect(t_glist *x)
{
467
	//fprintf(stderr,"glist_noselect\n");
Miller Puckette's avatar
Miller Puckette committed
468
469
    if (x->gl_editor)
    {
470
471
472
473
		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
474
475
        if (x->gl_editor->e_selectedline)
            glist_deselectline(x);
476
477
		if (c_selection == x)
			c_selection = NULL;
478
		canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
479
480
481
482
483
484
485
486
487
488
489
    }
}

void glist_selectall(t_glist *x)
{
    if (x->gl_editor)
    {
        glist_noselect(x);
        if (x->gl_list)
        {
            t_gobj *y = x->gl_list;
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#ifdef PDL2ORK
			// exception: if we are in K12 mode and preset_hub is hidden, do not select it
			if (sys_k12_mode && y->g_pd == preset_hub_class) {
				//fprintf(stderr,"glist_select do not select invised preset_hub in K12 mode\n");
				t_preset_hub *ph = (t_preset_hub *)y;
				if (ph->ph_invis > 0) y = y->g_next;
			}
#endif
			if (y) {
		        t_selection *sel = (t_selection *)getbytes(sizeof(*sel));
		        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;
				c_selection = x;
			}
Miller Puckette's avatar
Miller Puckette committed
514
        }
515
		canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
    }
}

    /* 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 */
561
const char *canvas_undo_name;
Miller Puckette's avatar
Miller Puckette committed
562
563
564
565

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

Miller Puckette's avatar
Miller Puckette committed
568
569
570
571
572
    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))
    {
573
		//fprintf(stderr,"hadone canvas_setundo\n");
Miller Puckette's avatar
Miller Puckette committed
574
575
576
577
578
579
580
581
        (*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;
582
583
584
    //if (x && glist_isvisible(x) && glist_istoplevel(x))
	if (x)
    	// enable undo in menu
Miller Puckette's avatar
Miller Puckette committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
        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)
{
600
	//fprintf(stderr,"canvas_undo\n");
Miller Puckette's avatar
Miller Puckette committed
601
602
603
604
605
606
    if (x != canvas_undo_canvas)
        bug("canvas_undo 1");
    else if (canvas_undo_whatnext != UNDO_UNDO)
        bug("canvas_undo 2");
    else
    {
607
        //fprintf(stderr,"undo\n");
Miller Puckette's avatar
Miller Puckette committed
608
609
610
611
612
        (*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;
613
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x);
Miller Puckette's avatar
Miller Puckette committed
614
615
616
617
618
    }
}

static void canvas_redo(t_canvas *x)
{
619
	//fprintf(stderr,"canvas_redo\n");
Miller Puckette's avatar
Miller Puckette committed
620
    if (x != canvas_undo_canvas)
621
        bug("canvas_redo 1");
Miller Puckette's avatar
Miller Puckette committed
622
    else if (canvas_undo_whatnext != UNDO_REDO)
623
        bug("canvas_redo 2");
Miller Puckette's avatar
Miller Puckette committed
624
625
626
627
628
629
630
631
    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;
632
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x);
Miller Puckette's avatar
Miller Puckette committed
633
634
635
    }
}

636
/* ------- specific undo methods: 1. connect -------- */
Miller Puckette's avatar
Miller Puckette committed
637
638
639
640
641
642
643
644
645

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

646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
void *canvas_undo_set_disconnect(t_canvas *x,
    int index1, int outno, int index2, int inno);

/* connect just calls disconnect actions backward... (see below) */
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));
}

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

/* ------- specific undo methods: 2. disconnect -------- */

void *canvas_undo_set_disconnect(t_canvas *x,
Miller Puckette's avatar
Miller Puckette committed
670
671
672
673
674
675
676
677
678
679
680
    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,
681
    t_float index1, t_float outno, t_float index2, t_float inno)
Miller Puckette's avatar
Miller Puckette committed
682
{
683
	//fprintf(stderr,"canvas_disconnect\n");
Miller Puckette's avatar
Miller Puckette committed
684
685
686
687
688
689
690
691
692
693
694
    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);
695
            // jsarlo
696
697
698
			if(x->gl_editor && x->gl_editor->gl_magic_glass) {
            	magicGlass_unbind(x->gl_editor->gl_magic_glass);
            	magicGlass_hide(x->gl_editor->gl_magic_glass);
699
700
			}
            // end jsarlo
Miller Puckette's avatar
Miller Puckette committed
701
            obj_disconnect(t.tr_ob, t.tr_outno, t.tr_ob2, t.tr_inno);
702
703
704

			// if we are dealing with a preset_node, make sure to also disconnect its invisible return node
			// we trust here that the object has been already connected to a valid object so we blindly
705
706
			// disconnect first outlet with the first inlet (EXCEPTION: print object that cannot be connected back to)
			if (pd_class(&t.tr_ob->ob_g.g_pd) == preset_node_class && pd_class(&t.tr_ob2->ob_g.g_pd) != print_class) {
707
708
709
				//fprintf(stderr,"gotta disconnect hidden one too...\n");
				obj_disconnect(t.tr_ob2, 0, t.tr_ob, 0);
			}
Miller Puckette's avatar
Miller Puckette committed
710
711
712
713
714
            break;
        }
    }
}

715
void canvas_undo_disconnect(t_canvas *x, void *z, int action)
Miller Puckette's avatar
Miller Puckette committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
{
    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));
}

732
/* ---------- ... 3. cut, clear, and typing into objects: -------- */
Miller Puckette's avatar
Miller Puckette committed
733
734
735

#define UCUT_CUT 1          /* operation was a cut */
#define UCUT_CLEAR 2        /* .. a clear */
736
737
738

// following action is not needed any more LATER remove any signs of UCUT_TEXT
// since recreate takes care of this in a more elegant way
Miller Puckette's avatar
Miller Puckette committed
739
740
741
742
743
744
745
746
#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 */
747
748
749
	int n_obj;					/* number of selected objects to be cut */
	int p_a[1];					/* array of original glist positions of selected objects */
								/* at least one object is selected, we dynamically resize it later */
Miller Puckette's avatar
Miller Puckette committed
750
751
} t_undo_cut;

752
void *canvas_undo_set_cut(t_canvas *x, int mode)
Miller Puckette's avatar
Miller Puckette committed
753
754
755
756
757
758
{
    t_undo_cut *buf;
    t_gobj *y;
    t_linetraverser t;
    t_outconnect *oc;
    int nnotsel= glist_selectionindex(x, 0, 0);
759
760
761
	int nsel = glist_selectionindex(x, 0, 1);
    buf = (t_undo_cut *)getbytes(sizeof(*buf) + sizeof(buf->p_a[0]) * (nsel - 1));
	buf->n_obj = nsel;
Miller Puckette's avatar
Miller Puckette committed
762
763
764
765
766
767
    buf->u_mode = mode;
    buf->u_redotextbuf = 0;

        /* store connections into/out of the selection */
    buf->u_reconnectbuf = binbuf_new();
    linetraverser_start(&t, x);
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
	//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);
	    }
784
	}
785
	//}
Miller Puckette's avatar
Miller Puckette committed
786
787
788
789
790
791
    if (mode == UCUT_TEXT)
    {
        buf->u_objectbuf = canvas_docopy(x);
    }
    else if (mode == UCUT_CUT)
    {
792
        buf->u_objectbuf = canvas_docopy(x);
Miller Puckette's avatar
Miller Puckette committed
793
794
795
796
797
    }
    else if (mode == UCUT_CLEAR)
    {
        buf->u_objectbuf = canvas_docopy(x);
    }
798
799
800
801
802
803
804
805
806
807

	//instantiate num_obj and fill array of positions of selected objects
	if (mode == UCUT_CUT || mode == UCUT_CLEAR)
	{
		int i = 0, j = 0;
		if (x->gl_list) {
			for (y = x->gl_list; y; y = y->g_next)
			{
				if (glist_isselected(x, y)) {
					buf->p_a[i] = j;
808
					i++;
809
810
811
812
813
814
815
816
				}
				j++;
			}
		}
		//for (i = 0; i < buf->n_obj; i++)
		//	fprintf(stderr,"%d position = %d\n", i, buf->p_a[i]);
	}

Miller Puckette's avatar
Miller Puckette committed
817
818
819
    return (buf);
}

820
void canvas_undo_cut(t_canvas *x, void *z, int action)
Miller Puckette's avatar
Miller Puckette committed
821
{
822
	//fprintf(stderr, "canvas_undo_cut canvas=%d buf=%d action=%d\n", (int)x, (int)z, action);
Miller Puckette's avatar
Miller Puckette committed
823
824
825
826
    t_undo_cut *buf = z;
    int mode = buf->u_mode;
    if (action == UNDO_UNDO)
    {
827
		do_not_redraw += 1;
828
		//fprintf(stderr,"UNDO_UNDO\n");
829
830
        if (mode == UCUT_CUT) {
			//fprintf(stderr, "UCUT_CUT\n");
831
            canvas_dopaste(x, buf->u_objectbuf);
832
833
834
		}
        else if (mode == UCUT_CLEAR) {
			//fprintf(stderr, "UCUT_CLEAR\n");
Miller Puckette's avatar
Miller Puckette committed
835
            canvas_dopaste(x, buf->u_objectbuf);
836
		}
Miller Puckette's avatar
Miller Puckette committed
837
838
        else if (mode == UCUT_TEXT)
        {
839
			//fprintf(stderr, "UCUT_TEXT\n");
Miller Puckette's avatar
Miller Puckette committed
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
            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"));
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874

		//now reposition objects to their original locations
		if (mode == UCUT_CUT || mode == UCUT_CLEAR) {
			//fprintf(stderr,"reordering\n");
			int i = 0;
			int paste_pos = glist_getindex(x,0) - buf->n_obj; //location of the first newly pasted object
			//fprintf(stderr,"paste_pos %d\n", paste_pos);
			t_gobj *y_prev, *y, *y_next;
			for (i = 0; i < buf->n_obj; i++) {
				//first check if we are in the same position already
				if (paste_pos+i != buf->p_a[i]) {
					//fprintf(stderr,"not in the right place\n");
					y_prev = glist_nth(x, paste_pos-1+i);
					y = glist_nth(x, paste_pos+i);
					y_next = glist_nth(x, paste_pos+1+i);
875
					//if the object is supposed to be first in the gl_list
876
877
878
879
880
881
882
883
884
					if (buf->p_a[i] == 0) {
						if (y_prev && y_next) {
							y_prev->g_next = y_next;
						}
						else if (y_prev && !y_next)
							y_prev->g_next = NULL;
						//now put the moved object at the beginning of the cue
						y->g_next = glist_nth(x, 0);
						x->gl_list = y;
885
						//LATER when objects are properly tagged lower y here
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
					}
					//if the object is supposed to be at the current end of gl_list	
					//can this ever happen???
					/*else if (!glist_nth(x,buf->p_a[i])) {

					}*/
					//if the object is supposed to be in the middle of gl_list
					else {
						if (y_prev && y_next) {
							y_prev->g_next = y_next;
						}
						else if (y_prev && !y_next) {
							y_prev->g_next = NULL;
						}
						//now put the moved object in its right place
						y_prev = glist_nth(x, buf->p_a[i]-1);
						y_next = glist_nth(x, buf->p_a[i]);

						y_prev->g_next = y;
						y->g_next = y_next;
906
						//LATER when objects are properly tagged lower y here
907
908
909
					}
				}
			}
910
			do_not_redraw -= 1;
911
			//LATER disable redrawing here
912
			canvas_redraw(x);
913
914
915
916
			if (x->gl_owner && glist_isvisible(x->gl_owner)) {
				gobj_vis((t_gobj *)x, x->gl_owner, 0);
				gobj_vis((t_gobj *)x, x->gl_owner, 1);
			}
917
			glob_preset_node_list_check_loc_and_update();
918
		}
Miller Puckette's avatar
Miller Puckette committed
919
920
921
    }
    else if (action == UNDO_REDO)
    {
922
		//fprintf(stderr,"UNDO_REDO\n");
923
924
925
926
927
928
929
930
        if (mode == UCUT_CUT || mode == UCUT_CLEAR) {
			//we can't just blindly do clear here when the user may have
			//unselected things between undo and redo, so first let's select
			//the right stuff
			glist_noselect(x);
			int i = 0;
			for (i = 0; i < buf->n_obj; i++)
            	glist_select(x, glist_nth(x, buf->p_a[i]));
Miller Puckette's avatar
Miller Puckette committed
931
            canvas_doclear(x);
932
			glob_preset_node_list_check_loc_and_update();
933
		}
Miller Puckette's avatar
Miller Puckette committed
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
        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)
    {
949
		//fprintf(stderr,"UNDO_FREE\n");
Miller Puckette's avatar
Miller Puckette committed
950
951
952
953
954
955
        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);
956
        if (buf != NULL) t_freebytes(buf, sizeof(*buf) + sizeof(buf->p_a[0]) * (buf->n_obj-1));
Miller Puckette's avatar
Miller Puckette committed
957
958
959
    }
}

960
/* --------- 4. motion, including "tidy up" and stretching ----------- */
Miller Puckette's avatar
Miller Puckette committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976

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;

977
void *canvas_undo_set_move(t_canvas *x, int selected)
Miller Puckette's avatar
Miller Puckette committed
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
    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++)
        {