g_graph.c 44 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
/* Copyright (c) 1997-2001 Miller Puckette and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* This file deals with the behavior of glists as either "text objects" or
"graphs" inside another glist.  LATER move the inlet/outlet code of g_canvas.c 
to this file... */

#include <stdlib.h>
#include "m_pd.h"
11
#include "m_imp.h"
Miller Puckette's avatar
Miller Puckette committed
12
13
#include "t_tk.h"
#include "g_canvas.h"
14
#include "g_all_guis.h" /* for canvas handle freeing */
Miller Puckette's avatar
Miller Puckette committed
15
16
17
18
19
20
21
22
23
24
25
#include "s_stuff.h"    /* for sys_hostfontsize */
#include <stdio.h>
#include <string.h>

/* ---------------------- forward definitions ----------------- */

static void graph_vis(t_gobj *gr, t_glist *unused_glist, int vis);
static void graph_graphrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2);
static void graph_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2);
26
27
void graph_checkgop_rect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2);
Miller Puckette's avatar
Miller Puckette committed
28

29
extern int do_not_redraw;
30
int gop_redraw = 0;
31

Miller Puckette's avatar
Miller Puckette committed
32
33
34
35
36
37
/* -------------------- maintaining the list -------------------- */

void canvas_drawredrect(t_canvas *x, int doit);

void glist_add(t_glist *x, t_gobj *y)
{
38
39
	//fprintf(stderr,"glist_add %lx %d\n", (t_int)x, (x->gl_editor ? 1 : 0));    
	t_object *ob;
Miller Puckette's avatar
Miller Puckette committed
40
    y->g_next = 0;
41
42
	int index = 0;

Miller Puckette's avatar
Miller Puckette committed
43
44
45
46
    if (!x->gl_list) x->gl_list = y;
    else
    {
        t_gobj *y2;
47
48
        for (y2 = x->gl_list; y2->g_next; y2 = y2->g_next)
			index++;
Miller Puckette's avatar
Miller Puckette committed
49
50
        y2->g_next = y;
    }
51
    if (x->gl_editor && (ob = pd_checkobject(&y->g_pd))) {
Miller Puckette's avatar
Miller Puckette committed
52
        rtext_new(x, ob);
53
54
55
56
57
58
		//let's now set up create undo
		//glist_select(x, y);
		//canvas_setundo(x, canvas_undo_create, canvas_undo_set_create(x, index),
        //    "create");
		//glist_noselect(x);
	}
Miller Puckette's avatar
Miller Puckette committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    if (x->gl_editor && x->gl_isgraph && !x->gl_goprect
        && pd_checkobject(&y->g_pd))
    {
        x->gl_goprect = 1;
        canvas_drawredrect(x, 1);
    }
    if (glist_isvisible(x))
        gobj_vis(y, x, 1);
    if (class_isdrawcommand(y->g_pd)) 
        canvas_redrawallfortemplate(template_findbyname(canvas_makebindsym(
            glist_getcanvas(x)->gl_name)), 0);
}

    /* this is to protect against a hairy problem in which deleting
    a sub-canvas might delete an inlet on a box, after the box had
    been invisible-ized, so that we have to protect against redrawing it! */
int canvas_setdeleting(t_canvas *x, int flag)
{
    int ret = x->gl_isdeleting;
    x->gl_isdeleting = flag;
    return (ret);
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
82
83
84
/* JMZ: emit a closebang message */
void canvas_closebang(t_canvas *x);

85
86
87
88
/* variant of the glist_findrtext found in g_rtext.c 
   that does not throw a consistency check */
extern t_rtext *glist_tryfindrtext(t_glist *gl, t_text *who);

Miller Puckette's avatar
Miller Puckette committed
89
90
91
    /* delete an object from a glist and free it */
void glist_delete(t_glist *x, t_gobj *y)
{
92
	//fprintf(stderr,"glist_delete?\n");
93
	if (x->gl_list) {
94
		//fprintf(stderr,"glist_delete YES\n");
95
96
97
98
99
100
		t_gobj *g;
		t_object *ob;
		t_gotfn chkdsp = zgetfn(&y->g_pd, gensym("dsp"));
		t_canvas *canvas = glist_getcanvas(x);
		int drawcommand = class_isdrawcommand(y->g_pd);
		int wasdeleting;
101
102
		t_rtext *rt = NULL;
		int late_rtext_free = 0;
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
		if (pd_class(&y->g_pd) == canvas_class) {
		  /* JMZ: send a closebang to the canvas */
		  canvas_closebang((t_canvas *)y);
		}
	 
		wasdeleting = canvas_setdeleting(canvas, 1);
		if (x->gl_editor)
		{
		    if (x->gl_editor->e_grab == y) x->gl_editor->e_grab = 0;
		    if (glist_isselected(x, y)) glist_deselect(x, y);

		        /* HACK -- we had phantom outlets not getting erased on the
		        screen because the canvas_setdeleting() mechanism is too
		        crude.  LATER carefully set up rules for when the rtexts
		        should exist, so that they stay around until all the
		        steps of becoming invisible are done.  In the meantime, just
		        zap the inlets and outlets here... */
		    if (pd_class(&y->g_pd) == canvas_class)
		    {
		        t_glist *gl = (t_glist *)y;
		        if (gl->gl_isgraph)
		        {
		            char tag[80];
		            //sprintf(tag, "graph%lx", (t_int)gl);
					//t_glist *yy = (t_glist *)y;
					sprintf(tag, "%s", rtext_gettag(glist_findrtext(x, &gl->gl_obj)));
		            glist_eraseiofor(x, &gl->gl_obj, tag);
		            text_eraseborder(&gl->gl_obj, x,
		                rtext_gettag(glist_findrtext(x, &gl->gl_obj)));
		        }
		        else
		        {
		            text_eraseborder(&gl->gl_obj, x,
		                rtext_gettag(glist_findrtext(x, &gl->gl_obj)));
		        }
		    }
		}
		    /* if we're a drawing command, erase all scalars now, before deleting
		    it; we'll redraw them once it's deleted below. */
		if (drawcommand)
		    canvas_redrawallfortemplate(template_findbyname(canvas_makebindsym(
		        glist_getcanvas(x)->gl_name)), 2);
		if (glist_isvisible(canvas))
		    gobj_vis(y, x, 0);
148
		if (x->gl_editor && (ob = pd_checkobject(&y->g_pd))) {
Ivica Bukvic's avatar
Ivica Bukvic committed
149
		    //rtext_new(x, ob);
150
			rt = glist_findrtext(x, ob);
151
152
			if (rt)
				late_rtext_free = 1;
153
		}
154
		if (x->gl_list == y) {
155
			if (y->g_next)
156
157
158
159
160
161
				x->gl_list = y->g_next;
			else
				x->gl_list = NULL;
		}
		else for (g = x->gl_list; g; g = g->g_next)
		{
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
162
163
164
165
166
167
168
		    if (g->g_next == y)
			{
				if (y->g_next)
				    g->g_next = y->g_next;
				else g->g_next = NULL;
		    	break;
			}
169
170
171
172
173
174
175
176
177
		}
		gobj_delete(y, x);
		pd_free(&y->g_pd);
		if (chkdsp) canvas_update_dsp();
		if (drawcommand)
		    canvas_redrawallfortemplate(template_findbyname(canvas_makebindsym(
		        glist_getcanvas(x)->gl_name)), 1);
		canvas_setdeleting(canvas, wasdeleting);
		x->gl_valid = ++glist_valid;
178
		if (late_rtext_free) {
Ivica Bukvic's avatar
Ivica Bukvic committed
179
			//fprintf(stderr,"glist_delete late_rtext_free\n");
180
181
			rtext_free(rt);
		}
182
	}
Miller Puckette's avatar
Miller Puckette committed
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
}

    /* remove every object from a glist.  Experimental. */
void glist_clear(t_glist *x)
{
    t_gobj *y, *y2;
    int dspstate = 0, suspended = 0;
    t_symbol *dspsym = gensym("dsp");
    while (y = x->gl_list)
    {
            /* to avoid unnecessary DSP resorting, we suspend DSP
            only if we hit a patchable object. */
        if (!suspended && pd_checkobject(&y->g_pd) && zgetfn(&y->g_pd, dspsym))
        {
            dspstate = canvas_suspend_dsp();
            suspended = 1;
        }
            /* here's the real deletion. */
        glist_delete(x, y);
    }
    if (suspended)
        canvas_resume_dsp(dspstate);
}

void glist_retext(t_glist *glist, t_text *y)
{
    t_canvas *c = glist_getcanvas(glist);
        /* check that we have built rtexts yet.  LATER need a better test. */
    if (glist->gl_editor && glist->gl_editor->e_rtext)
    {
        t_rtext *rt = glist_findrtext(glist, y);
        if (rt)
            rtext_retext(rt);
    }
}

void glist_grab(t_glist *x, t_gobj *y, t_glistmotionfn motionfn,
    t_glistkeyfn keyfn, int xpos, int ypos)
{
222
	//fprintf(stderr,"glist_grab\n");
Miller Puckette's avatar
Miller Puckette committed
223
224
225
226
227
228
229
230
231
232
233
234
235
    t_glist *x2 = glist_getcanvas(x);
    if (motionfn)
        x2->gl_editor->e_onmotion = MA_PASSOUT;
    else x2->gl_editor->e_onmotion = 0;
    x2->gl_editor->e_grab = y;
    x2->gl_editor->e_motionfn = motionfn;
    x2->gl_editor->e_keyfn = keyfn;
    x2->gl_editor->e_xwas = xpos;
    x2->gl_editor->e_ywas = ypos;
}

t_canvas *glist_getcanvas(t_glist *x)
{
236
	//fprintf(stderr,"glist_getcanvas\n");
237
    while (x->gl_owner && !x->gl_havewindow && x->gl_isgraph &&
238
        gobj_shouldvis(&x->gl_gobj, x->gl_owner)) {
239
            x = x->gl_owner;
240
241
			//fprintf(stderr,"+\n");
	}
Miller Puckette's avatar
Miller Puckette committed
242
243
244
    return((t_canvas *)x);
}

245
static t_float gobj_getxforsort(t_gobj *g)
Miller Puckette's avatar
Miller Puckette committed
246
247
248
{
    if (pd_class(&g->g_pd) == scalar_class)
    {
249
        t_float x1, y1;
Miller Puckette's avatar
Miller Puckette committed
250
251
252
253
254
255
256
257
258
        scalar_getbasexy((t_scalar *)g, &x1, &y1);
        return(x1);
    }
    else return (0);
}

static t_gobj *glist_merge(t_glist *x, t_gobj *g1, t_gobj *g2)
{
    t_gobj *g = 0, *g9 = 0;
259
    t_float f1 = 0, f2 = 0;
Miller Puckette's avatar
Miller Puckette committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    if (g1)
        f1 = gobj_getxforsort(g1);
    if (g2)
        f2 = gobj_getxforsort(g2);
    while (1)
    {
        if (g1)
        {
            if (g2)
            {
                if (f1 <= f2)
                    goto put1;
                else goto put2;
            }
            else goto put1;     
        }
        else if (g2)
            goto put2;
        else break;
    put1:
        if (g9)
            g9->g_next = g1, g9 = g1;
        else g9 = g = g1;
        if (g1 = g1->g_next)
            f1 = gobj_getxforsort(g1);
        g9->g_next = 0;
        continue;
    put2:
        if (g9)
            g9->g_next = g2, g9 = g2;
        else g9 = g = g2;
        if (g2 = g2->g_next)
            f2 = gobj_getxforsort(g2);
        g9->g_next = 0;
        continue;
    }
    return (g);
}

static t_gobj *glist_dosort(t_glist *x,
    t_gobj *g, int nitems)
{
    if (nitems < 2)
        return (g);
    else
    {
        int n1 = nitems/2, n2 = nitems - n1, i;
        t_gobj *g2, *g3;
        for (g2 = g, i = n1-1; i--; g2 = g2->g_next)
            ;
        g3 = g2->g_next;
        g2->g_next = 0;
        g = glist_dosort(x, g, n1);
        g3 = glist_dosort(x, g3, n2);
        return (glist_merge(x, g, g3));
    }
}

void glist_sort(t_glist *x)
{
    int nitems = 0, foo = 0;
321
    t_float lastx = -1e37;
Miller Puckette's avatar
Miller Puckette committed
322
323
324
    t_gobj *g;
    for (g = x->gl_list; g; g = g->g_next)
    {
325
        t_float x1 = gobj_getxforsort(g);
Miller Puckette's avatar
Miller Puckette committed
326
327
328
329
330
331
332
333
334
335
336
        if (x1 < lastx)
            foo = 1;
        lastx = x1;
        nitems++;
    }
    if (foo)
        x->gl_list = glist_dosort(x, x->gl_list, nitems);
}

void glist_cleanup(t_glist *x)
{
337
	//fprintf(stderr,"glist_cleanup =============\n");
Miller Puckette's avatar
Miller Puckette committed
338
339
    freebytes(x->gl_xlabel, x->gl_nxlabels * sizeof(*(x->gl_xlabel)));
    freebytes(x->gl_ylabel, x->gl_nylabels * sizeof(*(x->gl_ylabel)));
340
341
342
343
344
345
346
347
	if (x->x_handle) {	
		pd_unbind(x->x_handle, ((t_scalehandle *)x->x_handle)->h_bindsym);
		pd_free(x->x_handle);
	}
	if (x->x_mhandle) {
		pd_unbind(x->x_mhandle, ((t_scalehandle *)x->x_mhandle)->h_bindsym);
		pd_free(x->x_mhandle);
	}
Miller Puckette's avatar
Miller Puckette committed
348
349
350
351
352
    gstub_cutoff(x->gl_stub);
}

void glist_free(t_glist *x)
{
353
	//fprintf(stderr,"glist_free =============\n");
Miller Puckette's avatar
Miller Puckette committed
354
355
356
357
358
359
360
361
362
    glist_cleanup(x);
    freebytes(x, sizeof(*x));
}

/* --------------- inlets and outlets  ----------- */


t_inlet *canvas_addinlet(t_canvas *x, t_pd *who, t_symbol *s)
{
363
	//fprintf(stderr,"canvas_addinlet %d %lx %d\n", x->gl_loading, x->gl_owner, glist_isvisible(x->gl_owner));
Miller Puckette's avatar
Miller Puckette committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
    t_inlet *ip = inlet_new(&x->gl_obj, who, s, 0);
    if (!x->gl_loading && x->gl_owner && glist_isvisible(x->gl_owner))
    {
        gobj_vis(&x->gl_gobj, x->gl_owner, 0);
        gobj_vis(&x->gl_gobj, x->gl_owner, 1);
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
    }
    if (!x->gl_loading) canvas_resortinlets(x);
    return (ip);
}

void canvas_rminlet(t_canvas *x, t_inlet *ip)
{
    t_canvas *owner = x->gl_owner;
    int redraw = (owner && glist_isvisible(owner) && (!owner->gl_isdeleting)
        && glist_istoplevel(owner));
    
    if (owner) canvas_deletelinesforio(owner, &x->gl_obj, ip, 0);
    if (redraw)
        gobj_vis(&x->gl_gobj, x->gl_owner, 0);
    inlet_free(ip);
    if (redraw)
    {
        gobj_vis(&x->gl_gobj, x->gl_owner, 1);
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
    }
}

extern t_inlet *vinlet_getit(t_pd *x);
extern void obj_moveinletfirst(t_object *x, t_inlet *i);

void canvas_resortinlets(t_canvas *x)
{
    int ninlets = 0, i, j, xmax;
    t_gobj *y, **vec, **vp, **maxp;
    
    for (ninlets = 0, y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == vinlet_class) ninlets++;

    if (ninlets < 2) return;
    
    vec = (t_gobj **)getbytes(ninlets * sizeof(*vec));
    
    for (y = x->gl_list, vp = vec; y; y = y->g_next)
        if (pd_class(&y->g_pd) == vinlet_class) *vp++ = y;
    
    for (i = ninlets; i--;)
    {
        t_inlet *ip;
        for (vp = vec, xmax = -0x7fffffff, maxp = 0, j = ninlets;
            j--; vp++)
        {
            int x1, y1, x2, y2;
            t_gobj *g = *vp;
            if (!g) continue;
            gobj_getrect(g, x, &x1, &y1, &x2, &y2);
            if (x1 > xmax) xmax = x1, maxp = vp;
        }
        if (!maxp) break;
        y = *maxp;
        *maxp = 0;
        ip = vinlet_getit(&y->g_pd);
        
        obj_moveinletfirst(&x->gl_obj, ip);
    }
    freebytes(vec, ninlets * sizeof(*vec));
430
    if (x->gl_owner && glist_isvisible(x->gl_owner) && glist_isvisible(x) && !x->gl_owner->gl_loading && !x->gl_loading) {
Miller Puckette's avatar
Miller Puckette committed
431
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
432
433
434
435
436
437
438
439
440
441
		//fprintf(stderr,"good place to fix redrawing of inlets .x%lx owner=.x%lx %d (parent)%d\n", x, x->gl_owner, x->gl_loading, x->gl_owner->gl_loading);
/*		t_object *ob = pd_checkobject(&y->g_pd);
		t_rtext *rt = glist_findrtext(x->gl_owner, (t_text *)&ob->ob_g);
		for (i = 0; i < ninlets; i++) {
            sys_vgui(".x%x.c itemconfigure %si%d -fill %s -width 1\n",
                     x, rtext_gettag(rt), i, 
                     (obj_issignalinlet(ob, i) ? "$signal_nlet" : "$msg_nlet"));
		}
*/
		//glist_redraw(x);
442
	    graph_vis(&x->gl_gobj, x->gl_owner, 0); 
443
		graph_vis(&x->gl_gobj, x->gl_owner, 1);
444
	}
Miller Puckette's avatar
Miller Puckette committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
}

t_outlet *canvas_addoutlet(t_canvas *x, t_pd *who, t_symbol *s)
{
    t_outlet *op = outlet_new(&x->gl_obj, s);
    if (!x->gl_loading && x->gl_owner && glist_isvisible(x->gl_owner))
    {
        gobj_vis(&x->gl_gobj, x->gl_owner, 0);
        gobj_vis(&x->gl_gobj, x->gl_owner, 1);
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
    }
    if (!x->gl_loading) canvas_resortoutlets(x);
    return (op);
}

void canvas_rmoutlet(t_canvas *x, t_outlet *op)
{
    t_canvas *owner = x->gl_owner;
    int redraw = (owner && glist_isvisible(owner) && (!owner->gl_isdeleting)
        && glist_istoplevel(owner));
    
    if (owner) canvas_deletelinesforio(owner, &x->gl_obj, 0, op);
    if (redraw)
        gobj_vis(&x->gl_gobj, x->gl_owner, 0);

    outlet_free(op);
    if (redraw)
    {
        gobj_vis(&x->gl_gobj, x->gl_owner, 1);
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
    }
}

extern t_outlet *voutlet_getit(t_pd *x);
extern void obj_moveoutletfirst(t_object *x, t_outlet *i);

void canvas_resortoutlets(t_canvas *x)
{
    int noutlets = 0, i, j, xmax;
    t_gobj *y, **vec, **vp, **maxp;
    
    for (noutlets = 0, y = x->gl_list; y; y = y->g_next)
        if (pd_class(&y->g_pd) == voutlet_class) noutlets++;

    if (noutlets < 2) return;
    
    vec = (t_gobj **)getbytes(noutlets * sizeof(*vec));
    
    for (y = x->gl_list, vp = vec; y; y = y->g_next)
        if (pd_class(&y->g_pd) == voutlet_class) *vp++ = y;
    
    for (i = noutlets; i--;)
    {
        t_outlet *ip;
        for (vp = vec, xmax = -0x7fffffff, maxp = 0, j = noutlets;
            j--; vp++)
        {
            int x1, y1, x2, y2;
            t_gobj *g = *vp;
            if (!g) continue;
            gobj_getrect(g, x, &x1, &y1, &x2, &y2);
            if (x1 > xmax) xmax = x1, maxp = vp;
        }
        if (!maxp) break;
        y = *maxp;
        *maxp = 0;
        ip = voutlet_getit(&y->g_pd);
        
        obj_moveoutletfirst(&x->gl_obj, ip);
    }
    freebytes(vec, noutlets * sizeof(*vec));
516
    if (x->gl_owner && glist_isvisible(x->gl_owner) && glist_isvisible(x) && !x->gl_owner->gl_loading && !x->gl_loading) {
Miller Puckette's avatar
Miller Puckette committed
517
        canvas_fixlinesfor(x->gl_owner, &x->gl_obj);
518
519
		//fprintf(stderr,"good place to fix redrawing of outlets\n");
		//fprintf(stderr,"found it\n");
520
		//glist_redraw(x);
521
522
523
        graph_vis(&x->gl_gobj, x->gl_owner, 0); 
        graph_vis(&x->gl_gobj, x->gl_owner, 1);
	}
Miller Puckette's avatar
Miller Puckette committed
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
587
588
589
590
591
592
593
594
595
596
597
598
}

/* ----------calculating coordinates and controlling appearance --------- */


static void graph_bounds(t_glist *x, t_floatarg x1, t_floatarg y1,
    t_floatarg x2, t_floatarg y2)
{
    x->gl_x1 = x1;
    x->gl_x2 = x2;
    x->gl_y1 = y1;
    x->gl_y2 = y2;
    if (x->gl_x2 == x->gl_x1 ||
        x->gl_y2 == x->gl_y1)
    {
        error("graph: empty bounds rectangle");
        x1 = y1 = 0;
        x2 = y2 = 1;
    }
    glist_redraw(x);
}

static void graph_xticks(t_glist *x,
    t_floatarg point, t_floatarg inc, t_floatarg f)
{
    x->gl_xtick.k_point = point;
    x->gl_xtick.k_inc = inc;
    x->gl_xtick.k_lperb = f;
    glist_redraw(x);
}

static void graph_yticks(t_glist *x,
    t_floatarg point, t_floatarg inc, t_floatarg f)
{
    x->gl_ytick.k_point = point;
    x->gl_ytick.k_inc = inc;
    x->gl_ytick.k_lperb = f;
    glist_redraw(x);
}

static void graph_xlabel(t_glist *x, t_symbol *s, int argc, t_atom *argv)
{
    int i;
    if (argc < 1) error("graph_xlabel: no y value given");
    else
    {
        x->gl_xlabely = atom_getfloat(argv);
        argv++; argc--;
        x->gl_xlabel = (t_symbol **)t_resizebytes(x->gl_xlabel, 
            x->gl_nxlabels * sizeof (t_symbol *), argc * sizeof (t_symbol *));
        x->gl_nxlabels = argc;
        for (i = 0; i < argc; i++) x->gl_xlabel[i] = atom_gensym(&argv[i]);
    }
    glist_redraw(x);
}
    
static void graph_ylabel(t_glist *x, t_symbol *s, int argc, t_atom *argv)
{
    int i;
    if (argc < 1) error("graph_ylabel: no x value given");
    else
    {
        x->gl_ylabelx = atom_getfloat(argv);
        argv++; argc--;
        x->gl_ylabel = (t_symbol **)t_resizebytes(x->gl_ylabel, 
            x->gl_nylabels * sizeof (t_symbol *), argc * sizeof (t_symbol *));
        x->gl_nylabels = argc;
        for (i = 0; i < argc; i++) x->gl_ylabel[i] = atom_gensym(&argv[i]);
    }
    glist_redraw(x);
}

/****** routines to convert pixels to X or Y value and vice versa ******/

    /* convert an x pixel value to an x coordinate value */
599
t_float glist_pixelstox(t_glist *x, t_float xpix)
Miller Puckette's avatar
Miller Puckette committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
{
        /* if we appear as a text box on parent, our range in our
        coordinates (x1, etc.) specifies the coordinate range
        of a one-pixel square at top left of the window. */
    if (!x->gl_isgraph)
        return (x->gl_x1 + (x->gl_x2 - x->gl_x1) * xpix);

        /* if we're a graph when shown on parent, but own our own
        window right now, our range in our coordinates (x1, etc.) is spread
        over the visible window size, given by screenx1, etc. */  
    else if (x->gl_isgraph && x->gl_havewindow)
        return (x->gl_x1 + (x->gl_x2 - x->gl_x1) * 
            (xpix) / (x->gl_screenx2 - x->gl_screenx1));

        /* otherwise, we appear in a graph within a parent glist,
         so get our screen rectangle on parent and transform. */
    else 
    {
        int x1, y1, x2, y2;
        if (!x->gl_owner)
            bug("glist_pixelstox");         
        graph_graphrect(&x->gl_gobj, x->gl_owner, &x1, &y1, &x2, &y2);
        return (x->gl_x1 + (x->gl_x2 - x->gl_x1) * 
            (xpix - x1) / (x2 - x1));
    }
}

627
t_float glist_pixelstoy(t_glist *x, t_float ypix)
Miller Puckette's avatar
Miller Puckette committed
628
629
630
631
632
633
634
635
636
637
{
    if (!x->gl_isgraph)
        return (x->gl_y1 + (x->gl_y2 - x->gl_y1) * ypix);
    else if (x->gl_isgraph && x->gl_havewindow)
        return (x->gl_y1 + (x->gl_y2 - x->gl_y1) * 
                (ypix) / (x->gl_screeny2 - x->gl_screeny1));
    else 
    {
        int x1, y1, x2, y2;
        if (!x->gl_owner)
638
            bug("glist_pixelstoy");
Miller Puckette's avatar
Miller Puckette committed
639
640
641
642
643
644
645
        graph_graphrect(&x->gl_gobj, x->gl_owner, &x1, &y1, &x2, &y2);
        return (x->gl_y1 + (x->gl_y2 - x->gl_y1) * 
            (ypix - y1) / (y2 - y1));
    }
}

    /* convert an x coordinate value to an x pixel location in window */
646
t_float glist_xtopixels(t_glist *x, t_float xval)
Miller Puckette's avatar
Miller Puckette committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
{
    if (!x->gl_isgraph)
        return ((xval - x->gl_x1) / (x->gl_x2 - x->gl_x1));
    else if (x->gl_isgraph && x->gl_havewindow)
        return (x->gl_screenx2 - x->gl_screenx1) * 
            (xval - x->gl_x1) / (x->gl_x2 - x->gl_x1);
    else
    {
        int x1, y1, x2, y2;
        if (!x->gl_owner)
            bug("glist_pixelstox");
        graph_graphrect(&x->gl_gobj, x->gl_owner, &x1, &y1, &x2, &y2);
        return (x1 + (x2 - x1) * (xval - x->gl_x1) / (x->gl_x2 - x->gl_x1));
    }
}

663
t_float glist_ytopixels(t_glist *x, t_float yval)
Miller Puckette's avatar
Miller Puckette committed
664
665
666
667
668
669
670
671
672
673
{
    if (!x->gl_isgraph)
        return ((yval - x->gl_y1) / (x->gl_y2 - x->gl_y1));
    else if (x->gl_isgraph && x->gl_havewindow)
        return (x->gl_screeny2 - x->gl_screeny1) * 
                (yval - x->gl_y1) / (x->gl_y2 - x->gl_y1);
    else 
    {
        int x1, y1, x2, y2;
        if (!x->gl_owner)
674
            bug("glist_pixelstoy");
Miller Puckette's avatar
Miller Puckette committed
675
676
677
678
679
680
681
682
        graph_graphrect(&x->gl_gobj, x->gl_owner, &x1, &y1, &x2, &y2);
        return (y1 + (y2 - y1) * (yval - x->gl_y1) / (x->gl_y2 - x->gl_y1));
    }
}

    /* convert an X screen distance to an X coordinate increment.
      This is terribly inefficient;
      but probably not a big enough CPU hog to warrant optimizing. */
683
t_float glist_dpixtodx(t_glist *x, t_float dxpix)
Miller Puckette's avatar
Miller Puckette committed
684
685
686
687
{ 
    return (dxpix * (glist_pixelstox(x, 1) - glist_pixelstox(x, 0)));
}

688
t_float glist_dpixtody(t_glist *x, t_float dypix)
Miller Puckette's avatar
Miller Puckette committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
{
    return (dypix * (glist_pixelstoy(x, 1) - glist_pixelstoy(x, 0)));
}

    /* get the window location in pixels of a "text" object.  The
    object's x and y positions are in pixels when the glist they're
    in is toplevel.  Otherwise, if it's a new-style graph-on-parent
    (so gl_goprect is set) we use the offset into the framing subrectangle
    as an offset into the parent rectangle.  Finally, it might be an old,
    proportional-style GOP.  In this case we do a coordinate transformation. */
int text_xpix(t_text *x, t_glist *glist)
{
    if (glist->gl_havewindow || !glist->gl_isgraph)
        return (x->te_xpix);
    else if (glist->gl_goprect)
        return (glist_xtopixels(glist, glist->gl_x1) +
            x->te_xpix - glist->gl_xmargin);
    else return (glist_xtopixels(glist, 
            glist->gl_x1 + (glist->gl_x2 - glist->gl_x1) * 
                x->te_xpix / (glist->gl_screenx2 - glist->gl_screenx1)));
}

int text_ypix(t_text *x, t_glist *glist)
{
    if (glist->gl_havewindow || !glist->gl_isgraph)
        return (x->te_ypix);
    else if (glist->gl_goprect)
        return (glist_ytopixels(glist, glist->gl_y1) +
            x->te_ypix - glist->gl_ymargin);
    else return (glist_ytopixels(glist, 
            glist->gl_y1 + (glist->gl_y2 - glist->gl_y1) * 
                x->te_ypix / (glist->gl_screeny2 - glist->gl_screeny1)));
}

    /* redraw all the items in a glist.  We construe this to mean
    redrawing in its own window and on parent, as needed in each case.
    This is too conservative -- for instance, when you draw an "open"
    rectangle on the parent, you shouldn't have to redraw the window!  */
void glist_redraw(t_glist *x)
{  
    if (glist_isvisible(x))
    {
            /* LATER fix the graph_vis() code to handle both cases */
732
        if (glist_istoplevel(x) && x->gl_havewindow)
Miller Puckette's avatar
Miller Puckette committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
        {
            t_gobj *g;
            t_linetraverser t;
            t_outconnect *oc;
            for (g = x->gl_list; g; g = g->g_next)
            {
                gobj_vis(g, x, 0);
                gobj_vis(g, x, 1);
            }
                /* redraw all the lines */
            linetraverser_start(&t, x);
            while (oc = linetraverser_next(&t))
                sys_vgui(".x%lx.c coords l%lx %d %d %d %d\n",
                    glist_getcanvas(x), oc,
                        t.tr_lx1, t.tr_ly1, t.tr_lx2, t.tr_ly2);
            canvas_drawredrect(x, 0);
            if (x->gl_goprect)
            {
751
                //post("draw it");
752
753
				/* update gop rect size on toplevel in case font has
				changed and we are showing text */
754
				/*if (!x->gl_hidetext) {
755
756
757
758
					int x1, y1, x2, y2;
					graph_getrect((t_gobj *)x, x, &x1, &y1, &x2, &y2);
					if (x2-x1 > x->gl_pixwidth) x->gl_pixwidth = x2-x1;
					if (y2-y1 > x->gl_pixheight) x->gl_pixheight = y2-y1;
759
				}*/
Miller Puckette's avatar
Miller Puckette committed
760
761
762
                canvas_drawredrect(x, 1);
            }
        }
763
764
765
766
767
    }
    if (x->gl_owner && glist_isvisible(x->gl_owner))
    {
        graph_vis(&x->gl_gobj, x->gl_owner, 0); 
        graph_vis(&x->gl_gobj, x->gl_owner, 1);
Miller Puckette's avatar
Miller Puckette committed
768
769
770
771
772
773
774
775
776
777
778
779
780
    }
}

/* --------------------------- widget behavior  ------------------- */

int garray_getname(t_garray *x, t_symbol **namep);


    /* Note that some code in here would also be useful for drawing
    graph decorations in toplevels... */
static void graph_vis(t_gobj *gr, t_glist *parent_glist, int vis)
{
    t_glist *x = (t_glist *)gr;
781
	//fprintf(stderr,"graph vis gobj=.x%lx %d\n", (t_int)gr, vis);
782
783
	//fprintf(stderr,"graph_vis gr=.x%lx parent_glist=.x%lx glist_getcanvas(x->gl_owner)=.x%lx vis=%d\n", (t_int)gr, (t_int)parent_glist, (t_int)glist_getcanvas(x->gl_owner), vis);  
	char tag[50];
Miller Puckette's avatar
Miller Puckette committed
784
785
786
787
788
789
790
791
792
    t_gobj *g;
    int x1, y1, x2, y2;
        /* ordinary subpatches: just act like a text object */
    if (!x->gl_isgraph)
    {
        text_widgetbehavior.w_visfn(gr, parent_glist, vis);
        return;
    }

793
794
795
796
797
798
799
800
801
802
803
	// weird exception
	//int exception = 0;
	//t_canvas* tgt = glist_getcanvas(x->gl_owner);
	//if (parent_glist->gl_owner && !parent_glist->gl_mapped &&
	//		parent_glist->gl_owner->gl_mapped) {
	//	tgt = parent_glist;
	//	exception = 1;
	//}
	//fprintf(stderr,"tgt=.x%lx %d\n", (t_int)tgt, exception);

    if (vis && canvas_showtext(x) && gobj_shouldvis(gr, parent_glist))
Miller Puckette's avatar
Miller Puckette committed
804
805
        rtext_draw(glist_findrtext(parent_glist, &x->gl_obj));
    graph_getrect(gr, parent_glist, &x1, &y1, &x2, &y2);
806
	//fprintf(stderr,"%d %d %d %d\n", x1, y1, x2, y2);
Miller Puckette's avatar
Miller Puckette committed
807
808
809
    if (!vis)
        rtext_erase(glist_findrtext(parent_glist, &x->gl_obj));

810
811
812
    sprintf(tag, "%s", rtext_gettag(glist_findrtext(parent_glist, &x->gl_obj)));
    //sprintf(tag, "graph%lx", (t_int)x);
	//fprintf(stderr, "gettag=%s, tag=graph%lx\n", rtext_gettag(glist_findrtext(parent_glist, &x->gl_obj)),(t_int)x);
Miller Puckette's avatar
Miller Puckette committed
813
814
815
816
        /* if we look like a graph but have been moved to a toplevel,
        just show the bounding rectangle */
    if (x->gl_havewindow)
    {
817
818
	    if (vis && gobj_shouldvis(gr, parent_glist))
	    {
819
	        sys_vgui(".x%lx.c create polygon\
820
 %d %d %d %d %d %d %d %d %d %d -tags %sfill -fill $graph_outline\n",
821
822
823
824
825
826
	            glist_getcanvas(x->gl_owner),
				//parent_glist,
	            x1, y1, x1, y2, x2, y2, x2, y1, x1, y1, tag);
	    }
	    else if (gobj_shouldvis(gr, parent_glist))
	    {
827
	        sys_vgui(".x%lx.c delete %sfill\n",
828
829
830
	            glist_getcanvas(x->gl_owner), tag);
				//parent_glist, tag);
	    }
Miller Puckette's avatar
Miller Puckette committed
831
832
833
834
835
836
        return;
    }
        /* otherwise draw (or erase) us as a graph inside another glist. */
    if (vis)
    {
        int i;
837
        t_float f;
Miller Puckette's avatar
Miller Puckette committed
838
839
840
841
842
        t_gobj *g;
        t_symbol *arrayname;
        t_garray *ga;
            /* draw a rectangle around the graph */
        sys_vgui(".x%lx.c create line\
843
            %d %d %d %d %d %d %d %d %d %d -fill $graph_outline -tags %sR\n",
Miller Puckette's avatar
Miller Puckette committed
844
845
846
847
848
849
850
851
852
            glist_getcanvas(x->gl_owner),
            x1, y1, x1, y2, x2, y2, x2, y1, x1, y1, tag);
        
            /* if there's just one "garray" in the graph, write its name
                along the top */
        for (i = (y1 < y2 ? y1 : y2)-1, g = x->gl_list; g; g = g->g_next)
            if (g->g_pd == garray_class &&
                !garray_getname((t_garray *)g, &arrayname))
        {
853
854
            //i -= sys_fontheight(glist_getfont(x));
			i++;
Miller Puckette's avatar
Miller Puckette committed
855
            sys_vgui(".x%lx.c create text %d %d -text {%s} -anchor nw\
856
857
858
859
             -font {{%s} %d %s} -tags %s -fill %s\n",
             (long)glist_getcanvas(x),  x1+2, i, arrayname->s_name, sys_font,
                sys_hostfontsize(glist_getfont(x)), sys_fontweight, tag,
				(glist_isselected(x, gr) ? "$select_color" : "$graph_outline"));
Miller Puckette's avatar
Miller Puckette committed
860
861
862
863
864
865
        }
        
            /* draw ticks on horizontal borders.  If lperb field is
            zero, this is disabled. */
        if (x->gl_xtick.k_lperb)
        {
866
            t_float upix, lpix;
Miller Puckette's avatar
Miller Puckette committed
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
            if (y2 < y1)
                upix = y1, lpix = y2;
            else upix = y2, lpix = y1;
            for (i = 0, f = x->gl_xtick.k_point;
                f < 0.99 * x->gl_x2 + 0.01*x->gl_x1; i++,
                    f += x->gl_xtick.k_inc)
            {
                int tickpix = (i % x->gl_xtick.k_lperb ? 2 : 4);
                sys_vgui(".x%lx.c create line %d %d %d %d -tags %s\n",
                    glist_getcanvas(x->gl_owner),
                    (int)glist_xtopixels(x, f), (int)upix,
                    (int)glist_xtopixels(x, f), (int)upix - tickpix, tag);
                sys_vgui(".x%lx.c create line %d %d %d %d -tags %s\n",
                    glist_getcanvas(x->gl_owner),
                    (int)glist_xtopixels(x, f), (int)lpix,
                    (int)glist_xtopixels(x, f), (int)lpix + tickpix, tag);
            }
            for (i = 1, f = x->gl_xtick.k_point - x->gl_xtick.k_inc;
                f > 0.99 * x->gl_x1 + 0.01*x->gl_x2;
                    i++, f -= x->gl_xtick.k_inc)
            {
                int tickpix = (i % x->gl_xtick.k_lperb ? 2 : 4);
                sys_vgui(".x%lx.c create line %d %d %d %d -tags %s\n",
                    glist_getcanvas(x->gl_owner),
                    (int)glist_xtopixels(x, f), (int)upix,
                    (int)glist_xtopixels(x, f), (int)upix - tickpix, tag);
                sys_vgui(".x%lx.c create line %d %d %d %d -tags %s\n",
                    glist_getcanvas(x->gl_owner),
                    (int)glist_xtopixels(x, f), (int)lpix,
                    (int)glist_xtopixels(x, f), (int)lpix + tickpix, tag);
            }
        }

            /* draw ticks in vertical borders*/
        if (x->gl_ytick.k_lperb)
        {
903
            t_float ubound, lbound;
Miller Puckette's avatar
Miller Puckette committed
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
            if (x->gl_y2 < x->gl_y1)
                ubound = x->gl_y1, lbound = x->gl_y2;
            else ubound = x->gl_y2, lbound = x->gl_y1;
            for (i = 0, f = x->gl_ytick.k_point;
                f < 0.99 * ubound + 0.01 * lbound;
                    i++, f += x->gl_ytick.k_inc)
            {
                int tickpix = (i % x->gl_ytick.k_lperb ? 2 : 4);
                sys_vgui(".x%lx.c create line %d %d %d %d -tags %s\n",
                    glist_getcanvas(x->gl_owner),
                    x1, (int)glist_ytopixels(x, f), 
                    x1 + tickpix, (int)glist_ytopixels(x, f), tag);
                sys_vgui(".x%lx.c create line %d %d %d %d -tags %s\n",
                    glist_getcanvas(x->gl_owner),
                    x2, (int)glist_ytopixels(x, f), 
                    x2 - tickpix, (int)glist_ytopixels(x, f), tag);
            }
            for (i = 1, f = x->gl_ytick.k_point - x->gl_ytick.k_inc;
                f > 0.99 * lbound + 0.01 * ubound;
                    i++, f -= x->gl_ytick.k_inc)
            {
                int tickpix = (i % x->gl_ytick.k_lperb ? 2 : 4);
                sys_vgui(".x%lx.c create line %d %d %d %d -tags %s\n",
                    glist_getcanvas(x->gl_owner),
                    x1, (int)glist_ytopixels(x, f), 
                    x1 + tickpix, (int)glist_ytopixels(x, f), tag);
                sys_vgui(".x%lx.c create line %d %d %d %d -tags %s\n",
                    glist_getcanvas(x->gl_owner),
                    x2, (int)glist_ytopixels(x, f), 
                    x2 - tickpix, (int)glist_ytopixels(x, f), tag);
            }
        }
            /* draw x labels */
        for (i = 0; i < x->gl_nxlabels; i++)
            sys_vgui(".x%lx.c create text\
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
939
        %d %d -text {%s} -font {{%s} %d %s} -tags %s\n",
Miller Puckette's avatar
Miller Puckette committed
940
941
                glist_getcanvas(x),
                (int)glist_xtopixels(x, atof(x->gl_xlabel[i]->s_name)),
Miller Puckette's avatar
Miller Puckette committed
942
943
                (int)glist_ytopixels(x, x->gl_xlabely),
                x->gl_xlabel[i]->s_name, sys_font, 
Miller Puckette's avatar
Miller Puckette committed
944
                     glist_getfont(x), sys_fontweight, tag);
Miller Puckette's avatar
Miller Puckette committed
945
946
947
948

            /* draw y labels */
        for (i = 0; i < x->gl_nylabels; i++)
            sys_vgui(".x%lx.c create text\
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
949
        %d %d -text {%s} -font {{%s} %d %s} -tags %s\n",
Miller Puckette's avatar
Miller Puckette committed
950
951
952
                glist_getcanvas(x),
                (int)glist_xtopixels(x, x->gl_ylabelx),
                (int)glist_ytopixels(x, atof(x->gl_ylabel[i]->s_name)),
Miller Puckette's avatar
Miller Puckette committed
953
954
                x->gl_ylabel[i]->s_name, sys_font,
                glist_getfont(x), sys_fontweight, tag);
Miller Puckette's avatar
Miller Puckette committed
955
956

            /* draw contents of graph as glist */
957
        for (g = x->gl_list; g; g = g->g_next) {
958
959
			gop_redraw = 1;
			//fprintf(stderr,"drawing gop objects\n");
Miller Puckette's avatar
Miller Puckette committed
960
            gobj_vis(g, x, 1);
961
962
			//fprintf(stderr,"done\n");
			gop_redraw = 0;
963
		}
964
		/* reselect it upon redrawing if it was selected before */
965
966
        glist_drawiofor(parent_glist, &x->gl_obj, 1,
            tag, x1, y1, x2, y2);
967
968
		if (glist_isselected(parent_glist, gr))
			gobj_select(gr, parent_glist, 1);
Miller Puckette's avatar
Miller Puckette committed
969
970
971
972
973
    }
    else
    {
        sys_vgui(".x%lx.c delete %s\n",
            glist_getcanvas(x->gl_owner), tag);
974
975
        sys_vgui(".x%lx.c delete %sR\n",
            glist_getcanvas(x->gl_owner), tag);
976
	glist_eraseiofor(parent_glist, &x->gl_obj, tag);
Miller Puckette's avatar
Miller Puckette committed
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        for (g = x->gl_list; g; g = g->g_next)
            gobj_vis(g, x, 0);
    }
}

    /* get the graph's rectangle, not counting extra swelling for controls
    to keep them inside the graph.  This is the "logical" pixel size. */

static void graph_graphrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
    t_glist *x = (t_glist *)z;
    int x1 = text_xpix(&x->gl_obj, glist);
    int y1 = text_ypix(&x->gl_obj, glist);
    int x2, y2;
    x2 = x1 + x->gl_pixwidth;
    y2 = y1 + x->gl_pixheight;

    *xp1 = x1;
    *yp1 = y1;
    *xp2 = x2;
    *yp2 = y2;
}

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
	/* check if the gop size needs to change due to gop's text
	in case hidetext is not enabled */
void graph_checkgop_rect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2) {

	//fprintf(stderr,"graph_checkgop_rect\n");
	t_glist *x = (t_glist *)z;
	int x21, y21, x22, y22;
	text_widgetbehavior.w_getrectfn(z, glist, &x21, &y21, &x22, &y22);
    if (x22 > *xp2)
        *xp2 = x22;
    if (y22 > *yp2) 
        *yp2 = y22;
	int fw = sys_fontwidth(x->gl_font);
	int fh = sys_fontheight(x->gl_font);
	// WARNING: ugly hack trying to replicate rtext_senditup if we have no parent
	// later consider fixing hardwired values
	int tcols = strlen(x->gl_name->s_name) - 3;
	int th = fh + fh * (tcols/60) + 4;
	if (tcols > 60) tcols = 60;
	int tw = fw * tcols + 4;
	if (tw + *xp1 > *xp2)
		*xp2 = tw + *xp1;
	if (th + *yp1 > *yp2)
		*yp2 = th + *yp1;
}

Miller Puckette's avatar
Miller Puckette committed
1028
1029
1030
1031
1032
1033
    /* get the rectangle, enlarged to contain all the "contents" --
    meaning their formal bounds rectangles. */
static void graph_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
    int x1 = 0x7fffffff, y1 = 0x7fffffff, x2 = -0x7fffffff, y2 = -0x7fffffff;
1034
    int tx1 = 0x7fffffff, ty1 = 0x7fffffff, tx2 = -0x7fffffff, ty2 = -0x7fffffff;
Miller Puckette's avatar
Miller Puckette committed
1035
    t_glist *x = (t_glist *)z;
Ivica Bukvic's avatar
Ivica Bukvic committed
1036
	//fprintf(stderr,"graph_getrect %d\n", x->gl_isgraph);
Miller Puckette's avatar
Miller Puckette committed
1037
1038
1039
1040
1041
1042
1043
1044
    if (x->gl_isgraph)
    {
        int hadwindow;
        t_gobj *g;
        t_text *ob;
        int x21, y21, x22, y22;

        graph_graphrect(z, glist, &x1, &y1, &x2, &y2);
1045
		//fprintf(stderr,"%d %d %d %d\n", x1, y1, x2, y2);
1046

Miller Puckette's avatar
Miller Puckette committed
1047
1048
1049
1050
1051
1052
1053
        if (canvas_showtext(x))
        {
            text_widgetbehavior.w_getrectfn(z, glist, &x21, &y21, &x22, &y22);
            if (x22 > x2) 
                x2 = x22;
            if (y22 > y2) 
                y2 = y22;
1054
			//fprintf(stderr,"canvas_showtext %d %d %d %d\n", x1, y1, x2, y2);
Miller Puckette's avatar
Miller Puckette committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
        }
        if (!x->gl_goprect)
        {
            /* expand the rectangle to fit in text objects; this applies only
            to the old (0.37) graph-on-parent behavior. */
            /* lie about whether we have our own window to affect gobj_getrect
            calls below.  */
            hadwindow = x->gl_havewindow;
            x->gl_havewindow = 0;
            for (g = x->gl_list; g; g = g->g_next)
1065
                if (gobj_shouldvis(g, x))
Miller Puckette's avatar
Miller Puckette committed
1066
1067
1068
            {
                    /* don't do this for arrays, just let them hang outside the
                    box. */
1069
                if (pd_class(&g->g_pd) == garray_class || pd_class(&g->g_pd) == scalar_class)
Miller Puckette's avatar
Miller Puckette committed
1070
1071
1072
1073
1074
1075
1076
1077
1078
                    continue;
                gobj_getrect(g, x, &x21, &y21, &x22, &y22);
                if (x22 > x2) 
                    x2 = x22;
                if (y22 > y2) 
                    y2 = y22;
            }
            x->gl_havewindow = hadwindow;
        }
1079
1080
1081
1082
1083

		//fprintf(stderr,"%d %d %d %d\n", x1, y1, x2, y2);

		// check if the text is not hidden and if so use that as the limit of the gop's size
		if (!x->gl_hidetext) {
1084
			graph_checkgop_rect(z, glist, &x1, &y1, &x2, &y2);
1085
1086
		}

1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
		/* fix visibility of edge items for garrays */
		int has_garray = 0;
        for (g = x->gl_list; g; g = g->g_next) {
			if (pd_class(&g->g_pd) == garray_class) {
				has_garray = 1;
			}
		}
		if (has_garray) {
			x1 -= 1;
			y1 -= 2;
			//x2 += 1;
			y2 += 1;
		}
Miller Puckette's avatar
Miller Puckette committed
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
    }
    else text_widgetbehavior.w_getrectfn(z, glist, &x1, &y1, &x2, &y2);
    *xp1 = x1;
    *yp1 = y1;
    *xp2 = x2;
    *yp2 = y2;
}

static void graph_displace(t_gobj *z, t_glist *glist, int dx, int dy)
{
1110
	//fprintf(stderr,"graph_displace %d %d\n", dx, dy);
Miller Puckette's avatar
Miller Puckette committed
1111
1112
1113
1114
1115
1116
1117
    t_glist *x = (t_glist *)z;
    if (!x->gl_isgraph)
        text_widgetbehavior.w_displacefn(z, glist, dx, dy);
    else
    {
        x->gl_obj.te_xpix += dx;
        x->gl_obj.te_ypix += dy;
1118
1119
1120
1121
1122
1123
		/*char tag[80];
		sprintf(tag, "%s", rtext_gettag(glist_findrtext((x->gl_owner ? x->gl_owner: x), &x->gl_obj)));
        sys_vgui(".x%lx.c move %s %d %d\n",
            glist_getcanvas(x->gl_owner), tag, dx, dy);
        sys_vgui(".x%lx.c move %sR %d %d\n",
            glist_getcanvas(x->gl_owner), tag, dx, dy);*/
1124
		if (!do_not_redraw) {
1125
			//fprintf(stderr,"graph_displace redraw\n");
1126
        	glist_redraw(glist_getcanvas(glist));
1127
			//gobj_select(z, glist, 1);
1128
1129
        	canvas_fixlinesfor(glist_getcanvas(glist), &x->gl_obj);
		}
1130
1131
1132
    }
}

1133
extern int old_displace; //from g_editor.c for legacy drawing
1134
1135
1136
1137
1138
1139
1140
1141
1142

static void graph_displace_withtag(t_gobj *z, t_glist *glist, int dx, int dy)
{
	//fprintf(stderr,"graph_displace_withtag %d %d\n", dx, dy);
    t_glist *x = (t_glist *)z;
    if (!x->gl_isgraph)
        text_widgetbehavior.w_displacefnwtag(z, glist, dx, dy);
    else
    {
1143
1144
1145
		// first check for legacy objects that don't offer displacefnwtag and fallback on the old way of doing things
		t_gobj *g;
		for (g = x->gl_list; g; g = g->g_next) {
1146
1147
			//fprintf(stderr,"shouldvis %d %d\n", gobj_shouldvis(g, glist), gobj_shouldvis(g, x));
			if (g && gobj_shouldvis(g, x) && g->g_pd->c_wb->w_displacefnwtag == NULL && pd_class((t_pd *)g) != garray_class) {
1148
				//fprintf(stderr,"old way\n");
1149
				old_displace = 1;
1150
1151
1152
1153
				graph_displace(z, glist, dx, dy);
				return;
			}
		}
1154
		// else we do things the new and more elegant way
1155
		//fprintf(stderr,"new way\n");
1156
		
1157
1158
1159
        x->gl_obj.te_xpix += dx;
        x->gl_obj.te_ypix += dy;
        canvas_fixlinesfor(glist_getcanvas(glist), &x->gl_obj);
Miller Puckette's avatar
Miller Puckette committed
1160
1161
1162
1163
1164
    }
}

static void graph_select(t_gobj *z, t_glist *glist, int state)
{
1165
	//fprintf(stderr,"graph_select .x%lx .x%lx %d...\n", (t_int)z, (t_int)glist, state);
Miller Puckette's avatar
Miller Puckette committed
1166
1167
1168
    t_glist *x = (t_glist *)z;
    if (!x->gl_isgraph)
        text_widgetbehavior.w_selectfn(z, glist, state);
1169
    else //if(glist_istoplevel(glist))
Miller Puckette's avatar
Miller Puckette committed
1170
    {
1171
		//fprintf(stderr,"...yes\n");
1172
		//fprintf(stderr,"%lx %lx %lx\n", glist_getcanvas(glist), glist, x);
Miller Puckette's avatar
Miller Puckette committed
1173
1174
1175
        t_rtext *y = glist_findrtext(glist, &x->gl_obj);
        if (canvas_showtext(x))
            rtext_select(y, state);
1176
1177
1178
1179
1180
1181
		t_glist *canvas;
		if (!glist_istoplevel(glist)) {
			canvas = glist_getcanvas(glist);
		} else {
			canvas = glist;
		}
1182
1183
		if(glist_istoplevel(glist)) {
        	sys_vgui(".x%lx.c itemconfigure %sR -fill %s\n", canvas, 
1184
1185
                 rtext_gettag(y), (state? "$select_color" : "$graph_outline"));
/*
Miller Puckette's avatar
Miller Puckette committed
1186
        sys_vgui(".x%lx.c itemconfigure graph%lx -fill %s\n",
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1187
1188
                 glist_getcanvas(glist), z, 
                 (state? "$select_color" : "$graph_outline"));
1189
*/
1190
        	sys_vgui(".x%lx.c itemconfigure %s -fill %s\n",
1191
                 canvas, rtext_gettag(y), 
1192
                 (state? "$select_color" : "$graph_outline"));
1193
1194
1195
1196

        	sys_vgui(".x%lx.c itemconfigure %sfill -fill %s\n",
                 canvas, rtext_gettag(y), 
                 (state? "$select_color" : "$graph_outline"));
1197
		}
1198

1199
		t_gobj *g;
1200
		//fprintf(stderr,"graph_select\n");
1201
		if (x->gl_list)
1202
1203
1204
			for (g = x->gl_list; g; g = g->g_next) {
				//fprintf(stderr,"shouldvis %d\n",gobj_shouldvis(g, x));
				if ((g && gobj_shouldvis(g, x) && (g->g_pd->c_wb->w_displacefnwtag != NULL) || (g && pd_class((t_pd *)g) == garray_class)))
1205
					gobj_select(g, x, state);
1206
		}
1207
		sys_vgui("pdtk_select_all_gop_widgets .x%lx %s %d\n", canvas, rtext_gettag(glist_findrtext(glist, &x->gl_obj)), state);
Miller Puckette's avatar
Miller Puckette committed
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
    }
}

static void graph_activate(t_gobj *z, t_glist *glist, int state)
{
    t_glist *x = (t_glist *)z;
    if (canvas_showtext(x))
        text_widgetbehavior.w_activatefn(z, glist, state);
}

#if 0
static void graph_delete(t_gobj *z, t_glist *glist)
{
    t_glist *x = (t_glist *)z;
    if (!x->gl_isgraph)
        text_widgetbehavior.w_deletefn(z, glist);
    else
    {
        t_gobj *y;
        while (y = x->gl_list) glist_delete(x, y);
#if 0       /* I think this was just wrong. */
        if (glist_isvisible(x))
            sys_vgui(".x%lx.c delete graph%lx\n", glist_getcanvas(glist), x);
#endif
    }
}
#endif

static void graph_delete(t_gobj *z, t_glist *glist)
{
1238
	//fprintf(stderr,"graph_delete\n");
Miller Puckette's avatar
Miller Puckette committed
1239
1240
1241
    t_glist *x = (t_glist *)z;
    t_gobj *y;
    text_widgetbehavior.w_deletefn(z, glist);
1242
    while (y = x->gl_list) {
Miller Puckette's avatar
Miller Puckette committed
1243
        glist_delete(x, y);
1244
1245
1246
	}
	if (glist_istoplevel(glist) && glist_isvisible(glist))
		sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", glist);
Miller Puckette's avatar
Miller Puckette committed
1247
1248
}

1249
static t_float graph_lastxpix, graph_lastypix;
Miller Puckette's avatar
Miller Puckette committed
1250
1251
1252
1253

static void graph_motion(void *z, t_floatarg dx, t_floatarg dy)
{
    t_glist *x = (t_glist *)z;
1254
    t_float newxpix = graph_lastxpix + dx, newypix = graph_lastypix + dy;
Miller Puckette's avatar
Miller Puckette committed
1255
1256
1257
1258
1259
    t_garray *a = (t_garray *)(x->gl_list);
    int oldx = 0.5 + glist_pixelstox(x, graph_lastxpix);
    int newx = 0.5 + glist_pixelstox(x, newxpix);
    t_word *vec;
    int nelem, i;
1260
1261
    t_float oldy = glist_pixelstoy(x, graph_lastypix);
    t_float newy = glist_pixelstoy(x, newypix);
Miller Puckette's avatar
Miller Puckette committed
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
    graph_lastxpix = newxpix;
    graph_lastypix = newypix;
        /* verify that the array is OK */
    if (!a || pd_class((t_pd *)a) != garray_class)
        return;
    if (!garray_getfloatwords(a, &nelem, &vec))
        return;
    if (oldx < 0) oldx = 0;
    if (oldx >= nelem)
        oldx = nelem - 1;
    if (newx < 0) newx = 0;
    if (newx >= nelem)
        newx = nelem - 1;
    if (oldx < newx - 1)
    {
        for (i = oldx + 1; i <= newx; i++)
            vec[i].w_float = newy + (oldy - newy) *
1279
                ((t_float)(newx - i))/(t_float)(newx - oldx);
Miller Puckette's avatar
Miller Puckette committed
1280
1281
1282
1283
1284
    }
    else if (oldx > newx + 1)
    {
        for (i = oldx - 1; i >= newx; i--)
            vec[i].w_float = newy + (oldy - newy) *
1285
                ((t_float)(newx - i))/(t_float)(newx - oldx);
Miller Puckette's avatar
Miller Puckette committed
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
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
    }
    else vec[newx].w_float = newy;
    garray_redraw(a);
}

static int graph_click(t_gobj *z, struct _glist *glist,
    int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
    t_glist *x = (t_glist *)z;
    t_gobj *y;
    int clickreturned = 0;
    if (!x->gl_isgraph)
        return (text_widgetbehavior.w_clickfn(z, glist,
            xpix, ypix, shift, alt, dbl, doit));
    else if (x->gl_havewindow)
        return (0);
    else
    {
        for (y = x->gl_list; y; y = y->g_next)
        {
            int x1, y1, x2, y2;
                /* check if the object wants to be clicked */
            if (canvas_hitbox(x, y, xpix, ypix, &x1, &y1, &x2, &y2)
                &&  (clickreturned = gobj_click(y, x, xpix, ypix,
                    shift, alt, 0, doit)))
                        break;
        }
        if (!doit)
        {
            if (y)
                canvas_setcursor(glist_getcanvas(x), clickreturned);
            else canvas_setcursor(glist_getcanvas(x), CURSOR_RUNMODE_NOTHING);
        }
        return (clickreturned); 
    }
}

t_widgetbehavior graph_widgetbehavior =
{
    graph_getrect,
    graph_displace,
    graph_select,
    graph_activate,
    graph_delete,
    graph_vis,
    graph_click,
1332
	graph_displace_withtag,
Miller Puckette's avatar
Miller Puckette committed
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
};

    /* find the graph most recently added to this glist;
        if none exists, return 0. */

t_glist *glist_findgraph(t_glist *x)
{
    t_gobj *y = 0, *z;
    for (z = x->gl_list; z; z = z->g_next)
        if (pd_class(&z->g_pd) == canvas_class && ((t_glist *)z)->gl_isgraph)
            y = z;
    return ((t_glist *)y);
}

extern void canvas_menuarray(t_glist *canvas);

void g_graph_setup(void)
{
    class_setwidget(canvas_class, &graph_widgetbehavior);
    class_addmethod(canvas_class, (t_method)graph_bounds, gensym("bounds"),
        A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
    class_addmethod(canvas_class, (t_method)graph_xticks, gensym("xticks"),
        A_FLOAT, A_FLOAT, A_FLOAT, 0);
    class_addmethod(canvas_class, (t_method)graph_xlabel, gensym("xlabel"),
        A_GIMME, 0);
    class_addmethod(canvas_class, (t_method)graph_yticks, gensym("yticks"),
        A_FLOAT, A_FLOAT, A_FLOAT, 0);
    class_addmethod(canvas_class, (t_method)graph_ylabel, gensym("ylabel"),
        A_GIMME, 0);
    class_addmethod(canvas_class, (t_method)graph_array, gensym("array"),
        A_SYMBOL, A_FLOAT, A_SYMBOL, A_DEFFLOAT, A_NULL);
    class_addmethod(canvas_class, (t_method)canvas_menuarray,
        gensym("menuarray"), A_NULL);
    class_addmethod(canvas_class, (t_method)glist_sort,
        gensym("sort"), A_NULL);
}