g_text.c 69.3 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* changes by Thomas Musil IEM KUG Graz Austria 2001 */
/* the methods for calling the gui-objects from menu are implemented */
/* all changes are labeled with      iemlib      */

#include <stdlib.h>
#include "m_pd.h"
#include "m_imp.h"
#include "s_stuff.h"
#include "t_tk.h"
#include "g_canvas.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
18
19
#include "g_undo.h"
#include "x_preset.h"
Miller Puckette's avatar
Miller Puckette committed
20

21
22
#include "s_utf8.h"

Miller Puckette's avatar
Miller Puckette committed
23
t_class *text_class;
24
25
t_class *message_class;
static t_class *gatom_class;
Miller Puckette's avatar
Miller Puckette committed
26
27
28
29
30
static void text_vis(t_gobj *z, t_glist *glist, int vis);
static void text_displace(t_gobj *z, t_glist *glist,
    int dx, int dy);
static void text_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2);
31
32
void canvas_howputnew(t_canvas *x, int *connectp, int *xpixp, int *ypixp,
    int *indexp, int *totalp);
Miller Puckette's avatar
Miller Puckette committed
33
34
35
36

void canvas_startmotion(t_canvas *x);
t_widgetbehavior text_widgetbehavior;

37
38
static char *invalid_fill = "\"#ffdddd\"";

39
40
41
42
extern void canvas_apply_setundo(t_canvas *x, t_gobj *y);
extern void canvas_setundo(t_canvas *x, t_undofn undofn, void *buf, const char *name);
extern void *canvas_undo_set_create(t_canvas *x);
extern void canvas_undo_create(t_canvas *x, void *z, int action);
43
extern int we_are_undoing;
44
extern void glob_preset_node_list_seek_hub(void);
45

Miller Puckette's avatar
Miller Puckette committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ----------------- the "text" object.  ------------------ */

    /* add a "text" object (comment) to a glist.  While this one goes for any
    glist, the other 3 below are for canvases only.  (why?)  This is called
    without args if invoked from the GUI; otherwise at least x and y
    are provided.  */

void glist_text(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    t_text *x = (t_text *)pd_new(text_class);
    t_atom at;
    x->te_width = 0;                            /* don't know it yet. */
    x->te_type = T_TEXT;
    x->te_binbuf = binbuf_new();
    if (argc > 1)
    {
        x->te_xpix = atom_getfloatarg(0, argc, argv);
        x->te_ypix = atom_getfloatarg(1, argc, argv);
64
65
66
        if (argc > 2) {
            binbuf_restore(x->te_binbuf, argc-2, argv+2);
        }
Miller Puckette's avatar
Miller Puckette committed
67
68
69
70
71
72
73
74
75
        else
        {
            SETSYMBOL(&at, gensym("comment"));
            binbuf_restore(x->te_binbuf, 1, &at);
        }
        glist_add(gl, &x->te_g);
    }
    else
    {
76
77
78
        //int xpix, ypix;
		int connectme, xpix, ypix, indx, nobj;
		canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
Miller Puckette's avatar
Miller Puckette committed
79
80
81
        pd_vmess((t_pd *)glist_getcanvas(gl), gensym("editmode"), "i", 1);
        SETSYMBOL(&at, gensym("comment"));
        glist_noselect(gl);
82
83
84
        //glist_getnextxy(gl, &xpix, &ypix);
        x->te_xpix = xpix;
        x->te_ypix = ypix;
Miller Puckette's avatar
Miller Puckette committed
85
86
87
88
89
90
91
92
93
94
        binbuf_restore(x->te_binbuf, 1, &at);
        glist_add(gl, &x->te_g);
        glist_noselect(gl);
        glist_select(gl, &x->te_g);
            /* it would be nice to "activate" here, but then the second,
            "put-me-down" click changes the text selection, which is quite
            irritating, so I took this back out.  It's OK in messages
            and objects though since there's no text in them at menu
            creation. */
            /* gobj_activate(&x->te_g, gl, 1); */
95
96
97
		if (!we_are_undoing)
			canvas_undo_add(glist_getcanvas(gl), 9, "create",
				(void *)canvas_undo_set_create(glist_getcanvas(gl)));
98
        if (connectme == 0) canvas_startmotion(glist_getcanvas(gl));
Miller Puckette's avatar
Miller Puckette committed
99
100
101
102
103
104
105
106
107
108
109
    }
}

/* ----------------- the "object" object.  ------------------ */

extern t_pd *newest;
void canvas_getargs(int *argcp, t_atom **argvp);

static void canvas_objtext(t_glist *gl, int xpix, int ypix, int selected,
    t_binbuf *b)
{
110
	//fprintf(stderr,"canvas_objtext\n");
Miller Puckette's avatar
Miller Puckette committed
111
112
113
    t_text *x;
    int argc;
    t_atom *argv;
114
115
116
117
118
119

	// for hiding arguments
	t_atom *vec;
	int len, i, hidden;
	t_binbuf *hide;

Miller Puckette's avatar
Miller Puckette committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    newest = 0;
    canvas_setcurrent((t_canvas *)gl);
    canvas_getargs(&argc, &argv);
    binbuf_eval(b, &pd_objectmaker, argc, argv);
    if (binbuf_getnatom(b))
    {
        if (!newest)
        {
            binbuf_print(b);
            post("... couldn't create");
            x = 0;
        }
        else if (!(x = pd_checkobject(newest)))
        {
            binbuf_print(b);
            post("... didn't return a patchable object");
        }
    }
    else x = 0;
    if (!x)
    {
141
142
143
		/* LATER make the color reflect this */
		//fprintf(stderr,"creating blank object\n");
		x = (t_text *)pd_new(text_class);
Miller Puckette's avatar
Miller Puckette committed
144
    }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	/* special case: an object, like preset_hub, hides its arguments beyond the first n, so
	   we modify its binbuf here */
	vec = binbuf_getvec(b);
	len = binbuf_getnatom(b);
	hidden = 0;
	for (i = 0; i < len; i++) {
		if (!strcmp("%hidden%", atom_getsymbol(&vec[i])->s_name)) {
			//fprintf(stderr,"found hidden %d %s\n", i, atom_getsymbol(&vec[i])->s_name);
			hidden = i;
			break;
		}
	}
	if (hidden) {
		hide = binbuf_new();
		binbuf_add(hide, hidden, vec);
		binbuf_free(b);
		b = hide;
	}
	/* done special case */

Miller Puckette's avatar
Miller Puckette committed
165
    x->te_binbuf = b;
166

Miller Puckette's avatar
Miller Puckette committed
167
168
169
170
171
    x->te_xpix = xpix;
    x->te_ypix = ypix;
    x->te_width = 0;
    x->te_type = T_OBJECT;
    glist_add(gl, &x->te_g);
172

Miller Puckette's avatar
Miller Puckette committed
173
174
175
176
177
178
179
180
181
182
183
    if (selected)
    {
            /* this is called if we've been created from the menu. */
        glist_select(gl, &x->te_g);
        gobj_activate(&x->te_g, gl, 1);
    }
    if (pd_class(&x->ob_pd) == vinlet_class)
        canvas_resortinlets(glist_getcanvas(gl));
    if (pd_class(&x->ob_pd) == voutlet_class)
        canvas_resortoutlets(glist_getcanvas(gl));
    canvas_unsetcurrent((t_canvas *)gl);
184
185
186
187
188
189
190
191
192
	// here we recreate data buffer inside previously created undo snapshot
	//canvas_undo_create(glist_getcanvas(gl), glist_getcanvas(gl)->u_last->data, UNDO_FREE);
	//glist_getcanvas(gl)->u_last->data = canvas_undo_set_create(glist_getcanvas(gl));
	/*if (binbuf_getnatom(x->te_binbuf) && !we_are_undoing) {
		fprintf(stderr,"canvas_objtext calls create undo\n");
		//glist_select(gl, &x->te_g);
		canvas_undo_add(glist_getcanvas(gl), 9, "create",
			(void *)canvas_undo_set_create(glist_getcanvas(gl)));
	}*/
193
194
195
	if ( glist_isvisible( ((t_canvas *)gl) ) ) {
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", glist_getcanvas(gl));
	}
Miller Puckette's avatar
Miller Puckette committed
196
197
}

198
extern int sys_noautopatch;
Miller Puckette's avatar
Miller Puckette committed
199
200
    /* utility routine to figure out where to put a new text box from menu
    and whether to connect to it automatically */
201
void canvas_howputnew(t_canvas *x, int *connectp, int *xpixp, int *ypixp,
Miller Puckette's avatar
Miller Puckette committed
202
203
    int *indexp, int *totalp)
{
Miller Puckette's avatar
Miller Puckette committed
204
    int xpix, ypix, indx = 0, nobj = 0, n2, x1, x2, y1, y2;
205
206
	t_gobj *selected;

207
208
	/*
	int connectme = 0;
209
210
211
212
213
214
	if (x->gl_editor->e_selection &&
        !x->gl_editor->e_selection->sel_next &&
		!sys_noautopatch) {
			selected = x->gl_editor->e_selection->sel_what;
			t_object *ob = pd_checkobject(&selected->g_pd);
    		connectme = (obj_noutlets(ob) ? 1 : 0);
215
216
	}*/
    int connectme = (x->gl_editor->e_selection &&
217
        !x->gl_editor->e_selection->sel_next);
Miller Puckette's avatar
Miller Puckette committed
218
219
    if (connectme)
    {
Miller Puckette's avatar
Miller Puckette committed
220
        t_gobj *g, *selected = x->gl_editor->e_selection->sel_what;
Miller Puckette's avatar
Miller Puckette committed
221
        for (g = x->gl_list, nobj = 0; g; g = g->g_next, nobj++)
Miller Puckette's avatar
Miller Puckette committed
222
            if (g == selected)
Miller Puckette's avatar
Miller Puckette committed
223
224
225
226
227
228
        {
            gobj_getrect(g, x, &x1, &y1, &x2, &y2);
            indx = nobj;
            *xpixp = x1;
            *ypixp = y2 + 5;
        }
Miller Puckette's avatar
Miller Puckette committed
229
230
231
232
233
234
235
236
237
238
239
240
241
        glist_noselect(x);
            /* search back for 'selected' and if it isn't on the list, 
                plan just to connect from the last item on the list. */
        for (g = x->gl_list, n2 = 0; g; g = g->g_next, n2++)
        {
            if (g == selected)
            {
                indx = n2;
                break;
            }
            else if (!g->g_next)
                indx = nobj-1;
        }
242
		x->gl_editor->e_onmotion = MA_NONE;
243
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x);
Miller Puckette's avatar
Miller Puckette committed
244
245
246
247
248
    }
    else
    {
        glist_getnextxy(x, xpixp, ypixp);
        glist_noselect(x);
Miller Puckette's avatar
Miller Puckette committed
249
    }
250
251
252
253
	if (sys_noautopatch) {
		if (connectme == 1) connectme = -1;
		else connectme = 0;
	}
Miller Puckette's avatar
Miller Puckette committed
254
255
256
257
258
    *connectp = connectme;
    *indexp = indx;
    *totalp = nobj;
}

Miller Puckette's avatar
Miller Puckette committed
259
    /* object creation routine.  These are called without any arguments if
Miller Puckette's avatar
Miller Puckette committed
260
261
    they're invoked from the gui; when pasting or restoring from a file, we
    get at least x and y. */
Miller Puckette's avatar
Miller Puckette committed
262

263
264
EXTERN int connect_exception;

Miller Puckette's avatar
Miller Puckette committed
265
266
void canvas_obj(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
267
	//fprintf(stderr,"canvas_obj\n");
Miller Puckette's avatar
Miller Puckette committed
268
269
270
271
272
273
274
275
    t_text *x;
    if (argc >= 2)
    {
        t_binbuf *b = binbuf_new();
        binbuf_restore(b, argc-2, argv+2);
        canvas_objtext(gl, atom_getintarg(0, argc, argv),
            atom_getintarg(1, argc, argv), 0, b);
    }
Miller Puckette's avatar
Miller Puckette committed
276
277
278
279
280
281
        /* JMZ: don't go into interactive mode in a closed canvas */
    else if (!glist_isvisible(gl))
        post("unable to create stub object in closed canvas!");
    else
    {
            /* interactively create new obect */
Miller Puckette's avatar
Miller Puckette committed
282
        t_binbuf *b = binbuf_new();
Miller Puckette's avatar
Miller Puckette committed
283
284
        int connectme, xpix, ypix, indx, nobj;
        canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
Miller Puckette's avatar
Miller Puckette committed
285
286
        pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
        canvas_objtext(gl, xpix, ypix, 1, b);
287
        if (connectme == 1) {
288
			//fprintf(stderr,"canvas_obj calls canvas_connect\n");
289
			connect_exception = 1;
Miller Puckette's avatar
Miller Puckette committed
290
            canvas_connect(gl, indx, 0, nobj, 0);
291
			connect_exception = 0;
292
		}
293
        else if (connectme == 0) {
294
			//fprintf(stderr,"canvas_obj calls canvas_startmotion\n");
295
296
			canvas_startmotion(glist_getcanvas(gl));
		}
297
		//canvas_setundo(glist_getcanvas(gl), canvas_undo_create, canvas_undo_set_create(gl), "create");
298
299
300
		if (!we_are_undoing)
			canvas_undo_add(glist_getcanvas(gl), 9, "create",
				(void *)canvas_undo_set_create(glist_getcanvas(gl)));
Miller Puckette's avatar
Miller Puckette committed
301
302
303
    }
}

304
305
306
307
308
309
extern void glist_setlastxy(t_glist *gl, int xval, int yval);

/* invoked from tcl/tk: abstraction_name x_offset y_offset */
void canvas_obj_abstraction_from_menu(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
	//fprintf(stderr,"canvas_abstraction_from_menu\n");
310
311
    //t_text *x;
	t_gobj *y;
312
313
314
315
316
317

    t_binbuf *b = binbuf_new();
	binbuf_restore(b, 2, argv);
    int connectme, xpix, ypix, indx, nobj;
    canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
    pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
318
319
320
321
#ifdef PDL2ORK
	if (sys_k12_mode)
		pd_vmess (&gl->gl_pd, gensym("tooltips"), "i", 1);
#endif
322
323
    canvas_objtext(gl, xpix+atom_getintarg(1, argc, argv), ypix+atom_getintarg(2, argc, argv), 1, b);

324
325
326
327
328
329
330
	// the object is now the last on the glist so we locate it and send it loadbang
	// we know we have at least one object since we just created one so we don't check for y being valid
	y = gl->gl_list;
	while (y->g_next)
		y = y->g_next;
	canvas_loadbang((t_canvas *)y);

331
    if (connectme == 1) {
332
333
        canvas_connect(gl, indx, 0, nobj, 0);
	}
334
    else if (connectme == 0) {
335
336
337
338
339
340
341
		canvas_startmotion(glist_getcanvas(gl));
	}
	canvas_undo_add(glist_getcanvas(gl), 9, "create",
		(void *)canvas_undo_set_create(glist_getcanvas(gl)));
	glist_setlastxy(glist_getcanvas(gl), xpix, ypix);
}

Miller Puckette's avatar
Miller Puckette committed
342
343
344
345
346
/* make an object box for an object that's already there. */

/* iemlib */
void canvas_iemguis(t_glist *gl, t_symbol *guiobjname)
{
347
	//fprintf(stderr,"canvas_iemguis\n");
Miller Puckette's avatar
Miller Puckette committed
348
349
    t_atom at;
    t_binbuf *b = binbuf_new();
350
351
352
353
354
355
356
357
    //int xpix, ypix;

	if(!strcmp(guiobjname->s_name, "cnv"))
		glist_noselect(gl);

	int connectme, xpix, ypix, indx, nobj;
    canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);

358
	/*NOT NECESSARY ANY MORE: compensate for the iemgui sliders' xyoffset in case of autopatch
359
360
361
362
363
364
365
366
367
	if(connectme) {
		if(!strcmp(guiobjname->s_name, "hsl"))
			xpix = xpix + 3;
		else if(!strcmp(guiobjname->s_name, "vsl"))
			ypix = ypix + 2;
		else if(!strcmp(guiobjname->s_name, "vu")) {
			xpix = xpix + 1;
			ypix = ypix + 2;
		}
368
	}*/
369
	
Miller Puckette's avatar
Miller Puckette committed
370
371
372
373
    pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
    glist_noselect(gl);
    SETSYMBOL(&at, guiobjname);
    binbuf_restore(b, 1, &at);
374
	canvas_objtext(gl, xpix, ypix, 1, b);
375
    if (connectme == 1)
376
377
378
        canvas_connect(gl, indx, 0, nobj, 0);
    //glist_getnextxy(gl, &xpix, &ypix);
    //canvas_objtext(gl, xpix, ypix, 1, b);
379
    else if (connectme == 0 ) canvas_startmotion(glist_getcanvas(gl));
380
	//canvas_setundo(glist_getcanvas(gl), canvas_undo_create, canvas_undo_set_create(gl), "create");
381
382
	canvas_undo_add(glist_getcanvas(gl), 9, "create",
		(void *)canvas_undo_set_create(glist_getcanvas(gl)));
Miller Puckette's avatar
Miller Puckette committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
}

void canvas_bng(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("bng"));
}

void canvas_toggle(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("tgl"));
}

void canvas_vslider(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("vsl"));
}

void canvas_hslider(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("hsl"));
}

void canvas_hdial(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("hdl"));
}

void canvas_vdial(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("vdl"));
}

void canvas_hradio(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("hradio"));
}

void canvas_vradio(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("vradio"));
}

void canvas_vumeter(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("vu"));
}

void canvas_mycnv(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("cnv"));
}

void canvas_numbox(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_iemguis(gl, gensym("nbx"));
}

/* iemlib */

void canvas_objfor(t_glist *gl, t_text *x, int argc, t_atom *argv)
{
    x->te_width = 0;                            /* don't know it yet. */
    x->te_type = T_OBJECT;
    x->te_binbuf = binbuf_new();
    x->te_xpix = atom_getfloatarg(0, argc, argv);
    x->te_ypix = atom_getfloatarg(1, argc, argv);
    if (argc > 2) binbuf_restore(x->te_binbuf, argc-2, argv+2);
    glist_add(gl, &x->te_g);
}

/* ---------------------- the "message" text item ------------------------ */

typedef struct _messresponder
{
    t_pd mr_pd;
    t_outlet *mr_outlet;
} t_messresponder;

typedef struct _message
{
    t_text m_text;
    t_messresponder m_messresponder;
    t_glist *m_glist;
    t_clock *m_clock;
} t_message;

469
static t_class *messresponder_class;
Miller Puckette's avatar
Miller Puckette committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

static void messresponder_bang(t_messresponder *x)
{
    outlet_bang(x->mr_outlet);
}

static void messresponder_float(t_messresponder *x, t_float f)
{
    outlet_float(x->mr_outlet, f);
}

static void messresponder_symbol(t_messresponder *x, t_symbol *s)
{
    outlet_symbol(x->mr_outlet, s);
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
486
487
488
489
490
static void messresponder_blob(t_messresponder *x, t_blob *st)
{ /* MP 20070107 blob type */
    outlet_blob(x->mr_outlet, st);
}

Miller Puckette's avatar
Miller Puckette committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
static void messresponder_list(t_messresponder *x, 
    t_symbol *s, int argc, t_atom *argv)
{
    outlet_list(x->mr_outlet, s, argc, argv);
}

static void messresponder_anything(t_messresponder *x,
    t_symbol *s, int argc, t_atom *argv)
{
    outlet_anything(x->mr_outlet, s, argc, argv);
}

static void message_bang(t_message *x)
{
    binbuf_eval(x->m_text.te_binbuf, &x->m_messresponder.mr_pd, 0, 0);
}

static void message_float(t_message *x, t_float f)
{
    t_atom at;
    SETFLOAT(&at, f);
    binbuf_eval(x->m_text.te_binbuf, &x->m_messresponder.mr_pd, 1, &at);
}

static void message_symbol(t_message *x, t_symbol *s)
{
    t_atom at;
    SETSYMBOL(&at, s);
    binbuf_eval(x->m_text.te_binbuf, &x->m_messresponder.mr_pd, 1, &at);
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
522
523
524
525
526
527
528
static void message_blob(t_message *x, t_blob *st)
{
    t_atom at;
    SETBLOB(&at, st);
    binbuf_eval(x->m_text.te_binbuf, &x->m_messresponder.mr_pd, 1, &at);
}

Miller Puckette's avatar
Miller Puckette committed
529
530
531
532
533
534
535
536
537
538
static void message_list(t_message *x, t_symbol *s, int argc, t_atom *argv)
{
    binbuf_eval(x->m_text.te_binbuf, &x->m_messresponder.mr_pd, argc, argv);
}

static void message_set(t_message *x, t_symbol *s, int argc, t_atom *argv)
{
    binbuf_clear(x->m_text.te_binbuf);
    binbuf_add(x->m_text.te_binbuf, argc, argv);
    glist_retext(x->m_glist, &x->m_text);
539
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
540
541
542
543
544
545
}

static void message_add2(t_message *x, t_symbol *s, int argc, t_atom *argv)
{
    binbuf_add(x->m_text.te_binbuf, argc, argv);
    glist_retext(x->m_glist, &x->m_text);
546
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
547
548
549
550
551
552
553
}

static void message_add(t_message *x, t_symbol *s, int argc, t_atom *argv)
{
    binbuf_add(x->m_text.te_binbuf, argc, argv);
    binbuf_addsemi(x->m_text.te_binbuf);
    glist_retext(x->m_glist, &x->m_text);
554
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
555
556
557
558
559
560
561
562
}

static void message_addcomma(t_message *x)
{
    t_atom a;
    SETCOMMA(&a);
    binbuf_add(x->m_text.te_binbuf, 1, &a);
    glist_retext(x->m_glist, &x->m_text);
563
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
564
565
566
567
568
}

static void message_addsemi(t_message *x)
{
    message_add(x, 0, 0, 0);
569
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
570
571
572
573
574
575
576
577
578
579
580
}

static void message_adddollar(t_message *x, t_floatarg f)
{
    t_atom a;
    int n = f;
    if (n < 0)
        n = 0;
    SETDOLLAR(&a, n);
    binbuf_add(x->m_text.te_binbuf, 1, &a);
    glist_retext(x->m_glist, &x->m_text);
581
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
582
583
584
585
586
587
588
589
590
591
592
593
}

static void message_adddollsym(t_message *x, t_symbol *s)
{
    t_atom a;
    char buf[MAXPDSTRING];
    buf[0] = '$';
    strncpy(buf+1, s->s_name, MAXPDSTRING-2);
    buf[MAXPDSTRING-1] = 0;
    SETDOLLSYM(&a, gensym(buf));
    binbuf_add(x->m_text.te_binbuf, 1, &a);
    glist_retext(x->m_glist, &x->m_text);
594
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
}

static void message_click(t_message *x,
    t_floatarg xpos, t_floatarg ypos, t_floatarg shift,
        t_floatarg ctrl, t_floatarg alt)
{
    message_float(x, 0);
    if (glist_isvisible(x->m_glist))
    {
        t_rtext *y = glist_findrtext(x->m_glist, &x->m_text);
        sys_vgui(".x%lx.c itemconfigure %sR -width 5\n", 
            glist_getcanvas(x->m_glist), rtext_gettag(y));
        clock_delay(x->m_clock, 120);
    }
}

static void message_tick(t_message *x)
{
    if (glist_isvisible(x->m_glist))
    {
        t_rtext *y = glist_findrtext(x->m_glist, &x->m_text);
        sys_vgui(".x%lx.c itemconfigure %sR -width 1\n",
            glist_getcanvas(x->m_glist), rtext_gettag(y));
    }
}

static void message_free(t_message *x)
{
    clock_free(x->m_clock);
}

void canvas_msg(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
628
	//fprintf(stderr,"canvas_msg\n");
Miller Puckette's avatar
Miller Puckette committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
    t_message *x = (t_message *)pd_new(message_class);
    x->m_messresponder.mr_pd = messresponder_class;
    x->m_messresponder.mr_outlet = outlet_new(&x->m_text, &s_float);
    x->m_text.te_width = 0;                             /* don't know it yet. */
    x->m_text.te_type = T_MESSAGE;
    x->m_text.te_binbuf = binbuf_new();
    x->m_glist = gl;
    x->m_clock = clock_new(x, (t_method)message_tick);
    if (argc > 1)
    {
        x->m_text.te_xpix = atom_getfloatarg(0, argc, argv);
        x->m_text.te_ypix = atom_getfloatarg(1, argc, argv);
        if (argc > 2) binbuf_restore(x->m_text.te_binbuf, argc-2, argv+2);
        glist_add(gl, &x->m_text.te_g);
    }
Miller Puckette's avatar
Miller Puckette committed
644
645
    else if (!glist_isvisible(gl))
        post("unable to create stub message in closed canvas!");
Miller Puckette's avatar
Miller Puckette committed
646
647
    else
    {
Miller Puckette's avatar
Miller Puckette committed
648
649
650
        int connectme, xpix, ypix, indx, nobj;
        canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
        
Miller Puckette's avatar
Miller Puckette committed
651
        pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
Miller Puckette's avatar
Miller Puckette committed
652
653
        x->m_text.te_xpix = xpix;
        x->m_text.te_ypix = ypix;
Miller Puckette's avatar
Miller Puckette committed
654
655
656
657
        glist_add(gl, &x->m_text.te_g);
        glist_noselect(gl);
        glist_select(gl, &x->m_text.te_g);
        gobj_activate(&x->m_text.te_g, gl, 1);
658
        if (connectme == 1)
Miller Puckette's avatar
Miller Puckette committed
659
            canvas_connect(gl, indx, 0, nobj, 0);
660
        else if (connectme == 0) canvas_startmotion(glist_getcanvas(gl));
661
		//canvas_setundo(glist_getcanvas(gl), canvas_undo_create, canvas_undo_set_create(gl), "create");
662
663
		canvas_undo_add(glist_getcanvas(gl), 9, "create",
			(void *)canvas_undo_set_create(glist_getcanvas(gl)));
Miller Puckette's avatar
Miller Puckette committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    }
}

/* ---------------------- the "atom" text item ------------------------ */

#define ATOMBUFSIZE 40
#define ATOM_LABELLEFT 0
#define ATOM_LABELRIGHT 1
#define ATOM_LABELUP 2
#define ATOM_LABELDOWN 3

typedef struct _gatom
{
    t_text a_text;
    t_atom a_atom;          /* this holds the value and the type */
    t_glist *a_glist;       /* owning glist */
    t_float a_toggle;       /* value to toggle to */
    t_float a_draghi;       /* high end of drag range */
    t_float a_draglo;       /* low end of drag range */
    t_symbol *a_label;      /* symbol to show as label next to box */
    t_symbol *a_symfrom;    /* "receive" name -- bind ourselvs to this */
    t_symbol *a_symto;      /* "send" name -- send to this on output */
    char a_buf[ATOMBUFSIZE];/* string buffer for typing */
    char a_shift;           /* was shift key down when dragging started? */
    char a_wherelabel;      /* 0-3 for left, right, above, below */
    t_symbol *a_expanded_to; /* a_symto after $0, $1, ...  expansion */
} t_gatom;

    /* prepend "-" as necessary to avoid empty strings, so we can
    use them in Pd messages.  A more complete solution would be
    to introduce some quoting mechanism; but then we'd be much more
    complicated. */
static t_symbol *gatom_escapit(t_symbol *s)
{
    if (!*s->s_name)
        return (gensym("-"));
    else if (*s->s_name == '-')
    {
        char shmo[100];
        shmo[0] = '-';
        strncpy(shmo+1, s->s_name, 99);
        shmo[99] = 0;
        return (gensym(shmo));
    }
    else return (iemgui_dollar2raute(s));
}

    /* undo previous operation: strip leading "-" if found. */
static t_symbol *gatom_unescapit(t_symbol *s)
{
    if (*s->s_name == '-')
        return (gensym(s->s_name+1));
    else return (iemgui_raute2dollar(s));
}

static void gatom_redraw(t_gobj *client, t_glist *glist)
{
    t_gatom *x = (t_gatom *)client;
    glist_retext(x->a_glist, &x->a_text);
}

725
726
727
728
729
730
731
732
733
	/* recolor option offers 	0 ignore recolor
								1 recolor */
static void gatom_retext(t_gatom *x, int senditup, int recolor)
{
	t_canvas *canvas = glist_getcanvas(x->a_glist);
	t_rtext *y = glist_findrtext(x->a_glist, &x->a_text);
	if (recolor)
		sys_vgui(".x%lx.c itemconfigure %s -fill %s\n", canvas, 
	    	rtext_gettag(y), "$text_color");
Miller Puckette's avatar
Miller Puckette committed
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
    binbuf_clear(x->a_text.te_binbuf);
    binbuf_add(x->a_text.te_binbuf, 1, &x->a_atom);
    if (senditup && glist_isvisible(x->a_glist))
        sys_queuegui(x, x->a_glist, gatom_redraw);
}

static void gatom_set(t_gatom *x, t_symbol *s, int argc, t_atom *argv)
{
    t_atom oldatom = x->a_atom;
    int changed = 0;
    if (!argc) return;
    if (x->a_atom.a_type == A_FLOAT)
        x->a_atom.a_w.w_float = atom_getfloat(argv),
            changed = (x->a_atom.a_w.w_float != oldatom.a_w.w_float);
    else if (x->a_atom.a_type == A_SYMBOL)
        x->a_atom.a_w.w_symbol = atom_getsymbol(argv),
            changed = (x->a_atom.a_w.w_symbol != oldatom.a_w.w_symbol);
    if (changed)
752
        gatom_retext(x, 1, 0);
Miller Puckette's avatar
Miller Puckette committed
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
    x->a_buf[0] = 0;
}

static void gatom_bang(t_gatom *x)
{
    if (x->a_atom.a_type == A_FLOAT)
    {
        if (x->a_text.te_outlet)
            outlet_float(x->a_text.te_outlet, x->a_atom.a_w.w_float);
        if (*x->a_expanded_to->s_name && x->a_expanded_to->s_thing)
        {
            if (x->a_symto == x->a_symfrom)
                pd_error(x,
                    "%s: atom with same send/receive name (infinite loop)",
                        x->a_symto->s_name);
            else pd_float(x->a_expanded_to->s_thing, x->a_atom.a_w.w_float);
        }
    }
    else if (x->a_atom.a_type == A_SYMBOL)
    {
        if (x->a_text.te_outlet)
            outlet_symbol(x->a_text.te_outlet, x->a_atom.a_w.w_symbol);
        if (*x->a_symto->s_name && x->a_expanded_to->s_thing)
        {
            if (x->a_symto == x->a_symfrom)
                pd_error(x,
                    "%s: atom with same send/receive name (infinite loop)",
                        x->a_symto->s_name);
            else pd_symbol(x->a_expanded_to->s_thing, x->a_atom.a_w.w_symbol);
        }
    }
}

static void gatom_float(t_gatom *x, t_float f)
{
    t_atom at;
    SETFLOAT(&at, f);
    gatom_set(x, 0, 1, &at);
    gatom_bang(x);
}

static void gatom_clipfloat(t_gatom *x, t_float f)
{
    if (x->a_draglo != 0 || x->a_draghi != 0)
    {
        if (f < x->a_draglo)
            f = x->a_draglo;
        if (f > x->a_draghi)
            f = x->a_draghi;
    }
    gatom_float(x, f);
}

static void gatom_symbol(t_gatom *x, t_symbol *s)
{
    t_atom at;
    SETSYMBOL(&at, s);
    gatom_set(x, 0, 1, &at);
    gatom_bang(x);
}

    /* We need a list method because, since there's both an "inlet" and a
    "nofirstin" flag, the standard list behavior gets confused. */
static void gatom_list(t_gatom *x, t_symbol *s, int argc, t_atom *argv)
{
    if (!argc)
        gatom_bang(x);
    else if (argv->a_type == A_FLOAT)
        gatom_float(x, argv->a_w.w_float);
    else if (argv->a_type == A_SYMBOL)
        gatom_symbol(x, argv->a_w.w_symbol);
    else pd_error(x, "gatom_list: need float or symbol");
}

static void gatom_motion(void *z, t_floatarg dx, t_floatarg dy)
{
    t_gatom *x = (t_gatom *)z;
    if (dy == 0) return;
    if (x->a_atom.a_type == A_FLOAT)
    {
        if (x->a_shift)
        {
            double nval = x->a_atom.a_w.w_float - 0.01 * dy;
            double trunc = 0.01 * (floor(100. * nval + 0.5));
            if (trunc < nval + 0.0001 && trunc > nval - 0.0001) nval = trunc;
            gatom_clipfloat(x, nval);
        }
        else
        {
            double nval = x->a_atom.a_w.w_float - dy;
            double trunc = 0.01 * (floor(100. * nval + 0.5));
            if (trunc < nval + 0.0001 && trunc > nval - 0.0001) nval = trunc;
            trunc = floor(nval + 0.5);
            if (trunc < nval + 0.001 && trunc > nval - 0.001) nval = trunc;
            gatom_clipfloat(x, nval);
        }
    }
}

static void gatom_key(void *z, t_floatarg f)
{
854
	//fprintf(stderr,"gatom_key %f\n", f);
Miller Puckette's avatar
Miller Puckette committed
855
856
857
858
859
860
861
862
863
    t_gatom *x = (t_gatom *)z;
    int c = f;
    int len = strlen(x->a_buf);
    t_atom at;
    char sbuf[ATOMBUFSIZE + 4];
    if (c == 0)
    {
        /* we're being notified that no more keys will come for this grab */
        if (x->a_buf[0])
864
865
866
            gatom_retext(x, 1, 1);
		else
			gatom_retext(x, 0, 1);
Miller Puckette's avatar
Miller Puckette committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
        return;
    }
    else if (c == '\b')
    {
        if (len > 0)
        x->a_buf[len-1] = 0;
        goto redraw;
    }
    else if (c == '\n')
    {
        if (x->a_atom.a_type == A_FLOAT)
            x->a_atom.a_w.w_float = atof(x->a_buf);
        else if (x->a_atom.a_type == A_SYMBOL)
            x->a_atom.a_w.w_symbol = gensym(x->a_buf);
        else bug("gatom_key");
        gatom_bang(x);
883
        gatom_retext(x, 1, 1);
Miller Puckette's avatar
Miller Puckette committed
884
885
886
887
888
889
890
891
892
        x->a_buf[0] = 0;
    }
    else if (len < (ATOMBUFSIZE-1))
    {
            /* for numbers, only let reasonable characters through */
        if ((x->a_atom.a_type == A_SYMBOL) ||
            (c >= '0' && c <= '9' || c == '.' || c == '-'
                || c == 'e' || c == 'E'))
        {
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
            /* the wchar could expand to up to 4 bytes, which
             * which might overrun our a_buf;
             * therefore we first expand into a temporary buffer, 
             * and only if the resulting utf8 string fits into a_buf
             * we apply it
             */
            char utf8[UTF8_MAXBYTES];
            int utf8len = u8_wc_toutf8(utf8, c);
            if((len+utf8len) < (ATOMBUFSIZE-1))
            {
                int j=0;
                for(j=0; j<utf8len; j++)
                    x->a_buf[len+j] = utf8[j];
                 
                x->a_buf[len+utf8len] = 0;
            }
Miller Puckette's avatar
Miller Puckette committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
            goto redraw;
        }
    }
    return;
redraw:
        /* LATER figure out how to avoid creating all these symbols! */
    sprintf(sbuf, "%s...", x->a_buf);
    SETSYMBOL(&at, gensym(sbuf));
    binbuf_clear(x->a_text.te_binbuf);
    binbuf_add(x->a_text.te_binbuf, 1, &at);
    glist_retext(x->a_glist, &x->a_text);
}

static void gatom_click(t_gatom *x,
    t_floatarg xpos, t_floatarg ypos, t_floatarg shift, t_floatarg ctrl,
    t_floatarg alt)
{
    if (x->a_text.te_width == 1)
    {
        if (x->a_atom.a_type == A_FLOAT)
            gatom_float(x, (x->a_atom.a_w.w_float == 0));
    }
    else
    {
        if (alt)
        {
            if (x->a_atom.a_type != A_FLOAT) return;
            if (x->a_atom.a_w.w_float != 0)
            {
                x->a_toggle = x->a_atom.a_w.w_float;
                gatom_float(x, 0);
                return;
            }
            else gatom_float(x, x->a_toggle);
        }
        x->a_shift = shift;
        x->a_buf[0] = 0;
        glist_grab(x->a_glist, &x->a_text.te_g, gatom_motion, gatom_key,
            xpos, ypos);
    }
}

951
952
953
EXTERN int glist_getindex(t_glist *x, t_gobj *y);
EXTERN int canvas_apply_restore_original_position(t_canvas *x, int pos);

Miller Puckette's avatar
Miller Puckette committed
954
955
956
    /* message back from dialog window */
static void gatom_param(t_gatom *x, t_symbol *sel, int argc, t_atom *argv)
{
957
958
	canvas_apply_setundo(x->a_glist, (t_gobj *)x);

Miller Puckette's avatar
Miller Puckette committed
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
    t_float width = atom_getfloatarg(0, argc, argv);
    t_float draglo = atom_getfloatarg(1, argc, argv);
    t_float draghi = atom_getfloatarg(2, argc, argv);
    t_symbol *label = gatom_unescapit(atom_getsymbolarg(3, argc, argv));
    t_float wherelabel = atom_getfloatarg(4, argc, argv);
    t_symbol *symfrom = gatom_unescapit(atom_getsymbolarg(5, argc, argv));
    t_symbol *symto = gatom_unescapit(atom_getsymbolarg(6, argc, argv));

    gobj_vis(&x->a_text.te_g, x->a_glist, 0);
    if (!*symfrom->s_name && *x->a_symfrom->s_name)
        inlet_new(&x->a_text, &x->a_text.te_pd, 0, 0);
    else if (*symfrom->s_name && !*x->a_symfrom->s_name && x->a_text.te_inlet)
    {
        canvas_deletelinesforio(x->a_glist, &x->a_text,
            x->a_text.te_inlet, 0);
        inlet_free(x->a_text.te_inlet);
    }
    if (!*symto->s_name && *x->a_symto->s_name)
        outlet_new(&x->a_text, 0);
    else if (*symto->s_name && !*x->a_symto->s_name && x->a_text.te_outlet)
    {
        canvas_deletelinesforio(x->a_glist, &x->a_text,
            0, x->a_text.te_outlet);
        outlet_free(x->a_text.te_outlet);
    }
    if (draglo >= draghi)
        draglo = draghi = 0;
    x->a_draglo = draglo;
    x->a_draghi = draghi;
    if (width < 0)
        width = 4;
    else if (width > 80)
        width = 80;
    x->a_text.te_width = width;
    x->a_wherelabel = ((int)wherelabel & 3);
    x->a_label = label;
    if (*x->a_symfrom->s_name)
        pd_unbind(&x->a_text.te_pd,
            canvas_realizedollar(x->a_glist, x->a_symfrom));
    x->a_symfrom = symfrom;
    if (*x->a_symfrom->s_name)
        pd_bind(&x->a_text.te_pd,
            canvas_realizedollar(x->a_glist, x->a_symfrom));
    x->a_symto = symto;
    x->a_expanded_to = canvas_realizedollar(x->a_glist, x->a_symto);
    gobj_vis(&x->a_text.te_g, x->a_glist, 1);
1005
	gobj_select(&x->a_text.te_g, x->a_glist, 1);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1006
    canvas_dirty(x->a_glist, 1);
1007
	sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", (t_int)x->a_glist);
Miller Puckette's avatar
Miller Puckette committed
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

    /* glist_retext(x->a_glist, &x->a_text); */
}

    /* ---------------- gatom-specific widget functions --------------- */
static void gatom_getwherelabel(t_gatom *x, t_glist *glist, int *xp, int *yp)
{
    int x1, y1, x2, y2, width, height;
    text_getrect(&x->a_text.te_g, glist, &x1, &y1, &x2, &y2);
    width = x2 - x1;
    height = y2 - y1;
    if (x->a_wherelabel == ATOM_LABELLEFT)
    {
        *xp = x1 - 3 -
            strlen(canvas_realizedollar(x->a_glist, x->a_label)->s_name) *
            sys_fontwidth(glist_getfont(glist));
        *yp = y1 + 2;
    }
    else if (x->a_wherelabel == ATOM_LABELRIGHT)
    {
        *xp = x2 + 2;
        *yp = y1 + 2;
    }
    else if (x->a_wherelabel == ATOM_LABELUP)
    {
        *xp = x1 - 1;
        *yp = y1 - 1 - sys_fontheight(glist_getfont(glist));;
    }
    else
    {
        *xp = x1 - 1;
        *yp = y2 + 3;
    }
}

static void gatom_displace(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
1046
1047
1048
1049
1050
1051
1052
	//fprintf(stderr,"gatom_displace\n");
	t_gatom *x = (t_gatom*)z;
	text_displace(z, glist, dx, dy);
	if (glist_isvisible(glist)) {
		sys_vgui(".x%lx.c move %lx.l %d %d\n", glist_getcanvas(glist), 
		    x, dx, dy);
	}
Miller Puckette's avatar
Miller Puckette committed
1053
1054
1055
1056
}

static void gatom_vis(t_gobj *z, t_glist *glist, int vis)
{
1057
	//fprintf(stderr,"gatom_vis\n");
Miller Puckette's avatar
Miller Puckette committed
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
    t_gatom *x = (t_gatom*)z;
    text_vis(z, glist, vis);
    if (*x->a_label->s_name)
    {
        if (vis)
        {
            int x1, y1;
            gatom_getwherelabel(x, glist, &x1, &y1);
            sys_vgui("pdtk_text_new .x%lx.c %lx.l %f %f {%s} %d %s\n",
                glist_getcanvas(glist), x,
                (double)x1, (double)y1,
                canvas_realizedollar(x->a_glist, x->a_label)->s_name,
                sys_hostfontsize(glist_getfont(glist)),
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1071
                "$text_color");
Miller Puckette's avatar
Miller Puckette committed
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
        }
        else sys_vgui(".x%lx.c delete %lx.l\n", glist_getcanvas(glist), x);
    }
    if (!vis)
        sys_unqueuegui(x);
}

void canvas_atom(t_glist *gl, t_atomtype type,
    t_symbol *s, int argc, t_atom *argv)
{
1082
	//fprintf(stderr,"canvas_atom\n");
Miller Puckette's avatar
Miller Puckette committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
    t_gatom *x = (t_gatom *)pd_new(gatom_class);
    t_atom at;
    x->a_text.te_width = 0;                        /* don't know it yet. */
    x->a_text.te_type = T_ATOM;
    x->a_text.te_binbuf = binbuf_new();
    x->a_glist = gl;
    x->a_atom.a_type = type;
    x->a_toggle = 1;
    x->a_draglo = 0;
    x->a_draghi = 0;
    x->a_wherelabel = 0;
    x->a_label = &s_;
    x->a_symfrom = &s_;
    x->a_symto = x->a_expanded_to = &s_;
    if (type == A_FLOAT)
    {
        x->a_atom.a_w.w_float = 0;
        x->a_text.te_width = 5;
        SETFLOAT(&at, 0);
    }
    else
    {
        x->a_atom.a_w.w_symbol = &s_symbol;
        x->a_text.te_width = 10;
        SETSYMBOL(&at, &s_symbol);
    }
    binbuf_add(x->a_text.te_binbuf, 1, &at);
    if (argc > 1)
        /* create from file. x, y, width, low-range, high-range, flags,
            label, receive-name, send-name */
    {
        x->a_text.te_xpix = atom_getfloatarg(0, argc, argv);
        x->a_text.te_ypix = atom_getfloatarg(1, argc, argv);
        x->a_text.te_width = atom_getintarg(2, argc, argv);
            /* sanity check because some very old patches have trash in this
            field... remove this in 2003 or so: */
        if (x->a_text.te_width < 0 || x->a_text.te_width > 500)
            x->a_text.te_width = 4;
        x->a_draglo = atom_getfloatarg(3, argc, argv);
        x->a_draghi = atom_getfloatarg(4, argc, argv);
        x->a_wherelabel = (((int)atom_getfloatarg(5, argc, argv)) & 3);
        x->a_label = gatom_unescapit(atom_getsymbolarg(6, argc, argv));
        x->a_symfrom = gatom_unescapit(atom_getsymbolarg(7, argc, argv));
        if (*x->a_symfrom->s_name)
            pd_bind(&x->a_text.te_pd,
                canvas_realizedollar(x->a_glist, x->a_symfrom));

        x->a_symto = gatom_unescapit(atom_getsymbolarg(8, argc, argv));
        x->a_expanded_to = canvas_realizedollar(x->a_glist, x->a_symto);
        if (x->a_symto == &s_)
            outlet_new(&x->a_text,
                x->a_atom.a_type == A_FLOAT ? &s_float: &s_symbol);
        if (x->a_symfrom == &s_)
            inlet_new(&x->a_text, &x->a_text.te_pd, 0, 0);
        glist_add(gl, &x->a_text.te_g);
    }
    else
    {
Miller Puckette's avatar
Miller Puckette committed
1141
1142
        int connectme, xpix, ypix, indx, nobj;
        canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
Miller Puckette's avatar
Miller Puckette committed
1143
1144
1145
1146
1147
1148
1149
1150
1151
        outlet_new(&x->a_text,
            x->a_atom.a_type == A_FLOAT ? &s_float: &s_symbol);
        inlet_new(&x->a_text, &x->a_text.te_pd, 0, 0);
        pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
        x->a_text.te_xpix = xpix;
        x->a_text.te_ypix = ypix;
        glist_add(gl, &x->a_text.te_g);
        glist_noselect(gl);
        glist_select(gl, &x->a_text.te_g);
1152
        if (connectme == 1)
Miller Puckette's avatar
Miller Puckette committed
1153
            canvas_connect(gl, indx, 0, nobj, 0);
1154
        else if (connectme == 0) canvas_startmotion(glist_getcanvas(gl));
1155
		//canvas_setundo(glist_getcanvas(gl), canvas_undo_create, canvas_undo_set_create(gl), "create");
1156
1157
		canvas_undo_add(glist_getcanvas(gl), 9, "create",
			(void *)canvas_undo_set_create(glist_getcanvas(gl)));
Miller Puckette's avatar
Miller Puckette committed
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
    }
}

void canvas_floatatom(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_atom(gl, A_FLOAT, s, argc, argv);
}

void canvas_symbolatom(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
    canvas_atom(gl, A_SYMBOL, s, argc, argv);
}

static void gatom_free(t_gatom *x)
{
    if (*x->a_symfrom->s_name)
        pd_unbind(&x->a_text.te_pd,
            canvas_realizedollar(x->a_glist, x->a_symfrom));
    gfxstub_deleteforkey(x);
}

static void gatom_properties(t_gobj *z, t_glist *owner)
{
    t_gatom *x = (t_gatom *)z;
    char buf[200];
    sprintf(buf, "pdtk_gatom_dialog %%s %d %g %g %d {%s} {%s} {%s}\n",
        x->a_text.te_width, x->a_draglo, x->a_draghi,
            x->a_wherelabel, gatom_escapit(x->a_label)->s_name,
                gatom_escapit(x->a_symfrom)->s_name,
                    gatom_escapit(x->a_symto)->s_name);
    gfxstub_new(&x->a_text.te_pd, x, buf);
}


/* -------------------- widget behavior for text objects ------------ */

static void text_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
1197
	//fprintf(stderr,"text_getrect\n");
Miller Puckette's avatar
Miller Puckette committed
1198
1199
    t_text *x = (t_text *)z;
    int width, height, iscomment = (x->te_type == T_TEXT);
1200
    t_float x1, y1, x2, y2;
Miller Puckette's avatar
Miller Puckette committed
1201
1202
1203
1204
1205
1206

        /* for number boxes, we know width and height a priori, and should
        report them here so that graphs can get swelled to fit. */
    
    if (x->te_type == T_ATOM && x->te_width > 0)
    {
1207
		//fprintf(stderr,"	T_ATOM\n");
Miller Puckette's avatar
Miller Puckette committed
1208
1209
1210
        int font = glist_getfont(glist);
        int fontwidth = sys_fontwidth(font), fontheight = sys_fontheight(font);
        width = (x->te_width > 0 ? x->te_width : 6) * fontwidth + 2;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1211
        height = fontheight + 3; /* borrowed from TMARGIN, etc, in g_rtext.c */
1212
    }
1213
1214
1215
1216
1217
1218
1219
    // jsarlo
    else if (strcmp("magicGlass", class_getname(x->ob_pd)) == 0)
    {
        width = 0;
        height = 0;
    }
    // end jsarlo
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
    else if (x->te_type == T_TEXT)
    {
		//fprintf(stderr,"	T_TEXT\n");
        t_rtext *y = glist_findrtext(glist, x);
		if (y) {
		    width = rtext_width(y);
			height = rtext_height(y);
		} else {
			width = height = 10;
		}
		//fprintf(stderr,"T_TEXT width=%d height=%d\n", width, height);
Miller Puckette's avatar
Miller Puckette committed
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
    }
        /* if we're invisible we don't know our size so we just lie about
        it.  This is called on invisible boxes to establish order of inlets
        and possibly other reasons.
           To find out if the box is visible we can't just check the "vis"
        flag because we might be within the vis() routine and not have set
        that yet.  So we check directly whether the "rtext" list has been
        built.  LATER reconsider when "vis" flag should be on and off? */
    else if (glist->gl_editor && glist->gl_editor->e_rtext)
    {
        t_rtext *y = glist_findrtext(glist, x);
        width = rtext_width(y);
1243

1244
		//fprintf(stderr,"rtext_width=%d\n", width);
1245

1246
1247
		/*  now find if we have more inlets or outlets than
			what can comfortably fit and adjust accordingly
1248
1249
			NB: textless GOPs are unaffected and are treated
			as GUI objects
1250
1251
1252
		*/

		//fprintf(stderr,"isgraph %d\n", ((t_glist *)z)->gl_isgraph);
1253
		//if (!((t_glist *)z)->gl_isgraph) {
1254

1255
1256
1257
		t_object *ob = pd_checkobject(&x->te_pd);
		int no = obj_noutlets(ob);
		int ni = obj_ninlets(ob);
1258

1259
		int m = ( ni > no ? ni : no);
1260
1261
		//let's see if the object has more nlets than its text width and resize them accordingly
		//UNLESS we are gop in which case it is user's choice how big/small they want the object
1262
1263
		if (pd_class(&z->g_pd) != canvas_class || 
				pd_class(&z->g_pd) == canvas_class && !((t_glist *)z)->gl_isgraph) {
1264
1265
1266
1267
			if (width < (IOWIDTH * m) * 2 - IOWIDTH) {
				//we have to resize the object
				width = (IOWIDTH * m) * 2 - IOWIDTH;
			}
1268
		}
Miller Puckette's avatar
Miller Puckette committed
1269
1270
        height = rtext_height(y) - (iscomment << 1);
    }
1271
1272
1273
1274
    else {
		width = height = 10;
		//fprintf(stderr,"	default\n");
	}
Miller Puckette's avatar
Miller Puckette committed
1275
1276
1277
1278
    x1 = text_xpix(x, glist);
    y1 = text_ypix(x, glist);
    x2 = x1 + width;
    y2 = y1 + height;
1279
1280
	//x1 += iscomment*2;
    //y1 += iscomment*6;
Miller Puckette's avatar
Miller Puckette committed
1281
1282
1283
1284
1285
1286
1287
1288
1289
    *xp1 = x1;
    *yp1 = y1;
    *xp2 = x2;
    *yp2 = y2;
}

static void text_displace(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
1290
	//fprintf(stderr,"text_displace\n");
Miller Puckette's avatar
Miller Puckette committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
    t_text *x = (t_text *)z;
    x->te_xpix += dx;
    x->te_ypix += dy;
    if (glist_isvisible(glist))
    {
        t_rtext *y = glist_findrtext(glist, x);
        rtext_displace(y, dx, dy);
        text_drawborder(x, glist, rtext_gettag(y),
            rtext_width(y), rtext_height(y), 0);
        canvas_fixlinesfor(glist_getcanvas(glist), x);
    }
}

1304
1305
1306
static void text_displace_withtag(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
1307
	//fprintf(stderr,"text_displace_withtag\n");
1308
1309
1310
1311
1312
1313
    t_text *x = (t_text *)z;
    x->te_xpix += dx;
    x->te_ypix += dy;
    if (glist_isvisible(glist))
    {
        t_rtext *y = glist_findrtext(glist, x);
1314
1315
        //text_drawborder_withtag(x, glist, rtext_gettag(y),
        //    rtext_width(y), rtext_height(y), 0);
1316
        canvas_fixlinesfor(glist_getcanvas(glist), x);
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
		/*if this is a subpatcher in which case we may be possibly moving nlets around
		  which in turn requires that we redraw parent's nlets */
/*		if (glist->gl_owner && glist_isvisible(glist->gl_owner)) {
		    int resortin = 0, resortout = 0;
		    t_class *cl = pd_class(&z->g_pd);
		    if (cl == vinlet_class) resortin = 1;
		    else if (cl == voutlet_class) resortout = 1;
			fprintf(stderr,"vinlet=%d voutlet=%d\n", resortin, resortout);
			if (resortin) canvas_resortinlets(glist->gl_owner);
			if (resortout) canvas_resortoutlets(glist->gl_owner);

			char *buf;
			char name[6];
			int bufsize, i;
			rtext_gettext(y, &buf, &bufsize);
			for (i = 0; i < 5; i++) {
				name[i] = buf[i];
			}
			name[5] = '\0';
			//fprintf(stderr,"yes, this is a subpatch with visible parent %s\n", name);
			if (!strcmp(name, "inlet") ||
				!strcmp(name, "outle")) {
				
				//fprintf(stderr,"yes, we're moving nlets around\n");	
				glist_redraw(glist->gl_owner);
			}
		}*/
    }	
1345
1346
1347
1348
1349
1350
1351
}

static void gatom_displace_withtag(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
    t_gatom *x = (t_gatom*)z;
    text_displace_withtag(z, glist, dx, dy);
1352
1353
    //sys_vgui(".x%lx.c move %lx.l %d %d\n", glist_getcanvas(glist), 
    //    x, dx, dy);
1354
1355
}

Miller Puckette's avatar
Miller Puckette committed
1356
1357
1358
1359
static void text_select(t_gobj *z, t_glist *glist, int state)
{
    t_text *x = (t_text *)z;
    t_rtext *y = glist_findrtext(glist, x);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1360
    char *outline;
Miller Puckette's avatar
Miller Puckette committed
1361
    rtext_select(y, state);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1362
1363
1364
1365
    if (pd_class(&x->te_pd) == text_class)
        outline = "$dash_outline";
    else
        outline = "$box_outline";
1366
1367
	//fprintf(stderr,"text_select isvisible=%d shouldvis=%d istoplevel=%d\n", glist_isvisible(glist), gobj_shouldvis(&x->te_g, glist), glist_istoplevel(glist));
    if (gobj_shouldvis(&x->te_g, glist)) {
1368
1369
1370
1371
		if (glist_istoplevel(glist)) {
		    sys_vgui(".x%lx.c itemconfigure %sR -outline %s\n", glist_getcanvas(glist), 
		             rtext_gettag(y), (state? "$select_color" : outline));
			if (z->g_pd == gatom_class) {
1372
				sys_vgui(".x%lx.c itemconfigure %lx.l -fill %s\n", glist_getcanvas(glist), 
1373
1374
					x, (state? "$select_color" : "$text_color"));
			}
1375
		}
1376
1377
1378
		if (z->g_pd->c_wb && z->g_pd->c_wb->w_displacefnwtag) {
			int i, ni, no;
			if (state) {
1379
1380
1381
1382
				if (z->g_pd == gatom_class) {
					sys_vgui(".x%lx.c addtag selected withtag %lx.l\n",
						glist_getcanvas(glist), x);					
				}
1383
1384
1385
		    	sys_vgui(".x%lx.c addtag selected withtag %sR \n",
		    	    glist_getcanvas(glist), rtext_gettag(y));

1386
				if (pd_class(&x->te_pd) == text_class && glist_istoplevel(glist))
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
	        		sys_vgui(".x%lx.c itemconfigure %sR -width 1 -fill $obj_box_fill\n",
						glist_getcanvas(glist), rtext_gettag(y));

				t_object *ob = pd_checkobject(&x->te_pd);
				int no = obj_noutlets(ob);
				int ni = obj_ninlets(ob);

				for (i = 0; i < no; i++) {
					sys_vgui(".x%lx.c addtag selected withtag %so%d \n",
									glist_getcanvas(glist), rtext_gettag(y), i);
				}
				for (i = 0; i < ni; i++) {
					sys_vgui(".x%lx.c addtag selected withtag %si%d \n",
        	    		glist_getcanvas(glist), rtext_gettag(y), i);
				}
			} else {
1403
1404
1405
1406
				if (z->g_pd == gatom_class) {
					sys_vgui(".x%lx.c dtag %lx.l selected\n",
						glist_getcanvas(glist), x);					
				}
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
		    	sys_vgui(".x%lx.c dtag %sR selected\n",
		        	glist_getcanvas(glist), rtext_gettag(y));

				if (pd_class(&x->te_pd) == text_class)
			   		sys_vgui(".x%lx.c itemconfigure %sR -width 2 -fill %s\n",
						glist_getcanvas(glist), 
		            	rtext_gettag(y), invalid_fill);

				t_object *ob = pd_checkobject(&x->te_pd);
				int no = obj_noutlets(ob);
				int ni = obj_ninlets(ob);

				for (i = 0; i < no; i++) {
					sys_vgui(".x%lx.c dtag %so%d selected\n",
		    	    	glist_getcanvas(glist), rtext_gettag(y), i);
				}
				for (i = 0; i < ni; i++) {
					sys_vgui(".x%lx.c dtag %si%d selected\n",
		    	    	glist_getcanvas(glist), rtext_gettag(y), i);
				}
			}
		}
	}
Miller Puckette's avatar
Miller Puckette committed
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
}

static void text_activate(t_gobj *z, t_glist *glist, int state)
{
    t_text *x = (t_text *)z;
    t_rtext *y = glist_findrtext(glist, x);
    if (z->g_pd != gatom_class) rtext_activate(y, state);
}

static void text_delete(t_gobj *z, t_glist *glist)
{
    t_text *x = (t_text *)z;
    canvas_deletelinesfor(glist, x);
}

static void text_vis(t_gobj *z, t_glist *glist, int vis)
{
1447
	//fprintf(stderr,"text_vis %d\n", vis);
Miller Puckette's avatar
Miller Puckette committed
1448
    t_text *x = (t_text *)z;
1449
1450
1451
1452
1453

#ifdef PDL2ORK
	//if we are in k12 mode and this is hub with level 1 (global) don't draw it and make its width/height 0
	int exception = 0;
	if (pd_class(&x->te_pd) == preset_hub_class && sys_k12_mode) {
Ivica Bukvic's avatar
Ivica Bukvic committed
1454
		//fprintf(stderr,"text_vis reports preset_hub_class detected\n");
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
		t_preset_hub *h = (t_preset_hub *)z;
		if (h->ph_invis) {
			exception = 1;
			x->te_width = 0;
		}
	}
	if (!exception) {
#endif
		if (vis)
		{
		    if (gobj_shouldvis(&x->te_g, glist))
		    {
1467
				//fprintf(stderr,"	draw it\n");
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
		        t_rtext *y = glist_findrtext(glist, x);
		        if (x->te_type == T_ATOM)
		            glist_retext(glist, x);
		        text_drawborder(x, glist, rtext_gettag(y),
		            rtext_width(y), rtext_height(y), 1);
		        rtext_draw(y);
		    }
		}
		else
		{
		    t_rtext *y = glist_findrtext(glist, x);
		    if (gobj_shouldvis(&x->te_g, glist))
		    {
1481
				//fprintf(stderr,"	erase it\n");
1482
1483
1484
1485
1486
1487
1488
		        text_eraseborder(x, glist, rtext_gettag(y));
		        rtext_erase(y);
		    }
		}
#ifdef PDL2ORK
	}
#endif
Miller Puckette's avatar
Miller Puckette committed
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
}

static int text_click(t_gobj *z, struct _glist *glist,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
    t_text *x = (t_text *)z;
    if (x->te_type == T_OBJECT)
    {
        t_symbol *clicksym = gensym("click");
        if (zgetfn(&x->te_pd, clicksym))
        {
            if (doit)
                pd_vmess(&x->te_pd, clicksym, "fffff",
                    (double)xpix, (double)ypix,
                        (double)shift, (double)0, (double)alt);
            return (1);
        }
        else return (0);
    }
    else if (x->te_type == T_ATOM)
    {
1510
1511
1512
		t_canvas *canvas = glist_getcanvas(glist);
		t_rtext *y = glist_findrtext(glist, x);
        if (doit) {
Miller Puckette's avatar
Miller Puckette committed
1513
1514
            gatom_click((t_gatom *)x, (t_floatarg)xpix, (t_floatarg)ypix,
                (t_floatarg)shift, (t_floatarg)0, (t_floatarg)alt);
1515
1516
1517
1518
			//fprintf(stderr,"atom click\n");
			sys_vgui(".x%lx.c itemconfigure %s -fill %s\n", canvas, 
		    	rtext_gettag(y), "$select_color");
		}
Miller Puckette's avatar
Miller Puckette committed
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
        return (1);
    }
    else if (x->te_type == T_MESSAGE)
    {
        if (doit)
            message_click((t_message *)x, (t_floatarg)xpix, (t_floatarg)ypix,
                (t_floatarg)shift, (t_floatarg)0, (t_floatarg)alt);
        return (1);
    }
    else return (0);
}

void text_save(t_gobj *z, t_binbuf *b)
{
1533
	//fprintf(stderr, "text_save\n");
Miller Puckette's avatar
Miller Puckette committed
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
    t_text *x = (t_text *)z;
    if (x->te_type == T_OBJECT)
    {
            /* if we have a "saveto" method, and if we don't happen to be
            a canvas that's an abstraction, the saveto method does the work */
        if (zgetfn(&x->te_pd, gensym("saveto")) &&
            !((pd_class(&x->te_pd) == canvas_class) && 
                (canvas_isabstraction((t_canvas *)x)
                    || canvas_istable((t_canvas *)x))))
        {  
1544
			//fprintf(stderr, "saveto\n");
Miller Puckette's avatar
Miller Puckette committed
1545
1546
1547
1548
1549
1550
            mess1(&x->te_pd, gensym("saveto"), b);
            binbuf_addv(b, "ssii", gensym("#X"), gensym("restore"),
                (int)x->te_xpix, (int)x->te_ypix);
        }
        else    /* otherwise just save the text */
        {
1551
			//fprintf(stderr, "is this gop?\n");
Miller Puckette's avatar
Miller Puckette committed
1552
1553
1554
            binbuf_addv(b, "ssii", gensym("#X"), gensym("obj"),
                (int)x->te_xpix, (int)x->te_ypix);
        }
1555
		//fprintf(stderr, "this must be it\n");
Miller Puckette's avatar
Miller Puckette committed
1556
        binbuf_addbinbuf(b, x->te_binbuf);
1557
		//fprintf(stderr, "DONE this must be it\n");
Miller Puckette's avatar
Miller Puckette committed
1558
1559
1560
1561
        binbuf_addv(b, ";");
    }
    else if (x->te_type == T_MESSAGE)
    {
1562
		//fprintf(stderr, "message\n");
Miller Puckette's avatar
Miller Puckette committed
1563
1564
1565
1566
1567
1568
1569
        binbuf_addv(b, "ssii", gensym("#X"), gensym("msg"),
            (int)x->te_xpix, (int)x->te_ypix);
        binbuf_addbinbuf(b, x->te_binbuf);
        binbuf_addv(b, ";");
    }
    else if (x->te_type == T_ATOM)
    {
1570
		//fprintf(stderr, "atom\n");
Miller Puckette's avatar
Miller Puckette committed
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
        t_atomtype t = ((t_gatom *)x)->a_atom.a_type;
        t_symbol *sel = (t == A_SYMBOL ? gensym("symbolatom") :
            (t == A_FLOAT ? gensym("floatatom") : gensym("intatom")));
        t_symbol *label = gatom_escapit(((t_gatom *)x)->a_label);
        t_symbol *symfrom = gatom_escapit(((t_gatom *)x)->a_symfrom);
        t_symbol *symto = gatom_escapit(((t_gatom *)x)->a_symto);
        binbuf_addv(b, "ssiiifffsss", gensym("#X"), sel,
            (int)x->te_xpix, (int)x->te_ypix, (int)x->te_width,
            (double)((t_gatom *)x)->a_draglo,
            (double)((t_gatom *)x)->a_draghi,
            (double)((t_gatom *)x)->a_wherelabel,
            label, symfrom, symto);
        binbuf_addv(b, ";");
    }           
1585
    else    
Miller Puckette's avatar
Miller Puckette committed
1586
    {
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
        //fprintf(stderr,"comment\n");
        int natom = binbuf_getnatom(x->te_binbuf);
        t_atom *a = binbuf_getvec(x->te_binbuf);
        int i;
        for (i = 0;