g_editor.c 264 KB
Newer Older
1 2 3
/* Copyright (c) 2009-2013 Ivica Ico Bukvic and others. Code based on Pure-Data source

Original Pure-Data source copyright (c) 1997-2001 Miller Puckette and others.
Miller Puckette's avatar
Miller Puckette committed
4
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
5 6 7
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

*/
Miller Puckette's avatar
Miller Puckette committed
8 9 10 11 12 13

#include <stdlib.h>
#include <stdio.h>
#include "m_pd.h"
#include "m_imp.h"
#include "s_stuff.h"
14
#include "g_magicglass.h"
Miller Puckette's avatar
Miller Puckette committed
15
#include "g_canvas.h"
16
#include "s_utf8.h" /*-- moo --*/
17
#include "g_undo.h"
18
#include "x_preset.h"
Miller Puckette's avatar
Miller Puckette committed
19
#include <string.h>
20
#include <math.h>
21
#include "g_all_guis.h"
Miller Puckette's avatar
Miller Puckette committed
22 23 24 25 26 27 28

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

29 30
//static int toggle_moving = 0; //global variable

Miller Puckette's avatar
Miller Puckette committed
31 32
/* ------------------ forward declarations --------------- */
static void canvas_doclear(t_canvas *x);
33
void glist_setlastxymod(t_glist *gl, int xval, int yval, int mod);
34
void glist_setlastxy(t_glist *gl, int xval, int yval);
Miller Puckette's avatar
Miller Puckette committed
35 36 37 38 39 40
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;
41
static int clipboard_istext = 0;
42 43
//static char *canvas_textcopybuf;
//static int canvas_textcopybufsize;
Miller Puckette's avatar
Miller Puckette committed
44
static t_glist *glist_finddirty(t_glist *x);
45 46
static void canvas_reselect(t_canvas *x);
static void canvas_cut(t_canvas *x);
Aayush's avatar
Aayush committed
47
static void canvas_ascii_entry(t_canvas *x,t_float textarea_state);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
48
static int paste_xyoffset = 0; /* a counter of pastes to make x,y offsets */
49
//static void canvas_mouseup_gop(t_canvas *x, t_gobj *g);
50
void canvas_done_popup(t_canvas *x, t_float which, t_float xpos,
51 52 53
    t_float ypos);
static void canvas_doarrange(t_canvas *x, t_float which, t_gobj *oldy,
    t_gobj *oldy_prev, t_gobj *oldy_next);
54
static void canvas_paste_xyoffset(t_canvas *x);
55
void canvas_setgraph(t_glist *x, int flag, int nogoprect);
56 57
void canvas_mouseup(t_canvas *x, t_floatarg fxpos, t_floatarg fypos,
    t_floatarg fwhich);
58 59 60 61
static int outlet_issignal = 0;
static int inlet_issignal = 0;
static int last_inlet_filter = 0;
static int last_outlet_filter = 0;
62
static int copyfromexternalbuffer = 0;
63
static int tooltips = 0;
64
static int objtooltip = 0;
65
static int screenx1; /* screen coordinates when doing copyfromexternalbuffer */
66 67 68 69 70
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,
71
    t_floatarg yresize);
72
int canvas_apply_restore_original_position(t_canvas *x, int orig_pos);
73
extern void canvas_draw_gop_resize_hooks(t_canvas *x);
74 75
static void canvas_font(t_canvas *x, t_floatarg font, t_floatarg oldfont,
    t_floatarg resize, t_floatarg preview);
76
void canvas_displaceselection(t_canvas *x, int dx, int dy);
77 78 79 80
void canvas_motion(t_canvas *x, t_floatarg xpos, t_floatarg ypos,
    t_floatarg fmod);
/* for updating preset_node locations in case of operations that alter
   glist object locations (tofront/back, cut, delete, undo/redo cut/delete) */
81
extern void glob_preset_node_list_check_loc_and_update(void);
82 83
// for preset_node
extern t_class *text_class;
84 85 86
// for iemgui objects' wonky click area
//extern void iemgui_getrect_mouse(t_gobj *x, int *xp1, int *yp1,
//    int *xp2, int *yp2);
87

88
int do_not_redraw = 0;     // used to optimize redrawing
89 90
int old_displace = 0;      // for legacy displaces within gop that are not
                           // visible to displaceselection
91

92 93 94 95
/* 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 */
int connect_exception = 0;
96

97 98
/* used to test whether the shift is pressed and if so,
   handle various connection exceptions (e.g. multiconnect) */
99 100
int glob_lmclick = 0;
int glob_shift = 0;
101 102 103 104
int glob_ctrl = 0;
int glob_alt = 0;

static t_glist *canvas_last_glist;
105
static int canvas_last_glist_x=20, canvas_last_glist_y=20, canvas_last_glist_mod;
106

107 108 109 110 111 112 113 114 115 116 117 118 119
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 */
120 121 122
int gobj_filter_highlight_behavior(t_text *y)
{
    return (y->te_iemgui);
123
}
Miller Puckette's avatar
Miller Puckette committed
124

125 126 127
/* ---------------- mathieu 2014.08 --------------------------------- */

void canvas_raise_all_cords (t_canvas *x) {
128 129 130 131 132
    /* I think this is deprecated... instead of raising cords, we
       just insert all gobjs (and scalars) before the first patch
       cord.  This way we don't have to constantly raise all the
       patch cords above everything else. */
    //sys_vgui(".x%lx.c raise all_cords\n", x);
133
}
134

135
static void canvas_enteritem (t_canvas *x, int xpos, int ypos, const char *tag) {
136 137 138 139
    // This will be replaced by GUI-side functionality once we combine
    // object and xlet drawing in a single command from Pd.
    //sys_vgui("pdtk_canvas_enteritem .x%x.c %d %d %s -1\n",
    //    x, xpos, ypos, tag);
140
}
141

142
static void canvas_leaveitem (t_canvas *x) {
143 144
    // See comment above
    //sys_vgui("pdtk_canvas_leaveitem .x%x.c\n", x);
145
}
146

147 148 149 150 151 152
static void tooltip_erase (t_canvas *x) {
    if (objtooltip) {
        objtooltip = 0;
        canvas_leaveitem(x);
    }
}
153

154
static void canvas_nlet_conf (t_canvas *x, int type) {
155
    int isiemgui = type=='o' ? last_outlet_filter : last_inlet_filter;
156
    int issignal = type=='o' ? outlet_issignal : inlet_issignal;
157 158 159 160 161 162 163 164
    //sys_vgui(".x%x.c itemconfigure %s -stroke %s -fill %s -strokewidth 1\n", x,
    //  type=='o' ? x->gl_editor->canvas_cnct_outlet_tag : x->gl_editor->canvas_cnct_inlet_tag,
    //  (isiemgui ? "$pd_colors(iemgui_nlet)" : 
    //    (issignal ? "$pd_colors(signal_cord)" : "$pd_colors(control_cord)")),
    //    (issignal ? "$pd_colors(signal_nlet)" : "$pd_colors(control_nlet)"));

    /* this is rather confusing, but the canvas_cnct_[xlet]_tag already
       includes the type and index concatenated to the end. */
165
    gui_vmess("gui_gobj_configure_io", "xsiii",
166 167
        x,
        type == 'o' ? x->gl_editor->canvas_cnct_outlet_tag :
168
            x->gl_editor->canvas_cnct_inlet_tag,
169 170 171
        isiemgui,
        issignal,
        1);
172
}
173

Aayush's avatar
Aayush committed
174 175 176 177 178 179
static void canvas_ascii_entry(t_canvas *x, t_float textarea_state)
{
    gui_vmess("ascii_entry","xi",
        x,
        textarea_state);
} 
180
void canvas_getscroll (t_canvas *x) {
181
    //sys_vgui("pdtk_canvas_getscroll .x%lx.c\n",(long)x);
182
    gui_vmess("gui_canvas_get_scroll", "x", x);
183 184
}

Miller Puckette's avatar
Miller Puckette committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* ---------------- 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);
}

200 201 202 203 204 205
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
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
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);
}

224 225 226
int gobj_shouldvis(t_gobj *x, struct _glist *glist)
{
    t_object *ob;
227 228 229
    //fprintf(stderr,"shouldvis %d %d %d %d\n",
    //    glist->gl_havewindow, glist->gl_isgraph,
    //    glist->gl_goprect, glist->gl_owner != NULL);
230 231 232 233 234 235 236
        /* if our parent is a graph, and if that graph itself isn't
        visible, then we aren't either. */
    if (!glist->gl_havewindow && glist->gl_isgraph && glist->gl_owner
        && !gobj_shouldvis(&glist->gl_gobj, glist->gl_owner))
            return (0);
        /* if we're graphing-on-parent and the object falls outside the
        graph rectangle, don't draw it. */
237
    if (!glist->gl_havewindow && glist->gl_isgraph && glist->gl_goprect &&
238 239
        glist->gl_owner && (pd_class(&x->g_pd) != scalar_class) &&
        (pd_class(&x->g_pd) != garray_class))
240 241 242 243 244 245 246 247 248 249
    {
        /* 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);
250 251 252
        //fprintf(stderr,
        //    "gobj_shouldvis gop: %d %d %d %d || object %d %d %d %d\n",
        //    x1, x2, y1, y2, gx1, gx2, gy1, gy2);
253
        if (gx1 < x1 || gx1 > x2 || gx2 < x1 || gx2 > x2 ||
254 255 256
            gy1 < y1 || gy1 > y2 || gy2 < y1 || gy2 > y2)
        {
                //fprintf(stderr,"does not fit within boundaries\n");
257
                return (0);
258 259
        }
        if (glist==glist_getcanvas(glist))
260
            canvas_raise_all_cords(glist);
261 262 263 264 265 266
    }
    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. */
267 268 269
        //fprintf(stderr,"pd_checkobject %lx\n", x);
        /*fprintf(stderr,"pd_checkobject %d %d %d %d %d %d %d\n",
            glist->gl_havewindow, 
270
            (ob->te_pd != canvas_class ? 1:0),
271 272 273
            (ob->te_pd->c_wb != &text_widgetbehavior ? 1:0),
            (ob->te_pd == canvas_class ? 1:0),
            ((t_glist *)ob)->gl_isgraph,
274
            glist->gl_goprect,
275 276 277 278 279 280 281 282 283
            (ob->te_type == T_TEXT ? 1:0));*/
        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);
        }
        /*fprintf(stderr,"checking %d\n", (glist->gl_havewindow ||
Ivica Bukvic's avatar
Ivica Bukvic committed
284 285 286 287
            (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))));*/
288 289 290 291 292 293
        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)));
    }
294
    else {
295 296 297
        //fprintf(stderr,"else return 1\n");
        return (1);
    }
298 299
}

Miller Puckette's avatar
Miller Puckette committed
300 301
void gobj_vis(t_gobj *x, struct _glist *glist, int flag)
{
302
    if (do_not_redraw) return;
303
    if (x->g_pd->c_wb && x->g_pd->c_wb->w_visfn && gobj_shouldvis(x, glist))
Miller Puckette's avatar
Miller Puckette committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317
        (*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 ----------------- */

318
// direction -1 = lower, 1 = raise
319 320 321 322 323 324 325 326 327 328 329 330 331
int canvas_restore_original_position(t_glist *x, t_gobj *y, const char* objtag,
    int dir)
{
    /* we do this instead to save us costly redraw of the canvas */
    //fprintf(stderr,"canvas_restore_original_position %lx %lx %s %d\n",
    //    (t_int)x, (t_int)y, objtag, dir);
    int ret = 0;
    // we only do this if we are not embedded inside gop, otherwise
    // when gop is done redrawing we will get properly repositioned
    // by gop's call to restore original position
    if (pd_class(&y->g_pd) != canvas_class || ((t_glist *)y)->gl_owner == x)
    {
        t_object *ob = NULL;
332
        t_rtext *yrnxt = NULL;
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

        if (y->g_next)
        {
            ob = pd_checkobject(&y->g_next->g_pd);
        }
        if (ob)
        {
            yrnxt = glist_findrtext(x, (t_text *)&ob->ob_g);
        }
        if (y)
        {
            ob = pd_checkobject(&y->g_pd);
        }
        else
        {
            ret = 1;
        }
        if (ret != 1)
        {
            if (dir == -1)
            {
                if (x->gl_list == y)
                {
                    /* we get here if we are supposed to go all the way
                       to the bottom */
user's avatar
user committed
358
                    gui_vmess("gui_lower", "xs",
359
                        x, objtag ? objtag : "selected");
360 361 362 363
                }
                else if (yrnxt)
                {
                    /* lower into middle */
user's avatar
user committed
364 365
                    gui_vmess("gui_find_lowest_and_arrange", "xss",
                        x, rtext_gettag(yrnxt), objtag ? objtag : "selected");
366 367 368 369 370 371 372 373 374 375 376 377 378 379
                }
                else
                {
                    /* fall back to legacy redraw for objects
                       that are not patchable */
                    canvas_redraw(x);
                    ret = -1;
                }
            }
            else
            {
                if (yrnxt)
                {
                    /* raise into middle */
user's avatar
user committed
380 381
                    gui_vmess("gui_find_lowest_and_arrange", "xss",
                        x, rtext_gettag(yrnxt), objtag ? objtag : "selected");
382 383 384 385 386
                }
                else if (y->g_next == NULL)
                {
                    /* we get here if we are supposed to go all the way
                       to the top */
user's avatar
user committed
387
                    gui_vmess("gui_raise", "xs",
388
                        x, objtag ? objtag : "selected");
389 390 391 392 393 394 395 396 397 398 399 400
                }
                else
                {
                    /* fall back to legacy redraw for objects
                       that are not patchable */
                    canvas_redraw(x);
                    ret = -1;
                }
            }
        }
    }
    return(ret);
401 402
}

403 404
void canvas_check_nlet_highlights(t_glist *x)
{
405 406
    if (x->gl_editor->canvas_cnct_inlet_tag[0] != 0)
    {
407 408
        canvas_nlet_conf(x,'i');
        tooltip_erase(x);
409 410 411 412 413 414 415 416 417 418
        x->gl_editor->canvas_cnct_inlet_tag[0] = 0;
        //if (x->gl_editor->e_onmotion == MA_CONNECT) {
            x->gl_editor->e_onmotion = MA_NONE;
            canvas_setcursor(x, CURSOR_EDITMODE_NOTHING);
        //}
    }

    if (x->gl_editor->canvas_cnct_outlet_tag[0] != 0 &&
        x->gl_editor->e_onmotion != MA_CONNECT)
    {
419 420
        canvas_nlet_conf(x,'o');
        tooltip_erase(x);
421 422 423 424 425 426
        x->gl_editor->canvas_cnct_outlet_tag[0] = 0;
        //if (x->gl_editor->e_onmotion == MA_CONNECT) {
            x->gl_editor->e_onmotion = MA_NONE;
            canvas_setcursor(x, CURSOR_EDITMODE_NOTHING);
        //}            
    }
427 428
}

Miller Puckette's avatar
Miller Puckette committed
429 430 431
void glist_selectline(t_glist *x, t_outconnect *oc, int index1,
    int outno, int index2, int inno)
{
432
    char tagbuf[MAXPDSTRING];
Miller Puckette's avatar
Miller Puckette committed
433 434 435 436 437 438 439 440 441
    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;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
442
        sprintf(tagbuf, "l%lx", (long unsigned int)oc);
443
        gui_vmess("gui_canvas_select_line", "xs", x, tagbuf);
444 445
        c_selection = x;
        canvas_draw_gop_resize_hooks(x);
446
    }
Miller Puckette's avatar
Miller Puckette committed
447 448 449 450
}

void glist_deselectline(t_glist *x)
{
451
    char tagbuf[MAXPDSTRING];
Miller Puckette's avatar
Miller Puckette committed
452 453
    if (x->gl_editor)
    {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
454 455
        t_linetraverser t;
        t_outconnect *oc;
Miller Puckette's avatar
Miller Puckette committed
456
        x->gl_editor->e_selectedline = 0;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
457 458 459 460
        linetraverser_start(&t, glist_getcanvas(x));
        do {
            oc = linetraverser_next(&t);
        } while (oc && oc != x->gl_editor->e_selectline_tag);
461
        canvas_draw_gop_resize_hooks(x);
462 463
        sprintf(tagbuf, "l%lx",
            (long unsigned int)x->gl_editor->e_selectline_tag);
464
        gui_vmess("gui_canvas_deselect_line", "xs", x, tagbuf);
Miller Puckette's avatar
Miller Puckette committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    }    
}

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)
{
482 483
    //fprintf(stderr,"glist_select c_selection=%lx x=%lx\n",
    //    (t_int)c_selection, (t_int)x);
Miller Puckette's avatar
Miller Puckette committed
484 485
    if (x->gl_editor)
    {
486
        //fprintf(stderr,"have editor\n");
487
#ifdef PDL2ORK
488 489 490 491 492 493 494 495 496
        // 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;
        }
497
#endif
498 499 500 501 502
        if (c_selection && c_selection != x)
        {
            //fprintf(stderr,"deselecting\n");
            glist_noselect(c_selection);
        }
Miller Puckette's avatar
Miller Puckette committed
503
        t_selection *sel = (t_selection *)getbytes(sizeof(*sel));
504 505 506
        if (x->gl_editor->e_selectedline)
        {
            //fprintf(stderr,"deselecting line\n");
Miller Puckette's avatar
Miller Puckette committed
507
            glist_deselectline(x);
508
        }
Miller Puckette's avatar
Miller Puckette committed
509 510 511 512 513 514
            /* 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);
515
        c_selection = x;
516

517 518
        /* Not sure if this is still needed */
        //sys_vgui("pdtk_canvas_update_edit_menu .x%lx 1\n", x);
519
        canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
520
    }
521
    //fprintf(stderr,"select done\n");
Miller Puckette's avatar
Miller Puckette committed
522 523
}

Miller Puckette's avatar
Miller Puckette committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537
    /* 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);
}

538 539
int glist_getindex(t_glist *x, t_gobj *y);

Miller Puckette's avatar
Miller Puckette committed
540 541 542
    /* call this for selected objects only */
void glist_deselect(t_glist *x, t_gobj *y)
{
543
    //fprintf(stderr, "deselect\n");
Miller Puckette's avatar
Miller Puckette committed
544
    int fixdsp = 0;
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
545
    //static int reenter = 0;
Miller Puckette's avatar
Miller Puckette committed
546
    /* if (reenter) return; */
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
547
    //reenter = 1;
Miller Puckette's avatar
Miller Puckette committed
548 549 550
    if (x->gl_editor)
    {
        t_selection *sel, *sel2;
551
        t_rtext *fuddy = 0;
Miller Puckette's avatar
Miller Puckette committed
552 553
        t_rtext *z = 0;
        if (!glist_isselected(x, y)) bug("glist_deselect");
554 555 556 557
        // 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
558 559
        if (x->gl_editor->e_textedfor)
        {
560
            //fprintf(stderr, "e_textedfor\n");
561
            fuddy = glist_findrtext(x, (t_text *)y);
Miller Puckette's avatar
Miller Puckette committed
562 563
            if (x->gl_editor->e_textedfor == fuddy)
            {
564
                //fprintf(stderr, "e_textedfor == fuddy\n");
Miller Puckette's avatar
Miller Puckette committed
565 566
                if (x->gl_editor->e_textdirty)
                {
567
                    //fprintf(stderr, "textdirty yes\n");
Miller Puckette's avatar
Miller Puckette committed
568 569
                    z = fuddy;
                    canvas_stowconnections(glist_getcanvas(x));
Miller Puckette's avatar
Miller Puckette committed
570
                    glist_checkanddeselectall(x, y);
Miller Puckette's avatar
Miller Puckette committed
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
                }
                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;
                }
            }
        }
597 598 599 600 601 602 603
        // if we have an invalid object even if the text hasn't changed,
        // we should still try to recreate it
        if (!z && fuddy && pd_class(&((t_text *)y)->te_pd) == text_class &&
            ((t_text *)y)->te_type != T_TEXT)
        {
            z = fuddy;
        }
604

Miller Puckette's avatar
Miller Puckette committed
605 606
        if (z)
        {
607
            //fprintf(stderr, "setto\n");
Miller Puckette's avatar
Miller Puckette committed
608 609 610 611
            char *buf;
            int bufsize;

            rtext_gettext(z, &buf, &bufsize);
612
            text_setto((t_text *)y, x, buf, bufsize, pos);
Miller Puckette's avatar
Miller Puckette committed
613 614 615 616 617
            canvas_fixlinesfor(glist_getcanvas(x), (t_text *)y);
            x->gl_editor->e_textedfor = 0;
        }
        if (fixdsp)
            canvas_resume_dsp(1);
618 619 620
        /* Not sure if this is still needed */
        //if (!x->gl_editor->e_selection)
        //    sys_vgui("pdtk_canvas_update_edit_menu .x%lx 0\n", x);
621
        canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
622
    }
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
623
    //reenter = 0;
Miller Puckette's avatar
Miller Puckette committed
624 625 626 627
}

void glist_noselect(t_glist *x)
{
628
    //fprintf(stderr,"glist_noselect\n");
Miller Puckette's avatar
Miller Puckette committed
629 630
    if (x->gl_editor)
    {
631 632 633 634 635
        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
636 637
        if (x->gl_editor->e_selectedline)
            glist_deselectline(x);
638 639 640
        if (c_selection == x)
            c_selection = NULL;
        canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
641 642 643 644 645 646 647 648 649 650 651
    }
}

void glist_selectall(t_glist *x)
{
    if (x->gl_editor)
    {
        glist_noselect(x);
        if (x->gl_list)
        {
            t_gobj *y = x->gl_list;
652
#ifdef PDL2ORK
653 654 655 656 657 658 659 660 661
            // 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;
            }
662
#endif
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
            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;
            }
        }
        canvas_draw_gop_resize_hooks(x);
Miller Puckette's avatar
Miller Puckette committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    }
}

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

711
t_gobj *glist_nth(t_glist *x, int n)
Miller Puckette's avatar
Miller Puckette committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
{
    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 */
727
const char *canvas_undo_name;
Miller Puckette's avatar
Miller Puckette committed
728 729 730 731

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

Miller Puckette's avatar
Miller Puckette committed
734 735 736 737 738
    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))
    {
739
        //fprintf(stderr,"hadone canvas_setundo\n");
Miller Puckette's avatar
Miller Puckette committed
740 741 742 743 744 745 746 747
        (*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;
748
    //if (x && glist_isvisible(x) && glist_istoplevel(x))
749
    if (x)
750
    {
751
        // enable undo in menu
752 753 754
        gui_vmess("gui_undo_menu", "xss",
            x, name, "no");
    }
Miller Puckette's avatar
Miller Puckette committed
755
    else if (hadone)
756 757 758 759 760
    {
        /* can't figure out what this does... */
        gui_vmess("gui_undo_menu ", "xss",
            "nobody", "no", "no");
    }
Miller Puckette's avatar
Miller Puckette committed
761 762 763 764 765 766 767 768 769 770
}

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

771
/* ------- specific undo methods: 1. connect -------- */
Miller Puckette's avatar
Miller Puckette committed
772 773 774 775 776 777 778 779 780

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

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
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
805 806 807 808 809 810 811 812 813 814 815
    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,
816
    t_float index1, t_float outno, t_float index2, t_float inno)
Miller Puckette's avatar
Miller Puckette committed
817
{
818
    //fprintf(stderr,"canvas_disconnect\n");
819
    char tagbuf[MAXPDSTRING];
Miller Puckette's avatar
Miller Puckette committed
820 821 822 823 824 825 826 827 828 829
    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)
        {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
830
            sprintf(tagbuf, "l%lx", (long unsigned int)oc);
831
            gui_vmess("gui_canvas_delete_line", "xs",
user's avatar
user committed
832
                x, tagbuf);
833
            // jsarlo
834 835 836 837 838
            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);
            }
839
            // end jsarlo
Miller Puckette's avatar
Miller Puckette committed
840
            obj_disconnect(t.tr_ob, t.tr_outno, t.tr_ob2, t.tr_inno);
841

842 843 844
            /* 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
845 846
               so we blindly disconnect first outlet with the first inlet */
            if (pd_class(&t.tr_ob->ob_g.g_pd) == preset_node_class && t.tr_outno == 0)
847 848 849 850
            {
                //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
851 852 853 854 855
            break;
        }
    }
}

856
void canvas_undo_disconnect(t_canvas *x, void *z, int action)
Miller Puckette's avatar
Miller Puckette committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
{
    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));
}

873
/* ---------- ... 3. cut, clear, and typing into objects: -------- */
Miller Puckette's avatar
Miller Puckette committed
874 875 876

#define UCUT_CUT 1          /* operation was a cut */
#define UCUT_CLEAR 2        /* .. a clear */
877 878 879

// 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
880 881 882 883 884 885 886 887
#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 */
888 889 890 891
    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
892 893
} t_undo_cut;

894
void *canvas_undo_set_cut(t_canvas *x, int mode)
Miller Puckette's avatar
Miller Puckette committed
895 896 897 898 899 900
{
    t_undo_cut *buf;
    t_gobj *y;
    t_linetraverser t;
    t_outconnect *oc;
    int nnotsel= glist_selectionindex(x, 0, 0);
901 902 903 904
    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
905 906 907 908 909 910
    buf->u_mode = mode;
    buf->u_redotextbuf = 0;

        /* store connections into/out of the selection */
    buf->u_reconnectbuf = binbuf_new();
    linetraverser_start(&t, x);
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
    //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
929 930 931 932 933 934
    if (mode == UCUT_TEXT)
    {
        buf->u_objectbuf = canvas_docopy(x);
    }
    else if (mode == UCUT_CUT)
    {
935
        buf->u_objectbuf = canvas_docopy(x);
Miller Puckette's avatar
Miller Puckette committed
936 937 938 939 940
    }
    else if (mode == UCUT_CLEAR)
    {
        buf->u_objectbuf = canvas_docopy(x);
    }
941

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    //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;
                    i++;
                }
                j++;
            }
        }
        //for (i = 0; i < buf->n_obj; i++)
        //    fprintf(stderr,"%d position = %d\n", i, buf->p_a[i]);
    }
961

Miller Puckette's avatar
Miller Puckette committed
962 963 964
    return (buf);
}

965
void canvas_undo_cut(t_canvas *x, void *z, int action)
Miller Puckette's avatar
Miller Puckette committed
966
{
967 968
    //fprintf(stderr, "canvas_undo_cut canvas=%d buf=%d action=%d\n",
    //    (int)x, (int)z, action);
Miller Puckette's avatar
Miller Puckette committed
969 970 971 972
    t_undo_cut *buf = z;
    int mode = buf->u_mode;
    if (action == UNDO_UNDO)
    {
973 974 975 976 977
        do_not_redraw += 1;
        //fprintf(stderr,"UNDO_UNDO\n");
        if (mode == UCUT_CUT)
        {
            //fprintf(stderr, "UCUT_CUT\n");
978
            canvas_dopaste(x, buf->u_objectbuf);
979 980 981 982
        }
        else if (mode == UCUT_CLEAR)
        {
            //fprintf(stderr, "UCUT_CLEAR\n");
Miller Puckette's avatar
Miller Puckette committed
983
            canvas_dopaste(x, buf->u_objectbuf);
984
        }
Miller Puckette's avatar
Miller Puckette committed
985 986
        else if (mode == UCUT_TEXT)
        {
987
            //fprintf(stderr, "UCUT_TEXT\n");
Miller Puckette's avatar
Miller Puckette committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
            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"));
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
        //now reposition objects to their original locations
        if (mode == UCUT_CUT || mode == UCUT_CLEAR)
        {
            //fprintf(stderr,"reordering\n");
            int i = 0;

            /* location of the first newly pasted object */
            int paste_pos = glist_getindex(x,0) - buf->n_obj;
            //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);
                    //if the object is supposed to be first in the gl_list
                    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;
                        //LATER when objects are properly tagged lower y here
                    }
                    //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;
                        //LATER when objects are properly tagged lower y here
                    }
                }
            }
            do_not_redraw -= 1;
            //LATER disable redrawing here
            canvas_redraw(x);
            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);
            }
            glob_preset_node_list_check_loc_and_update();
        }
Miller Puckette's avatar
Miller Puckette committed
1077 1078 1079
    }
    else if (action == UNDO_REDO)
    {
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
        //fprintf(stderr,"UNDO_REDO\n");
        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
1090
            canvas_doclear(x);
1091 1092
            glob_preset_node_list_check_loc_and_update();
        }
Miller Puckette's avatar
Miller Puckette committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
        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)
    {
1108
        //fprintf(stderr,"UNDO_FREE\n");
Miller Puckette's avatar
Miller Puckette committed
1109 1110 1111 1112 1113 1114
        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);
1115 1116 1117 1118 1119
        if (buf != NULL)
        {
            t_freebytes(buf, sizeof(*buf) +
                sizeof(buf->p_a[0]) * (buf->n_obj-1));
        }
Miller Puckette's avatar
Miller Puckette committed
1120 1121 1122
    }
}

1123
/* --------- 4. motion, including "tidy up" and stretching ----------- */
Miller Puckette's avatar
Miller Puckette committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

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;

1140
void *canvas_undo_set_move(t_canvas *x, int selected)
Miller Puckette's avatar
Miller Puckette committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
{
    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);
}

1174
void canvas_undo_move(t_canvas *x, void *z, int action)
Miller Puckette's avatar
Miller Puckette committed
1175 1176
{
    t_undo_move *buf = z;
1177 1178
    t_class *cl;
    int resortin = 0, resortout = 0;
Miller Puckette's avatar
Miller Puckette committed
1179 1180 1181
    if (action == UNDO_UNDO || action == UNDO_REDO)
    {
        int i;
1182 1183
        int x1=0, y1=0, x2=0, y2=0, newx=0, newy=0;
        t_gobj *y;
1184
        //do_not_redraw = 1;
Miller Puckette's avatar
Miller Puckette committed
1185 1186 1187
        for (i = 0; i < buf->u_n; i++)
        {
            y = glist_nth(x, buf->u_vec[i].e_index);
1188 1189
            newx = buf->u_vec[i].e_xpix;
            newy = buf->u_vec[i].e_ypix;
Miller Puckette's avatar
Miller Puckette committed
1190 1191
            if (y)
            {
1192 1193 1194 1195 1196 1197 1198 1199 1200
                glist_noselect(x);
                glist_select(x, y);
                gobj_getrect(y, x, &x1, &y1, &x2, &y2);
                canvas_displaceselection(x, newx-x1, newy - y1);
                buf->u_vec[i].e_xpix = x1;
                buf->u_vec[i].e_ypix = y1;
                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
1201 1202
            }
        }
1203
        glist_noselect(x);
1204 1205
        for (i = 0; i < buf->u_n; i++)
        {
1206
            y = glist_nth(x, buf->u_vec[i].e_index);
1207
            if (y) glist_select(x, y);
1208 1209 1210 1211 1212
        }
        //do_not_redraw = 0;
        //canvas_redraw(x);
        if (resortin) canvas_resortinlets(x);
        if (resortout) canvas_resortoutlets(x);
Miller Puckette's avatar
Miller Puckette committed
1213 1214 1215 1216 1217 1218 1219 1220
    }
    else if (action == UNDO_FREE)
    {
        t_freebytes(buf->u_vec, buf->u_n * sizeof(*buf->u_vec));
        t_freebytes(buf, sizeof(*buf));
    }
}

1221
/* --------- 5. paste (also duplicate) ----------- */
Miller Puckette's avatar
Miller Puckette committed
1222 1223 1224

typedef struct _undo_paste      
{
1225 1226 1227 1228 1229 1230
    int u_index;            /* index of first object pasted */
    int u_sel_index;        /* index of object selected at the time the other
                               object was pasted (for autopatching) */
    int u_offset;           /* offset for duplicated items (since it differs
                               when duplicated into same or different canvas */
    t_binbuf *u_objectbuf;  /* here we store actual copied data */
Miller Puckette's avatar
Miller Puckette committed
1231 1232
} t_undo_paste;

1233 1234
void *canvas_undo_set_paste(t_canvas *x, int offset, int duplicate,
    int d_offset)
Miller Puckette's avatar
Miller Puckette committed
1235 1236
{
    t_undo_paste *buf =  (t_undo_paste *)getbytes(sizeof(*buf));
1237
    buf->u_index = glist_getindex(x, 0) - offset; //do we need offset at all?
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    if (!duplicate && x->gl_editor->e_selection &&
        !x->gl_editor->e_selection->sel_next)
    {
        //if only one object is selected which will warrant autopatching
        buf->u_sel_index = glist_getindex(x,
            x->gl_editor->e_selection->sel_what);
        //fprintf(stderr,"canvas_undo_set_paste selected object index %d\n",
        //    buf->u_sel_index);
    }
    else
    {
        buf->u_sel_index = -1;
    }
    buf->u_offset = d_offset;
    buf->u_objectbuf = binbuf_duplicate(copy_binbuf);
Miller Puckette's avatar
Miller Puckette committed
1253 1254 1255
    return (buf);
}

1256
void canvas_undo_paste(t_canvas *x, void *z, int action)
Miller Puckette's avatar
Miller Puckette committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
{
    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)
    {
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
        //if (buf->u_offset)
        //    do_not_redraw += 1;
        glist_noselect(x);
        //if the pasted object is supposed to be autopatched
        //then select the object it should be autopatched to
        if (buf->u_sel_index > -1)
        {
            //fprintf(stderr,"undo trying autopatch\n");
            glist_select(x, glist_nth(x, buf->u_sel_index));
        }
1279
        canvas_dopaste(x, buf->u_objectbuf);
Miller Puckette's avatar
Miller Puckette committed
1280
            /* if it was "duplicate" have to re-enact the displacement. */
1281 1282 1283
        if (buf->u_offset)
        {
            //do_not_redraw -= 1;
1284 1285
            //for (sel = x->gl_editor->e_selection; sel; sel = sel->sel_next)
            //    gobj_displace(sel->sel_what, x, 10, 10);
1286 1287
            canvas_paste_xyoffset(x);
        }
Miller Puckette's avatar
Miller Puckette committed
1288
    }
1289 1290
    else if (action == UNDO_FREE)
    {
1291 1292
        if (buf->u_objectbuf)
            binbuf_free(buf->u_objectbuf);
Miller Puckette's avatar
Miller Puckette committed
1293
        t_freebytes(buf, sizeof(*buf));
1294
    }
Miller Puckette's avatar
Miller Puckette committed
1295 1296
}

1297 1298
int clone_match(t_pd *z, t_symbol *name, t_symbol *dir);

Miller Puckette's avatar
Miller Puckette committed
1299 1300 1301 1302 1303 1304
    /* 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)
{
1305
    //fprintf(stderr,"doreload\n");
Miller Puckette's avatar
Miller Puckette committed
1306 1307
    t_gobj *g;
    int i, nobj = glist_getindex(gl, 0);  /* number of objects */
1308
    int hadwindow = gl->gl_havewindow;
1309
    int found = 0;
1310
    int remakeit = 0;
1311 1312 1313
    // to optimize redrawing we select all objects that need to be updated
    // and redraw them together. Then we look for sub-patches that may have
    // more of the same...
Miller Puckette's avatar
Miller Puckette committed
1314 1315
    for (g = gl->gl_list, i = 0; g && i < nobj; i++)
    {
1316 1317 1318
            /* remake the object if it's an abstraction that appears to have
            been loaded from the file we just saved */
        remakeit = (g != except && pd_class(&g->g_pd) == canvas_class &&
Miller Puckette's avatar
Miller Puckette committed
1319 1320
            canvas_isabstraction((t_canvas *)g) &&
                ((t_canvas *)g)->gl_name == name &&
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
                    canvas_getdir((t_canvas *)g) == dir);
            /* also remake it if it's a "clone" with that name */
        if (pd_class(&g->g_pd) == clone_class &&
            clone_match(&g->g_pd, name, dir))
        {
                /* LATER try not to remake the one that equals "except" */
            remakeit = 1;
        }

        if (remakeit)
Miller Puckette's avatar
Miller Puckette committed
1331 1332 1333 1334 1335
        {
                /* 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. */
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
1336
            //int j = glist_getindex(gl, g);
1337
            //fprintf(stderr, "rebuildlicious %d\n", j);
1338 1339 1340 1341 1342 1343 1344 1345

            // Bugfix for cases where canvas_vis doesn't actually create a
            // new editor. We need to fix canvas_vis so that the bug doesn't
            // get triggered. But since we know this fixes a regression we'll
            // keep this as a point in the history as we fix canvas_vis. Once
            // that's done we can remove this call.
            canvas_create_editor(gl);

1346 1347
            if (!gl->gl_havewindow)
            {
1348
                canvas_vis(glist_getcanvas(gl), 1);
1349 1350 1351 1352 1353 1354 1355
                //fprintf(stderr,"calling canvas_vis\n");
            }
            if (!found)
            {
                glist_noselect(gl);
                found = 1;
            }
Miller Puckette's avatar
Miller Puckette committed
1356
            glist_select(gl, g);
1357 1358
            //canvas_setundo(gl, canvas_undo_cut,
            //    canvas_undo_set_cut(gl, UCUT_CLEAR), "clear");
1359 1360
            //canvas_undo_add(gl, 3, "clear",
            //    canvas_undo_set_cut(gl, UCUT_CLEAR));
1361

1362 1363 1364
            //canvas_cut(gl);
            //canvas_undo_undo(gl);
            //glist_noselect(gl);
1365
            //g = glist_nth(gl, j+1);
Miller Puckette's avatar
Miller Puckette committed
1366
        }
1367 1368 1369 1370 1371 1372 1373 1374 1375
        g = g->g_next;
    }
    if (found)
    {
        canvas_cut(gl);
        canvas_undo_undo(gl);
        //canvas_undo_rebranch(gl);
        glist_noselect(gl);
    }
1376

1377
    // now look for sub-patches...
1378 1379 1380 1381 1382 1383 1384
    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)
1385
           )
Miller Puckette's avatar
Miller Puckette committed
1386
                glist_doreload((t_canvas *)g, name, dir, except);
1387
        g = g->g_next;
Miller Puckette's avatar
Miller Puckette committed
1388
    }
1389
    if (!hadwindow && gl->gl_havewindow)
1390
        canvas_vis(glist_getcanvas(gl), 0);
Miller Puckette's avatar
Miller Puckette committed
1391 1392
}

1393 1394 1395 1396
    /* 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;

1397
    /* call glist_doreload on everyone */
Miller Puckette's avatar
Miller Puckette committed
1398 1399 1400
void canvas_reload(t_symbol *name, t_symbol *dir, t_gobj *except)
{
    t_canvas *x;
Miller Puckette's avatar
Miller Puckette committed
1401
    int dspwas = canvas_suspend_dsp();
Miller Puckette's avatar
Miller Puckette committed
1402
    glist_amreloadingabstractions = 1;
Miller Puckette's avatar
Miller Puckette committed
1403
        /* find all root canvases */
1404
    for (x = pd_this->pd_canvaslist; x; x = x->gl_next)
Miller Puckette's avatar
Miller Puckette committed
1405
        glist_doreload(x, name, dir, except);
1406
    glist_amreloadingabstractions = 0;
Miller Puckette's avatar
Miller Puckette committed
1407
    canvas_resume_dsp(dspwas);
Miller Puckette's avatar
Miller Puckette committed
1408 1409
}

1410
/* --------- 6. apply  ----------- */
1411 1412 1413 1414 1415

typedef struct _undo_apply        
{
    t_binbuf *u_objectbuf;      /* the object cleared or typed into */
    t_binbuf *u_reconnectbuf;   /* connections into and out of object */
1416
    int u_index;                /* index of the previous object */
Ivica Bukvic's avatar