diff options
Diffstat (limited to 'toxy')
-rw-r--r-- | toxy/Makefile | 2 | ||||
-rw-r--r-- | toxy/Makefile.objects | 22 | ||||
-rw-r--r-- | toxy/Makefile.sources | 5 | ||||
-rw-r--r-- | toxy/build_counter | 3 | ||||
-rw-r--r-- | toxy/tot.c | 530 | ||||
-rw-r--r-- | toxy/tow.c | 26 | ||||
-rw-r--r-- | toxy/toxy-all.exclude | 8 | ||||
-rw-r--r-- | toxy/toxy-shared.include | 19 | ||||
-rw-r--r-- | toxy/toxy-test.exclude | 5 | ||||
-rw-r--r-- | toxy/toxy-vicious.exclude | 3 | ||||
-rw-r--r-- | toxy/widget.c | 1146 | ||||
-rw-r--r-- | toxy/widgettype.c | 300 | ||||
-rw-r--r-- | toxy/widgettype.h | 30 |
13 files changed, 2099 insertions, 0 deletions
diff --git a/toxy/Makefile b/toxy/Makefile new file mode 100644 index 0000000..fc022be --- /dev/null +++ b/toxy/Makefile @@ -0,0 +1,2 @@ +ROOT_DIR = .. +include $(ROOT_DIR)/Makefile.common diff --git a/toxy/Makefile.objects b/toxy/Makefile.objects new file mode 100644 index 0000000..d0e6476 --- /dev/null +++ b/toxy/Makefile.objects @@ -0,0 +1,22 @@ +TOT_OBJECTS = \ +common/loud.o \ +common/grow.o \ +hammer/file.o \ +hammer/gui.o \ +common/props.o \ +toxy/scriptlet.o + +TOW_OBJECTS = \ +common/loud.o \ +unstable/loader.o + +WIDGET_OBJECTS = \ +unstable/forky.o \ +common/loud.o \ +common/grow.o \ +common/dict.o \ +hammer/file.o \ +common/props.o \ +toxy/scriptlet.o + +WIDGET_PRIVATEOBJECTS = widgettype.o diff --git a/toxy/Makefile.sources b/toxy/Makefile.sources new file mode 100644 index 0000000..b0f3646 --- /dev/null +++ b/toxy/Makefile.sources @@ -0,0 +1,5 @@ +TYPES = TOT TOW WIDGET + +TOT_SOURCES = tot.c +TOW_SOURCES = tow.c +WIDGET_SOURCES = widget.c diff --git a/toxy/build_counter b/toxy/build_counter new file mode 100644 index 0000000..5774bf7 --- /dev/null +++ b/toxy/build_counter @@ -0,0 +1,3 @@ +#define TOXY_VERSION "0.1" +#define TOXY_RELEASE "alpha" +#define TOXY_BUILD 1 diff --git a/toxy/tot.c b/toxy/tot.c new file mode 100644 index 0000000..eced2b2 --- /dev/null +++ b/toxy/tot.c @@ -0,0 +1,530 @@ +/* Copyright (c) 2003 krzYszcz and others. + * For information on usage and redistribution, and for a DISCLAIMER OF ALL + * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +/* LATER handle stcriptlet's named arguments */ + +#include <stdio.h> +#include <string.h> +#include "m_pd.h" +#include "g_canvas.h" +#include "common/loud.h" +#include "hammer/file.h" +#include "hammer/gui.h" +#include "common/props.h" +#include "toxy/scriptlet.h" +#include "build_counter" + +//#define TOT_DEBUG + +/* probably much more than needed for canvas messages from gui */ +#define TOTSPY_MAXSIZE 32 + +typedef struct _tot +{ + t_object x_ob; + t_glist *x_glist; /* containing glist */ + t_symbol *x_cvremote; /* null if containing glist is our destination */ + t_symbol *x_cvname; + t_symbol *x_cvpathname; /* see tot_getpathname() */ + t_symbol *x_visedpathname; /* see tot__vised() */ + t_symbol *x_target; + int x_warned; + t_scriptlet *x_persistent; + t_scriptlet *x_transient; + t_outlet *x_out2; + t_outlet *x_out4; + t_symbol *x_defname; /* file name (if given as a creation arg) */ + t_hammerfile *x_filehandle; + t_pd *x_guidetached; + t_pd *x_guisink; + struct _totspy *x_spy; +} t_tot; + +typedef struct _totspy +{ + t_pd ts_pd; + int ts_on; + t_canvas *ts_cv; + t_symbol *ts_target; + t_symbol *ts_qsym; + double ts_lasttime; + t_symbol *ts_selector; + t_atom ts_outbuf[TOTSPY_MAXSIZE]; + t_outlet *ts_out3; +} t_totspy; + +static t_class *tot_class; +static t_class *totspy_class; +static t_class *totsink_class; +static t_class *tot_guiconnect_class = 0; + +static t_symbol *tot_ps_qpush; +static t_symbol *tot_ps_query; + +static t_canvas *tot_getcanvas(t_tot *x, int complain) +{ + t_canvas *cv = 0; + t_glist *glist = + (x->x_cvremote ? + (t_glist *)pd_findbyclass(x->x_cvremote, canvas_class) : x->x_glist); + if (glist) + cv = glist_getcanvas(glist); + else if (complain) + loud_error((t_pd *)x, "bad canvas name '%s'", x->x_cvname->s_name); + if (!x->x_warned && !x->x_cvremote) + { + x->x_warned = 1; + if (!cv) cv = x->x_glist; /* redundant */ + loud_warning((t_pd *)x, "using containing canvas ('%s')", + cv->gl_name->s_name); + } + return (cv); +} + +static t_canvas *tot_cvhook(t_pd *z) +{ + return (tot_getcanvas((t_tot *)z, 1)); +} + +static t_symbol *tot_dogetpathname(t_tot *x, int visedonly, int complain) +{ + t_canvas *cv = tot_getcanvas(x, complain); + if (cv) + { + if (visedonly && !glist_isvisible(cv)) + return (0); + else if (cv == x->x_glist) + /* containing glist is our destination, and we are not in a gop */ + return (x->x_cvpathname); + else + { + char buf[32]; + sprintf(buf, ".x%x.c", (int)cv); + return (gensym(buf)); + } + } + else return (0); +} + +static t_symbol *tot_getpathname(t_tot *x, int complain) +{ + return (tot_dogetpathname(x, 0, complain)); +} + +static t_symbol *tot_getvisedpathname(t_tot *x, int complain) +{ + return (tot_dogetpathname(x, 1, complain)); +} + +static void tot_reset(t_tot *x) +{ + scriptlet_reset(x->x_persistent); +} + +static void tot_prealloc(t_tot *x, t_floatarg f) +{ + int reqsize = (int)f; + scriptlet_prealloc(x->x_persistent, reqsize, 1); + scriptlet_prealloc(x->x_transient, reqsize, 1); /* LATER rethink */ +} + +static void tot_add(t_tot *x, t_symbol *s, int ac, t_atom *av) +{ + scriptlet_add(x->x_persistent, 0, 0, ac, av); +} + +static void tot_addnext(t_tot *x, t_symbol *s, int ac, t_atom *av) +{ + scriptlet_setseparator(x->x_persistent, '\n'); + scriptlet_add(x->x_persistent, 0, 0, ac, av); +} + +static void tot_push(t_tot *x, t_symbol *s, int ac, t_atom *av) +{ + if (scriptlet_evaluate(x->x_persistent, x->x_transient, 1, ac, av, 0)) + { + if (s == tot_ps_qpush) + scriptlet_qpush(x->x_transient); + else + scriptlet_push(x->x_transient); + } +} + +static void tot_tot(t_tot *x, t_symbol *s, int ac, t_atom *av) +{ + if (ac) + { + t_scriptlet *sp = x->x_transient; + scriptlet_reset(sp); + scriptlet_add(sp, 1, 1, ac, av); + if (s == tot_ps_query) + scriptlet_qpush(sp); + else + scriptlet_push(sp); + } +} + +static void tot_dooutput(t_tot *x, t_outlet *op, + t_symbol *s, int ac, t_atom *av) +{ + if (ac == 1) + { + if (av->a_type == A_FLOAT) + outlet_float(op, av->a_w.w_float); + else if (av->a_type == A_SYMBOL) + outlet_symbol(op, av->a_w.w_symbol); + } + else if (ac) + { + if (av->a_type == A_FLOAT) + outlet_list(op, &s_list, ac, av); + else if (av->a_type == A_SYMBOL) + outlet_anything(op, av->a_w.w_symbol, ac - 1, av + 1); + } + else outlet_bang(op); +} + +static void tot__reply(t_tot *x, t_symbol *s, int ac, t_atom *av) +{ + tot_dooutput(x, ((t_object *)x)->ob_outlet, s, ac, av); +} + +static void tot__callback(t_tot *x, t_symbol *s, int ac, t_atom *av) +{ + tot_dooutput(x, x->x_out2, s, ac, av); +} + +/* LATER use properties in widgetbehavior (if gop visibility rules change) */ +static void tot_click(t_tot *x, t_floatarg xpos, t_floatarg ypos, + t_floatarg shift, t_floatarg ctrl, t_floatarg alt) +{ + int nleft; + char *head = scriptlet_getcontents(x->x_persistent, &nleft); + char buf[MAXPDSTRING + 1]; + buf[MAXPDSTRING] = 0; + hammereditor_open(x->x_filehandle, "scriptlet editor"); + while (nleft > 0) + { + if (nleft > MAXPDSTRING) + { + strncpy(buf, head, MAXPDSTRING); + head += MAXPDSTRING; + nleft -= MAXPDSTRING; + } + else + { + strncpy(buf, head, nleft); + buf[nleft] = 0; + nleft = 0; + } + hammereditor_append(x->x_filehandle, buf); + } +} + +/* This is called for all Map (f==1) and all Destroy (f==0) events, + comming from any canvas. If visedpathname is zero, we assume our + canvas does not exist. So we ignore everything, waiting for a Map + event that fits tot_getpathname(). Once we spot it, we set + visedpathname, and ignore everything, waiting for a Destroy event + that fits visedpathname. Then we clear visedpathname, etc... */ +static void tot__vised(t_tot *x, t_symbol *s, t_floatarg f) +{ + int flag = f != 0.; +#ifdef TOT_DEBUG + t_symbol *pn = tot_getpathname(x, 0); + post("tot__vised %s %g (pathname %s) ", s->s_name, f, + (pn ? pn->s_name : "unknown")); +#endif + if (!x->x_visedpathname) + { + if (flag && s == tot_getpathname(x, 0)) + { + x->x_visedpathname = s; + outlet_bang(x->x_out4); + } + } + else if (!flag && s == x->x_visedpathname) + x->x_visedpathname = 0; /* LATER reconsider reporting this */ +} + +#ifdef TOT_DEBUG +static void tot_debug(t_tot *x) +{ + t_symbol *pn = tot_getpathname(x, 0); + int sz; + char *bp; + post("containing glist: %x", x->x_glist); + post("destination: %s", x->x_cvname->s_name); + post("pathname%s %s", (pn ? ":" : ""), (pn ? pn->s_name : "unknown")); + bp = scriptlet_getbuffer(x->x_transient, &sz); + post("transient buffer (size %d):\n\"%s\"", sz, bp); + bp = scriptlet_getbuffer(x->x_persistent, &sz); + post("persistent buffer (size %d):\n\"%s\"", sz, bp); +} +#endif + +static void tot_readhook(t_pd *z, t_symbol *fn, int ac, t_atom *av) +{ + scriptlet_read(((t_tot *)z)->x_persistent, fn); +} + +static void tot_writehook(t_pd *z, t_symbol *fn, int ac, t_atom *av) +{ + scriptlet_write(((t_tot *)z)->x_persistent, fn); +} + +static void tot_read(t_tot *x, t_symbol *s) +{ + if (s && s != &s_) + scriptlet_read(x->x_persistent, s); + else + hammerpanel_open(x->x_filehandle, 0); +} + +static void tot_write(t_tot *x, t_symbol *s) +{ + if (s && s != &s_) + scriptlet_write(x->x_persistent, s); + else + hammerpanel_save(x->x_filehandle, + canvas_getdir(x->x_glist), x->x_defname); +} + +static void tot_detach(t_tot *x) +{ + t_canvas *cv = tot_getcanvas(x, 1); + if (cv && glist_isvisible(cv)) + { + t_pd *gc; + t_symbol *target; + char buf[64]; + sprintf(buf, ".x%x", (int)cv); + target = gensym(buf); + if (!tot_guiconnect_class) + { + gc = (t_pd *)guiconnect_new(0, gensym("tot")); + tot_guiconnect_class = *gc; + typedmess(gc, gensym("signoff"), 0, 0); + } + if (gc = pd_findbyclass(target, tot_guiconnect_class)) + { + x->x_guidetached = gc; + pd_unbind(gc, target); + pd_bind(x->x_guisink, target); + } + } +} + +static void tot_attach(t_tot *x) +{ + t_canvas *cv = tot_getcanvas(x, 1); + if (cv && glist_isvisible(cv) && x->x_guidetached) + { + if (tot_guiconnect_class) + { + t_pd *gc; + t_symbol *target; + char buf[64]; + sprintf(buf, ".x%x", (int)cv); + target = gensym(buf); + if (gc = pd_findbyclass(target, tot_guiconnect_class)) + { + } + else + { /* assuming nobody else detached it in the meantime... */ + pd_unbind(x->x_guisink, target); + pd_bind(x->x_guidetached, target); + x->x_guidetached = 0; + } + } + else bug("tot_attach"); + } +} + +static void tot_capture(t_tot *x, t_symbol *s, t_floatarg f) +{ + t_totspy *ts = x->x_spy; + if ((int)f) + { + t_canvas *cv = tot_getcanvas(x, 1); + ts->ts_qsym = (s == &s_ ? 0 : s); + if (cv != ts->ts_cv) + { + if (ts->ts_target) + { + pd_unbind((t_pd *)ts, ts->ts_target); + ts->ts_cv = 0; + ts->ts_target = 0; + } + if (cv) + { + char buf[64]; + ts->ts_cv = cv; + sprintf(buf, ".x%x", (int)cv); + pd_bind((t_pd *)ts, ts->ts_target = gensym(buf)); + } + } + ts->ts_on = (ts->ts_target != 0); + if (ts->ts_on && ts->ts_qsym) + { + ts->ts_lasttime = clock_getlogicaltime(); + ts->ts_selector = gensym("add"); + SETFLOAT(&ts->ts_outbuf[0], 0); + SETSYMBOL(&ts->ts_outbuf[1], ts->ts_qsym); + SETSYMBOL(&ts->ts_outbuf[2], &s_); + outlet_anything(ts->ts_out3, gensym("clear"), 0, 0); + } + } + else ts->ts_on = 0; +} + +static void totspy_anything(t_totspy *ts, t_symbol *s, int ac, t_atom *av) +{ + if (ts->ts_on) + { + if (ts->ts_qsym) + { + int cnt = ac + 3; + if (cnt < TOTSPY_MAXSIZE) + { + t_atom *ap = ts->ts_outbuf; + ap++->a_w.w_float = (float)clock_gettimesince(ts->ts_lasttime); + ap++; + ap++->a_w.w_symbol = s; + while (ac--) *ap++ = *av++; + outlet_anything(ts->ts_out3, + ts->ts_selector, cnt, ts->ts_outbuf); + ts->ts_lasttime = clock_getlogicaltime(); + } + else loud_warning((t_pd *)ts, + "unexpectedly long message (\"%s...\"), ignored", + s->s_name); + } + else outlet_anything(ts->ts_out3, s, ac, av); + } +} + +static void totsink_anything(t_pd *x, t_symbol *s, int ac, t_atom *av) +{ + /* nop */ +} + +static void tot_free(t_tot *x) +{ + pd_unbind((t_pd *)x, x->x_target); + hammergui_unbindvised((t_pd *)x); + hammerfile_free(x->x_filehandle); + scriptlet_free(x->x_persistent); + scriptlet_free(x->x_transient); + if (x->x_spy->ts_target) + pd_unbind((t_pd *)x->x_spy, x->x_spy->ts_target); + pd_free((t_pd *)x->x_spy); + pd_free(x->x_guisink); +} + +static void *tot_new(t_symbol *s1, t_symbol *s2) +{ + t_tot *x = (t_tot *)pd_new(tot_class); + char buf[64]; + sprintf(buf, "tot%x", (int)x); + pd_bind((t_pd *)x, x->x_target = gensym(buf)); + x->x_transient = + scriptlet_new((t_pd *)x, x->x_target, x->x_target, 0, tot_cvhook); + x->x_persistent = + scriptlet_new((t_pd *)x, x->x_target, x->x_target, 0, tot_cvhook); + x->x_glist = canvas_getcurrent(); + if (s1 && s1 != &s_ && strcmp(s1->s_name, ".")) + { + x->x_cvremote = canvas_makebindsym(x->x_cvname = s1); + x->x_cvpathname = 0; + } + else + { + x->x_warned = (s1 && *s1->s_name == '.'); /* do not warn if explicit */ + x->x_cvremote = 0; + x->x_cvname = x->x_glist->gl_name; + sprintf(buf, ".x%x.c", (int)x->x_glist); + x->x_cvpathname = gensym(buf); + } + outlet_new((t_object *)x, &s_anything); + x->x_out2 = outlet_new((t_object *)x, &s_anything); + x->x_spy = (t_totspy *)pd_new(totspy_class); + x->x_spy->ts_on = 0; + x->x_spy->ts_cv = 0; + x->x_spy->ts_target = 0; + x->x_spy->ts_qsym = 0; + x->x_spy->ts_out3 = outlet_new((t_object *)x, &s_anything); + x->x_out4 = outlet_new((t_object *)x, &s_bang); + if (s2 && s2 != &s_) + { + x->x_defname = s2; + scriptlet_read(x->x_persistent, s2); + } + else x->x_defname = &s_; + x->x_filehandle = hammerfile_new((t_pd *)x, 0, + tot_readhook, tot_writehook, 0); + hammergui_bindvised((t_pd *)x); + x->x_visedpathname = tot_getvisedpathname(x, 0); + x->x_guidetached = 0; + x->x_guisink = pd_new(totsink_class); + return (x); +} + +void tot_setup(void) +{ + post("beware! this is tot %s, %s %s build...", + TOXY_VERSION, loud_ordinal(TOXY_BUILD), TOXY_RELEASE); + tot_ps_qpush = gensym("qpush"); + tot_ps_query = gensym("query"); + tot_class = class_new(gensym("tot"), + (t_newmethod)tot_new, + (t_method)tot_free, + sizeof(t_tot), 0, A_DEFSYM, A_DEFSYM, 0); + class_addmethod(tot_class, (t_method)tot_prealloc, + gensym("prealloc"), A_FLOAT, 0); + class_addmethod(tot_class, (t_method)tot_read, + gensym("read"), A_DEFSYM, 0); + class_addmethod(tot_class, (t_method)tot_write, + gensym("write"), A_DEFSYM, 0); + class_addmethod(tot_class, (t_method)tot_reset, + gensym("reset"), 0); + class_addmethod(tot_class, (t_method)tot_push, + gensym("push"), A_GIMME, 0); + class_addmethod(tot_class, (t_method)tot_push, + gensym("qpush"), A_GIMME, 0); + class_addmethod(tot_class, (t_method)tot_add, + gensym("add"), A_GIMME, 0); + class_addmethod(tot_class, (t_method)tot_addnext, + gensym("addnext"), A_GIMME, 0); + class_addmethod(tot_class, (t_method)tot_tot, + gensym("tot"), A_GIMME, 0); + class_addmethod(tot_class, (t_method)tot_tot, + gensym("query"), A_GIMME, 0); + class_addmethod(tot_class, (t_method)tot_detach, + gensym("detach"), 0); + class_addmethod(tot_class, (t_method)tot_attach, + gensym("attach"), 0); + class_addmethod(tot_class, (t_method)tot_capture, + gensym("capture"), A_FLOAT, A_DEFSYM, 0); + class_addmethod(tot_class, (t_method)tot__reply, + gensym("_rp"), A_GIMME, 0); + class_addmethod(tot_class, (t_method)tot__callback, + gensym("_cb"), A_GIMME, 0); + class_addmethod(tot_class, (t_method)tot_click, + gensym("click"), + A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0); + class_addmethod(tot_class, (t_method)tot__vised, + gensym("_vised"), A_SYMBOL, A_FLOAT, 0); +#ifdef TOT_DEBUG + class_addmethod(tot_class, (t_method)tot_debug, + gensym("debug"), 0); +#endif + hammerfile_setup(tot_class, 0); + totspy_class = class_new(gensym("tot spy"), 0, 0, + sizeof(t_totspy), CLASS_PD, 0); + class_addanything(totspy_class, totspy_anything); + totsink_class = class_new(gensym("tot sink"), 0, 0, + sizeof(t_pd), CLASS_PD, 0); + class_addanything(totsink_class, totsink_anything); +} diff --git a/toxy/tow.c b/toxy/tow.c new file mode 100644 index 0000000..5e4f902 --- /dev/null +++ b/toxy/tow.c @@ -0,0 +1,26 @@ +/* Copyright (c) 2003 krzYszcz and others. + * For information on usage and redistribution, and for a DISCLAIMER OF ALL + * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +/* The tow extern just loads the 'widget' library. + The tow class itself is defined in widget.c. */ + +#include "m_pd.h" +#include "common/loud.h" +#include "unstable/loader.h" + +void tow_setup(void) +{ + int result = LOADER_OK; + if (zgetfn(&pd_objectmaker, gensym("widget"))) + loud_warning(0, "widget is already loaded"); + else + result = unstable_load_lib("", "widget"); + if (result == LOADER_NOFILE) + loud_error(0, "widget library is missing"); + else if (!zgetfn(&pd_objectmaker, gensym("widget"))) + { + loud_error(0, "version mismatch"); + loud_errand(0, "use a more recent Pd release (or recompile toxy)."); + } +} diff --git a/toxy/toxy-all.exclude b/toxy/toxy-all.exclude new file mode 100644 index 0000000..d9e9df1 --- /dev/null +++ b/toxy/toxy-all.exclude @@ -0,0 +1,8 @@ +*~ +*.o +*.gz +*.html +*.out +ref +ref/* +dumpsetups diff --git a/toxy/toxy-shared.include b/toxy/toxy-shared.include new file mode 100644 index 0000000..e754469 --- /dev/null +++ b/toxy/toxy-shared.include @@ -0,0 +1,19 @@ +shared/common/loud.c +shared/common/loud.h +shared/common/grow.c +shared/common/grow.h +shared/common/dict.c +shared/common/dict.h +shared/hammer/file.c +shared/hammer/file.h +shared/hammer/gui.c +shared/hammer/gui.h +shared/unstable/forky.c +shared/unstable/forky.h +shared/unstable/loader.c +shared/unstable/loader.h +shared/unstable/pd_imp.h +shared/common/props.c +shared/common/props.h +shared/toxy/scriptlet.c +shared/toxy/scriptlet.h diff --git a/toxy/toxy-test.exclude b/toxy/toxy-test.exclude new file mode 100644 index 0000000..6b3cc43 --- /dev/null +++ b/toxy/toxy-test.exclude @@ -0,0 +1,5 @@ +*~ +import-result.pd +import-debug.pd +temporary +temporary/* diff --git a/toxy/toxy-vicious.exclude b/toxy/toxy-vicious.exclude new file mode 100644 index 0000000..5e5a82e --- /dev/null +++ b/toxy/toxy-vicious.exclude @@ -0,0 +1,3 @@ +*~ +old +old/* diff --git a/toxy/widget.c b/toxy/widget.c new file mode 100644 index 0000000..aff5e32 --- /dev/null +++ b/toxy/widget.c @@ -0,0 +1,1146 @@ +/* Copyright (c) 2003 krzYszcz and others. + * For information on usage and redistribution, and for a DISCLAIMER OF ALL + * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +/* LATER think about reloading method for .wid files */ +/* FIXME sink-binding */ + +#include <stdio.h> +#include <string.h> +#include "m_pd.h" +#include "g_canvas.h" +#include "common/loud.h" +#include "common/grow.h" +#include "unstable/forky.h" +#include "hammer/file.h" +#include "common/props.h" +#include "toxy/scriptlet.h" +#include "widgettype.h" +#include "build_counter" + +/* our proxy of the text_class (not in the API), LATER do not cheat */ +static t_class *makeshift_class; + +//#define WIDGET_DEBUG +//#define TOW_DEBUG + +enum { WIDGET_NOUPDATE = 0, WIDGET_RECONFIG, WIDGET_REVIS }; + +typedef struct _towentry +{ + struct _tow *te_tow; + struct _towentry *te_next; +} t_towentry; + +typedef struct _widgetentry +{ + struct _widget *we_widget; + struct _widgetentry *we_next; +} t_widgetentry; + +typedef struct _widget +{ + t_object x_ob; + t_glist *x_glist; /* containing glist */ + t_widgettype *x_typedef; + t_symbol *x_type; /* 1st creation arg: our type */ + t_symbol *x_tkclass; /* Tk widget class */ + t_symbol *x_name; /* 2nd creation arg: our name (common tag) */ + t_symbol *x_cbtarget; /* same, mangled (a target, and a tag) */ + t_symbol *x_rptarget; /* same, further mangled */ + t_symbol *x_cvpathname; /* see widget_getcvpathname() */ + t_props *x_options; /* instance options */ + t_props *x_handlers; /* instance handlers */ + t_props *x_arguments; /* instance arguments */ + t_scriptlet *x_iniscript; /* instance initializer */ + t_scriptlet *x_optscript; /* option scriptlet */ + t_scriptlet *x_auxscript; /* auxiliary scriptlet */ + t_scriptlet *x_transient; /* output buffer */ + t_hammerfile *x_filehandle; + int x_width; + int x_height; + t_symbol *x_background; + int x_hasstate; + int x_update; /* see widget_update() */ + int x_selected; + int x_disabled; + t_clock *x_transclock; + t_towentry *x_towlist; +} t_widget; + +typedef struct _tow +{ + t_object x_ob; + t_glist *x_glist; /* containing glist */ + t_symbol *x_cvremote; /* null if containing glist is our destination */ + t_symbol *x_cvname; + t_symbol *x_type; /* 2nd creation arg: widget's type */ + t_symbol *x_name; /* 3rd creation arg: widget's name */ + t_widgettype *x_typedef; + t_widgetentry *x_widgetlist; + struct _tow *x_next; /* next in the global towlist */ +} t_tow; + +static t_class *widget_class; +static t_class *tow_class; + +/* Global towlist, searched in widget_attach(). There is no global widgetlist, + because a destination glist is searched instead in tow_attach(). */ +static t_tow *towlist = 0; + +static char *widget_propsresolver(t_pd *z, int ac, t_atom *av) +{ + t_widget *x = (t_widget *)z; + int len; + scriptlet_reset(x->x_auxscript); + if (scriptlet_add(x->x_auxscript, 1, 0, ac, av)) + return (scriptlet_getcontents(x->x_auxscript, &len)); + else + return (0); +} + +static t_canvas *widget_cvhook(t_pd *z) +{ + return (glist_getcanvas(((t_widget *)z)->x_glist)); +} + +/* LATER move to scriptlet.c, use the scriptlet interface (.^) */ +static t_symbol *widget_getcvpathname(t_widget *x, t_glist *glist) +{ + t_canvas *cv; + if (glist && glist != x->x_glist) + { + bug("widget_getcvpathname"); + x->x_glist = glist; + } + cv = glist_getcanvas(x->x_glist); + if (cv == x->x_glist) + return (x->x_cvpathname); /* we are not in a gop */ + else + { + char buf[32]; + sprintf(buf, ".x%x.c", (int)cv); + return (gensym(buf)); + } +} + +/* LATER use the scriptlet interface (.-) */ +static t_symbol *widget_getmypathname(t_widget *x, t_glist *glist) +{ + char buf[64]; + t_symbol *cvpathname = widget_getcvpathname(x, glist); + sprintf(buf, "%s.%s%x", cvpathname->s_name, x->x_name->s_name, (int)x); + return (gensym(buf)); +} + +static void widget_postatoms(char *msg, int ac, t_atom *av) +{ + startpost(msg); + while (ac--) + { + if (av->a_type == A_FLOAT) + postfloat(av->a_w.w_float); + else if (av->a_type == A_SYMBOL) + poststring(av->a_w.w_symbol->s_name); + av++; + } + endpost(); +} + +static void widget_transtick(t_widget *x) +{ + glist_delete(x->x_glist, (t_gobj *)x); +} + +/* called from widget__failure(), LATER also bind this to F4 or something */ +static void widget_transedit(t_widget *x) +{ + t_text *newt, *oldt = (t_text *)x; + t_binbuf *bb = binbuf_new(); + int nopt, nbnd, narg; + t_atom *opt = props_getall(x->x_options, &nopt); + t_atom *bnd = props_getall(x->x_handlers, &nbnd); + t_atom *arg = props_getall(x->x_arguments, &narg); + binbuf_addv(bb, "sss", gensym("widget"), x->x_type, x->x_name); + if (narg) binbuf_add(bb, narg, arg); + if (nopt) binbuf_add(bb, nopt, opt); + if (nbnd) binbuf_add(bb, nbnd, bnd); + canvas_setcurrent(x->x_glist); + newt = (t_text *)pd_new(makeshift_class); + newt->te_width = 0; + newt->te_type = T_OBJECT; + newt->te_binbuf = bb; + newt->te_xpix = oldt->te_xpix; + newt->te_ypix = oldt->te_ypix; + glist_add(x->x_glist, &newt->te_g); + glist_noselect(x->x_glist); + glist_select(x->x_glist, &newt->te_g); + gobj_activate(&newt->te_g, x->x_glist, 1); + x->x_glist->gl_editor->e_textdirty = 1; /* force evaluation */ + canvas_unsetcurrent(x->x_glist); + canvas_dirty(x->x_glist, 1); + clock_delay(x->x_transclock, 0); /* LATER rethink */ +} + +/* FIXME x_glist field validation against glist parameter (all handlers) */ + +static void widget_getrect(t_gobj *z, t_glist *glist, + int *xp1, int *yp1, int *xp2, int *yp2) +{ + t_widget *x = (t_widget *)z; + float x1, y1, x2, y2; + x1 = text_xpix((t_text *)x, glist); + y1 = text_ypix((t_text *)x, glist); + x2 = x1 + x->x_width; + y2 = y1 + x->x_height; + *xp1 = x1; + *yp1 = y1; + *xp2 = x2; + *yp2 = y2; +} + +static void widget_displace(t_gobj *z, t_glist *glist, int dx, int dy) +{ + t_widget *x = (t_widget *)z; + t_text *t = (t_text *)z; + t->te_xpix += dx; + t->te_ypix += dy; + if (glist_isvisible(glist)) + sys_vgui("%s move %s %d %d\n", widget_getcvpathname(x, glist)->s_name, + x->x_cbtarget->s_name, dx, dy); + canvas_fixlinesfor(glist_getcanvas(glist), t); +} + +static void widget_select(t_gobj *z, t_glist *glist, int state) +{ + t_widget *x = (t_widget *)z; + char *mypathname = widget_getmypathname(x, glist)->s_name; + if (state) + { + sys_vgui("%s config -bg blue %s\n", mypathname, + (x->x_hasstate ? "-state disabled" : "")); + x->x_selected = 1; + } + else + { + if (x->x_disabled) + sys_vgui("%s config -bg %s\n", mypathname, + (x->x_background ? x->x_background->s_name : "gray")); + else + sys_vgui("%s config -bg %s\n", mypathname, + (x->x_background ? x->x_background->s_name : "gray"), + (x->x_hasstate ? "-state normal" : "")); + x->x_selected = 0; + } +} + +static void widget_delete(t_gobj *z, t_glist *glist) +{ + canvas_deletelinesfor(glist, (t_text *)z); +} + +static void widget_pushoptions(t_widget *x) +{ + char *mypathname = widget_getmypathname(x, x->x_glist)->s_name; + if (scriptlet_evaluate(x->x_optscript, x->x_transient, 0, 0, 0, 0)) + { +#ifdef WIDGET_DEBUG + int sz; + char *dp = scriptlet_getcontents(x->x_transient, &sz); + post("vis: \"%s\"", dp); +#endif + sys_vgui("%s config ", mypathname); + scriptlet_push(x->x_transient); + } + else + { + /* LATER if scriptlet not empty: bug("widget_pushoptions"); */ + } +} + +static void widget_pushinits(t_widget *x) +{ + if (masterwidget_evaluate(x->x_transient, 0, 0, 0, x->x_arguments)) + scriptlet_push(x->x_transient); + else + bug("widget_pushinits (master)"); + if (widgettype_isdefined(x->x_typedef)) + { + if (widgettype_evaluate(x->x_typedef, x->x_transient, 0, + 0, 0, x->x_arguments)) + scriptlet_push(x->x_transient); + else + { + /* LATER if scriptlet not empty: bug("widget_pushinits (type)"); */ + } + } + if (scriptlet_evaluate(x->x_iniscript, x->x_transient, 0, + 0, 0, x->x_arguments)) + scriptlet_push(x->x_transient); + else + { + /* LATER if scriptlet not empty: bug("widget_pushinits (instance)"); */ + } +} + +static void widget_vis(t_gobj *z, t_glist *glist, int vis) +{ + t_widget *x = (t_widget *)z; + t_text *t = (t_text *)z; + char *cvpathname = widget_getcvpathname(x, glist)->s_name; + char *mypathname = widget_getmypathname(x, glist)->s_name; + x->x_update = WIDGET_NOUPDATE; + if (vis) + { + float px1 = text_xpix((t_text *)x, glist); + float py1 = text_ypix((t_text *)x, glist); +#ifndef PD_MINOR_VERSION + rtext_new(glist, t, glist->gl_editor->e_rtext, 0); +#endif + sys_vgui("set ::toxy::itempath %s; set ::toxy::itemtarget %s\n\ + set ::toxy::itemfailure [catch {%s %s}]\n\ + if {$::toxy::itemfailure} {pd %s _failure\\;}\n", + mypathname, x->x_rptarget->s_name, + x->x_tkclass->s_name, mypathname, + x->x_rptarget->s_name); + widget_pushoptions(x); + sys_vgui("if {$::toxy::itemfailure == 0}\ + {%s create window %g %g\ + -anchor nw -window %s -tags {toxy%s %s}}\n", + cvpathname, px1, py1, mypathname, + x->x_name->s_name, x->x_cbtarget->s_name); + widget_pushinits(x); + sys_vgui("if {$::toxy::itemfailure == 0}\ + {pd %s _config %s [%s cget -bg]\ + [winfo reqwidth %s] [winfo reqheight %s]\ + [catch {%s config -state normal}]\\;}\n", + x->x_rptarget->s_name, x->x_rptarget->s_name, + mypathname, mypathname, mypathname, mypathname); + sys_gui("unset ::toxy::itempath; unset ::toxy::itemtarget\n"); + } + else + { +#ifndef PD_MINOR_VERSION + t_rtext *rt = glist_findrtext(glist, t); + if (rt) rtext_free(rt); +#endif + sys_vgui("destroy %s\n", mypathname); + } +} + +static void widget_save(t_gobj *z, t_binbuf *bb) +{ + t_widget *x = (t_widget *)z; + t_text *t = (t_text *)x; + int nopt, nbnd, narg; + t_atom *opt = props_getall(x->x_options, &nopt); + t_atom *bnd = props_getall(x->x_handlers, &nbnd); + t_atom *arg = props_getall(x->x_arguments, &narg); + binbuf_addv(bb, "ssiisss", gensym("#X"), gensym("obj"), + (int)t->te_xpix, (int)t->te_ypix, gensym("widget"), + x->x_type, x->x_name); + if (narg) binbuf_add(bb, narg, arg); + if (nopt) binbuf_add(bb, nopt, opt); + if (nbnd) binbuf_add(bb, nbnd, bnd); + binbuf_addsemi(bb); +} + +/* FIXME */ +static void widget_properties(t_gobj *z, t_glist *glist) +{ + t_widget *x = (t_widget *)z; + t_atom *ap; + int ac, nleft; + char *head = scriptlet_getcontents(x->x_optscript, &nleft); + char buf[MAXPDSTRING + 1]; + buf[MAXPDSTRING] = 0; + sprintf(buf, "%s %s", x->x_type->s_name, x->x_name->s_name); + hammereditor_open(x->x_filehandle, buf); + while (nleft > 0) + { + if (nleft > MAXPDSTRING) + { + strncpy(buf, head, MAXPDSTRING); + head += MAXPDSTRING; + nleft -= MAXPDSTRING; + } + else + { + strncpy(buf, head, nleft); + buf[nleft] = 0; + nleft = 0; + } + hammereditor_append(x->x_filehandle, buf); + } + scriptlet_reset(x->x_auxscript); + ap = props_getall(x->x_handlers, &ac); + if (ac) scriptlet_add(x->x_auxscript, 0, 0, ac, ap); + head = scriptlet_getcontents(x->x_auxscript, &nleft); + hammereditor_append(x->x_filehandle, "\n"); + while (nleft > 0) + { + if (nleft > MAXPDSTRING) + { + strncpy(buf, head, MAXPDSTRING); + head += MAXPDSTRING; + nleft -= MAXPDSTRING; + } + else + { + strncpy(buf, head, nleft); + buf[nleft] = 0; + nleft = 0; + } + hammereditor_append(x->x_filehandle, buf); + } +} + +static t_widgetbehavior widget_behavior = +{ + widget_getrect, + widget_displace, + widget_select, + 0, + widget_delete, + widget_vis, + 0, + FORKY_WIDGETPADDING +}; + +static void widget_update(t_widget *x) +{ + t_atom *ap; + int ac; + scriptlet_reset(x->x_optscript); + ap = props_getall(widgettype_getoptions(x->x_typedef), &ac); + if (ac) scriptlet_add(x->x_optscript, 0, 0, ac, ap); + ap = props_getall(x->x_options, &ac); + if (ac) scriptlet_add(x->x_optscript, 0, 0, ac, ap); + if (x->x_update && + glist_isvisible(x->x_glist)) /* FIXME the condition */ + { + if (x->x_update == WIDGET_REVIS) + { + widget_vis((t_gobj *)x, x->x_glist, 0); + widget_vis((t_gobj *)x, x->x_glist, 1); + } + else widget_pushoptions(x); + x->x_update = WIDGET_NOUPDATE; + } + /* LATER cache handlers */ +} + +static t_symbol *widget_addprops(t_widget *x, t_props *op, + t_symbol *s, int ac, t_atom *av) +{ + if (op) + { + t_symbol *empty; + empty = props_add(op, s, ac, av); + if (empty) + loud_error((t_pd *)x, "no value given for %s '%s'", + props_getname(op), empty->s_name); + widget_update(x); + return (empty); + } + else + { + bug("widget_addprops"); + return (0); + } +} + +static t_symbol *widget_addmessage(t_widget *x, t_symbol *s, int ac, t_atom *av) +{ + t_symbol *empty; + if (!(empty = widget_addprops(x, x->x_options, s, ac, av)) && + !(empty = widget_addprops(x, x->x_handlers, s, ac, av))) + empty = widget_addprops(x, x->x_arguments, s, ac, av); + return (empty); +} + +static void widget_anything(t_widget *x, t_symbol *s, int ac, t_atom *av) +{ + if (s && s != &s_) + { + if (*s->s_name == '-' || *s->s_name == '@' || *s->s_name == '#') + { + t_symbol *empty; + x->x_update = WIDGET_RECONFIG; + if (empty = widget_addmessage(x, s, ac, av)) + loud_errand((t_pd *)x, + "(use 'remove %s' if that is what you want).", + empty->s_name); + } + else + { + /* LATER cache this */ + int hlen; + t_atom *hp; + t_symbol *sel; + char buf[MAXPDSTRING]; + buf[0] = '@'; + strcpy(buf + 1, s->s_name); + sel = gensym(buf); + if (((hp = props_getone(x->x_handlers, sel, &hlen)) || + (hp = props_getone(widgettype_gethandlers(x->x_typedef), + sel, &hlen))) + && hlen > 1) + { + scriptlet_reset(x->x_auxscript); + scriptlet_add(x->x_auxscript, 0, 0, hlen - 1, hp + 1); + if (scriptlet_evaluate(x->x_auxscript, x->x_transient, + 1, ac, av, 0)) + scriptlet_push(x->x_transient); + } + else loud_nomethod((t_pd *)x, s); + } + } +} + +/* LATER cache this */ +static void widget_bang(t_widget *x) +{ + int ac; + t_atom *av; + t_symbol *sel = gensym("@bang"); + if ((av = props_getone(x->x_handlers, sel, &ac)) || + (av = props_getone(widgettype_gethandlers(x->x_typedef), sel, &ac))) + { + if (ac > 1) + { + scriptlet_reset(x->x_transient); + scriptlet_add(x->x_transient, 1, 1, ac - 1, av + 1); + scriptlet_push(x->x_transient); + } + } +} + +/* LATER cache this */ +static void widget_float(t_widget *x, t_float f) +{ + int ac; + t_atom *av; + t_symbol *sel = gensym("@float"); + if ((av = props_getone(x->x_handlers, sel, &ac)) || + (av = props_getone(widgettype_gethandlers(x->x_typedef), sel, &ac))) + { + if (ac > 1) + { + t_atom at; + SETFLOAT(&at, f); + scriptlet_reset(x->x_auxscript); + scriptlet_add(x->x_auxscript, 0, 0, ac - 1, av + 1); + if (scriptlet_evaluate(x->x_auxscript, + x->x_transient, 1, 1, &at, 0)) + scriptlet_push(x->x_transient); + } + } +} + +/* LATER cache this */ +static void widget_symbol(t_widget *x, t_symbol *s) +{ + int ac; + t_atom *av; + t_symbol *sel = gensym("@symbol"); + if ((av = props_getone(x->x_handlers, sel, &ac)) || + (av = props_getone(widgettype_gethandlers(x->x_typedef), sel, &ac))) + { + if (ac > 1) + { + t_atom at; + SETSYMBOL(&at, s); + scriptlet_reset(x->x_auxscript); + scriptlet_add(x->x_auxscript, 0, 0, ac - 1, av + 1); + if (scriptlet_evaluate(x->x_auxscript, + x->x_transient, 1, 1, &at, 0)) + scriptlet_push(x->x_transient); + } + } +} + +static void widget_remove(t_widget *x, t_symbol *s) +{ + if (s) + { + t_props *op; + if (*s->s_name == '-') + op = x->x_options; + else if (*s->s_name == '@') + op = x->x_handlers; + else if (*s->s_name == '#') + op = x->x_arguments; + else + op = 0; + if (op && props_remove(op, s)) + { + x->x_update = WIDGET_REVIS; + widget_update(x); + } + else loud_warning((t_pd *)x, "%s %s has not been specified", + props_getname(op), s->s_name); + } +} + +static void widget_ini(t_widget *x, t_symbol *s, int ac, t_atom *av) +{ + if (ac) + { + scriptlet_reset(x->x_iniscript); + scriptlet_add(x->x_iniscript, 0, 0, ac, av); + } +} +static void widget_tot(t_widget *x, t_symbol *s, int ac, t_atom *av) +{ + if (ac) + { + t_scriptlet *sp = x->x_transient; + scriptlet_reset(sp); + scriptlet_add(sp, 1, 1, ac, av); + scriptlet_push(sp); + } +} + +static void widget_refresh(t_widget *x) +{ + x->x_update = WIDGET_REVIS; + widget_update(x); +} + +static void widget__failure(t_widget *x) +{ + /* LATER pass error message from gui, and report here */ + loud_error((t_pd *)x, "creation failure"); + widget_transedit(x); +} + +static void widget__config(t_widget *x, t_symbol *target, t_symbol *bg, + t_floatarg fw, t_floatarg fh, t_floatarg fst) +{ +#ifdef WIDGET_DEBUG + post("config %d \"%s\" %g %g", bg->s_name, fw, fh); +#endif + x->x_width = (int)fw; + x->x_height = (int)fh; + if (bg != &s_) x->x_background = bg; + x->x_hasstate = ((int)fst == 0); + canvas_fixlinesfor(glist_getcanvas(x->x_glist), (t_text *)x); /* FIXME */ +} + +static void widget__callback(t_widget *x, t_symbol *s, int ac, t_atom *av) +{ + if (ac == 1) + { + if (av->a_type == A_FLOAT) + outlet_float(((t_object *)x)->ob_outlet, av->a_w.w_float); + else if (av->a_type == A_SYMBOL) + outlet_symbol(((t_object *)x)->ob_outlet, av->a_w.w_symbol); + } + else if (ac) + { + if (av->a_type == A_FLOAT) + outlet_list(((t_object *)x)->ob_outlet, &s_list, ac, av); + else if (av->a_type == A_SYMBOL) + outlet_anything(((t_object *)x)->ob_outlet, + av->a_w.w_symbol, ac - 1, av + 1); + } + else outlet_bang(((t_object *)x)->ob_outlet); +} + +/* FIXME this is a hack (see also widget_select) */ +/* FIXME why <Leave> is being issued on button press? */ +static void widget__inout(t_widget *x, t_floatarg f) +{ + if (x->x_disabled) + { + if (!x->x_glist->gl_edit) + { + if (!x->x_selected) + { + char *mypathname = widget_getmypathname(x, x->x_glist)->s_name; + if (x->x_hasstate) + sys_vgui("%s config -state normal\n", mypathname); + } + x->x_disabled = 0; + } + } + else if ((int)f && x->x_glist->gl_edit) + { + char *mypathname = widget_getmypathname(x, x->x_glist)->s_name; + if (x->x_hasstate) + sys_vgui("%s config -state disabled\n", mypathname); + x->x_disabled = 1; + } +} + +static void widget__click(t_widget *x, t_floatarg fx, t_floatarg fy, + t_floatarg fb, t_floatarg fm) +{ + t_text *t = (t_text *)x; + t_atom at[4]; + fx += t->te_xpix; + fy += t->te_ypix; + SETFLOAT(&at[0], fx); + SETFLOAT(&at[1], fy); + SETFLOAT(&at[2], fb); + SETFLOAT(&at[3], fm); + typedmess((t_pd *)x->x_glist, gensym("mouse"), 4, at); + widget__inout(x, 1.); +} + +static void widget__motion(t_widget *x, t_floatarg fx, t_floatarg fy) +{ + t_text *t = (t_text *)x; + t_atom at[3]; + fx += t->te_xpix; + fy += t->te_ypix; + SETFLOAT(&at[0], fx); + SETFLOAT(&at[1], fy); + SETFLOAT(&at[2], 0); + typedmess((t_pd *)x->x_glist, gensym("motion"), 3, at); +} + +int widget_iswidget(t_gobj *g, t_symbol *type, t_symbol *name) +{ + if (*(t_pd *)g == widget_class) + { + t_widget *x = (t_widget *)g; + return ((!type || type == x->x_type) && + (!name || name == x->x_name)); + } + else return (0); +} + +#ifdef WIDGET_DEBUG +static void widget_debug(t_widget *x) +{ + t_symbol *pn = widget_getcvpathname(x, 0); + t_symbol *mn = widget_getmypathname(x, 0); + int sz, i, nopt; + t_atom *ap; + char *bp, *key; + post("containing glist: %x", x->x_glist); + post("cv pathname%s %s", (pn ? ":" : ""), (pn ? pn->s_name : "unknown")); + post("my pathname%s %s", (mn ? ":" : ""), (mn ? mn->s_name : "unknown")); + if (ap = props_getall(widgettype_getoptions(x->x_typedef), &nopt)) + widget_postatoms("default options:", nopt, ap); + if (ap = props_getall(x->x_options, &nopt)) + widget_postatoms("instance options:", nopt, ap); + if (ap = props_getall(widgettype_gethandlers(x->x_typedef), &nopt)) + widget_postatoms("default handlers:", nopt, ap); + if (ap = props_getall(x->x_handlers, &nopt)) + widget_postatoms("instance handlers:", nopt, ap); + if (ap = props_getall(widgettype_getarguments(x->x_typedef), &nopt)) + widget_postatoms("default arguments:", nopt, ap); + if (ap = props_getall(x->x_arguments, &nopt)) + widget_postatoms("instance arguments:", nopt, ap); + post("dictionary:"); + bp = props_firstvalue(x->x_arguments, &key); + while (bp) + { + post("\t%s: \"%s\"", key, bp); + bp = props_nextvalue(x->x_arguments, &key); + } + bp = scriptlet_getcontents(x->x_transient, &sz); + post("transient buffer (size %d):\n\"%s\"", sz, bp); + bp = scriptlet_getcontents(x->x_optscript, &sz); + post("option buffer (size %d):\n\"%s\"", sz, bp); + bp = widgettype_getcontents(x->x_typedef, &sz); + post("type initializer (size %d):\n\"%s\"", sz, bp); + bp = scriptlet_getcontents(x->x_iniscript, &sz); + post("instance initializer (size %d):\n\"%s\"", sz, bp); +} +#endif + +static void widget_attach(t_widget *x); +static void widget_detach(t_widget *x); + +static void widget_free(t_widget *x) +{ + pd_unbind((t_pd *)x, x->x_cbtarget); + pd_unbind((t_pd *)x, x->x_rptarget); + props_freeall(x->x_options); + scriptlet_free(x->x_iniscript); + scriptlet_free(x->x_optscript); + scriptlet_free(x->x_auxscript); + scriptlet_free(x->x_transient); + hammerfile_free(x->x_filehandle); + if (x->x_transclock) clock_free(x->x_transclock); + widget_detach(x); +} + +static void *widget_new(t_symbol *s, int ac, t_atom *av) +{ + t_widget *x = (t_widget *)pd_new(widget_class); + char buf[MAXPDSTRING]; + masterwidget_initialize(); + x->x_type = 0; + x->x_name = 0; + if (ac && av->a_type == A_SYMBOL) + { + x->x_type = av->a_w.w_symbol; + ac--; av++; + } + if (ac && av->a_type == A_SYMBOL) + { + x->x_name = av->a_w.w_symbol; + ac--; av++; + } + /* LATER think about anonymous widgets (single arg, or '.') */ + if (!x->x_type || x->x_type == &s_ || + !x->x_name || x->x_name == &s_) + { + loud_error((t_pd *)x, "bad arguments for a widget"); + loud_errand((t_pd *)x, + "expecting \"widget <type> <name> [properties]\""); + return (0); + } + sprintf(buf, "%s%x", x->x_name->s_name, (int)x); + pd_bind((t_pd *)x, x->x_cbtarget = gensym(buf)); + sprintf(buf, "%s%x.rp", x->x_name->s_name, (int)x); + pd_bind((t_pd *)x, x->x_rptarget = gensym(buf)); + + x->x_typedef = widgettype_get(x->x_type); + if (!(x->x_tkclass = widgettype_tkclass(x->x_typedef))) + x->x_tkclass = x->x_type; + + x->x_iniscript = scriptlet_new((t_pd *)x, x->x_rptarget, x->x_cbtarget, + x->x_name, widget_cvhook); + x->x_optscript = scriptlet_new((t_pd *)x, x->x_rptarget, x->x_cbtarget, + x->x_name, widget_cvhook); + x->x_auxscript = scriptlet_new((t_pd *)x, x->x_rptarget, x->x_cbtarget, + x->x_name, widget_cvhook); + x->x_transient = scriptlet_new((t_pd *)x, x->x_rptarget, x->x_cbtarget, + x->x_name, widget_cvhook); + + x->x_options = props_new((t_pd *)x, "option", "-", 0, 0); + x->x_handlers = props_new((t_pd *)x, "handler", "@", x->x_options, 0); + x->x_arguments = props_new((t_pd *)x, "argument", "#", x->x_options, + widget_propsresolver); + + sprintf(buf, ".^.c.%s%x", x->x_name->s_name, (int)x); + x->x_glist = canvas_getcurrent(); + sprintf(buf, ".x%x.c", (int)x->x_glist); + x->x_cvpathname = gensym(buf); + outlet_new((t_object *)x, &s_anything); + /* LATER consider estimating these, based on widget class and options */ + x->x_width = 50; + x->x_height = 50; + props_clone(x->x_arguments, widgettype_getarguments(x->x_typedef)); + widget_addmessage(x, 0, ac, av); + x->x_filehandle = hammerfile_new((t_pd *)x, 0, 0, 0, 0); + x->x_transclock = clock_new(x, (t_method)widget_transtick); + x->x_background = 0; + x->x_hasstate = 0; + x->x_update = WIDGET_NOUPDATE; + x->x_disabled = 0; + widget_attach(x); + return (x); +} + +static t_glist *tow_getglist(t_tow *x, int complain) +{ + t_glist *glist = + (x->x_cvremote ? + (t_glist *)pd_findbyclass(x->x_cvremote, canvas_class) : x->x_glist); + if (!glist && complain) + loud_error((t_pd *)x, "bad canvas name '%s'", x->x_cvname->s_name); + return (glist); +} + +static void tow_bang(t_tow *x) +{ + t_widgetentry *we; + for (we = x->x_widgetlist; we; we = we->we_next) + widget_bang(we->we_widget); +} + +static void tow_float(t_tow *x, t_float f) +{ + t_widgetentry *we; + for (we = x->x_widgetlist; we; we = we->we_next) + widget_float(we->we_widget, f); +} + +static void tow_symbol(t_tow *x, t_symbol *s) +{ + t_widgetentry *we; + for (we = x->x_widgetlist; we; we = we->we_next) + widget_symbol(we->we_widget, s); +} + +static void tow_anything(t_tow *x, t_symbol *s, int ac, t_atom *av) +{ + t_widgetentry *we; + for (we = x->x_widgetlist; we; we = we->we_next) + typedmess((t_pd *)we->we_widget, s, ac, av); +} + +static void tow__callback(t_tow *x, t_symbol *s, int ac, t_atom *av) +{ + if (ac == 1) + { + if (av->a_type == A_FLOAT) + outlet_float(((t_object *)x)->ob_outlet, av->a_w.w_float); + else if (av->a_type == A_SYMBOL) + outlet_symbol(((t_object *)x)->ob_outlet, av->a_w.w_symbol); + } + else if (ac) + { + if (av->a_type == A_FLOAT) + outlet_list(((t_object *)x)->ob_outlet, &s_list, ac, av); + else if (av->a_type == A_SYMBOL) + outlet_anything(((t_object *)x)->ob_outlet, + av->a_w.w_symbol, ac - 1, av + 1); + } + else outlet_bang(((t_object *)x)->ob_outlet); +} + +static void tow_widgetattach(t_tow *x, t_widget *w) +{ + t_towentry *te = getbytes(sizeof(*te)); + t_widgetentry *we = getbytes(sizeof(*we)); + te->te_tow = x; + te->te_next = w->x_towlist; + w->x_towlist = te; + we->we_widget = w; + we->we_next = x->x_widgetlist; + x->x_widgetlist = we; + pd_bind((t_pd *)x, w->x_cbtarget); +#ifdef TOW_DEBUG + post("%s widget '%s' attached", w->x_type->s_name, w->x_cbtarget->s_name); +#endif +} + +static void tow_widgetdetach(t_tow *x, t_widget *w) +{ + t_widgetentry *we1, *we2; + for (we1 = 0, we2 = x->x_widgetlist; we2; we2 = we2->we_next) + { + if (we2->we_widget == w) + { +#ifdef TOW_DEBUG + post("%s widget '%s' detached by widget's destructor", + w->x_type->s_name, w->x_cbtarget->s_name); +#endif + pd_unbind((t_pd *)x, w->x_cbtarget); + if (we1) + we1->we_next = we2->we_next; + else + x->x_widgetlist = we2->we_next; + freebytes(we2, sizeof(*we2)); + return; + } + we1 = we2; + } + bug("tow_widgetdetach"); +} + +static void widget_attach(t_widget *x) +{ + t_tow *t; + for (t = towlist; t; t = t->x_next) + if (x->x_glist == tow_getglist(t, 0) && + t->x_type == x->x_type && t->x_name == x->x_name) + tow_widgetattach(t, x); +} + +static void widget_detach(t_widget *x) +{ + t_towentry *te; + while (te = x->x_towlist) + { + x->x_towlist = te->te_next; + tow_widgetdetach(te->te_tow, x); + freebytes(te, sizeof(*te)); + } +} + +static void tow_attach(t_tow *x) +{ + t_glist *glist = tow_getglist(x, 0); + if (glist) + { + t_gobj *g; + for (g = glist->gl_list; g; g = g->g_next) + { + if (*(t_pd *)g == widget_class) + { + t_widget *w = (t_widget *)g; + if (w->x_type == x->x_type && w->x_name == x->x_name) + tow_widgetattach(x, w); + } + } +#ifdef TOW_DEBUG + if (!x->x_widgetlist) + post("%s widget '%s' not found", + x->x_type->s_name, x->x_name->s_name); +#endif + } +#ifdef TOW_DEBUG + else post("glist '%s' not found", x->x_cvname->s_name); +#endif +} + +static void tow_detach(t_tow *x) +{ + t_widgetentry *we; + while (we = x->x_widgetlist) + { + t_widget *w = we->we_widget; + t_towentry *te1, *te2; + x->x_widgetlist = we->we_next; + pd_unbind((t_pd *)x, w->x_cbtarget); + freebytes(we, sizeof(*we)); + for (te1 = 0, te2 = w->x_towlist; te2; te2 = te2->te_next) + { + if (te2->te_tow == x) + { +#ifdef TOW_DEBUG + post("%s widget '%s' detached by tow's destructor", + w->x_type->s_name, w->x_cbtarget->s_name); +#endif + if (te1) + te1->te_next = te2->te_next; + else + w->x_towlist = te2->te_next; + freebytes(te2, sizeof(*te2)); + break; + } + te1 = te2; + } + if (!te2) bug("tow_detach"); + } +} + +#ifdef TOW_DEBUG +static void tow_debug(t_tow *x) +{ + t_widgetentry *we; + post("attached widgets:"); + for (we = x->x_widgetlist; we; we = we->we_next) + { + t_widget *w = we->we_widget; + t_towentry *te; + int other = 0, found = 0; + startpost("\t%s %s", w->x_type->s_name, w->x_cbtarget->s_name); + for (te = w->x_towlist; te; te = te->te_next) + if (te->te_tow == x) + found++; + else + other++; + post(" (%d other tow%s)", other, (other == 1 ? "" : "s")); + if (found != 1) post("BUG: listed %d times in widget's towlist", found); + } +} +#endif + +static void tow_free(t_tow *x) +{ + t_tow *t1, *t2; +#ifdef TOW_DEBUG + startpost("updating towlist..."); +#endif + for (t1 = 0, t2 = towlist; t2; t2 = t2->x_next) + { + if (t2 == x) + { + if (t1) + t1->x_next = t2->x_next; + else + towlist = t2->x_next; +#ifdef TOW_DEBUG + post("ok"); +#endif + break; + } + t1 = t2; + } + tow_detach(x); +} + +static void *tow_new(t_symbol *s1, t_symbol *s2, t_symbol *s3) +{ + t_tow *x = (t_tow *)pd_new(tow_class); + char buf[64]; + x->x_glist = canvas_getcurrent(); + if (s1 && s1 != &s_ && strcmp(s1->s_name, ".")) + x->x_cvremote = canvas_makebindsym(x->x_cvname = s1); + else + { + x->x_cvremote = 0; + x->x_cvname = x->x_glist->gl_name; + } + x->x_type = s2; + x->x_name = s3; + outlet_new((t_object *)x, &s_anything); + x->x_widgetlist = 0; + x->x_next = towlist; + towlist = x; + tow_attach(x); + return (x); +} + +void widget_setup(void) +{ + post("beware! this is widget %s, %s %s build...", + TOXY_VERSION, loud_ordinal(TOXY_BUILD), TOXY_RELEASE); + widgettype_setup(); + widget_class = class_new(gensym("widget"), + (t_newmethod)widget_new, + (t_method)widget_free, + sizeof(t_widget), 0, A_GIMME, 0); + class_setwidget(widget_class, &widget_behavior); + forky_setsavefn(widget_class, widget_save); + forky_setpropertiesfn(widget_class, widget_properties); + class_addbang(widget_class, widget_bang); + class_addfloat(widget_class, widget_float); + class_addsymbol(widget_class, widget_symbol); + class_addanything(widget_class, widget_anything); + class_addmethod(widget_class, (t_method)widget_remove, + gensym("remove"), A_SYMBOL, 0); + class_addmethod(widget_class, (t_method)widget_ini, + gensym("ini"), A_GIMME, 0); + class_addmethod(widget_class, (t_method)widget_tot, + gensym("tot"), A_GIMME, 0); + class_addmethod(widget_class, (t_method)widget_refresh, + gensym("refresh"), 0); + class_addmethod(widget_class, (t_method)widget__config, + gensym("_config"), + A_SYMBOL, A_SYMBOL, A_FLOAT, A_FLOAT, A_FLOAT, 0); + class_addmethod(widget_class, (t_method)widget__failure, + gensym("_failure"), 0); + class_addmethod(widget_class, (t_method)widget__callback, + gensym("_cb"), A_GIMME, 0); + class_addmethod(widget_class, (t_method)widget__inout, + gensym("_inout"), A_FLOAT, 0); + class_addmethod(widget_class, (t_method)widget__click, + gensym("_click"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0); + class_addmethod(widget_class, (t_method)widget__motion, + gensym("_motion"), A_FLOAT, A_FLOAT, 0); +#ifdef WIDGET_DEBUG + class_addmethod(widget_class, (t_method)widget_debug, + gensym("debug"), 0); +#endif + hammerfile_setup(widget_class, 0); + + makeshift_class = class_new(gensym("text"), 0, 0, + sizeof(t_text), + CLASS_NOINLET | CLASS_PATCHABLE, 0); + + tow_class = class_new(gensym("tow"), + (t_newmethod)tow_new, + (t_method)tow_free, + sizeof(t_tow), 0, A_SYMBOL, A_SYMBOL, A_SYMBOL, 0); + class_addbang(tow_class, tow_bang); + class_addfloat(tow_class, tow_float); + class_addsymbol(tow_class, tow_symbol); + class_addanything(tow_class, tow_anything); + class_addmethod(tow_class, (t_method)tow__callback, + gensym("_cb"), A_GIMME, 0); +#ifdef TOW_DEBUG + class_addmethod(tow_class, (t_method)tow_debug, + gensym("debug"), 0); +#endif +} diff --git a/toxy/widgettype.c b/toxy/widgettype.c new file mode 100644 index 0000000..6f26c27 --- /dev/null +++ b/toxy/widgettype.c @@ -0,0 +1,300 @@ +/* Copyright (c) 2003 krzYszcz and others. + * For information on usage and redistribution, and for a DISCLAIMER OF ALL + * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +#include <stdio.h> +#include <string.h> +#include "m_pd.h" +#include "common/loud.h" +#include "common/dict.h" +#include "common/props.h" +#include "toxy/scriptlet.h" +#include "widgettype.h" + +#define WIDGETTYPE_VERBOSE +//#define WIDGETTYPE_DEBUG + +struct _widgettype +{ + t_pd wt_pd; + t_symbol *wt_typekey; /* this is a typemap symbol */ + t_symbol *wt_tkclass; /* also 'undefined' flag (gensym symbol) */ + t_symbol *wt_tkpackage; /* gensym symbol */ + t_props *wt_options; + t_props *wt_handlers; + t_props *wt_arguments; + t_scriptlet *wt_iniscript; +}; + +struct _masterwidget +{ + t_pd mw_pd; + t_symbol *mw_target; + t_scriptlet *mw_setupscript; + t_dict *mw_typemap; + t_widgettype *mw_defaulttype; /* contains master iniscript */ + t_widgettype *mw_parsedtype; /* the type currently parsed, if loading */ + t_binbuf *mw_bb; /* auxiliary, LATER remove */ +}; + +static t_class *widgettype_class; +static t_class *masterwidget_class; + +static t_masterwidget *masterwidget = 0; + +static t_canvas *widgettype_cvhook(t_pd *z) +{ + return (0); +} + +static void widgettype_map(t_widgettype *wt, char *cls, char *pkg) +{ + wt->wt_tkclass = (cls ? gensym(cls) : 0); + wt->wt_tkpackage = (pkg ? gensym(pkg) : 0); +} + +static t_widgettype *widgettype_new(t_masterwidget *mw, + char *typ, char *cls, char *pkg) +{ + t_widgettype *wt = (t_widgettype *)pd_new(widgettype_class); + wt->wt_typekey = dict_key(mw->mw_typemap, typ); + widgettype_map(wt, cls, pkg); + wt->wt_options = props_new(0, "option", "-", 0, 0); + wt->wt_handlers = props_new(0, "handler", "@", wt->wt_options, 0); + wt->wt_arguments = props_new(0, "argument", "#", wt->wt_options, 0); + wt->wt_iniscript = scriptlet_new((t_pd *)wt, mw->mw_target, mw->mw_target, + 0, widgettype_cvhook); + dict_bind(mw->mw_typemap, (t_pd *)wt, wt->wt_typekey); + return (wt); +} + +static t_canvas *masterwidget_cvhook(t_pd *z) +{ + return (0); +} + +static t_scriptlet *masterwidget_cmnthook(t_pd *z, char *rc, + char sel, char *buf) +{ + t_masterwidget *mw = masterwidget; + if (!*buf) + return (0); + if (sel == '>') + { + t_symbol *typekey; + t_widgettype *typeval; + char *cls = scriptlet_nextword(buf); + char *pkg = (cls ? scriptlet_nextword(cls) : 0); + mw->mw_parsedtype = 0; + if (!cls) + cls = buf; + typekey = dict_key(mw->mw_typemap, buf); + typeval = (t_widgettype *)dict_value(mw->mw_typemap, typekey); + if (z == (t_pd *)mw) + { /* default.wid */ + if (typeval) + { + /* LATER rethink */ + loud_warning((t_pd *)mw, "redefinition of '%s'\ + in \"%s.wid\" file, ignored", buf, rc); + return (0); + } + } + else + { /* <type>.wid */ + if (z != (t_pd *)typeval) + { + loud_warning((t_pd *)mw, "alien definition of '%s'\ + in \"%s.wid\" file, ignored", buf, rc); + return (0); + } + } + if (pkg) + /* carve out a single word as pkg, LATER rethink */ + scriptlet_nextword(pkg); + if (typeval) + widgettype_map(typeval, cls, pkg); + else + typeval = widgettype_new(mw, buf, cls, pkg); + mw->mw_parsedtype = typeval; +#ifdef WIDGETTYPE_DEBUG + post("adding widget type '%s'", typeval->wt_typekey->s_name); +#endif + return (typeval->wt_iniscript); + } + else if (sel == '.') + { + if (mw->mw_parsedtype + && (*buf == '-' || *buf == '@' || *buf == '#')) + { + t_symbol *empty; + int ac; + /* LATER get rid of the binbuf thing */ + binbuf_text(mw->mw_bb, buf, strlen(buf)); + if (ac = binbuf_getnatom(mw->mw_bb)) + { + t_atom *av = binbuf_getvec(mw->mw_bb); + t_props *pp; + if (!(empty = props_add(pp = mw->mw_parsedtype->wt_options, + 0, ac, av)) && + !(empty = props_add(pp = mw->mw_parsedtype->wt_handlers, + 0, ac, av))) + empty = props_add(pp = mw->mw_parsedtype->wt_arguments, + 0, ac, av); + if (empty) + loud_warning((t_pd *)mw, + "no value given for %s '%s'\ + of a widget type '%s' in \"%s.wid\" file", + props_getname(pp), empty->s_name, + mw->mw_parsedtype->wt_typekey->s_name, rc); + } + } + } + return (0); +} + +t_widgettype *widgettype_get(t_symbol *s) +{ + t_widgettype *wt; + /* default.wid defs are NOT overridden by <type>.wid -- + feature stability comes first, LATER rethink */ + if (wt = (t_widgettype *)dict_value(masterwidget->mw_typemap, + dict_key(masterwidget->mw_typemap, + s->s_name))) + masterwidget->mw_parsedtype = 0; + else + { + /* first instance of a type not defined in default.wid */ + wt = widgettype_new(masterwidget, s->s_name, 0, 0); + masterwidget->mw_parsedtype = wt; + } + if (masterwidget->mw_parsedtype) + { + if (scriptlet_rcload(wt->wt_iniscript, s->s_name, ".wid", + masterwidget_cmnthook) == SCRIPTLET_OK) + { +#ifdef WIDGETTYPE_VERBOSE + post("using %s's initializer", s->s_name); +#endif + } + } + return (wt); +} + +int widgettype_isdefined(t_widgettype *wt) +{ + return (wt->wt_tkclass != 0); +} + +t_symbol *widgettype_tkclass(t_widgettype *wt) +{ + return (wt->wt_tkclass); +} + +t_props *widgettype_getoptions(t_widgettype *wt) +{ + return (wt->wt_options); +} + +t_props *widgettype_gethandlers(t_widgettype *wt) +{ + return (wt->wt_handlers); +} + +t_props *widgettype_getarguments(t_widgettype *wt) +{ + return (wt->wt_arguments); +} + +char *widgettype_getcontents(t_widgettype *wt, int *szp) +{ + return (scriptlet_getcontents(wt->wt_iniscript, szp)); +} + +int widgettype_evaluate(t_widgettype *wt, t_scriptlet *outsp, + int visedonly, int ac, t_atom *av, t_props *argprops) +{ + return (scriptlet_evaluate(wt->wt_iniscript, outsp, + visedonly, ac, av, argprops)); +} + +int masterwidget_evaluate(t_scriptlet *outsp, int visedonly, + int ac, t_atom *av, t_props *argprops) +{ + return (scriptlet_evaluate(masterwidget->mw_defaulttype->wt_iniscript, + outsp, visedonly, ac, av, argprops)); +} + +void widgettype_setup(void) +{ + static int done = 0; + if (!done) + { + widgettype_class = class_new(gensym("widget type"), 0, 0, + sizeof(t_widgettype), CLASS_PD, 0); + masterwidget_class = class_new(gensym("Widget"), 0, 0, + sizeof(t_masterwidget), CLASS_PD, 0); + done = 1; + } +} + +void masterwidget_initialize(void) +{ + t_scriptlet *sp; + t_symbol *typekey; + t_widgettype *typeval; + char buf[MAXPDSTRING]; + if (masterwidget) + return; + masterwidget = (t_masterwidget *)pd_new(masterwidget_class); + sprintf(buf, "mw%x", (int)masterwidget); + /* never unbound, LATER rethink */ + pd_bind((t_pd *)masterwidget, masterwidget->mw_target = gensym(buf)); + + masterwidget->mw_typemap = dict_new(0); + + sp = masterwidget->mw_setupscript = + scriptlet_new((t_pd *)masterwidget, masterwidget->mw_target, + masterwidget->mw_target, 0, 0); + masterwidget->mw_parsedtype = 0; + masterwidget->mw_bb = binbuf_new(); + + if (scriptlet_rcload(sp, "default", ".wid", + masterwidget_cmnthook) == SCRIPTLET_OK) + { +#ifdef WIDGETTYPE_VERBOSE + post("using file 'default.wid'"); +#endif + } + else + { + loud_warning((t_pd *)masterwidget, "missing file 'default.wid'"); + + /* no setup scriptlet, LATER use built-in default */ +#if 0 + scriptlet_reset(sp); + scriptlet_addstring(sp, ... +#endif + } + typekey = dict_key(masterwidget->mw_typemap, "default"); + if (typeval = (t_widgettype *)dict_value(masterwidget->mw_typemap, typekey)) + masterwidget->mw_defaulttype = typeval; + else + { + /* no master initializer, LATER use built-in default */ + masterwidget->mw_defaulttype = + widgettype_new(masterwidget, "default", 0, 0); + sp = masterwidget->mw_defaulttype->wt_iniscript; +#if 0 + scriptlet_reset(sp); + scriptlet_addstring(sp, ... +#endif + } + sp = scriptlet_new((t_pd *)masterwidget, + masterwidget->mw_target, masterwidget->mw_target, 0, 0); + if (scriptlet_evaluate(masterwidget->mw_setupscript, sp, 0, 0, 0, 0)) + scriptlet_push(sp); + else + bug("masterwidget_initialize"); + scriptlet_free(sp); +} diff --git a/toxy/widgettype.h b/toxy/widgettype.h new file mode 100644 index 0000000..55c8e9d --- /dev/null +++ b/toxy/widgettype.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2003 krzYszcz and others. + * For information on usage and redistribution, and for a DISCLAIMER OF ALL + * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +#ifndef __WIDGETTYPE_H__ +#define __WIDGETTYPE_H__ + +EXTERN_STRUCT _widgettype; +#define t_widgettype struct _widgettype + +EXTERN_STRUCT _masterwidget; +#define t_masterwidget struct _masterwidget + +t_widgettype *widgettype_get(t_symbol *s); +int widgettype_isdefined(t_widgettype *wt); +t_symbol *widgettype_tkclass(t_widgettype *wt); +t_props *widgettype_getoptions(t_widgettype *wt); +t_props *widgettype_gethandlers(t_widgettype *wt); +t_props *widgettype_getarguments(t_widgettype *wt); +char *widgettype_propname(t_symbol *s); +char *widgettype_getcontents(t_widgettype *wt, int *szp); +int widgettype_evaluate(t_widgettype *wt, t_scriptlet *outsp, + int visedonly, int ac, t_atom *av, t_props *argprops); +int masterwidget_evaluate(t_scriptlet *outsp, int visedonly, + int ac, t_atom *av, t_props *argprops); +void masterwidget_initialize(void); + +void widgettype_setup(void); + +#endif |