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

/* g_7_guis.c written by Thomas Musil (c) IEM KUG Graz Austria 2000-2001 */
/* thanks to Miller Puckette, Guenther Geiger and Krzystof Czaja */

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
8
#include "config.h"
Miller Puckette's avatar
Miller Puckette committed
9
10
11
12
13
14
15

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "m_pd.h"
#include "g_canvas.h"
16
#include "m_imp.h"
Miller Puckette's avatar
Miller Puckette committed
17
18
19
20
#include "t_tk.h"
#include "g_all_guis.h"
#include <math.h>

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
21
#ifdef HAVE_UNISTD_H
Miller Puckette's avatar
Miller Puckette committed
22
23
24
#include <unistd.h>
#endif

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
25
26
27
28
#ifdef HAVE_IO_H
#include <io.h>
#endif

29
30
t_symbol *s_empty;

Miller Puckette's avatar
Miller Puckette committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*------------------ global varaibles -------------------------*/

int iemgui_color_hex[]=
{
    16579836, 10526880, 4210752, 16572640, 16572608,
    16579784, 14220504, 14220540, 14476540, 16308476,
    14737632, 8158332, 2105376, 16525352, 16559172,
    15263784, 1370132, 2684148, 3952892, 16003312,
    12369084, 6316128, 0, 9177096, 5779456,
    7874580, 2641940, 17488, 5256, 5767248
};

/*------------------ global functions -------------------------*/


int iemgui_clip_size(int size)
{
    if(size < IEM_GUI_MINSIZE)
        size = IEM_GUI_MINSIZE;
    return(size);
}

int iemgui_clip_font(int size)
{
    if(size < IEM_FONT_MINSIZE)
        size = IEM_FONT_MINSIZE;
    return(size);
}

60
static int iemgui_modulo_color(int col)
Miller Puckette's avatar
Miller Puckette committed
61
{
62
63
64
    col %= IEM_GUI_MAX_COLOR;
    if (col<0) col += IEM_GUI_MAX_COLOR;
    return col;
Miller Puckette's avatar
Miller Puckette committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
}

t_symbol *iemgui_dollar2raute(t_symbol *s)
{
    char buf[MAXPDSTRING+1], *s1, *s2;
    if (strlen(s->s_name) >= MAXPDSTRING)
        return (s);
    for (s1 = s->s_name, s2 = buf; ; s1++, s2++)
    {
        if (*s1 == '$')
            *s2 = '#';
        else if (!(*s2 = *s1))
            break;
    }
    return(gensym(buf));
}

t_symbol *iemgui_raute2dollar(t_symbol *s)
{
    char buf[MAXPDSTRING+1], *s1, *s2;
    if (strlen(s->s_name) >= MAXPDSTRING)
        return (s);
    for (s1 = s->s_name, s2 = buf; ; s1++, s2++)
    {
        if (*s1 == '#')
            *s2 = '$';
        else if (!(*s2 = *s1))
            break;
    }
    return(gensym(buf));
}

void iemgui_verify_snd_ne_rcv(t_iemgui *iemgui)
{
99
100
    iemgui->x_put_in2out = 1;
    if(iemgui_has_snd(iemgui) && iemgui_has_rcv(iemgui))
Miller Puckette's avatar
Miller Puckette committed
101
    {
102
103
        if(iemgui->x_snd==iemgui->x_rcv)
            iemgui->x_put_in2out = 0;
Miller Puckette's avatar
Miller Puckette committed
104
105
106
107
108
109
110
111
112
113
114
115
116
    }
}

t_symbol *iemgui_new_dogetname(t_iemgui *iemgui, int indx, t_atom *argv)
{
    if (IS_A_SYMBOL(argv, indx))
        return (atom_getsymbolarg(indx, 100000, argv));
    else if (IS_A_FLOAT(argv, indx))
    {
        char str[80];
        sprintf(str, "%d", (int)atom_getintarg(indx, 100000, argv));
        return (gensym(str));
    }
117
    else return s_empty;
Miller Puckette's avatar
Miller Puckette committed
118
119
120
121
122
123
124
125
126
127
}

void iemgui_new_getnames(t_iemgui *iemgui, int indx, t_atom *argv)
{
    if (argv)
    {
        iemgui->x_snd = iemgui_new_dogetname(iemgui, indx, argv);
        iemgui->x_rcv = iemgui_new_dogetname(iemgui, indx+1, argv);
        iemgui->x_lab = iemgui_new_dogetname(iemgui, indx+2, argv);
    }
128
    else iemgui->x_snd = iemgui->x_rcv = iemgui->x_lab = s_empty;
Miller Puckette's avatar
Miller Puckette committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    iemgui->x_snd_unexpanded = iemgui->x_rcv_unexpanded =
        iemgui->x_lab_unexpanded = 0;
    iemgui->x_binbufindex = indx;
    iemgui->x_labelbindex = indx + 3;
}

    /* convert symbols in "$" form to the expanded symbols */
void iemgui_all_dollararg2sym(t_iemgui *iemgui, t_symbol **srlsym)
{
        /* save unexpanded ones for later */
    iemgui->x_snd_unexpanded = srlsym[0];
    iemgui->x_rcv_unexpanded = srlsym[1];
    iemgui->x_lab_unexpanded = srlsym[2];
    srlsym[0] = canvas_realizedollar(iemgui->x_glist, srlsym[0]);
    srlsym[1] = canvas_realizedollar(iemgui->x_glist, srlsym[1]);
    srlsym[2] = canvas_realizedollar(iemgui->x_glist, srlsym[2]);
}

    /* initialize a single symbol in unexpanded form.  We reach into the
    binbuf to grab them; if there's nothing there, set it to the
    fallback; if still nothing, set to "empty". */
static void iemgui_init_sym2dollararg(t_iemgui *iemgui, t_symbol **symp,
    int indx, t_symbol *fallback)
{
    if (!*symp)
    {
        t_binbuf *b = iemgui->x_obj.ob_binbuf;
        if (binbuf_getnatom(b) > indx)
        {
            char buf[80];
            atom_string(binbuf_getvec(b) + indx, buf, 80);
            *symp = gensym(buf);
        }
        else if (fallback)
            *symp = fallback;
164
        else *symp = s_empty;
Miller Puckette's avatar
Miller Puckette committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    }
}

    /* get the unexpanded versions of the symbols; initialize them if
    necessary. */
void iemgui_all_sym2dollararg(t_iemgui *iemgui, t_symbol **srlsym)
{
    iemgui_init_sym2dollararg(iemgui, &iemgui->x_snd_unexpanded,
        iemgui->x_binbufindex+1, iemgui->x_snd);
    iemgui_init_sym2dollararg(iemgui, &iemgui->x_rcv_unexpanded,
        iemgui->x_binbufindex+2, iemgui->x_rcv);
    iemgui_init_sym2dollararg(iemgui, &iemgui->x_lab_unexpanded,
        iemgui->x_labelbindex, iemgui->x_lab);
    srlsym[0] = iemgui->x_snd_unexpanded;
    srlsym[1] = iemgui->x_rcv_unexpanded;
    srlsym[2] = iemgui->x_lab_unexpanded;
}

void iemgui_all_col2save(t_iemgui *iemgui, int *bflcol)
{
    bflcol[0] = -1 - (((0xfc0000 & iemgui->x_bcol) >> 6)|
                      ((0xfc00 & iemgui->x_bcol) >> 4)|((0xfc & iemgui->x_bcol) >> 2));
    bflcol[1] = -1 - (((0xfc0000 & iemgui->x_fcol) >> 6)|
                      ((0xfc00 & iemgui->x_fcol) >> 4)|((0xfc & iemgui->x_fcol) >> 2));
    bflcol[2] = -1 - (((0xfc0000 & iemgui->x_lcol) >> 6)|
                      ((0xfc00 & iemgui->x_lcol) >> 4)|((0xfc & iemgui->x_lcol) >> 2));
}

void iemgui_all_colfromload(t_iemgui *iemgui, int *bflcol)
{
    if(bflcol[0] < 0)
    {
        bflcol[0] = -1 - bflcol[0];
        iemgui->x_bcol = ((bflcol[0] & 0x3f000) << 6)|((bflcol[0] & 0xfc0) << 4)|
            ((bflcol[0] & 0x3f) << 2);
    }
    else
    {
        bflcol[0] = iemgui_modulo_color(bflcol[0]);
        iemgui->x_bcol = iemgui_color_hex[bflcol[0]];
    }
    if(bflcol[1] < 0)
    {
        bflcol[1] = -1 - bflcol[1];
        iemgui->x_fcol = ((bflcol[1] & 0x3f000) << 6)|((bflcol[1] & 0xfc0) << 4)|
            ((bflcol[1] & 0x3f) << 2);
    }
    else
    {
        bflcol[1] = iemgui_modulo_color(bflcol[1]);
        iemgui->x_fcol = iemgui_color_hex[bflcol[1]];
    }
    if(bflcol[2] < 0)
    {
        bflcol[2] = -1 - bflcol[2];
        iemgui->x_lcol = ((bflcol[2] & 0x3f000) << 6)|((bflcol[2] & 0xfc0) << 4)|
            ((bflcol[2] & 0x3f) << 2);
    }
    else
    {
        bflcol[2] = iemgui_modulo_color(bflcol[2]);
        iemgui->x_lcol = iemgui_color_hex[bflcol[2]];
    }
}

230
static int iemgui_compatible_col(int i)
Miller Puckette's avatar
Miller Puckette committed
231
232
{
    if(i >= 0)
233
234
        return(iemgui_color_hex[(iemgui_modulo_color(i))]);
    return((-1-i)&0xffffff);
Miller Puckette's avatar
Miller Puckette committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
}

void iemgui_all_dollar2raute(t_symbol **srlsym)
{
    srlsym[0] = iemgui_dollar2raute(srlsym[0]);
    srlsym[1] = iemgui_dollar2raute(srlsym[1]);
    srlsym[2] = iemgui_dollar2raute(srlsym[2]);
}

void iemgui_all_raute2dollar(t_symbol **srlsym)
{
    srlsym[0] = iemgui_raute2dollar(srlsym[0]);
    srlsym[1] = iemgui_raute2dollar(srlsym[1]);
    srlsym[2] = iemgui_raute2dollar(srlsym[2]);
}

251
void iemgui_send(t_iemgui *x, t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
252
253
{
    t_symbol *snd;
254
255
256
    int oldsndrcvable=0;
    if(iemgui_has_rcv(x)) oldsndrcvable += IEM_GUI_OLD_RCV_FLAG;
    if(iemgui_has_snd(x)) oldsndrcvable += IEM_GUI_OLD_SND_FLAG;
Miller Puckette's avatar
Miller Puckette committed
257
258

    snd = iemgui_raute2dollar(s);
259
260
261
262
    x->x_snd_unexpanded = snd;
    x->x_snd = snd = canvas_realizedollar(x->x_glist, snd);
    iemgui_verify_snd_ne_rcv(x);
    x->x_draw(x, x->x_glist, IEM_GUI_DRAW_MODE_IO + oldsndrcvable);
Miller Puckette's avatar
Miller Puckette committed
263
264
}

265
void iemgui_receive(t_iemgui *x, t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
266
267
{
    t_symbol *rcv;
268
269
270
    int oldsndrcvable=0;
    if(iemgui_has_rcv(x)) oldsndrcvable += IEM_GUI_OLD_RCV_FLAG;
    if(iemgui_has_snd(x)) oldsndrcvable += IEM_GUI_OLD_SND_FLAG;
Miller Puckette's avatar
Miller Puckette committed
271
272

    rcv = iemgui_raute2dollar(s);
273
274
275
    x->x_rcv_unexpanded = rcv;
    rcv = canvas_realizedollar(x->x_glist, rcv);
    if(iemgui_has_rcv(x))
Miller Puckette's avatar
Miller Puckette committed
276
    {
277
        if(rcv!=x->x_rcv)
Miller Puckette's avatar
Miller Puckette committed
278
        {
279
280
281
282
            if(iemgui_has_rcv(x))
                pd_unbind((t_pd *)x, x->x_rcv);
            x->x_rcv = rcv;
            pd_bind((t_pd *)x, x->x_rcv);
Miller Puckette's avatar
Miller Puckette committed
283
284
        }
    }
285
    else if(s!=s_empty && iemgui_has_rcv(x))
Miller Puckette's avatar
Miller Puckette committed
286
    {
287
288
        pd_unbind((t_pd *)x, x->x_rcv);
        x->x_rcv = rcv;
Miller Puckette's avatar
Miller Puckette committed
289
    }
290
291
    iemgui_verify_snd_ne_rcv(x);
    x->x_draw(x, x->x_glist, IEM_GUI_DRAW_MODE_IO + oldsndrcvable);
Miller Puckette's avatar
Miller Puckette committed
292
293
}

294
void iemgui_label(t_iemgui *x, t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
295
{
296
297
298
299
    if (s == &s_) s = s_empty; //tb: fix for empty label
    t_symbol *lab = iemgui_raute2dollar(s);
    x->x_lab_unexpanded = lab;
    x->x_lab = lab = canvas_realizedollar(x->x_glist, lab);
Miller Puckette's avatar
Miller Puckette committed
300

301
    if(glist_isvisible(x->x_glist))
302
    {
Miller Puckette's avatar
Miller Puckette committed
303
        sys_vgui(".x%lx.c itemconfigure %lxLABEL -text {%s} \n",
304
305
306
            glist_getcanvas(x->x_glist), x,
            s!=s_empty?x->x_lab->s_name:"");
        iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_CONFIG);
307
    }
Miller Puckette's avatar
Miller Puckette committed
308
309
}

310
void iemgui_label_pos(t_iemgui *x, t_symbol *s, int ac, t_atom *av)
Miller Puckette's avatar
Miller Puckette committed
311
{
312
313
314
    x->x_ldx = (int)atom_getintarg(0, ac, av);
    x->x_ldy = (int)atom_getintarg(1, ac, av);
    if(glist_isvisible(x->x_glist))
315
316
    {
        sys_vgui(".x%lx.c coords %lxLABEL %d %d\n",
317
318
319
320
            glist_getcanvas(x->x_glist), x,
            text_xpix((t_object *)x,x->x_glist)+x->x_ldx,
            text_ypix((t_object *)x,x->x_glist)+x->x_ldy);
        iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_CONFIG);
321
    }
Miller Puckette's avatar
Miller Puckette committed
322
323
}

324
void iemgui_label_font(t_iemgui *x, t_symbol *s, int ac, t_atom *av)
Miller Puckette's avatar
Miller Puckette committed
325
326
{
    int f = (int)atom_getintarg(0, ac, av);
327
328
    if (f<0 || f>2) f=0;
    x->x_font_style = f;
Miller Puckette's avatar
Miller Puckette committed
329
330
331
    f = (int)atom_getintarg(1, ac, av);
    if(f < 4)
        f = 4;
332
333
    x->x_fontsize = f;
    if(glist_isvisible(x->x_glist))
334
    {
Ivica Bukvic's avatar
Ivica Bukvic committed
335
        sys_vgui(".x%lx.c itemconfigure %lxLABEL -font {{%s} -%d %s}\n",
336
337
338
            glist_getcanvas(x->x_glist), x, iemgui_font(x), 
            x->x_fontsize, sys_fontweight);
            iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_CONFIG);
339
    }
340
341
342
343
344
345
}

//Sans: 84 x 10 (14) -> 6 x 10 -> 1.0
//Helvetica: 70 x 10 (14) -> 5 x 10 -> 0.83333
//Times: 61 x 10 (14) -> 4.357 x 10 -> 0.72619; 0.735 appears to work better

346
347
348
349
350
351
// We use this global var to check when getrect should report label:
// It should report it when drawing inside gop to see if we truly fit.
// Otherwise we should not report it while inside gop to avoid label being
// misinterpreted as part of the "hot" area of a widget (e.g. toggle)
extern int gop_redraw;

352
353
354
355
356
357
358
void iemgui_label_getrect(t_iemgui x_gui, t_glist *x,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
    //fprintf(stderr,"gop_redraw = %d\n", gop_redraw);
    if (!gop_redraw)
    {
        //fprintf(stderr,"ignoring label\n");
359
        return;
360
361
362
    }

    t_float width_multiplier;
363
    int label_length;
364
365
366
367
    int label_x1;
    int label_y1;
    int label_x2;
    int label_y2;
368
    int actual_fontsize; //seems tk does its own thing when rendering
369
370
371
372
373
374
    int actual_height;

    if (x->gl_isgraph && !glist_istoplevel(x))
    {
        //fprintf(stderr,"iemgui_label_getrect\n");

375
        if (x_gui.x_lab!=s_empty)
376
        {
377
            switch(x_gui.x_font_style)
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
            {
                case 1:
                    width_multiplier = 0.83333;
                    break;
                case 2:
                    width_multiplier = 0.735;
                    break;
                default:
                    width_multiplier = 1.0;
                    break;
            }
            if (x_gui.x_fontsize % 2 == 0)
            {
                actual_fontsize = x_gui.x_fontsize;
            }
            else
            {
                actual_fontsize = x_gui.x_fontsize;
            }
            actual_height = actual_fontsize;
398
            //exceptions
399
            if (x_gui.x_font_style == 0 &&
400
401
402
403
404
405
406
                (actual_fontsize == 8 || actual_fontsize == 13 ||
                actual_fontsize % 10 == 1 || actual_fontsize % 10 == 6 ||
                    (actual_fontsize > 48 && actual_fontsize < 100 &&
                    (actual_fontsize %10 == 4 || actual_fontsize %10 == 9))))
            {
                actual_fontsize += 1;
            }
407
            else if (x_gui.x_font_style == 1 && actual_fontsize >= 5 &&
408
409
                actual_fontsize < 13 && actual_fontsize % 2 == 1)
                actual_fontsize += 1;
410
            else if (x_gui.x_font_style == 2 && actual_fontsize >= 5 &&
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
                actual_fontsize % 2 == 1)
                actual_fontsize += 1;
            if (actual_height == 9)
                actual_height += 1;
            //done with exceptions

            width_multiplier = width_multiplier * (actual_fontsize * 0.6);

            label_length = strlen(x_gui.x_lab->s_name);
            label_x1 = *xp1 + x_gui.x_ldx;
            label_y1 = *yp1 + x_gui.x_ldy - actual_height/2;
            label_x2 = label_x1 + (label_length * width_multiplier);
            label_y2 = label_y1 + actual_height*1.1;

            //DEBUG
            //fprintf(stderr,"%f %d %d\n", width_multiplier,
427
            //    label_length, x_gui.x_font_style);
428
429
430
431
432
433
434
435
436
437
438
439
440
441
            //sys_vgui(".x%lx.c delete iemguiDEBUG\n", x);
            //sys_vgui(".x%lx.c create rectangle %d %d %d %d "
            //    "-tags iemguiDEBUG\n",
            //    x, label_x1, label_y1, label_x2, label_y2);
            if (label_x1 < *xp1) *xp1 = label_x1;
            if (label_x2 > *xp2) *xp2 = label_x2;
            if (label_y1 < *yp1) *yp1 = label_y1;
            if (label_y2 > *yp2) *yp2 = label_y2;
            //DEBUG
            //sys_vgui(".x%lx.c delete iemguiDEBUG\n", x);
            //sys_vgui(".x%lx.c create rectangle %d %d %d %d "
            //    "-tags iemguiDEBUG\n", x, *xp1, *yp1, *xp2, *yp2);
        }
    }
442
443
}

444
void iemgui_shouldvis(t_iemgui *x, int mode)
445
{
446
    gop_redraw = 1;
447
    if(gobj_shouldvis((t_gobj *)x, x->x_glist))
448
    {
449
        if (!x->x_vis)
450
451
        {
            //fprintf(stderr,"draw new %d\n", mode);
452
453
454
455
            x->x_draw(x, x->x_glist, IEM_GUI_DRAW_MODE_NEW);
            canvas_fixlinesfor(glist_getcanvas(x->x_glist), (t_text*)x);
            x->x_vis = 1;
            if (x->x_glist != glist_getcanvas(x->x_glist))
456
457
458
459
            {
                /* if we are inside gop and just have had our object's
                   properties changed we'll adjust our layer position
                   to ensure that ordering is honored */
460
461
                t_canvas *canvas = glist_getcanvas(x->x_glist);
                t_gobj *y = (t_gobj *)x->x_glist;
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
                gobj_vis(y, canvas, 0);
                gobj_vis(y, canvas, 1);
                // reorder it visually
                glist_redraw(canvas);

                /*
                    // some day when the object tagging is
                    // properly done for all GUI objects
                glist_noselect(canvas);
                glist_select(canvas, y);
                t_gobj *yy = canvas->gl_list;
                if (yy != y)
                {
                    fprintf(stderr,"not bottom\n");
                    while (yy && yy->g_next != y)
                    {
                        fprintf(stderr,"+\n");
                        yy = yy->g_next;
                    }
                    // now we have yy which is right before our y graph
                    t_object *ob = NULL;
                    t_rtext *yr = NULL;
                    if (yy)
                    {
                        yr = glist_findrtext(canvas, (t_text *)yy);
                    }
                    if (yr)
                    {
                        fprintf(stderr,"lower\n");
                        sys_vgui(".x%lx.c lower selected %s\n",
                            canvas, rtext_gettag(yr));
                        sys_vgui(".x%lx.c raise selected %s\n",
                            canvas, rtext_gettag(yr));
                        //sys_vgui(".x%lx.c raise all_cords\n", canvas);
496
                    }
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
                    else
                    {
                        // fall back to legacy redraw for objects
                        //   that are not patchable
                        fprintf(stderr,"lower fallback redraw\n");
                        canvas_redraw(canvas);
                    }
                }
                else
                {
                    // we get here if we are supposed to go
                    //   all the way to the bottom
                    fprintf(stderr,"lower to the bottom\n");
                    sys_vgui(".x%lx.c lower selected\n", canvas);
                }
                glist_noselect(canvas);
                */
            }
        }
516
517
518
        //fprintf(stderr,"draw move x->x_w=%d\n", x->x_w);
        x->x_draw(x, x->x_glist, mode);
        canvas_fixlinesfor(glist_getcanvas(x->x_glist), (t_text*)x);
519
    }
520
    else if (x->x_vis)
521
522
    {
        //fprintf(stderr,"draw erase %d\n", mode);
523
524
        x->x_draw(x, x->x_glist, IEM_GUI_DRAW_MODE_ERASE);
        x->x_vis = 0;
525
526
    }
    gop_redraw = 0;
Miller Puckette's avatar
Miller Puckette committed
527
528
}

529
void iemgui_size(t_iemgui *x)
Miller Puckette's avatar
Miller Puckette committed
530
{
531
532
    if(glist_isvisible(x->x_glist))
        iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_MOVE);
Miller Puckette's avatar
Miller Puckette committed
533
534
}

535
void iemgui_delta(t_iemgui *x, t_symbol *s, int ac, t_atom *av)
Miller Puckette's avatar
Miller Puckette committed
536
{
537
538
539
    x->x_obj.te_xpix += (int)atom_getintarg(0, ac, av);
    x->x_obj.te_ypix += (int)atom_getintarg(1, ac, av);
    if(glist_isvisible(x->x_glist))
Miller Puckette's avatar
Miller Puckette committed
540
    {
541
        iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_MOVE);
Miller Puckette's avatar
Miller Puckette committed
542
543
544
    }
}

545
void iemgui_pos(t_iemgui *x, t_symbol *s, int ac, t_atom *av)
Miller Puckette's avatar
Miller Puckette committed
546
{
547
548
549
550
    x->x_obj.te_xpix = (int)atom_getintarg(0, ac, av);
    x->x_obj.te_ypix = (int)atom_getintarg(1, ac, av);
    if(glist_isvisible(x->x_glist))
        iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_MOVE);
Miller Puckette's avatar
Miller Puckette committed
551
552
}

553
void iemgui_color(t_iemgui *x, t_symbol *s, int ac, t_atom *av)
Miller Puckette's avatar
Miller Puckette committed
554
{
555
    x->x_bcol = iemgui_compatible_col(atom_getintarg(0, ac, av));
Miller Puckette's avatar
Miller Puckette committed
556
557
    if(ac > 2)
    {
558
559
        x->x_fcol = iemgui_compatible_col(atom_getintarg(1, ac, av));
        x->x_lcol = iemgui_compatible_col(atom_getintarg(2, ac, av));
Miller Puckette's avatar
Miller Puckette committed
560
561
    }
    else
562
563
564
        x->x_lcol = iemgui_compatible_col(atom_getintarg(1, ac, av));
    if(glist_isvisible(x->x_glist))
        x->x_draw(x, x->x_glist, IEM_GUI_DRAW_MODE_CONFIG);
Miller Puckette's avatar
Miller Puckette committed
565
566
567
568
}

void iemgui_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
569
570
571
572
    t_iemgui *x = (t_iemgui *)z;
    x->x_obj.te_xpix += dx;
    x->x_obj.te_ypix += dy;
    iemgui_shouldvis(x, IEM_GUI_DRAW_MODE_MOVE);
Miller Puckette's avatar
Miller Puckette committed
573
574
}

575
576
void iemgui_displace_withtag(t_gobj *z, t_glist *glist, int dx, int dy)
{
577
578
579
    t_iemgui *x = (t_iemgui *)z;
    x->x_obj.te_xpix += dx;
    x->x_obj.te_ypix += dy;
580
581
582
583
584
    //(*x->x_gui.x_draw)((void *)z, glist, IEM_GUI_DRAW_MODE_MOVE);
    canvas_fixlinesfor(glist_getcanvas(glist), (t_text *)z);
}


Miller Puckette's avatar
Miller Puckette committed
585
586
void iemgui_select(t_gobj *z, t_glist *glist, int selected)
{
587
    t_iemgui *x = (t_iemgui *)z;
Miller Puckette's avatar
Miller Puckette committed
588

589
590
    x->x_selected = selected;
    (*x->x_draw)((void *)z, glist, IEM_GUI_DRAW_MODE_SELECT);
Miller Puckette's avatar
Miller Puckette committed
591
592
593
594
595
596
597
598
599
}

void iemgui_delete(t_gobj *z, t_glist *glist)
{
    canvas_deletelinesfor(glist, (t_text *)z);
}

void iemgui_vis(t_gobj *z, t_glist *glist, int vis)
{
600
    t_iemgui *x = (t_iemgui *)z;
601
602
603
604
    if (gobj_shouldvis(z, glist))
    {
        if (vis)
        {
605
606
            (*x->x_draw)((void *)z, glist, IEM_GUI_DRAW_MODE_NEW);
            x->x_vis = 1;
607
608
609
        }
        else
        {
610
            (*x->x_draw)((void *)z, glist, IEM_GUI_DRAW_MODE_ERASE);
611
            sys_unqueuegui(z);
612
            x->x_vis = 0;
613
614
        }
    }
Miller Puckette's avatar
Miller Puckette committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
}

void iemgui_save(t_iemgui *iemgui, t_symbol **srl, int *bflcol)
{
    srl[0] = iemgui->x_snd;
    srl[1] = iemgui->x_rcv;
    srl[2] = iemgui->x_lab;
    iemgui_all_sym2dollararg(iemgui, srl);
    iemgui_all_col2save(iemgui, bflcol);
}

void iemgui_properties(t_iemgui *iemgui, t_symbol **srl)
{
    srl[0] = iemgui->x_snd;
    srl[1] = iemgui->x_rcv;
    srl[2] = iemgui->x_lab;
    iemgui_all_sym2dollararg(iemgui, srl);
    iemgui_all_dollar2raute(srl);
}

int iemgui_dialog(t_iemgui *iemgui, t_symbol **srl, int argc, t_atom *argv)
{
    char str[144];
    int init = (int)atom_getintarg(5, argc, argv);
    int ldx = (int)atom_getintarg(10, argc, argv);
    int ldy = (int)atom_getintarg(11, argc, argv);
    int f = (int)atom_getintarg(12, argc, argv);
    int fs = (int)atom_getintarg(13, argc, argv);
    int bcol = (int)atom_getintarg(14, argc, argv);
    int fcol = (int)atom_getintarg(15, argc, argv);
    int lcol = (int)atom_getintarg(16, argc, argv);
646
    int rcvable=1, oldsndrcvable=0;
Miller Puckette's avatar
Miller Puckette committed
647

648
    if(iemgui_has_rcv(iemgui))
Miller Puckette's avatar
Miller Puckette committed
649
        oldsndrcvable += IEM_GUI_OLD_RCV_FLAG;
650
    if(iemgui_has_snd(iemgui))
Miller Puckette's avatar
Miller Puckette committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
        oldsndrcvable += IEM_GUI_OLD_SND_FLAG;
    if(IS_A_SYMBOL(argv,7))
        srl[0] = atom_getsymbolarg(7, argc, argv);
    else if(IS_A_FLOAT(argv,7))
    {
        sprintf(str, "%d", (int)atom_getintarg(7, argc, argv));
        srl[0] = gensym(str);
    }
    if(IS_A_SYMBOL(argv,8))
        srl[1] = atom_getsymbolarg(8, argc, argv);
    else if(IS_A_FLOAT(argv,8))
    {
        sprintf(str, "%d", (int)atom_getintarg(8, argc, argv));
        srl[1] = gensym(str);
    }
    if(IS_A_SYMBOL(argv,9))
        srl[2] = atom_getsymbolarg(9, argc, argv);
    else if(IS_A_FLOAT(argv,9))
    {
        sprintf(str, "%d", (int)atom_getintarg(9, argc, argv));
        srl[2] = gensym(str);
    }
    if(init != 0) init = 1;
674
    iemgui->x_loadinit = init;
Miller Puckette's avatar
Miller Puckette committed
675
676
677
678
    iemgui_all_raute2dollar(srl);
    iemgui_all_dollararg2sym(iemgui, srl);
    if(rcvable)
    {
679
        if(srl[1]!=iemgui->x_rcv)
Miller Puckette's avatar
Miller Puckette committed
680
        {
681
            if(iemgui_has_rcv(iemgui))
Miller Puckette's avatar
Miller Puckette committed
682
683
684
685
686
                pd_unbind(&iemgui->x_obj.ob_pd, iemgui->x_rcv);
            iemgui->x_rcv = srl[1];
            pd_bind(&iemgui->x_obj.ob_pd, iemgui->x_rcv);
        }
    }
687
    else if(!rcvable && iemgui_has_rcv(iemgui))
Miller Puckette's avatar
Miller Puckette committed
688
689
690
691
692
693
694
695
696
697
698
    {
        pd_unbind(&iemgui->x_obj.ob_pd, iemgui->x_rcv);
        iemgui->x_rcv = srl[1];
    }
    iemgui->x_snd = srl[0];
    iemgui->x_lcol = lcol & 0xffffff;
    iemgui->x_fcol = fcol & 0xffffff;
    iemgui->x_bcol = bcol & 0xffffff;
    iemgui->x_lab = srl[2];
    iemgui->x_ldx = ldx;
    iemgui->x_ldy = ldy;
699
700
    if(f<0 || f>2) f=0;
    iemgui->x_font_style = f;
Miller Puckette's avatar
Miller Puckette committed
701
702
703
704
    if(fs < 4)
        fs = 4;
    iemgui->x_fontsize = fs;
    iemgui_verify_snd_ne_rcv(iemgui);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
705
    canvas_dirty(iemgui->x_glist, 1);
Miller Puckette's avatar
Miller Puckette committed
706
707
708
    return(oldsndrcvable);
}

709
void iem_inttosymargs(t_iemgui *x, int n)
Miller Puckette's avatar
Miller Puckette committed
710
{
711
712
713
714
715
    x->x_loadinit = (n >>  0);
    x->x_scale = (n >>  20);
    x->x_flashed = 0;
    x->x_locked = 0;
    x->x_reverse = 0;
Miller Puckette's avatar
Miller Puckette committed
716
717
}

718
int iem_symargstoint(t_iemgui *x)
Miller Puckette's avatar
Miller Puckette committed
719
720
{
    return (
721
722
        (((x->x_loadinit & 1) <<  0) |
        ((x->x_scale & 1) <<  20)));
Miller Puckette's avatar
Miller Puckette committed
723
724
}

725
void iem_inttofstyle(t_iemgui *x, int n)
Miller Puckette's avatar
Miller Puckette committed
726
{
727
728
729
730
731
732
733
734
735
736
737
    x->x_font_style = (n >> 0);
    x->x_selected = 0;
    x->x_finemoved = 0;
    x->x_put_in2out = 0;
    x->x_change = 0;
    x->x_thick = 0;
    x->x_lin0_log1 = 0;
    x->x_steady = 0;
}

int iem_fstyletoint(t_iemgui *x)
Miller Puckette's avatar
Miller Puckette committed
738
{
739
    return ((x->x_font_style << 0) & 63);
Miller Puckette's avatar
Miller Puckette committed
740
}
741
742
743

char *iem_get_tag(t_canvas *glist, t_iemgui *iem_obj)
{
744
745
    t_gobj *y = (t_gobj *)iem_obj;
    t_object *ob = pd_checkobject(&y->g_pd);
746

747
748
749
750
751
    /* GOP objects are unable to call findrtext
       triggering consistency check error */
    t_rtext *yyyy = NULL;
    if (!glist->gl_isgraph || glist_istoplevel(glist))
        yyyy = glist_findrtext(glist_getcanvas(glist), (t_text *)&ob->ob_g);
752

753
754
755
    /* on GOP we cause segfault as text_gettag() returns bogus data */
    if (yyyy) return(rtext_gettag(yyyy));
    else return("bogus");
756
757
}

758
//----------------------------------------------------------------
759
// SCALEHANDLE COMMON CODE (by Mathieu, refactored from existing code)
760

761
762
763
764
765
extern int gfxstub_haveproperties(void *key);

int mini(int a, int b) {return a<b?a:b;}
int maxi(int a, int b) {return a>b?a:b;}

766
767
768
769
770
771
772
773
774
775
776
777
778
// in all 20 cases :
// [bng], [tgl], [hradio], [vradio], [hsl], [vsl], [cnv], [nbx], [vu]
// for both scale & label, plus canvas' scale & move.
void scalehandle_bind(t_scalehandle *h) {
    sys_vgui("bind %s <Button> {pd [concat %s _click 1 %%x %%y \\;]}\n",
        h->h_pathname, h->h_bindsym->s_name);
    sys_vgui("bind %s <ButtonRelease> {pd [concat %s _click 0 0 0 \\;]}\n",
        h->h_pathname, h->h_bindsym->s_name);
    sys_vgui("bind %s <Motion> {pd [concat %s _motion %%x %%y \\;]}\n",
        h->h_pathname, h->h_bindsym->s_name);
}

// in 18 cases only, because canvas does not fit the pattern below.
779
780
// canvas has no label handle and has a motion handle
// but in the case of canvas, the "iemgui" tag is added (it wasn't the case originally)
781
782
783
void scalehandle_draw_select(t_scalehandle *h, t_glist *canvas, int px, int py,
const char *nlet_tag, const char *class_tag) {
    char tags[128]; // BNG may need up to 100 chars in 64-bit mode, for example
784
    t_text *x = (t_text *)h->h_master;
785
786
787
788
789
790
791
792
    //if (!nlet_tag) nlet_tag = iem_get_tag(canvas, (t_iemgui *)x);

    const char *cursor = h->h_scale ? "bottom_right_corner" : "crosshair";
    int sx = h->h_scale ? SCALEHANDLE_WIDTH  : LABELHANDLE_WIDTH;
    int sy = h->h_scale ? SCALEHANDLE_HEIGHT : LABELHANDLE_HEIGHT;

    //printf("scalehandle_draw_select(x%lx,x%lx,%d,%d,\"%s\",\"%s\")\n",h,canvas,px,py,nlet_tag,class_tag);

793
    if (h->h_vis) scalehandle_draw_erase(h,canvas);
794

795
796
//    sys_vgui("canvas %s -width %d -height %d -bg $pd_colors(selection) -bd 0 "
    sys_vgui("canvas %s -width %d -height %d -bg #0080ff -bd 0 "
797
798
799
800
801
802
803
804
        "-cursor %s\n", h->h_pathname, sx, sy, cursor);
    // there was a %lxBNG tag (or similar) in every scalehandle,
    // but it didn't seem to be used —mathieu
    if (h->h_scale) {
        sprintf(tags,"%lx%s %lxSCALE iemgui %s",
            (long)x,class_tag,(long)x,nlet_tag);
    } else {
        //sprintf(tags,"%lx%s %lxLABEL %lxLABELH iemgui %s", // causes unknown option "-fill"
805
806
        sprintf(tags,"%lx%s %lx%s iemgui %s", (long)x,class_tag,
            (long)x,strcmp(class_tag,"GOP")?"LABELH":"MOVE",nlet_tag);
807
808
    }
    sys_vgui(".x%x.c create window %d %d -anchor nw -width %d -height %d "
809
        "-window %s -tags {%s}\n", canvas, x->te_xpix+px-sx, x->te_ypix+py-sy,
810
811
        sx, sy, h->h_pathname, tags);
    scalehandle_bind(h);
812
    h->h_vis = 1;
813
814
}

815
void scalehandle_draw_select2(t_iemgui *x, t_glist *canvas, const char *class_tag, int sx, int sy) {
816
    char *nlet_tag = iem_get_tag(canvas, (t_iemgui *)x);
817
    scalehandle_draw_select(x->x_handle,canvas,sx,sy,nlet_tag,class_tag);
818
    if (x->x_lab!=s_empty)
819
820
821
822
823
    {
        scalehandle_draw_select(x->x_lhandle,canvas,x->x_ldx,x->x_ldy,nlet_tag,class_tag);
    }
}

824
825
826
void scalehandle_draw_erase(t_scalehandle *h, t_glist *canvas) {
        sys_vgui("destroy %s\n", h->h_pathname);
        sys_vgui(".x%lx.c delete %lx%s\n", canvas, h->h_master, h->h_scale ? "SCALE" : "LABELH");
827
        h->h_vis = 0;
828
829
830
}

void scalehandle_draw_erase2(t_iemgui *x, t_glist *canvas) {
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
	t_scalehandle *sh = (t_scalehandle *)(x->x_handle);
	t_scalehandle *lh = (t_scalehandle *)(x->x_lhandle);
    if (sh->h_vis) scalehandle_draw_erase(sh,canvas);
    if (lh->h_vis) scalehandle_draw_erase(lh,canvas);
}

void scalehandle_draw_new(t_scalehandle *h, t_glist *canvas) {
    sprintf(h->h_pathname, ".x%lx.h%lx", (t_int)canvas, (t_int)h);
}

t_scalehandle *scalehandle_new(t_class *c, t_iemgui *x, int scale) {
    t_scalehandle *h = (t_scalehandle *)pd_new(c);
    char buf[64];
    h->h_master = (t_gobj*)x;
    sprintf(buf, "_h%lx", (t_int)h);
    pd_bind((t_pd *)h, h->h_bindsym = gensym(buf));
    sprintf(h->h_outlinetag, "h%lx", (t_int)h);
    h->h_dragon = 0;
    h->h_scale = scale;
850
851
    //h->h_offset_x = 0; // unused (maybe keep for later)
    //h->h_offset_y = 0; // unused (maybe keep for later)
852
853
854
855
856
857
858
    h->h_vis = 0;
    return h;
}

void scalehandle_free(t_scalehandle *h) {
    pd_unbind((t_pd *)h, h->h_bindsym);
    pd_free((t_pd *)h);
859
}
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875

void properties_set_field_int(long props, const char *gui_field, int value) {
    sys_vgui(".gfxstub%lx.%s delete 0 end\n", props, gui_field);
    sys_vgui(".gfxstub%lx.%s insert 0 %d\n", props, gui_field, value);
};

void scalehandle_dragon_label(t_scalehandle *h, float f1, float f2) {
    if (h->h_dragon && !h->h_scale)
    {
        t_iemgui *x = (t_iemgui *)(h->h_master);
        int dx = (int)f1, dy = (int)f2;
        h->h_dragx = dx;
        h->h_dragy = dy;
        int properties = gfxstub_haveproperties((void *)x);
        if (properties)
        {
876
877
            int new_x = x->x_ldx + h->h_dragx;
            int new_y = x->x_ldy + h->h_dragy;
878
879
880
881
882
883
884
885
            properties_set_field_int(properties,"label.xy.x_entry",new_x);
            properties_set_field_int(properties,"label.xy.y_entry",new_y);
        }
        if (glist_isvisible(x->x_glist))
        {
            int xpos=text_xpix(&x->x_obj, x->x_glist);
            int ypos=text_ypix(&x->x_obj, x->x_glist);
            t_canvas *canvas=glist_getcanvas(x->x_glist);
886
887
888
            sys_vgui(".x%lx.c coords %lxLABEL %d %d\n", canvas, x,
                xpos+x->x_ldx + h->h_dragx,
                ypos+x->x_ldy + h->h_dragy);
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
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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966

void scalehandle_unclick_label(t_scalehandle *h) {
    t_iemgui *x = (t_iemgui *)h->h_master;
    canvas_apply_setundo(x->x_glist, (t_gobj *)x);
    if (h->h_dragx || h->h_dragy)
    {
        x->x_ldx += h->h_dragx;
        x->x_ldy += h->h_dragy;
        canvas_dirty(x->x_glist, 1);
    }
    if (glist_isvisible(x->x_glist))
    {
        iemgui_select((t_gobj *)x, x->x_glist, 1);
        canvas_fixlinesfor(x->x_glist, (t_text *)x);
        sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x->x_glist);
    }
}

void scalehandle_click_label(t_scalehandle *h) {
    t_iemgui *x = (t_iemgui *)h->h_master;
    if (glist_isvisible(x->x_glist))
    {
        sys_vgui("lower %s\n", h->h_pathname);
        t_scalehandle *othersh = x->x_handle;
        sys_vgui("lower .x%lx.h%lx\n",
            (t_int)glist_getcanvas(x->x_glist), (t_int)othersh);
    }
    h->h_dragx = 0;
    h->h_dragy = 0;
}

extern t_class *my_canvas_class;
void scalehandle_getrect_master(t_scalehandle *h, int *x1, int *y1, int *x2, int *y2) {
    t_iemgui *x = (t_iemgui *)h->h_master;
    t_class *c = pd_class((t_pd *)x);
    c->c_wb->w_getrectfn((t_gobj *)x,x->x_glist,x1,y1,x2,y2);
    //printf("%s\n",c->c_name->s_name);
    if (c==my_canvas_class) {
        t_my_canvas *xx = (t_my_canvas *)x;
        *x2=*x1+xx->x_vis_w;
        *y2=*y1+xx->x_vis_h;
    }
}

void scalehandle_click_scale(t_scalehandle *h) {
    int x1,y1,x2,y2;
    t_iemgui *x = (t_iemgui *)h->h_master;
    scalehandle_getrect_master(h,&x1,&y1,&x2,&y2);
    if (glist_isvisible(x->x_glist)) {
        sys_vgui("lower %s\n", h->h_pathname);
        sys_vgui(".x%x.c create prect %d %d %d %d -stroke $pd_colors(selection) -strokewidth 1 -tags %s\n",
            x->x_glist, x1, y1, x2, y2, h->h_outlinetag);
    }
    h->h_dragx = 0;
    h->h_dragy = 0;
}

void scalehandle_unclick_scale(t_scalehandle *h) {
    t_iemgui *x = (t_iemgui *)h->h_master;
    sys_vgui(".x%x.c delete %s\n", x->x_glist, h->h_outlinetag);
    iemgui_select((t_gobj *)x, x->x_glist, 1);
    canvas_fixlinesfor(x->x_glist, (t_text *)x);
    sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", x->x_glist);
}

void scalehandle_drag_scale(t_scalehandle *h) {
    int x1,y1,x2,y2;
    t_iemgui *x = (t_iemgui *)h->h_master;
    scalehandle_getrect_master(h,&x1,&y1,&x2,&y2);
    if (glist_isvisible(x->x_glist)) {
        sys_vgui(".x%x.c coords %s %d %d %d %d\n", x->x_glist, h->h_outlinetag,
            x1, y1, x2+h->h_dragx, y2+h->h_dragy);
    }
}

967
968
969
970
//----------------------------------------------------------------
// IEMGUI refactor (by Mathieu)

void iemgui_tag_selected(t_iemgui *x, t_glist *canvas, const char *class_tag) {
971
    if(x->x_selected)
972
973
974
975
976
977
978
979
980
981
        sys_vgui(".x%lx.c addtag selected withtag %lx%s\n", canvas, x, class_tag);
    else
        sys_vgui(".x%lx.c dtag %lx%s selected\n", canvas, x, class_tag);
}

void iemgui_label_draw_new(t_iemgui *x, t_glist *canvas, int xpos, int ypos, const char *nlet_tag, const char *class_tag) {
    sys_vgui(".x%lx.c create text %d %d -text {%s} -anchor w "
             "-font {{%s} -%d %s} -fill #%6.6x "
             "-tags {%lxLABEL %lx%s text iemgui %s}\n",
         canvas, xpos+x->x_ldx, ypos+x->x_ldy,
982
983
         x->x_lab!=s_empty?x->x_lab->s_name:"",
         iemgui_font(x), x->x_fontsize, sys_fontweight,
984
985
986
987
988
989
990
         x->x_lcol, x, x, class_tag, nlet_tag);
}
void iemgui_label_draw_move(t_iemgui *x, t_glist *canvas, int xpos, int ypos) {
    sys_vgui(".x%lx.c coords %lxLABEL %d %d\n",
        canvas, x, xpos+x->x_ldx, ypos+x->x_ldy);
}
void iemgui_label_draw_config(t_iemgui *x, t_glist *canvas) {
991
    if (x->x_selected && x->x_glist == canvas)
992
993
        sys_vgui(".x%lx.c itemconfigure %lxLABEL -font {{%s} -%d %s} "
                 "-fill $pd_colors(selection) -text {%s} \n",
994
995
             canvas, x, iemgui_font(x), x->x_fontsize, sys_fontweight,
             x->x_lab!=s_empty?x->x_lab->s_name:"");
996
997
998
    else
        sys_vgui(".x%lx.c itemconfigure %lxLABEL -font {{%s} -%d %s} "
                 "-fill #%6.6x -text {%s} \n",
999
1000
             canvas, x, iemgui_font(x), x->x_fontsize, sys_fontweight,
             x->x_lcol, x->x_lab!=s_empty?x->x_lab->s_name:"");
1001
1002
}
void iemgui_label_draw_select(t_iemgui *x, t_glist *canvas) {
1003
    if(x->x_selected)
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
        sys_vgui(".x%lx.c itemconfigure %lxLABEL "
            "-fill $pd_colors(selection)\n", canvas, x);
    else
        sys_vgui(".x%lx.c itemconfigure %lxLABEL -fill #%6.6x\n",
            canvas, x, x->x_lcol);
}

void iemgui_io_draw(t_iemgui *x, t_glist *canvas, int old_sr_flags, const char *class_tag) {
    int a,b;
    t_class *c = pd_class((t_pd *)x);
    //printf("--- iemgui_io_draw %s flags=%d\n",c->c_name->s_name,old_sr_flags);
    char *nlet_tag = iem_get_tag(/*glist*/ x->x_glist, (t_iemgui *)x);

    if (!(old_sr_flags&4) && (!glist_isvisible(canvas) || !(canvas == x->x_glist))) {
        //printf("---/iemgui_io_draw not visible\n");
        return;
    }

    int x1,y1,x2,y2;
    c->c_wb->w_getrectfn((t_gobj *)x,canvas,&x1,&y1,&x2,&y2);

    a=old_sr_flags&IEM_GUI_OLD_SND_FLAG;
1026
    //b=iemgui_has_snd(x); // not inited at moment of new
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
    b=x->x_snd!=s_empty;
    //printf("a=%d b=%d snd=%s\n",a,b,x->x_snd->s_name);
    if(a && !b)
        //printf("%s create outlet\n",c->c_name->s_name), fflush(stdout),
        sys_vgui(".x%lx.c create prect %d %d %d %d "
                 "-stroke $pd_colors(iemgui_nlet) "
                 "-tags {%lx%s%so%d %so%d %lx%s outlet iemgui %s}\n",
             canvas, x1, y2-1, x1 + IOWIDTH, y2,
             x, class_tag, nlet_tag, 0, nlet_tag, 0, x, class_tag, nlet_tag);
    if(!a && b)
        //printf("%s delete outlet\n",c->c_name->s_name), fflush(stdout),
        sys_vgui(".x%lx.c delete %lx%s%so%d\n", canvas, x, class_tag, nlet_tag, 0);

    a=old_sr_flags&IEM_GUI_OLD_RCV_FLAG;
1041
    //b=iemgui_has_rcv(x); // not inited at moment of new
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
    b=x->x_rcv!=s_empty;
    //printf("a=%d b=%d rcv=%s\n",a,b,x->x_rcv->s_name);
    if(a && !b)
        //printf("%s create inlet\n",c->c_name->s_name), fflush(stdout),
        sys_vgui(".x%lx.c create prect %d %d %d %d "
                 "-stroke $pd_colors(iemgui_nlet) "
                 "-tags {%lx%s%si%d %si%d %lx%s inlet iemgui %s}\n",
             canvas, x1, y1, x1 + IOWIDTH, y1+1,
             x, class_tag, nlet_tag, 0, nlet_tag, 0, x, class_tag, nlet_tag);
    if(!a && b)
        //printf("%s delete inlet\n",c->c_name->s_name), fflush(stdout),
        sys_vgui(".x%lx.c delete %lx%s%si%d\n", canvas, x, class_tag, nlet_tag, 0);
    //printf("---/iemgui_io_draw\n");
}

void iemgui_draw_erase(t_iemgui *x, t_glist* glist, const char *class_tag) {
    t_canvas *canvas=glist_getcanvas(glist);
    sys_vgui(".x%lx.c delete %lx%s\n", canvas, x, class_tag);
    sys_vgui(".x%lx.c dtag all %lx%s\n", canvas, x, class_tag);
    scalehandle_draw_erase2(x,glist);
}

void wb_init(t_widgetbehavior *wb, t_getrectfn gr, t_clickfn cl) {
    wb->w_getrectfn = gr;
    wb->w_displacefn = iemgui_displace;
    wb->w_selectfn = iemgui_select;
    wb->w_activatefn = NULL;
    wb->w_deletefn = iemgui_delete;
    wb->w_visfn = iemgui_vis;
    wb->w_clickfn = cl;
    wb->w_displacefnwtag = iemgui_displace_withtag;
}
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104

const char *iemgui_font(t_iemgui *x) {
	int f = x->x_font_style;
	if(f == 0) return sys_font;
    if(f == 1) return "helvetica";
    if(f == 2) return "times";
    return "invalid-font";
}

void iemgui_class_addmethods(t_class *c) {
    class_addmethod(c, (t_method)iemgui_delta,
        gensym("delta"), A_GIMME, 0);
    class_addmethod(c, (t_method)iemgui_pos,
        gensym("pos"), A_GIMME, 0);
    class_addmethod(c, (t_method)iemgui_color,
        gensym("color"), A_GIMME, 0);
    class_addmethod(c, (t_method)iemgui_send,
        gensym("send"), A_DEFSYM, 0);
    class_addmethod(c, (t_method)iemgui_receive,
        gensym("receive"), A_DEFSYM, 0);
    class_addmethod(c, (t_method)iemgui_label,
        gensym("label"), A_DEFSYM, 0);
    class_addmethod(c, (t_method)iemgui_label_pos,
        gensym("label_pos"), A_GIMME, 0);
    class_addmethod(c, (t_method)iemgui_label_font,
        gensym("label_font"), A_GIMME, 0);
}

void g_iemgui_setup (void) {
    s_empty = gensym("empty");
}