g_text.c 95 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
/* 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 "g_canvas.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
17
18
#include "g_undo.h"
#include "x_preset.h"
Miller Puckette's avatar
Miller Puckette committed
19

20
21
#include "s_utf8.h"

Miller Puckette's avatar
Miller Puckette committed
22
t_class *text_class;
23
24
t_class *message_class;
static t_class *gatom_class;
25
static t_class *dropdown_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
extern void canvas_displaceselection(t_canvas *x, int dx, int dy);
38
extern void canvas_apply_setundo(t_canvas *x, t_gobj *y);
39
40
extern void canvas_setundo(t_canvas *x, t_undofn undofn, void *buf,
    const char *name);
41
42
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_check_loc_and_update(void);
45
extern void glob_preset_node_list_seek_hub(void);
46

Miller Puckette's avatar
Miller Puckette committed
47
48
49
50
51
52
53
54
55
/* ----------------- 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)
{
56
    if (canvas_hasarray(gl)) return;
Miller Puckette's avatar
Miller Puckette committed
57
58
59
60
    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;
61
    x->te_iemgui = 0;
Miller Puckette's avatar
Miller Puckette committed
62
63
64
65
66
    x->te_binbuf = binbuf_new();
    if (argc > 1)
    {
        x->te_xpix = atom_getfloatarg(0, argc, argv);
        x->te_ypix = atom_getfloatarg(1, argc, argv);
67
68
        if (argc > 2)
        {
69
70
            binbuf_restore(x->te_binbuf, argc-2, argv+2);
        }
Miller Puckette's avatar
Miller Puckette committed
71
72
73
74
75
76
77
78
79
        else
        {
            SETSYMBOL(&at, gensym("comment"));
            binbuf_restore(x->te_binbuf, 1, &at);
        }
        glist_add(gl, &x->te_g);
    }
    else
    {
80
        //int xpix, ypix;
81
82
        int connectme, xpix, ypix, indx, nobj;
        canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
Miller Puckette's avatar
Miller Puckette committed
83
84
85
        pd_vmess((t_pd *)glist_getcanvas(gl), gensym("editmode"), "i", 1);
        SETSYMBOL(&at, gensym("comment"));
        glist_noselect(gl);
86
87
88
        //glist_getnextxy(gl, &xpix, &ypix);
        x->te_xpix = xpix;
        x->te_ypix = ypix;
Miller Puckette's avatar
Miller Puckette committed
89
90
91
92
93
94
95
96
97
98
        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); */
99
100
101
        if (!we_are_undoing)
            canvas_undo_add(glist_getcanvas(gl), 9, "create",
                (void *)canvas_undo_set_create(glist_getcanvas(gl)));
102
103
        if (connectme == 0)
        {
104
            canvas_displaceselection(glist_getcanvas(gl), -8, -8);
105
106
            canvas_startmotion(glist_getcanvas(gl));
        }
Miller Puckette's avatar
Miller Puckette committed
107
    }
108
109
    glob_preset_node_list_seek_hub();
    glob_preset_node_list_check_loc_and_update();
Miller Puckette's avatar
Miller Puckette committed
110
111
112
113
114
}

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

extern t_pd *newest;
115
int scalar_in_a_box;
116
extern void glist_scalar(t_glist *canvas, t_symbol *s, int argc, t_atom *argv);
Miller Puckette's avatar
Miller Puckette committed
117
118
void canvas_getargs(int *argcp, t_atom **argvp);

119
static void canvas_objtext(t_glist *gl, int xpix, int ypix,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
120
    int width, int selected, t_binbuf *b, int connectme)
Miller Puckette's avatar
Miller Puckette committed
121
{
122
    //fprintf(stderr,"canvas_objtext\n");
Miller Puckette's avatar
Miller Puckette committed
123
124
125
    t_text *x;
    int argc;
    t_atom *argv;
126

127
128
129
130
    // for hiding arguments
    t_atom *vec;
    int len, i, hidden;
    t_binbuf *hide;
131

Miller Puckette's avatar
Miller Puckette committed
132
133
134
135
136
137
138
139
    newest = 0;
    canvas_setcurrent((t_canvas *)gl);
    canvas_getargs(&argc, &argv);
    binbuf_eval(b, &pd_objectmaker, argc, argv);
    if (binbuf_getnatom(b))
    {
        if (!newest)
        {
140
141
142
143
144
145
            /* let's see if there's a scalar by this name... */
            t_atom *scalar_at = binbuf_getvec(b);
            if (scalar_at->a_type == A_SYMBOL)
            {
                t_symbol *templatesym = 
                    canvas_makebindsym(atom_getsymbol(scalar_at));
146
147
148
149
                t_template *tmpl = template_findbyname(templatesym);
                if (template_findbyname(templatesym) &&
                    template_cancreate(tmpl) &&
                    template_hasxy(tmpl))
150
                {
151
152
                    //post("Hmm, found a scalar from struct %s... ",
                    //    templatesym->s_name);
153
154
155
156
157
158
159
160
                    t_binbuf *scalarbuf = binbuf_new();
                    t_atom coords_at[2];
                    SETFLOAT(coords_at, (t_float)xpix);
                    SETFLOAT(coords_at+1, (t_float)ypix);
                    binbuf_add(scalarbuf, 1, scalar_at);
                    binbuf_add(scalarbuf, 2, coords_at);
                    binbuf_add(scalarbuf, binbuf_getnatom(b)-1, scalar_at+1);
                    t_atom *scalar_create_at = binbuf_getvec(scalarbuf);
161
162
                    glist_scalar(gl, gensym("scalar_from_canvas_objtext"),
                        binbuf_getnatom(b)+2, scalar_create_at);
163
                    binbuf_free(scalarbuf);
164
                    //binbuf_free(b);
165
                    canvas_unsetcurrent((t_canvas *)gl);
166
                    scalar_in_a_box = 1;
167
168
169
                    return;
                }
            }
Miller Puckette's avatar
Miller Puckette committed
170
171
172
173
174
175
176
177
178
179
180
            x = 0;
        }
        else if (!(x = pd_checkobject(newest)))
        {
            binbuf_print(b);
            post("... didn't return a patchable object");
        }
    }
    else x = 0;
    if (!x)
    {
181
182
        /* LATER make the color reflect this */
        x = (t_text *)pd_new(text_class);
183
184
185
186
187
188
189
190
191
192
        if (binbuf_getnatom(b))
        {
            int bufsize;
            char *buf;
            binbuf_gettext(b, &buf, &bufsize);
            buf = t_resizebytes(buf, bufsize, bufsize+1);
            buf[bufsize] = 0;
            pd_error(x, "couldn't create \"%s\"", buf);
            t_freebytes(buf, bufsize + 1);
        }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    }
    /* 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 */
217

Miller Puckette's avatar
Miller Puckette committed
218
    x->te_binbuf = b;
219

Miller Puckette's avatar
Miller Puckette committed
220
221
    x->te_xpix = xpix;
    x->te_ypix = ypix;
222
    x->te_width = width;
Miller Puckette's avatar
Miller Puckette committed
223
    x->te_type = T_OBJECT;
224
225
226
227
    /* let's see if iemgui objects did not already set the value to 1,
       otherwise set it explicitly to 0 */
    if (x->te_iemgui != 1)
        x->te_iemgui = 0;
Miller Puckette's avatar
Miller Puckette committed
228
    glist_add(gl, &x->te_g);
229

Miller Puckette's avatar
Miller Puckette committed
230
231
    if (selected)
    {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
232
233
234
            /* this is called if we've been created from the menu. we use
               connectme to be able to tell the GUI the difference between
               a newly created floating object and an autopatched one. */
Miller Puckette's avatar
Miller Puckette committed
235
        glist_select(gl, &x->te_g);
236
        gobj_activate(&x->te_g, gl,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
237
            connectme ? 1 : 2); // <-- hack to set floating mode for new obj box
Miller Puckette's avatar
Miller Puckette committed
238
239
240
241
242
243
    }
    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);
244
245
246
247

    glob_preset_node_list_seek_hub();
    glob_preset_node_list_check_loc_and_update();

248
249
250
251
252
253
254
255
256
257
258
259
260
    // 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)));
    }*/
    if ( glist_isvisible( ((t_canvas *)gl) ) )
    {
261
        canvas_getscroll(glist_getcanvas(gl));
262
    }
Miller Puckette's avatar
Miller Puckette committed
263
264
}

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

extern int sys_autopatch_yoffset;
static int get_autopatch_yoffset(t_canvas *x)
{
    if (sys_autopatch_yoffset)
        return sys_autopatch_yoffset;
    else
    {
        int fontsize = glist_getfont(x);
        switch (fontsize)
        {
        case 8: return 8;
        case 10: return 8;
        case 12: return 9;
        case 16: return 10;
        case 24: return 13;
        case 36: return 18;
        }
        return 10;
    }
}

287
extern int sys_noautopatch;
288
289
extern t_gobj *glist_nth(t_glist *x, int n);
extern int glist_getindex(t_glist *x, t_gobj *y);
Miller Puckette's avatar
Miller Puckette committed
290
291
    /* utility routine to figure out where to put a new text box from menu
    and whether to connect to it automatically */
292
void canvas_howputnew(t_canvas *x, int *connectp, int *xpixp, int *ypixp,
Miller Puckette's avatar
Miller Puckette committed
293
294
    int *indexp, int *totalp)
{
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
295
    int indx = 0, nobj = 0, n2, x1, x2, y1, y2;
296

297
298
299
    /*
    int connectme = 0;
    if (x->gl_editor->e_selection &&
300
        !x->gl_editor->e_selection->sel_next &&
301
302
303
304
305
306
        !sys_noautopatch)
    {
        selected = x->gl_editor->e_selection->sel_what;
        t_object *ob = pd_checkobject(&selected->g_pd);
        connectme = (obj_noutlets(ob) ? 1 : 0);
    }*/
307
    int connectme = (x->gl_editor->e_selection &&
308
        !x->gl_editor->e_selection->sel_next);
Miller Puckette's avatar
Miller Puckette committed
309
310
    if (connectme)
    {
Miller Puckette's avatar
Miller Puckette committed
311
        t_gobj *g, *selected = x->gl_editor->e_selection->sel_what;
312
        t_text *t = (t_text *)selected;
313
314
        // if selected object has not yet been activated we need to
        // recreate it first
315
        if (pd_class(&t->te_pd) == text_class && t->te_type != T_TEXT)
Miller Puckette's avatar
Miller Puckette committed
316
        {
317
318
319
320
            // we do this to explicitly activate object...
            glist_noselect(x);
            // then reselect it
            glist_select(x, glist_nth(x, glist_getindex(x, 0)-1));
321
            selected = x->gl_editor->e_selection->sel_what;
Miller Puckette's avatar
Miller Puckette committed
322
        }
323
        for (g = x->gl_list, nobj = 0; g; g = g->g_next, nobj++)
324
        {
325
326
327
328
329
            if (g == selected)
            {
                gobj_getrect(g, x, &x1, &y1, &x2, &y2);
                indx = nobj;
                *xpixp = x1;
330
                *ypixp = y2 + get_autopatch_yoffset(x);
331
            }
332
        }
Miller Puckette's avatar
Miller Puckette committed
333
334
335
336
337
338
339
340
341
342
343
344
345
        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;
        }
346
        x->gl_editor->e_onmotion = MA_NONE;
347
        canvas_getscroll(x);
Miller Puckette's avatar
Miller Puckette committed
348
349
350
351
352
    }
    else
    {
        glist_getnextxy(x, xpixp, ypixp);
        glist_noselect(x);
Miller Puckette's avatar
Miller Puckette committed
353
    }
354
355
356
357
358
    if (sys_noautopatch)
    {
        if (connectme == 1) connectme = -1;
        else connectme = 0;
    }
Miller Puckette's avatar
Miller Puckette committed
359
360
361
362
363
    *connectp = connectme;
    *indexp = indx;
    *totalp = nobj;
}

Miller Puckette's avatar
Miller Puckette committed
364
    /* object creation routine.  These are called without any arguments if
Miller Puckette's avatar
Miller Puckette committed
365
366
    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
367

368
369
EXTERN int connect_exception;

Miller Puckette's avatar
Miller Puckette committed
370
371
void canvas_obj(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
372
    //fprintf(stderr,"canvas_obj\n");
373
    if (canvas_hasarray(gl)) return;
Miller Puckette's avatar
Miller Puckette committed
374
375
376
377
378
    if (argc >= 2)
    {
        t_binbuf *b = binbuf_new();
        binbuf_restore(b, argc-2, argv+2);
        canvas_objtext(gl, atom_getintarg(0, argc, argv),
Jonathan Wilkes's avatar
Jonathan Wilkes committed
379
            atom_getintarg(1, argc, argv), 0, 0, b, 0);
Miller Puckette's avatar
Miller Puckette committed
380
    }
Miller Puckette's avatar
Miller Puckette committed
381
382
383
384
385
        /* 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
    {
386
            /* interactively create new object */
Miller Puckette's avatar
Miller Puckette committed
387
        t_binbuf *b = binbuf_new();
Miller Puckette's avatar
Miller Puckette committed
388
389
        int connectme, xpix, ypix, indx, nobj;
        canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
Miller Puckette's avatar
Miller Puckette committed
390
        pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
391
392
        canvas_objtext(gl,
            connectme ? xpix : xpix - 8,
393
            connectme ? ypix : ypix - 8,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
394
            0, 1, b, connectme);
395
396
397
398
        if (connectme == 1)
        {
            //fprintf(stderr,"canvas_obj calls canvas_connect\n");
            connect_exception = 1;
Miller Puckette's avatar
Miller Puckette committed
399
            canvas_connect(gl, indx, 0, nobj, 0);
400
401
402
403
404
            connect_exception = 0;
        }
        else if (connectme == 0)
        {
            //fprintf(stderr,"canvas_obj calls canvas_startmotion\n");
405
            //canvas_displaceselection(glist_getcanvas(gl), -8, -8);
406
407
408
409
410
411
412
            canvas_startmotion(glist_getcanvas(gl));
        }
        //canvas_setundo(glist_getcanvas(gl),
        //    canvas_undo_create, canvas_undo_set_create(gl), "create");
        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
413
414
415
    }
}

416
417
418
extern void glist_setlastxy(t_glist *gl, int xval, int yval);

/* invoked from tcl/tk: abstraction_name x_offset y_offset */
419
420
void canvas_obj_abstraction_from_menu(t_glist *gl, t_symbol *s,
    int argc, t_atom *argv)
421
{
422
    if (canvas_hasarray(gl)) return;
423
    //fprintf(stderr,"canvas_abstraction_from_menu\n");
424
    //t_text *x;
425
    t_gobj *y;
426
427

    t_binbuf *b = binbuf_new();
428
    binbuf_restore(b, 2, argv);
429
430
431
    int connectme, xpix, ypix, indx, nobj;
    canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
    pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
432
#ifdef PDL2ORK
433
434
    if (sys_k12_mode)
        pd_vmess (&gl->gl_pd, gensym("tooltips"), "i", 1);
435
#endif
436
    canvas_objtext(gl, xpix+atom_getintarg(1, argc, argv),
Jonathan Wilkes's avatar
Jonathan Wilkes committed
437
        ypix+atom_getintarg(2, argc, argv), 0, 1, b, 0);
438
439
440
441
442
443
444
445
446

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

448
449
    if (connectme == 1)
    {
450
        canvas_connect(gl, indx, 0, nobj, 0);
451
452
453
    }
    else if (connectme == 0)
    {
454
        //glist_setlastxy(glist_getcanvas(gl), xpix, ypix);
455
456
457
458
        canvas_startmotion(glist_getcanvas(gl));
    }
    canvas_undo_add(glist_getcanvas(gl), 9, "create",
        (void *)canvas_undo_set_create(glist_getcanvas(gl)));
459
460
}

Miller Puckette's avatar
Miller Puckette committed
461
462
463
464
465
/* make an object box for an object that's already there. */

/* iemlib */
void canvas_iemguis(t_glist *gl, t_symbol *guiobjname)
{
466
    if (canvas_hasarray(gl)) return;
467
    //fprintf(stderr,"canvas_iemguis\n");
Miller Puckette's avatar
Miller Puckette committed
468
469
    t_atom at;
    t_binbuf *b = binbuf_new();
470
471
    //int xpix, ypix;

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

475
    int connectme, xpix, ypix, indx, nobj;
476

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

479
480
481
482
    /* NOT NECESSARY ANY MORE: compensate for the iemgui sliders' xyoffset
       in case of autopatch
    if (connectme)
    {
483
        if (!strcmp(guiobjname->s_name, "hsl"))
484
            xpix = xpix + 3;
485
        else if (!strcmp(guiobjname->s_name, "vsl"))
486
            ypix = ypix + 2;
487
        else if (!strcmp(guiobjname->s_name, "vu"))
488
489
490
491
492
493
        {
            xpix = xpix + 1;
            ypix = ypix + 2;
        }
    }*/
    
Miller Puckette's avatar
Miller Puckette committed
494
495
496
497
    pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
    glist_noselect(gl);
    SETSYMBOL(&at, guiobjname);
    binbuf_restore(b, 1, &at);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
498
    canvas_objtext(gl, xpix, ypix, 0, 1, b, 0);
499
    if (connectme == 1)
500
501
        canvas_connect(gl, indx, 0, nobj, 0);
    //glist_getnextxy(gl, &xpix, &ypix);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
502
    //canvas_objtext(gl, xpix, ypix, 1, b, 0);
503
504
    else if (connectme == 0)
    {
505
        canvas_displaceselection(glist_getcanvas(gl), -8, -8);
506
507
        canvas_startmotion(glist_getcanvas(gl));
    }
508
509
510
511
    //canvas_setundo(glist_getcanvas(gl),
    //    canvas_undo_create, canvas_undo_set_create(gl), "create");
    canvas_undo_add(glist_getcanvas(gl), 9, "create",
        (void *)canvas_undo_set_create(glist_getcanvas(gl)));
Miller Puckette's avatar
Miller Puckette committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
}

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;
587
    t_glist *mr_glist;
Miller Puckette's avatar
Miller Puckette committed
588
589
590
591
592
593
594
595
596
597
598
    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;

599
t_class *messresponder_class;
Miller Puckette's avatar
Miller Puckette committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615

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

616
617
618
619
620
/* MP 20070107 blob type */
//static void messresponder_blob(t_messresponder *x, t_blob *st)
//{
//    outlet_blob(x->mr_outlet, st);
//}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
621

Miller Puckette's avatar
Miller Puckette committed
622
623
624
625
626
627
628
629
630
631
632
633
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);
}

634
635
636
637
638
639
/* get the glist cached in a message responder */
t_glist *messresponder_getglist(t_pd *x)
{
    return ((t_messresponder *)x)->mr_glist;
}

Miller Puckette's avatar
Miller Puckette committed
640
641
static void message_bang(t_message *x)
{
642
643
    /*  we do canvas_setcurrent/unsetcurrent to substitute canvas
        instance number for $0 */
Miller Puckette's avatar
Miller Puckette committed
644
645
646
647
648
649
650
    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);
651
652
    /*  we do canvas_setcurrent/unsetcurrent to substitute canvas
        instance number for $0 */
Miller Puckette's avatar
Miller Puckette committed
653
654
655
656
657
658
659
    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);
660
661
    /*  we do canvas_setcurrent/unsetcurrent to substitute canvas
        instance number for $0 */
Miller Puckette's avatar
Miller Puckette committed
662
663
664
    binbuf_eval(x->m_text.te_binbuf, &x->m_messresponder.mr_pd, 1, &at);
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
665
666
667
668
static void message_blob(t_message *x, t_blob *st)
{
    t_atom at;
    SETBLOB(&at, st);
669
670
    /*  we do canvas_setcurrent/unsetcurrent to substitute canvas
        instance number for $0 */
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
671
672
673
    binbuf_eval(x->m_text.te_binbuf, &x->m_messresponder.mr_pd, 1, &at);
}

Miller Puckette's avatar
Miller Puckette committed
674
675
static void message_list(t_message *x, t_symbol *s, int argc, t_atom *argv)
{
676
677
678
    // TODO: here and elsewhere in the message, do we want $0 to be parsed
    // into canvas instance? Makes sense since there is no such argument,
    // but will this break anything?
679
680
    /*  we do canvas_setcurrent/unsetcurrent to substitute canvas
        instance number for $0 */
Miller Puckette's avatar
Miller Puckette committed
681
682
683
684
685
686
687
688
    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);
689
    if (glist_isvisible(glist_getcanvas(x->m_glist)))
690
        canvas_getscroll(x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
691
692
693
694
695
696
}

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);
697
    if (glist_isvisible(glist_getcanvas(x->m_glist)))
698
        canvas_getscroll(x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
699
700
701
702
703
704
705
}

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);
706
    if (glist_isvisible(glist_getcanvas(x->m_glist)))
707
        canvas_getscroll(x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
708
709
710
711
712
713
714
715
}

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);
716
    if (glist_isvisible(glist_getcanvas(x->m_glist)))
717
        canvas_getscroll(x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
718
719
720
721
722
}

static void message_addsemi(t_message *x)
{
    message_add(x, 0, 0, 0);
723
    if (glist_isvisible(glist_getcanvas(x->m_glist)))
724
        canvas_getscroll(x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
725
726
727
728
729
730
731
732
733
734
735
}

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);
736
    if (glist_isvisible(glist_getcanvas(x->m_glist)))
737
        canvas_getscroll(x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
738
739
740
741
742
743
744
745
746
747
748
749
}

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);
750
    if (glist_isvisible(glist_getcanvas(x->m_glist)))
751
        canvas_getscroll(x->m_glist);
Miller Puckette's avatar
Miller Puckette committed
752
753
754
755
756
757
758
759
760
761
}

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);
762
        gui_vmess("gui_message_flash", "xsi",
763
            glist_getcanvas(x->m_glist), rtext_gettag(y), 1);
Miller Puckette's avatar
Miller Puckette committed
764
765
766
767
768
769
770
771
772
        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);
773
        gui_vmess("gui_message_flash", "xsi",
774
            glist_getcanvas(x->m_glist), rtext_gettag(y), 0);
Miller Puckette's avatar
Miller Puckette committed
775
776
777
778
779
780
781
782
    }
}

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

783
784
785
786
787
788
789
790
791
792
793
794
795
t_pd *pd_mess_from_responder(t_pd *x)
{
    if (pd_class(x) == messresponder_class)
    {
        /* do pointer math to try to get to the container message struct */
        void *tmp = (void *)x - sizeof(t_text);
        /* if it looks like a message, it must be a message */
        if(((t_text *)tmp)->te_pd == message_class)
            return ((t_pd *)tmp);
    }
    return x;
}

Miller Puckette's avatar
Miller Puckette committed
796
797
void canvas_msg(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
{
798
    if (canvas_hasarray(gl)) return;
799
800
801
802
803
804
805
806
807
808
809
    /*fprintf(stderr,"canvas_msg\n");
    int i = 0;
    while(i < argc)
    {
        if (argv[i].a_type == A_FLOAT)
            fprintf(stderr," %f", atom_getfloatarg(i, argc, argv));
        else
            fprintf(stderr," %s", atom_getsymbolarg(i, argc, argv)->s_name);
        i++;
    }
    fprintf(stderr,"\n");*/
Miller Puckette's avatar
Miller Puckette committed
810
811
812
    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);
813
    x->m_messresponder.mr_glist = gl;
Miller Puckette's avatar
Miller Puckette committed
814
815
    x->m_text.te_width = 0;                             /* don't know it yet. */
    x->m_text.te_type = T_MESSAGE;
816
    x->m_text.te_iemgui = 0;
Miller Puckette's avatar
Miller Puckette committed
817
818
819
820
821
822
823
824
825
826
    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
827
828
    else if (!glist_isvisible(gl))
        post("unable to create stub message in closed canvas!");
Miller Puckette's avatar
Miller Puckette committed
829
830
    else
    {
Miller Puckette's avatar
Miller Puckette committed
831
832
833
        int connectme, xpix, ypix, indx, nobj;
        canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
        
Miller Puckette's avatar
Miller Puckette committed
834
        pd_vmess(&gl->gl_pd, gensym("editmode"), "i", 1);
Miller Puckette's avatar
Miller Puckette committed
835
836
        x->m_text.te_xpix = xpix;
        x->m_text.te_ypix = ypix;
Miller Puckette's avatar
Miller Puckette committed
837
838
839
        glist_add(gl, &x->m_text.te_g);
        glist_noselect(gl);
        glist_select(gl, &x->m_text.te_g);
840
        gobj_activate(&x->m_text.te_g, gl,
841
            connectme ? 1 : 2); // <-- hack to signal we're a new message box
842
        if (connectme == 1)
Miller Puckette's avatar
Miller Puckette committed
843
            canvas_connect(gl, indx, 0, nobj, 0);
844
845
        else if (connectme == 0)
        {
846
            canvas_displaceselection(glist_getcanvas(gl), -8, -8);
847
848
            canvas_startmotion(glist_getcanvas(gl));
        }
849
850
851
852
        //canvas_setundo(glist_getcanvas(gl),
        //    canvas_undo_create, canvas_undo_set_create(gl), "create");
        canvas_undo_add(glist_getcanvas(gl), 9, "create",
            (void *)canvas_undo_set_create(glist_getcanvas(gl)));
Miller Puckette's avatar
Miller Puckette committed
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
    }
}

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

914
915
    /* recolor option offers     0 ignore recolor
                                1 recolor */
916
917
static void gatom_retext(t_gatom *x, int senditup, int recolor)
{
918
919
920
    t_canvas *canvas = glist_getcanvas(x->a_glist);
    t_rtext *y = glist_findrtext(x->a_glist, &x->a_text);
    if (recolor)
user's avatar
user committed
921
    {
922
        gui_vmess("gui_gatom_activate", "xsi",
923
            canvas, rtext_gettag(y), 0);
user's avatar
user committed
924
    }
Miller Puckette's avatar
Miller Puckette committed
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
    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)
943
944
945
946
947
948
    {
        if (x->a_atom.a_type == A_FLOAT && x->a_text.te_width == 1)
            gatom_retext(x, 1, 1);
        else
            gatom_retext(x, 1, 0);
    }
Miller Puckette's avatar
Miller Puckette committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
    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)
{
1050
    //fprintf(stderr,"gatom_key %f\n", f);
Miller Puckette's avatar
Miller Puckette committed
1051
1052
1053
1054
1055
1056
1057
1058
1059
    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])
1060
            gatom_retext(x, 1, 1);
1061
1062
        else
            gatom_retext(x, 0, 1);
Miller Puckette's avatar
Miller Puckette committed
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
        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);
1079
        gatom_retext(x, 1, 0);
Miller Puckette's avatar
Miller Puckette committed
1080
        x->a_buf[0] = 0;
1081
1082
1083
        /* We want to keep grabbing the keyboard after hitting "Enter", so
           we're commenting the following out */
        //glist_grab(x->a_glist, 0, 0, 0, 0, 0);
Miller Puckette's avatar
Miller Puckette committed
1084
1085
1086
1087
1088
1089
1090
1091
    }
    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'))
        {
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
            /* 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
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
            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);
            }
            else gatom_float(x, x->a_toggle);
1141
1142
            gatom_retext(x, 0, 1);
            return;
Miller Puckette's avatar
Miller Puckette committed
1143
1144
1145
1146
1147
1148
1149
1150
        }
        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);
    }
}

1151
1152
1153
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
1154
1155
1156
    /* message back from dialog window */
static void gatom_param(t_gatom *x, t_symbol *sel, int argc, t_atom *argv)
{
1157
1158
1159
1160
    /* Check if we need to set an undo point. This happens if the user
       clicks the "Ok" button, but not when clicking "Apply" or "Cancel" */
    if (atom_getintarg(7, argc, argv))
        canvas_apply_setundo(x->a_glist, (t_gobj *)x);
1161

Miller Puckette's avatar
Miller Puckette committed
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
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
    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);
1208
    gobj_select(&x->a_text.te_g, x->a_glist, 1);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1209
    canvas_dirty(x->a_glist, 1);
1210
    canvas_getscroll(x->a_glist);
Miller Puckette's avatar
Miller Puckette committed
1211
1212
1213
1214
1215
    /* glist_retext(x->a_glist, &x->a_text); */
}

    /* ---------------- gatom-specific widget functions --------------- */

user's avatar
user committed
1216
1217
1218
1219
1220
1221
static void gatom_getwherelabel(t_gatom *x, t_glist *glist, int *xp, int *yp)
{
    int x1, y1, x2, y2;
    text_getrect(&x->a_text.te_g, glist, &x1, &y1, &x2, &y2);
    if (x->a_wherelabel == ATOM_LABELLEFT)
    {
user's avatar
user committed
1222
        *xp = -3 -
user's avatar
user committed
1223
1224
            strlen(canvas_realizedollar(x->a_glist, x->a_label)->s_name) *
            sys_fontwidth(glist_getfont(glist));
user's avatar
user committed
1225
        *yp = y2 - y1 - 4;
user's avatar
user committed
1226
1227
1228
    }
    else if (x->a_wherelabel == ATOM_LABELRIGHT)
    {
user's avatar
user committed
1229
1230
        *xp = x2 - x1 + 3;
        *yp = y2 - y1 - 4;
user's avatar
user committed
1231
1232
1233
1234
    }
    else if (x->a_wherelabel == ATOM_LABELUP)
    {
        *xp = -1;
user's avatar
user committed
1235
        *yp = -3;
user's avatar
user committed
1236
1237
1238
1239
    }
    else
    {
        *xp = -1;
user's avatar
user committed
1240
        *yp = y2 - y1 + sys_fontheight(glist_getfont(glist));
user's avatar
user committed
1241
1242
1243
    }
}

Miller Puckette's avatar
Miller Puckette committed
1244
1245
1246
static void gatom_displace(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
1247
    text_displace(z, glist, dx, dy);
Miller Puckette's avatar
Miller Puckette committed
1248
1249
}

user's avatar
user committed
1250
/* for gatom's label */
Miller Puckette's avatar
Miller Puckette committed
1251
1252
static void gatom_vis(t_gobj *z, t_glist *glist, int vis)
{
1253
    //fprintf(stderr,"gatom_vis\n");
Miller Puckette's avatar
Miller Puckette committed
1254
1255
1256
1257
1258
1259
1260
    t_gatom *x = (t_gatom*)z;
    text_vis(z, glist, vis);
    if (*x->a_label->s_name)
    {
        if (vis)
        {
            int x1, y1;
user's avatar
user committed
1261
            t_rtext *y = glist_findrtext(x->a_glist, &x->a_text);
Miller Puckette's avatar
Miller Puckette committed
1262
            gatom_getwherelabel(x, glist, &x1, &y1);
1263
1264
            gui_vmess("gui_text_new", "xssiiisi",
                glist_getcanvas(glist),
user's avatar
user committed
1265
1266
1267
1268
1269
                rtext_gettag(y),
                "gatom",
                0,
                x1, // left margin
                y1, // top margin
Miller Puckette's avatar
Miller Puckette committed
1270
                canvas_realizedollar(x->a_glist, x->a_label)->s_name,
user's avatar
user committed
1271
1272
                sys_hostfontsize(glist_getfont(glist))
            );
Miller Puckette's avatar
Miller Puckette committed
1273
        }
1274
1275
1276
1277
1278
1279
1280
        else
        {
            /* We're just deleting the parent gobj in the GUI, which takes
               care of removing all the children. So we don't need to send
               a message here */
            //sys_vgui(".x%lx.c delete %lx.l\n", glist_getcanvas(glist), x);
        }
Miller Puckette's avatar
Miller Puckette committed
1281
1282
1283
1284
1285
1286
1287
1288
    }
    if (!vis)
        sys_unqueuegui(x);
}

void canvas_atom(t_glist *gl, t_atomtype type,
    t_symbol *s, int argc, t_atom *argv)
{
1289
    if (canvas_hasarray(gl)) return;
1290
    //fprintf(stderr,"canvas_atom\n");
Miller Puckette's avatar
Miller Puckette committed
1291
1292
1293
1294
    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;
1295
    x->a_text.te_iemgui = 0;
Miller Puckette's avatar
Miller Puckette committed
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
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
1345
1346
1347
1348
1349
    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
1350
1351
        int connectme, xpix, ypix, indx, nobj;
        canvas_howputnew(gl, &connectme, &xpix, &ypix, &indx, &nobj);
Miller Puckette's avatar
Miller Puckette committed
1352
1353
1354
1355
1356
1357
1358
1359
1360
        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);
1361
        if (connectme == 1)