summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/g_scalar.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/g_scalar.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/g_scalar.c400
1 files changed, 0 insertions, 400 deletions
diff --git a/apps/plugins/pdbox/PDa/src/g_scalar.c b/apps/plugins/pdbox/PDa/src/g_scalar.c
index b3c6d824fc..329003e63e 100644
--- a/apps/plugins/pdbox/PDa/src/g_scalar.c
+++ b/apps/plugins/pdbox/PDa/src/g_scalar.c
@@ -399,404 +399,4 @@ void g_scalar_setup(void)
399 class_setsavefn(scalar_class, scalar_save); 399 class_setsavefn(scalar_class, scalar_save);
400 class_setpropertiesfn(scalar_class, scalar_properties); 400 class_setpropertiesfn(scalar_class, scalar_properties);
401} 401}
402/* Copyright (c) 1997-1999 Miller Puckette.
403* For information on usage and redistribution, and for a DISCLAIMER OF ALL
404* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
405
406/* This file defines the "scalar" object, which is not a text object, just a
407"gobj". Scalars have templates which describe their structures, which
408can contain numbers, sublists, and arrays.
409
410Also, the "tscalar" object, an ordinary text object that owns a single "scalar"
411and draws it on the parent. This is intended as a way that abstractions can
412control their appearances by adding stuff to draw.
413*/
414
415/* IOhannes :
416 * changed the canvas_restore, so that it might accept $args as well (like "pd $0_test")
417 * so you can make multiple & distinguishable templates
418 * 1511:forum::für::umläute:2001
419 * changes marked with IOhannes
420 * added Krzysztof Czajas fix to avoid crashing...
421 */
422
423#include <stdlib.h>
424#include <string.h>
425#include <stdio.h> /* for read/write to files */
426#include "m_pd.h"
427#include "g_canvas.h"
428
429t_class *scalar_class;
430
431void word_init(t_word *wp, t_template *template, t_gpointer *gp)
432{
433 int i, nitems = template->t_n;
434 t_dataslot *datatypes = template->t_vec;
435 for (i = 0; i < nitems; i++, datatypes++, wp++)
436 {
437 int type = datatypes->ds_type;
438 if (type == DT_FLOAT)
439 wp->w_float = 0;
440 else if (type == DT_SYMBOL)
441 wp->w_symbol = &s_symbol;
442 else if (type == DT_ARRAY)
443 {
444 wp->w_array = array_new(datatypes->ds_arraytemplate, gp);
445 }
446 else if (type == DT_LIST)
447 {
448 /* LATER test this and get it to work */
449 wp->w_list = canvas_new(0, 0, 0, 0);
450 }
451 }
452}
453
454void word_restore(t_word *wp, t_template *template,
455 int argc, t_atom *argv)
456{
457 int i, nitems = template->t_n;
458 t_dataslot *datatypes = template->t_vec;
459 for (i = 0; i < nitems; i++, datatypes++, wp++)
460 {
461 int type = datatypes->ds_type;
462 if (type == DT_FLOAT)
463 {
464 float f;
465 if (argc)
466 {
467 f = atom_getfloat(argv);
468 argv++, argc--;
469 }
470 else f = 0;
471 wp->w_float = f;
472 }
473 else if (type == DT_SYMBOL)
474 {
475 t_symbol *s;
476 if (argc)
477 {
478 s = atom_getsymbol(argv);
479 argv++, argc--;
480 }
481 else s = &s_;
482 wp->w_symbol = s;
483 }
484 }
485 if (argc)
486 post("warning: word_restore: extra arguments");
487}
488
489void word_free(t_word *wp, t_template *template)
490{
491 int i;
492 t_dataslot *dt;
493 for (dt = template->t_vec, i = 0; i < template->t_n; i++, dt++)
494 {
495 if (dt->ds_type == DT_ARRAY)
496 array_free(wp[i].w_array);
497 else if (dt->ds_type == DT_LIST)
498 canvas_free(wp[i].w_list);
499 }
500}
501
502 /* make a new scalar and add to the glist. We create a "gp" here which
503 will be used for array items to point back here. This gp doesn't do
504 reference counting or "validation" updates though; the parent won't go away
505 without the contained arrays going away too. The "gp" is copied out
506 by value in the word_init() routine so we can throw our copy away. */
507
508t_scalar *scalar_new(t_glist *owner, t_symbol *templatesym)
509{
510 t_scalar *x;
511 t_template *template;
512 t_gpointer gp;
513 gpointer_init(&gp);
514 template = template_findbyname(templatesym);
515 if (!template)
516 {
517 error("scalar: couldn't find template %s", templatesym->s_name);
518 return (0);
519 }
520 x = (t_scalar *)getbytes(sizeof(t_scalar) +
521 (template->t_n - 1) * sizeof(*x->sc_vec));
522 x->sc_gobj.g_pd = scalar_class;
523 x->sc_template = templatesym;
524 gpointer_setglist(&gp, owner, x);
525 word_init(x->sc_vec, template, &gp);
526 return (x);
527}
528
529 /* Pd method to create a new scalar, add it to a glist, and initialize
530 it from the message arguments. */
531
532int glist_readscalar(t_glist *x, int natoms, t_atom *vec,
533 int *p_nextmsg, int selectit);
534
535void glist_scalar(t_glist *glist,
536 t_symbol *classname, t_int argc, t_atom *argv)
537{
538 t_symbol *templatesym =
539 canvas_makebindsym(atom_getsymbolarg(0, argc, argv));
540 t_binbuf *b;
541 int natoms, nextmsg = 0;
542 t_atom *vec;
543 if (!template_findbyname(templatesym))
544 {
545 pd_error(glist, "%s: no such template",
546 atom_getsymbolarg(0, argc, argv)->s_name);
547 return;
548 }
549
550 b = binbuf_new();
551 binbuf_restore(b, argc, argv);
552 natoms = binbuf_getnatom(b);
553 vec = binbuf_getvec(b);
554
555 glist_readscalar(glist, natoms, vec, &nextmsg, 0);
556 binbuf_free(b);
557}
558
559/* -------------------- widget behavior for scalar ------------ */
560void scalar_getbasexy(t_scalar *x, float *basex, float *basey)
561{
562 t_template *template = template_findbyname(x->sc_template);
563 *basex = template_getfloat(template, gensym("x"), x->sc_vec, 0);
564 *basey = template_getfloat(template, gensym("y"), x->sc_vec, 0);
565}
566
567static void scalar_getrect(t_gobj *z, t_glist *owner,
568 int *xp1, int *yp1, int *xp2, int *yp2)
569{
570 t_scalar *x = (t_scalar *)z;
571 int hit = 0;
572 t_template *template = template_findbyname(x->sc_template);
573 t_canvas *templatecanvas = template_findcanvas(template);
574 int x1 = 0x7fffffff, x2 = -0x7fffffff, y1 = 0x7fffffff, y2 = -0x7fffffff;
575 t_gobj *y;
576 float basex, basey;
577 scalar_getbasexy(x, &basex, &basey);
578 /* if someone deleted the template canvas, we're just a point */
579 if (!templatecanvas)
580 {
581 x1 = x2 = glist_xtopixels(owner, basex);
582 y1 = y2 = glist_ytopixels(owner, basey);
583 }
584 else
585 {
586 int hit = 0;
587 x1 = y1 = 0x7fffffff;
588 x2 = y2 = -0x7fffffff;
589 for (y = templatecanvas->gl_list; y; y = y->g_next)
590 {
591 t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
592 int nx1, ny1, nx2, ny2;
593 if (!wb) continue;
594 (*wb->w_parentgetrectfn)(y, owner,
595 x->sc_vec, template, basex, basey,
596 &nx1, &ny1, &nx2, &ny2);
597 if (hit)
598 {
599 if (nx1 < x1) x1 = nx1;
600 if (ny1 < y1) y1 = ny1;
601 if (nx2 > x2) x2 = nx2;
602 if (ny2 > y2) y2 = ny2;
603 }
604 else x1 = nx1, y1 = ny1, x2 = nx2, y2 = ny2, hit = 1;
605 }
606 if (!hit) x1 = y1 = x2 = y2 = 0;
607 }
608 /* post("scalar x1 %d y1 %d x2 %d y2 %d", x1, y1, x2, y2); */
609 *xp1 = x1;
610 *yp1 = y1;
611 *xp2 = x2;
612 *yp2 = y2;
613}
614
615static void scalar_select(t_gobj *z, t_glist *owner, int state)
616{
617 t_scalar *x = (t_scalar *)z;
618 /* post("scalar_select %d", state); */
619 /* later */
620 if (state)
621 {
622 int x1, y1, x2, y2;
623 scalar_getrect(z, owner, &x1, &y1, &x2, &y2);
624 x1--; x2++; y1--; y2++;
625 sys_vgui(".x%x.c create line %d %d %d %d %d %d %d %d %d %d \
626 -width 0 -fill blue -tags select%x\n",
627 glist_getcanvas(owner), x1, y1, x1, y2, x2, y2, x2, y1, x1, y1,
628 x);
629 }
630 else sys_vgui(".x%x.c delete select%x\n", glist_getcanvas(owner), x);
631}
632
633static void scalar_displace(t_gobj *z, t_glist *glist, int dx, int dy)
634{
635 t_scalar *x = (t_scalar *)z;
636 t_symbol *templatesym = x->sc_template;
637 t_template *template = template_findbyname(templatesym);
638 t_symbol *zz;
639 int xonset, yonset, xtype, ytype, gotx, goty;
640 if (!template)
641 {
642 error("scalar: couldn't find template %s", templatesym->s_name);
643 return;
644 }
645 gotx = template_find_field(template, gensym("x"), &xonset, &xtype, &zz);
646 if (gotx && (xtype != DT_FLOAT))
647 gotx = 0;
648 goty = template_find_field(template, gensym("y"), &yonset, &ytype, &zz);
649 if (goty && (ytype != DT_FLOAT))
650 goty = 0;
651 if (gotx)
652 *(t_float *)(((char *)(x->sc_vec)) + xonset) +=
653 dx * (glist_pixelstox(glist, 1) - glist_pixelstox(glist, 0));
654 if (goty)
655 *(t_float *)(((char *)(x->sc_vec)) + yonset) +=
656 dy * (glist_pixelstoy(glist, 1) - glist_pixelstoy(glist, 0));
657 glist_redrawitem(glist, z);
658 if (glist_isselected(glist, z))
659 {
660 scalar_select(z, glist, 0);
661 scalar_select(z, glist, 1);
662 }
663}
664
665static void scalar_activate(t_gobj *z, t_glist *owner, int state)
666{
667 /* post("scalar_activate %d", state); */
668 /* later */
669}
670
671static void scalar_delete(t_gobj *z, t_glist *glist)
672{
673 /* nothing to do */
674}
675
676static void scalar_vis(t_gobj *z, t_glist *owner, int vis)
677{
678 t_scalar *x = (t_scalar *)z;
679 t_template *template = template_findbyname(x->sc_template);
680 t_canvas *templatecanvas = template_findcanvas(template);
681 t_gobj *y;
682 float basex, basey;
683 scalar_getbasexy(x, &basex, &basey);
684 /* if we don't know how to draw it, make a small rectangle */
685 if (!templatecanvas)
686 {
687 if (vis)
688 {
689 int x1 = glist_xtopixels(owner, basex);
690 int y1 = glist_ytopixels(owner, basey);
691 sys_vgui(".x%x.c create rectangle %d %d %d %d -tags scalar%x\n",
692 glist_getcanvas(owner), x1-1, y1-1, x1+1, y1+1, x);
693 }
694 else sys_vgui(".x%x.c delete scalar%x\n", glist_getcanvas(owner), x);
695 return;
696 }
697
698 for (y = templatecanvas->gl_list; y; y = y->g_next)
699 {
700 t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
701 if (!wb) continue;
702 (*wb->w_parentvisfn)(y, owner, x->sc_vec, template, basex, basey, vis);
703 }
704}
705
706static int scalar_click(t_gobj *z, struct _glist *owner,
707 int xpix, int ypix, int shift, int alt, int dbl, int doit)
708{
709 t_scalar *x = (t_scalar *)z;
710 int hit = 0;
711 t_template *template = template_findbyname(x->sc_template);
712 t_canvas *templatecanvas = template_findcanvas(template);
713 t_gobj *y;
714 float basex, basey;
715 scalar_getbasexy(x, &basex, &basey);
716 for (y = templatecanvas->gl_list; y; y = y->g_next)
717 {
718 t_parentwidgetbehavior *wb = pd_getparentwidget(&y->g_pd);
719 if (!wb) continue;
720 if (hit = (*wb->w_parentclickfn)(y, owner,
721 x, template, basex, basey,
722 xpix, ypix, shift, alt, dbl, doit))
723 return (hit);
724 }
725 return (0);
726}
727
728void canvas_writescalar(t_symbol *templatesym, t_word *w, t_binbuf *b,
729 int amarrayelement);
730
731static void scalar_save(t_gobj *z, t_binbuf *b)
732{
733 t_scalar *x = (t_scalar *)z;
734 t_binbuf *b2 = binbuf_new();
735 t_atom a, *argv;
736 int i, argc;
737 canvas_writescalar(x->sc_template, x->sc_vec, b2, 0);
738 binbuf_addv(b, "ss", &s__X, gensym("scalar"));
739 binbuf_addbinbuf(b, b2);
740 binbuf_addsemi(b);
741 binbuf_free(b2);
742}
743
744static void scalar_properties(t_gobj *z, struct _glist *owner)
745{
746 t_scalar *x = (t_scalar *)z;
747 char *buf, buf2[80];
748 int bufsize;
749 t_binbuf *b;
750 glist_noselect(owner);
751 glist_select(owner, z);
752 b = glist_writetobinbuf(owner, 0);
753 binbuf_gettext(b, &buf, &bufsize);
754 binbuf_free(b);
755 buf = t_resizebytes(buf, bufsize, bufsize+1);
756 buf[bufsize] = 0;
757 sprintf(buf2, "pdtk_data_dialog %%s {");
758 gfxstub_new((t_pd *)owner, x, buf2);
759 sys_gui(buf);
760 sys_gui("}\n");
761 t_freebytes(buf, bufsize+1);
762}
763
764static t_widgetbehavior scalar_widgetbehavior =
765{
766 scalar_getrect,
767 scalar_displace,
768 scalar_select,
769 scalar_activate,
770 scalar_delete,
771 scalar_vis,
772 scalar_click,
773};
774
775static void scalar_free(t_scalar *x)
776{
777 int i;
778 t_dataslot *datatypes, *dt;
779 t_symbol *templatesym = x->sc_template;
780 t_template *template = template_findbyname(templatesym);
781 if (!template)
782 {
783 error("scalar: couldn't find template %s", templatesym->s_name);
784 return;
785 }
786 word_free(x->sc_vec, template);
787 gfxstub_deleteforkey(x);
788 /* the "size" field in the class is zero, so Pd doesn't try to free
789 us automatically (see pd_free()) */
790 freebytes(x, sizeof(t_scalar) + (template->t_n - 1) * sizeof(*x->sc_vec));
791}
792
793/* ----------------- setup function ------------------- */
794 402
795void g_scalar_setup(void)
796{
797 scalar_class = class_new(gensym("scalar"), 0, (t_method)scalar_free, 0,
798 CLASS_GOBJ, 0);
799 class_setwidget(scalar_class, &scalar_widgetbehavior);
800 class_setsavefn(scalar_class, scalar_save);
801 class_setpropertiesfn(scalar_class, scalar_properties);
802}